code:
import sys, os
import logging
from instant import inline

print 'This is printed from python to stdout'
stdout = os.fdopen(os.dup(sys.stdout.fileno()), 'w')
stderr = os.fdopen(os.dup(sys.stderr.fileno()), 'w')

logging.basicConfig(stream=stderr, level=logging.DEBUG)

redirect = inline("""
void redirect(void) {
    freopen("my_stdout.txt", "w", stdout);
    freopen("my_stderr.txt", "w", stderr);
}
""")
redirect()

cout = inline("""
void cout(void) {
    std::cout << "This is written from C++ to my_stdout.txt" << std::endl;
    std::cerr << "This is written from C++ to my_stderr.txt" << std::endl;
}
""")
cout()

print 'This is written from python to my_stdout.txt'

stdout.write('This is printed from python to stdout\n')
stderr.write('This is printed from python to stderr\n')
logging.info('This is printed to stderr from python using logging')

rawDescription: Controlling C++ Output from Python Script


code:
my_friends = [0, 2, 666, 123132]
print "select uid1 from friends where uid1 in (%s)" % (",".join(my_friends))

rawDescription: Translate Python code to C# equivalent

description: Translate Python code to C# equivalent


code:
string = line.split(',')
if string[0] == 'Aa' or string[0] == 'Bb':
  print string

rawDescription: Condition with pass statement

description: Condition with pass statement


code:
print "Content-type: application/json"
print
print "jsonCallback(%s)" % allergy

rawDescription: AJAX JSONP Failure returns statustext "success", correct callback


code:
for s in string_list:
    print re.search(r'(\d+)/python', s).group(1)

rawDescription: Kill a port that is listening with regex


code:
import subprocess
import os

# Open a pipe and run a command that writes to the write end
input_fd, output_fd = os.pipe()
subprocess.Popen(["cat", "foo.txt", "bar.txt"], shell=False, stdout=output_fd)
os.close(output_fd);

# Run a command that uses /dev/fd/* to read from the read end
proc = subprocess.Popen(["wc", "/dev/fd/" + str(input_fd)],
                        shell=False, stdout = subprocess.PIPE)

# Read that command's output
print proc.communicate()[0]

rawDescription: How to execute '<(cat fileA fileB)' using python?

description: Execute '<(cat fileA fileB)' using python


code:
import email

cfg = \
"""Load-Files: a,b,c
Export-Files: x,y,z

# Python code starts here
while 1:
    do_stuff()
"""

msg = email.message_from_string(cfg)
print msg.items()
print
print msg.get_payload()

rawDescription: Reading raw section with ConfigParser

description: Reading raw section with ConfigParser


code:
lst = [(1, 'John', 129L, 37L), (2, 'Tom', 231L, 23L)]
[tuple(str(x) for x in xs) for xs in lst]

rawDescription: Convert list of tuples of mixed data types into all string


code:
from suds.client import Client
url_service = 'http://www.webservicex.net/globalweather.asmx?WSDL'
client = Client(url_service)
list_of_methods = [method for method in client.wsdl.services[0].ports[0].methods]
print list_of_methods

rawDescription: How to extract method using Suds in Python

description: Extract method using Suds in Python


code:
file_info = ['{file:file1, directory:dir1}', '{file:file2, directory:directory2}']

for item in file_info:
    item = item.strip('}{').replace('file:', '').replace('directory:', '')
    print item.split(', ')

rawDescription: Extract values from within strings in a list - python


code:
for i in range(10):
    print i, '\r',
    sys.stdout.flush()
    time.sleep(1)

rawDescription: Python Shell How to Update Status in Shell Without Newline


code:
from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://en.wikipedia.org")

print len(driver.find_elements_by_xpath('//li'))

rawDescription: How to create Selenium objects?

description: Create Selenium objects


code:
class A:
    def __init__(self, a, b):
        self.a = a
        self.b = b

a = A(1, 2)
vars(a)
for attribute in vars(a).keys():
    print '%s = %s' % (attribute, str(getattr(a, attribute)))

rawDescription: Making an object's attributes iterable

description: Making an object's attributes iterable


code:
cur.execute("SELECT * FROM seats ORDER BY code")
for letter, rows in groupby(cur, key=lambda r: r[0][0]):
    print ' '.join([r[0] for r in rows])

rawDescription: SQLite Python printing in rows?

description: SQLite Python printing in rows


code:
for item in soup.select('section#price div.row h4.col-sm-4 b'):
    print item.text

rawDescription: how to obtain class contents in html using beautifulsoup?

description: Obtain class contents in html using beautifulsoup


code:
def hex2(n):
    return hex (n & 0xffffffff)[:-1]
print hex2(-1)
print hex2(17)

rawDescription: How to get hex string from signed integer

description: Get hex string from signed integer


code:
dir = SimpleFSDirectory(File(indexdir))
analyzer = StandardAnalyzer(Version.LUCENE_30)
writer = IndexWriter(dir, analyzer, True, IndexWriter.MaxFieldLength(512))
print "Currently there are %d documents in the index..." % writer.numDocs()
print "Adding %s Documents to Index..." % docs.count()
for val in terms:
    doc = Document()
    #Store the field, as-is, with term-vectors.
    doc.add(Field("field_name", val, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.YES))
    writer.addDocument(doc)

writer.optimize()
writer.close()

rawDescription: Finding a single fields terms with Lucene (PyLucene)


code:
from datetime import datetime
import urllib
from bs4 import BeautifulSoup

data = urllib.urlopen("http://www.blu-ray.com/itunes/movies.php?show=newreleases")
soup = BeautifulSoup(data)

def is_date(d):
    try:
        datetime.strptime(d, '%B %d, %Y')
        return True
    except (ValueError, TypeError):
        return False

for date in soup.find_all('h3', text=is_date):
    print date.text

    for element in date.find_next_siblings(['h3', 'table']):
        if element.name == 'h3':
            break

        print element.a.get('title')
    print "----"

rawDescription: Find HTML between two variables using Python regex


code:
urls = ["http://google.com","http://example.com","http://yahoo.com","http://linkedin.com","http://orkut.com","http://quora.com","http://facebook.com","http://myspace.com","http://gmail.com","http://nltk.org","http://cyber.com"]

def btl_test(url):
    import urllib2
    return url, urllib2.urlopen(url).read()

from contextlib import closing # http://stackoverflow.com/a/25968716/968442
from multiprocessing.pool import Pool

with closing(Pool(len(urls))) as pool:
    result = pool.map(btl_test, urls)

print result

rawDescription: How to run script in multithreading or multiprocessing

description: Run script in multithreading or multiprocessing


code:
import multiprocessing

def list_append(out_list, q):
    my_sum = sum(out_list)
    print "sum: ", my_sum
    q.put(my_sum)

if __name__ == "__main__":
    lista_1 = [i for i in xrange(500)]# Number of random numbers to add
    lista_2 = [i for i in xrange(500,1000)]
    procs = 2   # Number of processes to create

    # Create a list of jobs and then iterate through
    # the number of processes appending each process to
    # the job list
    jobs = []
    q = multiprocessing.Queue()
    process_1 = multiprocessing.Process(target=list_append, args=(lista_1, q))
    process_2 = multiprocessing.Process(target=list_append, args=(lista_2, q))
    jobs.append(process_1)
    jobs.append(process_2)

    # Start the processes (i.e. calculate the random number lists)
    for j in jobs:
        j.start()

    total = q.get() + q.get()

    # Ensure all of the processes have finished
    for j in jobs:
        j.join()

    print total

rawDescription: Multiprocessing of summing 2 lists doesn't work in python


code:
import pandas as pd
people = pd.DataFrame(people_dict)
print people['Charles Lindberg']['address']

rawDescription: Pythonic alternative to (nested) dictionaries with the same keys?


code:
for i in xrange(256):
    print hex(i)[2:].zfill(2).upper()

rawDescription: Python - counting up in hex

description: Python - counting up in hex


code:
import matplotlib as mpl
import matplotlib.cm as cm

norm = mpl.colors.Normalize(vmin=-20, vmax=10)
cmap = cm.hot
x = 0.3

m = cm.ScalarMappable(norm=norm, cmap=cmap)
print m.to_rgba(x)

rawDescription: How to map number to color using matplotlib's colormap?

description: Map number to color using matplotlib's colormap


code: print [(k, mydict[k]) for k in dict_nlargest(mydict, 2)] # [('c', 3), ('b', 2)]

rawDescription: How can I print the values of keys returned by heapq?

description: Print the values of keys returned by heapq


code:
def the_func() :
   print "Poo"
   the_func()

the_func()

rawDescription: Python - How to make program go back to the top of the code instead of closing


code:
def ask(question, wrong, correct):
    """ask: asks a multiple choice question, randomizing possible answers.

question: string containing a question
wrong:    list of strings, each one a possible answer, all of them wrong
correct:  list of strings, each one a possible answer, all of them correct

Returns True if user gives one of the correct answers, False otherwise."""

    from random import shuffle

    # the add operator (+) concatenates lists, [a,b]+[c,d]->[a,b,c,d]
    answers = wrong + correct

    # we'll need the lengths of the lists, let compute those lengths
    lw = len(wrong)
    lc = len(correct)

    # indices will be a random list containing integers from 0 to lw+lc-1
    indices = range(lw+lc) ; shuffle(indices)

    # print the question
    print question

    # print the possible choices, using the order of the randomized list
    # NB enumerate([1,3,0,2]) -> [(0,1),(1,3),(2,0),(3,2)] so that
    #    initially i=0, index=1, next i=1, index=3, etc
    for i, index in enumerate(indices):
        # print a progressive number and a possible answer,
        # i+1 because humans prefer enumerations starting from 1...
        print "  %2d:\t%s" % (i+1, answers[index])

    # repeatedly ask the user to make a choice, until the answer is
    # an integer in the correct range
    ask = "Give me the number (%d-%d) of the correct answer: "%(1,lw+lc)
    while True:
        n = raw_input(ask)
        try:
            n = int(n)
            if 0<n<lw+lc+1: break
        except ValueError:
            pass
        print 'Your choice ("%s") is invalid'%n

    # at this point we have a valid choice, it remains to check if
    # the choice is also correct...
    return True if indices[n-1] in range(lw,lw+lc) else False

rawDescription: Python: How to track "correct" answer in randomized multiple choice


rawDescription: First common element from different lists of numbers


code:
crewList2 = []
for element in tree.xpath('//video/crew/crew_member/roles'):
    role = element.xpath('.//role[1]')
    if role:
        crewList2.append(role[0].text)

print crewList2

rawDescription: get multiple tag values, except one

description: Get multiple tag values, except one


code:
def print_nested(d, prefix=''):
    for k, v in d.items():
        if isinstance(v, dict):
            print_nested(v, '{}{}_'.format(prefix, k))
        else:
            print '{}{} = {}'.format(prefix, k, v)

rawDescription: Printing key values of multi level dictionary in python


code:
import requests
for i in xrange(2500,7000):
    page = requests.get("http://bvet.bytix.com/plus/trainer/default.aspx?id={}".format(i))
    if not page.ok:
        continue
    soup = BeautifulSoup(requests.text)
    for eachuniversity in soup.findAll('fieldset',{'id':'ctl00_step2'}):
        print re.sub(r'\s+',' ',','.join(eachuniversity.findAll(text=True)).encode('utf-8'))
        print '\n'
        number = number + 1

rawDescription: Loop URL to scrape using beautiful soup python


code:
my_list = [1, 2, 3, 4]
print my_list[2:]
print my_list[3:]
print my_list[:2]

rawDescription: Ask for five values, subtract the lowest and take the average


code:
from mock import patch

class FooObject():
    def update(self, obj):
        print obj

    def restore(self, obj):
        print obj

@patch.object(FooObject, 'restore')
@patch.object(FooObject, 'update')
def test(mock1, mock2):
    assert FooObject.update is mock1
    assert FooObject.restore is mock2
    foo = FooObject()
    foo.update('foo')
    foo.restore('bar')
    return mock1, mock2

mock1, mock2 = test()
mock1.assert_called_once_with('foo')
mock2.assert_called_once_with('bar')

rawDescription: Python: using Mock to override class method


