code:
import string
import hashlib
from itertools import product

CHARSET = string.ascii_lowercase +  string.digits
MAX_LENGTH = 5
NUM_PROCESSES = None # defaults to all available cores

HASHALG = "md5"
HASH = "5a105e8b9d40e1329780d62ea2265d8a"

PAIRS = ["".join(t) for t in product(CHARSET, repeat=2)]

def make_bases(count):
    bases = [PAIRS] * (count // 2)
    if count & 1:
        bases.insert(0, CHARSET)
    return bases

# string_gen is what the workers run.  Everything else
# runs in the main program.
def string_gen(prefix, suffix_len, length):
    # Generate all strings of length `length` starting with `prefix`.
    # If length > suffix_len, only the last suffix_len characters
    # need to be generated.
    if length <= suffix_len:
        assert prefix == ""
        bases = make_bases(length)
    else:
        assert len(prefix) + suffix_len == length
        bases = make_bases(suffix_len)
    for t in product(*bases):
        result = prefix + "".join(t)
        # do something with result
        if hashlib.new(HASHALG, result).hexdigest() == HASH:
            return result

def record_done(result):
    global all_done, the_secret
    print ".",
    if result is not None:
        print
        the_secret = result
        all_done = True
        pool.close()
        pool.terminate() # stop all workers! we're done

def do_work(pool, strings_per_chunk=1000000):
    global all_done, the_secret
    all_done = False
    the_secret = None
    # What's the most chars we can cycle through without
    # exceeding strings_per_chunk?
    N = len(CHARSET)
    suffix_len = 1
    while N**suffix_len <= strings_per_chunk:
        suffix_len += 1
    suffix_len -= 1
    print "workers will cycle through the last", suffix_len, "chars"

    # There's no point to splitting up very short strings.
    max_short_len = min(suffix_len, MAX_LENGTH)
    for length in range(1, max_short_len + 1):
        pool.apply_async(string_gen, args=("", suffix_len, length),
                         callback=record_done)
        if all_done:
            return
    # And now the longer strings.
    for length in range(max_short_len + 1, MAX_LENGTH + 1):
        for t in product(*make_bases(length - suffix_len)):
            prefix = "".join(t)
            pool.apply_async(string_gen, args=(prefix, suffix_len, length),
                             callback=record_done)
            if all_done:
                return

if __name__ == "__main__":
    import multiprocessing
    pool = multiprocessing.Pool(NUM_PROCESSES)
    do_work(pool)
    pool.close()
    pool.join()
    if the_secret is None:
        print "didn't crack it!"
    else:
        print "the plaintext is", repr(the_secret)

rawDescription: Python Parallel hash bruteforce

description: Python Parallel hash bruteforce


code:
x = (0,1,21)

# Assumes that the values in your tuple will always be an integer.
s = '{:d} days, {:d}:{:d}'.format(*x)

print s # 0 days, 1:21

rawDescription: Transform multiple return values to a custom string in default django admin interface


code:
from Tkinter import *

class App(object):
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()

        optionvalue = IntVar(master)
        optionvalue.set(2)
        optionvalue.trace("w", self.callbackfunc)
        self.optionmenu = OptionMenu(master, optionvalue, 1, 2, 3, 4)
        self.optionmenu.pack()
        self.state = []

    def callbackfunc(self, *args):
        self.state.append(args)
        print self.state

root = Tk()
app = App(root)
root.mainloop()

rawDescription: What are the limitations of callback functions associated with Tkinter traces?


code:
def Foo_np(seed=None):
    local_state = np.random.RandomState(seed)
    print local_state.uniform(0, 1, 5)

pool.map(Foo_np, range(20))

rawDescription: Seeding random number generators in parallel programs


code:
url = '/description/id/34/the cat sat on'
print 'quote: ', urllib.quote(url)
print 'quote_plus: ', urllib.quote_plus(url)
print 'quote_plus with safe set: ', urllib.quote_plus(url, safe='/')

rawDescription: using python quote_plus with slashes

description: Using python quote_plus with slashes


code:
class First:
    def __init__(self):
        self.text = "This is a text in First class"
        self.second = Second(self)

class Second:
    def __init__(self, first):
        self.first = first
        print "How to show self.text from First?"
        print self.first.text

rawDescription: Python: Calling a method from the importing class


rawDescription: What should I decorate with @asyncio.coroutine for async operations?


code:
@coroutine
def UndefinedCustomer():
    while 1:
        cust = (yield)
        print "No such customer type '%s'" % cust.custtype

rawDescription: Implementing Chain of responsibility pattern in python using coroutines


code:
from itertools import combinations

def get_all_combinations(input_list):
    for i in xrange(len(input_list)):
        for item in combinations(input_list, r = i + 1):
            yield list(item)

input_list = [1,4,6,8,11,13]
for item in get_all_combinations(input_list):
    print item

rawDescription: Make all possible combinations of list elements - using python


code:
def get_static_path(request, name):
    from pyramid.config.views import StaticURLInfo
    registrations = StaticURLInfo()._get_registrations(request.registry)
    if not name.endswith('/'):
        name = name + '/'
    route_name = '__%s' % name
    for _url, spec, reg_route_name in registrations:
        print ':', reg_route_name
        if reg_route_name == route_name:
            return spec

rawDescription: How to get file path of a add_static_view() in Pyramid

description: Get file path of a add_static_view() in Pyramid


code:
from BeautifulSoup import BeautifulSoup as bs

your_string = """<test name1="" name2="" name3="0.0.0.0" name4="Linux"></test>"""

soup = bs(your_string)
s = soup.test
s['name4'] = 'Ubuntu'
print soup

rawDescription: Extract value from tag python

description: Extract value from tag python


code:
for index, row in enumerate(cur):
    if index:
        output[index] = int(row)
print output

rawDescription: How to Append "Decimal" from MySQL query result in Python


code:
class LogManager(Manager):
    def get_query_set(self):
        print inspect.stack()
        return super(LogManager, self).get_query_set()

rawDescription: get call stack for Model Classes in Django

description: Get call stack for Model Classes in Django


code:
import re
str1 = "ACGCUGAGAGGACGAUGCGGACGUGCUUAGGACGUUCACACGGUGGAAGUUCACAACAAGCAGACGACUCGCUGAGGAUCCGAGAUUGCUCGCGAUCGG"
str2 = "...((.((....(((..((....(((((.((((.(((((...))))).)))).....)))))..))..))))).))((((((((....)))).)))).."
pat = re.compile(r"\([^()]+\)")
for m in pat.finditer(str2):
    print '%02d-%02d: %s' % (m.start(), m.end(), m.group())
    print str1[m.start():m.end()]

rawDescription: Searching multiple times a pattern in a string via regex in python


code:
import nwise
ll = range(10)
for tup in nwise.nwise(ll,3): print tup

rawDescription: Referencing list entries within a for loop without indexes, possible?


code:
ascii_counts=[0]*255
s="abracadabra"

for c in s:
    ascii_counts[ord(c)]+=1

for i, e in enumerate(ascii_counts):
    if e:
        print chr(i), e

rawDescription: Nested for-loops and dictionaries in finding value occurrence in string


code:
text = '''
hello world
this    is  tabbed
'''

print list(text)

rawDescription: Copying Text in Python and Preserving/Finding Special Characters


code:
def findSel(self, current):
    currentItem = self.itemWidget(current)
    pixmap = currentItem.pixmap()
    print pixmap

rawDescription: Getting ListWidget Item Text and Image in PyQt


code:
t = dt.decode("hex")
print list(t)

rawDescription: Convert time-string into individual hex bytes


code:
with open('t.csv') as fp:
  read = csv.reader(fp)
  for line in read:
    print line

rawDescription: Fix a CSV File's Format in Python

description: Fix a CSV File's Format in Python


code: print a.format(**data)

rawDescription: python's ".format" function

description: Python's ".format" function


code:
empty = {'151': {'1': 'empty', '0': 'empty', '2': '2.30'}}
full = {'151': {'1': 3.4, '0': 3.6, '2': 2}}

def repl(a, b):
    clean = {}
    for k, v in a.items():
        # This is the case where we want to replace what we have in b if we have something.  Just in case, use the dict.get method and provide a default.
        if v == 'empty':
            clean[k] = b.get(k, 'Not there')
        # If the value is another dict, then call this function with the value, and put the return as the value for our current key
        elif isinstance(v, dict):
            v_clean = repl(v, b.get(k, {}))
            clean[k] = v_clean
        # The value isn't equal to 'empty', and it isn't another dict, so just keep the current value.
        else:
            clean[k] = v
    # Finally, return the cleaned up dictionary.
    return clean

print repl(empty, full)

rawDescription: Modifying nested dictionaries

description: Modifying nested dictionaries


code:
import pandas as pd

df = pd.read_csv("trainData.csv", dtype={'col1': int, 'col2': int, 'col3': str, 'col4': str, 'col5': float, 'col6':int,
                                  'col7': int, 'col8': float, 'col9':float, 'col10': str, 'col11': str})
df = map(list, df.values)
print df

rawDescription: How to read csv

description: Read csv


code:
from sortedcontainers import SortedList

def solve_sorted_list(numbers):
    lst = SortedList()
    acc = 0.0
    for n in numbers:
        pos = lst.bisect(n)
        lst.add(n)
        acc += len(lst) - pos
    return acc / len(numbers)

print solve_sorted_list([100, 200, 150, 170, 50])
#2.2

rawDescription: Efficient way to find the average of the rank of added scores to a list. Possibly a more efficient way of sorting or a math way?


code:
list1, list2 = ["a", "b", "c", "d", "e"], ["e", "d", "c", "b", "a"]
print [index for index, (e1, e2) in enumerate(zip(list1, list2)) if e1 == e2]

rawDescription: list index comparison in python

description: List index comparison in python


code:
for rule in ptree.productions():
    print rule

rawDescription: Grammar rule extraction from parsed result


code:
import time

while True:
    print "loop"
    time.sleep(0.5)

rawDescription: Behavior of Python's time.sleep(0) under linux - Does it cause a context switch?


code:
words =['the.cat.and.hat']
html = 'the.cat.and.hat'
for w in words:
      if html.count(w)>1:
          print 'more than one match'
      elif html.count(w) == 1:
          print 'only one match found'
      else:
          print 'no match found'

rawDescription: Check if word appears twice in webpage with python


code:
from bs4 import BeautifulSoup as bs

dic = {}
data = \
"""

        Name: Pasan

        Surname:  Wijesingher

        Former/AKA Name: No Former/AKA Name

        Gender: Male

        Language Fluency: ENGLISH

"""

soup = bs(data)
# Get the text on the html through BeautifulSoup
text = soup.get_text()

# parsing the text
lines = text.splitlines()
for line in lines:
    # check if line has ':', if it doesn't, move to the next line
    if line.find(':') == -1:
        continue
    # split the string at ':'
    parts = line.split(':')

    # You can add more tests here like
    # if len(parts) != 2:
    #     continue

    # stripping whitespace
    for i in range(len(parts)):
        parts[i] = parts[i].strip()
    # adding the vaules to a dictionary
    dic[parts[0]] = parts[1]
    # printing the data after processing
    print '%16s %20s' % (parts[0],parts[1])

rawDescription: Python extracting data from HTML using split


code:
from unittest import TestCase

class SomeClass(TestCase):
    def test_stuff(self):
        print "I just did some stuff"
        self.assertFalse(True)

rawDescription: Increasing logging verbosity of class being unit-tested in Python


code:
import glob
files = glob.glob("C:\Users\User\Desktop\*.xlsx")

rawDescription: Scraping data from Excel worksheets with different sheet names


code:
for row in mapped_data:
    print template.format(*row)

rawDescription: Python tabulate, appending element to current table


code:
with open('data.txt') as f:
    d = {}
    for l in f:
        name,hours = l.split(',',1)
        if name in d:
            d[name] = d[name] +  int(sum([float(x) for x in hours.split(',')]))
        else:
            d[name] = int(sum([float(x) for x in hours.split(',')]))
for n in d:
    print '%s:%s' % (n, d[n])

rawDescription: How to load txt and sum hours?

description: Load txt and sum hours


code:
import time
t = time.clock()
for i in range(1000):
    # your code here, for example:
    [mydict[key]**exp[key] for key in mydict.keys()]
print "time", time.clock() - t

rawDescription: Fastest Way to Multiply Dictionary Values and Output to 'list type'


code:
query = collections.Counter([j.amount for j in allrecords if j.time in ('afternoon', 'night')])
print query

rawDescription: Good libraries in R or Python for analytics of network/graph data


code:
import argparse

parser = argparse.ArgumentParser(description='Tool for extracting genes')
parser.add_argument('infile', help='source file with the genes')
parser.add_argument('outfile', help='outfile file in a FASTA format')
parser.add_argument('-s', '--spliced', action='store_true', help='remove introns')

if __name__ == '__main__':
    result = parser.parse_args('-s myin myout'.split())
    print vars(result)

rawDescription: Optional Parameter Python?

description: Optional Parameter Python


code:
with open("text.txt", 'r') as file:
    for line in file:
        line = line.rstrip('\n' + '').split(':')
        # If line is just empty
        if line != ['']:
            print line

rawDescription: Removing empty elements from an array in Python


rawDescription: pymongo: how to use $or operator to an column that is an array?

description: Pymongo: how to use $or operator to an column that is an array


code:
print '{} minute{}, {} hour{}'.format(minutes, 's' if minutes != 1 else '',
                                      hours, 's' if minutes != 1 else '')

rawDescription: How to convert datetime.timedelta to minutes, hours in Python?

description: Convert datetime.timedelta to minutes, hours in Python


code:
def test1():
    text = "Foo is pretty"
    print "Inside test1()"
    def test2():
        print "Inside test2()"
        print "test2() -> ", text
    return test2

test1()() # first way, prints "Foo is pretty"

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

rawDescription: How do I call function inside another function?

description: Call function inside another function


code:
def dicts_from_file(file):
    dicts_from_file = []
    with open(file,'r') as inf:
        for line in inf:
            dicts_from_file.append(eval(line))
    return dicts_from_file

def get_setups(dicts):
    setups = []
    for dict in dicts:
        for key in dict:
            if key == "setup":
                setups.append(dict[key])
    return setups

print get_setups(dicts_from_file("data.txt"))

rawDescription: How to get values of one key from multiple dictionaries in python?

description: Get values of one key from multiple dictionaries in python


code:
def propagate_button_press_event(parent, event, *data):
    parent_alloc = parent.get_allocation()
    x = parent_alloc.x + int(event.x)
    y = parent_alloc.y + int(event.y)
    children = parent.get_children()
    print "Propagating event:%r" % event
    print "- from parent:%r" % parent
    while children:
        for child in children:
            child_alloc = child.get_allocation()
            if child_alloc.x <= x <= child_alloc.x + child_alloc.width and child_alloc.y <= y <= child_alloc.y + child_alloc.height:
                print "- to child:%r" % child
                if child.get_property('can-focus'):
                    event.send_event = True
                    child.grab_focus()
                    child.emit('button-press-event', event, *data)
                    return True
                else:
                    children = child.get_children() if hasattr(child, 'get_children') and callable(child.get_children) else None
                    break;
        else:
            children = None
    return False

rawDescription: PyGTK Entry widget in TreeViewColumn header


code:
class TestSomething(object):

    def setup_method(self, method):
        print "\n%s:%s" % (type(self).__name__, method.__name__)

    def teardown_method(self, method):
        pass

    def test_the_power(self):
        assert "foo" != "bar"

    def test_something_else(self):
        assert True

rawDescription: py.test: how to get the current test's name from the setup method?

description: Py.test: how to get the current test's name from the setup method


code:
class MyString(str):
    pass

my_str = MyString()
if type(my_str) == 'str':
    print 'I hope this prints'
else:
    print 'cannot check subclasses'
if isinstance(my_str, str):
    print 'definitely prints'

rawDescription: Difference between isinstance and type in python


rawDescription: SQLite: Select all date-value-pairs where the distance between each date and the first date is a multiple of a specific period


code: print "Hello {foo:s} and {bar:s}".format(foo="World", bar="Foobar!")

rawDescription: Replacing section of python string with a designated variable


code:
import re
matches_up_to_4_words = '( [^ ]*){0,4} ?'
matches_context_word = '(' + '|'.join(context_words) + ')'
matches_target_word = target_word
context_before = matches_context_word + matches_up_to_4_words + matches_target_word
context_after = matches_target_word + matches_up_to_4_words + matches_context_word
pattern = re.compile('(' + context_before + '|' + context_after + ')')

matching_metadata = []
for filename in filenames:
    filestring = open(filename, 'rb').read()
    ## you can tokenize here for better word segmentation
    ## http://www.nltk.org/api/nltk.tokenize.html
    if re.search(pattern, filestring):
        print "the target word appeared near a context word"
        ## get the metadata
        metadata = get_the_metadata(filename, filestring)
        matching_metadata.append(metadata)

rawDescription: NLTK Find occurrences of a word within 5 words (left/right) of context words in a corpus


code:
def writeTowns(text):
    input = open(text, 'r')
    file = input.readlines()
    dict = {}
    for line in file:
        tmp = line.split()
        dict[tmp[1]]  = dict.get(tmp[1]) or []
        dict.get(tmp[1]).append(tmp[0])
    return dict

writeTown = writeTowns('file.txt')
print writeTown['Anzegem']

rawDescription: Python: dictionary to collection

description: Python: dictionary to collection


code:
counter = tf.Variable(0)
counter_val = tf.identity(counter)  # Take alias before the `assign_add` happens.
counter_update = counter.assign_add(1)

with tf.control_dependencies([counter_update]):
  # Force a copy after the `assign_add` happens.
  result = counter_val + 0

sess = tf.Session()
sess.run(tf.initialize_all_variables())

print sess.run(result)  # ==> 1  (result has effect of `assign_add`)

rawDescription: How to dereference _ref tensor type in TensorFlow?

description: Dereference _ref tensor type in TensorFlow


code:
class Demo(object):

    def instance_method(self):
        print "Called an instance method"

    @classmethod
    def class_method(cls):
        print "Called a class method"

    @staticmethod
    def static_method():
        print "Called a static method"

rawDescription: Is it possible to import class method without instantiating class?


code:
class Test:
    var = 3
    def __init__(self):
        pass

a = Test()
print "a " +  str(a.var)
a.var = 2   # Making var a member of the instance a
Test.var = 5
b = Test()
print "b " + str(b.var)
print "a " + str(a.var)

rawDescription: Create Shared Member Variable between instances (static)


code:
from ctypes import *
class TestStruct(Structure):
 _fields_ = [("voidField", c_void_p)]
x.voidField = 0
print repr(x.voidField)

rawDescription: Python Ctypes converts void pointer to nonetype and long. What are the implications? What does this mean?


code:
for tag in soup.find_all( "a", { "class" : "gb1" } ):
    print tag["href"]

rawDescription: Using Beautifulsoup and Urllib2 in Python, how can I find the data surrounded by specific tags?


code:
from collections import Counter,defaultdict

d = [{'condition': 'functional', 'location':'Sydney' }, {'condition':'functional', 'location': 'Adelaide'}, {'condition':'broken', 'location':'Sydney'}]

cities = [j['location'] for j in d]

#initialize data
data = defaultdict(float)
for city in cities:
    data[city]=0
#Count occurrances of a single city as a counter dictionary
counters = Counter((i['location'] for i in d))

#Do the calculation
for i in d:
    if i['condition']== 'functional':
        inc = (counters[i['location']]*100)/len(d)
        data[i['location']]+= float(inc)
    elif i['condition']== 'broken':
        dec = (counters[i['location']]*100)/len(d)
        data[i['location']]-=float(dec)
    else:
        raise Exception("Error")

print {k:"{0}%".format(v) for k,v in data.items()}

rawDescription: Comparing the elements in two JSON dicts and getting the difference as a ratio or percentage


code:
from mako.lookup import TemplateLookup
import haml

class Lookup(TemplateLookup):
    def get_template(self, uri):
        if uri.rsplit('.')[1] == 'haml':
            # change preprocessor used for this template
            default = self.template_args['preprocessor']
            self.template_args['preprocessor'] = haml.preprocessor
            template = super(Lookup, self).get_template(uri)
            # change it back
            self.template_args['preprocessor'] = default
        else:
            template = super(Lookup, self).get_template(uri)
        return template

lookup = Lookup(['.'])
print lookup.get_template('index.haml').render()

rawDescription: Choose Mako preprocessor based on file extension?


code:
from itertools import islice
from collections import deque
from bisect import bisect_left,insort

def median(s):
    sp = [nz for nz in s if nz!=0]
    print sp
    Mnow = len(sp)
    mnow = Mnow // 2
    return sp[mnow] if bool(Mnow&1) else (sp[mnow-1]+sp[mnow])*0.5

def RunningMedian(seq, M):
    seq = iter(seq)
    s = []
    m = M // 2

    # Set up list s (to be sorted) and load deque with first window of seq
    s = [item for item in islice(seq,M)]
    d = deque(s)
    ## Simple lambda function to handle even/odd window sizes
    #median = lambda : s[m] if bool(M&1) else (s[m-1]+s[m])*0.5

    # Sort it in increasing order and extract the median ("center" of the sorted window)
    s.sort()
    medians = [median(s)]
    for item in seq:
        old = d.popleft()          # pop oldest from left
        d.append(item)             # push newest in from right
        del s[bisect_left(s, old)] # locate insertion point and then remove old
        insort(s, item)            # insert newest such that new sort is not required
        medians.append(median(s))
    return medians

rawDescription: running median excluding zeros

description: Running median excluding zeros


code:
r = res.getheaders()
redirected = False
for e in r:
    if(e[0] == 'location'):
        redirected = e
if(redirected != False):
    print redirected[1]

rawDescription: Django: check if an image exists at some particular url


code:
text = 'tekst'

with open('1.feature', 'r') as feat:
    tabula = feat.readlines()

current_scenario = -1
for i in range(len(tabula)):
    if 'Scenario' in tabula[i]:
        current_scenario = i+1
    elif tabula[i].strip('\n') == text and current_scenario != -1:
        print '1.feature:', current_scenario
        current_scenario = -1

rawDescription: Get number of line recursively

description: Get number of line recursively


code:
#!/usr/bin/env python
import xml.etree.cElementTree as etree

def getelements(source, tag):
    context = iter(etree.iterparse(source, events=('start', 'end')))
    _, root = next(context) # get root element
    for event, elem in context:
        if event == 'end' and elem.tag == tag:
            yield elem
            root.clear() # free memory

for elem in getelements('big.xml', 'person'):
    print '^'.join(elem.find(tag).text for tag in 'name age address'.split())

rawDescription: Converting xml data using Python

description: Converting xml data using Python


code:
a = [['hi', 'monday'], [], ['hello', 'indent', 'joe'], ['hi', 'low']]
a.sort(key=lambda l:(-len(l), l))
print a

rawDescription: Sorting a group of words inside a list

description: Sorting a group of words inside a list


code:
class X(object):
    def __init__(self, flag):
        if flag:
            self.method = self._method

    def _method(self):
        print "I'm a method!"

rawDescription: How to access "self" inside the scope of a class?

description: Access "self" inside the scope of a class


code:
from StringIO import StringIO
foo = StringIO()  # A fake file to write to
df.to_csv(foo)  # Output the df to a CSV file
foo.seek(0)  # And rewind the file to the beginning
print ''.join(foo.readlines()[:3])

rawDescription: Parsing through a deep-nested XML File in Python


code:
import json
test = {}
for i in range(10):
    nested = test[str(i)] = {}
    for j in range(15):
        nested[str(j)] = i * j

print json.dumps(test, sort_keys=True, indent=4, separators=(',', ': '))

rawDescription: Error in writing Python dictionary to a file


code:
from random import randint, seed
seed(345)

def bizarre(total, slots):
    tot = total
    acc = []
    for _ in range(slots-1):
        r = randint(0,tot)
        tot -= r
        acc.append(r)
    acc.append(total-sum(acc))
    return acc

# testing code

for i in range(10):
    tot = randint(50,80)
    n   = randint(5,10)
    b = bizarre(tot, n)
    print "%3d %3d  %s -> %d" % (tot, n, b, sum(b))

rawDescription: Python Randomly assign a list from a set number


code:
import win32api
import win32net

ip = '192.168.1.18'
username = 'ram'
password = 'ram@123'

try:
    use_dict={}
    use_dict['remote']=unicode('\\\\192.168.1.18\C$')
    use_dict['password']=unicode(password)
    use_dict['username']=unicode(username)
    win32net.NetUseAdd(None, 2, use_dict)
except:
    print  "connection not established"

rawDescription: Read all system's drive data in network using python


code:
html = driver.page_source
soup = BeautifulSoup(html)

# Get *all* 'p' tags with the specified class attribute.
p_tags = soup.findAll('p',{'class':'market_value'})
for p in p_tags:
    print p.text

rawDescription: selenium webdriver in python

description: Selenium webdriver in python


code:
from itertools import takewhile
import operator

def highest_divisors(a, b):
    _divisors = sorted({var: len([x for x in range(a, b + 1) if not var % x])
                        for var in range(a, b + 1)}.iteritems(),
                       key=operator.itemgetter(1))[::-1]
    _max = _divisors[0][1]
    return [n for n, v in takewhile(lambda y: y[1] == _max, _divisors)]

if __name__ == '__main__':
    for var in highest_divisors(2, 10):
        print var

rawDescription: Find the highest number of divisors between range in python?


code:
leaked_things = [[x] for x in range(10)]
for i in get_objects():
    after[type(i)] += 1

print [(k, after[k] - before[k]) for k in after if after[k] - before[k]]

rawDescription: Python - Working around memory leaks

description: Python - Working around memory leaks


code:
def start():
    pid1 = os.fork()
    if pid1 == 0:
        os.setsid()
        x = 30
        while x > 0:
            sleep(1)
            with open('/tmp/sleep.log', 'a') as fd:
                pid = str(os.getpid())
                print "child pid = ", pid
                fd.write(pid  + ' PID \n')
            x -= 1
        os._exit(0)
    else:
        signal.signal(signal.SIGCHLD, signal.SIG_IGN)
        print os.getpid()
        while True:
            pass
start()

rawDescription: How fork process correctly under the Flask?


code:
# re like: r'.*?(\w+)(?<=.{30})'
m = re.match(r'.*?(\w+)(?<=.{%d})' % (offset), my_string)
if m: print m.group(1)
else: print "no match"

rawDescription: Can you search backwards from an offset using a Python regular expression?

description: Search backwards from an offset using a Python regular expression


code:
response = {
  "result": [
    {
      "attribute0": [
        "attrbVal0"
      ],
      "attribute1": [],
      "attribute2": "attrbVal1",
      "attribute3": [
        "val1",
        "val2"
      ],
      "creator": "abc",
      "key": [
        "val2",
        "val3"
      ]
    }
  ]
}
for result in response['result']:
    for attrname, value in result.items():
        if not value:  # empty list or None
            print attrname

rawDescription: Parsing a json (a response from freebase) in python


code:
from collections import OrderedDict

my_dict = {
100: {0.5: 1, 0.6: 0         },
200: {0.5: 1, 0.6: 1         },
300: {0.5: 5,         0.7: 6}}

# header columns
cols = set()
for d in my_dict.values():
    cols.update(d.keys())
cols = list(sorted(cols))
print "\t", "\t".join([str(c) for c in cols])

# data rows
for row, d_cols in sorted(my_dict.items()):
    e_cols = OrderedDict().fromkeys(cols)
    e_cols.update(d_cols)
    print row, "\t", "\t".join([str(v) for v in e_cols.values()])

rawDescription: write a dictionary of dictionaries (with unknown size) as a matrix

description: Write a dictionary of dictionaries (with unknown size) as a matrix


code:
for category in categories:
    function = locals()['Get' + category]

    print 'v{} = {}'.format(category, function())

rawDescription: Call functions from string concatenations in for loop [Python]


code:
import numpy as np

line = "#!/usr/bin/ruby\n#\n#  Gen"
array = np.fromstring(line, dtype=float)
print array

rawDescription: Ruby NArray.to_na() and Python numpy.array()


code:
import datetime as dt

t_start = dt.time(19,0,0)
t_end = dt.time(19,0,3)
with open('search.txt') as f:
    for line in f:
        fields = [ x.strip() for x in line.split(',') ]
        timestamp = dt.datetime.strptime(fields[0], "%H:%M:%S").time()

        if t_start < timestamp < t_end:  # use "<=" if you want to search with boundaries included
            print fields[1],

rawDescription: getting the data between some time interval

description: Getting the data between some time interval


code:
def reverseString(aStr, newStr= ''): #define a default value for newStr

    if len(aStr) == 0:
        return newStr
    else:
        newStr = newStr + aStr[len(aStr)-1]  #better use aStr[-1]
        return reverseString(aStr[:len(aStr)-1], newStr) #pass the new value of newStr

print reverseString("foobar")# No value is passed for newStr. So, default is used .

rawDescription: Writing a string backwards

description: Writing a string backwards


code:
#!/usr/bin/python

import time

def createDictionary():
    startTime = time.clock()
    dict = {'Dog': 'der Hund', 'Cat': 'die Katze', 'Bird': 'der Vogel'}
    dict[ 'Spider' ] = 'die Spinne'
    key = 'blabla'
    if key in dict:
        print dict[key]
    else:
        print "not vaild"
    endTime = time.clock()
    print "Time taken for programe (in seconds): ", endTime - startTime

createDictionary()

rawDescription: python script not working

description: Python script not working


code: print np.hstack((np.vstack((a, x[:-1])), x[:, None]))

rawDescription: Concatenating numpy arrays vertically and horizontally


code:
manual_dict = {}
with open('foo.data', 'r') as foo:
    lines = foo.readlines()

for idx,l in enumerate(lines):
    word = l.split(" ")[0]
    if not word in manual_dict:
        manual_dict[word] = {'count' : 0, 'pos' : 0}
    manual_dict[word]['count'] +=1
    if not manual_dict[word]['pos']:
        manual_dict[word]['pos'] = idx

for w,w_config in sorted(manual_dict.items(), key=lambda x: x[1]['pos']):
  print w, w_config['count']

rawDescription: How to count a set of strings in a file in Python?

description: Count a set of strings in a file in Python


code:
import nltk
grammar = nltk.parse_cfg("""
NP -> Det N | Det N PP
N -> 'Kim' | 'Dana' | 'everyone'
""")
sent = "Kim".split()
parser = nltk.ChartParser(grammar)
print parser.nbest_parse(sent)

rawDescription: Python and NLTK: How to analyze sentence grammar?


code:
# ...
hand = []
for _ in range(NUM_CARDS):
    while True:
        cv = random.randint(2,14)#value
        cs = random.randint(0,3)#suit
        if (cv, cs) not in hand:
            break
    hand.append((cv, cs))
    cards[0].append('_' * (CARD_WIDTH+2))
    cards[1].append(card_row(''))
    cards[2].append(card_row(get_card_name(cv)))
    cards[3].append(card_row('of'))
    cards[4].append(card_row(SUITS[cs][0]))
    cards[5].append(card_row(SUITS[cs][1]*(CARD_WIDTH)))
    cards[6].append(card_row("_" * CARD_WIDTH))
print 'Welcome to SANDCRAB. This is your deck of cards.'
for card in hand:
    print card
for lst in cards:
    print ''.join(lst)

rawDescription: How to remove duplicates in card game- python

description: Remove duplicates in card game- python


code:
data = """\
0010GEORGE JETSON    12345 SPACESHIP ST   HOUSTON       TX
0020WILE E COYOTE    312 ACME BLVD        TUCSON        AZ
0030FRED FLINTSTONE  246 GRANITE LANE     BEDROCK       CA
0040JONNY QUEST      31416 SCIENCE AVE    PALO ALTO     CA""".splitlines()

fieldslices = [slice(*fielddef) for fielddef in [
    (0,4), (4, 21), (21,42), (42,56), (56,58),
    ]]
fields = "id name address city state".split()

for rec in data:
    for field,sl in zip(fields, fieldslices):
        print field, ':', rec[sl]
    print

rawDescription: Python slice how-to, I know the Python slice but how can I use built-in slice object for it?


code:
f = open('file.txt', 'r')
for line in f.readlines():
    id = line.strip('\n')
    url = "https://example.com/{0}".format(id)
    urlobj = urllib.urlopen(url)
    try:
        json_data = json.loads(urlobj)
        print json_data
    except:
        print urlobj.readlines()

rawDescription: Passing a variable in url?

description: Passing a variable in url


code:
from dateutil import rrule
from datetime import datetime

a = '20120525'
b = '20120627'

for dt in rrule.rrule(rrule.DAILY,
                      dtstart=datetime.strptime(a, '%Y%m%d'),
                      until=datetime.strptime(b, '%Y%m%d')):
    print dt.strftime('%Y%m%d')

rawDescription: How to get all dates (month, day and year) between two dates in python?

description: Get all dates (month, day and year) between two dates in python


code:
pal_count = {
    x: word_list.count(x)  # reduce-ish
    for x in word_list     # map-ish
    if x == x[::-1]        # filter-ish
    }
for x, y in pal_count.iteritems():
    print x, y             # print the results!

rawDescription: Map reduce problem in python

description: Map reduce problem in python


code:
substring="test\\1234\\n"
filename="temp.txt"
file = open(filename, "r")

for line in file:
    if substring in line:
        print line

rawDescription: How to Search in file with sub-string contain backslash and \n


code:
import threading

class MyScript(threading.Thread):
    def __init__(self):
        super(MyScript, self).__init__()
        self.__stop = threading.Event()

    def stop(self):
        self.__stop.set()

    def stopped(self):
        return self.__stop.isSet()

    def run(self):
        while not self.stopped():
            # Put your script execution here
            print "running"

rawDescription: Managing script execution using GUI "buttons" from Tkinter module


code:
import api
def Run():
    api.MOTD="HELLO WORLD!"
    print "This is a plugin running!"

rawDescription: Python Plugin Capbility

description: Python Plugin Capbility


code:
import json
lst = [("name", "value"), ("name2", "value2")]
rs = json.dumps(dict(lst))
print rs

rawDescription: more pythonic way to format a JSON string from a list of tuples

description: More pythonic way to format a JSON string from a list of tuples


code:
s = '644'
i = int(s, 8) # 420 decimal
print i == 0644 # True

rawDescription: Most pythonic way to convert a string to a octal number


code:
class A(tuple):
    def speak(self):
        print "Bark!"

a = A((1,2,3)) # extra parens needed to distinguish single tuple arg from 3 scalar args
a * 3
a.speak()

rawDescription: Python: can a class instance evaluate to any type?


code:
vowels = "aeiou"
consonants = "bcdfghjklmnpqrstvwyz"

def pintoword(n):
    a = []
    while n:
        n, r = divmod(n, 100)
        c, v = divmod(r, 5)
        a.append(vowels[v])
        a.append(consonants[c])
    return ''.join(reversed(a))

for n in (3463470, 3464140):
    print n, pintoword(n)

rawDescription: converting numbers to consonant-vowel pairs

description: Converting numbers to consonant-vowel pairs


code:
print overlap_split_list(l,s)
print [i for i in generator_overlap_split_list(l,s)]
print list(generator_overlap_split_list(l,s))

rawDescription: split a list in python

description: Split a list in python


code:
print r
print r2

rawDescription: Pandas rolling sum, variating length

description: Pandas rolling sum, variating length


code:
from sys import version_info;
print version_info >= (2, 2) and version_info < (3, 0)

rawDescription: Custom Python shell for Emacs python-mode


code:
years = r'((?:19|20)\d\d)'
pattern = r'(%s) +(%s), *%s' % years

thirties = pattern % (
     "September|April|June|November",
     r'0?[1-9]|[12]\d|30')

thirtyones = pattern % (
     "January|March|May|July|August|October|December",
     r'0?[1-9]|[12]\d|3[01]')

fours = '(?:%s)' % '|'.join('%02d' % x for x in range(4, 100, 4))

feb = r'(February) +(?:%s|%s)' % (
     r'(?:(0?[1-9]|1\d|2[0-8]), *%s' % years, # 1-28 any year
     r'(?:(29), *((?:(?:19|20)%s)|2000))' % fours)  # 29 leap years only

result = '|'.join('(?:%s)' % x for x in (thirties, thirtyones, feb))
r = re.compile(result)
print result

rawDescription: Matching dates with regular expressions in Python?


code:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from scipy.interpolate import griddata

def randrange(n, vmin, vmax):
    return (vmax - vmin)*np.random.rand(n) + vmin

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
n = 100
for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
    xs = randrange(n, 23, 32)
    ys = randrange(n, 0, 100)
    zs = randrange(n, zl, zh)
    ax.scatter(xs, ys, zs, c=c, marker=m)

# define grid
xi = np.linspace(np.min(xs),np.max(xs),n)
yi = np.linspace(np.min(ys),np.max(ys),n)
zi = griddata((xs, ys), zs, (xi[None,:], yi[:,None]), method='cubic')

# set desire levels and plot contour
levels = [(np.min(zs)+np.max(zs))/2.]
cs = plt.contour(xi,yi,zi,linewidths=1.25,colors='k', levels=levels)

# get x,y of contour lines' segments and calc contour lines length
print "x, y of contour lines ", levels
length = 0
x0,y0 =  cs.allsegs[0][0][0]
startx = x0
starty = y0
print x0, y0
for coords in cs.allsegs[0][0][1:]:
    x1,y1 =  coords[0], coords[1]
    length += np.sqrt((x1-x0)**2 + (y1-y0)**2)
    x0,y0 = x1,y1
    print x0,y0

length += np.sqrt((startx-x0)**2 + (starty-y0)**2)
print "length: ", length

plt.show()

rawDescription: Python matplotlib plot3d contour length

description: Python matplotlib plot3d contour length


code:
from ctypes import *
my_dll = WinDLL(r'full/path/to/TestDLL.dll')
my_dll.Script_GetFindedList.restype = None
size = c_uint()
my_dll.Script_GetFindedList(None, byref(size))
result = (c_uint*(size.value//4))()
my_dll.Script_GetFindedList(result, byref(size))
result = list(result)
print result

rawDescription: How do I access wity python ctypes to array values returned with a pointer from a dll coded in Delphi?

description: Access wity python ctypes to array values returned with a pointer from a dll coded in Delphi


code:
def nestList(f,x,c):
    for i in range(c):
        yield x
        x = f(x)
    yield x

import math
print list(nestList(math.cos, 1.0, 10))

rawDescription: how to write a function like Mma's NestList in Python

description: Write a function like Mma's NestList in Python


code:
class Dummy(object):

    @classmethod
    def some_function(cls,*args,**kwargs):
        print cls

#both of these will have exactly the same effect
Dummy.some_function()
Dummy().some_function()

rawDescription: When should I use @classmethod and when def method(self)?


rawDescription: How to create categorical variable based on a numerical variable

description: Create categorical variable based on a numerical variable


code:
import functools, sys
namespace = sys._getframe(0).f_globals

def myfunction(label, request):
    print request
    return "app/data/mydata_%s.csv" % (label)

my_labels = ['a','b','c']
for label in my_labels:
    namespace['myfunction_%s'%label] = functools.partial(myfunction, label)

print myfunction_a('request1')
print myfunction_b('request2')

rawDescription: how to loop list to pass string to function name in python

description: Loop list to pass string to function name in python


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

rawDescription: Flattening a very nested loop

description: Flattening a very nested loop


code:
from itertools import groupby
from operator import itemgetter
state_key = itemgetter('State')
for state, cities in groupby(sorted(rows, key=state_key), state_key):
    print state
    city_key = itemgetter('City')
    for city, group in groupby(sorted(cities, key=city_key), city_key):
        print '\t', city
        for row in group:
            print '\t\t', row['Issue'], row['Date']

rawDescription: Creating a data structure in python for use in a Jinja2 template


code:
week=[{'item': 'Electrelane', 'weight': 140}, {'item': 'Kraftwerk', 'weight': 117},{'item': 'The Flaming Lips', 'weight': 113}]

print "<?xml version='1.0' ?>"
print "<week>"
for day in week:
    print "  <day>"
    for key, value in day.items():
        print "    <%s>%s</%s>" % (key, value, key)
    print "  </day>"
print "</week>"

rawDescription: Extract resulted list data to a xml file in python


code:
p = regex.compile(r'(?r)\w+(?:-\w+)*\M', regex.U)
for line in s.split('\n'):
    print p.search(line).group(0)

rawDescription: Unicode Regex with regex not working in Python


code:
obj = obj["NewDataSet"]
root_elements = obj["Root"] if type(obj) == OrderedDict else [obj["Root"]]
# Above step ensures that root_elements is always a list
for element in root_elements:
    print element["Phonetic"]

rawDescription: Parsing an xml file with an ordered dictionary


code:
# selfimport.py
import selfimport

def foo():
    print selfimport.foo

bar = 3

if __name__ == '__main__':
    print selfimport.bar
    foo()

rawDescription: python: can i import the module namespace within the module itself?

description: Python: can i import the module namespace within the module itself


code:
for group in getMatches(people, "age"):
    print [people.name for people in group]

rawDescription: Python get objects in list with matching attributes


code:
import re

text= '5-yr online store  Real pictures Mirror Quality 1:1 Whatsapp/Viber +861776345378 spikydudewonderland@gmail.com ✈✈Worldwide Shipping'
pattern= r'(?i)([\w.]+@[\w.]+)|(?:(?:\b|[,/]\s*)(?:whatsapp|viber|wechat))+\b\s*[:：]?\s*(\+?\d+)|\bline(?:\sid)?\s*(?:[:：]\s*)?(@\w+)'

for mobj in re.finditer(pattern, text):
    if mobj.group(1):
        print 'email:', mobj.group(1)
    elif mobj.group(2):
        t= mobj.group().lower()
        if 'whatsapp' in t:
            print 'whatsapp:', mobj.group(2)
        if 'viber' in t:
            print 'viber:', mobj.group(2)
        if 'wechat' in t:
            print 'wechat:', mobj.group(2)
    elif mobj.group(3):
        print 'line:', mobj.group(3)

rawDescription: How to get the the required data from a string using Regexp or any string manipulation method

description: Get the the required data from a string using Regexp or any string manipulation method


code:
import datetime

import yaml

time_format = '%H:%M:%S'

def time_representer(dumper, data):
    return dumper.represent_scalar(u'!time', data.strftime(time_format))

def time_constructor(loader, node):
    value = loader.construct_scalar(node)
    return datetime.datetime.strptime(value, time_format).time()

yaml.add_representer(datetime.time, time_representer)
yaml.add_constructor(u'!time', time_constructor)

class A(yaml.YAMLObject):
    yaml_tag = u'!A'

    def __init__(self, val):
        self.val = val

if __name__ == '__main__':
    t = datetime.time()
    a = A(t)
    print yaml.dump(a, default_flow_style=False)
    print yaml.load(yaml.dump(a)).val == t

rawDescription: Formatting custom class output in PyYAML


code:
mylist = [1,2,3]
for item in mylist:
    print item

mydict  = {1:'one', 2:'two', 3:'three'}
for key in mydict:
    print key, mydict[key]

rawDescription: for loop in Python

description: For loop in Python


code:
class Test(object):
    def __init__(self, value):
        self.value = value
    def __new__(cls, value):
        if value > 1:
            return object.__new__(cls)
        return None
    def __repr__(self):
        return "Test value %d" % self.value

t1 = Test(2)
print repr(t1)
t2 = Test(1)
print repr(t2)

rawDescription: return something other than the object on creation of object

description: Return something other than the object on creation of object


rawDescription: Parse date/time from a string

description: Parse date/time from a string


code:
# print 5 lines of context, 3rd line will be the 'current' line.
print stack(5)[1][-2]

rawDescription: Python : Name of the calling file in different file


rawDescription: Using owl:Class prefix with rdflib and xml serialization


rawDescription: Regular expression not capturing all the elements


code:
def save(self, filepath):
    with open(filepath, 'w') as outfile:
        for k,v in self.bok.iteritems():
            outfile.write("{}:{}\n".format(v,k))

def load(self, filepath):
    if not os.path.isfile(filepath):
        print "No file was found"
        return
    with open(filepath) as infile:
        for line in infile:
            v,k = line.strip().split(":")
            self.bok[k] = v

rawDescription: Python 2.7.8 - Dictionary to file and back with style


code:
from google.appengine.ext import ndb
import json

class ExtendedJsonProperty(ndb.BlobProperty):
  def _to_base_type(self, value):
    return json.dumps(value)

  def _from_base_type(self, value):
    return value
    # originally return json.loads(value)

class DataForBrowser(ndb.Model):
    json = ExtendedJsonProperty()

data = {'a': 'A'}
data_for_browser = DataForBrowser()
data_for_browser.json = data
print type(data_for_browser.json)  # returns <type 'dict'>
data_for_browser.put()
print type(data_for_browser.json) # returns <type 'str'>
data_for_browser_retrieverd = DataForBrowser.query().get()
print type(data_for_browser_retrieverd.json) # returns <type 'str'>

rawDescription: App Engine return JSON from JsonProperty


code:
# __main__.py
import pkg_resources
from PIL import Image

print pkg_resources.resource_string(__name__, 'README.txt')

im = Image.open(pkg_resources.resource_stream('app', 'im.png'))
im.rotate(45).show()

rawDescription: python: can executable zip files include data files?

description: Python: can executable zip files include data files


code:
#!/usr/bin/python

import pygraph.classes.graph
import pygraph.algorithms
import pygraph.algorithms.minmax
import time
import random
import sys

if len(sys.argv) != 3:
    print ('usage %s: node_exponent edge_exponent' % sys.argv[0])
    sys.exit(1)

nnodes = 10**int(sys.argv[1])
nedges = 10**int(sys.argv[2])

start_time = time.clock()
def timestamp(s):
    t = time.gmtime(time.clock() - start_time)
    print 'biggraph', s.ljust(24), time.strftime('%H:%M:%S', t)

timestamp('generate %d nodes' % nnodes)
bg = pygraph.classes.graph.graph()
bg.add_nodes(xrange(nnodes))

timestamp('generate %d edges' % nedges)
edges = set()
while len(edges) < nedges:
    left, right = random.randrange(nnodes), random.randrange(nnodes)
    if left == right:
        continue
    elif left > right:
        left, right = right, left
    edges.add((left, right))

timestamp('add edges')
for edge in edges:
    bg.add_edge(edge)

timestamp("Dijkstra")
target = 0
span, dist = pygraph.algorithms.minmax.shortest_path(bg, target)
timestamp('shortest_path done')

# the paths from any node to target is in dict span, let's
# pick any arbitrary node (the last one) and walk to the
# target from there, the associated distance will decrease
# monotonically
lastnode = nnodes - 1
path = []
while lastnode != target:
    nextnode = span[lastnode]
    print 'step:', nextnode, dist[lastnode]
    assert nextnode in bg.neighbors(lastnode)
    path.append(lastnode)
    lastnode = nextnode
path.append(target)
timestamp('walk done')
print 'path:', path

rawDescription: Efficiently finding the shortest path in large graphs


code:
import sys
def get_caller(ext=False):
    """ Get the caller of the caller of this function. If the optional ext parameter is given, returns the line's text as well. """
    f=sys._getframe(2)
    s=(f.f_code.co_filename, f.f_lineno)
    del f
    if ext:
    	import linecache
    	s=(s[0], s[1], linecache.getline(s[0], s[1]))

    return s

def post_event(e):
    caller=get_caller(True)
    print "Event %r posted from %r"%(e, caller)

## Testing the functions.

def q():
    post_event("baz")

post_event("foo")
print "Hello!"
q()

rawDescription: Locating (file/line) the invocation of a constructor in python


code:
for attr in ('thing', 'owner', 'color'):
    for item in bin:
        print '%-10s'%getattr(item, attr),
    print

rawDescription: How do I print parameters of multiple Python objects in table form?

description: Print parameters of multiple Python objects in table form


code:
def printEnums(obj,field):
     a=client.factory.create(str(getattr(client.factory.create( str(obj.__class__).replace('suds.sudsobject.','')),field).__class__).replace('suds.sudsobject.',''))
     for i in a:
         print i[0]

rawDescription: Listing all possible values for SOAP enumeration with Python SUDS


code:
root.geometry("{0}x{1}+0+0".format(maxW, maxH))

def callback(event):
    print "clicked at: ", event.x, "and: ", event.y

root.bind("<Button-1>", callback)

rawDescription: Get global mouse position and use in python program?


code:
import numpy as np

# Create an array to work with.
np.random.seed(123)
full = np.random.randint(1, 99, size=(8, 8))

# Get the indices for the largest `num_largest` values.
num_largest = 8

indices = (-full).argpartition(num_largest, axis=None)[:num_largest]
# OR, if you want to avoid the temporary array created by `-full`:
# indices = full.argpartition(full.size - num_largest, axis=None)[-num_largest:]

x, y = np.unravel_index(indices, full.shape)

print "full:"
print full
print "x =", x
print "y =", y
print "Largest values:", full[x, y]
print "Compare to:    ", np.sort(full, axis=None)[-num_largest:]

rawDescription: Find multiple maximum values in a 2d array fast


code:
from selenium import webdriver

driver = webdriver.PhantomJS()
driver.get('http://lema.rae.es/drae/srv/search?val=azul')
print driver.find_element_by_css_selector("body>div").text

rawDescription: Load web page in python AFTER JavaScripts executes


code:
print list(dd["Java", "s1.replaceAll(str1, str2);"])
# ['Python', 'Haxe', 'JavaScript']

rawDescription: How can I keep track of equivalent functions in different programming languages?

description: Keep track of equivalent functions in different programming languages


code:
X = np.random.normal(size=(10,5))
Y = np.random.normal(size=(10,5))
Z1 = inner1d(X,Y)
Z2 = [np.dot(X[k],Y[k]) for k in range(10)]
print np.allclose(Z1,Z2)

rawDescription: find the dot product of sub-arrays in numpy

description: Find the dot product of sub-arrays in numpy


code:
"""
This example module shows various types of documentation available for use
with pydoc.  To generate HTML documentation for this module issue the
command:

    pydoc -w foo

"""

class Foo(object):
    """
    Foo encapsulates a name and an age.
    """
    def __init__(self, name, age):
        """
        Construct a new 'Foo' object.

        :param name: The name of foo
        :param age: The ageof foo
        :return: returns nothing
        """
        self.name = name
        self.age

def bar(baz):
    """
    Prints baz to the display.
    """
    print baz

if __name__ == '__main__':
    f = Foo('John Doe', 42)
    bar("hello world")

rawDescription: How to comment parameters for pydoc

description: Comment parameters for pydoc


rawDescription: What's the best way to initialize a dict of dicts in Python?


code:
import pyqtgraph as pg
import numpy as np
app = pg.mkQApp()
data = np.random.normal(size=(120,20000), scale=0.2) + \
       np.arange(120)[:,np.newaxis]
view = pg.GraphicsLayoutWidget()
view.show()
w1 = view.addPlot()
w1.disableAutoRange()
now = pg.ptime.time()
for n in data:
    w1.plot(n)
w1.autoRange() # only after plots are added
print "Plot time: %0.2f sec" % (pg.ptime.time()-now)
app.exec_()

rawDescription: Plotting large arrays in pyqtgraph

description: Plotting large arrays in pyqtgraph


code:
sensortag=0
while sensortag != "B4:99:4C:64:33:E0":
    #call the command and write to scan.txt file and then fill the process.
    #loop to find if the MAC address given is available
    os.system("hcitool lescan> scan.txt & pkill --signal SIGINT hcitool")
    scan = open("scan.txt","r")
    readscan = scan.read()
    if "B4:99:4C:64:33:E0" in readscan:
        print "SensorTag found."
        sensortag = "B4:99:4C:64:33:E0"

rawDescription: Raspberry RFSniffer read output with python


code:
import netifaces as ni
ni.ifaddresses('eth0')
ip = ni.ifaddresses('eth0')[2][0]['addr']
print ip  # should print "192.168.100.37"

rawDescription: How can I get the IP address of eth0 in Python?

description: Get the IP address of eth0 in Python


code:
for idx, item in enumerate(list_a):
    if idx == len(list_a) - 1:
        print item, "is the last"
    else:
        print item, "is not the last"

rawDescription: Accessing the last element in a list in Python


code:
#!/usr/bin/env python
y = 0
def myfunction(x):
  global y
  y += x

input = (1, 2, 3, 4)

print "Filter output: %s" % repr(filter(myfunction, input))
print "Side effect result: %d" % y

rawDescription: Is there a map without result in python?

description: Is there a map without result in python


code:
d = eval('4 + 5')
print d

rawDescription: Math operations from string

description: Math operations from string


code:
for line in iter(lambda: sys.stdin.readline().decode('utf-8'), ''):
    print 'Echo:', line.strip()

rawDescription: Python read unicode stdin without batching


code:
def foo():
  x = 3
  f = sys._getframe()
  print f.f_locals['x']
  x = 4
  print f.f_locals['x']
  d = f.f_locals
  x = 5
  print d['x']
  f.f_locals
  print d['x']
foo()

rawDescription: Is it possible to write to a python frame object as returned by sys._getframe() from python code running within the interpreter?


code:
print foo
print bar

rawDescription: php `exec` calls python script. how return strings from python?

description: Php `exec` calls python script. how return strings from python


code:
# Create Example instances
a = Example()
b = Example()
c = Example()

# Set their name attributes
#   ( this actually calls <Example>._name.set() )
a.name = "ThingA"
b.name = "ThingB"
c.name = "ThingC"

# Access and print their name attributes
#   ( this actually calls <Example>._name.get() )
print a.name, b.name, c.name    # ThingA ThingB ThingC

# We can't bind like you suggested, but we can change what b._name points to
#   So here, we change b._name to point to the FakeMutableString a._name points to
b._name = a._name

# Now when we print the names, we see something different
print a.name, b.name, c.name    # ThingA ThingA ThingC

# And we can make a change in a, and have it reflected in b
a.name = "CommonName"
print a.name, b.name, c.name    # CommonName CommonName ThingC

# And vice-versa
b.name = "StillCommon"
print a.name, b.name, c.name    # StillCommon StillCommon ThingC

rawDescription: Data-Binding in Python?

description: Data-Binding in Python


code:
from PIL import Image

img = Image.open('a.png')
width, height = img.size

for y in range(0, height):
    if img.getpixel((1, y)) == (255, 255, 255):
        print "White line on Y == {}".format(y)

rawDescription: How should I select a colored line in a picture using Python?


code:
from bs4 import BeautifulSoup

page = open('file.html')
soupPage = BeautifulSoup(page)
findId = soupPage.find(id='url')
findId.contents[0].replace_with('NewString')
print soupPage

rawDescription: Change string Beautiful Soup

description: Change string Beautiful Soup


code: print df

rawDescription: Vectorized Operations on a datetime column in pandas


code:
for x,y in zip(predict_results,first_list):
    if(x==y):
        print False
    else:
        print True

rawDescription: How do you compare integer values in two separate lists?

description: Compare integer values in two separate lists


code:
import wx

# spoof the necessary matplotlib objects
class FakePlot:
    def __init__(self):
        self.figure = FakeFigure()
    def get_figure(self):
        return self.figure
class FakeFigure:
    def __init__(self):
        self.axes = [FakeAxis() for i in range(0,2)]
class FakeAxis:
    def __init__(self):
        self.lines = [FakeLine(i) for i in range(0, 4)]
class FakeLine:
    def __init__(self,i):
        self.label = "line #%s"%i
    def get_label(self):
        return self.label

class PlotEditFrame(wx.Frame):
    """
    This class holds the frame for plot editing tools
    """

    def __init__(self, parent, plot, size):
        """Constructor for PlotEditFrame"""
        wx.Frame.__init__(self, parent, -1, "Edit Plot", size=size)
        self.parent = parent
        self.plot = plot
        self.figure = plot.get_figure()
        self.advanced_options = None
        self.scroll = wx.ScrolledWindow(self, -1)
        self.InitControls()

    def InitControls(self):
        """Create labels and controls based on the figure's attributes"""

        # Get current axes labels
        self.lineCtrls = [( wx.StaticText(self.scroll, -1, "Column:"),
                            wx.StaticText(self.scroll, -1, "Color:"),
                            wx.StaticText(self.scroll, -1, ""))]

        for axis in self.figure.axes:
            for line in axis.lines:
                color = wx.Colour(255,0,0,0)
                lineTxt = wx.TextCtrl(self.scroll, -1, line.get_label(), size=(175,-1))
                lineColor = wx.TextCtrl(self.scroll, -1, "#%02x%02x%02x"%color.Get())
                lineBtn = wx.Button(self.scroll, -1, size=(25,25))
                lineBtn.SetBackgroundColour(color)
                self.lineCtrls.append((lineTxt, lineColor, lineBtn))

        # Place controls
        boxSizer = wx.BoxSizer(wx.VERTICAL)

        lineBox = wx.StaticBox(self, -1, "Lines")
        lineBoxSizer = wx.StaticBoxSizer(lineBox, wx.VERTICAL)
        lineSizer = wx.FlexGridSizer(rows=len(self.lineCtrls)+1, cols=4, vgap=3, hgap=3)
        for ctrls in self.lineCtrls:
            lineSizer.AddMany([(ctrls[0], 0, wx.ALIGN_LEFT | wx.EXPAND),
                               (ctrls[1], 0, wx.ALIGN_LEFT),
                               (ctrls[2], 0, wx.ALIGN_CENTER| wx.FIXED_MINSIZE),
                               ((3,3),    0, wx.ALIGN_CENTER)])
        lineSizer.AddGrowableCol(0)

        # Set size
        self.scroll.SetSizer(lineSizer)
        width = self.scroll.GetBestSize().width
        height = self.scroll.GetBestSize().height
        if height > 400:
            height = 400
            width = width + 25 # button size
        self.scroll.SetSize((width, height))
        self.scroll.SetScrollbars(0, 1, 1,1)
        print "set scrollbars at %s x %s"%(width, height)

        lineBoxSizer.Add(self.scroll, 1, wx.EXPAND)

        boxSizer.Add(lineBoxSizer, 1, wx.EXPAND)

        self.SetSizer(boxSizer)
        self.SetAutoLayout(1)
        #self.Fit()

        height = self.GetSize().GetHeight()
        self.SetSizeHints(minH=height, maxH=height,
                                minW=width, maxW=width*5)

if __name__ == '__main__':
    app = wx.App(False)
    plot = FakePlot()
    frame = PlotEditFrame(None, plot, size=(300,300))
    frame.Show()
    app.MainLoop()

rawDescription: wxPython ScrolledWindow too small

description: WxPython ScrolledWindow too small


code:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.utils.markup import remove_tags

class ExampleSpider(CrawlSpider):
    name = "goal3"
    allowed_domains = ["whoscored.com"]
    start_urls = ["http://www.whoscored.com"]
    download_delay = 1

    rules = [Rule(SgmlLinkExtractor(allow=()), follow=True, callback='parse_item')]

    def parse_item(self,response):
        paragraphs = response.selector.xpath("//p").extract()
        text = "".join(remove_tags(paragraph).encode('utf-8') for paragraph in paragraphs)
        print text

rawDescription: Scrapy not returning text body of web page with my current syntax


code:
class Base():
    templ = """
    - Base:
        - ????
    """
    reprtempl = "<Base: {self.__dict__}>"
    def report(self):
        return self.templ.strip().format(self=self)
    def __repr__(self):
        return self.reprtempl.format(self=self)

class Customer(Base):
    templ = """
    - Customer:
        - Address: {self.address}
        - Firstname: {self.first_name}
        - Surname: {self.surname}
        - Notes: {self.notes}
    """
    reprtempl = "<Customer: {self.__dict__}>"

    def __init__(self, first_name, surname, address, phone_home=None, phone_mobile=None, notes=None, **kwargs):
        self.first_name = first_name
        self.surname = surname
        self.address = address
        self.notes = notes
        self.phone_home = phone_home
        self.phone_mobile = phone_mobile

class Order(Base):
    templ = """
    - Order:
        - Order_date: {self.order_date}
        - Purchase_order: {self.purchase_order}
        - Total: {self.total}
    """
    reprtempl = "<Order: {self.__dict__}>"

    def __init__(self, order_date, purchase_order, total=None, **kwargs):
        self.order_date = order_date
        self.purchase_order = purchase_order
        self.total = total

if __name__ == "__main__":
    customer_dct = {"first_name": "Bob", "surname": "Smith", "address": "Sezam Street 1A",
            "phone_home": "11223344", "phone_mobile": "88990077"}
    customer = Customer(**customer_dct)
    print customer
    print customer.report()
    order_dct = {"order_date": "2014/01/01", "purchase_order": "abc211", "total": "$12"}
    order = Order(**order_dct)
    print order
    print order.report()

rawDescription: Parsing CSV data based on header fields using Pyparsing


code:
#!/usr/bin/env python
from multiprocessing import Process
import sslserver
import logging
if __name__ == "__main__":
    #logger = multiprocessing.log_to_stderr()
    #logger.setLevel(logging.DEBUG)

    #Fire up the server

    listenerProcess = Process(target = sslserver.startServer)
    listenerProcess.start()
    #logger.debug("Starting listener.")
    print "Starting Listener\n"
    listenerProcess.join()
    #logger.debug("Done.")
    print "Done\n";

rawDescription: multiprocessing not spawning new processes

description: Multiprocessing not spawning new processes


code:
def format_main_response(json_string):
    print "json:       " + json_string
    content = json.loads(str(json_string))
    for key, value in content.iteritems():
        print key
        if type(value) == type(['']):
            for sub_value in value:
                strg = str(json.dumps(sub_value))
                format_main_response(strg)
        else:
            print value

rawDescription: Reading a Json response recursevely with python


code:
def num_routes(list_of_lists):
    if len(list_of_lists) == 0:
        return 0

    return num_routes_starting_with(list_of_lists, min(list_of_lists[0]) - 1)

# You should add here some memoization decorator, something like:
# @memoized
def num_routes_starting_with(list_of_lists, starting_elem):
    if not list_of_lists:
        return 1

    s = 0
    for e in list_of_lists[0]:
        if e > starting_elem:
            s += num_routes_starting_with(list_of_lists[1: ], e)
    return s

list_of_lists = [[1, 10], [5, 16], [3, 20]]
print num_routes(list_of_lists)

rawDescription: Number of route in a list of lists

description: Number of route in a list of lists


code:
from sklearn.cluster import KMeans

data = [[1200, 1210], [1212, 1222], [1190, 1200], [300, 310], [800, 810]]
centers = [[sum(x) / len(x)] for x in data]

clf = KMeans(n_clusters=3)

clf.fit(centers)

for points in data:
    center = sum(points) / len(points)
    print points, center, clf.predict([center])

rawDescription: Finding "hotspots" in unidimensional list in python


code:
from timeit import timeit

setup = """
from random import sample, shuffle
a = range(100000)
b = sample(a, 1000)
a.reverse()
"""

forin = setup + """
def forin():
    # a = set(a)
    for obj in b:
        if obj in a:
            return obj
"""

setin = setup + """
def setin():
    return tuple(set(a) & set(b))[0]
"""

print timeit("forin()", forin, number = 100)
print timeit("setin()", setin, number = 100)

rawDescription: Speed differences between intersection() and 'object for object in set if object in other_set'


code:
from itertools import product
for k in range(2, 5):
    print list(product(range(2), repeat=k))

rawDescription: python all possible combinations of 0,1 of length k

description: Python all possible combinations of 0,1 of length k


code:
from sqlalchemy import Column, Integer, Sequence, types, Float
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql.expression import cast

Base = declarative_base()

class CastToFloatType(types.TypeDecorator):
    '''Converts stored Decimal values to Floats via CAST operation
    '''
    impl = types.Numeric
    def column_expression(self, col):
        return cast(col, Float)

class myTable(Base):
    __tablename__ = u'mytable'
    id = Column(Integer, Sequence('table_id_seq'), primary_key=True)
    wrapped_field = Column(CastToFloatType, nullable=True)

from sqlalchemy.orm import Session
s = Session()
print s.query(myTable)

rawDescription: How to wrap a column in a CAST operation

description: Wrap a column in a CAST operation


code: print [' '.join(w[i:j+1]) for i in range(len(w)) for j in range(i, len(w))]

rawDescription: Break string into words and phrases

description: Break string into words and phrases


code:
thetds = soup.findAll('td', attrs={'class': 'author'})
for thetd in thetds:
    print thetd.string

rawDescription: Python web scraping involving HTML tags with attributes


code:
for line in open(FunctionPath):
    line = line.strip()
    if line.startswith('def '):
        print '%s: %s' % (FunctionPath, line.partition(':')[0])

rawDescription: Making Python code Pythonic

description: Making Python code Pythonic


code:
output = [chr(int(element,2)) for element in MyArray]
print ''.join(output)

rawDescription: Convert a while loop into a for loop in Python 3.3


code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.PhantomJS()
driver.get("http://m.imdb.com/feature/bornondate")

# waiting for posters to load
wait = WebDriverWait(driver, 10)
posters = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "section.posters")))

