code:
#!/usr/bin/env python

import lxml
from lxml.html.clean import Cleaner

cleaner = Cleaner()
cleaner.javascript = True # This is True because we want to activate the javascript filter
cleaner.style = True      # This is True because we want to activate the styles & stylesheet filter

print "WITH JAVASCRIPT & STYLES"
print lxml.html.tostring(lxml.html.parse('http://www.google.com'))
print "WITHOUT JAVASCRIPT & STYLES"
print lxml.html.tostring(cleaner.clean_html(lxml.html.parse('http://www.google.com')))

rawDescription: Remove all javascript tags and style tags from html with python and the lxml module


code:
import numpy as np
import time

l = np.array([1.0, 2.0, 3.0])

def posFunc(x,y):
    return x*y

def negFunc(x,y):
    return x*(y+1)

def myFunc(x, y):
    if x > 0:
        return posFunc(x, y)
    if x < 0:
        return negFunc(x, y)
    else:
        return 1.0

myArray = np.array([
        [ 0.,0.,0.],
        [ 0.32, -6.79,  0.],
        [ 0.,0.,0.],
        [ 0.,1.5,0.],
        [ 0.,0., -1.71]])

t1 = time.time()
a = np.array([reduce(lambda x, (y,z): x*myFunc(z,l[y]), enumerate(x), 1) for x in myArray])
t2 = time.time()
print (t2-t1)*1000000
print a

rawDescription: Applying several functions to each row of an array


code:
def convertToDict(key, my_list):
    result_dict = {}
    for item in my_list:
        if item[0] == key:
            result_dict.setdefault(item[0], []).append(item[1])
    return result_dict

print convertToDict("id1", my_list)
# {'id1': ['name1', 'name2']}

rawDescription: How to extract from this list of tuples and convert into this dictionary?

description: Extract from this list of tuples and convert into this dictionary


code:
if __name__ == "__main__":
    config = {}
    execfile("example.conf", config)
    # python 3: exec(open("example.conf").read(), config)

    print config["value1"]
    print config["value5"]

rawDescription: Python configuration file: Any file format recommendation? INI format still appropriate? Seems quite old school


code:
import matplotlib.pyplot as plt
import numpy as np
import time

x = np.arange(0, 2*np.pi, 0.1)
y = np.sin(x)

fig, axes = plt.subplots(nrows=6)

fig.show()

# We need to draw the canvas before we start animating...
fig.canvas.draw()

styles = ['r-', 'g-', 'y-', 'm-', 'k-', 'c-']
def plot(ax, style):
    return ax.plot(x, y, style, animated=True)[0]
lines = [plot(ax, style) for ax, style in zip(axes, styles)]

# Let's capture the background of the figure
backgrounds = [fig.canvas.copy_from_bbox(ax.bbox) for ax in axes]

tstart = time.time()
for i in xrange(1, 2000):
    items = enumerate(zip(lines, axes, backgrounds), start=1)
    for j, (line, ax, background) in items:
        fig.canvas.restore_region(background)
        line.set_ydata(np.sin(j*x + i/10.0))
        ax.draw_artist(line)
        fig.canvas.blit(ax.bbox)

print 'FPS:' , 2000/(time.time()-tstart)

rawDescription: why is plotting with Matplotlib so slow?

description: Why is plotting with Matplotlib so slow


code:
from xlrd import open_workbook

wb = open_workbook('tmp.xls', formatting_info=True)
sheet = wb.sheet_by_name("1")
cell = sheet.cell(6, 0)
print "cell.xf_index is", cell.xf_index
fmt = wb.xf_list[cell.xf_index]
print "type(fmt) is", type(fmt)
print
print "fmt.dump():"
fmt.dump()

rawDescription: How to get Excel cell properties in Python

description: Get Excel cell properties in Python


code:
item_numN = int(raw_input("Please enter the number of items on your grocery list.\n"))
item_list = [str(raw_input("What is the item #" + str(count + 1) + " on your list?\n")) for count in range(item_numN)]

item_numB = int(raw_input("Please enter the number of items you bought.\n"))
item_bought = [str(raw_input("What is the item #" + str(count + 1) + " that you bought?\n")) for count in range(item_numB)]

items_needed = set(item_list) - set(item_bought)
print 'You still need {}.'.format(', '.join(items_needed))

rawDescription: Python: Grocery list/ comparing two sets of user input


code:
import os

def find_leaf_directories(top):
    for root, dirs, files in os.walk(top):
        if not dirs:    # if root has no sub-directories it's a leaf
            yield root

with open('list', 'w') as outfile:
    for dir in sorted(find_leaf_directories('./customers/')):
        print dir
        outfile.write(dir+"\n")

rawDescription: How to print only the subdirectory tree using python?

description: Print only the subdirectory tree using python


code:
import numpy as np
from nltk.probability import FreqDist
from nltk.classify import SklearnClassifier
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline

pipeline = Pipeline([('tfidf', TfidfTransformer()),
                     ('chi2', SelectKBest(chi2, k=1000)),
                     ('nb', MultinomialNB())])
classif = SklearnClassifier(pipeline)

from nltk.corpus import movie_reviews
pos = [FreqDist(movie_reviews.words(i)) for i in movie_reviews.fileids('pos')]
neg = [FreqDist(movie_reviews.words(i)) for i in movie_reviews.fileids('neg')]
add_label = lambda lst, lab: [(x, lab) for x in lst]
classif.train(add_label(pos[:100], 'pos') + add_label(neg[:100], 'neg'))

l_pos = np.array(classif.classify_many(pos[100:]))
l_neg = np.array(classif.classify_many(neg[100:]))
print "Confusion matrix:\n%d\t%d\n%d\t%d" % (
          (l_pos == 'pos').sum(), (l_pos == 'neg').sum(),
          (l_neg == 'pos').sum(), (l_neg == 'neg').sum())

rawDescription: Implementing Bag-of-Words Naive-Bayes classifier in NLTK


code:
import cv2
img = cv2.imread('sof.jpg') # load a dummy image
while(1):
    cv2.imshow('img',img)
    k = cv2.waitKey(33)
    if k==27:    # Esc key to stop
        break
    elif k==-1:  # normally -1 returned,so don't print it
        continue
    else:
        print k # else print its value

rawDescription: Using other keys for the waitKey() function of opencv


rawDescription: Python float overflow, what happens when float overflows?


code:
import gc
import operator
import random
import time

nk = 1000

def popcon(d):
  for x in xrange(nk*1000):
    d['id%s' % x] = random.randrange(100*1000)

def sorted_container():
  ctr = dict()
  popcon(ctr)
  start = time.time()
  ctr_sorted = ctr.items()
  ctr_sorted.sort(key=operator.itemgetter(1))
  stend = time.time()
  return stend-start, ctr_sorted

def do_update(ctr, newones):
  start = time.time()
  dicol = dict(ctr)
  ctr.extend((k,v) for (k,v) in newones if v!=dicol.get(k,None))
  dicnu = dict(newones)
  ctr.sort(key=operator.itemgetter(1))
  newctr = [(k,v) for (k,v) in ctr if v==dicnu.get(k,v)]
  stend = time.time()
  return stend-start, newctr

def main():
  random.seed(12345)
  for x in range(3):
    duration, ctr = sorted_container()
    print 'dict-to-sorted, %d: %.2f sec, len=%d' % (x, duration, len(ctr))
    newones = [('id%s' % y, random.randrange(nk*100))
                for y in xrange(nk*990,nk*1010)]
    duration, ctr = do_update(ctr, newones)
    print 'updt-to-sorted, %d: %.2f sec, len=%d' % (x, duration, len(ctr))
    del ctr
    gc.collect()

main()

rawDescription: Python: Zope's BTree OOSet, IISet, etc... Effective for this requirement?


code:
def cleanup(temp):
   return temp.strip().translate(None,",!@#$&").encode("utf-8")

address2='#4th avenue'
price='price is $45'

address2 = cleanup(address2)
price = cleanup(price)
print address2
print price

rawDescription: Parsing(removing extra space,encoding) multiple attributes through same funtion in python


code:
from  xml.dom.minidom import *
def codin(code):
    document = 'fourth.xml'
    xmldoc = parse(document)
    items = xmldoc.getElementsByTagName('items')
    kool = ""
    countryKool = {}
    for n in items:
        rv = getChild(n,'item')
        for v in rv:
            country = v.childNodes[0].nodeValue
            attr = v.getAttributeNode('name')
            if attr:
                kool = attr.nodeValue.strip()
                print "One of item is " , country, " and attribute is ",kool
                countryKool[kool] = country

    if code in countryKool:
        print "found"
    else:
        print "not found"

    print "Mapping of Country and kool ", countryKool #contains mapping for country and kool

def getChild(n,v):
    for child in n.childNodes:
        if child.localName==v:
            yield child

codin('001')

rawDescription: Compare node value from xml with String in Python


code:
def print_recursive(members, indent=''):
    for member in members:
        print "%s%s = %s"%(indent, member['name'], member['value'])
        print_recursive(member['members'], indent + '    ')

print_recursive(mydata)

rawDescription: Multidimensional List/Dict/whatever?

description: Multidimensional List/Dict/whatever


code:
from Tkinter import Tk, Button

def run_command():
    machines_off = []
    # Some stuff .....
    machineName = "foo"
    machines_off.append(machineName)
    # Some stuff ....
    wol_machines(machines_off)

def wol_machines(machines_off):
    print "wol_machines was called"
    print "contents of machines_off: ", machines_off
    # Some stuff ....

root = Tk()
a = Button(text="do the first thing", command=run_command)
b = Button(text="do the second thing", command=wol_machines)
a.pack()
b.pack()

root.mainloop()

rawDescription: Sharing list between functions

description: Sharing list between functions


code:
# Input data
master = 'animals_goat=1; country=3 4 5; Foo=1 2 3;'
lines = """country=0 1 2; Foo=0 0 0;
country=0 0 0; Foo=1 1 1;"""

# Extract info from master string
items = [s.strip().split('=') for s in master.split(';') if s]
keys = [item[0] for item in items]

# Process input lines, one at a time
for line in lines.splitlines():
    d = dict(items)
    new = dict(s.strip().split('=') for s in line.split(';') if s)
    d.update(new)
    for k in keys:
        print '%s=%s;' % (k, d[k]),
    print ""

rawDescription: String Manipulation in python, different length


code:
from script1 import creator

for each in creator.cars:
     print car.color

rawDescription: Create and retrieve object list in Python enhanced


code:
[array(3L, dtype=int64)]
[array(1L, dtype=int64)]
[array(0L, dtype=int64)]
[array(0L, dtype=int64)]
[array(0L, dtype=int64)]

rawDescription: How to handle empty sequence with theano.scan()?

description: Handle empty sequence with theano.scan()


code:
import urlparse
import os

url = "http://www.example.com/category/location/filename"

url_path = urlparse.urlparse(url).path
local_filename = os.path.join(r'd:', os.sep, 'links', os.path.normpath(url_path)[1:]) + ".html"
print local_filename

rawDescription: How to mention the local drive path to the url using python-2.7

description: Mention the local drive path to the url using python-2.7


code:
import numpy as np
a = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6], dtype=np.float)
print np.where(a < 0.5, 1 / a * (-1), a)

rawDescription: Python: numpy/pandas change values on condition


code:
from bs4 import BeautifulSoup

s = '
Go to 1, Go to page 2Go to page 3, Go to page 4
'
soup = BeautifulSoup(s)
print [el.string for el in soup('a')]
# [u'Go to 1', u'Go to page 2', u'Go to page 3', u'Go to page 4']

rawDescription: Getting all instances of a regular expression in python with


code:
import csv

original = file('southpark.csv', 'rU')
reader = csv.reader(original)

for row in reader:
    #will print each row by itself (all columns from names up to what they wear)
    print row
    print "-----------------"
    #will print first column (character names only)
    print row[0]

rawDescription: How to Store Rows from CSV File into Python and Print Data with HTML


