code:
from BeautifulSoup import BeautifulSoup
import urllib2
import re

html_page = urllib2.urlopen("http://www.yourwebsite.com")
soup = BeautifulSoup(html_page)
for link in soup.findAll('a'):
    print link.get('href')

rawDescription: how can I get href links from html code

description: Get href links from html code


code:
import xml.etree.ElementTree as ET
tree = ET.ElementTree(file='work.xml')

indent = 0
ignoreElems = ['displayNameKey', 'displayName']

def printRecur(root):
    """Recursively prints the tree."""
    if root.tag in ignoreElems:
        return
    print ' '*indent + '%s: %s' % (root.tag.title(), root.attrib.get('name', root.text))
    global indent
    indent += 4
    for elem in root.getchildren():
        printRecur(elem)
    indent -= 4

root = tree.getroot()
printRecur(root)

rawDescription: Recursive XML parsing python using ElementTree


code:
source
from functools import partial

def do_something(foo, bar):
    """this function raises an Exception"""
    print foo,bar
    raise Exception('uhoh')

def evaluate(fcall, testname):
    "print message if func call throws exception"
    try:
        fcall()
    except Exception:
        funct = fcall.func
        print ("Error in "+ testname+ " "
               + funct.__name__
               + " " + funct.__doc__
               )

def evaluate2(testname, funct, *args, **kwargs):
    "print message if func call throws exception"
    try:
        funct(*args, **kwargs)
    except Exception:
        print ("Error in "+ testname+ " "
               + funct.__name__
               + " " + funct.__doc__
               )

evaluate(partial(do_something, foo='beer'), 'mytest')
evaluate2('mytest2', do_something, 'tasty', foo='beer')

rawDescription: How to get a function's attributes when passing it as a parameter

description: Get a function's attributes when passing it as a parameter


code:
def getDisplayDimensions(width,FormatX,FormatY):
    Format = float(FormatX)/FormatY
    if FormatX < FormatY:
        return "illegal format."
    for height in range(1,int(width)+1):
        if float(width)/height == float(Format):
            return width,height
            break
        elif height == width:
            return getDisplayDimensions(float(width)-1,FormatX,FormatY)

print getDisplayDimensions(801,16,9)

rawDescription: Function returns None after recursion

description: Function returns None after recursion


code:
#import sys
print sys.argv

rawDescription: Can I add arguments to python code when I submit spark job?

description: Add arguments to python code when I submit spark job


code:
from itertools import izip_longest

d={1: [0, 1, 0, 0, 3, 0],
 2: [1, 0, 0, 0, 0, 4, 5],
 3: [0, 5, 6, 0, 1, 1]}

# there are other variations like
# [max(column) for column in izip_longest(*d.itervalues())]
print map(max, izip_longest(*d.values()))
# [1, 5, 6, 0, 3, 4, 5]

rawDescription: mapping to the max a variable number of lists

description: Mapping to the max a variable number of lists


code:
import maya.cmds as cmds
import maya.OpenMayaUI as ui

index = cmds.displayColor("grid" , q=True)
color = ui.M3dView().colorAtIndex(index)
print color.r, color.g,  color.b

rawDescription: Maya - How to access grid colors through script commands?


code:
from lxml import objectify
table = objectify.fromstring(xml_source)
print "table name:", table.attrib['name']
for col in table.column:
    print "column name:", col.attrib['name']
    print "source type:", col.source.attrib['type']
    print "source col:",  col.source.text

rawDescription: Using an xml "map" to import data to MySQL via python?


code:
from xml.etree import ElementTree as ET

root = ET.parse("foobar.xml")
ann = root.find(".//{http://openoffice.org/2000/office}annotation")
print ann.tail.strip()

rawDescription: how can I find text in xmlns with elementtree

description: Find text in xmlns with elementtree


code:
#!/usr/bin/env python

import os, time, sys, re
from sets import Set as sett

def sorting_merge(o_file , c_file, m_file ):

    pat = re.compile('[0123]\d/[01]\d/\d{2} [012]\d:[0-6]\d:[0-6]\d'
                     '(?=[ \t]+[0123]\d/[01]\d/\d{2} [012]\d:[0-6]\d:[0-6]\d)')

    def kl(line,pat = pat):
        return time.mktime(time.strptime((pat.search(line).group()),'%d/%m/%y %H:%M:%S'))

    output = sett()
    head = []

    fa = open(o_file)
    fa.readline() # first line is skipped
    while True:
        line1 = fa.readline()
        mat1  = pat.search(line1)
        if not mat1: head.append(line1) # line1 is here a line of the header
        else: break # the loop ends on the first line1 not being a line of the heading
    output = sett( fa )
    fa.close()

    fb = open(c_file)
    while True:
        line1 = fb.readline()
        if pat.search(line1):  break
    output = output.union(sett( fb ))
    fb.close()

    output = [ (kl(line),line.rstrip()) for line in output]
    output.sort()

    fm = open(m_file,'w')
    fm.write(time.strftime('On %d/%m/%y %H:%M:%S\n')+(''.join(head)))
    for t,line in output:
        fm.write(line + '\n')
    fm.close()

te = time.clock()
sorting_merge('ytre.txt','tataye.txt','merged.file.txt')
print time.clock()-te

rawDescription: How to merge only the unique lines from file_a to file_b?

description: Merge only the unique lines from file_a to file_b


code:
import re
x="{key1 value1} {key2 value2} {key3 {value with spaces}}"
print dict(re.findall(r"\{(\S+)\s+\{*(.*?)\}+",x))

rawDescription: Turning string with embedded brackets into a dictionary


code: print l[-2:]

rawDescription: How can I "count from the back" in python lists?

description: "count from the back" in python lists


code:
import fileinput

ignore = False
for line in fileinput.input('inp.txt', inplace=True):
    if not ignore:
        if line.startswith('# my comment'):
            ignore = True
        else:
            print line,
    if ignore and line.isspace():
        ignore = False

rawDescription: Deleting n number of lines after specific line of file in python


rawDescription: Python decode "\u041b" string

description: Python decode "\u041b" string


code:
def test_var_args_call(*args):
    print args

rawDescription: Function with extensive arguments

description: Function with extensive arguments


code:
def frob(f, x):
    if str(x) == str(x)[::-1]:
        return x
    else:
        return f(f, x + int(str(x)[::-1]))

print frob(frob, 29)

rawDescription: Generating list until certain condition is met using a single line


code: print s % tuple(x)

rawDescription: Using Python String Formatting with Lists


code:
import argparse

def func(foo=1, bar=True):
    """Sample function.

        Parameters:
            foo: float
                An example argument.
            bar: bool
                Another argument.
    """
    print foo, bar

parser = argparse.ArgumentParser(prog='script.py',
    description=func.__doc__,
    formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-f', '--foo', type=float)
parser.add_argument('-b', '--bar', action='store_false')
parser.print_help()

rawDescription: Python’s argh library: preserve docstring formatting in help message


code:
for search_text in ('HDMI ports quantity', 'DVI port'):
    pattern = re.compile(r'\s*%s\s*' % search_text)
    ds_data = soup.find(text=pattern).findNext('div', {'class': 'ds_data'})
    if ds_data.find('img', {'class': 'spacer_top n-sign'}):
        result_text = '0'
    else:
        result_text = ds_data.text
    print search_text, result_text

rawDescription: Can I return a text value if a certain tag is found?

description: Return a text value if a certain tag is found


code:
list1, list2 = [5, 4, 7, 1, 9], [3, 1, 10, 11, 5]
indices1 = range(len(list1))
indices1.sort(key=lambda x: list2[x])
list1 = map(lambda i: list1[i], indices1)
print list1, list2

rawDescription: sorting list based on other array contents

description: Sorting list based on other array contents


code:
print qtwi.backgroundColor()
print qtwi.backgroundColor()

rawDescription: PyQt QColor setAlpha not working?

description: PyQt QColor setAlpha not working


code:
def extract_entities(text):
    for sent in nltk.sent_tokenize(text):
        for chunk in nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(sent))):
            if hasattr(chunk, 'node'):
                print chunk.node, ' '.join(c[0] for c in chunk.leaves())

extract_entities("to play to Atlanta")

#Output: [('to', 'TO'),('play', 'VB'),('to', 'TO'),('play', 'NN')],

rawDescription: Python NLTK interpret a fixed pattern of sentence and tokenize it


code:
def FindDuplicates(in_list):
    duplicates = []
    unique = set(in_list)
    for each in unique:
        count = in_list.count(each)
        if count > 1:
            duplicates.append(each)
    print duplicates

rawDescription: return duplicates in a list

description: Return duplicates in a list


code:
try:
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
except ValueError:
    print "Your gunners were asleep on the job!"
    if turn == 3:
        print "Game Over"
    continue

rawDescription: Сodecademy 18/19 Battleship

description: Сodecademy 18/19 Battleship


code:
doc = etree.fromstring(xml)
btags = doc.xpath('//a/b')
for b in btags:
    print b.text

rawDescription: Efficient way to iterate throught xml elements


code:
from functools import partial
import requests

def some_request(uri, post):
    req = requests.post(uri, post)
    return req

uri = 'http://someuri.com'
post = {'test': 'yes'}

try:
    retry_func(partial(some_request, uri, post), max_retry=3)
except RetryException, e:
    print(e)

rawDescription: Pythonic way of retry running a function


code:
def retrieve_group_byuser(self, username):
    # retrieves group by username
    query = self.session.query(Group).\
    filter(Group.users.any(User.username == username)).all()
    print "retrieved by username:", query
    return query

rawDescription: Entity Relational Model Design and Query Method


code:
import ast
import re

def collect_offsets(call_string):
    def _abs_offset(lineno, col_offset):
        current_lineno = 0
        total = 0
        for line in call_string.splitlines():
            current_lineno += 1
            if current_lineno == lineno:
                return col_offset + total
            total += len(line)
    # parse call_string with ast
    call = ast.parse(call_string).body[0].value
    # collect offsets provided by ast
    offsets = []
    for arg in call.args:
        a = arg
        while isinstance(a, ast.BinOp):
            a = a.left
        offsets.append(_abs_offset(a.lineno, a.col_offset))
    for kw in call.keywords:
        offsets.append(_abs_offset(kw.value.lineno, kw.value.col_offset))
    if call.starargs:
        offsets.append(_abs_offset(call.starargs.lineno, call.starargs.col_offset))
    if call.kwargs:
        offsets.append(_abs_offset(call.kwargs.lineno, call.kwargs.col_offset))
    offsets.append(len(call_string))
    return offsets

def argpos(call_string):
    def _find_start(prev_end, offset):
        s = call_string[prev_end:offset]
        m = re.search('(\(|,)(\s*)(.*?)$', s)
        return prev_end + m.regs[3][0]
    def _find_end(start, next_offset):
        s = call_string[start:next_offset]
        m = re.search('(\s*)$', s[:max(s.rfind(','), s.rfind(')'))])
        return start + m.start()

    offsets = collect_offsets(call_string)

    result = []
    # previous end
    end = 0
    # given offsets = [9, 14, 21, ...],
    # zip(offsets, offsets[1:]) returns [(9, 14), (14, 21), ...]
    for offset, next_offset in zip(offsets, offsets[1:]):
        #print 'I:', offset, next_offset
        start = _find_start(end, offset)
        end = _find_end(start, next_offset)
        #print 'R:', start, end
        result.append((start, end))
    return result

if __name__ == '__main__':
    try:
        while True:
            call_string = raw_input()
            positions = argpos(call_string)
            for p in positions:
                print ' ' * p[0] + '^' + ((' ' * (p[1] - p[0] - 2) + '^') if p[1] - p[0] > 1 else '')
            print positions
    except EOFError, KeyboardInterrupt:
        pass

rawDescription: Parsing Python function calls to get argument positions


code:
from xml.etree import ElementTree as ET

