code:
import riak

client = riak.RiakClient(pb_port=8087, host="172.31.0.1", protocol='pbc')

test_bucket = client.bucket('test_bucket')

data1 = test_bucket.new('key1',data={'field1':'1data1','field2':'1data2','field3':1, 'zone':'D'})
data1.store()

data2 = test_bucket.new('key2',data={'field1':'2data1','field2':'2data2','field3':2, 'zone':'A'})
data2.store()

data3 = test_bucket.new('key3',data={'field1':'3data1','field2':'3data2','field3':3, 'zone':'C'})
data3.store()

def printresult(q):
  for result in q.run():
    print "%s" % (result)

print "\nCount the number of values in the bucket"
query = riak.RiakMapReduce(client).add('test_bucket')
query.map(['custom_mr','mapcount'])
query.reduce(['custom_mr','reducecount'])
printresult(query)

print "\nList all values in natual sort order"
query = riak.RiakMapReduce(client).add('test_bucket')
query.map(['custom_mr','mapvalue'])
query.reduce(['custom_mr','reducesort'])
printresult(query)

print "\nList all values sorted by 'zone'"
query = riak.RiakMapReduce(client).add('test_bucket')
query.map(['custom_mr','mapfield'])
query.reduce(['custom_mr','reducepropsort'],{'arg':'zone'})
printresult(query)

print "\nList all values sorted by 'zone', also sort the fields in each object"
query = riak.RiakMapReduce(client).add('test_bucket')
query.map(['custom_mr','mapfield'])
query.reduce(['custom_mr','reducedoublesort'],{'arg':'zone'})
printresult(query)

print "\nList just field3, sorted"
query = riak.RiakMapReduce(client).add('test_bucket')
query.map(['custom_mr','mapfield'],{'arg':'field3'})
query.reduce(['custom_mr','reducepropsort'],{'arg':'field3'})
printresult(query)

print "\nList just bucket,key,field3, sorted by field3"
query = riak.RiakMapReduce(client).add('test_bucket')
query.map(['custom_mr','mapfieldwithid'],{'arg':'field3'})
query.reduce(['custom_mr','reducepropsort'],{'arg':'field3'})
printresult(query)

print "\nReturn just the zone for key2"
query = riak.RiakMapReduce(client).add('test_bucket','key2')
query.map(['custom_mr','mapfield'],{'arg':'zone'})
printresult(query)

print "\nReturn the bucket,key,zone for key1 and key3"
query = riak.RiakMapReduce(client).add('test_bucket',['key1','key3'])
query.map(['custom_mr','mapfieldwithid'],{'arg':'zone'})
query.reduce(['custom_mr','reducepropsort'],{'arg':'zone'})
printresult(query)

rawDescription: What is the exact way to send erlang module and erlang function to the mapreduce face in python-riak client l


code:
def my_generator():
    while True:
        yield 123
print zip([1,2], my_generator())
# [(1, 123), (2, 123)]

rawDescription: Why does zip on a generator return just one item?


code:
import inspect

class A():
    def __init__(self):
        self.one = 1
        self.two = B()

class B():
    def __init__(self):
        self.three = "hello"
        self.parent = inspect.currentframe().f_back.f_locals['self']

        print self.parent.one

rawDescription: Acessing a property from a parent object


code:
for item in inspect.getmembers(MyClass, predicate=inspect.ismethod):
    print item[0]
    print item[1].__doc__

rawDescription: How to convert a Python file docstring's to HTML

description: Convert a Python file docstring's to HTML


code:
myList=[]

myList.append(("a",0))
myList.append(("b",0))
myList.append(("b",0))
myList.append(("a",2))
myList.append(("a",1))
myList.append(("a",1))

Dict   = {}
Unique = []
for Data in myList :
    Dict.setdefault(Data[1], []).append(Data[0])
    if Data[1] not in Unique: Unique.append(Data[1])

print [(Dict[Data][0], Data) for Data in Unique]

rawDescription: Getting unique items from a two dimensional array python


code:
from StringIO import StringIO
txt = """date,page_id,score
2001-05-23 19:50:14,  1,3
2001-05-25 11:53:55,  2,4
2001-05-27 17:36:37,  1,5
2001-05-28 19:36:37,  1,1
2001-05-28 19:36:38,  3,6
2001-05-28 19:36:39,  3,9
"""

df = pd.read_csv(StringIO(txt), index_col=0)

def score_sum_py(page_id, scores):
    from itertools import izip
    score_sum = 0
    last_score = [0]*(np.max(page_id)+1)
    result = np.empty_like(scores)
    for i, (pid, score) in enumerate(izip(page_id, scores)):
        score_sum = score_sum - last_score[pid] + score
        last_score[pid] = score
        result[i] = score_sum
    result.name = "score_sum"
    return result

print score_sum_py(pd.factorize(df.page_id)[0], df.score)

rawDescription: How to accumulate unique sum of columns across pandas index

description: Accumulate unique sum of columns across pandas index


code:
dictionary = {'word': 2, 'other': 5, 'string': 10}
words = "this is a string of words you see and other things"

counter = 0
for word in words.split():
    if word in dictionary:
        counter += dictionary[word]
print counter

rawDescription: Keeping values from an if statement

description: Keeping values from an if statement


code:
global_func_items = []
def append_loop2(items):
  for i in items:
      if isinstance(i, list):
          append_loop2(i)
      else:
          global_func_items.append(i)
append_loop2([1,2,[3,[4,5]]])
print global_func_items

rawDescription: How to append a multiply nested list into and empty one?

description: Append a multiply nested list into and empty one


code:
import xml.etree.ElementTree as ET
def loadfile():
    tree = ET.parse('filename')
    root = tree.getroot()
    for envelope in root.iter('{http://www.opengis.net/gml}Envelope'):
        key = envelope.attrib
        srsName = key.get('srsName')
        print "SRS Name: ", srsName

if __name__== "__main__":
    loadfile()

rawDescription: no results when parsing CityGML in Python

description: No results when parsing CityGML in Python


code:
import datetime
import zipfile

def print_info(archive_name):
    zf = zipfile.ZipFile(archive_name)
    for info in zf.infolist():
        print info.filename
        print '\tComment:\t', info.comment
        print '\tModified:\t', datetime.datetime(*info.date_time)
        print '\tSystem:\t\t', info.create_system, '(0 = Windows, 3 = Unix)'
        print '\tZIP version:\t', info.create_version
        print '\tCompressed:\t', info.compress_size, 'bytes'
        print '\tUncompressed:\t', info.file_size, 'bytes'
        print

if __name__ == '__main__':
    print_info('example.zip')

rawDescription: Check a .zip archive integrity/filepaths/compression methods from python?


code:
from fabric.api import run

def super_status():
    uname = "zen"
    pswd = "then"
    cmd = "supervisorctl -u {uname} -p {pswd} status logproxy".format(uname=uname, pswd=pswd)
    # to see the command you are going to call, just for show
    print cmd
    # and run it
    run(cmd)

rawDescription: How to exit a supervisor process with fabric file?

description: Exit a supervisor process with fabric file


code:
for i in my_range(0, 1, 10):
    print i

rawDescription: Is there a built-in Python function to generate 100 numbers from 0 to 1?


code:
with open('webster.txt') as fp:
    definition = find_definition(fp, 'Canada')
    print ''.join(definition)

rawDescription: Read out definitions of a text file based dictionary


code:
class Article(object):
       title = '111'
       body  = '222'
       def __init__(self):
              setattr(Article,"user",145)

aaaaaaa = Article()
print '# aaaaaaa = Article()  /done'
print '  aaaaaaa.__dict__ == %r ' % aaaaaaa.__dict__
print '  aaaaaaa.user == %r ' % aaaaaaa.user
print '  Article.user == %r\n' % Article.user

aaaaaaa.user = 2000
print '# aaaaaaa.user = 2000  /done'
print '  aaaaaaa.__dict__ == %r ' % aaaaaaa.__dict__
print '  aaaaaaa.user == %r ' % aaaaaaa.user
print '  Article.user == %r\n' % Article.user

Article.user = 'JUJU'
print "# Article.user = 'JUJU'  /done"
print '  aaaaaaa.__dict__ == %r ' % aaaaaaa.__dict__
print '  aaaaaaa.user == %r ' % aaaaaaa.user
print '  Article.user == %r\n' % Article.user

bbbbbbb = Article()
print '# bbbbbbb = Article()  /done'
print '  aaaaaaa.__dict__ == %r ' % aaaaaaa.__dict__
print '  aaaaaaa.user == %r ' % aaaaaaa.user
print '    bbbbbbb.__dict__ == %r ' % bbbbbbb.__dict__
print '    bbbbbbb.user == %r ' % bbbbbbb.user
print '    Article.user == %r ' % Article.user

rawDescription: confused between class attributes and instance attributes

description: Confused between class attributes and instance attributes


code:
for line in sys.stdin:
    parts = shlex.split(line)
    print parts

rawDescription: Python module for parsing cusomized config files


code:
print 'Missing values =', ','.join(set(list1).difference(list2))
print 'Additional values =', ','.join(set(list2).difference(list1))

rawDescription: Missing and Additional values in 2 list

description: Missing and Additional values in 2 list


code:
def is_water_dwelling(animal):
    return animal in ('frog', 'fish')

def is_land_dwelling(animal):
    return animal in ('frog', 'human', 'giraffe')

animals = ('human', 'frog', 'fish', 'giraffe')

land_dwelling = (x for x in animals if is_land_dwelling(x))
water_dwelling = (x for x in animals if is_water_dwelling(x))

print list(land_dwelling)
print list(water_dwelling)

rawDescription: python: many to many groupings using iterator module

description: Python: many to many groupings using iterator module


code:
from kivy.adapters.listadapter import ListAdapter
from kivy.uix.listview import ListView, ListItemButton
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.app import App

Builder.load_string("""
<CellarDoor>:
    ToggleButton:
        text: 'Reverse'
        on_state: root.beer_sort((True if self.state=='down' else False))
#    ListView:
#        adapter: root.beer_la
""")

class CellarDoor(BoxLayout):
    def __init__(self, **kwargs):
        self.beer_archive = ["Item #{0}".format(i) for i in range(10)]
        self.beer_la = ListAdapter(data=self.beer_archive,
                          cls=ListItemButton,
                          selection_mode='single',
                          allow_empty_selection=True)

        super(CellarDoor, self).__init__(**kwargs)
        self.add_widget(ListView(adapter=self.beer_la))

    def beer_sort(self, reverse):
        self.beer_la.data = sorted(self.beer_archive,
                                   reverse=reverse)
        print self.beer_la.data

class TestApp(App):
    def build(self):
        return CellarDoor()

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

rawDescription: Kivy: Having trouble getting ListView to update when sorting ListAdapter data


code:
import itertools

key_func = lambda item: item["teamID"]

for key, group in itertools.groupby(sorted(batting, key=key_func), key_func):
    print key, sum([item["R"] for item in group])

rawDescription: Grouping in Python

description: Grouping in Python


code:
def my_generator(d):
    for key, value in d.items():
        yield {key: value}

d =  {'a': (3,1), 'b': (1,2), 'c': (1,1)}
for subdict in my_generator(d):
    print subdict

rawDescription: generate items from dictionary

description: Generate items from dictionary


code:
from Tkinter import *

main = Tk()

def leftKey(event):
    print "Left key pressed"

def rightKey(event):
    print "Right key pressed"

frame = Frame(main, width=100, height=100)
frame.bind('<Left>', leftKey)
frame.bind('<Right>', rightKey)
frame.focus_set()
frame.pack()
main.mainloop()

rawDescription: Tkinter - Can't bind arrow key events

description: Tkinter - Can't bind arrow key events


code:
from mechanize import Browser
from bs4 import BeautifulSoup

br = Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.2;\
                    WOW64) AppleWebKit/537.11 (KHTML, like Gecko)\
                    Chrome/23.0.1271.97 Safari/537.11')]

url = "https://encrypted.google.com/search?hl=en&q=site%3Asomedomain.com&start=0"

r = br.open(url)

soup = BeautifulSoup(r)

nextpage = soup.find("a", {"id": "pnnext"})
print nextpage['href']

rawDescription: Follow First 5 Pages of Google Results Python Mechanize