code:
def do_this_weird_thing(a_list):
    from itertools import groupby
    for grp,item in groupby(sorted(a_list)):
        item = list(item)
        yield [grp] * (len(item)//2)

from itertools import chain
print list(chain.from_iterable(do_this_weird_thing([2,0,2,4])))

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

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


code:
from aaa import AA
print AA
# Prints Foo

rawDescription: I want to refer to a variable in another python script


code:
columns = 7
for i in range(1, 32):
 if i % columns != 0:
  print i,
 else:
  print i

rawDescription: Print a row of numbers and then have it make a new line and continue counting


code: print a

rawDescription: numpy - issue with creating a 2D matrix

description: Numpy - issue with creating a 2D matrix


code:
def four_digit_escape(string):
    return u''.join(char if 32 <= ord(char) <= 126 else u'\\u%04x'%ord(char) for char in string)

symbol = u"hello ¥"
print symbol.encode('unicode_escape')
print four_digit_escape(symbol)

rawDescription: Convert a symbol to its 4 digit unicode escape representation and vice versa


code:
import re

dateRegex = re.compile(r'\w{3}\s\d\d?\s\d\d:\d\d:\d\d')

f = open("logfile.log","r")

for line in f.readlines():
    matches = re.match(dateRegex,line)
    if matches:
        print matches.group()

rawDescription: printing dates in a log file

description: Printing dates in a log file


code:
def synchronized(wrapped):
    lock = threading.Lock()
    @functools.wraps(wrapped)
    def _wrap(*args, **kwargs):
        print "Calling '%s' with Lock %s" % (wrapped.__name__, id(lock))
        with lock:
            return wrapped(*args, **kwargs)
    return _wrap

rawDescription: How to create a synchronized function across all instances

description: Create a synchronized function across all instances


rawDescription: Splitting a number pattern

description: Splitting a number pattern


code:
from collections import defaultdict
import xml.etree.ElementTree as ET

data = """<root>
    <article_date>09/09/2013
    <article_time>1
        <article_name>aaa1</article_name>
        <article_link>1aaaaaaa</article_link>
    </article_time>
    <article_time>0
        <article_name>aaa2</article_name>
        <article_link>2aaaaaaa</article_link>
    </article_time>
    <article_time>1
        <article_name>aaa3</article_name>
        <article_link>3aaaaaaa</article_link>
    </article_time>
    <article_time>0
        <article_name>aaa4</article_name>
        <article_link>4aaaaaaa</article_link>
    </article_time>
    <article_time>1
        <article_name>aaa5</article_name>
        <article_link>5aaaaaaa</article_link>
    </article_time>
    </article_date>
</root>
"""

tree = ET.fromstring(data)

root = ET.Element('root')
article_date = ET.SubElement(root, 'article_date')
article_date.text = tree.find('.//article_date').text

data = defaultdict(list)
for article_time in tree.findall('.//article_time'):
    text = article_time.text.strip()
    name = article_time.find('./article_name').text
    link = article_time.find('./article_link').text
    data[text].append((name, link))

for time_value, items in data.iteritems():
    article_time = ET.SubElement(article_date, 'article_time')
    article_name = ET.SubElement(article_time, 'article_name')
    article_link = ET.SubElement(article_time, 'article_name')

    article_time.text = time_value
    article_name.text = '+'.join(name for (name, _) in items)
    article_link.text = '+'.join(link for (_, link) in items)

print ET.tostring(root)

rawDescription: Merge child nodes with the similar parent node, xml, python


code:
r = requests.get(url)
if "text/html" in r.headers["content-type"]:
    html = r.text
else:
    print "non html page"

rawDescription: Python-requests: Check if URL is not HTML webpage


code:
from lxml import etree

xmlstr = """
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns="http://purl.org/rss/1.0/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/"
    xmlns:admin="http://webns.net/mvcb/"
    xmlns:syn="http://purl.org/rss/1.0/modules/syndication/">
    <channel rdf:about="http://somelink.com">
        <!-- ordinary stuff goes here -->
    </channel>
    <item rdf:about="http://www.some/random/link/123">
        <title>title</title>
        <link>
        http://www.some/random/link/123
        </link>
        <description>
            <![CDATA[
                ..description..
                ]]>
        </description>
        <dc:date>the date</dc:date>
    </item>
</rdf:RDF>"""

xmldoc = etree.fromstring(xmlstr)
nsmap = {"purl": "http://purl.org/rss/1.0/"}
res = xmldoc.xpath("//purl:item", namespaces=nsmap)
print res

print "xml", etree.tostring(res[0])

rawDescription: problems with xpath in python using lxml on xml file

description: Problems with xpath in python using lxml on xml file


code:
from BeautifulSoup import BeautifulSoup
from urllib2 import urlopen
html = urlopen('http://investing.businessweek.com/research/stocks/private/person.asp?personId=668561&privcapId=160900&previousCapId=285930&previousTitle=LOEWS%20CORP')
soup = BeautifulSoup(html)
headings = soup.findAll('h2', text='BACKGROUND')
for section in headings:
    paragraph = section.findNext('p')
    if paragraph and paragraph.string:
        print '> ', paragraph.string
    else:
        print '> ', section.parent.next.next.strip()

rawDescription: problems ... BeautifulSoup Parsing

description: Problems ... BeautifulSoup Parsing


rawDescription: How to Send Packets to a Remote Minecraft Classic Server in Python?


code:
from bs4 import NavigableString

for line in soup.find_all('ol'):
    result = []
    for descendant in line.li.descendants:
        if isinstance(descendant, NavigableString):
            result.append(unicode(descendant).strip())
        elif descendant.name == 'dl':
            break

    print u' '.join(result)

rawDescription: Python/BeautifulSoup - How can I extract text between <li> and <dl> tags


code:
def f(i,x,numbers):
    if (i < x):
        print "At the top i is %d" %i
        numbers.append(i)

        i += 1
        print "Numbers now: ", numbers
        print "At the bottom i is %d" %i
        f(i,x,numbers)
    return numbers

numbers = f(0,6,[])
for num in numbers:
    print num

rawDescription: Converting from While Loop Structure to Function Call


code:
import os
import re
import datetime as dt

pat=re.compile(r'^Data_(A|B)_(\d{4}-\d{2}-\d{2}_\d+-\d+-\d+-\d+)')

fnames={}
for fn in os.listdir(source):
    p=os.path.join(source, fn)
    if os.path.isfile(p):
        m=pat.search(fn)
        if m:
            d=round_time(dt.datetime.strptime(m.group(2), '%Y-%m-%d_%H-%M-%S-%f'), round_to=60)
            fnames.setdefault(str(d), []).append((p, m.group(1)))

print fnames

rawDescription: Python: Compare Filenames in Folder

description: Python: Compare Filenames in Folder


code:
import maya.cmds as cmd

def func():
    print cmd.ls()

# we want this to execute on import
func()

rawDescription: getting list of camera or render globals from maya scene file without opening scene in maya

description: Getting list of camera or render globals from maya scene file without opening scene in maya


code:
# create a dict
d = {}
# add tuple as key with some value
d[('some domain', 'some date')] = 'some ip'
print d

rawDescription: How can I add a dictionary inside an existing dictionary ,Python

description: Add a dictionary inside an existing dictionary ,Python


code:
target = 'Element 1'
for line in lines:
    if target in line:
        print line
        break #exit from loop

rawDescription: Search for a word in elements of list, and copy the whole element string and print


code:
import re
from BeautifulSoup import BeautifulSoup

html = '''Text:

        url'''

soup = BeautifulSoup(html)
label = soup.find("strong" , text='Text:')
contact = label.findNext('a')

if contact.get('href') != None:
    print contact
else:
    print "No href"

rawDescription: Beautiful Soup - handling errors

description: Beautiful Soup - handling errors


code:
def N(d, memoize = dict()):
    if d == 1 or d == 2: return 1
    if d in memoize: return memoize[d]
    result = 0
    for i in xrange(1, d):
        result += (d - i) * (i) * N(d - i) * N(i)
    memoize[d] = result
    return result

print N(1000)

rawDescription: Coding summation formula and recursive function


code:
# foo.py
import sys

print sys.argv

rawDescription: Run Python program from shell and keep variables in scope


code:
import types
from functools import partial

class APIWrapper(object):
    def __init__(self, apicls, instance):
        self._apicls = apicls
        self._instance = instance
    def __getattribute__(self, attr):
        apicls = object.__getattribute__(self, "_apicls")
        instance = object.__getattribute__(self,"_instance")
        obj = getattr(apicls, attr)
        if isinstance(obj, types.MethodType):
            return partial(obj,instance)
        return obj

class APIProperty(object):
    def __init__(self, cls):
        self.cls = cls
    def __get__(self, instance, cls):
        return APIWrapper(self.cls, instance)

class Y(object):
    @classmethod
    def method(cls, x, *args):
        print cls, x, args

class X(object):
    Y = APIProperty(Y)

#Example usage:
x = X()
x.Y.method(1,2,3)

rawDescription: Partially Evaluating Python Classmethod Based on Where It's Accessed From


code:
def find_repeats(L, num_repeats):
    idx = 0
    while idx < len(L):
        if [L[idx]]*num_repeats == L[idx:idx+num_repeats]:
            L[idx:idx+num_repeats] = [True]*num_repeats
            idx += num_repeats
        else:
            idx += 1
    return L

L=[1,1,1,4,6]
print find_repeats(L, 2)
L=[1,1,1,4,6]
print find_repeats(L, 3)

rawDescription: Python finding n consecutive numbers in a list


code:
if string_name.startswith(multi[:3]):
    print 'test'

rawDescription: If one or more letters equals the same as a variable; Python


code:
import re
p = re.compile(ur'(?<=time=)\S+')
test_str = u"\n\n Pinging 10.1.1.1 with 32 bytes of data:\n\n Reply from 10.1.1.1: bytes=32 time=39ms TTL=253\n\n Reply from 10.1.1.1: bytes=32 time=17ms TTL=253\n\n Reply from 10.1.1.1: bytes=32 time=17ms TTL=253\n\n Reply from 10.1.1.1: bytes=32 time=17ms TTL=253\n\n Ping statistics for 10.1.1.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 17ms, Maximum = 39ms, Average = 22ms\n"
re.findall(p, test_str)

rawDescription: Python - Return some characters after phrase in string


code:
x = mySet(['item1','item2','item3'])
print x

rawDescription: Writing a Set To a File in Python

description: Writing a Set To a File in Python


code:
M = [['T', 'T'], ['F', 'F'], ['T', 'T']]             #sample values
N = [['F', 'F', 'F', 'F'], ['T', 'T', 'T', 'T']]

T = 1
F = 0

P = []

for i in range(len(M)):                              #result matrix creation
    P.append([None] * len(N[0]))

for i in range(len(M)):
    for j in range(len(N[0])):
        P[i][j] = 0                                  #initialization
        for k in range(len(N)):
            P[i][j] |= eval(M[i][k]) & eval(N[k][j]) #logical operators
        P[i][j] = 'T' if P[i][j] else 'F'            #conversion back to 'T' and 'F'

print P

rawDescription: asked to calculate the logical matrix P=mn

description: Asked to calculate the logical matrix P=mn


code:
class Decor(object):
    def __init__(self, func):
         self.func = func

    def __get__(self, instance, owner):
        if instance is None:
            return self
        d = self
        # use a lambda to produce a bound method
        mfactory = lambda self, *args, **kw: d(self, *args, **kw)
        mfactory.__name__ = self.func.__name__
        return mfactory.__get__(instance, owner)

    def __call__(self, instance, *args, **kwargs):
        def closure(*args, **kwargs):
            print instance, args, kwargs
            return self.func(instance, *args, **kwargs)
        return closure(*args, **kwargs)

rawDescription: Callable object decorator applied to method doesn't get self argument on input


code:
class Meta(type):
    def shadowed(cls):
        print "Meta.shadowed() on %s" % (cls.__name__,)
    def unshadowed(cls):
        print "Meta.unshadowed() on %s" % (cls.__name__,)

class Foo(object):
    __metaclass__ = Meta

    def shadowed(self):
        print "Foo.shadowed()"

class Bar(object):
    __metaclass__ = Meta

Bar.unshadowed()                   #Meta.unshadowed() on Bar
Bar.shadowed()                     #Meta.shadowed() on Bar
Foo.unshadowed()                   #Meta.unshadowed() on Foo
#Foo.shadowed()                    #TypeError
Meta.shadowed.__get__(Foo, Meta)() #Meta.shadowed() on Foo

rawDescription: How to get the bound method derived from a metaclass when the class has a method of the same name

description: Get the bound method derived from a metaclass when the class has a method of the same name


code:
import argparse

class UniqueAppendAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        unique_values = set(values)
        setattr(namespace, self.dest, unique_values)

servers = ["ApaServer", "BananServer", "GulServer", "SolServer",
           "RymdServer", "SkeppServer", "HavsServer", "SovServer" ]
parser = argparse.ArgumentParser()
parser.add_argument('-o', '--only', nargs='*', choices=servers, action=UniqueAppendAction,
                    help='Space separated list of case sensitive server names to process')
print parser.parse_args()

rawDescription: Python argparse: Force a list item to be unique


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

print "enter val: ",
sys.stdout.flush()
val = raw_input()
print val

rawDescription: Python - how can I read stdin from shell, and send stdout to shell and file


code:
from matplotlib.pyplot import plot, draw, show
plot([1,2,3])
draw()
print 'continue computation'

# at the end call show to ensure window won't close.
show()

rawDescription: Is there a way to detach matplotlib plots so that the computation can continue?


code:
import re

filename = 'tr.txt'

with open(filename,'r') as f:
    ch = f.read()

pat = re.compile('createNode newnode -n ("\w+?_SET");(.*?)(?=createNode|\Z)',re.DOTALL)
pit = re.compile('^ *setAttr.+?("[^"\n]+").+("[^"\n]+");(?:\n|\Z)',re.MULTILINE)

dic = dict( (mat.group(1),dict(pit.findall(mat.group(2)))) for mat in pat.finditer(ch))
print dic

rawDescription: Python multiline regex + multi entries reading a file in one go


code:
def iq_to_distance(iq):
     if iq<=95: return "low"
     if iq<=100: return "mid"
     return "high"

for name,iq in intelligence.items():
    print {'name': name, 'iq': iq, 'distance': iq_to_distance(iq)}

rawDescription: Doing mapping from range of numbers to words


code:
def print_top(filename):
    word_count = word_dictionary(filename)
    items = sorted(word_count.items(), key=get_count, reverse=True)
    for item in sorted(items[:20]):
        print item[0], item[1]

rawDescription: Python trying to sort a list alphabetically after it has been sorted numerically


code:
vals, ix = max_in_panes(x, (2,2))
print vals
print ix

rawDescription: What is a pythonic way of finding maximum values and their indices for moving subarrays for numpy ndarray?


code:
import re
from time import clock
iterat = 100

from datetime import datetime
dates = ['10/02/09', '07/22/09', '09-08-2008', '9/9/2008', '11/4/2010',
         ' 03-07-2009', '09/01/2010']

reobj = re.compile(
r"""\s*  # optional whitespace
(\d+)    # Month
[-/]     # separator
(\d+)    # Day
[-/]     # separator
(?:20)?  # century (optional)
(\d+)    # years (YY)
\s*      # optional whitespace""",
re.VERBOSE)

te = clock()
for i in xrange(iterat):
    ndates = (reobj.sub(r"\1/\2/20\3", date) for date in dates)
    fdates1 = [datetime.strftime(datetime.strptime(date,"%m/%d/%Y"), "%m/%d/%Y")
               for date in ndates]
print "Tim's method   ",clock()-te,'seconds'

regx = re.compile('[-/]')

te = clock()
for i in xrange(iterat):
    ndates = (reobj.match(date).groups() for date in dates)
    fdates2 = ['%s/%s/20%s' % tuple(x.zfill(2) for x in tu) for tu in ndates]
print "mixing solution",clock()-te,'seconds'

te = clock()
for i in xrange(iterat):
    ndates = (regx.split(date.strip()) for date in dates)
    fdates3 = ['/'.join((m.zfill(2),d.zfill(2),('20'+y.zfill(2) if len(y)==2 else y)))
              for m,d,y in ndates]
print "eyquem's method",clock()-te,'seconds'

te = clock()
for i in xrange(iterat):
    fdates4 = ['{:0>2}/{:0>2}/20{}'.format(*reobj.match(date).groups()) for date in dates]
print "Tim + format   ",clock()-te,'seconds'

print fdates1==fdates2==fdates3==fdates4

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

description: Parse multiple (unknown) date formats in python


code:
import itertools
def negations(seq):
    for prefixes in itertools.product(["", "-"], repeat=len(seq)):
        yield [prefix + value for prefix, value in zip(prefixes, seq)]

print list(negations(["a", "b", "c"]))

rawDescription: all combination of a complicated list

description: All combination of a complicated list


code:
import urllib
import urllib2

text =  """
        Further, while specific constitutive binding to the peri-kappa B site is seen in monocytes, stimulation with phorbol esters induces additional, specific binding. Understanding the monocyte-specific function of the peri-kappa B factor may ultimately provide insight into the different role monocytes and T-cells play in HIV pathogenesis.

### This is a sample. Replace this with your own text.
        """
data = {
        "paragraph" : text
       }

encoded_data = urllib.urlencode(data)
content = urllib2.urlopen("http://text0.mib.man.ac.uk/software/geniatagger/a.cgi",
        encoded_data)
print content.readlines()

rawDescription: A python script that automatically input some text in a website and get its source code


code:
from selenium import webdriver

driver = webdriver.Chrome()  # can be Firefox(), PhantomJS() and more
driver.get("http://topsy.com/trackback?url=http%3A%2F%2Fmashable.com%2F2014%2F08%2F27%2Faustralia-retail-evolution-lab-aopen-shopping%2F")

for tweet in driver.find_elements_by_class_name('result-tweet'):
    print tweet.find_element_by_xpath('.//div[@class="media-body"]//ul[@class="inline"]/li//a').get_attribute('href')

driver.close()

rawDescription: Urllib Python is not providing with the html code I see with with inspect element


code:
import wx

class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial", size=(500,500))

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)
        bindings = [
                  (wx.ACCEL_CTRL,  wx.WXK_UP, self.on_move_up),
                  (wx.ACCEL_CTRL,  wx.WXK_DOWN, self.on_move_down),
                  (wx.ACCEL_CTRL,  wx.WXK_LEFT, self.on_move_left),
                  (wx.ACCEL_CTRL,  wx.WXK_RIGHT, self.on_move_right),
                  ]

        accelEntries = []

        for binding in bindings:
            eventId = wx.NewId()
            accelEntries.append( (binding[0], binding[1], eventId) )

            self.Bind(wx.EVT_MENU, binding[2], id=eventId)

        accelTable  = wx.AcceleratorTable(accelEntries)
        self.SetAcceleratorTable(accelTable )
     #----------------------------------------------------------------------
    def on_move_up(self, event):
        print "You pressed CTRL+up"
    def on_move_down(self, event):
        print "You pressed CTRL+down"
    def on_move_left(self, event):
        print "You pressed CTRL+left"
    def on_move_right(self, event):
        print "You pressed CTRL+right"

# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

rawDescription: Programmatic binding of accelerators in wxPython


code:
import itertools

list1 = [1,2,3]
list2 = [1,2,3]
list3 = [1,2,3,4,5,6,7,8,9]

for (x, y), z in zip(itertools.product(list1, list2), list3):
    print x, y, z

rawDescription: How to iterate through three lists in python

description: Iterate through three lists in python


rawDescription: Calcuate Duration and Average of CSV file data using python


code:
from xlrd import open_workbook

class Arm(object):
    def __init__(self, id, dsp_name, dsp_code, hub_code, pin_code, pptl):
        self.id = id
        self.dsp_name = dsp_name
        self.dsp_code = dsp_code
        self.hub_code = hub_code
        self.pin_code = pin_code
        self.pptl = pptl

    def __str__(self):
        return("Arm object:\n"
               "  Arm_id = {0}\n"
               "  DSPName = {1}\n"
               "  DSPCode = {2}\n"
               "  HubCode = {3}\n"
               "  PinCode = {4} \n"
               "  PPTL = {5}"
               .format(self.id, self.dsp_name, self.dsp_code,
                       self.hub_code, self.pin_code, self.pptl))

wb = open_workbook('sample.xls')
for sheet in wb.sheets():
    number_of_rows = sheet.nrows
    number_of_columns = sheet.ncols

    items = []

    rows = []
    for row in range(1, number_of_rows):
        values = []
        for col in range(number_of_columns):
            value  = (sheet.cell(row,col).value)
            try:
                value = str(int(value))
            except ValueError:
                pass
            finally:
                values.append(value)
        item = Arm(*values)
        items.append(item)

for item in items:
    print item
    print("Accessing one single value (eg. DSPName): {0}".format(item.dsp_name))
    print

rawDescription: Read Excel File in Python

description: Read Excel File in Python


code: print json.dumps(e, cls=new_alchemy_encoder(), check_circular=False)

rawDescription: How to serialize SqlAlchemy result to JSON?

description: Serialize SqlAlchemy result to JSON


code:
for entry in data:
    for inning in entry['innings']:
        print inning['wickets']

rawDescription: Decoding nested JSON with multiple 'for' loops


code:
gen = e_dec()
e = [str(gen.next()) for i in xrange(1000)]
e.insert(1, '.')
print ''.join(e)

rawDescription: Code to Generate e one Digit at a Time

description: Code to Generate e one Digit at a Time


code:
def calculate_rank(data):
    data = {v:k for k, v in enumerate(sorted(data,key=lambda x:int(data[x]),reverse=True),1)}
    def get_rank(name):
        return data[name]
    return get_rank

get_rank = calculate_rank({"rich": 15, "tom": 19, "bob": 7})
print "You are ranked #{}".format(get_rank("tom"))
print "You are ranked #{}".format(get_rank("rich"))
print "You are ranked #{}".format(get_rank("bob"))

rawDescription: Getting the index of a players rank by points


code:
for name, group in df.groupby('Trustworthy'):
    print "name:", name, "| len:", len(group)

'''
name: 0 | len: 7
name: 1 | len: 3
'''

rawDescription: Pandas Modify Dataset to Have Equal Values from both Classifications


code:
from bs4 import BeautifulSoup
import requests

CATALOG = 10001
STORE = 30556

url = 'http://calstate-la.bncollege.com/webapp/wcs/stores/servlet/TBWizardView?catalogId={catalog}&langId=-1&storeId={store}'.format(catalog=CATALOG,
                                                                                store=STORE)
xhr_url = 'http://calstate-la.bncollege.com/webapp/wcs/stores/servlet/TextBookProcessDropdownsCmd'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36'}

session = requests.Session()
response = session.get(url, headers=headers)
soup = BeautifulSoup(response.content)

campus = soup.find('input', attrs={'name': 'campus1'}).get('value')
book_row = soup.find('div', class_='bookRowContainer')

params = {
    'campusId': campus,
    'deptId': '',
    'courseId': '',
    'sectionId': '',
    'storeId': STORE,
    'catalogId': CATALOG,
    'langId': '-1',
    'dropdown': 'term'
}

terms = book_row.select('li.termOption')
for term in terms:
    params['termId'] = term.get('data-optionvalue')
    response = session.post(xhr_url, params=params, headers=headers)
    print response.content

rawDescription: Get all combinations from selenium drop down list without options or select tag


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

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


code:
print "sub2 output: start"
erroneous_command()

rawDescription: Custom Scheduler to have sequential + semi-sequential scripts with timeouts/kill switches?


code:
def contains(s, sub):
    return sub in s

if __name__ == '__main__':
    fruits = ["apples", "bananas", "oranges", "watermelon", "apricots"]
    substring = raw_input("Please enter a substring of fruits: ")
    if contains(fruits, substring):
        print substring, "is a substring of fruits."

