code:
l = links[0]
type(l)
print l.split
None()   # :)

rawDescription: TypeError : 'NoneType' object not callable when using split in Python with BeautifulSoup


code:
df = []

for one_file in folder:
        with open(one_file) as f:
            df.append( pd.read_csv(f) )

print df[0]
print df[1]
# etc.

rawDescription: Python: Multiple CSVs

description: Python: Multiple CSVs


code:
from scipy.optimize import brentq

# This function should recursively find ALL the roots in the interval
# and return them ordered from smallest to largest.

from scipy.optimize import brentq
def find_all_roots(f, a, b, pars=(), min_window=0.01):
    try:
        one_root = brentq(f, a, b, pars)
        print "Root at %g in [%g,%g] interval" % (one_root, a, b)
    except ValueError:
        print "No root in [%g,%g] interval" % (a, b)
        return [] # No root in the interval

    if one_root-min_window>a:
        lesser_roots = find_all_roots(f, a, one_root-min_window, pars)
    else:
        lesser_roots = []

    if one_root+min_window<b:
        greater_roots = find_all_roots(f, one_root+min_window, b, pars)
    else:
        greater_roots = []

    return lesser_roots + [one_root] + greater_roots

rawDescription: root finding in python

description: Root finding in python


code:
import ruamel.yaml
from ruamel.yaml.scalarstring import PreservedScalarString
import sqlparse

sql = ("SELECT DISTINCT p.id_product, "
       "p.price AS price, "
       "sp.reduction AS discount, "
       "sp.reduction_type AS discount_type, "
       "pl.description_short AS description "
       "FROM ....")
fsql = sqlparse.format(sql, reindent=True, keyword_case="upper").encode('utf-8')

app_config = dict(sql=PreservedScalarString(fsql))
print ruamel.yaml.dump(app_config, Dumper=ruamel.yaml.RoundTripDumper)

rawDescription: Save SQL to YAML as is

description: Save SQL to YAML as is


code:
my_con = MyContainer([{"a": 1, "b": 2}, {"a": 3, "b": 4}])
for val1, val2 in my_con:
    print "a = %s; b = %s" % (val1, val2)

rawDescription: python iterating list of dicts

description: Python iterating list of dicts


code:
def sepbin2(x, y, classes_number=100, log_scale=True):
    if log_scale:
        if x[0]<=0:
            print 'Warning: zero value in array about to be log-scaled. Ignoring it.'
            x=x[1:]
            y=y[1:]
        bins=np.logspace(np.log(x[0]), np.log(x[-1]), classes_number+1, base=np.e)
    else:
        bins=np.linspace(x[0], x[-1], classes_number+1)
    # pre-allocate return values
    xsm = np.zeros(classes_number)
    ysm = np.zeros(classes_number)
    # find elements in each bin
    for i in range(classes_number):
        if i == classes_number - 1:
            sel = bins[i] <= x
        else:
            sel = (bins[i] <= x) & (x < bins[i+1])
        xsm[i] = np.mean(x[sel])
        ysm[i] = np.mean(y[sel])
    return xsm, ysm

rawDescription: Optimized function to separate arrays into bins


rawDescription: auto-name keyword arguments after variable names

description: Auto-name keyword arguments after variable names


code:
dictlist = [{'url': 'google.com', 'a': 10, 'content': 'google', 'd': 80, 'f': 1, 'lock': 'dd'},
            {'url': 'fb.com', 'z': 25, 'content': 'google', 'd': 60, 'p': 1, 'a': 19}]

result = []
sumlist = ['a', 'z', 'd']
for d in dictlist:
    result.append({'newurl': d['url'],
                   'newtitle': d['content'],
                   'sumvalue': sum(d.get(item, 0) for item in sumlist)})

print result

rawDescription: get keys value's from dictionary in python

description: Get keys value's from dictionary in python


code:
for d in dict_list:
    print d['a']

rawDescription: Refer to dictionary element in 'for' loop


code:
@app.after_request
def after(response):
  # todo with response
  print response.status
  print response.headers
  print response.get_data()
  return response

rawDescription: Debugging a request/response in Python flask


code:
import pyparsing as pp

src1 = '''
Test("This is a long string")
'''

src2 = '''
Test("This is a long"
    "string")
'''

src3 = '''

Test("This is a long" \\
     "string")
'''

_lp = pp.Suppress('(')
_rp = pp.Suppress(')')
_str = pp.QuotedString('"')
_slash = pp.Suppress(pp.Optional("\\"))
_multiline_str = pp.Combine(pp.OneOrMore(_str + _slash), adjacent=False)

func = pp.Word(pp.alphas)

function = func + _lp + _multiline_str + _rp

print src1
print function.parseString(src1)
print '-------------------------'
print src2
print function.parseString(src2)
print '-------------------------'
print src3
print function.parseString(src3)

rawDescription: Python/Pyparsing - Multiline quotes

description: Python/Pyparsing - Multiline quotes


code:
def is_close(l):
    for n in l:
        c = sum([1 for x in l if x >= 0.8 *n and x <= 1.2 * n])
        if c >= 0.7 * len(l):
            return True
    return False

print is_close([1,7,8,9])
print is_close([3,4,100,101,102,103,104,105])
print is_close([1,8,9])
print is_close([1,10])
print is_close([100,200,300,400,500])

rawDescription: Given a list of integers, determine if 70% of the values are within 20% of one of the values


code:
def namedlist(name, *attrs):
    """Create a named list class named `name` with attributes `attrs`.
       `attrs` must be strings representing valid Python identifiers.
    """
    class MutableEfficientNamedList(object):
        __slots__ = attrs

        def __init__(self, *values):
            for k, v in zip(self.__slots__, values):
                setattr(self, k, v)

        def __getitem__(self, i):
            return getattr(self, self.__slots__[i])

        def __setitem__(self, i, v):
            return setattr(self, self.__slots__[i], v)

        def __repr__(self):
            return '{}({})'.format(type(self).__name__,
                ', '.join(repr(getattr(self, s)) for s in self.__slots__))

    MutableEfficientNamedList.__name__ = name
    return MutableEfficientNamedList

MyList = namedlist('MyList', 'foo', 'bar', 'baz')
nl = MyList(1, 2, 3)
print nl  # MyList(1, 2, 3)
print nl.bar  # 2
print nl[1]  # 2

rawDescription: python - which data structure to use as an array of dicts?

description: Python - which data structure to use as an array of dicts


code:
for name, section in load_sections('/tmp/example'):
    print name, section

rawDescription: How can I parse a formatted file into variables using Python?

description: Parse a formatted file into variables using Python


code: print l5

rawDescription: python list average over bin sizes

description: Python list average over bin sizes


code:
import myModule
pValue = myModule.doSomething()
print pValue

rawDescription: Python for ios interpreter

description: Python for ios interpreter


code:
for row in cursor.fetchall():
  r = reg(cursor, row)
  print r.a

rawDescription: presto fetch data

description: Presto fetch data


code:
import sys

for i in range(len(sys.argv)):
   print str(i) + ": " + sys.argv[i]

rawDescription: Running a Python program from Go

description: Running a Python program from Go


code:
values = [3,6,7,8]
list1 = [2,5]
list2 = [4,8]

print [[i for i in values if x<i<y] for x,y in zip(list1, list2)]

rawDescription: Grouping values into lists based on parent list


code: print sorted(record.items(), key=lambda a: a[1])

rawDescription: Profiling in Python: Who called the function?


code:
string = '  Will   Ferrell  ( Nick\tHalsey ) , Rebecca Hall (Samantha), Michael\fPena (Frank Garcia)'

import re
pat = re.compile(r'([^(]+)\s*\(([^)]+)\)\s*(?:,\s*|$)')

def nws(s):
    """normalize white space.  Replaces all runs of white space by a single space."""
    return " ".join(w for w in s.split())

lst = [tuple(nws(item) for item in t) for t in pat.findall(string)]

print lst # prints: [('Will Ferrell', 'Nick Halsey'), ('Rebecca Hall', 'Samantha'), ('Michael Pena', 'Frank Garcia')]

rawDescription: Extracting contents of a string within parentheses


code:
import os
import sys

class Renamer:
    def start(self, args):
        os.chdir(args[1])
        dirs = os.listdir(".")

        for dir in dirs:
            try:
                os.rename(dir + "\\" + args[2], dir + "\\" + dir + "." + args[3])
                print "Renamed file in directory: " + dir
            except Exception:
                print "Couldn't find file to rename in directory: " + dir

Renamer().start(sys.argv)

rawDescription: Renaming a file on a remote file server in C# / Python


code:
texts = []
for item in header1.next_siblings:
    if getattr(item, 'name') == 'h4' and item.text == 'Header2':
        break

    try:
        texts.append(item.text)
    except AttributeError:
        texts.append(item)

print ''.join(texts)

rawDescription: Getting multiple blocks of text between tags


code:
import numpy as np
np.random.seed(0)
# Make a random masked array
ar = np.ma.array(np.round(np.random.normal(50, 10, 20), 1),
                     mask=np.random.binomial(1, .2, 20)).reshape((4,5))
# Sort the array from lowest to highest, with a flattened index
sorted_ind = ar.argsort(axis=None)

tmp = ar.flatten()[sorted_ind]
print sorted_ind[np.ma.where(tmp<=51.0)]
print sorted_ind[np.ma.where(tmp>=51.0)]

rawDescription: NumPy: Find sorted indices from a masked 2D array above and below a threshold


code:
for thestring in listdata:
    firstcomma = thestring.find(',')
    havename = thestring.find(name, 0, firstcomma)
    if havename >= 0:
        print "found name:", thestring[:firstcomma]

rawDescription: Confusion about string find?

description: Confusion about string find


code:
def g(x,y):
    return x+y
def g(x,y):
    return x*y
x,y=6,7
print (g(x,y))

def hello():
    return 'hello'
def hello():
    return 'bye'

print hello()

def withone(word):
    return word

def withone():
    return 1==1

print withone('ok')

rawDescription: Method Overriding?

description: Method Overriding


code:
#!/usr/bin/env ipython

import os
from time import time
import numpy as np

temp = 'temp'
if not os.path.exists(temp):
    os.makedirs(temp)
    print 'temp dir created'
os.chdir(temp)

nx = ny = 1200
nof = 1000
print '\n*** Writing random data to files ***\n'
t1 = time(); t2 = 0; t3 = 0
for i in range(nof):
    if not i%10:
        print str(i),
    tt = time()
    data = np.array(np.random.rand(nx*ny), dtype=np.float32)
    t2 += time()-tt
    fn = '%d.bin' %i
    tt = time()
    f = open(fn, 'wb')
    f.write(data)
    f.close
    t3 += time()-tt
print '\n*****************************'
print 'Total time: %f seconds' %(time()-t1)
print '%f seconds (on average) per random data production' %(t2/nof)
print '%f seconds (on average) per file write' %(t3/nof)

print '\n*** Reading random data from files ***\n'
t1 = time(); t3 = 0
for i,fn in enumerate(os.listdir('./')):
    if not i%10:
        print str(i),
    tt = time()
    f = open(fn, 'rb')
    data = np.fromfile(f)
    f.close
    t3 += time()-tt
print '\n*****************************'
print 'Total time: %f seconds' %(time()-t1)
print '%f seconds (on average) per file read' %(t3/(i+1))

# cleen up:
for f in os.listdir('./'):
    os.remove(f)
os.chdir('../')
os.rmdir(temp)

rawDescription: Plotting thousands of files with python

description: Plotting thousands of files with python


code: print eval('2 + 4')

rawDescription: Math operations from string

description: Math operations from string


code:
xl = []
while True:
    x = raw_input("enter > ")
    xl.append(x)
    print xl

rawDescription: How to keep adding inputs into a list

description: Keep adding inputs into a list


code:
lens = [3, 5, 10, 1]
for i, l in enumerate(lens):
  print '%02d:%s' % (i + 1, '*' * l)

rawDescription: How to print a horizontal line that is as long as the user inputs it

description: Print a horizontal line that is as long as the user inputs it


code:
a,b=float(3),2
print a/b

rawDescription: In Python 2.* division which has more overhead?