xml = '<root>start here<child1>some text<sub1/>here</child1>and<child2>here as well<sub2/><sub3/></child2>end here</root>'
root = ET.fromstring(xml)

def content(tag):
    return tag.text + ''.join(ET.tostring(e) for e in tag)

print content(root)
print content(root.find('child2'))

rawDescription: Python and ElementTree: return "inner XML" excluding parent element


code:
class MyClass(object):
    def create(self, itemType, itemName):
        print "Creating item %s with name %s" % (itemType, itemName)

    def __getattr__(self, attrName):
        def create_wrapper(name):
            self.create(attrName, name)
        return create_wrapper

rawDescription: Python : Allowing methods not specifically defined to be called ala __getattr__


code:
#list of lines
lines = ['34512340', '0.5\r\n', 'Plain Brackets'];

#looping through the whole list
for i in range(len(lines)):

    #stripping unwanted characters \n and \r from each line
    lines[i] = lines[i].rstrip('\n').rstrip('\r')

    #printing the line without the unwanted characters
    print lines[i]

rawDescription: Stripping Unwanted Characters When Writing To A File


code:
from collections import Sequence

class SlicedList(Sequence):
    def __init__(self, iterable, size):
        self.seq = list(iterable)
        self.slices = [slice(i,i+size) for i in range(0,len(self.seq), size)]

    def __contains__(self, item):
        # checks if a item is in this sequence
        return item in self.seq

    def __iter__(self):
        """ iterates over all slices """
        return (self.seq[slice] for slice in self.slices)

    def __len__(self):
        """ implements len( .. ) """
        return len(self.slices)

    def __getitem__(self, n):
        # two forms of getitem ..
        if isinstance(n, slice):
            # implements sliced[a:b]
            return [self.seq[x] for x in self.slices[n]]
        else:
            # implements sliced[a]
            return self.seq[self.slices[n]]

s = SlicedList(range(100), 5)

# length
print len(s) # 20

#iteration
print list(s) # [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], ... , [95, 96, 97, 98, 99]]
# explicit iteration:
it = iter(s)
print next(it) # [0, 1, 2, 3, 4]

# we can slice it too
print s[0], s[-1] # [0, 1, 2, 3, 4] [95, 96, 97, 98, 99]
# get the first two
print s[0:2] # [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
# every other item
print s[::2] # [[0, 1, 2, 3, 4], [10, 11, 12, 13, 14], [20, 21, 22, 23, 24], ... ]

rawDescription: python: how to slice/store iter pointed data in a fixed buffer class?

description: Python: how to slice/store iter pointed data in a fixed buffer class


code:
def solution(S):
    length = len(S)
    if length == 0:
        return 1
    count = 0
    i = 1 # Don't test the empty string!
    while i <= (length + 1)/2: # Test up to halfway
        prefix = S[:i] # Up to i
        suffix = S[:length-i-1:-1] # Reverse string, up to length - i - 1
        print prefix, suffix
        if prefix == suffix:
            count += 1
        i += 1
    return count

print solution("")
print solution("abbabba")
print solution("codility")

rawDescription: Intersection between two list of strings python


code:
def my_other_func(a, *b, **c):
    print a, b, c

rawDescription: Python newbie: What does this code do?

description: Python newbie: What does this code do


code:
print float('AAAA'.count('AA')) / (len('AAAA') - 1)
0.666666

rawDescription: Frequencies not adding up to one

description: Frequencies not adding up to one


code:
from heapq import nsmallest
for name, score in nsmallest(10, names.iteritems(), key=lambda (k, v): (-v, k)):
    print name, score

rawDescription: How to get the top 10 students in the class using Python dict

description: Get the top 10 students in the class using Python dict


code:
arr = ["a","b","c"] #random number of elements
for i in range(len(arr)):
    def f(val=arr[i]):
        print val
    but = Button(None,text="Press Here",command=f).pack()

rawDescription: Create a random number of function

description: Create a random number of function


code:
import re
yourstring = 'type=weaksubj len=1 word1=wrestle pos1=verb stemmed1=y priorpolarity=negative'

m = re.search(r'word1=(\w+)', yourstring)
print m.group(1)

rawDescription: Regular Expressions

description: Regular Expressions


code:
soup = BeautifulSoup(html_doc)
content = soup.find('blockquote',attrs={'class':'messagetext'})

for tag in content.findChildren():
    if tag.name == 'div':
        tag.clear()

print content.text

rawDescription: How to skip a particular tag and crawl other tag's text in Beautifulsoup

description: Skip a particular tag and crawl other tag's text in Beautifulsoup


code:
def countHits(file):
    f2 = open(file,'rU')
    l2 = f2.readlines()
    f2.close()
    l2 = map(lambda x: x.strip(), l2)
    user_input = raw_input(
       "Enter the URL that you wish to chck fr the nmber of Hits"
    )
    print "The number of HITS for the given STRING/URL is : %s"% (
       l2.count(user_input)
    )

rawDescription: Issue with List count method

description: Issue with List count method


code:
for i in xrange(value):
    z = i**2
    if(i==1000000):
        print i
    if z < i:
        print "yes"

rawDescription: Why there is a huge performance difference between these two codes in Python and Cython?


code:
from itertools import izip_longest
import csv
import StringIO

data='''table1    table2    table3  table4   table5
paper     paper     pen     book     book
pen       pencil    pencil  charger  apple
apple     pen       charger beatroot sandle
beatroot  mobile    apple   pen      paper
sandle    book      paper   paper'''

f = StringIO.StringIO(data)
reader = csv.reader(f, delimiter=' ', skipinitialspace=True)
cols = map(set, izip_longest(*reader))

print set.intersection(*cols)

rawDescription: common between all csv columns

description: Common between all csv columns


code:
data = "hello world,    this, is       John"
import re
print re.split("\W+", data)
# ['hello', 'world', 'this', 'is', 'John']

rawDescription: Python regex splitting on multiple whitespaces


code:
from pyparsing import *

s = r"foo = 'ab\'cd\\'"  # <--- IMPORTANT - use a raw string literal here

ident = Word(alphas)
strValue = QuotedString("'", escChar='\\')
strAssign = ident + '=' + strValue

results = strAssign.parseString(s)
print results.asList() # displays repr form of each element

for r in results:
    print r # displays str form of each element

# count the backslashes
backslash = '\\'
print results[-1].count(backslash)

rawDescription: With pyparsing, how do you parse a quoted string that ends with a backslash


code:
d = {i: i * 2 for i in range(10000)}
print 10 in d.viewvalues()
# True

rawDescription: Reversing dictionary: time and space

description: Reversing dictionary: time and space


code:
input = 'the flory of gthys inhibition in this proffession by in aquaporin protein-1 its inhibition b , aquaporin protein-1'

a = 'aquaporin protein-1'
b = 'inhibition'

multiwords = ['aquaporin protein-1']
for mw in multiwords:
    mw_no_space = mw.replace(' ', '__')
    text = input.replace(mw, mw_no_space)
k1 = a.replace(' ', '__')
k2 = b.replace(' ', '__')

l = text.split()

d_idx = {k1:[], k2:[]}
for k,v in enumerate(l):
    if v == k1:
        d_idx[k1].append(k)
    elif v == k2:
        d_idx[k2].append(k)

distance = 10
data = []
for idx1 in d_idx[k1]:
    for idx2 in d_idx[k2]:
        d = abs(idx1 - idx2)
        if d<=distance:
            data.append((d,idx1,idx2))

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

print "Least distance: ", data[0][0]
print "Index of kw1 and kw2: ", data[0][1:]
print "Number of occurences: ", len(data)

rawDescription: [tricky]Searching for multiple occurrences of word pairs based on vicinity. Python


code:
from collections import defaultdict

worddictlist2 = []
for innertweet in namelist:
    worddict = defaultdict(lambda: 0)
    category = innertweet[1]
    for tweet in innertweet[0]:
        for word in wordlist:
            worddict[word] += tweet.count(word)

    # optional - transform defaultdict into standard dict to make it printable
    worddictClean = {}
    worddictClean.update(worddict)

    worddictlist2.append([worddictClean, category])

print worddictlist2

rawDescription: Trying to manipulate data, assign a list to the first item in a higher list, second item will be information about that list


code:
from BeautifulSoup import BeautifulSoup

text = """

PLZ:

8047

"""

number = BeautifulSoup(text).find("dt",text="PLZ:").parent.findNextSiblings("dd")
print BeautifulSoup(''.join(number[0]))

rawDescription: Using beautifulsoup to parse tag with some text


code:
with open('data.txt') as inf:
    ysum = 0
    for line in inf:
        line = line.split()
        x, y = [float(i) for i in line]
        ysum += y
        print x, ysum

rawDescription: the sum of values of a second column of a file

description: The sum of values of a second column of a file


rawDescription: how do I pass a var including the quotes("")

description: Pass a var including the quotes("")


code: print type(someObject).__name__

rawDescription: Convert a python 'type' object to a string


code:
from collections import OrderedDict
data = OrderedDict(zip(('first', 'second', 'last'), (1, 10, 3)))
print json.dumps(dict(data)) # {"second": 10, "last": 3, "first": 1}
print json.dumps(data) # {"first": 1, "second": 10, "last": 3}

rawDescription: How do I sort objects inside of objects in JSON? (using Python 2.7)

description: Sort objects inside of objects in JSON? (using Python 2.7)


code:
consonant = ('b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z')
print "\n".join(["%so%s" % ( letter, letter) for letter in raw_input("type smth: ") if letter in consonant])

rawDescription: Getting wrong output when working with tuple and list in Python


code:
import urllib2
from BeautifulSoup import BeautifulSoup

q = urllib2.urlopen('http://stackoverflow.com/questions/3884419/')
soup = BeautifulSoup(q.read())

for link in soup.findAll('a'):
    if link.has_key('href'):
        print str(link.string) + " -> " + link['href']
    elif link.has_key('id'):
        print "ID: " + link['id']
    else:
        print "???"

rawDescription: How would I make a simple URL extracter in Python?


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

subject = '{"page":1,"results": [{"adult":false,"backdrop_path":"/ba4CpvnaxvAgff2jHiaqJrVpZJ5.jpg","id":807,"original_title":"Se7en","release_date":"1995-09-22","p'

match = re.search('"id":([^,]+)', subject)
if match:
    result = match.group(1)
else:
    result = "no result"
print result

rawDescription: Python Regex Match Integer After String

description: Python Regex Match Integer After String


rawDescription: How to clear terminal (Mac osx) scrollback?

description: Clear terminal (Mac osx) scrollback


code:
from multiprocessing import Pool

def f(x):
    return x*x
def func(num):
    pool = Pool(processes=4)
    result = pool.apply_async(f, [10])
    print result.get(timeout=1)
    print pool.map(f, range(num))

if __name__ == '__main__':
    func(1000)

rawDescription: Python multiprocessing: call module with multiprocessing from another script with passing variable


code:
import xml.etree.cElementTree as et
# Produces identical answers with import lxml.etree as et
import cStringIO

def normtext(t):
    return repr("" if t is None else t.strip())

def dump(el):
    print el.tag, normtext(el.text), normtext(el.tail), el.attrib

def my_filtered_elements(source, skip_parent_tag="feature"):
    # get an iterable
    context = et.iterparse(source, events=("start", "end"))
    # turn it into an iterator
    context = iter(context)
    # get the root element
    event, root = context.next()
    tag_stack = [None, root.tag]
    for event, elem in context:
        # print event, elem.tag, tag_stack
        if event == "start":
            tag_stack.append(elem.tag)
        else:
            assert event == "end"
            my_tag = tag_stack.pop()
            assert my_tag == elem.tag
            parent_tag = tag_stack[-1]
            if parent_tag is not None and parent_tag != skip_parent_tag:
                dump(elem)
                # yield elem
            root.clear()