code:
# You'd need to replace the below with the appropriate code to open your file
file = """love, like, 200
love, like, 50
love, 20
say, claim, 30"""
file = file.split("\n")

words = {}
for line in file:
    word,count=line.rsplit(",",1)   # Note this uses String.rsplit() NOT String.split()
    words[word] = words.get(word,0) + int(count)
for word in words:
    print word,": ",words[word]

rawDescription: Merge and sum similar CSV entries

description: Merge and sum similar CSV entries


code:
import sys

try:
    Tfile = open("transitions2.txt", "r")
except IOError:
    print "Could not open file transitions2.txt"
    sys.exit()
Transitions = []

ReadLine = Tfile.readline()
while ReadLine != "":
    ReadLine = ReadLine.rstrip()
    CS, IN, NS = ReadLine.split(",")
    Transitions.append((CS, IN, NS))
    ReadLine = Tfile.readline()

print "Transitions:\n", Transitions, "\n"
Tfile.close()

try:
    Strfile = open("strings2.txt", "r")
except IOError:
    print "Could not open file strings2.txt"
    sys.exit()
Strings = []

ReadLine = Strfile.readline()
while ReadLine != "":
    Readline = ReadLine.rstrip()
    Strings.append(Readline)
    ReadLine = Strfile.readline()

print "Strings:\n", '\n'.join(Strings), '\n'
Strfile.close()

Start = 'State1'
Accept = 'State4'

for S in Strings:
    CurrentState = Start
    print "String:", S
    for C in S:
        print " Number:", C
        # find matching state and and input number
        for CS, IN, NS in Transitions:
            if CS == CurrentState and IN == C:
                CurrentState = NS  # make transition to next state
                print " NS ->", NS
                break

    if CurrentState == Accept:
        print " "+S, "Is accepted"
    else:
        print " "+S, "Doesn't work"
    print

rawDescription: Python State Machine: Resetting Loop?

description: Python State Machine: Resetting Loop


code:
import threading
import time
import inspect

class Thread(threading.Thread):
    def __init__(self, t, *args):
        threading.Thread.__init__(self, target=t, args=args)
        self.start()

count = 0
lock = threading.Lock()

def incre():
    global count
    caller = inspect.getouterframes(inspect.currentframe())[1][3]
    print "Inside %s()" % caller
    print "Acquiring lock"
    with lock:
        print "Lock Acquired"
        count += 1
        time.sleep(2)

def bye():
    while count < 5:
        incre()

def hello_there():
    while count < 5:
        incre()

def main():
    hello = Thread(hello_there)
    goodbye = Thread(bye)

if __name__ == '__main__':
    main()

rawDescription: Python threading. How do I lock a thread?


code:
var = 0
def main(number):
    print number
    inc = raw_input("Want to increase your variable?") # Or input() if python 3.x
    if inc == "yes":
        number += 1
        return main(number)
    if inc == "no":
        return number

var = main(var)
print var

rawDescription: Python Variable Additions without reset

description: Python Variable Additions without reset


code:
import numpy as np
for x, y in np.ndindex((3,2)):
    print x, y

rawDescription: Is there a Python equivalent of range(n) for multidimensional ranges?


code:
def print_content():
     print web.page().mainFrame().toHtml() # or toPlainText()
     # or
     # print web.page().currentFrame().toHtml() # or toPlainText()

rawDescription: PySide. Extracting DOM HTML. AccessNetworkmanager


code:
class MockDocument(object):

    def __init__(self):
        self.value = ''

    def write(self, *args):
        self.value += ''.join(str(i) for i in args)

class Global(PyV8.JSClass):
    def __init__(self):
        self.document = MockDocument()

scope = Global()
ctx = PyV8.JSContext(scope)
ctx.enter()
ctx.eval(js)
print scope.document.value

rawDescription: Executing Javascript from Python

description: Executing Javascript from Python


code:
if 'none,' in y[0]:
    y[0] = y[0].replace('none,','')
print y

rawDescription: Remove part of first row in a list

description: Remove part of first row in a list


code:
results = set()
df['text'].str.lower().str.split().apply(results.update)
print results

set(['someone', 'ft.jgt', 'my', 'is', 'to', 'going', 'place', 'nickname'])

rawDescription: Count distinct words from a Pandas Data Frame


code:
import re
a = "hello, i am 8 years old and have 12 toys"
numbers = re.compile(r'(\d+)')
a = numbers.sub(r'\\ref{\1}', a)
print a

rawDescription: python search string for numbers, put brackets around them

description: Python search string for numbers, put brackets around them


code:
def inc(lst):
    lst[0] += 1

a = 7
b = [a]
inc(b)
print b, a

rawDescription: Python procedure for changing an object

description: Python procedure for changing an object


code: print ''.join([ '\\x%02x'% ord(c) for c in a ])

rawDescription: Python - string of binary escape sequences as literal


rawDescription: Efficiently select subsection of numpy array


code:
for kind in numpy.int8, numpy.int16, numpy.int32, numpy.int64:
    for power in 1, 2, 5, 20:
        print kind, power, kind(10), kind(10)**power

rawDescription: numpy beginner array plain python vs. numpy vectors: faulty results

description: Numpy beginner array plain python vs. numpy vectors: faulty results


code:
import requests
import json
try:
    from packaging.version import parse
except ImportError:
    from pip._vendor.packaging.version import parse

URL_PATTERN = 'https://pypi.python.org/pypi/{package}/json'

def get_version(package, url_pattern=URL_PATTERN):
    """Return version of package on pypi.python.org using json."""
    req = requests.get(url_pattern.format(package=package))
    version = parse('0')
    if req.status_code == requests.codes.ok:
        j = json.loads(req.text.encode(req.encoding))
        if 'releases' in j:
            releases = j['releases']
            for release in releases:
                ver = parse(release)
                if not ver.is_prerelease:
                    version = max(version, ver)
    return version

if __name__ == '__main__':
    print "Django==%s" % get_version('Django')

rawDescription: PyPI API - How to get stable package version


code:
# merger.py
# this python script takes in a file with sorted lines and gives your output
import sys

prevtag = None
data = ['', '', '']
printed = True
for line in sys.stdin:
    fields = line.strip().split()
    if fields[0] != prevtag:
        if not printed:
            print prevtag, ' '.join(data)
            printed = True
        prevtag = fields[0]
        data = ['', '', '']
    if fields[1] == '<first>':
        data[0] = fields[2]
        printed = False
    if fields[1] == '<second>':
        data[1] = fields[2]
        printed = False
    if fields[1] == '<third>':
        data[2] = fields[2]
        printed = False
if not printed:
    print prevtag, ' '.join(data)

rawDescription: python how to efficiently group together terms

description: Python how to efficiently group together terms


code:
import sys
from StringIO import StringIO
sys.stdout = StringIO()
print 'Hello world!'
output = sys.stdout.getvalue()
sys.stdout = sys.__stdout__
output

rawDescription: Python 2.7 Keeping track of what's been printed


code:
def my_func():
    print 'got called'

rawDescription: Calling a Python function from C++

description: Calling a Python function from C++


code:
with open("yourFile") as fd:
    l = [x.strip().split() for x in fd.read().split("done")][:-1]
    print dict(zip(map(str,range(len(l))),l))

rawDescription: Python name variable from string

description: Python name variable from string


code:
import pymongo
conn = pymongo.Connection('localhost', 27017)
db = conn['databasename']

for item in db.collection1.find():
    _id = item['_id']
    item2 = db.collection2.find({'_id':_id})
    print "{}: {}, {}: {}, diff: {}, a>b?:{}".format(
        item['name'], item['price'], item1['name'],
        item1['price'], item['price'] - item1['price'],
        item['price'] > item1['price'])

rawDescription: Scraped data into two MongoDB collections--now how do I compare the results?


code:
print mystring
unicode(mystring)
mystring.decode('utf-8')

rawDescription: How to pass a variable to unicode/raw (ur"") conversion function

description: Pass a variable to unicode/raw (ur"") conversion function


code:
import cgi
inlist = '<>'
transform = cgi.escape(inlist)
print transform

rawDescription: Python Input Sanitization

description: Python Input Sanitization


code:
from multiprocessing import Process
import os

def info(title):  # For learning purpose, remove when you got the PID\PPID idea
    print title
    print 'module:', __name__
    print 'parent process:', os.getppid(),
    print 'process id:', os.getpid()

def f(name):      # Working function
    info('function f')
    print 'hello', name

if __name__ == '__main__':
    info('main line')
    processes=[Process(target=f, args=('bob-%d' % i,)) for i  in range(12)]
    [p.start() for p in processes]
    [p.join()  for p in processes]

rawDescription: Multithreaded thumbnail generation in Python


code:
for line in lines_gen:
    print line

rawDescription: How to read file N lines at a time in Python?

description: Read file N lines at a time in Python


code: print ' '.join(format(f, '.3f') for f in z)

rawDescription: Formatting a tuple of floats

description: Formatting a tuple of floats


code:
class _test(object): pass

import sys
ref = sys.modules['__main__']   # i will create another reference of this module.
sys.modules[__name__] = _test()   # Now it get overwrite but it will not be
                                  # delete because i still have a reference
                                  # to it ref.

print __name__, _test
# __main__ <class '__main__._test'>

rawDescription: Why is the value of __name__ changing after assignment to sys.modules[__name__]?


code:
import pickle
stats = pickle.load(open('stats.p', 'rb'))
print stats #{'hp': 150, 'speed': 15, 'mp': 100}

rawDescription: Save game feature for Python?

description: Save game feature for Python


code: print sum(l_item[0] for l_item in l)

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


code:
inlist = [1, 2, 2, 2, 3, 3, 1, 2, 3, 1, 1, 1, 1, 2, 1, 1,]
#         1                 1        1--2--3--4     1--2
def lifetime(num):
    output = []
    count = 0
    for x in inlist:
        if x == num:
            count += 1
        else:
            if count != 0:
                output.append(count)
                count = 0
    if count != 0:
        output.append(count)

    print output

lifetime(1)
lifetime(2)
lifetime(3)

rawDescription: counter in loop /list/ count same vale if they are one after another (python)

description: Counter in loop /list/ count same vale if they are one after another (python)


code:
import sys, json
from sqlalchemy import *
import requests

print "hello_worker initialized, connecting to database..."

payload = None
payload_file = None
for i in range(len(sys.argv)):
    if sys.argv[i] == "-payload" and (i + 1) < len(sys.argv):
        payload_file = sys.argv[i + 1]
        break

f = open(payload_file, "r")
contents = f.read()
f.close()

payload = json.loads(contents)

print "contents: ", contents
print "payload as json: ", payload

db_url = payload['WORKER_DB_URL']

print "connecting to database ", db_url

db = create_engine(db_url)
metadata = MetaData(db)

print "connection to the database established"

users = Table('users', metadata, autoload=True)
s = users.select()

#def run(stmt):
#    rs = stmt.execute()
#    for row in rs:
#        print row

#run(s)

callback_url = payload['WORKER_CALLBACK_URL']
print "task finished, sending post to ", callback_url
r = requests.post(callback_url)
print r.text

rawDescription: Ironworker job done notification

description: Ironworker job done notification


rawDescription: Creating sublist from a give list of items


code:
for k1, gp1 in df.groupby(df.A):
    print '1st key', k1
    for k2, gp2 in gp1.groupby(gp1.B):
        print '2nd key', k2
        print gp2

rawDescription: Iterating properly through groupby object


code:
nsmap = {'s': 'http://www.tei-c.org/ns/1.0'}

for line in tree.xpath('.//s:sp[@who="Barnardo"]/s:l/text()', namespaces=nsmap):
    print line.strip()

rawDescription: What is an ElementTree object exactly, and how can I get data from it?


code:
#!/usr/bin/env python

import json
with open('json.txt', 'r') as f:
    d = json.load(f)
missings = [None, ""]
def generate_missing_paths(path, item):
    if isinstance(item, list):
        for v in item:
            for current_path in generate_missing_paths(path, v):
                yield current_path
    if isinstance(item, dict):
        for k, v in item.iteritems():
            for current_path in generate_missing_paths(path + '.' + k, v):
                yield current_path
    if item in missings:
        yield path

