code:
import random, string
from itertools import izip_longest
import timeit

def _randstr(n):
    ls = []
    for i in range(n):
        ls.append(random.choice(string.lowercase))
    return ''.join(ls)

def _sortstr(n):
    return ''.join(sorted(_randstr(n)))

def badstr(nish):
    res = ""
    for i in range(nish):
        res += _sortstr(i)
        if len(res) >= nish:
            break
    return res

def achampion(s):
    start = end = longest = 0
    best = ""
    for c1, c2 in izip_longest(s, s[1:]):
        end += 1
        if c2 and c1 <= c2:
            continue
        if (end-start) > longest:
            longest = end - start
            best = s[start:end]
        start = end
    return best

def bjorn(s):
    cur_start = 0
    cur_stop = 1
    long_start = cur_start
    long_end = cur_stop

    for ch in s[1:]:
        if ch < s[cur_stop-1]:
            if (long_end-long_start) < (cur_stop-cur_start):
                long_start = cur_start
                long_end = cur_stop
            cur_start = cur_stop
        cur_stop += 1

    if (long_end-long_start) < (cur_stop-cur_start):
        return s[cur_start:cur_stop]
    return s[long_start:long_end]

def or1426(s):
    longest = [s[0]]
    current = [s[0]]
    for char in s[1:]:
        if char >= current[-1]: # current[-1] == current[len(current)-1]
            current.append(char)
        else:
            current=[char]
        if len(longest) < len(current):
            longest = current
    return ''.join(longest)

if __name__ == "__main__":
    print 'achampion:', round(min(timeit.Timer(
        "achampion(rstr)",
        setup="gc.enable();from __main__ import achampion, badstr; rstr=badstr(30000)"
    ).repeat(15, 50)), 3)

    print 'bjorn:', round(min(timeit.Timer(
        "bjorn(rstr)",
        setup="gc.enable();from __main__ import bjorn, badstr; rstr=badstr(30000)"
    ).repeat(15, 50)), 3)

    print 'or1426:', round(min(timeit.Timer(
        "or1426(rstr)",
        setup="gc.enable();from __main__ import or1426, badstr; rstr=badstr(30000)"
    ).repeat(15, 50)), 3)

rawDescription: Why won't my for loop work? (Python)

description: Why won't my for loop work? (Python)


code:
import socket
from socket import *
import struct
import sys

if __name__ == "__main__":
    UDP_IP = ""
    UDP_PORT = 6139

    inbound = socket(AF_INET, SOCK_DGRAM)
    inbound.bind((UDP_IP, UDP_PORT))

    while True:
        data, wherefrom = inbound.recvfrom(1024, 0)
        data = struct.unpack("!L3i", data)
        data = str(data)
        sys.stderr.write(repr(wherefrom) + "\n")
        sys.stderr.flush()
        sys.stdout.write(data + "\n")
        sys.stdout.flush()
        inbound.sendto('lala', wherefrom)
        print wherefrom

rawDescription: UDP communication to device that responds to port request comes from, how to get that response


code:
import textwrap

lorem = """Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Phasellus ac commodo libero, at dictum leo. Nunc convallis est id purus porta,
malesuada erat volutpat. Cras commodo odio nulla. Nam vehicula risus id lacus
vestibulum. Maecenas aliquet iaculis dignissim. Phasellus aliquam facilisis
pellentesque ultricies. Vestibulum dapibus quam leo, sed massa ornare eget.
Praesent euismod ac nulla in lobortis.
Sed sodales tellus non semper feugiat."""

def wrapped_lines(line, width=80):
    whitespace = set(" \n\t\r")
    length = len(line)
    start = 0

    while start < (length - width):
        # we take next 'width' of characters:
        chunk = line[start:start+width+1]
        # if there is a newline in it, let's return first part
        if '\n' in chunk:
            end = start + chunk.find('\n')
            yield line[start:end]
            start = end+1 # we set new start on place where we are now
            continue

        # if no newline in chunk, let's find the first whitespace from the end
        for i, ch in enumerate(reversed(chunk)):
            if ch in whitespace:
                end = (start+width-i)
                yield line[start:end]
                start = end + 1
                break
            else: # just for readability
                continue
    yield line[start:]

for line in wrapped_lines(lorem, 30):
    print line

rawDescription: How to limit the number of characters per line without affecting a word?

description: Limit the number of characters per line without affecting a word


code:
while my_list.count(1) != 25: # check if there is more or less 1 (heads) in the list
    print "Throwing heads and tails:"
    my_list = []
    for i in range(50):
        toss = random.randint(int(1),int(2)) #tried this also without int() = (1,2)
        my_list.append(toss)

rawDescription: Why my_list.remove(x) doesn't work

description: Why my_list.remove(x) doesn't work


code:
print json.loads('{}', cls=ConcatJSONDecoder)
print json.load(open('file'), cls=ConcatJSONDecoder)
print json.loads('{}{} {', cls=ConcatJSONDecoder)

rawDescription: Retrieving JSON objects from a text file (using Python)


code:
#!/usr/bin/env python

from brcd_ncclient import *

manager.logging.basicConfig(filename='ncclient.log', level=manager.logging.DEBUG)

manager.CAPABILITIES = ['urn:ietf:params:netconf:capability:writeable-running:1.0', 'urn:ietf:params:netconf:base:1.0']

with manager.connect(host='host', username='user', password='password') as m:
    netironcfg = brcd_new_ele('netiron-config', BROCADE_1_0)
    mplsconfig = brcd_sub_ele(netironcfg, 'mpls-config', BROCADE_1_0)
    filterstr = to_xml(netironcfg)
    c2 = m.get_config(source='running', filter=('subtree', filterstr))
    print c2

rawDescription: How to override stuff in a package at runtime?

description: Override stuff in a package at runtime


code:
output = r'\begin{{document}} ... \author{{{email}}}'.format(
email='user@example.org')
print output

rawDescription: How to format a LaTeX string in python?

description: Format a LaTeX string in python


rawDescription: how to use numpy.vectorize or numpy.frompyfunc

description: Use numpy.vectorize or numpy.frompyfunc


code:
def makeDict(f2):
  dOut = {}
  with open(f2) as f:
     for l in f:
        dOut[ l.split(':')[0] ] = l.split(':')[1]

  return dOut

def merge(f1,f2,f3):

  pwd = makeDict(f2)
  print pwd
  with open(f3, "a") as outputFile:

     with open(f1) as usernameFile:
        for line in usernameFile:
           if line.strip() == '': continue
           username = line.split(':')[0]
           lastname = line.split(':')[3]
           if lastname in pwd:
              outputFile.write(username + ':' + pwd[lastname] + '\n')

merge('f1.txt', 'f2.txt', 'f3.txt'  )

rawDescription: Python - Writing to a new file from another file


code:
import os

path = '/home/chris/sources/a/b/c/d/e/test.py'.split('sources')
if len(path) > 1:
    print os.path.join(path[0], 'sources')

rawDescription: Path manipulation

description: Path manipulation


code:
for number, count in output.iteritems():
    print "{0}: {1}".format(number, "X " * count)

rawDescription: Python count the frequency of each number in a list without any count() functions or without importing anything


rawDescription: Dynamic string formatting in python

description: Dynamic string formatting in python


code:
seen = set()
for number in myList:
    if number in seen:
        print "Number repeated!"
    seen.add(number) # won't duplicate

rawDescription: How to check if two numbers in a list are the same

description: Check if two numbers in a list are the same


code: print 'old_{:03d}'.format(1)

rawDescription: How do I add serial number by replacing character in string in Python?

description: Add serial number by replacing character in string in Python


code:
max_y = max(y_av)  # Find the maximum y value
max_x = x[y_av.argmax()]  # Find the x value corresponding to the maximum y value
print max_x, max_y

rawDescription: Python find x value to corresponding max y value in plot


code:
operators = ["add", "mul"]

class OperatorHackiness(object):
  """
  Use this base class if you want your object
  to intercept __add__, __iadd__, __radd__, __mul__ etc.
  using __getattr__.
  __getattr__ will called at most _once_ during the
  lifetime of the object, as the result is cached!
  """

  def __init__(self):
    # create a instance-local base class which we can
    # manipulate to our needs
    self.__class__ = self.meta = type('tmp', (self.__class__,), {})

# add operator methods dynamically, because we are damn lazy.
# This loop is however only called once in the whole program
# (when the module is loaded)
def create_operator(name):
  def dynamic_operator(self, *args):
    # call getattr to allow interception
    # by user
    func = self.__getattr__(name)
    # save the result in the temporary
    # base class to avoid calling getattr twice
    setattr(self.meta, name, func)
    # use provided function to calculate result
    return func(self, *args)
  return dynamic_operator

for op in operators:
  for name in ["__%s__" % op, "__r%s__" % op, "__i%s__" % op]:
    setattr(OperatorHackiness, name, create_operator(name))

# Example user class
class Test(OperatorHackiness):
  def __init__(self, x):
    super(Test, self).__init__()
    self.x = x

  def __getattr__(self, attr):
    print "__getattr__(%s)" % attr
    if attr == "__add__":
      return lambda a, b: a.x + b.x
    elif attr == "__iadd__":
      def iadd(self, other):
        self.x += other.x
        return self
      return iadd
    elif attr == "__mul__":
      return lambda a, b: a.x * b.x
    else:
      raise AttributeError

## Some test code:

a = Test(3)
b = Test(4)

# let's test addition
print a + b # this first call to __add__ will trigger
            # a __getattr__ call
print a + b # this second call will not!

# same for multiplication
print a * b
print a * b

# inplace addition (getattr is also only called once)
a += b
a += b
print a.x # yay!

rawDescription: Intercept operator lookup on metaclass

description: Intercept operator lookup on metaclass


code:
data.ts_placed = pd.to_datetime(data.ts_placed)
if(not isinstance(data.ts_placed[0], pd.lib.Timestamp)):
    print 'Dates not converted correctly'

rawDescription: how to import dates in python

description: Import dates in python


code:
import requests

def wrapper(thelist):
    return thelist[0], thelist[1:]

# URL = "http://wps.aw.com/wps/media/objects/8992/9208383/Data_Sets/Ascii/Chapter1/HTWT1.txt"
URL = "http://wps.aw.com/wps/media/objects/8992/9208383/Data_Sets/Ascii/Chapter7/CARS7.txt"

response = requests.get(URL)

data = dict()

# headers = ['X', 'Y']
headers = ['Make', 'Model', 'Time', 'Speed', 'Top', 'Weight', 'HP'] # Must be in order and without OBS

text = response.content.decode('ISO-8859-1').encode('utf-8').replace('\x00', '').strip()[2:]
for row in text[2:].splitlines()[1:]:
    OBS, extras = wrapper(row.split('\t'))
    helper_dict = dict()

    for extra in extras:
        header = headers[extras.index(extra)]
        helper_dict[header] = extra
    data[int(OBS)] = helper_dict

print data

rawDescription: Collecting data from .txt url in Python with strange encoding


code:
threshold = .6
a = np.random.random(10000) - threshold
b = np.random.random(10000) - threshold

def f1(x_in, y_in):
    i = np.nonzero( (x_in>0) & (y_in>0) )   # main line of interest
    return x_in[i], y_in[i]

def f2(x_in, y_in):
    i = (x_in>0) & (y_in>0)  # main line of interest
    return x_in[i], y_in[i]

print threshold, len(f1(a,b)[0]), len(f2(a,b)[0])
print timeit("f1(a, b)", "from __main__ import a, b, f1, f2", number = 1000)
print timeit("f2(a, b)", "from __main__ import a, b, f1, f2", number = 1000)

rawDescription: Get pairs of positive values efficiently with numpy


code:
somelist = ['About', 'Absolutely', 'After', 'Aint', 'Alabama', 'AlabamaBill', 'All', 'Also', 'Amos', 'And', 'Anyhow', 'Are', 'As', 'At', 'Aunt', 'Aw', 'Bedlam', 'Behind', 'Besides', 'Biblical', 'Bill', 'Billgone']
from itertools import groupby
from operator import itemgetter

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

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