rawDescription: Error: name 's' is not defined

description: Error: name 's' is not defined


code:
class Foo(object):
    def __init__(self, bar):
        self.bar = bar
print Foo(42)

rawDescription: Identifying the underlying dataset from the print statement


code:
with open('x.txt') as f:
    print np.loadtxt(f, dtype=str, delimiter='\n')

rawDescription: Converting plain text list into array in python program


code:
from fabric.decorators import task
from functools import wraps

def custom_decorator(func):
    @wraps(func)
    def decorated(*args, **kwargs):
        print "this function is decorated."
        return func(*args, **kwargs)
    return decorated

@task
@custom_decorator
def some_function():
    print "this is function"

rawDescription: How to add a custom decorator to a fabric task

description: Add a custom decorator to a fabric task


code:
from selenium import webdriver

url = "http://rads.stackoverflow.com/amzn/click/1597805483"

driver = webdriver.Firefox()
driver.get(url)

iframe = driver.find_element_by_id('bookDesc_iframe')
driver.switch_to.frame(iframe)

print driver.find_element_by_id('iframeContent').text

driver.close()

rawDescription: how to scrape imbeded script on webpage in python

description: Scrape imbeded script on webpage in python


code:
import re
array = [3, "$x" , "$y", "$hi_buddy"]
rep_array = [3, "A", "B", "C"]
string = "$xena is here $x and $y. foo$x matches too!"
for pattern, replacement in zip(array[1:], rep_array[1:]):
    pattern = r'{}\b'.format(re.escape(pattern))
    string = re.sub(pattern, replacement, string)

print string

rawDescription: Python replace, using patterns in array

description: Python replace, using patterns in array


code:
from collections import Counter, deque

GROUP_SIZE = 5
counter = Counter()
mydeque = deque(maxlen=GROUP_SIZE)

with open("tags.txt", "r") as tagfile:
    tags = (line.strip() for line in tagfile)
    try:
        while len(mydeque) < GROUP_SIZE-1:
            mydeque.append(tags.next())
    except StopIteration:
        pass

    for tag in tags:   # main loop
        mydeque.append(tag)
        counter.update((tuple(mydeque),))

if len(counter) < 1:
    print 'too few tags in file'
else:
    for tags, count in counter.most_common(10):  # top 10
        print '{}, count = {:,d}'.format(list(tags), count)

rawDescription: List the most common lists, from a list

description: List the most common lists, from a list


code:
import os

path = "/home/luai/Desktop/python/test"
files = os.walk(path).next()[2]

print "Here is a list of all files: "

for filenumber in range(len(files)):
    print filenumber, files[filenumber]

filenumber = raw_input ("Number of file which to update: ")
filenumber = int(filenumber)

if files[filenumber] in files:
    inputFile = open(os.path.join(path, files[filenumber]))
    inputFile.close()
else:
    print "no match found"
    sys.exit()

rawDescription: Add list of files into a dictionary using python


code:
#! /usr/bin/env python

''' Validate the keys in kwargs

    Test keys against a container (set, tuple, list) of good keys,
    supplying a value of None for missing keys

    Also, if a key ends with an underscore, strip it.

    Written by PM 2Ring 2014.11.15

    From
    http://stackoverflow.com/questions/26945235/best-practice-handle-functions-with-lots-of-parameters-and-reserved-names

'''

import sys

def test(**kwargs):
    good_keys = ("container", "class_", "color",
        "vassal", "level", "line", "filename",
        "func", "with_total", "range")
    new_kwargs = validate_keys(kwargs, good_keys)
    for t in new_kwargs.items():
        print "%-12s : %r" % t

#def alarms(**kwargs):
    #good_keys = ("container", "class_", "color",
        #"vassal", "level", "line", "filename",
        #"func", "with_total", "range")
    #return self.get('alarms', validate_keys(kwargs, good_keys))

def validate_keys(kwargs, good_keys):
    good_keys = set(good_keys)
    bad_keys = set(kwargs.keys()) - good_keys
    if bad_keys:
        bad_keys = ', '.join(bad_keys)
        print >>sys.stderr, "Unknown parameters: %s\n" % bad_keys
        raise KeyError, bad_keys

    new_kwargs = {}
    for k in good_keys:
        new_kwargs[k.rstrip('_')] = kwargs.get(k, None)
    return new_kwargs

test(color="red", class_="top",
    #bar=1, foo=3,  #Some bad keys
    level=2, func="copy",filename="text.txt")

rawDescription: Best practice: handle functions with lots of parameters and reserved names


code:
def get_text(element):
    return " ".join(t.nodeValue for t in element[0].childNodes
                    if t.nodeType == t.TEXT_NODE)

dom = xml.dom.minidom.parseString(data)
itemList = dom.getElementsByTagName('item')
for item in itemList[1:]:
    summaryList = item.getElementsByTagName('summary')
    statusList = item.getElementsByTagName('status')
    keyList = item.getElementsByTagName('key')

    print get_text(summaryList)
    print get_text(statusList)
    print get_text(keyList)
    print "----"

rawDescription: Getting multiple children's values using minidom


code:
def get_content(f):
  my_data = []
  for line in f:
    if line.startswith('>'):
      yield my_data
      my_data = []
    else:
      my_data.append(line)
  yield my_data  # the last on

f.seek(0)
for i in get_content(f):
  print i

rawDescription: How to read lines without iterating

description: Read lines without iterating


code:
from itertools import permutations
print list(permutations([1, 2, 3]))

rawDescription: Python generator cannot be converted to list


code:
import time

fps = 5
time_delta = 1./fps

while True:
    t0 = time.clock()
    time.sleep(time_delta)
    t1 = time.clock()
    print 1. / (t1 - t0)

rawDescription: Limiting Fps in python

description: Limiting Fps in python


code:
a = ["part 1", "part 3" , "part 10", "part 2"]
print sorted(a, key=lambda x:int(x.split()[1]))

rawDescription: how to sort a list of string by every element's numeric value

description: Sort a list of string by every element's numeric value


code:
contents = open('mytsv.tsv')
linenum = 0
for line in contents:
    linenum+=1
    print 'The maximum in line %d is in column %d' %(linenum, line.index(max(line.split())))

rawDescription: Python: Identify column that has greatest value


code:
chapter = "home"
while  True:
    if chapter == "home":
        chapter = menu()
    if chapter == "1":
        print "Welcome to chapter 1"
        print " 'home' back"
        chapter = C_selection()

rawDescription: menu selection with functions

description: Menu selection with functions


code:
#!/usr/bin/env python

''' Time various implementations of string diff function

    From http://stackoverflow.com/q/28581218/4014959

    Written by PM 2Ring 2015.02.18
'''

from itertools import imap, izip, starmap
from operator import ne

from timeit import Timer
from random import random, seed

def h_dist0(s1,s2):
    ''' For loop '''
    tot = 0
    for d1, d2 in zip(s1, s2):
        if d1 != d2:
            tot += 1
    return tot

def h_dist1(s1,s2):
    ''' List comprehension '''
    return sum([dgt1 != dgt2 for dgt1, dgt2 in zip(s1, s2)])

def h_dist2(s1,s2):
    ''' Generator expression '''
    return sum(dgt1 != dgt2 for dgt1, dgt2 in zip(s1, s2))

def h_dist3(s1,s2):
    ''' Generator expression with if '''
    return sum(1 for dgt1, dgt2 in zip(s1, s2) if dgt1 != dgt2)

def h_dist3a(s1,s2):
    ''' Generator expression with izip '''
    return sum(1 for dgt1, dgt2 in izip(s1, s2) if dgt1 != dgt2)

def h_dist4(s1,s2):
    ''' imap '''
    return sum(imap(ne, s1, s2))

def h_dist5(s1,s2):
    ''' starmap '''
    return sum(starmap(ne, izip(s1, s2)))

funcs = [
    h_dist0,
    h_dist1,
    h_dist2,
    h_dist3,
    h_dist3a,
    h_dist4,
    h_dist5,
]

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

def check_full():
    print 'Testing all functions with strings of length', len(s1)
    for func in funcs:
        print '%s:%s\n%d\n' % (func.func_name, func.__doc__, func(s1, s2))

def check():
    print 'Testing all functions with strings of length', len(s1)
    print [func(s1, s2) for func in funcs], '\n'

def time_test(loops=10000, reps=3):
    ''' Print timing stats for all the functions '''
    slen = len(s1)
    print 'Length = %d, Loops = %d, Repetitions = %d' % (slen, loops, reps)

    for func in funcs:
        #Get function name and docstring
        fname = func.func_name
        fdoc = func.__doc__

        print '\n%s:%s' % (fname, fdoc)
        t = Timer('%s(s1, s2)' % fname, 'from __main__ import s1, s2, %s' % fname)
        results = t.repeat(reps, loops)
        results.sort()
        print results
    print '\n' + '- '*30 + '\n'

def make_strings(n, r=0.5):
    print 'r:', r
    s1 = 'a' * n
    s2 = ''.join(['b' if random() < r else 'a' for _ in xrange(n)])
    return s1, s2

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

seed(37)

s1, s2 = make_strings(100)
#print '%s\n%s\n' % (s1, s2)
check()
time_test(10000)

s1, s2 = make_strings(100, 0.1)
check()
time_test(10000)

s1, s2 = make_strings(100, 0.9)
check()
time_test(10000)

s1, s2 = make_strings(10)
check()
time_test(50000)

s1, s2 = make_strings(1000)
check()
time_test(1000)

rawDescription: Speed/efficiency comparison for loop vs list comprehension vs other methods


code:
class SomeClass:
    def do(self, entity):
        print entity.Param1

rawDescription: access host class from IronPython script (lightswitch)

description: Access host class from IronPython script (lightswitch)


code:
class example_class():
    _example_text_default = 'I print this: '
    def example_function(self, number, text=None):
        if text is None:
            text = self._example_text_default
        print text, number

rawDescription: How to change default of function parameter for an instance of an object?

description: Change default of function parameter for an instance of an object


code:
from collections import defaultdict

def exactly_one(iter):
    count = 0
    for x in iter:
        if x:
            count += 1
            if count > 1:
                break
    return count == 1

def are_one_letter_apart(a,b):
    if len(a) != len(b): return False
    return exactly_one(a_char != b_char for a_char, b_char in zip(a,b))

def pairs(seq):
    for i in range(len(seq)):
        for j in range(i+1, len(seq)):
            yield (seq[i], seq[j])

def search(graph, node):
    seen = set()
    to_visit = set()
    to_visit.add(node)
    while to_visit:
        cur = to_visit.pop()
        if cur in seen: continue
        for neighbor in graph[cur]:
            if neighbor not in seen:
                to_visit.add(neighbor)
        seen.add(cur)
    return seen

def get_islands(graph):
    seen = set()
    islands = []
    for item in graph.iterkeys():
        if item in seen: continue
        group = search(graph, item)
        seen = seen | group
        islands.append(group)
    return islands

def create_classes(seq, f):
    graph = defaultdict(list)
    for a,b in pairs(seq):
        if f(a,b):
            graph[a].append(b)
            graph[b].append(a)
    #one last pass to pick up items with no relations to anything else
    for item in seq:
        if item not in graph:
            graph[item].append(item)

    return [list(group) for group in get_islands(graph)]

seq = ['cat','ace','car','zip','ape','pip']
print create_classes(seq, are_one_letter_apart)

rawDescription: Python - partitioning a list of strings using an equivalence relation


code:
a = 1
b = 2
print "test %d" %(a+b)

rawDescription: Python Script on Kali Linux

description: Python Script on Kali Linux


code:
import re

print sorted([element for element in dir(re) if 'find' in element])

rawDescription: learnpython.org modules exercise

description: Learnpython.org modules exercise


rawDescription: How is returning the output of a function different than printing it?


code:
import time
import zmq

ctx = zmq.Context()
pub = ctx.socket(zmq.PUB)
sub = ctx.socket(zmq.SUB)

url = "tcp://127.0.0.1:5555"
pub.bind(url)
sub.connect(url)

