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

domain = "http://www.ncbi.nlm.nih.gov/"
url_tail = "pubmed?term=(%222013%22%5BDate%20-%20Publication%5D%20%3A%20%222013%22%5BDate%20-%20Publication%5D)"
url = domain + url_tail

browser = webdriver.PhantomJS()
browser.get(url)

def extract_data(browser):
    titles = browser.find_elements_by_css_selector("div.rprt div.rslt p.title a")
    return [title.text for title in titles]

page_start, page_end = 1, 10

with open('titles.txt', 'a') as f:
    for page in range(page_start, page_end):
        WebDriverWait(browser, 10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "div.rprt p.title"))
        )

        for line in extract_data(browser):
            f.write(line.encode('utf-8') + '\n')

        print "page %d" % page

        browser.find_element_by_css_selector("div.pagination a.next").click()

browser.close()

rawDescription: get python UnicodeEncodeError when i was using selenium to crawl a website

description: Get python UnicodeEncodeError when i was using selenium to crawl a website


rawDescription: Regex - Expression for matching international characters doesn't work when I add ^ $


code:
# for each class in a's module...
for klass in inspect.getmembers(inspect.getmodule(a), inspect.isclass):
    # see if a is in that class
    if a in [x[1] for x in inspect.getmembers(klass[1], inspect.isclass)]:
        print a, "is a member of", klass[0]

rawDescription: How to find out the nested class hierarchy?

description: Find out the nested class hierarchy


code:
print 'Hello...'
os.system('clear') #or os.system('cls') for Windows
print "All cleared!"

rawDescription: Clearing previous terminal output in python


code:
d = {1: {'AA': {'a': 100, 'b': 1, 'c': 45}},
     2: {'AA': {'c': 2}},
     3: {'BB': {'d': 122, 'a': 4, 't': 22, 'r': 23, 'w': 12}},
     4: {'CC': {'y': 12, 'g': 15, 'b': 500}}}

def sortkey(x):
   num,d1 = x
   key,d2 = d1.items()[0] #Some may prefer `next(d.iteritems())`
   return len(d2),max(d2.values())

exactly_how_you_want_it = [([k] + v.keys() + v.values()) for k,v in
                           sorted(d.items(),reverse=True,key=sortkey)]

for item in exactly_how_you_want_it:
   print item

rawDescription: How to sort a complex nested dictionary to a nested list

description: Sort a complex nested dictionary to a nested list


code:
from itertools import groupby
from operator import itemgetter

mydict={
        ('a', 1): 0,
        ('a', 2): 0,
        ('a', 3): 0,
        ('a', 4): 1,
        ('a', 5): 2,
        ('a', 6): 2,
        ('a', 7): 0,
        ('a', 8): 0,
}

data = mydict.items()
data.sort()

def groupkey(item):
    return item[0][0], item[1]

result = {}
for v, group in groupby(data, key=groupkey):
    char, value = v
    nums = [item[0][1] for item in group]
    result[char, min(nums), max(nums)] = value

print result

rawDescription: aggregate dictionary items into summary results

description: Aggregate dictionary items into summary results


code:
for name, player in doc['RealBank']['Players'].items():
    print name, player['Balance']

rawDescription: How can I filter a YAML dataset with an attribute value?

description: Filter a YAML dataset with an attribute value


code:
import paramiko

client = paramiko.SSHClient()

client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

client.connect('127.0.0.1')

stdin, stdout, stderr = client.exec_command('date')

for line in stdout:
    print line,
print

rawDescription: Using Python to SSH, with the known hosts from file, to extract result of a command


code:
import sys

from my_module import RootClass

# Loop over all subclasses of RootClass.
for class_ in RootClass.__subclasses__():
    # from module import subclass.
    __import__(class_.__module__, globals(), locals(), [class_.__name__,])

    # Update the global namespace with the new class
    globals().update({class_.__name__:
                      getattr(sys.modules[class_.__module__],
                              class_.__name__)})

print ChildClass
# OutPut : <class 'my_module.ChildClass'>

rawDescription: How to automatically Import all subclasses of a root class in Python?

description: Automatically Import all subclasses of a root class in Python


rawDescription: Python - slice a list of list

description: Python - slice a list of list


code:
import mechanize

br = mechanize.Browser()
br.open("http://www.google.com/")

for f in br.forms():
    print f

rawDescription: python - mechanize (setting input into form)

description: Python - mechanize (setting input into form)


code:
from xml.dom import minidom

dom = minidom.parseString(xml_text)
params=dom.getElementsByTagName("Parameter")
for param in params:
    name=param.getAttribute("name")
    if name in data:
        for item in param.getElementsByTagName("*"): # You may change to "Result" or "Value" only
            item.firstChild.replaceWholeText(data[name])

print dom.toxml()

#write to file
open("output.xml","wb").write(dom.toxml())

rawDescription: Editing the XML texts from a XML file using Python


code:
def partition(it, item):
    a = [[]]
    for x in it:
        if x == item and len(a) == 1:
            a.append([item])
            a.append([])
        else:
            a[-1].append(x)
    return a

print partition((x for x in range(10)), 7)

rawDescription: More compact way to skip initial list items? (or partition list based on an item)


code:
from numbers import Number
class MyFloat(object):
  def __init__(self, a):
    self.a = a

  def __mul__(self, other):
    return type(self)(self.a * other.a)

  def __repr__(self):
    return str(self.a)

class MyFloatExt(MyFloat):
  def __init__(self, a):
    super(MyFloatExt,self).__init__(a)

  def __add__(self, other):
    return type(self)(self.a + other.a)

  def __mul__(self, other):
    if isinstance(other,Number):
      return type(self)(self.a * other)
    else:
      return super(MyFloatExt,self).__mul__(other)

a = MyFloatExt(0.5)
b = MyFloatExt(1.5)

c = a + b
print c

d = a * b
print d

e = d * c
print e

print isinstance(e, MyFloat)

f = e * 0.5
print f

print map(type,[a,b,c,d,e,f]) == [MyFloatExt]*6

rawDescription: Override __mul__ from a child class using parent implementation: leads to problems


code:
#!/usr/bin/python

import os, stat, subprocess, shlex, re, sys

dev=os.stat('/')[stat.ST_DEV]
major=os.major(dev)
minor=os.minor(dev)

out = subprocess.Popen(shlex.split("df /"), stdout=subprocess.PIPE).communicate()
m=re.search(r'(/[^\s]+)\s',str(out))

if m:
    mp= m.group(1)
else:
    print "cannot parse df"
    sys.exit(2)

print "'/' mounted at '%s' with dev number %i, %i" % (mp,major,minor)

rawDescription: Determine Device of Filesystem in Python


code:
name = raw_input("Enter your name: ")
print "Hello, " + name

rawDescription: Running Python in powershell?

description: Running Python in powershell


code:
def test_append(self):
        start_stats = {'step':250,'offset':13,'inc':113,'danger':0,'input':None}
        gen = StatGenerator(start_stats)
        table = PathTable(gen)

        stats_list = []

        for i in xrange(50):
            stats_list.append(next(table._row_gen).copy())
            print stats_list[i]

        assert stats_list[0]['step'] == 250

rawDescription: How to stop elements in list from changing?

description: Stop elements in list from changing


code:
image = [[(15, 103, 225), (0, 3, 19)],
        [(22, 200, 1), (8, 8, 8)],
        [(0, 0, 0), (5, 123, 19)]]

key = 'hello'

encoded_image = encode(image, key)
decoded_image = encode(encoded_image, key)

print encoded_image
print decoded_image

rawDescription: how to change values in a nested list depending on elements of two other different lists and function is difficult to read

description: Change values in a nested list depending on elements of two other different lists and function is difficult to read


code:
data = np.array(data)
filtered = data[data[:, 4] == 'Iris-virginica'][:, :3].astype(np.float)
print 'mean', np.mean(filtered, axis=0)
print 'var ', np.var(filtered, axis=0)

rawDescription: Best way to separate data into 3 classes

description: Separate data into 3 classes


code:
import re
tests = ["This is a sentence."
,"Hello, world."
,"Hi there; hi there."
,"i ate breakfast."
,"This is , a sentence."
,"What time is it?"]
rex = re.compile(r'^[A-Z][a-z]*(\s*|[a-z]*|(?<![\s])[;,])*[.]$')
for test in tests:
    print rex.match(test)

rawDescription: Regular expression of a sentence

description: Regular expression of a sentence


code:
import struct

class Particle:
    """A single particle.  Attributes added in BinFile."""
    pass

class BinFile:
    """Parse and store the contents of a RealFlow .bin file."""
    def __init__(self, fname):
        self.bindata = open(fname, "rb").read()
        self.off = 0

        self.verify = self.peel("=i")[0]
        assert self.verify == 0xfabada
        self.name = self.string(250)

        (self.version, self.scale, self.fluid_type, self.simtime, self.frame_number,
         self.fps, self.num_particles, self.radius) = self.peel("=hfifiiif")
        self.pressure = self.peel("=fff")
        self.speed = self.peel("=fff")
        self.temperature = self.peel("=fff")
        if self.version >= 7:
            self.emitter_position = self.peel("=fff")
            self.emitter_rotation = self.peel("=fff")
            self.emitter_scale = self.peel("=fff")

        self.particles = [self.peel_particle() for i in range(self.num_particles)]

    def peel_particle(self):
        """Read one particle from the file."""
        p = Particle()
        p.position = self.peel("=fff")
        p.velocity = self.peel("=fff")
        p.force = self.peel("=fff")
        if self.version >= 9:
            p.vorticity = self.peel("=fff")
        if self.version >= 3:
            p.normal = self.peel("=fff")
        if self.version >= 4:
            p.neighbors = self.peel("=i")[0]
        if self.version >= 5:
            p.texture = self.peel("=fff")
            p.infobits = self.peel("=h")[0]
        (p.age, p.isolation_time, p.viscosity, p.density, p.pressure, p.mass,
         p.temperature, p.id) = self.peel("=fffffffi")
        print p.id, p.neighbors, p.position
        return p

    def peel(self, fmt):
        """Read some struct data from `self.bindata`."""
        data = struct.unpack_from(fmt, self.bindata, self.off)
        self.off += struct.calcsize(fmt)
        return data

    def string(self, length):
        s = self.bindata[self.off:self.off+length].split("\0")[0]
        self.off += length
        return s

b = BinFile("Circle0100001.bin")
print "Name:", b.name
print "Particles:", b.num_particles
print "Position of first particle", b.particles[0].position

rawDescription: How to read .bin files?

description: Read .bin files


rawDescription: Simplfy row AND column extraction, numpy


rawDescription: How to improve the speed of my selection process, python

description: Improve the speed of my selection process, python


code:
for i, j in enumerate(cycle([2, 4])):
    if i == 10:  # Only loop 10 times
        break
    print j

rawDescription: Oscillating Values in Python

description: Oscillating Values in Python


code:
print np.dtype(float).itemsize
print np.dtype(np.float32).itemsize
print np.dtype('|S10').itemsize

rawDescription: Numpy size of data type

description: Numpy size of data type


code:
data = mixture.DataSet()
data.fromArray(mix)

# start them off with something arbitrary (probably based on a guess from the figure)
n1 = mixture.NormalDistribution(-1,1)
n2 = mixture.NormalDistribution(1,1)
m = mixture.MixtureModel(2,[0.5,0.5], [n1,n2])

# perform expectation maximization
m.EM(data, 40, .1)
print m

rawDescription: Separate mixture of gaussians in Python

description: Separate mixture of gaussians in Python


code:
p = multiprocessing.Pool()
rs = p.imap_unordered(do_work, xrange(num_tasks))
p.close() # No more work
while (True):
  completed = rs._index
  if (completed == num_tasks): break
  print "Waiting for", num_tasks-completed, "tasks to complete..."
  time.sleep(2)