def other_filtered_elements(source, skip_parent_tag="feature"):
    in_feature_tag = False
    for event, element in et.iterparse(source, events=('start', 'end')):
        if element.tag == skip_parent_tag:
            in_feature_tag = event == 'start'
        if event == 'end' and not in_feature_tag:
            dump(element)

test_input = """
<top>
    <lev1 guff="1111">
        <lev2>aaaaa</lev2>
        <lev2>bbbbb</lev2>
    </lev1>
    <feature>
        feat text 1
        <fchild>fcfcfcfc
            <fgchild>ggggg</fgchild>
        </fchild>
        feat text 2
    </feature>
    <lev1 guff="2222">
        <lev2>ccccc</lev2>c-tail
        <lev2>ddddd</lev2>d-tail
        <notext1></notext1>e-tail
        <notext2 />f-tail
     </lev1>g-tail
</top>
"""

print "=== me ==="
my_filtered_elements(cStringIO.StringIO(test_input))
print "=== other ==="
other_filtered_elements(cStringIO.StringIO(test_input))

rawDescription: How can I know the parent of an element when using iterparse methode of cElementTree?

description: Know the parent of an element when using iterparse methode of cElementTree


code:
a = False
b = ""
c = "test"
d = {}
e = []
f = ["Test", "Test"]
g = ("One", "1", "Two", "2")
for k, v in vars().items():
    if not (k.startswith('__') and k.endswith('__')):
        print k,'--',v

rawDescription: Print / view all variables whilst a script is running in python


code: print type(passedDict)

rawDescription: Iterate a tuple of dictionaries and pass the nested dictionaries to a function


code:
for line in lines:
    if re.match(r'^http://www.toto.com/(\w+/){4}$', line):
        print line

rawDescription: Regexp on specific URL

description: Regexp on specific URL


code:
def test1():
    print "Inside test1()"
    def test2():
        print "Inside test2()"
        def test3():
            print "Inside test3()"
            return "Foo is pretty."
        return test3
    return test2

print test1()()() # first way, prints the return string "Foo is pretty."

test2 = test1() # second way
test3 = test2()
print test3() # prints "Foo is pretty."

rawDescription: How do I call function inside another function?

description: Call function inside another function


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
for a in soup.find_all('a', text='View Rollover Dates'):
    print a.previous_element

rawDescription: Beautifulsoup Python nested text

description: Beautifulsoup Python nested text


code:
def add_to(amount):
    def f(x):
        return x + amount
    return f

if __name__ == "__main__":
    add_2 = add_to(2)
    add_3 = add_to(3)

    print add_2(42)
    print add_3(42)

rawDescription: Using a function to create a function in Python


code:
import urllib2
from bs4 import BeautifulSoup

url = 'http://www.homedepot.com/p/Husky-41-in-16-Drawer-Tool-Chest-and-Cabinet-Set-HOTC4016B1QES/205080371'
soup = BeautifulSoup(urllib2.urlopen(url=url))

print [meta.get('content') for meta in soup.find_all('meta', itemprop='datePublished')]

rawDescription: beautifulsoup to retrieve the date

description: Beautifulsoup to retrieve the date


code:
import re

fn_list = ['IMG_0064.png',
           'IMG_0064.JPG',
           'IMG_0064_1.JPG',
           'IMG_0064_2.JPG',
           'IMG_0064_2.PNG',
           'IMG_0064_2.BMP',
           'IMG_0064_3.JPEG',
           'IMG_0065.JPG',
           'IMG_0065.JPEG',
           'IMG-20150623-00176-preview-left.jpg',
           'IMG-20150623-00176-preview-left_2.jpg',
           'thumb_2595.bmp',
           'thumb_2595_1.bmp',
           'thumb_2595_15.bmp']

pattern = re.compile(r'(\d|t)(_\d+){1}\.', re.IGNORECASE)

for line in fn_list:
    search_obj = re.search(pattern, line)  # CHANGED HERE
    if search_obj:
        matching_group = search_obj.groups()
        print matching_group

rawDescription: Regex works fine on Pythex, but not in Python


code:
soup = BeautifulSoup(page)
for td in soup.find_all('td', class_='prodSpecAtribute'):
    print td.contents[0]

rawDescription: Regex in Python for html

description: Regex in Python for html


code:
import re
text = 'I have a problem in Regex, How do I get rid of the Capital I\'s provided I want to retain words occurring together as logical entity with a Capital letter in the beginning of each word like International Business Machine'
found = re.findall('(?:[A-Z](?:[\w]|(?<=\w\w)\')+\s?)+',text)
print found

rawDescription: Getting rid of few entities using regex python


code:
# Parsing information from games4you.ucoz.ua

import csv
import urllib
from bs4 import BeautifulSoup

BASE_URL = "http://games4you.ucoz.ua/news/"

def get_html(url):
    response = urllib.urlopen(url)
    return response.read()

def get_page_count(html):
    soup = BeautifulSoup(html)
    paggination = soup.find('div', class_='catPages1')
    return int(paggination.find_all('a')[-2].text)

def save(games, path):
    with open(path, 'w+') as csvfile:
        writer = csv.writer(csvfile)

        writer.writerow(('Title', 'Category', 'Date', 'Time'))

        writer.writerows(
            (game['title'],game['category'], game['date'], game['time']) for game in games
        )

def parse(html):
    soup = BeautifulSoup(html)
    # Getting the <div> that contains all posts on page
    all_entries = soup.find('div',id='allEntries')

    # Getting all of the posts (every table represents one post)
    tables = all_entries.find_all('table',class_='eBlock')

    # Creating a list o dictionaries for games information
    games = []
    for table in tables:

        try:
            # Getting the game title
            game_title = table.tr.td.a.text
            game_post_body = table.find('div',class_='eMessage')
            # Getting the game description
            game_description = game_post_body.p.text.split('....')[0] + '.'
            game_details = table.find('div',class_='eDetails')
            # Getting the game category
            game_category = game_details.a.text
            game_post_details = game_details.text
        except:
            print 'Some error'
            continue

        # Getting the post views count
        post_views = game_post_details[:game_post_details.find('function')].split()[-2]

        # Getting the post date
        post_date = game_details.span.text
        # Getting the post time
        post_time = game_details.span['title']

        # Converting all data from Unicode to Utf-8
        game_title = game_title.encode("utf-8")
        game_category = game_category.encode("utf-8")
        game_description = game_description.encode("utf-8")
        post_date = post_date.encode("utf-8")
        post_time = post_time.encode("utf-8")

        # Writing data to the list
        games.append({
            'title': game_title,
            'category' : game_category,
            'description' : game_description,
            'date' : post_date,
            'time' : post_time
        })

    return games

def main():
    total_pages = get_page_count(get_html(BASE_URL))
    print('Total found %d pages...' % total_pages)

    games = []

    for page in range(1, total_pages + 1):
        print('Parsing %d%% (%d/%d)' % (page / total_pages * 100, page, total_pages))
        games.extend(parse(get_html(BASE_URL + "?page%d" % page)))

    print('Saving...')
    save(games, 'games.csv')

main()

rawDescription: How to parse russian text properly using Python 2.7 and BeautifulSoup?

description: Parse russian text properly using Python 2.7 and BeautifulSoup


code:
new_data = ('%02X' %i for i in data)
print ''.join(new_data)

rawDescription: Convert Integer to Hex with result always having 2 bytes


code:
word="HELLO WORLD"
word2=[ord(c) for c in word]
print(word2)

shifted_by_3 = [num + 3 for num in word2]
print(shifted_by_3)

word3 = [chr(num) for num in shifted_by_3]
print word3

rawDescription: Changing ASCII values in a list

description: Changing ASCII values in a list


code:
import matplotlib.pylab as plt
import pandas as pd
fig, ax = plt.subplots(2, 3)
for row in range(2):
    for col in range(3):
        f_index = range(50, 101, 10)[row+1 * col]
        print row, col, f_index
        Var=(700* f_index)/ 100
        Names1 = ['ig','M_GZ']
        Data1 = pd.read_csv('~/File/JTL_'+str(Var)+'/GZ.csv', names=Names1)
        ig = Data1['ig']
        M_GZ=Data1['M_GZ']
        MGZ = Data1[Data1.M_GZ != 0]
        ax[row, col].plot(MGZ['ig'][:4], MGZ['M_GZ'][:4], '--v',linewidth=1.75)
plt.tight_layout()
plt.show()

rawDescription: Plot panda series in separate subplots using matplotlib


code:
for row in S.iterrows():
    M = np.matrix( [row[1]['x'], row[1]['center']] )
    print pdist(M, 'euclidean')

rawDescription: Measure distance between data set of 5D

description: Measure distance between data set of 5D


code:
f = open("t.txt")

import re

identifier=None
brace_level=0
section=None
clause=None
req_clause_found=False
def in_req_clause(): return section=='WFS' and clause=='trees'

for l in (l.strip() for l in f):
    if req_clause_found and not in_req_clause(): break

    m=re.match(r'[A-Z]+',l)    #adjust if section names can be different
    if m and section is None:
        identifier=m.group(0)
        continue
    m=re.match(r'\{(\s|$)',l)
    if m:
        brace_level+=1
        if identifier is not None and brace_level==1:
            section=identifier
            identifier=None
        continue
    else: identifier=None
    m=re.match(r'\}(\s|$)',l)
    if m:
        brace_level-=1
        if brace_level==0: section=None
        clause=None
        continue
    m=re.match(r'-([A-Za-z]+)',l)   #adjust if clause names can be different
    if m and brace_level==1:
        clause=m.group(1)
        continue
    m=re.match(r'"(.*)"$',l)
    if m and in_req_clause():
        print m.group(1)
        req_clause_found=True
        continue

rawDescription: get the path in a file inside {} by python

description: Get the path in a file inside {} by python


code:
my_array = []
for i in xrange(number_of_times):
    my_array.append(raw_input('Input: '))

print my_array  # Shows you the results

rawDescription: Fill array from raw_input

description: Fill array from raw_input


code:
file_contents = open(file).readlines()  #file as list
"""use zip to get the entire file as list of 5-line chunk tuples"""
five_line_increments = zip(*[iter(file_contents)]*5)
for chunk in five_line_increments:  #for each 5-line chunk...
    word_freq = {} #word frequencies for each chunk
    for sentence in chunk:          #...and for each sentence in that chunk
        words = "".join(sentence.split('\t')[3]).strip('\n').split(', ') #get the ITEMS column at index 3 and put them in list
        for word in words:
            if word not in word_freq:
                word_freq[word] = 1
            else:
                word_freq[word] += 1

    print word_freq

rawDescription: Extract items from n-line chunks in a file, count frequency of items for each chunk, Python


code:
from itertools import chain

wordlist = ["ONE","TWO","THREE","FOUR"]
string = ''.join(wordlist) # "ONETWOTHREEFOUR"
stringpositions = [6,7,8,0,4]

# construct a list that maps every position in string to a single source word
which_word = list(chain( [ii]*len(w) for ii, w in enumerate(wordlist) ))

# it's now trivial to use which_word to construct the set of words
# represented in the list stringpositions
words_used = set( which_word[pos] for pos in stringpositions )

print "which_word=", which_word
print "words_used=", words_used

rawDescription: Map string positions to list positions

description: Map string positions to list positions


code:
from bs4 import BeautifulSoup

html = """

    1
    2

"""

soup = BeautifulSoup(html)

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

print '/'.join(link.split('/')[:-1])

rawDescription: Improving a python snippet

description: Improving a python snippet


code: print validate_email(item.strip())

rawDescription: Python Validate_Email Doesn't work when pulling from MongoDB


code:
if e.type == pygame.QUIT:
   print 'Close!'
   self.isRunning = 0
   running = 0

rawDescription: Confusion with python global variables, pygame event


code:
def callback(*args):
    print "variable changed!"

var = DoubleVar()
var.trace("w", callback)

rawDescription: Python Tkinter update when entry is changed


code:
#!/usr/bin/env python