code:
def my_func(choice_pattern, input):
    # Search in input for some things to choose from.
    choice_iterator = choice_pattern.finditer(input, re.M)
    if not choice_iterator:
        print "No choices. Exiting..."
        sys.exit()

    # Show choices to the user. For each one, ask user for a yes/no response. If
    # choice accepted, return a result. Otherwise show user next choice. If no
    # choices accepted by user, quit.
    for choice in choice_iterator:
        print choice
        # get_yes_no_answer() returns True or False depending on user response.
        if get_yes_no_answer():
            return choice
    # Loop exited without matches.
    print "No matches. Exiting..."
    sys.exit()

rawDescription: Testing for an empty iterator in a Python for... loop


rawDescription: compare lists in python

description: Compare lists in python


code:
import calendar
import collections
from datetime import date

def get_weeks(year, month, holidays):
    cal = calendar.Calendar(0)
    weeks = collections.defaultdict(list)
    for i, week in enumerate(cal.monthdatescalendar(year, month)):
        # Get just mon-fri
        for day in week[:-2]:
            if day.month == month and day not in holidays:
                weeks['week%s' % (i+1)].append(day)
    return weeks
holidays = [date(2013, 8, 2), date(2013, 8, 6), date(2013, 8, 29)]
print get_weeks(2013, 8, holidays)

rawDescription: Store business days according to week in Python (2.7)?


code:
def _traverse_output_with_dict(record, fields, row_values, field_name=''):
    for obj, value in record.iteritems():
        new_field_name = '{}.{}'.format(field_name, obj) if field_name else obj
        print new_field_name
        if not isinstance(value, dict):
            if new_field_name in fields:
                row_values[new_field_name] = value
        else:
            _traverse_output_with_dict(value, fields, row_values, new_field_name)

rawDescription: Python Recursively Maintain Keyed Depth

description: Python Recursively Maintain Keyed Depth


code:
root = ET.fromstring(statsTF2)
for stat in root.findall("./stats/stat"):
    if stat.find('name').text.endswith('.accum.iPlayTime'):
        print stat.find('value').text

rawDescription: Extract text value in xml using python

description: Extract text value in xml using python


code:
for x in iter1d(a):
    print x

rawDescription: Iteration through all 1 dimensional subarrays of a multi-dimensional array


code:
import numpy as np

class CustomArray(np.ndarray):
    def __new__(cls, *args, **kwargs):
        return np.asarray(args[0]).view(cls)

    def __getitem__(self, index):
        return np.ndarray.__getitem__(self, index % len(self))

a = CustomArray([1, 2, 3])
for index in xrange(5):
    print a[index]

rawDescription: Circular array indices in Python

description: Circular array indices in Python


code:
class Foo(object):
    def __init__(self, name):
        self.__name = name

    def getname(self):
        return self.__name

    def setname(self, name):
        print "Intercepted: name attribute of Foo set to " + name
        self.__name = name

    name = property(getname, setname)

rawDescription: Protecting a mutable object from changing in Python


code:
def child(heartbeat):
    for _ in xrange(10):
        if not heartbeat.isSet():
            print "Parent is dead. Leaving child."
            return

        heartbeat.clear()

        print "Child alive"
        time.sleep(1)

def parent():
    heartbeat = Event()
    heartbeat.set()

    t = Thread(target=child, args=(heartbeat,))
    t.start()

    i = 0
    while i < 20:
        print "Parent alive"
        i += 1
        heartbeat.set()
        time.sleep(.1)

    print "Parent done looping...pretending to be deadlocked"
    time.sleep(5)

    t.join()

rawDescription: Check if parent thread is running

description: Check if parent thread is running


code: print df_list[ df_list['b'].apply(lambda x:test_all_2([2,3,8], x) ) ]

rawDescription: Selecting rows by members of a list contained in a cell


code:
import threading

mydata = threading.local()

class x:
    def __init__(self):
        print "x got created!"
    def __del__(self):
        print "x got deleted!"

def run():
    mydata.foo = x()

t = threading.Thread(target=run)
print "t created"
t.start()
print "t started"
del mydata
print "mydata deleted"
t.join()
print "t joined"
print "Done!"

rawDescription: What's the life-time of a thread-local value in Python?


code:
import argparse

parser= argparse.ArgumentParser()
parser.add_argument ('-f', '--file', nargs=3, action='append')

files = parser.parse_args('-f file1 string1 string2 -f file2 string3 string4 -f file3 string5 string6'.split()).file

for f in files:
    print tuple(f)

rawDescription: Getting command line arguments as tuples in python


code:
for example in ('data_T_1e-05.d', 'data_T_7.2434.d', 'data_T_0.001.d'):
    print float(example[7:-2])

rawDescription: Extract number from file name in python

description: Extract number from file name in python


code:
import logging

FORMAT = '%(asctime)s %(levelname)s %(message)s %(funcName)s'
logging.basicConfig(filename='example.log', level=logging.DEBUG, format=FORMAT, datefmt='%m/%d/%Y %I:%M:%S %p')

def debug(func):
    def _debug(msg, deb=0):
        if deb == 1:
            func(msg, deb)
        if deb == 2:
            print 'Log: ' + msg + ' Level: ' + str(deb)
            logging.debug(msg)
    return _debug

@debug
def echo(msg, deb):
    print 'Echo: ' + msg + ' Level: ' + str(deb)

if __name__ == "__main__":
    echo("This is a debugging message!")
    echo("This is a debugging message!", 0)
    echo("This is a debugging message!", 1)
    echo("This is a debugging message!", 2)

rawDescription: Conditionals decoration of functions in Python


code:
import subprocess

list_of_ips = ["10.0.2.5", "8.8.8.8"]
for ip in list_of_ips:
    ping_process = subprocess.Popen(['ping', '-c', '1', ip], stdout=subprocess.PIPE)
    stdout = ping_process.stdout.read()
    print stdout

rawDescription: Pythononly pings 1 out of 2 IP's (Subprocess)


code:
from multiprocessing import Process, Value, Array

def f(n, a):
    n.value = 3.1415927
    for i in range(len(a)):
        a[i] = -a[i]

if __name__ == '__main__':
    num = Value('d', 0.0)
    arr = Array('i', range(10))

    p = Process(target=f, args=(num, arr))
    p.start()
    p.join()

    print num.value
    print arr[:]

rawDescription: Share a class variable across multiple processes in python


code:
if args in ('acc1', 'acc2', 'acc3', 'acc4'):
    execfile('tokens/%s.py' % args)
    print "Account tokens were successfully loaded."
else:
    print "Account tokens were not found, or the argument is invalid."
    quit()

rawDescription: Can't make execfile() variables be global instead of local


code:
user_input = "Current Level: 1e100 db"
for token in user_input.split():
    try:
        # if this succeeds, you have your (first) float
        print float(token), "is a float"
    except ValueError:
        print token, "is something else"

# => Would print ...
#
# Current is something else
# Level: is something else
# 1e+100 is a float
# db is something else

rawDescription: How to extract a floating number from a string in Python

description: Extract a floating number from a string in Python


code:
new_strs = []
for character in phrase:
    x = ord(character)
    x = x + shift
    new_strs.append(chr(x))   #store the new shifted character to the list
    #use this if you want z to shift to 'a'
    #new_strs.append(chr(x if 97 <= x <= 122 else 96 + x % 122))
print "".join(new_strs)       #print the new string

rawDescription: Python Caesar Cipher Using Ord and Chr

description: Python Caesar Cipher Using Ord and Chr


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

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

    for _method in ['baz', 'blob']:
        @property
        def _tmp_func(self, _name=_method):
            return getattr(self.foo, _name)
        locals()[_method] = _tmp_func
    # del to avoid name pollution
    del _method, _tmp_func

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

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


rawDescription: find all digits between a character in python

description: Find all digits between a character in python


code:
def same_diff(a, b):
    sames = []
    diffs = []
    i = 0
    j = 0
    while i < len(a) and j < len(b):
        if a[i] == b[j]:
            print 'aligned', a[i]
            sames.append(a[i])
            i += 1
            j += 1
        elif a[i] > b[j]:
            print 'miss-aligned', b[j]
            diffs.append(b[j])
            j += 1
        else:
            print 'miss-aligned', a[i]
            diffs.append(a[i])
            i += 1
    while i < len(a):
        print 'miss-aligned', a[i]
        diffs.append(a[i])
        i += 1
    while j < len(b):
        print 'miss-aligned', b[j]
        diffs.append(b[j])
        j += 1
    return sames, diffs

rawDescription: Align two lists and do action on aligned and miss-aligned objects


code:
for row in soup.select("div#page_filling_chart table tr")[1:]:
    cells = row('td')
    print cells[1].get_text(strip=True), cells[2].get_text(strip=True)

rawDescription: Pointing BeautifulSoup towards a certain <tr> class


code: print file.readlines()[0]

rawDescription: Need help reading data from input files and displaying it


code:
from optparse import OptionParser

class FileBase(object):
    def __init__(self, fname):
        self.fname = fname
        self.path = None
class LocalFile(FileBase):
    pass
class RemoteFile(FileBase):
    pass

class FileOptionParser(object):
    def __init__(self):
        self.last_file = None
        self.files = []

    def set_path(self, option, opt, value, parser):
        self.last_file.path = value

    def set_file(self, option, opt, value, parser):
        if   option.dest=="local" : cls = LocalFile
        elif option.dest=="remote": cls = RemoteFile
        else:                       assert False
        self.last_file = cls(value)
        self.files.append(self.last_file)
        setattr(parser.values, option.dest, self.last_file)

fop = FileOptionParser()

parser = OptionParser()
parser.add_option('-L', '--local',  type='string', action='callback', callback=fop.set_file)
parser.add_option('-R', '--remote', type='string', action='callback', callback=fop.set_file)
parser.add_option('-p', '--path',   type='string', action='callback', callback=fop.set_path)

(options, args) = parser.parse_args()
print [(f.fname, f.path) for f in fop.files]

rawDescription: Python optparse: how to make a repeatable group of options?


code:
i=3
def foo():
    print i
i=4
foo()  #prints 4, when you expected 3

rawDescription: Python TkInter - Assigning tags to indeterminate number of images


code:
import time

for i in range(100):
    print i
    time.sleep(2)

rawDescription: Python 3.4 Compiler?

description: Python 3.4 Compiler


code:
for k,v in map(lambda x: x.items()[0], y):
    print k, v

rawDescription: Iterate over a list of objects getting ke/value pairs


rawDescription: Python reading Wiegand dropping Zeros

description: Python reading Wiegand dropping Zeros


code:
from bs4 import BeautifulSoup as bs
import bs4
html = "<foo>Something something <bar> blah blah</bar> something <bar2>GONE!</bar2> else</foo>"
def get_only_text(elem):
    for item in elem.children:
        if isinstance(item,bs4.element.NavigableString):
            yield item

print ''.join(get_only_text(bs(html).find_all('foo')[0]))

rawDescription: How to remove content in nested tags with BeautifulSoup?

description: Remove content in nested tags with BeautifulSoup


code:
t = timeit.Timer(stmt="itertools.chain(*(('aa',), ('bb',), ('cc',)))")
print t.timeit()
t = timeit.Timer(stmt="[a[0] for a in (('aa',), ('bb',), ('cc',))]")
print t.timeit()

rawDescription: How do I convert tuple of tuples to list in one line (pythonic)?

description: Convert tuple of tuples to list in one line (pythonic)


code:
import shapely.geometry as geom
import numpy as np

coords = np.loadtxt('points.txt')

line = geom.LineString(coords)
point = geom.Point(0.8, 10.5)

# Note that "line.distance(point)" would be identical
print point.distance(line)

rawDescription: Find minimum distance from point to complicated curve


code: print list(set(zip(*myList)[1]))

rawDescription: Getting unique items from a two dimensional array python


code:
class MySet(frozenset):

    def __new__(cls, *args):
        if args and isinstance (args[0], basestring):
            args = (args[0].split (),) + args[1:]
        return super (MySet, cls).__new__(cls, *args)

print MySet ('foo bar baz')

rawDescription: Python, subclassing immutable types

description: Python, subclassing immutable types


code:
#!/usr/bin/python
from SimpleXMLRPCServer import SimpleXMLRPCServer
import xmlrpclib

