code:
from datetime import datetime,timedelta

# Convert the timestamps to datetime objects
usetimes = sorted(datetime.strptime(d, '%Y-%m-%d %H:%M:%S') for d in timestamp)

# Set the idle time to compare with later
idletime = timedelta(minutes = 30)

# Start the running total with a timedelta of 0
usage = timedelta()
last = usetimes[0]

for d in usetimes[1:]:
    delta = d - last
    if delta < idletime:
        usage += delta
    last = d

print "total usage:",usage

rawDescription: Python - generate the time difference

description: Python - generate the time difference


code:
import scrapy
from scrapy.http import Request

class FtpSpider(scrapy.Spider):
    name = "mozilla"
    allowed_domains = ["ftp.mozilla.org"]

    handle_httpstatus_list = [404]

    def start_requests(self):
        yield Request('ftp://ftp.mozilla.org/pub/mozilla.org/firefox/releases/README',
                      meta={'ftp_user': 'anonymous', 'ftp_password': ''})

    def parse(self, response):
        print response.body

rawDescription: Using Scrapy to crawl a public FTP server


code:
import pandas as pd
import numpy as np
import io
import datetime as dt

temp=u"""TRX_DATE;A
2013-07-05;1
2013-08-06;1
2015-09-05;2
2015-10-08;2
2015-11-05;2
2015-11-25;2
2015-12-06;3"""

df = pd.read_csv(io.StringIO(temp), sep=";", parse_dates=[0])
print df
#    TRX_DATE  A
#0 2013-07-05  1
#1 2013-08-06  1
#2 2015-09-05  2
#3 2015-10-08  2
#4 2015-11-05  2
#5 2015-11-25  2
#6 2015-12-06  3

print df.dtypes
#TRX_DATE    datetime64[ns]
#A                    int64
#dtype: object

#copy column TRX_DATE to index
#df.index = pd.to_datetime(df['TRX_DATE'])
#no copy, only set column TRX_DATE to index
df = df.set_index(['TRX_DATE'])
print df
#            A
#TRX_DATE
#2013-07-05  1
#2013-08-06  1
#2015-09-05  2
#2015-10-08  2
#2015-11-05  2
#2015-11-25  2
#2015-12-06  3

currentdate = dt.date.today()
print currentdate
#2015-11-06

startdate = currentdate - pd.Timedelta(days=30)
print startdate
#2015-10-07

dflast30 = df.loc[startdate:currentdate]
print dflast30
#            A
#TRX_DATE
#2015-10-08  2
#2015-11-05  2

dflast30 = dflast30.reset_index()
print dflast30
#    TRX_DATE  A
#0 2015-10-08  2
#1 2015-11-05  2

rawDescription: how to slice a dataframe having date field as index?

description: Slice a dataframe having date field as index


code:
import string, itertools

for p in itertools.product(string.digits + string.ascii_lowercase, repeat=3):
    print '&#xf' + ''.join(p)

rawDescription: Python count 0-9 then a-z

description: Python count 0-9 then a-z


rawDescription: python -> combinations of numbers and letters

description: Python -> combinations of numbers and letters


rawDescription: python how to transfer between a list of integers and string of bits?

description: Python how to transfer between a list of integers and string of bits


code:
import inspect

print inspect.getsource(inspect.getmodule(inspect.currentframe()))

rawDescription: How to get the current script's code in Python?

description: Get the current script's code in Python


rawDescription: Variance image in python using gdal and a running window approach


code:
def get_nodes(items=[], ages=[]):
    myresults = []
    print "Searching..."
    # pseudo code
    for item in items:
        if item.age in ages:
            print "\t","valid age"
            myresults.append(item)

        myresults.extend( get_nodes(items=item.children, ages=ages) )

    return myresults

rawDescription: python - return flat list of objects from recursive function

description: Python - return flat list of objects from recursive function


code:
for package in root.xpath('//ns:package', namespaces=namespaces):
    for provider in package.xpath('ns:provider', namespaces=namespaces):
        providerValue = provider.text
        print providerValue
    for actor in package.xpath('ns:actor', namespaces=namespaces):
        print actor.attrib['name']

rawDescription: xmlns namespace breaking lxml

description: Xmlns namespace breaking lxml


code:
import os, fnmatch

#globals
start_dir = os.getenv('md')

def excelfiles(pattern):
    file_list = []
    for root, dirs, files in os.walk(start_dir):
        for filename in files:
            if fnmatch.fnmatch(filename.lower(), pattern):
                if filename.endswith(".xls") or filename.endswith(".xlsx") or filename.endswith(".xlsm"):
                    file_list.append(os.path.join(root, filename))
    return file_list

file_list = excelfiles('*cd*')
for i in file_list: print i

rawDescription: Search through directories for specific Excel files and compare data from these files with inputvalues


rawDescription: Python: Combining 'complex' functions; one has oswalk other writes to file/database


code:
def foo(s):
    nums = [substring.split(',') for substring in s.split()];return ' '.join(','.join(row.__setitem__(1, '0') or row) for row in nums)

print foo('a,b,c d,e,f, g,h,i')

rawDescription: How to write multiple lines of code in Python as a single line?

description: Write multiple lines of code in Python as a single line


code:
# If you're on a POSIX compliant system, and this script is marked as
# executable, the following line will make this file be automatically
# run by the Python interpreter rather than interpreted as a shell script
#!/usr/bin/env python

# We need the sys module to read arguments from the terminal
import sys

# Open the input file, default mode is 'r', readonly, which is a safe default
infile = open(sys.argv[1])

# Prepare a variable for the cluster number to be used within the loop
cluster = ''

# loop through all lines in the file, but first set up a list comprehension
# that strips the newline character off the line for each line that is read
for line in (line.strip() for line in infile):
    if line.startswith('>'):
        # string.split() splits on whitespace by default
        # we want the cluster number at index 1
        cluster = line.split()[1]

        # output this line to stdout unmodified
        print line

    else:
        # output any other line modified by adding the cluster number
        print cluster + ' ' + line

rawDescription: Match a line, append those under it

description: Match a line, append those under it


code:
a = [2, 3, 4, 1.5, 6, 8, 3.2]

for element in a:
    if element == 4:
        a.remove(element)
    else:
        print element

rawDescription: Scrolling objects are jerky and sometimes jump to the right a few pixels [pygame]


code:
for key in your_dict:
  print key

rawDescription: Enumerate Keys in a dictionary?

description: Enumerate Keys in a dictionary


code:
def sort_list(new_list):
    data_list = [1, 3, 2, 5, 4]

    while data_list:
        minimum = data_list[0]  # arbitrary number in list
        for x in data_list:
            if x < minimum:
                minimum = x
        new_list.append(minimum)
        data_list.remove(minimum)

    print new_list

def main():
    new_list = []
    sort_list(new_list)

if __name__ == "__main__":
    main()

rawDescription: Python: creating customer sort method & loop stops before list sorted


code:
#!/usr/bin/python

import pexpect
import threading
import time

# Consumes output of some subprocess in a background thread
class OutletThread(threading.Thread):
    # obj:   pexpect.spawn object
    # delay: time (in seconds) to wait between two reads
    def __init__(self, obj, delay = 1):
        threading.Thread.__init__(self)
        self.obj = obj
        self.on = True
        self.delay = delay
    # main
    def run(self):
        while self.on:
            try:
                self.obj.read_nonblocking(size=2048, timeout=0)
            except pexpect.TIMEOUT:
                pass
            time.sleep(self.delay)

# Brief
#    Print expected output
# Arguments
#    obj:   pexpect.spawn object
#      s:   expected string
# Exceptions
#    pexpect.TIMEOUT if default timeout is not met
def pp(obj, s):
    obj.expect(s)
    print obj.before + obj.after

print("Setting up the environment...")

print("Registry...")
r = pexpect.spawn('./registry.sh')
# Hold a moment for Java RMI Registry to start.
# So that controller can connect properly.
time.sleep(1)

print("Controller...")
c = pexpect.spawn ('./controller.sh')
pp(c, '>')
c_rlt = OutletThread(c)
c_rlt.start()

print("Node...")
n = pexpect.spawn ('./node.sh')
pp(n, '>')
n_rlt = OutletThread(n)
n_rlt.start()

print("Client...")
cl = pexpect.spawn ('./client.sh')
pp(cl, '>')
cl_rlt = OutletThread(cl)
cl_rlt.start()

raw_input("Press Enter to run 'info' at the controller...")

# Disable controller outlet
c_rlt.on = False
# Run command
c.sendline('info')
# Expect output
pp(c, '>')
# Enable controller outlet
c_rlt.on = True

raw_input("Press Enter to exit...")

# Disable and join all outlet threads
c_rlt.on = False
c_rlt.join()
n_rlt.on = False
n_rlt.join()
cl_rlt.on = False
cl_rlt.join()

# Terminate all spawned processes
cl.sendintr()
n.sendintr()
c.sendintr()
r.sendintr()

rawDescription: Expect fails to spawn more than two commands


code: print cookie

rawDescription: Backslashes being added into my cookie in Python


code:
def merger( (leftfile,rightfile,leftvarname,outvarname) ):
    do_the_merge

rawDescription: How to use a map with *args to unpack a tuple in a python function call

description: Use a map with *args to unpack a tuple in a python function call


code:
numbers = [5,1,5,2,4]

for h in range(max(numbers), 0, -1):
   for x in numbers:
      if x >= h:
        print '*',
      else:
        print ' ',
   print ""

rawDescription: Can't figure out how to print * & whitespace vertically instead of hoirzontally


code:
import os

for filename in os.listdir(path):
    print filename

rawDescription: How can I get all filename in different directories?

description: Get all filename in different directories


code:
ARRAY_MACHINE= ['machine1', 'machine2', 'machine3']
print 'The machines are {}'.format(', '.join(ARRAY_MACHINE))
ARRAY_MACHINE= ['machine1', 'machine2']
print 'The machines are {}'.format(', '.join(ARRAY_MACHINE))

rawDescription: print out the pattern in an array in python

description: Print out the pattern in an array in python


code:
df['rank'] = df['conference'].rank(ascending=False)
print df
#                                          conference     IF2013  AR2013  rank
#0                                          HOTMOBILE  16.333333    31.5     1
#1                                               FOGA  13.772727    60.0     2
#2                                            IEA/AIE  10.433735    28.2     3
#3  IEEE Real-Time and Embedded Technology and App...  10.250000    29.0     4
#4                Symposium on Computational Geometry   9.880342    35.0     5
#5                                               WISA   9.693878    43.6     6
#6                                               ICMT   8.750000    22.0     7
#7                                            Haskell   8.703704    39.0     8

rawDescription: Add a ranking ordered column to a Pandas Dataframe


code:
from itertools import chain
print list(chain(*[list((element[1:],[element[0]])) for element in a]))

rawDescription: How can a Python list be sliced such that a column is moved to being a separate element column?


rawDescription: Autocorrelation of a multidimensional array in numpy


code:
charset='ABC'
print '\n'.join(''.join(p) for i in xrange(1,len(charset)+1)
                for p in itertools.product(charset, repeat=i))

A
B
C
AA
AB
AC
BA
BB
BC
CA

rawDescription: How to create every permutation of a character set in every order -Python

description: Create every permutation of a character set in every order -Python


code:
class loadfile(object):

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

    def load(self):
        x = open(self.filename).read()     # open as file and read
        d,k = x.split('; Key configuration;')   # split at Key configration
        d = d.splitlines()
        k = k.splitlines()
        ddict = {}
        for i in d:
                i = i.split(':')
                i = [z.strip() for z in i] # strip whitespace(s)
                if len(i) == 2: # a valid assignment line
                        ddict[i[0]] = i[1] # assignment of value
        #same for keys
        kdict = {}
        for i in k:
                i = i.split(':')
                i = [z.strip() for z in i]
                if len(i) == 2:
                        kdict[i[0]] = i[1]
        print '---Display---'
        for key in kdict:
            print '%10s:%19s' % (key,kdict[key]) # print display part
        print '---Keys---'
        for key in ddict:
            print '%10s:%19s' % (key,ddict[key]) # print key part
        self.kdict = kdict
        self.ddict = ddict
    def save(self):
        pass

x = loadfile("config.ini")
x.load()