rawDescription: Show the progress of a Python multiprocessing pool map call?


code:
from bs4 import BeautifulSoup
import mechanize

br = mechanize.Browser()
br.addheaders = [('User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.122 Safari/537.36')]
br.open("http://de.fakenamegenerator.com/gen-male-gr-gr.php")

soup = BeautifulSoup(br.response())
address = soup.find('div', attrs={"class": "address"})

print address.h3.text.strip()
print address.find('div', attrs={'class': 'adr'}).text.strip()

rawDescription: How to extract it from HTML with Python?

description: Extract it from HTML with Python


rawDescription: How to split a line of txt file into 3 values per line

description: Split a line of txt file into 3 values per line


code:
def foo_wrapper(wrapped_func):
    wrapped_func = wrapped_func.__func__
    def _w(*args, **kwargs):
        print '<foo_wrap>'
        wrapped_func(*args, **kwargs)
        print '</foo_wrap>'
    return classmethod(_w)

rawDescription: How to wrap (monkey patch) @classmethod

description: Wrap (monkey patch) @classmethod


code:
import cookielib
import urllib2
import mechanize

# Browser
br = mechanize.Browser()

# Enable cookie support for urllib2
cookiejar = cookielib.LWPCookieJar()
br.set_cookiejar( cookiejar )

# Broser options
br.set_handle_equiv( True )
br.set_handle_gzip( True )
br.set_handle_redirect( True )
br.set_handle_referer( True )
br.set_handle_robots( False )

# ??
br.set_handle_refresh( mechanize._http.HTTPRefreshProcessor(), max_time = 1 )

br.addheaders = [ ( 'User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1' ) ]

# authenticate
br.open( the/page/you/want/to/login )
br.select_form( name="the name of the form from above" )
# these two come from the code you posted
# where you would normally put in your username and password
br[ "USERID" ] = yourLogin
br[ "PASSWDTXT" ] = yourPassword
res = br.submit()

print "Success!\n"

rawDescription: Python Mechanize - Login

description: Python Mechanize - Login


code:
import random

def random_line(f):
    return random.choice([line for line in f])

f = open("sample.txt", 'r')

print random_line(f)

rawDescription: How to put random lines from a file into a string in python?

description: Put random lines from a file into a string in python


code:
import re
p = re.compile(ur'{([^}]+)}')
test_str = u"{'AuthorSite': None,\n 'FirstText': None,\n 'Image': None,\n 'SrcDate': None,\n 'Title': None,\n 'Url': None}"

re.findall(p, test_str)

rawDescription: Regular expressions matching across multiple line in Sublime Text


code:
import datetime
import time
src = "161,00:00:00,8.19,1,4.46,4,7.87,4,6.54,null,4.45,6,2010-04-12"
asFloat = lambda x : float(x)
asInt = lambda x : int(x)
asTime = lambda x : datetime.datetime(*time.strptime(x,'%H:%M:%S')[:7]).time()
asDate = lambda x : datetime.datetime(*time.strptime(x,'%Y-%m-%d')[:3]).date()
asNone = lambda x : None if x=='null' else int('ValueError')
converters = (asDate,asTime,asInt,asFloat,asNone,)
def convert(x):
  for cnv in converters:
    try:
      return cnv(x)
    except ValueError:
      pass
  return x

clean_data = [convert(s) for s in src.split(',')]
print clean_data

rawDescription: Error when feeding a mysql db with a python-parsed data


code:
t0 = time()
out=io.open(fnam + '_out5.txt', 'w')
with io.open(fnam, 'r', encoding='utf8') as fin:
    for line in fin:
        out.write(rm_control_chars_1(line))
out.close()
print time() - t0

rawDescription: Is there a faster way to clean out control characters in a file?


code:
class Formula():
    def __init__(self, formula):
        self.formula = formula

    def parse(self, d):
        return eval(self.formula.format(**d))

formula = Formula('{units}/({units}+{other_units})*100')
d = {'units': 100.0, 'other_units': 50.0}

my_stat = formula.parse(d)

print my_stat

rawDescription: python module/framework for math formulas?

description: Python module/framework for math formulas


code:
def get_field_value(soup, field):
    return soup.find('div', text=field+':').find_next_sibling('div', class_='dva').text

soup = BeautifulSoup(data)
print get_field_value(soup, 'Telefon')  # prints 013 / 688 27 78
print get_field_value(soup, 'Mobil')  # prints 0993 999 999

rawDescription: BeautifulSoup: get tag text behind another tag


code:
# main.py

import settings
import subfile

settings.init()          # Call only once
subfile.stuff()         # Do stuff with global var
print settings.myList[0] # Check the result

rawDescription: Using global variables between files in Python?


code:
from mock import Mock, MagicMock, patch

sep = '***********************\n'

# non-mock mocks
def get_a():
    return A()
def extern_func():
    return 'foo'

def do_stuff(self):
    # get a new instance of A
    a = get_a()
    while True:
        a.update()
        print a.state
        if a.state == 'state':
            break

class A(object):
    def __init__(self):
        self.state = ''

    def update(self):
        # call to external system
        self.state = extern_func()

rawDescription: Mocking a python class which changes an instance variable after a method call?


code:
instrument = visa.instrument('GPIB::01')  # Assuming a GPIB device on channel 1
print instrument.stb  # Print status register

rawDescription: Python VISA serial polling function

description: Python VISA serial polling function


code: for i in range(2,2): print i

rawDescription: When range is range(x, x)

description: When range is range(x, x)


code:
# Update the dictionary list entry matching the id in dictitem with a new position
def updatedictlist(dictlist, dictitem):

    # Get the dict item from the dict list matching the id in dictitem
    upditem = next(item for (index, item) in enumerate(dictlist) if item['_id'] == dictitem['_id'])

    # If there is a StopIteration exception then the dictitem position we
    # are trying to update TO doesn't exist so we can go ahead and update
    # the dictitem with a new position without making any other changes.
    try:
        dstitem = next(item for (index, item) in enumerate(dictlist) \
                            if item['position'] == dictitem['position'])
    except StopIteration:
        # Update the dictlist and return the list.
        upditem['position'] = dictitem['position']
        return dictlist

    # We only want to update the values between range of values we care about
    # leaving the rest alone.
    rangelow, rangehigh = (min(upditem['position'], dstitem['position']), \
                           max(upditem['position'], dstitem['position']))

    # Since we are replacing a value it matters whether the destination is
    # ahead of us or behind us.
    direction = -1 if upditem['position'] - dstitem['position'] < 0 else 1

    # Shift the values that fall within the position range we are processing
    for item in dictlist:
        if (rangelow <= item['position'] <= rangehigh):
            item['position'] += direction

    # Finally process the dictitem we wanted to update
    upditem['position'] = dictitem['position']

    return dictlist

items = [{'position': 0, '_id': 'Bob'}, {'position': 1, '_id': 'Tom'}, {'position': 2, u'_id': 'Sam'}]
data = {'_id': 'Tom', 'position': 2}
print updatedictlist(copy.deepcopy(items), data)

data = {'_id': 'Bob', 'position': 2}
print updatedictlist(copy.deepcopy(items), data)

rawDescription: Updating array of dicts position field

description: Updating array of dicts position field


code:
cur.execute('select * from ' + tablename1 + ' WHERE unique_column=?', ('somevalue',))
row = cur.fetchone()
if row is not None:
    # there was a matching row, rejoice
    print row

rawDescription: SQLite3, Python: fetchone() works on table1 but not table2 but fetchall() works on both


rawDescription: Determine number of row in matrix with duplicate rows


code:
result = []
for item in testlist:
    temp = item[:]
    for number1, number2 in reflist:
        if number1 == item[1]:
            temp.append(number2)
            result.append(temp)
            break
    else:
        result.append(temp)

print result
# [['asd', 7, 2], ['bla', 5, 948], ['hi', 3, 123]]

rawDescription: How to compare one value from a list of lists to another value from a list of lists in Python

description: Compare one value from a list of lists to another value from a list of lists in Python


code:
class CrazyList(object):
    def append(self, value):
        def new_append(value):
            print "Hello world"

        self.append = new_append

rawDescription: Bytecode optimization

description: Bytecode optimization


code: False = True

rawDescription: Detecting built-in redefinition (False = True)


code:
n = 10
import itertools, operator
for cuts in itertools.combinations_with_replacement(range(n+1), 3):
    combi = map(operator.sub, cuts + (n,), (0,) + cuts)
    if max(combi) < n:
        print combi

rawDescription: Find all combination that sum to N with multiple lists


code:
for i, val in enumerate(range(600)):
    if i == 50:
        break
    print i

rawDescription: How do I return (or print) a maximum number of hits from a statement?

description: Return (or print) a maximum number of hits from a statement


code:
#------------------------------------------------------------------------------
import sympy
import sympy.mpmath
import matplotlib.pyplot as plt
import json
import pprint

import Library_GenerateBesselFunction
import Library_SympyChebyShevApproximationOneDimension
import Library_SympyExpressionToPythonFunction
import Library_GraphOneDimensionalFunction

ApproximationOrder = 10

#CREATE THE EXAMPLE EXRESSION:
Kind = 1
Order = 2
ExampleSympyExpression = sympy.sin(sympy.Symbol('x'))

"""
Library_GenerateBesselFunction.Main(
    ResultType =  'sympy',
    Kind =  Kind,
    Order =  Order,
    VariableNames = ['x'],
    )
"""
PythonOriginalFunction = Library_SympyExpressionToPythonFunction.Main(
    ExampleSympyExpression ,
    FloatPrecision = 100,
    )

#CREATE THE NATIVE CHEBY APPROXIMATION

ChebyDomainMin = 5.
ChebyDomainMax = 10.
ChebyDomain = [ChebyDomainMin, ChebyDomainMax]
ChebyExpandedPolynomialCoefficients, ChebyError = sympy.mpmath.chebyfit(
    PythonOriginalFunction,
    ChebyDomain,
    ApproximationOrder,
    error=True
    )
print 'ChebyExpandedPolynomialCoefficients'
pprint.pprint( ChebyExpandedPolynomialCoefficients )
def PythonChebyChevApproximation(Point):
    Result = sympy.mpmath.polyval(ChebyExpandedPolynomialCoefficients, Point)
    return Result

#CREATE THE GENERIC ONE DIMENSIONAL CHEBY APPROXIMATION:
SympyChebyApproximation = Library_SympyChebyShevApproximationOneDimension.Main(
    SympyExpression = ExampleSympyExpression*sympy.cos( sympy.Symbol('a') ),
    ApproximationSymbol = sympy.Symbol('x'),
    DomainMinimumPoint = [ChebyDomainMin],
    DomainMaximumPoint = [ChebyDomainMax],
    ApproximationOrder = ApproximationOrder
    )

print 'SympyChebyApproximation', SympyChebyApproximation

SympyChebyApproximation = SympyChebyApproximation.subs(sympy.Symbol('a'), 0.0)

print 'SympyChebyApproximation', SympyChebyApproximation

PythonCastedChebyChevApproximationGeneric = Library_SympyExpressionToPythonFunction.Main(
    SympyChebyApproximation ,
    FloatPrecision = 100,
    )

print 'PythonCastedChebyChevApproximationGeneric(1)', PythonCastedChebyChevApproximationGeneric(1.)

rawDescription: Python Sympy Arbitrary Approximation to Arbitrary Sympy Expression?


code:
from lxml import etree
from collections import defaultdict
from pprint import pprint

doc="""<?xml version="1.0" encoding="utf-8"?>
<Basic>
    <Segment>
        <Sample value="12" data2="25" data3="23"/>
        <Sample value="13" data2="0" data3="323"/>
        <Sample value="14" data2="2" data3="3"/>
    </Segment>
</Basic>
"""
el = etree.fromstring(doc)
data2 = el.xpath('//@data2')
dataX = el.xpath('//@*[starts-with(name(), "data")]')
print data2
print dataX