for path in generate_missing_paths('root', d):
    print path

rawDescription: python parse Json for all null values

description: Python parse Json for all null values


code:
a=1.2
b='hi'
print "The number is {} and the text is {}".format(a, b)

rawDescription: printing variables in python using % and variable name

description: Printing variables in python using % and variable name


code:
from Tkinter import *

def key(event=None):
    print 'You pressed Ctrl+Shift+Tab'

root = Tk()

frame = Frame(root, width=100, height=100)
frame.focus_set()
frame.bind('<Control-Shift-KeyPress-Tab>', key)
frame.pack()

root.mainloop()

rawDescription: Tkinter keybinding for Control-Shift-Tab


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

urls = 'http://www.w3schools.com/xml/guestbook.asp', 'http://www.w3schools.com/xml/guestbook.asp'
xml_list = [requests.get(url).content for url in urls]

root1 = ET.fromstring(xml_list[0])
root2 = ET.fromstring(xml_list[1])

for element in root2.findall('guest'):
    root1.append(element)

print ET.tostring(root1)

rawDescription: Join XML strings pulled from the web using python


code:
tree = [Counter() for _ in range(3)]
tree[0] is tree[1]
tree[0] is tree[2]
for counter in tree: print id(counter)

rawDescription: Python list simultaneous update

description: Python list simultaneous update


code:
from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.w3schools.com/cssref/tryit.asp?filename=trycss_background")

driver.switch_to.frame("iframeResult")

body = driver.find_element_by_tag_name("body")

print driver.execute_script("""
var element = arguments[0],
    style = element.currentStyle || window.getComputedStyle(element, false);

return style['background-image'];
""", body)

rawDescription: How to get background-url in Python?

description: Get background-url in Python


code: print 'Loop ' + str(j) + ': ' + str(Array[i:j])

rawDescription: Understanding/implementing this enumeration solution for maximum subarray


code:
for u, p in zip(actual_user.values(), passwd.values()):
    if login == u and password == p:
        print "WELCOME !!"
        break
else:
    print "
Invalid Login
"

rawDescription: Unable to execute the else part of an if statment when conditon does not match in python


code:
import multiprocessing as mp

def f(name):
  print name

p = mp.Process(target=f, args=('bob',))
q = mp.Process(target=f, args=('jeff',))
p.start()
q.start()
p.join()
q.join()

rawDescription: Looking into getting into Python specifically for a project that involves threads and graphs


code:
import pandas as pd
from StringIO import StringIO

data = """ 90 ,  90 ,  A  ,     ,  A  ,  A
101 , 101 ,     ,     ,  T  ,  T
245 , 246 ,  AG ,  AG ,  AG ,  AG
302 , 302 ,  C  ,     ,     ,
324 , 324 ,     ,  G  ,     ,  G """

df = pd.read_csv(StringIO(data), header=None)
s = df.ix[:, 2:].max(axis=1)
df = df.ix[:, :1]
df["new"] = s
print df

rawDescription: Combine sparsely populated columns of the same data in pandas


code:
seenKeys = set()
for row in reader:
    if row[1] in seenKeys: continue

    seenKeys.add( row[1] )
    print ', '.join(row)

rawDescription: Python - remove duplicate lines with specific key


code:
print sorted.__doc__
lis=[1,2,3]
lis
list(lis)    #same result, but different object. (A shallow copy)

rawDescription: Python list tuples

description: Python list tuples


code:
import inquirer
questions = [
  inquirer.List('size',
                message="What size do you need?",
                choices=['Jumbo', 'Large', 'Standard', 'Medium', 'Small', 'Micro'],
            ),
]
answers = inquirer.prompt(questions)
print answers["size"]

rawDescription: How to let the user select an input from a finite list?

description: Let the user select an input from a finite list


code:
ts = [-2, -2, -4,  0, -1,  0, -1,  1, -2,  2, -3, -2,  3,  1, -2,
      -3, -4, -3,  3, -3, -3, -3, -1, -3,  3,  3, -4, -1, -1,  0]

in_draught = False
draught = []

for i in range(len(ts)-1):
    if in_draught and ts[i] > 0 and ts[i+1] > 0:
        in_draught = False
        draught.append(i)
    elif not in_draught and ts[i] <0 and ts[i+1] < 0:
        in_draught = True
        draught.append(i)
if in_draught:
    draught.append(len(ts)-1)

print [draught[i:i+2] for i in range(0,len(draught),2)]

rawDescription: Python - Find sequence in array: start when two values below threshold, end when two values above threshold


rawDescription: Adding delimeters and removing commas

description: Adding delimeters and removing commas


code:
from lxml.html import parse
from urllib2 import urlopen

url = 'http://www.cfilt.iitb.ac.in/indowordnet/first?langno=3&queryword=%E0%A6%97%E0%A6%BE%E0%A6%A7%E0%A6%BE'
tree = parse(urlopen(url))

for label in tree.xpath('.//label[@id="words"]/a'):
    print label.text

rawDescription: How to get a data miner to do specific search on web page?

description: Get a data miner to do specific search on web page


code:
print m; m.shape
(m.transpose() - v).transpose()

rawDescription: numpy subtract every row of matrix by vector

description: Numpy subtract every row of matrix by vector


code:
methname = int2meth.get(my_choice)
if methname is None:
    print 'Invalid choice, try again!'
else:
    getattr(self, methname)()

rawDescription: Python - Understanding __repr__

description: Python - Understanding __repr__


code:
import time
from functools import wraps

class Return(Exception):
    def __init__(self, value):
        self.value = value

def sleeper(func):
    """ Coroutine decorator that sleeps after every yield.

    Any yield inside a function decorated with sleeper will
    result in a 3 second sleep after the operation being
    yielded has run.

    """
    @wraps(func)
    def wrapper(*args, **kwargs):
        def execute(gen):
            try:
                x = next(gen)
                time.sleep(3)
                while True:
                    x = gen.send(x)
                    time.sleep(3)
            except (Return, StopIteration) as e:
                return getattr(e, "value", None)
        gen = func(*args, **kwargs)
        return execute(gen)
    return wrapper

def f():
    print "should sleep"
    return "abc"

def g(val):
    print "should also sleep"
    return "%s-def" % (val,)

def h():
    print "this won't sleep"
    return "ghi"

@sleeper
def test():
    z = yield f()
    print "hey there, got %s" % (z,)
    y = yield g(z)
    print "ok: %s" % (y,)
    l = h()
    print "see %s" % (l,)
    z = yield f()
    print "done %s" % z
    raise Return("all done") # You can use return "all done" if you have Python 3.x

if __name__ == "__main__":
    final = test()
    print "final is %s" % final

rawDescription: Sleeping after every Selenium function call


code:
stuff = r'property.${general.name}.ip = ${general.ip}'
stuff2 = r'''
general {
  name = myname
  ip = 127.0.0.1
}
'''

print ' stuff '.center(60, '=')
for tok in tokenize(stuff):
    print tok

print ' stuff2 '.center(60, '=')
for tok in tokenize(stuff2):
    print tok

rawDescription: Python - lexical analysis and tokenization


code:
import xml.etree.ElementTree as etree

data = """
<xml>
    <letter name="B">
        <letter name="D">
            <letter name="E">
                <letter name="F">
                    <letter name="G">

                    </letter>
                </letter>
            </letter>
        </letter>
    </letter>
</xml>
"""

XPATH = './/letter[@name="E"]'

tree = etree.fromstring(data)
letter = tree.find(XPATH)
parent = tree.find(XPATH + '/..')

parent.remove(letter)
parent.extend(letter)

print etree.tostring(tree)

rawDescription: Remove a node from etree but leaving child


code:
a1=[]
a2=[]
a1.append("abcdef")
x="abc"
new = ""
for item in a1:
    for letter in item:
        if letter not in x:
            new = new + letter

a2.append(new)

print a1, a2

rawDescription: Appending unique part of string to list

description: Appending unique part of string to list


code:
s = [1,4,2,3,5,4,5,6,7,8,1,3,4,5,9,10,11,42]
maxrun = -1
rl = {}
for x in s:
    run = rl[x] = rl.get(x-1, 0) + 1
    print x-run+1, 'to', x
    if run > maxrun:
        maxend, maxrun = x, run
print range(maxend-maxrun+1, maxend+1)

rawDescription: In python, how does one efficiently find the largest consecutive set of numbers in a list that are not necessarily adjacent?


code: print your_list

rawDescription: Solving the unicode output in Python

description: Solving the unicode output in Python


code:
for i in range(500):
    print " ".join('%8.1f' % 0 for j in range(16))
    print " ".join('%8.1f' % 0 for j in range(16))
    print " ".join('%8.1f' % 0 for j in range(4))

rawDescription: python writing txt file 500* 36 zero,then wrap lines in every 16

description: Python writing txt file 500* 36 zero,then wrap lines in every 16


code:
modelPerDate = df.groupby("date").apply(fit)
print modelPerDate

rawDescription: pandas groupby apply function that take N-column frame and returns object

description: Pandas groupby apply function that take N-column frame and returns object


code:
import Tkinter

# create the tcl interpreter
tcl = Tkinter.Tcl()

# define a python function
def pycommand(*args):
    print "pycommand args:", ", ".join(args)

# register it as a tcl command:
tcl_command_name = "pycommand"
python_function = pycommand
cmd = tcl.createcommand(tcl_command_name, python_function)

# call it, and print the results:
result = tcl.eval("pycommand one two three")
print "tcl result:", result

rawDescription: Using Python functions in Tkinter.Tcl()

description: Using Python functions in Tkinter.Tcl()


rawDescription: How to write a regex to match multiple file path

description: Write a regex to match multiple file path


code:
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait

try:
    element = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_id('user_first_name'))
    # do smth with the found element
except TimeoutException:
    print "Element Not Found"
    driver.close()

rawDescription: Selenium Python- If cannot find element, close everything and restart


code:
class BeginningPrinter(Protocol):
    def __init__(self, finished):
        self.finished = finished
        self.remaining = 1024 * 10
        self.buff = None

    def dataReceived(self, bytes):
        if self.buff is None:
            self.buff = bytes
        else:
            self.buff += bytes
        if self.remaining:
            display = bytes[:self.remaining]
            print 'Some data received:'
        print display
        self.remaining -= len(display)

    def connectionLost(self, reason):
        print 'Finished receiving body:', reason.getErrorMessage()
        self.finished.callback(self.buff)

rawDescription: How do I make a GET request and then print out the response body with Python (Twisted)

description: Make a GET request and then print out the response body with Python (Twisted)


code:
import numpy as np
import cv2

mx = np.array([[-1, 0, 1]])
my = np.array([[-1, 0, 1]]).T
im = np.array([[1, 2, 3, 4, 5],
               [2, 3, 4, 5, 6],
               [3, 4, 5, 6, 7],
               [4, 5, 6, 7, 8],
               [5, 6, 7, 8, 9],]).astype(np.uint8)

gx = cv2.filter2D(im, cv2.CV_32F, mx)
gy = cv2.filter2D(im, cv2.CV_32F, my)

print gx.shape
print gx.dtype
print gx

rawDescription: Non-sobel discrete gradients in python-Opencv or numpy


code:
import PyV8
ctx = PyV8.JSContext()
ctx.enter()
ctx.locals.a = [{'a':1}]
print ctx.locals.a
print ctx.eval("a.length")
print ctx.eval("a[0].a")
ctx.locals.blub = {'a':1}
print ctx.eval("blub.a")
print ctx.eval("Object.keys(blub)")
ctx.locals.blub = {'a':[1,2,3]}
print ctx.eval("Object.keys(blub)")
print ctx.eval("blub.a")
ctx.locals.blub2 = [{'a':[1,2,3]}]
print ctx.eval("blub2")
print ctx.eval("blub2.length")
print ctx.eval("Array.isArray(blub2)")
print ctx.eval("typeof(blub2)")
print ctx.eval("blub2[0].a")
print ctx.eval("typeof(blub.a)")
print ctx.eval("Array.isArray(blub.a)")

rawDescription: Converting Python objects to JavaScript for PyV8