rawDescription: pygame.RESIZABLE giving KeyError

description: Pygame.RESIZABLE giving KeyError


code:
def debug(s):
    print s

rawDescription: Alias a function in Python

description: Alias a function in Python


code:
# Get some toy data and extract some information from it
import pandas.io.data as pd
X = pd.DataReader('aapl','yahoo')
rows, cols = X.shape
flds = list(X.columns)

# Indexing with numbers on a numpy matrix will probably be faster
corr = X.corr().values

for i in range(cols):
    for j in range(i+1, cols):
        if corr[i,j] > 0.7:
            print flds[i], ' ', flds[j], ' ', corr[i,j]

rawDescription: How do I loop across a correlation matrix to only give me pairs of correlations above a certain threshold? And/or make it more efficient

description: Loop across a correlation matrix to only give me pairs of correlations above a certain threshold? And/or make it more efficient


code: print results['result'][0]['id']

rawDescription: Exception Value: list indices must be integers, not str


code:
leapyear.py
def leap():
    starting = int(raw_input('Enter starting year: '))
    ending = int(raw_input('Enter ending year: '))

    print 'Leap years between', starting, 'and', ending
    while starting <= ending:
        if starting % 4 == 0 and starting % 100 != 0:
            print(starting)
        if starting % 100 == 0 and starting % 400 == 0:
            print(starting)
        starting += 1

rawDescription: Leap-year program in Python

description: Leap-year program in Python


code:
for link in soup('a'):
    print link.get_text()

rawDescription: How can I extract the text between ?

description: Extract the text between


code:
from itertools import chain
from collections import defaultdict

class Graph(object):
    def __init__(self, edges, vertices=()):
        edges = list(list(x) for x in edges)
        self.edges = edges
        self.vertices = set(chain(*edges)).union(vertices)
        self.tails = defaultdict(list)
        for head, tail in self.edges:
            self.tails[head].append(tail)

    @classmethod
    def from_dict(cls, edge_dict):
        return cls((k, v) for k, vs in edge_dict.iteritems() for v in vs)

class _StrongCC(object):
    def strong_connect(self, head):
        lowlink, count, stack = self.lowlink, self.count, self.stack
        lowlink[head] = count[head] = self.counter = self.counter + 1
        stack.append(head)

        for tail in self.graph.tails[head]:
            if tail not in count:
                self.strong_connect(tail)
                lowlink[head] = min(lowlink[head], lowlink[tail])
            elif count[tail] < count[head]:
                if tail in self.stack:
                    lowlink[head] = min(lowlink[head], count[tail])

        if lowlink[head] == count[head]:
            component = []
            while stack and count[stack[-1]] >= count[head]:
                component.append(stack.pop())
            self.connected_components.append(component)

    def __call__(self, graph):
        self.graph = graph
        self.counter = 0
        self.count = dict()
        self.lowlink = dict()
        self.stack = []
        self.connected_components = []

        for v in self.graph.vertices:
            if v not in self.count:
                self.strong_connect(v)

        return self.connected_components

strongly_connected_components = _StrongCC()

if __name__ == '__main__':
    edges = [('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'),
             ('E', 'A'), ('A', 'E'), ('C', 'A'), ('C', 'E'),
             ('D', 'F'), ('F', 'B'), ('E', 'F')]
    print strongly_connected_components(Graph(edges))
    edge_dict = {'a':['b', 'c', 'd'],
                 'b':['c', 'a'],
                 'c':['d', 'e'],
                 'd':['e'],
                 'e':['c']}
    print strongly_connected_components(Graph.from_dict(edge_dict))

rawDescription: Tarjan's strongly connected components algorithm in python not working


code:
while maxtasks is None or (maxtasks and completed < maxtasks):
    try:
        task = get()  # Pulls an item off the taskqueue
    except (EOFError, IOError):
        debug('worker got EOFError or IOError -- exiting')
        break

    if task is None:
        debug('worker got sentinel -- exiting')
        break

    job, i, func, args, kwds = task
    try:
        result = (True, func(*args, **kwds))  # Runs the function you passed to map
    except Exception, e:
        result = (False, e)
    try:
        put((job, i, result))  # Sends the result back to the parent
    except Exception as e:
        wrapped = MaybeEncodingError(e, result[1])
        debug("Possible encoding error while sending result: %s" % (
            wrapped))

rawDescription: Python Multiprocessing - number of process


rawDescription: GitPython: git.diff(commit_a, commit_b) always returns empty string


code:
from lxml import etree

string = "<text><?oasys _dc21-?>Text <i>contents</i></text>"
tree = etree.fromstring(string)

pis = tree.xpath("//processing-instruction()")
for pi in pis:
    etree.strip_tags(pi.getparent(), pi.tag)

print etree.tostring(tree)

rawDescription: Removing Processing Instructions with Python lxml


code:
source
import multiprocessing, Queue

def myproc(arg):
    return arg*2

def worker(inqueue):
    for num in iter(inqueue.get, None):
        print myproc(num)

inq = multiprocessing.Queue()
# prefill with 3 jobs
for num in range(3):
    inq.put(num)
# signal end of jobs
inq.put(None)

worker_p = multiprocessing.Process(
    target=worker, args=(inq,),
)
worker_p.start()

worker_p.join()

rawDescription: Control a separate process in python using multiprocessing module


code:
for a, b in itertools.izip_longest(L[0], L[1]):
    print a, b

rawDescription: Loop tuples inside a list

description: Loop tuples inside a list


code:
import random
import numpy

list_1 = sorted(random.sample(xrange(1, 101), 10))
list_2 = sorted(random.sample(xrange(1, 101), 10))

A = numpy.array(list_1).reshape(1,10)
B = numpy.array(list_2).reshape(10,1)

# Inner product
print A.dot(B)

# Outer product
print A.T.dot(B.T)

rawDescription: Python Doing Math with Lists, Converting Lists to Matrices


code:
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt

class Window(QtGui.QWidget):
    def __init__(self, rows, columns):
        QtGui.QWidget.__init__(self)
        self.table = QtGui.QTableWidget(rows, columns, self)
        for row in range(rows):
            qwidget = QtGui.QWidget()
            checkbox = QtGui.QCheckBox()
            checkbox.setCheckState(QtCore.Qt.Unchecked)
            qhboxlayout = QtGui.QHBoxLayout(qwidget)
            qhboxlayout.addWidget(checkbox)
            qhboxlayout.setAlignment(Qt.AlignCenter)
            qhboxlayout.setContentsMargins(0, 0, 0, 0)
            self.table.setCellWidget(row, 0, qwidget)
            self.table.setItem(row, 1, QtGui.QTableWidgetItem(str(row)))
        layout = QtGui.QVBoxLayout(self)
        self.button = QtGui.QPushButton()
        self.button.setObjectName("loadButton")
        layout.addWidget(self.table)
        layout.addWidget(self.button)
        self.button.clicked.connect(self.ButtonClicked)

    def ButtonClicked(self):
        checked_list = []
        for i in range(self.table.rowCount()):
            if self.table.cellWidget(i, 0).findChild(type(QtGui.QCheckBox())).isChecked():
                checked_list.append(self.table.item(i, 1).text())
        print checked_list

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window(3, 2)
    window.resize(350, 300)
    window.show()
    sys.exit(app.exec_())

rawDescription: PyQt allign checkbox and put it in every row


code:
data="""lname    fname   rno_cd    eri_cd
0    CRUISE   TOM     E         1
1    DEPP     JOHNNY  Y         0
2    DICAPR   LENARDO Nan       1
3    PITT     BRAD    Nan       1
4    MOST     JEFF    A         0
5    HANKS    TOM     Nan       1
6    BRANDO   MARLON  C         1
7    WILLIAMS ROBIN   F         1
8    DOWNEY   ROBERT  B         1
9    PACINO   AL      E         1"""

import pandas as pd
from collections import Counter
from io import StringIO

df= pd.read_csv(StringIO(data.decode('UTF-8')),delim_whitespace=True )

D={"A":"AI/AK Native","B":"Asian","C":"Black/AA","D":"Hispanic","E":"White","F":"Asian","G":"Asian","H":"Haw/Pac Isl","Y":"White"}

def transform(x):
    if x['rno_cd']=="Nan":
        return "Unknown"
    else:
        return D[x['rno_cd']]

df["rno_defined"]= df.apply(lambda x: transform(x) ,axis=1)

print df

rawDescription: How can I create a new column and assign values to it based off of critieria?

description: Create a new column and assign values to it based off of critieria


code:
from bs4 import BeautifulSoup

data = """

anchor1text1

anchor2important text to extract

anchor3text3
...

"""

soup = BeautifulSoup(data)
print soup.find('a', title='title2').next_sibling

rawDescription: Extracting text following a specific a-tag


code:
import json
import random
import re

from bs4 import BeautifulSoup
import requests
from slimit import ast
from slimit.parser import Parser
from slimit.visitors import nodevisitor

# start a session
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36'}
session = requests.Session()
response = session.get('http://uslpro.uslsoccer.com/schedules/index_E.html', headers=headers)

# get the dynamic part of the JS url
soup = BeautifulSoup(response.content)
script = soup.find('script', text=lambda x: x and 'var DIISchedule' in x)
tg = re.search(r"tg: '(\d+)',", script.text).group(1)

# request to JS url
js_url = "http://uslpro.uslsoccer.com/schedules/2014/{tg}.js?{rand}".format(tg=tg, rand=random.randint(1000, 9999))
response = session.get(js_url, headers=headers)

# parse js
parser = Parser()
tree = parser.parse(response.content)
matches, teams = [json.loads(node.right.to_ecma())
                  for node in nodevisitor.visit(tree)
                  if isinstance(node, ast.Assign) and isinstance(node.left, ast.DotAccessor)]

for match in matches.itervalues():
    print teams[match['tm1']]['name'], '%s : %s' % (match['sc1'], match['sc2']), teams[match['tm2']]['name']

rawDescription: Any way to get JS object using scrapy

description: Any way to get JS object using scrapy


code:
from time import time
lst = [0,1,0,1,1,0,1,1,1,0,1,1,1,1,0]

s = time()
for i in xrange(5000000):
    a = lst[::-1]          # creates a reversed list
print time() -s

s = time()
for i in xrange(5000000):
    b = lst[:]             # copies the list
    b.reverse()            # reverses it
print time() -s

rawDescription: reverse() does not work on a Python literal?

description: Reverse() does not work on a Python literal


code:
for c in cross_refs:
    if c.xpath('boolean(following-sibling::node()[1][self::cross-ref])'):
        print c.text

rawDescription: Detect whether is the immediate sibling of an element is text or another element in lxml


code:
import Tkinter as tk

class PasswordDialog(tk.Toplevel):
    def __init__(self, parent):
        tk.Toplevel.__init__(self)
        self.parent = parent
        self.entry = tk.Entry(self, show='*')
        self.entry.bind("<KeyRelease-Return>", self.StorePassEvent)
        self.entry.pack()
        self.button = tk.Button(self)
        self.button["text"] = "Submit"
        self.button["command"] = self.StorePass
        self.button.pack()

    def StorePassEvent(self, event):
        self.StorePass()

    def StorePass(self):
        self.parent.password = self.entry.get()
        self.destroy()
        print '1: Password was', self.parent.password

class MainApplication(tk.Frame):
    def __init__(self, root):
        tk.Frame.__init__(self, root)
        self.password = None
        self.button = tk.Button(self)
        self.button["text"] = "Password"
        self.button["command"] = self.GetPassword
        self.button.pack()

    def GetPassword(self):
        self.wait_window(PasswordDialog(self))
        print '2: Password was', self.password

if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

rawDescription: Implementing a Password Dialog with Tkinter


code:
for partition in sum_to_n(6, 3):
    print partition

[2, 2, 2]
[3, 2, 1]
[4, 1, 1]

rawDescription: Get all numbers that add up to a number

description: Get all numbers that add up to a number


code:
import os.path

def os_path_split_asunder(path, debug=False):
    parts = []
    while True:
        newpath, tail = os.path.split(path)
        if debug: print repr(path), (newpath, tail)
        if newpath == path:
            assert not tail
            if path: parts.append(path)
            break
        parts.append(tail)
        path = newpath
    parts.reverse()
    return parts