# With iteration over Sample elements, like in J.F. Sebastian answer, but with XPath
d = defaultdict(list)
for sample in el.xpath('//Sample'):
    for attr_name, attr_value in sample.items():
        d[attr_name].append(attr_value)

pprint(dict(d))

rawDescription: LXML, how to get multiple set of attributes to lists


code:
def itemmap(dicts, key):
    for item in dicts:
        yield item[key]

for dict_elem in itemmap(dict_lst, 'a'):
    print dict_elem

rawDescription: Refer to dictionary element in 'for' loop


rawDescription: How to find the maximum distance between five points?

description: Find the maximum distance between five points


code:
class Land(object):
    def __init__(self):
        print "a new Land"
        self.farms = []

    def addfarm(self):
        self.farms.append(Farm())

    def __iter__(self):
        for farm in self.farms:
            yield farm

class Farm(object):
    def __init__(self):
        print "a new farm"
        self.animals = {}

    def addanimal(self,name):
        if not name in self.animals:
            self.animals[name] = Animal(name)
            return True
        return False

    def __iter__(self):
        for name, animal in self.animals.iteritems():
            yield animal

class Animal(object):
    def __init__(self, name):
        print "hi, I am %s" % name
        self.name = name

rawDescription: python multiple nested classes

description: Python multiple nested classes


code: print wnl.lemmatize('going') #going

rawDescription: Remove word extension in python

description: Remove word extension in python


code:
import random

def g3(func):
    def g4(a):
        def g5(b):
            print a
            return a+b
        return g5

    if random.random() < 0.1:
        return g4(1)
    else:
        return g4(func(1))

f1(g3)

rawDescription: Decoding a Cryptic Python statement

description: Decoding a Cryptic Python statement


code:
cmd = ["bash", 'childProc.sh']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                          stderr=subprocess.STDOUT,
                          close_fds=True)
time.sleep(3)
print 'killing pid', p.pid
os.kill(p.pid, signal.SIGTERM)
print "timed out and killed child, collecting what output exists so far"
out  = p.communicate()[0]
print "got it", out

rawDescription: Python's subprocess.Popen object hangs gathering child output when child process does not exit


rawDescription: Check if file line sttime is older than 2 days - Python


code:
def print_multiples(n, m = 10):
    for i in range(0, m + 1):
        print n * i,
    print ""

rawDescription: How to generalize a multiplication table for (n * m)

description: Generalize a multiplication table for (n * m)


code: print getAbstract(text, lines=7)

rawDescription: Computing article abstracts

description: Computing article abstracts


code:
def main():
    def power(x):
        return x(r)

    funcs = [square, cube]
    for r in range(5):
        value = map(power, funcs)
        print value

rawDescription: map with lambda vs map with function - how to pass more than one variable to function?

description: Map with lambda vs map with function - how to pass more than one variable to function


code:
from itertools import groupby

things = [("animal", "bear"), ("animal", "duck"), ("plant", "cactus"), ("vehicle", "speed boat"), ("vehicle", "school bus")]

for key, group in groupby(things, lambda x: x[0]):
    for thing in group:
        print "A %s is a %s." % (thing[1], key)
    print " "

rawDescription: How do I use Python's itertools.groupby()?

description: Use Python's itertools.groupby()


code:
from urllib2 import urlopen
from bs4 import BeautifulSoup

soup = BeautifulSoup(urlopen('http://www.processorstore.nl/product/476816/category-212194/intel-core-i7-4790k.html'))

for spec in soup.select('div.product-specs > dl.product-specs--list > dt.product-specs--item-title'):
    print spec.get_text(strip=True)

rawDescription: Python Crawler Beatifulsoup decompose() function


code:
# Check
print 'Checking correct mapping for each particle F order'
for k in xrange(N):
    ii = box_index_F[k]
    jj = linind_F[tuple(binassign[:,k])]
    print 'particle %d %s (%d %d)' % (k,ii == jj,ii,jj)

print 'Checking correct mapping for each particle C order'
for k in xrange(N):
    ii = box_index_C[k]
    jj = linind_C[tuple(binassign[:,k])]
    print 'particle %d %s (%d %d)' % (k,ii == jj,ii,jj)

rawDescription: Numpy interconversion between multidimensional and linear indexing


code:
for note in range(22):
    print notes[note % len(notes)],

rawDescription: Python wrapping around on a list when list index is out of range


code:
def func(**args):
   class BindArgs(object):
       foo = args['foo']
       print 'foo is ', foo
       def __init__(self,args):
          print " hello i am here"
   return BindArgs

f = func(foo=3)
f(args="yellow")

rawDescription: Writing a class inside a function in python


code:
def td_with_du(tag):
    return tag.name == 'td' and tag.text.startswith('du')

print soup.find_all(td_with_du)

rawDescription: Beautiful Soup, searching by content of tag with tag in it


code:
def process_item(self, item, spider):
        try:
            self.cursor.execute("""UPDATE scraped SET score=%s, count=%s WHERE url=%s""", (item['reviewScore'], item['reviewCount'], item['reviewURL']))
            self.conn.commit()
        except MySQLdb.Error, e:
            print "Error %d: %s" % (e.args[0], e.args[1])

            return item

rawDescription: Saving Scrapy data to corresponding URL in MySQL


code:
def func(i):
   print id(i)

a="Some string"
print id(a)
func(a)

rawDescription: Python 2.7 Local Variables are acting like global variables


code: print x.rpartition('-')[0]

rawDescription: how to get the last part of a string before a certain character?

description: Get the last part of a string before a certain character


code:
from Tkinter import *
import ImageTk
import time

def scrollToTop():
    print "I'm in scrollToTop()"
    canvas.move(image, 0, -1)
    t.after(1000, scrollToTop)

t = Tk()
canvas = Canvas(t,height=256,width=256)
canvas.pack()

arrows = [1]
arrows[0] = ImageTk.PhotoImage(file="arrow.bmp")
image = canvas.create_image(20,100,image=arrows[0],tags="token")
scrollToTop()

t.mainloop();

rawDescription: Can't move image with time delay

description: Can't move image with time delay


rawDescription: Convert my list output to a dataframe in pandas


code:
for i in range(8, 12):
    print format(i, '#04x')

rawDescription: Convert an integer to a 2 byte Hex value in Python


code:
import string

fileName = raw_input("What file name: ")
lines = []
try:
    file = open(fileName)
    lines = file.readlines()
    file.close()
except:
    print "Unable to open file"
sum = 0
values = 0
if(len(lines) > 0):
    for line in lines:
        value = 0
        try:
            value = int(string.strip(line))
        except ValueError:
            pass
        if(value != 0):
            sum = sum + value
            values += 1
    print "Average = %f for %d lines, sum = %f"%(sum/values,values,sum)
else:
    print "No lines in the file"

rawDescription: Calculating average of numbers in a file


code:
def print_chord(note_list):
  clef = ['-----',
          '     ',
          '-----',
          '     ',
          '-----',
          '     ',
          '-----',
          '     ',
          '-----']

  for note in note_list:
    # insert the notes into the clef
    clef[note] = clef[note][:2] + 'o' + clef[note][3:]

  # lower notes have lower values, so reverse the list
  clef.reverse()

  for line in clef:
    # show the result to the user
    print line

rawDescription: Name those music notes - python console

description: Name those music notes - python console


code:
import requests

url = "http://spotcrime.com/#77801"
crimes_url = "http://api.spotcrime.com/crimes.json"

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36'}
with requests.Session() as session:
    session.headers = headers

    session.get(url)

    data = {
        "lat": "30.6423514",
        "lon": "-96.3704778",
        "radius": "0.02",
        "key": "spotcrime-private-api-key",
        "_": "1435453242689"
    }
    response = session.get(crimes_url, data=data)
    response = response.json()
    for item in response["crimes"]:
        print item

rawDescription: Finding specific frame in URL to scrape data using Python BeautifulSoup


code:
p = Person('John')
p2 = Person('Mary')
for personobject in Person:
    print personobject

rawDescription: Iterating over object instances of a given class in Python


code:
import gevent.monkey
gevent.monkey.patch_socket()
from gevent.pool import Pool
import requests

def check_urls(urls):

    def fetch(url):
        response = requests.request('GET', url, timeout=5.0)
        print "Status: [%s] URL: %s" % (response.status_code, url)

    pool = Pool(20)
    for url in urls:
        pool.spawn(fetch, url)
    pool.join()

rawDescription: Using grequests to send a pool of requests, how can I get the response time of each individual request?


code:
import shelve

# Write URLS to shelve
urls= ['http://www.airmagnet.com/', 'http://www.alcatel-lucent.com/',
       'http://www.ami.com/', 'http://www.apcc.com/', 'http://www.stk.com/',
       'http://www.apani.com/', 'http://www.apple.com/',
       'http://www.arcoide.com/', 'http://www.areca.com.tw/',
       'http://www.argus-systems.com/', 'http://www.ariba.com/',
       'http://www.asus.com.tw/']

s=set(urls)                        # Store URLs as set - Search is O(1)
sh=shelve.open('/tmp/shelve.tmp')  # Dump set (as one unit) to shelve file
sh['urls']=s
sh.close()

sh=shelve.open('/tmp/shelve.tmp')  # Retrieve set from file
s=sh['urls']
print 'http://www.apple.com/' in s # True
print 'http://matan.name/'    in s # False

rawDescription: Very basic persistence or data storage for a Python script


code:
class SettingProxy(models.Model):

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

    class Meta:
        abstract = True

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

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

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

rawDescription: Returning a custom model instance from a manager


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

rawDescription: Sorting a group of words inside a list

description: Sorting a group of words inside a list


code:
def printargs(*args):
    print args
import inspect
from functools import partial
inspect.isfunction(printargs)
f = partial(printargs, 1)
inspect.isfunction(f)
hasattr(f, "__call__")
hasattr(1, "__call__")
hasattr(printargs, "__call__")

rawDescription: <method-wrapper '__call__' of functools.partial object at 0x1356e10> is not a Python function


code:
from collections import defaultdict

lookup = defaultdict(lambda: [0,0,0])

lookup['a'][0] += 1
lookup['b'][1] += 1
lookup['a'][0] += 1

print lookup

rawDescription: Updating integer values of a dictionary in python


code:
from contextlib import contextmanager

@contextmanager
def context():
    print "entering"
    try:
        yield
    finally:
        print "exiting"

def test():
    with context():
        for x in range(10):
            yield x

for x in test():
    if x == 5:
        break  # or raise

rawDescription: Is there a meaningful way to use context managers inside generators?


code:
from bs4 import BeautifulSoup
import re
soup = BeautifulSoup(html_doc)
html_from_javascript = re.findall("varHTML = '(.*)';",soup.text)
print str(BeautifulSoup(html_from_javascript[0]))

rawDescription: BeautifulSoup doesn't read html within javascript with the right encoding


code:
xy = insertloc.values()
print xy
y = xy[0]
x = xy[1]
print y
print x

rawDescription: Get coordinates from location_once_scrolled_into_view result


code: print trimmed_text

rawDescription: Beautiful Soup [Python] and the extracting of text in a table


code:
#!usr/bin/python

n = 9
s = 'prime'
if n == 1:
    s = 'non-prime'
else:
    i = 2
    while i * i <= n:
        if n % i == 0:
            s= 'non-prime'
            break
        i = i + 1
print s

rawDescription: Ordering of evaluation using boolean or

description: Ordering of evaluation using boolean or