# extracting the data poster by poster
for a in posters.find_elements_by_css_selector('a.poster'):
    img = a.find_element_by_tag_name('img').get_attribute('src').split('._V1.')[0] + '._V1_SX214_AL_.jpg'

    person = a.find_element_by_css_selector('div.detail').text
    title = a.find_element_by_css_selector('span.title').text

    print img, person, title

rawDescription: Webscraping an IMDb page using BeautifulSoup


code:
pattern = re.compile(r'(?:SETUP|PRO|INSTALL).{1,3}\d+')
print pattern.findall("SETUP 1234")
# ['SETUP 1234']
print pattern.findall("PRO 1234")
# ['PRO 1234']
print pattern.findall("INSTALL 1234")
# ['INSTALL 1234']

rawDescription: Combine many similar regex in one in Python


rawDescription: Handling monthly-binned data in pandas

description: Handling monthly-binned data in pandas


code:
# bool mask for empty cells that have non-empty cell before them
nofills = (df.iloc[:,:3] == 'x').cumsum(axis=1) & ((df.iloc[:,:3] == 'x') == False) > 0

# fill these with empty strings
df[nofills] = ''

# replace 'x'es with values from column D, ffill up NaNs then concat together into a new column
df['solution2'] = df.iloc[:,:3].where(df.iloc[:,:3] != 'x', df.D, axis=0).ffill().apply(''.join, axis=1)