arrayToAppend = []

def appendArray(value):
    global arrayToAppend
    arrayToAppend.append(value)
    return 1

def valueOfArray():
    return arrayToAppend

server = SimpleXMLRPCServer(("localhost", 8000))
print "Listening on port 8000..."
server.register_function(appendArray, "appendArray")
server.register_function(valueOfArray, "valueOfArray")
server.serve_forever()

rawDescription: Add item to list in python script on other server


code:
import urllib
from bs4 import BeautifulSoup

HTML_File = urllib.urlopen("http://uk.weather.com/weather/10day/New+Romney+KEN+United+Kingdom+UKXX1121:1:UK")
HTML_Text = HTML_File.read()
soup = BeautifulSoup(HTML_Text, 'html.parser')

print soup.select('.wx-timestamp')[0].text

rawDescription: Python trying to get paragraph from weather website


code:
lis = [1,2,1,'a',4,2,3,3,5,'a',2]
dic={}
index=0
for item in lis:
    if item in dic:
        dic[item]+=[index]
        index+=1
    else:
        dic[item]=[index]
        index+=1
print dic

rawDescription: Finding multiple occurrences of dictionary?


code:
import itertools

items = [
    ("Edern", 38),
    ("Pellam", 34),
    ("Ban", 32),
    ("Lionel", 30),
    ("Geraint", 30),
    ("Brangaine", 28),
    ("Erec", 28),
    ("Guiron", 28),
    ("Fisher", 28),
    ("Elyan", 28),
    ("Segwarides", 26)
]

result = []
for k,v in itertools.groupby(items, lambda item: item[1]):
    result.extend(sorted(v))

print result

rawDescription: How to sort keys only with same values?

description: Sort keys only with same values


code:
def kill(o):
    if o is duck:
        o.die()
    elif o is dog:
        print "WHY????"
        o.die()
    elif o is nyancat:
        raise Exception("NYAN "*9001)
    else:
       print "can't kill it."

rawDescription: In Python, when should I use a function instead of a method?


code:
for search_txt, replacement_txt in Label.iteritems():
    data = re.sub(search_txt, replacement_txt, data)
print data

rawDescription: Search and replace between two files with search phrase in first file: Python


code:
testbank = {"question 1":["answer1a", "answer1b", "answer1c", "answer1d"],
 "question n":["answerna", "answernb", "answernc", "answernd"]}
testbank['question 1']
def validate(answer, question):
 if answer in testbank['question 1']:
  print 'Correct!'
 else:
  print 'Try again.'
answer = "answer1a"
validate(answer, 'question 1')
wrongansw = 'foobar'
validate(wrongansw, 'question 1')

rawDescription: Python: How to use a similar operator to "or"/"in" in a list


rawDescription: how to encode url in python

description: Encode url in python


code:
from itertools import groupby
from operator import itemgetter

for letter, words in groupby(sorted(somelist), key=itemgetter(0)):
    print letter
    for word in words:
        print word
    print

rawDescription: Python: Split list based on first character of word


code:
import itertools
a = [[1, 2, 3], [4, 5], [3], [1]]
p = itertools.product(*a)
print list(p)

rawDescription: generating cross product for list of lists

description: Generating cross product for list of lists


code: print u'ĉ'.upper()

rawDescription: Convert Unicode/UTF-8 string to lower/upper case using pure & pythonic library


code:
import csv
from collections import namedtuple
from StringIO import StringIO
def read_csv(file, columns, type_name="Row"):
  try:
    row_type = namedtuple(type_name, columns)
  except ValueError:
    row_type = tuple
  rows = iter(csv.reader(file))
  header = rows.next()
  mapping = [header.index(x) for x in columns]
  for row in rows:
    row = row_type(*[row[i] for i in mapping])
    yield row

testdata = """\
AAA,bbb,ccc,DDD,eee,FFF,GGG,hhh
1,2,3,4,50,3,20,4
2,1,3,5,24,2,23,5
4,1,3,6,34,1,22,5
2,1,3,5,24,2,23,5
2,1,3,5,24,2,23,5
"""
testfile = StringIO(testdata)
for row in read_csv(testfile, "AAA GGG DDD".split()):
  print row

rawDescription: Read specific columns in csv using python


code:
from lxml import etree
xml = """<IssueTracking>
  <Issue>
    <SequenceNum>123</SequenceNum>
    <Subject>Subject of Ticket 123</Subject>
    <Description>Line 1 in Description field of Ticket 123.
Line 2 in Description field of Ticket 123.
Line 3 in Description field of Ticket 123.</Description>
  </Issue>
  <Issue>
    <SequenceNum>124</SequenceNum>
    <Subject>Subject of Ticket 124</Subject>
    <Description>Line 1 in Description field of Ticket 124.
Line 2 in Description field of Ticket 124.
Line 3 in Description field of Ticket 124.</Description>
  </Issue>
</IssueTracking>
"""

root = etree.fromstring(xml)
for issue in root.findall('Issue'):
    as_list = [issue.find(n).text for n in ('SequenceNum', 'Subject', 'Description')]
    as_list[2] = as_list[2].split('\n')
    print as_list

rawDescription: Python - How to nest file read loops?

description: Python - How to nest file read loops


code:
import argparse

foo_default=None

class BarAction(argparse.Action):
    def __call__(self,parser,namespace,values,option_string=None):
        didfoo=getattr(namespace,'foo',foo_default)
        if(didfoo == foo_default):
            parser.error( "foo before bar!")
        else:
            setattr(namespace,self.dest,values)

parser=argparse.ArgumentParser()
parser.add_argument('--foo',default=foo_default)
parser.add_argument('--bar',action=BarAction,help="Only use this if --foo is set")

#testing.
print parser.parse_args('--foo baz'.split())
print parser.parse_args('--foo baz --bar cat'.split())
print parser.parse_args('--bar dog'.split())

rawDescription: python, argparse: enable input parameter when another one has been specified

description: Python, argparse: enable input parameter when another one has been specified


code:
def my_loop(self):
    if time.time() < self.end_time:
       print "listening"
       root.after(5000, self.my_loop) # run my_loop again after 5000 milliseconds

def start_game(self):
        global p2score

        # use self. to get access in other function
        # self.game_time = 30
        self.end_time = time.time() + 30

rawDescription: Python RaspberryPi GPIO Event Detection in Tkinter Failing


code:
import requests
from bs4 import BeautifulSoup
href_val = requests.get("http://www.afl.com.au/news/teams?round=9")
soup1 = BeautifulSoup(href_val.content)
for player in soup1.select('ul.team1 li.player'):
    text = player.get_text().strip()
    print text

for player in soup1.select('ul.team1 li.player'):
    number, name = player.span.get_text().strip(), player.span.next_sibling.strip()
    print name

rawDescription: Parser returns "\n" instead of desired output.


code:
class MCLidarActions( object ):
    li = []

    tcp_send = 'Event'

    def __init__(self):
        if self.__class__ != MCLidarActions:
            self.li.append(self)

    def object__updated_changed( self, info ):
        print info;
        pass;

    def _tcp_send_changed( self ):
        print( "Click" )
        for x in self.li:
            x._tcp_send_changed()

class MCUDPActions( MCLidarActions ):
    def object__updated_changed( self, info ):
        pass;
    def _tcp_send_changed( self ):
        print( "Click UDP" )

class MC_uuuuuuuuuuuuuutp_Actions( MCLidarActions ):
    def object__updated_changed( self, info ):
        pass;
    def _tcp_send_changed( self ):
        print( "Click _uuuuuuuuuuuuuutp_" )

rawDescription: Python function calling

description: Python function calling


code:
vara = tf.Variable(0)
varb = tf.Variable(0)
init_op = tf.initialize_all_variables()
sess = tf.InteractiveSession()
sess.run([init_op])
sess.run([vara.assign_add(1)])
print 'variable a', vara.eval()
print 'variable b', varb.eval()
sess.run([varb.assign(vara)])
print 'variable b', varb.eval()

rawDescription: Initializing variable with another variable using tensorflow


code:
f = open(sys.argv[1])
mb_toc_urlpart = "%20".join(
    str(x) for x in calculate_mb_toc_numbers(filter_toc_entries(f)))
print mb_toc_urlpart

rawDescription: Python kludge to read UCS-2 (UTF-16?) as ASCII


code:
import threading

class CountThread(threading.Thread):
  def __init__(self, gen):
      threading.Thread.__init__(self)
      self.gen = gen
      self.numbers_seen = 0

  def run(self):
      for i in self.gen:
          self.numbers_seen += 1

def generator(data):
    for _ in data:
        yield data

gen = generator(xrange(10000))

a = CountThread(gen)
b = CountThread(gen)

a.start()
b.start()

a.join()
b.join()

print "Numbers seen in a", a.numbers_seen
print "Numbers seen in b", b.numbers_seen

rawDescription: Python multithreading: Why generators are not thread-safe? What happens when it is shared among threads?


code:
import sys
from PyQt4 import QtGui, QtCore

class TreeModel(QtCore.QAbstractItemModel):
    def __init__(self):
        QtCore.QAbstractItemModel.__init__(self)
        self.nodes = ['node0', 'node1', 'node2']

    def index(self, row, column, parent):
        return self.createIndex(row, column, self.nodes[row])

    def parent(self, index):
        return QtCore.QModelIndex()

    def rowCount(self, index):
        if index.internalPointer() in self.nodes:
            return 0
        return len(self.nodes)

    def columnCount(self, index):
        return 1

    def data(self, index, role):
        if role == 0:
            return index.internalPointer()
        else:
            return None

    def supportedDropActions(self):
        return QtCore.Qt.CopyAction | QtCore.Qt.MoveAction

    def flags(self, index):
        if not index.isValid():
            return QtCore.Qt.ItemIsEnabled
        return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | \
               QtCore.Qt.ItemIsDragEnabled | QtCore.Qt.ItemIsDropEnabled

    def mimeTypes(self):
        return ['text/xml']

    def mimeData(self, indexes):
        mimedata = QtCore.QMimeData()
        mimedata.setData('text/xml', 'mimeData')
        return mimedata

    def dropMimeData(self, data, action, row, column, parent):
        print 'dropMimeData %s %s %s %s' % (data.data('text/xml'), action, row, parent)
        return True

class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        self.treeModel = TreeModel()

        self.view = QtGui.QTreeView()
        self.view.setModel(self.treeModel)
        self.view.setDragDropMode(QtGui.QAbstractItemView.InternalMove)

        self.setCentralWidget(self.view)

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

rawDescription: QTreeView with drag and drop support in PyQt


code:
from twython import Twython
twitter = Twython()
search_results = twitter.search(q="#somehashtag", rpp="50")

for tweet in search_results["results"]:
    print "Tweet from @%s Date: %s" % (tweet['from_user'].encode('utf-8'),tweet['created_at'])
    print tweet['text'].encode('utf-8'),"\n"

rawDescription: Fetching tweets with hashtag from Twitter using Python


code:
# ab_decider.py
import time
import boto.swf.layer2 as swf

DOMAIN = 'stackoverflow'
ACTIVITY1 = 'ServerAActivity'
ACTIVITY2 = 'ServerBActivity'
VERSION = '1.0'

class ABDecider(swf.Decider):

    domain = DOMAIN
    task_list = 'default_tasks'
    version = VERSION

    def run(self):
        history = self.poll()
        # Print history to familiarize yourself with its format.
        print history
        if 'events' in history:
            # Get a list of non-decision events to see what event came in last.
            workflow_events = [e for e in history['events']
                               if not e['eventType'].startswith('Decision')]
            decisions = swf.Layer1Decisions()
            # Record latest non-decision event.
            last_event = workflow_events[-1]
            last_event_type = last_event['eventType']
            if last_event_type == 'WorkflowExecutionStarted':
                # At the start, get the worker to fetch the first assignment.
                decisions.schedule_activity_task('%s-%i' % (ACTIVITY1, time.time()),
                   ACTIVITY1, VERSION, task_list='a_tasks')
            elif last_event_type == 'ActivityTaskCompleted':
                # Take decision based on the name of activity that has just completed.
                # 1) Get activity's event id.
                last_event_attrs = last_event['activityTaskCompletedEventAttributes']
                completed_activity_id = last_event_attrs['scheduledEventId'] - 1
                # 2) Extract its name.
                activity_data = history['events'][completed_activity_id]
                activity_attrs = activity_data['activityTaskScheduledEventAttributes']
                activity_name = activity_attrs['activityType']['name']
                # 3) Optionally, get the result from the activity.
                result = last_event['activityTaskCompletedEventAttributes'].get('result')

                # Take the decision.
                if activity_name == ACTIVITY1:
                    # Completed ACTIVITY1 just came in. Kick off ACTIVITY2.
                    decisions.schedule_activity_task('%s-%i' % (ACTIVITY2, time.time()),
                        ACTIVITY2, VERSION, task_list='b_tasks', input=result)
                elif activity_name == ACTIVITY2:
                    # Server B completed activity. We're done.
                    decisions.complete_workflow_execution()

            self.complete(decisions=decisions)
            return True

