code:
from functools import wraps

class MetaA(type):
    def __new__(meta, name, bases, attr):
        method1 = attr['method1']
        if not getattr(method, '__isabstractmethod__'):
            @wraps(method1)
            def wrapper(self, *args, **kw):
                res = method1(self, *args, **kw)
                self.method2()
                return res
            attr['method1'] = wrapper
        return super(MetaA, meta).__new__(meta, name, bases, attr)

class A(object):
     __metaclass__ = MetaA

     @abc.abstractmethod
     def method1(self):
         raise Exception("Unimplemented method")

     def method2(self):
         print "method1 finished"

rawDescription: Specifying an exit function for an abstract function


code:
#Build default data list
defdata = '''
Data1 = 1
Data2 = 2
Data3 = 3
Data4 = 4
Data5 = 5
Data6 = 6
'''.splitlines()[1:]

#Build output data list
outdata = '''
Data1 = 1
Data2 = 2
Data3 = 8
Data4 = 7
'''.splitlines()[1:]

outdict = dict(line.split(' = ') for line in outdata)

for line in defdata:
    key, val = line.split(' = ')
    if key in outdict:
        outval = outdict[key]
        if outval != val:
            print '%s = %s' % (key, outval)
    else:
        print line

rawDescription: Compare two text files to find differences and output them to a new text file


code:
import numpy as np

def test(input):
    return input

def memoize(function):
    cache = {}
    def decorated_function(*args):
        if args in cache:
            print 'cached'
            return cache[args]
        else:
            print 'not cached'
            val = function(*args)
            cache[args] = val
            return val
    return decorated_function

test = memoize(test)
print test(9)
print test(9)
test = np.vectorize(test)
print test(9)
print test(10)
print test(10)

rawDescription: Memoize a NumPy vectorized function

description: Memoize a NumPy vectorized function


code:
def func():
    print func.__name__

func.__name__
func()

rawDescription: getting object name and function name

description: Getting object name and function name


code:
groups={}
for e in flat:
    groups.setdefault(len(e), []).append(e)

print groups
# {5: ['Shoes', 'pants', 'shirt'], 3: ['tie', 'hat'], 6: ['jacket']}

rawDescription: Nest a flat list based on an arbitrary criterion


code:
import lxml
import lxml.etree

data = '''  ... html_as_previouse  ... '''

et = lxml.etree.fromstring(data)

fields = et.xpath('//field')

for x in fields:
    print x.text

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

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


code:
import sys
print sys.maxunicode

rawDescription: How to find out if Python is compiled with UCS-2 or UCS-4?

description: Find out if Python is compiled with UCS-2 or UCS-4


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

def print_add(a, b):
    print "Your answer is: ", str(add(a, b))    ## print add's return value

answer = add(10, 15)
print answer
## Will print 25

print_add(10, 20)
## Will print """Your answer is 30"""

rawDescription: How to tell if a function will be returning a value to a variable?

description: Tell if a function will be returning a value to a variable


code:
filesize = 10        # number of entries per file
filenum = 0
filehandle = ""
OutString = ""

print record_dict
for itemcount,keys in enumerate(keys_in_dict):
    # check if it is time to open a new file,
    # whenever itemcount/filesize has no remainder
    OutString += ">{0}\n{1}\n".format(keys,record_dict[keys])
    if not itemcount % filesize:
        if filehandle:
            filehandle.write(OutString)
            filehandle.close()
            OutString =""
        filenum+=1
        PathToFile = "D:/Research/Transcriptome_sequences/input{0}.fasta".format(filenum)
        filehandle = open(PathToFile,'w')

filehandle.write(OutString)
filehandle.close()

rawDescription: How to write multiple files from a dictionary in python

description: Write multiple files from a dictionary in python


code:
#length is 40k
df = pd.concat([df]*10000).reset_index(drop=True)
df1 = df.copy()
df2 = df.copy()

def a(df):
    dates = (u'2015-06-23',u'2015-06-29')
    df = df.set_index('received_date')
    df.index = pd.DatetimeIndex(df.index)
    return df[dates[0]:dates[1]]

def b(df):
    dates = (u'2015-06-23',u'2015-06-29')
    dates = pd.to_datetime(dates).date
    df['received_date'] = pd.to_datetime(df['received_date']).dt.date
    df = df[(df['received_date'] > dates[0]) & (df['received_date'] < dates[1])]
    return df

def c(df):
    dates = (u'2015-06-23',u'2015-06-29')
    df['received_date'] = pd.to_datetime(df['received_date'])
    df = df.set_index('received_date')
    return df[dates[0]:dates[1]]

print a(df)
print b(df1)
print c(df2)

rawDescription: how to compare unicode date u'2006-07-23' format and 25-06-15 08:42:43.830000000 PM using python pandas?

description: Compare unicode date u'2006-07-23' format and 25-06-15 08:42:43.830000000 PM using python pandas


code:
print prop.fget
# <function myprop at 0x7fe1b595d5f0>

print prop.fset
# <function myprop at 0x7fe1b595d668>

print prop.fdel  # We never defined a deleter method
# None

a = A()
print prop.fget(a)
#  Hello

rawDescription: Set and Get @property method in Python by string variable


code:
import re

data = ":::::::::::BLAH:::::::::, ::::::::MORE:::::::"

def replace(matchobj):
  # this seems to work:
  tag = matchobj.group(0)
  findkey = matchobj.group(1)

  print findkey

  return '********************  NONSENSE  ********************'

data = re.sub(r':::(?P<inner>[A-Z,a-z]+):::', replace, data)

print data

rawDescription: in a Python regex matchobj, how to do a substring match, and assign the match to a variable?

description: In a Python regex matchobj, how to do a substring match, and assign the match to a variable


code:
def compareTest():
  ports1 = [ x for x in portfolios() ]
  ports2 = [ x for x in portfolios2() ]
  print "ports1 length:", len(ports1)
  print "ports2 length:", len(ports2)
  for x in ports1:
    if x not in ports2: print "not in ports2:", x
  for x in ports2:
    if x not in ports1: print "not in ports1:", x

rawDescription: Python - most efficient way to generate combinations of large sets subject to criteria?


code:
import requests
from lxml import html

response = requests.get("http://www.flightclub.com/air-jordan-1-retro-high-og-unc-white-dk-powder-blue-012304")
tree = html.fromstring(response.content)

style_id = tree.xpath('//ul[@class="mb-padding product-attribute-list"]/li[@class="attribute-list-item"][1]/text()[2]')[0].replace(',','').strip()
print style_id

rawDescription: How to extract the text between two spans with lxml (or BeautifulSoup)?

description: Extract the text between two spans with lxml (or BeautifulSoup)


code:
from pyparsing import *

# use an Empty to define a null token that just records its
# own location in the input string
locnMarker = Empty().leaveWhitespace().setParseAction(lambda s,l,t: l)

# define a example expression and save the start and end locations
markedInteger = locnMarker + Word(nums)('value') + locnMarker

# assign named results for the start and end values,
# and pop them out of the actual list of tokens
def markStartAndEnd(s,l,t):
    t['start'],t['end'] = t.pop(0),t.pop(-1)
markedInteger.setParseAction(markStartAndEnd)

# find all integers in the source string, and print
# their value, start, and end locations; use dump()
# to show the parsed tokens and any named results
source = "ljsdlj2342 sadlsfj132 sldfj12321 sldkjfsldj 1232"
for integer in markedInteger.searchString(source):
    print integer.dump()

rawDescription: Pyparsing Token Source Range

description: Pyparsing Token Source Range


code:
class A:
 def __init__(self):
   self.v = B()

class B:
 def __init__(self):
  self.list = [1,2,3]
 def function(self):
  self.list[2] = 1

x = A()
g = [x]
print g[0].v.function()
print g[0].v.list

rawDescription: object inside another object in python

description: Object inside another object in python


code:
try:
    __import__(mod_name)
    print 'Such a module exists'
except ImportError:
    print 'No such module'

rawDescription: Is there a better way to determine something is a module in python


code:
class A(int):
    def sqr(self):
        return A(self*self)

sqr = A.sqr
a = A(5)
print sqr(a) # Prints 25

rawDescription: Referencing a class' method, not an instance's


code:
def func_dec(foo, is_method=False):
    def wrapper(fun):
        @wraps(fun)
        def bar(*args, **kwds):
            val = args[1] if is_method else args[0]
            print foo, val
            return fun(*args, **kwds)
        return bar
    return wrapper

rawDescription: python decorator for class OR function

description: Python decorator for class OR function


code:
import argparse
from operator import itemgetter

class OptionListGroup(object):
  class GroupAction(object):
    def __init__(self, left, right):
      self.help = right
      self.option_strings = [left]
      self.nargs = 0

  def __init__(self, lists):
    self.description = None
    self.title = "referenced sets"
    self._group_actions = [self.GroupAction(name, self.format_list(lst))
                           for name, lst in sorted(lists)]

  def format_list(self, lst):
    return '{%s}' % ', '.join(map(str, lst))

class MyArgParser(argparse.ArgumentParser):
  def __init__(self, *args, **kwargs):
    self._option_lists = {}
    super(MyArgParser, self).__init__(*args, **kwargs)

  def parse_args(self, *args, **kw):
    self._action_groups.append(OptionListGroup(self._option_lists.values()))
    return super(MyArgParser, self).parse_args(*args, **kw)

  def add_option_list(self, name, lst):
    if name in map(itemgetter(0), self._option_lists.values()):
      raise ValueError, "Name already existing"
    self._option_lists[id(lst)] = (name, lst)

  def add_argument(self, *args, **kw):
    name_list = self._option_lists.get(id(kw.get('choices')))
    if name_list:
      kw['metavar'] = name_list[0]
    return super(MyArgParser, self).add_argument(*args, **kw)

rawDescription: Extend argparse to write set names in the help text for optional argument choices and define those sets once at the end


code:
from urllib2 import urlopen
from xml.etree import ElementTree

response = urlopen('https://gdata.youtube.com/feeds/api/videos/Ej4_G-E1cAM/comments')
tree = ElementTree.parse(response)

nsmap = {'a': 'http://www.w3.org/2005/Atom'}
for author in tree.findall('.//a:author/a:name', namespaces=nsmap):
    print author.text

rawDescription: Single out tags in an xml document?

description: Single out tags in an xml document