code:
import numpy as np
import random
import itertools
import colorsys
hue, saturation, value = np.arange(0.0,1,0.05), np.arange(0.3,1.01,0.345), np.arange(0.3,1.01,0.345)
rlist= [colorsys.hsv_to_rgb(hue, saturation, value) for hue, saturation, value in
        itertools.product(random.sample(hue,len(hue)), random.sample(saturation, len(saturation)), random.sample(value, len(value)))]
print rlist

rawDescription: Suggestions for writing this small piece of code more elegantly


code:
def get_values_flexibly(file, keyword):
    values = []
    with open(file, 'r') as f:
        reader = csv.reader(f)
        for row in reader:
            for cell in row:
                if cell.endswith(' ' + keyword):
                    values.append(cell[:-len(keyword) - 1])
                elif cell.split(':')[0].strip() == keyword:
                    values.append(cell.split(':')[1].strip())
    return values

print get_values_flexibly(file, 'tissue')    # ['ab', 'bc  gf', 'bc  gf']
print get_values_flexibly(file, 'organism')  # ['human', 'human', 'human']

rawDescription: Calling data from a csv file with its header Python


code: print list(count_chars())

rawDescription: Get length of line in Java file using python


code: print [sum(i) for i in combinations(a, 2)]

rawDescription: Perform operation on every array element pair quickly


code:
from copy import deepcopy

list1 = [[1], [1]]
list2 = list1[:]   # while id(list1) != id(list2), it's items have the same id()s
list3 = deepcopy(list1)

list1[0] += [3]

print list1
print list2
print list3

rawDescription: Passed argument/parameter in function is still being changed after removing the reference/alias


code:
from jinja2 import Template

with open("table.jinja2") as f:
    templ = Template(f.read())
required = {"boto": "1.1.1", "plac": "2.2.3", "pyyaml": "3.3.3", "jinja2": "4.4.4"}
installed = {"boto": "1.1.1", "plac": "2.2.2", "pyyaml": "3.3.3", "jinja2": "4.4.4"}

packages = [(key, installed[key], required[key]) for key in sorted(installed)]

print templ.render(packages=packages)

rawDescription: Comparing Symmetric Dictionaries in Jinja2 template


code:
import pygame

def main():
    """
    Pygame Example
    """
    pygame.init()
    screen = pygame.display.set_mode((200, 200))
    app_running = True
    while app_running:
        # Get all key/mouse events from system.
        events = pygame.event.get()
        # Loop thru each event...
        for e in events:
            # Handle when the program is killed.
            if e.type == pygame.QUIT:
                app_running = False
                break
            # Handle key events.
            elif e.type == pygame.KEYDOWN:
                # Exit if escape is pressed.
                if e.key == pygame.K_ESCAPE:
                    app_running = False
                # Do something when the right arrow
                # is pressed.
                elif e.key == pygame.K_RIGHT:
                    print "right arrow pressed"
                # Do something when the left arrow
                # is pressed.
                elif e.key == pygame.K_LEFT:
                    print "left arrow pressed"
                # and so on ...
        # Fill the screen to blank it.
        #screen.fill(mycolor)
        # Write someting to the screen to display.
        #screen.blit(some_image, some_position)
        # Flip to display.
        #screen.flip()
    pygame.quit()

if __name__ == '__main__':
    main()

rawDescription: Simple UI to capture data

description: Simple UI to capture data


code:
import pandas as pd
df = pd.read_csv('data.csv',index_col=0, delim_whitespace=True)
df2 = df.applymap(lambda x: int(not pd.isnull(x)))
print df2.T.dot(df2)

rawDescription: Pairwise matrix from a pandas dataframe

description: Pairwise matrix from a pandas dataframe


code:
import sys
import numpy as np
import bottleneck as bn

N = 100
n = 12
r = np.sqrt(2)

x,y,z = np.meshgrid(*[np.arange(-N, N+1)]*3)
gridvectors = np.vstack((x,y,z)).reshape(3, -1)

def measure_time(func):
    import time
    def modified_func(*args, **kwargs):
        t0 = time.time()
        result = func(*args, **kwargs)
        duration = time.time() - t0
        print("%s duration: %.3f s" % (func.__name__, duration))
        return result
    return modified_func

@measure_time
def method_columnvecs(vectors):
    mod2bool = np.sum(vectors, axis=0) % 2 == 0
    vectorsubset = vectors[:,mod2bool]
    lengths = np.sqrt(np.sum(vectorsubset**2, axis=0))
    with np.errstate(divide='ignore'):
        p = (r/lengths)**n
    return  np.sum(p[np.isfinite(p)])

@measure_time
def method_columnvecs_opt(vectors):
    # On my system, bn.nansum is even slightly faster than np.sum.
    mod2bool = bn.nansum(vectors, axis=0) % 2 == 0
    # Use ss from bottleneck or scipy.stats (axis=0 is default).
    lengths = np.sqrt(bn.ss(vectors[:,mod2bool]))
    with np.errstate(divide='ignore'):
        p = (r/lengths)**n
    return  bn.nansum(p[np.isfinite(p)])

@measure_time
def method_original(x,y,z):
    ind = np.where((x+y+z)%2==0)
    x = x[ind]
    y = y[ind]
    z = z[ind]
    ind = np.where((x*x+y*y+z*z)!=0)
    x = x[ind]
    y = y[ind]
    z = z[ind]
    p=np.sqrt(x*x+y*y+z*z)/r
    return np.sum((1/p)**n)

@measure_time
def method_original_finitesum(x,y,z):
    ind = np.where((x+y+z)%2==0)
    x = x[ind]
    y = y[ind]
    z = z[ind]
    lengths = np.sqrt(x*x+y*y+z*z)
    with np.errstate(divide='ignore'):
        p = (r/lengths)**n
    return  np.sum(p[np.isfinite(p)])

print method_columnvecs(gridvectors)
print method_columnvecs_opt(gridvectors)
print method_original(x,y,z)
print method_original_finitesum(x,y,z)

rawDescription: 3D distance vectorization

description: 3D distance vectorization


code:
class Template(_Jinja2Template):
    """Fixes the incompabilites between Jinja2's template class and
    Django's.

    The end result should be a class that renders Jinja2 templates but
    is compatible with the interface specfied by Django.

    This includes flattening a ``Context`` instance passed to render
    and making sure that this class will automatically use the global
    coffin environment.
    """

    def __new__(cls, template_string, origin=None, name=None, source=None):
        # We accept the "origin" and "name" arguments, but discard them
        # right away - Jinja's Template class (apparently) stores no
        # equivalent information.
        from coffin.common import env

        try:
            return env.from_string(template_string, template_class=cls)
        except JinjaTemplateSyntaxError, e:
            raise _generate_django_exception(e, source)

    def __iter__(self):
        # TODO: Django allows iterating over the templates nodes. Should
        # be parse ourself and iterate over the AST?
        raise NotImplementedError()

    def render(self, context=None):
        """Differs from Django's own render() slightly in that makes the
        ``context`` parameter optional. We try to strike a middle ground
        here between implementing Django's interface while still supporting
        Jinja's own call syntax as well.
        """
        if not context:
            context = {}
        else:
            context = dict_from_django_context(context)

        try:
            return super(Template, self).render(context)
        except JinjaTemplateSyntaxError, e:
            raise _generate_django_exception(e)

def dict_from_django_context(context):
    """Flattens a Django :class:`django.template.context.Context` object.
    """
    if isinstance(context, DjangoContext):
        dict_ = {}
        # Newest dicts are up front, so update from oldest to newest.
        for subcontext in reversed(list(context)):
            dict_.update(dict_from_django_context(subcontext))
        return dict_
    else:
        return context

rawDescription: Jinja2 template compilation information

description: Jinja2 template compilation information


code:
def test(callback):
    print "Hello World"
    callback()

rawDescription: Running function from script it was imported by


code:
def add_old_setattr_to_class(cls):
    def __setattr__(self, attr, val):
        super_setattr = super(self.__class__, self).__setattr__
        if attr.startswith('old_'):
            super_setattr(attr, val)
        else:
            super_setattr('old_' + attr, getattr(self, attr))
            super_setattr(attr, val)
    cls.__setattr__ = __setattr__

class MyObject(object):
    def __init__(self):
        self.attr_one = None
        self.attr_two = 1

obj = MyObject()
add_old_setattr_to_class(obj.__class__)
obj.attr_one = 'new value'
obj.attr_two = 2

print obj.old_attr_one
print obj.attr_one
print obj.old_attr_two
print obj.attr_two

rawDescription: Track changes of atributes in instance. Python


code:
import psycopg2
from psycopg2 import extensions

conn = psycopg2.connect(user='scott', password='tiger', host='localhost', database='test')

cursor = conn.cursor()
cursor.execute("""
create table data (
    id SERIAL primary key,
    data varchar(500)
)
""")

cursor.executemany("insert into data (data) values (%(data)s)", [
        {"data":"abcdefghij" * 50} for i in xrange(10000)
    ])
cursor.close()

def one(conn):
    cursor = conn.cursor()
    cursor.execute("SELECT data FROM data")
    for row in cursor:
        row[0]

def two(conn):
    cursor = conn.cursor()
    extensions.register_type(extensions.UNICODE, cursor)
    cursor.execute("SELECT data FROM data")
    for row in cursor:
        row[0]

def three(conn):
    cursor = conn.cursor()
    cursor.execute("SELECT data FROM data")
    for row in cursor:
        row[0].decode('utf-8')

def four(conn):
    cursor = conn.cursor()
    def conv_unicode(value):
        return value.decode('utf-8')
    cursor.execute("SELECT data FROM data")
    for row in cursor:
        conv_unicode(row[0])

import timeit

print "no unicode:", timeit.timeit("one(conn)", "from __main__ import conn, one", number=100)

print "native unicode:", timeit.timeit("two(conn)", "from __main__ import conn, two", number=100)

print "in Python unicode:", timeit.timeit("three(conn)", "from __main__ import conn, three", number=100)

print "more like SQLA's unicode:", timeit.timeit("four(conn)", "from __main__ import conn, four", number=100)

rawDescription: Why does SQLAlchemy with psycopg2 use_native_unicode have poor performance?


code:
for s in re.finditer(r"([A-Z][a-z]+[. ]+)+([A-Z][a-z]+)?", text):
    print s.group(0)

rawDescription: How to find Title case phrases from a passage or bunch of paragraphs

description: Find Title case phrases from a passage or bunch of paragraphs


code: print len(urlopen(url).read())

rawDescription: urllib2.urlopen(): getting the size of the content

description: Urllib2.urlopen(): getting the size of the content


code:
for i in generate_with_padding('abcdefg', 10, '?'):
   print i

rawDescription: Iterating with padding

description: Iterating with padding


code:
searchfile = open("file.txt", "r")
for line in searchfile:
    if "searchphrase" in line: print line
searchfile.close()

rawDescription: Search a text file and print related lines in Python?


code:
x = 0

def functionA(x):
    if x is 0:
        return True
    else:
        return False

def functionB(bool):
    print bool
    if bool is True:
        print "Halleluhja"

bool = functionA(x)
functionB(bool)

print x, bool

rawDescription: Check boolean from another function

description: Check boolean from another function


code:
from pymongo import MongoClient

if __name__ == '__main__':
    db = MongoClient().test
    pipeline = [
        { "$unwind" : "$Properties" },
        { "$group": { "_id": "$Properties.type", "count": { "$sum": 1 } } }
    ]
    print list(db.collection.aggregate(pipeline))

rawDescription: Building a histogram from MongoDB with Pymongo


code:
import threading
import time
import random

class ActivePool(object):
    def __init__(self):
        super(ActivePool, self).__init__()
        self.active=[]
        self.lock=threading.Lock()
    def makeActive(self, name):
        with self.lock:
            self.active.append(name)
    def makeInactive(self, name):
        with self.lock:
            self.active.remove(name)
    def numActive(self):
        with self.lock:
            return len(self.active)
    def __str__(self):
        with self.lock:
            return str(self.active)
def worker(pool):
    name=threading.current_thread().name
    pool.makeActive(name)
    print 'Now running: %s' % str(pool)
    time.sleep(random.randint(1,3))
    pool.makeInactive(name)