DNAstring = '''AAACAACAGGGTACAAAGAGTCACGCTTATCCTGTTGATACT
TCTCAATGGGCAGTACATATCATCTCTNNNNNNNNNNNNNNNNNNNNNNN
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
NNNNNNNNNNNNNNNNNNNNNNNNNNNAAAACGTGTGCATGAACAAAAAA
CGTAGCAGATCGTGACTGGCTATTGTATTGTGTCAATTTCGCTTCGTCAC
TAAATCAACGGACATGTGTTGC'''

sequences = [u for u in DNAstring.split('N') if u and u != '\n']

for i, seq in enumerate(sequences):
    print i
    print seq.replace('\n', '') + '\n'

rawDescription: Python splitting with string as delimiter


code: print '\n'.join('\t' * p + '\\'.join(x[p:]) for p, x in eachWithPrefix(x.split('\\') for x in v))

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

description: Group strings with similar beginnings in a sorted list


code:
def line_intersection(line1, line2):
    xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
    ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1]) #Typo was here

    def det(a, b):
        return a[0] * b[1] - a[1] * b[0]

    div = det(xdiff, ydiff)
    if div == 0:
       raise Exception('lines do not intersect')

    d = (det(*line1), det(*line2))
    x = det(d, xdiff) / div
    y = det(d, ydiff) / div
    return x, y

print line_intersection((A, B), (C, D))

rawDescription: How do I compute the intersection point of two lines in Python?

description: Compute the intersection point of two lines in Python


code:
class MyClass(object):
    def __init__(self, version):
        self.version = version
    def __repr__(self):
        return self.version

myObjs = {}
o = MyClass("1")
myObjs[o] = o
print type(o)
print o.__class__
print myObjs
print type(myObjs[o])

rawDescription: python __repr__

description: Python __repr__


code: print self.RadIPAddressInfo.GetStringSelection(self) #returns selected item string

rawDescription: How do I determine which button in a RadioBox is selected in wxPython?

description: Determine which button in a RadioBox is selected in wxPython


code:
class Exact(xml.sax.handler.ContentHandler):
    def __init__(self, stdpath):
        self.stdpath = stdpath

        self.thisrow = {}
        self.curpath = []
        self.getvalue = None

        self.conn = MySQLConnect()
        self.table = None
        self.numrows = 0

    def __del__(self):
        self.conn.close()

        print '%s rows affected' % self.numrows

    def startElement(self, name, att):
        self.curpath.append(name)

    def characters(self, data):
        if self.getValue is not None:
            self.thisrow[self.getValue.strip()] = data.strip()
            self.getValue = None

    def endElement(self, name):
        self.curpath.pop()

        if name == self.stdpath[len(self.stdpath) - 1]:
            self.EndRow()
            self.thisrow = { }

    def EndRow(self):
        self.numrows += MySQLInsert(self.conn, self.thisrow, True, self.table)
        #for k, v in self.thisrow.iteritems():
        #   print '%s: %s,' % (k, v),
        #print ''

    def curPath(self, full=False):
        if full:
            return ' > '.join(self.curpath)
        else:
            return ' > '.join(self.curpath).replace(' > '.join(self.stdpath) + ' > ', '')

rawDescription: Parsing XML with Python xml.sax: How does one "keep track" of where in the tree you are?


code:
hits.sort()

for x in hits:
    d = x[0]
    date = "%d/%d/%d"%(getattr(d,'month'),getattr(d,'day'), getattr(d,'year'))
    final = str(date)+"\t\t"+str(x[1])

    print final

rawDescription: Python Google Calendar sort by date

description: Python Google Calendar sort by date


code:
import re

value = 'h&(()^%el!!l000o w@0or!ld'
value = ''.join(re.findall('[a-zA-Z\s]+',value))
print value

rawDescription: How to Preserve Whitespace (Regex)

description: Preserve Whitespace (Regex)


code:
def recursivefun(word):
    if len(word) == 2:
        return word
    else:
        x = []
        x.append(word)
        x.extend(recursivefun(word[1:]))
        x.append(word)
        return x

print recursivefun("banana")

rawDescription: Scheme to Python: most elegant translation of a recursive procedure?


code:
from vardata import verb_list, other_list
import random

print random.choice(verb_list)

rawDescription: python using variables from another file

description: Python using variables from another file


code:
#! /usr/bin/env python
import subprocess
import sys

def main(argv):
    filename = argv[1]
    match=argv[2]
    p = subprocess.Popen(['tail', '-n', '0', '-f', filename], stdout=subprocess.PIPE)
    while True :
        line = p.stdout.readline()
        print line ,
        if match in line :
            break
    p.terminate()

if __name__ == "__main__":
    main(sys.argv)

rawDescription: Automatically stop "tail -f catalina.out" when exception found or server is running


code:
import unittest

class TestNightlife(unittest.TestCase):
    _my_param = 0

    def test_a(self):
        print 'object id: %d' % id(self)
        print 'test A = %d' % self._my_param
        self._my_param = 1

    def test_b(self):
        print 'object id: %d' % id(self)
        print 'test B = %d' % self._my_param
        self._my_param = 2

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

rawDescription: self.attr resets between tests in unittest.TestCase

description: Self.attr resets between tests in unittest.TestCase


code:
i = 0

def foo():
    # here i will be lookup in the global namespace *when foo will be executed*
    print i

foo() # => 0
i = 1
foo() # => 1

# if we want to force the "evaluation" of i at function definition,
# we have to put it in the function definition
def bar(i=i):
    # here i is the function argument, and it default value is the value of the "global i" at the time the function was defined
    print i

bar() # => 1
i=2
bar() # => 1
foo() # => 2

rawDescription: Issue with generating ttk checkboxes in loops and passing arguments


code:
import pandas as pd
import numpy as np
import io
import datetime as dt

temp=u"""TRX_DATE;A
2013-07-05;1
2013-08-06;1
2015-09-05;2
2015-10-08;2
2015-11-05;2
2015-11-25;2
2015-12-06;3"""

df = pd.read_csv(io.StringIO(temp), sep=";", parse_dates=[0])
print df
#    TRX_DATE  A
#0 2013-07-05  1
#1 2013-08-06  1
#2 2015-09-05  2
#3 2015-10-08  2
#4 2015-11-05  2
#5 2015-11-25  2
#6 2015-12-06  3

print df.dtypes
#TRX_DATE    datetime64[ns]
#A                    int64
#dtype: object

currentdate = dt.date.today()
print currentdate
#2015-11-06

startdate = currentdate - pd.Timedelta(days=30)
print startdate
#2015-10-07

dflast30 = df[(df.TRX_DATE >= startdate) & (df.TRX_DATE <= currentdate)]
print dflast30
#    TRX_DATE  A
#3 2015-10-08  2
#4 2015-11-05  2

rawDescription: how to slice a dataframe having date field as index?

description: Slice a dataframe having date field as index


code:
proc = subprocess.Popen("third_party.exe", stdout=subprocess.PIPE, bufsize=1)
print proc.communicate()

rawDescription: Python subprocess output on windows?

description: Python subprocess output on windows


code:
def myprint(x):
    print x

_=[ myprint(x) for x in range(5)]

rawDescription: python in-line for loops

description: Python in-line for loops


code:
for k in my_key_order:
    print dct[k]

rawDescription: How to iterate through dictionary in our preferred keys order in python 2.7?

description: Iterate through dictionary in our preferred keys order in python 2.7


code:
print "\nIDs appended to list...\n"

random.shuffle(a)

print "\nlist shuffled\n"

rawDescription: is there a way to speed up this python program? (short)

description: Is there a way to speed up this python program? (short)


code:
import re

tgt='21357.53 84898.10 Mckenzie Meadows Golf Course 80912.48 84102.38'

nums=[m.group() for m in re.finditer(r'[\d\.]+',tgt)]
words=[m.group() for m in re.finditer(r'[a-zA-Z]+',tgt)]
print '"{}" {}'.format(' '.join(words),' '.join(nums))

rawDescription: How do you split a string into quoted sentence and numbers using python

description: Split a string into quoted sentence and numbers using python


code:
formP = soup.find("form", attrs={ "method" : "post" }).find("p").get_text()
print formP

rawDescription: beautifulsoup4 getting second paragraph and form

description: Beautifulsoup4 getting second paragraph and form


code:
class SettingProxy(models.Model):

    domain = models.ForeignKey(Domain, null=False, verbose_name="Domain")
    theme = models.CharField(null=False, default='mytheme', max_length=16)
    message = models.CharField(null=False, default='Waddup', max_length=64)

    class Meta:
        abstract = True

    def __init__(self, *args, **kwargs):
        super(SettingProxy, self).__init__(*args, **kwargs)
        for field in self._meta.fields:
            if isinstance(field, models.AutoField):
                del field

    def save(self, *args, **kwargs):
        with transaction.commit_on_success():
            Setting.objects.filter(domain=self.domain).delete()

            for field in self._meta.fields:
                if isinstance(field, models.ForeignKey) or isinstance(field, models.AutoField):
                    continue
                else:
                    print field.name + ': ' + field.value_to_string(self)
                    Setting.objects.create(domain=self.domain,
                        name=field.name, value=field.value_to_string(self)
                    )

rawDescription: Store form fields as key-values / individual rows


code:
def processRecords():
        userInput = raw_input(USERPROMPT)
        # Check if a user has entered a text or not
        if userInput:
            # check the index from zipcode

            if userInput in zipcode:
                # the index of the zipcode in the zipcode list is the same
                # to get related cities and states.
                index_ = zipcode.index(userInput)
                print "The city is %s and the state is %s " % \
                      (city[index_], state[index_])
            else:
                print "\nThe zip code does not exist."
        else:
            print "Please enter a data"

rawDescription: Having trouble parsing a txt file into a list full of zip codes in my zipcode lookup program


code:
elements = WebDriverWait(driver, 10).until(lambda driver: driver.find_elements_by_class_name('video-item'))
for s in elements:
    print s.text
    video = {}
    videourl = s.find_element_by_css_selector('a').get_attribute('href')
    video['url'] = videourl
    videotitle = s.find_element_by_css_selector('a').get_attribute('alt')
    video['title'] = videotitle
    video_trend.append(video)
print video_trend

rawDescription: Does selenium not work with & in url

description: Does selenium not work with & in url


code:
fruits = [{
    'name':"apple",
    'color':["red","green"],
    'weight':1
}, {
    'name':"banana",
    'color':["yellow","green"],
    'weight':1
}, {
    'name':"orange",
    'color':"orange",
    'weight':[1,2]
}]

def findit(fruits,color):
    for indv in fruits:
        if color in indv['color']:
            return indv['name'], indv['weight']

print findit(fruits,"red")

rawDescription: How to retrieve a variable from a list located in a dictionary, located in a list? two

description: Retrieve a variable from a list located in a dictionary, located in a list? two


code:
def test(s):
    exprtokens = bnf.parseString(s,parseAll=True)
    print exprtokens

test("10")
test("10+20")
test("avg(10)")
test("avg(+10)")
test("column_1+8")
test("avg(column_1+10)+10")

rawDescription: How to get pyparser to work in a particular form

description: Get pyparser to work in a particular form


code: for l in files[:4]: print l.split()[-1]

rawDescription: Python and urllib

description: Python and urllib


code:
from lxml import etree

parser = etree.XMLParser(recover=True) # recover from bad characters.
root = etree.fromstring(broken_xml, parser=parser)
print etree.tostring(root)

rawDescription: escaping characters in a xml file with python

description: Escaping characters in a xml file with python


code:
import re

from bs4 import BeautifulSoup
import requests

url = 'http://www.growingagreenerworld.com/episode125/'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36'}

with requests.Session() as session:
    session.headers = headers

    response = session.get(url)

    soup = BeautifulSoup(response.content)

    # follow the iframe url
    response = session.get('http:' + soup.iframe['src'], headers={'Referer': url})
    soup = BeautifulSoup(response.content)

    # extract the video URL from the script tag
    print re.search(r'"url":"(.*?)"', soup.script.text).group(1)