print df

rawDescription: Conditional replacement of values in column A, B, C with value in column D


code:
tst = ([['name', u'bob-21'], ['name', u'john-28']], True)
tst2 = str(tst)
print tst2
repr(tst2)

rawDescription: Tuple to string

description: Tuple to string


code:
l = [(1,2),(2,3),(3,4),(4,5),(7,8),(8,9)]
import itertools,math

## Create sublists for each set of adjacent pairs
l.sort(lambda x,y:cmp(x[0],y[0]))
newl = []
subl = []
for i in range(len(l)):
    if subl == []:
        subl.append(l[i])
    else:
        if l[i][0] == subl[-1][1]:
            subl.append(l[i])
        else:
            newl.append(subl)
            subl = [l[i]]
newl.append(subl)
pair_lists = []

## Find all combinations for each sublist
for subl in newl:
    cur_pair_list = []
    for perm in itertools.combinations(subl,int(math.ceil(len(subl) / 2.0))):
        pair_list = []
        for pair in perm:
            if pair[0] in itertools.chain(*pair_list) \
            or pair[1] in itertools.chain(*pair_list):
                break
            pair_list.append(pair)
        if len(pair_list) == int(math.ceil(len(subl) / 2.0)):
            cur_pair_list.append(pair_list)
    pair_lists.append(cur_pair_list)

