code:
## The following was modified based on ChrisA's code in
## http://www.gossamer-threads.com/lists/python/python/1242366.
## Titled " How to read from a file to an arbitrary delimiter efficiently?"
import re

class ChunkIter:
    def __init__(self, f, delim):
        """ f: file object
        delim: regex pattern"""
        self.f = f
        self.delim = re.compile(delim)
        self.buffer = ''
        self.part = '' # the string to return

    def read_to_delim(self):
        """Return characters up to the last delim, or None if at EOF"""

        while "delimiter not found":
            b = self.f.read(256)
            if not b: # if EOF
                self.part = None
                break
            # Continue reading to buffer
            self.buffer += b
            # Try regex split the buffer string
            parts = self.delim.split(self.buffer)
            # If pattern is found
            if parts[:-1]:
                # Retrieve the string up to the last delim
                self.part = ''.join(parts[:-1])
                # Reset buffer string
                self.buffer = parts[-1]
                break

        return self.part

if __name__ == '__main__':
    with open('input.txt', 'r') as f:
        chunk = ChunkIter(f, '(\[[^]]*\]\s+(?:[^[]+)\s+\[[^]]+\])')
        while chunk.read_to_delim():
             print re.findall('\[[^]]*\]\s+([^[]+)\s+\[[^]]+\]', chunk.part)

    print 'job done.'

rawDescription: How to read a file and extract data between multiline patterns?

description: Read a file and extract data between multiline patterns


code:
cs = CacheService()
cs[1] = 'one'
cs[2] = 'two'
print cs.getcount(1)
cs[1]
print cs.getcount(1)

rawDescription: python cache dictionary - counting number of hits

description: Python cache dictionary - counting number of hits


code:
#import Tkinter as tk # Python 2.7.x
import tkinter as tk # Python 3.x

from PIL import Image, ImageTk

class GrayScaleConvertor():

    def __init__(self):

        self.window = tk.Tk()
        self.window.title('# Grayscale')

        #Opens image, creates window and draws image
        picToConvert = 'sea.jpg'
        #picToConvert = 'background.jpg'
        self.pic = Image.open(picToConvert)

        # Treats the image as a 2d array, iterates through changing the
        #values of each pixel with the algorithm for gray
        self.tkPic1 = ImageTk.PhotoImage(self.pic, master = self.window)
        self.label1 = tk.Label(self.window, image = self.tkPic1)

        # Converting to a tkinter PhotoImage
        self.tkPic2 = ImageTk.PhotoImage(self.pic, master = self.window)
        self.label2 = tk.Label(self.window, image = self.tkPic2)

        # Draws the images to the window
        self.label1.pack() # The image before grayscale
        self.label2.pack() # The image after being grayscaled

        self.column = 0 # start column
        self.step = 10 # number columns in one step
        self.window.after(1, self.change_pixel) # 1ms

    def run(self):
        self.window.mainloop()

    def change_pixel(self):

        rgbList = self.pic.load() #Get a 2d array of the pixels
        picWidth, picHeight = self.pic.size

        # not to leave image
        if self.column + self.step > picWidth:
            self.step = picWidth - self.column

        # change columns
        for column in range(self.column, self.column+self.step):
            for row in range(picHeight):
                rgb = rgbList[column,row]
                r,g,b = rgb # Unpacks the RGB value tuple per pixel
                grayAlgorithm1 = (r+g+b) // 3
                rgbList[column,row] = (grayAlgorithm1, grayAlgorithm1, grayAlgorithm1)

        # change image in label
        if self.tkPic2:
            del self.tkPic2
        self.tkPic2 = ImageTk.PhotoImage(self.pic, master = self.window)
        self.label2.config(image = self.tkPic2)

        # move start column
        self.column += self.step

        # if still are columns - call again
        if self.column < picWidth:
            print "change image"
            self.window.after(1, self.change_pixel)
        else:
            print "the end"

GrayScaleConvertor().run()

rawDescription: How do I get my program to update this tkinter label in my nested loop so that it shows the grayscale process?

description: Get my program to update this tkinter label in my nested loop so that it shows the grayscale process


code:
from djangoappengine.db.compiler import create_key
async = datastore.GetAsync(create_key(Project._meta.db_table, project_id))
p = async.get_result()
p['id'] = c.key().id() #Convert from app engine key
print Project.from_json(p)

rawDescription: Django (nonrel), App engine and asynchronous database calls


code: for item in b[2]: print '{}.v'.format(item)

rawDescription: grouping a list in individual lists in a base list or completely individual in maya python

description: Grouping a list in individual lists in a base list or completely individual in maya python


code:
def pad(x, n):
    p = alphabet[0] * (n - len(x))
    return '%s%s' % (x, p)

def average(a, b):
    n = max(len(a), len(b))
    a = debase(pad(a, n))
    b = debase(pad(b, n))
    return enbase((a + b) / 2)

print average('a', 'z') #m
print average('aa', 'zz') #mz
print average('aa', 'az') #m (equivalent to ma)
print average('cat', 'doggie') #cumqec
print average('google', 'microsoft') #jlilzyhcw
print average('microsoft', 'google') #jlilzyhcw

rawDescription: Average of two strings in alphabetical/lexicographical order


code:
from itertools import groupby

servers = []
for AllConfigurations in yXML.getElementsByTagName('AllConfigurations'):
    for DeployConfigurations in AllConfigurations.getElementsByTagName('DeployConfigurations'):
        for Servers in DeployConfigurations.getElementsByTagName('Servers'):
            for Group in Servers.getElementsByTagName('Group'):
                for GApp in Group.getElementsByTagName('GApp'):
                    for Server in Group.getElementsByTagName('Server'):
                        servers.append((Server.getAttribute('name'),
                                Group.getAttribute('name'),
                                Server.getAttribute('ip'),
                                GApp.getAttribute('type')))

def line(machine, group, ip, services):
    return " | ".join([machine.ljust(8), group.ljust(20), ip.ljust(15), services])

print line("Machine", "Group", "IP", "Services")
for server, services in groupby(sorted(servers), lambda server: server[0:3]):
    print line("- " + server[0], server[1], server[2],
            ", ".join(service[3] for service in set(services)))

rawDescription: Printing data to list styled (table) from XML in Python


code:
import re
pattern = re.compile(r'0\s*,0\s*,0\s*')
with open(filename) as f:
    for line in f:
            if pattern.search(line):
                    print line

rawDescription: finding a pattern in text file in python

description: Finding a pattern in text file in python


code: print etree.tostring(root.find('iframe'), encoding='utf-8', method="html")

rawDescription: lxml modify tags prevent

description: Lxml modify tags prevent


code:
a = [3, 5, 7, 9]
b = [2, 3, 4, 5]
c = [2, 4, 6, 8]

a_ex, b_ex, c_ex = zip(*(
                        [elem if elem in col else None
                            for col in a, b, c]
                                for elem in set(a).union(b, c)
                      ))

rawDescription: Stretch columns to pair matched cells in rows


code:
from somewhere import GPIO

from twisted.internet import reactor, task
from autobahn.websocket import WebSocketServerFactory, \
                               WebSocketServerProtocol, \
                               listenWS

class EchoServerProtocol(WebSocketServerProtocol):

    def check_movement(self):
        print "checking sensor"
        sensor_state = GPIO.input(11)
        if sensor_state == 1:
            self.send_m("sensor detected movement")

    def connectionMade(self):
        WebSocketServerProtocol.connectionMade(self)
        self.movement_checker = task.LoopingCall(self.check_movement)
        self.movement_checker.start(4)

    def onMessage(self, msg, binary):
        self.movement_checker.stop()

    def send_m(self, msg):
        self.sendMessage(msg)

if __name__ == '__main__':
   factory = WebSocketServerFactory("ws://localhost:9000")
   factory.protocol = EchoServerProtocol
   listenWS(factory)
   reactor.run()

rawDescription: Call a twisted protocol method from another thread


code:
import re
from bs4 import BeautifulSoup, SoupStrainer
import requests

# start session and get the search page
session = requests.Session()
response = session.get('https://acadinfo.wustl.edu/Courselistings/Semester/Search.aspx')

# parse the search page using SoupStrainer and lxml
strainer = SoupStrainer('form', attrs={'id': 'form1'})
soup = BeautifulSoup(response.content, 'lxml', parse_only=strainer)

# get the view state, event target and validation values
viewstate = soup.find('input', id='__VIEWSTATE').get('value')
eventvalidation = soup.find('input', id='__EVENTVALIDATION').get('value')
search_button = soup.find('input', value='Search')
event_target = re.search(r"__doPostBack\('(.*?)'", search_button.get('onclick')).group(1)

# configure post request parameters
data = {
    '__EVENTTARGET': event_target,
    '__EVENTARGUMENT': '',
    '__LASTFOCUS': '',
    '__VIEWSTATE': viewstate,
    '__EVENTVALIDATION': eventvalidation,
    'ctl00$Body$ddlSemester': '201405',
    'ctl00$Body$ddlSession': '',
    'ctl00$Body$ddlDept': '%',
    'ctl00$Body$ddlAttributes': '0',
    'ctl00$Body$Days': 'rbAnyDay',
    'ctl00$Body$Time': 'rbAnyTime',
    'ctl00$Body$cbMorning': 'on',
    'ctl00$Body$cbAfternoon': 'on',
    'ctl00$Body$cbEvening': 'on',
    'ctl00$Body$tbStart': '9:00am',
    'ctl00$Body$tbEnds': '5:00pm',
    'ctl00$Body$ddlUnits': '0',
    'ctl00$Body$cbHideIStudy': 'on',
    'ctl00$Body$courseList$hidHoverShow': 'Y',
    'ctl00$Body$courseList$hidDeptBarCnt': '',
    'ctl00$Body$courseList$hidSiteURL': 'https://acadinfo.wustl.edu/Courselistings',
    'ctl00$Body$courseList$hidExpandDetail': '',
    'ctl00$Body$hidDay': ',1,2,3,4,5,6,7',
    'ctl00$Body$hidLevel': '1234',
    'ctl00$Body$hidDefLevel': ''
}

# get the list of options
strainer = SoupStrainer('div', attrs={'id': 'Body_courseList_tabSelect'})
options = soup.select('#Body_ddlSchool > option')
for option in options:
    print "Processing {option} ...".format(option=option.text)

    data['ctl00$Body$ddlSchool'] = option.get('value')

    # make the search post request for a particular option
    response = session.post('https://acadinfo.wustl.edu/Courselistings/Semester/Search.aspx',
                            data=data)
    result_soup = BeautifulSoup(response.content, parse_only=strainer)
    print [item.text[:20].replace('&nbsp', ' ') + '...' for item in result_soup.select('div.CrsOpen')]

rawDescription: Speeding up beautifulsoup

description: Speeding up beautifulsoup


code:
from bs4 import BeautifulSoup

data = """

      test1
      0
      1
      2
      3

      test2
      0
      1
      2
      3

"""

soup = BeautifulSoup(data)

data = []
for row in soup.select("table#data tr"):
    name = row.find("td", class_="name").get_text(strip=True)
    values = row.find_all("td", class_="values")

    data.append({
        "name": name,
        "value1": values[0].get_text(strip=True),
        "value3": values[3].get_text(strip=True)
    })

print data

rawDescription: Data parsing from a table with BeautifulSoup


code:
import random
for x in range(20):
  print random.randint(1,11)*5,
print

rawDescription: Python: Generate random number between x and y which is a multiple of 5


code:
s.loc[s == 0] = np.nan

s.loc[s == 0] = np.nan
print s.fillna(method='bfill').astype(int).tolist()
[1, -1, -1, -1, 1, 1, -1, -1, -1, -1]

rawDescription: Python: How to set values of zero in a list/array/pd.Series to be the next non-zero value?


code:
def str_counter(counter):
    if sys.version_info.major > 2:
        # python3, no need to do anything
        return str(counter)
    # python2: we manually create a unicode representation.
    result = u'{%s}'
    parts = [u'%s: %s' % (unicode(key), unicode(value)) for key, value in counter.items()]
    return result % u', '.join(parts)