rawDescription: Python, trouble getting embedded video url


code:
from time import sleep
from socket import socket, AF_INET, SOCK_DGRAM, SOL_SOCKET, SO_BROADCAST, gethostbyname, gethostname

s = socket(AF_INET, SOCK_DGRAM) #create UDP socket
s.bind(('', 0))
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) #this is a broadcast socket
my_ip= gethostbyname(gethostname()) #get our IP. Be careful if you have multiple network interfaces or IPs

while 1:
    data = MAGIC+my_ip
    s.sendto(data, ('<broadcast>', PORT))
    print "sent service announcement"
    sleep(5)

rawDescription: Python service discovery: Advertise a service across a local network


code:
from mpi4py import MPI
import numpy

comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()

assert size == 2

# Declare the array that will store all the temp results
temps = numpy.zeros((4,5))

# Loop over all directories
if rank==0:
   counter = 0
   for i in range(2):
      for j in range(5):
         temps[i,j] = counter
         counter = counter + 1

else:
   counter = 20
   for i in range(2,4):
      for j in range(5):
         temps[i,j] = counter
         counter = counter + 1

comm.Allreduce(MPI.IN_PLACE,temps,op=MPI.MAX)

if rank==0:
   print temps

rawDescription: How to share data between processes in Python using MPI?

description: Share data between processes in Python using MPI


code:
import re

ch = '>AB0012xxxxaaaaaaaaaaaa'\
     '>CD00192aaabbbllyybbbaaayyyuubbbaaaggggaaabbb'\
     '>ZP0199000012mmmm3m4mmmxxxxaaabbbaaaaaaaaaaaaa'\
     '>QD1547zzzzzzzzjjjiii'\
     '>SE457895ffffaaabbbbbbbgjhgjgjhgjhgbbbbbaaa'

print ch,'\n'

regx = re.compile('(>.{7})|(aaabbb|bbbaaa)')

matches = []
for mat in regx.finditer(ch):
    g1,g2= mat.groups()
    if g1:
        head = g1
    else:
        matches.append((head,g2))

print matches

rawDescription: Regular expression help

description: Regular expression help


code:
score = 1
f = open('highscores.p', 'wb')
pickle.dump(score, f)
f.close()
score = 15
f = open('highscores.p', 'wb')
pickle.dump(score, f)
f.close()
f = open('highscores.p', 'rb')
print pickle.load(f)
print pickle.load(f)
print pickle.load(f)

rawDescription: I am trying to save high scores with pickle, but how do I add to an already pickled document and then get the max?


code:
for task in projects.find({ "Tasks.resources":{ '$in': ["246"] }}):
   print task

rawDescription: MongoDB - Check if exists in a list in a list of dicts


rawDescription: How can I parse multiple (unknown) date formats in python?

description: Parse multiple (unknown) date formats in python


code:
for node in t.traverse("postorder"):
  # Do some analysis on node
  print node.name

rawDescription: What is the efficient way to define and traverse tree in python?


code:
# RegOrLog

import pickle # new line!
RegOrLog = raw_input('[Register/Log]:') # why str()?

# register the user detail
if RegOrLog in('r', 'reg', 'register'):
    reguser = raw_input('username:') # raw_input gives string by default, if you want other data type then you do have to convert it.
    regpass = raw_input('password:')
    regage = int(raw_input('age:'))
    reggender =raw_input('gender:')

# creating file for the user
    data = {"username": reguser, "password": regpass, "age": regage, "gender": reggender}
    pickle.dump( data, open( "whatever_file_name", "wb" ) )
    RegOrLog = 'log'

###login as a user###
if RegOrLog in('l','log','login'):
    loguser = raw_input('Login username:')
    regpass = raw_input('password:')

#registered user#
    data = pickle.load( open( "whatever_file_name", "rb" ) )
    if regpass == data["password"]:
        print 'Welcome', loguser
        print 'You are', gender
        print 'You are', age,'years old'

rawDescription: Reading and writing variables from file

description: Reading and writing variables from file


rawDescription: Assume zero for subsequent dimensions when slicing an array


code:
for item in find:
    print item.text
    output.writerows([item.text])

rawDescription: Web scraper returning list of elements

description: Web scraper returning list of elements


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup(myhtml)
td_list = soup.find_all('td')
i = 0
for elem in td_list:
    if elem.text == 'Year Built':
        ind = i
    i += 1
print td_list[ind].text

rawDescription: Find index of tag with certain text in beautifulsoup/python


rawDescription: Research a string with a variable number in python


rawDescription: Sum float values at each index point in nested lists


code:
with open("qwer.txt",'r') as v1:
  for line in v1:
    if 'good' in line: # note 'line' from here on
       print(line)
    if '==' in line: # '==' first
       print line.split('==')[1]
    elif '=' in line: # 'elif', not just 'if'
        print line.split('=')[1]

rawDescription: python parsing from dynamic input line

description: Python parsing from dynamic input line


code:
mapping2 = {tuple(s_en): s_de for (s_en ,s_de) in izip(sentences_en, sentences_de)}
print mapping2

rawDescription: Combine two lists of lists into a dictionary python


code:
from multiprocessing import Pool
from collections import Counter
import glob

def build_histogram(filepath):
    """This function is run by a worker process.
    The `filepath` argument is communicated to the worker
    through a pipe. The return value of this function is
    communicated to the manager through a pipe.
    """
    hist = Counter()
    with open(filepath) as f:
        for line in f:
            hist[line.strip()] += 1
    return hist

def main():
    """This function runs in the manager (main) process."""

    # Collect paths to data files.
    datafile_paths = glob.glob("data*.txt")

    # Create a pool of worker processes and distribute work.
    # The input to worker processes (function argument) as well
    # as the output by worker processes is transmitted through
    # pipes, behind the scenes.
    pool = Pool(processes=3)
    histograms = pool.map(build_histogram, datafile_paths)

    # Properly shut down the pool of worker processes, and
    # wait until all of them have finished.
    pool.close()
    pool.join()

    # Merge sub-histograms. Do not create too many intermediate
    # objects: update the first sub-histogram with the others.
    # Relevant docs: collections.Counter.update
    merged_hist = histograms[0]
    for h in histograms[1:]:
        merged_hist.update(h)

    for word, count in merged_hist.items():
        print "%s: %s" % (word, count)

if __name__ == "__main__":
    main()

rawDescription: Python: 'before' and 'after' for multiprocessing workers


code:
try:
    inp = raw_input
except NameError:
    inp = input

def getInt(msg):
    while True:
        try:
            return int(inp(msg))
        except ValueError:
            pass

START, ODD, EVEN = range(3)
state_next = [ODD, EVEN, ODD]
state_str  = ['no zeros yet', 'an odd number of zeros', 'an even number of zeros']

state = START
while True:
    num = getInt('Enter a number (-1 to exit)')

    if num==-1:
        break
    elif num==0:
        state = state_next[state]

    print 'I have seen {0}.'.format(state_str[state])

rawDescription: determinating if the input is even or odd numbers

description: Determinating if the input is even or odd numbers


code:
class Obj(object):
    def __init__(self, value):
        self.value = value
    def __lt__(self, other):
        return self.value < other.value
    def __repr__(self):
        return 'Obj(%r)' % self.value

obj_list = [Obj(2), Obj(1), Obj(4), Obj(3)]

print max(obj_list)
print min(obj_list)
print sorted(obj_list)

rawDescription: Is there a way to override the "key" function for list.max() and list.sort() in the class of the list items?


code:
import nltk

sentence = 'This bucket holds 5 litres of water.'

parser = nltk.RegexpParser(
    """
    INDICATOR: {<CD><NNS>}
    """)

print parser.parse(nltk.pos_tag(nltk.word_tokenize(sentence)))

rawDescription: Extract associated values from text using NLP


code:
import feedparser
import requests
from bs4 import BeautifulSoup

d = feedparser.parse('http://www.crunchyroll.com/rss/anime')
for url in d.entries:
    #print url.link
    r = requests.get(url.link)
    soup = BeautifulSoup(r.text)
    #print soup
    subtitles = soup.find_all('span',{'class':'showmedia-subtitle-text'})
    for ssid in subtitles:
        x = ssid.findAll('a')
        for a in x:
            print a['href']

rawDescription: search a specific word in BeautifulSoup python

description: Search a specific word in BeautifulSoup python


code:
import Tkinter as tk

opt = []
def chkbox_checked():
    for ix, item in enumerate(cb):
        opt[ix]=(cb_v[ix].get())
    print opt
root = tk.Tk()
mylist = [
'NR',
'ECEF X',
'ECEF Y',
'ECEF Z',
'height'
]
cb = []
cb_v = []
for ix, text in enumerate(mylist):
    cb_v.append(tk.StringVar())
    off_value=0  #whatever you want it to be when the checkbutton is off
    cb.append(tk.Checkbutton(root, text=text, onvalue=text,offvalue=off_value,
                             variable=cb_v[ix],
                             command=chkbox_checked))
    cb[ix].grid(row=ix, column=0, sticky='w')
    opt.append(off_value)
    cb[-1].deselect() #uncheck the boxes initially.
label = tk.Label(root, width=20)
label.grid(row=ix+1, column=0, sticky='w')
root.mainloop()

rawDescription: How to get the list of text values of multiple Checkbuttons in Tkinter in Python?

description: Get the list of text values of multiple Checkbuttons in Tkinter in Python


code:
class TestClassParent(unittest.TestCase):
    context_guard = context_mgr()
    def setUp(self):
        #do common setup tasks here
        self.c = self.context_guard.__enter__()
    def tearDown(self):
        #do common teardown tasks here
        self.context_guard.__exit__(None,None,None)

class TestA(TestClassParent):
    context_guard = context_mgr('A')
    def test_normal(self):
        print "task A"

class TestB(TestClassParent):
    context_guard = context_mgr('B')
    def test_normal(self):
        print "task B"

rawDescription: Testing methods each with a different setup/teardown


code:
import requests

url = 'http://www.danglefactory.com/api/DailySkaterProjections?_=1415200157912'
response = requests.get(url)

data = response.json()
print data

rawDescription: Scrape website data for CSV

description: Scrape website data for CSV


code: print [sum([dict[letter] for letter in word]) for word in nested]

rawDescription: gematria function - processing a text according to numerical values

description: Gematria function - processing a text according to numerical values


code:
s2 = s.split('&')[0]
print s2

rawDescription: Python - How to cut a string in Python?

description: Python - How to cut a string in Python


code:
codons = {'atg':'Methionine','tgg':'Tryptophan','ggg':'Glycine',
          'ggc':'Glycine','gcc':'Alanine','ccc':'Proline'}

seq='atgggggggcccccc'
seqlen= len(seq)
aaseq=[[],[],[]]

for i in range(seqlen-2):
     codon = seq[i:i+3]
     aa = codons[codon]
     aaseq[i%3].append(aa)

print 'aaseq1 ='
print ''.join(aaseq[0])
print 'aaseq2 ='
print ''.join(aaseq[1])
print 'aaseq3 ='
print ''.join(aaseq[2])

rawDescription: Using a for loop in python with more than 1 variable


code:
text = "abc"
text_list = []
for i in range(len(text)):
    text_list.append(text[i])
permu_text = Permutations(text_list)
print permu_text

rawDescription: recursive for with dynamic pointer

description: Recursive for with dynamic pointer


code:
def yahooscores():
    results = {}

    games = []

    for table in soup.find_all('table', class_='scores'):
        teams = []

        for row in table.find_all('tr'):
            scores = []
            name = None
            for cell in row.find_all('td', class_='yspscores'):
                link = cell.find('a')
                if link:
                    name = link.text
                elif cell.text.isdigit():
                    scores.append(cell.text)
            if name is not None:
                results[name] = scores
                teams.append(name)

        games.append(teams)

    for teams in games:
        for name in teams:
            scores = results[name]
            print ('%s: %s' % (name, ', '.join(scores)) + '.')
        print ""