## Combine combinations for each sublist and determine unused for each combination
final_list = list(list(itertools.chain(*i)) for i in itertools.product(*pair_lists))
for subl in final_list:
    unused = []
    for n in itertools.chain(*l):
        if n not in itertools.chain(*subl) and n not in unused:
            unused.append(n)
    print subl,'unused:',unused

rawDescription: Generate adjacent pair permutations where each side of the pair can only be used once


code:
from multiprocessing import Pool

def f(x, *args, **kwargs):
    print x, args, kwargs

args, kw = (1,2,3), {'cat': 'dog'}

print "# Normal call"
f(0, *args, **kw)

print "# Multicall"
P = Pool()
sol = [P.apply_async(f, (x,) + args, kw) for x in range(2)]
P.close()
P.join()

for s in sol: s.get()

rawDescription: multiprocess.apply_async How do I wrap *args and **kwargs?

description: Multiprocess.apply_async How do I wrap *args and **kwargs


code:
import gobject
import dbus
import dbus.service

from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)

OPATH = "/com/example/HelloWorld"
IFACE = "com.example.HelloWorld"
BUS_NAME = "com.example.HelloWorld"

class Example(dbus.service.Object):
    def __init__(self):
        bus = dbus.SessionBus()
        bus.request_name(BUS_NAME)
        bus_name = dbus.service.BusName(BUS_NAME, bus=bus)
        dbus.service.Object.__init__(self, bus_name, OPATH)

    @dbus.service.method(dbus_interface=IFACE + ".SayHello",
                         in_signature="", out_signature="")
    def SayHello(self):
        print "hello, world"