code:
while True:
    userinput = raw_input("Please enter your letters: ").lower()
    # create a tuple with all combinations of letters
    perms = tuple( ''.join(letter_permutation)
                   for letter_permutation in permutations(userinput) )
    # now check if any of the words starts or ends with any of the letter-perms
    for word in wordlist:
        # startswith, endswith also take tuples as arguments
        if word.startswith(perms) or word.endswith(perms):
            print "'%s' can be found in %s" % (userinput, word)

    if raw_input('Press q to exit, any key to continue... ').lower() == 'q':
        break

rawDescription: Finding words that start/end with input letters (tiles) from the user in a file


code:
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = ['Saya benci awak',
          'Saya cinta awak',
          'Saya x happy awak',
          'Saya geram awak',
          'Saya taubat awak']

# HERE YOU DO YOUR MAGIC: you open your file and load the list of STOP WORDS
stop_words = [unicode(x.strip(), 'utf-8') for x in open('stopword.txt','r').read().split('\n')]

vectorizer = TfidfVectorizer(analyzer='word', stop_words = stop_words)
X = vectorizer.fit_transform(corpus)
idf = vectorizer.idf_
print dict(zip(vectorizer.get_feature_names(), idf))

rawDescription: Sklearn - How to add custom stopword list from txt file


code:
from pylab import *

delta = 0.01
x = arange(-3.0, 3.0, delta)
y = arange(-3.0, 3.0, delta)
X,Y = meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = Z2 - Z1 # difference of Gaussians

cmap = cm.get_cmap('seismic', 5)    # PiYG

for i in range(cmap.N):
    rgb = cmap(i)[:3]
    print matplotlib.colors.rgb2hex(rgb)

im = imshow(Z, cmap=cmap, interpolation='bilinear',
            vmax=abs(Z).max(), vmin=-abs(Z).max())
axis('off')
colorbar()

show(block=True)

rawDescription: Extract matplotlib colormap in hex-format


code:
class _Infinity(object):

    def __lt__(self, other):
        return False

    def __gt__(self, other):
        return True

    def timetuple(self):
        return tuple()

import datetime
INF = _Infinity()
now = datetime.datetime.now()
print cmp(INF, now)
print cmp(now, INF)

rawDescription: Creating infinity and negative infinity in python for any object


code:
import Tkinter as tk
import bluetooth

class Bluetooth():

    def connect(self, name="My Phone"):

        target_name = name
        target_address = None

        nearby_devices = bluetooth.discover_devices()

        for bdaddr in nearby_devices:
            if target_name == bluetooth.lookup_name( bdaddr ):
                target_address = bdaddr
                break

        if target_address:
            print "found target bluetooth device with address ", target_address
        else:
            print "could not find target bluetooth device nearby"

    def retrieve_input(self):
        return self.textfield.get() #("0.0",'END-1c')

    def text_field(self):
        self.textfield = tk.Entry()
        self.textfield.pack()
        #self.textfield.pack_forget()

rawDescription: Replacing a string with text from Entry box


code:
while raw_input(">").lower() not in ("great", "good", "lacking", "poor"):
    print "I didn't understand that. Please try again."

rawDescription: Python: Repeating a function when raw_input is not valid


code:
import os
try:
    filename = "test123.pdf"
    folder = "someFolder"
    os.mkdir (folder)
    os.rename(filename,"someFolder\\test123.pdf")
except:
    if os.path.isdir(folder):
        os.rmdir(folder)
    print "File not found"

rawDescription: Skip all lines in try if one line fails

description: Skip all lines in try if one line fails


code:
import numpy as np
import timeit

x = range(1000)
# or
#x = np.random.standard_normal(1000)

def pure_sum():
    return sum(x)

def numpy_sum():
    return np.sum(x)

n = 10000

t1 = timeit.timeit(pure_sum, number = n)
print 'Pure Python Sum:', t1
t2 = timeit.timeit(numpy_sum, number = n)
print 'Numpy Sum:', t2

rawDescription: Python's sum vs. NumPy's numpy.sum

description: Python's sum vs. NumPy's numpy.sum


code:
import csv
reader = csv.reader(open("yourfile.csv", "rb"), delimiter=';', quoting=csv.QUOTE_NONE )
for row in reader:
    print row

rawDescription: python split function

description: Python split function


code:
arr = [10, 7, 15, 12, 3, 21]

main_stack = []

def get_min():
    if len(main_stack) == 0:
        return None
    return main_stack[-1][0]

def push(key):
    current_min = get_min()
    if current_min:
        if key < current_min:
            current_min = key
    else:
        current_min = key
    main_stack.append((current_min, key))

def pop():
    key = main_stack.pop()
    return key[1]

for key in arr:
    push(key)

def print_state():
    print(", ".join(str(x[1]) for x in main_stack))
    print("min: %d" %(get_min(),))

for _ in arr:
    print_state()
    print "popped:", pop()

rawDescription: Finding minumum element in a stack in constant time


code:
def function1(self, x, y):
    print 'Function 1 ', x
    print 'Function 2 ', y

rawDescription: Python Inheritance with closures

description: Python Inheritance with closures


code: print soup

rawDescription: Reading <content:encoded> tags using BeautifulSoup 4


code:
import itertools

print list(itertools.islice(ifibonacci(), 10))
# prints [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

print list(itertools.islice(ifibonacci(), 6, 6+1))
# prints [13] which isn't quite right; see below

rawDescription: How to get nth result from simple function in python?

description: Get nth result from simple function in python


code:
import gevent.monkey, gevent.socket
gevent.monkey.patch_all()
import socket

if socket.socket is gevent.socket.socket:
    print "gevent monkey patch has occurred"

rawDescription: How do I detect if Gevent's monkey-patching is active?

description: Detect if Gevent's monkey-patching is active


code:
s = numpy.sinc(x/numpy.pi)
print s.round(2)

rawDescription: Fixing the singularity of a function

description: Fixing the singularity of a function


code:
#!/usr/bin/python
import os, sys, subprocess, shlex, re
from subprocess import call
def probe_file(filename):
    cmnd = ['ffprobe', '-show_format', '-pretty', '-loglevel', 'quiet', filename]
    p = subprocess.Popen(cmnd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    print filename
    out, err =  p.communicate()
    print "==========output=========="
    print out
    if err:
        print "========= error ========"
        print err

probe_file('drop.avi')

rawDescription: Getting FFProbe Information With Python

description: Getting FFProbe Information With Python


code:
list_of_lists = [
    [4, 5, 6],
    [7, 0, 8],
    [6, 2, 9]
]

def index_of(value, matrix):
    for i, r in enumerate(matrix):
        for j, c in enumerate(r):
            if c == value:
                # returns the first instance
                return (i,j)
    return None

if index_of(0, list_of_lists) == (1,1):
    print "hey"

list_of_lists2 = [
    [0, 5, 6],
    [7, 0, 8],
    [0, 2, 9]
]

def indexes_of(value, matrix):
    return [(i,j) for i, r in enumerate(matrix) for j, c in enumerate(r) if c == value]

print indexes_of(0, list_of_lists2)

rawDescription: Python: Check an index against multiple other indexes


code: print map(hex,parities)

rawDescription: Python XOR on hex and string (parity check function)


code:
if verbose:
    def verboseprint(*args):
        # Print each argument separately so caller doesn't need to
        # stuff everything to be printed into a single string
        for arg in args:
           print arg,
        print
else:
    verboseprint = lambda *a: None      # do-nothing function

rawDescription: How to implement the --verbose or -v option into a script?

description: Implement the --verbose or -v option into a script


rawDescription: Identify groups of continuous numbers in a list


code:
def findbest(origarray, denom):
    current = origarray
    i = 1
    while(i < size):
        if(i in denom):
            current[i] = 1
            coinlist[i] = [i]
        else:
            k = 1
            while(k < 1 + (i/2)):
                c = current[k] + current[i-k]
                if(c < current[i]):
                    current[i] = c
                    coinlist[i] = coinlist[k] + coinlist[i-k]
                k+=1
        #print i, current[i], coinlist[i]
        i+=1
    return current

size = 100

def reset_cache():
  i = 1
  global coinlist
  coinlist = [[]]
  global origarray
  origarray = [0]

  while(i < size):
      origarray.append(100)
      coinlist.append([])
      i += 1

reset_cache()

denom = [1,5,10,25,50]

x = findbest(origarray, denom)

total=0

for value in findbest(origarray,denom):
    total += value

print total

print "\n\n\n"
print x

from itertools import combinations

best = ((0,0,0), 1e6)
for comb in combinations(range(1, 101), 3):
  #print "Considering: %s" % comb
  reset_cache()
  total = 0
  for value in findbest(origarray, comb):
    total += value
  if total < best[1]:
    print "%s beat best with %d" % (comb, total)
    best = (comb, total)

print best

rawDescription: Finding Optimal Change for Lowest Coin Total


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)

for schedbar in soup.find_all('div', 'schedbar'):
  print "start: " +  schedbar.find('div', 'schedbar_m').find('span', 'start').string
  print "end: " +  schedbar.find('div', 'schedbar_m').find('span', 'end').string

rawDescription: Regex handling of a phrase that may occur once or twice


code:
import itertools
seq = [[1,2,3], [1,2,3], [1,2,3]]
for item in itertools.product(*[itertools.permutations(sublist) for sublist in seq]):
    print item

rawDescription: Iterating members within lists within a tuple


code:
def write_data(self, my_dict=None):
    my_dict = my_dict or self.bookmark_data['roots']
    if 'name' in my_dict:
        print my_dict['name']

    for key, value in my_dict.items():
        if type(my_dict[key]) is dict:
            self.write_data(my_dict[key])
        elif type(my_dict[key]) is list:
            for item in my_dict[key]:
                self.write_data(item)
        else:
            print key, ": ",  my_dict[key]

rawDescription: How can I dynamically build a path/reference to a nested dictionary in python

description: Dynamically build a path/reference to a nested dictionary in python


code:
input = open('input')

for line in input.xreadlines():
  if line.startswith('firstName'):
    firstname = line.split()[1]
  elif line.startswith('lastName'):
    lastname = line.split()[1]
  elif line.startswith('family'):
    family = line.split()[1]

input.close()

print firstname
print lastname
print family

rawDescription: trouble with regular expressions

description: Trouble with regular expressions


code:
class Person(object):
    def bound(self):
            print "Hi, I'm bound."

def unbound(self):
    print "Hi, I'm unbound."

rawDescription: Why isn't self being automatically passed to a method that is set on an object after its instantiation?


code:
class Wizard(object):
    def __init__(self):
        self.mana = 0

    def __dir__(self):
        natdir = set(self.__dict__.keys() + dir(self.__class__))
        if self.mana <= 0:
            natdir.remove("domagic")
        return list(natdir)

    def addmana(self):
        self.mana += 1

    def domagic(self):
        if self.mana <= 0:
            raise NotEnoughMana()
        print "Abracadabra!"
        self.mana -= 1

rawDescription: Is it possible to delete a method from an object (not class) in python?


code:
print repr('АБВ\\u003d\\"res')
print repr('АБВ\\u003d\\"res'.decode('latin1'))
print 'АБВ\\u003d\\"res'.decode('latin1')

rawDescription: Python: decoding a string that consists of both unicode code points and unicode text


code:
import glob
path = "c:\\Python27\\test"

for fpath in glob.glob(os.path.join(path, '*.txt')):
    with open(fpath) as fin:
        print fpath, 'opened'

rawDescription: how to iterate over all files in path?

description: Iterate over all files in path


rawDescription: 3D Array in Python with mutliple data types


code:
print response['Domain required']
print response['minutes needed']
print response.Submit

rawDescription: Parsing responses from web.py

description: Parsing responses from web.py


code:
# Valid Code.
outputData = []

for i in range(100):
  outputData.append(str(i))

print ''.join(outputData)

# Again, also valid code, wildly different behavior.
outputData = []

for i in range(100):
  outputData.append(str(i))

  print ''.join(outputData)

rawDescription: indent python file (with pydev) in eclipse

description: Indent python file (with pydev) in eclipse