# subscribe to 'a' and 'b'
sub.setsockopt(zmq.SUBSCRIBE, b'a')
sub.setsockopt(zmq.SUBSCRIBE, b'b')

time.sleep(1)

for word in [ 'alpha', 'beta', 'gamma', 'apple', 'carrot', 'bagel']:
    pub.send(word)

time.sleep(1)

for i in range(4):
    print sub.recv(zmq.NOBLOCK)

rawDescription: ZeroMQ and multiple subscribe filters in Python


code:
imgs = set()
for img in stories[1].get('images', []):
    for variant in img.get('variants', []):
        if variant.get('layout') == '1024 Landscape':
            imgs.add(variant['src'])
print imgs
set([u'http://static.stuff.co.nz/1341147692/827/7202827.jpg'])

rawDescription: Python json get specific story and image


code: print weapons[x1]

rawDescription: How to set a variable to a dictionary in python?

description: Set a variable to a dictionary in python


code: print " ".join(word)

rawDescription: How to put a blank space in a list?

description: Put a blank space in a list


rawDescription: Finding a match in two sets of synchronized lists


code:
from itertools import groupby
for k,v in groupby(lst,key=lambda x:x['date'][:7]):
   print k, list(v)

rawDescription: How do I group this list of dicts by the same month?

description: Group this list of dicts by the same month


code:
if version == f:
      APP_VER = 'APP_FREE'
elif version == p:
      APP_VER = 'APP_PAID'
else:
    print 'version <{0}> unknown'.format(version)
    exit()
    # or raise an exception here

rawDescription: Assign dynamic key string in python

description: Assign dynamic key string in python


rawDescription: Get a clean summary of nlme.lme() or lme4.lmer() in RPy


rawDescription: Format float with padding and justification


code:
import urllib2,sys
f = open("url_file.txt","a+")
urlhere_list = sys.argv[1].split(",")

for urls in urlhere_list:
    print "for url: " + urls + ":"
    try:
        fileHandle = urllib2.urlopen(urls)
        data = fileHandle.read()
        fileHandle.close()
        f.write(urls+ "\n")

        print "It exists"

    except urllib2.URLError, e:
        print 'PAGE 404: It Doesnt Exist', e
    except:
        print "invalid url"
f.close()

rawDescription: Writing text to txt file in python on new lines?


code:
import xml.dom.minidom

dom = xml.dom.minidom.parseString("""
<root xmlns:media="http://media/" xmlns:yt="http://media/yt/">
    <media:thumbnail url="http://i.ytimg.com/vi/k8J-72MmTGg/default.jpg" height="90" width="120" time="00:01:48.500" yt:name="default" />
    <media:thumbnail url="http://i.ytimg.com/vi/k8J-72MmTGg/mqdefault.jpg" height="180" width="320" yt:name="mqdefault" />
    <media:thumbnail url="http://i.ytimg.com/vi/k8J-72MmTGg/hqdefault.jpg" height="360" width="480" yt:name="hqdefault" />
</root>""")

media_thumbnail = dom.getElementsByTagNameNS("http://media/","thumbnail")
print media_thumbnail[0].getAttribute("height")
print media_thumbnail[0].getAttribute("width")
print media_thumbnail[0].getAttribute("time")
print media_thumbnail[0].getAttributeNS("http://media/yt/","name")
media_thumbnail[0].setAttribute("unit","px")
media_thumbnail[0].setAttributeNS("http://media/yt/","value","1")
print dom.toxml()

rawDescription: How to scrape XML with Python?

description: Scrape XML with Python


code:
import urllib
print urllib.splithost.__doc__

rawDescription: Python BeautifulSoup - Grab internal links from page


code:
import urlparse

from scrapy.spider import BaseSpider
from scrapy.http.request import Request

from scrapy_demo.items import ScrapyDemoItem

class ScrapyDemoSpider(BaseSpider):
    name = "scrapy_demo"
    allowed_domains = ["buffalo.craigslist.org"]
    start_urls = ['http://buffalo.craigslist.org/search/cps/']

    def parse(self, response):
        # processing listings
        for listing in response.css('p.row > a[data-id]'):
            link = listing.xpath('@href').extract()[0]
            yield Request(urlparse.urljoin(response.url, link), callback=self.parse_listing_page)

        # following next page
        next_page = response.xpath('//a[contains(@class, "next")]/@href').extract()
        print next_page
        if next_page:
            yield Request(urlparse.urljoin(response.url, next_page[0]), callback=self.parse)

    def parse_listing_page(self, response):
        item = ScrapyDemoItem()
        item['link'] = response.url
        item['title'] = response.xpath('//title/text()').extract()[0].strip()
        item['content'] = response.xpath('//section[@id="postingbody"]/text()').extract()[0].strip()
        yield item

rawDescription: Scrapy JSON export issues

description: Scrapy JSON export issues


rawDescription: Explain the code function in call by object


code:
import sys

for line in sys.stdin:
    city, temperature = line.rsplit(None, 1)
    print '%s\t%s' % (city ,temperature)

rawDescription: get city and temperature values into variables to use in reducer file

description: Get city and temperature values into variables to use in reducer file


code:
def test():
    # Make sure you patch on MyClass, not on a MyClass instance, otherwise
    # you'll get an AttributeError, because mock is using settattr and
    # last_transaction is a readonly property so there's no setter.
    with mock.patch(MyClass, 'last_transaction') as mock_last_transaction:
        mock_last_transaction.__get__ = mock.Mock(return_value=Transaction())
        myclass = MyClass()
        print myclass.last_transaction

rawDescription: How to mock a readonly property with mock?

description: Mock a readonly property with mock


code:
lines = output.splitlines()[::-1]
width = max(len(l) for l in lines)

reversed = []
for line in lines:
    tree, dashes, label = line.rpartition('--')
    tree = (tree + dashes)[::-1]
    line = '{1:>{0}}{2}'.format(width - len(tree), label, tree).rstrip()
    reversed.append(line)

print '\n'.join(reversed)

rawDescription: How to print a reversed ascii tree?

description: Print a reversed ascii tree


code:
def some_function(s):
    # do something with or to 's'...
    return s

def add_unique(function, data):
    result = []
    seen = set(result) # init to empty set
    for s in data:
        if s not in seen:
            t = function(s)
            result.append(t)
            seen.add(t)
    return result

print add_unique(some_function, raw_data)
# ['a', 'b', 'c', 'd', 'e', 'f']

rawDescription: LIst Comprehensions: References to the Components


code:
data = range(10)
print [x*x for x in GetItem(data)]

rawDescription: Python: the mechanism behind list comprehension


code:
import time

def some_func(**kwargs):
    for k,v in kwargs.items():
        pass

def main():
    name = 'felix'
    location = 'here'
    user_type = 'player'

    kwds = {'name': name,
            'location': location,
            'user_type': user_type}

    start = time.time()
    for i in range(10000000):
        some_func(**kwds)

    end = time.time()
    print 'Time using expansion:\t{0}s'.format(start - end)
    start = time.time()
    for i in range(10000000):
        some_func(name=name, location=location, user_type=user_type)

    end = time.time()
    print 'Time without expansion:\t{0}s'.format(start - end)

if __name__ == '__main__':
    main()

rawDescription: Is there a reason not to send super().__init__() a dictionary instead of **kwds?


code:
#!/usr/bin/env python

''' Find the 1st Collatz sequence containing a given number of prime terms

    From http://stackoverflow.com/q/29920691/4014959

    Written by PM 2Ring 2015.04.29

    [Seed == 1805311, prime count == 67]
'''

import sys

def is_prime(a):
    ''' Test if odd `a` >= 3 is prime '''
    for i in xrange(3, int(1 + a ** 0.5), 2):
        if not a % i:
            return 0
    return 1

#Track the highest number generated so far; use a list
# so we don't have to declare it as global...
hi = [2]

#Cache for sequence prime counts. The key is the sequence seed,
# the value is the number of primes in that sequence.
lookup = {1:0, 2:1}

def count_prime_terms_iterative(n):
    ''' Count the number of primes terms in a Collatz sequence
        Iterative version '''
    seq = []
    while n not in lookup:
        if n > hi[0]:
            hi[0] = n

        if n % 2:
            seq.append((n, is_prime(n)))
            n = n * 3 + 1
        else:
            seq.append((n, 0))
            n = n // 2

    count = lookup[n]
    for n, isprime in reversed(seq):
        count += isprime
        lookup[n] = count

    return count

def count_prime_terms_recursive(n):
    ''' Count the number of primes terms in a Collatz sequence
        Recursive version '''
    if n not in lookup:
        if n > hi[0]:
            hi[0] = n

        if n % 2:
            next_n = n * 3 + 1
            isprime = is_prime(n)
        else:
            next_n = n // 2
            isprime = 0
        lookup[n] = count_prime_terms(next_n) + isprime

    return lookup[n]

def find_seed(numprimes, start):
    ''' Find the seed of the 1st Collatz sequence containing
        `numprimes` primes, starting from odd seed `start` '''
    i = start
    mcount = 0

    print 'seed, prime count, highest term, dict size'
    while mcount < numprimes:
        count = count_prime_terms(i)
        if count > mcount:
            mcount = count
            print i, count, hi[0], len(lookup)
        i += 2

#count_prime_terms = count_prime_terms_recursive
count_prime_terms = count_prime_terms_iterative

def main():
    if len(sys.argv) > 1:
        numprimes = int(sys.argv[1])
    else:
        print 'Usage: %s numprimes [start]' % sys.argv[0]
        exit()

    start = int(sys.argv[2]) if len(sys.argv) > 2 else 3

    #Round `start` up to the next odd number
    if start % 2 == 0:
        start += 1

    find_seed(numprimes, start)

if __name__ == '__main__':
    main()

rawDescription: finding the lowest collatz sequence that gives more that 65 primes

description: Finding the lowest collatz sequence that gives more that 65 primes


code:
import pymel.core as pm
import pymel.core.uitypes as pmui

modelPanelList = []
modelEditorList = pm.lsUI(editors=True)
for myModelPanel in modelEditorList:
    if myModelPanel.find('modelPanel') != -1:
        modelPanelList.append(myModelPanel)

for modelPanel in modelPanelList:
    if pmui.ModelEditor(modelPanel).getActiveView():
        try:
            # Always better to try in the case of active panel operations
            # as the active panel might not be a viewport.
            print pmui.ModelEditor(modelPanel).getRendererName()
        except Exception as e:
            # Handle exception
            print e

rawDescription: How to query current viewport renderer in maya

description: Query current viewport renderer in maya


code:
class Enumeration(type):
    def __instancecheck__(self, other):
        print 'hi'
        return True

class EnumInt(int):
    __metaclass__ = Enumeration

print isinstance('foo', EnumInt) # prints True

rawDescription: class method __instancecheck__ does not work

description: Class method __instancecheck__ does not work


code:
from lxml import etree

# Create an ElementTree instance
tree = etree.parse("bookstore.xml")

# Get all 'book' elements that have a 'name' child with a string value of 'abc'
books = tree.xpath('book[name="abc"]')

# Print name and price of those books
for book in books:
    print book.find("name").text, book.find("price").text

rawDescription: Find an xml element with some specific text using xpath or find in python using lxml


code:
# Here is the special class @mgilson mentioned:
class DotDict(dict):
    def __getattr__(self, attr):
        return self.get(attr, None)
    __setattr__=dict.__setitem__
    __delattr__=dict.__delitem__

accuracy=[{'value':1,'key':'apple'}, {'value':2,'key':'orange'}]
for fruit in accuracy:
    # Convert dict to DotDict before passing to .format() method:
    print 'name={fruit.key}'.format(fruit=DotDict(fruit))

rawDescription: Getting value of a list in Python

description: Getting value of a list in Python


code:
say = ''
while say not in ['hello', 'bye']:
    say = raw_input('say: ')

if say == 'hello':
    print "hi"
elif say == 'bye':
    print 'goodbye!'

rawDescription: how to go back and check for another input

description: Go back and check for another input


rawDescription: Extract all URLS from a page matching a particular pattern in BeautifulSoup