if __name__ == "__main__":
    a = Example()
    loop = gobject.MainLoop()
    loop.run()

rawDescription: Register a "Hello World" DBus service, object and method using Python


code:
for k1, d1 in dictionary.items():
    for k2, v in d1.items():
        print k1, k2, v

rawDescription: Python reading complex nested json values


code:
import numpy as np
import pandas as pd

df['Zeros'] = (df['Val']==0)
df['Valdf'] = np.hstack((nan, diff(df['Zeros'].values))) #how do you treat the first cell?
df['Valdr'] = np.hstack((diff(df['Zeros'].values), nan)) #how do you treat the last cell?
pf2 = pd.concat([df[((df['Zeros']!=True)&(df['Valdf']==1))],df[((df['Zeros'])&(df['Valdr']==1))]]).sort_index()
pf2['State'] = np.where(pf2['Zeros'], 'Start', 'End')
pf2['idx2'] = pf2['OpID'] + np.arange(len(pf2))/2*0.00000001 #need unique index for .pivot()
print pf2.pivot(index='idx2', columns='State', values='OpTime')

rawDescription: pandas - determine the duration of an event

description: Pandas - determine the duration of an event


code:
from lxml import etree
from cStringIO import StringIO
t = etree.parse(StringIO("""

A title

Some text

"""))
root = t.getroot()
for child in root.iterdescendants(),:
 print etree.tostring(child)