code:
from BeautifulSoup import BeautifulSoup
from BeautifulSoup import Tag

soup = BeautifulSoup(" 20 min Start time:  10 min Other time:  0 min")
span = soup.find('span')
for e in span.contents:
 if type(e) is Tag:
   print "found a tag:", e.name
 else:
   print "found text:", e

rawDescription: Using BeautifulSoup to extract parts of string


code:
graph = {
        '1': ['2', '3', '4'],
        '2': ['5', '6'],
        '5': ['9', '10'],
        '4': ['7', '8'],
        '7': ['11', '12']
        }

def backtrace(parent, start, end):
    path = [end]
    while path[-1] != start:
        path.append(parent[path[-1]])
    path.reverse()
    return path

def bfs(graph, start, end):
    parent = {}
    queue = []
    queue.append(start)
    while queue:
        node = queue.pop(0)
        if node == end:
            return backtrace(parent, start, end)
        for adjacent in graph.get(node, []):
            parent[adjacent] = node # <<<<< record its parent
            queue.append(adjacent)

print bfs(graph, '1', '11')

rawDescription: How to trace the path in a Breadth-First Search?

description: Trace the path in a Breadth-First Search


code:
r"""
This is an extension of the re module. It stores the last successful
match object and lets you access it's methods and attributes via
this module.

This module exports the following additional functions:
    expand  Return the string obtained by doing backslash substitution on a
            template string.
    group   Returns one or more subgroups of the match.
    groups  Return a tuple containing all the subgroups of the match.
    start   Return the indices of the start of the substring matched by
            group.
    end     Return the indices of the end of the substring matched by group.
    span    Returns a 2-tuple of (start(), end()) of the substring matched
            by group.

This module defines the following additional public attributes:
    pos         The value of pos which was passed to the search() or match()
                method.
    endpos      The value of endpos which was passed to the search() or
                match() method.
    lastindex   The integer index of the last matched capturing group.
    lastgroup   The name of the last matched capturing group.
    re          The regular expression object which as passed to search() or
                match().
    string      The string passed to match() or search().
"""

import re as re_

from re import *
from functools import wraps

__all__ = re_.__all__ + [ "expand", "group", "groups", "start", "end", "span",
        "last_match", "pos", "endpos", "lastindex", "lastgroup", "re", "string" ]

last_match = pos = endpos = lastindex = lastgroup = re = string = None

def _set_match(match=None):
    global last_match, pos, endpos, lastindex, lastgroup, re, string
    if match is not None:
        last_match = match
        pos = match.pos
        endpos = match.endpos
        lastindex = match.lastindex
        lastgroup = match.lastgroup
        re = match.re
        string = match.string
    return match

@wraps(re_.match)
def match(pattern, string, flags=0):
    return _set_match(re_.match(pattern, string, flags))

@wraps(re_.search)
def search(pattern, string, flags=0):
    return _set_match(re_.search(pattern, string, flags))

@wraps(re_.findall)
def findall(pattern, string, flags=0):
    matches = re_.findall(pattern, string, flags)
    if matches:
        _set_match(matches[-1])
    return matches

@wraps(re_.finditer)
def finditer(pattern, string, flags=0):
    for match in re_.finditer(pattern, string, flags):
        yield _set_match(match)

def expand(template):
    if last_match is None:
        raise TypeError, "No successful match yet."
    return last_match.expand(template)

def group(*indices):
    if last_match is None:
        raise TypeError, "No successful match yet."
    return last_match.group(*indices)

def groups(default=None):
    if last_match is None:
        raise TypeError, "No successful match yet."
    return last_match.groups(default)

def groupdict(default=None):
    if last_match is None:
        raise TypeError, "No successful match yet."
    return last_match.groupdict(default)

def start(group=0):
    if last_match is None:
        raise TypeError, "No successful match yet."
    return last_match.start(group)

def end(group=0):
    if last_match is None:
        raise TypeError, "No successful match yet."
    return last_match.end(group)

def span(group=0):
    if last_match is None:
        raise TypeError, "No successful match yet."
    return last_match.span(group)

del wraps  # Not needed past module compilation

rawDescription: How do you translate this regular-expression idiom from Perl into Python?

description: Translate this regular-expression idiom from Perl into Python


code:
from ast import literal_eval
from datetime import datetime
import requests

date = datetime(year=2014, month=1, day=1)
url = 'http://www.whoscored.com/tournamentsfeed/8273/Fixtures/'

params = {'d': date.strftime('%Y%m'), 'isAggregate': 'false'}
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36'}

response = requests.get(url, params=params, headers=headers)

fixtures = literal_eval(response.content)
print fixtures

rawDescription: Cannot locate displayed data in source code when Scraping with Scrapy


code:
import re

ss = """commit a8c11fcee68881dfb86095aa36290fb304047cf1
log size 110
Author: XXXXXX XXXXXXXX <XXXXXXXXXXXXXXX@XXXXX.XXX>
Date:   Tue, 10 Apr 2012 11:19:44 +0300

    First commit
3       0       README.MD
blablah bla
commit 12458777AFDRE1254
log size 170
   Author: Jim Bluefish <jimblfsh@gmail.com>
Date   :   Yesterday 21:45:01 +0800
  A key with whitespace :       A_stupid_value

    Funny commit
  From far from you
457      popo       not_README.MD"""

n = 0
print ('------ DISPLAY OF THE TEXT ------\n'
       ' col 1: index of line,\n'
       ' col 2: number of chars in the line\n'
       ' col 3: total of the numbers of chars of lines\n'
       ' col 4: repr(line)\n')
for j,line in enumerate(ss.splitlines(1)):
    n += len(line)
    print '%2d  %2d  %3d  %r' % (j,len(line),n,line)

print '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
print '\n\n\n------ ANALYSER 2 OF THE TEXT ------'

regx1 = re.compile('^commit +(.+) *\r?\n'
                   'log size +(\d+) *\r?\n'
                   '((?:^ *.+?(?<! ) *: *.+(?<! ) *\r?\n)+)'
                   '((?:.*\r?\n(?!commit))+)',
                   re.MULTILINE)

regx2 = re.compile('^ *(.+?)(?<! ) *: *(.+)(?<! ) *\r?\n',
                   re.MULTILINE)

for mat in regx1.finditer(ss):

    commit_value,logsize,dicolines,msg = mat.groups()

    print ('\ncommit_value == %s\n'
           'logsize == %s'
           % (commit_value,logsize))

    print 'dictionary :\n',dict(regx2.findall(dicolines))

    actual_log_message = msg[0:int(logsize)-len(dicolines)].strip(' \r\n')
    print 'actual_log_message ==',repr(actual_log_message)

rawDescription: Using string length defined in the matched string


code:
from collections import defaultdict
def tree():
    return defaultdict(tree)

nested = defaultdict(tree)
words = ['Apple','Ape','Bark','Barn']
for word in words:
    node = nested
    for char in word:
        node = node[char.upper()]

nested
def print_nested(d, indent=0):
    for k, v in d.iteritems():
        print '{}{!r}:'.format(indent * '  ', k)
        print_nested(v, indent + 1)

print_nested(nested)

rawDescription: Create a nested dictionary for a word python


rawDescription: is it possible to convert this loop into a list comprehension in python

description: Is it possible to convert this loop into a list comprehension in python


code:
from HTMLParser import HTMLParser

class MyHTMLParser(HTMLParser):

    def handle_starttag(self, tag, attrs):
        print "Encountered the beginning of a %s tag" % tag

    def handle_endtag(self, tag):
        print "Encountered the end of a %s tag" % tag

    def handle_data(self, data):
        print "Encountered data %s" % data