if __name__=='__main__':
    poolA=ActivePool()
    poolB=ActivePool()
    jobs=[]
    for i in range(5):
        jobs.append(
            threading.Thread(target=worker, name='A{0}'.format(i),
                             args=(poolA,)))
        jobs.append(
            threading.Thread(target=worker, name='B{0}'.format(i),
                             args=(poolB,)))
    for j in jobs:
        j.daemon=True
        j.start()
    while threading.activeCount()>1:
        for j in jobs:
            j.join(1)
            print 'A-threads active: {0}, B-threads active: {1}'.format(
                poolA.numActive(),poolB.numActive())

rawDescription: python - how to get the numebr of active threads started by specific class?

description: Python - how to get the numebr of active threads started by specific class


code:
row = ['alpha', 'beta', 'gamma', 'delta', 'epsilon']

print row[2:4]

rawDescription: Python: How to iterate over specific columns in rows in text file


rawDescription: SQLAlchemy (sql) conditionnal query

description: SQLAlchemy (sql) conditionnal query


code:
from multiprocessing import Process
import time

class Starter(object):
    def __init__(self):
        self.state = False

x = Starter()

class EdgeRenderer(Process):
    def __init__(self,starter,*args,**kwargs):
        Process.__init__(self,*args,**kwargs)
        self.starter=starter
    def run(self):
        self.starter.state = "HAM SANDWICH"
        time.sleep(1)
        print self.starter.state

a = EdgeRenderer(x)
a.start()
x.state = True
a.join()
print x.state

rawDescription: Python Multiprocessing: Sending data to a process


code: print dict(d_out)

rawDescription: How to map dict into another dict Python

description: Map dict into another dict Python


code:
actions = {
    'normal' : {
        ' ' : lambda x: ('eating', ' '),
        '"' : lambda x: ('quoted', '"'),
        None: lambda x: ('normal', x)
    },
    'eating' : {
        ' ' : lambda x: ('eating', ''),
        '"' : lambda x: ('quoted', '"'),
        None: lambda x: ('normal', x)
    },
    'quoted' : {
        '"' : lambda x: ('normal', '"'),
        '\\': lambda x: ('escaped', '\\'),
        None: lambda x: ('quoted', x)
    },
    'escaped' : {
        None: lambda x: ('quoted', x)
    }
}

def reduce(s):
    result = ''
    state = 'eating'
    for ch in s:
        state, ch = actions[state].get(ch, actions[state][None])(ch)
        result += ch
    return result

s = '''that's my     string, "   keep these spaces     "    but reduce these '''
print reduce(s)

rawDescription: Python loop through string in nested for loops


code:
import config # This line just imports, does not print nothing
config.run()  # OTOH, this one prints the line...
try:
  sys.exit(1)
except SystemExit:
  try:
    config.run() # This one prints the line too
  except:
    print "errr"

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

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


code: print sorted(A, key=lambda n: n if isinstance(n, numbers.Number) else sys.float_info.min, reverse=True)

rawDescription: Sorting numbers in a list - Python

description: Sorting numbers in a list - Python


code:
def cmp_function(x, y):
    return cmp(len(x), len(y))

a = ['green egg','snail and lettuce','bacon','dorse naga','rutabaga ripple','cheese']
a.sort(cmp=cmp_function)
print a

rawDescription: Arranging number of strings using Python's lambda function


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

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


code:
def minavmax( X ):
    m = np.nanmin(X)
    M = np.nanmax(X)
    av = np.mean( X[ - np.isnan(X) ])  # masked ?
    histo = np.histogram( X, bins=5, range=(m,M) ) [0]
    return "min %.2g  av %.2g  max %.2g  histo %s" % (m, av, M, histo)

for nm, x in zip( "X Y Z  Xnew Ynew Znew".split(),
                (X,Y,Z, Xnew,Ynew,Znew) ):
    print nm, minavmax(x)

rawDescription: Problem with 2D interpolation in SciPy, non-rectangular grid


code:
dynamicInstance = Dynamic()
dynamicInstance.newProperty = 'newValue'
dynamicInstance['otherProperty'] = 'otherValue'

print [(name, dynamicInstance[name]) for name in dynamicInstance]

del dynamicInstance['newProperty']

print [(name, dynamicInstance[name]) for name in dynamicInstance]

del dynamicInstance.otherProperty

print [(name, dynamicInstance[name]) for name in dynamicInstance]

#output:
# [('otherProperty', 'otherValue'), ('newProperty', 'newValue')]
# [('otherProperty', 'otherValue')]
# []

rawDescription: Dynamic objects in Python like in AS3

description: Dynamic objects in Python like in AS3


code:
for name in files:
    with open(name, 'r') as f:
        for line in f:
            matches = re.findall("\d{2}-\d{8}", line)
            for mt in matches:
                print mt

rawDescription: Python Regex And encode

description: Python Regex And encode


code:
import itertools
lists = [
    ['loop01item01', 'loop01item02', 'loop01item03', 'loop01item04', 'loop0item05'],
    ['loop02item01', 'loop02item02', 'loop02item03', 'loop0item04', 'loop0item05'],
    ['loop03item01', 'loop03item02', 'loop03item03', 'loop03item04', 'loop03item05']
]

loops = int(raw_input("Enter the number of nested loops you want: "))
for elements in itertools.product(*lists[:loops]):
    print elements

rawDescription: All combinations with multiple loops

description: All combinations with multiple loops


code:
tables = soup.findAll("table")

for table in tables:
     if table.findParent("table") is None:
         print str(table)

rawDescription: Find all tables in html using BeautifulSoup


code:
from so.models import *
from django.contrib.auth.models import User
u1 = User()
u1.username='user1'
u1.save()
u2 = User()
u2.username='user2'
u2.save()
u3=User()
u3.username='user3'
u3.save()
# so we have 3 users now
r1 = Relationship()
r1.from_user=u1
r1.to_user=u2
r1.save()
r2=Relationship()
r2.from_user=u1
r2.to_user=u3
r2.save()
r3=Relationship()
r3.from_user=u2
r3.to_user=u3
r3.save()
rels = Relationship.objects.all()
rels.count()
# we have 3 relationships: user1 follows user2, user1 follows user3, user2 follows user3
users = User.objects.annotate(num_followers=Count('to_users')).order_by('-num_followers')
for user in users:
    print user.username, user.num_followers

rawDescription: Django - access foreign key data in an annotated query


rawDescription: Convert floating point array to integer array?


code:
import re
tag = 'unique'
# p = re.compile("<" + tag + ">([aA-zZ0-9\-\s:]*)</" + tag + ">")
# aA-zZ means a, range A-z and Z. this includes some sign like '[', '^' ... .
# the last '-' in character group( inside [ and ] ) matches '-' self.
p = re.compile("<" + tag + ">([a-zA-Z0-9\s:-]*)</" + tag + ">")

ml = "<unique>UNIQUE Passive - Maim:</unique> Basic attacks deal 10 bonus magic damage to monsters on hit.
<unique>UNIQUE Passive - Butcher:</unique> Damage dealt to monsters increased by 10%.

ique Passives with the same name don't stack.)"
m = p.search(ml)
print m.group()
print

print p.findall(ml)
print

for m in p.finditer(ml):
    print m.group()

rawDescription: Regular expression not finding both matches


code:
import types
def buildMethod(name, instance):
    def namefunc(self, *args, **kwargs):
        print "My name is %s" % name
        print "__called, args=%r, **kwargs=%r" % (args, kwargs)
    namefunc.__name__ = name
    setattr(instance, name, types.MethodType(namefunc, instance, type(instance)))

f = foo()
names = ["a","b","c"]
for name in names:
    buildMethod(name, f)

rawDescription: I want to be able to add function to a python class dynamically, not knowing their names in advance


code:
from collections import defaultdict
# inilialize necessary dict
dict_compute = defaultdict(list)

for tenants,instances in tenant_id_dict.iteritems():
    for single_ins in instances:
        compute_value_for_instance = ck.reports.get_total(tenant_id=tenants, service='compute', instance_id=single_ins)
        dict_compute[tenants].append({single_ins:compute_value_for_instance})
        print dict_compute

rawDescription: Dictionary and list value processing

description: Dictionary and list value processing


code:
expected = d + datetime.timedelta(days=1)
print expected

rawDescription: parse using dateutil

description: Parse using dateutil


code:
# FluffyBunny.py
from ctypes import *

# Declare a class that will be used for context info in calls to FluffyBunny()
class Rabbit:
    def __init__(self):
        self.count = 0

# FluffyBunny() wants a callback function with the following C prototype:
#     typedef int (*FLUFFYBUNNY_CALLBACK)(void *context);
FLUFFYBUNNY_CALLBACK = CFUNCTYPE(c_int, c_void_p)

# This DLL has been compiled with __cdecl calling convention.
FluffyBunny_dll = CDLL('FluffyBunny.dll')

# Get the function from the library. Its C prototype is:
#     int FluffyBunny(FLUFFYBUNNY_CALLBACK cb, void *context);
# Note that I use "py_object" instead of "c_void_p" for the context argument.
FluffyBunny          = FluffyBunny_dll.FluffyBunny
FluffyBunny.restype  = c_int
FluffyBunny.argtypes = [FLUFFYBUNNY_CALLBACK, py_object]

# Create Python version of the callback function.
def _private_enumerateBunnies(context):
    # Convert the context argument to a py_object, and extract its value.
    # This gives us the original Rabbit object that was passed in to
    # FluffyBunny().
    furball = cast(context, py_object).value
    # Do something with the context object.
    if furball:
        furball.count += 1
        print 'furball.count =', furball.count
        # Return non-zero as signal that FluffyBunny() should terminate
        return 0 if (furball.count < 10) else -1
    else:
        return -1

# Convert it to a C-callable function.
enumerateBunnies = FLUFFYBUNNY_CALLBACK(_private_enumerateBunnies)

# Try with no context info.
print 'no context info...'
result = FluffyBunny(enumerateBunnies, None)
print 'result=', result

# Give it a Python object as context info.
print 'instance of Rabbit as context info...'
furball = Rabbit()
result = FluffyBunny(enumerateBunnies, py_object(furball))
print 'result=', result

rawDescription: Back-casting a ctypes.py_object in a callback


code:
from time import time
from sqlalchemy import Column, String, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

Base = declarative_base()
class DataBase(object):
    def __init__(self, Base):
        self.Base = Base
        self.engine = create_engine('sqlite:///')
        self.session = sessionmaker()
        self.session.configure(bind=self.engine)
        self.s = self.session()
        self.Base.metadata.create_all(self.engine)

    def Add_Record(self, data):
        record = Listing(self.engine, **data)
        self.s.add(record)

    def Commit(self):
        self.s.commit()

    def Close(self):
        self.s.close()

class Empty_Init():
    def __init__(self, engine, **kwargs):
        #This class and the structure of the table need to be sync'd.
        for k,v in kwargs.items():
               if hasattr(self, k): #if the class already has the attribute initialized,
                setattr(self, k, v) # then assign it the new value
               else: # if it doesn't...
                if k not in engine.execute("SELECT * FROM `{tbl}`".format(tbl = self.__tablename__))._metadata.keys:
                    engine.execute("ALTER TABLE `{tbl}` ADD COLUMN {col} {typ}".format(
                                tbl = self.__tablename__,
                                col = k,
                                typ = "INT" if type(v) is int else ("DOUBLE" if type(v) is float else "VARCHAR")))
                setattr(self.__class__, k, Column(k, String))
                setattr(self, k, v)

class Listing(Empty_Init, Base):
    __tablename__ = 'Listings'
    __table_args__ = {'sqlite_autoincrement': True}
    id = Column(Integer, primary_key=True, nullable=False)
    make = Column(String)
    model = Column(String)
    year = Column(Integer)

t = time()

DBC = DataBase(Base)

data = {'make':'Chevy',
    'model' : 'Corvette',
    'year' : 1964,
    'price' : 50000}
DBC.Add_Record(data)

data = {'make':'Chevy',
    'model' : 'Camaro',
    'year' : 1967,
    'doors' : 2,
    'wheels_rwd' : 4.1,
    "test" : "bladads"}
DBC.Add_Record(data)
DBC.Commit()
DBC.Close()

print str(time() - t) + " s."

rawDescription: Wrap SQLAlchemy in Classes in Python