def spacedman_parts(path):
    components = []
    while True:
        (path,tail) = os.path.split(path)
        if tail == "":
            components.reverse()
            return components
        components.append(tail)

if __name__ == "__main__":
    tests = [
        '',
        'foo',
        'foo/',
        'foo\\',
        '/foo',
        '\\foo',
        'foo/bar',
        '/',
        'c:',
        'c:/',
        'c:foo',
        'c:/foo',
        'c:/users/john/foo.txt',
        '/users/john/foo.txt',
        'foo/bar/baz/loop',
        'foo/bar/baz/',
        '//hostname/foo/bar.txt',
        ]
    for i, test in enumerate(tests):
        print "\nTest %d: %r" % (i, test)
        drive, path = os.path.splitdrive(test)
        print 'drive, path', repr(drive), repr(path)
        a = os_path_split_asunder(path)
        b = spacedman_parts(path)
        print "a ... %r" % a
        print "b ... %r" % b
        print a == b

rawDescription: cross-platform splitting of path in python

description: Cross-platform splitting of path in python


code:
import collections,itertools
compare_func = lambda x, y: collections.Counter(x) != collections.Counter(y)
lst = [[1, 2, 3],[1, 2, 3,4]]

def adder(mylist,indx,val):
    mylist[indx].append(val)
    if  all([compare_func(*i) for i in list(itertools.combinations(lst,2))]):
        print "Added item"
    else:
        print "Did not add item"
        mylist[indx].pop()
    return mylist

rawDescription: how to predict unique list in lists?

description: Predict unique list in lists


code:
for tag in soup.body or soup:  # prefer the body tag if exists
    print tag

rawDescription: Beautifulsoup showing duplicates

description: Beautifulsoup showing duplicates


code:
l = ['Kaiserstra\xc3\x9fe']
l
l[0]
print l[0]

rawDescription: How to convert a unicode list of tuples into utf-8 with python

description: Convert a unicode list of tuples into utf-8 with python


code:
L = ['San Francisco', 'Christchurch ', 'Sydney ', 'Bangkok ', 'Copenhagen ']

max_length = len(max(L, key = lambda x : len(x)))

new_L = []

for e in L:
    new_L.append(e + ' ' * (max_length - len(e)))

for e in zip(*new_L):
    for el in e:
        if el != ' ':
            print el,

rawDescription: I have been trying to write a program python


code:
# ==================== AttributeAccess.py ====================
import maya.cmds as cmds
import maya.mel as mel
import sys
import maya.OpenMaya as OM        # Version 1
import math

import inspect
import types

# ---------- Common Stuff ----------

# "something" can be any Python object.
def Exists(something):
    return something is not None

def printElements(ob):
    print '----- Elements: -----'
    i = 0
    for x in ob:
        print ' [' + str(i) + ']:  ' + repr(x)
        i += 1
    print '---------------------'

def printDictElements(ob):
    print ''
    print '-----------------------'
    for x in ob: print repr(x) + ':  ' + repr(ob[x])
    print '-----------------------'

# ---------- inspect Attributes ----------

# NOTE: ob is an instance, NOT a type object.
def TypeName(ob):
    return ob.__class__ .__name__

# Excludes 'internal' names (start with '__').
def Public(name):
    return not name.startswith('__')

# member is element of inspect.getmembers:
#   a two-element tuple.
def MemberWithType(member):
    return ( member[0], TypeName(member[1]), member[1] )
#print MemberWithType( (1.1, 2) )

def Members(ob):
    return inspect.getmembers(ob)

# True for Maya Python's 'this' member.
# member [1] is attribute value.
def SwigThis(member):
    return (member[0] == 'this') and (TypeName(member[1]) == 'SwigPyObject')

# HACK: "not SwigThis": omit Maya Python's 'this' member.
def PublicMembers(ob):
    members = filter(lambda member: Public(member[0]) and not SwigThis(member), Members(ob))
    return map(MemberWithType, members)

# Excludes 'internal' names (start with '__').
def Dir(ob):
    return filter(Public, dir(ob))

def _Type_And_Features(ob, names):
    return '{0}.({1})'.format(TypeName(ob), ', '.join(names))

def MemberName(member):
    return member[0]

# member with typename inserted as [1]. So descriptor is [2].
# member type-name is [1].
def CallableMember(member):
    #return (member[2].__class__  is types.MethodType)
    return inspect.isroutine(member[2])

def MemberNames(members):
    return map(MemberName, members)

def Features(ob):
    return _Type_And_Features(ob, MemberNames(PublicMembers(ob)) )
    #return _Type_And_Features(ob, Dir(ob))

def Callable(ob):
    return _Type_And_Features(ob, MemberNames(filter(lambda a: CallableMember(a), PublicMembers(ob))))
    #return _Type_And_Features(ob, filter(lambda a: callable(a), Dir(ob)))

def IsClassVar(self, attrName):
    return hasattr(self.__class__, attrName)

# REQUIRE attrName already known to be supported by self.
# But just in case, return False if exception, so will be skipped.
def IsNotSameAsClassVar(self, attrName):
    try:
        if not IsClassVar(self, attrName):
            return True
        # If it has different value than class' attribute, it is on the instance.
        return getattr(self, attrName) is not getattr(self.__class__, attrName)
    except:
        return False

# ---------- _MayaValues ----------

# NOTE: 'ob' is an instance, not the class (type) itself.
def _ClassVars(ob):
    attributes = filter(lambda a: not CallableMember(a), PublicMembers(ob))
    # Keep class variables.
    # "not IsProperty": HACK: Skip Maya/Swig 'property' class variables.
    classVars = filter(lambda desc: IsClassVar(ob, desc[0]) and not IsProperty(getattr(ob.__class__, desc[0])), attributes)
    return MemberNames(classVars)

# NOTE: 'ob' is an instance, not the class (type) itself.
def ClassVars(ob):
    return _Header_And_Values(TypeName(ob) + ' Class_Variables',
        map(lambda attr: attr + ': ' + Repr(getattr(ob, attr)), _ClassVars(ob)),
        0
        )

# If it is invocable without parameters, return (attrName, typename, result of invocation).
# if Not reportExceptions, return None for Exception.
def CallAttribute_AsTriple(self, attrName, reportExceptions=False):
    try:
        expressionString = 'self.{0}()'.format(attrName)
        result = eval(expressionString)
        typename = TypeName(result)
    except Exception as e:
        if reportExceptions:
            result = e
            typename = '*** Exception'
        else:
            return None
    return (attrName, typename, result)

# member is tuple (attrName, typeName, value)
# If it is invocable without parameters, return (attrName, typename, result of invocation).
# if Not reportExceptions, return None for Exception.
def CallMember_AsTriple(self, member, reportExceptions=False):
    return CallAttribute_AsTriple(self, member[0], reportExceptions)

# If it is invocable without parameters, return string: pretty-printed result of invocation.
# if Not reportExceptions, return None for Exception.
def CallAttribute(self, attrName, reportExceptions=False):
    try:
        #printElements(locals())
        expressionString = 'self.{0}()'.format(attrName)
        #print Eval(expressionString, locals())
        result = eval(expressionString)
        resultString = Repr(result)
        typename = TypeName(result)
    except Exception as e:
        if reportExceptions:
            #result = '*** Exception  ' + str(e)
            result = e
            resultString = str(e)
            typename = '*** Exception'
        else:
            return None
    return ' .{0} {{{1}}}= {2}'.format(attrName, typename, resultString)

# member is tuple (attrName, typeName, value)
# If it is invocable without parameters, return string: pretty-printed result of invocation.
# if Not reportExceptions, return None for Exception.
def CallMemberRepr(self, member, reportExceptions=False):
    return CallAttribute(self, member[0], reportExceptions)

def FirstLine(string):
    lines = string.split('\n')
    if len(lines) > 1:
        return lines[0] + '...'
    return string

def ArgLessRoutines_AsTriples(ob):
    members = PublicMembers(ob)
    members_WithNones = map(lambda member: CallMember_AsTriple(ob, member), members)
    # member is tuple (attrName, typeName, value)
    members = filter(Exists, members_WithNones)
    return members

def ArgLessRoutines(ob):
    members = PublicMembers(ob)
    members_WithNones = map(lambda member: CallMember_AsTriple(ob, member), members)
    # member is tuple (attrName, typeName, value)
    members = filter(Exists, members_WithNones)
    resultStrings = map(lambda string: FirstLine(string), resultStrings)
    return _Header_And_Values(TypeName(ob) + ' ArgLessRoutines', resultStrings)

def _MayaCallables_Common(mayaType):
    try:
        typeName = mayaType.__name__
        if typeName == 'MDagPath':
            return ['fullPathName']
        if typeName == 'MTypeId':
            return ['id']
        if typeName == 'MFnMesh':
            return ['numPolygons', 'numVertices', 'numEdges', 'numFaceVertices']
        if typeName == 'MDagPath':
            return ['fullPathName']
    except Exception as e:
        print e
    return []

def _MayaCallables_Version1(mayaType):
    return _MayaCallables_Common(mayaType)

def _MayaCallables_Version2(mayaType):
    return _MayaCallables_Common(mayaType)

# Names of callable attributes to include in Repr of 'ob'.
# For instances of types in 'maya.OpenMaya'.
def MayaCallables(ob):
    try:
        typ = ob.__class__
        if typ == type:
            return []
        if typ.__module__ == 'maya.OpenMaya':
            return _MayaCallables_Version1(typ)
        if typ.__module__ == 'OpenMaya':
            return _MayaCallables_Version2(typ)
    except Exception as e:
        print e
    return []

# Return (name, typename, value) per maya callable.
def _MayaValues(ob):
    callables = MayaCallables(ob)
    members_WithNones = map(lambda attrName: CallAttribute_AsTriple(ob, attrName), callables)
    members = filter(Exists, members_WithNones)
    return members

# TODO: If all results fit on single line, remove "{typename}" so is more readable.
#def MayaValues(ob):
#    resultStrings = _MayaValues(ob)
#    return _Header_And_Values(TypeName(ob) + ' MayaValues', resultStrings)

# ---------- Attributes ----------
def _AttributeNames(ob):
    attributes = filter(lambda a: not CallableMember(a), PublicMembers(ob))
    # Omit class variables.
    attributes = filter(lambda desc: IsNotSameAsClassVar(ob, desc[0]), attributes)
    return MemberNames(attributes)

def AttributeNames(ob):
    return _Type_And_Features(ob, _AttributeNames(ob))
    #return _Type_And_Features(ob, filter(lambda a: not callable(a), Dir(ob)))

def _Header_And_Values(headerString, valueStrings, maxWidth=100):
    if sum(map(len, valueStrings)) > maxWidth:
        # pretty print, with one value per line.
        return '{0}(\n  {1}\n)'.format(headerString, '\n  '.join(valueStrings))
    return '{0}({1})'.format(headerString, ', '.join(valueStrings))

def _Type_And_Values(ob, valueStrings, maxWidth=100):
    return _Header_And_Values(TypeName(ob), valueStrings, maxWidth)

def AttributeValues(ob):
    return _Type_And_Values(ob, map(lambda attr: str(getattr(ob, attr)), _AttributeNames(ob)))

def Attributes(ob, depth=0):
    # Limit recursion.
    # If deep, don't include MayaValues.
    if depth >= 2:
        return _Type_And_Values(ob, map(lambda attr: attr + ': ' + str(getattr(ob, attr)), _AttributeNames(ob)))
    attributes = map(lambda attr: attr + ': ' + Repr(getattr(ob, attr), depth + 1), _AttributeNames(ob))
    if depth == 0:
        mayaValues = _MayaValues(ob)
        if len(mayaValues) > 0:
            for mayaValue in mayaValues:
                attribute = mayaValue[0] + ': ' + Repr(mayaValue[2])
                attributes.append(attribute)
    return _Type_And_Values(ob, attributes)

def IsProperty(ob):
    return (TypeName(ob) == 'property')

# ---------- Repr ----------
def Repr(ob, depth=0):
    r = repr(ob)
    # Helps avoid undesired recursion.
    if ob.__class__ == type:
        return r
    if (r.__class__ == types.StringType) and (len(r) > 0) and (r.find('<') <> 0):
        # Has a good repr.
        return r
    # Doesn't have a good repr; inspect it instead.
    return '#' + Attributes(ob, depth)