code:
for xrange, yrange, zrange in product(rangeList, repeat=3):
    print xrange, yrange, zrange

rawDescription: Python itertools: Best way to unpack product of product of list of lists


code:
from itertools import takewhile
for s in takewhile(lambda x: x.count(" ") < 3, abc):
    print s

rawDescription: Python: printing elements from a list till a specific element


code:
def chartoupper(instring, inchar):
    string = instring.replace(inchar, inchar.upper())
    return string

promt='enter word:\n'
originalString=raw_input(promt)
promt='enter a character (note that only the 1st character will be used):\n'
upperCharacter=raw_input(promt)[0]
newstring=chartoupper(originalString, upperCharacter)

print originalString
print upperCharacter
print newstring

rawDescription: uppercasing a character in string with a letter of users choosing

description: Uppercasing a character in string with a letter of users choosing


code:
A= [4,2,7,3,9,1]

def rev(A):
    if len(A)==1:
        return A[0]
    lst = rev(A[:-1])
    return str(A[-1]) + ';' + str(lst)

print rev(A)

rawDescription: Write a recursive function called rev, which reverses the order of elements in an array


code:
state = None
def with_closure(f):
     def helper(*args, **kwargs):
        # state is in scope for this function
        print "Current state is: {}.".format(state)
        return f(*args, **kwargs)
     return helper

@with_closure
def foo():
    return "something"

rawDescription: variables as decorator arguments

description: Variables as decorator arguments


code:
import re, string, timeit

clean = re.compile(r'[^a-zA-Z0-9_-]')
keep = string.ascii_letters + string.digits + '_-'
keep_set = set(string.ascii_letters + string.digits + '_-')
test = '$pam and_Eggs##-!'
cur_encoding_bytes = 256 # 8 for UTF-8, in Python 3 this would be different for Unicode
all_else = ''.join(chr(i) for i in range(cur_encoding_bytes) if chr(i) not in keep_set) # taken from http://stackoverflow.com/a/3588485/1219006

def clean1(s):
    return ''.join(x for x in s if x in keep)

def clean1_filter(s):
    return filter(keep.__contains__, s)

def clean1_filter_set(s):
    return filter(keep_set.__contains__, s)

def clean2(s):
    return clean.sub('', s)

def clean3(s):
    return s.translate(None, all_else)

print timeit.timeit('clean1(test)', 'from __main__ import clean1, test')
print timeit.timeit('clean1_filter(test)', 'from __main__ import clean1_filter, test')
print timeit.timeit('clean1_filter_set(test)', 'from __main__ import clean1_filter_set, test')
print timeit.timeit('clean2(test)', 'from __main__ import clean2, test')
print timeit.timeit('clean3(test)', 'from __main__ import clean3, test')

rawDescription: With Python, what is the most efficient way to remove all char from string except a-z, A-Z, 0-9, _, and -?


code:
import numpy as np
import pandas as pd

np.random.seed(0) # so we can all play along at home

categories = list('abc')
categories = categories * 4
data_1 = np.random.randn(len(categories))
data_2 = np.random.randn(len(categories))

df = pd.DataFrame({'category': categories, 'data_1': data_1, 'data_2': data_2})

import traceback
import inspect
import itertools

def f(x):
    flag = True
    stack = itertools.dropwhile(lambda x:"#stop here" not in x,
                                traceback.format_stack(inspect.currentframe().f_back))
    print "*"*20
    print x
    print type(x)
    print
    print "\n".join(stack)
    return x

df.groupby(['category'])[['data_1', 'data_2']].transform(f) #stop here

rawDescription: What Pandas data type is passed to transform or apply in a groupby


rawDescription: python string including double quote character

description: Python string including double quote character


code:
x = str(x)
dec_index = x.find('.')
tenth_index = dec_index + 1
tenth_place = x[tenth_index] # will be a string of length 1
should_round_up = 5 + tenth_place.find('5') + tenth_place.find('6') + tenth_place.find('7') + tenth_place.find('8') + tenth_place.find('9')

print int(x[0:dec_index]) + should_round_up

rawDescription: Round to whole numbers without using conditional statements in Python - Logic


code:
from bs4 import BeautifulSoup
data = '
 s.r.o., small
'
soup = BeautifulSoup(data)
print soup.find('a').contents[0]

rawDescription: BS4: Getting text in tag

description: BS4: Getting text in tag


code:
from collections import OrderedDict
import json

allsites = [
    {
        'A5': 'G',
        'A10': 'G',
        'site': 'example1.com',
        'A1': 'G'
    },
    {
        'A5': 'R',
        'A10': 'Y',
        'site': 'example2.com',
        'A1': 'G'
    }
]

sort_order = ['site', 'A1', 'A5', 'A10']
allsites_ordered = [OrderedDict(sorted(item.iteritems(), key=lambda (k, v): sort_order.index(k)))
                    for item in allsites]

data = {'Author': "joe", 'data': allsites_ordered}
print json.dumps(data, indent=4, separators=(',', ': '))

rawDescription: How to custom-sort a list of dict to use in json.dumps

description: Custom-sort a list of dict to use in json.dumps


code:
inputs = ['e', 'f', 'g', 'h']
weights = [10, 30, 50, 10]

print myRand(inputs, weights)

rawDescription: Returning a value at random based on a probability weights


code:
import scraperwiki
import simplejson
import urllib2

QUERY = 'meetup'
RESULTS_PER_PAGE = '100'
NUM_PAGES = 10

for page in range(1, NUM_PAGES+1):
    base_url = 'http://search.twitter.com/search.json?q=%s&rpp=%s&page=%s' \
         % (urllib2.quote(QUERY), RESULTS_PER_PAGE, page)
    try:
        results_json = simplejson.loads(scraperwiki.scrape(base_url))
        for result in results_json['results']:
            #print result
            data = {}
            data['id'] = result['id']
            data['text'] = result['text']
            data['location'] = scraperwiki.geo.extract_gb_postcode(result['text'])
            data['from_user'] = result['from_user']
            data['created_at'] = result['created_at']
            if data['location']:
                print data['location'], data['from_user']
                scraperwiki.sqlite.save(["id"], data)
    except:
        print 'Oh dear, failed to scrape %s' % base_url
        break

rawDescription: ScraperWiki/Python: filtering out records when property is false


code:
from bisect import bisect_left

# Based on code in the SortedCollection recipe:
#   http://code.activestate.com/recipes/577197-sortedcollection/
def insert(seq, keys, item, keyfunc):
    """Insert an item into the sorted list using separate corresponding
       keys list and a keyfunc to extract key from each item.
    """
    k = keyfunc(item)  # get key
    i = bisect_left(keys, k)  # determine where to insert item
    keys.insert(i, k)  # insert key of item in keys list
    seq.insert(i, item)  # insert the item itself in the corresponding spot

# initialize data and keys lists
data = [('red', 5), ('blue', 1), ('yellow', 8), ('black', 0)]
data.sort(key=lambda r: r[1]) # sort data by key value
keys = [r[1] for r in data]   # initialize keys list
print data
# --> [('black', 0), ('blue', 1), ('red', 5), ('yellow', 8)]

insert(data, keys, ('brown', 7), keyfunc=lambda x: x[1])
print data
# --> [('black', 0), ('blue', 1), ('red', 5), ('brown', 7), ('yellow', 8)]

rawDescription: Python: Using bisect.insort with key

description: Python: Using bisect.insort with key


code:
stuff = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']

for item in dragonLoot:
    stuff[item] = stuff.get(item, 0) + 1

print stuff

rawDescription: Value in dict won't let me change it

description: Value in dict won't let me change it


code:
import csv
from collections import defaultdict

with open('input.csv') as csv_input:
    read_csv = csv.reader(csv_input)
    values = defaultdict(lambda : [0,0])
    for row in read_csv:
        values[row[0]][0] += float(row[1]) + float(row[2])
        values[row[0]][1] += float(row[2])
print values

rawDescription: Python: Adding multiple values to a dictionary key


code: print '\n'.join(df.apply(func, axis=1))

rawDescription: How do convert a pandas/dataframe to XML?


code:
for tup in itertools.product(range(3), [2], range(3)):
    print tup

rawDescription: Python: multidimensional dict

description: Python: multidimensional dict


code:
from datetime import datetime
c_time = '2015-06-08 23:13:57'
c_time_converted = datetime.strptime(c_time,"%Y-%m-%d %H:%M:%S")
print c_time_converted
print c_time_converted.time()

rawDescription: having issues with python time object

description: Having issues with python time object


code:
for note in root.xpath('//note'):  # Locate all tags under the root named 'note'. There's one.
    for child in note.getchildren():
        print child.tag, repr(child.text)

rawDescription: Extracting each instance of a group of tags from an xml file and Parsing as a Column


code:
import tokenize
from itertools import count
import re

with open('your_file') as fin:
    output = []
    tokens = tokenize.generate_tokens(fin.readline)
    for num, val in (token[:2] for token in tokens):
        if num == tokenize.STRING:
            val = re.sub('{}', lambda L, c=count(): '{{{0}}}'.format(next(c)), val)
        output.append((num, val))

print tokenize.untokenize(output) # write to file instead...

rawDescription: String formatting without index in python2.6


code:
#http://www.parallelpython.com/

import sys,time,pp
import numpy

n=int(raw_input("Enter the value of n: "))
#Enter any integer value

#Define is_low function as a filter
def is_low(n):
    if n <= 20:
       return True
    if n > 20:
       return False

def expansion(n):
    return (numpy.arange(n))

def task(n):
    if is_low(n)==True:
       return (expansion(n))
    if is_low(n)==False:
       return "SORRY"

ppservers=()
if len(sys.argv) > 1:
    ncpus = int(sys.argv[1])
    job_server = pp.Server(ncpus, ppservers=ppservers)
else:
    job_server = pp.Server(ppservers=ppservers)

print job_server.get_ncpus()
job1=job_server.submit(task,(n,),(is_low,expansion,),("numpy","sys",))
result=job1()
print result

ppservers = ()

if len(sys.argv) > 1:
    ncpus = int(sys.argv[1])
    job_server = pp.Server(ncpus, ppservers=ppservers)
else:
    job_server = pp.Server(ppservers=ppservers)

print "Starting pp with", job_server.get_ncpus(), "workers"

start_time = time.time()
inputs = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,21,22,23,24,25,26)
jobs = [(input, job_server.submit(task,(input,),(expansion,is_low,),  ("numpy","sys","time",))) for input in inputs]
for input, job in jobs:
    print "Expansion for the below",input,"is",job()

print "Time elapsed: ", time.time() - start_time, "s"
job_server.print_stats()

rawDescription: How to limit the parallel python code for specific/ required value

description: Limit the parallel python code for specific/ required value


code:
data = '''2,2,2
3,3,3
6,6,6
0,0,3
4,4,4
0,0,6'''

from StringIO import StringIO

import pandas as pd

df = pd.DataFrame.from_csv(StringIO(data), index_col=None, header=None)

print '\n all rows \n'
print df

print '\n group by column 2 \n'

for name, gr in df.groupby(2):
    print '\n name: %s \n' % name
    print gr
    print 'indexes:', gr.index

rawDescription: CSV rows and columns calculating

description: CSV rows and columns calculating


code: print ord("c") - (ord("a")) + 1

rawDescription: python: How do I assign values to letters?

description: Python: How do I assign values to letters


code:
eqs = soup.find_all('div', {'style': 'margin:7px 5px 0px;vertical-align:top;text-align:center;display:inline-block;line-height:normal;width:120px;'})

for equipment in eqs:
    print equipment.getText()

rawDescription: BeautifulSoup Scraping Returning Duplicate Results


code: print sumsqrt # print the address of the object in memory, similar to id(sumsqrt)

rawDescription: my script prints the function without calling the function

description: My script prints the function without calling the function


rawDescription: Printing first field and (and only) matching fields in record, using awk


code:
realms= data['realms']
realm_from_name= dict( [(realm['name'], realm) for realm in realms])
print realm_from_name['Silvermoon']['population']
print realm_from_name['Silvermoon']['wintergrasp']['controlling-faction']

rawDescription: Navigating Multi-Dimensional JSON arrays in Python


rawDescription: Regexp: symbols missing in group