code: print res.x

rawDescription: How to perform non-linear optimization with scipy/numpy or sympy?

description: Perform non-linear optimization with scipy/numpy or sympy


code:
from Tkinter import *

main = Tk()
l = Label(main, text = "Label", background = "lime")
l.pack()

if l["background"] == 'lime':
    print "Lime!"
if l.cget("background") == 'lime':
    print "Still Lime!"

main.mainloop()

rawDescription: Getting the value of the bg property of a Label in Tkinter


code:
import re
inputString='type="NN" span="123..145" confidence="1.0" '
strings=re.findall('"([^"]*)"', inputString)
print strings

rawDescription: Breaking up substrings in Python based on characters


code:
wanted_topics = ['host_updates_queue', 'foo_bar']

topics = subprocess.check_output([os.path.join(KAFKABIN, 'kafka-topics.sh'),
        '--list',
        '--zookeeper', '{}:2181'.format(KAFKAHOST)])

for wanted in wanted_topics:
    if wanted in topics:
        print '\'{}\' topic exists!'.format(wanted)
    else:
        print '\'{}\' topic does NOT exist!'.format(wanted)

    topic_desc = subprocess.check_output([os.path.join(KAFKABIN, 'kafka-topics.sh'),
        '--describe',
        '--topic', wanted,
        '--zookeeper', '{}:2181'.format(KAFKAHOST)])

    if not topic_desc:
        print 'No description found for the topic \'{}\''.format(wanted)

rawDescription: Check whether a Kafka topic exists in Python


code:
i = range(len(s)) # i = [0, 1, 2, 3, ...,  14]
s = 'azcbobobegghakl'
for number in i:
    if s[number:number+3] == 'bob':
        print 'I found bob!'

rawDescription: Search consecutive indices in string

description: Search consecutive indices in string


code:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import Selector

from slimit import ast
from slimit.parser import Parser
from slimit.visitors import nodevisitor

def get_fields(data):
    parser = Parser()
    tree = parser.parse(data)
    return {getattr(node.left, 'value', ''): getattr(node.right, 'value', '')
            for node in nodevisitor.visit(tree)
            if isinstance(node, ast.Assign)}

class ExampleSpider(CrawlSpider):
    name = "goal2"
    allowed_domains = ["whoscored.com"]
    start_urls = ["http://www.whoscored.com/Teams/32/Statistics/England-Manchester-United"]
    download_delay = 5

    rules = [Rule(SgmlLinkExtractor(allow=('http://www.whoscored.com/Teams/32/Statistics/England-Manchester-United'),deny=('/News', '/Graphics', '/Articles', '/Live', '/Matches', '/Explanations', '/Glossary', 'ContactUs', 'TermsOfUse', 'Jobs', 'AboutUs', 'RSS'),), follow=False, callback='parse_item')]

    def parse_item(self, response):
        sel = Selector(response)
        titles = sel.xpath("normalize-space(//title)")
        myheader = titles.extract()[0]

        script = sel.xpath('//div[@id="team-stage-stats"]/following-sibling::script/text()').extract()[0]
        script_fields = get_fields(script)
        print script_fields['stageId'], script_fields['field'], script_fields['teamId']

rawDescription: Obtaining correct data using Regex/List

description: Obtaining correct data using Regex/List


rawDescription: how to cut a serial numbers into intervals in python code?

description: Cut a serial numbers into intervals in python code


code:
mystuff = [1, 2, "Foo", 34, 42, "Ni", 12, 14, "Blue"]
fmt = "{0} {1} {2}"
num_terms = sum(1 for x in Formatter().parse(fmt))
for x in zip(*[iter(mystuff)]*num_terms):
    print fmt.format(*x)

rawDescription: Iterate a format string over a list

description: Iterate a format string over a list


code:
def test_enqueue(self):
    job = self.q.enqueue(do_test, "supercalafragelistic")
    while True:
        job.refresh()     #<--- well, duh, freddy
        print job.get_id(), job.get_status(), job.meta.get('word')
        if job.is_finished:
            print "Result: ", job.result, job.meta.get('word')
            break
        time.sleep(0.25)

rawDescription: Storing "meta" data on redis job is not working?


code:
F, H, A, B, C = Bools('F H A B C')
s = Solver()
s.add(F, H, If(A, B, C))
print s

rawDescription: How to use Implies and if boolean commands in z3 python API

description: Use Implies and if boolean commands in z3 python API


code:
class Character(object): # Changed this since I don't know how class 'Card' looks like
    def __init__(self, str, dex, con, int, wis, char):
        self.str = str
        self.dex = dex
        self.con = con
        self.int = int
        self.wis = wis
        self.char = char

    def __str__(self):
        return "str: {}, dex: {}, con: {}, int: {}, wis: {}, char: {}".format(self.str, self.dex, self.con, self.int, self.wis, self.char)

# Testing
list_of_characters = [Character(1, 2, 3, 4, 5, "A"), Character(9, 8, 7, 6, 5, "B")]

for e in list_of_characters:
    print e

rawDescription: python: printing __dict__ : is it possible to print in the same order as what's listed in the class?

description: Python: printing __dict__ : is it possible to print in the same order as what's listed in the class


code:
from pyparsing import Suppress, alphas, alphanums, nums, Word
from itertools import groupby

# you could do this with regular expressions too, if you prefer
LBRACK,RBRACK = map(Suppress, '[]')
ident = Word('_' + alphas, '_' + alphanums)
integer = Word(nums).setParseAction(lambda t : int(t[0]))
subscriptedRef = ident + 2*(LBRACK + (ident | integer) + RBRACK)

def simplify_value(v):
    if isinstance(v,list) and len(v)==1:
        return simplify_value(v[0])
    if v == integer:
        return int(v)
    return v

def regroup_args(dd):
    ret = {}
    subscripts = []
    for k,v in dd.items():
        # this is a pyparsing short-cut to see if a string matches a pattern
        # I also used it above in simplify_value to test for integerness of a string
        if k == subscriptedRef:
            subscripts.append(tuple(subscriptedRef.parseString(k))+
                                    (simplify_value(v),))
        else:
            ret[k] = simplify_value(v)

    # sort all the matched subscripted args, and then use groupby to
    # group by name and list index
    # this assumes all indexes 0-n are present in the parsed arguments
    subscripts.sort()
    for name,nameitems in groupby(subscripts, key=lambda x:x[0]):
        ret[name] = []
        for idx,idxitems in groupby(nameitems, key=lambda x:x[1]):
            idd = {}
            for item in idxitems:
                name, i, attr, val = item
                idd[attr] = val
            ret[name].append(idd)

    return ret

request_args = {'grid[1][y]': ['184'], 'grid[0][y]': ['185'], 'grid[1][x]': ['18'], 'request': ['loadTiles'], 'grid[0][x]': ['17']}
print regroup_args(request_args)

rawDescription: How to properly parse request strings with Twisted (python)

description: Properly parse request strings with Twisted (python)


code:
q1 = """
select ?birthPlace where {
<http://dbpedia.org/resource/Claude_Monet> <http://dbpedia.org/property/birthPlace> ?birthPlace .
 }"""
print query(q1,"http://dbpedia.org/sparql")

rawDescription: Example python script that uses DBPedia?

description: Example python script that uses DBPedia


code:
import collections
import heapq
import time

def distance(start, end):
    steps = 0
    for pos in range(len(start)):
        if start[pos] != end[pos]:
            steps += 1
    return steps

class SearchHeap(object):
    def __init__(self):
        self.on_heap = set()
        self.heap = []

    def push(self, distance, word, path):
        if word in self.on_heap:
            return
        self.on_heap.add(word)
        heapq.heappush(self.heap, ((distance, len(path)), word, path))

    def __len__(self):
        return len(self.heap)

    def pop(self):
        return heapq.heappop(self.heap)

class OneLetterGame(object):
    _word_data = None

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

    def run(self, start_word, end_word):
        start_time = time.time()
        self._word_data = collections.defaultdict(list)
        if len(start_word) != len(end_word):
            print 'words of different length; no path'
            return

        found_start, found_end = self._load_words(start_word, end_word)
        if not found_start:
            print 'start word %r not found in dictionary' % start_word
            return
        if not found_end:
            print 'end word %r not found in dictionary' % end_word
            return

        search_start_time = time.time()
        path = self._shortest_path(start_word, end_word)
        search_time = time.time() - search_start_time
        print 'search time was %.4f seconds' % search_time

        if path:
            print path
        else:
            print 'no path found from %r to %r' % (start_word, end_word)

        run_time = time.time() - start_time
        print 'total run time was %.4f seconds' % run_time

    def _load_words(self, start_word, end_word):
        found_start, found_end = False, False
        length = len(start_word)
        with open(self.dict_path) as words:
            for word in words:
                word = word.strip()
                if len(word) == length:
                    if start_word == word: found_start = True
                    if end_word == word: found_end = True
                    for bucket in self._buckets_for(word):
                        self._word_data[bucket].append(word)
        return found_start, found_end

    def _shortest_path(self, start_word, end_word):
        heap = SearchHeap()
        heap.push(distance(start_word, end_word), start_word, (start_word,))
        while len(heap):
            dist, word, path = heap.pop()
            if word == end_word:
                return path
            for neighbor in self._neighbors_of(word):
                heap.push(
                    distance(neighbor, end_word),
                    neighbor,
                    path + (neighbor,))
        return ()

    def _buckets_for(self, word):
        buckets = []
        for pos in range(len(word)):
            front, back = word[:pos], word[pos+1:]
            buckets.append(front+'*'+back)
        return buckets

    def _neighbors_of(self, word):
        for bucket in self._buckets_for(word):
            for word in self._word_data[bucket]:
                yield word

if __name__ == '__main__':
    import sys
    if len(sys.argv) not in [3, 4]:
        print 'Usage: python one_letter_game.py start_word end_word'
    else:
        matt = OneLetterGame(dict_path = '/usr/share/dict/words')
        matt.run(*sys.argv[1:])

rawDescription: One letter game Issue?

description: One letter game Issue


code:
import random, collections, csv

def pick_non_zero(count):
    ditems = collections.defaultdict(int)

    # Read the current stock file in
    with open("stock.csv", "r") as f_input:
        csv_input = csv.reader(f_input, delimiter="\t")
        headers = csv_input.next()

        for item, quantity in csv_input:
            ditems[item] += int(quantity)

    lchoices = []

    for n in range(count):
        # Create a list of items with quantity remaining
        lnon_zero = [item for item, quantity in ditems.items() if quantity > 0]

        if len(lnon_zero) == 0:
            lchoices.append("No more stock")
            break

        # Pick one
        choice = random.choice(lnon_zero)
        # Reduce quantity by 1
        ditems[choice] -= 1
        lchoices.append(choice)

    # Write the updated stock back to the file
    with open("stock.csv", "wb") as f_output:
        csv_output = csv.writer(f_output, delimiter="\t")
        csv_output.writerow(headers)

        for item, quantity in ditems.items():
            csv_output.writerow([item, quantity])

    print "Stock left"

    for item, quantity in ditems.items():
        print "%-10s  %d" % (item, quantity)

    return lchoices

lpicked = pick_non_zero(6)

print
print "Picked:", lpicked

rawDescription: How do i randomly select more than one item from a list in linux scripting?

description: Randomly select more than one item from a list in linux scripting


code:
from itertools import izip, repeat
import sqlite3

conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute('''create table domains
(domain text, linkid text)''')

domain_to_insert = 'domain_name'
ids = ['id1', 'id2']
c.executemany("""insert into domains
      values (?, ?)""", izip(repeat(domain_to_insert), ids))
conn.commit()

domain_to_select = 'domain_name'
c.execute("""select * from domains where domain=?""", (domain_to_select,))

# this is just an example
def some_function_of_row(row):
    return row[1] + ' value'

fields = dict((row[1], some_function_of_row(row)) for row in c)
print fields
c.close()

rawDescription: Storing a List into Python Sqlite3

description: Storing a List into Python Sqlite3