def Eval(expressionString, _locals=locals(), _globals=globals()):
    return str(expressionString) + "= " + str(Repr(eval(expressionString, _globals, _locals)))

# ---------- Testing ----------

# ---------- class Vector ----------
class Vector(object):
    def __init__(self, x=0.0, y=0.0, z=0.0):
        self.x, self.y, self.z = x, y, z
    # Provide useful info for 'repr(self)', 'str(self)', and 'print self'.
    def __repr__(self):
        return 'Vector({0}, {1}, {2})'.format(self.x, self.y, self.z)
    # math operators
    def __add__(self, other):
        return Vector(self.x + other.x, self.y + other.y, self.z + other.z)
    # ==
    def __eq__(self, other):
        return (self.__class__ == other.__class__) and \
            (self.x == other.x) and \
            (self.y == other.y) and \
            (self.z == other.z)
    # a simple method
    def ApproximateLength(self):
        return self.x + self.y + self.z
    # list/sequence/iterator support.
    def tolist(self):
        return [self.x, self.y, self.z]
    def __len__(self):
        return 3
        # No need for "next(self)", because we create a list, use its iterator.
    def __iter__(self):
        return iter(self.tolist())
# class variable
Vector.Zero = Vector()

# ---------- inspecting Vector ----------
def Testing_Vector_Attributes():
    #vec = (1, 2, 3)
    #vec = [1, 2, 3]
    #vec = Vector(1.0, 2.0, 3.0)
    vec = OM.MFloatVector(1, 2, 3)
    print vec
    #for x in vec: print x
    print dir(vec)
    print TypeName(vec)
    print Dir(vec)
    print Features(vec)
    print Callable(vec)
    print '-----------------------'
    printElements(PublicMembers(vec))
    print '-----------------------'
    print AttributeNames(vec)
    #print vec.x
    #print eval('vec.x')
    #print getattr(vec, 'x')
    print AttributeValues(vec)
    print Attributes(vec)
    vec = OM.MFloatVector(1, 2, 3)
    #print repr(vec)
    #print Repr('Hi')
    print Repr( (1,2,3) )
    print Repr(vec)
    print ClassVars( Vector(1.0, 2.0, 3.0) )
    print ClassVars( OM.MFloatVector(1, 2, 3) )
    print Eval('OM.MMatrix()')
    print Eval('OM.MMatrix().matrix')

if __name__ == "__main__":
    Testing_Vector_Attributes()

rawDescription: OpenMaya API (python): print more useful info from MObjects? E.g. MFloatVector(x, y, z)


code:
import numpy as np
import pandas as pd

df1 = pd.DataFrame(np.random.rand(3, 2))
df2 = pd.DataFrame(np.random.rand(4, 2))
df3 = pd.DataFrame(np.random.rand(5, 2))

print pd.concat([df1, df2, df3], keys=["A", "B", "C"])

rawDescription: Add multi-index to pandas dataframe and keep current index


code:
from django.core.urlresolvers import reverse

class ExampleView(TemplateView):
    some_var = request.build_absolute_uri(reverse('example2'))
    print some_var

rawDescription: Django reference url in views.py

description: Django reference url in views.py


code:
from itertools import islice, count
for i in islice(count(0), 4000000000):
    print i

rawDescription: For loop with more iterations than sys.maxint


rawDescription: Parsing an XML file using ElementTree

description: Parsing an XML file using ElementTree


code:
for uri in extract_urls('http://foo.bar/baz irc://freenode.org/bash'):
  print uri

rawDescription: Regex to extract all URLs from a page

description: Regex to extract all URLs from a page


code:
with open("mustang.txt", "r") as f:
    count = f.read().count('.')
print count

rawDescription: Count how many full stops a text file contains in Python


code:
def print_map(self):
    for y in range(0, self.height):
        print "".join(["[%s]%s" %
                    ("u" if self.x == x and self.y == y else " ",
                     "-" if ((x, y), (x + 1, y)) in self.paths else " ")
                        for x in range(0, self.width)])
        print " " + "   ".join(["|" if ((x, y), (x, y + 1)) in self.paths
                              else " " for x in range(0, self.width)])

rawDescription: Python Drawing ASCII Map

description: Python Drawing ASCII Map


code:
import struct
bytes = ''.join([chr(int(c, 16)) for c in inputlist])
value = struct.unpack('<I', bytes)[0]
print hex(value)

rawDescription: How to append a list of Hex to one Hex number

description: Append a list of Hex to one Hex number


code:
while True:
    text = raw_input("What is the text?")
    if text == "a":
        print "Correct"
    elif text == "stop":
        print "Bye"
        break
    else:
        print "Not correct text"

rawDescription: Pipe into raw_input() in Python

description: Pipe into raw_input() in Python


code:
import operator

fdists = [(u'seeing', 2), (u'said.', 2), (u'one', 2), (u'death', 2), (u'entertainment',   2), (u'it\u2019s', 2), (u'weiss', 2), (u'read', 2), (u'\u201cit', 1), (u'shot', 1), (u'show\u2019s', 1), (u'people', 1), (u'dead,\u201d', 1), (u'bloody', 1)]
fdists2 = [(u'seeing', 3), (u'said.', 4), (u'one', 2), (u'death', 2), (u'entertainment',   2), (u'it\u2019s', 2), (u'weiss', 2), (u'read', 2)]
fdists += fdists2

fdict = {}
for i in fdists:
    if i[0] in fdict:
        fdict[i[0]] += i[1]
    else:
        fdict[i[0]] = i[1]

sorted_f = sorted(fdict.items(), key=operator.itemgetter(1), reverse=True)
print sorted_f[:30]

[(u'said.', 6), (u'seeing', 5), (u'death', 4), (u'entertainment', 4), (u'read', 4), (u'it\u2019s', 4), (u'weiss', 4), (u'one', 4), (u'\u201cit', 1), (u'shot', 1), (u'show\u2019s', 1), (u'people', 1), (u'dead,\u201d', 1), (u'bloody', 1)]

rawDescription: Getting cumulative counts of word frequencies founds in a documents


code:
moves = {
        'up': lambda (x,y): (x, y+1),
        'dn': lambda (x,y): (x, y-1),
    }

rawDescription: Python dictionaries and select case

description: Python dictionaries and select case


code:
import operator
for choices in itertools.product([0, 1], repeat=len(off)):
    elts = [(on[i] if choice else off[i]) for i, choice in enumerate(choices)]
    print reduce(operator.mul, elts, 1)

rawDescription: Python combinations of 2 lists

description: Python combinations of 2 lists


code:
import re

st = "abc/def/([default], [testing], [something])"
dic = {'abc/def/[default]' : '2.7',
       'abc/def/[testing]' : '2.1',
       'bcd/xed/[something]' : '3.1'}

prefix_regex = "^[\w*/]*"
tag_regex = "\[\w*\]"

prefix = re.findall(prefix_regex, st)[0]
tags = re.findall(tag_regex, st)

for key in dic:
    key_prefix = re.findall(prefix_regex, key)[0]
    key_tag = re.findall(tag_regex, key)[0]
    if prefix == key_prefix:
        for tag in tags:
            if tag == key_tag:
                st = st.replace(tag, dic[key])

print st

rawDescription: How to replace text in curly brackets with another text based on comparisons using Python Regex

description: Replace text in curly brackets with another text based on comparisons using Python Regex


code:
def triangle(n):
    for i in range(1, n +1):
        print ' ' * (n - i) + 'x' * i

rawDescription: How to make a triangle of x's in python?

description: Make a triangle of x's in python


code:
def processlinks(text):
    output = urllib.urlopen('http://google.com/' % text)
    print output.geturl()
    print output.read()

map(processlinks, jsonobj['data'])

rawDescription: Parse a json file and add the strings to a URL


code:
import ast
mystr  = 'myString={"name", "age", "address", "contacts", "Email"}'

tree   = ast.parse(mystr)
name   = tree.body[0].targets[0].id
values = [val.s for val in tree.body[0].value.elts]

print name, values
# prints: myString ['name', 'age', 'address', 'contacts', 'Email']

rawDescription: Python Parse regex

description: Python Parse regex


code:
import sys
from itertools import product

base_globs = {"M":['A','C'], "R":['A','G'], "W":['A','T'],
              "S":['C','G'], "Y":['C','T'], "K":['G','T'],

              "V":['A','C','G'], "H":['A','C','T'],
              "D":['A','G','T'], "B":['C','G','T'],
              }

def base_glob(glob_sequence):
    production_sequence = [base_globs.get(base, [base]) for base in glob_sequence]
    return map("".join, product(*production_sequence))

for line in sys.stdin.readlines():
    productions = base_glob(line.strip())
    print "\n".join(productions)

rawDescription: Looking for elegant glob-like DNA string expansion


code:
mask = np.eye(512)
k = np.ones((25, 25), np.float32)/625
list_mask = zip(np.where(mask==1)[0], np.where(mask==1)[1])
tic = time.time()
conv_image = image_convolve_mask(lena, list_mask, k)
print 'time: ', time.time()-tic # 0.08136 sec

rawDescription: Image convolution at specific points

description: Image convolution at specific points


code:
from pathos.pools import ProcessPool as Pool
pool = Pool()
data_list = [range(4), range(4,8), range(8,12), range(12,16)]
squared = lambda x:x**2
mean = lambda x: sum(x)/len(x)
for data in data_list:
  result = pool.map(squared, data)
  print mean(result)

rawDescription: How to reuse a process pool for parallel programming in Python 3

description: Reuse a process pool for parallel programming in Python 3


code:
def calledfunction():
  default_local = 'some default'
  var = globalvar if 'globalvar' in globals() else default_local
  print var

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

print "calling function before the variable is defined"
print
calledfunction()

globalvar = "created outside the function"

print "calling function after the variable is defined"
print
calledfunction()

rawDescription: Python: checking for the existence of a variable in globals() makes it invisible in the local context


code:
def solve(numLegs, numHeads):
     for numBees in range(0, numHeads + 1):
             for numChicks in range(0, numHeads - numBees + 1):
                     numPigs = numHeads - numChicks - numBees
                     totLegs = 4*numPigs + 2*numChicks + 6*numBees
                     if totLegs == numLegs:
                             yield [numPigs, numChicks, numBees]

def barnYard(heads, legs):
    for pigs, chickens, bees in solve(legs, heads):
             print 'Number of pigs: ', pigs
             print 'Number of chickens: ', chickens
             print 'Number of bees: ', bees

barnYard(20,56)

rawDescription: Unexpected result in a simple example

description: Unexpected result in a simple example


code:
import StringIO

f=StringIO.StringIO()

for line in ['line {}\n'.format(i) for i in range(25)]:
    f.write(line)

f.seek(0)

for line in f:
    print line.strip()

rawDescription: Encrypt/Decrypt an animated gif in Python without storing the final copy to HD


code:
from ciscoconfparse import CiscoConfParse

input_file = 'some_site.conf'
parse = CiscoConfParse(input_file)

## Build a list of vty ACLs here (and flag if a vty doesn't have one)
vty_acl_names = set([])
for vtyobj in parse.find_objects(r'^line\svty'):

    vty_acl_name = vtyobj.re_match_iter_typed('access-class\s+(\S+)\s+in',
        result_type=str, default="")

    if not vty_acl_name:
        print "FAIL: '{0}' doesn't have an ACL".format(vtyobj.text)
    else:
        vty_acl_names.add(vty_acl_name)

## Check ACL log compliance here (and ensure the ACL is even in the config)
for vty_acl_name in vty_acl_names:
    try:
        aclobj = parse.find_objects(r'ip\s+access-list\s+extended\s+{0}'.format(vty_acl_name))[0]
    except IndexError:
        print "FAIL: ACL {0} is applied to a vty, but it's missing from the config!".format(vty_acl_name)
    ## NOTE: this only works if you are using extended ACLs on the VTY
    for ace in aclobj.children:

        if 'remark' in ace.text:
            continue
        if 'log' in ace.text:
            print "NOT A FINDING - ACL {0}, ACE: {1}".format(vty_acl_name, ace.text)
        else:
            print "OPEN (no log) - ACL {0}, ACE: {1}".format(vty_acl_name, ace.text)

rawDescription: using CiscoConfParse to find ACL applied to VTY and then check ACL for log statement