description: Wrap SQLAlchemy in Classes in Python


code:
import math

upto=1000000

sieve = [True] * upto
primes = set()

for n in xrange(2,upto):
    if sieve[n]:
        primes.add(n)
        for k in xrange(2,(upto+n-1)//n):
            sieve[k*n] = False

def good(e):
    w = str(e)
    for c in w:
        if c not in '1379':
            return False
    for i in xrange(1,len(w)):
        x = int(w[i:]+w[:i])
        if x not in primes:
            return False
    return True

print filter(good,primes)

rawDescription: Python Code Optimisation

description: Python Code Optimisation


code:
alist = ['\tfirst with tab', 'second item', 'third\titem\twith\ttabs']
print alist
print '\n'.join(alist)
from __future__ import print_function
print(*alist, sep='\n')

rawDescription: How to make the output shows tabs instead of showing \t?

description: Make the output shows tabs instead of showing \t


rawDescription: How to create array and initialize string values in python in one step?

description: Create array and initialize string values in python in one step


code:
data = [10, 20, 30]
lists = [[], [], []]

for i in range(len(data)):
    lists[i].append(data[i])

for lst in lists:
    print lst

rawDescription: Python: Appending items to lists by iterating through list of lists


code:
dbf = pysal.open("/path/to/shapefile.dbf", "r")
print dbf.header
FIPS = dbf.by_col("name_of_fips_code_column")
FIPS = map(str, FIPS) #make sure we're writing strings
out = open('fips.txt','w')
out.write('\n'.join(FIPS))
out.close()

rawDescription: Saving full QUEEN neighbor array to CSV in Pysal (Census Block Groups)


code:
#! /usr/bin/env python

import time

def timestr_to_secs(timestr):
    fmt = '%Y-%m-%d %H:%M:%S'
    time_struct = time.strptime(timestr, fmt)
    secs = time.mktime(time_struct)
    return int(secs)

timestrs = [
    '2013-09-03 17:34:04',
    '2013-09-03 17:34:05',
    '2013-09-03 17:35:04',
    '1970-01-01 00:00:00'
]

for ts in timestrs:
    print ts,timestr_to_secs(ts)

rawDescription: From date-time to usable value Python

description: From date-time to usable value Python


code:
conditions = ["fo","bar"]

s = "foo bar"
print all(x in s  for x in conditions)
True

rawDescription: Check if all list items exist in line(regex)


code:
with open("inputfile.txt") as myfile:
    for line_no,l in enumerate(myfile.readlines()):
        print "Line number",line_no
        for word_no,w in enumerate(l.split()):
            print "word number",word_no
            print "word: ",w

rawDescription: Python: printing a specific word on a specific line


code:
t0, t1 = pd.to_datetime(['9:00:00 AM', '02:00:00 PM']).time
print df[(df['Col3(time)'].dt.time > t0) & (df['Col3(time)'].dt.time < t1)]

rawDescription: Convert a column into time in pandas dataframe


code:
import types

class Human(object):
    def __getattribute__(self, attr):
        method = object.__getattribute__(self, attr)
        if not method:
            raise Exception("Method %s not implemented" % attr)
        if type(method) == types.MethodType:
             print "I am:"
        return method

    def eat(self):
        print "eating"

    def sleep(self):
       print "sleeping"

    def throne(self):
        print "on the throne"

John = Human()
John.eat()
John.sleep()
John.throne()

rawDescription: Python: Do something for any method of a class?


code:
from lxml import etree

for item in ["test1", "test2", "test3"]:
    quiz = etree.Element('quiz')

    etree.SubElement(quiz, 'stuff', attrib={"attr": item})
    print etree.tostring(quiz, pretty_print = True)
    print "---"

rawDescription: How to reset etree using lxml

description: Reset etree using lxml


code:
a = Counter(your_list)
print a['1']

rawDescription: Fastest way to count number of occurrences in a Python list


code:
import copy

class Ex(object):
    clvar='foo'
    def __init__(self,i):
        self.i=i

    def __repr__(self):
        return 'clvar={}, self.i={}'.format(self.clvar,self.i)

ex1=Ex(1)
ex2=Ex(2)

excopy1=copy.deepcopy(ex1)
excopy2=copy.deepcopy(ex2)

print ex1,ex2

print excopy1,excopy2

excopy1.i=22
excopy1.clvar='bar'

print ex1,excopy1

class Ex2(Ex):
    pass

ex2_2=Ex2(222)

print ex2_2

rawDescription: How can I clone a class?

description: Clone a class


code:
print os.getpid()
os.execl( '/usr/bin/gvim', 'gvim' )

rawDescription: background process in python with -e option on terminal

description: Background process in python with -e option on terminal


code:
#!/usr/bin/env python
import sys
from cStringIO import StringIO
from code import InteractiveConsole
from contextlib import contextmanager
from multiprocessing import Process, Pipe

@contextmanager
def std_redirector(stdin=sys.stdin, stdout=sys.stdin, stderr=sys.stderr):
    tmp_fds = stdin, stdout, stderr
    orig_fds = sys.stdin, sys.stdout, sys.stderr
    sys.stdin, sys.stdout, sys.stderr = tmp_fds
    yield
    sys.stdin, sys.stdout, sys.stderr = orig_fds

class Interpreter(InteractiveConsole):
    def __init__(self, locals=None):
        InteractiveConsole.__init__(self, locals=locals)
        self.output = StringIO()
        self.output = StringIO()

    def push(self, command):
        self.output.reset()
        self.output.truncate()
        with std_redirector(stdout=self.output, stderr=self.output):
            try:
                more = InteractiveConsole.push(self, command)
                result = self.output.getvalue()
            except (SyntaxError, OverflowError):
                pass
            return more, result

def myfunc(conn, commands):
    output = StringIO()
    py = Interpreter()
    results = ""

    for line in commands.split('\n'):
        if line and len(line) > 0:
            more, result = py.push(line + '\n')
            if result and len(result) > 0:
                results += result

    conn.send(results)
    conn.close()

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()

    commands = """
print "[42, None, 'hello']"

def greet(name, count):
    for i in range(count):
        print "Hello, " + name + "!"

greet("Beth Cooper", 5)
fugazi
print "Still going..."
"""
    p = Process(target=myfunc, args=(child_conn, commands))
    p.start()
    print parent_conn.recv()
    p.join()

rawDescription: How can I send python multiprocessing Process output to a Tkinter gui

description: Send python multiprocessing Process output to a Tkinter gui


code:
#/usr/bin/env python
import os

@outputSchema("ls:chararray}")
def list_files(x,f):
    #ls =  os.listdir('.')
    fin = open(f,'rb')
    return [x,fin.readlines()]

if __name__ == '__main__':
    print "ok"

rawDescription: Access a file in a python script that is stored in HDFS distributed cache


code:
for p in Purchase.objects.filter(ticker=ticker).get_with_todays_price(100):
  print p
  print p.gain

rawDescription: Adding dynamic fields to Django models

description: Adding dynamic fields to Django models


code:
ar = "0.212,1.231,0.112:1.001,3.212,0.002:0.002,0.0001,1.1"
x_data = ar.split(":")
x_final = 1
for i in x_data:
    x_each = i.split(",")
    x_add = 0             # Here you not initialize it
    for j in x_each:
        x_add = x_add + float(j)
    x_final = x_add * x_final

print x_final

rawDescription: for each gives different result?

description: For each gives different result


code:
from scipy.optimize import minimize

def objective_function(xs):
    """ Function to optimize. """
    x, y = xs
    return (x-1)**2 + (y-2)**4

def print_callback(xs):
    """
    Callback called after every iteration.

    xs is the estimated location of the optimum.
    """
    print xs

minimize(objective_function, x0 = (0., 0.), callback=print_callback)

rawDescription: print currently evaluated params during scipy minimization

description: Print currently evaluated params during scipy minimization


code:
arraysList = []
for i in range(0,3):
    arraysList.append([1,2,3])

print arraysList
# [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
print arraysList[1]
# [1, 2, 3]

rawDescription: How to rename variables in a loop in Python

description: Rename variables in a loop in Python


rawDescription: Multiple arguments for Python MySQLdb clause


code:
from bs4 import BeautifulSoup as soup, Tag

input = """David<span class="flag away"

    b
    99'

James<span class="flag home""""

web_soup = soup(input)
for t in web_soup.findAll(text='David'):
    for item in t.parent.next_siblings:
        if isinstance(item, Tag):
            if 'class' in item.attrs and 'name' in item.attrs['class']:
                break
            print item

rawDescription: BeautifulSoup return next sibling after using findAll(text=' ')


code:
import re

sequence_list = ['atgttttgatgATGTTTTGATTT',
                 'atggggtagatggggATGGGGTGA',
                 'atgaaataatggggATGAAATAA',
                 'aaggtacttctcggctaACTTTTTCCAAGT']

pat = '(atg).+?(?:TAA|TGA|TAG)'
reg = re.compile(pat,re.IGNORECASE)

def find_ORF_seq(fdna,messages=None,s=0,reg=reg):
    ORF_sequences = []
    if messages:
        print 's before == ',s
    while True:
        if messages:
            print ('---------------------------\n'
                   's == %d\n'
                   'fdna[%d:] == %r' % (s,s,fdna[s:]))
        ma = reg.search(fdna[s:])
        if messages:
            print 'reg.search(fdna[%d:]) == %r' % (s,ma)
        if ma:
            if messages:
                print ('ma.group() == %r\n'
                       'ma.group(1) == %r'
                       % (ma.group(),ma.group(1)))
            if ma.group(1)=='ATG':
                if messages:
                    print "ma.group(1) is uppercased 'ATG' then I break"
                break
            else:
                ORF_sequences.append(ma.group().upper())
                p = ma.start()
                if messages:
                    print (' The match is at position p == %d in fdna[%d:]\n'
                           ' and at position s + p == %d + %d == %d in fdna\n'
                           ' then I put s = s + p + 1 == %d'
                           % (p,s, s,p,s+p, s+p+1))
                s = s + p + 1
        else:
            break
    if messages:
        print '\n==== RESULT ======\n'
    return ORF_sequences

for fdna in sequence_list:
    print ('\n============================================')
    print ('fdna == %s\n'
           'ORF_sequences == %r'
           % (fdna, find_ORF_seq(fdna,True)))

###############################

print '\n\n\n######################\n\ninput sample'
fdna = 'atgttttgatggATGTTTGATTTATTTTAG'
print '  fdna == %s' % fdna
print '  **atgttttga**tggATGTTTGATTTATTTTAG'
print '  atgttttg**atggATGTTTGA**TTTATTTTAG'
print 'output sample'
print "  ORF_sequences = ['ATGTTTTGA','ATGGATGTTTGA']"

print '\nfind_ORF_seq(fdna) ==',find_ORF_seq(fdna)

rawDescription: Limiting re.findall() to record values before a certain number. Python


code:
from gevent import sleep, socket
from gevent.server import StreamServer

class Connection:
    def __init__(self, host, port):
        self.server = StreamServer((host, port), self.handle)
        self.data = []
        self.socks = []
        self.pointer = 0

    def handle(self, sock, address):
        self.socks.append(sock)
        while True:
            line = sock.recv(1024)
            if line:
                self.data += [line]
            else:
                break
        sock.close()
        self.socks.remove(sock)

    def send(self, msg):
        if self.socks:
            sock2send = self.socks[-1]
            try:
                sock2send.send(msg)
            except IOError, e:
                print "Can't send message '%s'! Exception:" % msg, e
        else:
            print "No sockets to send the message to"

    def start(self):
        self.server.start()

    def serve_forever(self):
        self.server.serve_forever()

    def close(self):
        self.server.stop()
        for sock in self.socks:
            sock.close()

    def new_data(self):
        newest = self.data[self.pointer:]
        self.pointer = len(self.data)
        return newest

rawDescription: Control TCP server in Python

description: Control TCP server in Python


rawDescription: How to read negative numbers from a file?

description: Read negative numbers from a file


rawDescription: Select highest value from python list of dicts


code:
import struct
print struct.unpack('i', struct.pack('I', 0xA867Df55))[0]

rawDescription: Replicating Javascript bitwise operation in Python


code:
import time

l = [1,2,4,5,3]

print "Run 1"
t1 = time.time()
print "Min =", min(l)
print "Max =", max(l)
print "time =", time.time() - t1
print ""
print "l =", l
print ""

l = [1,2,4,5,3]
l1 = list(l)

print "Run 2"
t1 = time.time()
l1.sort()
print "Min =", l1[0]
print "Max =", l1[-1]
print "time =", time.time() - t1
print ""
print "l =", l
print "l1 =", l1
print ""

l = [1,2,4,5,3]

print "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
print "time =", time.time() - t1
print ""
print "l =", l

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:
for i in sorted(s.keys()):
    print i,max(s[i])

rawDescription: Select and sort integers/strings from a text file in Python


code:
from random import shuffle

foo = []
foo.append([1, 2, 3])
foo.append([4, 5, 6])
foo.append([7, 8, 9])

shuffle(foo)
map(shuffle, foo)

print foo

rawDescription: How do I shuffle a multidimensional list in Python

description: Shuffle a multidimensional list in Python


code: for row in rows: print row[0].xpath('a/text()'), [img.attrib['alt'] for img in row[1].xpath('img')], row[2].text, row[3].xpath('a/text()'), row[4].text, row[5].text, row[6].text, row[7].text, row[8].xpath('span/text()'), row[9].text, [img.attrib['alt'] for img in row[10].xpath('img')], [img.attrib['alt'] for img in row[11].xpath('img')], [img.attrib['alt'] for img in row[12].xpath('img')], row[13].text, row[14].text

rawDescription: Extracting a table from web with different hierarchical tags


code:
import urllib2,sys
from BeautifulSoup import BeautifulSoup

address = 'http://www.presidency.ucsb.edu/ws/index.php?pid=29644&st=&st1=#axzz1fD98kGZW'
soup = BeautifulSoup(urllib2.urlopen(address).read())

span = soup.find("span", {"class":"displaytext"})  # span.string gives you the first bit
paras = [x.contents[0] for x in span.findAllNext("p")]  # this gives you the rest
# use .contents[0] instead of .string to deal with last para that's not well formed

print "%s\n\n%s" % (span.string, "\n\n".join(paras))

rawDescription: Scraping with BeautifulSoup and multiple paragraphs


code:
for entry in dom.cssselect('.downloads a'):
    document = {
        'title': entry.text_content(),
        'url': entry.get('href')
    }
    print document

rawDescription: Scraperwiki Python Loop Issue

description: Scraperwiki Python Loop Issue


code:
for i,page in enumerate(doc.xpath('/mw:mediawiki/mw:page',namespaces=NSMAP)):
  title = first(page.xpath('./mw:title/text()',namespaces=NSMAP))
  title_id = first(page.xpath('./mw:title_id/text()',namespaces=NSMAP))
  print "Page %s: %s (%s)"  % (i,title,title_id)

rawDescription: Reading XML file tags

description: Reading XML file tags


code:
if (count < 18):
    data = data + ","
    print data,
    count += 1
elif (count==19):
    print data
    count = 0

rawDescription: Python, code to csv. Troubles

description: Python, code to csv. Troubles


code:
def f(a, b, c=3, d=4):
    print a, b, c, d

rawDescription: Remove *args and **kwargs from method signatures using rope


code:
soup.find('tr', 'adr').text.strip()
print soup.find('tr', 'adr').text.strip()

rawDescription: Way to get contents of different children using beautifulSoup?


code:
a = []
b = a.append('hello')
print a
print b

rawDescription: Python: How to add tuple to an empty list?


code: print str.isdigit.__doc__

rawDescription: python if user input contains string

description: Python if user input contains string


code:
states = {
     'Alaska' : 'AK',
     'Alabama' : 'AL',}
astring = "I love Alaska"

for state in states:
     if state in astring:
        print astring.replace(state, states[state])

rawDescription: Search for Dictionary Values in String

description: Search for Dictionary Values in String


code:
import itertools

extra = itertools.cycle(['fL', 'fD', 'dep', 'AOV'])
for line in fileLines:
    for word in line.split():
        print word, next(extra)

rawDescription: Add in words after line split and loop it?


code:
import urllib
from bs4 import BeautifulSoup
HTML_Text = '''

Phone Number:	(701) 684-1390

Username:	Username here
'''
soup = BeautifulSoup(HTML_Text, 'html.parser')

td = soup.select('td')
for i,j in enumerate(td):
    if j.text == 'Username:':
        print td[i+1].text
        break

rawDescription: python regular expression to grab username froma webpage code

description: Python regular expression to grab username froma webpage code


code:
import csv

class MyDialect(csv.excel):
    delimiter = ';'

with open('in.csv', 'r') as f:
    reader = csv.reader(f, MyDialect())
    data = list(reader)

data = [[float(elem.replace(',', '.')) for elem in line] for line in data]

for line in data:
    print line

rawDescription: Decimal error in list

description: Decimal error in list


rawDescription: how to match all 3 digit except a particular number

description: Match all 3 digit except a particular number


code:
s = 'ABCD'

for i in range(10):
    s += unichr(0x2713 + i)

print s

rawDescription: How to iterate a Unicode string in Python?

description: Iterate a Unicode string in Python


rawDescription: Nicest way to pad zeroes to string

description: Nicest way to pad zeroes to string


code:
import re

SourceList = ['08/20/2014',
                 '10:04:27',
                 'pm',
                 'complete',
                 'vendor',
                 'per',
                 'mfg/recommend',
                 '08/20/2014',
                 '10:04:27',
                 'pm',
                 'complete']

OutputList = filter(
    lambda ThisWord: not re.match('^(?:(?:[0-9]{2}[:\/,]){2}[0-9]{2,4}|am|pm)$', ThisWord),
    SourceList)

for ThisValue in OutputList:
  print ThisValue

rawDescription: How to remove dates from a list in Python

description: Remove dates from a list in Python


code: print "the value of x is","foo"

rawDescription: printing string containing variables in python

description: Printing string containing variables in python


code:
from scrapy.item import BaseItem
from scrapy.contrib.loader import ItemLoader

class FlexibleItem(dict, BaseItem):
    pass

if __name__ == '__main__':
    item = FlexibleItem()
    loader = ItemLoader(item)

    loader.add_value('foo', 'bar')
    loader.add_value('baz', 123)
    loader.add_value('baz', 'test')
    loader.add_value(None, {'abc': 'xyz', 'foo': 555})

    print loader.load_item()

    if 'meow' not in item:
        print "it's not a cat!"

rawDescription: Scraping data without having to explicitly define each field to be scraped


code:
stx = '\xF7'  # hex(input("enter STX Value"))
device_id = hex(input("enter device id"))  # deviceid might also be fine
sub_id = hex(input("enter address of the Device and load details"))
comnd = hex(41)
data = hex(01)
eor_code = input("enter EOR Code")
add_sum = '\xF2'  # hex(input("Enter Add sum value"))

values  = (stx, device_id, sub_id, comnd, data, eor_code, add_sum)
print values  # tuple is a keyword - it's best to *not* override them if possible

rawDescription: Hex code using Python

description: Hex code using Python


code:
from animals import animals

print animals['CAT']['number_of_legs']

rawDescription: Grouping constants in python

description: Grouping constants in python


code:
gmt = pytz.timezone('GMT')
eastern = pytz.timezone('US/Eastern')
d = datetime.datetime(2014,03,21,7)
dateeastern = eastern.localize(d)
dateeastern
dategmt = dateeastern.astimezone(gmt)
dategmt

rawDescription: Incorrectly converting between Eastern and GMT time


code:
class Rectangle(object):
    def __init__(self, width, height):
        self.width = width
        self.height = height
    def area(self):
        return self.width * self.height

class ColoredRectangle(Rectangle):
    def __init__(self, rect, color):
        self.__dict__ = rect.__dict__
        self.color = color

rect = Rectangle(3, 5)
crect = ColoredRectangle(rect, color="blue")
print crect.width, crect.height, crect.color
#3 5 blue

rawDescription: Subclassing in python of instantiated superclass


code:
text = [ 'a', 'abcdef', 'abc' ]

longest = max( len(x) for x in text )

for x in text:
   print "| %*s |" % (longest, x)

print

for x in text:
   print "| %*s |" % (-longest, x)

rawDescription: columnize with ljust in python

description: Columnize with ljust in python


code:
num = 35
print write_roman(num)
# XXXV

num = 994
print write_roman(num)
# CMXCIV

num = 1995
print write_roman(num)
# MCMXCV

num = 2015
print write_roman(num)
# MMXV

rawDescription: Basic Python program to convert integer to Roman numerals?


code:
from itertools import combinations

def venn_count(named_sets):
    names = set(named_sets)
    for i in range(1, len(named_sets)+1):
        for to_intersect in combinations(sorted(named_sets), i):
            others = names.difference(to_intersect)
            intersected = set.intersection(*(named_sets[k] for k in to_intersect))
            unioned = set.union(*(named_sets[k] for k in others)) if others else set()
            yield to_intersect, others, len(intersected - unioned)

ns = {"a": {1,2,5,10,12}, "b": {1,2,6,9,12,15}, "c": {1,2,7,8,15}}
for intersected, unioned, count in venn_count(ns):
    print 'len({}{}) = {}'.format(' & '.join(sorted(intersected)),
                                  ' - ' + ' - '.join(sorted(unioned)) if unioned else '',
                                  count)

rawDescription: Counting intersections for all combinations in a list of sets


code:
str1 = "room no 135 chancery hotel,block k aecs layout"
for match in ['hsr layout','aecs layout','garden layout','k aecs layout']:
    if match in str1:
        print match
        break

rawDescription: Return Match with respect to Occurrence ,Regex


code:
import csv
with open('curr.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',')
    print spamreader.next()

rawDescription: Import csv as list in python

description: Import csv as list in python


code:
with open('/Users/settingj/Desktop/NOxMultiplier.csv') as f:
    a, b = zip(*csv.reader(f, delimiter='\t'))
print a
print b

rawDescription: Parsing with Python

description: Parsing with Python


code:
import itertools
c = itertools.count()
next(c)
next(c)
for i in itertools.islice(c, 5):
    print i

rawDescription: list with infinite elments

description: List with infinite elments


code:
test_string = 'A177T'
to_search = 'COADREAD ZNF271 Missense_Mutation MAAA-AA-3947 Q14591 A177T'
if test_string in to_search:
    print "found", test_string

rawDescription: creating and match token between two dictionaries

description: Creating and match token between two dictionaries


code:
import re

def nat_cmp(a, b):
    convert = lambda text: int(text) if text.isdigit() else text.lower()
    alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]

    return cmp(alphanum_key(a), alphanum_key(b))

print nat_cmp('foo10z', 'foo100z')
print cmp('foo10z', 'foo100z')  # <- notice the builtin yields a different result

rawDescription: python natural comparison between strings?

description: Python natural comparison between strings


code:
import cPickle
import timeit
class MyObject(object):
    def __init__(self, val):
            self.val = val
    def method(self):
            print self.val

timeit.timeit('cPickle.dumps(MyObject(100), 0)', 'from __main__ import cPickle, MyObject')
timeit.timeit('cPickle.dumps(MyObject(100), 1)', 'from __main__ import cPickle, MyObject')
timeit.timeit('cPickle.dumps(MyObject(100), 2)', 'from __main__ import cPickle, MyObject')

rawDescription: Why does it take longer to dump and load with pickle.HIGHEST_PROTOCOL?


code:
ss = 'one [two [three] ] end of line'
print re.sub(r'\[[^\][]*\]\s?','',ss)

rawDescription: Using regex to replace object within brackets in a text file


code:
class A:
    @staticmethod         #not required in py3.x
    def __private():
        print 'hello'

A._A__private()

rawDescription: private method in python

description: Private method in python


rawDescription: Multiple assignment using map function

description: Multiple assignment using map function


code:
class Example:
    @classmethod
    def print_stuff(cls, *args):
        print 'My *cls* is {}'.format(cls)
        print 'My *args* is {}'.format(args)

Example().print_stuff(1, 2, 3)
# My *cls* is __main__.Example
# My *args* is (1, 2, 3)

rawDescription: Function inside of Object generating different print output than stand-alone function in Python 3.5


code:
for r in range(1,10):
    for c in range(r):
        print c + 1,
    print ""

for r in range(1,10):
    for c in range(1, r + 1):
        print c,
    print ""

rawDescription: range() function is giving me trouble

description: Range() function is giving me trouble


code:
df1 = df.copy()

def date_convert(time):
    return time.date()

def hour_convert(time):
    return time.hour

def lamb(df):
    df['date'] = df.apply(lambda row: date_convert(pd.to_datetime(row['_sent_time_stamp'], unit='s')), axis=1)
    df['hour'] = df.apply(lambda row: hour_convert(pd.to_datetime(row['_sent_time_stamp'], unit='s')), axis=1)
    return df

def new(df):
    dat = pd.to_datetime(df['_sent_time_stamp'], unit='s')
    df['date'] = dat.dt.date
    df['hour'] = dat.dt.hour
    return df

print lamb(df)
print new(df1)

rawDescription: Unix time stamp conversion using pandas issue


code: print filter(lambda ticket: ticket["summary"] == issue["title"], tickets)

rawDescription: Find a dictionary in a list of dictionaries


code:
from string import maketrans   # Required to call maketrans function.
intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)
str = "this is string example....wow!!!";
print str.translate(trantab);