description: Regexp: symbols missing in group


code:
def scan(s1, s2):
    # Find the longest match where s1 starts with s2
    # Returns None if no matches
    l = len(s1)
    while 1:
        if not l:
            return None
        elif s1[:l] == s2[:l]:
            return s1[:l]
        else:
            l -= 1

def contains(s1, s2):
    D = {} # Remove duplicates using a dict
    L1 = s1.split(' ')
    L2 = s2.split(' ')

    # Don't add results which have already
    # been processed to satisfy example #1!
    DProcessed = {}

    for x in L1:
        yy = 0
        for y in L2:
            if yy in DProcessed:
                yy += 1
                continue

            # Scan from the start to the end of the words
            a = scan(x, y)
            if a:
                DProcessed[yy] = None
                D[a] = None
                break

            # Scan from the end to the start of the words
            a = scan(x[::-1], y[::-1])
            if a:
                DProcessed[yy] = None
                D[a[::-1]] = None
                break
            yy += 1

    return list(D.keys())

print contains("12 November 2010 - 1 visitor",
               "6 July 2010 - 100 visitors")
print contains("Welcome, John!",
               "Welcome, Peter!")
print contains("Welcome, Sam!",
               "Welcome, Tom!")

rawDescription: How to detect identical part(s) inside string?

description: Detect identical part(s) inside string


code:
original_foo = f.foo
with patch.object(f, 'foo') as mock_foo:
    def side_effect(a):
        print "mock foo", a
        return original_foo(a*2)
    mock_foo.side_effect = side_effect

    f.foo(2)

rawDescription: How to call mocked method in Python mock

description: Call mocked method in Python mock


code:
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10, 2), columns=list('AB'))
df['Tenant'] = np.random.choice(['Babar', 'Rataxes', ''], 10)
print df

rawDescription: Python Pandas DataFrame remove Empty Cells


code:
mylist = []
for y,x in zip(range(0,4,1),range(0,8,2)):
    mylist.append(str(x+y))
print mylist

rawDescription: How to convert a for loop output into a list (python)

description: Convert a for loop output into a list (python)


code:
from fractions import Fraction
x = Fraction('1/4')
print float(x)  # 0.25

rawDescription: Flexible numeric string parsing in Python


code:
chr(157)
print repr(chr(157)) # equivalent to the above
print chr(157)

rawDescription: encrypting a string via binary manipulation

description: Encrypting a string via binary manipulation


code:
import json

input = '{\"0001\": [\"Oliver\", \"Jackson\", \"newlog01\", \"newpass01\"], \"0002\": [\"Samuel\", \"Depp\", \"samuel\", \"pass321\"]}'

data = dict()

try:
    data = json.loads(input)
except ValueError as e:
    print e

print 'ID - NAME - LASTNAME - USER - PASSWORD'
for key in sorted(data.keys()):
    print ' - '.join([key] + data[key])

rawDescription: Creating a .txt file from a dictionary

description: Creating a .txt file from a dictionary


code:
import subprocess