description: Using CiscoConfParse to find ACL applied to VTY and then check ACL for log statement


code:
import numpy as np

signal = [
    [0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1], #time values
    [1,1,1,2,3,4,4,4,4,2,1,1] #function values
    ]

def npcompress(signal):
    sig=np.array(signal)
    idx = np.where(sig[1][1:] != sig[1][:-1])[0]
    idx_arr = np.sort(np.array(list(set(idx) | set(idx + 1) | set([0]) | set([len(sig[1]) - 1]))))
    return sig.T[idx_arr]

print npcompress(signal).T

rawDescription: Signal Compressing

description: Signal Compressing


code:
from lxml import etree

tree = etree.parse('data.xml')

paragraphs = tree.xpath('/abstract/para/text()')
keywords = tree.xpath('/keywordSet/keyword/text()')

print paragraphs
print keywords

rawDescription: Handling nested elements with Python lxml


code:
class Base(object):
    def method(self):
        return "I'm a base object!"

class Derived(Base):
    def method(self):
        return "I'm a derived object!"

obj = Derived()
print obj.method()

rawDescription: How to extend class method

description: Extend class method


code:
print df['item_list'].apply(pd.Series).stack().tolist()
['beer', 'apple', 'pear', 'rice', 'egg', 'banana', 'oil',
 'pear', 'beer', 'noodle', 'tomato', 'milk', 'apple']

rawDescription: Python Pandas dataframe: Collect values of a column


code: print player.stats()

rawDescription: Why does this script print an extraneous 'none' in the output


code:
import dns.resolver

for txtrecord in dns.resolver.query('google.com', 'TXT').answer.response:
   print txtrecord.to_text()

rawDescription: How can I check the value of a DNS TXT record for a host?

description: Check the value of a DNS TXT record for a host


code:
import win32gui, win32api, win32con, ctypes

class Mouse:
    """It simulates the mouse"""
    MOUSEEVENTF_MOVE = 0x0001 # mouse move
    MOUSEEVENTF_LEFTDOWN = 0x0002 # left button down
    MOUSEEVENTF_LEFTUP = 0x0004 # left button up
    MOUSEEVENTF_RIGHTDOWN = 0x0008 # right button down
    MOUSEEVENTF_RIGHTUP = 0x0010 # right button up
    MOUSEEVENTF_MIDDLEDOWN = 0x0020 # middle button down
    MOUSEEVENTF_MIDDLEUP = 0x0040 # middle button up
    MOUSEEVENTF_WHEEL = 0x0800 # wheel button rolled
    MOUSEEVENTF_ABSOLUTE = 0x8000 # absolute move
    SM_CXSCREEN = 0
    SM_CYSCREEN = 1

    def _do_event(self, flags, x_pos, y_pos, data, extra_info):
        """generate a mouse event"""
        x_calc = 65536L * x_pos / ctypes.windll.user32.GetSystemMetrics(self.SM_CXSCREEN) + 1
        y_calc = 65536L * y_pos / ctypes.windll.user32.GetSystemMetrics(self.SM_CYSCREEN) + 1
        return ctypes.windll.user32.mouse_event(flags, x_calc, y_calc, data, extra_info)

    def _get_button_value(self, button_name, button_up=False):
        """convert the name of the button into the corresponding value"""
        buttons = 0
        if button_name.find("right") >= 0:
            buttons = self.MOUSEEVENTF_RIGHTDOWN
        if button_name.find("left") >= 0:
            buttons = buttons + self.MOUSEEVENTF_LEFTDOWN
        if button_name.find("middle") >= 0:
            buttons = buttons + self.MOUSEEVENTF_MIDDLEDOWN
        if button_up:
            buttons = buttons << 1
        return buttons

    def move_mouse(self, pos):
        """move the mouse to the specified coordinates"""
        (x, y) = pos
        old_pos = self.get_position()
        x =  x if (x != -1) else old_pos[0]
        y =  y if (y != -1) else old_pos[1]
        self._do_event(self.MOUSEEVENTF_MOVE + self.MOUSEEVENTF_ABSOLUTE, x, y, 0, 0)

    def press_button(self, pos=(-1, -1), button_name="left", button_up=False):
        """push a button of the mouse"""
        self.move_mouse(pos)
        self._do_event(self.get_button_value(button_name, button_up), 0, 0, 0, 0)

    def click(self, pos=(-1, -1), button_name= "left"):
        """Click at the specified placed"""
        self.move_mouse(pos)
        self._do_event(self._get_button_value(button_name, False)+self._get_button_value(button_name, True), 0, 0, 0, 0)

    def double_click (self, pos=(-1, -1), button_name="left"):
        """Double click at the specifed placed"""
        for i in xrange(2):
            self.click(pos, button_name)

    def get_position(self):
        """get mouse position"""
        return win32api.GetCursorPos()

rawDescription: ctypes mouse_events

description: Ctypes mouse_events


code:
from datetime import tzinfo, timedelta, datetime

class FixedOffset(tzinfo):
    def __init__(self, offset):
        self.__offset = timedelta(hours=offset)
        self.__dst = timedelta(hours=offset-1)
        self.__name = ''

    def utcoffset(self, dt):
        return self.__offset

    def tzname(self, dt):
        return self.__name

    def dst(self, dt):
        return self.__dst

print datetime.now()
print datetime.now(FixedOffset(9))

rawDescription: Python: Convert local time to another time zone


code:
import numbers
from ast import literal_eval

def number_type(x):
    try:
        n = literal_eval(x)
        if isinstance(n, numbers.Number):
            print type(n).__name__
        else:
            print 'not a number'
    except (ValueError, SyntaxError):
        print 'not a number'

rawDescription: Determing the type of number stored as a string


code:
for key, value in item.iteritems():
    print key, value

rawDescription: How to convert an associative array in python?

description: Convert an associative array in python


code:
result = {}
for key, value in my_dict.iteritems():
    result.setdefault(key[:key.rindex("_")], []).append(value)
print result

rawDescription: Modification of a Dictionary in PYTHON

description: Modification of a Dictionary in PYTHON


code:
import os
rootdir = '/tmp/so'
# print full path for first file in rootdir and for each subdir
for topdir, dirs, files in os.walk(rootdir):
    firstfile = sorted(files)[0]
    print os.path.join(topdir, firstfile)

rawDescription: Python: how to obtain only first filename in the directory


code:
from BeautifulSoup import BeautifulSoup as bs

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

soup = bs(your_string)
res = soup.findAll('test')
for i in res:
    print i.get('name4')

rawDescription: Extract value from tag python

description: Extract value from tag python


code:
import io
import sys

sys.stdout = io.StringIO()   # or BytesIO

exec the_command

output = sys.stdout.getvalue()
sys.stdout = sys.__stdout__

rawDescription: How to switch pid between python script and subprocess?

description: Switch pid between python script and subprocess


code:
l = [None, int, 0, 'abc', [1, 2, 3], ('a', 'b')]
for i in itertools.product(*[i if isinstance(i, (list, tuple)) else [i] for i in l]): print list(i)

rawDescription: Recursive "all paths" through a list of lists - Python


code:
graph = tree.find(graphml.get("graph"))
nodes = graph.findall(graphml.get("node"))
edges = graph.findall(graphml.get("edge"))

for node in nodes + edges:
    attribs = {}
    for data in node.findall(graphml.get('data')):
        attribs[data.get('key')] = data.text
    print 'Node', node, 'have', attribs

rawDescription: How to iterate over GraphML file with lxml

description: Iterate over GraphML file with lxml


code:
from Tkinter import *
import tkMessageBox
import threading
import time
root = Tk()
root.geometry("450x250+300+300")
root.title("Raspberry PI Test")
print dir(root)
count = 0
def start_counter():
    global count
    count = 500
    root.after(1,update_counter)
def update_counter():
    global count
    count -= 1
    if count < 0:
        count_complete()
    else:
        root.after(1,update_counter)

def count_complete():
    print "DONE COUNTING!! ... I am now back in the main thread"
def mymessage():
    tkMessageBox.showinfo(title="Alert", message="Hello World!")

buttonLoop = Button(root, text="Start Loop", command=myloop)
buttonLoop.place(x=5, y=15)

buttonMessage = Button(root, text="Start Loop", command=mymessage)
buttonMessage.place(x=85, y=15)

root.mainloop()

rawDescription: Python - While Loop Locks Application

description: Python - While Loop Locks Application


code:
import xml.dom.minidom

data = """
your xml goes here
"""

dom = xml.dom.minidom.parseString(data)
for image in dom.getElementsByTagName('image'):
    comment = image.getElementsByTagName('comment')[0]
    comment_name_text = comment.getElementsByTagName('name')[0].firstChild.nodeValue
    if comment_name_text == 'Patient 1 Brain Tumer':
        location =  image.getElementsByTagName('location')[0]
        print location.firstChild.nodeValue

rawDescription: XML Tree parsing with condition in Python


code:
def wrapper(f):
    def wrapped(*args, **kwargs):
        print kwargs["uname"]
        return mainfunc(*args, **kwargs)
    return wrapped

@wrapper
def foo(uname="Test"):
    return uname + "bar"

# the @decorator is equivalent to `foo = wrapper(foo)`

rawDescription: Using variable length argument lists and named parameters together


code:
import timeit
import types

# http://stackoverflow.com/questions/6527633/how-can-i-make-a-deepcopy-of-a-function-in-python
def copy_func(f, name=None):
    return types.FunctionType(f.func_code, f.func_globals, name or f.func_name,
        f.func_defaults, f.func_closure)

def f(a, b, c):
    return a + b + c

i = copy_func(f, 'i')
i.func_defaults = (4, 5, 3)

print timeit.timeit('f(4,5,3)', setup = 'from __main__ import f', number=100000)
print timeit.timeit('i()', setup = 'from __main__ import i', number=100000)

rawDescription: Python functools partial efficiency

description: Python functools partial efficiency


code:
list = node.getElementsByTagName('Item')
items = {}
for item in list:
  output_id = item.getAttribute('id')
  items[output_id] = item # Put items into dict to use them later.
for id in items:
  print "Item[%d] -> %s" % (id, items[id]) # Only single item per id left.

rawDescription: Python minidom parser: Ignore tag with identical attribute values


code:
class A:

    def __init__(self):
        A.state = 'CHAT'

    def method1(self):
        A.state = 'SEND'

    def printer(self):
        print A.state

class B(A):
    def method2(self):
        self.method1()
        print B.state

ob_B = B()
ob_A = A()
ob_B.method2()
ob_A.printer()

rawDescription: How to access a class variable from another class in python?

description: Access a class variable from another class in python


code:
dt = dt.strftime('%Y%m%d%H%M%S')
print list(dt)

rawDescription: Convert time-string into individual hex bytes


code:
import numpy as np
import scipy as sp
import pylab as pl
import math

x = np.array([ 2, 4, 8, 10, 12, 14, 16])
y = np.array([ 5, 10, 15, 20, 25, 30, 35])
angles = np.array([45,275,190,100,280,18,45])

def draw_line(x,y,angle,length):
  cartesianAngleRadians = (450-angle)*math.pi/180.0
  terminus_x = x + length * math.cos(cartesianAngleRadians)
  terminus_y = y + length * math.sin(cartesianAngleRadians)
  pl.plot([x, terminus_x],[y,terminus_y])
  print [x, terminus_x],[y,terminus_y]

pl.axis('equal')
pl.axis([-5,20,-5,40])
for i in range(0,len(x)):
  print x[i],y[i],angles[i]
  draw_line(x[i],y[i],angles[i],1)

pl.show()

rawDescription: Drawing Arrow in (x,y) coordinate in Python


code:
n = 3
def f(x,y):
    if(x >= n or y >= n):
        return 0
    return max(f(x+1,y),f(x+1,y+1)) + a[x][y] #take the maximum of both the paths

a = [[0 for i in range(n)] for i in range(n)]
a[0][0] = 1
a[1][0] = 2
a[1][1] = 3
a[2][0] = 4
a[2][1] = 5
a[2][2] = 6
maximum = 0
for i in range(0,n):
    maximum = max(maximum,f(0,i)) #we can start from any of the cell in the first row
print maximum

rawDescription: Biggest sum in tree

description: Biggest sum in tree