p = MyHTMLParser()
p.feed('
test
')

rawDescription: Python HTMLParser

description: Python HTMLParser


code:
key[0] = 2
print d[key]        # id(key) hasn't changed, so surely the lookup should still work
print d[[1, 2, 3]]  # but also, we stored a piece of data at [1, 2, 3], didn't we?
print d[[2, 2, 3]]  # but if d[key] works, surely we can expand key to its value

rawDescription: Why implement two so similar data structures like List and Tuple


code:
def rolling(df, window_size=2):
    dfs = [df]
    for i in range(1, window_size):
        dfs.append(df.shift(-i))
    res = pd.concat(dfs, axis=1).iloc[:-(window_size-1)]
    colnames = [c + str(i) for i in range(1, window_size+1) for c in df.columns]
    reorder = [c + str(i) for c in df.columns for i in range(1, window_size+1)]
    res.columns = colnames
    return res[reorder]

print rolling(df, 3)

rawDescription: pandas long to wide windowing

description: Pandas long to wide windowing


code:
import urllib
import urllib2
from bs4 import BeautifulSoup

textToSearch = 'hello world'
query = urllib.quote(textToSearch)
url = "https://www.youtube.com/results?search_query=" + query
response = urllib2.urlopen(url)
html = response.read()
soup = BeautifulSoup(html)
for vid in soup.findAll(attrs={'class':'yt-uix-tile-link'}):
    print 'https://www.youtube.com' + vid['href']

rawDescription: Returning the URL's from a Youtube search


code:
import time
import Queue
import threading

class worker(threading.Thread):

    def __init__(self,qu):
        threading.Thread.__init__(self)
        self.que=qu

    def run(self):
        for item in iter(self.que.get, None): # This will call self.que.get() until None is returned, at which point the loop will break.
            print "Going to sleep.."
            time.sleep(item)
            print "Slept .."
            self.que.task_done()
        self.que.task_done()

q = Queue.Queue()

for j in range(3):
    work = worker(q);
    work.setDaemon(True)
    work.start()

for i in range(5):
    q.put(1)

for i in range(3):  # Shut down all the workers
    q.put(None)

q.join()

print "done!!"

rawDescription: How to handle multiple jobs in a queue with fixed number of threads in Python

description: Handle multiple jobs in a queue with fixed number of threads in Python


code:
class InnerClassDescriptor(object):
  def __init__(self, cls):
    self.cls = cls
  def __get__(self, instance, outerclass):
    class Wrapper(self.cls):
      outer = instance
    Wrapper.__name__ = self.cls.__name__
    return Wrapper

class Outer(object):
  @InnerClassDescriptor
  class Inner(object):
    def __init__(self):
      print self.outer

o = Outer()
i = o.Inner()
print 'Outer is a', type(Outer)
print 'Inner is a', type(o.Inner)

rawDescription: Inner Classes: How can I get the outer-class object at construction time?


code:
#!/usr/bin/python2.7

import os
import sys

print "Hello!"
sys.stdout.flush()
os.close(sys.stdout.fileno())
os.close(sys.stderr.fileno())

data = sys.stdin.read()
with open("data_received_by_child.txt", "w") as fp:
    print >>fp, data

rawDescription: C++ start a script ( bash, python ... ) and using stdin/stdout for datatransfare [linux]


code:
from collections import OrderedDict

mylist = [(5, 3), (1, 4), (1, 7), (12, 7), (8, 3), (12, 4), (12, 5)]
mylist.sort()
print mylist

mydict = OrderedDict()
mydict.update(mylist)
print mydict

rawDescription: Python turning a tuple into a dict

description: Python turning a tuple into a dict


code:
#you can add to this
urls = ["http://www.google.com", "http://www.reddit.com"]

def parse_websites(list_of_urls):
    for url in list_of_urls:
        html = urllib.urlopen(url).read()
        soup = BeautifulSoup(html)
        # kill all script and style elements

        for script in soup(["script", "style"]):
            script.extract()    # rip it out

        # get text
        text = soup.get_text()

        # break into lines and remove leading and trailing space on each
        lines = (line.strip() for line in text.splitlines())
        # break multi-headlines into a line each
        chunks = (phrase.strip() for line in lines for phrase in line.split("  "))
        # drop blank lines
        text = '\n'.join(chunk for chunk in chunks if chunk)

        #print(text)

        wiki = TextBlob(text)
        r = wiki.sentiment.polarity

        print r

parse_websites(urls)

rawDescription: How to iterate over many websites and parse text using web crawler

description: Iterate over many websites and parse text using web crawler


rawDescription: Best way to identify some string in Python

description: Identify some string in Python


code:
for person in json['items']:  #  'people'?
    print person['name']

rawDescription: Formatting a Python Dictionary for JSON

description: Formatting a Python Dictionary for JSON


code:
for company in companies:
    print company.getText().strip()

rawDescription: How can I best isolate 2 different unlabeled pieces of html using beautiful soup to be printed to a CSV?

description: Best isolate 2 different unlabeled pieces of html using beautiful soup to be printed to a CSV


code:
import sys, cgi, re
from bs4 import BeautifulSoup, element
magical_class = "Nd92KSx3u2"
restricted_tags="title script object embed".split()
re_my_span = re.compile(r'<span class="%s">(.+?)</span>' % magical_class)

def no_nl(s): return str(s).replace("\r", "").replace("\n", " ")

if len(sys.argv) != 3:
    print "Usage: %s <input_html_file> <output_html_file>" % sys.argv[0]
    sys.exit(1)

def process(elem):
    for x in elem.children:
        if isinstance(x, element.Comment): continue
        if isinstance(x, element.Tag):
            if x.name in restricted_tags:
                continue
        if isinstance(x, element.NavigableString):
            if not len(no_nl(x.string).strip()):
                continue  # it's just empty space
            print '[', no_nl(x.string).strip(), ']',  # debug output of found strings
            s = ""
            for c in x.string:
                if c in (' ', '\r', '\n', '\t'): s += c
                else: s += '%s' % (magical_class, c)
            x.replace_with(s)
            continue
        process(x)

soup = BeautifulSoup(open(sys.argv[1]))
process(soup)
output = re_my_span.sub(r'\1' % magical_class, str(soup))
with open(sys.argv[2], 'w') as f:
    f.write(output)

rawDescription: generate list of text content with pyqt?

description: Generate list of text content with pyqt


code:
xml = ET.fromstring(xml_string)
root = ET.Element('Books')
for booktype in xml.findall('Booktype'):
    current = ET.SubElement(root, 'Booktype')
    for t in map(lambda x: x.strip(), booktype.get('List').split('>')):
        current = ET.SubElement(current, t)
    current.text = booktype.text
print ET.tostring(root, 'utf-8')

rawDescription: variable in XML subelement

description: Variable in XML subelement


code:
import urllib2
import re

IDlist = ['C9JVZ1', 'C9JLN0', 'C9J872']
function_dict = {}

# Cycle through the data files, keyed by ID
for id in IDlist:

    # Start a new list of functions for this file.
    # Open the file and read line by line.
    function_list = []
    textfile = urllib2.urlopen("http://www.uniprot.org/uniprot/"+id+".txt")
    myfile = textfile.readlines()

    for line in myfile:

        # When you find a function tag, extract the function and add it to the list.
        found = re.search(' [PCF]:(.+?);', line)
        if found:
            function = found.group(1)
            function_list.append(function)

    # At end of file, insert the list into the dictionary.
    function_dict[id] = function_list

print function_dict

rawDescription: Python: extract string on webtext from a list of URL


code:
nbIteration = 0
def getChildren(uiItem, nbIteration):
    for childItem in cmds.layout(uiItem, query=True, childArray=True):
        try:
            print "|___"*nbIteration + childItem
            getChildren(uiItem + "|" + childItem, nbIteration+1)
        except:
            pass
getChildren("GE_ui_window|GE_ui_frameLayout|GE_ui_scriptedPanel", nbIteration)

rawDescription: How do I parent new, user-created buttons inside the Graph Editor window?

description: Parent new, user-created buttons inside the Graph Editor window


code:
if double(4 == 1):
   print "bla"

rawDescription: High Ordering Function - printing out thrice


code:
import re

dicreg = re.compile('(?<=\{)[^}]*}')

kvregx = re.compile('[ \r\n]*'
                    '(" *)?((location)|[^:]+?)(?(1) *")'
                    '[ \r\n]*'
                    ':'
                    '[ \r\n]*'
                    '(?(3)|(" *)?)([^:]*?)(?(4) *")'
                    '[ \r\n]*(?:,(?=[^,]+?:)|\})')

checking_dict = {}
checking_list = []

filename = 'zzz.txt'

with open(filename) as f:
    content = f.read()

######## First part: to gather all the keys in all the dictionaries

ecr = []

for mat_dic in dicreg.finditer(content):
    ecr.append('\nmmmmmmm dictionary found in ss mmmmmmmmmmmmmm')
    for mat_kv in kvregx.finditer(mat_dic.group()):
        k,v = mat_kv.group(2,5)
        ecr.append('%s  :  %s' % (k,v))
        if k in checking_list:
            checking_dict[k] += 1
        else:
            checking_list.append(k)
            checking_dict[k] = 1

print '\n'.join(ecr)
print '\n\n\nchecking_dict == %s\n\nchecking_list        == %s' %(checking_dict,checking_list)

######## The keys are sorted in order that the less frequent ones are at the end
checking_list.sort(key=lambda k: checking_dict[k], reverse=True)
posis = dict((k,i) for i,k in enumerate(checking_list))
print '\nchecking_list sorted == %s\n\nposis == %s' % (checking_list,posis)

######## Now, the file is read again to build a list of rows

base = [ '' for i in xrange(len(checking_list))]
rows = []

for mat_dic in dicreg.finditer(content):
    li = base[:]
    for mat_kv in kvregx.finditer(mat_dic.group()):
        k,v = mat_kv.group(2,5)
        li[posis[k]] = v
    rows.append(li)

print '\n\n%s\n%s' % (checking_list,30*'___')
print '\n'.join(str(li) for li in rows)

rawDescription: Reading a file with unproperly marked dictionaries


code:
from sys import argv

suPass = argv[1]    # we use the second index of argv, as the first index
                    # is actually the name of the script, test.py

print suPass

rawDescription: Python - Sharing getpass() Input with another script?


code:
date_ranges = [
    {'start': '2015-02-12 08:30', 'end': '2015-02-12 13:30', 'name': 'a'},
    # Omitting...
    {'start': '2015-02-12 12:30', 'end': '2015-02-12 18:30', 'name': 'f'},
]

edges = set()
for date_range in date_ranges:
    edges.add(date_range['start'])
    edges.add(date_range['end'])
edges = sorted(list(edges))

intervals = []
for i, edge in enumerate(edges):
    if i == 0:
        continue
    previous_edge = edges[i - 1]
    current_edge = edge
    overlaps = [date_range['name']
                for date_range in date_ranges
                if date_range['start'] <= previous_edge and
                   current_edge <= date_range['end']]
    if overlaps:
        interval = {
            'name': ' + '.join(sorted(overlaps)),
            'start': previous_edge,
            'end': current_edge
        }
        intervals.append(interval)

for interval in intervals:
    print interval

rawDescription: Python compare list of date ranges

description: Python compare list of date ranges


code:
def single_element_permutation(given, new):
    for i in xrange(len(given)):
        for ele in new:
            yield given[:i] + (ele,) + given[i+1:]

for e in single_element_permutation((0, 0, 0, 0, 0), range(5)):
    print e

rawDescription: Generate all one step variations of a permutation


code:
import fileinput

for line in fileinput.input ("b.txt",inplace=1):
    print "%d: %s" % (fileinput.lineno(),line),

rawDescription: Open file, read it, process, and write back - shortest method in Python


code:
import sys
from PyQt4 import QtGui

class QCustomTableWidget (QtGui.QTableWidget):
    def __init__ (self, parentQWidget = None):
        super(QCustomTableWidget, self).__init__(parentQWidget)
        self.setColumnCount(2)
        self.setRowCount(3)
        self.setItem(0, 0, QtGui.QTableWidgetItem('Test 1'))
        self.setItem(0, 1, QtGui.QTableWidgetItem('Test 2'))
        self.setItem(1, 0, QtGui.QTableWidgetItem('Work 1'))
        self.setItem(1, 1, QtGui.QTableWidgetItem('Work 2'))
        self.setItem(2, 0, QtGui.QTableWidgetItem('Area 1'))
        self.setItem(2, 1, QtGui.QTableWidgetItem('Area 2'))
        self.itemSelectionChanged.connect(self.itemSelectionChangedCallback)

    def itemSelectionChangedCallback (self):
        print '#' * 80
        for currentQTableWidgetItem in self.selectedItems():
            print currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text()

if __name__ == '__main__':
    myQApplication = QtGui.QApplication(sys.argv)
    myQCustomTableWidget = QCustomTableWidget()
    myQCustomTableWidget.show()
    sys.exit(myQApplication.exec_())

rawDescription: Get cell highlight for specfic row & column in QTableWidget using Python pyqt4


code: user_details = map(lambda (i,x): {'name':x, 'rank':i}, enumerate(ranked_users))

rawDescription: Getting index of item while processing a list using map in python


code:
def myfunction(myarray = np.zeros(0)):
    print "myfunction : before = ", myarray    # This line should not be modified
    data = np.loadtxt("test.txt", unpack=True) # This line should not be modified
    concatenated = np.concatenate((myarray, data))
    print "myfunction : after = ", myarray     # This line should not be modified
    return  concatenated

rawDescription: Concatenate numpy arrays inside a function and return it?


code:
import gzip
with gzip.GzipFile("zipped_ex.gz.2016") as f:
    text = f.read()
    print text

rawDescription: Bytes from zip file to text in python

description: Bytes from zip file to text in python


code:
l = ([u'VESSELTYPE', u'VESSELNAME', u'PERIODSTART', u'PERIODEND', u'ESTREDELI', u'HIRE', u'DAYS Mar-14', u'DAYS Q2 2014', u'DAYS Q3 2014', u'DAYS Q4 2014', u'DAYS Q1 2015', u'DAYS Q2 2015', u'DAYS Q3 2015', u'DAYS Q4 2015', u'NUMDAYS'], 'AnotherItem')

days = [s for s in l[0] if s.startswith('DAYS')]
print days

rawDescription: Python select columns with similar names


code:
tabledata = cursor.fetchall()
for row in enumerate(sorted(tabledata, key=lambda x: x[4:7])):
    print str(row)

rawDescription: group by function in sqlite3 or itertools.groupy in python, sorting and grouping middle values within a string

description: Group by function in sqlite3 or itertools.groupy in python, sorting and grouping middle values within a string


code:
r = requests.get("http://orlando.craigslist.org/search/apa?sort=priceasc")
from bs4 import BeautifulSoup

html = r.content

soup = BeautifulSoup(html)
prices = [int(pr.text.strip("$")) for pr in soup.select("span.price")]
print "Highest price: ${}".format(prices[-1])
print "Lowest price: ${}".format(prices[0])
print "Average price: ${}".format(sum(prices, 0.0)/len(prices))

rawDescription: web scrapping craigslist appartment prices in python not showing highest cost appartment

description: Web scrapping craigslist appartment prices in python not showing highest cost appartment


code:
import random
import time
import sys

size_url      = 10000
size_resolved = 10000

random.seed(time.time())

url = [ random.randint(1,sys.maxint) for x in xrange(size_url)]
resolved = [ random.randint(1,sys.maxint) for x in xrange(size_resolved)]

a = time.time()
intersection = [ x for x in url if x in resolved ]
print "Search in list:",time.time() - a

resolved = set(resolved)

a = time.time()
intersection = [ x for x in url if x in resolved ]
print "Search in set:",time.time() - a

rawDescription: Make algorithm faster that runs over multiple lists


code: return regex.sub(ur"[^\P{P}-]+", " ", text)

rawDescription: Remove selected punctuation from unicode strings


code:
import inspect

def get_type(cls, attr):
    try:
        return [a.kind for a in inspect.classify_class_attrs(cls) if a.name == attr][0]
    except IndexError:
        return None

class Test(object):
    @classmethod
    def foo(cls):
        print 'foo'

    def bar(self):
        print 'bar'

    @staticmethod
    def baz():
        print 'baz'

rawDescription: getattr on class objects

description: Getattr on class objects


code:
c = 1
while True:
    if c==3:
        print "Now break because value of c (%d) is equal to 3"%(c)
        break
    print "In loop c=%d"%c
    c += 1

rawDescription: How to exit for loop once valid file name is provided and give 3 chances for correct input

description: Exit for loop once valid file name is provided and give 3 chances for correct input


code:
for x,y in zip(w,c):
    l=hamming_distance(x, y)
    print l

rawDescription: Iterative summation of output generated by a function in a for loop (python)


code:
import glob
import os
import sys

path = "/tmp"
file_mask = "file*.txt"
results = {}

for file in glob.glob(os.path.join(path, file_mask)):
    column_names = {}
    exchange_col = None
    with open(file, "r") as f:
        for line_num, line in enumerate(f.xreadlines()):
            # process header
            if not line_num:
                line_parsed = line.strip().split("|")
                for column_num, column in enumerate(line_parsed):
                    if column.strip() == "exchnage":
                        exchange_col = column_num
                    else:
                        column_names[column_num] = column.strip()
                if exchange_col is None:
                    print "Can't find exchnage field"
                    sys.exit(1)
                continue
            line_parsed = line.strip().split("|")
            if len(line_parsed) != len(column_names) + 1:
                continue
            # prepare empty structure for excahnge, if not added yet
            if not line_parsed[exchange_col].strip() in results:
                results[line_parsed[exchange_col].strip()] = {column_name:set() for column_name in column_names.values()}
            # add uniq items to exchange
            for column_num, column in enumerate(line_parsed):
                column_val = column.strip()
                # add only non empty values
                if column_val and column_num != exchange_col:
                    results[line_parsed[exchange_col].strip()][column_names[column_num]].add(column_val)

column_names = column_names.values()
print "exchnage|" + "|".join("%8s" %c for c in column_names)
for exchange, values in results.iteritems():
    print "%8s|" % exchange + "|".join("%8s" % str(len(values[column])) for column in column_names)

rawDescription: Count unique values in each column of pipe delimited text file using Perl


code:
s = ">>> testFunction(2, 3, 4)\r\nTrue >>> testFunction(2, 1, 4)\r\nFalse >>> legalTriangles(-1, -1, -1)\r\nFalse"
for func, result in [i.splitlines() for i in s.split(">>>") if i]:
    print "
%s	%s
" % (func.strip(), result.strip())

rawDescription: Web2py Python split rows based on true/false from database result


code:
class Foo():
    pass

class Bar(Foo):
    pass

class Bar2():
    pass

print issubclass(Bar, Foo)  # True
print issubclass(Bar2, Foo) # False

rawDescription: What is the issubclass equivalent of isinstance in python?


rawDescription: Python list with filter function (excel-way)


code:
fname = 'dogD.txt'
fh = open(fname)
res = []
for line in fh:
    t2 = line.strip().split()
    res.extend(t2)
print res

rawDescription: make one list from multi items in a variable in python

description: Make one list from multi items in a variable in python


code:
import glob

for fpath in glob.glob(mypath):
    print fpath

rawDescription: Open file knowing only a part of its name


code:
def search(input,inputList):
    for i in range( len( inputList ) ):
        if inputList[i]>input:
            return i
    return len( inputList )

print search(-5, (1, 5, 10))
#Result: 0
print search(3, (1, 5, 10))
#Result: 1

rawDescription: Python-search function

description: Python-search function


code:
import pyqtgraph as pg
import numpy as np
app = pg.mkQApp()

y = np.random.normal(size=(120,20000), scale=0.2) + np.arange(120)[:,np.newaxis]
x = np.empty((120,20000))
x[:] = np.arange(20000)[np.newaxis,:]
view = pg.GraphicsLayoutWidget()
view.show()
w1 = view.addPlot()

class MultiLine(pg.QtGui.QGraphicsPathItem):
    def __init__(self, x, y):
        """x and y are 2D arrays of shape (Nplots, Nsamples)"""
        connect = np.ones(x.shape, dtype=bool)
        connect[:,-1] = 0 # don't draw the segment between each trace
        self.path = pg.arrayToQPath(x.flatten(), y.flatten(), connect.flatten())
        pg.QtGui.QGraphicsPathItem.__init__(self, self.path)
        self.setPen(pg.mkPen('w'))
    def shape(self): # override because QGraphicsPathItem.shape is too expensive.
        return pg.QtGui.QGraphicsItem.shape(self)
    def boundingRect(self):
        return self.path.boundingRect()

now = pg.ptime.time()
lines = MultiLine(x, y)
w1.addItem(lines)
print "Plot time: %0.2f sec" % (pg.ptime.time()-now)

app.exec_()

rawDescription: Plotting large arrays in pyqtgraph

description: Plotting large arrays in pyqtgraph


code:
for x, y in zip(list, list2):
  print "list1 - " + str(x)
  print "list2 - " + str(y)

rawDescription: Print contents of two lists so corresponding records from each print sequentially


code:
from IPython.core.debugger import Tracer
zz = Tracer()

print "iPython"
zz()
print "is a"
print "command shell"
zz()
print "for"
print "interactive computing"
print "in"
print "multiple programming languages"
print "including"
print "Python"

rawDescription: Debug Python Script Using Multiple Breakpoints in IPython


code: print "<%*s>" % (len(text)-2,text)

rawDescription: variable length of %s with the % operator in python

description: Variable length of %s with the % operator in python


code:
import requests

r = requests.get("http://httpbin.org/headers")

print r # status 200
print r.text # your headers

rawDescription: Python Get Data From HTML Page

description: Python Get Data From HTML Page


code:
import re
import requests
from bs4 import BeautifulSoup

def get_link_by_keyword(keyword):
    links = set()
    for i in soup.find_all(href=re.compile(r"[http|/].*"+str(keyword))):
        links.add(i.get('href'))

    for i in links:
        if i[0] == 'h':
            yield i
        elif i[0] == '/':
            yield link+i
        else:
            pass

global link
link = raw_input('Please enter a link: ')
if link[-1] == '/':
    link = link[:-1]

r = requests.get(link, verify=True)
data = r.text
soup = BeautifulSoup(data, "html.parser")

for i in get_link_by_keyword(raw_input('Enter a keyword: ')):
    print i

rawDescription: Python web Automation to get Email from Webpage


code:
def mixer(*args):
    """Decorator for mixing mixins"""
    def inner(cls):
        for a,k in ((a,k) for a in args for k,v in vars(a).items() if callable(v)):
            setattr(cls, k, getattr(a, k).im_func)
        return cls
    return inner

class Mixin(object):
    def b(self): print "b()"
    def c(self): print "c()"

class Mixin2(object):
    def d(self): print "d()"
    def e(self): print "e()"

@mixer(Mixin, Mixin2)
class Foo(object):
    # Somehow mix in the behavior of the Mixin class,
    # so that all of the methods below will run and
    # the issubclass() test will be False.

    def a(self): print "a()"

f = Foo()
f.a()
f.b()
f.c()
f.d()
f.e()
print issubclass(Foo, Mixin)

rawDescription: In Python can one implement mixin behavior without using inheritance?


code:
class Car(object):
    def __init__(self):

        #Store vehicle passengers in a dictionary by row
        # - by default we'll have 1 in the front seat and 1 in the back
        self._contents = {'row1': 1,
                          'row2': 1}

    @property
    def occupants(self):
        """
        Number of occupants in the vehicle
        """

        #Get the number of people in row 1
        row1 = self._contents['row1']
        #Get the number of people in row 2
        row2 = self._contents['row2']
        return row1 + row2

    @occupants.setter
    def occupants(self, val):

        #Start with an empty car
        self._contents['row1'] = 0
        self._contents['row2'] = 0

        #Check to see whether there are more than 2 people entering the car
        if val > 2:
            #Put 2 in the front seats
            self._contents['row1'] = 2

            #Put the rest in the back seat - no matter how many there are!
            self._contents['row2'] = val - 2
        else:
            #Since there are 2 or fewer people, let them sit in the front
            self._contents['row1'] = val

class Van(Car):
    def __init__(self):
        super(Van, self).__init__()

        #Van's have an additional 3rd row
        self._contents['row3'] = 1

    @property
    def occupants(self):

        #Number of people in first 2 rows
        first_2_rows = super(Van, self).occupants
        #Number of people in 3rd row
        row3 = self._contents['row3']

        #Total number of people
        return first_2_rows + row3

    @occupants.setter
    def occupants(self, val):

        #Start with an empty van (first 2 rows handled by Car class)
        self._contents['row3'] = 0

        #Check if there are more than 4 people entering the van
        if val > 4:
            #Put all but 4 folks in the back row
            self._contents['row3'] = val - 4

            #Load the first 2 rows in the same manner as for a car
            Car.occupants.fset(self, 4)
        else:
            #Load the first 2 rows in the same manner as for a car
            Car.occupants.fset(self, val)

if __name__ == '__main__':

    van1 = Van()
    van2 = Van()

    print "Van has {0} people".format(van1.occupants)

    print "Seating 6 people in van1"
    van1.occupants = 6

    print "Seating 2 people in van2"
    van2.occupants = 2

    print "van1 has {0} people".format(van1.occupants)
    print "van2 has {0} people".format(van2.occupants)

rawDescription: Unable to call overridden setter property of superclass


code:
readings = yahoo.get_historical('2014-04-25', '2014-04-29')
for reading in readings:
    print reading['Volume']

rawDescription: How to get data from this "list" looking thing in python 2.7

description: Get data from this "list" looking thing in python 2.7


code:
import re

datasource = {
    "foo":(
        "paper-1:::author1::author2::authorN:::title1\n"
        "paper-2:::author21::author22::author23::author2N:::title2\n"
        "paper-3:::author31::author32:::title3"
        )
    }

def mapfn(k, v):
    for line in v.splitlines():
        data = re.split(r":{2,3}", line)
        words = data[-1].split()
        for author in data[1:-1]:
            for word in words:
                yield author + word, 1

def main():
    for k, v in datasource.items():
        for result in mapfn(k, v):
            print result

if __name__ == "__main__":
    main()

rawDescription: mapreduce simple function - using python

description: Mapreduce simple function - using python


code:
def okalready(self,event):
    for i,cb in enumerate(self.checkboxes):
       print "CB:%d = %s"%(i,cb.GetValue())
    print "Checked:",[cb.GetLabel() for cb in self.checkboxes if cb.GetValue()]
    self.Destroy()

rawDescription: How to record multiple checkboxes in wxPython?

description: Record multiple checkboxes in wxPython


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

sxml="""
<encspot>
  <file>
   <Name>some filename.mp3</Name>
   <Encoder>Gogo (after 3.0)</Encoder>
   <Bitrate>131</Bitrate>
  </file>
  <file>
   <Name>another filename.mp3</Name>
   <Encoder>iTunes</Encoder>
   <Bitrate>128</Bitrate>
  </file>
</encspot>
"""
tree=et.fromstring(sxml)

for el in tree.findall('file'):
    print '-------------------'
    for ch in el.getchildren():
        print '{:>15}: {:<30}'.format(ch.tag, ch.text)

print "\nan alternate way:"
el=tree.find('file[2]/Name')  # xpath
print '{:>15}: {:<30}'.format(el.tag, el.text)

rawDescription: Parse XML file into Python object

description: Parse XML file into Python object


code: print config_params[0]['hostname']

rawDescription: Get value from dict

description: Get value from dict


code:
import datetime as DT
import numpy as np
import pandas as pd
import pandas.io.data as PID

def setup_df():
    df1 = PID.get_data_yahoo("IBM",
                             start=DT.datetime(1970, 1, 1),
                             end=DT.datetime.today())
    df1.rename(columns={'Adj Close': 'ibm'}, inplace=True)

    df2 = PID.get_data_yahoo("F",
                             start=DT.datetime(1970, 1, 1),
                             end=DT.datetime.today())
    df2.rename(columns={'Adj Close': 'ford'}, inplace=True)

    df = df1.join(df2.ford, how='inner')
    df = df[['ibm', 'ford']]
    df['sh ibm'] = 0
    df['sh ford'] = 0
    df['ibm value'] = 0
    df['ford value'] = 0
    df['ratio'] = 0
    return df

def invest(df, i, amount):
    """
    Invest amount dollars evenly between ibm and ford
    starting at ordinal index i.
    This modifies df.
    """
    c = dict([(col, j) for j, col in enumerate(df.columns)])
    halfvalue = amount/2
    df.iloc[i:, c['sh ibm']] = halfvalue / df.iloc[i, c['ibm']]
    df.iloc[i:, c['sh ford']] = halfvalue / df.iloc[i, c['ford']]

    df.iloc[i:, c['ibm value']] = (
        df.iloc[i:, c['ibm']] * df.iloc[i:, c['sh ibm']])
    df.iloc[i:, c['ford value']] = (
        df.iloc[i:, c['ford']] * df.iloc[i:, c['sh ford']])
    df.iloc[i:, c['ratio']] = (
        df.iloc[i:, c['ibm value']] / df.iloc[i:, c['ford value']])

def rebalance(df, tol, i=0):
    """
    Rebalance df whenever the ratio falls outside the tolerance range.
    This modifies df.
    """
    c = dict([(col, j) for j, col in enumerate(df.columns)])
    while True:
        mask = (df['ratio'] >= 1+tol) | (df['ratio'] <= 1-tol)
        # ignore prior locations where the ratio falls outside tol range
        mask[:i] = False
        try:
            # Move i one index past the first index where mask is True
            # Note that this means the ratio at i will remain outside tol range
            i = np.where(mask)[0][0] + 1
        except IndexError:
            break
        amount = (df.iloc[i, c['ibm value']] + df.iloc[i, c['ford value']])
        invest(df, i, amount)
    return df

df = setup_df()
tol = 0.05
invest(df, i=0, amount=100)
rebalance(df, tol)

df['portfolio value'] = df['ibm value'] + df['ford value']
df['ibm weight'] = df['ibm value'] / df['portfolio value']
df['ford weight'] = df['ford value'] / df['portfolio value']

print df['ibm weight'].min()
print df['ibm weight'].max()
print df['ford weight'].min()
print df['ford weight'].max()

# This shows the rows which trigger rebalancing
mask = (df['ratio'] >= 1+tol) | (df['ratio'] <= 1-tol)
print(df.loc[mask])

rawDescription: Portfolio rebalancing with bandwidth method in python


code:
def wind_chill(temp, wind_speed):
    """ Compute wind chill given temperature and wind speed if the temperature
        is 50 degrees Fahrenheit or less and the wind speed is above 3 mph,
        otherwise return 'nan' (not-a-number) because it's undefined otherwise.
    """
    return (35.74 + (0.6215 * temp) - 35.75 * (wind_speed ** 0.16)
            + 0.4275 * temp * (wind_speed ** 0.16)
            if temp <= 50 and wind_speed > 3 else float('nan'))

def main():
    # print table header
    temps = xrange(-20, 70, 10)
    num_temps = len(temps)
    data = [" "] + [temp for temp in temps]
    print ("{:3s}" + num_temps * " {:5d}").format(*data)
    data = [" "] + num_temps * [5 * "-"]
    print ("{:3s}" + num_temps * " {:5s}").format(*data)

    # print table rows
    row_format_string = "{:3d}" + num_temps * " {:5.1F}"
    for wind_speed in xrange(0, 35, 5):
        data = [wind_speed] + [wind_chill(temp, wind_speed) for temp in temps]
        print row_format_string.format(*data)

main()

rawDescription: windchill table using nested loop

description: Windchill table using nested loop


code:
for li in soup.select('ol.trendingnow_trend-list > li > a'):
    print li.get_text()

rawDescription: parsing data using beautifulsoup

description: Parsing data using beautifulsoup


code:
from nltk.corpus import wordnet

fList = open("frequencyList.txt","r")#Read the file
lines = fList.readlines()

eWords = open("eng_words_only.txt", "a")#Open file for writing

for w in lines:
    if not wordnet.synsets(w):#Comparing if word is non-English
        print 'not '+w
    else:#If word is an English word
        print 'yes '+w
        eWords.write(w)#Write to file

eWords.close()#Close the file

rawDescription: how to remove all non english characters and words using NLTK >

description: Remove all non english characters and words using NLTK >


code:
import re
mystr = 'HelloWorldToYou'
pat = re.compile(r'([A-Z][a-z]*)')
# or your version with `.*?`: pat = re.compile(r'([A-Z].*?)(?=[A-Z]+|$)')
print pat.findall(mystr)

rawDescription: Python regular expression, matching the last word


code:
a = [1,2,3]
b = a.insert(1, "inserted")
print a
print b

rawDescription: Print everything right of ":" at the same width


code:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy.dialects.postgresql import INET, CIDR

Base = declarative_base()

# workaround before 0.9.2
from sqlalchemy.sql import operators
is_contained_by = operators.custom_op("<<")
operators._comparison.add(is_contained_by)

class IPA(Base):
    __tablename__ = 'ip_address'

    id = Column(Integer, primary_key=True)
    v4address = Column(INET)

    network = relationship("Network",
                        primaryjoin=lambda: is_contained_by(
                                     IPA.v4address,
                                     (foreign(Network.v4representation))
                                    ),
                        viewonly=True
                    )
class Network(Base):
    __tablename__ = 'network'

    id = Column(Integer, primary_key=True)
    v4representation = Column(CIDR)

print Session().query(IPA).join(IPA.network)

rawDescription: SQLAlchemy, eager load postgresql INET/CIDR relationships


code:
while True:
    a, b = b, a + b
    if b > 4000000:
        break
    print b
    if b < 4000000 and b % 2 == 0:
        result.append(b)

rawDescription: Python: yield vs print in a loop

description: Python: yield vs print in a loop


code:
stream = StringIO()
handler = wx.richtext.RichTextHTMLHandler()
handler.SaveStream( myRichTextCtrl.GetBuffer(), stream )
print stream.getvalue()

rawDescription: WxRichTextCtrl python retrieving entries from file


code:
html = response.read()
soup = BeautifulSoup(html, from_encoding=response.info().getparam('charset'))
for div in soup.findAll('div','zitat'):
    for span in div.findAll('span', 'quote_zeile'):
        print span.string

rawDescription: python how to parse html

description: Python how to parse html


code:
input = tf.placeholder(tf.float32, [128, 32])  # Defines a matrix.
output = tf.expand_dims(tf.transpose(input, [1, 0]), 1)

print output.get_shape()
# ==> TensorShape([Dimension(32), Dimension(1), Dimension(128)])

rawDescription: Theano Dimshuffle equivalent in Google's TensorFlow?


code:
previous_val = 0
file = open("text.txt","r")
for line in file:
    current = float(line.strip())
    print current - previous_val
    previous_val = current

rawDescription: How can I update a variable to keep track of the current value minus the previous value in Python?

description: Update a variable to keep track of the current value minus the previous value in Python


code:
import foo

print foo.bar

rawDescription: Little confused with import python

description: Little confused with import python


rawDescription: Combined list and dict comprehension

description: Combined list and dict comprehension


code:
import csv

with open("banklist.csv", "rU") as infile:
    reader = csv.reader(infile)
    next(reader, None)  # skip the first row
    for row in reader:
        print row

rawDescription: Python - CSV read as one big line

description: Python - CSV read as one big line


code:
print root.findtext('ts')
print "----"

for child in root.iter('r'):
        print child.attrib.get('t'), child.attrib.get('n'), child.findtext('i')

rawDescription: Using XML python library to acces data

description: Using XML python library to acces data


code:
def all_perms(elements):
    if len(elements) <=1:
        yield elements
    else:
        for perm in all_perms(elements[1:]):
            for i in range(len(elements)):
                yield perm[:i] + elements[0:1] + perm[i:]

word = "abc"
print list(all_perms(word))

rawDescription: producing all the anagrams from a string python

description: Producing all the anagrams from a string python


code:
from lxml import etree

data = """

        <d/>

    <c attr1="important"/>

        <d/>

    <c attr1="so important" />

"""
xmltree = etree.fromstring(data)
elems = xmltree.xpath('//d')

for el in elems:
    try:
        print el.xpath("preceding::c[@attr1]")[-1].get("attr1")
    except IndexError:
        print "No preceding 'c' element."

rawDescription: lxml etree find closest element before

description: Lxml etree find closest element before


code:
mx = None
mn = None
for acid in formic_acid:
    weight = sum(weights[a] for a in acid)
    if mx is None or weight > mx:
        mx = weight
    if mn is None or weight < mn:
        mn = weight
    weight_list.append((acid,weight))
    print "The molecular weight of the protein {} is {}".format(acid,weight)
print("The minimum and maximum weights are:  {}, {}".format(mn,mx))

rawDescription: How I can add the molecular Weight of my new list of strings using a mw dictionary?


code:
with open('urllist.txt', 'r') as urls, open('somefile.txt', 'a') as output:
     for url in urls:
         page = requests.get(url.strip())

         tree = html.fromstring(page.content)
         visitors = tree.xpath('//b["no-visitors"]/text()')
         print 'Visitors: ', visitors
         print >> output, 'Visitors:', visitors

rawDescription: Python URLs in file Requests

description: Python URLs in file Requests


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

  def __eq__(self, other):
    print self.name, "==", other.name, "?"
    return self.name == other.name

rawDescription: Would it be better to use "if x in (y, z)" over "if x == y or x == z"?


code:
from bs4 import BeautifulSoup
import requests
import time

url = 'https://google.com'

t1 = time.clock();
name = requests.get(url)
t2 = time.clock();
soup = BeautifulSoup(name.content)
t3 = time.clock();

print t2 - t1, t3 - t2

rawDescription: Python - save requests or BeautifulSoup object locally


code:
import moneyed

country_name = 'France'

for currency, data in moneyed.CURRENCIES.iteritems():
    if country_name.upper() in data.countries:
        print currency
        break

rawDescription: django countries currency code

description: Django countries currency code


code:
import time

from bs4 import BeautifulSoup
import requests
from requests.cookies import cookiejar_from_dict

fund_id = '10306'
last_agreed_time = str(int(time.time() * 1000))
url = 'https://www.fidelity.com.hk/investor/en/fund-prices-performance/fund-price-details/factsheet-historical-nav-dividends.page'

with requests.Session() as session:
    session.headers = {'User-Agent': 'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'}
    session.cookies = cookiejar_from_dict({
        'accepted': 'true',
        'irdFundId': fund_id,
        'isAgreed': 'yes',
        'isExpand': 'true',
        'lastAgreedTime': last_agreed_time
    })

    response = session.get(url, params={'FundId': fund_id})

    soup = BeautifulSoup(response.content)
    print soup.title

rawDescription: How to reach a forwarded webpage using Python Requests?

description: Reach a forwarded webpage using Python Requests


code:
# create a test file
with open('data.dat', 'w') as outf:
    for i in xrange(1, 110):
        outf.write('line #%d\n' % i)

for line in lines_of_interest('data.dat'):
    # process line...
    print line,

rawDescription: python read file and skip n lines and read again and skip

description: Python read file and skip n lines and read again and skip