code:
replacements = { 'a': 'l', 'b': '5', 'c': 'o' }

x = input("Enter your message: ")
print "".join([replacements.get(val, "") for val in x])

rawDescription: How to use input string as variable call in python?

description: Use input string as variable call in python


code:
import numpy as np
from sklearn import datasets
from sklearn.decomposition import PCA
from sklearn.svm import SVC
from sklearn import cross_validation

iris = datasets.load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.4, random_state=0)
pca = PCA(n_components=2)# adjust yourself
pca.fit(X_train)
X_t_train = pca.transform(X_train)
X_t_test = pca.transform(X_test)
clf = SVC()
clf.fit(X_t_train, y_train)
print 'score', clf.score(X_t_test, y_test)
print 'pred label', clf.predict(X_t_test)

rawDescription: How to do PCA and SVM for classification in python

description: Do PCA and SVM for classification in python


code: print '\n'.join(' '.join(str(n) for n in row).center(80) for row in triangle)

rawDescription: getting pascals to print in multiple rows

description: Getting pascals to print in multiple rows


code:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import SocketServer
import simplejson
import random

class S(BaseHTTPRequestHandler):
    def _set_headers(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        self._set_headers()
        f = open("index.html", "r")
        self.wfile.write(f.read())

    def do_HEAD(self):
        self._set_headers()

    def do_POST(self):
        self._set_headers()
        print "in post method"
        self.data_string = self.rfile.read(int(self.headers['Content-Length']))

        self.send_response(200)
        self.end_headers()

        data = simplejson.loads(self.data_string)
        with open("test123456.json", "w") as outfile:
            simplejson.dump(data, outfile)
        print "{}".format(data)
        f = open("for_presen.py")
        self.wfile.write(f.read())
        return

def run(server_class=HTTPServer, handler_class=S, port=80):
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    print 'Starting httpd...'
    httpd.serve_forever()

if __name__ == "__main__":
    from sys import argv

if len(argv) == 2:
    run(port=int(argv[1]))
else:
    run()

rawDescription: Reading JSON from SimpleHTTPServer Post data


code:
for elm in favs.getchildren()[0]:
    print ElementTree.tostring(elm)

rawDescription: flickAPI: How do I list the sets for my user account?

description: FlickAPI: How do I list the sets for my user account


code:
ss = """A<B<C>D
 E<F<G>H
I<J>K>
 L<<M>N
   O<P>>Q
 R<<S>    T<<>"""
print '\n\n%s\n' % ss
print reg.findall(ss)

rawDescription: Tokenization using regexp in Python

description: Tokenization using regexp in Python


code: print diff(lambda t: math.exp(-(t-4)**2), 0)

rawDescription: Arbitrary functions

description: Arbitrary functions


code:
import scipy
import math
import numpy
from scipy import interpolate

x= [1,2,3,4,5,6]
y= [10,20,30]

Y = numpy.array([[i]*len(x) for i in y])
X = numpy.array([x for i in y])
Z = numpy.array([[2.3,3.4,5.6,7.8,9.6,11.2],
                 [4.3,5.4,7.6,9.8,11.6,13.2],
                 [6.3,7.4,8.6,10.8,13.6,15.2]])

tck = interpolate.bisplrep(X,Y,Z)
print interpolate.bisplev(3.5,15,tck)

7.84921875

rawDescription: 2 dimensional interpolation problem

description: 2 dimensional interpolation problem


code:
import Tkinter as tk

# --- functions ---

def check(arg1, arg2, arg3): # 3 args always send by `trace`
    print 'check:', status.get() # always use `get`

def callback():
    # no needed `global status`

    status.set(0) # always use `set` instead of `=`

    print 'callback:', status.get() # always use `get`

# --- main ---

master = tk.Tk()

status = tk.IntVar() # now always use get(), set()
status.set(1)

status.trace('w', check) # w - write (`set` was used)

# use IntVar in Label
l = tk.Label(master, textvariable=status)
l.pack()

b = tk.Button(master, text="Enter", command=callback)
b.pack()

tk.mainloop()

rawDescription: While loop after tkraise?

description: While loop after tkraise


code:
import food

for t in food.Taco.select():
    print "Taco", t.id, ("has" if t.has_cheese else "doesn't have"), "cheese"

rawDescription: Modularizing peewee

description: Modularizing peewee


code:
x = 4
print hex(id(x))

rawDescription: print memory address of Python variable

description: Print memory address of Python variable


rawDescription: How to index a DataFrame based on the number of consecutive days

description: Index a DataFrame based on the number of consecutive days


code:
class A(object):
    def __getattr__(self, attr):
        try:
            return self.__dict__[attr]
        except KeyError:
            self.__dict__[attr] = 'Attribute set to string'
            print 'Assigned attribute'
            return self.__dict__[attr]

obj = A()
print obj.foo

rawDescription: Python __getattr__ behavior? IN ECLIPSE/PyDev console?


code:
print funct(None, do_print=False)
funct(None)

rawDescription: Functions: return and or print

description: Functions: return and or print


code:
RSS_URLS = [
    'http://feeds.feedburner.com/RockPaperShotgun',
    'http://www.gameinformer.com/b/MainFeed.aspx?Tags=preview',
    ]

feeds = []
for url in RSS_URLS:
    feeds.append(feedparser.parse(url))

for feed in feeds:
    for post in feed.entries:
        print post.title

rawDescription: How can I parse multiple URLs in feedparser (Python)?

description: Parse multiple URLs in feedparser (Python)


code:
credential = ('joeuser', 'secret123')
print 'Username:', credential[0]
print 'Password:', credential[1]

rawDescription: Structure accessible by attribute name or index options


code:
import pandas as pd

# first let's create dataframes
colnames = 'label weight quantity'.split()
A = pd.DataFrame.from_records([
        ('A', 5, 3),
        ('B', 6, 1),
        ('D', 10, 2),
        ('C', 2, 4),
    ], columns=colnames)

B = pd.DataFrame.from_records([
        ('A', 5, 2),
        ('D', 2, 1),
        ('B', 5, 4),
    ], columns=colnames)

# we can just concatenate those DataFrames and do calculation:
df = pd.concat([A, B])
df['value'] = df.weight * df.quantity

# sum each group with the same label
df = df.groupby('label').sum()
del df['weight']  # it got messed up anyway and we don't need it

# and calculate means:
df['mean'] = df.value / df.quantity

print df
print(df[['mean', 'quantity']])
#            mean  quantity
# label
# A      5.000000         5
# B      5.200000         5
# C      2.000000         4
# D      7.333333         3

rawDescription: Merge two tables based on intersection in Python


code:
from xml.dom import minidom

text = "

xxxx

"
doc = minidom.parseString(text)

# For each div in the root document
for tag in doc.childNodes:
    # If it's a
 and there's only one
    if len(tag.childNodes) == 1 and tag.childNodes[0].tagName == 'p':
        # p_node =
xxx

        p_node = tag.childNodes[0]
        # p_text_node = xxx
        p_text_node = p_node.childNodes[0]
        value = p_node.nodeValue
        # Delete the
xxx

        p_node.parentNode.removeChild(p_node)
        # Set the

 ->
xxx

        tag.appendChild(p_text_node)

print doc.toxml()

rawDescription: DOM manipulations in Python (if some element contains only one other element...)


code:
#!/usr/bin/env python

import os

text_file = 'text.txt'
cwd = os.path.curdir # '.'

# Walk thru each directory starting at '.' and if the directory contains
# 'text.txt', print each line of the file prefixed by the name containing
# directory.
for root, dirs, files in os.walk(cwd):
    if text_file in files: # We only care IF the file is in this directory.
        print 'Found %s!' % root
        filepath = os.path.join(root, text_file) # './the_peasant/text.txt'
        root_base = os.path.basename(root)       # './the_peasant' => 'the_peasant'
        output = ''
        with open(filepath, 'r') as reader:      # Open file for read/write
            for line in reader:                  # Iterate the lines of the file
                new_line = "%s %s" % (root_base, line)
                print new_line,
                output += new_line               # Append to the output

        with open(filepath, 'w') as writer:
            writer.write(output)                 # Write to the file

        print

rawDescription: Use the folder name as a column in a text file


code:
import sys

def func1(arg):
    print 'func1 received: %s' % arg

if __name__ == '__main__':
    # better to validate sys.argv here
    func1(sys.argv[1])

rawDescription: python invoke script with arguments two folders above

description: Python invoke script with arguments two folders above


code:
import trie

def longest_prefix_match(prefix_trie, search):
    # There may well be a more elegant way to do this without using
    # "hidden" method _getnode.
    try:
        return list(node.value for node in prefix_trie._getnode(search).walk())
    except KeyError:
        return list()

url_list = [
    'http://www.google.com/mail',
    'http://www.google.com/document',
    'http://www.facebook.com',
]

url_trie = trie.Trie()

for url in url_list:
    url_trie[url] = url

searches = ("http", "http://www.go", "http://www.fa", "http://fail")

for search in searches:
    print "'%s' ->" % search, longest_prefix_match(url_trie, search)

rawDescription: Longest Prefix Matches for URLs

description: Longest Prefix Matches for URLs


code: print "{" + ", ".join("{} : {}".format(repr(str(key)), repr(str(value))) for key, value in d.iteritems()) + "}"

rawDescription: python output different when printed to std out than stored in dict

description: Python output different when printed to std out than stored in dict


code:
command = """Application.exe inputFile=./Input.txt
outputFile="Output/Output.txt" outputFileSize=20Kb"""

inputfilename = './File1.txt'
outputfilename = 'Output/File2.txt'
outputfilesize = '90Kb'

command = command.replace("./Input.txt", inputfilename)
command = command.replace("Output/Output.txt", outputfilename)
command = command.replace("20Kb", outputfilesize)

print command

rawDescription: replace multiple strings in python

description: Replace multiple strings in python


code:
data = "\r\n\tSoci\u00e9t\u00e9 implant\u00e9 dans l'internet recrute des t\u00e9l\u00e9conseillers en b to b pour effectuer de la prise de rendez-vous qualifi\u00e9 pour de la conception de site internet et du r\u00e9f\u00e9rencement google."
data.decode('unicode_escape')
print data.decode('unicode_escape')

rawDescription: python ascii to unicode conversion

description: Python ascii to unicode conversion


code:
import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def mousePressEvent(self, QMouseEvent):
        print QMouseEvent.pos()

    def mouseReleaseEvent(self, QMouseEvent):
        cursor =QtGui.QCursor()
        print cursor.pos()

    def initUI(self):

        qbtn = QtGui.QPushButton('Quit', self)
        #qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        #qbtn.clicked.connect(self.test)
        qbtn.resize(qbtn.sizeHint())
        qbtn.move(50, 50)

        self.setGeometry(0, 0, 1024, 768)
        self.setWindowTitle('Quit button')
        self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint)
        #self.connect("clicked", self.test)
        self.show()
    def test(self):
      print "test"

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

rawDescription: Python - PyQT4 how to detect the mouse click position anywhere in the window?


code: print [sql + entry for entry in entries]

rawDescription: printing a "for i in range" command

description: Printing a "for i in range" command


code:
import sys, csv, urllib2
class SmfImpl():
    def __init__( self, ctx ):
        self.ctx = ctx
        self.csv_reader = ''
        self.flag = ['Not Available', '']
        self.ticker = 'XOM'

    def getMorningKey(self, datacode):
        return fetch_keyratios(self, datacode)

#these functions are not in the SmfImpl class because they're in a seperate file
def query_morningstar(self, url_ending):
    MORNING_STAR = 'http://financials.morningstar.com/ajax/exportKR2CSV.html'
    url = MORNING_STAR + '?&callback=?&t=XNYS:%s%s' % (self.ticker, url_ending)
    req = urllib2.Request(url)
    response = sniff_query(req)
    response.readline()
    return csv.reader(response)