print str_counter(Counter(u'不'))   # python2

rawDescription: Word-counter won't print foreign characters


code:
from itertools import groupby
k = [{'Titel': 'Rush', 'Name': 'Floris', 'Starttime': '20:30', 'Email':      'Floris@email.com', 'Supplier': 'RTL8', 'Surname': 'Cake', 'Code': 'ABC123'},
{'Titel': 'Rush', 'Voornaam': 'Jaron', 'Starttime': '20:30', 'Email': 'JaronPie@email.com', 'Supplier': 'RTL8', 'Surname': 'Pie', 'Code': 'XYZ123'},
{'Titel': 'Underneath', 'Name': 'Klaas', 'Starttime': '04:00', 'Email': 'Klassieboy@gmail.com', 'Supplier': 'RTL8', 'Surname': 'Klassie', 'Code': 'fbhwuq8674'}]

lst=[(i["Titel"],i["Starttime"],i["Surname"],i.get("Name","None"), i["Email"]) for i in k]
lst.sort(key=lambda x:(x[0],x[1]))
for key,groups in groupby(lst,key=lambda x:(x[0],x[1])):
    print "{}\t{}".format(key[0],key[1])
    for value in groups:
        print "{}\t{}\t{}".format(value[2],value[3],value[4])
    print ""

rawDescription: Python - Extracting information from dictionary in a specific way


code:
import django
print django.__file__

rawDescription: where does django install in ubuntu

description: Where does django install in ubuntu


code:
def glue(a, b):
    maxn = 0
    for n in xrange(1, 1 + min(len(a), len(b))):
        suffix = a[-n:]
        prefix = b[:n]
        if prefix == suffix:
            maxn = n
    # BUG: return maxn, a[:-maxn] + b
    # FAILS when maxn == 0
    # EXTRA TEST: ['nil', 'overlap']
    return a + b[maxn:]

def multiglue(words):
    if not words: return ""
    result = words[0]
    for word in words[1:]:
        nx, rx = glue(result, word)
        ny, ry = glue(word, result)
        result = rx if nx > ny else ry
    return result

tests = [line.split() for line in """
    one
    two one
    one two
    overlap nil
    nil overlap
    toad dog rabbit
    frog ogham
    ogham frog
    hopper grasshopper
    grass grasshopper person
    foooo oooof
    oooof foooo""".splitlines()]

for test in tests:
    out = multiglue(test)
    print test, repr(out)

rawDescription: Python Combining strings into the shortest possible string?


code:
def print_fix(games,team):
    """Takes a list of games (as team-pairs) and prints information for the opposing team in each match that contains the specified team."""
    team=team.upper()  #just for convenience
    for game in games:
        if team in game:  #The in statement returns True if the team element is equal to at least one of the teams in the game-array.
            #For example: team in ["CHE","LIE"] would return true if team was either "CHE" or "LIE"
            if game[0] == team: #If "my team" is the first one of the pair, then the "Other team" must be the second, and the other way around.
                other=game[1]
            else:
                other=game[0]
            print other, teams[other]

matches= read_fix("fixtures.txt")
print_fix(matches,"CHE")

rawDescription: Data parsing, the pythonic way

description: Data parsing, the pythonic way


code:
import numpy as np

N = 50
M = 1000

x = np.random.rand(N)
X = np.c_[x, np.ones_like(x)]
beta = np.random.rand(M, 2)
Y = np.dot(beta, X.T)
Y += np.random.normal(scale=0.1, size=Y.shape)
mask = np.random.randint(0, 2, size=Y.shape).astype(np.bool)

a = np.swapaxes(np.dot(X.T, (X[None, :, :] * mask[:, :, None])), 0, 1)
b = np.dot(X.T, (mask*Y).T)
beta2 = np.linalg.solve(a, b.T)

i = 123
print "real:", beta[i]
print "by solve:", beta2[i]

m = mask[i]
x2 = X[m]
y2 = Y[i, m]
print "by lstsq:", np.linalg.lstsq(x2, y2)[0]

rawDescription: Efficient 1D linear regression for each element of 3D numpy array


code:
a = ["paul Parthipan","paul","sdds","sdsdd",'Mala_Koala','Czarna,Pala']
last_name = "Parthipan"
names=[]
breakers=[' ','_',',']
for ai in a:
    for b in breakers:
        if b in ai:
            names.append(ai.split(b))
full_names=[ai for ai in names if len(ai)==2]
last_names=[ai[1] for ai in full_names]

if any(last_name in ai for ai in last_names):
    print "matched"

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

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


code:
import Tkinter as tk
import time

root = tk.Tk()

left_pressed = False
rigth_pressed = False

def on_left_click(event):
    global left_pressed, rigth_pressed

    if rigth_pressed:
        rigth_pressed = False
    else:
        left_pressed = True

    root.after(300, on_left_later)

def on_left_later():
    global left_pressed

    if left_pressed:
        left_pressed = False
        print "left pressed"
    else:
        print "both pressed"

def on_right_click(event):
    global left_pressed, rigth_pressed

    if left_pressed:
        left_pressed = False
    else:
        rigth_pressed = True

    root.after(300, on_right_later)

def on_right_later():
    global rigth_pressed

    if rigth_pressed:
        rigth_pressed = False
        print "rigth pressed"
#    else:
#        print "(right_do_nothing)"

button = tk.Button(root, text="Clik me! - left, right, both")
button.pack()
button.bind('<Button-1>',on_left_click)
button.bind('<Button-3>',on_right_click)

tk.mainloop()

rawDescription: Detect simultaneous left and right clicks in python gui


code:
import numpy as np

for words in cleaned_example:
    a_values = []
    for word in words:
        a_values.append(sentiment(word)[0])
    if sentiment(words[0])[0] < np.mean(a_values):
        print "Increasing"

rawDescription: How do I check if the values I am getting are increasing/decreasing?

description: Check if the values I am getting are increasing/decreasing


code:
def tryconvert(value, default, *types):
    for t in types:
        try:
            return t(value)
        except ValueError, TypeError:
            continue
    return default

rawDescription: Python Try Catch Block inside lambda

description: Python Try Catch Block inside lambda


code:
import requests

s = requests.Session()
payload = {
    'd2l_referrer': '',
    'target': '/d2l/lp/ouHome/loginHome.d2l?isMobile=1',
    'loginPath': '/d2l/m/login',
    'userName': 'MYUSERNAME',
    'password': 'MYPASSWORD!'
}
r = s.POST('http://learn.ou.edu/d2l/lp/auth/login/login.d2l', data=payload)
print r.status_code
print r.content

rawDescription: Trouble with "Python Requests" username/password login


code:
import random

def read_file():
    res = {}
    start = True
    with open('pairs.txt', 'r') as f:
        for line in f.readlines():
            if start:
                start = False
                continue
            woman, matches = line.strip().split(': ')
            woman = int(woman)
            matches = map(int, matches.split(' '))
            res[woman] = matches
    return res

def build_random_match(graph):
    edges = {}
    for woman in graph:
        for man in graph[woman]:
            if already_in_edges(man, edges):
                continue
            else:
                edges[woman] = man
                break
    return edges

def already_in_edges(man, edges):
    for woman in edges:
        if edges[woman] == man:
            return True
    else:
        return False

def get_unmatched_women(match, graph):
    return  [woman for woman in graph.keys() if woman not in match.keys()]

def not_in_match(man, match):
    for woman in match:
        if match[woman] == man:
            return False
    else:
        return True

def find_unmatched_man(graph, match, woman):
    potentials = graph[woman]
    for man in potentials:
        if not_in_match(man, match):
            return man
    else:
        return False

def remove_man_from_match(man, unmatched_woman, match, graph):
    # find the woman that this man is currently matched with
    # and cancel this matching
    for woman in match:
        if match[woman] == man:
            match_to_del = woman
            break
    del match[match_to_del]
    # also remove the man from the orig woman (graph)
    # to prevent infinite loop
    men = graph[unmatched_woman]
    men.remove(man)
    graph[unmatched_woman] = men

    return match_to_del

def relax(unmatched_woman, match, graph):
    unmatched_man = find_unmatched_man(graph, match, unmatched_woman)
    if unmatched_man:
        match[unmatched_woman] = unmatched_man
    elif len(graph[unmatched_woman]) == 0:
        return match
    else:
        # grab one of the possible matchings randomly
        rand_index = random.randint(0, len(graph[unmatched_woman])-1)
        man = graph[unmatched_woman][rand_index]
        new_unmatched_woman = remove_man_from_match(man, unmatched_woman, match, graph)
        match[unmatched_woman] = man
        match = relax(new_unmatched_woman, match, graph)

    return match

def improve_match(match, graph):
    if len(match) == len(graph):
        return match

    unmatched_women = get_unmatched_women(match, graph)
    for woman in unmatched_women:
        copy_graph = graph.copy()
        suggested = relax(woman, match, copy_graph)
        if len(suggested) > len(match):
            return suggested
        else:
            suggested = match
    else:
        return suggested

def main():
    graph = read_file()
    match = build_random_match(graph)
    if len(match) == len(graph):
        print 'Got a perfect match:', match
    else:
        match_size = 0
        while match_size < len(match):
            match_size = len(match)
            match = improve_match(match, graph)

    return match

if __name__ == '__main__':
    res = main()
    print "Size of match:", len(res)
    print "Match:", res

rawDescription: How do I find the maximum amount of possible correct matches in these arrays?

description: Find the maximum amount of possible correct matches in these arrays


code:
sampleguid.append(allguid[1:5])

# for each item, let's call it guid, in the list sampleguid
# do the following
for guid in sampleguid:
    # list of unneeded characters you want to remove
    unneeded_chars = ['"','(',')',"'"]
    splitter_char = ","
    for c in unneeded_chars:
        guid = guid.replace(c,splitter_char)
    for guid_item in guid:
        full_url = url + "?" + str(guid_item)
        print full_url

rawDescription: List to string and for loop

description: List to string and for loop


code:
import re

matches = re.findall('[^()]+', '(abc)(def)(ghi)h')
map = []
for m in matches:
  map.append(list(m))
for e in map:
  print e

rawDescription: Regex Group Matching

description: Regex Group Matching


code:
neisets = [set(g.neighbors(i)) for i in xrange(g.vcount())]
for v1, v2 in g.get_edgelist():
    common_neis = neisets[v1].intersection(neisets[v2])
    print "%d --> %d: %d" % (v1, v2, len(common_neis))

rawDescription: Common neighbors and preferential attachment score matrixes using igraph for python


code:
from HTMLParser import HTMLParser

class print_tags(HTMLParser):
    def handle_starttag(self, tag, attrs):
        print tag # TODO: add/update dict

parser = print_tags()
parser.feed(html)

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


code:
import ast

class DictDisplayTransformer(ast.NodeTransformer):
    def visit_Dict(self, node):
        self.generic_visit(node)
        list_node = ast.List(
            [ast.copy_location(ast.Tuple(list(x), ast.Load()), x[0])
             for x in zip(node.keys, node.values)],
            ast.Load())
        name_node = ast.Name("dict", ast.Load())
        new_node = ast.Call(ast.copy_location(name_node, node),
                            [ast.copy_location(list_node, node)],
                            [], None, None)
        return ast.copy_location(new_node, node)

def my_execfile(filename, globals=None, locals=None):
    if globals is None:
        globals = {}
    if locals is None:
        locals = globals
    node = ast.parse(open(filename).read())
    transformed = DictDisplayTransformer().visit(node)
    exec compile(transformed, filename, "exec") in globals, locals

rawDescription: Override the {...} notation so i get an OrderedDict() instead of a dict()?


code:
import urllib2
import urllib
from json import loads
import cookielib