code:
from __future__ import print_function, unicode_literals

from twisted.internet import defer

class Foo(object):

    def __init__(self):
        self.dfd = defer.Deferred()

    def async(self):
        justForThisCall = defer.Deferred()
        def callbackForDFD(result):
            justForThisCall.callback(result)
            return result
        self.dfd.addCallback(callbackForDFD)
        return justForThisCall

    @defer.inlineCallbacks
    def func(self):
        print('Started!')
        result = yield self.async()
        print('Stopped with result: {0}'.format(result))

if __name__ == '__main__':
    foo = Foo()
    print("calling func")
    foo.func()
    print("firing dfd")
    foo.dfd.callback('no need to wait!')
    print("calling func again")
    foo.func()
    print("done")

rawDescription: Re-using deferred objects in Twisted

description: Re-using deferred objects in Twisted


code:
import sys
class A():
    def func(self):
        func_name = sys._getframe().f_code.co_name
        class_name = self.__class__.__name__
        print 'Executing {} from {}'.format(func_name, class_name)

A().func()

rawDescription: getting object name and function name

description: Getting object name and function name


code:
# helper function to parse a data block
def parse_SIF(lines):
    SIF = []
    while lines:
        line = lines.pop(0).lstrip()
        if line == '' or line.startswith('#'):
            continue
        if line.startswith('**#'):
            lines.insert(0, line)
            break
        data = line.split(',')
        # pick only columns 0, 4, 5 and
        # convert to appropiate numeric format
        # and append to list for current SIF and step
        SIF.append([int(data[0]), float(data[4]), float(data[5])])
    return SIF

# your global data structure - nested lists
raw = []
mls = []

# read whole file into one list - ok if your data is not large
with open('data') as fptr:
    lines = fptr.readlines()

# global parse routine - call helper function to parse data blocks
while lines:
    line = lines.pop(0)
    if line.startswith('**#'):
        if line.find('Raw SIFs at Crack Propagation Step:') > -1:
            raw.append(parse_SIF(lines))
        if line.find('MLS SIFs at Crack Propagation Step:') > -1:
            mls.append(parse_SIF(lines))

# show results for your example data
from pprint import pprint
for raw_step, mls_step in zip(raw, mls):
    print 'raw:'
    pprint(raw_step)
    print 'mls:'
    pprint(mls_step)

rawDescription: How to extract specific lines of data from a big text file

description: Extract specific lines of data from a big text file


code:
#!/usr/bin/python
import turtle as t

def draw(lst, width):
  for x in lst:
    height = x[0]
    t.color(x[1])
    t.forward(width)
    t.left(90)
    t.forward(height)
    t.left(90)
    t.forward(width)
    t.left(90)
    t.forward(height)
    t.left(90)
    t.penup()
    t.forward(30)
    t.pendown()

def bubble_sort(orig_lst):
  lst = [ (x, "blue") for x in orig_lst ]
  n = len(lst)-1
  t.pensize(3)
  draw(lst, width)
  swapped = True
  while swapped:
    swapped = False
    for i in range(n):
        if lst[i+1][0] < lst[i][0]:
            lst[i], lst[i+1] = lst[i+1], lst[i]
            lst[i] = (lst[i][0], "green")
            swapped = True
            next = raw_input("hit any key to continue ")
            t.home()
            t.clear()
            draw(lst,width)
  newLst = [ x[0] for x in lst ]
  return newLst

# TOP LEVEL
original_lst = input("Enter list to be sorted: ")
width = input("Provide the width: ")

newLst = bubble_sort(original_lst)
print "Done! Sorted list is: ", newLst
next = raw_input("hit any key to exit ")

rawDescription: Python Bubble sort

description: Python Bubble sort


code: print list(foo.c)

rawDescription: Using existing table in sqlalchemy

description: Using existing table in sqlalchemy


code:
seq = ListViwthAView(range(100))
view = seq.view[10:20][5:7]
view[0] = 1337
print seq[15]    # 1337

rawDescription: Does Python have a shorthand notation for passing part of a list without copy?


code:
# changes fill character to dash, and fills template with empty strings
print template.replace(':', ':-').format('', '', '')

rawDescription: Python tabulate, appending element to current table


code:
import time
import struct

f = open(r'\\.\pipe\NPtest', 'r+b', 0)
i = 1

while True:
    s = 'Message[{0}]'.format(i)
    i += 1

    f.write(struct.pack('I', len(s)) + s)   # Write str length and str
    f.seek(0)                               # EDIT: This is also necessary
    print 'Wrote:', s

    n = struct.unpack('I', f.read(4))[0]    # Read str length
    s = f.read(n)                           # Read str
    f.seek(0)                               # Important!!!
    print 'Read:', s

    time.sleep(2)

rawDescription: Can Python read from a Windows Powershell namedpipe?


code:
times = collections.Counter([j.time for j in allrecords])
print times
amounts =  collections.Counter([j.amount for j in allrecords])
print amounts

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


code:
import re
f = open('xmlfile', 'r')
data = f.read()
f.close()

close = 'gdai55ff2a2244343a0b6b30dbb745rtdf'

reg = re.compile(r'<prime>(.*?)</prime>')
list = re.split(reg, data)
list[1] = '<prime>'+ close + '</prime>'
result = ''.join(list)
print result

rawDescription: Update a specific tag in xml using python 2.7


code:
import os
import json
import subprocess

hello1 = "Hello World 1"
hello2 = "Hello World 2"

jsonData = '{"pp": [0,3,5,7,9], "sp": [1,2,4,6,8]}'
jj = json.loads(jsonData)

print jj['pp']
print jj['sp']

os.putenv( 'jj', ' '.join( str(v) for v in jj['pp']  ) )

print "start"
subprocess.call(['./test.sh', hello1, hello2 ])
print "end"

rawDescription: How do I pass Python arrays to shell script as an Array?

description: Pass Python arrays to shell script as an Array


code:
import random
n = random.randint(1, 10)
g = int(raw_input("Enter a whole number between 1 and 10: "))

while g != n:

    if g > n:
        g = int(raw_input("Your number was too high, pick a lower one: "))

    elif g < n:
        g = int(raw_input("Your number was too low, pick a higher one: "))

else:
    print "You win."
    print "Your number was ", g, " and my number was ", n

rawDescription: Hotter/Colder Number Game in Python

description: Hotter/Colder Number Game in Python


rawDescription: How do i create a permutation on Python without using the itertools module nor recursive programming?

description: Create a permutation on Python without using the itertools module nor recursive programming


code:
import lxml.etree as ET

ban_word = 'REPLACE_ME'
replacement = 'HELLO'

data = """<root>
    <some_xml_tag>REPLACE_ME</some_xml_tag>
    <some_xml_tag attr="REPLACE_ME">some text</some_xml_tag>
    <REPLACE_ME>some text</REPLACE_ME>
</root>
"""

root = ET.fromstring(data)

for item in root.xpath('//*[. = "%s"]' % ban_word):
    item.text = item.text.replace(ban_word, replacement)

print ET.tostring(root)

rawDescription: Replace words in XML file

description: Replace words in XML file


code: print sorted( ( (k,sum(v)/len(v)) for k,v in input ), key=lambda t: t[1] )

rawDescription: Need to take average of long, strangely formatted set of data in Python


code:
for x in target.xpath('//target'):
    print x.text

rawDescription: Quickest/Best way to traverse XML with lxml in Python


code:
# Create a new country:
us = Country(name='USA')
us.put()

# Create a new state
ca = State(name='California', country=us)
ca.put()

# Create a new city
la = City(name='Los Angeles', state=ca)
la.put()

# And another
sf = City(name='San Francisco', state=ca)
sf.put()

# Print states
for state in us.states:
    print state.name

    # Print cities
    for city in state.cities:
        print ' ' + city.name

rawDescription: Google app engine - structuring model layout with regards to parents?


code:
import sys

def read_data(source):
    N = int(next(source))
    for case in range(N):
        num_rows = int(next(source))
        rows = [next(source).split() for i in range(num_rows)]
        yield rows

for case in read_data(sys.stdin):
    print case

rawDescription: Multiple Testcases Stdin (Python)

description: Multiple Testcases Stdin (Python)


code:
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--group', nargs=4, action='append')

print parser.parse_args()

rawDescription: grouping an unknown number of arguments with argparse

description: Grouping an unknown number of arguments with argparse


code: print d[whatever_char]  #prints the cipher associated with that character

rawDescription: Python Lists Comparison & Print

description: Python Lists Comparison & Print


code:
class CauseCode(Base):
    __tablename__ = "cause_code"

class NDTicket(Base):
    __tablename__ = "ndticket"
    cause_code = relationship("CauseCode", backref = "ndticket")

q = session.query(NDTicket)
for r in q.limit(10):
    print r.ticket_id, r.cause_code.cause_code

rawDescription: Can SQLAlchemy automatically create relationships from a database schema?


code:
#!/usr/bin/python

import subprocess
from time import gmtime, strftime

p = subprocess.Popen(["ping", "-c", "3", "-i", "2", "127.0.0.1"],
                 stdout=subprocess.PIPE)

for ln in p.stdout:
    print strftime("%H:%M:%S", gmtime()) + " received " + ln

# Now I start the same process again, reading the input the other way.

p = subprocess.Popen(["ping", "-c", "3", "-i", "2", "127.0.0.1"],
                 stdout=subprocess.PIPE)

while True:
    ln = p.stdout.readline()
    if '' == ln:
            break
    print strftime("%H:%M:%S", gmtime()) + " received " + ln

rawDescription: Capture output from C program on stdout using Python subprocess? Pexpect?


code:
Python
from ctypes import *

test = CDLL('test')
size = c_int(0)
test.testout.argtypes=[c_void_p,POINTER(c_int)]
print "Result of NULL pointer and zero size:",test.testout(None,byref(size))
print "Returned size:",size.value
mem = (c_ubyte * 2)()
size = c_int(sizeof(mem))
print "sizeof(mem):",size.value
print "Result of small buffer:",test.testout(byref(mem),byref(size))
print "Returned size:",size.value
mem = (c_ubyte * size.value)()
print "Result of exact size buffer:",test.testout(byref(mem),byref(size))
print "Returned size:",size.value
for i in range(size.value):
    print mem[i]
mem = (c_ubyte * 20)()
size = c_int(20)
print "Result of bigger buffer:",test.testout(byref(mem),byref(size))
print "Returned size:",size.value
for i in range(size.value):
    print mem[i]

rawDescription: Python and C interop: Dynamic resizing of ctypes


code:
def calc_stats(xs, ys):
    print 'doing the calculations'

rawDescription: Return a portion of a plot using matplotlib


code:
l = ['abc','defghi' 'abcdef','def','defdef','defdef', 'polopolo']
l = sorted(l, key = len)
print [j for i,j in enumerate(l) if all(j == k or (j not in k) for k in l[i+1:])]
# ['defdef', 'defdef', 'polopolo', 'defghiabcdef']

rawDescription: python delete substrings from list of strings

description: Python delete substrings from list of strings


code:
a = Something()
print a.proxy
a.update({1: 42})
print a.proxy
a.update({"foo": "bar"})
print a.proxy_keys()
print a.get_proxy_wrappers()

rawDescription: Access a subset of functions of a Python class


code:
# sample usage
class SWF_RGBA(object):
    __metaclass__ = MetaStruct
    fields = 'red, green, blue, alpha'
    default_field_value = 0  # otherwise would be None

c1 = SWF_RGBA()
print vars(c1)  # {'blue': 0, 'alpha': 0, 'green': 0, 'red': 0}
c2 = SWF_RGBA(red=1, blue=4)
print vars(c2)  # {'blue': 4, 'green': 0, 'alpha': 0, 'red': 1}

rawDescription: Does this Python class look stupid?

description: Does this Python class look stupid


rawDescription: How to dynamically move inside a nested dict in Python

description: Dynamically move inside a nested dict in Python


code:
def _wait_for_server(cloud_server, server):
    while server.status != 'ACTIVE':
        sleep(30)
        server = cloud_server.servers.get(server.id)
        print '%s: %s (%s%%)' % (server.id, server.status, server.progress)

    return server

rawDescription: Python while loop does not exit, why?

description: Python while loop does not exit, why