yahooscores()

rawDescription: Taking These Tables and Associating Their Appropriate Teams Together


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

rawDescription: Python csv without header

description: Python csv without header


code:
#!/usr/bin/env python
import hashlib
import random

password = str(random.getrandbits(8000))
salt = str(random.getrandbits(256))
print hashlib.sha512(password + salt).hexdigest()

rawDescription: bcrypt in python

description: Bcrypt in python


code:
def query_single_sql(cursor):
    print 'query_single_sql'
    sql = []
    sql.append("""CREATE TEMPORARY TABLE tmp1 (id int)""")
    sql.append("""INSERT INTO tmp1 VALUES (1)""")
    sql.append("""SELECT * from tmp1""")

    cursor.execute(';'.join(sql))
    print list(cursor.fetchall())

rawDescription: Python MySQLdb doesn't wait for the result


rawDescription: Recursive pattern to take input integer and return palindrome


code:
while True:
    lock2.acquire()
    if counter == totalthreads: # in this case 3
        lock2.release()
        break
    lock2.release()
print "Done! %r" % myresults

rawDescription: Python: How to use threads in network?

description: Python: How to use threads in network


rawDescription: Issues ith loop is django

description: Issues ith loop is django


code:
#!/usr/bin/env python

import sys, time, threading
from daemon import Daemon
from server import run_tornado, run_base_http_server

class ServerDaemon(Daemon):
    def run(self):
        threads = [
            threading.Thread(target=run_tornado),
            threading.Thread(target=run_base_http_server)
        ]

        for thread in threads:
            thread.start()
        for thread in threads:
            thread.join()

if __name__ == "__main__":
    daemon = ServerDaemon('/tmp/server-daemon.pid')

    if len(sys.argv) == 2:
        if 'start' == sys.argv[1]:
            daemon.start()
        elif 'stop' == sys.argv[1]:
            daemon.stop()
        elif 'restart' == sys.argv[1]:
            daemon.restart()
        else:
            print "Unknown command"
            sys.exit(2)
        sys.exit(0)
    else:
        print "usage: %s start|stop|restart" % sys.argv[0]
        sys.exit(2)

rawDescription: How to wrap python daemon around my code

description: Wrap python daemon around my code


code:
import os
os.umask(077) # Only keep rights for owner
import test

rawDescription: python and securing pyc files on disk

description: Python and securing pyc files on disk


code:
def g():
 print "Worker", "Hello"
dis.dis(g)

rawDescription: Why there is space in the code example in python threading


code:
import random

number = int(raw_input())
countries = random.sample(capital, number)
for country in countries:
    guess = raw_input('What is the capital of {}?'.format(country))
    if guess == capital[country]:
        print 'Correct!'

rawDescription: How can I create an EU quiz without 28 "if" statements

description: Create an EU quiz without 28 "if" statements


code:
for i in range(4):
    temp = MyClass()
    p = Process(target=myFunc, args=(temp, i))
    p.start()
    p.join()
    print i,temp.x

rawDescription: Relink python object (i.e. pass by reference)


code:
Example
import os

search_folder = '/Users/temp/'

results = []
for root, dirs, files in os.walk(search_folder):
       for f in files:
            results.append(os.path.join(root, f))

sorted_results = sorted(results, key=lambda x: os.path.splitext(x)[1])

for path in sorted_results:
    print path
    print os.path.getsize(path)

rawDescription: python sort on extension / Name of the file

description: Python sort on extension / Name of the file


code:
mapping = {country.name: country.alpha2 for country in pycountry.countries}
for column in csv_file:
    print column['name'], mapping.get(column['name'], 'No country found')

rawDescription: Convert Country Names to Country Code using Python DictReader/DictWriter


code:
if '-' == something:
   print 'hurray!'

rawDescription: Python / Regex: exclude everything except one thing


code:
import csv
import sys #used for passing in the argument
file_name = sys.argv[1] #filename is argument 1
with open(file_name, 'rU') as f:  #opens PW file
    reader = csv.reader(f)
    data = list(list(rec) for rec in csv.reader(f, delimiter=',')) #reads csv into a list of lists

    for row in data:
        print row[0] #this alone will print all the computer names
        for username in row: #Trying to run another for loop to print the usernames
            print username

rawDescription: Building list of lists from CSV file

description: Building list of lists from CSV file


code:
import libxml2
import libxslt

styledoc = libxml2.parseFile("test.xsl")
style = libxslt.parseStylesheetDoc(styledoc)
doc = libxml2.parseFile("test.xml")
html = style.applyStylesheet(doc, None)

print style.saveResultToString(html)

rawDescription: Python - convert <type 'instance'> to <type 'str'>?


code:
for story in top_story_ids:
   print hn.get_item(story)

rawDescription: How to use Hacker News API in Python?

description: Use Hacker News API in Python


rawDescription: Find Two Sets of Python Numpy Arrays on Common Column


code:
for index, item in enumerate(b):
    print index, item

rawDescription: How can I iterate over a list of strings with ints in python?

description: Iterate over a list of strings with ints in python


code:
#!/usr/bin/python
for i in xrange(100000):
    print "def foo%d(): pass\n" % i

rawDescription: Efficiency of imports in Python

description: Efficiency of imports in Python


code:
from datetime import datetime, timedelta
s = '2013-08-11 09:48:49'
mytime = datetime.strptime(s, "%Y-%m-%d %H:%M:%S")
mytime += timedelta(hours=6)
print mytime.strftime("%Y.%m.%d %H:%M:%S")

rawDescription: Python datetime add

description: Python datetime add