def doit(search_term, base_url = "https://graph.facebook.com/search?q="):
    opener = urllib2.build_opener()
    opener.addheaders = [('Accept-Language', 'en-US,en;q=0.8')]

    encoded_search_term = urllib.quote(search_term)
    type="&type=post"
    url = "%s%s%s" % (base_url,encoded_search_term,type)

    print encoded_search_term
    print url

    data = True
    while data:
        response = opener.open(url)

        content = loads(response.read())

        print "=================================="
        for c in content["data"]:
            print c.keys()
        print "****************************************"

        if "paging" in content:
            url = content["paging"]["next"]
        else:
            print "Empty response"
            print content
            data = False

doit('"Plastic Planet"')

rawDescription: Unable to retrieve search results from server side : Facebook Graph API usig Python


code:
def where1(cond, x):
    # elements of x where cond
    row, col, data = sparse.find(cond) # effectively the coo format
    data = np.ones(data.shape, dtype=x.dtype)
    zs = sparse.coo_matrix((data, (row, col)), shape=cond.shape)
    xx = x.tocsr()[row, col][0]
    zs.data[:] = xx
    zs = zs.tocsr()
    zs.eliminate_zeros()
    return zs

def where2(cond, y):
    # elements of y where not cond
    row, col, data = sparse.find(cond)
    zs = y.copy().tolil() # faster for this than the csr format
    zs[row, col] = 0
    zs = zs.tocsr()
    zs.eliminate_zeros()
    return zs

def where(cond, x, y):
    # like np.where but with sparse matrices
    ws1 = where1(cond, x)
    # ws2 = where1(cond==0, y) # cond==0 is likely to produce a SparseEfficiencyWarning
    ws2 = where2(cond, y)
    ws = ws1 + ws2
    # test against np.where
    w = np.where(cond.A, x.A, y.A)
    assert np.allclose(ws.A, w)
    return ws

m,n, d = 100,90, 0.5
cs = sparse.rand(m,n,d)
xs = sparse.rand(m,n,d)
ys = sparse.rand(m,n,d)
print where(cs, xs, ys).A

rawDescription: Scipy equivalent of numpy where for sparse matrices


code:
from bs4 import BeautifulSoup
markup = '<span class="cd__headline-text">Is this model too thin for Yves Saint Laurent? </span>'
soup = BeautifulSoup(markup)
headline = soup.find(class_='cd__headline-text')
print headline.string
print list(headline.strings)
print list(headline.stripped_strings)
print headline.get_text()
print headline.get_text(strip=True)

rawDescription: How to obtain the content between a tag and it's ending in HTML using python' beautiful soup?

description: Obtain the content between a tag and it's ending in HTML using python' beautiful soup


code:
import sqlite3
import uuid

sqlite3.register_converter('GUID', lambda b: uuid.UUID(bytes_le=b))
sqlite3.register_adapter(uuid.UUID, lambda u: buffer(u.bytes_le))

conn = sqlite3.connect('test.db', detect_types=sqlite3.PARSE_DECLTYPES)

c = conn.cursor()
c.execute('CREATE TABLE test (guid GUID PRIMARY KEY, name TEXT)')

data = (uuid.uuid4(), 'foo')
print 'Input Data:', data
c.execute('INSERT INTO test VALUES (?,?)', data)

c.execute('SELECT * FROM test')
print 'Result Data:', c.fetchone()

rawDescription: Proper way to store GUID in sqlite

description: Proper way to store GUID in sqlite


code:
import re

number_mapping = {'1': 'one',
                  '2': 'two',
                  '3': 'three'}
s = "1 testing 2 3"

print re.sub(r'\d', lambda x: number_mapping[x.group()], s)

rawDescription: Passing a function to re.sub in Python

description: Passing a function to re.sub in Python


code:
from fpipe import macros, fpipe
from macropy.quick_lambda import macros, f
# The `from module import macros, ...` must be used for macropy to know which
# macros it should apply to your code.
# Here two macros have been imported `fpipe`, which does what you want
# and `f` which provides a quicker way to write lambdas.

from math import sqrt

# Using the fpipe macro in a single expression.
# The code between the square braces is interpreted as - str(sqrt(12))
print fpipe[12 | sqrt | str] # prints 3.46410161514

# using a decorator
# All code within the function is examined for `x | y` constructs.
x = 1 # global variable
@fpipe
def sum_range_then_square():
    "expected value (1 + 2 + 3)**2 -> 36"
    y = 4 # local variable
    return range(x, y) | sum | f[_**2]
    # `f[_**2]` is macropy syntax for -- `lambda x: x**2`, which would also work here

print sum_range_then_square() # prints 36

# using a with block.
# same as a decorator, but for limited blocks.
with fpipe:
    print range(4) | sum # prints 6
    print 'a b c' | f[_.split()] # prints ['a', 'b', 'c']

rawDescription: functional pipes in python like %>% from dplyr

description: Functional pipes in python like %>% from dplyr


code:
class Method(object):
    def __init__(self, client, method_name):
        self.client = client
        self.method_name = method_name

    def __getattr__(self, key):
        return Method(self.client, '.'.join((self.method_name, key)))

    def __call__(self, **kwargs):
        print self.method_name, kwargs

class Client(object):
    def __getattr__(self, key):
        return Method(self, key)

rawDescription: Overriding __getattr__ to support dynamic nested attributes


code:
def var_states(varss):
    print "var_states", varss
    for var in varss:
        print var, var.get()

rawDescription: Python Tkinter Checkbutton Issue

description: Python Tkinter Checkbutton Issue


code:
import itertools
password_list = [''.join(tup) for tup in itertools.product(*char_list)]
print 'appletree' in password_list
print 'apfelbaum' in password_list
print 'apfletrum' in password_list

rawDescription: Add specific columns to a single row list (might be a little basic)


code:
import sys, re

user= sys.argv[2]
pattern= r'(?ms)^Started [^\n]*(?:(?!^Completed\b).)*?Current user: %s\b.*?^Completed\b[^\n]*'%re.escape(user)
with open(sys.argv[1]) as f:
    print '\n'.join(re.findall(pattern, f.read()))

rawDescription: RegExp to extract data from a Redmine log file


code:
from bs4 import BeautifulSoup
data = """

    1
    2
    3

"""
soup = BeautifulSoup(data)
for element in soup.find_all('td', class_=lambda x: x != 'foo'):
    print element.text

rawDescription: BeautifulSoup4: select elements where attributes are not equal to x


code:
def open_file_handler():
    filePath = askopenfilename()
    print filePath
    return filePath

def handle_file():
    filePath = open_file_handler()
    # handle the file

rawDescription: python file dialog accessing file name

description: Python file dialog accessing file name


code:
def foo(a, b):
     print a, b

def bar(a, b, c = None):
     print a, b, c

p = DynamicEvent(engine)
p.addHandler(foo)
p.addHandler(bar)

p.invoke("a", "b")
p.invoke("a", "b", "c")

rawDescription: IronPython function with variable number of arguments as a delegate


code:
import subprocess

p = subprocess.Popen(
    [
        'java',
        '-cp',
        '/Users/7stud/java_programs/myjar.jar',
        'MyProg'
    ],
    stdout = subprocess.PIPE,
    stdin = subprocess.PIPE,
)

p.stdin.write("haha\n")
print "i am done"
print p.stdout.readline().rstrip()

rawDescription: stdout from python to stdin java

description: Stdout from python to stdin java


code:
from test import *
print foo_py(2.9) # will output 2.9

rawDescription: Finding the simplest way to wrap a double->double C function to Python


code:
from bs4 import BeautifulSoup

data = """

                    12.5 kilograms
             $619.06 $523.91

                	"""
soup = BeautifulSoup(data)

print soup.td.span.text

rawDescription: Using REGEX to match elements between lines in Python


code:
checking.__doc__ = ''.join(checking.__doc__.split('#doctest: +ELLIPSIS'))
print checking.__doc__
doctest.testmod(optionflags=doctest.ELLIPSIS)

rawDescription: python doctest check for a specific line

description: Python doctest check for a specific line


code:
class Command(object):
    @staticmethod
    def subcommand(method):
        method.is_subcommand = True

        return method

class CommandBase(Command):

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

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


code:
print '\u2022'
print len('\u2022')
import unicodedata
map(unicodedata.name, '\u2022'.decode('ascii'))

rawDescription: How do I split a unicode string on code points in python? (eg. \u00B7 or \u2022)?

description: Split a unicode string on code points in python? (eg. \u00B7 or \u2022)


code:
lines =f_lrc.readlines()
lyrics = lines[10:]
first_part = lines[:10]
yrics = re.sub(ur'<\d{1,2}:\d{2}\.\d{1,2}>', '', '\n'.join(lyrics))
last = '\n'.join(first_part) + yrics

rawDescription: How to replace all except first 10 elements matched with regex?

description: Replace all except first 10 elements matched with regex


code:
def run():
    print "RUNNING"
    return 1

class dict_nokeyerror(dict):
    def __missing__(self, key):
        return run()

test = dict_nokeyerror()

print test['a']
# RUNNING
# 1

rawDescription: Callable as the default argument to dict.get without it being called if the key exists


code:
import sys

class Map:
    def __init__(self, height, width, player_x, player_y, paths):
        self.height = height
        self.width = width
        self.x = player_x
        self.y = player_y
        self.paths = paths

    def move(self, direction):
        if direction == "n":
            if ((self.x, self.y - 1), (self.x, self.y)) not in self.paths:
                print "Cannot go north"
            else:
                self.y -= 1
        if direction == "s":
            if ((self.x, self.y), (self.x, self.y + 1)) not in self.paths:
                print "Cannot go south"
            else:
                self.y += 1
        if direction == "e":
            if ((self.x, self.y), (self.x + 1, self.y)) not in self.paths:
                print "Cannot go east"
            else:
                self.x += 1
        if direction == "w":
            if ((self.x - 1, self.y), (self.x, self.y)) not in self.paths:
                print "Cannot go west"
            else:
                self.x -= 1

    def print_map(self):
        for y in range(0, self.height):
            # print the yth row of rooms
            for x in range(0, self.width):
                if self.x == x and self.y == y:
                    sys.stdout.write("[u]")  # this is the player's room
                else:
                    sys.stdout.write("[ ]")  # empty room
                # now see whether there's a path to the next room
                if ((x, y), (x + 1, y)) in self.paths:
                    sys.stdout.write("-")
                else:
                    sys.stdout.write(" ")
            # now that we've written the rooms, draw paths to next row
            print  # newline
            for x in range(0, self.width):
                sys.stdout.write(" ")  # spaces for above room
                if ((x, y), (x, y + 1)) in self.paths:
                    sys.stdout.write("|  ")
                else:
                    sys.stdout.write("   ")
            print

paths = [((0, 0), (1, 0)), ((0, 0), (1, 0)), ((1, 0), (1, 1)), ((1, 1),
         (2, 1)), ((1, 1), (1, 2)), ((0, 2), (1, 2)), ((1, 2), (2, 2)),
         ((0, 2), (0, 3)), ((0, 3), (1, 3)), ((1, 3), (2, 3)), ((2, 3),
         (3, 3))]
m = Map(4, 4, 0, 0, paths)

while True:
    m.print_map()
    direction = raw_input("What direction do you want to move? [n/e/s/w] ")
    m.move(direction)

rawDescription: Python Drawing ASCII Map

description: Python Drawing ASCII Map


code:
from ast import literal_eval
def solve(x):
    try:
        return literal_eval(x)
    except (ValueError, SyntaxError):
        return x

s = '365\tFoo\r\nBar\t-90.01\r\n48\tspam\r\n12e10\t-174\r\n30\t-156\r\n'
print [map(solve, x.split('\t')) for x in s.rstrip().split('\r\n')]
#[[365, 'Foo'], ['Bar', -90.01], [48, 'spam'], [120000000000.0, -174], [30, -156]]

rawDescription: How to split a string at line breaks in python?

description: Split a string at line breaks in python


code:
for row in reader:
    for word in ol:
        if word==row[1]:
            break
    else:
        print row[0],row[1],row[2]

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

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


code: for imp in get_imports("/path/to/file.py"): print imp

rawDescription: Python easy way to read all import statements from py module


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

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


code:
array[2][4] = True
for row in array:
  print row

rawDescription: How to create a 2D list in python where all elements are the same?