rawDescription: Equivalent to InnerHTML when using lxml.html to parse HTML


code:
l = sorted(["abcd", "abc", "ab", "a"], key = len)
print [ss for idx, ss in enumerate(l) if all(ss not in cs for cs in l[idx + 1:])]

rawDescription: How to compute the minimum number of maximal strings?

description: Compute the minimum number of maximal strings


code:
def OnConnect(self, event):
    #print [event.IsChecked()]
    mon = Monitor("", "", "", self.path, "60", self.StatusBar)
    if event.IsChecked():
        print "Set Menu Label Disconnected"
        self._Connect.SetItemLabel("Disconnect")
        print "Start Monitor"
        mon.Start()
        print "Start Finished"
    else:
        print "Set Menu Label Connected"
        self._Connect.SetItemLabel("Connect")
        print "Stop Monitor"
        mon.Stop()
        print "Stop Finished"

rawDescription: Problems stopping a thread when signaled from a menu event?


code:
import hashlib

hash = hashlib.md5('test')
print hash.hexdigest()

c_evp_obj = cast(c_void_p(id(hash)), POINTER(EVPobject)).contents
ctx = c_evp_obj.ctx
digest = ctx.digest.contents
state = ctx.md_data[:digest.ctx_size]

hash2 = hashlib.md5()
c_evp_obj = cast(c_void_p(id(hash2)), POINTER(EVPobject)).contents
ctx = c_evp_obj.ctx
digest = ctx.digest.contents
memmove(ctx.md_data, state, digest.ctx_size)
print hash2.hexdigest()

rawDescription: Expose _hashlib.pyd internals for EVP_MD_CTX?


code:
inp_val = raw_input('Guess the number of legs: ')
try:
    guess_num = int(inp_val)
    if guess_num == leg:
        print('True')
    else:
        print('False')