code:
def printkwargs(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        ret = func(*args, **kwargs)
        print ret, args, kwargs
    return wrapper

rawDescription: Printing options of the function in the command prompt line


code: print df_merged.reset_index().set_index('date').resample('6M', how='sum', closed='left', loffset='-1M')

rawDescription: Custom Time Periods with Groupby

description: Custom Time Periods with Groupby


code:
#!/usr/bin/env python
from ftplib import FTP

class Reader:
  def __init__(self):
    self.data = ""
  def __call__(self,s):
     self.data += s

ftp = FTP('ftp.kernel.org')
ftp.login()
r = Reader()
ftp.retrbinary('RETR /pub/README_ABOUT_BZ2_FILES', r)

print r.data

rawDescription: Is it possible to read FTP files without writing them using Python?


code:
# All from app/conf/users/sites.mysite.conf
print config_root.sites.mysite.name
print config_root.sites.mysite.owner.name
print config_root.sites.mysite.owner.address
print config_root.sites.mysite.owner.phone

# All from app/conf/admin/root.conf
print config_root.sites.admin.name
print config_root.sites.admin.owner.name
print config_root.sites.admin.owner.address
print config_root.sites.admin.owner.phone

print config_root.server.name       # from _builtin_config
print config_root.server.version    # from app/conf/admin/root.conf
print config_root.server.host       # from app/conf/admin/server.conf
print config_root.server.port       # from app/conf/admin/server.conf

print config_root.licence_file      # from ../other_path/../special.conf
print config_root.log_file          # from ../other_path/../special.conf

rawDescription: Python configuration library

description: Python configuration library


code:
from xml.dom.minidom import parseString
documentNode = parseString("<types name='character' shortName='chrs'></types>")
typesNode = documentNode.firstChild

# Getting an attribute
print typesNode.attributes["name"].value # will print "character"

# Setting an attribute
typesNode.attributes["mynewattribute"] = u"mynewvalue"
print documentNode.toprettyxml()

rawDescription: How to retrieve attributes of xml tag in Python?

description: Retrieve attributes of xml tag in Python


code:
firstbinomial =  (((b * -1) + math.sqrt((b ** 2) - (4 * a * c))) / (2 * a)) * -1
secondbinomial = (((b * -1) - math.sqrt((b ** 2) - (4 * a * c))) / (2 * a)) * -1

print "The binomials are: (x{:+.0f})(x{:+.0f})".format(firstbinomial, secondbinomial)

# prints "The binomials are: (x-3)(x+5)"

rawDescription: How to make the plus sign show for a variable in python

description: Make the plus sign show for a variable in python


code:
def readRows():
    for rownum in range(sh1.nrows):
        rows = sh1.row_values(rownum)
        userNm = rows[4]
        Password = rows[5]
        supID = rows[6]
        print userNm, Password, supID

readRows()

rawDescription: Help with Excel, Python and XLRD

description: Help with Excel, Python and XLRD


code:
l = sorted(l, key = len)
print [j for i, j in enumerate(l) if all(j not in k for k in l[i + 1:])]

rawDescription: python delete substrings from list of strings

description: Python delete substrings from list of strings


code:
data = pd.read_csv('data.csv')
data_pieces = [data.ix[:, [0]], data.ix[:, [1, 2]], data.ix[:, [3,4]]]
data = pd.concat(data_pieces, axis=1, keys=['','Subject1', 'Subject2'])
print data

rawDescription: apply hierarchy or multi-index to panda columns

description: Apply hierarchy or multi-index to panda columns


code:
# Create CBSA list
CBSAlist = []
for value in sheet.col(1):
    CBSAlist.append(float(str(value).split(":")[1]))
print CBSAlist

rawDescription: Import xlrd values append to list

description: Import xlrd values append to list


code:
from collections import defaultdict
my_dict = defaultdict(str)
print my_dict['some_key_that_doesnt_exist']  #should print empty string
my_dict.update(form)
msg = """
%(Frist_Name)s

%(Last_Name)s
 """ % my_data_dict

rawDescription: How can I replace named vars in a string in python from an incomplete dictionary?

description: Replace named vars in a string in python from an incomplete dictionary


code:
for row in char("A"):
    print bin(row[0])[2:].zfill(8)

rawDescription: Storing image data (fonts) in hexadecimal format in Python


code:
#!/usr/bin/python

import fileinput
for line in fileinput.input():
    print line

rawDescription: Reading files using raw_input or argv

description: Reading files using raw_input or argv


code:
import sqlite3
db = sqlite3.connect(':memory:', detect_types=sqlite3.PARSE_DECLTYPES)
sqlite3.register_adapter(bool, int)
sqlite3.register_converter("BOOLEAN", lambda v: bool(int(v)))
db.execute('CREATE TABLE foo (bar BOOLEAN)')
db.execute('INSERT INTO foo VALUES (?)', (True,))
db.execute('INSERT INTO foo VALUES (?)', (False,))
for row in db.execute('SELECT * FROM foo'):
    print row

rawDescription: Storing bools in SQLite database

description: Storing bools in SQLite database


code:
for e in a:
    print e+(100 if e<4 else 1)

rawDescription: Fastest way to modify list values with condition python


code: print [d[0] for d in a]

rawDescription: Python Slicing List

description: Python Slicing List


code:
from lxml.html import parse

tree = parse("http://chem.sis.nlm.nih.gov/chemidplus/name/acetone")

def get_contents_by_title(tree, title):
    return tree.xpath("//h3[. = '%s']/following-sibling::*[1]//div/text()" % title)

print get_contents_by_title(tree, "Name of Substance")
print get_contents_by_title(tree, "MeSH Heading")

rawDescription: Can I access the subchild of a parent in XPath?

description: Access the subchild of a parent in XPath


code:
#!/usr/bin/env python

def add_item(list, item):
    list.append(item)
    return list

mylist = []
mylist = add_item(mylist, 1)
mylist = add_item(mylist, 2)

print mylist    # prints [1, 2]

rawDescription: adding to a list defined in a function

description: Adding to a list defined in a function


code:
import dis

# Here are the things we might want to do
def do_something_a():
    print 'I did a'

def do_something_b():
    print 'I did b'

def do_something_c():
    print 'I did c'

# Case 1
def f1(x):
    if x == 1:
        do_something_a()
    elif x == 2:
        do_something_b()
    elif x == 3:
        do_something_c()

# Case 2
FUNC_MAP = {1: do_something_a, 2: do_something_b, 3: do_something_c}
def f2(x):
    FUNC_MAP[x]()

# Show how the functions execute
print 'Case 1'
dis.dis(f1)
print '\n\nCase 2'
dis.dis(f2)

rawDescription: Python Dictionary vs If Statement Speed

description: Python Dictionary vs If Statement Speed


code:
if rank == 0:
    cb = numpy.zeros(size)
    rstat = MPI.Status()
    for i in range((size-1)*3):
        neww = comm.recv(source=MPI.ANY_SOURCE, tag=1, status=rstat)
        src = rstat.Get_source()
        newdeltaw = comm.recv(source=src, tag=2)
        print "newdelw is",newdeltaw,"neww is",neww
        cb[neww]=cb[neww]+newdeltaw
        print "cb=",cb
else:
    data = rank
    for i in range(3):
        comm.send(rank,dest=0,tag=1)
        comm.send(data,dest=0,tag=2)

rawDescription: Ensuring two messages come from the same MPI task


code:
roll = []
for i in itertools.product([str(j) for j in xrange(1,10)], repeat=6):
    roll += [str(j)+''.join(i) for j in xrange(1,8)]
print roll

rawDescription: Generate all n digit numbers using itertools, with bounds on range of 1st digit


code:
from multiprocessing import Pool
from multiprocessing.managers import BaseManager, DictProxy, ListProxy
from collections import defaultdict

class MyManager(BaseManager):
    pass

MyManager.register('defaultdict', defaultdict, DictProxy)
MyManager.register('list', list, ListProxy)

def test(i,x,T, order):
    target_sum = 100
    # T[x, i] is True if 'x' can be solved
    # by a linear combination of data[:i+1]
    #T = defaultdict(bool)          # all values are False by default
    T[0, 0] = True                  # base case
    for s in range(target_sum + 1): # set the range of one higher
                                    # than sum to include sum itself
        for c in range(s / x + 1):
            if T[s - c * x, i]:
                T[s, i + 1] = True
                order.append(i)

def setup():
    mgr = MyManager()
    mgr.start()
    run_order = mgr.list()
    T = mgr.defaultdict(bool)
    T[0, 0] = True
    data = [2,5,8]
    return data, T, run_order

def async_loop(data, func, output, run_order, wait=False):
    pool = Pool(processes=6)
    for i, x in enumerate(data):    # i is index, x is data[i]
        p=pool.apply_async(func, (i, x, output, run_order))
        if wait:
            p.wait()
    pool.close()
    pool.join()
    pool.terminate()

def output_size(output, run_order, wait):
    wait = 'out' if wait else ''
    print 'size of Table (with{0} multiprocesing) is: {1}'.format(
        wait, len(output))
    count_of_true = []
    for (x, result) in output.items():
        if output[x] == True:
            count_of_true.append(x)
    print 'total number of true (with{0} multiprocesing) is: {1}'.format(
        wait, len(count_of_true))
    print 'run order is: {0}'.format(run_order)

data, table, run_order = setup()
async_loop(data, test, table, run_order, wait=True)
output_size(table, run_order, True)
data, table, run_order = setup()
async_loop(data, test, table, run_order, wait=False)
output_size(table, run_order, false)

rawDescription: Python defaultdict behavior possible with multiprocessing?


code:
blanks = "Welcome"
secretWord = "WELCOME"
i = 3
print blanks[:i] + secretWord[i] + blanks[i+1:]

rawDescription: List Slicing in Python 2

description: List Slicing in Python 2


code:
import requests, json
r = requests.get("https://www.python.org/")
print json.dumps(dict(r.headers))

rawDescription: Get a header with Python and convert in JSON (requests - urllib2 - json)


code:
from MyConfig import config

print config.checkForAbort

import foo

assert config is foo.config

rawDescription: Creating a class with all the elements specified in a file using ConfigParser


rawDescription: trying to get reasonable values from scipy powerlaw fit

description: Trying to get reasonable values from scipy powerlaw fit


code:
import re
# pattern to get values of UFD, DF, and IE from dict1
p1 = '(?s){\s+"AP":\s+{\s+"UFD":\s+(.*?),\s+"DF":\s+"(.*?)"\s+},\s+"IE":\s+(.*?),\s+"\$":\s+"PDAE"\s+}'
# pattern to match dict2
p2 = '({[\s\S]+?"UFD":\s+)(.*?)(,\s+"DF":\s+")(.*?)("[\s\S]+?"IE":\s+)(.*?)(,[\s\S]+?})'

dict1 = """
{
   "AP": {
           "UFD": xxx,
           "DF": "xxxxxx"
         },
   "IE": xxxx,
   "$": "PDAE"
}
"""

dict2 = """
{
   "FileNameModifierItemPosition": 1,
   "FileNameModifierItemType": 0,
   "FileNameModifierParameters": {
                                  "DateParameters": {
                                                      "DT": 0,
                                                      "UFD": true,
                                                      "DF": "yyyyMMdd"
                                                    },
                                  "FqlParameters":  {
                                                      "FExpression": null
                                                    }
                                 },
    "IE": true,
    "$": "PDMFN
}
"""
# retrieve values from dict1
dict1_values = re.findall(p1, dict1)[0]
# replace dict2 with corressponding values from dict1
print re.sub(p2, r'\1%s\3%s\5%s\7' % dict1_values, dict2)

rawDescription: How to find and replace all occurrences of a json dictionary in json file using Python?

description: Find and replace all occurrences of a json dictionary in json file using Python


code:
print timeit.timeit("z2.mean(axis=(1, 2))", setup="from __main__ import z1, z2", number=N)
print timeit.timeit("z1.mean(axis=2).mean(axis=1)", setup="from __main__ import z1, z2", number=N)

0.134118080139  # z2.mean  normal numpy
1.08952116966   # z1.mean  masked

rawDescription: Conditioning and operation on multidimensional (3D) array in Python-NumPy


code:
import subprocess

subprocess.Popen(['python', 'foo.py'])
print '123'

rawDescription: Run a python script independently when a method is called


code:
from bs4 import BeautifulSoup

html = """

   val1
   val2

text of no interesttext of interest
"""

soup = BeautifulSoup(html)
p = soup.find('span', attrs={'class': 'class_name_2'}).parent
p.span.em.extract()
print p.text

rawDescription: Python & Beautifulsoup web scraping - select a paragraph with a specific child tag


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup(raw, "html.parser")
print soup.prettify()

rawDescription: Scrapy xpath get text of an element that starts with <


code:
import re
line ='(j V t) ^ (p V q) ^ (~p V t),j=T,t=F,p=F,q=T'
re.findall(r'^([^,]*),(.*)$', line)
KB = re.findall(r'^[^,]*', line)[0]
Truth = re.findall(r'^[^,]*,(.*)', line)[0]
print KB
print Truth

rawDescription: return all the text before the first comma

description: Return all the text before the first comma


code:
import ConfigParser

config = ConfigParser.ConfigParser(allow_no_value = True)
config.add_section('default_settings')
config.set('default_settings', '; comment here')
config.set('default_settings', 'test', 1)
with open('config.ini', 'w') as fp:
    config.write(fp)

config = ConfigParser.ConfigParser(allow_no_value = True)
config.read('config.ini')
print config.items('default_settings')

rawDescription: Writing comments to files with ConfigParser


code:
import csv, json

with open('csv_to_json_test.csv', 'rb') as csvfile:
    csvfields = 'group', 'name', 'average_weight', 'total_return'
    reader = csv.DictReader(csvfile, fieldnames=csvfields)
    database = {}
    stocks = database['stocks'] = []  # initialize item to be parsed
    group = None
    for row in reader:
        if row['group']:
            group = row['group']
        else:
            stocks.append(
                {
                    'data': {
                        "portfolio_average_weight": row['average_weight'],
                        "portfolio_total_return": row['total_return']
                    },
                    'name': row['name'].rstrip(),  # strips trailing spaces
                    'group': group,
                }
            )

print 'database =',
print json.dumps(database, indent=4)

rawDescription: CSV to JSON Python, Append Group Category to Dictionary


code:
for theme in Theme.objects.all():
    print theme.name
    print theme.other_attribute

rawDescription: Model.objects.only("columnname") doesn't work. It shows me everything


code:
a = None
b = None

def somefunc():
    # This works.
    print a # None
    print b # None

def main():
    global a, b
    a = 'whatever'
    b = 10
    # somefunc() exist in the local scope, you don't need the getattr.
    # just call it.
    somefunc()

if __name__ == '__main__': # You were missing the undescores surrounding main
    main()

rawDescription: python variable change scope

description: Python variable change scope


code:
from re import match

def rules(name):
    for badWord in bannedWords:
        if name.lower().find(badWord) >= 0:
            return False
    return match(r'^([A-Z][a-z]+(?:[A-Z]?[a-z]+)* ?){1,}$', name)

bannedWords = ('really', 'awesome')
input = ['000000AAAAAARob Alsod', 'AAAPerson Person', 'Jeff the awesome Guy', 'Jenna DEeath', 'GEOFFERY EVERDEEN', 'shy guy', 'Theone Normalperson', '  Guywith Whitespace', 'Someone Middlename MacIntyre', '', 'Jack Really Awesome']
results = filter(rules, input)
print results

rawDescription: Using Regex to 'Clean Up' a List of Names


code:
import re
data = '''Barnes, Michael8:00a10:00aTech
Collins, Jessica8:00a4:00pSupervisor
Hamilton, Patricia8:00a10:00aTech
Smith, Jan8:00a10:00aTech
Park, Kimberly8:00a10:00aTech
Edwards, Terrell10:00a12:00pTech
Green, Harrold12:00p2:00pTech
Tait, Jessica12:00p2:00pTech
Tait, Jessica2:00p4:00pTech
Hernandez, William (Monte)4:00p6:30pSupervisor
Tait, Chioma4:00p6:00pTech
Hernandez, William (Monte)6:30p7:00pSupervisor
Hernandez, William (Monte)7:00p9:00pSupervisor
Tailor, Thomas (Jason)9:00p12:00aSupervisor
Jones, Deslynne10:00p12:00aTech'''

print re.findall(r'(.*?)(\d{1,2}:\d\d[ap])(\d{1,2}:\d\d[ap])(.*)', data)

rawDescription: Parsing Text Document Lines in Python on Wildcards/Patterns


code:
# original
print row.xpath(".//td[@class='actual']/text()").extract()
# ported
print extract_text(row.find(r'.//td[@class="actual"]'))

rawDescription: converting scrapy to lxml

description: Converting scrapy to lxml


code:
def listen(self):
    while True:
        try:
            chunk = self.stream.read(INPUT_FRAMES_PER_BLOCK)
        except IOError, e:
            self.errorcount += 1
            print( "(%d) Error recording: %s"%(self.errorcount,e) )
            self.noisycount = 1
            return

        mx = audioop.max(chunk, 2)
        if mx > 2300:               #print the volume level whenever the volume is above 2300
                print(mx)

        elif mx < 2300:             #If the volume is below 2300:
            time.sleep(2)           #Wait two seconds
            if mx > 2300:           #If after two seconds the volume is back up, repeat
                continue

rawDescription: (Almost Done) Python program that closes stream when below audio threshold


code:
fib_matrix = [[1,1],
              [1,0]]

def matrix_square(A, mod):
    return mat_mult(A,A,mod)

def mat_mult(A,B, mod):
  if mod is not None:
    return [[(A[0][0]*B[0][0] + A[0][1]*B[1][0])%mod, (A[0][0]*B[0][1] + A[0][1]*B[1][1])%mod],
            [(A[1][0]*B[0][0] + A[1][1]*B[1][0])%mod, (A[1][0]*B[0][1] + A[1][1]*B[1][1])%mod]]

def matrix_pow(M, power, mod):
    #Special definition for power=0:
    if power <= 0:
      return M

    powers =  list(reversed([True if i=="1" else False for i in bin(power)[2:]])) #Order is 1,2,4,8,16,...

    matrices = [None for _ in powers]
    matrices[0] = M

    for i in range(1,len(powers)):
        matrices[i] = matrix_square(matrices[i-1], mod)

    result = None

    for matrix, power in zip(matrices, powers):
        if power:
            if result is None:
                result = matrix
            else:
                result = mat_mult(result, matrix, mod)

    return result

print matrix_pow(fib_matrix, 10**19, 1000000007)[0][1]

rawDescription: Nth Fibonacci number for n as big as 10^19?


code:
class DjangoBaseClass(object):
    def method_that_is_hidden(self):
        print "The hidden method is there!"

rawDescription: In Python, can I hide a base class's members?


code:
from itertools import islice
a = range(10)
i = iter(a)
print list(islice(i, 1, 3))
print list(islice(i, 1, 3))
print list(islice(i, 1, 3))
print list(islice(i, 1, 3))

rawDescription: Python: itertools.islice not working in a loop


code:
import os.path

for val in table.values():
    if val.startswith('/') and not os.path.exists(val):
        print "BAD FILE!!! ", val

rawDescription: how to verify a hash in python?

description: Verify a hash in python


code:
import cv2
import cv2.cv as cv
import getopt, sys

def detect(img, cascade):
    for scale in [float(i)/10 for i in range(11, 15)]:
        for neighbors in range(2,5):
            rects = cascade.detectMultiScale(img, scaleFactor=scale, minNeighbors=neighbors,
                                             minSize=(20, 20), flags=cv2.cv.CV_HAAR_SCALE_IMAGE)

            print 'scale: %s, neighbors: %s, len rects: %d' % (scale, neighbors, len(rects))

def find_face_from_img(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = cv2.equalizeHist(gray)
    rects = detect(gray, cascade)

if __name__ == '__main__':

    args, video_src = getopt.getopt(sys.argv[1:], '', ['cascade=', 'nested-cascade='])
    try: video_src = video_src[0]
    except: video_src = 0
    args = dict(args)

    cascade_fn = args.get('--cascade', "cascades/haarcascade_frontalface_alt.xml")
    cascade = cv2.CascadeClassifier(cascade_fn)

    c=cv2.VideoCapture(0)
    while(1):
        ret, frame = c.read()
        rects = find_face_from_img(frame)
        if 0xFF & cv2.waitKey(5) == 27:
                break

rawDescription: Using OpenCV detectMultiScale to find my face


code:
paste
print sum(vol)
print vol

rawDescription: Comparing nested list of tuples

description: Comparing nested list of tuples


code:
from BeautifulSoup import BeautifulSoup

test_html = load_html_from_above()
soup = BeautifulSoup(test_html)

for t in soup.findAll(text=True):
    text = unicode(t)
    for vowel in u'aeiou':
        text = text.replace(vowel, u'')
    t.replaceWith(text)

print soup

rawDescription: Using Beautiful Soup, how do I iterate over all embedded text?


code:
from BeautifulSoup import BeautifulSoup
import re

soup = BeautifulSoup('
foo
')

r = soup.findAll('p', text=re.compile('foo'))

print r[0].parent

rawDescription: Using BeautifulSoup's findAll to search html element's innerText to get same result as searching attributes?


code:
a = [None] + a + [None]
for i, j, k in zip(a, a[1:], a[2:]):
 print i, j, k

rawDescription: Is there an easy way to get next and prev values in a for-loop?


code:
import itertools

dict = {3: 54, 37: 100, 56: 33}

combos = []

for i in range(len(dict.keys())):

    combos.extend([sorted(l) for l in itertools.combinations(dict.keys(), i+1)])

print combos

rawDescription: is it possible to create a list of lists on a recursive function?

description: Is it possible to create a list of lists on a recursive function


code:
f = open('metadata.txt', 'r') #open file for reading

def build_data(f): #build dictionary

    output = {} #Dict
    for line in f.readlines(): #Iterates through every line in the string
        if "=" in line: #make sure line has data as wanted
            l = line.split("=") #Seperate by "=" and put into a list
            output[l[0].strip()] = l[1].strip() #First word is key, second word is value

    return output #Returns a dictionary with the key, value pairs.

data = build_data(f)

print data["IMAGE_QUALITY"] #prints 9

rawDescription: convert contents of metadata file into variables list

description: Convert contents of metadata file into variables list


code:
import xml.etree.ElementTree as ET

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

for child in root.iter('register'):
    name = child.attrib.get('name', None)
    offset = child.attrib.get('offset', None)
    width = child.attrib.get('width', None)
    access = child.attrib.get('access', None)
    description = child.find('description')

    if description is not None:
        desc = description.text
    else:
        desc = child.find('field').find('description').text

    print "[*] Register Name: {}".format(name)
    print "[*] Register Offset: {}".format(offset)
    print "[*] Register Width: {}".format(width)
    print "[*] Register Access: {}".format(access)
    print "[*] Description: {}".format(desc)
    print ""

rawDescription: Display the contents of an element in xml using python


code:
d = [1]
print code_scram(d)

rawDescription: super simple cipher function in python. comparing a list to keys in a dictionary

description: Super simple cipher function in python. comparing a list to keys in a dictionary


code:
#! /usr/bin/env python

line = '"bromine ";" 00:23:AE:90:FA:C6 ";" 144.38.198.130";151'
print line

line = [s.strip() for s in line.replace('"', '').split(';')]
print line

name, macAddress, ipAddress, roomNum = line
d = {'ip': ipAddress, 'mac': macAddress, 'name': name, 'room': roomNum}

print d

rawDescription: Python converting string to list and then some


code:
from bs4 import BeautifulSoup as bsoup

html = """

	Snap Id	Snap Time	Sessions	Cursors/Session

Begin Snap:	98810	29-Jun-15 08:00:02	700	    129.6

End Snap:	98864	29-Jun-15 17:00:23	703	    129.1

Elapsed:	 	             540.35 (mins)

DB Time:	 	           2,963.17 (mins)

"""

soup = bsoup(html)
print soup.find_all('td', class_='awrnc')[2].get_text()
# 29-Jun-15 08:00:02

rawDescription: extract date from html document

description: Extract date from html document


code:
kain.width=10
print eve.area(), kain.area(), abel.area()
# 15 50 15

rawDescription: Subclassing in python of instantiated superclass


code:
MyClass.temp.append(3)
print instance1.temp   # [1, 2, 3]
instance1.temp = []
print instance1.temp   # []         uses the instances temp
print instance2.temp   # [1, 2, 3]
del instance1.temp
print instance1.temp   # [1, 2, 3]  uses the class' temp again

rawDescription: Understanding instance and class variable python


code:
stringToData = "aaa bbb ffffffff eeeee"
char, num = collections.Counter(stringToData).most_common(1)[0]
print "In your string, the most frequent character is %s, which occurred %d times" % (char, num)

rawDescription: Pretty-printing data from counter - Python


code:
import time
import random

def get_x(i):
    if i == 11 or i == 111:
        return 0.5
    if (12 <= i and i <= 100) or (112 <= i and i <= 200):
        return 1
    return 0

x_array = [get_x(i) for i in range(300)]
i_array = [i % 300 for i in range(1000000)]
print "Sequential access"
start = time.time()
for i in i_array:
    x = get_x(i)
end = time.time()
print "get_x method:", end-start

start = time.time()
for i in i_array:
    x = x_array[i]
end = time.time()
print "Array method:", end-start
print

random.seed(123)
i_array = [random.randint(0,299) for i in range(1000000)]
print "Random access"
start = time.time()
for i in i_array:
    x = get_x(i)
end = time.time()
print "get_x method:", end-start

start = time.time()
for i in i_array:
    x = x_array[i]
end = time.time()
print "Array method:", end-start

rawDescription: Store Values through Array or Use Function? (to optimize)


code:
list = "a|dhg|shg|abc|oyi"
items = list.split("|")
count = len(items)
print count

rawDescription: Given a list, How to count items in that list?


code:
df = df.set_index('Label1')
print df.T.to_dict(outtype='list')

{'key3': ['col1value3', 'col2value3'],
 'key2': ['col1value2', 'col2value2'],
 'key1': ['col1value1', 'col2value1']}

rawDescription: Convert Dataframe to a Dictionary with List Values


code:
for number in row:
    print number

rawDescription: Split values from arrays

description: Split values from arrays


code:
import urllib2
webpage = urllib2.urlopen('http://www.dmx.com/about/our-clients')
webpage_text = webpage.read()
webpage.close()
for name in ['Caribou Coffee', 'Express', 'Sears']:
    if name in webpage_text:
        print name, "found!"

rawDescription: Fuzzy matching a string within a large body of text in Python (url)


code:
mystring = "abcdefghijklmnopqrstuvwxyz"
for letter1 in mystring:
    for letter2 in mystring[mystring.index(letter1):]:
        for letter3 in mystring[mystring.index(letter2):]:
            for letter4 in mystring[mystring.index(letter3):]:
                for letter5 in mystring[mystring.index(letter4):]:
                    for letter6 in mystring[mystring.index(letter5):]:
                        for letter7 in mystring[mystring.index(letter6):]:
                            for letter8 in mystring[mystring.index(leter7):]:
                                for letter9 in mystring[mystring.index(letter8):]:
                                    for letter10 in mystring[mystring.index(letter9):]:
                                        strr = "".join([letter3,letter7,letter2,letter1,letter4,letter5,letter6,letter10,letter8,letter9])
                                        if hashlib.md5(strr).hexdigest() == md5:
                                            print "key = " ,strr
                                            break

rawDescription: Key Bruteforcing

description: Key Bruteforcing


code:
def a(name, age, id, words, phone):
     name = name
     age=age
     id=id
     words=words
     phone=phone
     print name+age+id+words+phone

a("piyush", "23", 123, *f("12","123"))
piyush23123123hello

rawDescription: Pass function outputs as arguments to another function


code:
import string
intermediate=""
with open("key_Value.txt", 'r') as fin:
    for line in fin:
        intermediate+=line.strip() + "\n"
i = intermediate.splitlines()
heading = []
values = []
splitstring = " "
for s in i:
    if "{" in s:
        splitstring = " : "
    if not (("{" in s) or ("}" in s)):
        try:
            s=string.replace(s,",","")
            key = s.split(splitstring,1)
            key[1]=string.replace(key[1]," : ","")
            key[1]=string.replace(key[1]," :","")
            heading.append(key[0])
            values.append(key[1])
        except:
            pass

head=""
for h in heading:
    head+=h+"\t"

val=""
for v in values:
    val+=v+"\t"

print head
print val

rawDescription: formating text, rows to columns in python

description: Formating text, rows to columns in python


code:
process = Popen(shlex.split(cmd), stdout=PIPE, stderr=PIPE)
# Popen.wait() waits for the command to complete and
# returns the command's return code
if process.wait() != 0:
    print "AN ERROR OCCURED"
logger.error(process.stderr)
logger.info(process.stdout)

rawDescription: Printing Function Output to Syslog?

description: Printing Function Output to Syslog


code:
import urllib2
from xml.dom.minidom import parse, parseString
u1=urllib2.urlopen('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml')
dom=parse(u1)
print dom
dom.childNodes
dom.childNodes[0].childNodes

rawDescription: Opening remote documents in Python

description: Opening remote documents in Python


code:
@sched.cron_schedule(second='*/5')
def some_decorated_task():
    date_now = datetime.datetime.now().date()
    sess = Session()
    for item in sess.query(user_groups_n_hosts).filter(user_groups_n_hosts.end_time < str(date_now)):
        print item.id, item.server_users.native_linux_user
    sess.close()

rawDescription: Flask-SQLAlchemy using global DB instance


code:
import os

print "We're going to write to a file you'll be prompted for"
targetfile = raw_input('Enter a filename: ')
if os.path.isfile(targetfile) == True:
    targetfilefound = open(targetfile , 'w')
    print "What do we write in this file?"
    targetfilefound.write(raw_input())
    targetfilefound.close()
else:
    print "File does not exist, do you want to create it? (Y/n)"
    action = raw_input('> ')
    if action == 'Y' or action == 'y':
        targetfilefound = open(targetfile , 'w')
        print "What do we write in this file?"
        targetfilefound.write(raw_input())
        targetfilefound.close()
    else:
        print "No action taken"

rawDescription: Python raw_input for file writing

description: Python raw_input for file writing


code:
def wait_on(name, e):
    print "Waiting on %s..." % (name,)
    e.wait()
    print "%s fired!" % (name,)

def test():
    import time

    e1 = threading.Event()
    e2 = threading.Event()

    or_e = OrEvent(e1, e2)

    threading.Thread(target=wait_on, args=('e1', e1)).start()
    time.sleep(0.05)
    threading.Thread(target=wait_on, args=('e2', e2)).start()
    time.sleep(0.05)
    threading.Thread(target=wait_on, args=('or_e', or_e)).start()
    time.sleep(0.05)

    print "Firing e1 in 2 seconds..."
    time.sleep(2)
    e1.set()
    time.sleep(0.05)

    print "Firing e2 in 2 seconds..."
    time.sleep(2)
    e2.set()
    time.sleep(0.05)

rawDescription: Python threading: can I sleep on two threading.Event()s simultaneously?


code:
lst = range(1, 8)
while lst:
    print lst.pop(len(lst)/2)

rawDescription: Python - iterating beginning with the middle of the list and then checking either side


code:
from pyparsing import *

integer = Word(nums)
variable = Word(alphas, alphanums)
boolLiteral = oneOf("true false")

operand = boolLiteral | variable | integer

comparison_op = oneOf("== <= >= != < >")
QM,COLON = map(Literal,"?:")
expr = infixNotation(operand,
    [
    (comparison_op, 2, opAssoc.LEFT),
    ((QM,COLON), 3, opAssoc.LEFT),
    ])

print expr.parseString("(x==1? true: (y == 10? 100 : 200) )")

rawDescription: Parsing nested ternary expressions

description: Parsing nested ternary expressions


code:
import timeit

bad_chars = '(){}<>'

setup = """import re
import string
s = 'Barack (of Washington)'
bad_chars = '(){}<>'
rgx = re.compile('[%s]' % bad_chars)"""

timer = timeit.Timer('o = "".join(c for c in s if c not in bad_chars)', setup=setup)
print "List comprehension: ",  timer.timeit(100000)

timer = timeit.Timer("o= rgx.sub('', s)", setup=setup)
print "Regular expression: ", timer.timeit(100000)

timer = timeit.Timer('for c in bad_chars: s = s.replace(c, "")', setup=setup)
print "Replace in loop: ", timer.timeit(100000)

timer = timeit.Timer('s.translate(string.maketrans("", "", ), bad_chars)', setup=setup)
print "string.translate: ", timer.timeit(100000)

rawDescription: Python strip() multiple characters?

description: Python strip() multiple characters


code:
for link in soup.find_all("a",
                          href=lambda href: href and "term" in href,
                          text=lambda text: text and "2015" in text):
    print link["href"]

rawDescription: How do I grab all the links within an element in HTML using python?

description: Grab all the links within an element in HTML using python


code:
import csv

with open('/tmp/test.csv', 'r') as csvin:
    for line in csv.reader(csvin):
        if not ''.join(x.strip() for x in line) or line[0].strip()[0]=='#':
            continue
        else:
            break
    data={k:[] for k in line}
    for line in csv.DictReader(csvin, data.keys()):
        for k,v in line.items():
            data[k].append(v)

print data

rawDescription: Use DictReader in Python to determine table header in CSV file


code: print 'current input number is %s' %(len(In)-1)

rawDescription: get current prompt number in ipython

description: Get current prompt number in ipython


code:
from subprocess import Popen, PIPE
from struct import calcsize, unpack

cprog = Popen("cprog", stdout=PIPE)

fmt = "@ii"
str = cprog.stdout.read(calcsize(fmt))
cprog.stdout.close()

(i, j) = unpack(fmt, str)
print i, j

rawDescription: How to pass a C struct to Python to get data?

description: Pass a C struct to Python to get data


code:
import requests

access_token = '<REDACTED>'
member_id = '<REDACTED>'

has_more = True
params = {}
while has_more:
    response = requests.post('https://api.dropbox.com/1/delta', data=params, headers={
        'Authorization': 'Bearer ' + access_token,
        'X-Dropbox-Perform-As-Team-Member': member_id
    }).json()

    for entry in response['entries']:
        print entry[0]

    has_more = response['has_more']
    params['cursor'] = response['cursor']

rawDescription: Dropbox /delta ignoring cursor

description: Dropbox /delta ignoring cursor


code:
a = "\\n"
len(a)
print a
a
a[0]
len(a[0])

rawDescription: Newline literal read from file is being escaped automatically


code:
import itertools

def subsets(seq):
    for mask in itertools.product([False, True], repeat=len(seq)):
        yield [item for x, item in zip(mask, seq) if x]

def ordered_groups(seq):
    for indices in subsets(range(1, len(seq))):
        indices = [0] + indices + [len(seq)]
        yield [seq[a:b] for a,b in zip(indices, indices[1:])]

for group in ordered_groups([1,3,7,8,9]):
    print group

rawDescription: Find all possible ordered groups in a list


code:
soup = BeautifulSoup(twit)

name_tag = soup('strong', {'class': 'fullname js-action-profile-name show-popup-with-id'})
user = name_tag[0].contents[0]

action_tag = soup('span', {'class': 'username js-action-profile-name'})
at_sign = action_tag[0].contents[0].contents[0]
show_name = action_tag[0].contents[1].contents[0]

twit_text = soup('p', {'class': 'js-tweet-text'})
message = twit_text[0].contents[0]
url = twit_text[0].contents[1]['data-expanded-url']

print user, at_sign, show_name, message, url

rawDescription: Python, BeautifulSoup - Parsing out a Tweet


code:
import time

def main(num,text_edit_box, app):
    for i in range(int(num)):
        text_edit_box.setText(str(i))
        print i
        time.sleep(.1)
        app.processEvents()
    return "success"

rawDescription: pyqt4 QLineEdit realtime output using other py module

description: Pyqt4 QLineEdit realtime output using other py module


code:
import os, tarfile

def main():
    output_path = "/Users/rs/Documents/clients_file.txt"
    path = []
    with open(output_path) as f:
        for exptini_path_raw in f:
            exptini_path = exptini_path_raw.strip()
            print "Adding to path: {}".format(exptini_path)
            path.append(exptini_path)

    for i in range(1,10):
        for j in range(0,len(path)):
            p = "{}/{:0>2}/middleware".format(path[j], i)
            print "Path to search for tar files: {}".format(p)
            for root, dirs, files in os.walk(p):
                for f in files:
                    print "Investigating file: {}".format(f)
                    if not ('client-logs' in f or 'middleware-logs' in f):
                        print "This file does not match expected file name...skipping: {}".format(f)
                        continue
                    print 'going to extract {} to folder {}'.format(os.path.join(p,f), p)
                    f1 = os.path.join(p,f)
                    tar = tarfile.open(f1)
                    tar.extractall(path=p)
                    tar.close()

if __name__ == '__main__':
    main()

rawDescription: Untar file using python no error/warnings?


code:
import ast

ldapData = "[[('CN=LName\\, FName,OU=MinorUserGroup,OU=MajorUserGroup,DC=my,DC=company,DC=com', {'department': ['theDepartment'], 'mail': ['theEmail@mycompany.com']})]]"

# Using the ast module's function is much safer than using eval. (See below!)
obj = ast.literal_eval(ldapData)[0][0]
rawDict = obj[1]
data = { k: v[0] for k, v in rawDict.iteritems() }

# The dictionary.
print data

rawDescription: What is the best Pythonic way to parse this data?


code:
li = [SpecialNumber(1), SpecialNumber(3), SpecialNumber(2)]
[i.number for i in li]
min_num = min(li,key=attrgetter('number'))
print min_num.number

rawDescription: Python min function with a list of objects


code:
roller = lambda (i, x): np.roll(x, -i)
row_terator = enumerate(df.values)
rolled = map(roller, row_terator)
result = DataFrame(np.vstack(rolled), index=df.index, columns=df.columns)

rawDescription: Pandas: convert upper triangular dataframe by shifting rows to the left


code:
from lxml import etree

s = """<?xml version="1.0" encoding="UTF-8"?>
<tmx version="1.4a"/>"""

tree = etree.fromstring(s)
header = etree.SubElement(tree,'header',{'adminlang': 'EN'})
body = etree.SubElement(tree,'body')

print etree.tostring(tree, encoding="UTF-8",
                     xml_declaration=True,
                     pretty_print=True,
                     doctype='<!DOCTYPE tmx SYSTEM "tmx14a.dtd">')

rawDescription: How to create <!DOCTYPE> with Python's cElementTree

description: Create <!DOCTYPE> with Python's cElementTree


code:
import time
apples = 0
gold = 0

def start():
    print "Hello\n"
    print "My name is Jason\n"
    name = raw_input("What is your Name?\n")
    print "Welcome %s.\n" % name
    print "The object of this game is to pick apples and sell them for gold.\n"
    time.sleep(1)
    begin = raw_input("Would you like to play?\nY or N\n")
    if begin == "Y":
        time.sleep(1)
        beginfunction()
    if begin == "N":
        print "Okay, Please exit the terminal.\n"
        print "Goodbye!!!\n"
def beginfunction():
    global apples
    global gold
    papple = raw_input("Would you like to Pick some Apples?\nY or N\n")
    if papple == "Y":
        time.sleep(1)
        apples=apples+5
        print "Computer: You picked 5 apples.\n"
        print "You now have %s apples!!!\n" % apples
        beginfunction()
    if papple == "N":
        sell = raw_input("Would you like to sell some apples for gold?\nY or N\n")
        if sell == "Y":
            time.sleep(1)
            gold=gold+5
            apples=apples-5
            print "Computer: You sold 5 Apples for 5 Gold.\n"
            print "You now have '%s' apples, and '%s' Gold Pieces.\n" % (apples, gold)
            beginfunction()
        if sell == "N":
            print "Okay\n"
            beginfunction()

if __name__ == "__main__":
    start()

rawDescription: What is Wrong with this Code?(Python test Project)


code:
import os
from os.path import join, getsize
dirs_dict = {}

#We need to walk the tree from the bottom up so that a directory can have easy
# access to the size of its subdirectories.
for root, dirs, files in os.walk('python/Lib/email',topdown = False):

    # Loop through every non directory file in this directory and sum their sizes
    size = sum(getsize(join(root, name)) for name in files)

    # Look at all of the subdirectories and add up their sizes from the `dirs_dict`
    subdir_size = sum(dirs_dict[join(root,d)] for d in dirs)

    # store the size of this directory (plus subdirectories) in a dict so we
    # can access it later
    my_size = dirs_dict[root] = size + subdir_size

    print '%s: %d'%(root,my_size)

rawDescription: How to generate directory size recursively in python, like du . does?

description: Generate directory size recursively in python, like du . does


code:
x='Coord="GLOB"AL   Axis=X   Type="Y   ZR"   Color="Gray Dark"   Alt="Q   Z"qz   Loc=End'
print re.split(r'\s+(?=(?:[^"]*"[^"]*")*[^"]*$)',x)

rawDescription: Python: split string by a multi-character delimiter unless inside quotes


code:
# example data:
data_5d = [(1,2,3,4,5), (5,6,7,8,9)]
data_4d = [(1,2,3,4), (5,6,7,8)]
data_3d = [(1,2,3), (4,5,6)]
data_2d = [(1,2), (2,5)]
data_inconsistent = [(1,2,3,5), (8,9)]

# Version handling only 3D and 4D data:
# =====================================

def frontend(data):
  #check data.shape:
  # get number of dimensions by checking first element:
  dimensions = len(data[0])

  #optain values and reshape:
  if dimensions == 4:
    return [[x*y*z, t] for x, y, z, t in data]
  elif dimensions == 3:
    return [[x*y, t] for x, y, t in data]
  else:
    #handle error?
    return []

print "frontend():"
print "5D: ", frontend(data_5d)
print "4D: ", frontend(data_4d)
print "3D: ", frontend(data_3d)
print "2D: ", frontend(data_2d)
# this would raise an exception:
#print frontend(data_inconsistent)

# General Version
# ===============

def frontendx(data):
  return [[reduce(lambda a, b: a*b, elem[:-1]), elem[-1]] for elem in data]

print "\nfrontendx():"
print "5D: ", frontendx(data_5d)
print "4D: ", frontendx(data_4d)
print "3D: ", frontendx(data_3d)
print "2D: ", frontendx(data_2d)
print "?D: ", frontendx(data_inconsistent)

rawDescription: Get a 3D or 4D array as input and reshape it to 2D array in a function from external script


code:
r = re.compile(r'^\W*(var1)\W*$')
matches = [r.match(item).group(1) for item in variable_list if r.match(item)]
print matches

rawDescription: Regular expression in python - help needed


code:
list1 = []
list2 = []
with open("stackoverflow.txt", "r") as f:
    for line in f.readlines():
        l = line.split()
        list1.append(l[:6])
        list2.append(l[6:9])
print list1
print list2

rawDescription: Python load txt into array with string

description: Python load txt into array with string


code:
myDict = {'111, A': 4, '112, A': 3, '111, B': 10} # input

# keep track of the first half of the composite keys that you've already handled
# This is used to avoid redundant printing
done = set()

for key in myDict:
    # first half of your composite key (eg. '111')
    # I'll be using '111' to explain the rest of the code
    prefix = key.split(',')[0]

    if prefix not in done: # if you haven't already printed out the stuff for '111'
        print prefix # print '111'
        done.add(prefix) # add '111' to done, so that you don't print it out again

        # for all keys in myDict that are of the form "111,X" where X can be anything (e.g. A)
        for k in [k for k in myDict if k.split(',')[0]==prefix]:

            # print a <tab> and the suffix (in our example, "A") and the count value (in myDict, this value is 4)
            print '\t', k.split(',')[1], myDict[k]

rawDescription: Python Dictionary Print First occurence

description: Python Dictionary Print First occurence


code:
df2 = pd.melt(df[['at_k', 'mae', 'ndcg', 'rmse']], id_vars=['at_k'])
print df2

rawDescription: Convert from matplotlib to ggplot2 within python


code:
f = open("eng.txt", "r")
myDict = {}
linenum = 0

for word in f.read().split():
    if not word in myDict:
        myDict[word] = []
    myDict[word].append(linenum)

print "%-15s %-15s" %("Word", "Frequency")
for key in sorted(myDict):
    print '%-15s: %-15d' % (key, len(myDict[key]))

rawDescription: How to read the data from a file and get the concordance output

description: Read the data from a file and get the concordance output


code:
tweet = u"//@lilei: dd //@Bob: cc//@Girl: dd//@魏武: 利益所致 自然念念不忘// @诺什: 吸引优质  客户，摆脱屌丝男！！！//@MarkGreene: 转发微博"
RTpattern = r'''//?@(\w+)'''
for word in re.findall(RTpattern, tweet, re.UNICODE):
    print word

# lilei
# Bob
# Girl
# 魏武
# MarkGreene

rawDescription: Using Python regex to identify retweeters from tweets with Chinese characters


code:
x = "\xc3\xb4".decode('utf-8')
x
x + 'a'
'a' + x
'a %s' % x
'a {}'.format(x)
u'a {}'.format(x)
print u"Foo bar {}".format(x)

rawDescription: Python's string.format() and Unicode

description: Python's string.format() and Unicode


code:
def very_expensive_initializer(field):
    print 'Doing loads of work'
    return set()

class subclassed_dict(dict):
    def __missing__(self, key):
        item = self[key] = very_expensive_initializer(key)
        return item

stats = subclassed_dict()
stats['foo'].add(2)
stats['foo'].add(3)
stats['foo'].add(4)
stats['bar']
stats['bar']

rawDescription: How do I initialize values in a dictionary?

description: Initialize values in a dictionary


code:
from collections import Counter
with open("Input.txt") as inFile:
    print Counter([" ".join(line.split()[::2]) for line in inFile])

rawDescription: Python to group elements in specified column


code:
for i in range(5):
    a=i+1
print a

rawDescription: How does the Python's range function work?


code:
s = str(Decimal('1500'))
print s.rstrip('0').rstrip('.') if '.' in s else s

rawDescription: drop trailing zeros from decimal

description: Drop trailing zeros from decimal


code:
import os
import re
import fnmatch
import collections

coords_t = collections.namedtuple('coords_t', ['x0', 'y0', 'x1', 'y1', 'x2', 'y2', 'x3', 'y3'])
data_collection = []

rule = re.compile(fnmatch.translate('*.txt'), re.IGNORECASE)
for fname in sorted([name for name in os.listdir('.') if rule.match(name)]):
    with open(fname, 'r') as f:
        data = f.read()
        data_collection.append(coords_t(*data.replace('\n', ' ').split(' ')[:-1]))

print data_collection

rawDescription: Python reading text files for calculation


code:
s = 'aspire systems is in america'
li = s.split(' ') #["aspire", "systems", "is", "in", "america"]
#Reverse alternate words
for index in range (0,len(li),2): #0, 2, 4
    li[index] = reversed(li[index])
print ' '.join(reversed(li)) #Should print expected result

rawDescription: generic code to reverse alternate words in a sentence using python

description: Generic code to reverse alternate words in a sentence using python


rawDescription: stripping the zeros in csv with python

description: Stripping the zeros in csv with python


code:
df = pd.DataFrame(np.random.randint(1, 20, (10, 2)), columns=list('AB'))
df['inc_index'] = pd.cut(df.A, bins=[0, 7, 13, 15, 20])
print df.sort_values('inc_index')

rawDescription: Python/Pandas - Best way to group by criteria?


code:
#!/usr/bin/python
import sys
import re

str = sys.stdin.read()
print re.findall(ur"'\\[0-7]{1,3}'", str)

rawDescription: Regex and Octal Characters

description: Regex and Octal Characters


code:
def test():
    yield True
    print 'one'
    yield False
    print 'two'
    yield True
    print 'three'
all(test())

rawDescription: Does Python all(list) use short circuit evaluation?


code:
listA = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
listB = ['g', 'h', 'i', 'j', 'k', 'l']

di={k:v for v,k in enumerate(listA,1) if k not in set(listA) & set(listB)}
i=max(di.values())
di.update({k:v for v,k in enumerate(listB,i) if k not in set(listA) & set(listB)})
i=max(di.values())

print di
# {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'f': 6, 'i': 8, 'h': 7, 'k': 10, 'j': 9, 'l': 11}

rawDescription: Generate a Dictionary using two lists

description: Generate a Dictionary using two lists


code:
#!/usr/bin/env python

import readline

values = ['Paul Eden <paul@domain.com>',
          'Eden Jones <ejones@domain.com>',
          'Somebody Else <somebody@domain.com>']
completions = {}

def completer(text, state):
    try:
        matches = completions[text]
    except KeyError:
        matches = [value for value in values
                   if text.upper() in value.upper()]
        completions[text] = matches
    try:
        return matches[state]
    except IndexError:
        return None

readline.set_completer(completer)
readline.parse_and_bind('tab: menu-complete')

while 1:
    a = raw_input('> ')
    print 'said:', a

rawDescription: Howto do python command-line autocompletion but NOT only at the beginning of a string


code:
def ugly_math():
    print 'you will hit this once'
    for j in range(10):
        print 'loop ', j
        # insert math here
        yield  np.random.random(10) * j

rawDescription: Using events with matplotlib in a for loop


code:
print filter(None, starmap(solve, product(a, b, c)))
#[(1, 2, 16)]

rawDescription: Pythonistic way to intersect and add elements of lists at the same time


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

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


code:
from scapy.all import rdpcap
paks = rdpcap('ipv6.pcap')
for idx, pak in enumerate(paks):
    if pak.haslayer('ICMPv6ND_NS'):
        print "Index %s: %s" % (idx, repr(pak))

rawDescription: identifying icmpv6 type

description: Identifying icmpv6 type


code:
for usersocialauth in request.user.usersocialauth_set.all():
    print usersocialauth.provider

rawDescription: What method to use to figure out if user is connected to facebook


code:
html_txt = """0.32%	,
    >-0.01	, 0.29%	, 0.38%	,
    0.00
    """
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_txt)
print [tag.text for tag in soup.find_all('td') if tag.text.strip().endswith("%")]

rawDescription: Beautiful Soup HTML Extraction

description: Beautiful Soup HTML Extraction


code:
import itertools
import operator

probabilities = {'W':0.837, 'L':0.163}

winProbability = 0
for product in itertools.product(['W','L'], repeat=3): #3=number of games
    p = reduce(operator.mul,
               [probabilities[p] for p in product])

    if product.count('W') > 1: #works only for 3 games
        winProbability += p
        print "Susie wins:", product, "with probability:", p
    else:
        print "Susie looses:", product, "with probability:", p

print "Total probability of Susie winning:", winProbability

rawDescription: Itertools to create a list and work out probability


code: print freq_threshold(3)

rawDescription: What is my function missing? (python)

description: What is my function missing? (python)


code:
def checkio(words):
    count = ''
    for i in words.split():
        try: # try to convert the word to an integer
            int(i)
            count += "o"
        except ValueError as e: # if the word cannot be converted to an integer
            count += "k"
    print count

rawDescription: Concatenate letter to a string with for loop


code:
import re
line = 'blahblahblah|person=[[Old McDonald]]blahblahblah'
matchLine = re.search ('\|[a-z]+=(?:\[\[)?([A-Z][A-Za-z ]*)\]\]', line)
print matchLine.group (1)

rawDescription: Extracting text from a line: Regex in Python


code:
d = dict(list_2)
print [[d.get(item, None) for item in setItem] for setItem in list_1]

rawDescription: Efficiently combining values of two lists in Python


code:
if self.caseload:
     print ""
     for i, name in enumerate(self.caseload, 1):
          print "%d. %s" % (i, name)

rawDescription: Ordered dictionaries

description: Ordered dictionaries


code:
def get_message():
    message = stomp.get
    return simplejson.loads(message.body)

message = get_message()
while message[0]['fieldname1']:
    try:
        # get the first item in message[0]['fieldname2'][0]['fieldname2-1']
        # such item['fieldname2-1-1'] is true.  Whether we
        # find one and do this code, or don't and catch the
        # StopIteration, we wrap back to the while loop.
        item = next(x
                    for x in message[0]['fieldname2'][0]['fieldname2-1']
                    if item['fieldname2-1-1'])
        list1_new[len(list_new),:] = item['fieldname2-1-2']
        list1_state = (set(list1) == set(list1_new))

        if not list1_state:
            list1 = list1_new # should print the new list1 and then reset the while loop
            print list1
    except StopIteration:
        # There was no such item.
        pass
    message = get_message()

global ShutdownState
ShutdownState = True

rawDescription: Python Continue inside nested loops, getting to the right level of the nesting


code:
class A(object):
  def __init__(self):
    print "A"

class B(A):
  def __init__(self):
    A.__init__(self)
    print "B"

class C(A):
  def __init__(self):
    A.__init__(self)
    print "C"

class D(B,C):
  def __init__(self):
    B.__init__(self)
    C.__init__(self)
    print "D"

D()

rawDescription: Class Initialization

description: Class Initialization


code: print sniff.__doc__

rawDescription: Scapy - count number of sniffed packets?

description: Scapy - count number of sniffed packets


code:
import numpy as np

class Extractor:
    def extract(self, alist):
        raise NotImplementedError()

class IndexRangeExtractor(Extractor):
    def __init__(self, fromIndex, toIndex):
        self.fromIndex = fromIndex
        self.toIndex = toIndex

    def extract(self, alist):
        return tuple(alist[self.fromIndex:self.toIndex])

class EuclideanDistanceExtractorDecorator(Extractor):
    def __init__(self, innerExtractor):
        self.innerExtractor = innerExtractor

    def extract(self, alist):
        innerResult = self.innerExtractor.extract(alist)
        distance = np.sqrt(sum(x**2 for x in innerResult))

        return innerResult + (distance,)

#...

class ExtractorFactory:
    def __init__(self):
        self.extractors = {
            'position':IndexRangeExtractor(0, 3),
            'velocity':EuclideanDistanceExtractorDecorator(
                IndexRangeExtractor(3, 6)),
            'forces':EuclideanDistanceExtractorDecorator(
                IndexRangeExtractor(6, 9))}

    def createExtractor(self, quantity):
        return self.extractors[quantity]

alist = [1,2,3,4,5,6,7,8,9]
ef = ExtractorFactory()
e1 = ef.createExtractor('position')
e2 = ef.createExtractor('velocity')
e3 = ef.createExtractor('forces')

print e1.extract(alist)
print e2.extract(alist)
print e3.extract(alist)

rawDescription: Code smell - if/else construct

description: Code smell - if/else construct


code:
#! /usr/bin/env python
import bjsonrpc
from bjsonrpc.handlers import BaseHandler
from SRPSocket import SRPSocket
import time

class handler(BaseHandler):
    def time(self):
        return time.time()

socket, key = SRPSocket.SRPSocket('localhost', 1337, 'dht', 'testpass')

connection = bjsonrpc.connection.Connection(socket, handler_factory=handler)

test = connection.call.time()
print test
time.sleep(1)

rawDescription: How to add authentication to a (Python) twisted xmlrpc server

description: Add authentication to a (Python) twisted xmlrpc server


code:
def multiply(num1, num2):
    print num1 * num2 #Notice the print, not return

x = multiply(5, 6)
print x
def multiply(num1, num2):
    return num1 * num2 #Notice the return this time

x = multiply(5, 6)
print x

rawDescription: writing information to a .txt file and getting None as my result

description: Writing information to a .txt file and getting None as my result


rawDescription: why is my code still displaying the old panes?

description: Why is my code still displaying the old panes


code:
#!/usr/bin/python

import functools
import multiprocessing
import mmap
import os
import struct
import sys

FILE = "/tmp/random"  # dd if=/dev/random of=/tmp/random bs=1024k count=1024
SIZE = os.stat(FILE).st_size
BYTES = 2
SKIP = 10

def bymmap(poolsize, n):
    partition = SIZE/poolsize
    initial = n * partition
    end = initial + partition
    sum = 0.0
    unpacks = 0
    with open(FILE, "r") as fd:
        mm = mmap.mmap(fd.fileno(), 0, prot=mmap.PROT_READ)
        for offset in xrange(initial, end, SKIP*BYTES):
            data = mm[offset:offset+BYTES]
            sum += struct.unpack('h', data)[0]
            unpacks += 1
    return (sum, unpacks)

poolsize = int(sys.argv[1])
pool = multiprocessing.Pool(poolsize)
results = pool.map(functools.partial(bymmap, poolsize), range(0, poolsize))
print results
print reduce(lambda x, y: (x[0] + y[0], x[1] + y[1]), results)

rawDescription: Extract specific bytes from a binary file in Python


code:
from bs4 import BeautifulSoup

soup = BeautifulSoup(html)

for row in soup.select('tr.bandingon, tr.bandingoff'):
    print row.get_text()

rawDescription: Regex in Python for TR

description: Regex in Python for TR


code:
#NOTE: take only the first extension
mime2ext = {}
for x in mime2exts_list:
    mime2ext[x[0]] = x[1]

if __name__ == '__main__':
    import os
    import sys

    # this version supports only stdin (part of fileinput.input() functionality)
    lines = sys.stdin.read().split('\n')
    for line in lines:
        filename = line.rstrip()
        output = os.popen('file -bi ' + filename).read()
        mime = output.split(';')[0].lower().strip()
        try: ext = mime2ext[mime]
        except KeyError:
             ext = 'undefined'
        print filename + '.' + ext

rawDescription: How to add file extensions based on file type on Linux/Unix?

description: Add file extensions based on file type on Linux/Unix


code:
print os.path.commonprefix(['/usr/var/log', '/usr/var/security'])
print os.path.commonprefix(['/tmp', '/usr/var'])  # No common prefix: the root is the common prefix

rawDescription: Python: Get relative path from comparing two absolute paths


code:
sent_detector = nltk.data.load('tokenizers/punkt/english.pickle')
print '\n-----\n'.join(sent_detector.tokenize(text.strip()))

rawDescription: Using regular expressions in Python

description: Using regular expressions in Python


rawDescription: python - filter function for nested loop and if condition

description: Python - filter function for nested loop and if condition


code:
import sys
print sys.argv[1]

rawDescription: accessing files in a folder using python

description: Accessing files in a folder using python


code:
from xml.dom import minidom

xml = """\
<target depends="create-build-dir" name="build-Folio">
   <property name="project.name" value="Folio"/>
   <ant antfile="build.xml" dir="Folio/FolioUI" inheritall="false" target="package"/>
   <ant antfile="build.xml" dir="Folio/Folio" inheritall="false" target="package"/>
</target>
"""

def comment_node(node):
    comment = node.ownerDocument.createComment(node.toxml())
    node.parentNode.replaceChild(comment, node)
    return comment

def uncomment_node(comment):
    node = minidom.parseString(comment.data).firstChild
    comment.parentNode.replaceChild(node, comment)
    return node

doc = minidom.parseString(xml).documentElement

comment_node(doc.getElementsByTagName('ant')[-1])

xml = doc.toxml()

print 'comment_node():\n'
print xml
print

doc = minidom.parseString(xml).documentElement

comment = doc.lastChild.previousSibling

print 're-parsed comment:\n'
print comment.toxml()
print

uncomment_node(comment)

print 'uncomment_node():\n'
print doc.toxml()
print

rawDescription: Commenting and uncommenting XML via Python


code:
zebra = 'Yout caretak taking care of something'
words = nltk.word_tokenize(zebra)
for x in words:
    print correct(x),

rawDescription: Python Converting output into sentence

description: Python Converting output into sentence


code:
from BeautifulSoup import BeautifulSoup

data='TEXT I WANT'
soup=BeautifulSoup(data)
t=soup.find('span',{'class':'rating-count'})
print t.text

rawDescription: How to get text from inside a TEXT I WANT in Python

description: Get text from inside a TEXT I WANT in Python


code:
from itertools import chain
for elem in chain(*(x.split(",") for x in strs.splitlines())):
    print elem

rawDescription: How to split on two items in a string?

description: Split on two items in a string


code:
import datetime
import time

user_reg_time = datetime.datetime.strptime("20091129060140", "%Y%M%d%H%m%S")
if datetime.datetime.now() - user_reg_time > datetime.timedelta(weeks=4):
    print "User was registered more than 4 weeks ago"

rawDescription: Python: Convert Varbinary type timestamp to unix time stamp


code:
def data(self, data):
    if data:
        print '... data called ...'
        print repr(data), 'length -' , len(data)

rawDescription: Python XMLParser : When is data() method called


code:
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

    def addNode(self, data):
        if data < self.data:
            if self.left is None:
                self.left = Node(data)
                self.printSubtree()
            else:
                self.left.addNode(data)  # recursively calling addNode method
        else:
            if self.right is None:
                self.right = Node(data)
                self.printSubtree()
            else:
                self.right.addNode(data)

    def printSubtree(self):
        if not self.left is None and not self.right is None:
            print self.left.data, self.data, self.right.data
        elif self.left is None and not self.right is None:
            print None, self.data, self.right.data
        elif not self.left is None and self.right is None:
            print self.left.data, self.data, None
        else:
            print None, self.data, None

if __name__ == '__main__':
    n = Node(5)
    n.printSubtree()
    n.addNode(3)
    n.addNode(2)
    n.addNode(8)
    n.addNode(4)

rawDescription: Best way to print every stage of Binary Tree insertion in Python

description: Print every stage of Binary Tree insertion in Python


code:
import serial, time

try:
    station1 = serial.Serial('/dev/ttyUSB0', 9600)
    print 'station one Is Ready!! Please Show your Card'
except serial.SerialException:
        print 'Station 1 is Down'

while True:
    card_dataa = station1.read(12)
    print card_dataa

rawDescription: Jumbled out put of RFID Tags

description: Jumbled out put of RFID Tags


code:
unique_ids=set(data['Third col'])

while True:
    try:
        print ', '.join([data[e].pop(0) for e in header])
    except IndexError:
        break
print 'Unique IDs:', len(unique_ids)

rawDescription: Python - compare columns in a text file, loop and pop lists


code:
import urllib, json

data = json.loads(urllib.urlopen("http://ip.jsontest.com/").read())
print data["ip"]

rawDescription: Finding network (external) IP addresses using Python


code:
class Child:
   var3 = "whatever"

c = Child()
c.var3 = "something_else"

print c.var3
print Child.var3

rawDescription: Child classes and parent classes in Python


code: print soup.find_all(id='contact1')

rawDescription: Find a html tag using BeautifulSoup in Python


code: d = collections.OrderedDict(sorted(d.items(), key=lambda (key, value): value['b']))

rawDescription: python: sorting an ordered dictionary

description: Python: sorting an ordered dictionary


code:
import ipyparallel as ipp
rc = ipp.Client()  # Create the client that will connect to the ipython engines
lview = rc.load_balanced_view()

@lview.parallel()
def show_polls(user_range):
    from poll.models import Poll
    return list(Poll.objects.filter(user_id__gte=user_range, user_id__lt=user_range+100))

for res in show_polls.map(range(0, 1000, 100)):
    print res

rawDescription: how to run a custom script in parallel with django

description: Run a custom script in parallel with django


code:
import pandas as pd
import numpy as np
df = pd.read_csv('data.csv')
print df.head(len(df))

rawDescription: Pandas Grouping and Reducing DataFrame

description: Pandas Grouping and Reducing DataFrame


code:
class SparseMatrix():
    def __init__(self, x_label, y_label):
        self.data = {}
        for x,y in zip(x_label,y_label):
            print x,y
            self.data[x] = {}
            for attr in y:
                self.data[x][attr] = 1
        return

    def __getitem__(self, index):
        x,y = index
        if type(x) is str:
            if type(y) is str:
                return 1 if y in self.data[x] else 0
            if type(y) is slice:
                return self.data[x].keys()
        if type(x) is slice:
            if type(y) is str:
                res = []
                for key in self.data.keys():
                    if y in self.data[key]:
                        res.append(key)
                return res
            if type(y) is list:
                res = []
                for attr in y:
                    res += self.__getitem__((x,attr))
                return res

rawDescription: python: can I have a sparse matrix representation without (explicitly) using integer indices?

description: Python: can I have a sparse matrix representation without (explicitly) using integer indices


code:
dzip = lambda f, a, b: {key: f(a.get(key, ""), b.get(key, ""))
        for key in set.intersection(set(a.keys()), set(b.keys()))}

print dzip(lambda a,b: a+b,x,y)

rawDescription: Merge values of a dictionary by key based on custom function


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

    def __setattr__(self, name, value):
        if not hasattr(self, name) or getattr(self, name) != value:
            self.on_change()
        super(Container, self).__setattr__(name, value)

    def on_change(self):
        pass

class SomeClass(object):
    def __init__(self, attr):
        self._attr = attr
        self._attr.on_change = self._on_change

    @property
    def attr(self):
        return self._attr

    @attr.setter
    def attr(self,value):
        if self._attr != value:
            self._on_change()
        self._attr = value

    def _on_change(self):
        print "Do some code here every time attr changes"

rawDescription: Catching changes to a mutable attribute in python


code:
for a_movie in root.xpath('/movies/a_movie[studio]'):
    name = a_movie.find('name').text
    studio = a_movie.find('studio')
    print "<studio>" + studio.text + "</studio>"

rawDescription: Use a variable for an Xpath query in Python


code:
#!/usr/bin/env python

''' Weighted means of ranks

    From http://stackoverflow.com/q/29413531/4014959

    Written by PM 2Ring 2015.04.03
'''

from pprint import pprint

#Create a weighted mean function with weights from list/tuple weights
def make_weighted_mean(weights):
    wt = float(sum(weights))
    #A function that calculates the weighted mean of values in seq
    #weighted by the weights passed to make_weighted_mean()
    def wm(seq):
        return sum(w * v for w, v in zip(weights, seq)) / wt
    return wm

#Get the ranks for a list of (class, score) tuples sorted by score
#and return them in a dict
def get_ranks(sd):
    #The first class in the list has rank 1
    k, val = sd[0]
    r = 1
    rank = {k: r}

    for k, v in sd[1:]:
        #Only update the rank number if this value is
        #greater than the previous
        if v > val:
            val = v
            r += 1
        rank[k] = r
    return rank

#Make the weighted mean function
weights = [0.50, 0.25]
weighted_mean = make_weighted_mean(weights)

#Some test data
sorted_dicts = [
    [('class1', 15.17), ('class2', 15.95), ('class3', 15.95), ('class4', 16.0)],
    [('class2', 9.10), ('class3', 9.22), ('class1', 10.60), ('class4', 11.0)]
]
print 'Sorted dicts:'
pprint(sorted_dicts, indent=4)

all_ranks = [get_ranks(sd) for sd in sorted_dicts]
print '\nAll ranks:'
pprint(all_ranks, indent=4)

#Get a sorted list of the keys
keys = sorted(k for k,v in sorted_dicts[0])
#print '\nKeys:', keys

means = [(k, weighted_mean([ranks[k] for ranks in all_ranks])) for k in keys]
print '\nWeighted means:'
pprint(means, indent=4)

rawDescription: Retrieve the ranking of elements in various list to compute the weighted average of their ranking scores Python


code:
test = Test(np.ctypeslib.as_ctypes(array_1d_double))

print text.x[0]
# outputs: 1.0

print test.y[0]
# raises ValueError: NULL pointer access

rawDescription: Access np.array in ctypes struct

description: Access np.array in ctypes struct


code:
image = cv2.imread('gray.jpg')
print image.shape

rawDescription: In OpenCV (Python), why am I getting 3 channel images from a grayscale image?


code:
no_of_lines = 5
lines = ""
for i in xrange(5):
    lines+=input()+"\n"

print lines

rawDescription: How to get multiline input from user

description: Get multiline input from user


code:
class Foo(object):

    def __init__(self):
        self.value = 0

    def keys(self):
        return ['a', 'b']

    def __getitem__(self, key):
        v = self.value
        self.value += 1
        return self.value

foo = Foo()
print dict(foo)
print dict(foo)

rawDescription: allow a shelve object to be pickled in python, pypar and mpich2

description: Allow a shelve object to be pickled in python, pypar and mpich2


code:
import shlex, subprocess
command_line = raw_input()
args = shlex.split(command_line)
print args
p = subprocess.Popen(args) # Success!

rawDescription: Wget problems using python subprocess

description: Wget problems using python subprocess


code:
common, uncommon = lists[0], lists[0]
for clist in lists:
    common = [item for item in common if item in clist]
uncommon = [[item for item in clist if item not in common] for clist in lists ]
print common, uncommon

rawDescription: Python: return list of sequentially occuring common items from lists and also a list of uncommon ones


code:
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.treeview import TreeView, TreeViewNode
from kivy.uix.treeview import TreeViewLabel
from kivy.app import App
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.button import Button

class TreeViewButton(Button, TreeViewNode):
    pass

modGroups = [u'Fruit', u'Fruit', u'Meat', u'Dairy', u'Dairy', u'Fruit']
modItems = [u'Apple', u'Pear', u'Spam', u'Egg', u'Milk', u'Banana']
modDict = dict()
modDictUnique = dict()

def populate_tree_view(tv):
    modDict = zip(modGroups, modItems)
    print modGroups
    print modItems
    for k, v in modDict:
        if k not in modDictUnique:
            modDictUnique[k] = [v]
        else:
            modDictUnique[k].append(v)
    sortedGroups = modDictUnique.keys()
    sortedGroups.sort()
    #print modItems
    #print modDictUnique
    n = tv.add_node(TreeViewLabel(text='Food', is_open=True))
    for group in sortedGroups:
        g = tv.add_node(TreeViewLabel(text='%s' % group), n)
        for item in modDictUnique[group]:
            tv.add_node(TreeViewButton(text='%s' % item), g)

class POSFMApp(App):

    def build(self):
        #for i in range(30):
        #    btn = Button(text=str(i), size=(480, 40),
        #                 size_hint=(None, None))
        #    layout.add_widget(btn)
        tv = TreeView(root_options=dict(text='Tree One'), hide_root=True, indent_level=4)
        tv.size_hint = 1, None
        tv.bind(minimum_height = tv.setter('height'))
        populate_tree_view(tv)
        root = ScrollView(pos = (0, 0))
        root.add_widget(tv)
        return root

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

rawDescription: TreeView in a ScrollView in Kivy - No Scroll


rawDescription: Python multi-dimensional array initialization without a loop


code:
evens = []
odds = []
out = [evens,odds]
for elem in numbers:
    out[elem%2].append(elem)

print evens

rawDescription: Even number check without: if

description: Even number check without: if


code:
import Queue
import threading

# input queue to be processed by many threads
q_in = Queue.Queue(maxsize=0)

# output queue to be processed by one thread
q_out = Queue.Queue(maxsize=0)

# number of worker threads to complete the processing
num_worker_threads = 10

# process that each worker thread will execute until the Queue is empty
def worker():
    while True:
        # get item from queue, do work on it, let queue know processing is done for one item
        item = q_in.get()
        q_out.put(do_work(item))
        q_in.task_done()

# squares a number and returns the number and its square
def do_work(item):
    return (item,item*item)

# another queued thread we will use to print output
def printer():
    while True:
        # get an item processed by worker threads and print the result. Let queue know item has been processed
        item = q_out.get()
        print "%d squared is : %d" % item
        q_out.task_done()

# launch all of our queued processes
def main():
    # Launches a number of worker threads to perform operations using the queue of inputs
    for i in range(num_worker_threads):
         t = threading.Thread(target=worker)
         t.daemon = True
         t.start()

    # launches a single "printer" thread to output the result (makes things neater)
    t = threading.Thread(target=printer)
    t.daemon = True
    t.start()

    # put items on the input queue (numbers to be squared)
    for item in range(10):
        q_in.put(item)

    # wait for two queues to be emptied (and workers to close)
    q_in.join()       # block until all tasks are done
    q_out.join()

    print "Processing Complete"

main()

rawDescription: Learning about Queue module in python (how to run it)


code:
import datetime
import multiprocessing
from multiprocessing import Queue

def worker(work_queue):
    if work_queue.get() == "Start":
            for i in range(1,10):
                if i == 5:
                    # do something
                    print datetime.datetime.now()

def main():
    worker_queues = {}
    for i in xrange(1, 6):
        q = Queue()
        worker_queues[i] = q # one queue per process here
        d = multiprocessing.Process(target = worker, args = (q,))
        d.daemon = True
        d.start()
    for wq in worker_queues.values():
        wq.put("Start")

if __name__ == "__main__":
    main()

rawDescription: Python - Locking a shared resource (Multiprocessing)


code:
def logged(*setting_args, **setting_kwargs):
    no_args = False
    if len(setting_args) == 1 \
        and not setting_kwargs \
        and callable(setting_args[0]):
        # We were called without args
        func = setting_args[0]
        no_args = True

    def outer(func):
        @wraps(func)
        def with_logging(*args, **kwargs):
            print "{} was called".format(func.__name__)
            print "Setting args are: {}".format(setting_args)
            print "Setting keyword args are: {}".format(setting_kwargs)
            return func(*args, **kwargs)
        return with_logging

    if no_args:
        return outer(func)
    else:
        return outer

rawDescription: Python decorator optional argument

description: Python decorator optional argument


code:
def perm(prefix, rest):
    print prefix, rest

    for i in range(len(rest)):
        perm(prefix + [rest[i]], rest[:i] + rest[i + 1:])

perm([], ['a','b','c'])

rawDescription: How to make a shallow copy of a list in Python

description: Make a shallow copy of a list in Python


code:
a = b = [1]  # a and b are both bound to the same list
print a,b    # [1] [1]
b[0] = 2     # modifies the object to which a and b are bound
print a,b    # [2] [2]
b = [0]      # now b is bound to a new list
print a,b    # [2] [0]

rawDescription: Python: sharing a list between threads

description: Python: sharing a list between threads


code:
import yaml

class PrettySafeLoader(yaml.SafeLoader):
    def construct_python_tuple(self, node):
        return tuple(self.construct_sequence(node))

PrettySafeLoader.add_constructor(
    u'tag:yaml.org,2002:python/tuple',
    PrettySafeLoader.construct_python_tuple)

doc = yaml.dump(tuple("foo bar baaz".split()))
print repr(doc)
thing = yaml.load(doc, Loader=PrettySafeLoader)
print thing

rawDescription: How can I add a python tuple to a YAML file using pyYAML?

description: Add a python tuple to a YAML file using pyYAML


code:
def selectItem(a):
    curItem = tree.focus()
    print tree.item(curItem)

rawDescription: python tkinter tree get selected item values

description: Python tkinter tree get selected item values


code:
from math import sqrt

def d(number):
    sum = 1
    for foo in range(2, int(sqrt(number)) + 1):
        if number % foo == 0:
            sum += foo
            sum += number/foo
    return sum

def runit():
    d_value = {}
    amicable_number_sum = 0

    for number in range(2, 10000):
        if not number in d_value.keys():
            d_value[number] = d(number)
        if not d_value[number] in d_value.keys():
            d_value[d_value[number]] = d(d_value[number])
        if number == d_value[d_value[number]] and not number == d_value[number]:
            amicable_number_sum += number
    print amicable_number_sum

import cProfile
cProfile.run('runit()')

rawDescription: Why is my method using lookup table so slow compared to brute force method?


code:
import subprocess

with open('output.txt', 'wb') as f:
    subprocess.check_call(cmd, stdout=f)

# to read line by line
with open('output.txt') as f:
    for line in f:
        print line,

rawDescription: How do I get all of the output from my .exe using subprocess and Popen?

description: Get all of the output from my .exe using subprocess and Popen


code:
import sys

four = eval(sys.argv[4])
print four[1]

rawDescription: How to pass a list and other values as command line arguments?

description: Pass a list and other values as command line arguments


rawDescription: Reshaping Arrays in Numpy with variables


code:
r = xrange(2000, 2005)
r2 = xrange(1, len(r) + 1)
h = zip(r2, r)
print h

rawDescription: How to enumerate a range of numbers starting at 1

description: Enumerate a range of numbers starting at 1


code: print 3 + 1

rawDescription: Return Statements in Recursion

description: Return Statements in Recursion


code:
import wx, wx.lib.newevent, time, sys
from threading import *
size_width = 320
size_height = 240
background_color = (226,223,206)

SomeNewEvent, EVT_SOME_NEW_EVENT = wx.lib.newevent.NewEvent()

class StatusWindow(wx.Frame):
    def __init__(self, parent):
        super(StatusWindow, self).__init__(parent, title='Monitor', size=(size_width, size_height))

        self.Bind(EVT_SOME_NEW_EVENT, self.updateStatus)

        staticBox = wx.StaticBox(self, label='Monitor Status:', pos=(5, 105), size=(size_width - 28, size_height/3))
        self.statusLabel = wx.StaticText(staticBox, label='None', pos=(10, 35), size=(size_width, 20), style=wx.ALIGN_LEFT)
        self.count = 0
        self.InitUI()

        # Set up event handler for any worker thread results
        self.monitor = cMonitor(self)
        self.monitor.start()

    def InitUI(self):
        panel = wx.Panel(self)
        self.SetBackgroundColour(background_color)
        self.Centre()
        self.Show()

    def updateStatus(self, evt):
        self.statusLabel.SetLabel(evt.attr1)

class cMonitor(Thread):
    def __init__(self, wxWindow):
        super(cMonitor, self).__init__()
        self.wxWindow = wxWindow

    def run(self):
        time.sleep(2)
        print 'This is an update'
        #create the event
        evt = SomeNewEvent(attr1="Some event has just occured")
        #post the event
        wx.PostEvent(self.wxWindow, evt)

if __name__ == '__main__':

    app = wx.App()
    window = StatusWindow(None)
    app.MainLoop()

rawDescription: Simple wxPython post event from Process

description: Simple wxPython post event from Process


code:
class MyClass(object):
 def sayhello(self):
  print "Hello World!"

m=MyClass()
getattr(m,"sayhello")()

rawDescription: Python: How do you call a method when you only have the string name of the method?


code:
def drain_seqs(remaining, done=[]):

    if remaining:
        reference = remaining.pop(0)

        if remaining and remaining[0][0] <= reference[1]:
            if remaining[0][1] > reference[1]:
                # It's overlapping
                reference[1] = remaining[0][1]
            # Delete overlapped and embedded
            remaining.pop(0)
            # And insert modified reference at the beginning
            remaining.insert(0, reference)
        else:
            # Next is not overlapped or this is the last one
            # Archive the reference and continue
            done.append(reference)

        drain_seqs(remaining, done)

    return done

sample_list = [[400, 650], [100, 500], [200, 300], [400, 600],
               [700, 800], [750, 900]]

sample_list.sort(key=lambda x: x[0])

print drain_seqs(sample_list)

rawDescription: Biopython: extract sequences not covered by any SeqFeatures


code: print list(islice(primes3(), 10))

rawDescription: Merged iterators produce obscure results


code:
tests = ["aa234",
         "aa234 bbb2b ccc ddd eee ",]
for test in tests:
    results = parser.parseString(test)
    print results.dump()
    print results.first
    print results.second

rawDescription: How to parse this string with pyparsing

description: Parse this string with pyparsing


code:
for image in soup.find_all('img', src=True):
    print image['src']

rawDescription: python regular expression could not find all images files

description: Python regular expression could not find all images files


code:
for i in range(0, len(sample), 5):
    chunk = sample[i:i + 4]  # ignore the 5th byte, a space
    print chunk.decode('utf8')
    if i == 20: break

rawDescription: Split array byte string in Python

description: Split array byte string in Python


code:
import time
from multiprocessing import Process, Manager, Value

def foo(data, name=''):
    print type(data), data.value, name
    data.value += 1

if __name__ == "__main__":
    manager = Manager()
    x = manager.Value('i', 0)
    y = Value('i', 0)

    for i in range(5):
        Process(target=foo, args=(x, 'x')).start()
        Process(target=foo, args=(y, 'y')).start()

    print 'Before waiting: '
    print 'x = {0}'.format(x.value)
    print 'y = {0}'.format(y.value)

    time.sleep(5.0)
    print 'After waiting: '
    print 'x = {0}'.format(x.value)
    print 'y = {0}'.format(y.value)

rawDescription: Shared variable in python's multiprocessing


rawDescription: Python: how to map one list and one list of list together


code:
from urlparse import urljoin

from bs4 import BeautifulSoup
import requests

base_url = 'http://www.computerstore.nl'
curl = ["http://www.computerstore.nl/category/100852/interne-harde-schijven.html?6437=19598"]

session = requests.Session()
for url in curl:
    soup = BeautifulSoup(session.get(url).content)
    links = [urljoin(base_url, item['href']) for item in soup.select("div.product-list a.product-list-item--image-link")]

    for link in links:
        soup = BeautifulSoup(session.get(link).content)
        print soup.find('span', itemprop='name').get_text(strip=True)

rawDescription: Beautifulsoup find_all does not find all


code:
def mkpp(breaks,coefs,*args):
    """
    Takes in the breaks, coefs, and optionally (d) then creates a pp from the
    PiecePoly class and constructs the polynomial
    Returns: the constructed polynomial
    """
    if len(args)==1:
        d = np.transpose(args[0])
    else:
        d = 1
    sum=0
    try:
        #Just make sure coefs is not a 4D matrix
        for i in range(len(coefs)):
            for j in range(len(coefs[i])):
                sum = sum+len(coefs[i][j])
    except:
        #First try to count a 2D coefs array this should be the one that works
        try:
            for i in range(len(coefs)):
                sum = sum+len(coefs[i])
        #Coefs must be 1 dimensional
        except:
            sum = len(coefs)

    dlk = sum
    l = len(breaks)-1

    try:
        if len(d) > 1:
            prod = 0
            for i in range(len(d)):
                prod = prod*d[i]
            dl = prod*l
        else:
            dl = d*l
    except:
        dl = d*l

    k = dlk/dl+100*(math.pow(2,-52))
    if k<0:
        k = math.ceil(k)
    else:
        k = math.floor(k)

    if k<=0 or (dl*k!=dlk):
        print "ERROR: MISMATCH PP AND COEF"
        return None

    pp = PiecePoly()
    pp.form = 'pp'
    pp.breaks = np.reshape(breaks,1,l+1)
    pp.coefs = np.reshape(coefs,dl,k)
    pp.order = k
    pp.dim = d

    return pp

rawDescription: Converting matlabs mkpp to python

description: Converting matlabs mkpp to python


code:
import re

packageDetails = "package: name='com.example.tracker' versionCode='1' versionName='1.0'"
name = re.search("name='(.*?)'", packageDetails).group(1)
versionCode = re.search("versionCode='(.*?)'", packageDetails).group(1)
versionName = re.search("versionName='(.*?)'", packageDetails).group(1)

print "package name is :", name
print "version code is :", versionCode
print "version name is :", versionName

rawDescription: Extract a substring from a string based on the position of character - Python


code:
def fib(end):
    a, b = 0, 1
    for _ in xrange(end):
        a, b = b, a+b
        yield str(a)

counter = input("Please enter the term you wish to end at: ")
print '\n\n'.join(fib(counter))

rawDescription: Checking efficiency of a Python Fibonacci Sequence Generator?


code:
import re

all = [sub.start() for sub in re.finditer('\|', fullstr)]
print fullstr[all[0]+1:all[1]]

rawDescription: Getting Substring in Python

description: Getting Substring in Python


code:
from random import randint

lists_of_random_numbers = [[randint(1, 10) for x in range(randint(2,4))] for _ in range(5)]
reversed_lists_of_random_numbers = [x[::-1] for x in lists_of_random_numbers]

print lists_of_random_numbers
print reversed_lists_of_random_numbers

rawDescription: Reversing the elements of lists which are in list


code:
# chattyimport.py
import imp
import sys
import types

class ChattyType(type):
    def __init__(cls, name, bases, dct):
        print "Class init", name
        super(ChattyType, cls).__init__(name, bases, dct)

class ChattyImporter(object):

    __chatty_for__ = []

    def __init__(self, path_entry):
        pass

    def find_module(self, fullname, path=None):
        if fullname not in self.__chatty_for__:
            return None
        try:
            if path is None:
                self.find_results = imp.find_module(fullname)
            else:
                self.find_results = imp.find_module(fullname, path)
        except ImportError:
            return None
        (f,fn,(suf,mode,typ)) = self.find_results
        if typ == imp.PY_SOURCE:
            return self
        return None

    def load_module(self, fullname):
        #print '%s loading module %s' % (type(self).__name__, fullname)
        (f,fn,(suf,mode,typ)) = self.find_results
        data = f.read()
        if fullname in sys.modules:
            module = sys.modules[fullname]
        else:
            sys.modules[fullname] = module = types.ModuleType(fullname)

        module.__metaclass__ = ChattyType
        module.__file__ = fn
        module.__name__ = fullname
        codeobj = compile(data, fn, 'exec')
        exec codeobj in module.__dict__
        return module

class ChattyImportSomeModules(ChattyImporter):
    __chatty_for__ = 'foo bar'.split()

sys.meta_path.append(ChattyImportSomeModules(''))

import foo # prints 'Class init Foo'
import bar # prints 'Class init Bar'
import baaz

rawDescription: python modify __metaclass__ for whole program

description: Python modify __metaclass__ for whole program