code:
from ctypes import *
TenIntegers = c_int * 10
ii = TenIntegers(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print ii
for i in ii: print i,

rawDescription: What does (ctypes.c_int * len(x))(*x) do?


code:
def foo():
  bar = 123
  baz = 'Mike!'
  print locals()
foo()

rawDescription: How does CPython match variables to values?


rawDescription: Converting numpy arrays of arrays into one whole numpy array


code:
from collections import namedtuple

variables = {}
Fruit = namedtuple("Fruit", ["name", "colour", "shape"])
for args in Applist:
    fruit = Fruit(*args)
    variables[fruit.name] = fruit

print "Apple shape: " + variables["Apple"].shape

rawDescription: Python List as variable name

description: Python List as variable name


code:
from itertools import product

d3 = [[[0 for col in range(3)]for row in range(3)] for x in range(3)]

d3[0][2][2] = 1

for i, j, k in product(xrange(3), xrange(3), xrange(3)):
    if(d3[i][j][k]): print "%s %s %s = %s" % (i, j, k, d3[i][j][k])

rawDescription: finding non zero entries in 3D matrix using only maps, lambda and reduce

description: Finding non zero entries in 3D matrix using only maps, lambda and reduce


code:
command = sys.argv[1]
arguments = map(ast.literal_eval, command.split())
print arguments

rawDescription: python sys.argv differentiate int and string

description: Python sys.argv differentiate int and string


code: print results.most_common(3)

rawDescription: Disregarding a line while calculating frequency in python


code:
listOfStrings = ['i_am_exercising', 'python_functional', 'lists_comprehension']

result = [ "".join([elem.title() for elem in split]) for split in [el.split("_")for el in listOfStrings]]
print result

result = []
for split in [el.split("_") for el in listOfStrings]:
    result.append("".join([elem.title() for elem in split]))
print result

result = []
temp1 = []
for el in listOfStrings:
    temp1.append(el.split("_"))
for split in temp1:
    result.append("".join([elem.title() for elem in split]))
print result

result = []
temp1 = []
for el in listOfStrings:
    temp1.append(el.split("_"))
for split in temp1:
    temp2 = []
    for elem in split:
        temp2.append(elem.title())
    result.append("".join(temp2))
print result

rawDescription: Understanding Python List Comprehension equivalent


code:
import numpy as np

A = np.array([[0, 1, 0],
              [1, 0, 1],
              [1, 0, 0]]).astype(bool)

A = A
print A.astype(int)
B = A.transpose() & ~A
print B.astype(int)

rawDescription: Find all the missing directed edges in an adjacency matrix


code:
from itertools import product
import numpy as np

parameter={'alpha1':np.linspace(0.3,0.4,10),'alpha2':np.linspace(0.9,2,100)}

def parameter_options(parameter):
    for values_option in product(*parameter.values()):
        yield dict(zip(parameter.keys(), values_option))

for opt in parameter_options(parameter):
    print opt

rawDescription: How to write a recursion function for all the possible parameter combinations in python

description: Write a recursion function for all the possible parameter combinations in python


code:
import fileinput
replacing = False
for line in fileinput.input('thefile.txt', inplace=True):
    if replacing and 'sequence' in line:
        line = line.replace('sequence', 'structure')
        replacing = False
    elif '3RVYA' in line:
        replacing = True
    print line,

rawDescription: How can read lines in python

description: How can read lines in python


code:
import sys
import subprocess
s2_out = subprocess.check_output([sys.executable, "script2.py", "34"])
print s2_out

rawDescription: return value from one python script to another

description: Return value from one python script to another


code:
liNums = xrange(1, 20)
x = 0
line = ""
for i in liNums:
    x+=1
    line += "%s " % i
    if not x%7:
        line += "\n"
#send line to output, here I will just print it
print line

rawDescription: How to print several array elements per line to text file in Python

description: Print several array elements per line to text file in Python


rawDescription: Replace some string in some phrases using a dictionary of strings in Python


code:
print yourString.strip()
#Samsung Floor Standing Invertor Air Conditioner - 2.0 Ton - AF-28FSSDAWKXFA (Q-9000) - White (Brand Warranty)

rawDescription: Remove extra spaces using regex

description: Remove extra spaces using regex


code:
data = "196465702|91017022|['95592022', '96094942', '100920382']|91048702"
from ast import literal_eval
first, second, third, last = data.split("|")
third = literal_eval(third)
for item in third:
    print "{}|{}|{}|{}".format(first, second, item, last)

rawDescription: Transpose list to rows of data

description: Transpose list to rows of data


code:
def solution(S):
    length = len(S)
    if length == 0:
        return 1
    count = 0
    i = 1 # Don't test the empty string!
    while i < length: # Don't test the whole string! Use '<'
        prefix = S[:i] # Up to i
        suffix = S[length-i:] # length - i to the end
        print prefix, suffix
        if prefix == suffix:
            count += 1
        i += 1
    return count

print solution("")
print solution("abbabba")
print solution("codility")

rawDescription: Intersection between two list of strings python


code:
import lxml
import lxml.html

data = '''    <main>
      <header>
        <row type="info">
          <field name="description"><![CDATA[Results]]></field>
          <field name="created"><![CDATA[6/12/2014 6:45:00 PM]]></field>
        </row>
        <row>
          <field name="profile"><![CDATA[Intel]]></field>
        </row>
      </header>
      <sections>
        <section name="Results">
          <description />
          <parameters />
          <header />
          <content>
            <row>
              # A row-dependend number of fields exist before the Full Content field
              <field name="Full Content"><![CDATA[ I am the text of interest]]></field>
              # A row-dependend number of fields follow here
            </row>
            # There are 40,000 of these row elements
          </content>
          <footer>
            <row type="content_count">
              <field name="count"><![CDATA[9981]]></field>
            </row>
          </footer>
        </section>
      </sections>
    </main>'''

html = lxml.html.fromstring(data)

fields = html.cssselect('field')

for x in fields:
     print lxml.etree.tostring(x)

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

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


code:
for entry1 in file1list:
    for entry2 in file2list:
        if(np.all(np.abs(entry1[1:7] - entry2[1:7]) > tol)):
            print entry1,entry2

rawDescription: efficient string comparison in python including numerical evaluation

description: Efficient string comparison in python including numerical evaluation


code:
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-f", "--filename", dest="filename",help="file!")
options, args = parser.parse_args()
print options.filename

rawDescription: How to parse arguments in python 2.6 with prefix option as -f file.xml

description: Parse arguments in python 2.6 with prefix option as -f file.xml


code:
def numberBowTie(num):
    part1 = ''
    part2 = ''
    for i in range(num+1):
        part1 = part1 + str(i)*i + ' '* (2 * (num - i)) + str(i)*i +'\n'
        part2 = part2 + str(num-i)*(num-i) + ' '* (2 * i) + str(num-i)*(num-i) + '\n'
    print part1 + part2

rawDescription: Bow tie shape function

description: Bow tie shape function


code:
import math, array
import numpy as np
from PIL import Image

size = (800,800)
width, height = size

s1x = width * 1./8
s1y = height * 1./8
s2x = width * 7./8
s2y = height * 7./8

r,g,b = (255,255,255)

array_i,array_j = np.mgrid[0:width,0:height]

arr = np.abs(np.hypot(array_i-s1x, array_j-s1y) -
             np.hypot(array_i-s2x, array_j-s2y))

arr = (arr * np.ones((3,1,1))).transpose(1,2,0)

arr2 = np.zeros((width,height,3),dtype="uint8")
for ld in [200,116,100,84,68,52,36,20,8,4,2]:
    print 'now computing image for ld = '+str(ld)
    # Rest as before

rawDescription: Python optimization problem?

description: Python optimization problem


code:
A = 3
B = range(10)
f = range(10)

for i in range(10):
    for j in range(len(B)):
        if (A == B[j]):
        # if ((A - B[j]) == 0):
            print i, f[j]
            print 'test'

rawDescription: If statement working in a strange way

description: If statement working in a strange way


code:
zipped = zip(Openmarket, someotherlist, someotherotherlist)
filtered = filter(lambda (a, b, c): "FX" not in a, zipped)
Openmarket, someotherlist, someotherotherlist = zip(*filtered)

rawDescription: Filtering a python list, while coupling non-filtered terms to other lists.


code:
a = [1,2,3]
b = (1,2,3)
c = set([1,2,3,1])

from collections import namedtuple
Foo = namedtuple('Foo', 'x y z')
foo = Foo(1,2,3)
print '{} {} {}'.format(*foo)

print '{} {} {}'.format(*a)
print '{} {} {}'.format(*b)
print '{} {} {}'.format(*c)
print '{} {} {}'.format(*foo)

rawDescription: Can I express named Python string arguments more succintly?

description: Express named Python string arguments more succintly


code:
for dt, src in sorted(
        [(t,'dic1') for t in dic1] + [(t,'dic2') for t in dic2] ):
    print '{} from source {}'.format(dt, src)

rawDescription: Combing 2 python Lists and sorting by date while preserving source


code:
class TACRG(QtGui.QMainWindow, design.Ui_MainWindow):
    def __init__(self, parent=None):
        # Your init code
        self.report_start, self.report_end, self.accs = [None] * 3

    def r_chosen(self, text):
        self.report_start, self.report_end = report_period(text)
        other_method(self.report_start, self.report_end, self.accs)

    def c_chosen(self, text):
        self.accs = get_ucs(text)
        other_method(self.report_start, self.report_end, self.accs)

def other_method(report_start, report_end, accs):
    #validate if your fields have valid values
    if report_start and report_end and accs:
        print report_start, report_end, accs

rawDescription: How to use QComboBox selection in function

description: Use QComboBox selection in function


code:
import unicodedata as ucd
allsorts =u"\u0473\u0660\u06c9\u24e8\u4e0a\u3020\u3021"
for x in allsorts:
    print repr(x), ucd.category(x), ucd.name(x)
rx.findall(allsorts)

rawDescription: python-re: How do I match an alpha character

description: Python-re: How do I match an alpha character


code:
for i in xrange(10):
  if i == 5:
    continue
  print i

rawDescription: Python - Way to restart a for loop, similar to "continue" for while loops?


code: print re.sub("^(\S+)\s", "\\1 - ", mystring['song'])

rawDescription: Auto completing song titles and parsing a string


code:
a = [ u'hello', u'world' ]

print a

[u'hello', u'world']

for x in a:
    print x

hello
world

rawDescription: Removing <u> character from text using Scrapy


code:
import appscript
print appscript.app("Safari").windows.first.current_tab.URL()

rawDescription: Determine current Mac Safari web page using Python


code:
#save as text decompressed image into bytes
for filename in os.listdir(imagePath):
    if filename!='.DS_Store' and filename[-3:]=='jpg':
        b = imread(filename,flatten=0).flatten()
        print b.shape
        out = ','.join('%d'%i for i in b)+'\n'
        print len(out)
        with open(savepath+'trainMatrix2.txt','a') as fut:
            fut.write(out)
            fut.close()

rawDescription: convert JPG to txt causes change in file size in python

description: Convert JPG to txt causes change in file size in python


code:
name_level_1_translated = util.translate("iw", "en", name_level_1.encode('utf-8'))
print name_level_1_translated

name_level_2_translated = util.translate("iw", "en", name_level_2.encode('utf-8'))
do_stuff(name_level_2_translated)

rawDescription: Basic Python question: referencing original variable inside for loop?


code:
class AttributeDescriptor(object):
    def __init__(self):
        self.initialized = False
        self.base = 0
        self.adjustments = []

    def compute(self):
        return self.base + sum(self.adjustments)

    def __set__(self, inst, value):
        if not self.initialized:
            self.base = value
            self.initialized = True
            print("Attribute initialized to %s" % value)
        else:
            # Calculate delta
            delta = (value - self.compute())
            self.adjustments.append(delta)
            print("Adjustment added: %s" % delta)

    def __get__(self, inst, owner):
        return self.compute()

class Creature(object):
    health = AttributeDescriptor()
    armor  = AttributeDescriptor()

    def __init__(self, health=100, armor=0):
        self.health = health
        self.armor  = armor

c1 = Creature(50)
c1.health = 60      # Adds a +10 adjustment
print c1.health     # 60
c1.health += 10     # Add a +10 adjustment
print c1.health     # 70
#print c1.health.adjustments     # This won't work ('int' object has no attribute 'adjustments')

rawDescription: Creating an Attribute class that allows adjusting a base value?


code:
class SimpleConsumer(Consumer):
     def _callback(self, body):
         print body

rawDescription: Register unique callbacks per instance using classmethod


code:
class my_ndarray(np.ndarray):
    def __setattr__(self, name, value):
        print '__setattr__ called with name={}, value={}'.format(name, value)
        super(my_ndarray, self).__setattr__(name, value)

a_ = my_ndarray(a.shape)
a_[:] = a

rawDescription: Difference between setting an attribute and calling an in-place mutating method?


code:
node = build_tree(5)
print node.eval()

rawDescription: Python: Optimizing a tree evaluator

description: Python: Optimizing a tree evaluator


code:
import json
import zmq

request_data = {
        "values": [10, 20, 30 , 40],
        }

context = zmq.Context()
socket = context.socket(zmq.DEALER)
socket.connect("ipc:///tmp/myapp.ipc")
socket.send(json.dumps(request_data))
print socket.recv()

rawDescription: Exposing python daemon as a service

description: Exposing python daemon as a service


code:
def do_xslt(xsl, xml):
    print 'do_xslt(%r, %r)' % (xsl, xml)
    return xml + '*' + xsl
from functools import partial
TRANSFORM_PIPELINE = [
    partial(do_xslt, 'pass1.xsl'),
    partial(do_xslt, 'pass2.xsl'),
    partial(do_xslt, 'pass3.xsl')
]
x = 'xml is lame'
for transform in TRANSFORM_PIPELINE:
    x = transform(x)
    print x

rawDescription: How to give a constant and a dynamic argument to functions which are stored in an array

description: Give a constant and a dynamic argument to functions which are stored in an array


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

rawDescription: Print first element or 2nd element from text file


code: print a, b, c, d

rawDescription: How to pass an integer series as argument to a python function?

description: Pass an integer series as argument to a python function


code:
test = Test()
test.name = "foo"
test.isValid = True
print repr(test)
print str(test)
print test

rawDescription: Extending __str__() rather than replacing it


code:
import argparse
import codecs
from sklearn.feature_extraction.text import TfidfVectorizer

parser = argparse.ArgumentParser()
parser.add_argument("stop_word_list", help="The stop word list")
args = parser.parse_args()

with codecs.open(args.stop_word_list, encoding='utf-8') as f:
    spanish_stop_words = [line.strip() for line in f]
    print spanish_stop_words

from sklearn.feature_extraction import text
my_stop_words = text.ENGLISH_STOP_WORDS.union(spanish_stop_words)

tfidf_vect = TfidfVectorizer(stop_words=set(my_stop_words))

rawDescription: How to merge this functionality with argparse?

description: Merge this functionality with argparse


code:
d = {}
for k, v in dict_a.items():
    if k in dict_b:
        val1, val2 =  (v[0]+ dict_b[k][0]) / 2, (v[1] + dict_b[k][1]) / 2 # average sum of ele1 and ele2 in each value list
        d[k] = [val1, val2] # set new value to key
    else:
        d[k] = v # else just add the k,v

for k,v in dict_b.items(): # add rest of keys from dict_b
    if k not in d: # if key not already in d add it
        d[k] = v
print d

{'key3': [37, 89.56], 'key2': [35, 76], 'key1': [39, 71], 'key4': [46, 89]}

rawDescription: Combine Dictionaries With average values for similar keys,Python


code:
from bs4 import BeautifulSoup

html = """
PC1	zz:zz:zz:zz:zz:ce	10.0.0.244	23 hours, 55 minutes, 25 seconds

PC2	zz:zz:zz:zz:zz:cf	10.0.0.245	23 hours, 23 minutes, 27 seconds
"""

soup = BeautifulSoup(html)
mac_ips = []

for tr in soup.find_all('tr'):
    cols = [td.text for td in tr.find_all('td')]
    mac_ips.append((cols[1], cols[2]))

for mac, ip in mac_ips:
    print '{}  {}'.format(mac, ip)

rawDescription: regex/beautifulsoup how to extract the all values of column from html table?

description: Regex/beautifulsoup how to extract the all values of column from html table


code:
x=[1,2,3]  # The name x is assigned to the list object [1,2,3]
y=x        # The name y is assigned to the same list object referenced by x
x[1]=3     # This *modifies* the list object referenced by both x and y
print y    # The modified list object is printed

rawDescription: List vs. int assignment - "Every variable is a pointer"


code:
from lxml import html
str = ' Text1 ' \
      ' Text2 ' \
      ' Text3 '
tree = html.fromstring(str)
items = tree.xpath('//a/text() | //a/@href')

for href, text in zip(*[iter(items)]*2):
    print text, href

rawDescription: Get attributes and text from Xpath query as a list


code:
import socket

client, testsocket = socket.socketpair()
client.sendall("test_recv")
print testsocket.recv(1024)
testsocket.close()
client.close()

rawDescription: How to test loopback socket connections with python?

description: Test loopback socket connections with python


code:
limit = 100  # Reddit maximum limit
total_list = []
submissions = r.get_subreddit('soccer').get_new(limit=limit)
submissions_list = [
    x for x in submissions
    if datetime.utcfromtimestamp(x.created_utc) >= an_hour_ago
]
total_list += submissions_list
if len(submissions_list) == limit:
    submissions = r.get_subreddit('soccer').get_new(
        # get limit of items past the last item in the total list
        limit=100, params={"after": total_list[-1].fullname}
    )
submissions_list_2 = [
    # iterate through the submissions generator object
    x for x in submissions
    # add item if item.created_utc is newer than an hour ago
    if datetime.utcfromtimestamp(x.created_utc) >= an_hour_ago
]
total_list += submissions_list_2
print total_list

rawDescription: Praw: How to filter search results based on created date?


code:
reduce(lambda (bcount, ccount), (b, c): (bcount + (b is not None), ccount + (c is not None)),
       bullsandcows(given, number), (0, 0))

rawDescription: Reduce list of tuples

description: Reduce list of tuples


code:
from collections import Counter
print Counter(n1) - Counter(n2)
# output: Counter({3: 1})

rawDescription: How to find the difference between 3 lists that may have duplicate numbers

description: Find the difference between 3 lists that may have duplicate numbers


code:
from heapq import heappush, heappop, heapify

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

    def __cmp__(self, a):
        return cmp(self.weight, a.weight)

class Symbol(object):
    def __init__(self, name, weight):
        self.name = name
        self.weight = weight
        self.code = None

    def __cmp__(self, a):
        return cmp(self.weight, a.weight)

def encode(symbfreq):
    # return pair (sym2object, tree), where
    # sym2object is a dict mapping a symbol name to its Symbol object,
    # and tree is the root of the Huffman tree
    sym2object = {sym: Symbol(sym, w) for sym, w in symbfreq}
    tree = sym2object.values()
    heapify(tree)
    while len(tree) > 1:
        lo = heappop(tree)
        hi = heappop(tree)
        heappush(tree, Node(lo.weight + hi.weight, lo, hi))
    tree = tree[0]

    def assigncode(node, code):
        node.code = code
        if isinstance(node, Node):
            assigncode(node.left, code + "0")
            assigncode(node.right, code + "1")
    assigncode(tree, "")

    return sym2object, tree

i = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
s2o, t = encode(i)
for v in s2o.values():
    print v.name, v.code

rawDescription: Huffman encoding issue

description: Huffman encoding issue


code:
import subprocess

output = subprocess.check_output(["Matlab.exe", "/home/user/docs/myapp.mat"])
print output

rawDescription: (Python) Launching an external application and wait for the end of it


code:
s = """
for i in range(5):
    print(i)
"""
exec s

rawDescription: How to evaluate arbitrary code in Python?

description: Evaluate arbitrary code in Python


code:
st = "\B1\B1xxA1xxMdl1zzInoAEROzzMofIN"
s = re.sub(r"\\","",st)
idx = s.rindex("B1")
print s[idx:]

rawDescription: regex in python 2.4

description: Regex in python 2.4


code:
query2 = Blog.objects.filter(comment__created__gt=from_dt, comment__created__lt=till)
print query2.query

rawDescription: Django Queryset: Efficiency of Equivalent Filter Clauses


code:
#!/usr/bin/env python
import sys
from xml.etree import ElementTree

def run(files):
    first = None
    for filename in files:
        data = ElementTree.parse(filename).getroot()
        if first is None:
            first = data
        else:
            first.extend(data)
    # Creating without duplicates here, which contains the unique list of elements determined by values of subelements
    withoutDuplicates = []
    if first is not None:
        for row in first.findall('row'):
            if ( (row[0].text, row[1].text) ) in withoutDuplicates:
                first.remove(row) # Removing duplicate row
            else:
                withoutDuplicates.append((row[0].text, row[1].text))
        print ElementTree.tostring(first)

if __name__ == "__main__":
    run(sys.argv[1:])

rawDescription: Merge XML files and delete duplicate rows


code:
import xml.etree.ElementTree as ET
from StringIO import StringIO

s = '''<w>
  <shortening>n</shortening>
  ūmi
  <mor type="mor">
    <mw>
      [extra stuff]
    </mw>
    <menx>rest</menx>
    <menx>sleep</menx>
    <gra type="gra" relation="ROOT" head="0" index="1"/>
  </mor>
</w>'''

tree = ET.parse(StringIO(s))
root = tree.getroot()

for i in root.iter('shortening'):
    print i.tail

rawDescription: Python (xml.etree) not reading XML text

description: Python (xml.etree) not reading XML text


code: print urls, len(response.read())

rawDescription: Is there a better way to retrieve webpage sizes with Python?


rawDescription: Complex Data Manipulation in Python

description: Complex Data Manipulation in Python


code:
class C1:
    c=-1
    def output( self ):
        print self.c
        print C1.c

class C2( C1 ):
    c = "1"
    def C1output():
      print C1.c
      print self.c

c = C2()

c.output()

rawDescription: How to access a 'static' field of a child class in a base class?

description: Access a 'static' field of a child class in a base class


code:
from BeautifulSoup import BeautifulSoup

xml = """<xml>
  <web:Web>
    <web:Total>4000</web:Total>
    <web:Offset>0</web:Offset>
  </web:Web>
</xml>"""

doc = BeautifulSoup( xml )
print doc.find( 'web:total' ).string
print doc.find( 'web:offset' ).string

rawDescription: How can I access namespaced XML elements using BeautifulSoup?

description: Access namespaced XML elements using BeautifulSoup


code:
cows = "111 cows 222 cows "
cows_lst = [cow for cow in cows.split(" cows ") if cow]
print cows_lst # now it prints ['111', '222']

rawDescription: 'Int' object is not iterable. But, it is a string


code:
f = codecs.open(r'c:\temp\temp.txt', 'r', 'utf-8-sig')
line = f.readline()
print line
line.find(u'\u0634')

rawDescription: Finding the index of a UTF-8 character in a Python string


code:
import numpy as np

x = np.random.rand(4);
filter_bools = x < .5;
sorted_indices = np.argsort(x)
sorted_filter = filter_bools[sorted_indices]

filtered_sorted_indices = sorted_indices[sorted_filter == True]

print 'x:\t\t\t\t', x
print 'sorted indices:\t\t\t', sorted_indices
print 'indices for x < .5, sorted:\t', filtered_sorted_indices
print 'sorted, filtered x values:\t', x[filtered_sorted_indices]

rawDescription: return indices from filtered, sorted array with numpy

description: Return indices from filtered, sorted array with numpy


code:
import smtplib,ssl

def SendMail(username,password,contents):
    server = Connect(username)
    try:
        server.login(username,password)
        server.sendmail(username,username,contents)
    except smtplib.SMTPException,error:
        Print(error)
    Disconnect(server)

def Connect(username):
    serverName = username[username.index("@")+1:username.index(".")]
    while True:
        try:
            server = smtplib.SMTP(serverDict[serverName])
        except smtplib.SMTPException,error:
            Print(error)
            continue
        try:
            server.ehlo()
            if server.has_extn("starttls"):
                server.starttls()
                server.ehlo()
        except (smtplib.SMTPException,ssl.SSLError),error:
            Print(error)
            Disconnect(server)
            continue
        break
    return server

def Disconnect(server):
    try:
        server.quit()
    except smtplib.SMTPException,error:
        Print(error)

serverDict = {
    "gmail"  :"smtp.gmail.com",
    "hotmail":"smtp.live.com",
    "yahoo"  :"smtp.mail.yahoo.com"
}

SendMail("your_username@your_provider.com","your_password",str(lowest_bid))

rawDescription: Scrape Data Point Using Python

description: Scrape Data Point Using Python


code:
from pandas import *

data = 'MTAQDD,MTAQDD,MTSQED,MTAQDD,MKAQHD'

df = DataFrame([list(row) for row in data.split(',')])

print DataFrame({str(col)+val:(df[col]==val).apply(int)
        for col in df.columns for val in set(df[col])})

rawDescription: numerical coding of mutated residues and positions

description: Numerical coding of mutated residues and positions


code:
from itertools import groupby, islice

def accumulate(iterable): # in py 3 use itertools.accumulate
    ''' Simplified version of accumulate from python 3'''
    it = iter(iterable)
    total = next(it)
    yield total
    for element in it:
        total += element
        yield total

power = [
    ['1234-4321-1', [5, 6, -4, 11, 22]],
    ['1234-7834-1', [43, -5, 0, 0, -1]],
    ['1234-5376-1', [3, 0, -5, -6, 0]],
    ['1567-3454-1', [4, 6, 8, 3, 3]],
    ['1567-9987-1-', [1, 2, 1, -4, 5]]
]

groups = ((k, (nums for station, nums in g))
          for k, g in
          groupby(power, lambda x: x[0].partition('-')[0]))

new = [(station, zip(*(islice(accumulate(col), 1, None) for col in zip(*nums))))
        for station, nums in groups]

print new

print dict(new) # or as a dictionary which is unordered

rawDescription: Cumulative addition whilst looping over a list


code:
import readline

def pre_input_hook():
    readline.insert_text('DefaultValue')
    readline.redisplay()

readline.set_pre_input_hook(pre_input_hook)

while True:
    line = raw_input('Prompt ("stop" to quit): ')
    if line == 'stop':
        break
    print 'ENTERED: "%s"' % line

rawDescription: Pre-initialize raw_input with default value


code: print str(a) + " plus " + str(b) + " equals " + str(a+b)

rawDescription: How does python know to add a space between a string literal and a variable?


code:
source
import multiprocessing, sys, time

record_ev = multiprocessing.Event()
name = 'beer'

def recordLeverPos(rec_ev, name): #e = multiprocessing.Event()
    posData = []
    lever = iter(xrange(999))
    while True:
        datBuffer = str(lever.next()) + '\n'
        posData.append(datBuffer)
        print 'posData',posData
        if rec_ev.is_set():
            rec_ev.clear()
            with open('%s_leverPos.tsv' % name, 'a') as dataf:
                for dat in posData:
                    dataf.write(dat)
            print 'wrote: {}'.format(posData)
            print 'Done Writing to leverPos file.'
            posData = []
        time.sleep(1)

record_proc = multiprocessing.Process(
    target=recordLeverPos, args = (record_ev, name)
)
record_proc.start()

time.sleep(2)
print 'triggering record'
record_ev.set()

time.sleep(2)
print 'triggering record #2'
record_ev.set()

time.sleep(2)
record_proc.terminate()
record_proc.join()
sys.exit(0)

rawDescription: Best way to communicate in multiprocessing between processes.

description: Communicate in multiprocessing between processes.


code:
import random

class DailyStatement(object):  # changed from C to c
  def __init__(self):  # Added self
     self.masterdict={}
     # Removed self.subdict
     self.list_of_subdicts=['yesterday', 'today']
     self.keys=['high', 'low', 'open', 'close']
     self._create_masterdict()

  def _create_masterdict(self):
     for subdict in self.list_of_subdicts:
       self.masterdict[subdict] = {}
       self.masterdict[subdict] = self.create_subdict(subdict)  # added self
       print self.masterdict[subdict]

  def create_subdict(self, subdict):
     sub_dict = {}
     for item in self.keys:
        sub_dict[item] = self.get_value(item, subdict)  # added self
     return sub_dict

  def get_value(self, item, subdict):  # added self
     return random.randrange(1,4)

if __name__ == "__main__":
  ds=DailyStatement()
  print ds.masterdict

rawDescription: dictionary within a dictionary

description: Dictionary within a dictionary


code:
t = timeit.timeit(stmt='ndimage.convolve(x, y, output=x)', number=1,
setup="""
import numpy as np
from scipy import ndimage
x = np.random.random((2048, 2048)).astype(np.float32)
y = np.random.random((32, 32)).astype(np.float32)
""")
print t

rawDescription: Fastest 2D convolution or image filter in Python


code:
class Foo (object) :
    def baz (self) :
        return 324

class Bar (object) :
    def __init__ (self) :
        self.foo = Foo()

    @property
    def baz(self):
        return self.foo.baz

    def __getattr__(self, attr):
        return getattr(self.foo, attr)

bar = Bar()
print bar.foo.baz()
print bar.baz()

rawDescription: Calling an instance variable's method from the instance itself


code:
def capitals(word):
    print word
    lst = []
    for i,ch in enumerate(word):
        if ch.isupper():
            lst.append(i)
    return lst

rawDescription: How do I get the index of multiple occurrences of the same character in a string?

description: Get the index of multiple occurrences of the same character in a string


code:
parent = ndb.Key(MyModel, 1)
child = MyModel(parent=parent)
print child.key

rawDescription: Setting parent key but not child key while using endpoint-proto-datastore