rawDescription: C# equivalence of python maketrans and translate


code:
import sys
print sys.argv

rawDescription: check_call and command line arguments on Windows

description: Check_call and command line arguments on Windows


code:
L = range(400)
size = 20
nested = [L[size*i : size*(i+1)] for i in xrange(len(L)/size)]
for sub in nested: print sub

rawDescription: divide the list into rows (create a nested list from a list)

description: Divide the list into rows (create a nested list from a list)


code:
import urllib2
import urllib
import sys
import json

LANG={
    "arabic":"ar", "bulgarian":"bg", "chinese":"zh-CN",
    "croatian":"hr", "czech":"cs", "danish":"da", "dutch":"nl",
    "english":"en", "finnish":"fi", "french":"fr", "german":"de",
    "greek":"el", "hindi":"hi", "italian":"it", "japanese":"ja",
    "korean":"ko", "norwegian":"no", "polish":"pl", "portugese":"pt",
    "romanian":"ro", "russian":"ru", "spanish":"es", "swedish":"sv" }

def translate(text,lang1,lang2):
    base_url='http://ajax.googleapis.com/ajax/services/language/translate?'
    langpair='%s|%s'%(LANG.get(lang1.lower(),lang1),
                      LANG.get(lang2.lower(),lang2))
    params=urllib.urlencode( (('v',1.0),
                       ('q',text.encode('utf-8')),
                       ('langpair',langpair),) )
    url=base_url+params
    content=urllib2.urlopen(url).read()
    try: trans_dict=json.loads(content)
    except AttributeError:
        try: trans_dict=json.load(content)
        except AttributeError: trans_dict=json.read(content)
    return trans_dict['responseData']['translatedText']