rawDescription: Using Amazon SWF To communicate between servers


code:
from itertools import groupby

def summarize_inventory(inventory):
    # We use -price so that we are also sorting by descending price
    sort_key = lambda (code, (cat, price, sold)): (cat, -price)
    group_key = lambda (code, (cat, price, sold)): cat

    new_dict = {}
    sorted_inventory = sorted(inventory.iteritems(), key=sort_key)
    for cat, group in groupby(sorted_inventory, key=group_key):
        group = list(group)
        if len(group) > 1:
            for (code, item) in group[:2]:
                new_dict[code] = item
    return new_dict

rawDescription: Summarizing dictionary with top two choices


code:
import mechanize
from bs4 import BeautifulSoup as BS
import threading

# Max no. of threads allowed to be alive.
limit = 10

entrada = "entrada.txt"
saida = "saida.txt"

def write(text):
    with open(saida, "a") as f:
        f.write(text)

# Threading locks
fileLock = threading.Lock()
printLock = threading.Lock()

def print_out(text):
        printLock.acquire()
        print text
        printLock.release()

# Thread for each user
class UserThread(threading.Thread):
    def __init__(self, user):
        threading.Thread.__init__(self)
        self.user = user.rstrip()
    def run(self):
        to_file = ""
        try:
            cont = 1

            # Initialize Mechanize
            br = mechanize.Browser()
            br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
            br.set_handle_robots(False)
            br.open("https://site")

            # Submit form
            br.select_form(nr=0)
            br["username"] = self.user
            br["password"] = self.user
            br.submit()

            # Soup Response
            soup = BS(br.response().read(), "lxml")
            value = soup.find_all("a")
            # Write to file
            txt = "\nConta - Saldo["+value[2].text+"]\n"
            print_out(txt)
            to_file += txt

            # Retrieve response from another page
            br.open("https://test/sub")
            soup = BS(br.response().read(), "lxml")
            # Write to file
            txt = "Procurando por cartoes na conta"
            print_out(txt)
            to_file += txt

            for tds in soup.find_all("td"):
                if len(tds.text) > 30:
                    # Write to file
                    cc = "CC["+str(cont)+"] ~> "+tds.text+"\n"
                    print_out(cc)
                    to_file += cc
                    cont += 1

            txt = "\nTotal ["+str(cont-1)+"]\n-------------------------------------------------\n"
            to_file += txt
        except Exception:
            erro = "\n[!]Erro ao logar["+self.user+"]\n-------------------------------------------------\n"
            to_file += erro
            print_out(erro)

        # Write everything to file
        fileLock.acquire()
        write(to_file)
        fileLock.release()

threads = []

with open(entrada) as fp:
    for user in fp:
        threads.append(UserThread(user))

active_threads = []
for thread in threads:
    if len(active_threads) <= limit:
        # Start threads
        thread.start()
        active_threads.append(thread)
    else:
        for t in active_threads:
            # Wait for everything to complete before moving to next set
            t.join()
        active_threads = []

rawDescription: How to make mechanize script multthread in python

description: Make mechanize script multthread in python


code:
old_help = help
def help(thing):
    if hasattr(thing, '__help__'):
        print thing.__help__
    else:
        old_help(thing)

rawDescription: Where does Python's pydoc help function get its content?


code:
from bs4 import BeautifulSoup, CData
import urllib

source_txt = urllib.urlopen("http://voanews.com/api/epiqq")
soup = BeautifulSoup.BeautifulSoup(source_txt.read())
for cd in soup.findAll(text=True):
    if isinstance(cd, CData):
        print 'CData value: %r' % cd

rawDescription: how to get content inside CDATA of XML tag django

description: Get content inside CDATA of XML tag django


code:
class b:
    def __init__(self):
        self.attribute_function = None

class a:
    def __init__(self):
        self._temp = 10
        self.counter = 0

    def temp(self):
        obj = b()
        obj.attribute_function = lambda self=self: self.counter < self._temp
        return obj.attribute_function()

if __name__ == "__main__":
    obj = a()
    print obj.temp()

rawDescription: Python Class Objects Attribute Referencing


code:
letters = ['h','e','l','l','o']
mystr = ' '.join(letters)
print mystr

rawDescription: Iterating over list of str to make a str with spaces


code: print file_contents.rstrip('\n')

rawDescription: remove empty last line from string (output from pipe)

description: Remove empty last line from string (output from pipe)


code:
def blackjack(A,B):
   print "Welcome to Blackjack!"
   print "Your cards are",name[A-1],"&",name[B-1]
   total = value[A-1] + value[B-1]
   print "Your card total is",total

rawDescription: random.randint not generating random values

description: Random.randint not generating random values


code:
import sqlite3
from xlsxwriter.workbook import Workbook
workbook = Workbook('output2.xlsx')
worksheet = workbook.add_worksheet()

conn=sqlite3.connect('test.sqlite')
c=conn.cursor()
c.execute("select * from abc")
mysel=c.execute("select * from abc ")
for i, row in enumerate(mysel):
    print row
    worksheet.write(i, 0, row[0])
    worksheet.write(i, 1, row[1])
workbook.close()

rawDescription: how to import data from SQLITE to excel using xlsxwriter in python

description: Import data from SQLITE to excel using xlsxwriter in python


code:
# your setup code here
df2 = pd.concat(df['c'].tolist(), keys=df.index)
df3 = pd.merge(df[["a", "b"]], df2, left_index=True, right_on=df2.index.get_level_values(0))
df4 = df3.drop("key_0", axis=1).reset_index(level=0, drop=True)
print df4

rawDescription: Explode a nested pandas dataframe out as columns, and repeat parent rows for each of its row


code:
from collections import defaultdict
from itertools import groupby

def class_chars(chrs):
    if 'N' in chrs:
        return 'unknown'
    elif chrs[0] == chrs[1]:
        return 'match'
    else:
        return 'not_match'

s1 = 'aaaaaaaaaaN123bbbbbbbbbbQccc'
s2 = 'aaaaaaaaaaN456bbbbbbbbbbPccc'
n = 0
consec_matches = []
chars = defaultdict(int)

for k, group in groupby(zip(s1, s2), class_chars):
    elems = len(list(group))
    chars[k] += elems
    if k == 'match':
        consec_matches.append((n, n+elems-1))
    n += elems

print chars
print consec_matches
print [x for x in consec_matches if x[1]-x[0] >= 9]

rawDescription: Identify short sequences of matches between 2 strings


code:
import re
from functools import partial

text = u'''
[...]
'''
# The key is the external tag to extract
# The value a list of attributes whose content has to be replaced
sub_dict = {"RoomRatesWithoutServices": ['CurrencyCode1', 'CurrencyCode2'],
            "AnotherTag": ['AnotherAttr']}

replacement = '_REPLACED_'

def closure(attr, replacement, m):
    attr_pattern = '(?<=(?:%s)=")[^"]+(?=")' % attr
    return re.sub(attr_pattern, replacement, m.group())

for ext_tag, attr_list in sub_dict.iteritems():
    attr = r"|".join(attr_list)
    tag_pattern = r"(?s)<%s>.*?</%s>" % (ext_tag, ext_tag)
    text = re.sub(tag_pattern, partial(closure, attr, replacement), text)

print text

rawDescription: Regex to match everything between (potentially multiple) pairs of tags


code:
import sys
import socket
import Queue as Q
import time
from multiprocessing import Process, Queue

UDP_ADDR = ("", 13000)

def send(m):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.sendto(m, UDP_ADDR)

def receive(q):
    buf = 1024
    Sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    Sock.bind(UDP_ADDR)
    while True:
      (data, addr) = Sock.recvfrom(buf)
      q.put(data)

def doit():
  # ... what the processing thread will do ...
  while True:
    print "sleeping..."
    time.sleep(3)

def client():
  q = Queue()
  r = Process(target = receive, args=(q,))
  r.start()

  print "client loop started"
  t = None   # the processing thread
  while True:
      m = q.get()
      if m == "start":
        if t:
          print "processing thread already started"
        else:
          t = Process(target = doit)
          t.start()
          print "processing thread started"
      elif m == "stop":
        if t:
          t.terminate()
          t = None
          print "processing thread stopped"
        else:
          print "processing thread not running"
      elif m == "quit":
        print "shutting down"
        if t:
          t.terminate()
          t = None  # play it safe
        break
      else:
        print "huh?"
  r.terminate()

if __name__ == '__main__':
  args = sys.argv
  if len(args) > 1:
    send(args[1])
  else:
    client()

rawDescription: Handle multiple messages with Queue get()


code:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('limit', type=int, nargs='?')
args = parser.parse_args()
if args.limit is not None:
    print "Limiting to %d." % -args.limit

rawDescription: Python argument to provide less-like limiting functionality


code:
#!/usr/bin/env python
from __future__ import division
import functools
import math
import sys

from timeit import default_timer as timer

import numpy as np
import matplotlib.pyplot as plt
import scipy.linalg as linalg