except ValueError:
    if inp_val == 'back':
        loop = 0
    else:
        print 'Invalid entry'
    animalmenu()

rawDescription: Returning to menu after command? Python

description: Returning to menu after command? Python


code:
class MultiIter(object):
    def __init__(self, f, *a, **k):
        if a or k:
            self.create = lambda: f(*a, **k)
        else: # optimize
            self.create = f
    def __iter__(self):
        return self.create()

l = MultiIter(lambda: imap(str, xrange(1,4)))
# or
l = MultiIter(imap, str, xrange(1,4))
# or even
@MultiIter
def l():
    return imap(str, xrange(1,4))

# and then
print list(l)
print list(l)

rawDescription: Itertools for containers

description: Itertools for containers


code:
for a_dict in dict_generator:
    print a_dict

rawDescription: Returning elements from a loop, one at a time


code:
data = {
 'jan': ['John Smith', 'Cat Stevens', 'Andrew Alexander', 'El Gordo Baba'],
 'feb': ['Louis le Roy', 'John Smith'],
 'mar': ['Cat Stevens', 'Louis le Roy']
}

from itertools import izip

keys = 'jan feb mar'.split()
for m1,m2 in izip(keys,keys[1:]):
    a = set(data[m1])
    b = set(data[m2])
    print m1, '\n\tsubscribed:', ','.join(b-a), '\n\tquit:', ','.join(a - b )

rawDescription: Comparing Lists

description: Comparing Lists


code:
import math
x = raw_input("Input action to be peformed on your number, followed by your number: ")
# Assume "Pi 2"  is entered
x = x.split()
action = x[0]
number = int(x[1])
if action.startswith("Pi"):
    print number * math.pi

rawDescription: Python check last word/number in a string?


code:
jobs = ['engineer','artist','mechanic','teacher','teacher','engineer','engineer']

print list(set(jobs))
['engineer','artist','mechanic','teacher']

rawDescription: removing duplicate content python

description: Removing duplicate content python


code:
import itertools

list1 = ['Like', 'Hate']
list2 = ['Big', 'Medium', 'Small']
list3 = ['Trucks', 'Cars']

sub23 =  [' '.join(x) for x in itertools.product(list2, list3)]
sub12 = [' '.join(x) for x in itertools.product(list1, list2 + sub23)]
res = list1 + sub12 + sub23 + list3
print res

rawDescription: Get combinations of words lists

description: Get combinations of words lists


code:
def sort_key(a):
    return a[0], -int(a[2]), a[1]

for i in sorted(m, key=sort_key): print i

rawDescription: Sort string-matrix [[a, b, c], ...] by a, -c, b, maintaining order?


code:
rdflib.plugin.register('sparql', rdflib.query.Processor,
                       'rdfextras.sparql.processor', 'Processor')
rdflib.plugin.register('sparql', rdflib.query.Result,
                       'rdfextras.sparql.query', 'SPARQLQueryResult')

 #Parse the file
g = rdflib.Graph()
g.parse("yourdata.nquads")

query = """
    SELECT ?pred ?obj WHERE {
         <http://www.someuri.org/for/your/subject> ?pred ?obj
    }
    """
for row in g.query(query):
    print "Predicate:%s Object:%s"%(row[0],row[1])

rawDescription: how to look-up rdf object knowing subject or other way around?

description: Look-up rdf object knowing subject or other way around


rawDescription: What does *map mean in python?

description: What does *map mean in python


code:
import re
from itertools import izip

s = 'Title: Example: of a title Date: November 23 1234 Other: Other information'

keys = ['Title', 'Date', 'Other']
pattern = re.compile('({})\s+'.format(':|'.join(keys)))

print dict(izip(*[(i.strip() for i in (pattern.split(s)) if i)]*2))

rawDescription: Create a dictionary with a string with colons


code:
accuracy=[{'value':1,'key':'apple'}, {'value':2,'key':'orange'}]
for fruit in accuracy:
    print 'name={key}'.format(**fruit)
    print 'value={value}'.format(**fruit)

rawDescription: Getting value of a list in Python

description: Getting value of a list in Python


code:
from pyparsing import *

# define arithmetic items
identifier = Word(alphas, alphanums+'_')
integer = Word(nums)
real = Regex(r'\d+\.\d*')
oper = oneOf("* + - /")

# define arithOperand as a Forward, since it could contain nested power expression
arithOperand = Forward()
arithExpr = arithOperand + ZeroOrMore(oper + arithOperand)
groupedArithExpr = '(' + arithExpr + ')'

# define expression for x^y, where x could be any kind of arithmetic term
powerOp = Literal('^')
powerExpr = (groupedArithExpr|real|integer|identifier) + powerOp + integer
powerExpr.setParseAction(lambda tokens: 'pow(%s,%s)' % (tokens[0], tokens[2]))

# now define the possible expressions for arithOperand, including a powerExpr
arithOperand <<= powerExpr | real | integer | identifier | groupedArithExpr

# convert parsed list of strings to a single string
groupedArithExpr.setParseAction(''.join)

# show how transform string will apply parse actions as transforms
print arithOperand.transformString("x = (4*(1 + 3^2) * a)^10")
print

rawDescription: Pyparsing - Finding Nested Polynomials

description: Pyparsing - Finding Nested Polynomials


code:
## rab.py
import foo
foo.bar()
# expected to return and it does
foo.check(5)
print 'bar: ok'
def rab():
    global a
    a = 6
foo.rab = type(rab)(rab.func_code, foo.__dict__)
foo.rab()
# may be expected to return but it raises AssertionError:
foo.check(6)
print 'rab: ok'

rawDescription: Python 2 inject into module scope with global keyword and closures


code:
import ast
data = ast.literal_eval('{' + s2 + '}')
print data['Matt']['Items']['Banana']
# 2

rawDescription: Parse Key Value Pairs in Python

description: Parse Key Value Pairs in Python


code:
@app.route('/contribute', methods=['POST'])
def contribute():
    try:
        data = request.stream.read()
        result = eval(zlib.decompress(data))
        db.result.insert(result)
    except:
        print sys.exc_info()
        pass
    return 'OK'

rawDescription: Python send data over http

description: Python send data over http


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

rawDescription: Solving the unicode output in Python

description: Solving the unicode output in Python


code:
class x(object):
  def c1(*args): print 'c1', args
  c1 = classmethod(c1)
  @classmethod
  def c2(*args): print 'c2', args

inst = x()
x.c1()
x.c2()
inst.c1()
inst.c2()

rawDescription: What does classmethod do in this code?

description: What does classmethod do in this code


code:
from pyDes import *