proc = subprocess.Popen(["cat", "/etc/services"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print "program output:", out

rawDescription: Assign output of os.system to a variable and prevent it from being displayed on the screen


code:
from collections import deque
from datetime import datetime

import transaction
from zope.app.component.hooks import setSite
from Testing.makerequest import makerequest
from AccessControl.SecurityManagement import newSecurityManager

from my.newproduct.types import ArchetypesContentType

site_id = 'Plone'     # adjust to match your Plone site object id.
admin_user = 'admin'  # usually 'admin', probably won't need adjusting
app = makerequest(app)
site = app[site_id]
setSite(site)
user = app.acl_users.getUser(admin_user).__of__(site.acl_users)
newSecurityManager(None, user)

def treeWalker(root):
    # stack holds (parent, id, obj) tuples
    stack = deque([(None, None, root)])
    while stack:
        parent, id, next = stack.popleft()
        try:
            stack.extend((next, id, child) for id, child in next.objectItems())
        except AttributeError:
            # No objectItems method
            pass
        yield parent, id, next

count = 0
for parent, id, obj in treeWalker(site):
    if isinstance(obj, ArchetypesContentType):
        print 'Found content type object {} at {}'.format(id, '/'.join(object.getPhysicalPath()))
        obj._p_changed = True  # mark it as changed, force a commit
        count += 1
        if count % 100 == 0:
            # flush changes so far to disk to minimize memory usage
            transaction.savepoint(True)
            print '{} - Processed {} items'.format(datetime.now(), count)

transaction.commit()

rawDescription: Looping through broken objects in Plone

description: Looping through broken objects in Plone


rawDescription: Ansible lineine file with regex is adding new line when it shouldn't


code:
N=5
for n in range(0,N):
    x=[1]*n
    for i in range(0,(n+1)):  # len(x) == n
        for j in range(i,(n+1)):
            print i, j - i, n - j

rawDescription: General bars and stars

description: General bars and stars


code:
import sys
_stdout = sys.stdout

class MyStream(object):
    def __init__(self, target):
        self.target = target

    def write(self, s):
        s = 'Foo : {!r}'.format(s)
        self.target.write(s)
sys.stdout = MyStream(sys.stdout)
print 'Hi'
sys.stdout = _stdout   # return print to its old behavior

rawDescription: Overwrite built-in function

description: Overwrite built-in function


code:
import re

player_string = "player a 34 45 56 player b 38 93 75 playerc 39 29 18 playerd 38 98"

pattern = re.compile(r"([\w ]*?)\s+(\d+)\s+(\d+)\s+(\d+)")
matches = pattern.findall(player_string)
d = {}
for m in matches :
    print m
    d[m[0].strip()] = m[1:]

print d

rawDescription: Python Sorting Regular Expression

description: Python Sorting Regular Expression


code:
def main_loop():
    while not shutdown_flag:
        collect_data() # contains some print "data" statements
        time.sleep(5)
        import threading
        print threading.enumerate()
    print "done (killed)"

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


code:
if sums[i-1] != sums[i] and sums[i] == sums[i+1]:
    if n<t:
        idx += 1
        n = sums[i]
        print "%10d"%(n)
    else:
        break

rawDescription: How to fix Python If Statement for finding Taxicab numbers?

description: Fix Python If Statement for finding Taxicab numbers


code:
def exit():
    return "Exit"

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

def func():
    for ind, element in enumerate(arr, 1):
        print element
        if ind % 3 == 0:
            print(exit())

rawDescription: Break from array and run another function in python


code: print words.count('hello')

rawDescription: I want to create a program that can analyze downloaded text file in python


code:
items = ['a','b','c']
def print_kwargs(**kwargs):
  for key,value in kwargs.iteritems():
    print "%s = %s" % (key, value)
d = {items[0] : items[1]}
print_kwargs(**d)

rawDescription: Arbitrary number of search terms

description: Arbitrary number of search terms


code:
from numpy import array
image_array = array([Image.open(f, 'r').size for f in filenames])
print image_array.argmax()

rawDescription: Find the largest image dimensions from list of images


code:
import csv

with open('input.csv') as f:
    reader = csv.reader(f)
    next(reader)   # thow away the header
    for row in reader:
        print row

rawDescription: Python CSV file manipulation

description: Python CSV file manipulation


code: for k in keys: print k,Dictionary[k]

rawDescription: How to implement associative array (not dictionary) in Python?

description: Implement associative array (not dictionary) in Python


rawDescription: python, elegant way of creating vectors

description: Python, elegant way of creating vectors


code:
class Card(object):

    def __init__(self,rank,suit):
         self.rank=rank()
         self.suit=suit()

class Rank:
    pass

class Suit:
    pass

def main():
    c=Card(Rank,Suit)
    print c.rank

main()

rawDescription: Class as parameter in python __init__

description: Class as parameter in python __init__


code: print "\n".join(fullText.split('\n'))

rawDescription: Preserve line breaks when converting string to list and then back again with join


code:
likes_green = User.query.join(User.favorites).filter(
    UserFavorites.color == 'green'
).all()
print [u.email for u in likes_green]

rawDescription: Creating a basic Table with Flask-SQLAlchemy


code:
from selenium import webdriver

link = 'http://www.movieposterdb.com/Movie/20684'

driver = webdriver.Firefox()

driver.get(link)
links = driver.find_elements_by_tag_name('img')

for i in links:
    print i.get_attribute('src')

rawDescription: Beautifulsoup parse not working

description: Beautifulsoup parse not working


code:
from bs4 import BeautifulSoup

html="""

headline 1

text

text

headline 2

text

"""

soup = BeautifulSoup(html)

for h2 in soup.findAll("h2"):
    group = []
    node = h2.next_sibling

    while node is not None and node.name != "h2":
        group.append(node)
        node = node.next_sibling

    # Do w/e you want w/ the group
    print group

rawDescription: How to determine a logic part with python's beautiful soup when crawling

description: Determine a logic part with python's beautiful soup when crawling


code:
sorted_scores = [
    ('Apolo Ohno', 0),
    ('Shanie Davis', -1),
    ('Bodie Miller', -2),
    ('Lindsay Vohn', -3),
    ('Shawn White', -3),
    ('Bryan Veloso',-4)
]

res = {}
prev = None
for i,(k,v) in enumerate(sorted_scores):
    if v!=prev:
        place,prev = i+1,v
    res[k] = place

print res

rawDescription: Positional Rankings and Dealing with Ties in Python


code:
memcon.row_factory = sqlite3.Row
memdata.execute("select * from tabletwo where tabletwo.Gene_ID == 'NFYA'")
nfya_row = memdata.fetchone()

memdata.execute("SELECT tableone.SampleID from tableone WHERE tableone.Diagnos=='RH'")
for row in memdata:
    sampleID = row['SampleID']
    print sampleID, nfya_row[sampleID]

rawDescription: sql, matching row values obtained from first table to the column names of second table without loop

description: Sql, matching row values obtained from first table to the column names of second table without loop


code:
x = np.arange(9)
print x.strides

rawDescription: Efficient Numpy 2D array construction from 1D array


code: print sorted(globalHotItems.items(), key=lambda x:x[1])[-4:]

rawDescription: Most efficient way to find longest incrementing subsequence in a list of lists


code:
for b in B:
    for cperm in permutations(C):
        res = {''.join([a, b, c]) for a, c in zip(A, cperm)}
        print '{{{0!r}, {1!r}, {2!r}}}'.format(*sorted(res))

rawDescription: Permutation between three sets in python


code:
def printResults(result_dict):
    print "a, e, i, o, u, appear, respectively, {a},{e},{i},{o},{u} times".format(**result_dict)

rawDescription: Function that takes a string as input and counts the number of times the vowel occurs in the string


code:
class Client(threading.Thread):

    def __init__(self, host=SERVER_IP, port=PORT):
        threading.Thread.__init__(self)
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock = socket.create_connection((host, port), 1)
        self.sock.setblocking(0)

    def run(self):
        while 1:
            try:
                global MESSAGES
                ready = select.select([self.sock], [], [], TIMEOUT*1000)
                if ready[0]:
                    buf = self.sock.recv(BUF_SIZE)
                    print buf
                    #TODO:do stuff with buf

                    print 'messages left:'+str(len(MESSAGES))
                    if len(MESSAGES)>0:
                        self.sock.send(MESSAGES.pop())
            except KeyboardInterrupt:
                self.sock.close()
                sys.exit(1)
            except Exception, e:
                print '\n[ERR] %s' % e
                self.sock.close()
                sys.exit(1)

    def sendData(self, data):
        global MESSAGES
        print 'appending data:%s' % data
        MESSAGES.append(data)

rawDescription: Sending data from outside socket object in python


code:
def running_id():
    n = 1
    while True:
        yield 'A{0:05d}'.format(n)
        n += 1

C = running_id()
for n in xrange(5):
    print next(C)

rawDescription: A cleaner way to generate a list of running IDs


code:
import MySQLdb

def my_connect(*args, **kwargs):
    print args, kwargs
    return MySQLdb.real_connect(*args, **kwargs)

MySQLdb.real_connect = MySQLdb.connect
MySQLdb.connect = my_connect
execfile('/path/to/myscript.py')

rawDescription: Python security: Danger of uncollected variables out of scope


code: print '{0:+d}'.format(score)

rawDescription: How to print +1 in Python, as +1 (with plus sign) instead of 1?

description: Print +1 in Python, as +1 (with plus sign) instead of 1


code:
#!/usr/bin/python2.7
import csv
import re
with open('x.tsv') as input_file:
  input_file = csv.reader(input_file, delimiter='\t')
  for row in input_file:
    compiled_regex = re.compile(row[0])
    print row[0], bool(compiled_regex.search('remix')), bool(compiled_regex.search('re-mix'))

rawDescription: How to read in string, convert to regex, and compile it?

description: Read in string, convert to regex, and compile it


code:
import pdb
a = "aaa"
pdb.set_trace()
b = "bbb"
c = "ccc"
final = a + b + c
print final

rawDescription: Python Debugging Using Pdb

description: Python Debugging Using Pdb


code:
prompt='''\
    Enter a motif to search for it in the sequence
    (The wildcard character '?' represents any nucleotide in that position,
     and * represents none or many nucleotides in that position.)
'''
pattern=None
while pattern==None:
    print prompt
    user_input=raw_input('>>> ')
    letterlist = ['A','C','G','T', '?', '*']
    user_input=user_input.upper()
    if len(user_input)>1 and all(c in letterlist for c in user_input):
        pattern=user_input.replace('?', '.').replace('*', '.*?')
    else:
        print 'Bad pattern, please try again'

rawDescription: Python: use string variable as search pattern in regex


code:
# You don't even use this so why import it? --> import string

def main():
    user_string = raw_input()
    all_caps = user_string.upper() # guess you wanted to make it uppercase
    output = [] # this will hold the decoded characters

    for char in all_caps:
        if char.isalpha():
            value = ord(char)
            if 70 <= value <= 90: # look at this, almost no other programming language supports that syntax
                num = value - 5
            elif 65 <= value <= 69:
                num = value + 21
            output.append(chr(num)) # add the decoded character to the output list
        else:
            output.append(char) # add the character verbatim to the output list (e.g. whitespace)

    print "".join(output) # print out the list by putting it together into a string

main()

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


code:
#!/usr/bin/env python

import cgi
import os
import cgitb; cgitb.enable()  # for troubleshooting

form = cgi.FieldStorage()
secretword = form.getfirst("w", "")
remotehost = cgi.escape(os.environ["REMOTE_HOST"] if "REMOTE_HOST" in os.environ else os.environ["REMOTE_ADDR"])

print "Content-Type: text/html"
print # blank line, end of headers
print "
The secret word is " + secretword + " and your IP is " + remotehost + ".
"

rawDescription: Translate simple Perl script into Python that sends a response to client?


code:
import re
print re.split(r'[\s\[\]]+',open("general.txt", "r").read())

rawDescription: Extracting float numbers from file using python


code:
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("filename", nargs='?', default="defaultfile.txt",
                    help="File to process, default is 'defaultfile.txt.")
args = parser.parse_args()

# Now filename is in args.filename
print args.filename

rawDescription: Most compact way to store argv into variable with default value in Python


code:
from heapq import nlargest
for key in nlargest(10, x, key=x.get):
    print key, x[key]

rawDescription: Iterating through a dictionary for X number of times


code:
def merge(a, b):
    """Merges two iterators a and b, returning a single iterator that yields
    the elements of a and b in non-decreasing order.  a and b are assumed to each
    yield their elements in non-decreasing order."""

    done = object()
    aNext = next(a, done)
    bNext = next(b, done)

    while (aNext is not done) or (bNext is not done):
        if (bNext is done) or ((aNext is not done) and (aNext < bNext)):
            yield aNext
            aNext = next(a, done)
        else:
            yield bNext
            bNext = next(b, done)

for i in merge(iter(a), iter(b)):
    print i

rawDescription: how to use python iterators elegently

description: Use python iterators elegently


code:
#!/usr/bin/env python

import threading
import time
import re
import os
from subprocess import Popen, PIPE

maxUsage = 0
keepThreadRunning = True

def memWatch(freq=20):

    global maxUsage
    global keepThreadRunning

    while keepThreadRunning:

        p = Popen(["grep", "VmRSS", "/proc/%s/status" % os.getpid()],
                  shell=False, stdout=PIPE)

        curUsage = int(re.search(r'\d+', p.communicate()[0]).group())

        if curUsage > maxUsage:

            maxUsage = curUsage

        time.sleep(1.0 / freq)

if __name__ == "__main__":

    t = threading.Thread(target=memWatch)
    t.start()

    print maxUsage
    [p for p in range(1000000)]
    print maxUsage
    [str(p) for p in range(1000000)]
    print maxUsage
    keepThreadRunning = False
    t.join()

rawDescription: Resource.getrusage() always returns 0

description: Resource.getrusage() always returns 0


code:
source
import glob
import multiprocessing
import os
import subprocess

MAXCPU = multiprocessing.cpu_count()
TEST = False

def do_work(args):
    path,numproc = args
    curproc = multiprocessing.current_process()
    print curproc, "Started Process, args={}".format(args)
    devnull = open(os.devnull, 'w')
    cmd = ["sh", "Child.sh", path, str(numproc)]
    if TEST:
        cmd.insert(0, 'echo')
    try:
        return subprocess.check_output(
            cmd, shell=False,
            stderr=devnull,
        )
    finally:
        print curproc, "Ended Process"

if TEST:
    cp = MAXCPU
    list_pdb = glob.glob('t*.py')
else:
    cp = int(raw_input("Enter Number of processes to use (%d CPUs) = " % MAXCPU))
    list_pdb = glob.glob('*.pdb') # Input PDB files
# assert cp <= MAXCPU

print '{} files, {} procs'.format(len(list_pdb), cp)
assert len(list_pdb) != 0

pool = multiprocessing.Pool(cp)
print pool.map(
    do_work, [ (path,cp) for path in list_pdb ],
)
pool.close()
pool.join()

rawDescription: Run python process (using Pool of Multiprocessing ) parallel for batch


code:
from abc import ABCMeta, abstractmethod
import random
import inspect

available_srcs = []

def register(newclass):
    if inspect.isabstract(newclass):
        print ("newclass %s is abstract, and has abstract"
                " methods: %s. Refusing to register"
                % (newclass, newclass.__abstractmethods__))
        return
    if newclass not in available_srcs:
        available_srcs.append(newclass)
        print "Registered %s as available source" % newclass

class MyMetaClass(ABCMeta):
    def __new__(cls, clsname, bases, attrs):
        newclass = super(MyMetaClass, cls).__new__(cls, clsname, bases, attrs)
        register(newclass)  # here is your register function
        return newclass

class BaseSource(object):
    __metaclass__ = MyMetaClass

    @abstractmethod
    def get(self):
        pass

class ManualSrc(BaseSource):
    def get(self):
        return float(raw_input('gimme a number - '))

class RandomSrc(BaseSource):
    def get(self):
        return random.random()

if __name__ == '__main__':
    for use_src in range(4):
        print 'using source choice {}'.format(use_src)
        src = available_srcs[use_src]()
        print src.get()

rawDescription: Duck-typable classes, ABC, iheritance, __new__


code:
import sys
import os
userinput = sys.argv[1]
data = open(userinput, 'rb').read()
datalength = len(data)
fname = os.path.basename(userinput)
print "%s datasize = %s" % (fname, datalength)

rawDescription: Python on Windows: path as subprocess argument gets modified and generating error


code: print [ord(i) for i in section[0:2]]

rawDescription: unprintable string to number in python

description: Unprintable string to number in python


code:
from itertools import dropwhile
d = {"a": 1, "b": 2, "h": 55 }
for key in dropwhile(lambda k: k != 'b', sorted(d)):
    print key, d[key]

rawDescription: Python. Iterating dictionary starting from certain key


code:
def Cities():
    print 'Guess the name of this city'
    return CITIES

rawDescription: Hangman program

description: Hangman program


code:
for fname in tarfiles:
    print fname

rawDescription: Assigning elements from a for loop to another list


code:
mylist = [98, 10, 12]
w = 85
c = 90
status = 'OK' # assume it's ok until you found a value exceeding the threshold
for i in mylist:
    if status == 'OK' and i < w: # Only check OK as long as the status is ok
        status = "OK"
    elif i >= w and i < c:
        status = "WARNING"
    elif i >= c:
        status = "CRITICAL"
        break # End the loop as soon as a value triggered critical

print status

rawDescription: iterating list through multiple if statement in python

description: Iterating list through multiple if statement in python


code:
import sympy.physics.units as u
subs = {}
for k, v in u.__dict__.items():
    if isinstance(v, Expr) and v.has(u.Unit):
        subs[Symbol(k)] = v # Map the `Symbol` for a unit to the unit
# sympify returns `Symbol`s, `subs` maps them to `Unit`s
print sympify('yard*millimeter/ly').subs(subs)

rawDescription: How to parse and simplify a string like '3cm/µs² + 4e-4 sqmiles/km/h**2' treating physical units correctly?

description: Parse and simplify a string like '3cm/µs² + 4e-4 sqmiles/km/h**2' treating physical units correctly


code:
from sklearn.tree import DecisionTreeClassifier
from sklearn.cross_validation import cross_val_score
import pandas as pd
from sklearn import preprocessing
def main():
    df, _ = readCSVFile("../Data/discretized.csv")
    newdf, classl = catToInt(df)
    model = DecisionTreeClassifier()
    print cross_val_score(model, newdf, classl, cv=10)

def readCSVFile(filepath):
    df = pd.read_csv(filepath, index_col=0)
    (_, _, sufix) = filepath.rpartition('\\')
    (prefix, _, _) =sufix.rpartition('.')
    print "csv read and converted to dataframe !!"
    # df['class'] = df['class'].apply(replaceLabel)
    return df, prefix

def catToInt(df):
    # replace the Nan with "NA" which acts as a unique category
    df.fillna("NA", inplace=True)
    mapper={}

    # make list of all column headers
    categorical_list = list(df.columns.values)

    #exclude the class column
    categorical_list.remove('class')
    newdf = pd.DataFrame(columns=categorical_list)

    #Converting Categorical Data to integer labels
    for x in categorical_list:
        mapper[x]=preprocessing.LabelEncoder()
    for x in categorical_list:
        newdf[x]= mapper[x].fit_transform(df.__getattr__(x))

    # make a class series encoded :
    le = preprocessing.LabelEncoder()
    myclass = le.fit_transform(df.__getattr__('class'))

   #newdf is the dataframe with all columns except classcoumn and myclass is the class column
    return newdf, myclass

main()

rawDescription: How to Prepare Data for DecisionTreeClassifier Scikit


code:
def get_frequency(input_string):
    result = {}
    for char in input_string:
        result[char] = result.get(char, 0) + 1
    return result

if get_frequency(s1) == get_frequency(s2):
    print "Anagram!"
else:
    print "Not Anagram!"

rawDescription: Finding anagrams in Python

description: Finding anagrams in Python


rawDescription: NumPy: Execute function over each ndarray element


code:
from datetime import timedelta, datetime

def daterange(start_date, end_date):
    for n in range(int((end_date - start_date).days)):
        yield start_date + timedelta(n)

start_date = datetime(year=2015, month=1, day=1)
end_date = datetime.today()

for single_date in daterange(start_date, end_date):
    value = single_date.strftime("%Y-%m-%d")

    print value

rawDescription: Writing loop to get the data during some specific periods in Python


code:
green = np.vstack(contours).reshape((-1, 2))
print "Min X:", green[np.where(green[:, 0] == green[:, 0].min()), :]
print "Man X:", green[np.where(green[:, 0] == green[:, 0].max()), :]
print "Min Y:", green[np.where(green[:, 1] == green[:, 1].min()), :]
print "Man Y:", green[np.where(green[:, 1] == green[:, 1].max()), :]

rawDescription: detect 4 interesting pixels

description: Detect 4 interesting pixels


code: print "Pototo\b\b\ba\x1b[2Ces"

rawDescription: What's the opposite of \b character, i.e. a kind of non-erasing space?


code:
def foo():
    def bar():
        foo.foobar = 'foobaz'  #change the function attribute

    foo.foobar = 'foobar'     #declare as function attribute
    print foo.foobar
    bar()
    print foo.foobar
foo()

rawDescription: changing values inside a local space by a function in that local space

description: Changing values inside a local space by a function in that local space


code:
import pickle

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

class class2(object):
    def __init__(self, name, otherclass):
        self.name = name
        self.otherclass = otherclass

def test(c1, c2):
    print c1.name, ':', c1
    print c2.name, ':', c2

    print c2.name, 'has', c2.otherclass.name, ':', c2.otherclass
    print c2.name, "'s 'inside' class is c1:", (c2.otherclass is c1)

c1 = class1("class1")
c2 = class2("class2", c1)

test(c1, c2)
universe = [c1, c2]
pickled_universe = pickle.dumps(universe)
c1, c2 = pickle.loads(pickled_universe)
test(c1, c2)

rawDescription: Pickle linked objects part 2

description: Pickle linked objects part 2


code:
import ast
import re

with open("C:\XXXX\nemo\Test.mrk") as f:
    doc = f.read()

block_re = re.compile(r'v_dims=\{.*?\}', re.S)
tree     = ast.parse(block_re.search(doc).group(0))

print [val.s for val in tree.body[0].value.elts]
# ['name', 'age', 'address', 'contacts', 'Email']

rawDescription: Python Parse regex

description: Python Parse regex


code:
result = {}
res= [result.setdefault(a, {}).update({k:b}) for k, v in d.iteritems() for a,b in v.iteritems()]
print result

#Output: {'R2': {'eth2': 'bw2', 'eth1': 'bw1'}, 'R3': {'eth2': 'bw4', 'eth1': 'bw3'}}

rawDescription: Reverse mapping on nested dictionary

description: Reverse mapping on nested dictionary


code:
import instant_debug
import time

for i in xrange(1000000):
  print i
  time.sleep(0.1)

rawDescription: Drop into an Interpreter anytime in Python


rawDescription: Python: Joining tuples with backslash

description: Python: Joining tuples with backslash


code:
print {gender: percentage_dict({name: cnt[name] for name in persons if persons[name] == gender})
       for gender in set(persons.values())}

# {'Eunuch': {'Varys': 100.0}, 'Male': {'Hodor': 40.0, 'Theon': 0.0, 'Tyrion': 60.0}, 'Female': {'Daenerys': 66.66666666666667, 'Arya': 33.333333333333336, 'Sansa': 0.0}}

rawDescription: Reverse multiple values with keys in dictionary


code:
from itertools import imap
with open("dictionary.txt","r") as f:
    for line in imap(str.strip, f):
        print "Count {} {}".format(line, a_master[line])

rawDescription: Counting occurences of words that appear in a list using Python


code:
#Assuming all the sub lists have same length
num_rows = len(l)
num_cols = len(l[0])

sum_l = []

for i in range(0, num_cols):
    cur_sum = 0
    for j in range(0, num_rows):
            cur_sum += l[j][i]

    sum_l.append(cur_sum)

print sum_l

rawDescription: Add only the first value in my list of arrays


code:
# clients listing on the WebSocket
clients = set()

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        return self.render("index.html")

    def post(self):
        global clients

        event = self.get_argument("event")
        print "got event", event
        if not clients:
            print "No WebSockets, no point in querying the database"
            return
        for coordinate in self.get_coordinates(event):
            for client in clients:
                print "sending coordinate", coordinate, "to client", client
                client.write_message(json.dumps(coordinate,
                                                default=json_util.default))

    def get_coordinates(self, event):
        # replace with a real database query
        for coordinate in ("No", "man's", "land"):
            time.sleep(1)
            yield coordinate

class WSHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        global clients
        print "WebSocket opened..."
        clients.add(self)

    def on_close(self):
        global clients
        print "WebSocket closed..."
        clients.remove(self)

rawDescription: HTML form data not received by Tornado class


code:
mapping
for v2 in sorted(mapping):
    print '{} maps to {}'.format(v2, ', '.join(map(str, sorted(mapping[v2]))))

rawDescription: Python mapping between lists for duplicates


code: print tuple(max(lst) for lst in scores[0:3])

rawDescription: Max() returns highest list, not number

description: Max() returns highest list, not number


code:
import nltk

text = "
 hello

 world
"
print nltk.clean_html(text)  # prints "hello world"

rawDescription: Python, get text value of an html document


code:
import itertools

rows =  [("A", 76),  ("B", 56), ("C", 42), ("D", 31), ("E", 31), ("F", 25), ("G", 24)]

def print_multi_cols(lrows, split_at=5, space_each=4):
    for row in itertools.izip(*itertools.izip_longest(*[iter(lrows)]*split_at, fillvalue=(" ", " "))):
        for col in row:
            print " ".join(["%-*s" % (space_each, item) for item in col]),
        print

print_multi_cols(rows, 2)
print_multi_cols(rows, 3)

rawDescription: How to columnate a list in python?

description: Columnate a list in python


code:
def test():
  return "ACV"

print test()

a = test()
print a

rawDescription: Why python is printing None in the output?


code:
index=[1,2,3,4]
text=['animal','gv','asdfasdfasd','adfasdfasdf']
size=['1kb','2kb','3kb','4kb']
displaylist=[]

for i in zip(index,text,size):
    s=""
    for j in range(0,3):
        s +='{0:{fill}{align}16}'.format(i[j], fill=' ', align='<')

    print s
    displaylist.append(s)

for i in displaylist:
    print i

rawDescription: String formating in wx.ListBox

description: String formating in wx.ListBox


code:
import inspect

def foo(): pass

class Test(object):
    def method(self): pass

print inspect.ismethod(foo) # False
print inspect.ismethod(Test) # False
print inspect.ismethod(Test.method) # True
print inspect.ismethod(Test().method) # True

print callable(foo) # True
print callable(Test) # True
print callable(Test.method) # True
print callable(Test().method) # True

rawDescription: Python : Assert that variable is instance method?


code:
class TestGetitem(object):
  def __getitem__(self, item):
    print type(item), item

t = TestGetitem()

rawDescription: "Slicing" in Python Expressions documentation


rawDescription: convert xml to python dict

description: Convert xml to python dict


code:
html=\
"""

I contain a link.

blah

blah2

"""
import lxml.html as lh
doc=lh.fromstring(html)
for elem in doc.xpath('.//ul[@class="myclass"]/li'):
    print elem.text_content()

rawDescription: Get bulletted list in lxml

description: Get bulletted list in lxml


code: print e

rawDescription: Adjacent cells of multiple cell patches in a numpy array


code:
import re
inputstring = '\U0424\U0444\U0444\U042d'
escaped = re.sub(r'\\U([a-zA-Z0-9]{4})', r'\\u\1', inputstring)
escaped
escaped.decode('unicode_escape')
print escaped.decode('unicode_escape')

rawDescription: Problems with Unicode like string

description: Problems with Unicode like string


code:
tag = 'blockquote'

def recurse(d, tag):
    if isinstance(d, (dict, OrderedDict)):
        for k in d:
            yield '<' + tag + '>' + k
            for sub in recurse(d[k], tag):
                yield sub
            yield '</' + tag + '>'
    elif isinstance(d, (list, tuple)):
        d = ['<{1}>{0}</{1}>'.format(el, tag) for el in d]
        yield '<' + tag + '>' + ' '.join(d) + '</' + tag + '>'

print '\n'.join(list(recurse(j, tag)))

rawDescription: Python: convert arbitrarily structured nested list to html


code:
from dis import HAVE_ARGUMENT, opmap

def getglobals(func):
    GLOBAL_OPS = opmap["LOAD_GLOBAL"], opmap["STORE_GLOBAL"]
    EXTENDED_ARG = opmap["EXTENDED_ARG"]

    func = getattr(func, "im_func", func)
    code = func.func_code
    names = code.co_names

    op = (ord(c) for c in code.co_code)
    globs = set()
    extarg = 0

    for c in op:
        if c in GLOBAL_OPS:
            globs.add(names[next(op) + next(op) * 256 + extarg])
        elif c == EXTENDED_ARG:
            extarg = (next(op) + next(op) * 256) * 65536
            continue
        elif c >= HAVE_ARGUMENT:
            next(op)
            next(op)

        extarg = 0

    return sorted(globs)

print getglobals(f)               # ['y']

rawDescription: Detect all global variables within a python function?


code:
foo = 'baz "\\"'
foo
print foo

rawDescription: Python - Backslash Quoting in String Literals


code:
def convert_empty_to_none(func):
    def inner_function(*args, **kwargs):
        args = (None if item == "" else item for item in args)
        kwargs = {k:(None if v == "" else v) for k, v in kwargs.items()}
        return func(*args, **kwargs)
    return inner_function

@convert_empty_to_none
def test_function(a, b, c):
    print a, b, c

test_function("", "", "")

rawDescription: Convert all Input parameters of function with generic function


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

    print mydict

rawDescription: make dictionary from csv file columns

description: Make dictionary from csv file columns


code:
TAG0 = 'EULER'
TAG1 = 'FRAC'

def extract_rows(tokens):
    pos = 0
    while True:
        try:
            pos = tokens.index(TAG0, pos)
        except ValueError:
            return
        assert pos + 8 <= len(tokens)
        assert tokens[pos+4] == TAG1
        yield (
            tuple(map(float, tokens[pos+1:pos+4])),
            tuple(map(float, tokens[pos+5:pos+8])),
            )
        pos += 8

for rowx, row in enumerate (extract_rows(alist)):
    print rowx, 'TAG0', row[0]
    print rowx, 'TAG1', row[1]

rawDescription: How to I extract floats from a file in Python?

description: I extract floats from a file in Python


code:
input = [1, 1.0, '1', -1, 1]

output = [x[0] for x in {(y, type(y)) for y in input}]

print output

rawDescription: How to distinguish same int and float elements?

description: Distinguish same int and float elements


code:
def progress():
    # This prints out the time left until rebuild is done.
    loudEchoCMD("storage::rebuild-eta")
    # This prints out the % of the rebuild that is done.
    rebuildProgress = loudEchoCMD("storage::rebuild-progress")
    print rebuildProgress
    if rebuildProgress != '100%':
        t = threading.Timer(5.0, progress)
        t.start()

rawDescription: I want to make a function in python that runs every 5 seconds until a variable reaches 100%


code:
def print_table(self, lines, sum_columns=None, extra_width=5):
    columns = zip(*lines)

    widths = [max(len(str(l)) for l in column) for column in columns]

    for line in lines:
        print ''.join(['{0:{1}}'.format(c, w + extra_width) for c, w in zip(line, widths)])

    if sum_columns is not None:
        totals = [sum(columns[i]) if i + 1 in sum_columns else 0 for i in range(len(columns))]
        print ''.join(['{0:{1}}'.format(c, w + extra_width) for c, w in zip(totals, widths)])

rawDescription: dynamically building strings 'string'.format()

description: Dynamically building strings 'string'.format()


rawDescription: calculating distance between two numpy arrays

description: Calculating distance between two numpy arrays


code:
def cheese(cheese_count):
    print "You have %d cheeses!" % cheese_count

cheese_count = 20
print cheese(10)

rawDescription: Explain why variables in my function are not related to variables in my script in Python 2


code:
with open("data") as f:
        lines=[line.strip().split() for line in f]
combinations={}
for combination in lines[1:]:
        key=", ".join([lines[0][i]
                for i in xrange(len(combination))
                if combination[i] != '0'])
        combinations[key]=combinations.setdefault(key, 0)+1
for key, value in combinations.iteritems():
        print value, '\t', key

rawDescription: Computing the combinations for presence or absence of a factor in a table


code:
import re
with open("input") as f:
    for line in f:
        mo=re.match(r'.*?(\d+).*?(mega|kilo|metric|) (tons|feet|lbs)', line)
        if mo: print mo.group(1), mo.group(2), mo.group(3)

rawDescription: Matching Regex in Python

description: Matching Regex in Python


code:
p = Popen("a.exe", stdout=PIPE)

for line in iter(p.stdout.readline,""):
     print line

rawDescription: Capture stdout from a running C program with Python


code:
if __name__ == '__main__':
    url = LPCWSTR("http://user:password@www.host.com:8080/url-path?key=value")
    url_length = len(url.value)
    flags = 0
    url_components = URL_COMPONENTS()
    if not wininet.InternetCrackUrlW(url, url_length, flags,
                                     byref(url_components)):
        raise WinError(get_last_error())
    for name, dtype in url_components._fields_:
        print '%s: %s' % (name, getattr(url_components, name))

rawDescription: InternetCrackUrlW does not fill the values of strings behind pointers in structured class


code:
def sum(X,v):
    rows, cols = X.shape
    row_start_stop = as_strided(X.indptr, shape=(rows, 2),
                            strides=2*X.indptr.strides)
    for row, (start, stop) in enumerate(row_start_stop):
        data = X.data[start:stop]
        data -= v[row]

sum(mat, vec.A)
print mat.A

rawDescription: Efficiently Subtract Vector from Matrix (Scipy)


code:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
from scipy import interpolate

fig=plt.figure(figsize=(8,8))
ax=plt.subplot()
#xi,yi = np.linspace(195.2260,391.2260,50),np.linspace(4108.9341,4304.9341,50)
xi,yi = np.linspace(225,275,50),np.linspace(4200,4260,50)

# A made up function of height (in place of your data)
XI,YI = np.meshgrid(xi,yi)
height = (XI-230.)**2 + (YI-4220.)**2
#height=np.array(list(csv.reader(open("/Users/HYF/Documents/SJZ_vis/Concentration/work/terr_grd.csv","rb"),delimiter=','))).astype('float')

cmap = cm.get_cmap(name='terrain', lut=None)
terrf = plt.contourf(xi, yi, height,10, cmap=cmap)
terr = plt.contour(xi, yi, height, 10,
             colors='k',alpha=0.5
             )
plt.clabel(terr, fontsize=7, inline=20)
ax.autoscale(False)

# Some made up sample points
dat_so2 = np.array([(230,4210),(240,4220),(250,4230),(260,4240),(270,4250)],dtype=[("xp","f4"),("yp","f4")])

point= plt.scatter(dat_so2["xp"], dat_so2["yp"], marker='o',c="grey",s=40)

# The interpolation function
hfunc = interpolate.interp2d(xi,yi,height)

# Now, for each point, lets interpolate the height
pointheights = np.zeros(dat_so2["xp"].shape)
for i,(x,y) in enumerate(zip(dat_so2["xp"],dat_so2["yp"])):
    pointheights[i]=hfunc(x,y)
print pointheights

ax.autoscale(False)
for i in range(0,len(dat_so2["xp"]),1):
    plt.text(dat_so2["xp"][i], dat_so2["yp"][i],
             str(i),color="White",fontsize=16)
    # We can also add a height label to the plot
    plt.text(dat_so2["xp"][i], dat_so2["yp"][i],
             "{:4.1f}".format(pointheights[i]),color="black",fontsize=16,ha='right',va='top')

ax.set_xlim(225,275)
ax.set_ylim(4200,4260)

plt.show()

rawDescription: How to obtain the contour plot data for each scatter points?

description: Obtain the contour plot data for each scatter points


rawDescription: Python, using BeautifulSoup parsing values from a table


code:
import string
x = string.Formatter()
elements = x.parse(y)
for a,b,c,d in elements: print b

rawDescription: Check if a string to interpolate provides expected placeholders


code:
from lxml import html, etree
import requests

filepath = "587-islam-is-dominated-by-radicals"
try:
    contents = open(filepath).read()
    print "(reading cached copy)"
except IOError:
    url = "http://intelligencesquaredus.org/debates/past-debates/item/587-islam-is-dominated-by-radicals"
    print "(getting file from the net; please stand by)"
    r = requests.get(url)
    contents = r.content
tree = html.fromstring(contents)
count = tree.xpath('count(//*[@id="body"])')
print count

rawDescription: Most efficient way to count nodes using XPath in Python


code:
for item in msg:
     if '$' in item:
        starting=item.index('{')
        ending=item.index('}')

        print item[3:starting],item[starting+1:ending]

rawDescription: How to retrieve a specific field whith a special character in a list

description: Retrieve a specific field whith a special character in a list


code:
from numpy import *

x = array([[ 0.46006547,  0.5580928 ,  0.70164242,  0.84519205,  1.4       ],
           [ 0.00912908,  0.00912908,  0.05      ,  0.05      ,  0.05      ]])

low, high = array([ 0.633,  0.01 ]), array([ 1.325,  0.99 ])

# construct an array of two rows of bools expressing your conditions
indices1 = array((x[0,:]<low[0], x[1,:]<low[1]))
print indices1

# do an or of the values along the first axis
indices1 = any(indices1, axis=0)
# now it's a single row array
print indices1

# use the indices1 to extract what you want,
# the double transposition because the elements
# of a 2d array are  the rows
tmp1 = x.T[indices1].T
print tmp1

# [[ True  True False False False]
#  [ True  True False False False]]
# [ True  True False False False]
# [[ 0.46006547  0.5580928 ]
#  [ 0.00912908  0.00912908]]

rawDescription: Efficiently select subsection of numpy array


code:
import re

data={}
with open('/tmp/src.txt','r') as src:
    for line in src:
        m=re.search(r'>([^\s]*)\s(.*)$',line)
        if m:
            data[m.group(1)]=m.group(2)

print data

rawDescription: Dictionary accession numbers

description: Dictionary accession numbers


code:
sentinel = ''
print list(iter(raw_input, sentinel))

rawDescription: Get a list (object in new line) and print a list in one line


code:
for keys in fastaDict1.keys():
    print keys
    if keys in fastaDict2:
        for base in range(0, len(fastaDict1[keys])):
            if fastaDict1[keys][base] != 'N' and fastaDict1[keys][base] == fastaDict2[keys][base]:
                print fastaDict1[keys][base] + '\t' + fastaDict2[keys][base] + '\t' + str(base)
            else:
                print 'Bases do not match' + '\t' + str(base)

rawDescription: Multiple conditions to compare values of two dictionaries in python


code:
def loadlist(anIterable):
    # collect data in ordinary list
    data = defaultdict(list)
    for line in anIterable:
        f = line.rstrip().split(',')
        if len(f)<4:
            continue
        aGene = Gene(f)
        data[aGene.chr].append(aGene)
    return data

data = loadlist(txt.splitlines())
print 3, find(data['A'],3)

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

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


code:
from ast import literal_eval
import re
import urllib2

from bs4 import BeautifulSoup

url = 'http://archive.org/details/HQSherlockRathboneTCS'
soup = BeautifulSoup(urllib2.urlopen(url))

script = soup.find('script', text=lambda x: x and 'jw6' in x)
text = script.text.replace('\n', '')

pattern = re.compile(r"Play\('jw6', (.*?),\s+\{\"start")

playlist = literal_eval(pattern.search(text).group(1).strip())
for track in playlist:
    print track['title']

rawDescription: Why can't I get track titles from url?

description: Why can't I get track titles from url


code:
def new_func(self, param):
    print param

class a:
    pass

a.my_func = new_func
b = a()
b.my_func(10)

rawDescription: Python: inject new functions into an instance of class?


code: print position_nmers

rawDescription: Pandas Dataframe to Dictionary with Multiple Keys


code:
import urllib
sock = urllib.urlopen("http://en.wikipedia.org/wiki/Python_(programming_language)")
htmlsource = sock.read()
sock.close()
print htmlsource

rawDescription: How do I get data from stdin using os.system()

description: Get data from stdin using os.system()


code:
import re

s = '* TODO [#A] Do something important :Tag1:Tag2:Tag3:'
m = re.split('\s+:|:$', s)

print "\n".join([m[0], m[1]])

rawDescription: Extracting the tags from an org mode line using Python regex


code:
# Import PuLP modeler functions
from pulp import *

# INSTANCE
EDGES = [ (0,10), (0,20), (0,50), (0,30), (0,10), (0,40) ] # lower/upper flow
COSTS = [0.3, 0.7, 0.2, 0.6, 0.2, 0.5]
D1, D2 = [24, 10]

# MODEL
prob = LpProblem("Problem",LpMaximize)
# VARS
x11, x12, x13 = LpVariable("x11", *EDGES[0]), LpVariable("x12", *EDGES[1]), LpVariable("x13", *EDGES[2])
x21, x22, x23 = LpVariable("x21", *EDGES[3]), LpVariable("x22", *EDGES[4]), LpVariable("x23", *EDGES[5])
# OBJECTIVE FUNCTION -> added before constraints => important ,
prob += lpDot(COSTS, [x11,x12,x13,x21,x22,x23]), "Costs"
# CONSTRAINTS
prob += x11 + x12 + x13 == D1
prob += x21 + x22 + x23 == D2

# SOLVE
status = prob.solve()

# PRINT SOLUTION
print LpStatus[status]
for var in [x11,x12,x13]:
    print value(var)
print
for var in [x21,x22,x23]:
    print value(var)
print("Total Costs = ", value(prob.objective))

rawDescription: Transportation Algorithm in Python

description: Transportation Algorithm in Python


code:
with open( file.txt, 'r') as f:
     for line in f.readlines()[6:]:
          print line

rawDescription: How to start reading from certain line?

description: Start reading from certain line


code:
def GetMenuChoice():
  MenuChoice = raw_input("Enter your choice: ")
  return MenuChoice.lower()

expected="Menu1"
while GetMenuChoice() != expected.lower():
    print "Try again"
print "Correct input"

rawDescription: Python 2.7 accepting capital letters

description: Python 2.7 accepting capital letters


code:
def print_nested_list(input_list):
    if type(input_list) is list:
        for item in input_list:
            print_nested_list(item)
    else:
        print input_list

mylist = [[['cs1110', 'opython'], 'nested'], 'recursion', 'test']
print_nested_list(mylist)

rawDescription: Recursive means of printing elements in a list in new lines


code:
print results
[False, False, True]

rawDescription: Find the first column in the matrix, in which all elements are positive.


code:
import httplib
headers = {"Content-type": "text/plain",
           "Accept": "text/plain"}

conn = httplib.HTTPConnection("localhost:8004")
conn.request("POST", "/", "28", headers)
response = conn.getresponse()
print response.read()

rawDescription: HTTP telnet POST/GAE server question (SIMPLE STUFF)


code:
class TestHandler(tornado.web.RequestHandler):    #  localhost:8888/test
    @tornado.web.asynchronous
    def get(self):
        print "Thread starting %s" % time.time()
        t = threading.Thread(target = self.newThread)
        t.start()

    def newThread(self):
        print "new thread called, sleeping %s" % time.time()
        time.sleep(10)
        self.write("Awake after 10 seconds!" % time.time())
        self.finish()

rawDescription: Python Tornado - Asynchronous Request is blocking


code:
result = re.findall(r"protocol': (.*?),", strevent)
print [ r.strip() for r in result ]

rawDescription: Python regex findall unexpected output

description: Python regex findall unexpected output


code:
import elementtree.ElementTree as ET

tree = ET.parse("sample.xml")

# the tree root is the toplevel `PacketHeader` element
print tree.findtext("HeaderField/name")

rawDescription: Parsing xml using python minidom

description: Parsing xml using python minidom


code:
[submission] = submissions.get_new(limit=1)
print submission.permalink

rawDescription: get URL of any submission for subreddits

description: Get URL of any submission for subreddits


code:
from scrapy import signals, log
from scrapy.xlib.pydispatch import dispatcher
from scrapy.crawler import CrawlerProcess
from scrapy.conf import settings
from scrapy.http import Request

def handleSpiderIdle(spider):
    '''Handle spider idle event.''' # http://doc.scrapy.org/topics/signals.html#spider-idle
    print '\nSpider idle: %s. Restarting it... ' % spider.name
    for url in spider.start_urls: # reschedule start urls
        spider.crawler.engine.crawl(Request(url, dont_filter=True), spider)

mySettings = {'LOG_ENABLED': True, 'ITEM_PIPELINES': 'mybot.pipeline.validate.ValidateMyItem'} # global settings http://doc.scrapy.org/topics/settings.html

settings.overrides.update(mySettings)

crawlerProcess = CrawlerProcess(settings)
crawlerProcess.install()
crawlerProcess.configure()

class MySpider(BaseSpider):
    start_urls = ['http://site_to_scrape']
    def parse(self, response):
        yield item

spider = MySpider() # create a spider ourselves
crawlerProcess.queue.append_spider(spider) # add it to spiders pool

dispatcher.connect(handleSpiderIdle, signals.spider_idle) # use this if you need to handle idle event (restart spider?)

log.start() # depends on LOG_ENABLED
print "Starting crawler."
crawlerProcess.start()
print "Crawler stopped."

rawDescription: Running Scrapy from a script - Hangs

description: Running Scrapy from a script - Hangs


code:
s5='monkey 20 January 2015 loves 2014 bananas'
print word_find(s5)

rawDescription: Separating a date from a string in Python


code:
from __future__ import print_function
rdd = sc.parallelize(["e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "e10"])
transformed = rdd.zipWithIndex().groupBy(lambda (_, i): i / 3).map(lambda (_, list): tuple([elem[0] for elem in list]))
transformed.foreach(print)

rawDescription: Partition RDD into tuples of length n

description: Partition RDD into tuples of length n


code:
if __name__ == '__main__':
    a, b, c, d, e, f= 'A', 'B', 'C', 'D', 'E', 'F'
    G= {a: [b, c], b: [c, d], c: [d], d: [c], e: [f], f: [c]}
    print 'All paths from:', a, 'to:', d
    print 'BFS'
    for path in paths(G, a, d): print path
    print 'DFS'
    for path in paths(G, a, d, fringe('queue')): print path

rawDescription: Python - Graph Data Structure - How Do I Implement Breadth-First Search to Find Reachable Vertices Correctly


code:
from threading import Thread

class ClassX(Thread):
    def __init__(self, _parent):
        Thread.__init__(self)
        self.parent = _parent

    def run(self):
        while 1:
            print 'Im here!!!'

class App():
    # def __init__(self) goes here....
    def startRunning(self):
        self.listen = ClassX(self)
        self.listen.start()

rawDescription: Get the thread state stated from another class


code: print ET.tostring(grandchild).strip()

rawDescription: Iterate through XML child of a child tags in Python


code:
for line in random_lines('file.txt'):
    print line

rawDescription: Retrieve 10 random lines from a file

description: Retrieve 10 random lines from a file


code:
import numpy as np

# First let's generate some data and set a few duplicate values
data = np.arange(100).reshape(10,10)
data[9,9] = 2
data[8,6] = 53

print 'Original Data:'
print data

# We want to replace the _first_ occurences of "samples" with the corresponding
# value in "grid_details" within a 3x3 window...
samples = [2, 53, 69]
grid_details = [200,500,100]

nrows, ncols = data.shape
for value, new_value in zip(samples, grid_details):
    # Notice that were're indexing the _first_ item than argwhere returns!
    i,j = np.argwhere(data == value)[0]

    # We need to make sure that we don't index outside the boundaries of the
    # array (which would cause a "wraparound" assignment)
    istart, istop = max(i-1, 0), min(i+2, nrows)
    jstart, jstop = max(j-1, 0), min(j+2, ncols)

    # Set the value within a 3x3 window to be "new_value"
    data[istart:istop, jstart:jstop] = new_value

print 'Modified Data:'
print data

rawDescription: Python 3.1- Grid Simulation Conceptual issue


code:
line = data.readline()
while(line):
    print line
    line = data.readline()

rawDescription: Analyze and grab link from an html page

description: Analyze and grab link from an html page


code:
text = ['a', 'b', 'c']
text = '\n'.join(text)
print text

rawDescription: Python and Line Breaks

description: Python and Line Breaks


code:
def loop_forever():
    while 1:
        print 'sec'
        try:
            time.sleep(10)
        except:
            continue

rawDescription: Timeout on a function call

description: Timeout on a function call


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

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


code:
def callback(msg):
    print msg

def callback_factory(m):
    return lambda: callback(m)

funcList=[]
for m in ('do', 're', 'mi'):
    funcList.append(callback_factory(m))
for f in funcList:
    f()

rawDescription: Scope of python lambda functions and their parameters


code:
doc = BeautifulSoup(html)
for a in doc.findAll('a', 'question-hyperlink'):
    print a.text

rawDescription: Python: Find a Sentence between some website-tags using regex


rawDescription: Python - workaround with sets

description: Python - workaround with sets


code: print s.split(",")

rawDescription: Sum of all numbers inside a string

description: Sum of all numbers inside a string


code:
output_lines = [i for i in re.split(r'\n', output) if "Lorem" in i]
print output_lines

rawDescription: Regex returns text of every line with match, when text is inside variable


code:
od=collections.OrderedDict(Arr)
for k in od: print k,od[k]

rawDescription: How to implement associative array (not dictionary) in Python?


code:
res = {}

f = open("log.txt", "r")
for line in f.readlines():
    print line
    m = re.match(regexp, line)
    if m:
        print m.groups()
        id = m.groups()[1]
        if id in res:
            #print "added to existing ID"
            res[id].append([m.groups()[0], m.groups()[2]])
        else:
            #print "new ID"
            res[id] = [m.groups()[0], m.groups()[2]]

for id in res:
    print "ID: ", id
    print res[id]

rawDescription: Return All Matching Lines in a Logfile

description: Return All Matching Lines in a Logfile


code:
import csv
num_headers = 5
with open("input.csv", 'r') as file_in, open("output.csv", 'w') as file_out:
    csv_in = csv.reader(file_in)
    csv_out = csv.writer(file_out)
    for i, row in enumerate(csv_in):
        if len(row) == num_headers:
            csv_out.writerow(row)
        else:
            print "line %d is malformed" % i

rawDescription: eliminate malformed records from a large .csv file

description: Eliminate malformed records from a large .csv file


code:
from StringIO import StringIO
c = StringIO()
d = {
   'value_1' : 'hello',
   'value_2' : False,
   'value_3' : 29
}
for k, v in d.items():
    c.write("{} = {!r}\n".format(k, v))
c.seek(0)
print c.read()

rawDescription: How do I format a string value as an actual string

description: Format a string value as an actual string


code:
# Common code goes in this new class
class DerHelper:
    def __init__(self, parent):
        self._parent = parent
    def OwnFoo(self):
        print 'Do something 1', self._parent
    def OwnFoo2(self):
        print 'Do something 2', self._parent
    def Foo(self):
        print 'Do something 3', self._parent

# First derived class
class Der1(Base):
    def __init__(self):
        # include helper class by composition
        self._helper = DerHelper('Der1')
    def OwnFoo(self):
        self._helper.OwnFoo()
    def OwnFoo2(self):
        self._helper.OwnFoo2()
    def Foo(self):
        self._helper.Foo()

# Second derived class
class Der2(Base):
    def __init__(self):
        # include helper class by composition
        self._helper = DerHelper('Der2')
    def OwnFoo(self):
        self._helper.OwnFoo()
    def OwnFoo2(self):
        self._helper.OwnFoo2()
    def Foo(self):
        self._helper.Foo()

rawDescription: Question about python and classes

description: Question about python and classes


code:
import math, random

def poisson(m, n):
    p=math.exp(-m)
    r=[p]
    for i in range(1, n):
        p*=m/float(i)
        r.append(p)
    return r

def simulate(mx, my, n):
    r=[0.0 for i in range(3)]
    px, py = (poisson(mx, n),
              poisson(my, n))
    for i in range(n):
        for j in range(n):
            if i > j:
                k=0
            elif i < j:
                k=1
            else:
                k=2
            r[k]+=px[i]*py[j]
    return r

from scipy import optimize

Target, N = [0.5, 0.2, 0.3], 10

def error(p, target, n):
    r = simulate(p[0], p[1], n)
    return np.sum(np.subtract(target, r)**2)

r = optimize.fmin(error, (0, 0), args=(Target, N))
print simulate(r[0], r[1], n)

rawDescription: Python dual- variable search algorithm

description: Python dual- variable search algorithm


code:
def interleave(my_list, filler):
    for item in my_list:
        yield item
        yield filler

print list(interleave(['a', 'b', 'c'], 'x'))
# ['a', 'x', 'b', 'x', 'c', 'x']

rawDescription: Memory efficient padding a list

description: Memory efficient padding a list


code:
for line in fileinput.input():
    if fileinput.isfirstline():
        if fileinput.isstdin():
            print 'stdin'
        else:
            print fileinput.filename()

    print line

rawDescription: Reading files using raw_input or argv

description: Reading files using raw_input or argv


code:
from string import punctuation
import re

puncrx = re.compile(r'[{}\s]'.format(re.escape(punctuation)))
print filter(None, puncrx.split(your_tweet))

rawDescription: How to format tweets using python through twitter api?

description: Format tweets using python through twitter api


code:
import csv

with open('file_.csv','rb') as f :
    csvreader=csv.reader(f,delimiter=' ')
    print zip(*csvreader)

rawDescription: how to split .csv data in python?

description: Split .csv data in python


code:
from operator import mul

def validate(array, sizes):
    if reduce(mul, sizes) != len(array):
        raise ValueError("Array dimension incompatible with desired sizes")

    return array, sizes

def reshape(array, sizes):
    for s in sizes:
        array = [array[i:i + s] for i in range(0, len(array), s)]

    return array[0]

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
length = [2, 2, 3]

print reshape(validate(data, length))

length = [2, 2, 2]

print reshape(validate(data, length))

rawDescription: Python list to XML and vice versa

description: Python list to XML and vice versa


code:
import fileinput
for line in fileinput.FileInput("file",inplace=1):
    s=line.split(",")
    s[0]="1"
    print ','.join(s)

rawDescription: Replace a word in a file

description: Replace a word in a file


code:
import csv

data = {}

with open('myfile.csv', 'rb') as csvfile:
    ntreader = csv.reader(csvfile, delimiter="\t", quotechar='"')
    for rowI, rowData in enumerate(ntreader):
        if rowI == 0:
            headers = rowData[1:]
        else:
            data[rowData[0]] = {k: int(v) for k, v in zip(headers, rowData[1:])}

print data

rawDescription: Parse a text file in an array python

description: Parse a text file in an array python


code: print zz.pivot_table(index='date', columns='feccandid', fill_value='0', aggfunc=np.mean)

rawDescription: Pandas: reshape data frame

description: Pandas: reshape data frame


code:
class A(Base):
    # ...
    def get_children(self, offset, count):
        # @todo: might need to handle some border cases
        qry = B.query.with_parent(self)
        #or: qry = object_session(self).query(B).with_parent(self)
        return qry[offset:offset+count]

my_a = session.query(A).get(a_id)
print my_a.get_children( 0, 10) # print first 10 children
print my_a.get_children(10, 10) # print second 10 children

rawDescription: How to load a collection with limited number in Sqlalchemy?

description: Load a collection with limited number in Sqlalchemy


code:
print soup.find_all(text=must_contain_all("world", "puzzle"))
# [u"\nWho in the world am I? Ah, that's the great puzzle.\n"]

rawDescription: Find different strings and return containing tag in BeautifulSoup


code:
import math
import os

def avg_dev(arr):
    """
    Calculate mean and standard deviation on an array of values

    @param arr Array of values
    """
    if len(arr)==0:
        return 0.0, 0.0
    else:
        avg = float(sum(arr)) / len(arr)
        dev = math.sqrt(sum([(i-avg)**2 for i in arr]))
        return avg,dev

class Flight:
    """
    Class wraps data about a single flight
    """
    def __init__(self, txt):
        """
        Initialize flight information

        @param txt  List of strings containing per-flight data
                    First row is header, beginning with 'ACM';
                    remainder are space-delimited columnar data
        """

        self.flightName = txt[0][3:].strip()
        self.numLines = len(txt)-1

        height = []
        wind = []
        temp = []
        for ln in txt[1:]:
            data = ln.split()
            height.append(float(data[4]))
            wind.append(float(data[3]))
            temp.append(float(data[8]))

        self.avg_height,     self.std_dev_height  = avg_dev(height)
        self.avg_wind_speed, self.std_dev_speed   = avg_dev(wind)
        self.avg_temp,       self.std_dev_temp    = avg_dev(temp)

    def __str__(self):
        return """Results for %s:
Speed: avg %f, stddev %f
Temp: avg %f, stddev %f
Height: avg %f, stddev %f

""" % (
            self.flightName,
            self.avg_wind_speed, self.std_dev_speed,
            self.avg_temp, self.std_dev_temp,
            self.avg_height, self.std_dev_height)

class Flights:
    """
    Container class for multiple flights expressed in a single log file
    """
    def __init__(self, fname):
        """
        Read a flight log file

        @param fname  Name of the log file
        """
        self.filename = fname
        self.flight = []

        inf = file(fname, 'r')
        txt = []    # per-flight data buffer
        for ln in inf.readlines():
            if ln[:3]=='ACM':   # found start of new record
                if len(txt)>0:  # flush buffer
                    self.flight.append(Flight(txt))
                txt = []
            txt.append(ln)
        if len(txt)>0:          # clear buffer
            self.flight.append(Flight(txt))
        inf.close()

    def __iter__(self):
        return self.flight.__iter__()

def main():
    fname = raw_input("Enter the filename: ")

    try:
        # read flight data
        flight = Flights(fname)
    except IOError:
        print "File doesn't exist"
        sys.exit(1)

    logname = os.path.splitext(fname)[0] + ".log"
    logf = file(logname, 'w')
    for f in flight:
        logf.write(str(f))
    logf.close()

if __name__=="__main__":
    main()

rawDescription: Implementing classes in python

description: Implementing classes in python


code:
for entry in list_of_entries:
    print entry, " : ", entry.string1, entry.string2
    for s in entry:
        print s,
    print ""

rawDescription: how to make a formated print of list with list within it in python

description: Make a formated print of list with list within it in python


code:
my_list = [
    ['Direct General Auto Insurance', 'IT Project Manager', 'Nashville', 'TN', '(615) 331-7540'],
    ['Direct General Auto Insurance', 'IT Systems Manager', 'Nashville', 'TN', '(615) 340-6291'],
    ['Direct General Auto Insurance', 'Junior web Developer', 'Nashville', 'TN', '(615) 399-4700'],
    ['Grand Slam Universal, LLC', 'IT Specialist', 'Nashville', 'TN', '(615) 457-3516'],
    ['Ingram Content Group', 'Desktop Engineer', 'La Vergne', 'TN', '(615) 793-5000'],
    ['Direct General Auto Insurance', 'Data Scientist', 'Nashville', 'TN', '(615) 726-3091'],
    ['Direct General Auto Insurance', 'Janitor', 'Nashville', 'TN', '(615) 831-2600'],
]

index = {}

for item in my_list:
    existing = index.get(item[0])
    if not existing:
        index[item[0]] = item
        # or this, if you want don't want to modify the existing table
        # index[item[0]] = item[:]
    else:
        existing.insert(1, item[1])

for item in sorted(index.values()):
    print item

rawDescription: Inserting elements of one list into another list if some other element matches


rawDescription: Getting scalar values of z2(x,y) numpy array along a contour defined by a coordinate list


code:
def fun1(a):
    for i in range(len(a)):
        a[i] = a[i] * a[i]
    return a

test1 = [1,2,3,4,5]
print 'Original list', test1
test2 = fun1(test1)
print 'Result', test2
print 'Original list', test1

rawDescription: What does this function do?

description: What does this function do


code:
c = "abacca"
m = regex.search(rx, c)
if len(max(m.captures(1))) - len(min(m.captures(1))) == 0: # all of equal length ?
    print m.group(0)

rawDescription: How to match length of variable size backreference but not content

description: Match length of variable size backreference but not content


code:
#!/usr/bin/python

import sys
import string
from fabric.api import *
from fabric.tasks import *
from fabric.contrib import *

class StreamFilter(object):

    def __init__(self, filter, stream):
        self.stream = stream
        self.filter = filter

    def write(self,data):
        data = data.replace(self.filter, '[[TOP SECRET]]')
        self.stream.write(data)
        self.stream.flush()

    def flush(self):
        self.stream.flush()

@task
def can_you_see_the_password():
    sys.stdout = StreamFilter(env.password, sys.stdout)
    print 'Hello there'
    print 'My password is %s' % env.password

rawDescription: How to hide the password in fabric when the command is printed out?

description: Hide the password in fabric when the command is printed out


code: print [a for a,b in zip(my_list[::2],my_list[1::2]) if a == b]

rawDescription: How to get all seperate all equal pairs from a list in python

description: Get all seperate all equal pairs from a list in python


code:
# demo.py
#
# Usage:
#
#    Add some work to the queues.
#
#        python demo.py add add mul mul add
#
#    Start up Celery:
#
#        celery worker -A demo --queues add,mul

from __future__ import absolute_import

import sys
from celery import Celery

app = Celery('demo', broker  = 'redis://localhost:6379/0')
app.conf.update(CELERY_ACCEPT_CONTENT = ['pickle'])

@app.task
def add(x, y):
    print 'add({}, {}) = {}'.format(x, y, x + y)

@app.task
def mul(x, y):
    print 'mul({}, {}) = {}'.format(x, y, x * y)

def main(qs):
    for q in qs:
        func = add if q == 'add' else mul
        for i in xrange(5, 8):
            func.apply_async((i, i), queue = q)

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

rawDescription: Does a Celery worker check queues in a specific order?


code:
from itertools import izip
import random
import timeit

def test_setup(n, flag_option=4):
    setup = 'from itertools import izip\nfrom random import randint\n'
    setup += 'flag_option = {}\n'.format(flag_option)
    setup += 'changed_val = [randint(0, flag_option) for _ in xrange({})]\n'.format(n)
    setup += 'chromosome = [randint(0, 8) for _ in xrange({})]\n'.format(n)
    return setup

test_izip  = '[v if c <= flag_option else c '
test_izip += 'for c, v in izip(chromosome, changed_val)]'

test_zip   = '[v if c <= flag_option else c '
test_zip  += 'for c, v in zip(chromosome, changed_val)]'

test_enum  = '[changed_val[i] if c <= flag_option else c '
test_enum += 'for i,c in enumerate(chromosome)]'

test_orig  = 'for (i,c) in enumerate(chromosome):\n'
test_orig += '\tif c <= flag_option:\n'
test_orig += '\t\tchromosome[i] = changed_val[i]'

if '__main__' == __name__:
    for n in [10 ** i for i in xrange(7)]:
        print 'izip {}: '.format(n),
        print min(timeit.Timer(test_izip, setup=test_setup(n)).repeat(7, 1000))

        print 'zip {}: '.format(n),
        print min(timeit.Timer(test_zip, setup=test_setup(n)).repeat(7, 1000))

        print 'enum {}: '.format(n),
        print min(timeit.Timer(test_enum, setup=test_setup(n)).repeat(7, 1000))

        print 'orig {}: '.format(n),
        print min(timeit.Timer(test_orig, setup=test_setup(n)).repeat(7, 1000))

        print

rawDescription: Replace item in list with corresponding item in another list (Python)


code:
for item in tree.xpath('//tr[@class="closedCall"]'):
    callSignal = item.xpath('.//span[contains(@id, "lblIncidentNbr")]')[0].text_content()
    dispatchTime = item.xpath('.//span[contains(@id, "lblDispatchTime")]')[0].text_content()
    location = item.xpath('.//a[contains(@id, "lnkAddress")]')[0].text_content()

    print 'Call Signal: ', callSignal
    print "Dispatch Time: ", dispatchTime
    print "Location: ", location
    print "------"

rawDescription: Scraping an Element using lxml and Xpath


code:
def generateOutput(input, value=0, offset=-1):
  values = []
  for i in range(len(input)):
    if i < 1 or input[i] == input[i-1]:
      yield value
    else:  # value change in input detected
      values.append(input[i-1])
      if len(values) >= -offset:
        value = values.pop(0)
      yield value

input = [1,1,1,1,5,5,3,3,3,3,3,3,2,2,2,5,5]
print list(generateOutput(input))

rawDescription: Algorithm to offset a list of data

description: Algorithm to offset a list of data


code:
def y(i=i):
    print i

rawDescription: How to get a closure to refer to the value that a variable had at the time of its definition

description: Get a closure to refer to the value that a variable had at the time of its definition


code:
from bs4 import BeautifulSoup

page = """

    9
    38
    56

"""

soup = BeautifulSoup(page)
badges = soup.body.find('div', attrs={'class': 'badges'})
for span in badges.span.find_all('span', recursive=False):
    print span.attrs['title']

rawDescription: Beautifulsoup get span content

description: Beautifulsoup get span content


code:
def printer(self):
    print self.sender().text()

rawDescription: Cannot connect multiple widgets to the same slot


code:
for line in st.splitlines():
    if line.endswith(',5'): print line

rawDescription: Python regular expression issues

description: Python regular expression issues


code:
import re
numeric_tail = re.compile('_C\d*$')
mylist = ['summer_C','summer_C1','summer_P','summer_C123','summer_p32']
for item in mylist:
    if not numeric_tail.search(item):
        print item

rawDescription: print item if it does not ends with a given pattern python

description: Print item if it does not ends with a given pattern python


code: print """ "A word that needs quotation marks" """

rawDescription: Python - Using quotation marks inside quotation marks


code:
for item in wild_type :
    print my_find(data_1,item)

rawDescription: DNA find all matching items from a list in a string (python 2.7)


code:
import json

results = json.loads(soup.find('div', {'data-pageraillist': True})['data-pageraillist'])
for result in results:
    print result["title"]

rawDescription: find_all() not finding any results when using Beautiful Soup + Requests

description: Find_all() not finding any results when using Beautiful Soup + Requests


code:
dist={}
ex='The rain in Spain stays mainly in the plain.'
for ch in ex.lower():
    if ch.isalpha(): dist.setdefault(ch,[]).extend(ch)

out={k:''.join(val) for k,val in dist.iteritems()}.values()
print '\n'.join(sorted(out, key=lambda x: (-len(x),x)))

rawDescription: Character Distributions

description: Character Distributions


code:
ans = raw_input('Enter: ')
if not ans:
    print "You entered nothing!"
else:
    print "You entered something!"

rawDescription: How to check for a necessary input of raw_input?

description: Check for a necessary input of raw_input


code:
filename = 'MyApp.apk'
filename = filename.rsplit('.', 1)[0]

print filename

rawDescription: Regex to select a file extension

description: Regex to select a file extension


code:
import json
from bs4 import BeautifulSoup

data = """

" [{ "foo": "Bar",
"foo2": "Bar2"
}] "

"""

soup = BeautifulSoup(data)

json_data = soup.pre.text.strip('" ')
print json.loads(json_data)

rawDescription: Scraping a page with awkwardly formatted JSON in python


code:
from nltk.stem import WordNetLemmatizer
from nltk import pos_tag, word_tokenize

sentence = "I am going to buy some gifts"
tagged = pos_tag(word_tokenize(sentence))

synsets = []
lemmatzr = WordNetLemmatizer()

for token in tagged:
    wn_tag = penn_to_wn(token[1])
    if not wn_tag:
        continue

    lemma = lemmatzr.lemmatize(token[0], pos=wn_tag)
    synsets.append(wn.synsets(lemma, pos=wn_tag)[0])

print synsets

rawDescription: NLTK convert tokenized sentence to synset format


code:
from bs4 import BeautifulSoup
html = '''

    YOB: 1987

    RACE: WHITE

    GENDER: FEMALE

    HEIGHT: 5'05''

    WEIGHT: 118

    EYE COLOR: GREEN

    HAIR COLOR: BROWN

'''
soup = BeautifulSoup(html)
for strong_tag in soup.find_all('strong'):
    print strong_tag.text, strong_tag.next_sibling

rawDescription: Using BeautifulSoup Extract Text without Tags


code:
import itertools

l = [s.strip() for s in open('test.txt')]

for i in range(len(l)):
  print '\n'.join(map(' | '.join, itertools.combinations(l, i + 1)))

rawDescription: Algorithm to perform K-combinations for all k on sentences


code:
y = sympy.symbols('y')
print sech(y)
print csch(y)
print arccos(y)

rawDescription: Sympy custom function

description: Sympy custom function