description: Create a 2D list in python where all elements are the same


code:
print counter
break

rawDescription: My 'lowest common multiple' program hangs, and doesn't output an answer


code:
a = array([1,1,-1,-2,-3,4,5])
asign = np.sign(a)
signchange = ((np.roll(asign, 1) - asign) != 0).astype(int)
print signchange
array([0, 0, 1, 0, 0, 1, 0])

rawDescription: How to detect a sign change for elements in a numpy array

description: Detect a sign change for elements in a numpy array


code:
night = raw_input('Which night? mon,tue,wed,thu or fri. ')
time = raw_input('Which time? t11,t12,t01,t02 or t03. ')
print occurrences[night][times[time]]

rawDescription: Manipulations with raw_input

description: Manipulations with raw_input


code:
data ='''

                M4A4 | Poseidon

            Factory New
            Classified Rifle

$195.00

 Suggested Price: $258.52

Inspect
                <button class="btn btn-orange" type="button" id="shopItem" onclick="addToCart(9462141)">Add to Cart</button>

                Chroma 2 Case Key

            Base Grade Key

$2.11

 Suggested Price: $2.70

                <button class="btn btn-orange" type="button" id="shopItem" onclick="addToCart(9462120)">Add to Cart</button>

'''

import lxml, lxml.html

html = lxml.html.fromstring(data)

divs = html.xpath('//div[@class="featured-item col-xs-12 col-sm-6 col-md-4 col-lg-3 center-block app_730_2"]')

for x in divs:
    a = x.xpath('.//a/text()')[0]
    print a.strip()

    small = x.xpath('.//small[@class="text-muted"]/text()')
    if small:
        print small[0]

    div = x.xpath('.//div[@class="item-amount"]/text()')[0]
    print div

    a_href = x.xpath('.//a/@href')
    item = a_href[1].split('=')[-1]
    print item

    img = x.xpath('.//img[@class="item-img"]/@src')[0]
    print img

rawDescription: Parsing multiple values from html with lxml


code:
import os
for root, folders, files in os.walk('C:\\'):
    print 'In ' + root + ' There are ' + str(len(folders)) + ' folders and ' + str(len(files)) + '!'

rawDescription: python construct to know the list of all the directories and recursive sub-directories

description: Python construct to know the list of all the directories and recursive sub-directories


code:
print np.all(v == v2), np.all(v == v3)
# True, True

rawDescription: Initialising a vector field in numpy

description: Initialising a vector field in numpy


code:
i = 0
def f():
    global i
    i += 1
    return i

for x in iter(f, 5):
    print x

rawDescription: How to use iter(v,w) with a function?

description: Use iter(v,w) with a function


code:
h, i, j = (np.zeros(5),)*3
print h is i
# True

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


code:
fzz = skew(x,e,w,a) + norm.rvs(0,0.04,size=n) # fuzzy data

def optm(l,x):
    return skew(x,l[0],l[1],l[2]) - fzz

print leastsq(optm,[0.5,0.5,0.5],(x,))

rawDescription: skew normal distribution in scipy

description: Skew normal distribution in scipy


code:
for heading in root.iter("h3"):
    print "----", heading
    for sibling in heading.itersiblings():
        if sibling.tag == 'h3':
            break
        print sibling

rawDescription: what is the next tag after the specific tag in html using xpath

description: What is the next tag after the specific tag in html using xpath


code:
print [(fruit, weights[fruit], prices[fruit]) for fruit in weights]
# [('coconut', 14, 3.2), ('apple', 5, 0.99), ('banana', 9, 1.24)]

rawDescription: Create a 3-dimension list from two 2-dimension lists with a common element in each


code:
L = [(u'Delhi NCR', 148, 'checked'),
  (u'New Delhi', 51),
  (u'Gurgaon', 20),
  (u'Faridabad', 18),
  (u'Ghaziabad', 17),
  (u'Greater Noida', 16),
  (u'Noida', 11, 'checked'),
  (u'Sonepat', 7),
  (u'Palwal', 5),
  (u'Gautam Buddh Nagar', 2),
  (u'Rewari', 1)]
print sorted(L, key=len, reverse=True)

rawDescription: sort tuple based on parameter that may exists or not

description: Sort tuple based on parameter that may exists or not


code:
import sys
sys.stdout = open('file', 'w')
print 'test'

rawDescription: Redirect stdout to a file in Python?

description: Redirect stdout to a file in Python


code:
from itertools import combinations

for key in mydict:
    print "{}: '{}'".format(key, mydict[key])

for x, y in combinations(mydict, 2):
    print "{}: '{}'".format(x+y, '-'.join((mydict[x], mydict[y])))

rawDescription: Generate all possible combinations in dictionary


code:
e = Error(code="0",value="OK")
print e.render(fragment=True)

rawDescription: Python framework for object-XML mapping through decorators?


code:
import heapq, glob

def my_iterable():
    for fname in glob.glob("in_s*.txt"):
        f = open(fname, "r")
        for line in f:
            items = line.split()
            yield fname, items[0], float(items[6])
        f.close()

result = heapq.nsmallest(10, my_iterable(), lambda x: x[2])
print result

rawDescription: Search and sort data from several files

description: Search and sort data from several files


code:
import urllib2, json

user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'
header = { 'User-Agent' : user_agent }

fullurl = "http://ws.parlament.ch/votes/councillors?councillorNumberFilter=2565&format=json"
response = urllib2.Request(fullurl, headers=header)
data = urllib2.urlopen(response)
print json.loads(data.read())

rawDescription: How to download webpage in python, url includes GET parameters

description: Download webpage in python, url includes GET parameters


code:
import numpy as np

# Some data...
x = np.arange(3 * 4 * 5).reshape(5, 4, 3)

print x[0,0,:]

# Reverse the last axis
y = x[:, :, ::-1]

print y[0,0,:]

rawDescription: Reverse a 3d array in python

description: Reverse a 3d array in python


code:
for value in MyList:
    print value.i

inst1.i = 4
raw_values = [o.i for o in MyList]
[4, 5]

inst1.i = 2
raw_values = [o.i for o in MyList]
[2, 5]

rawDescription: Python : How to create a dynamic list of class values


code:
import subprocess,re
p = subprocess.Popen('commands',stdout=subprocess.PIPE,stderr=subprocess.PIPE)
results, errors = p.communicate()
print results
re.search('<(\d+)>', results).group(1) #Cheers, Jon Clements

rawDescription: os.system for submitting command

description: Os.system for submitting command


code:
file = open("input.txt", "rb")

bytes = []
while True:
    byte = file.read(1)
    print byte
    bytes.append(byte)
    if ord(byte) == 0x50:
        break

print bytes

rawDescription: extract binary data and convert them following a specific alphabet

description: Extract binary data and convert them following a specific alphabet


code:
def remove_adjacent(items):
    # iterate the items
    it = iter(items)
    # get the first one
    last = next(it)
    # yield it in any case
    yield last
    for current in it:
        # if the next item is different yield it
        if current != last:
            yield current
            last = current
        # else: its a duplicate, do nothing with it

print list(remove_adjacent([1,1,1,2,2,3,4,4]))

rawDescription: A cleaner/shorter way to solve this problem?


code:
import httplib2, os, sys, tempfile, urllib, urlparse
import oauth2 as oauth

######## OAUTH: GET REQUEST TOKEN #############
consumer = oauth.Consumer('anonymous', 'anonymous')
creds = {'oauth_token_secret': 'INSERT_SECRET_FROM_ABOVE', 'oauth_token': 'INSERT_TOKEN_FROM_ABOVE'}
client = oauth.Client(consumer)
client.token = oauth.Token(creds['oauth_token'], creds['oauth_token_secret'])
url = 'https://docs.google.com/feeds/default/private/full?v=3'
resp, content = client.request(url, 'GET')
if resp['status'] == '200':
    print 'list status OK'
    fh = open('/tmp/list.xml', 'w')
    fh.write(content)
    fh.close()
else:
    print 'list status: %s' % resp['status']
    print content
    sys.exit(1)

rawDescription: Google OAuth problem: invalid_signature

description: Google OAuth problem: invalid_signature


rawDescription: Python functions call by reference

description: Python functions call by reference


code:
if My_dict.get(2):
    print True
else:
    print False

rawDescription: python defaultdict get method return value

description: Python defaultdict get method return value


code:
results = sm.OLS(y, X).fit()
print results.summary(xname=['Fred', 'Mary', 'Ethel', 'Bob'])

rawDescription: Naming explanatory variables in regression output


code:
key1 = lambda (key, (value, hash)): key
key2 = lambda (key, (value, hash)): value
key3 = lambda (key, (value, hash)): hash

rawDescription: Sort a dictionary by key, value (tuple)

description: Sort a dictionary by key, value (tuple)


code: (lambda (a,b,c): [a,b.upper(),c])(inp)

rawDescription: Apply function to one element of a list in Python


code:
x = int(raw_input("Please enter an integer: "))
if x < 0:
    x = 0
    print 'Negative changed to zero'
elif x == 0:
    print 'Zero'
elif x == 1:
    print 'Single'
else:
    print 'More'

rawDescription: How should the if statement look like?

description: How should the if statement look like


code:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *

POINT_SIZE = 7
class Test_Box(QGraphicsItem):
    # Constants
    WIDTH           = 50 * POINT_SIZE
    HEIGHT          = 13 * POINT_SIZE
    RECT            = QRectF(0, 0, WIDTH, HEIGHT)
    CORNER_RADIUS   = 1.5 * POINT_SIZE

    def __init__(self, position, parent=None):
        super(Test_Box, self).__init__(parent)

        # Settings
        self.setFlags(  self.flags()                            |
                        QGraphicsItem.ItemIsSelectable          |
                        QGraphicsItem.ItemIsMovable             |
                        QGraphicsItem.ItemIsFocusable           |
                        QGraphicsItem.ItemSendsScenePositionChanges )
        self.setPos(position)

    def boundingRect(self):
        return Test_Box.RECT

    def paint(self, painter, option, widget):
        # Draw Box
        brush   = QBrush()
        painter.setBrush(brush)
        painter.drawRoundedRect(Test_Box.RECT, Test_Box.CORNER_RADIUS, Test_Box.CORNER_RADIUS)

    def itemChange(self, change, variant):
        super(Test_Box, self).itemChange(change, variant)
        if change == QGraphicsItem.ItemScenePositionHasChanged:
            self.setParentItem(None)
        return QGraphicsItem.itemChange(self, change, variant)