print translate("Good morning to you friend!", "English", "German")
print translate("Good morning to you friend!", "English", "Italian")
print translate("Good morning to you friend!", "English", "Spanish")

rawDescription: How can I deal with accented letters, german letters and other characters?

description: Deal with accented letters, german letters and other characters


code:
config = FixedConfig()
print config.get("domain names", "127.0.0.1")
#>> 'localhost'
config.set = super(FixedConfig,self).set  # swap FixedConfig.set with ConfigParser.set
config.set("domain names", "127.0.0.1", "CommandAndControl")
print config.get("domain names", "127.0.0.1")
#>> 'CommandAndControl'

rawDescription: Lock ConfigParser in read-only mode

description: Lock ConfigParser in read-only mode


code:
import Image
import ImageFile
import subprocess

tiff = 'test.tiff'
im = Image.open(tiff)
print 'size', im.size
try:
    print 'extrema', im.getextrema()
except IOError as e:
    print 'help!', e, '\n'

print 'I Get by with a Little Help from my Friends'
pbm_proc = subprocess.Popen(['tifftopnm', tiff],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)
(pbm_data, pbm_error) = pbm_proc.communicate()
ifp = ImageFile.Parser()
ifp.feed(pbm_data)
im = ifp.close()
print 'conversion message', pbm_error,
print 'extrema', im.getextrema()
print 'size', im.size
# houston: we have an image
im.show()

rawDescription: What is the best way to load a CCITT T.3 compressed tiff using python?


code:
some_list = [u'Data', u'Svoris', u'Perdavimo laikas',
    u'\u012evykio vietos adresas', u'Kvietimo prie\u017eastis']

print 'hello %s' % ', '.join(some_list)

rawDescription: Print without unicode symbol, but keep as unicode


code: print 'Value is "{}"'.format(value)

rawDescription: How to print variables without spaces between values

description: Print variables without spaces between values


code:
a = slices("aab", 2)
b = slices("abb", 2)
c = slices("abc", 2)
all = [a, b, c]
import operator
a_unique = reduce(operator.sub, (set(x) for x in all if x is not a), set(a))
# a_unique is only the keys so far
a_unique = dict((k, a[k]) for k in a_unique)
# now it's a dict of slice -> location(s)
print a_unique # {"aa": 0} or {"aa": [0]}
               # (depending on which slices function used)

rawDescription: Python, Huge Iteration Performance Problem


code:
foo = lambda x: pd.Series({'B_max':x.B.max(),'C_max':x.C[x.B.idxmax()]})
print df.join(df.groupby('A').apply(foo),on='A')

rawDescription: pandas df getting the value of a column associated with the max of a groupby

description: Pandas df getting the value of a column associated with the max of a groupby


code:
from openpyxl import *

#import the excel workbook
wb = load_workbook('input.xlsx')
#this should give you the sheet names
WSheets = wb.sheetnames

#iterate through sheets
for i in WSheets:
    ws = wb[i]
    #To access values from a particular column of the sheet, for example column B
    print ws['B']

rawDescription: Using openpxl and python to import a xlsx file


code: print __file__

rawDescription: How to determine file, function and line number?

description: Determine file, function and line number


code:
import pelican
print pelican.__file__

rawDescription: which is pelican executable script that user executes from shell?

description: Which is pelican executable script that user executes from shell


code:
# This is just the very beginning of a script that can be used to process
# arithmetic expressions.  At the moment it just defines a few classes
# and prints a couple example expressions.

# Possible additions include methods to evaluate expressions and generate
# some random expressions.

class Expression:
    pass

class Number(Expression):
    def __init__(self, num):
        self.num = num

    def __str__(self):
        return str(self.num)

class BinaryExpression(Expression):
    def __init__(self, left, op, right):
        self.left = left
        self.op = op
        self.right = right

    def __str__(self):
        return str(self.left) + " " + self.op + " "  + str(self.right)

class ParenthesizedExpression(Expression):
    def __init__(self, exp):
        self.exp = exp

    def __str__(self):
        return "(" + str(self.exp) + ")"

e1 = Number(5)
print e1

e2 = BinaryExpression(Number(8), "+", ParenthesizedExpression(BinaryExpression(Number(7), "*", e1)))
print e2

rawDescription: Is there a way to autogenerate valid arithmetic expressions?


code:
from PyKCS11 import *
import getpass
libacospkcs = '/usr/lib/libacospkcs11.so'

def createTokenAES256(label):
   pkcs11 = PyKCS11Lib()
   pkcs11.load(libacospkcs)
   theOnlySlot = pkcs11.getSlotList()[0]
   session = pkcs11.openSession(theOnlySlot, CKF_SERIAL_SESSION | CKF_RW_SESSION)
   PIN = getpass.getpass('Enter User PIN to login:')
   session.login(PIN)

   print pkcs11.getTokenInfo(theOnlySlot)

   template = (
      (CKA_CLASS, CKO_SECRET_KEY),
      (CKA_KEY_TYPE, CKK_AES),
      (CKA_VALUE_LEN, 32),
      (CKA_LABEL, label),
      (CKA_ID, "1244"),
      (CKA_PRIVATE, True),
      (CKA_SENSITIVE, True),
      (CKA_ENCRYPT, True),
      (CKA_DECRYPT, True),
      (CKA_TOKEN, True),
      (CKA_WRAP, True),
      (CKA_UNWRAP, True),
      (CKA_EXTRACTABLE, False))
   ckattr = session._template2ckattrlist(template)
   m = LowLevel.CK_MECHANISM()
   m.mechanism = LowLevel.CKM_AES_KEY_GEN

   key = LowLevel.CK_OBJECT_HANDLE()
   returnValue = pkcs11.lib.C_GenerateKey( session.session, m, ckattr, key)

   if returnValue != CKR_OK:
      raise PyKCS11Error(returnValue)

# Now execute the above to create AES256 key
createTokenAES256('TestKey')

rawDescription: Smartcard PKCS11 AES Key Gen Failure

description: Smartcard PKCS11 AES Key Gen Failure


code:
for r in filter(None, map(regex.match, str)):
    print r.groups()

rawDescription: pythonic way to rewrite an assignment in an if statement

description: Pythonic way to rewrite an assignment in an if statement


code:
from itertools import combinations,chain
from collections import Counter

def Mutation(SEQUENCES,conA):

    #mutations=map(lambda x:x.strip(),open('a.txt','r').readlines())

    mutation_combinations= chain.from_iterable([list(combinations(conA,i))for i in range(1,4)])
    #[('E2R',), ('C4W',), ('E6G',), ('E2R', 'C4W'), ('E2R', 'E6G'), ('C4W', 'E6G'), ('E2R', 'C4W', 'E6G')]

    for i in mutation_combinations:
        print "combination :"+'_'.join(i)
        c=Counter({})
        temp_string=SEQUENCES
        for j in i:
            c[j[1]]=j[2].lower()
        for index,letter in c.items():
            temp_string=temp_string[:int(index)-1]+letter+temp_string[int(index):]
        print temp_string

rawDescription: how to make two or more combinations of specific letter?

description: Make two or more combinations of specific letter


code:
from StringIO import StringIO
import pandas as pd

df = pd.DataFrame(
    {'correlation':[0.5, 0.1,0.9],
     'p_value':[0.1,0.8,0.01]})
strformat = '%f'
significant = lambda x: strformat % x if x<0.05 else str(x)
formatters = {'p_value': significant}

buf = StringIO()
df.to_html(buf, formatters=formatters, escape=False)
print buf.getvalue()

rawDescription: pandas.to_html() returning None

description: Pandas.to_html() returning None


code:
o = """{
   Return Code: 1
   Key        : <None>
   Files      : [
     {
       Return Code: 0
       Data       : 'uname' is not recognized as an internal or external command,
 operable program or batch file.

     }
   ]
 }"""
a = o.replace('\n', '')
import re
print re.findall('Data\s+:\s+(.+?)\}', a)[0].strip()

rawDescription: Create python map based on bash output

description: Create python map based on bash output


code:
for y in range(1, 11):
    for x in range(1, 11):
        # print the value followed by a single space
        print board[x, y],

    # print a new line
    print

rawDescription: Print an ordered dict info at specific lines in python


code:
initial_list=['C2', 'DA', 'DA', 'C6', 'D6']
print ['{:02X}'.format( int(i, 16) ^ 0xBA)  for i in initial_list]

rawDescription: How to XOR hex values in a list with another fixed hex value


code:
import urllib2
from bs4 import BeautifulSoup

url = 'http://www.homedepot.com/b/Husky/N-5yc1vZrd/Ntk-All/Ntt-chest%2Band%2Bcabinet?Ntx=mode+matchall&NCNI-5'
soup = BeautifulSoup(urllib2.urlopen(url))

for link in soup.select('div.product-image > a:nth-of-type(1)'):
    print link.get('href')

rawDescription: Beautifulsoup to retrieve the href list

description: Beautifulsoup to retrieve the href list


code:
import random

def main():
    graph_map = []
    max_nodes = 5
    for i in range(max_nodes):
        graph_map.append([])
    for i in range(max_nodes):
        for j in range(max_nodes-1-i):
            d = random.randint(1,5)
            graph_map[i].append(d)
            graph_map[i+j+1].append(d)

    print graph_map

if __name__ == '__main__':
    main()

rawDescription: Generating a map/graph for a traveling salesmanproblem (Python)