code:
import json
your_json = '["foo", {"bar":["baz", null, 1.0, 2]}]'
parsed = json.loads(your_json)
print json.dumps(parsed, indent=4, sort_keys=True)

rawDescription: How to Python prettyprint a JSON file

description: Python prettyprint a JSON file


code:
class Employee:
   'Common base class for all employees'
   empCount = 0

   def __init__(self, name, salary):
      self.name = name
      self.salary = salary
      Employee.empCount += 1

   def displayCount(self):
     print "Total Employee %d" % Employee.empCount

   def displayEmployee(self):
      print "Name : ", self.name,  ", Salary: ", self.salary

rawDescription: "For each attribute, give the class it is."


code:
test = {}
for i in range(10):
    for j in range(15):
        test['{},{}'.format(i, j)] = i * j

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

rawDescription: Error in writing Python dictionary to a file


code: print sum(n for n in xrange(2, 2000001) if all(n % i for i in xrange(2, int(n**0.5)+1)))

rawDescription: Python recursive function error: "maximum recursion depth exceeded"


code:
for pos_X in set(program_X):

    #find the position with 375
    if pos_X == '375':
       print program_time

rawDescription: code keep firing like 7 times when using the for loops

description: Code keep firing like 7 times when using the for loops


code:
import functools
from itertools import product

def the_list():
    return ["foo", "bar", "baz"]

def do_all(func):
    if hasattr(func, 'get_from'):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            return tuple(func(*args) for args in
                         product(*(lf() for lf in func.get_from)))
        return wrapper
    return func

def gets_arg_from(*list_funcs):
    def decorator(func):
        func.get_from = list_funcs
        return func
    return decorator

@gets_arg_from(the_list)
def print_func(word):
    return word

print print_func('foo')

all = do_all(print_func)
print all()

rawDescription: Decorator which conditionally activates another decorator?


code:
from ply import lex
from ply import yacc

# lexer
tokens = ('OPEN_PAREN', 'NUMBER', 'CLOSE_PAREN', 'ADD', 'SUB', 'MUL', 'DIV')

t_OPEN_PAREN = r'\('
t_NUMBER = r'\d+\.\d+'
t_CLOSE_PAREN = r'\)'
t_ADD = r'\+'
t_SUB = r'-'
t_MUL = r'\*'
t_DIV = r'/'

t_ignore = ' \t'

# parser
def p_expression_add(p):
    'expression : expression ADD term'
    p[0] = ('+', p[1], p[3])

def p_expression_sub(p):
    'expression : expression SUB term'
    p[0] = ('-', p[1], p[3])

def p_expression_term(p):
    'expression : term'
    p[0] = p[1]

def p_term_mul(p):
    'term : term MUL factor'
    p[0] = ('*', p[1], p[3])

def p_term_div(p):
    'term : term DIV factor'
    p[0] = ('/', p[1], p[3])

def p_term_factor(p):
    'term : factor'
    p[0] = p[1]

def p_factor(p):
    'factor : OPEN_PAREN number ADD DIV SUB number CLOSE_PAREN'
    p[0] = (p[2], p[6])

def p_number(p):
    'number : NUMBER'
    p[0] = float(p[1])

# oh, and handle errors
def p_error(p):
    raise SyntaxError("Syntax error in input on line %d" % lex.lexer.lineno)

def parse(input):
    '''
    parses a string with the contents of the idf file and returns the dictionary
    representation.
    '''
    lexer = lex.lex(debug=True)
    lexer.input(input)
    parser = yacc.yacc()
    result = parser.parse(debug=True)
    return result

if __name__ == '__main__':
    #result = parse('(18.13053098972887 +/- 0.9408208676613867)')
    result = parse('(18.13053098972887 +/- 0.9408208676613867) + (4.198532451786269 +/- 1.006181051488966)')
    print result

rawDescription: How to evaluate line of data in python

description: Evaluate line of data in python


code:
import re
motif = r"\section"
txt = r"abcd\sectiondefghi"
pattern = re.compile(re.escape(motif))
txt = r"abcd\sectiondefghi"
print pattern.findall(txt)

rawDescription: Convert user input strings to raw string literal to construct regular expression


code:
import operator

def higest_divisors(a, b):
    _ret = {var: len([x for x in range(a, b+1) if not var % x]) for var in range(a, b+1)}.items()
    max = 0
    _to_ret = []
    for n, t in sorted(_ret, key=operator.itemgetter(1))[::-1]:
        if max <= t:
            _to_ret.append(n)
            max = t

    return _to_ret

if __name__ == '__main__':
    print higest_divisors(2, 10)

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


code:
from sklearn import linear_model
clf = linear_model.LinearRegression()
clf.fit ( Xs, ys )
print clf.coef_

rawDescription: How to predict time series in scikit-learn?

description: Predict time series in scikit-learn


code:
for x in xrange(0,numrows):
  row = cursor.fetchone()
  print row[0], "-->", row[1]

rawDescription: Python MySQLdb iterate through table

description: Python MySQLdb iterate through table


code:
import xml.etree.ElementTree as ET
tree = ET.parse(source)
root = tree.getroot()
# Look at each element that has 'row' tag
for row in root.iter('row'):
    print row.get('PostTypeId')

rawDescription: How to parse/extract specific values from an input huge file in python?

description: Parse/extract specific values from an input huge file in python


code:
my_new_string = "Looking feeding dancing prancing"
offset = 16
m = re.match(r'.*(\b\w+ing)(?<!.{%d})' % offset, my_new_string)
if m: print m.group(1)

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

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


code:
branch = '├'
pipe = '|'
end = '└'
dash = '─'

class Tree(object):
    def __init__(self, tag):
        self.tag = tag

class Node(Tree):
    def __init__(self, tag, *nodes):
        super(Node, self).__init__(tag)
        self.nodes = list(nodes)

class Leaf(Tree):
    pass

def _draw_tree(tree, level, last=False, sup=[]):
    def update(left, i):
        if i < len(left):
            left[i] = '   '
        return left

    print ''.join(reduce(update, sup, ['{}  '.format(pipe)] * level)) \
          + (end if last else branch) + '{} '.format(dash) \
          + str(tree.tag)
    if isinstance(tree, Node):
        level += 1
        for node in tree.nodes[:-1]:
            _draw_tree(node, level, sup=sup)
        _draw_tree(tree.nodes[-1], level, True, [level] + sup)

def draw_tree(trees):
    for tree in trees[:-1]:
        _draw_tree(tree, 0)
    _draw_tree(trees[-1], 0, True, [0])

rawDescription: Visualize tree in bash, like the output of unix "tree"


code:
import os

with os.fdopen(3, 'r') as fp:
  for line in fp:
    print line,

rawDescription: How can I run Python source from stdin that itself reads from stdin?

description: Run Python source from stdin that itself reads from stdin


code:
def y(f):
  def a(x):
    def b(*r):
      return x(x)(*r)
    return f(b)
  return a(a)

def fx(f):
  def x(q, n):
    # changed "a and b or c": different semantics if b can be falsy
    if len(q) <= n:
      return q
    else:
      return f(q[len(q) / 2:], n) + f(q[:len(q) / 2], n)
  return x

print "\n".join(
  " ".join(y(fx)(k, z + 1) for z, k in enumerate(i[:-1].split()))
  for i in list(s)[1:])

rawDescription: Translating a lambda to a normal function in Python


code:
from xml.etree import cElementTree as ET
xml = ET.fromstring(xmlData)

for child in xml.iter('field'):
    print child.tag, child.attrib, child.text

rawDescription: Translate xml string to Python list

description: Translate xml string to Python list


code:
def chunks(l, n):
    return [l[i:i+n] for i in range(0, len(l), n)]

mylist = [1,2,3,4,5,6,7,8]
while 1:
    try:
        size = int(raw_input('What size? ')) # Or input() if python 3.x
        break
    except ValueError:
        print "Numbers only please"

print chunks(yourlist, size)

rawDescription: Python transforming one dimensional array into two dimensional array


code:
class B(Observer):
    ''' class B is an observer of A '''

    def __init__(self, name, a):
        self.name = name

        # don't keep a reference to 'a', or 'a' will not be deleted!
        a.registerObserver(self)

    def __str__(self):
        return "B[ {0} ]".format(self.name)

    def __del__(self):
        print("{0}.__del__".format(self))

    def deleting(self, subject):
        ''' notification from the subject (A) that it is being deleted. I'm
        assuming here that the subject is actually the one we registered in
        __init__, since I couldn't store a reference or else __del__ would
        not have been called! '''
        print "B[{0}].deleting, subject={1}".format(self.name, subject)

        del self

rawDescription: Using the observer pattern to delete an Observer when a Subject has been "deleted" in Python


code:
import xml.etree.ElementTree as ET
document = ET.parse("recipe.xml")
root = document.getroot()
TITLE = 0

for child in root.getiterator('references'):
    author=[]
    score=[]
    for k in child.getiterator('person'):
        author.append(k.get('name'))
    for l in child.getiterator('score'):
        score.append(l.text)

    print child[TITLE].text
    print ', '.join(author)
    print ', '.join(score)

rawDescription: parsing data from xml file in python

description: Parsing data from xml file in python


code:
print next_date(10, 2, 2014)
print next_date(28, 2, 2014)
print next_date(29, 2, 2014)
print next_date(31, 12, 2014)

rawDescription: Is there a simpler code for defining next_date?


code:
def reverseString(aStr, newStr = ''):
    if len(aStr) == 0:
        return newStr
    else:
        newStr = newStr + aStr[-1]  #-1 returns the last element from the string
        return reverseString(aStr[:-1], newStr) #slice the string up to second last char
print reverseString("foobar")
#raboof

rawDescription: Writing a string backwards

description: Writing a string backwards


code:
def trav(node, acc = []):
    acc += [node['name']]
    if 'children' in node:
        for child in node['children']:
            trav(child, acc)

acc = []
trav(x, acc)
print acc

rawDescription: Extract the object names from different nesting levels in JSON


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

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


code:
from random import randint
from time import sleep

funds = 50

### Straight Up number bet = 35:1
### Odd/Even payout = 1:1

class RouletteTable(object):

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

    def spin(self, bets):
        print "Spinning..."
        sleep(2)
        print "The winner is..."
        sleep(1)
        winner = 25 #randint(0, 36) #FOR TESTING
        print "Number ", winner
        if winner in bets:
            return True

        else:
            print "You bet on: ", bets
            print "Better luck next time."
            return False

    def game(self):

        while self.wallet >= 0:
            print "\n\nWelcome to Roulette."
            print "Test your luck, and place your bets!"
            print "Current funds: $", self.wallet
            print "\n(1)Place bet or (2)Exit"
            choice = raw_input("> ")

            if choice == "1":
                bets = []
                print '''\n\n\n
          __________
         [     0    ]
         [ 1][ 2][ 3]
         [ 4][ 5][ 6]
         [ 7][ 8][ 9]
         [10][11][12]
         [13][14][15]
         [16][17][18]
         [19][20][21]
         [22][23][24]
         [25][26][27]
         [28][29][30]
         [31][32][33]
         [34][35][36]
         [ODD] [EVEN]

        '''
                print "How much will you bet (per number)?"
                bet_amount = int(raw_input("> $"))
                print "Type a number to bet on, and press Enter."
                print "When finished choosing, just press Enter."
                while True:
                    print "Funds: $", self.wallet
                    print "Current Bets: ", bets
                    number_choice = raw_input("> ")
                    if number_choice != "":
                        bets.append(int(number_choice))
                        self.wallet -= bet_amount

                    else:
                        # start spin
                        did_win = self.spin(bets)
                        # payout for bets
                        if did_win == True:
                            ammount = bet_amount*35
                            self.wallet += ammount
                            print "You win $", ammount

                        break

            if choice == "2":
                return self.wallet
                break

        if self.wallet == 0:
            print "You're out of money!\n"

roulette = RouletteTable(funds)
funds = roulette.game()

rawDescription: Roulette program will not recognize correct guess. Why?


code:
import numpy
from scipy import integrate
yvalues = numpy.arange(-2,3,0.1)
fvalues = - numpy.sin(yvalues)
f = lambda t,y: numpy.interp(y, yvalues, fvalues)
r = integrate.ode(f)
r.set_initial_value(1)
t1 = 10
dt = 0.1
while r.successful() and r.t < t1:
    r.integrate(r.t+dt)
    print r.t, r.y