code:
s = set([1, 2, 3, 3])
t = set([2, 4, 6])
print s
print t
print s - t

rawDescription: Deleting equal elements from a list

description: Deleting equal elements from a list


code:
accept_intersect=[]
seen = {}

for item in totallist:
    for sub_item in item:
        if sub_item not in seen:
            accept_intersect.append(sub_item)
            seen[sub_item] = True;

print accept_intersect

rawDescription: How to find list which items are not in output list and add list to output list?

description: Find list which items are not in output list and add list to output list


code:
for ct, values in sorted(averages.iteritems()):
  print ct, ', '.join(map(lambda x: '%.2f' % x, values))

rawDescription: Averaging along DataFrame columns with some missing rows


code:
string = line.split(',')
if string[0] != 'Aa' and string[0] != 'Bb': # You need 'and' up here
  pass
else:
  print string # This will only be executed if the condition is false

rawDescription: Condition with pass statement

description: Condition with pass statement


code:
def binary(a,l=[]):
 if a>0:
    l.append(a%2)
    return binary(a/2)
 else:
    return ''.join(map(str,l[::-1]))

print binary(22)

rawDescription: Python Binary Divide by 2 method

description: Python Binary Divide by 2 method


code:
import codecs, re, time
from itertools import chain

from sklearn.feature_extraction.text import HashingVectorizer
from sklearn.linear_model import Perceptron

trainfile = 'train.txt'
testfile = 'test.txt'

# Vectorizing data.
train = []
word_vectorizer = HashingVectorizer(analyzer='word')
trainset = word_vectorizer.fit_transform(codecs.open(trainfile,'r','utf8'))
tags = ['bs','pt','es','sr']

# Training Perceptron
pct = Perceptron(n_iter=100)
pct.fit(trainset, tags)

# Tagging the documents
codecs.open(testfile,'r','utf8')
testset = word_vectorizer.transform(codecs.open(testfile,'r','utf8'))
results = pct.predict(testset)

print results

rawDescription: How to vectorize bigrams with the hashing-trick in scikit-learn?

description: Vectorize bigrams with the hashing-trick in scikit-learn


code: print ''.join([str(ord(ch) - 96) if ch.isalpha() else ch for ch in raw_input('Write Text: ').lower()])

rawDescription: How to convert letters to numbers using python

description: Convert letters to numbers using python


code:
mg = re.match(r"# (?P<key>[^:]+): (?P<value>.*)", line)
print mg.group('key')

rawDescription: Python Match Group

description: Python Match Group


code:
aDict = {'a':[1,2,3],'b':[2,3,4],'c':[3,3,6]}
aDict2 = dict((k, max(aDict[k])) for k in aDict.keys())
print aDict2

rawDescription: How to iterate over a dictionary and find the max values python?

description: Iterate over a dictionary and find the max values python


code:
def insertNewlines(text, lineLength):

    if len(text) <= lineLength:
        return text
    else:
        return text[:lineLength] + '\n' + insertNewlines(text[lineLength:], lineLength)

text = "Given text and a desired line length, wrap the text as a typewriter would. Insert a newline character  '\\n' after each word that reaches or exceeds the desired line length."
print insertNewlines(text, 15)

rawDescription: Recursively create a new line break after nth character for a given block of text


code:
import twitter

api = twitter.Api()
page = 1
while page <= 500:
    tweets = api.GetSearch("#Burma", page=page, per_page=100)
    for t in tweets:
        print t.text
    page += 1

rawDescription: Result duplication while crawling Twitter


code: print lcount('int', 'foo.cpp')

rawDescription: Python - Search for Word In Line, Count it, Add It


code:
#!/usr/bin/python

import logging
import sys
import time

class FlushFile(object):
    """Write-only flushing wrapper for file-type objects."""
    def __init__(self, f):
        self.f = f
    def write(self, x):
        self.f.write(x)
        self.f.flush()

logging.basicConfig(level=logging.INFO)

sys.stdout = FlushFile(sys.stdout)

i = 0
while 1:
    i += 1
    logging.info('i: {0}'.format(i))
    print i
    time.sleep(0.2)

rawDescription: How to get subprocess output during its running?

description: Get subprocess output during its running


code:
a=['string1','string2','string3','string4','string5']
for item in a[:3]:
      print item

rawDescription: Python: loop through list item x times?

description: Python: loop through list item x times


code:
for k, s in list2:
    print s, d1[k]

rawDescription: Python merge lists by common element

description: Python merge lists by common element


rawDescription: Python Numpy - Matrix replaces defined matrix in global matrix.. similar to Matlab


code:
soup = BeautifulSoup.BeautifulSoup(html)
for child in soup.recursiveChildGenerator():
     name = getattr(child, "name", None)
     if name is not None:
         print name
     elif not child.isspace(): # leaf node, don't print spaces
         print child

rawDescription: Depth First Traversal on BeautifulSoup Parse Tree


code:
for row in results:
    print row.split('_')[0]

rawDescription: python - get part of string from query results

description: Python - get part of string from query results


code:
for i in get_string("x = '\'Stack Overflow\''"): print i

for i in get_string(r"x = '\'Stack Overflow\''"): print i

rawDescription: Python find python strings in string

description: Python find python strings in string


code:
string = 'I love my           world of dreams'
print re.search (r'(?<=my)\s+([^ -.]*)', string).group(1)

rawDescription: Regular expression to find a word after multiple spaces


code:
_, params = cgi.parse_header('text/html; charset=utf-8')
print params['charset'] # -> utf-8

rawDescription: A good way to get the charset/encoding of an HTTP response in Python


code:
a = defaultlist(int)
print a
a[2] = 7
a[4] = 'x'
print a

rawDescription: defaultdict equivalent for lists

description: Defaultdict equivalent for lists


code:
class dotdictify(dict):
    def __init__(self, value=None):
        if value is None:
            pass
        elif isinstance(value, dict):
            for key in value:
                self.__setitem__(key, value[key])
        else:
            raise TypeError, 'expected dict'

    def __setitem__(self, key, value):
        if '.' in key:
            myKey, restOfKey = key.split('.', 1)
            target = self.setdefault(myKey, dotdictify())
            if not isinstance(target, dotdictify):
                raise KeyError, 'cannot set "%s" in "%s" (%s)' % (restOfKey, myKey, repr(target))
            target[restOfKey] = value
        else:
            if isinstance(value, dict) and not isinstance(value, dotdictify):
                value = dotdictify(value)
            dict.__setitem__(self, key, value)

    def __getitem__(self, key):
        if '.' not in key:
            return dict.__getitem__(self, key)
        myKey, restOfKey = key.split('.', 1)
        target = dict.__getitem__(self, myKey)
        if not isinstance(target, dotdictify):
            raise KeyError, 'cannot get "%s" in "%s" (%s)' % (restOfKey, myKey, repr(target))
        return target[restOfKey]

    def __contains__(self, key):
        if '.' not in key:
            return dict.__contains__(self, key)
        myKey, restOfKey = key.split('.', 1)
        target = dict.__getitem__(self, myKey)
        if not isinstance(target, dotdictify):
            return False
        return restOfKey in target

    def setdefault(self, key, default):
        if key not in self:
            self[key] = default
        return self[key]

    __setattr__ = __setitem__
    __getattr__ = __getitem__

rawDescription: Python: Easily access deeply nested dict (get and set)


code:
from pprint import pprint
import webbrowser
search = raw_input("Find this Song: ")
results= m.searchSong(search)
pprint(results)

sortedResultByTitle = m.sortByTitle(results)

print "Sorted by Title"

# expression: enumerate(sortedResultByTitle)
# explanation: this generates some kind of iterator, that puts the index of the every list-entry in front of it
# example: (0, first), (1, second), ...
#
# expression: list(enumerate(…))
# explanation: we know that enumerate returns some kind of iterator. for printing the contents, we have to build a sequence like a list.
# example: [ (0, first), (1, second), (2, third), ... ]
pprint(list(enumerate(sortedResultByTitle)))

"""
The following while-loop will continue looping until the break-statement is hit
and that will happen, if there is no error occurring within the try-clause
"""
while True:
    try:
        # throws an error, if input can not be converted to integer
        song_number = int(raw_input("Choose song by number: "))
        # throws an error, if song_number is not within those boundaries
        assert 0 <= song_number < len(sortedResultByTitle)
    except:
        print "Error"
    else:
        break

webbrowser.open_new_tab(sortedResultByTitle[song_number]['link'])

rawDescription: How should I write my method to get the 'link' value from the result I get after searching through JSON?


code:
import pygame
import pygame.locals

pygame.init()

screen = pygame.display.set_mode((300,200))

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False
            elif event.key == pygame.K_a and pygame.key.get_mods() & pygame.KMOD_SHIFT:
                print "pressed: SHIFT + A"

pygame.quit()

rawDescription: Python keydown combinations (ctrl + key or shift + key)


code: print "%s" % 5

rawDescription: How to Send Packets to a Remote Minecraft Classic Server in Python?


code:
import distutils.sysconfig as sysconfig
import os
std_lib = sysconfig.get_python_lib(standard_lib=True)
for top, dirs, files in os.walk(std_lib):
    for nm in files:
        if nm != '__init__.py' and nm[-3:] == '.py':
            print os.path.join(top, nm)[len(std_lib)+1:-3].replace('\\','.')

rawDescription: How can I get a list of all the Python standard library modules

description: Get a list of all the Python standard library modules


code:
from bs4 import BeautifulSoup

data = """

    test1

    test2

"""

soup = BeautifulSoup(data, 'html')

for div in soup.select('div[class=box]'):
    print div.text.strip()

rawDescription: Isolating divs in Beautiful Soup

description: Isolating divs in Beautiful Soup


code:
import os
inputdir = "/Location of directory that contains files"
files = []
for dirpath, dirnames, filenames in os.walk(inputdir):
  for filename in filenames:
    if filename.endswith('.h5'):
      pathname = os.path.join(dirpath, filename)
      files.append(pathname)
print files

rawDescription: Python - name output file to include part of input file name


code:
s1="This is a text with mathjax like $1<2$, let's see if this works till here $2>1$!"
import lxml.html.soupparser as sp
from lxml import html
soup1 = sp.fromstring(s1)
print sp.unescape(html.tostring(soup1, encoding='unicode'))

rawDescription: Parse '<' Symbol with lxml

description: Parse '<' Symbol with lxml


code:
fig2=plt.figure(figsize=(5,5),dpi=100)
ax1 = fig2.add_subplot(121)
ax2 = fig2.add_subplot(122)
ax1.scatter(x,y)
ax1.set_xlim(0,5)
ax2.set_ylim(0,5)
ax2.plot(range(10))

transDataToFig2 = ax1.transData+fig2.transFigure.inverted()
for ix,iy in zip(x,y):
    inDots = ax1.transData.transform((ix,iy))
    inFigIndirect = fig2.transFigure.inverted().transform(inDots)
    inFigDirect = transDataToFig2.transform((ix,iy))
    print inDots,"->",inFigIndirect," or ",inFigDirect

fig2.savefig('scatterPos2.png')

rawDescription: Matplotlib: Centroid of Scatter Plot Circles


code:
while True:
    username = raw_input("Please enter your username: ")
    password = getpass.getpass("Please enter your password: ")
    if username in credentials and password == credentials[username][0]:
        print "Logged in successfully as " + username
        break
    elif password in credentials.get(username, []):
        print "Specific error message " + username
    else:
        print "Login incorrect!"

rawDescription: Python Login Script; multiple passwords per account


code:
for idx, plist in enumerate(self.queueCode["PicklistValues"]["PickListValue"]):
    print "PickList with index {} has value {} and label {}".format(
        idx, plist["Value"], plist["Label"]
    )

rawDescription: Parsing PickList values from AutotaskAPI


code:
from itertools import takewhile
from string import digits

def find_between(s, first):
    try:
        start = s.index(first) + len(first)
        # Keep taking the next character while it's either a ':' or a digit
        # You can also just cast this into a list and forget about joining and later splitting.
        # Also, consider storing ':'+digits in a variable to avoid recreating it all the time
        return ''.join(takewhile(lambda char: char in ':'+digits, s[start:]))
    except ValueError:
        return ""