class Window(QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.scene = QGraphicsScene()

        self.item1 = Test_Box(QPointF(0, 0))
        self.item2 = Test_Box(QPointF(20, 20))
        self.item11 = Test_Box(QPointF(10, 5), self.item1)

        self.scene.addItem(self.item1)
        self.scene.addItem(self.item2)

        self.view = QGraphicsView(self.scene)

        self.listItems = QPushButton('list')
        self.listItems.clicked.connect(self.printItems)

        layout = QHBoxLayout()
        layout.addWidget(self.view)
        layout.addWidget(self.listItems)
        self.setLayout(layout)

    def printItems(self):
        for item in self.scene.items():
            print item, item.parentItem()

app = QApplication(sys.argv)

w = Window()
w.show()

sys.exit(app.exec_())

rawDescription: Removing a QGraphicsItem's parent

description: Removing a QGraphicsItem's parent


code:
for n in range(2,100):
    if all(n%x != 0 for x in range(2,n)):
        print n,'is a prime number'

rawDescription: Prime number in python, prog not working


rawDescription: Find roots of a function a x^n + bx - c = 0 where n isnt an integer with Numpy?


code:
def read_only_lines(f, start, finish):
    for ii,line in enumerate(f):
        if ii>=start and ii<finish:
            yield line
        elif ii>=finish:
            return

f = open("big text file.txt", "r")
for line in read_only_lines(f, 17, 34):
    print line

rawDescription: Memory-efficent way to iterate over part of a large file


code:
#!/usr/bin/python
# I use /usr/bin/env python; but making SO's syntax highlighter happy.

class generic_form:
    def __init__(self, element_list):
        self.form_elements = element_list
        self.contents= dict()

    def fill_it_in(self):
        for prompt in self.form_elements:
            self.contents[prompt] = raw_input(prompt)

    def get(self, item):
        return self.contents[item]

    def print_it(self):
        for each in self.form_elements:
            print each, self.contents[each]

if __name__ == '__main__':
    sample_fields = ("Given Name: ",
                     "Surname: ",
                     "Date of Birth: ",
                     "Notes: ")

    example = generic_form(sample_fields)

    print "Fill in my form:"
    example.fill_it_in()

    print
    print "Please review your input:"
    example.print_it()

    # store(:%s, %s: %s" % (example.get('Surname: '), \
    #    example.get('Given Name: '), example.get('Notes: '))

rawDescription: Reading a series of input / output in Python


code:
from datetime import datetime
from dateutil import rrule
import itertools as it
from collections import Counter

rule = rrule.rrule(rrule.DAILY,
                   dtstart=datetime(2004, 01, 01),
                   until=datetime(2005, 10, 01))

dict(Counter(d.strftime('%A') for d in rule))
# {'Friday': 92,
#  'Monday': 91,
#  'Saturday': 92,
#  'Sunday': 91,
#  'Thursday': 92,
#  'Tuesday': 91,
#  'Wednesday': 91}

dict(Counter(k for k,v in it.groupby(d.strftime('%B') for d in rule)))
# or
dict(Counter(k for k,v in it.groupby(rule, key=lambda x: x.strftime('%B'))))

# {'April': 2,
#  'August': 2,
#  'December': 1,
#  'February': 2,
#  'January': 2,
#  'July': 2,
#  'June': 2,
#  'March': 2,
#  'May': 2,
#  'November': 1,
#  'October': 2,
#  'September': 2}

rawDescription: Number of particular months/weekdays between two dates


code:
import os
import fnmatch

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

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


code:
import xml.etree.ElementTree as ET

s = '''<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
    </country>
</data>'''

tree = ET.fromstring(s)

# I use iterfind, you can use whatever method to locate this node
for node in tree.iterfind('.//country[@name="Liechtenstein"]/year'):
    # this will alter the "year"'s text to '2015'
    node.text = '2015' # Please note it has to be str '2015', not int like 2015

print ET.tostring(tree)

rawDescription: Python XML Element Replacement

description: Python XML Element Replacement


rawDescription: How can I calculate the variance of a list in python?

description: Calculate the variance of a list in python


code:
from datetime import date
from itertools import groupby

def keyfunc(line):
    global key
    row = map(int, line.strip().split())
    if len(row) == 2:
        month, year = row
        key = date(year, month, 1)
    return key

def read_file(fname):
    with open(fname, 'r') as f:
        for rec_date, lines in groupby(f, keyfunc):
            data = []
            for line in lines:
                line = map(int, line.strip().split())
                if len(line) == 2:
                    continue
                data.extend(line)
            yield rec_date, data

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

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

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


code:
import itertools

for v1, v2 in itertools.combinations(vect, 2):
    a = np.array(v1)
    b = np.array(v2)
    space = np.linalg.norm(b - a)
    print space

rawDescription: How to avoid quadratic computation resulting from double 'for loop' when computing distances between vectors

description: Avoid quadratic computation resulting from double 'for loop' when computing distances between vectors


code:
st = "\B1\B1xxA1xxMdl1zzInoAEROzzMofIN"
idx = st.rindex("\\")
print st[idx+1:]

rawDescription: regex in python 2.4

description: Regex in python 2.4


code:
from ctypes import cdll, windll, c_char_p
c = cdll.msvcrt
k = windll.kernel32
LC_ALL = 0  # from locale.h
# reference: https://msdn.microsoft.com/en-US/library/x99tb11d.aspx
c.setlocale.restype = c_char_p
result = c.setlocale(LC_ALL, '.1252')
assert result == 'English_United States.1252', result

from ctypes import create_string_buffer
# cp1252 is classified as "multi-byte" by the msapi along with utf8
mb = create_string_buffer(1)
wc1 = create_string_buffer(2)
wc2 = create_string_buffer(2)

print 'IN | MSVC  KERN'
print '---+-----------'
for b in range(0x80, 0xA0):
    mb.value = chr(b)

    # reference: https://msdn.microsoft.com/en-us/library/yk02bkxb.aspx
    result = c.mbtowc(wc1, mb, 1)
    assert result == 1, result

    # reference:
    #     https://msdn.microsoft.com/en-us/library/windows/desktop/dd319072.aspx
    result = k.MultiByteToWideChar(1252, 0, mb, 1, wc2, 1)
    assert result == 1, result

    print '%02X | %02X%02X  %02X%02X' % (
        ord(mb.value),
        # little-endian:
        ord(wc1.raw[1]), ord(wc1.raw[0]),
        ord(wc2.raw[1]), ord(wc2.raw[0]),
    )

rawDescription: Windows: directly examine cp1252

description: Windows: directly examine cp1252


code:
import requests
endpoint = 'http://data.alexa.com/data'
qparams = (
    ('cli', 10),
    ('url', 'www.google.com'),
)
response = requests.get(endpoint, params=qparams)
response.url
print response.content

rawDescription: Python 'requests' adding parameters in the wrong order


code:
import gdb

def _type_is_container(t):
    return t.code == gdb.TYPE_CODE_STRUCT

class WZD(gdb.Command):
    '''print fields of a struct: wzd struct_object

Iterate through the fields of a struct, and display
a human-readable form of the objects.'''

    def __init__(self):
        gdb.Command.__init__(self, "wzd", gdb.COMMAND_DATA, gdb.COMPLETE_SYMBOL, True)

    def invoke(self, arg, from_tty):

        arg_list = gdb.string_to_argv(arg)
        if len(arg_list) < 1:
            print "usage: wzd struct"
            return

        n = arg_list[0]
        l = gdb.parse_and_eval(arg_list[0])
        (t, m) = (l.type, l.type.tag)

        print "  variable %s " % n, " type %s " % t

        if l.type.code == gdb.TYPE_CODE_STRUCT:
            print "Found a struct  %s " % n
            self._print_fields(n, t)
        else:
            print "Found no struct"

    def _print_fields(self, n, typeobject):
        print typeobject
        flds = typeobject.fields()
        for x in flds:
            sn = n + "." + x.name
            if _type_is_container(x.type):
                tag_msg = ', tag: %r' % (x.type.tag,)
            else:
                tag_msg = ''
            print '  field %r type %s (code: %s%s)' % (sn, x.type, x.type.code, tag_msg)
            if _type_is_container(x.type):
                print "Found sub level struct  %s " % sn
                sl = gdb.parse_and_eval(sn)
                sm = sl.type.tag
                st = sl.type
                self._print_fields(sn, x.type)

    def _deep_items (self, type_):
        for k, v in type_.iteritems():
            if k:
                print " k v %s " % k , " %s " % v
            else:
                print "   v    ",      " %s " % v

WZD()

rawDescription: gdb python : Can anyone explain me how to use this script written in this post?

description: Gdb python : Can anyone explain me how to use this script written in this post


code:
import numpy as np
data1 = np.loadtxt("1.txt", dtype=np.object, delimiter=",")
data2 = np.loadtxt("2.txt", dtype=np.object, delimiter=",")
print data1

rawDescription: How can I load a csv file and store its contents into an (numpy) array in python?

description: Load a csv file and store its contents into an (numpy) array in python


code:
parseResults = grammar.parseString("123,084040,ABC,A,231108")
datetime = parseResults.datetime
print datetime.dump()

rawDescription: How to group tokens placed in different positions using pyparsing?

description: Group tokens placed in different positions using pyparsing


code:
from lxml import html

data = """

if test = true
    foo;
    bar;
endif

"""

tree = html.fromstring(data)

p = tree.find('.//p')
p.text += '\n'

for element in tree.xpath('.//br'):
    element.tail += '\n'

print html.tostring(tree)

rawDescription: How to modify XML as text in lxml

description: Modify XML as text in lxml


code:
for n1, n2 in G.edges_iter():
    print G.node[n1]['dob'], G.node[n2]['dob']
    print G.node[n1]['pob'], G.node[n2]['pob']
    # Etc.

rawDescription: Storing and Accessing node attributes python networkx


code:
def handle_zip(name):
    print name, 'is a zip file'
    return 'zip'

def handle_7z(name):
    print name, 'is a 7z file'
    return '7z'

rawDescription: python: Organizing object model of an application

description: Python: Organizing object model of an application


code:
from BeautifulSoup import BeautifulSoup

page = """

1

2

3

4

5

6

7
"""

def cond(x):
    if x:
        return x.startswith("class1") and not "class2 class3" in x
    else:
        return False

soup = BeautifulSoup(page)
rows = soup.findAll('tr', {'class': cond})

for row in rows:
    print row

rawDescription: Selecting specific <tr> tags with BeautifulSoup


code:
# downloading
f, metadata = client.get_file_and_metadata('/magnum-opus.txt')
for line in f:
    print line
f.close()

rawDescription: Read and Write Dropbox Files without External Files


code:
for order in userorders[1]:
  for item in orders[order]:
    print items[item]

rawDescription: Re-organize a list using python with 1 to many relation


code:
import itertools

def permutations(tup, limit=10):
    for candidate in itertools.product([0,1], repeat=limit):
        segment_lengths = [len(list(b)) for a,b in itertools.groupby(candidate) if a == 1]
        if tup == tuple(segment_lengths):
            yield candidate

for seq in permutations((4, 3), 10):
    print seq

rawDescription: fill in list in multiple steps

description: Fill in list in multiple steps


code:
with open('day_temps.txt', 'w') as handle:
    handle.write("10.3,10.1,9.9,9.9,9.8,9.6,9.0,10.1,10.2,11.1")

def get_stats(file_name):
    with open(file_name, 'r') as handle:
        numbers = map(float, handle.read().split(','))

    return min(numbers), max(numbers), sum(numbers) / len(numbers)

if __name__ == '__main__':
    stats = get_stats('day_temps.txt')
    print "({0:.5}, {1:.5}, {2:.5})".format(*stats)

rawDescription: Why I can't read anything with File.open in python when I open the file to write first?


code:
from subprocess import check_output
print check_output(["dpkg", "-s" , "r-base"])

rawDescription: Check if R is installed from python

description: Check if R is installed from python


code:
if 'foobar'.startswith('foo'):
    print "it does!"

rawDescription: strncmp in python

description: Strncmp in python


code:
def loading_values(csvfile):
    f=open(csvfile, 'r')
    results = []
    for line in f:
        numbers = list(map(lambda x: x.strip(), line.split(',')))
        results.append((numbers[0], numbers[1:]))

    return results

print loading_values(`statistics.csv`)

rawDescription: Loading data from a csv file and display in list of tuples


code:
import sqlparse
from sqlparse import tokens

queries = '''
CREATE FUNCTION func1(a integer) RETURNS void
    LANGUAGE plpgsql
        AS $$
        BEGIN
                -- comment
       END;
       $$;
SELECT -- comment
* FROM -- comment
TABLE foo;
-- comment
INSERT INTO foo VALUES ('a -- foo bar');
INSERT INTO foo
VALUES ('
a
-- foo bar'
);

'''

IGNORE = set(['CREATE FUNCTION',])  # extend this

def _filter(stmt, allow=0):
    ddl = [t for t in stmt.tokens if t.ttype in (tokens.DDL, tokens.Keyword)]
    start = ' '.join(d.value for d in ddl[:2])
    if ddl and start in IGNORE:
        allow = 1
    for tok in stmt.tokens:
        if allow or not isinstance(tok, sqlparse.sql.Comment):
            yield tok

for stmt in sqlparse.split(queries):
    sql = sqlparse.parse(stmt)[0]
    print sqlparse.sql.TokenList([t for t in _filter(sql)])

rawDescription: Howto clean comments from raw sql file

description: Howto clean comments from raw sql file


code:
if not all(x in "agct " for x in Var1.lower()):
    print "Please input valid DNA sequence"
    sys.exit(1)

rawDescription: How to validate user input in python

description: Validate user input in python


code:
for key, value in sorted(myDict.items(), key=lambda e: e[1][2]):
  print key, value

rawDescription: Python: sorting a dictionary of lists

description: Python: sorting a dictionary of lists


code:
from django.db.models import Count
all_countries = Country.objects.annotate(Count('story')).order_by('-story__count')

for country in all_countries:
    print "Country %s (%s)" % (country.name, country.story__count)
    all_cities = City.objects.filter(country = country).annotate(Count('story')).order_by('-story__count')
    for city in all_cities:
        print "\tCity %s (%s)" % (city.name, city.story__count)

rawDescription: Django: output aggregation of aggregation ordered by counts


code:
from operator import itemgetter
from itertools import groupby
data = [2, 3, 4, 5, 12, 13, 14, 15, 16, 17]
for k, g in groupby(enumerate(data), lambda (i,x):i-x):
    print map(itemgetter(1), g)

rawDescription: Identify groups of continuous numbers in a list


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


code:
import requests

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

# call
consumePOSTRequestSync()

rawDescription: Python REST Consumption

description: Python REST Consumption


code:
my_dict={'account_0':123445,'seller_account':454545,'seller_account_0':454676, 'seller_account_number':3433343}

for key, value in my_dict.iteritems():   # iter on both keys and values
        if key.startswith('seller_account'):
                print key, value

rawDescription: Python dictionary search values for keys using regular expression


code:
x = Counter(3,8)
for i in x:
    print i

rawDescription: Iterator (iter()) function in Python

description: Iterator (iter()) function in Python


code:
from itertools import tee, izip_longest
in1, in2 = tee(sys.stdin, 2)
next(in2)
pairs = izip_longest(in1, in2, fillvalue='')
res = (line for line, prevline in pairs
       if not line.startswith('---(more') and not prevline.startswith('---(more'))
for line in res:
    print line

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


code:
def circular_crosscorelation(X, y):
    """
    Input:
        symbols for X [n, m]
        and y[m,]

    Returns:
        symbol for circular cross corelation of each of row in X with
        cc[n, m]
    """
    n, m = X.shape
    corr_expr = T.signal.conv.conv2d(X, y[::-1].reshape((1, -1)), image_shape=(1, m), border_mode='full')
    corr_len = corr_expr.shape[1]
    pad = m - corr_len%m
    v_padded = T.concatenate([corr_expr, T.zeros((n, pad))], axis=1)
    circ_corr_exp = T.sum(v_padded.reshape((n, v_padded.shape[1] / m, m)), axis=1)
    return circ_corr_exp[:, ::-1]

X = T.dmatrix()
y = T.dmatrix()
cc = theano.function([X, y], circular_crosscorelation(X, y))
print cc( np.array([[2, 3, 5], [4, 5, 6]]), np.array([[7, 11, 13]]) )

rawDescription: Circular Correlation in Theano

description: Circular Correlation in Theano


code:
from selenium import webdriver
import time

browser = webdriver.Firefox()
browser.get("http://www.prudential.com.hk/PruServlet?module=fund&purpose=searchHistFund&fundCd=MMFU_U")

elem = browser.find_element_by_xpath('//*[@id="disclaimer"]/div/div')
elem.click()
time.sleep(0.2)

elem = browser.find_element_by_xpath("//*")
print elem.get_attribute("outerHTML")

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


code:
import urllib,json
data = urllib.urlencode({'files':json.dumps(file_dict)}) #file_dict has the list of stats about the files
response = urllib.urlopen('http://example.com/json/', data)
print response.read()

rawDescription: How do I post a lot of data to Django?

description: Post a lot of data to Django


code:
for match in repeat.finditer("a-bb-cccccccc"):
    print match.group(), match.start(), match.end()

rawDescription: Python split string by pattern

description: Python split string by pattern


code:
from itertools import chain, imap, product

alphabet = "ab"

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

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


code:
posts = (Post
         .select(Post.post_tile,User.user_name,Act.act_title)
         .join(Act)
         .switch(Post)
         .join(User)
         .where(Act.id==actId))
for post in posts:
    print post.post_title, post.user.user_name, post.act.act_title

rawDescription: Get the foreign key field using Peewee ORM


code:
import os
import os.path
import itertools

#creates dummy files with a given number of bytes.
def create_file(name, size):
    if os.path.isfile(name): return
    file = open(name, "w")
    file.write("X" * size)
    file.close()

#create some sample files
create_file("foo.txt", 4)
create_file("bar.txt", 4)
create_file("baz.txt", 4)
create_file("qux.txt", 8)
create_file("lorem.txt", 8)
create_file("ipsum.txt", 16)

#get the filenames in this directory
filenames = [filename for filename in os.listdir(".") if os.path.isfile(filename)]

#sort by size
filenames.sort(key=lambda name: os.stat(name).st_size)

#group by size and iterate
for size, items_iterator in itertools.groupby(filenames, key=lambda name: os.stat(name).st_size):
    items = list(items_iterator)
    print "{} item(s) of size {}:".format(len(items), size)
    #insert hashlib code here, or whatever else you want to do
    for item in items:
        print item

rawDescription: Group files by size, then find hash duplicates


code:
from htmlmin import minify
html=u"
      Hello     World
"
minified_html = minify(html)
print minified_html

rawDescription: How to remove insignificant whitespace in lxml.html?

description: Remove insignificant whitespace in lxml.html


code:
source
import multiprocessing as mp, subprocess

def run_script(script_name):
    curproc = mp.current_process()
    cmd = ['python', script_name]
    print curproc, 'start:', cmd
    try:
        return subprocess.check_output(
            cmd, shell=False)
    except subprocess.CalledProcessError as err:
        print '{} error: {}'.format(
            curproc, dict(
                status=err.returncode,
                command=cmd,
            )
        )
    finally:
        print curproc, "done"

scripts = ['zhello.py', 'blam']

pool = mp.Pool()   # default: num of CPUs
print pool.map(
    run_script, scripts,
)
pool.close()
pool.join()

rawDescription: Python Multiprocessing Object/Function to replace Bash &/GNU Screen?


code: print "The temperature ", day, "December was:", December[day - 1], "celsius"

rawDescription: Using a for loop while maintaining list object


code:
for span in soup.find_all('span', {'class': 'sr_gs_rackrate_total'}):
  b = span.parent
  b.span.extract()
  print b.text

rawDescription: How to search for string with beautiful soup

description: Search for string with beautiful soup


code:
your_list = [
    'Australia/AU/36/8177/22919',
    'Austria/AT/40/6936/8429',
    'Azerbaijan/AZ/31/2896/9421'
    'Bahamas/BS/44/79/351'
]

for element in your_list:
    print element

rawDescription: How to use a loop to print out data from a list

description: Use a loop to print out data from a list


code:
def foo(lst):
    d = {}
    for (k,v) in lst: d[k] = d.get(k,0) + v     # Accumulate
    return max(d.items(), key=lambda x: x[1])   # Find max value, return corresponding tuple

print foo([(1,200),(1,50),(2,275)])             # (2, 275)
print foo([(1,100),(2,50),(2,300),(1,1000)])    # (1, 1100)

rawDescription: Python, in a list of (a,b) pairs find the a value with the largest sum(b values)


code:
import os
for dirpath, dirnames, filenames in os.walk('.'):
    for f in filenames:
        print os.path.join(dirpath, f)

rawDescription: Print out all files in current directory and subdirectories in Python


code:
def decorator1(func):
    def wrapper1(a1):
        a2 = "hello from decorator 1"
        func(a1, a2)
    return wrapper1

def decorator2(func):
    def wrapper2(a1, a2):
        a3 = "hello from decorator 2"
        func(a1, a2, a3)
    return wrapper2

@decorator1
@decorator2
def my_func(a1, a2, a3):
    print a1, a2, a3

my_func("who's there?")

# Prints:
# who's there?
# hello from decorator 1
# hello from decorator2

rawDescription: daisy-chaining Python/Django custom decorators

description: Daisy-chaining Python/Django custom decorators


code:
import re
patt = 'not\s+(.+\s)?poor'
s = 'I am not that poor'
sub_s = re.sub(patt, 'rich', s)
print s, '->', sub_s
s2 = 'I am not poor'
sub_s2 = re.sub(patt, 'rich', s2)
print s2, '->', sub_s2
s3 = 'I am poor not'
sub_s3 = re.sub(patt, 'rich', s3)
print s3, '->', sub_s3

rawDescription: Replacing a word with another in a string if a condition is met


code:
def parse(string):
    front, paren, back = string.partition('(')
    return [front, paren + back]

def parse_many(string):
    return [parse(s) for s in string.split()]

aString = "foo1(bar1(foo2bar2)) foo3(bar3)"
print parse_many(aString)

rawDescription: Finding a string of varying length within parentheses that contain parentheses


code:
@matryoshka
class outer(object):

    answer = 42

    class inner(object):

        def __call__(self):
            print self.answer

outer.inner()()   # 42

rawDescription: Nested Python class needs to access variable in enclosing class


code: print url, len(response.read())

rawDescription: Is there a better way to retrieve webpage sizes with Python?


code:
from itertools import *
from operator import ne

def f1(list1, list2, enumerate=enumerate, izip=izip):
    out = []
    out_append = out.append
    for e1, e2 in izip(list1, list2):
        if e1 == e2:
            out_append(e1)
        else:
            break
    return out

def f2(list1, list2, list=list, takewhile=takewhile, izip=izip):
    return [i for i, j in takewhile(lambda (i,j):i==j, izip(list1, list2))]

def f3(list1, list2, next=next, compress=compress, count=count, imap=imap,
       ne=ne):
    return list1[:next(compress(count(), imap(ne, list1, list2)), 0)]

def f4(list1, list2):
    out = []
    out_append = out.append
    i = 0
    end = min(len(list1), len(list2))
    while i < end and list1[i]==list2[i]:
        out_append(list1[i])
        i+=1
    return out

def f5(list1, list2, len=len, enumerate=enumerate):
    if len(list1) > len(list2):
        list1, list2 = list2, list1
    for i, e in enumerate(list1):
        if list2[i] != e:
            return list1[:i]
    return list1[:]

def f6(list1, list2, enumerate=enumerate):
    result = []
    append = result.append
    for i,e in enumerate(list1):
        if list2[i] == e:
            append(e)
            continue
        break
    return result

from timeit import timeit
list1 =[1,2,3,4,5,6];list2=[1,2,4,3,5,6]
sol = f3(list1, list2)

for func in 'f1', 'f2', 'f3', 'f4', 'f5', 'f6':
    assert eval(func + '(list1, list2)') == sol, func + " produces incorrect results"
    print func
    print timeit(stmt=func + "(list1, list2)", setup='from __main__ import *')

rawDescription: The fastest way to find common elements at the beginning of 2 python lists?


code:
import sys
from cStringIO import StringIO

class PatchStdin(object):
    def __init__(self, value):
        self._value = value
        self._stdin = sys.stdin
    def __enter__(self):
        # Monkey-patch stdin
        sys.stdin = StringIO(self._value)
        return self
    def __exit__(self, typ, val, traceback):
        # Undo the monkey-patch
        sys.stdin = self._stdin

# Usage
with PatchStdin('Dan'):
    name = raw_input('What is your name? ')
    print 'Hello, %s' % name

rawDescription: How to feed in an answer to input in script in Python

description: Feed in an answer to input in script in Python


code:
import re

# this is the same data just copy/pasted from your question
data = """    ahi1
    b/se
ahi
test    -2.435953
        1.218364
    ahi2
    b/se
ahi
test    -2.001858
        1.303935"""

# what we're gonna do, is search through it line-by-line
# and parse out the numbers, using regular expressions

# what this basically does is, look for any number of characters
# that aren't digits or '-' [^-\d]  ^ means NOT
# then look for 0 or 1 dashes ('-') followed by one or more decimals
# and a dot and decimals again: [\-]{0,1}\d+\.\d+
# and then the same as first..
pattern = re.compile(r"[^-\d]*([\-]{0,1}\d+\.\d+)[^-\d]*")

results = []
for line in data.split("\n"):
    match = pattern.match(line)
    if match:
        results.append(match.groups()[0])

pairs = []
i = 0
end = len(results)
while i < end - 1:
    pairs.append((results[i], results[i+1]))
    i += 2

for p in pairs:
    print "%s, %s" % (p[0], p[1])

rawDescription: Python: How to extract floating point numbers from a text file with mixed content?


code:
import datetime
tasks = {'ID1':[[datetime.datetime( 2015, 1, 1), datetime.datetime( 2015, 1, 2)], [datetime.datetime( 2015, 10, 1), None]],
         'ID2':[[datetime.datetime( 2015, 1, 1), datetime.datetime( 2015, 1, 3)]]
        }

projects = [['ID1'], ['ID2'], ['ID3']]
projects_dict = {}

for key, item in enumerate(projects):
    projects_dict[item[0]] = []
    if item[0] not in tasks:
        projects_dict[item[0]].append(None)
        projects_dict[item[0]].append(None)
    else:
        start_date = [x[0] for x in tasks[item[0]]]
        if start_date.count(None) != 0: #checks to see if there is a None value
            projects_dict[item[0]].append(None)
        else:
            projects_dict[item[0]].append(min(start_date))
        end_date = [x[1] for x in tasks[item[0]]]
        if end_date.count(None) != 0: #checks to see if there is a None value
            projects_dict[item[0]].append(None)
        else:
            projects_dict[item[0]].append(max(end_date))

print projects_dict

rawDescription: pulling min and max values from list of list

description: Pulling min and max values from list of list


code:
weapons = 'apples', 'bananas', 'oranges', 'pears', 'coconuts',
for i, item in enumerate(item for item in weapons if item != 'bananas'):
    print "%s : %s"%(i, item)

rawDescription: Most pythonic way enumerating broken a table?


code:
import nltk

# create some text and tokenize it
text = "This is a text. Written for test purposes, this is a text."
tokens = nltk.wordpunct_tokenize(text)

# initialize finder object with the tokens
finder = nltk.collocations.BigramCollocationFinder.from_words(tokens)

# build a dictionary with bigrams and their frequencies
bigram_measures = nltk.collocations.BigramAssocMeasures()
scored = dict(finder.score_ngrams(bigram_measures.raw_freq))

# build a word list
words = ['test', 'This']

# create the filter...
myfilter = create_filter_minfreq_inwords(scored, words, 0.1)

print 'Before filter:\n', list(finder.score_ngrams(bigram_measures.raw_freq))

# apply filter
finder.apply_ngram_filter(myfilter)

print '\nAfter filter:\n', list(finder.score_ngrams(bigram_measures.raw_freq))

rawDescription: Combining filters in nltk collocations

description: Combining filters in nltk collocations


code:
from bs4 import BeautifulSoup

html = '''

 test

'''

content = BeautifulSoup(html)

for div in content.findAll('div', attrs={'class':'category5'}):
    print div.text

test

rawDescription: BeautifulSoup: How to get nested divs

description: BeautifulSoup: How to get nested divs


code:
def split_submatrix(x,submat_shape,order='C'):
    p,q = submat_shape      # Store submatrix shape
    m,n = x.shape

    if np.any(np.mod(x.shape,np.array(submat_shape))!=0):
        raise Exception('Input array shape is not divisible by submatrix shape!')

    if order == 'C':
        x4D = x.reshape(-1,p,n/q,q).transpose(0,2,1,3).reshape(-1,p,q)
        return np.array_split(x4D,x.size/(p*q),axis=0)

    elif order == 'F':
        x2D = x.reshape(-1,n/q,q).transpose(1,0,2).reshape(-1,q)
        return np.array_split(x2D,x.size/(p*q),axis=0)

    else:
        print "Invalid output order."
        return x

rawDescription: How to get all submatrices of given size in numpy?

description: Get all submatrices of given size in numpy


rawDescription: What is python's equivalent of Matlab's ranksum?


code:
import urllib2
import json
import time

def getEntity( entityText ):
    url = 'https://maps.googleapis.com/maps/api/geocode/json?address=%s' % urllib2.quote(entityText)

    response = urllib2.urlopen(url)
    jsonaddress = json.loads(response.read())

    time.sleep(0.2)

    if jsonaddress['status'] == 'OK':
        return jsonaddress['results'][0]['types'][0]
    else:
        return None

with open('a.txt') as f:
    for line in f:
        entityText = line.strip()
        entity = getEntity( entityText )
        print entityText, entity

rawDescription: Searching online from text file with python checking if the names are cities in a specific country


code:
find = r'C:\\somedir\\(.*?\.log)'
repl = r'C:\\somedir\\logs\\\1'
print re.sub(find,repl,r'random text ... C:\somedir\test.log more \nrandom \ntext C:\somedir\test.xls...')

rawDescription: Python Regular Expression Question

description: Python Regular Expression Question


code:
#!/usr/bin/env python
import sys, re, os, glob

path = "./"
files = os.listdir(path)
for finding in glob.glob('*.txt'):
    file = os.path.join(path, finding)
    text = open(file, "r")
    CH = []
    doPayAttention = False

    for line in text:
        if re.match("Texts which I want to specify", line):
            doPayAttention = True
            continue
        if not doPayAttention:
            continue
        mm = re.match(r"^\s*XX\s+\S+\s+(\S+)\s*$", line)
        if mm is not None:
            CH.append(mm.group(1))
    CH = sorted(CH, reversed=True)
    for _ch in CH:
        print 'XX ', _ch

rawDescription: finding some values after the specified text in python

description: Finding some values after the specified text in python


code:
import calendar

cal = calendar.Calendar()

fridays = set() # Constructs a new, empty set

# build a list of months, which are a list of weeks, etc.
months = [cal.monthdatescalendar(2014, month) for month in range(1, 13)]
for month in months:
    for week in month:
        for day in week:
            if day.weekday() == 4 and day.year == 2014:
                fridays.add(day)

for friday in sorted(fridays): # iterate through sorted fridays
    print friday

rawDescription: iterating over yeardatescalendar repeats dates

description: Iterating over yeardatescalendar repeats dates


code:
source
import threading, time
from random import random

class Child(threading.Thread):
    def run(self):
        print "\t%s: child doing child stuff" % self
        # sleep 1-3 seconds
        time.sleep( 1. + random()*2 )

nchildren = 3
tpool = []

for _ in range(4):
    diff = nchildren - len(tpool)
    if diff < 1:
        print '* okay'
    else:
        print '* starting {} children'.format(diff)
        new_children = [ Child() for _ in xrange(diff) ]
        for thread in new_children:
            thread.start()
        tpool.extend( new_children )

    time.sleep(1)
    print '* scan...',
    tpool = [ thread for thread in tpool
              if thread.isAlive()
              ]
    print '{} children'.format(len(tpool))

rawDescription: Parent process clean up thread that raised exception


code:
import sys
for i in sys.path:
    print i

rawDescription: How come when I move a python file into scipy.stats folder in ubuntu it disappears?


code:
import random
import time

x=random.sample(xrange(60), 60) #create randomly ordered list of 1s with size 60

d={}  #create empty dictionary
i=0

with open('data.txt') as f:
    for letter in f:
        letter=letter.rstrip()
        d[letter]=x[i]    #assign timeslots to letters
        i+=1

print d    #print dictionary

j=1
with open('data.txt') as f:
    while j<=60:
        time.sleep(1)     #sleep for one second
        for key, value in d.items():  #check if the timeslots are reached
            if value == j:
                print key, j
        j+=1

rawDescription: Python script loop to assign timeslot to each line in input file


code:
x, y = (1, 2)
print x
print y
# 1
# 2

rawDescription: Breaking down instances of classes into lists, or: a better way to sum class variables?


code:
from pprint import pprint
print "Likes:"
pprint(likes,indent=4)
print "Dislikes:"
pprint(dislikes,indent=4)

rawDescription: Parsing a pretty printed table into Python objects


code:
from PySide import QtCore, QtGui

class Communicate(QtCore.QObject):
    signal = QtCore.Signal(str)

class label(QtGui.QLabel):
    def __init__(self, parent=None):
        super(label, self).__init__(parent)
        self.show()

    def labelFunction(self, arg=None):
        print '...labelFunction: arg = %s'%arg

class button(QtGui.QPushButton):
    def __init__(self, callback=None, parent=None):
        super(button, self).__init__(parent)
        self.callback=callback
        self.clicked.connect(self.click)
        self.show()

    def click(self):
        connector=Communicate()
        connector.signal.connect(self.callback)
        connector.signal.emit("Hello World")

lbl = label()
btn = button(callback=lbl.labelFunction)

rawDescription: Signals And Slots: When simple example is not working


code:
def midpoint(edge):
    x1 = edge[0][0]
    y1 = edge[0][9]
    x2 = edge[1][0]
    y2 = edge[1][10]

    mid_x = x1+((x2-x1)/2.0)
    mid_y = y1+((y2-y1)/2.0)

    return (mid_x, mid_y)

def ccw(A,B,C): # from http://www.bryceboe.com/2006/10/23/line-segment-intersection-algorithm/
    return (C[1]-A[1])*(B[0]-A[0]) > (B[1]-A[1])*(C[0]-A[0])

def intersect(segment1, segment2): # from http://www.bryceboe.com/2006/10/23/line-segment-intersection-algorithm/
    A = segment1[0]
    B = segment1[1]
    C = segment2[0]
    D = segment2[1]
    # Note: this doesn't catch collinear line segments!
    return ccw(A,C,D) != ccw(B,C,D) and ccw(A,B,C) != ccw(A,B,D)

def points_from_edges(edges):
    point_set = set()
    for i in range(0,len(edges)):
          point_set.add(edges[i][0])
          point_set.add(edges[i][11])

    points = []
    for point in point_set:
          points.append({'x':point[0], 'y':point[1]})

    return list(points)

def centroids_for_points_and_edges(points, edges):

    centroids = []

    # for each voronoi_node,
    for i in range(0,len(points)):
        cell_edges = []

        # for each edge
        for j in range(0,len(edges)):
            is_cell_edge = True

            # let vector be the line from voronoi_node to the midpoint of edge
            vector = (points[i],midpoint(edges[j]))

            # for each other_edge
            for k in range(0,len(edges)):

                # if vector crosses other_edge
                if (k != j and intersect(edges[k], vector)):
                    # edge is not in voronoi_node's polygon
                    is_cell_edge = False
                    break

            # if the vector didn't cross any other edges, it's an edge for the current node
            if (is_cell_edge):
                cell_edges.append(edges[j])

        # find the hull for the cell
        convex_hull = convex_hull_for_polygon(points_from_edges(cell_edges))

        # calculate the centroid of the hull
        centroids.append(centroid_for_polygon(convex_hull))

    return centroids

edges = [
  ((10,  200),(30,  50 )),
  ((10,  200),(100, 140)),
  ((10,  200),(200, 180)),
  ((30,  50 ),(100, 140)),
  ((30,  50 ),(150, 75 )),
  ((30,  50 ),(200, 10 )),
  ((100, 140),(150, 75 )),
  ((100, 140),(200, 180)),
  ((150, 75 ),(200, 10 )),
  ((150, 75 ),(200, 180)),
  ((150, 75 ),(220, 80 )),
  ((200, 10 ),(220, 80 )),
  ((200, 10 ),(350, 100)),
  ((200, 180),(220, 80 )),
  ((200, 180),(350, 100)),
  ((220, 80 ),(350, 100))
]

points = [
  (50,130),
  (100,95),
  (100,170),
  (130,45),
  (150,130),
  (190,55),
  (190,110),
  (240,60),
  (245,120)
]

centroids = centroids_for_points_and_edges(points, edges)
print "centroids:"
for centroid in centroids:
    print "  (%s, %s)" % (centroid['x'], centroid['y'])

rawDescription: Finding centre of a polygon using limited data


code:
payload = {'f':'get_email_address'}
req2 = s.get('http://api.guerrillamail.com/ajax.php', params=payload)
print req2.text #prints out the current email address

rawDescription: Pass JSON function arguements using requests


code:
flagged, good = [],[]
splitters = ['-', ',', '/']
for d in bad_dates:
    try:
        a = None
        for s in splitters:
            if len(d.split(s)) == 3:
                a = parse(d)
                good.append(a)
        if not a:
            raise Exception
    except Exception, err:
        flagged.append(d)

rawDescription: formatting really inconsistent dates with python

description: Formatting really inconsistent dates with python


rawDescription: How to match a number in middle using python regular expression?

description: Match a number in middle using python regular expression


code:
inNum = int(input("Please enter a number.  0 to quit."))
minNum = inNum
maxNum = inNum
theSum = inNum
while inNum != 0:
    inNum = int(input("Please enter a number.  0 to quit."))
    if inNum == 0:
        break
    minNum = minimum(minNum, inNum)
    maxNum = maximum(maxNum, inNum)
    theSum += inNum
print minNum, maxNum, theSum

rawDescription: Implementation of a function that finds the minimum of two values


code:
import re

s = '<a href="http://www.etf.rs/" target="_top">'
print re.search('"http://(.*)"\s', s).group(1)

rawDescription: Find string between 2 chars regex python


code:
class myobject:
    def __init__(self, i):
        self.text = 'hello ' + str(i)

def mylist():
    for i in range(5):
        elem = myobject(i)
        yield elem

for obj in mylist():
    print obj.text

rawDescription: iterate over a list as iterator in python

description: Iterate over a list as iterator in python


code:
import urllib2
from BeautifulSoup import BeautifulSoup

page = urllib2.urlopen('http://finance.yahoo.com/q/ks?s=NATI%20Key%20Statistics').read()

soup = BeautifulSoup(page)

all_data = soup.findAll('td', "yfnc_tabledata1")

print all_data[2].getText()

# or - for more elements

for element in all_data[0:3]:
    print element.getText()

# write to CSV

import csv

with open("results.csv", "wb") as f:
    writer = csv.writer(f)

    writer.writerow(["header1", "header2", "header3"])

    for element in all_data[0:3]:
        writer.writerow([element.getText(), "column2", "column3"])

rawDescription: Setting a Limit With BeautifulSoup

description: Setting a Limit With BeautifulSoup


code:
import re

wikiText = """==Mainsection1==
Some text here
===Subsection1.1===
Other text here

==Mainsection2==
Text goes here
===Subsecttion2.1===
Other text goes here. """

outputArray = re.findall('(?<!=)==([^=]+)==(?!=)([\s\S]*?(?=$|(?<!=)==[^=]+==(?!=)))', wikiText)
print outputArray

rawDescription: python regex mediawiki section parsing

description: Python regex mediawiki section parsing


code:
import pickle
PIK = "pickle.dat"

data = ["A", "b", "C", "d"]
with open(PIK, "wb") as f:
    pickle.dump(data, f)
with open(PIK, "rb") as f:
    print pickle.load(f)

rawDescription: Saving and loading multiple objects in python pickle file


code: print ' '.join(soFar)

rawDescription: use .replace() on only one character of a string with repetitive characters

description: Use .replace() on only one character of a string with repetitive characters


code:
from lxml import etree
doc = etree.XML("""
<Group id="1" name="The Perfect Life"
    username="root" password="mypasswd123" state="ok">
    <Server id="1" name="Machine (250)" ip="192.168.1.250" username="" password="" state="ok">
        <App id="1" type="Tomcat" state="ok" />
    </Server>
    <Server id="2" name="Machine (251)" ip="192.168.1.251" username="" password="" state="ok">
        <App id="1" type="JBoss" path="" state="ok" />
        <App id="2" type="Tomcat" path="" state="ok" />
    </Server>
    <Server id="3" name="Machine (252)" ip="192.168.1.252" username="" password="" state="ok">
        <App id="1" type="JBoss" path="" state="ok" />
        <App id="2" type="Tomcat" path="" state="ok" />
    </Server>
    <Server id="4" name="Machine (253)" ip="192.168.1.253" username="" password="" state="ok">
        <App id="1" type="JBoss" path="" state="ok" />
    </Server>
</Group>
""")

# group id from user
group_id = "1"
# unique apps under group_id
app_types = set(doc.xpath('//Group[@id=$group_id]/descendant::App/@type',group_id=group_id))
app_dict = dict((x+1,y) for (x,y) in enumerate(app_types))

print "Apps:\n\t" + "\n\t".join("%d) %s" % x for x in sorted(app_dict.items()))
app_selection = input("Select a service: ")
app_type = app_dict.get(int(app_selection))

# Servers in group_id with app_type
for server in doc.xpath('//Group[@id=$group_id]/Server[./App[@type=$app_type]]',group_id=group_id,app_type=app_type):
  print etree.tostring(server) # Don't know what you want to do per-server...

rawDescription: Printing different types of attributes and listing all App elements for the selected type in Group Element by Python


code:
##this is class number 1 and you find class 1 people in second index of list by i[1] == 1(class number)
avg_list = [[i[0], float(sum(i[2:]))/len(i[2:])] for i in class1 if i[1] == 1 ]
dd = sorted(avg_list, key=lambda x: x[1])
dd.reverse()
print dd

rawDescription: Sort tuples by averages of second element


code:
def import_by_path(path):
  if '.' not in path:
    return __import__(path, globals(), locals())

  path, module = path.rsplit('.', 1)
  mod = __import__(path, globals(), locals(), [module])

  try:
    return getattr(mod, module)
  except AttributeError:
    raise ImportError, "Could not import %s" % path

rawDescription: How to import from command line and then iterate over imported modules?

description: Import from command line and then iterate over imported modules


code:
import gevent.monkey; gevent.monkey.patch_all()

import itertools
import json
import random
import time

import bottle

@bottle.get("/foo")
def foo():
  for i in range(5):
    o = {"i": i, "r": random.random()}
    print 'sending: {}'.format(o)
    yield json.dumps(o) + "\n"
    time.sleep(1)

if __name__ == '__main__':
  bottle.run(
    host="localhost",
    port=1234,
    server="gevent"
  )

rawDescription: Imitating Twitter's Streaming API

description: Imitating Twitter's Streaming API


code:
import json

yourData = []
with open("fileName.txt") as inputData:
    for line in inputData:
        try:
            yourData.append(json.loads(line.rstrip(';\n')))
        except ValueError:
            print "Skipping invalid line {0}".format(repr(line))

print yourData

rawDescription: Load multiple dictionaries from a file

description: Load multiple dictionaries from a file


code:
#file a.py
def my_func(whichmod=None):
    if whichmod == 'b':
         print 'You are module b!'
    elif whichmod == 'c':
         print 'You are module c!'
    else:
         print 'You are not B nor C!'

#files b.py/c.py
import a
a.my_func(whichmod='b')   # or 'c' in module c

#file d.py
import a
a.my_func()

rawDescription: How to know from inside a module, during run time, which script has called a function in it

description: Know from inside a module, during run time, which script has called a function in it


code: print digit_sum(input("enter num: "))

rawDescription: Digit Sum in Python with user input

description: Digit Sum in Python with user input


code:
punctuation_marks = ".!?"
first_word = True
uppercase_words = []

for w in text:
    if not first_word and re.match("[A-Z][a-z]*$", w):
        uppercase_words.append(w)
    first_word = w in punctuation_marks

print uppercase_words

rawDescription: Change from re.findall(regex, text) to nltk.Text.findall(regex)


code:
import a
x = a.Sample()
print x.count
y = a.Sample()
print x.count

rawDescription: What's the equivalent of Ruby's class @@variable in Python?


code:
import collections
d = collections.defaultdict(dict)
data = csv.DictReader(f, delimiter="\t")
for row in data:
    d[row["item"]][row["value"]] = row["need"]
print d

rawDescription: Appending a nested dictionary for values with same key


code:
if choice_day == "monday" and choice_period == "NO":
    print '\n'.join(monday.values())

rawDescription: Shortening down printing single values in a dictionary


code:
class XAndYWithMethods(XAndYClass):
    def method(self):
        print 'doing stuff...'

rawDescription: python - cast a class to a tuple and "autobox" to a for loop

description: Python - cast a class to a tuple and "autobox" to a for loop


code:
def find_rev(str,target,start):
    str = str[::-1]
    index = str.find(target,len(str) - start)
    if index != -1:
        index = len(str) - index
    return index

def title_limit(title, limit):
    if len(title) <= limit: return title
    cut = find_rev(title, ' ', limit - 3 + 1)
    if cut != -1:
        title = title[:cut-1] + "..."
    else:
        title = title[:limit-3] + "..."
    return title

print title_limit('The many Adventures of Bob', 10) # The...
print title_limit('The many Adventures of Bob', 20) # The many...
print title_limit('The many Adventures of Bob', 30) # The many Adventures of Bob

rawDescription: Truncate a string in mako template

description: Truncate a string in mako template


code:
import inspect
def f():
  print 'ciao'

inspect.getsource(f)

rawDescription: Display function definition in interactive shell


code:
import requests
from bs4 import BeautifulSoup

url = "http://abcnews.go.com/"

r = requests.get(url)

soup = BeautifulSoup(r.content, "html.parser")

for x in range(1):
    d=soup.select("div.h a")
    for j in d:
        j = str(j)
        f = BeautifulSoup(j,'html.parser')
        while f.span:
            f.span.decompose()
        print f.text.encode('utf-8')

rawDescription: Is there a way to erase or separate web scraping data? in Python


code:
from itertools import chain

# Read CSV file, get
df = pd.read_csv("books2.csv", index_col="id")
authors_notflat = [a.split() for a in df['author']]
counter = Counter(chain.from_iterable(authors_notflat))
print counter

rawDescription: words frequency using pandas and matplotlib

description: Words frequency using pandas and matplotlib


code:
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.svm import LinearSVC
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.multiclass import OneVsRestClassifier

X_train = np.array(["new york is a hell of a town",
                    "new york was originally dutch",
                    "the big apple is great",
                    "new york is also called the big apple",
                    "nyc is nice",
                    "people abbreviate new york city as nyc",
                    "the capital of great britain is london",
                    "london is in the uk",
                    "london is in england",
                    "london is in great britain",
                    "it rains a lot in london",
                    "london hosts the british museum",
                    "new york is great and so is london",
                    "i like london better than new york"])
y_train = [[0],[0],[0],[0],[0],[0],[1],[1],[1],[1],[1],[1],[0,1],[0,1]]
X_test = np.array(['nice day in nyc',
                   'welcome to london',
                   'hello welcome to new york. enjoy it here and london too'])
target_names = ['New York', 'London']

classifier = Pipeline([
    ('vectorizer', CountVectorizer(min_n=1,max_n=2)),
    ('tfidf', TfidfTransformer()),
    ('clf', OneVsRestClassifier(LinearSVC()))])
classifier.fit(X_train, y_train)
predicted = classifier.predict(X_test)
for item, labels in zip(X_test, predicted):
    print '%s => %s' % (item, ', '.join(target_names[x] for x in labels))

rawDescription: use scikit-learn to classify into multiple categories

description: Use scikit-learn to classify into multiple categories


rawDescription: reading json wrapped status report from tinyG

description: Reading json wrapped status report from tinyG


code:
import numpy as np
from scipy.integrate import quad
from scipy.optimize import fsolve

def integrand(t, x):
    return t*(1 - x*t)

def func(x):
    y, err = quad(integrand, 0, x, args=(x,))
    return y

# Use 1.0 as the initial guess.  Note that a bad initial guess
# might generate a warning and return the degenerate solution at x=0.
sol = fsolve(func, 1.0)

print "Solution:      ", sol[0]

# The exact solution that we want is sqrt(3/2)
print "Exact solution:", np.sqrt(1.5)

rawDescription: python: fsolve with unknown inside the upper limit of an integral

description: Python: fsolve with unknown inside the upper limit of an integral