def report_time(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        start = timer()
        try: return func(*args, **kwargs)
        finally:
            print '%s takes %.2f seconds' % (func.__name__, timer()-start)
    return wrapper

@report_time
def calc(Nmesh,POWER,L ,numwav=0):
    #
    dx=L/Nmesh
    x = np.arange(-L,L+0.0001,dx)
    Npts=len(x)

    V = x**POWER

    #
    ai = np.empty((2,Npts))   # ai[:,i] = a[:,i-1]
    ai[0,:] = 1/dx**2 + V     #
    ai[1,:] = -1.0/dx**2/2    #
    ai[1,Npts-2] = -99.0      #
    a = ai[:,1:-1]
    f = report_time(linalg.eig_banded)
    eig, vec = f(a, lower=True,overwrite_a_band=True,
                 select='i',select_range=(0,numwav)
                 ) #

    wave = np.empty(Npts)
    wave[1:-1] = vec[:,numwav]
    wave[0] = 0             #
    wave[Npts-1] = 0        #
    wave = 150*wave + eig[numwav]
    return x, V, wave, eig[numwav]

def main():
    try: numwav = int(sys.argv[1])
    except (IndexError, ValueError):
        numwav = 0
    POWER=100
    L=4.0
    Nmesh = 512
    print 'Nmesh=%d' % Nmesh
    x, V, wave, y = calc(Nmesh, POWER, L,numwav)

    #
    line = plt.plot(x,V)
    plt.setp(line,color='firebrick',linewidth=2)

    #
    plt.plot(x,wave,"b-",linewidth=2,color='forestgreen')

    #
    plt.axhline(y=y,linewidth=2,color='steelblue')

    plt.xlabel(r'$x$',size=14)
    plt.ylabel(r'$V(x)$',size=14)
    plt.title(r'$V(x)=x^{%d}$, ' % POWER)
    plt.axis([-(abs(L)-1), abs(L)-1,min(min(wave), y, min(V))-1, max(max(wave), y)+1]) #
    plt.grid(True)
    plt.legend((r'$V(x)$',r'$E_%d$' % numwav,r'$\psi_%d$' % numwav))
    plt.savefig('V_%d_%d_%d.png' % (Nmesh, POWER, numwav))
    plt.show()

if __name__=="__main__":
    main()

rawDescription: Discretization of a diff. equation gives weird results


code:
x = range(10)
for i in x:
    print i
    if i%2==0:
            x.remove(i)

rawDescription: Why is my list out of order consistently?


code:
def do_loop(self):
    for line in self.connections[0].iter_lines():
        if self.new_conn.is_set():
            break # or could raise exception here
        print line

rawDescription: Break out of loop immediately after array is changed


code:
import sqlite3 as lite

con = lite.connect('test.db')
with con:
    cur = con.cursor()
    for row in cur.execute("Select words, Sentences From test"):
        inpt = "what the meaning of Python ?"
        if row[0] in inpt:
            print row[1]

rawDescription: confuse create print out when create some condition in Sqlite3 with python

description: Confuse create print out when create some condition in Sqlite3 with python


code:
obj = data.json()
for child in obj['data']['children']:
    print child['data']['title']

rawDescription: How to filter a big chunk of text which contains no line breaks in python?

description: Filter a big chunk of text which contains no line breaks in python


code:
PathwayList = []
for InFileName in FileList:
    sys.stderr.write("Processing file %s\n" % InFileName)
    InFile = open(InFileName, 'r')
    PathwayList.append(InFile.readlines())
    InFile.close()

for contents in PathwayList:
    # do something with contents which is a list of strings
    print contents

rawDescription: python beginner - how to read contents of several files into unique lists?

description: Python beginner - how to read contents of several files into unique lists


rawDescription: average of certain values of an array

description: Average of certain values of an array


code:
for cols in zip(*matrix):  # transposed
    print ' '.join(cols)

rawDescription: How to make matrices in Python?

description: Make matrices in Python


code:
import numpy as np
from scipy import stats

dataset1 = np.random.randn(100)  #random distribution
dataset2 = np.random.randn(100)

res = stats.ks_2samp(dataset1, dataset2)

print res

rawDescription: Processing randomly distributed experimental data using ks_2samp in scipy


code:
with open(waveform, 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        print row[:-1]

rawDescription: Python CSV: reading rows terminated by comma results in an empty result


code:
for codon in mutated_codon:
    print codon_lookup[codon]

rawDescription: How can I do the following comparison without having to write 20 if-statements or making 20 lists/dictionaries?

description: Do the following comparison without having to write 20 if-statements or making 20 lists/dictionaries


code: print msg

rawDescription: python mail no subject coming through

description: Python mail no subject coming through


code:
if True:
    for key in vardict:
        vardict[key] = addTen(vardict[key])

print vardict

rawDescription: Most Pythonic way to update variables if condition is met


code:
import numpy as np
import itertools

def maenumerate(marr):
    mask = ~marr.mask.ravel()
    for i, m in itertools.izip(np.ndenumerate(marr), mask):
        if m: yield i

N = 12
a = np.arange(N).reshape(2, 2, 3)+10

b = np.ma.array(a, mask = (a%5 == 0))
for i, val in maenumerate(b):
    print i, val

rawDescription: numpy: ndenumerate for masked arrays?

description: Numpy: ndenumerate for masked arrays


code:
import random
print "WELCOME TO LOTTERY HACK MACHINE!!!\n"
today = int(raw_input( "Please enter today's date: " ))
if today<=31:
    print "Please enter the 4-digit prize winning lottery number for the last 7 days"
    y = raw_input( "Enter 7 numbers separated by commas: " )
    input_list = y.split(',')
    numbers = [float(x.strip()) for x in input_list]
    print random.choice(numbers)

elif today>31:
    print "A month has only 31 days ;P"

rawDescription: How to choose a random input from 7 given input?

description: Choose a random input from 7 given input


code:
#!/usr/bin/python
from sys import argv
script, param = argv
print param

rawDescription: Syntax for submitting a qsub job without an actual job file?


code:
from random import random

def shuffle(string, factor):
    string    = list(string)
    length      = len(string)
    if length < 2:
        return string
    shuffles    = int(length * factor)
    for i in xrange(shuffles):
        i, j    = tuple(int(random() * length) for i in xrange(2))
        string[i], string[j]    = string[j], string[i]

    return "".join(string)

x = "computer"
print shuffle(x, .2)
print shuffle(x, .5)
print shuffle(x, .9)

rawDescription: How can I scramble a word with a factor?

description: Scramble a word with a factor


code:
import re
seq = "blahblahblahLALALAblahblahLALA"
substring="LALA"
lenss=len(substring)
overlapsearch="(?=(\\"+substring+"))"
xx=[[x.start(),x.start()+lenss] for x in list(re.finditer(overlapsearch,seq))]
check=[seq[x[0]:x[1]] for x in xx]
print xx
print check

rawDescription: Match and index all substrings, including overlapping ones


code:
def wrapper(*args, **kwargs):
    print kwargs["uname"]
    return mainfunc(*args, **kwargs)

rawDescription: Using variable length argument lists and named parameters together


code:
import math
v = raw_input("please enter the velocity: ")
theta = raw_input("please enter the theta (i.e. degree of liftoff): ")
v, theta = float(v) , float(theta)
t = (v * math.sin(theta)) / float(9.81)
print "assuming that g = 9.81"
print "projectile motion =", t

rawDescription: How to get input from user and apply calculation in Python?

description: Get input from user and apply calculation in Python


code:
rs = ','.join(result).split(',')
idx = [i for i,j in enumerate(rs) if j.strip().startswith('Mr')][0]
final_result = (rs[idx] +',' + ','.join(rs[1:idx] + rs[idx+1:])).strip()
print final_result

rawDescription: Arranging the string of a list in a required format in python


code:
import random
print '\n'.join(sorted(['I am here', 'I like spring', 'My house has two floors', 'Sun is bright'], key=lambda *args: random.random()))

rawDescription: Printing sequence of different print commands in python


code:
# Load the configuration from somewhere.
import json
config = json.load(open("./config.json"))

class BaseBuild(object):
    pass

class Something(BaseBuild):

    def __init__(self, name, build_type):
        self.name = name

    def setup(self):
        print self.name, "something setup called"

class SomethingElse(BaseBuild):

    def __init__(self, name, build_type):
        self.name = name

    def setup(self):
        print self.name, "something else setup called"

for build_name, build_options in config['builds'].iteritems():
    cls = globals()[build_options['build_type']]
    build = cls(name=build_name, **build_options)
    build.setup()

rawDescription: In the context of a buildbot master.cfg file, how do I instantiate a class based on a string using native modules


code:
for combo in get_combos(b):
  print "Combo:"
  for branch in combo:
    print "  %r" % (branch,)

rawDescription: Python Combinatorics, part 2

description: Python Combinatorics, part 2


code:
from collections import Counter
a = ['fruit','Item#001']
b = ['fruit','Item#002']
c = ['meat','Item#003']
foods = [a,b,c]
food_groups = {}
for food in foods:
    food_groups.setdefault(food[0], []).append(food[1])

food_groups
group_counts = Counter({f: len(items) for f, items in food_groups.iteritems()})
for food, count in group_counts.most_common(2):
    print '{} ({}):'.format(food, count)
    print '  items {}\n'.format(', '.join(food_groups[food]))

rawDescription: Using counter on array for one value while keeping index of other values


code:
import xmltodict

data = """your xml here"""

data = xmltodict.parse(data)['XMLSchemaPalletLoadTechData']['TechDataParams']
for params in data:
    print dict(params)

rawDescription: parsing nested xml in python

description: Parsing nested xml in python


code:
import sys

text = sys.stdin.read()

print text

rawDescription: STDIN file read query

description: STDIN file read query


code:
span = soup.find('span', text=re.compile('Expiry Date'))
print span.next_sibling.strip()  # prints 2016

rawDescription: Using Python + BeautifulSoup to pick up text in a table on webpage


code:
time = time.strftime('%H:%M%p')
print time

Time = Entry(root, relief = RIDGE, bd = 5, width = 16, cursor = 'plus', fg = 'red', selectforeground = 'red', selectbackground = 'black')
Time.place(x = 0, y = 0)
Time.insert(0, time)

rawDescription: How to INSERT current time in an Entry Widget

description: INSERT current time in an Entry Widget


code:
def prefix(num):
    return int(str(num)[:5])

print 20000 < prefix(20000201501) < 20150

rawDescription: Python integer prefix range algorithm

description: Python integer prefix range algorithm


code:
import ast

def guess(s):
    try:
        return ast.literal_eval(s)
    except ValueError:
        return s

s = "a:1 b:2 c:x d:2.13e-5"
print dict(map(guess, x.split(':')) for x in s.split())

rawDescription: Python convert a paritcular string to dict


code:
try:
    el = soup.select('table')[0].find('th', text='Foo')
except AttributeError, TypeError:
    print('element not found')

rawDescription: Soup.select, get only first result back

description: Soup.select, get only first result back


code:
for i, s in enumerate(sequence):
    print "{}/{}".format(i,c)
    #do stuff

rawDescription: Python patten for progress count

description: Python patten for progress count


code:
a = [1, 2]
b = [100, 200]
c = [1000, 2000]

import itertools
for p in itertools.product(a, b, c):
    print p

rawDescription: Printing all combinations, python

description: Printing all combinations, python


code:
print find_command('se') # returns search
print find_command('search') # returns search
print find_command('qu') # returns None
print find_command('quaf') # returns quaff
print find_command('qu', ['search', 'quit']) # returns quit

rawDescription: Example of a Sufficient MUD-Style and working Text Parser for Python


code:
#!/usr/bin/env python
import byteplay, marshal, sys
if __name__ == '__main__':
    sys.stdin.read(8)
    c = byteplay.Code.from_code(marshal.load(sys.stdin)).code
    labels = set([ x for l in c for x in l if isinstance(x, byteplay.Label) ])
    labels = dict([(l,i) for (i,l) in enumerate(labels)])
    byteplay.Label.__repr__ = lambda self: "labels[%d]" % labels[self]
    print repr(c)

rawDescription: How to recompile the Python 2.5 bytecode to 2.7?

description: Recompile the Python 2.5 bytecode to 2.7


code:
import pickle
a=[1,2,3,4]
h=pickle.dumps(a)
print hash(h)

# or like this

from hashlib import sha512
print sha512(h).hexdigest()

c=pickle.loads(h)
assert c==a

rawDescription: Hash sum of an object in python

description: Hash sum of an object in python


code:
for line in iter(sys.stdin.readline, ''):
    print line,

rawDescription: python cat (echo) equivalent for stdin

description: Python cat (echo) equivalent for stdin


code:
import numpy as np
import theano
import theano.tensor as T
theano.config.floatX = 'float32'

mat = T.fmatrix()
sym1 = T.triu(mat) + T.triu(mat).T
diag = T.diag(T.diagonal(mat))
sym2 = sym1 - diag

f_sym1 = theano.function([mat], sym1)
f_sym2 = theano.function([mat], sym2)

m = np.arange(9).reshape(3, 3).astype(np.float32)

print m
# [[ 0.  1.  2.]
#  [ 3.  4.  5.]
#  [ 6.  7.  8.]]
print f_sym1(m)
# [[  0.   1.   2.]
#  [  1.   8.   5.]
#  [  2.   5.  16.]]
print f_sym2(m)
# [[ 0.  1.  2.]
#  [ 1.  4.  5.]
#  [ 2.  5.  8.]]

rawDescription: Initializing a symmetric Theano dmatrix from its upper triangle


code:
def OnOptionMenuChnage(omv,av, *pargs):
    print omv.get(), av.get()
    # do more. set av value based on omv value

OptionMenuVar.trace("w", lambda *pargs: OnOptionMenuChnage(OptionMenuVar,AgeVar, *pargs))

rawDescription: Tkinter - variable to trace change in other variable


code:
proc = subprocess.Popen(shlex.split(cmd))
print "the commandline is %s" % cmd

rawDescription: output the command line called by subprocess?

description: Output the command line called by subprocess


rawDescription: python list/sequence formating

description: Python list/sequence formating


code:
from datetime import timedelta

c_time_later = '2015-06-08 23:50:21'
c_time_later_converted = datetime.strptime(c_time_later,"%Y-%m-%d %H:%M:%S")
dt = c_time_later_converted - c_time_converted
print dt  # time I was away from stackoverflow

rawDescription: having issues with python time object

description: Having issues with python time object


code:
@decorator
def simple_function(x, y, z, n=m):
    print 'this is a simple_function'

rawDescription: Decorating a function with a method (with arbitrary arguments)


rawDescription: multiple replacement values in python3

description: Multiple replacement values in python3


code:
import csv

# Open csv
r = csv.reader(open('car_parking.csv'))
line_in = [k for k in r]

#look for plate
number_plate  = 'NH08OTZ'

#get current parking status
find_plate=([i for i,k in enumerate(line_in) if k[0] == number_plate]) #find plate in array
current_park_status = line_in[find_plate[0]][5]

print current_park_status

#edit array position, with new status
new_parking_status = 'IN'
line_in[find_plate[0]][5] = new_parking_status

#overwrite cv with new result
out = csv.writer(open('car_parking.csv', 'w'))
out.writerows(line_in)

rawDescription: Overwriting/changing a field on a CSV in Python


code:
import time
import sys
while True:
    print u"Message"
    sys.stdout.flush()
    time.sleep(1)

rawDescription: Node.js Python-shell: while true loop not working


code:
from bs4 import BeautifulSoup

data = """
Product Details

Hardcover: 156 pages

Publisher: Insight Editions; Har/Pstr edition (June 18, 2013)

Language: English

ISBN-10: 1608871827

ISBN-13: 978-1608871827

"""

soup = BeautifulSoup(data)

print soup.find('b', text='Hardcover:').next_sibling
print soup.find('b', text='Publisher:').next_sibling

rawDescription: How to extract text within HTML lists using beautifulsoup python

description: Extract text within HTML lists using beautifulsoup python


code:
for link in soup.find_all('a'):
    print link['href'], ''.join(link.stripped_strings), link.next_sibling

rawDescription: Splitting a BeautifulSoup into 2 Soup-Trees


code:
import html5lib

body = "
Hello World. Greetings from Mars.
"
doc = html5lib.parse(body, treebuilder="lxml")
print doc.text_content()

rawDescription: Use html5lib to convert an HTML fragment to plain text


code:
driver.get("http://www.hotleathers.com/Front-Printed-T-Shirts-C1232.aspx?s=OrderBy%20ASC&&v=all")

returnText = []
returnText = self.driver.execute_script("var iterator = document.evaluate(\"//span[@class='tableNode']/text()[preceding-sibling::br and following-sibling::br]\", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ); var arrayXpath = new Array();  var thisNode = iterator.iterateNext(); while (thisNode) {arrayXpath.push(thisNode.textContent);  thisNode = iterator.iterateNext(); }    return arrayXpath;")

for item in returnText:
    print item

rawDescription: Run text() of Xpath in selenium

description: Run text() of Xpath in selenium


code:
import lxml.etree
doc = lxml.etree.parse('t.xml')
print doc.xpath('//element[text()="A"]')[0].text
print doc.xpath('//element[text()="A"]')[0].tag

rawDescription: Find element by text with XPath in ElementTree


code:
def quit(event):
    print "you pressed control-forwardslash"
    root.quit()

root = tk.Tk()
root.bind('<Control-slash>', quit)      # forward-slash
# root.bind('<Control-backslash>', quit)  # backslash
root.mainloop()

rawDescription: How to bind Ctrl+/ in python tkinter?

description: Bind Ctrl+/ in python tkinter


code:
B_update1, B_update2, B_mean, B_sum = [0,4,10,10,15,15], [0,4,12,10,20,20], [],[]
from itertools import groupby
from operator import itemgetter
for num, grp in groupby(enumerate(B_update1), itemgetter(1)):
    tmp_list = [B_update2[idx] for idx, _ in grp]
    B_mean.append(sum(tmp_list)/len(tmp_list))
    B_sum.append(sum(tmp_list))
print B_mean, B_sum

rawDescription: How to take sum and average of indexed values in a list?

description: Take sum and average of indexed values in a list


code:
url = '''https://archive.org/details/20070519_detroit2'''
spl = url.split('/')
spl[3] = 'embed'
embed = "/".join(spl)
print embed

rawDescription: How to find specific video html tag using beautiful soup?

description: Find specific video html tag using beautiful soup


code:
from multiprocessing.pool import Pool

def calcNum(n):#some arbitrary, time-consuming calculation on a number
  print "Calcs Started on ", n
  m = n
  for i in range(5000000):
    m += i%25
    if m > n*n:
      m /= 2
  return m

if __name__ == "__main__":
  p = Pool(processes=3)

  nums = [12,25,76,38,8,2,5]
  finList = []

  result = p.map(calcNum, nums)
  p.close()
  p.join()

  print result

rawDescription: Using threading/multiprocessing in python to do multiple calculations at the same time


rawDescription: Regex: Match a malformed date

description: Regex: Match a malformed date


code:
#!/usr/bin/env python

''' pyte VTxxx terminal emulator demo

    Interpret a byte string containing text and ANSI / VTxxx control sequences

    Code adapted from the demo script in the pyte tutorial at
    http://pyte.readthedocs.org/en/latest/tutorial.html#tutorial

    Posted to http://stackoverflow.com/a/30571342/4014959

    Written by PM 2Ring 2015.06.02
'''

import pyte

#hex dump of data
#00000000  48 45 4c 4c 4f 20 54 48  49 53 20 49 53 20 54 48  |HELLO THIS IS TH|
#00000010  45 20 54 45 53 54 1b 5b  31 36 44 20 20 20 20 20  |E TEST.[16D     |
#00000020  20 20 20 20 20 20 20 20  20 20 20 1b 5b 31 36 44  |           .[16D|
#00000030  20 20                                             |  |

data = 'HELLO THIS IS THE TEST\x1b[16D                \x1b[16D  '

#Create a default sized screen that tracks changed lines
screen = pyte.DiffScreen(80, 24)
screen.dirty.clear()
stream = pyte.ByteStream()
stream.attach(screen)
stream.feed(data)

#Get index of last line containing text
last = max(screen.dirty)

#Gather lines, stripping trailing whitespace
lines = [screen.display[i].rstrip() for i in range(last + 1)]

print '\n'.join(lines)

rawDescription: How to filter only printable characters in a file on Bash (linux) or Python?

description: Filter only printable characters in a file on Bash (linux) or Python


code:
if blocked.keys() == all_odds.keys():
    print {blocked.keys()[0] : {k:v for k, v in all_odds.values()[0].iteritems() if k not in blocked.values()[0]}}

rawDescription: Removing nested dict items with dict comprehension


code: print session.query(sa.case([(Product.target == '', Product.actual)], else_=Product.target))

rawDescription: SQLAlchemy (sql) conditionnal query

description: SQLAlchemy (sql) conditionnal query


code:
print repr('test\ttest 2'.expandtabs())
# output: 'test    test 2'

print repr('test\ttest 2'.expandtabs(2))
# output: 'test  test

rawDescription: Length of tab character

description: Length of tab character


code:
import config # <-- here...
try:
  sys.exit(1)
except SystemExit:
  try:
    import config # <-- and here
  except:
    print "errr"

rawDescription: is it possible to use import after sys.exit(1)?

description: Is it possible to use import after sys.exit(1)


code:
#!/usr/bin/env python

from collections import defaultdict

my_list = [
    'Stahl ',
    'Stahl ',
    'Die ',
    '*die ',
    'Rheinhausener ',
    'Rhein=Hausener ',
    'Mittelstreckenraketen',
    'Mittel=Strecken=Rakete',
    'Mittel=strecken=Rakete',
    'Mittels=trecken=Rakete'
]

my_dict = defaultdict(list)

key = None
for word in my_list:
    if '=' in word:
        if key is None:
            print 'Error: No key found for', word
        my_dict[key].append(word)
    else:
        key = word

for key in my_dict:
    print '%r: %r' % (key, my_dict[key])

rawDescription: Python: Convert a subset of a list into a dictionary by matching specific elements


rawDescription: How to create a date range until some date inclusive with python dateutils?

description: Create a date range until some date inclusive with python dateutils


code:
response = 'Hello there. Your ID is: 6. Your name is: Number 6.'
before, matched, after = response.partition('Your ID is: ')
if after:
    print after

rawDescription: Read characters in a string only if I have found the part that I need? [Python]


code:
number = 423
if number & 1<<3:
    print 'this bit is set'

rawDescription: translating a recursive solution to find all subsets of a list

description: Translating a recursive solution to find all subsets of a list


code:
string = u"Magic Cookie® Extra"
print repr(string)
print repr('a ' + string + ' b')

rawDescription: Special character encoding is lost when string is passed to function


code:
import os

homedir = os.path.expanduser("~")
print len([
    name
    for name in os.listdir(homedir)
    if os.path.isfile(os.path.join(homedir, name))
])

rawDescription: would like to get count of files in home dir in python

description: Would like to get count of files in home dir in python


code:
def dict_ops(d1, d2, setop):
    """Apply set operation `setop` to dictionaries d1 and d2

    Note: In cases where values are present in both d1 and d2, the value from
    d1 will be used.
    """
    return {k:d1.get(k,k in d1 or d2[k]) for k in setop(set(d1), set(d2))}

print "d1 - d2:", dict_ops(d1, d2, lambda x,y: x-y)
print "d2 - d1:", dict_ops(d1, d2, lambda x,y: y-x)
import operator as op
print "intersection:", dict_ops(d1, d2, op.and_)
print "union:", dict_ops(d1, d2, op.or_)

rawDescription: How can I perform set operations on Python dictionaries?

description: Perform set operations on Python dictionaries


code:
fin,fout=os.popen4("wget --version | grep Wget")
print fout.read()

rawDescription: Parsing a stdout in Python

description: Parsing a stdout in Python


code:
#!/usr/bin/env python
import matplotlib
import matplotlib.pyplot as plt

matplotlib.use('Agg')   # This can also be set in ~/.matplotlib/matplotlibrc
for i in range(0,3):
    print 'Plotting',i
    # Reading a single column data file
    plt.plotfile('gen%s'%i,linestyle='none', marker='*')

    plt.xlabel('Population')
    plt.ylabel('Function Value')
    plt.title('Generation%s'%i)
    plt.axis([0,500,0,180])
    # This (old-style string formatting) also works, especial for Python versions <2.6:
    # filename='plot%03d.png'%i
    filename='plot{0:03d}.png'.format(i)
    print(filename)
    plt.savefig(filename)
    # plt.clf()  # clear current figure

rawDescription: Couple of matplotlib newbie doubts

description: Couple of matplotlib newbie doubts


code:
ner_output = [(u'Remaking', u'O'), (u'The', u'O'), (u'Republican', u'ORGANIZATION'), (u'Party', u'ORGANIZATION')]
chunked, pos = [], ""
for i, word_pos in enumerate(ner_output):
    word, pos = word_pos
    if pos in ['PERSON', 'ORGANIZATION', 'LOCATION'] and pos == prev_tag:
        chunked[-1]+=word_pos
    else:
        chunked.append(word_pos)
    prev_tag = pos

clean_chunked = [tuple([" ".join(wordpos[::2]), wordpos[-1]]) if len(wordpos)!=2 else wordpos for wordpos in chunked]

print clean_chunked

rawDescription: Chunking Stanford Named Entity Recognizer (NER) outputs from NLTK format


code:
now = datetime.now(tzoffset('EDT', +4*60*60)).replace(microsecond=0)
print now.isoformat()

rawDescription: Timestamp YYYY-MM-DDThh:mm:ss.sssTZD in Python


code:
import datetime

def minute_interval(start, end):
     reverse = False
     if start > end:
          start, end = end, start
          reverse = True

     delta = (end.hour - start.hour)*60 + end.minute - start.minute + (end.second - start.second)/60.0
     if reverse:
          delta = 24*60 - delta
     return delta

t1 = datetime.time(12, 10, 0) # 12:10
t2 = datetime.time(13, 13, 0) #13:13
t3 = datetime.time(23, 55, 0) #23:55
t4 = end = datetime.time(01, 10, 0) #01:10

print minute_interval(t1, t2)
print minute_interval(t3, t4)

rawDescription: python datetime.time operation

description: Python datetime.time operation


code:
from collections import defaultdict
from nltk.corpus import wordnet as wn

# Loading the Wordnet domains.
domain2synsets = defaultdict(list)
synset2domains = defaultdict(list)
for i in open('wn-domains-3.2-20070223', 'r'):
    ssid, doms = i.strip().split('\t')
    doms = doms.split()
    synset2domains[ssid] = doms
    for d in doms:
        domain2synsets[d].append(ssid)

# Gets domains given synset.
for ss in wn.all_synsets():
    ssid = str(ss.offset).zfill(8) + "-" + ss.pos()
    if synset2domains[ssid]: # not all synsets are in WordNet Domain.
        print ss, ssid, synset2domains[ssid]

# Gets synsets given domain.
for dom in sorted(domain2synsets):
    print dom, domain2synsets[dom][:3]

rawDescription: How to get domain of words using WordNet in Python?

description: Get domain of words using WordNet in Python


code:
def temp():
    return 1, "Welcome"
if temp()[1] == "Welcome":
    print 1

rawDescription: How to compare a tuple return from a function within an if statement

description: Compare a tuple return from a function within an if statement


code:
from bs4 import BeautifulSoup
import requests

r = requests.get("http://www.cbc.ca/cmlink/rss-topstories")
soup = BeautifulSoup(r.content)
print [''.join(s.findAll(text=True)) for s in soup.findAll('p')]

[u"Search teams are returning to the home of Kathy and Alvin Liknes today for another sweep of the property, close to two weeks after the couple and their grandson Nathan O'Brien were discovered missing in Calgary.", u"Israel widened its air assault against the Gaza Strip's Hamas militants on Saturday, hitting targets that included a mosque the Israeli military said was being used to conceal rockets. Meanwhile, there are reports Hamas has launched rockets at Tel Aviv.", u'The Sunni militant group ISIS, which wants to create an Islamic state spanning Iraq and Syria, has issued a recruitment video using the image and words of a dead Ontario man who had become a jihadist and joined the fighting in Syria.', u'A Hamilton-area man\u2019s dashcam may have saved him a pricey car insurance payout \u2013 and maybe even from falling victim to an insurance scam, an industry expert says.', u'Tommy Ramone, a co-founder of the seminal punk band the Ramones and the last surviving member of the original group, has died, a business associate said Saturday.', u"During high-stake police interrogations and on seemingly meaningless online dating profiles, some people find themselves lying. So, how can you tell if someone isn't telling you the truth?", u"Israeli strikes in Gaza have led to sleepless nights and anxious Palestinian children, CBC's Derek Stoffel reports from a refugee camp in Gaza City.", u'Saskatchewan Premier Brad Wall has been a vocal proponent of abolishing the Senate. With the Prime Minister now under pressure to fill vacancies in the upper chamber, Wall argues that not appointing new senators might be the way to get rid of the institution.', u"Bassist Charlie Haden, who helped change the shape of jazz more than a half-century ago as a member of Ornette Coleman's groundbreaking quartet and liberated the bass from its traditional rhythm section role, has died. He was 76.", u"Tracy Morgan has sued Wal-Mart over last month's highway crash that seriously injured him and killed a fellow comedian.", u'Buying pot is normally a subtle affair, but not for Mike Boyer, who camped out to become the first person to legally purchase marijuana in Washington state.', u"Monika Platek, CBC's lead producer for social media during the World Cup, looks at some of the standout moments so far from the 2014 World Cup", u'Our weekly round-up of remarkable photos includes scenes from Brazil, Spain, Germany, India and elsewhere around the world.', u'The European Union said on Saturday that it has extended sanctions to cover 11 leaders of the pro-Moscow rebellion in eastern Ukraine.', u'The Edmonton Public School Board has said it will tell teachers not to use an anti-abortion centre to teach part of its sexual education curriculum, after a McNally high school student filed a human rights complaint over what she was taught.']

rawDescription: Retrieve info between paragraph tags with feedparser


rawDescription: Calculating element with most occurrences in list


code: print '\t'.join([x if i != 1 else 'No_Match' for i, x in enumerate(cols)])

rawDescription: Python: Elegantly print all *remaining* elements from list in one line


code:
import xmltodict
with open('/home/jurkij/Downloads/testarticles.xml') as xml_file:
    dic_xml = xmltodict.parse(xml_file.read())
    for page in dic_xml['mediawiki']['page']:
        for rev in  page['revision']:
            if 'username' in rev['contributor'] and rev['contributor']['username'] == 'Aristophanes68':
                print rev['timestamp']
                print rev['id']

rawDescription: Extract data from XML file if arguments are of certain values


code:
while closeProgram != "q":
    try:
        numTimes = int(raw_input("Enter number of dice rolls: "))
    except ValueError:
        print("Integer values only allowed")
        continue
    for i in range(numTimes):
        print rollDice()
    closeProgram = raw_input("Press 'q' to quit or 'enter' to roll again.")

rawDescription: python dice roll simulator, issues with quit and roll again

description: Python dice roll simulator, issues with quit and roll again


code:
#!/usr/bin/python

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
import unittest

class GoogleTest(unittest.TestCase):
    def test_basic_search(self):
        # Create a new instance of the Firefox driver
        driver = webdriver.Firefox()
        driver.implicitly_wait(10)

        # go to the google home page
        driver.get("http://www.google.com")

        # find the element that's name attribute is q (the google search box)
        inputElement = driver.find_element_by_name("q")

        # type in the search
        inputElement.send_keys("Cheese!")

        # submit the form (although google automatically searches
        # now without submitting)
        inputElement.submit()

        # the page is ajaxy so the title is originally this:
        original_title = driver.title

        try:
            # we have to wait for the page to refresh, the last thing
            # that seems to be updated is the title
            WebDriverWait(driver, 10).until(lambda driver :
                                     driver.title != original_title)
            self.assertIn("cheese!", driver.title.lower())

            # You should see "cheese! - Google Search"
            print driver.title
        finally:
            driver.quit()

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

rawDescription: Python webdriver from Selenium

description: Python webdriver from Selenium


code:
import sys
import os
from multiprocessing import Pool

def mute():
    sys.stdout = open(os.devnull, 'w')

def f(x):
    print 'hello'
    return x*x

if __name__ == '__main__':
    p = Pool(10, initializer=mute)
    p.map(f, [1,2,3])
    print('Hello')

rawDescription: Supress output in multiprocessing process


code:
l = sc.parallelize([1, 2, 3])
index = sc.parallelize(range(0, l.count()))
z = index.zip(l)

rdd = sc.parallelize([['p1', 'a'], ['p2', 'b'], ['p3', 'c']])
rdd_index = index.zip(rdd)

# just in case!
assert(rdd.count() == l.count())
# perform an inner join on the index we generated above, then map it to look pretty.
new_rdd = rdd_index.join(z).map(lambda (x, y): [y[0][0], y[0][1], y[1]])
new_df = new_rdd.toDF(["product", 'name', 'new_col'])

rawDescription: PySpark: Add a column to DataFrame when column is a list


code:
for k in xrange(3) :
    b = np.zeros((3,))
    b[k] = 1
    print lu_obj.solve(b)

rawDescription: Inverting large sparse matrices with scipy


code:
from timeit import timeit

inp = """
def return_words(string):
    return list1, list2
return_words(string)
   """

for s in list_of_inputs:
    print '{}'.format(s), '->', timeit(stmt=inp,
                                       number=1000000,
                                       setup="string = '{}'".format(s))

rawDescription: Time function with arguments in python

description: Time function with arguments in python


code:
class ScreenBooking(object):

    def __init__(self):
        super(ScreenBooking, self).__init__()
        self.seats = {
            ('screen1','day4'):{'show1':100,'show2':100,'show3':100,'show4':100},
            ('screen1','day5'):{'show1':100,'show2':100,'show3':100,'show4':100},
            ('screen1','day6'):{'show1':100,'show2':100,'show3':100,'show4':100},
            ('screen1','day7'):{'show1':100,'show2':100,'show3':100,'show4':100},
            ('screen2','day1'):{'show1':100,'show2':100,'show3':100,'show4':100},
            ('screen2','day2'):{'show1':100,'show2':100,'show3':100,'show4':100},
            ('screen2','day3'):{'show1':100,'show2':100,'show3':100,'show4':100},
        }
        self.shows = ['show1', 'show2', 'show3', 'show4']

    def check_valid_details(self, screen, show, day):
        """
        Check if booking details are valid and return True/False accordingly.
        """
        if (screen, day) not in self.seats or show not in self.shows:
            return  False
        return True

    def book_seats(self, screen, show, no_of_seats, day):
        """
        Book seats after checking valid booking details and the remaining seats.
        """
        valid_details = self.check_valid_details(screen, show, day)
        if not valid_details:
            print 'Invalid booking details!'
            return
        show_total_seats = self.seats[(screen, day)][show]
        if show_total_seats > int(no_of_seats):
            show_remaining_seats = show_total_seats - int(no_of_seats)
            self.seats[(screen, day)][show] = show_remaining_seats #update the seats count
            print '%s seat(s) booked'%(no_of_seats)
        else:
            print 'Sorry, No seats available in %s. Please try other Screens'%(screen)

a1 = ScreenBooking()
a1.book_seats('screen1','show1','98','day4')
a1.book_seats('screen1','show1','10','day4')

rawDescription: how to access values of nested dictionary keys in python?

description: Access values of nested dictionary keys in python


code:
import StringIO
import csv

scsv = """1,2,3
a,b,c
d,e,f"""

f = StringIO.StringIO(scsv)
reader = csv.reader(f, delimiter=',')
for row in reader:
    print '\t'.join(row)

rawDescription: Python csv string to array

description: Python csv string to array


code:
print "Items:"
for i in ['a', 'b', 'c']: print "\t" + i

rawDescription: Python. One line for loop printer with format


code:
import csv

mas = csv.reader(open('master.csv','rb'))
upd = csv.reader(open('update.csv','rb'))

set24 = set((row[2],row[4]) for row in mas)
print set24
print

updkept = [ row for row in upd if (row[2],row[4]) not in set24]
print '\n'.join(map(str,updkept))

rawDescription: searching & comparing valuse in csv files with python

description: Searching & comparing valuse in csv files with python


code:
funclist = [lambda x,y: x+y, lambda x,y,z: x+y+z, lambda x: -x]
paramlist = [(1,2), (1,2,3), (1,)]

for index, func in enumerate(funclist):
    print func(*paramlist[index])

rawDescription: Python function pointer with different argument size


code:
t = Foo('x')
print 4*t + 3*t
print 7*t

rawDescription: Any way to have undeclared variables for algebraic expressions?


code:
def print_gaus(n):
    if n == 1:
        return
    else:
        if n % 2 == 0:
            print (n / 2),
            print (n / 2 * 3 + 1),
            print_gaus(n / 2 * 3 + 1)
        print_gaus(n - 1)

print print_gaus(30)

rawDescription: A python non finished function

description: A python non finished function


code: print parser.prog  # prints "some_other_name"

rawDescription: Is it possible to obtain the path of the script using argparse?


code:
result = {}
for row in data:
    path, item = row[0], row[1:]
    d = result
    for p in path[:-1]:
        d = d.setdefault(p, {})
    d.setdefault(path[-1], []).append(item)
print result

rawDescription: Sorting/Combining a list of lists

description: Sorting/Combining a list of lists


code:
import datetime
from pprint import pprint
today = datetime.date(2013, 07, 12)
pprint([today + datetime.timedelta(days=i) for i in range(-7 - today.weekday(), 14 - today.weekday())])

rawDescription: Build array of dates in last week, this week and next week


code:
import pandas as pd
import numpy as np

data = {
    'Datum': ['2014-12-01'] * 96 + ['2014-12-02'] * 96,
    'Uhrzeit': range(96) + range(96),
    'RZS': np.random.rand(96*2),
}

df = pd.DataFrame(data).set_index('Datum')[['Uhrzeit', 'RZS']]
df.reset_index(inplace=True) # Now this df looks like the input you described
df = pd.pivot_table(df, values='RZS', rows='Uhrzeit', cols='Datum')
print df[:10]

rawDescription: Python - get a dataframe with time as index and several dates as columns


code:
import struct
def create_pbm(size,lst):
    out = ['P4\n'+' '.join(map(str,size))+'\n'] #header
    for j in xrange(0,len(lst),size[1]):
        #single row of data
        row = lst[j:j+size[1]]
        #padded string which can be turned into a number with `int`
        s = ''.join(map(str,row))+'0000000'
        #Turn the string into a number and pack it (into unsigned int) using struct.
        vals = [struct.pack('B',int(s[i*8:(i+1)*8],2)) for i in xrange(size[0]//8+1) ]
        out.append(''.join(vals))
    return ''.join(out)

a = [1]*25 #flat black image.
print repr(create_pbm((5,5),a))

rawDescription: Create binary PBM/PGM/PPM

description: Create binary PBM/PGM/PPM


code:
A = [[381],[376], [402], [400]]
A = [inner_item for item in A for inner_item in item]
print A

rawDescription: Append a list of arrays as column to pandas Data Frame with same column indices


code:
names = ["Peter", "Max"]
#list of tuples
peter_values = [('1/15', 7), ('2/15', 3), ('4/15', 4), ('5/15', 2), ('7/15', 2)]
max_values = [('1/15', 2), ('2/15', 4), ('3/15', 4), ('5/15', 3), ('6/15', 5)]
values = [peter_values, max_values]

# dictionary, note that dictionaries aren't always iterated in the same order
people = {}

# fill dictionary with data
for i in range(0, len(names)):
    people[names[i]] = values[i]

print people

for person in people:
    print person
    for t1, t2 in people[person]:
        print "tuple value 1: %s, tuple value 2: %s" % (t1, t2)

rawDescription: Design-Help Python Variable DataStructure List/Dict/Tuple


rawDescription: How to use index to assign address and formula to arrays?

description: Use index to assign address and formula to arrays


code:
def poison(spam):
    tmp = spam.update
    def newUpdate():
        print 'this spam has been poisoned!'
        tmp()
    spam.update = newUpdate

rawDescription: How can I augment the method of a Python object?

description: Augment the method of a Python object


code:
def readFile(filepath):
    with open(filepath, "rb") as fp:
        data = fp.read()
    db = eval(data)
    if "God" in db:
        godkeys= db["God"].keys()
        print "godkeys:-", godkeys
        print "Data:", ','.join(godkeys)

filepath = '/home/infogrid/Desktop/input1.txt'
readFile(filepath)

rawDescription: read data from file with python

description: Read data from file with python


code:
h = 'c3 a1'
u = h.replace(' ','').decode('hex').decode('utf8')
u
print u

rawDescription: Convert UTF-8 as string of binary 0 and 1s to code point


code:
def main():
    fout = CleanFile ("options.log_file.log", 'w')

    sys.stdout = os.fdopen (sys.stdout.fileno(), 'w', 0)
    os.dup2 (fout.fileno(), sys.stdout.fileno());

    p = pexpect.spawn ('tclsh')
    p.expect (['%',pexpect.EOF])

    p.sendline('ls');
    p.expect (['%',pexpect.EOF])
    ls = p.before

    p.sendline('info tclversion');
    p.expect (['%',pexpect.EOF])
    tclversion = p.before

    p.sendline('info commands');
    p.expect (['%',pexpect.EOF])
    commands = p.before

    p.sendline('exit');
    p.close()

    print "Got into tclsh."
    print ls
    print tclversion
    print "Got the version\n"
    print commands
    print "Ended session"

rawDescription: Print and pexpect logging

description: Print and pexpect logging


code: print str.isalpha.__doc__

rawDescription: python if user input contains string

description: Python if user input contains string


rawDescription: Getting 2nd Next Value in For Loop Python


code:
def recursiveRef(nested,depth):
    """Return element from nested list
    list ->int
    """
    if len(depth) == 0:
        return nested
    else:
        return recursiveRef(nested[depth[0]],depth[1:])

print recursiveRef([[1,2,3],[4,[5,6],7]],[1])
print recursiveRef([[1,2,3],[4,[5,6],7]],[1,1])
print recursiveRef([[1,2,3],[4,[5,6],7]],[1,1,1])

rawDescription: Python recursion not returning value

description: Python recursion not returning value


code:
#!/usr/bin/python

import smbus
import time
bus = smbus.SMBus(1)
address = 0x2a

while True:
    data = ""
    for i in range(0, 5):
            data += chr(bus.read_byte(address));
    print data
    time.sleep(1);

rawDescription: How to read data from Arduino with Raspberry Pi with I2C

description: Read data from Arduino with Raspberry Pi with I2C


code:
from lxml import etree
from StringIO import StringIO

tree = etree.parse(StringIO('''<?xml version="1.0" encoding="iso-8859-1"?>
 <!DOCTYPE root SYSTEM "test" [ <!ENTITY tasty "eggs"> ]>
  <root>
   <a>&tasty;</a>
 </root>
'''))

docinfo = tree.docinfo
print etree.tostring(tree, xml_declaration=True, encoding=docinfo.encoding)

rawDescription: Preserving original doctype and declaration of an lxml.etree parsed xml


code:
# plotter.py
import pickle

with open('data_miner.pik', 'rb') as f:
    animals, population = pickle.load(f)

print animals, population

rawDescription: How do I store then retrieve Python-native data structures into and from a file?

description: Store then retrieve Python-native data structures into and from a file


code:
def commasplit(s):
    start = 0
    level = 0
    for i, c in enumerate(s):
        if c == '{':
            level += 1
        elif c == '}':
            level -= 1
        elif c == ',' and level == 0:
            yield s[start:i]
            start = i+1
    yield s[start:]

def split(s):
    import re
    found = False
    for m in re.finditer("(\{.*\})",s):
        found = True
        for p in commasplit(s[m.start() + 1:m.end() - 1]):
            for i in split(p):
                yield s[:m.start()] + i + s[m.end():]
    if not found:
        yield s

cs = "a,b,c,{d,e,f},g"
print list(commasplit(cs)) # -> seems to work

s = "project/{DEBIAN,usr/{bin,usr/{applications,icons,share}},computer}"
print s
for n, i in enumerate(split(s)): print n, i # ->  as well.

rawDescription: Can python act like this, in bash?

description: Can python act like this, in bash


code:
from xml.etree import ElementTree as et

raw = '''<e3r>
<moreData>false</moreData>
<data>
<CashDividend Isin="IE00BYM8JD58" AdjustmentFactor="" Adjustment_Date="" Adjustment_Factor=""/>
<CashDividend Isin="IE00BZ163G84" AdjustmentFactor="" Adjustment_Date="" Adjustment_Factor="" Amount_Status=""/>
<CashDividend Isin="IE00BZ163H91" AdjustmentFactor="" Adjustment_Date="" Adjustment_Factor=""/>
<CashDividend Isin="IE00BZ163M45 " AdjustmentFactor="" Adjustment_Date="" Adjustment_Factor="" />
</data>
</e3r>'''

root = et.fromstring(raw)
isin = "IE00BYM8JD58"
adjustment_factor = "FOO"
element = root.find(".//CashDividend[@Isin='%s']" % isin)
element.set("AdjustmentFactor", adjustment_factor)

print et.tostring(root)

rawDescription: Add value to tag XML Python

description: Add value to tag XML Python


code: print min(stuff, key = lambda (x, y, z): z)[0]

rawDescription: pick the minimum value out of a list of sublists and display another value in that sublist with the minimum

description: Pick the minimum value out of a list of sublists and display another value in that sublist with the minimum


code:
print '-#J%4Ya5@@\n'
print repr('-#J%4Ya5@@\n')

rawDescription: Python variables stored in text files not working, but should be


code:
def list_print(lst): # PEP-8 function name
    """Print the list, including sub-lists, item by item.""" # docstring
    for index, item in enumerate(lst): # use enumerate to get item and index
        print "List member {0}: ".format(index) # use str.format to create output
        print repr(item) # repr gives e.g. quotes around strings
        if isinstance(item, list):
            print "and the members are:"
            for subitem in item: # iterate directly over list
                print repr(subitem)
        print "" # blank line between items

rawDescription: List in a list recognition in python

description: List in a list recognition in python


code:
count = 0
for line in open('myfile'):
    count += 1
print count, line # it will be the last line

rawDescription: Two simple questions about python

description: Two simple questions about python


code:
def process_hand(hand):
    """do something to a hand of cards"""
    h.hit()
    print h.cards()
    h.hit()
h = Hand(cards)
process_hand(h)

rawDescription: dynamic class instantiation confusion in python

description: Dynamic class instantiation confusion in python


code:
import unicodedata
comp = unicodedata.normalize('NFC', letter_c + combining_cedilla)
decomp = unicodedata.normalize('NFD', c_with_cedilla)
print comp
print decomp

rawDescription: How do I iterate through unicode symbols, not bytes in python?

description: Iterate through unicode symbols, not bytes in python


code:
import collections

def write_roots(roots, writer):
    for x in xrange(roots):
        node = Node('root{}'.format(x))
        writer.writerow([node.id, node.name, ''])
        yield node

def write_row(parent_nodes, writer, children):
    for parent_node in parent_nodes:
        for x in xrange(children):
            node = Node('cat{}-{}'.format(parent_node.id, x), parent_node.id)
            writer.writerow([node.id, node.name, node.parent])
            yield node

roots = 1000

with open("data.csv", "wb") as f:
    writer = csv.writer(f)

    nodes = write_roots(roots, writer)

    expected_total = leaf_nodes = roots
    for i in xrange(9):
        childcount = random.randrange(1, 11)
        leaf_nodes *= childcount
        expected_total += leaf_nodes
        print 'Generating row {} with {} nodes per parent'.format(i + 1, childcount)
        nodes = write_row(nodes, writer, childcount)

    print 'Writing out {} nodes'.format(expected_total)
    # we need to loop over the last `nodes` generator to have everything written to a file:
    collections.deque(nodes, maxlen=0)  # empty generator without storing anything

rawDescription: Write an efficient python adjacency tree generation script


code:
import numpy as np

# Five bins, spaced from 0, 10
bins = np.linspace(0,10,5)

# Some random test data, from 0,10
data = np.random.random(size=20)*10
print data

# Binned data
print np.digitize(data,bins)

rawDescription: Using scipy and numpy to sort floats

description: Using scipy and numpy to sort floats


code:
a = A(name="some_name", addr="somewhere")
a.save()
a_note = Note(content="blablabla", related_to=a.id)
a_note.save()
notes_related_to_a = Note.objects.filter(related_to=a.id)
links_from_notes_to_a = {note_x.related_to.a for note_x in notes_related_to_a}
if len(links_from_notes_to_a) and links_from_notes_to_a[0] == a:
    print "It works!"

rawDescription: Django: I want to connect about one-to-many


code:
T = [[{"first":None,"second":None} for y in range(4)] for x in range(4)]
print T

rawDescription: How to create a matrix with values as dictionaries in python

description: Create a matrix with values as dictionaries in python


code: formatter = argparse.HelpFormatter(`prog`, max_help_position=40)

rawDescription: How to change the format of subparser with argparse

description: Change the format of subparser with argparse


code:
import re
p = re.compile(ur'''(?P<Alternation1>
     \$\{(?P<braced>[_a-zA-Z][a-zA-Z0-9]*(?::[_a-zA-Z]\w*)+)
     \}
     )
     |
     (?P<Alternation2>
      \$(?P<named>[_a-zA-Z][a-zA-Z0-9]*(?::[_a-zA-Z]\w*)+
     )
    )
''', re.VERBOSE)
test_str = u"asdasd $asd:sd + ${asd123:asd} $HOME $$asd"

print [x for x in re.findall(p, test_str)]

rawDescription: why does the first alternation not match?

description: Why does the first alternation not match


code:
def flatten(parent):
    L = [parent]
    for child in parent.children:
        L += flatten(child)
    return L
flattened_tree = flatten(t)
print "All members: ", [person.name for person in flattened_tree]
print "Number of members:", len(flattened_tree)
print "Number of levels:", max([person.level for person in flattened_tree]) + 1
cooper = flattened_tree[6]
cooper_fl = flatten(cooper)
print "Members below Cooper: ", [person.name for person in cooper_fl]
print "Number:", len(cooper_fl)
levels = [person.level for person in cooper_fl]
print "Number of levels:", max(levels) - min(levels) + 1

rawDescription: How to create Family tree?

description: Create Family tree