for _ in l:
    print _
    print find_between(s, _ + ':').split(':')

rawDescription: python - looping through values in a string

description: Python - looping through values in a string


code:
from itertools import izip_longest

rows = [line.split() for line in myfile]
columns = list(izip_longest(*rows, fillvalue="0"))
column_width = [max(len(num) for num in col) for col in columns]

# We make a template of the form "{0:>a} {1:>b} {2:>c} ...",
# where a, b, c, ... are the column widths:
column_template = "{{{0}:>{1}s}}"
row_template = " ".join(column_template.format(i, n) for
    i, n in enumerate(column_width))

print "\n".join(row_template.format(*row) for row in zip(*columns))

rawDescription: zero fill empty spaces at specific locations

description: Zero fill empty spaces at specific locations


rawDescription: Delete an item from a list by match not index


code:
if not name.startswith(('prefix', 'another', 'yetanother')):
     print name

rawDescription: In python, how do I exclude files from a loop if they begin with a specific set of letters?


code:
def _parse_args(args):
    parser = argparse.ArgumentParser(
        description='Script to configure appliance.'
    )
    parser.add_argument('--config_file',
                        help='Path to a JSON configuration file')
    print parser.parse_args(args)
    return parser.parse_args(args)

rawDescription: Python Unit Test: Mimic a command line argument for test purposes


code:
code = """
from module1 import method1
from module2 import method2

class foo(object):
   def say_foo(self):
      method1()
   def talk(self):
      method2()
"""

import ast, _ast

t = ast.parse(code)
for node in ast.walk(t):
    if isinstance(node, ast.ClassDef) and node.name == "foo":
        klass = node
        break

for node in ast.walk(klass):
    if isinstance(node, _ast.Call):
        print node.func.id

rawDescription: Python How to list all methods/classes that have been imported into a class


code:
def euclid(a, b):
    '''Solve x*a + y*b = ggt(a, b) and return (x, y, ggt(a, b))'''
    # Non-recursive approach hence suitable for large numbers
    x = yy = 0
    y = xx = 1
    while b:
        q = a // b
        a, b = b, a % b
        x, xx = xx - q * x, x
        y, yy = yy - q * y, y
    return xx, yy, a

def inv(a, n):
    '''Perform inversion 1/a modulo n. a and n should be COPRIME.'''
    # coprimality is not checked here in favour of performance
    i = euclid(a, n)[0]
    while i < 0:
        i += n
    return i

def add(a,p,P,Q):
   #Check For Neutral Element
   if P == (0,0) or Q == (0,0):
       return (P[0]+Q[0],P[1]+Q[1])

   #Check For Inverses (Return Neutral Element - Point At Infinity)
   if P[0] == Q[0]:
       if (-P[1])%p == Q[1] or (-Q[1])%p == P[1]:
           return (0,0)

   #Calculate Slope
   if P != Q:

       # perform multiplication with the inverse modulo p
       s = (Q[1]-P[1]) * inv(Q[0]-P[0], p)
   else:
       s = ((3*(P[0]*P[0])+a)%p) ** (2*P[1])

   #Calculate Third Intersection
   x = s*s - P[0] - Q[0]
   y = (s*(P[0]-x)) - P[1]

   y = y%p
   x = x%p

   return (x,y)

r = (5,1)
for i in range(1,20):
   print i,':',r
   r = add(2,17,r,(5,1))

rawDescription: ECC - Unable to produce entire cyclic group


code:
from datetime import datetime
import re

from bs4 import BeautifulSoup

data = '
January 13, 1991: At least 40 people
'
soup = BeautifulSoup(data)

date_string, rest = soup.li.text.split(':', 1)

print datetime.strptime(date_string, '%B %d, %Y').strftime('%d/%m/%Y')
print re.match(r'At least (\d+)', rest.strip()).group(1)

rawDescription: Web crawler to extract in between the list


rawDescription: Uniqueness in a list of lists with lists


code:
from math import radians, cos, sin, asin, sqrt

class mypointpair(object):
    def __init__(self):
        self.coord=[]
        pass
    def add_coords(self,a,b):
        self.coord.append((a,b))
    def __len__(self):
        return self.haversine(self.coord[0][0], self.coord[0][1], self.coord[1][0], self.coord[1][1])

    def haversine(self,lon1, lat1, lon2, lat2):
        """
        Calculate the great circle distance between two points
        on the earth (specified in decimal degrees)
        """
        # convert decimal degrees to radians
        lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
        # haversine formula
        dlon = lon2 - lon1
        dlat = lat2 - lat1
        a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
        c = 2 * asin(sqrt(a))
        km = 6367 * c
        return km

pp1=mypointpair()
pp1.add_coords(53.32055555555556 , -1.7297222222222221 )
pp1.add_coords(53.31861111111111, -1.6997222222222223 )

print len(pp1)

rawDescription: Need for float __len__, not integer

description: Need for float __len__, not integer


code:
class A():
    def go(self):
        b=B()
        b.func()

class B():
    def func(self):
        import inspect
        print inspect.currentframe().f_back.f_locals['self']

if __name__ == '__main__':
    a = A()
    a.go()

rawDescription: Introspect calling object

description: Introspect calling object


code:
from collections import Counter
c = Counter()

for row in data:
    try:
        name, what, cnt = row
    except ValueError: # Empty/broken line
        continue
    try:
        cnt = int(cnt)
    except ValueError: # Invalid quantity
        continue
    c[(name, what)] += cnt

for k in sorted(c):
    print "%s, %s, %d" % (k + (c[k], ))

rawDescription: Python - Create total sum of items per person from a csv file


rawDescription: Check list of tuples where first element of tuple is specified by defined string


code:
import numpy as np
import scipy.optimize as opt

#Returns the distance from a point to the list of spheres
def calc_distance(point):
    return np.power(np.sum(np.power(centers-point,2),axis=1),.5)-rad

#Latitude/longitude to carteisan
def geo2cart(lat,lon):
    lat=np.deg2rad(lat)
    lon=np.deg2rad(lon)
    points=np.vstack((earth_radius*np.cos(lat)*np.cos(lon),
           earth_radius*np.cos(lat)*np.sin(lon),
           earth_radius*np.sin(lat))).T
    return points

#Cartesian to lat/lon
def cart2geo(xyz):
    if xyz.ndim==1: xyz=xyz[None,:]
    lat=np.arcsin(xyz[:,2]/earth_radius)
    lon=np.arctan2(xyz[:,1],xyz[:,0])
    return np.rad2deg(lat),np.rad2deg(lon)

#Minimization function.
def minimize(point):
    dist= calc_distance(point)
    #Here you can change the minimization parameter, here the distances
    #from a sphere to a point is divided by its radius for linear weighting.
    err=np.linalg.norm(dist/rad)
    return err

earth_radius = 6378
p1 = (31.2297, 121.4734, 3335.65)
p2 = (34.539, 69.171, 2477.17)
p3 = (47.907, 106.91, 1719.65)
p4 = (50.43, 80.25, 1242.27)

points = np.vstack((p1,p2,p3,p4))
lat    = points[:,0]
lon    = points[:,1]
rad    = points[:,2]

centers = geo2cart(lat,lon)

out=[]
for x in range(30):
    latrand=np.average(lat/rad)*np.random.rand(1)*np.sum(rad)
    lonrand=np.average(lon/rad)*np.random.rand(1)*np.sum(rad)
    start=geo2cart(latrand,lonrand)
    end_pos=opt.fmin_powell(minimize,start)
    out.append([cart2geo(end_pos),np.linalg.norm(end_pos-geo2cart(36.989,91464))])

out = sorted(out, key=lambda x: x[1])
print 'Latitude:',out[0][0][0],'Longitude:',out[0][0][1],'Distance:',out[0][1]

rawDescription: Finding an unknown point using weighted multilateration


code: for f in tree[0]: print f.getName(), f.asList()

rawDescription: Iterating through a pyparsing tree yields only unique items?


code:
import resource
import time

from stoppable_thread import StoppableThread

class MyLibrarySniffingClass(StoppableThread):
    def __init__(self, target_lib_call, arg1, arg2):
        super(MyLibrarySniffingClass, self).__init__()
        self.target_function = target_lib_call
        self.arg1 = arg1
        self.arg2 = arg2
        self.results = None

    def startup(self):
        # Overload the startup function
        print "Calling the Target Library Function..."

    def cleanup(self):
        # Overload the cleanup function
        print "Library Call Complete"

    def mainloop(self):
        # Start the library Call
        self.results = self.target_function(self.arg1, self.arg2)

        # Kill the thread when complete
        self.stop()

def SomeLongRunningLibraryCall(arg1, arg2):
    max_dict_entries = 2500
    delay_per_entry = .005

    some_large_dictionary = {}
    dict_entry_count = 0

    while(1):
        time.sleep(delay_per_entry)
        dict_entry_count += 1
        some_large_dictionary[dict_entry_count]=range(10000)

        if len(some_large_dictionary) > max_dict_entries:
            break

    print arg1 + " " +  arg2
    return "Good Bye World"

if __name__ == "__main__":
    # Lib Testing Code
    mythread = MyLibrarySniffingClass(SomeLongRunningLibraryCall, "Hello", "World")
    mythread.start()

    start_mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    delta_mem = 0
    max_memory = 0
    memory_usage_refresh = .005 # Seconds

    while(1):
        time.sleep(memory_usage_refresh)
        delta_mem = (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss) - start_mem
        if delta_mem > max_memory:
            max_memory = delta_mem

        # Uncomment this line to see the memory usuage during run-time
        # print "Memory Usage During Call: %d MB" % (delta_mem / 1000000.0)

        # Check to see if the library call is complete
        if mythread.isShutdown():
            print mythread.results
            break;

    print "\nMAX Memory Usage in MB: " + str(round(max_memory / 1000.0, 3))

rawDescription: Tracking *maximum* memory usage by a Python function


code:
data['Category'] = data.Description.map(category)

print data

rawDescription: Create column of categories from a column of transactions using regular expressions within a dictionary


code:
subcolors = {'darkblue': ['first', 'second'],
             'teal': ['first-teal', 'second-teal'],
             'pink': ['first-pink', 'second-pink'],
             # etc..
            }

for (color,value) in colors:
    print color #teal, pink, red, etc...
    for val in subcolors[color]:
        print val #first-teal, second-teal, first-pink, second-pink, etc...

rawDescription: python help listing values in nested for loop

description: Python help listing values in nested for loop


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

rawDescription: Python - counting up in hex

description: Python - counting up in hex


code:
import pandas as pd
import io

temp=u"""A,B,C,D,E,F,G
        1,2,3,4,5,6,7"""

temp1=u"""Name
B
B
C
B
C
C
E
F"""

subset = pd.read_csv(io.StringIO(temp1), index_col=[0]).index.unique()
print subset

#use subset as filter of columns
df = pd.read_csv(io.StringIO(temp), usecols=subset)

print df.head()
print df.shape

rawDescription: How to improve the code with more elegant way and low memory consumed?

description: Improve the code with more elegant way and low memory consumed


code:
import calendar
import collections
from datetime import date

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

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


code:
import multiprocessing as mp
import datetime as dt
def work():
    t = dt.datetime.now()
    print mp.current_process().name, t
    i = 0
    while i < 100000000:
        i+=1
    t2 = dt.datetime.now()
    print mp.current_process().name, t2, t2-t

if __name__ == '__main__':
    print "single process:"
    t1 = mp.Process(target=work)
    t1.start()
    t1.join()

    print "multi process:"
    t1 = mp.Process(target=work)
    t1.start()
    t2 = mp.Process(target=work)
    t2.start()
    t1.join()
    t2.join()

rawDescription: Parallelism in python isn't working right


code:
allFruits = set()
for i,(k,v) in enumerate(sorted(dd.iteritems())):
    allFruits.update(v)
    print i+1, len(allFruits)

rawDescription: Cumulative count of unique values in a dictionary