def sniff_query(req):
    try:
        response = urllib2.urlopen(req)
    except urllib2.URLError:
        return 'Check Connection'
    sniff = response.readline()
    if str(sniff) == '':
        return 'Not Available'
    return response

def fetch_keyratios(self, datacode):
    if datacode < 1 or datacode > 990:
        return 'Invalid Datacode'
    #check if we already have the data we need
    if(self.flag[0] == 'Check Connection' or
       self.flag[0] == 'Not Available' or self.flag[1] != self.ticker):
        #query remote and check for errors
        self.csv_reader = query_morningstar(self,
                                '&region=usa&culture=en-US&cur=USD&order=desc')
        if(self.csv_reader == 'Check Connection' or
           self.csv_reader == 'Not Available'):
            self.flag[1] = ''
            return self.csv_reader  # actually response status message
        else:
            self.flag[0] = ''
            self.flag[1] = self.ticker
            # read entire dataset in memory skipping lines as neccessary
            self.data = [row[1:] for row in self.csv_reader if len(row) == 12]

    return sort_keyratios(self, datacode)

def sort_keyratios(self, datacode):
    # convert datacode to row, column and return data in that position of list
    row, col = divmod(datacode-1, 11)
    return self.data[row][col]

if __name__ == "__main__":
    smf = SmfImpl(sys.argv)
    ticker = 'XOM'
    for val in range(1, 24):
        print ticker, val,':', smf.getMorningKey(val)

rawDescription: Iterating csvDictReader with nested for loops


rawDescription: How to use csv.reader in python with french character like é,à,ç,ê,ë,

description: Use csv.reader in python with french character like é,à,ç,ê,ë,


code:
fruits = [['apple', 'pear'],
        ['apple', 'pear', 'banana'],
        ['banana', 'pear'],
        ['pear', 'pineapple'],
        ['apple', 'pear', 'banana', 'watermelon']]

from itertools import imap, ifilter, compress

def f1():
    sets={frozenset(e) for e in fruits}
    us=[]
    while sets:
        e=sets.pop()
        if any(e.issubset(s) for s in sets) or any(e.issubset(s) for s in us):
            continue
        else:
            us.append(list(e))
    return us

def f2():
    supersets = imap(lambda a: list(ifilter(lambda x: len(a) < len(x) and set(a).issubset(x), fruits)), fruits)
    new_list = list(compress(fruits, imap(lambda x: 0 if x else 1, supersets)))
    return new_list

def f3():
    return filter(lambda f: not any(set(f) < set(g) for g in fruits), fruits)

def f4():
    sets={frozenset(e) for e in fruits}
    us=[]
    for e in sets:
        if any(e < s for s in sets):
            continue
        else:
            us.append(list(e))
    return us

if __name__=='__main__':
    import timeit
    for f in (f1, f2, f3, f4):
        print f.__name__, timeit.timeit("f()", setup="from __main__ import f, fruits"), f()

rawDescription: Fastest way to remove subsets of lists from a list in Python


rawDescription: Insert a list into another list, without brackets and by replacing the current item in that index


code:
for cur, nxt in pairwise('abc'):
    print cur, 'and', nxt

rawDescription: How to iterate through list in Python/Jython (like with HashMaps)?

description: Iterate through list in Python/Jython (like with HashMaps)


code:
class Foo(object):

  def __init__(self):
    self.bar = None

  def __enter__(self):
    if self.bar != 'open':
      print 'opening the bar'
      self.bar = 'open'
    return self # this is bound to the `as` part

  def close(self):
    if self.bar != 'closed':
      print 'closing the bar'
      self.bar = 'close'

  def __exit__(self, *err):
    self.close()

if __name__ == '__main__':
  with Foo() as foo:
    print foo, foo.bar

rawDescription: How to force deletion of a python object?

description: Force deletion of a python object


code:
for i in xrange(1, N):
  v = p[:N-i] - p[i:]
  dist = numpy.sqrt(numpy.sum(numpy.square(v), axis=1))
  for j in numpy.nonzero(dist > 1.4)[0]:
    print j, i+j

rawDescription: Java vs. Python specific code-fragment performance improvement


code:
import sys
import foobar

def main():
    thefile = foobar.FileParser(sys.argv[1])
    print thefile.cleaned_input()

if __name__ == '__main__':
  main()

rawDescription: Making a Python script Object-Oriented

description: Making a Python script Object-Oriented