data = "Please encrypt my data"
k = des("DESCRYPT", CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
d = k.encrypt(data)
print "Encrypted: %r" % d
print "Decrypted: %r" % k.decrypt(d)
assert k.decrypt(d, padmode=PAD_PKCS5) == data

rawDescription: using DES/3DES with python

description: Using DES/3DES with python


code:
"""H1"""
from multiprocessing import Process, Pipe
import sys

def P1(conn):
    print 'P1: some short lived work'
    sys.stdout.flush()
    conn.send('work done')
    # wait for shutdown command...
    conn.recv()
    conn.close()
    print 'P1: shutting down'

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()
    p = Process(target=P1, args=(child_conn,))
    p.start()
    print parent_conn.recv()
    print 'H1: some other IO bound task'
    parent_conn.send("game over")
    p.join()

rawDescription: How do I start a subprocess which runs in the background until it is told to terminate gracefully?

description: Start a subprocess which runs in the background until it is told to terminate gracefully


code:
from ast import literal_eval

def str_to_type(s):
    try:
        k=literal_eval(s)
        return type(k)
    except:
        return type(s)

l = ['444', '1.2', 'foo', '[1,2]', '[1']
for v in l:
    print str_to_type(v)

rawDescription: Get type of data stored in a string in python


code:
from random import randint
from itertools import takewhile, islice

file = "/etc/passwd"
f = open(file, "r")

f.seek(randint(10,250))
pos = f.tell()

print "pos=%d" % pos

def countbytes(iterable):
    bytes = 0
    for item in iterable:
        bytes += len(item)
        yield bytes

print 1+len(list(takewhile(lambda x: x <= pos, countbytes(open(file, "r")))))

rawDescription: How to get the current open file line in python?

description: Get the current open file line in python


rawDescription: interpolate linear array to non linear array using python numpy or scipy

description: Interpolate linear array to non linear array using python numpy or scipy


code:
# List of possible Strings
comma_check_list = ['hello, world', 'hello world', 'hello,  world', 'hello world, good, morning']

# Dictionary of punctuation that's not a comma
punct = set('@^! #%$&)(+*-="')

# Function to check the string
def string_check(comma_check_list, punct):
    for string in comma_check_list:
        new_list = string.split(", ")
        print string, not any(char in punct for item in new_list for char in item)

# Usage
string_check(comma_check_list, punct)

rawDescription: Less clunky way of checking a string for comma separation in Python?


code:
def get_file(self, dir, url, name):
    s = urllib2.urlopen(url)
    f = open('xml/test.xml','w')
    meta = s.info()
    file_size = int(meta.getheaders("Content-Length")[0])
    print "Downloading: %s Bytes: %s" % (name, file_size)
    current_file_size = 0
    block_size = 4096
    while True:
        buf = s.read(block_size)
        if not buf:
            break
        current_file_size += len(buf)
        f.write(buf)
        status = ("\r%10d  [%3.2f%%]" %
                 (current_file_size, current_file_size * 100. / file_size))
        status = status + chr(8)*(len(status)+1)
        sys.stdout.write(status)
        sys.stdout.flush()
    f.close()
    print "\nDone getting feed"
    return 1

rawDescription: How to scrape xml urls with scrapy

description: Scrape xml urls with scrapy


code:
from itertools import chain, groupby

def get_longest_interval(x):
    longest_interval = max(
        ([v for _, v in grp] for k, grp in groupby(enumerate(
            set.intersection(*(set(chain(*(range(int(start), int(end)+1) for (start, end) in f_intervals))) for f_intervals in x))
        ), lambda (index, num): index-num)), key=len
    )
    return longest_interval[0], longest_interval[-1]

x1 = [[['3', '7'], ['9', '17']],
      [['1', '5'], ['10', '20']],
      [['0', '6'], ['12', '19']]]

x2 = [[[3, 7], [9, 21]],
      [[1, 5], [10, 20]],
      [[0, 6]]]

for x in x1, x2:
    print get_longest_interval(x)

# This prints
(12, 17)
(3, 5)

rawDescription: Find the longest common interval between points on graph


code:
from operator import itemgetter

zero_index = itemgetter(0)

print map(zero_index, x)

rawDescription: Python list of tuples, need to unpack and clean up


code:
import re

txt= {"delete(http://www.waynesworld123.com)",
      "delete(12!@Ww)",
      "elete(test)",
      "delete[test]",
      "test"}

pattern=re.compile('^delete\(.*\)$', re.DOTALL)

for line in txt:
    if pattern.search(line):
        print 'PASS', line
    else:
        print 'FAIL',line

rawDescription: python: regex needed for format: 'delete([any text here])'

description: Python: regex needed for format: 'delete([any text here])'


code: print list(tb_notes.select().execute())

rawDescription: SQLAlchemy - Dictionary of tags

description: SQLAlchemy - Dictionary of tags


code:
from collections import OrderedDict

od = OrderedDict([
        (u'totalSize', 418),
        (u'done', True),
        (u'records', [
            OrderedDict([
                (u'attributes', OrderedDict([
                                    (u'type', u'Case'),
                                    (u'url', u'/services/Case/11111')
                                    ])),
                (u'Id', u'11111'),
                (u'Subject', u'Case 1')
                ]),
            OrderedDict([
                (u'attributes', OrderedDict([
                                    (u'type', u'Case'),
                                    (u'url', u'/services/Case/2222222')
                                    ])),
                (u'Id', u'2222222'),
                (u'Subject', u'Case 2')
                ]),
            #...
            ])
        ])

list1 = [(record['Id'], record['Subject']) for record in od['records']]
print list1  # -> [(u'11111', u'Case 1'), (u'2222222', u'Case 2')]

rawDescription: Parsing OrderedDict to Python list

description: Parsing OrderedDict to Python list


code:
import sys

print "Number of arguments", len(sys.argv)
for arg in sys.argv :
    print arg

# or

print 'second method'

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

# take the test name given by the Jenkins parameter $Test_Name
testName = sys.argv[2] # arg 0 is script name, arg 1 == '-t', arg 2 == '<the provided test name in Jenkins Run Job interface as a string>', ...
print testName

rawDescription: Jenkins and Python

description: Jenkins and Python


code:
import pickle
import unittest

class SafePickler(pickle.Pickler):
    def save(self, obj):
        try:
            pickle.Pickler.save(self, obj)
        except TypeError, e:
            pickle.Pickler.save(self, None)

pickle_file = open("temp" + ".pickle", 'wb')
u = unittest.TestResult()
SafePickler(pickle_file).dump(u)

rawDescription: How to save unittest.TestResult

description: Save unittest.TestResult


code:
import timeit
from random import shuffle

l = range(10000)
shuffle(l)

def Run_1():
    #print "Min =", min(l)
    #print "Max =", max(l)
    return min(l), max(l)

def Run_2():
    l1 = list(l)
    l1.sort()
    #print "Min =", l1[0]
    #print "Max =", l1[-1]
    return l1[0], l1[-1]

def Run_3():
    minimum = float('inf')
    maximum = float('-inf')
    for item in l:
        if item < minimum:
            minimum = item
        if item > maximum:
            maximum = item
    #print "Min =", minimum
    #print "Max =", maximum
    return minimum, maximum

if __name__ == '__main__':
    num_runs = 10000
    print "Run 1"
    run1 = timeit.Timer(Run_1)
    time_run1 = run1.repeat(3, num_runs)
    print ""
    print "Run 2"
    run2 = timeit.Timer(Run_2)
    time_run2 = run2.repeat(3,num_runs)
    print ""
    print "Run 3"
    run3 = timeit.Timer(Run_3)
    time_run3 = run3.repeat(3,num_runs)
    print ""

    print "Run 1"
    for each_time in time_run1:
        print "time =", each_time
    print ""
    print "Run 2"
    for each_time in time_run2:
        print "time =", each_time
    print ""
    print "Run 3"
    for each_time in time_run3:
        print "time =", each_time
    print ""

rawDescription: How can I get the minimum and the maximum element of a list in python

description: Get the minimum and the maximum element of a list in python


code:
import random
for i in xrange(len(urls)):
    all_urls = []
    count = i
    while (count>=0):
        rand_url = urls[random.randint(0,len(urls)-1)]
        if not rand_url in all_urls:
            all_urls.append(rand_url)
            count-=1
    print all_urls

rawDescription: How to obtain this behavior in python

description: Obtain this behavior in python


code:
crect.width=10
print rect.width, rect.height
#10 5

rawDescription: Subclassing in python of instantiated superclass


code:
import sys
##Initials,Last,First,ShiftStart,ShiftEnd
s='''BAB,Smith,Bob,10:00a,1:00p
JCJ,Jones,Jill,11:00a,3:00p
JIH,Hernandez,Jose,1:00p,4:00p
BAB,Smith,Bob,1:00p,3:00p
JIH,Hernandez,Jose,5:00p,9:00p
JCJ,Jones,Jill,3:00p,3:30p
JCJ,Jones,Jill,3:30p,5:00p
DJM,Martin,Dominique,8:00a,11:00a'''

class Shift(object):
    def __init__(self,ShiftStart,ShiftEnd):
        self.ShiftStart,self.ShiftEnd = ShiftStart,ShiftEnd
    def __str__(self):
        return '%s,%s' % (ShiftStart,ShiftEnd)

class Person(object):
    def __eq__(self, p):
        if self.Initials != p.Initials:
            return False
        if p.Last is not None and self.Last != p.Last:
            return False
        if p.First is not None and self.First != p.First:
            return False
        return True
    def __init__(self,Initials,Last,First):
        self.Initials,self.Last,self.First = Initials,Last,First
        self.Shifts = []
    def __str__(self):
        return '%s,%s,%s' % (self.Initials,self.Last,self.First)

def AddShift(people, person, shift):
    try:
        person = people[people.index(person)]
    except ValueError:
        people.append(person)
    person.Shifts.append(shift)

people = []
for line in s.split('\n'):
     Initials,Last,First,ShiftStart,ShiftEnd = line.split(',')
     AddShift(people, Person(Initials,Last,First), Shift(ShiftStart,ShiftEnd))

for person in people:
    print '%s,%s' %(person, ','.join(map(str,person.Shifts)))

rawDescription: Consolidate .csv File to One Row per Unique Key (Example, Person) in Python


code:
import re
url = 'http://xyzzy.com?paxdiablo=awesome'
url = re.sub(r'^[^?]*\?','',url)
url = re.sub(r'=',' is ',url);
print url

rawDescription: passing a variable instead of input file to sed

description: Passing a variable instead of input file to sed


code:
print "Fancy Indexing:"
print a[n1, n2]

print "Manual indexing:"
for i, j in zip(n1, n2):
    print a[i, j]

rawDescription: Subsetting a 2D numpy array

description: Subsetting a 2D numpy array


code:
def POST(self):
    s = web.input(primary = [])
    print str(s.primary)

rawDescription: Web.py : How to Post values from a multiple select in a form?Submit only gives me one of the selected values


code:
from PIL import Image, ImageTk
#from graphics import GraphWin # not needed

# for Python 3.x
from tkinter import filedialog # Will be used to open the file from the user
import tkinter

# for Python 2.x
#import Tkinter as tkinter
#import tkFileDialog as filedialog # Will be used to open the file from the user

import os

# --- Global variables for radio buttons ---

radio1 = True
radio2 = False
radio3 = False
radio4 = False

#--------------------------------------------

# --- Global variables for picture ---

pic = None
tkPic = None
tkPic2 = None
picToConvert = None
picWidth = 0
picHeight = 0
canvas1 = None

var = None #create global variable

#----------------------------------------------------------------------

def rotateIt():
    global pic

    print "(debug) rotateIt:", pic
    pic = pic.rotate(180)

# --- Function for radio buttons ---

def whichSelected(numberSelected):
        global radio1
        global radio2
        global radio3
        global radio4

        if numberSelected == 4:
            radio1 = False
            radio4 = True
        elif numberSelected == 3:
            radio1 = False
            radio3 = True
        elif numberSelected == 2:
            radio1 = False
            radio2 = True
        elif numberSelected == 1:
            radio1 = True

# --- Gray Algorithms ---

def grayAverage(r, g, b):
    return (r + g + b) // 3

def invertRGB(r,g,b):
    return (255 - r, 255 - g, 255 - b)

def lightness(r,g,b):
    return (max(r, g, b) + min(r, g, b)) // 2

def luminosity(r,g,b):
    return int(((0.21 * r) + (0.71 * g) + (0.07 * b)))

def getRGB(r,g,b):
    red   = eval(input("Enter the value of red: "))
    green = eval(input("Enter the value of green: "))
    blue  = eval(input("Enter the value of blue: "))

    return red-r + green-g + blue-b // 3

# --- End Gray Algorithms ---

# Draws window, opens picture selected by user, packs the canvas
def drawWindow():
    window = tkinter.Tk()
    window.title(os.environ.get( "USERNAME" )) # sets the window title to the

    return window

def drawCanvas():
    global window
    global canvas1
    canvas1 = tkinter.Canvas(window, width = 820, height =340) # Draws a canvas onto the tkinter window
    canvas1.pack()

    return canvas1

# Global variables for window and canvas
window = drawWindow()
canvas1 = drawCanvas()

#----------------------------------------------------------------------

# --- Radio Button Code ---

def drawRadioButtons():
    global window, var

    var = tkinter.IntVar()

    option1 = tkinter.Radiobutton(window, text ='Average Grayscale',    variable = var, value = 1, command = lambda: whichSelected(1))
    option2 = tkinter.Radiobutton(window, text ='Lightness Grayscale',  variable = var, value = 2, command = lambda: whichSelected(2))
    option3 = tkinter.Radiobutton(window, text ='Luminosity Grayscale', variable = var, value = 3, command = lambda: whichSelected(3))
    option4 = tkinter.Radiobutton(window, text ='Invert',               variable = var, value = 4, command = lambda: whichSelected(4))
    option5 = tkinter.Radiobutton(window, text ='Rotate 180',           variable = var, value = 5)

    # Sets the first button to clicked
    option1.select()

    # Pack Radio Buttons
    option1.pack(anchor = 'sw')
    option2.pack(anchor = 'sw')
    option3.pack(anchor = 'sw')
    option4.pack(anchor = 'sw')
    option5.pack(anchor = 'sw')
    # End Radio Button code

def openImage():
    global window
    global canvas1
    global pic
    global picWidth
    global picHeight
    global tkPic
    global tkPic2
    global picToConvert

    canvas1.delete('all')
    del pic
    del tkPic

    picToConvert = filedialog.askopenfilename(defaultextension='.jpg') # Used to open the file selected by the user
    pic = Image.open(picToConvert).convert('RGB')
    picWidth, picHeight = pic.size # PIL method .size gives both the width and height of a picture
    tkPic = ImageTk.PhotoImage(pic, master = window) # Converts the pic image to a tk PhotoImage
    canvas1.create_image(10,10,anchor='nw', image = tkPic)

#----------------------------------------------------------------------

def saveImage():
    global pic

    toSave = filedialog.asksaveasfile(mode='w',defaultextension='.jpg')
    pic.save(toSave)

#----------------------------------------------------------------------

def onConvert():
    global var
    global tkPic2
    global window

    if var.get() == 5:
        rotateIt(pic)
    else:
        change_pixel()

    # Converting to a tkinter PhotoImage

    if tkPic2:
        del tkPic2

    tkPic2 = ImageTk.PhotoImage(pic, master = window)
    canvas1.create_image(815,170, anchor='e',image = tkPic2)

#----------------------------------------------------------------------

def change_pixel():
    global window
    global canvas1
    global pic

    global radio1
    global radio2
    global radio3
    global radio4

    # Treats the image as a 2d array, iterates through changing the
    #values of each pixel with the algorithm for gray

    rgbList = pic.load() #Get a 2d array of the pixels
    for row in range(picWidth):
        for column in range(picHeight):
            rgb = rgbList[row,column]
            #print rgb
            r,g,b = rgb # Unpacks the RGB value tuple per pixel
            if radio1 == True:
                grayAlgorithm1 = grayAverage(r,g,b)
                rgbList[row,column] = (grayAlgorithm1, grayAlgorithm1, grayAlgorithm1)
            elif radio2 == True:
                grayAlgorithm1 = lightness(r,g,b)
                rgbList[row,column] = (grayAlgorithm1, grayAlgorithm1, grayAlgorithm1)
            elif radio3 == True:
                grayAlgorithm1= luminosity(r,g,b)
                rgbList[row,column] = (grayAlgorithm1, grayAlgorithm1, grayAlgorithm1) # Gives each pixel a new RGB value
            elif radio4 == True:
                rgbList[row,column] = invertRGB(r,g,b) # Gives each pixel a new RGB value

#----------------------------------------------------------------------

# Function to create a button, takes the button text and the function to be called on click
def tkButtonCreate(text, command):
    tkinter.Button(window, text = text, command = command).pack()

#----------------------------------------------------------------------

def main():
    drawRadioButtons()
    tkButtonCreate('Open Image',openImage)
    tkButtonCreate('Convert', onConvert)
    tkButtonCreate('Save',saveImage)
    window.mainloop()
    #convertButton = tkinter.Button(window,text = 'Convert', command = change_pixel).pack()

#----------------------------------------------------------------------

main()

rawDescription: Having some trouble converting a tk PhotoImage back to a PIL image to save


code:
import re

def parse_float(str):
  re_float = re.compile(r'(-?)(\d+)\.(\d+)')
  grps = re_float.search(str)
  sign, decimal, fraction = grps.groups()
  float_val = float('%s%s.%s' % (sign, decimal, fraction))
  total_len = len(grps.group(0))
  print '%*.*f'  % (total_len, len(fraction), float_val)

parse_float('1.2345678')
parse_float('1.234')
parse_float('1.2340000')

rawDescription: keeping same formatting for floating point values

description: Keeping same formatting for floating point values


code: print v1

rawDescription: Method to convert object from class to subclass in Python