rawDescription: ODE integration with discretized values

description: ODE integration with discretized values


code:
from time import clock

iterat = 10000
N = 100

LastCoin=[0, 1, 2, 1, 2, 5, 1, 2, 1, 2, 5, 1, 2,
          1, 2, 5, 1, 2, 1, 2, 5, 1, 2, 1, 2, 5,
          1, 2, 1, 2, 5, 1, 2, 1]

counter = 33
te = clock()
for i in xrange(iterat):
    final_list = [LastCoin[reduce(lambda x, y: x - LastCoin[x],
                                   range(counter, i, -1))]
                   for i in range(counter -1, 0, -1)
                   if reduce(lambda x, y: x - LastCoin[x],
                             range(counter, i, -1))]
print clock()-te,'Omid Raha'

st=33
E1 = []
for n in xrange(N):
    te = clock()
    for i in xrange(iterat):
        susu2 = [st]
        susu2.extend(x for x in xrange(st,0,-1)
                     if x==(susu2[-1]-LastCoin[susu2[-1]]))
        fifi2 = [LastCoin[x] for x in susu2]
        del fifi2
    E1.append(clock()-te)
t1 = min(E1)
print t1,'eyquem 1'

E2 = []
for n in xrange(N):
    te = clock()
    for i in xrange(iterat):
        def nekst(x,L,rec):
            rec.append(L[x])
            if x-L[x]>0: nekst(x-L[x],L,rec)
            return rec
        fifi3 = nekst(st,LastCoin,[])
        del fifi3,nekst
    E2.append(clock()-te)
t2 = min(E2)
print t2,'eyquem 2, nekst redefined at each turn of the measurement loop'

def nekst(x,L,rec):
    rec.append(L[x])
    if x-L[x]>0: nekst(x-L[x],L,rec)
    return rec

E22 = []
for n in xrange(N):
    te = clock()
    for i in xrange(iterat):
        fifi3 = nekst(st,LastCoin,[])
        del fifi3
    E22.append(clock()-te)
t22 = min(E22)
print t22,'eyquem 2, nekst defined outside of the measurement loop'

W = []
for n in xrange(N):
    te = clock()
    for i in xrange(iterat):
        y = 33
        final_list=[]
        while y>0:
            final_list.append(LastCoin[y])
            y-=LastCoin[y]
        del final_list,y
    W.append(clock()-te)
tw = min(W)
print tw,'while-loop == %.1f %% of %s' % (100*min(W)/min(E22),min(E22))

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

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


code:
import itertools as it

rank_string = ("ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king")
suit_string = ("clubs","diamonds","hearts","spades")

print 'The cards are:'
for i, card in enumerate(it.product(rank_string, suit_string)):
    print i, '{0[1]} of {0[0]}'.format(card)

rawDescription: How to use for loop to print cards?

description: Use for loop to print cards


code:
new0.data += new1.data
print new0.data

rawDescription: How does one update a FITS data array with PyFITS?

description: Update a FITS data array with PyFITS


code:
def OnDownload(self, e):
    modisPathFile = 'MODIS_data_directory_path.txt'
    dlg = wx.DirDialog(self, "Choose a directory:",
                       style=wx.DD_DEFAULT_STYLE
                       #| wx.DD_DIR_MUST_EXIST
                       #| wx.DD_CHANGE_DIR
                       )

    if dlg.ShowModal() == wx.ID_OK:
        print "You chose %s" % dlg.GetPath()
        self.snowDIR = dlg.GetPath()
        print 'snowDIR : ', self.snowDIR
        dlg.Destroy()

rawDescription: call a variable created in an event function in wx python

description: Call a variable created in an event function in wx python


code:
def velocity():
    #Rows omitted for succinctness' sake

    if 'Ux_0' in lf:
        print "Entered"
        plt.plot(time_x, value_x, color = 'r', label = 'x-vel')
    elif 'Uy_0' in lf:
        print "Entered"
        plt.plot(time_y, value_y, color = 'b', label = 'y-vel')
    elif 'Uz_0' in lf:
        print "Entered"
        plt.plot(time_z, value_z, color = 'g', label = 'z-vel')

    return (time_x, value_x, lf)  # Do Padraic's fixes here!
    return (time_y, value_y, lf)
    return (time_z, value_z, lf)

def save_velocity():
    plt.legend()
    plt.savefig(os.path.join(plotsDir, 'velocity'), bbox_inches='tight', dpi=100)
    plt.close()

for path, dirs, files in os.walk(logsDir, topdown=False):
    for lf in files:
        #Rows omitted for succinctness' sake
        if 'Uz_0' in lf:
            logFile = os.path.join(path, lf)
            data_z = np.loadtxt(logFile)
            time_z, value_z = data_z[:,0], data_z[:,1]
            (time_z, value_z, lf) = velocity()

save_velocity()

rawDescription: If statement issues in Python

description: If statement issues in Python


code:
MOTD="HI"
def Run():
    global MOTD
    print "API MOTD: "+MOTD

rawDescription: Python Plugin Capbility

description: Python Plugin Capbility


code:
u = "foo\\-bar]baz"
o = re.search('[\[\]]', u) # Does this match the \ ?
print o.group(0)
# => ']'
# Behold!

rawDescription: Search for brackets in a case insensitive using regular expressions


code:
b, c = ndimage.label(a)
e = numpy.zeros(a.shape)
for i in xrange(c):

    e += ndimage.distance_transform_cdt((b == i + 1) == 0) == 1

print e

rawDescription: Adjacent cells of multiple cell patches in a numpy array


code:
for line in output.split("\n"):
    value = None
    metric = ""
    for word in line.split():
        if word.isdigit():
            value = int(word)
        else:
            metric = "{} {}".format(metric, word)
    print "{} => {}".format(metric.strip(":"), value)

rawDescription: Python Regex Either/Or within Line

description: Python Regex Either/Or within Line


code:
from babel.messages.pofile import read_po

with open('ru.po') as po_file:
    cat = read_po(po_file)

for message in cat:
    if message.id:
        print '{!r} -> {!r}'.format(message.id, message.string)

rawDescription: Storing a text file into SQLite3 database using python


code:
wordlist = ['stardictionaries','hop','stardicto',
            'stardictionaria', 'jumper',
            'gedit','leafpad','qstardict',
            'stardictionary','stardict',]

def seurch1(deb,wordlist):
    for item in wordlist:
        if item.startswith(deb):
            yield item

def seurch2(deb,wordlist):
    L = len(deb)
    for item in wordlist:
        if item[0:L]==deb:
            yield item

from pprint import pprint

pprint( list(seurch1('stardict',wordlist)) )
print
pprint( sorted(seurch1('stardict',wordlist),key=len) )

print '--------------------'

pprint( list(seurch2('stardict',wordlist)) )
print
pprint( sorted(seurch2('stardict',wordlist),key=len) )

rawDescription: Finding out exact match in python

description: Finding out exact match in python


code:
import sys
if "__main__" == __name__:

    new_lines = dict()
    # start reading file
    with open(sys.argv[1]) as a:

        # iterate file by lines - removing newlines
        for a_line in a.read().splitlines():

            # candidate is first word in every sentence
            candidate = a_line.split()[0] # split on whitespace by default

            # dictionary keys are previous candidates
            if candidate in new_lines.keys():

                # word already included
                old_line = new_lines[candidate]
                new_lines[candidate] = "%s %s" % (old_line, a_line)

            else:

                # word not included
                new_lines[candidate] = a_line

# now we have our dictionary. print it (or do what you want with it)
for key in new_lines.keys():
    print "%s -> %s" % (key, new_lines[key])

rawDescription: how to merge file lines having the same first word in python?

description: Merge file lines having the same first word in python


code:
print Source
[u'SUMMARY', u'University of Minnesota', u'Minnesota State Colleges and Universities', u'Education', u'Minnesota State Academies', u'Perpich Center for Arts Education', u'Natural Resources', u'Pollution Control Agency', u'Board of Water and Soil Resources', u'Agriculture', u'Zoological Garden', u'Administration', u'Minnesota Amateur Sports Commission', u'Military Affairs', u'Public Safety', u'Transportation', u'Metropolitan Council', u'Human Services', u'Veterans Affairs', u'Corrections', u'Employment and Economic Development', u'Public Facilities Authority', u'Housing Finance Agency', u'Minnesota Historical Society', u'Bond Sale Expenses', u'Cancellations', u'TOTAL', u'Bond Proceeds Fund (General Fund Debt Service)', u'Bond Proceeds Fund (User Financed Debt Service)', u'State Transportation Fund', u'Maximum Effort School Loan Fund', u'Trunk Highway Fund', u'Bond Proceeds Cancellations']
print Amount
[u'119,367,000', u'159,812,000', u'7,491,000', u'11,354,000', u'2,000,000', u'63,480,000', u'2,625,000', u'8,000,000', u'203,000', u'12,000,000', u'127,000,000', u'7,973,000', u'3,244,000', u'4,030,000', u'57,263,000', u'45,968,000', u'86,387,000', u'2,800,000', u'11,881,000', u'92,130,000', u'45,993,000', u'20,000,000', u'12,002,000', u'900,000', u'(10,849,000)', u'893,054,000', u'814,745,000', u'39,104,000', u'36,613,000', u'5,491,000', u'7,950,000', u'(10,849,000)']

rawDescription: Python 3.x - Create dataframe and specify column names


rawDescription: Python: Print dictionary using column formatting?


rawDescription: Python - workaround with sets

description: Python - workaround with sets


code:
def test_login_invalid_user(self):
    print "test_login_invalid_user"
    login_page = page.LoginPage(self.driver)

rawDescription: I am using unittest.TestCase if i have more than 1 test case it will not run. Only 1 test case runs


code:
class system:
    @staticmethod
    def do():
        pass
print system.do()

rawDescription: Python def function title

description: Python def function title


code:
class A(object):
    x = 0

class B(A):
    pass

ai = A()
bi = B()

print id(ai.x) == id(bi.x)
ai.x=3
print id(ai.x) == id(bi.x)

rawDescription: Is it possible to create a completely new instance of the members of a parent class?


code:
for (x,y) in list_of_tuples:
   print x,y

rawDescription: Can I put a tuple into an array in python?

description: Put a tuple into an array in python


code:
from tools.itertoolz import sliding_window, remove
pairs = sliding_window(2, sys.stdin)
cond = lambda x: x[0].startswith('---(more') or x[1].startswith('---(more')
res = remove(cond, pairs)
for line in res:
    print line

rawDescription: What's an elegant way for one loop iteration to affect another?


code:
events = win32evtlog.ReadEventLog(hand, flags,0)
events_list = [event for event in events if event.EventID == "27035"]
if event_list:
    print 'Event Category:', events_list[0].EventCategory

rawDescription: Read Specific Windows Event Log Event

description: Read Specific Windows Event Log Event


code:
import nltk
grammar = nltk.parse_cfg("""
S -> NP VP
PP -> P NP
NP -> Det N | Det N PP | N
VP -> V NP | V PP
N -> 'Kim' | 'Dana' | 'everyone'
V -> 'arrived' | 'left' |'cheered'
P -> 'or' | 'and'
""")
sent = "Kim arrived or Dana left".split()
parser = nltk.ChartParser(grammar)
print parser.nbest_parse(sent)
sent = "Kim arrived or Dana left and".split()
parser = nltk.ChartParser(grammar)
print parser.nbest_parse(sent)

sent = "Kim arrived or Dana left and everyone".split()
parser = nltk.ChartParser(grammar)
print parser.nbest_parse(sent)

sent = "Kim arrived or Dana left and everyone cheered".split()
parser = nltk.ChartParser(grammar)
print parser.nbest_parse(sent)

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


code:
with open('input.txt') as input_file:
    rows = input_file.readlines()
rows = zip(*[row.strip() for row in rows])
rows = [''.join(row) for row in rows]
print rows

rawDescription: Using Zip to read a file vertically and search through the zipped list


code:
sentence = "Ceci est une toute petite phrase."
sentence = sentence.replace(' ', '*')
print sentence
# Ceci*sest*sune*stoute*spetite*sphrase.

rawDescription: Going character by character in a string and swapping whitespaces with python