code:
# Find runs of consecutive numbers using groupby.  The key to the solution
# is differencing with a range so that consecutive numbers all appear in
# same group.
data = [ 1,  4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
for k, g in groupby(enumerate(data), lambda (i,x):i-x):
    print map(itemgetter(1), g)

rawDescription: Python splitting list based on missing numbers in a sequence


code:
from django.forms.models import model_to_dict
print model_to_dict(profile.user, fields=[], exclude=[]) ## All information of user relation.

rawDescription: Fetch data from models related with foreign key


code:
s1 = structtype()
print s1.a
s1.a=10
print s1.a

rawDescription: Complex matlab-like data structure in python (numpy/scipy)


code:
for x in user['ban_info']:
    print x

rawDescription: Showing key values Array Items on Django Template


code:
import urllib2
import socket

def is_bad_proxy(pip):
    try:
        proxy_handler = urllib2.ProxyHandler({'http': pip})
        opener = urllib2.build_opener(proxy_handler)
        opener.addheaders = [('User-agent', 'Mozilla/5.0')]
        urllib2.install_opener(opener)
        req=urllib2.Request('http://www.example.com')  # change the URL to test here
        sock=urllib2.urlopen(req)
    except urllib2.HTTPError, e:
        print 'Error code: ', e.code
        return e.code
    except Exception, detail:
        print "ERROR:", detail
        return True
    return False

def main():
    socket.setdefaulttimeout(120)

    # two sample proxy IPs
    proxyList = ['125.76.226.9:80', '213.55.87.162:6588']

    for currentProxy in proxyList:
        if is_bad_proxy(currentProxy):
            print "Bad Proxy %s" % (currentProxy)
        else:
            print "%s is working" % (currentProxy)

if __name__ == '__main__':
    main()

rawDescription: Proxy Check in python

description: Proxy Check in python


code:
import lxml.etree

tree_english = lxml.etree.parse('english.xml')
tree_japanese = lxml.etree.parse('japanese.xml')

# index the japanese catalog
j_index = {}
for catalog in tree_japanese.xpath('MessageCatalogue/*[@key]'):
    j_index[catalog.get('key')] = catalog

# find catalog entries in english and merge the japanese
for catalog in tree_english.xpath('MessageCatalogue/*[@key]'):
    j_catalog = j_index.get(catalog.get('key'))
    if j_catalog is not None:
        print 'found match'
        for child in j_catalog:
            print 'add one'
            catalog.append(child)

print lxml.etree.tostring(tree_english, pretty_print=True, encoding='utf8')

rawDescription: Merge two XML files by matching elements by attribute value


code:
from lxml import html

data = """

            span tag contents

        WHAT I WANT

"""

tree = html.fromstring(data)
print tree.xpath('//div[@class="grouptext"]/span[1]/following-sibling::text()')[0].strip()

rawDescription: Extracting the value by xpath in python between tags


code:
def usewalk():
   for root, dirname, filenames in os.walk("."):
       print filename

rawDescription: Initial Directory for os.walk()

description: Initial Directory for os.walk()


code:
from Bio.Align.Applications import ClustalwCommandline
clustalw_exe = r"C:\path\to\clustalw-2.1\clustalw2.exe"
assert os.path.isfile(clustalw_exe), "Clustal W executable missing"
clustalw_cline = ClustalwCommandline(clustalw_exe, infile=fasta_path)
stdout, stderr = clustalw_cline()
print stdout

rawDescription: Create a Dendogram from Genome

description: Create a Dendogram from Genome


code:
def transpose(grid):
    return zip(*grid)

def removeBlankRows(grid):
    return [list(row) for row in grid if any(row)]

print removeBlankRows(transpose(removeBlankRows(transpose(grid))))

rawDescription: Remove row or column from 2D list if all values (in that row or column) are None


code:
import bisect, itertools

locations = [
    "Aaaa|aaaa|Test",
    "Bbbb|bbbb|Test",
    "New Abbey|Ceredigion|United Kingdom",
    "New Albany|Indiana|United States",
    "New Albany|Kansas|United States",
    "New Albany|Mississippi|United States",
    "New Albany|Ohio|United States",
    "Zzzz|zzzz|Test"
    ]

search = "New"
start_index = bisect.bisect_left(locations, search)
print list(itertools.takewhile(lambda x: x.startswith(search), itertools.islice(locations, start_index, None)))

rawDescription: how to apply binary search in python on sorted list of string elements?

description: Apply binary search in python on sorted list of string elements


code:
from lxml import etree

root = etree.Element("root")
root.set("{http://www.w3.org/XML/1998/namespace}space", "preserve")

print etree.tostring(root)

rawDescription: Setting 'xml:space' to 'preserve' Python lxml


code:
for (op, code) in zip(s[0::2], s[1::2]):
    print op, code

rawDescription: Iterate over a string 2 (or n) characters at a time in Python


code: print round.__doc__

rawDescription: Printing a mixed type dictionary with format in python


rawDescription: Remove unicode [u']

description: Remove unicode [u']


code:
alphabet = 'abcdefghijklmnopqrstuvwxyz'

def enbase(x):
    n = len(alphabet)
    if x < n:
        return alphabet[x]
    return enbase(x/n) + alphabet[x%n]

def debase(x):
    n = len(alphabet)
    result = 0
    for i, c in enumerate(reversed(x)):
        result += alphabet.index(c) * (n**i)
    return result

def average(a, b):
    a = debase(a)
    b = debase(b)
    return enbase((a + b) / 2)

print average('a', 'z') #m
print average('aa', 'zz') #mz
print average('cat', 'doggie') #budeel
print average('google', 'microsoft') #gebmbqkil
print average('microsoft', 'google') #gebmbqkil

rawDescription: Average of two strings in alphabetical/lexicographical order


code:
import re
urString = "/dev/vg10/lv10:cp:99"
Matches = re.findall("vg[0-9]*", mv)
print Matches

rawDescription: Extract a particular number followed by a command line argument variable from a string in python


code:
import numpy as np

def split(arr, cond):
  return [arr[cond], arr[~cond]]

a = np.array([1,3,5,7,2,4,6,8])
print split(a, a<5)

a = np.array([[1,2,3],[4,5,6],[7,8,9],[2,4,7]])
print split(a, a[:,0]<3)

rawDescription: How to split an array according to a condition in numpy?

description: Split an array according to a condition in numpy


code:
import sys
from PyQt4 import QtGui, QtCore

class Button(QtGui.QPushButton):
    def __init__(self, parent):
        super(Button, self).__init__(parent)
        self.setAcceptDrops(True)
        #self.setDragDropMode(QAbstractItemView.InternalMove)

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls():
            event.acceptProposedAction()
        else:
            super(Button, self).dragEnterEvent(event)

    def dragMoveEvent(self, event):
        super(Button, self).dragMoveEvent(event)

    def dropEvent(self, event):
        if event.mimeData().hasUrls():
            for url in event.mimeData().urls():
                print str(url.toLocalFile())
            event.acceptProposedAction()
        else:
            super(Button,self).dropEvent(event)

class MyWindow(QtGui.QWidget):
    def __init__(self):
        super(MyWindow,self).__init__()
        self.setGeometry(100,100,300,400)
        self.setWindowTitle("Filenames")

        self.btn = Button(self)
        self.btn.setGeometry(QtCore.QRect(90, 90, 61, 51))
        self.btn.setText("Change Me!")
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.btn)

        self.setLayout(layout)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

rawDescription: PyQt4 - Dragging and dropping files into QPushButton


code:
# Build an example list of sections, e.g. sections = [' tax 1', ' tax 2' ..... ' tax 79']
sections = [" tax {}".format(i) for i in range(1, 80)]

for index, value in enumerate(sections, start=1):
    if index not in [12, 13, 55, 67, 70]:
        print value

rawDescription: Loop except for certain values using Python


code:
from sys import stdin
from re import compile
from datetime import datetime
myFormat = compile(r'(?=([0-2]\d|3[0-1])-(0\d|1[0-2])-(201[2-5]))' )
str1=("0012-10-2012-10-2012", "0031-02-2012");
for s in str1:
    for date in myFormat.findall(s):
        (d,m,y) = map(int, date)
        try:
           datetime(y,m,d)
           print date
        except: pass

rawDescription: How to match a date pattern in strings

description: Match a date pattern in strings


code:
class Junk(object):
    def __ne__(self, other):
        return False

    def __eq__(self, other):
        return False

j = Junk()
print not j == 1
print j != 1

rawDescription: Is there a significant difference between: if not a == 'bar' and if a != 'bar'?


code:
class FrozenSortedTuple:
  """A frozenset that cares about order of tuples. And is not actually frozen."""
  def __init__(self, vals):
    if type(vals) not in [list, set, tuple]:
      raise Exception('Value not a list or set')
    self.vals = vals

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

  def __iter__(self):
    return iter(self.vals)

  def __getitem__(self, key):
    return list(self.vals)[key]

  def __eq__(self, other):
    if len(self) != len(other):
      print "len(self)"
      return False

    for i,item in enumerate(self.vals):
      if item != other[i]:
        return False
    return True

  def __str__(self):
    str_val = str()
    for val in self:
      str_val += str(val)
    return str_val

  def __hash__(self):
    return hash(str(self))

rawDescription: Python class: Why can't I use the method len() inside __eq__(self, other)?


code:
def a_code():
    print "a"
def b_code():
    print "b"

d={'a':a_code,'b':b_code}
test=raw_input("enter a or b")
d[test]()

rawDescription: Is it possible to check if a value is equal to another in python quickly when referring it to a list?


code:
import re
from bs4 import BeautifulSoup

data = """ your html here """

soup = BeautifulSoup(data)
tr = soup.tr

# get classes to exclude
classes_to_exclude = []
for line in tr.style.text.split():
    match = re.match(r'^\.(.*?)\{display:none\}', line)
    if match:
        classes_to_exclude.append(match.group(1))

tags_to_exclude = ['style', 'script']

texts = []
for item in tr.find_all(text=True):
    if item.parent.name in tags_to_exclude:
        continue

    class_ = item.parent.get('class')
    if class_ and class_[0] in classes_to_exclude:
        continue

    if item.parent.get('style') == 'display:none':
        continue

    texts.append(item)

print ''.join(texts.strip())

rawDescription: Exclude hidden tags while scraping using b4


code:
def fun(x,y,z):
    return [j for i in zip(x,y,z) for j in i]
x = ['a', 'b']
y = [1, 2]
z = ['orange', 'apple']
print fun(x,y,z)

rawDescription: Can anyone help me with the following 2.7 Python assignment? [Adding/Sorting List]


code:
tuple = (1,2,3)
one,two,three = tuple
print one,two,three

rawDescription: list of Directorys,files using Python Script

description: List of Directorys,files using Python Script


code:
import requests

data = {
    'year': '1999',
    'month': '05',
    'day': '20',
    'locale': 'en-us'
}
response = requests.post('http://jebbushemails.com/api/email.py', data=data)

results = response.json()
for email in results['emails']:
    print email['dateCentral'], email['subject']

rawDescription: Programmatically download text that doesn't appear in the page source


code:
def convert(x):
    try:
        return float(x)
    except ValueError, e:
        return x.title()

data = [[u'alpha',u'0.1'], [u'bravo',u'0.2'], [u'charlie', u'0.1', u'0.2', u'0.3', u'0.4']]
data = [[convert(element) for element in entry] for entry in data]

print data

rawDescription: Change only one element in list to float or other transformation


code:
import re
a = ["paul Parthipan","paul","sdds","sdsdd",'Mala_Koala','Czarna,Pala']
last_name = "Parthipan"
names=[]
breakers=[' ','_',',']
for ai in a:
    for b in breakers:
        if b in ai:
            names.append(ai.split(b))
full_names=[ai for ai in names if len(ai)==2]
last_names=[r"(?mis){0}".format(re.escape(ai[1])) for ai in full_names]
print last_names
if any(last_name in ai for ai in last_names):
    print "matched"

rawDescription: how to find a particular string in an element of array in python

description: Find a particular string in an element of array in python


code:
# this will iterate through all 'table' and 'tr' tags within each 'table'
res = [tr.text.encode('ascii', 'ignore').replace(':','').split() \
        for table in soup.findAll('table') for tr in table.findAll('tr') \
        if 'color' in tr.text.lower()]

print res
[['Color', 'White']]

rawDescription: Python + BS Picking a specific word(location) form webpage table


code: print "\n".join((url + "?" + str(guid) for guid in sampleguid))

rawDescription: List to string and for loop

description: List to string and for loop


code:
for combo in islice(dashed_combos('the quick brown fox jumped over the lazy dog'), 10):
    print combo

for combo in islice(dashed_combos('the quick brown fox jumped over the lazy dog'), 200, 210):
    print combo

rawDescription: Python replace spaces in string iteratively


code: print sorted(speeds)[len(speeds) * 9 // 10]

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

description: Improve the speed of my selection process, python


code:
import pandas as pd

label_map = {
    1: 'label 1',
    2: 'label 2',
    3: 'label 3',
    4: 'label 4',
    5: 'label 5',
    6: 'label 6',
    7: 'label 7',
    8: 'label 8',
    9: 'label 9',
}

df = pd.read_clipboard()
df['labelID'] = df['labelID'].map(label_map)
print df.to_string(index=False)

rawDescription: In pandas, how do I replace label IDs with label strings?


code:
# print all tags
for tag in soup.findAll():
    print tag.name # TODO: add/update dict

rawDescription: Is there a way in beautiful soup to count the number of tags in a html page


code:
tries = 0
while tries < 3:
    x = raw_input("Enter password Alpha or Alphanumeric! No spaces! ")
    if x.isalnum():
        print "Saved"
        break
    print "Try again"
    tries += 1

rawDescription: Repeat Loop upon False

description: Repeat Loop upon False


code:
with open('Users.csv', 'rt') as f:
     reader = csv.reader(f, delimiter=',')
     for row in reader:
          if username == row[2]: # if the username shall be on column 3 (-> index 2)
              print "is in file"

rawDescription: Check whether string is in CSV

description: Check whether string is in CSV


code:
from functools import wraps
from flask import (
    current_app,
    jsonify,
    request,
)

def validate_json(f):
    @wraps(f)
    def wrapper(*args, **kw):
        try:
            request.json
        except BadRequest, e:
            msg = "payload must be a valid json"
            return jsonify({"error": msg}), 400
        return f(*args, **kw)
    return wrapper

def validate_schema(schema_name):
    def decorator(f):
        @wraps(f)
        def wrapper(*args, **kw):
            try:
                validate(request.json, current_app.config[schema_name])
            except ValidationError, e:
                return jsonify({"error": e.message}), 400
            return f(*args, **kw)
        return wrapper
    return decorator

rawDescription: Flask: Decorator to verify JSON and JSON Schema


code:
from StringIO import StringIO # Makes strings look like files

s = """abcdefg
abcdefghijklmnopqrstuvwxyz"""

for line in hard_wrap(StringIO(s), 12):
   print line,

rawDescription: Python Textwrap - forcing 'hard' breaks

description: Python Textwrap - forcing 'hard' breaks


rawDescription: Cannot convert ascii to utf-8 in python

description: Cannot convert ascii to utf-8 in python


code:
import requests
import json

url = "http://www.polskieradio.pl/Portal/WebPageMethods/Playlista/playlist.aspx"

data = 'program=4&count=1'
headers = {
        'User-Agent': 'curl/7.35.0',
        'Host': 'www.polskieradio.pl',
        'Accept':'*/*',
        'Proxy-Connection': 'Keep-Alive',
        'Content-Type': 'application/x-www-form-urlencoded'
        }

r = requests.post(url, data=data, headers=headers)
print r.content

rawDescription: POST requests to ASP site don't bring a good result


rawDescription: How to perform an operation on this tuple and convert to another tuple in Python?

description: Perform an operation on this tuple and convert to another tuple in Python


code:
import re

li = ('ksjdhfqsd\n'
      '5 12478 abdefgcd ocean__12      ty--\t\t ghtr789\n'
      'qfgqrgqrg',

      '6 48788 bcfgdebc atlantic__7899 %fg#\t\t ghtu12340\n',

      '2 47890 bbcedefg arctic__124    **juyf\t\t ghtr89877',

      '9 54879 bbdecddf antarctic__13  18:13pomodoro\t\t ghtr6798',

      'ksjdhfqsd\n'
      '5 12478 abdefgcd ocean__1247101247887 ty--\t\t ghtr789\n'
      'qfgqrgqrg',

      '6 48788 bcfgdebc atlantic__7899 %fg#\t\t ghtu12940\n',

      '25 47890 bbcedefg arctic__124    **juyf\t\t ghtr89877',

      '9 54879 bbdeYddf antarctic__13  18:13pomodoro\t\t ghtr6798')

tupleRE = ('^\d',
           ' ',
           '\d{5}',
           ' ',
           '[abcdefghi]+',
           ' ',
           '(?=[a-z\d_ ]{14} [^ ]+\t\t ght)',
           '[a-z]+',
           '__',
           '[\d]+',
           ' +',
           '[^\t]+',
           '\t\t',
           ' ',
           'ght',
           '(r[5-9]+|u[0-4]+)',
           '$')

def REtest(ch, tuplRE, flags = re.MULTILINE):
    for n in xrange(len(tupleRE)):
        regx = re.compile(''.join(tupleRE[:n+1]), flags)
        testmatch = regx.search(ch)
        if not testmatch:
            print '\n  -*- tupleRE :\n'
            print '\n'.join(str(i).zfill(2)+' '+repr(u)
                            for i,u in enumerate(tupleRE[:n]))
            print '   --------------------------------'
            # tupleRE doesn't works because of element n
            print str(n).zfill(2)+' '+repr(tupleRE[n])\
                  +"   doesn't match anymore from this ligne "\
                  +str(n)+' of tupleRE'
            print '\n'.join(str(n+1+j).zfill(2)+' '+repr(u)
                            for j,u in enumerate(tupleRE[n+1:
                                                         min(n+2,len(tupleRE))]))

            for i in xrange(n):
                match = re.search(''.join(tupleRE[:n-i]),ch, flags)
                if match:
                    break

            matching_portion = match.group()
            matching_li = '\n'.join(map(repr,
                                        matching_portion.splitlines(True)[-5:]))
            fin_matching_portion = match.end()
            print ('\n\n  -*- Part of the tested string which is concerned :\n\n'
                   '######### matching_portion ########\n'+matching_li + '\n'
                   '##### end of matching_portion #####\n'
                   '-----------------------------------\n'
                   '######## unmatching_portion #######')
            print '\n'.join(map(repr,
                                ch[fin_matching_portion:
                                   fin_matching_portion+300].splitlines(True)) )
            break
    else:
        print '\n  SUCCES . The regex integrally matches.'

for x in li:
    print '  -*- Analyzed string :\n%r' % x
    REtest(x,tupleRE)
    print '\nmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwm'

rawDescription: Traceback for regular expression

description: Traceback for regular expression


code:
nouns = []
with open('tesu.txt', 'r') as file_p:
  for word in file_p.read().split():
    word, tag = word.split('/')
    if (tag.lower() == 'np'):
      nouns.append(word)
print nouns

rawDescription: chunks using python

description: Chunks using python


code:
results_dict = {v:k for k,v in results}
print results_dict['111.111.5.6']
print results_dict['111.111.5.1']
print results_dict['blah']
print results_dict.get('111.111.5.5')
print results_dict.get('123456')

rawDescription: Get item with value from tuple in python


code:
int('280' + 'F', 16)  # => 0x280F, 16: hexadecimal
unichr(int('280' + 'F', 16))  # to unicode object
print unichr(int('280' + 'F', 16))

rawDescription: How to concatenate and output unicode text variables in Python

description: Concatenate and output unicode text variables in Python


code: print "run time (s):", time.time() - start_time

rawDescription: Timer in Python(simultany)

description: Timer in Python(simultany)


code:
import os
from lxml import etree

def dir_as_tree(path):
    """Recursive function that walks a directory and returns a tree
    of nested etree nodes.
    """
    basename = os.path.basename(path)
    node = etree.Element("node")
    node.attrib['name'] = basename
    # Gather some more information on this path here
    # and write it to attributes
    # ...
    if os.path.isdir(path):
        # Recurse
        node.tag = 'dir'
        for item in sorted(os.listdir(path)):
            item_path = os.path.join(path, item)
            child_node = dir_as_tree(item_path)
            node.append(child_node)
        return node
    else:
        node.tag = 'file'
        return node

# Create a tree of the current working directory
cwd = os.getcwd()
root = dir_as_tree(cwd)

# Create an element tree from the root node
# (in order to serialize it to a complete XML document)
tree = etree.ElementTree(root)

xml_document = etree.tostring(tree,
                              pretty_print=True,
                              xml_declaration=True,
                              encoding='utf-8')
print xml_document

rawDescription: Getting a File System Listing Python

description: Getting a File System Listing Python


code:
for i in sorted(labels):
    if true_positives[i] == 0:
        fscore = 0
    else:
        precision = true_positives[i] / float(true_positives[i]+false_positives[i])
        recall = true_positives[i] / float(true_positives[i]+false_negatives[i])
        fscore = 2 * (precision * recall) / float(precision + recall)
    print i, fscore

rawDescription: How to use the confusion matrix module in NLTK? - python

description: Use the confusion matrix module in NLTK? - python


code:
from decorator import decorator
@decorator
def dec(func, *a, **k):
    return func()

@dec
def f(arg1, arg2, arg3=1):
    return

import inspect
print inspect.getargspec(f)

rawDescription: preserve argspec when decorating?

description: Preserve argspec when decorating


code:
a = 'asd'
b = 'dsa'

print all([a,b])
#True

rawDescription: check None in multiples variables

description: Check None in multiples variables


code:
def deco(f):
    print 'applying deco' # this will print when deco is applied to test
    def wrapper():
            print("start")
            f()
            print("end")
    return wrapper
@deco
def test():
    print("Hello world!")

rawDescription: How decorator works?

description: How decorator works


code:
t = "\"productId\":\"111111\""
m = re.match("\W*productId[^:]*:\D*(\d+)", t)
if m:
    print m.group(1)

rawDescription: Extracting Data with Python Regular Expressions


code:
data=[]
flag=False
with open('/tmp/test.txt','r') as f:
    for line in f:
        if line.startswith('&'):
            flag=True
        if flag:
            data.append(line)
        if line.strip().endswith('!'):
            flag=False

print ''.join(data)

rawDescription: Extract string between characters from a txt file in python


code:
import re
import json

str = '{wrapper:{one:{now:""},up:"north",down:"south"}}'
str = re.sub('(?<=[{,])(.*?)(?=:)', r'"\1"', str)
jsonobj = json.loads(str)
print jsonobj

rawDescription: Python parse data with regular expressions turn into dictionary.


code:
def getDF(dic, gender, year):
  return dic[gender][year]

print genDF(dic, 'male', 2014)

rawDescription: how do you filter pandas dataframes by multiple columns

description: Filter pandas dataframes by multiple columns


code:
import SimpleHTTPServer
import SocketServer
import sys

PORT = 1548

class MyHTTPHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    log_file = open('logfile.txt', 'w')
    def log_message(self, format, *args):
        self.log_file.write("%s - - [%s] %s\n" %
                            (self.client_address[0],
                             self.log_date_time_string(),
                             format%args))

Handler = MyHTTPHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT

httpd.serve_forever()

rawDescription: Save logs - SimpleHTTPServer

description: Save logs - SimpleHTTPServer


code: print "If there was a birth every 7 seconds, there would be: ",births,"births"

rawDescription: How can I print variable and string on same line in Python?

description: Print variable and string on same line in Python


code:
fullstr = "my|name|is|will"
all = [p for p, e in enumerate(fullstr) if e == '|']
print fullstr[all[0]+1:all[1]]

rawDescription: Getting Substring in Python

description: Getting Substring in Python


code:
def genfactors(fdict):
    factors = set([1])

    for factor, count in fdict.iteritems():
        for ignore in range(count):
            factors.update([n*factor for n in factors])
            # that line could also be:
            # factors.update(map(lambda e: e*factor, factors))

    return factors

factors = {2:3, 3:2, 5:1}

for factor in genfactors(factors):
    print factor

rawDescription: Python factorization

description: Python factorization


code: print patt.parseString(test).dump()

rawDescription: How can I build a regular expression which has options part

description: Build a regular expression which has options part


code:
class Command(object):
    def subcommand(method): # Regular function in class definition scope.
        method.is_subcommand = True

        return method

    @subcommand
    def common(self):
        print "this subcommand is available to all child classes"

    subcommand = staticmethod(subcommand)
    # Now a static method. Can no longer be called during class definition phase.

rawDescription: Defining a decorator in a class, which is also usuable within the class definition


code:
from BeautifulSoup import BeautifulSoup
html = 'Some text Some other text  MORE TEXT '
soup = BeautifulSoup(html)
for m in soup.findAll('img'):
    m.replaceWithChildren()
print soup

rawDescription: Remove selected text using regular expressions


code:
import subprocess
try:
    if subprocess.call(["/bin/bash", "tar_ssh.sh"]) == 0:
        print "success!"
    else:
        print "failed! :("
except Exception as e:
    print(str(e))

rawDescription: Resuming Python after Tar SSH Pipe shell call?


code:
for row in reader:
    match = False
    for word in ol:
        if word==row[1]:
            match = True
            break

    if not match:
        print row[0],row[1],row[2]

rawDescription: match word list from a file and output remaining words

description: Match word list from a file and output remaining words


rawDescription: Return index values of a sorted row using pandas?


code:
while True:
    try:
        age = int(raw_input("\n So tell me " + str(name) + ", how old are you?: "))
        if age < 18:
            print "\n %s. A minor. You got to be atleast 18 to go on this adventure. Your adventure ends here." % age
            time.sleep(7)
            exit(0)
        elif age >= 60:
            print "\n %s. I think you're to old to go on an adventure. Your adventure ends here." % age
            time.sleep(5)
            exit(0)
        else:
            print "\n %s. You're starting to get old." % age
            break
    except ValueError:
        print "\n Please, put only in numbers!"
        time.sleep(3)

rawDescription: While loop for a raw_input gives strange output in Python


code:
f = lambda x, y: x + y
print apply_pairwise(f, a)

rawDescription: How can I apply a function one column at a time?

description: Apply a function one column at a time


code:
import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
    print root, "consumes",
    print sum(getsize(join(root, name)) for name in files),
    print "bytes in", len(files), "non-directory files"
    if 'CVS' in dirs:
        dirs.remove('CVS')  # don't visit CVS directories

rawDescription: How to generate directory size recursively in python, like du . does?

description: Generate directory size recursively in python, like du . does


code:
def event_stream():
    pubsub = red.pubsub()
    pubsub.subscribe('chat')
    for message in pubsub.listen():
        print message
        yield 'data: %s\n\n' % message['data']

@app.route('/post', methods=['POST'])
def post():
    message = flask.request.form['message']
    user = flask.session.get('user', 'anonymous')
    now = datetime.datetime.now().replace(microsecond=0).time()
    red.publish('chat', u'[%s] %s: %s' % (now.isoformat(), user, message))

@app.route('/stream')
def stream():
    return flask.Response(event_stream(),
                          mimetype="text/event-stream")

rawDescription: How to implement server push in Flask framework?

description: Implement server push in Flask framework


code:
str1= 'add(multiply(1, 4461.2419), multiply(subtract(X1, 5), 475.5))'

def replf(matchobj):
     a= matchobj.group(0)
     float= re.findall('\d+',a)[0]+'.0'
     return re.sub('\d+', float,a)

print re.sub('[\(\,\s](\d+)[\)\,\s]' ,replf,str1)   #no decimals allowed

rawDescription: converting integers to decimals in a string

description: Converting integers to decimals in a string


code:
for key in set(aa) & set(bb):
    if aa[key] == bb[key]:
        print '%s: %s is present in both aa and bb' % (key, value)

rawDescription: python dictionary match key values in two dictionaries

description: Python dictionary match key values in two dictionaries


code:
import sys
import sys
import Queue

def get_shortest_path(par, src, dest):
    '''
    Returns the shortest path as a list of integers
    par - parent information
    src - source vertex
    dest - destination vertex
    '''
    if dest == src:
        return [src]
    else:
        ret = get_shortest_path(par, src, par[dest])
        ret.append(dest)
        return ret

def bfs(edgeList, src, dest):
    '''
    Breadth first search routine. Returns (distance, shortestPath) pair from src to dest. Returns (-1, []) if there is no path from src to dest
    edgeList - adjacency list of graph. Either list of lists or dict of lists
    src - source vertex
    dest - destination vertex
    '''
    vis = set() # stores the vertices that have been visited
    par = {} # stores parent information. vertex -> parent vertex in BFS tree
    distDict = {} # stores distance of visited vertices from the source. This is the number of edges between the source vertex and the given vertex
    q = Queue.Queue()
    q.put((src, 0)) # enqueue (source, distance) pair
    par[src] = -1 # source has no parent
    vis.add(src) # minor technicality, will explain later
    while not q.empty():
        (v,dist) = q.get() # grab vertex in queue
        distDict[v] = dist # update the distance
        if v == dest:
            break # reached destination, done
        nextDist = dist+1
        for nextV in edgeList[v]:
            # visit vertices adjacent to the current vertex
            if nextV not in vis:
                # not yet visited
                par[nextV] = v # update parent of nextV to v
                q.put((nextV, nextDist)) # add into queeu
                vis.add(nextV) # mark as visited
    # obtained shortest path now
    if dest in distDict:
        return (distDict[dest], get_shortest_path(par, src, dest))
    else:
        return (-1, []) # no shortest path

# example run, feel free to remove this
if __name__ == '__main__':
    edgeList = {
        0: [6,],
        1: [2, 7],
        2: [1, 3, 6],
        3: [2, 4, 5],
        4: [3, 8],
        5: [3, 7],
        6: [0, 2],
        7: [1, 5],
        8: [4],
    }
    while True:
        src = int(sys.stdin.readline())
        dest = int(sys.stdin.readline())
        (dist, shortest_path) = bfs(edgeList, src, dest)
        print 'dist =', dist
        print 'shortest_path =', shortest_path

rawDescription: How to find shortest path for raw data

description: Find shortest path for raw data


code:
for index, post in enumerate(posts, 1):
    json_data["post{}".format(index)] = {
           'subject': post.subject,
           'content': post.blog,
           'day'    : post.day_created.strftime('%d %b %Y')
    }

print {"allposts": json_data}

rawDescription: Trying to loop through all entries in a datastore object and adding post numbers in dictionary


code:
form = cgi.FieldStorage()

if "Submit1" in form:
    button = 1
elif "Submit2" in form:
    button = 2
else:
    print "Couldn't determine which button was pressed."

rawDescription: Python CGI Script: Basic HTML Form With Two Submit Buttons


code:
a, b, c = (np.zeros(5) for _ in xrange(3))   # generator
d, e, f = [np.zeros(5) for _ in xrange(3)]   # list comp
print a is b, d is e
# False False

rawDescription: Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()


code:
flags = dict(dict.fromkeys(["a", "b", "c"], True),
             **dict.fromkeys(["d", "e"], False))
print flags

rawDescription: More elegant way of declaring multiple variables at the same time


code: for line in results: print line

rawDescription: Use one line of code to print a for loop


code:
from itertools import combinations

for n in range(len(mydict)):
    for combs in combinations(sorted(mydict), n+1):
        print "{}: '{}'".format(sum(combs), '-'.join([mydict[key] for key in combs]))

rawDescription: Generate all possible combinations in dictionary


code:
import numpy as np
a1 = np.array([[1,1,1],[1,1,1],[1,1,1]])
a2 = np.array([[3,3,3],[3,3,3],[3,3,3]])
a3 = np.array([[5,5,5],[5,5,5],[5,5,5]])
a3d = np.dstack((a1,a2,a3))

a2d = np.array([[1,2,4],[5,5,2],[2,3,3]])

classOut = np.empty_like(a2d)

def find_nearest_idx(array,value):
    idx = (np.abs(array-value)).argmin()
    return idx

# enumerate to get indices
for i,a in enumerate(a2d):
    for ii,v in enumerate(a):
        valStack = a3d[i,ii]
        nearest = find_nearest_idx(valStack,v)
        classOut[i,ii] = nearest

print classOut

rawDescription: numpy classification comparison with 3d array

description: Numpy classification comparison with 3d array


code:
class Person():
    def __init__(self, name, title, children=None):
        self.name = name
        self.title = title
        if children is None:
            self.children = []
        else:
            self.children = children
    def desc(self):
        out = []
        for kid in self.children:
            out.append(kid)
            out.extend(kid.desc())
        return out

p1 = Person("p1", "test")
p2 = Person("p2", "test", children = [p1])
p3 = Person("p3", "test")
p4 = Person("p4", "test", children = [p2, p3])
p5 = Person("p5", "boss", children = [p4])
print [person.title for person in p5.desc()]
print [person.name for person in p5.desc()]

rawDescription: Python: Family-class

description: Python: Family-class


code:
import ConfigParser

cp = ConfigParser.ConfigParser()
cp.read('test.ini')

eg = cp.get('EG SECTION', 'EG')
print repr(eg)  # '\nitem 1,\nitem 2,\nitem 3'

cleaned = ''.join(ch for ch in eg if ch != '\n')  # remove newlines
print cleaned.split(',')  # ['item 1', 'item 2', 'item 3']

rawDescription: New lines with Python's ConfigParser?

description: New lines with Python's ConfigParser


code:
def sliding_sum(someList):
    if len(someList) == 1:
        return []
    return [someList[0] + someList[1]] + sliding_sum(someList[1:])

def pascal_row(row):
    if row == 0:
        return [1]
    previous_row = pascal_row(row-1)
    new_row = [1] + sliding_sum(previous_row) + [1]
    return new_row

for i in range(6):
    print pascal_row(i)

rawDescription: Value for each row in Pascal Triangle - Recursion


code:
# this goes in mystuff.py
def apple():
    print "I AM APPLES!"

rawDescription: How to build the nfcpy source code to a python module?

description: Build the nfcpy source code to a python module


code:
import atexit

def exit_handler():
    print 'My application is ending!'

atexit.register(exit_handler)

rawDescription: How can I turn my python script off from the terminal?

description: Turn my python script off from the terminal


code:
from collections import Counter
scores={'L Messi':18, 'R Falcao':16, 'R Van Persie':14, 'F Torres':0, 'C Ronaldo':16}
scores = Counter(scores)
for name, score in scores.most_common(4):
    print '{}: {}'.format(name, score)

rawDescription: N biggest values of dict with duplicates


code: print bool(My_dict.get(2))

rawDescription: python defaultdict get method return value

description: Python defaultdict get method return value


code:
import csv

file1 =  '/home/vivek/Desktop/stackoverflow/fil1.csv'
file2 =  '/home/vivek/Desktop/stackoverflow/fil2.csv'

with open(file1) as fp1:
    root = csv.reader(fp1)
    rows1 = {}
    for i in root:
        rows1[i[0]]=i
    if "id" in rows1:
        del rows1["id"]

with open(file2) as fp1:
    root = csv.reader(fp1)
    rows2 = {}
    for i in root:
        rows2[i[0]]=i
    if "id" in rows2:
        del rows2["id"]

result = set(rows1.keys()).intersection(set(rows2.keys()))

print "Same Id :", list(result)

rawDescription: How to verify that two different .csv files column ids match with python?

description: Verify that two different .csv files column ids match with python


code:
while not result_queue.empty():
    result_queue.get(False)
    result_queue.task_done()
print "Done"

rawDescription: Processes fail to exit() when job count exceeds n


code:
import glob
import os

filelist = []

os.chdir("folderwithcsvs/")
for counter, files in enumerate(glob.glob("*.csv")):
    filelist.append(files)
    print "do stuff with file:", files, counter

print filelist

for fileitem in filelist:
    print fileitem

rawDescription: Load all csv/txt files from one directory and merge them via python


code:
from scrapy.http import FormRequest
from scrapy.spider import BaseSpider

class LoginSpider(BaseSpider):
    name = 'pinterest'
    start_urls = ['https://www.pinterest.com/login/']
    # you can set the user agent either in the settings or the spider
    user_agent = ('Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) '
                  'AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 '
                  'Mobile/9A334 Safari/7534.48.3')

    def parse(self, response):
        data = {'email': 'XXX@xxx', 'password': 'xxx'}
        # no need for dont_filter
        return FormRequest.from_response(response, formdata=data, callback=self.after_login)

    def after_login(self, response):
        print response.url

rawDescription: Scrapy CSRF cookies not accepted and results in a 302 Redirect


code:
json_data = msg.json()
print json_data['request']

rawDescription: Pushover Acknowlage in python

description: Pushover Acknowlage in python


code:
import timeit
for i in range(1, 10000001, 1000000):
    random_str = randomly(i)
    print timeit.timeit('hashlib.sha1(random_str).hexdigest()',
                        setup='import hashlib; random_str="%s".encode("utf-8")' % random_str,
                        number=200)

rawDescription: What is the complexity of the hash function sha1


code:
for sol in range(numComb):
    print resultsAsync[sol].get()[2,-1] #print final value of z

rawDescription: Python's multiprocessing: speed up a for-loop for several sets of parameters, "apply" vs. "apply_async"


code:
for item in ii1:
   if (item % 4 == 0):
       print s1,item,list1[item-1],dt1,er1

rawDescription: How should the if statement look like?

description: How should the if statement look like


code:
keywords = ["hello", "hi", "how are you", "goodbye", "great"]
completer = MyCompleter(keywords)
readline.set_completer(completer.complete)
readline.parse_and_bind('tab: complete')
for kw in keywords:
    readline.add_history(kw)

input = raw_input("Input: ")
print "You entered", input

rawDescription: How to code autocompletion in python?

description: Code autocompletion in python


code: print list(mytable.filter(Row.val4.__contains__("h"))) #old broken way to call in (__contains__)

rawDescription: Error implementing __contains__ (in) for a Python class


code:
import StringIO
buf = StringIO.StringIO()
outf = csv.writer(buf)
outf.writerow(labels)
outf.writerows(rows)
print buf.getvalue()
# 1,2,3
# 12,16,19
# 13,17,20
# 14,18,21
# 15,,22

rawDescription: Dictionary of Lists to CSV in Python

description: Dictionary of Lists to CSV in Python


code:
print "Beautiful is better than ugly."
##
print "Explicit is better than implicit."
print "Simple is better than complex."
print "Complex is better than complicated."
# this is a comment
print "Flat is better than nested."
### this is also a comment
print "Sparse is better than dense."
##
print "Readability counts."
print "Special cases aren't special enough to break the rules."
print "Although practicality beats purity."
print "Errors should never pass silently."
print "Unless explicitly silenced."

rawDescription: Cell mode in Emacs

description: Cell mode in Emacs


code:
for g in grbs:
    print g.shortName, g.typeOfLevel, g.level # print info about all GRIB records and check names
    if (g.shortName == shortName and g.typeOfLevel == typeOfLevel and g.level == level):
        tmp = np.array(g.values)
        # now work with tmp as numpy array

rawDescription: How to iterate over latitude longitude values of a grib2 file?

description: Iterate over latitude longitude values of a grib2 file


code:
#!/usr/bin/env python

# run-cron.py
# sets environment variable crontab fragments and runs cron

import os
from subprocess import call
import fileinput

# read docker environment variables and set them in the appropriate crontab fragment
environment_variable = os.environ["TEST_ENV"]

for line in fileinput.input("/etc/cron.d/cron-python",inplace=1):
    print line.replace("XXXXXXX", environment_variable)

args = ["cron","-f", "-L 15"]
call(args)

rawDescription: Running cron python jobs within docker

description: Running cron python jobs within docker


code:
while True :
    choice=raw_input("Your choice: ")
    for i in range(2):
     if choice==i+1 and fileOrRandom()=="A" :
       fileName=raw_input("Please Enter The Name of the File to sort: ")
       sorter = choices[choice](fileName)
       sorter.timedSort(sorter.sort)
     elif choice==i+1 and fileOrRandom()=="B" :
       num = raw_input("How many random numbers would you like to sort: ")
       sorter = choices[choice](None,num)
       sorter.timedSort(sorter.sort)
     elif choice in ['q','Q']: break
     else: print "Bad choice"

rawDescription: Nested Choices in a python program

description: Nested Choices in a python program


code:
dtstart = datetime(2004, 01, 01)
until = datetime(2005, 10, 01)
rule = [dtstart + timedelta(i) for i in xrange((until - dtstart).days + 1)]

rawDescription: Number of particular months/weekdays between two dates


code:
p = re.compile(r'^(?P<FullName>(?P<Given>\w+) (?P<Middle>\w\.) (?P<Family>\w+))$', re.MULTILINE)
str = "Jack A. Smith\nMary B. Miller"
matches = re.finditer(p, str)
for match in matches:
    info = match.groupdict()  ## pulling out the match as dictionary
    print info
    print info['Family']

rawDescription: basic regex questions in python to help me learn it

description: Basic regex questions in python to help me learn it


code:
import os
import fnmatch

for root, dirs, files in os.walk('.'):
    for d in dirs[:]:
        if fnmatch.fnmatch(d, 'tmp_*'):
            dirs.remove(d)
    print root

rawDescription: Python implementation of Unix find's -prune flag


code: print '1'.zfill(6)

rawDescription: How to add enough zeros before a number in order to have six digits for this number using Python?

description: Add enough zeros before a number in order to have six digits for this number using Python


code:
from datetime import date

def read_file2(fname):
    data = []
    with open(fname, 'r') as f:
        for line in f:
            row = map(int, line.strip().split())
            if len(row) == 2:
                if data:
                    yield key, data
                month, year = row
                key = date(year, month, 1)
                data = []
            else:
                data.extend(row)
        if data:
            yield key, data

for rec_date, data in read_file2('data.txt'):
    print rec_date, data[:5], '... (', len(data), ")"

rawDescription: How to read this ASCII data into Python lists or numpy arrays?

description: Read this ASCII data into Python lists or numpy arrays


code:
import numpy as np
m = np.array(my_LINEARSVC_cross)
print m.sum() - m.trace()

rawDescription: Addition of values in a 10x10 python pandas dataframe excluding the diagonal


code:
import locale
import os

locale.setlocale(locale.LC_ALL, "")

def get_size(state, root, names):
    paths = [os.path.realpath(os.path.join(root, n)) for n in names]
    # handles dangling symlinks
    state[0] += sum(os.stat(p).st_size for p in paths if os.path.exists(p))

def print_sizes(root):
    total = 0
    paths = []
    state = [0]
    n_ind = s_ind = 0
    for name in sorted(os.listdir(root)):
        path = os.path.join(root, name)
        if not os.path.isdir(path):
            continue

        state[0] = 0
        os.path.walk(path, get_size, state)
        total += state[0]
        s_size = locale.format('%8.0f', state[0], 3)
        n_ind = max(n_ind, len(name), 5)
        s_ind = max(s_ind, len(s_size))
        paths.append((name, s_size))

    for name, size in paths:
        print name.ljust(n_ind), size.rjust(s_ind), 'bytes'
    s_total = locale.format('%8.0f', total, 3)
    print '\ntotal'.ljust(n_ind), s_total.rjust(s_ind), 'bytes'

print_sizes('.')

rawDescription: Python: List Directories and Sizes

description: Python: List Directories and Sizes


code:
def bazaar1(player_data):
  print "\"Welcome to the Hyrule Bazaar\", the storekeeper mumbles."
  print '\n'.join(["What would you like to buy? We have:\n",
                   'Swords (10c)',
                   'Shields (10c)',
                   'Bows (10c)',
                   'Arrows (10 for 10c)'])

  bazaar_c = raw_input("> ")
  if "sword" in bazaar_c:
    if player_data['coins'] >= 10:
        print "You got the Hero Sword!\n"
        player_data['inventory'].append('sword')
        player_data['coins'] -= 10
    else:
        print "You don't have enough coins"
    bazaar1(player_data)
#...

rawDescription: Coin count consistency through game

description: Coin count consistency through game


code:
from subprocess import Popen, PIPE
import re

class v2Manager(object):
    def __init__(self, addr='127.0.0.1', community='public'):
        self.addr      = addr
        self.community = community
        self._index = dict()

    def bulkwalk(self, oid='ifName'):
        cmd = 'snmpbulkwalk -v 2c -Osq -c %s %s %s' % (self.community,
            self.addr, oid)
        po = Popen(cmd, shell=True, stdout=PIPE, executable='/bin/bash')
        output = po.communicate()[0]
        result = dict()
        for line in re.split(r'\r*\n', output):
            if line.strip()=="":
                continue
            idx, value = re.split(r'\s+', line, 1)
            idx = idx.replace(oid+".", '')
            result[idx] = value
        return result

    def bulkwalk_index(self, oid='ifOutOctets'):
        result = dict()
        if not (self._index==dict()):
            vals = self.bulkwalk(oid=oid)
            for key, val in vals.items():
                idx = self._index.get(key, None)
                if not (idx is None):
                    result[idx] = val
                else:
                    raise ValueError, "Could not find '%s' in the index (%s)" % self.index
        else:
            raise ValueError, "Call the index() method before calling bulkwalk_index()"
        return result

    def get_index(self, oid='ifOutOctets', index=''):
        # This method is horribly inefficient... improvement left as exercise for the reader...
        if index:
            return self.bulkwalk_index().get(index, "<unknown>")
        else:
            raise ValueError, "Please include an index to get"

    def index(self, oid='ifName'):
        self._index = self.bulkwalk(oid=oid)

rawDescription: How can I check the data transfer on a network interface in python?

description: Check the data transfer on a network interface in python


code:
columns = [name for name in percentages]
for name in columns:
    print str(percentages[name]) + "% of 1 in column " + name

rawDescription: Python Import data dictionary and pattern


code:
from pyparsing import *

functor1 = Literal('a') | Literal('b') | Literal('c')
functor2 = Word('a') | Word('b') | Word('c')
functor3 = Keyword('a') | Keyword('b') | Keyword('c')
functor4 = Combine(Keyword('a') | Keyword('b') | Keyword('c'))

functor1.setName("Literal('a') | Literal('b') | Literal('c')")
functor2.setName("Word('a') | Word('b') | Word('c')")
functor3.setName("Keyword('a') | Keyword('b') | Keyword('c')")
functor4.setName("Combine(Keyword('a') | Keyword('b') | Keyword('c'))")
functors = [functor1, functor2, functor3, functor4]

tests = "a b c aaa bbb ccc after before".split()
for func in functors:
    print func
    for t in tests:
        try:
            print t, ':', func.parseString(t)
        except ParseException as pe:
            print pe
    print

rawDescription: pyparsing - defining keywords - compare Literal, Word, Keyword and Combine

description: Pyparsing - defining keywords - compare Literal, Word, Keyword and Combine


code:
import os.path

filename = 'foo.zip'

dispatch = {'.zip': handle_zip, '.7z': handle_7z}
_, extension = os.path.splitext(filename)
try:
    rv = dispatch[extension](filename)
except KeyError:
    print 'Unknown extension', extension
    rv = None

rawDescription: python: Organizing object model of an application

description: Python: Organizing object model of an application


code:
from functools import wraps
import config

def decorator_example(f):
    @wraps(f)
    def f_example(*args, **kwargs):
        print "The value I need is", config.test_value
        return f(*args, **kwargs)
    return f_example

@decorator_example
def test():
    print "running test"

if __name__ == '__main__':
    config.configure()
    test()

rawDescription: how can I avoid using a global variable as a parameter to a python decorator

description: Avoid using a global variable as a parameter to a python decorator


rawDescription: Selective iteration over list

description: Selective iteration over list


code:
import xml.etree.ElementTree as ET
et = ET.parse('s.xml')
print [e.findtext('.') for e in et.findall('.//*')]

rawDescription: Python Dom Parsing

description: Python Dom Parsing


code:
from splinter import Browser

browser = Browser()
browser.visit("http://mdn.github.io/web-storage-demo")

# get the local storage
storage = LocalStorage(browser)

# get an item
print storage.get("bgcolor")

# set an item
storage.set("size", 452)

# remove an item
storage.remove("size")

# get all items
for key, value in storage.get_all().iteritems():
  print key + ': ' + value

# delete all items
storage.clear()

rawDescription: Access and manage local storage from Splinter?


code:
import os, sys
from collections import defaultdict

def walkIt(folder):
    for (path, dirs, files) in os.walk(folder):
        size = getDirSize(path)
        stats = getExtensionStats(files)

        # only get the top 3 extensions
        print '%s (%s, %s)'%(path, size, stats[:3])

def getExtensionStats(files):
    # get all file extensions
    extensions = [f.rsplit(os.extsep, 1)[-1]
        for f in files if len(f.rsplit(os.extsep, 1)) > 1]

    # count the extensions
    exCounter = defaultdict(int)
    for e in extensions:
        exCounter[e] += 1

    # convert count to percentage
    percentPairs = [(e, 100*ct/len(extensions)) for e, ct in exCounter.items()]

    # sort them
    percentPairs.sort(key=lambda i: i[1])
    return percentPairs

def getDirSize(root):
    size = 0
    for path, dirs, files in os.walk(root):
        for f in files:
            size +=  os.path.getsize( os.path.join( path, f ) )
    return size

if __name__ == '__main__':
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    walkIt(path)

rawDescription: Directory sizes and extensions

description: Directory sizes and extensions


code:
x = numpy.linspace(-1,1,11)
print x

rawDescription: Fixing the singularity of a function

description: Fixing the singularity of a function


code:
# pip install selenium
from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get("http://voyager.jpl.nasa.gov/where/index.html")
time.sleep(5)
elem = driver.find_element_by_class_name("tr_dark")
print elem.text
driver.close()

rawDescription: Using BeautifulSoup for html scraping

description: Using BeautifulSoup for html scraping


code:
#!/usr/bin/env python

print "The numbers: "
for num in xrange(6):
    print num

rawDescription: Using For Loop In stead of While loop in Python


code:
d = { 1:['a','a'],2:['b','b'], 3:['c'] }
amount = []
for key, value in d.iteritems():
    amount += [key] * len(value)
print amount

rawDescription: Duplicate values in a dictionary

description: Duplicate values in a dictionary


code:
source
import socket, time, urllib, urlparse

URL = 'http://example.com/123'

print URL, 'timing:'

urlinfo = urlparse.urlparse(URL)

start = time.time()
ip = socket.gethostbyname( urlinfo.netloc )
dns_tm = time.time()-start
print 'DNS:\t\t{:.3f} seconds'.format( dns_tm )

start = time.time()
_data = urllib.urlopen(URL).read()
load_tm = time.time()-start
print 'load:\t\t{:.3f} seconds'.format( load_tm )
print 'w/o DNS:\t{:.3f} seconds'.format( load_tm-dns_tm )

rawDescription: Measuring page speed and network time with Python like HTML5 Navigation Timing does?


code:
divisor = 1.0
with open("input.txt") as fd:
    for line in fd:
        fields = line.strip().split()
        if len(fields) > 0:
            if fields[0] == 'TOPIC:':
                divisor = float(fields[-1])
            if len(fields) == 2:
                fields[-1] = str(float(fields[-1]) / divisor)
        print ' '.join(fields)

rawDescription: Divide column 2 with a particular value that is in the heading


code:
#!/usr/bin/env python

def main():
    print 'main function'
    args = one() # returns my_args, which i'm assigning to args
    print 'i got args from one():', args
    print args.pop()

def one():
    print 'one function'
    my_args = ["QA-65"]
    return my_args

def two():
    print 'two function'

if __name__ == "__main__":
    main()
    #getattr(my_args, pop)
    # ^^^ Moved this up to main() ^^^

rawDescription: AttributeError: 'function' object has no attribute 'my_args'


code:
for line in parsedlog:
    if 'LOG_ADDR' in line:
        print re.search(r'\S+(?=\n)', line).group()

rawDescription: Cleanest way to get a value from a list element


code:
import requests

def consumeGETRequestSync():
 params = {'test1':'param1','test2':'param2'}
 url = 'http://httpbin.org/get'
 headers = {"Accept": "application/json"}
 # call get service with headers and params
 response = requests.get(url, headers = headers,data = params)
 print "code:"+ str(response.status_code)
 print "******************"
 print "headers:"+ str(response.headers)
 print "******************"
 print "content:"+ str(response.text)

consumeGETRequestSync()

rawDescription: Python REST Consumption

description: Python REST Consumption


code:
import cookielib
import mechanize

br = mechanize.Browser()

cj = cookielib.CookieJar()
ck = cookielib.Cookie(version=0, name='ListFundShowDisclaimer', value='true', port=None, port_specified=False,
                      domain='www.prudential.com.hk', domain_specified=False, domain_initial_dot=False, path='/',
                      path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None,
                      rest={'HttpOnly': None}, rfc2109=False)
cj.set_cookie(ck)
br.set_cookiejar(cj)

br.open("http://www.prudential.com.hk/PruServlet?module=fund&purpose=searchHistFund&fundCd=MMFU_U")
print br.response().read()

rawDescription: Newbie: How to overcome Javascript "onclick" button to scrape web page?


code:
from multiprocessing import Pool

def task(arg):
    """Called by the workers"""
    return arg

def callback(arg):
    """Called by the main process"""
    pass

if __name__ == "__main__":
    pool = Pool()
    njobs = 10000
    print "Enqueuing tasks"
    for i in xrange(njobs):
        pool.apply_async(task, (i,), callback=callback)
    print "Closing the pool"
    pool.close()
    print "Joining the pool"
    pool.join()
    print "Done"

rawDescription: Processes fail to exit() when job count exceeds n


rawDescription: Scipy label dilation

description: Scipy label dilation


code:
import sys
for l in sys.stdin:
    print l

rawDescription: How do I create a python subprocess using a file for stdin starting at a certain line?

description: Create a python subprocess using a file for stdin starting at a certain line


code:
import re

text = "! This is an example.A particular case.Made up of more "\
       "than once sentence?Yes, it is.But no blank !!!That's"\
       " a problem ????Yes.I think so! :)"

res = re.split('([.?!])',text)

print [ ''.join(res[i:i+2]) for i in xrange(0,len(res),2) ]

rawDescription: Non-consuming regular expression split in Python


code:
alphabet = "ab"

for x in itertools.imap(''.join, itertools.chain(
          *(itertools.product(alphabet, repeat=i)
            for i in range(1,9)))):
    print x

rawDescription: Python 2.7: iterate over all combinations of a string in one line of code


code:
from django.dispatch import receiver
from django.http import HttpResponse

from johnny.signals import qc_hit, qc_miss
from models import TestModel

def index(self):
    objs = TestModel.objects.all()
    print objs
    print objs.filter(prop_a='a') #Causes another database or cache hit
    return HttpResponse("success")

def generate(self):
    generate_data()
    return HttpResponse("generated")

def generate_data():
    properties = [ 'a', 'b', 'c', 'd', 'e']
    for i in xrange(len(properties)):
        for j in xrange(len(properties)):
            test_model = TestModel(prop_a=properties[i], prop_b=properties[j])
            test_model.save()

@receiver(qc_hit)
def cache_hit(sender, **kwargs):
    print "cache hit"

@receiver(qc_miss)
def cache_miss(sender, **kwargs):
    print "cache miss"

rawDescription: Accessing Johnny Cache Data

description: Accessing Johnny Cache Data


rawDescription: Inserting values into a list whilst looping over it (Python)


code:
print rfid_formatter(120368)
print "0000120368"
print rfid_formatter(4876298)
print "0004876298"

rawDescription: Python reading Wiegand dropping Zeros

description: Python reading Wiegand dropping Zeros


code:
class Child(BaseClass):
    def Method1(self, x=None, y=None):
        if x is None and y is None:
            # arguments left at defaults, call base method
            return super(Child, self).Method1()

        print 'BAR: {} and {}'.format(x, y)

rawDescription: Overridden vs. virtual class methods in Python


code:
import urllib2
import datetime
from bs4 import BeautifulSoup

URL = 'http://tides.mobilegeographics.com/locations/3881.html'
soup = BeautifulSoup(urllib2.urlopen(URL))
pre = soup.find_all('table')[3].find('pre').text

today = datetime.date.today().strftime("%Y-%m-%d")
for line in pre.split('\n'):
    if today in line:
        print line

rawDescription: Scrape only rows with today's date from table


code:
import random
import time

class Data(object):
    ID = 0
    def __init__(self, data):
        Data.ID+=1
        self.id =Data.ID
        self.data = data
        self.duplicates = None

def fill_data(N):
    data_list = []
    # create alist of random data
    sample = list("anuraguniyal")
    for i in range(N):
        random.shuffle(sample)
        data_list.append(Data("".join(sample)))
    return data_list

def find_duplicate(data_list):
    data_map = {}
    for data in data_list:
        if data.data in data_map:
            data_map[data.data].append(data)
        else:
            data_map[data.data] = [data]

        data.duplicates = data_map[data.data]

st = time.time()
data_list = fill_data(500000)
print "fill_data time:", time.time()-st
st = time.time()
find_duplicate(data_list)
print "find_duplicate time:", time.time()-st

total_duplicates = 0
max_duplicates = 0
for data in data_list:
    total_duplicates += (len(data.duplicates) - 1)
    max_duplicates = max(len(data.duplicates),max_duplicates)
print "total_duplicates count:",total_duplicates
print "max_duplicates count:",max_duplicates

rawDescription: Large temporary database to sanitize data in Python


code:
import collections
words = "hey hey hello"
c = collections.Counter(words.split())
print c  # Counter({'hey': 2, 'hello': 1})

rawDescription: python dictionary counting how many times a word occurs and linking to the word in a dictionary

description: Python dictionary counting how many times a word occurs and linking to the word in a dictionary


code:
import subprocess
output = subprocess.check_output(['echo', '640x360'])
print output

rawDescription: Redirect subprocess to a variable as a string


code:
import re
input_string = '{string}{string}*{string}{string}*'
for match in re.finditer(r'{([^}]*)}\*?', input_string):
     print str(match.group(0)) #outputs {string}

rawDescription: Python: Regex match or condition?

description: Python: Regex match or condition


rawDescription: How do you change a list of integers and strings into a list of integers and floats in python?

description: Change a list of integers and strings into a list of integers and floats in python


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup("""<project id=""><name>Main account</name><balance>1236.59</balance><htmlchar>$</htmlchar><currency>HKD</currency><balancem>1236.59</balancem></project>""")

print soup.find("name").string

rawDescription: How to extract the value in name using BeautifulSoup?

description: Extract the value in name using BeautifulSoup


code:
#!/usr/bin/env python

def addr_type(context, obj):
    for k, v in context:
        # if id(v) == id(a):
        if v is obj:
            print "k: %s" % k
            print "v: %s" % v
            print "a: %s" % hex(id(v))
            print
    print 15 * "-"

a = b = c = 10
b = 11

for k, v in list(locals().iteritems()):
    if k.startswith('__'):
        continue
    print "k: %s" % k
    print "v: %s" % v
    print "a: %s" % hex(id(v))
    print "##############"

print

if a is b:
    print "a and b is the same objects"
else:
    print "a and b is NOT the same objects"

if a is c:
    print "a and c is the same objects"
else:
    print "a and c is NOT the same objects"

if b is c:
    print "b and c is the same objects"
else:
    print "b and c is NOT the same objects"

print

context = list(locals().iteritems())
addr_type(context, a)
addr_type(context, b)
addr_type(context, c)

new_name = addr_type
print 'Function names =', addr_type.__name__, new_name.__name__

rawDescription: function to print name of passed variable

description: Function to print name of passed variable


code:
import subprocess
import select
import multiprocessing
import time

def log_file_reader(filename, counter):
    f = subprocess.Popen(['tail', '-F',filename], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    p = select.poll()
    p.register(f.stdout)
    while True:
        if p.poll(1):
            line = f.stdout.readline().strip()
            if line:
                '''appends 1 to counter if line is valid'''
                with counter.get_lock():
                    counter.value += 1

def main():
    m = multiprocessing.Manager()
    counter = multiprocessing.Value('i', 0) # A process-safe int, initialized to 0
    # starts up a process keep tailing file
    reader_process = multiprocessing.Process(target=log_file_reader, args=("/home/haifzhan/logfile.log", counter))
    reader_process.start()

    # main thread check the counter every 5 seconds
    while True:
        time.sleep(5)
        with counter.get_lock():
            print "periodically check---counter:{0},id:{1}".format(counter.value, id(counter))

rawDescription: Why are changes to a list made in a sub-process not showing up in the parent process?


code:
# griddata vs SmoothBivariateSpline
# http://stackoverflow.com/questions/3526514/
#   problem-with-2d-interpolation-in-scipy-non-rectangular-grid

# http://www.scipy.org/Cookbook/Matplotlib/Gridding_irregularly_spaced_data
# http://en.wikipedia.org/wiki/Natural_neighbor
# http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html

from __future__ import division
import sys
import numpy as np
from scipy.interpolate import SmoothBivariateSpline  # $scipy/interpolate/fitpack2.py
from matplotlib.mlab import griddata

__date__ = "2010-10-08 Oct"  # plot diffs, ypow
    # "2010-09-13 Sep"  # smooth relative

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

def cosr( x, y ):
    return 10 * np.cos( np.hypot(x,y) / np.sqrt(2) * 2*np.pi * cycle )

def cosx( x, y ):
    return 10 * np.cos( x * 2*np.pi * cycle )

def dipole( x, y ):
    r = .1 + np.hypot( x, y )
    t = np.arctan2( y, x )
    return np.cos(t) / r**3

#...............................................................................
testfunc = cosx
Nx = Ny = 20  # interpolate random Nx x Ny points -> Newx x Newy grid
Newx = Newy = 100
cycle = 3
noise = 0
ypow = 2  # denser => smaller error
imclip = (-5., 5.)  # plot trierr, splineerr to same scale
kx = ky = 3
smooth = .01  # Spline s = smooth * z2sum, see note
    # s is a target for sum (Z() - spline())**2  ~ Ndata and Z**2;
    # smooth is relative, s absolute
    # s too small => interpolate/fitpack2.py:580: UserWarning: ier=988, junk out
    # grr error message once only per ipython session
seed = 1
plot = 0

exec "\n".join( sys.argv[1:] )  # run this.py N= ...
np.random.seed(seed)
np.set_printoptions( 1, threshold=100, suppress=True )  # .1f

print 80 * "-"
print "%s  Nx %d Ny %d -> Newx %d Newy %d  cycle %.2g noise %.2g  kx %d ky %d smooth %s" % (
    testfunc.__name__, Nx, Ny, Newx, Newy, cycle, noise, kx, ky, smooth)

#...............................................................................

    # interpolate X Y Z to xnew x ynew --
X, Y = np.random.uniform( size=(Nx*Ny, 2) ) .T
Y **= ypow
    # 1d xlin ylin -> 2d X Y Z, Ny x Nx --
    # xlin = np.linspace( 0, 1, Nx )
    # ylin = np.linspace( 0, 1, Ny )
    # X, Y = np.meshgrid( xlin, ylin )
Z = testfunc( X, Y )  # Ny x Nx
if noise:
    Z += np.random.normal( 0, noise, Z.shape )
# print "Z:\n", Z
z2sum = np.sum( Z**2 )

xnew = np.linspace( 0, 1, Newx )
ynew = np.linspace( 0, 1, Newy )
Zexact = testfunc( *np.meshgrid( xnew, ynew ))
if imclip is None:
    imclip = np.min(Zexact), np.max(Zexact)
xflat, yflat, zflat = X.flatten(), Y.flatten(), Z.flatten()

#...............................................................................
print "SmoothBivariateSpline:"
fit = SmoothBivariateSpline( xflat, yflat, zflat, kx=kx, ky=ky, s = smooth * z2sum )
Zspline = fit( xnew, ynew ) .T  # .T ??

splineerr = Zspline - Zexact
print "Zspline - Z:", avminmax(splineerr)
print "Zspline:    ", avminmax(Zspline)
print "Z:          ", avminmax(Zexact)
res = fit.get_residual()
print "residual %.0f  res/z2sum %.2g" % (res, res / z2sum)
# print "knots:", fit.get_knots()
# print "Zspline:", Zspline.shape, "\n", Zspline
print ""

#...............................................................................
print "griddata:"
Ztri = griddata( xflat, yflat, zflat, xnew, ynew )
        # 1d x y z -> 2d Ztri on meshgrid(xnew,ynew)

nmask = np.ma.count_masked(Ztri)
if nmask > 0:
    print "info: griddata: %d of %d points are masked, not interpolated" % (
        nmask, Ztri.size)
    Ztri = Ztri.data  # Nans outside convex hull
trierr = Ztri - Zexact
print "Ztri - Z:", avminmax(trierr)
print "Ztri:    ", avminmax(Ztri)
print "Z:       ", avminmax(Zexact)
print ""

#...............................................................................
if plot:
    import pylab as pl
    nplot = 2
    fig = pl.figure( figsize=(10, 10/nplot + .5) )
    pl.suptitle( "Interpolation error: griddata - %s, BivariateSpline - %s" % (
        testfunc.__name__, testfunc.__name__ ), fontsize=11 )

    def subplot( z, jplot, label ):
        ax = pl.subplot( 1, nplot, jplot )
        im = pl.imshow(
            np.clip( z, *imclip ),  # plot to same scale
            cmap=pl.cm.RdYlBu,
            interpolation="nearest" )
                # nearest: squares, else imshow interpolates too
                # todo: centre the pixels
        ny, nx = z.shape
        pl.scatter( X*nx, Y*ny, edgecolor="y", s=1 )  # for random XY
        pl.xlabel(label)
        return [ax, im]

    subplot( trierr, 1,
        "griddata, Delaunay triangulation + Natural neighbor: max %.2g" %
        np.nanmax(np.abs(trierr)) )

    ax, im = subplot( splineerr, 2,
        "SmoothBivariateSpline kx %d ky %d smooth %.3g: max %.2g" % (
        kx, ky, smooth, np.nanmax(np.abs(splineerr)) ))

    pl.subplots_adjust( .02, .01, .92, .98, .05, .05 )  # l b r t
    cax = pl.axes([.95, .05, .02, .9])  # l b w h
    pl.colorbar( im, cax=cax )  # -1.5 .. 9 ??
    if plot >= 2:
        pl.savefig( "tmp.png" )
    pl.show()

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