code:
def sentiment_analysis():
    fo = open("positive_words.txt", "r")
    positive_words = fo.readlines()
    fo.close()
    positive_words = map(lambda positive_words: positive_words.strip(), positive_words)
    fo = open("negative_words.txt", "r")
    negative_words = fo.readlines()
    fo.close()
    negative_words = map(lambda negative_words: negative_words.strip(), negative_words)
    fo = open("BAC.csv", "r")
    data = fo.readlines()
    fo.close()
    data = map(lambda data: data.strip(), data)
    x1 = 0 #number of bullish
    x2 = 0 #number of bearish
    x3 = 0 #number of unknown

    fo = open("C:\Users\Siddhartha\Documents\INFS 772\Project\Answer\BAC2_answer.csv", "w")
    writer = csv.writer(fo)

    for info in data:
        data_specs = info.split(',')
        time_n_date = data_specs[0]
        sentiment = data_specs[2]
        '''Possibly precede with a nested for loop for data_specs???'''
        if sentiment == 'Bullish':
            '''fo.write(time + ',' + 'Bullish' + '\n')'''
        elif sentiment == 'Bearish':
            ''' fo.write(time + ',' + 'Bearish' + '\n')'''
        else:
            x3 += 1
            positive = 0
            negative = 0
            content_words = data_specs[1].split()
            for a in positive_words:
                for b in content_words:
                    if (a == b):
                        positive = positive + 1
            for c in negative_words:
                for d in content_words:
                    if (c == d):
                        negative = negative + 1
            if positive > negative:
                '''fo.write(time + ',' + 'Bullish' + '\n')'''
                sentiment = 'Bullish'
            elif positive < negative:
                sentiment = 'Bearish'
            else:
                sentiment = 'Neutral'

        bac2data = [time_n_date, sentiment]
        print bac2data
        writer.writerow(bac2data)

    fo.close()

rawDescription: Writing a list from a for loop into a csv


code:
import re
regexp = re.compile(r'\[(\d{2}:\d{2}:\d{2}\.xxx\d{3})\][\s]+status[\s]+XYZ[\s]+ID:([0-9A-Zx]+)(.+)')

f = open("log.txt", "r")
for line in f.readlines():
    print line
    m = re.match(regexp, line)
    #print m
    if m:
        print m.groups()

rawDescription: Return All Matching Lines in a Logfile

description: Return All Matching Lines in a Logfile


code:
import subprocess
r = subprocess.call('unrar e {0}'.format(zfilename), shell=True)
print "Return code for {0}: {1}".format(zfilename, r)

rawDescription: How can I call WinRar in Python on Windows? STILL Problematic

description: Call WinRar in Python on Windows? STILL Problematic


code:
for col in dp.columns:
    print 'column', col,':', type(dp[col][0])

rawDescription: Setting column types while reading csv with pandas


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

rawDescription: Sorting a group of words inside a list

description: Sorting a group of words inside a list


code:
import hashlib

def obfuscate(s):
    m = hashlib.sha256()
    m.update(s)
    return m.hexdigest()

print obfuscate('Tag 1')
print obfuscate('Tag 2')
print obfuscate('Tag 3')

rawDescription: Encrypt a string in Python. Restrict the characters used to only alphanumeric


code:
import re
rx = re.compile("""
        ginstance\s*\{
        [^}]*
        (?:node\ "FINDME")
        [^}]*
        \}
        """, re.VERBOSE)
string = re.sub(rx, '', your_string_here)
print string

rawDescription: Regex to filter and remove specific multiple lines of text from a file with python


code:
r = re.compile(r',0\*[0-9A-F]{2}')
m = r.search("kdjrnnj,0*B3;,w0l44")
if m : print m.start()
m = r.search("qui8ecc),0*21qxxcd4))")
if m : print m.start()

rawDescription: Python using a regex to find start position of a substring


code:
import re
import lxml.html

tree = lxml.html.fromstring(data)

pattern = re.compile("http://example.com/Stuff/preview/v/\d+/fl/1/t/")
for element, attribute, link, pos in tree.iterlinks():
    if not pattern.match(link):
        continue
    print link

rawDescription: Finding all links matching specific URL template in an HTML page


code:
import gtk

def report_death(obj):
    # arrange for the death of OBJ to be announced
    def announce(wr):
        print 'gone'
    import weakref
    report_death.wr = weakref.ref(obj, announce)

def make_dynamic_handler():
    def handler():
        pass
    # for debugging - we want to know when the handler is freed
    report_death(handler)
    return handler

w = gtk.Window()
w.connect('realize', make_dynamic_handler())
w.destroy()

rawDescription: Does pygtk garbage-collect runtime-created functions connected to signals?


code: print textwrap.fill(s, 80)

rawDescription: Is there a nice way splitting a (potentially) long string without splitting in words in Python?


code:
import numpy as np

# Generate a few unix time stamps near today...
x = np.arange(1326706251, 1326706260)

# Convert to datetimes...
x *= 1e6
x = x.view(np.datetime64)

print x

rawDescription: What is the fastest way to convert from a unixtime to a numpy.datetime64?


code:
if event.type == pygame.MOUSEBUTTONDOWN:
    print event.button

rawDescription: How to distinguish left click , right click mouse clicks in pygame?

description: Distinguish left click , right click mouse clicks in pygame


code:
from scipy.integrate import odeint
import numpy as np

Ap_data = [2, 7, 91, 1.6, 0.4, 5]
tdata= np.array([0, 1, 4, 5, 10, 20])
Cv_data = np.array([43, 580, 250, 34, 30, 3])

#Define parameters
kn = 1E-5 #change
ks = 1E+5 #change
kd = 0.058

def deriv (CAi,t, Cv):
    CA0 = (-kd-kn*Cv)*CAi/(1+(CAi/ks))
    return CA0

#Initial conditions
CA_init = 21.6

#Solve the ODE
(CAb_soln) = odeint (deriv, CA_init, tdata, (Cv_data,))
print CAb_soln

rawDescription: Pass an array in python odeint

description: Pass an array in python odeint


code:
def OnSelectMustHave(self,event):
    info = event.GetData()
    InfoList.append(info)
    map(lambda x:self.list_ctrl.SetItemBackgroundColour(x,wx.LIGHT_GREY) ,set(InfoList))
    print info,InfoList
    return

rawDescription: Python and multiple select with a ListCtrl


code:
a = ["abc", "de", "f", "ghijkl"]
print sorted(a, lambda a,b: cmp(len(a), len(b)))

rawDescription: merge sort implementation to sort by string length - python

description: Merge sort implementation to sort by string length - python


code:
for x in [0]:
   while 1:
       for x in [x+1]:
           print x

rawDescription: Counting from 1 to infinity without assignment


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

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


code:
import requests

def getServer(uri, default = "-- Server not given --"):
    request = requests.get(uri)
    if "server" in request.headers:
        return request.headers["server"]
    else:
        return default

if __name__ == "__main__":
    uris = [
        "http://superuser.com",
        "http://google.com",
        "http://docs.python-requests.org/en/latest/",
        "http://yahoo.com"
    ]

    for theURI in uris:
        print "Website: %s uses %s" % (theURI, getServer(theURI))

rawDescription: Python netcraft API or replacement library


code:
from pyparsing import *

survey = '''GPS,PN1,LA52.125133215643,LN21.031048525561,EL116.898812'''

number = Word(nums+'.').setParseAction(lambda t: float(t[0]))
separator = Suppress(',')
latitude = Suppress('LA') + number
longitude = Suppress('LN') + number
elevation = Suppress('EL') + number

line = (Suppress('GPS,PN1,')
        + latitude
        + separator
        + longitude
        + separator
        + elevation)

print line.parseString(survey)

rawDescription: pyparsing example

description: Pyparsing example


code:
dicts = [{"ab":3, "bc":4},{"ac":3, "df":3},{"ab":2, "de":4}]
print 'dicts', dicts

master = {}
for i, d in enumerate(dicts):
    for k in d:
        master.setdefault(k, []).append(i)

print 'master', master

rawDescription: How to judge whether a key is in the other dictionaries of a dictionary list(at least occurred in 2 different dictionaries) with python

description: Judge whether a key is in the other dictionaries of a dictionary list(at least occurred in 2 different dictionaries) with python


code:
s = df['0'].str.split(' ')

d = [dict(w.split(':', 1) for w in x) for x in s]
print d
#[{'baz': '3', 'foo': '1', 'bar': '2'}, {'baz': '5', 'bar': '4'}, {'foo': '6'}]

print pd.DataFrame.from_records(d).fillna(0)
#  bar baz foo
#0   2   3   1
#1   4   5   0
#2   0   0   6

rawDescription: Pandas: Reconstruct dataframe from strings of key:value pairs


code:
import yaml

with open('file.yaml''', 'r') as stream:

    facts = yaml.load(stream)

    multiple = ', '.join([val for (key,val) in facts['values'].iteritems() if "network" in key])
    # single interface
    print facts['values']['network_lo']
    # 127.0.0.1

    # all interfaces
    print multiple

rawDescription: python key unknown in yaml

description: Python key unknown in yaml


code:
class UnitFloat(float):

    def __new__(self, value, unit=None):
       return float.__new__(self, value)

    def __init__(self, value, unit=None):
        self.unit = unit

x = UnitFloat(35.5, "cm")
y = UnitFloat(42.5)

print x
print x.unit

print y
print y.unit

print x + y

rawDescription: Python values with units

description: Python values with units


code:
offer =  [ 1, 8, 3 ]
demand = [ 1, 10, 11, 7 ]
connections = []
disThresh = 100

for i in xrange(len(offer)):
    for j in xrange(len(demand)):
        dist = offer[i] - demand[j]
        if dist < disThresh and min(offer[i], demand[j]) > 0: #if there is z available to send
            link = min(offer[i], demand[j])
            offer[i] = offer[i] - link
            demand[j] = demand[j] - link
            connections.append([i,j])
            print "Connected ",i,"and",j,"offer",offer,"demand",demand

print connections

rawDescription: Modifying a list with objects inside a loop


code:
import re
p = re.compile(ur'([^|]+)\|[^|]+\|([^|]+)')
test_str = u"..."

re.findall(p, test_str)

rawDescription: Regex grab first and last item separated by pipes in a string python


code:
import random
import numpy as np

x_arr = np.array([], dtype = object).reshape((1,0))
for x in range(5):
    y_arr = np.array([], dtype=np.int32)
    for y in range(5):
        r = random.random()
        if  r < 0.5:
            y_arr = np.append(y_arr,y)
    if random.random() < 0.9:
        x_arr = np.append(x_arr, 0)
        x_arr[-1] = y_arr

print type(x_arr)
print x_arr

rawDescription: array/list/tuple of nparrays with variable length?

description: Array/list/tuple of nparrays with variable length


code:
class Foo(object):
    def __init__(self, func, parent=None):
        self.func = func
        self.parent = parent

    def run(self, n):
        if self.parent is None:
            return self.func(n)
        else:
            return self.func(self.parent.run(n))

a = Foo(f)
print a.run(6)
# [1, 2, 3, 6]
b = Foo(s, a)
print b.run(5)
# True

rawDescription: Python generic method scope

description: Python generic method scope


code:
print strfdelta(delta_obj, "{days} days {hours}:{minutes}:{seconds}")
print strfdelta(delta_obj, "{hours} hours and {minutes} to go")

rawDescription: formatting python timedelta objects

description: Formatting python timedelta objects


code:
# run.py
def run(*args):
    print args  # real application code belongs here

if __name__ == "__main__":
    run(*sys.argv[1:])

rawDescription: If module 'example' contains both function 'run' and submodule 'run', can I count on 'from example import run' to always import the former?


code:
import gtk
import wnck
import glib

class WindowTitle(object):
    def __init__(self):
        self.title = None
        glib.timeout_add(100, self.get_title)

    def get_title(self):
        try:
            title = wnck.screen_get_default().get_active_window().get_name()
            if self.title != title:
                self.title  = title
                print title
        except AttributeError:
            pass
        return True

WindowTitle()
gtk.main()

rawDescription: Xorg, Python, and Current Window Title

description: Xorg, Python, and Current Window Title


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

class MyListWidget(QListWidget):
    def __init__(self, parent=None):
        super(MyListWidget, self).__init__(parent)
        self.itemClicked.connect(self.on_item_clicked)

    def mousePressEvent(self, event):
        self._mouse_button = event.button()
        super(MyListWidget, self).mousePressEvent(event)

    def on_item_clicked(self, item):
        print item.text(), self._mouse_button

class Frame(QWidget):
    def __init__(self, parent=None):
        super(Frame, self).__init__(parent)
        self.item_ctrl = items = MyListWidget(self)
        self.item_ctrl.addItem("Item1")
        self.item_ctrl.addItem("Item2")
        box = QVBoxLayout()
        box.addWidget(self.item_ctrl)
        self.setLayout(box)

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    main = Frame()
    main.show()
    sys.exit(app.exec_())

rawDescription: Catch which mousebutton is pressed on item


code:
result = []
for k in d:
    temp = tuple()
    for item in zip(*d[k]):
        temp += (sum(map(float, item)),)
    result.append((k,) + temp)
print result

rawDescription: Adding values corresponding to one key in python dictionary


code:
import os
import collections

subnets_file = "subnets.txt"

# Load the subnets file into a deque
with open(subnets_file, 'r') as f:
    subnets = collections.deque(f.read().splitlines())

# Print the top subnet
print subnets[0]

# Rotate the subnets
subnets.rotate(-1)

# Save the rotated subnets
with open(subnets_file, 'w') as f:
    for s in subnets:
        f.write("%s\n" % s)

rawDescription: python: how can I get a new value in round robin style every time i invoke the script

description: Python: how can I get a new value in round robin style every time i invoke the script


code: print post['title']

rawDescription: Feedparser newbie questions

description: Feedparser newbie questions


code:
dic = {'z':'a', 'y':'b', 'x':'c', 'w':'d'} # actual contents of a dictionary
for k in ordered_keys:
    if k in dic: # avoid trouble if a key is missing
        print k, dic[k]

rawDescription: How to order xml element attributes in Python?

description: Order xml element attributes in Python


code:
import re
p = re.compile(ur'(?:[0-9a-fA-F]:?){12}')
test_str = u"TEXT WITH SOME MAC ADDRESSES 00:24:17:b1:cc:cc TEXT CONTINUES WITH SOME MORE TEXT 20:89:86:9a:86:24"

re.findall(p, test_str)

rawDescription: Python regex extract MAC addresses from string


code:
import numpy as np
import pandas as pd

input_params = {
    'input1': [0.3], # fixed
    'input2': [1.5, 4.5, 4], # variable param: [start, end, number_of_intermediate_values]
    'input3': [1200.0], # fixed
    'input4': [-0.1, -0.5, 10], # variable param: [start, end, number_of_intermediate_values]
    'input5': [1e-3], # fixed
}

def expand_input(inputs):
    if len(inputs) == 1:
        return inputs
    return np.linspace(*inputs).tolist()

def sim(in_pars):
    "dummy simulation that returns three results"
    return np.min(in_pars), np.mean(in_pars), np.max(in_pars)

items = sorted(input_params.items())
keys = [item[0] for item in items]
inputs = [expand_input(item[1]) for item in items]

idx = pd.MultiIndex.from_product(inputs, names=keys)
df = pd.DataFrame(np.zeros((len(idx), 3)), columns="res1 res2 res3".split(), index=idx)

for key, row in df.iterrows():
    row[:] = sim(key)

print df

rawDescription: How to prepare, populate and explore an n-dimensional numpy array?

description: Prepare, populate and explore an n-dimensional numpy array


code:
import re

def check_password(password):
    pattern = '(?:[a-z]{3}|[A-Z]{3}|\d{3}|[\x20-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]{3})'
    return re.search(pattern, password)

passwords = ['a', 'abc', 'ABC', 'aBc', '1bc', '123']

for password in passwords:
    if check_password(password):
        print 'password failed: ', password
    else:
        print 'password passed: ', password

rawDescription: Finding a recurrance of three ASCII families in a string


code:
banner = '''
This program will calculate the unit price (price per oz) of store items.
You will input the weight in lbs and oz, the name and cost.
'''

print banner

item_name = raw_input("Please enter the name of the item. ")

item_lb_price = float(raw_input("Please enter the price per pound of your item."))
item_lbs = float(raw_input("Please enter the pounds of your item."))
item_oz = float(raw_input("plese enter the ounces of your item."))

unit_price = item_lb_price / 16.0
total_price = item_lb_price * (item_lbs + (item_oz / 16.0))

print "The total price per oz is", unit_price
print "The total price for", item_name, "is a total of", total_price

rawDescription: Python float numbers and correct use

description: Python float numbers and correct use


code:
a = A()
try:
    a.d = 89
except AttributeError:
    print "It works!"
else:
    print "It doesn't work."
a.c = 42
print a.a
print a.c
with a.declare_attributes():
    a.d = 28
print a.d

rawDescription: In Python, count the number of variables in a class or prevent adding new class variables


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

x = np.arange(-10,10)
y = x**2

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)

coords = []

def onclick(event):
    global ix, iy
    ix, iy = event.xdata, event.ydata
    print 'x = %d, y = %d'%(
        ix, iy)

    global coords
    coords.append((ix, iy))

    if len(coords) == 2:
        fig.canvas.mpl_disconnect(cid)

    return coords
cid = fig.canvas.mpl_connect('button_press_event', onclick)

rawDescription: Store mouse click event coordinates with matplotlib


code:
a = 1L
b = 1L
a is b

rawDescription: Checking if for-loop variable resides at two indexes of a list


code:
path="images\\ts\\trainingset.dump"
fp = open(path, "r")
im = pickle.load(fp)
label = pickle.load(fp)
imlist = im.tolist()
imlist = [item for sublist in imlist for item in sublist]
y.append(label)
x.append(imlist)

print "Setting up the SVM problem"
prob = svm_problem(y, x)

param = svm_parameter('-t 2 -g 0.00001')
param.C = 1

print "Starting the training process"
m=svm_train(prob, param)

print "Storing the model"

svm_save_model(model, m)

rawDescription: Convert a color image to input vector for libsvm


code:
import csv
import matplotlib.pyplot as plt

def crime_rate(*state):
    for s in state:
        r = csv.reader(open('CrimeStatebyState.csv', 'rb'))
        line1 = r.next()
        orig_dict = {}
        for n in range (1960,2006):
            orig_dict[n] = []
        for line in r:
            if line[0] == s:
                orig_dict[int(line[3])].append(int(line[4]))
        for y in orig_dict:
            orig_dict[y] = sum(orig_dict[y])
        plt.plot(orig_dict.keys(), orig_dict.values(),'r')
        print orig_dict.values()
        print s

crime_rate("Alabama", "California", "New York")

rawDescription: For-Loop Execution in Python - Does the Executable Code Reset?


code:
import scrapy
import re
import json
from scrapy.crawler import CrawlerProcess
from scrapy import Request
from scrapy.contrib.spiders import CrawlSpider , Rule
from scrapy.selector import HtmlXPathSelector
from scrapy.selector import Selector
from scrapy.contrib.loader import ItemLoader
from scrapy.contrib.loader import XPathItemLoader
from scrapy.contrib.loader.processor import Join, MapCompose
from concert_comparator.items import ComparatorItem, ComparatorItem3
from urlparse import urljoin

from scrapy.crawler import CrawlerRunner
from twisted.internet import reactor, defer
from scrapy.utils.log import configure_logging
from pipelines.pipelines import ComparatorPipeline, ComparatorPipeline2

bandname = raw_input("Enter a bandname \n")

vs_url = "http://www.vividseats.com/concerts/" + bandname + "-tickets.html"
tc_url = "https://www.ticketcity.com/concerts/" + bandname + "-tickets.html"

class MySpider(CrawlSpider):
    pipeline = set([
        ComparatorPipeline
        ])
    handle_httpstatus_list = [416]
    name = 'comparator'
    allowed_domains = ["www.vividseats.com"]
    start_urls = [vs_url]
    tickets_list_xpath = './/*[@itemtype="http://schema.org/Event"]'

    def parse_json(self, response):
        loader = response.meta['loader']
        jsonresponse = json.loads(response.body_as_unicode())
        ticket_info = jsonresponse.get('tickets')
        price_list = [i.get('p') for i in ticket_info]
        if len(price_list) > 0:
            str_Price = str(price_list[0])
            ticketPrice = unicode(str_Price, "utf-8")
            loader.add_value('ticketPrice', ticketPrice)
        else:
            ticketPrice = unicode("sold out", "utf-8")
            loader.add_value('ticketPrice', ticketPrice)
        return loader.load_item()
    def parse_price(self, response):
        loader = response.meta['loader']
        ticketsLink = loader.get_output_value("ticketsLink")
        json_id_list= re.findall(r"(\d+)[^-]*$", ticketsLink)
        json_id=  "".join(json_id_list)
        json_url = "http://www.vividseats.com/javascript/tickets.shtml?productionId=" + json_id
        yield scrapy.Request(json_url, meta={'loader': loader}, callback = self.parse_json, dont_filter = True)

    def parse(self, response):
        """
        # """
        selector = HtmlXPathSelector(response)
        # iterate over tickets
        for ticket in selector.select(self.tickets_list_xpath):
            loader = XPathItemLoader(ComparatorItem(), selector=ticket)
            # define loader
            loader.default_input_processor = MapCompose(unicode.strip)
            loader.default_output_processor = Join()
            # iterate over fields and add xpaths to the loader
            loader.add_xpath('eventName' , './/*[@class="productionsEvent"]/text()')
            loader.add_xpath('eventLocation' , './/*[@class = "productionsVenue"]/span[@itemprop  = "name"]/text()')
            loader.add_xpath('ticketsLink' , './/*/a[@class = "btn btn-primary"]/@href')
            loader.add_xpath('eventDate' , './/*[@class = "productionsDate"]/text()')
            loader.add_xpath('eventCity' , './/*[@class = "productionsVenue"]/span[@itemprop  = "address"]/span[@itemprop  = "addressLocality"]/text()')
            loader.add_xpath('eventState' , './/*[@class = "productionsVenue"]/span[@itemprop  = "address"]/span[@itemprop  = "addressRegion"]/text()')
            loader.add_xpath('eventTime' , './/*[@class = "productionsTime"]/text()')

            print "Here is ticket link \n" + loader.get_output_value("ticketsLink")
            #sel.xpath("//span[@id='PractitionerDetails1_Label4']/text()").extract()
            ticketsURL = "concerts/" + bandname + "-tickets/" + bandname + "-" + loader.get_output_value("ticketsLink")
            ticketsURL = urljoin(response.url, ticketsURL)
            yield scrapy.Request(ticketsURL, meta={'loader': loader}, callback = self.parse_price, dont_filter = True)

class MySpider3(CrawlSpider):
    pipeline = set([
        ComparatorPipeline2
        ])
    handle_httpstatus_list = [416]
    name = 'comparator3'
    allowed_domains = ["www.ticketcity.com"]
    start_urls = [tc_url]
    tickets_list_xpath = './/div[@class = "vevent"]'

    def parse_json(self, response):
        loader = response.meta['loader']
        jsonresponse = json.loads(response.body_as_unicode())
        ticket_info = jsonresponse.get('B')
        price_list = [i.get('P') for i in ticket_info]
        if len(price_list) > 0:
            str_Price = str(price_list[0])
            ticketPrice = unicode(str_Price, "utf-8")
            loader.add_value('ticketPrice', ticketPrice)
        else:
            ticketPrice = unicode("sold out", "utf-8")
            loader.add_value('ticketPrice', ticketPrice)
        return loader.load_item()

    def parse_price(self, response):
        print "parse price function entered \n"
        loader = response.meta['loader']
        event_City = response.xpath('.//span[@itemprop="addressLocality"]/text()').extract()
        eventCity = ''.join(event_City)
        loader.add_value('eventCity' , eventCity)
        event_State = response.xpath('.//span[@itemprop="addressRegion"]/text()').extract()
        eventState = ''.join(event_State)
        loader.add_value('eventState' , eventState)
        event_Date = response.xpath('.//span[@class="event_datetime"]/text()').extract()
        eventDate = ''.join(event_Date)
        loader.add_value('eventDate' , eventDate)
        ticketsLink = loader.get_output_value("ticketsLink")
        json_id_list= re.findall(r"(\d+)[^-]*$", ticketsLink)
        json_id=  "".join(json_id_list)
        json_url = "https://www.ticketcity.com/Catalog/public/v1/events/" + json_id + "/ticketblocks?P=0,99999999&q=0&per_page=250&page=1&sort=p.asc&f.t=s&_=1436642392938"
        yield scrapy.Request(json_url, meta={'loader': loader}, callback = self.parse_json, dont_filter = True)

    def parse(self, response):
        """
        # """
        selector = HtmlXPathSelector(response)
        # iterate over tickets
        for ticket in selector.select(self.tickets_list_xpath):
            loader = XPathItemLoader(ComparatorItem(), selector=ticket)
            # define loader
            loader.default_input_processor = MapCompose(unicode.strip)
            loader.default_output_processor = Join()
            # iterate over fields and add xpaths to the loader
            loader.add_xpath('eventName' , './/span[@class="summary listingEventName"]/text()')
            loader.add_xpath('eventLocation' , './/div[@class="divVenue location"]/text()')
            loader.add_xpath('ticketsLink' , './/a[@class="divEventDetails url"]/@href')
            #loader.add_xpath('eventDateTime' , '//div[@id="divEventDate"]/@title') #datetime type
            #loader.add_xpath('eventTime' , './/*[@class = "productionsTime"]/text()')

            print "Here is ticket link \n" + loader.get_output_value("ticketsLink")
            #sel.xpath("//span[@id='PractitionerDetails1_Label4']/text()").extract()
            ticketsURL = "https://www.ticketcity.com/" + loader.get_output_value("ticketsLink")
            ticketsURL = urljoin(response.url, ticketsURL)
            yield scrapy.Request(ticketsURL, meta={'loader': loader}, callback = self.parse_price, dont_filter = True)

if __name__ == "__main__":
    configure_logging()
    runner = CrawlerRunner()

    @defer.inlineCallbacks
    def crawl():
        yield runner.crawl(MySpider)
        yield runner.crawl(MySpider3)
        reactor.stop()

    crawl()
    reactor.run()

rawDescription: How to use a decorator to enable a spider to distinguish between scrapy pipelines

description: Use a decorator to enable a spider to distinguish between scrapy pipelines


code:
from urllib2 import urlopen
from BeautifulSoup import BeautifulSoup

url = 'http://www.uscho.com/rankings/d-i-mens-poll/'
soup = BeautifulSoup(urlopen(url).read())

section = soup.find('section', id='rankings')
h1 = section.find('h1')
print h1.text
h3 = section.find('h3')
print h3.text
print

rows = section.find('table').findAll('tr')[1:-1]
for row in rows:
    columns = [data.text for data in row.findAll('td')[1:]]
    print '{0:20} {1:4} {2:>6} {3:>4}'.format(*columns)

rawDescription: Extracting text from a table with python and lxml


code: print "%05d" % val

rawDescription: how to retain the leading "00" when incrementing a value

description: Retain the leading "00" when incrementing a value


code:
Class.NUMBER = "Hah!"
print [x.NUMBER for x in classes]

rawDescription: Different class variables and usage

description: Different class variables and usage


code:
load_profile = open('users/file.txt', "r")
read_it = load_profile.read()
myLines = []
for line in read_it.splitlines():
    # if line.startswith("Start of line..."):
    # if line.endswith("...line End."):
    # if line.find("SUBSTRING") > -1:
    if line == "This is the line I am looking for":
        myLines.append(line)
print myLines

rawDescription: How do you read a specific line of a text file in Python?

description: Read a specific line of a text file in Python


code:
# method decorator -- makes undeclared 'self' argument available to method
injectself = lambda f: lambda self: eval(f.func_code, dict(self=self))

class TestClass:
    def __init__(self, thing):
        self.attr = thing

    @injectself
    def method():
        print 'in TestClass::method(): self.attr = %r' % self.attr
        return 42

test = TestClass("attribute's value")
ret = test.method()
print 'return value:', ret

# output:
# in TestClass::method(): self.attr = "attribute's value"
# return value: 42

rawDescription: How to get self into a Python method without explicitly accepting it

description: Get self into a Python method without explicitly accepting it


code:
import SimpleHTTPServer
import SocketServer

PORT = 80

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

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

print "serving at port", PORT
httpd.serve_forever()

rawDescription: upload python code on web server that runs it on on specific time intervals

description: Upload python code on web server that runs it on on specific time intervals


code:
result = []
A = [1,2,3,4]
for i in range(0,2**len(A)):
    binary = bin(i)[2:]
    binary = '0'*(len(A)-len(binary)) + binary
    subset = [ A[i] for i,x in enumerate(binary) if x=='1' ]
    print binary,subset
    result.append(subset)
print result

rawDescription: translating a recursive solution to find all subsets of a list

description: Translating a recursive solution to find all subsets of a list


code:
from bs4 import BeautifulSoup
import urllib2

soup = BeautifulSoup(urllib2.urlopen('http://www.livefootballontv.com/').read())

for row in soup('div', {'id': 'tv-guide'})[0]('ul'):
    tds = row('li')
    print tds[1].contents[0]

rawDescription: Web scraper is scraping both text and <span> text </span>. Span text not needed


code:
#/usr/bin/python

import re

# myfile is saved with utf-8 encoding
with open('myfile', 'r') as myfile:

    text = myfile.read()
    print text
    data = re.sub('<a.+?</a>', r'\\footnotemark', text)

print data

rawDescription: Python Print/Write String containing "\f"


code:
import csv

with open('input.txt', 'rb') as f:
    for i in range(47):
        next(f, None)
    reader = csv.reader(f,delimiter=' ')
    values = list(reader)

# if you want to remove the ''
#for idx, val in enumerate(values):
#    values[idx] = [x for x in values[idx] if x != '']

print values

with open('output.txt', 'wb') as f:
    writer = csv.writer(f, delimiter=' ', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    for line in values:
        writer.writerow(line)

rawDescription: How to read this file,with skipping some lines using Python?

description: Read this file,with skipping some lines using Python


code:
customer = 'ABCDEF'
line='ABC'
sql = "{CALL MYLIB.MYSQLB('%(customer)s','%(line)s')}" % vars()
cursor.execute(sql)
print cursor.fetchone()

rawDescription: ODBC return values from iSeries on Linux


code:
my = myArray # just for brevity

t1 = time.time()
# First set the positive and negative values
# complicated - [my.itemset((x,y), posFunc(my.item(x,y), l[y])) for (x,y) in zip(*np.where(my > 0))]
# changed to
my = np.where(my > 0, my*l, my)
# complicated - [my.itemset((x,y), negFunc(my.item(x,y), l[y])) for (x,y) in zip(*np.where(my < 0))]
# changed to
my = np.where(my < 0, my*(l+1), my)
# print my - commented out to time it.

# Now set the zeroes to 1.0s
my = np.where(my == 0.0, 1.0, my)
# print my  - commented out to time it

a = np.prod(my, axis=1)
t2 = time.time()
print (t2-t1)*1000000

print a

rawDescription: Applying several functions to each row of an array


code:
def hello():
    return 'hello'

iamhello = hello # bind to the old one

def hello():
    return 'bye'

print hello() # here is the new guy

print iamhello()

rawDescription: Method Overriding?

description: Method Overriding


rawDescription: Sorting string by specific column

description: Sorting string by specific column


code:
from collections import defaultdict
def convertToDict(key, my_list):
    result_dict = defaultdict(list)
    for item in my_list:
        if item[0] == key:
            result_dict[item[0]].append(item[1])
    return result_dict

print convertToDict("id1", my_list)

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

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


code:
import mock

ClassMock = mock.MagicMock

class RealClass(ClassMock):

    def lol(self):
        print 'lol'

real = RealClass()
real.lol()  # Does not print lol, but returns another mock

print real # prints <MagicMock id='...'>

rawDescription: Python mock: mocking base class for inheritance


code:
result = client.service.getPercentBodyFat('jeff', 68, 170)
print result

rawDescription: Python and SOAP

description: Python and SOAP


code:
import re

# Goal:  '717 N 2ND ST, MANKATO, MN 56001',
# Goal:  '717 N 2ND ST, MANKATO, MN, 56001',
# Goal:  '717 2ND ST NE, MANKATO, MN, 56001',
# Goal:  '717 N 2ND, MANKATO, MN, 56001',
regex = r'''
    (?x)            # verbose regular expression
    (?i)            # ignore case
    (?: # Matches any sort of street address
        (?: # Matches '717 N 2ND ST' or '717 N 2ND'
            (?P<HouseNumber>\d+)\s+      # Matches '717 '
            (?P<Direction>[news])\s+     # Matches 'N '
            (?P<StreetName>\w+)\s*       # Matches '2ND ', with optional trailing space
            (?P<StreetDesignator>\w*)\s* # Optionally Matches 'ST '
        )
        | # OR
        (?:  # Matches '717 2ND ST NE' or '717 2ND NE'
            (?P<HouseNumber2>\d+)\s+      # Matches '717 '
            (?P<StreetName2>\w+)\s+       # Matches '2ND '
            (?P<StreetDesignator2>\w*)\s* # Optionally Matches 'ST '
            (?P<Direction2>[news]+)       # Matches 'NE'
        )
    )
    ,\s+                             # Force a comma after the street
    (?P<TownName>.*),\s+             # Matches 'MANKATO, '
    (?P<State>[A-Z]{2}),?\s+         # Matches 'MN ' and 'MN, '
    (?P<ZIP>\d{5})                   # Matches '56001'
'''

regex = re.compile(regex)

for item in (
    '717 N 2ND ST, MANKATO, MN 56001',
    '717 N 2ND ST, MANKATO, MN, 56001',
    '717 N 2ND, Makata, 56001',   # Should reject this one
    '1234 N D AVE, East Boston, MA, 02134',
    '717 2ND ST NE, MANKATO, MN, 56001',
    '717 N 2ND, MANKATO, MN, 56001',
    ):
    match = regex.match(item)
    print item
    if match:
        d = match.groupdict()
        print "    House is on {0} side of {1}".format(
            d['Direction'] or d['Direction2'],
            d['TownName'])
    else:
        print "    invalid entry"

rawDescription: Python make sure address matches specific format


code:
import os

with open('list', 'w') as outfile:
    for root, dirs, files in os.walk("./customers/"):
        if not dirs:    # if root has no sub-directories it's a leaf
            print root
            outfile.write(root+"\n")

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

description: Print only the subdirectory tree using python


code: print '{0} and {1}'.format(arr[0],arr[1])

rawDescription: Python print array in one print function


rawDescription: How do i replace a single number (from a set of lines) from a list of numbers?

description: Replace a single number (from a set of lines) from a list of numbers


code:
# List of strings: ['1', '-3', '10', ...]
numbers = A.split()

print len(numbers)

rawDescription: length of list in python?

description: Length of list in python


code:
def read_file(file_name):
    fp = open(file_name,'r')
    relations = []
    commands = []
    reading_relations = True
    for l in fp:
        l = l.strip('\n')
        if not l:
            reading_relations = False
            continue
        if reading_relations:
            relations.append(l.strip())
        else:
            commands.append(l.strip())
    fp.close()
    return relations,commands

def form_relation_dict(relations):
    relation_dict = {}
    for l in relations:
        names = l.split(':')
        mother = names[0].strip()
        children = [x.strip() for x in names[1].split(',')]
        existing_children = relation_dict.get(mother,[])
        existing_children.extend(children)
        relation_dict[mother] = existing_children
    return relation_dict

def search_name(relation_dict,name):
    #Returns True if name occurs anywhere in relation_dict
    #Else return False
    for k,v in relation_dict.items():
        if name ==k or name in v:
            return True
    return False

def find_parent(relation_dict,param):
    #Finds the parent of 'param' in relation_dict
    #Returns None if no mother found
    #Returns mother name otherwise
    mother = None
    for k,v in relation_dict.items():
        if param in v:
            mother = k
            break
    return mother

def process_commands(commands,relation_dict):
    output = []
    for c in commands:
        coms = c.split()
        if len(coms) < 2:
            output.append("Invalid Command")
            continue
        action = coms[0]
        param = coms[1]
        if action == "mother":
            name_found = search_name(relation_dict,param)
            if not name_found:
                output.append("person not found")
                continue
            else:
                person = find_parent(relation_dict,param)
                if person is None:
                    output.append("mother not known")
                else:
                    output.append("mother - %s" %(person))
        elif action == "ancestors":
            name_found = search_name(relation_dict,param)
            if not name_found:
                output.append("person not found")
                continue
            else:
                #Loop through to find the mother till dead - end (None) is not reached
                ancestor_list = []
                person = param
                while True:
                    person = find_parent(relation_dict,person)
                    if person == None:
                        #Top of the ancestor found
                        break
                    else:
                        ancestor_list.append(person)
                if ancestor_list:
                    output.append(",".join(ancestor_list))
                else:
                    output.append("No known ancestors")
    return output

def main():
    file_name = 'try.txt'
    relations,commands = read_file(file_name)
    #Process Relqations to form a dictionary of the type {parent: [child1,child2,...]}
    relation_dict = form_relation_dict(relations)
    print relation_dict
    #Now process commands in the file
    output = process_commands(commands,relation_dict)
    print '\n'.join(output)

if __name__ == '__main__':
    main()

rawDescription: Looping dictionary values

description: Looping dictionary values


code:
from Tkinter import Tk, Button

machines_off = []

def run_command():
    #reset machines_off to the empty list.
    #delete these next two lines if you want to retain old values.
    global machines_off
    machines_off = []
    # Some stuff .....
    machineName = "foo"
    machines_off.append(machineName)
    # Some stuff ....

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

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

root.mainloop()

rawDescription: Sharing list between functions

description: Sharing list between functions


code:
import sys, random
from threading import Thread
from time import sleep
from urllib import urlopen
from PySide import QtCore, QtGui

class Window(QtGui.QMainWindow):
    update_signal = QtCore.Signal(int)
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.progress_bar = QtGui.QProgressBar(self)
        self.progress_bar.setRange(0, 10)
        self.setCentralWidget(self.progress_bar)
        self.update_signal[int].connect(self.progress_bar.setValue)
        self.show()
        self.t = Thread(target=self.worker)
        self.t.start()

    def worker(self):
        while self.progress_bar.value() < 10:
            self.update_signal.emit(self.progress_bar.value()+1)
            print "Starting Sleep"
            sleep(5)
            print "End of Sleep"

if __name__ == '__main__':
    qapp = QtGui.QApplication(sys.argv)
    win = Window()
    sys.exit(qapp.exec_())

rawDescription: How can I print to QTextEdit to mimic printing to console? (Python 3.3)

description: Print to QTextEdit to mimic printing to console? (Python 3.3)


code:
import os
#hard code the path to the external file
external_file = 'names.txt'
username = 'testuser'# Our hardcoded name

#if file doesn' exists, create a new file
if not os.path.exists(external_file):
    #Ask the user's name
    name = raw_input("What's your name?")
    with open(external_file, "a") as f: # using "a" will append to the file
        f.write(name)# Write the name to names.txt
        f.write("\n")
        f.close()
else:
    #if file exists, use it to load name, else ask user
    with open(external_file, "r+") as f:# r+ open a file for reading & writing
        lines = f.read().split('\n') # split the names
        print lines
        if username in lines: #Check if the file has any username as 'testuser'
            print "Hi {}".format(username)
        else: # If there is no username as 'testuser' then ask for a name
            name = raw_input("What's your name?")
            f.seek(0,2) # Resolves an issue in Windows
            f.write(name)# Write the name to names.txt
            f.write("\n")
            f.close()

rawDescription: Store input based on computer and change stored input on command


code:
for el in ((x, my_dictionary[x]) for x in my_list):
    print el

rawDescription: Is it possible to take an ordered "slice" of a dictionary in Python based on a list of keys?


code:
import itertools

alphabet = ["a", "b", "c"]
other_alphabet = ["a", "b", "d"]
another_alphabet = ["a", "b", "c"]

# here we take the nth char of each alphabet that is used as prediction
# (this position is indicated by the number of the currently entered char)
# zip takes the lists, and well, zips :) them, it means it creates new lists, so every
# first elements end up together, second elemends are together and so on.
# as far as current position you have to track it when user enters data (you have to
# know which (first, second, tenth) letter user is entering
current_position=1

letters = zip(alphabet,other_alphabet, another_alphabet)[current_position]
letters = list(letters)
letters.sort()
print 'letters at current position', letters

# here we group all occurences of the same letters,
letter_groups = itertools.groupby(letters, key=lambda x: x[0])

# here we count the number of occurences of each letter
# from the alphabets, and divide it by the lenght
# of the list of letters

letter_probabilities = [[a[0], sum (1 for _ in a[1])/float(len(letters))] for a in letter_groups]
print 'letter probablilities at the current postion ', letter_probabilities

rawDescription: Performing (string/list) prediction in Python


code:
for indices in itertools.product(*(range(len(p)) for p in probs)):
    # get the values corresponding to the indices
    prob = [probs[x][indices[x]] for x in range(len(probs))]
    print indices, prob

rawDescription: Nested for loops to recursive function in Python


code:
context = etree.iterparse(xmlFile)
counter = collections.Counter()
for action, elem in context:
    if elem.tag == "word" and (elem.attrib.get('tag') in ['NN','AD']): #AD I just used for adjective, use whatever is correct.
      counter[elem.text] += 1
print counter.most_common(10)

rawDescription: Using lxml to retrieve both tag and subtag categories with unicode encoding


code:
import math
print int(math.ceil(1.47))

rawDescription: Round up numbers such as 1.23 to 2 in Python


code: print hash(str(sorted({1 : 2, 3 : 4}.items())))

rawDescription: Python: Compare dict with a static reference?


code:
print df
#      col 1  col 2
#0       NaN    NaN
#1       NaN    NaN
#1000      1    NaN
#1001    NaN      1
#1002      3    NaN
#1003      4      3
#3999    NaN    NaN
#4000    NaN    NaN

df.loc[df['col 1'].idxmin(): df['col 1'].idxmax()] = df.loc[df['col 1'].idxmin(): df['col 1'].idxmax()].fillna(method='ffill')
df.loc[df['col 2'].idxmin(): df['col 2'].idxmax()] = df.loc[df['col 2'].idxmin(): df['col 2'].idxmax()].fillna(method='ffill')
print df
#      col 1  col 2
#0       NaN    NaN
#1       NaN    NaN
#1000      1    NaN
#1001      1      1
#1002      3      1
#1003      4      3
#3999    NaN    NaN
#4000    NaN    NaN

rawDescription: Pandas: interpolation where first and last data point in column is NaN


code:
import re
s = 'some data \n 8 \n more data'
if re.search("\s+[0-9]+\s+", s): print True
s = 'some data \n 8) \n more data'
if re.search("\s+[0-9]+\s+", s): print True
s = 'some data \n 8343 \n more data'
if re.search("\s+[0-9]+\s+", s): print True

rawDescription: python regex, only digit between whitespaces

description: Python regex, only digit between whitespaces


code:
import urllib2

u = 'http://www.google.com/'

pages =  open('pages.txt', 'r').readlines()

for page in pages:
        url = u+page
        try:
                req = urllib2.urlopen(url)
        except urllib2.HTTPError as e:
                if e.code == 404:
                        print url+" does not exists"
        else:
                print url+" exists"

rawDescription: How to know if a specific page in a website is down (Batch or Python)

description: Know if a specific page in a website is down (Batch or Python)


code:
data
while data:
    print ", ".join(data[:-2] + [" and ".join(data[-2:])])
    data.pop()

rawDescription: Grammatical List Join in Python

description: Grammatical List Join in Python


code:
import ssl
import os
import json
import email
import uuid
from io import BytesIO
import httplib

input_folder = os.path.dirname(os.path.abspath(__file__))
output_folder = os.path.join(input_folder, 'output')

def get_filename(ext, base, sub_folder):
    filename = '{}.{}'.format(base, ext)
    return os.path.join(output_folder, sub_folder, filename)

def compare_files(file1, file2):
    with open(file1, 'rb') as f_file1, open(file2, 'rb') as f_file2:
        if f_file1.read() == f_file2.read():
            print 'Same:\n  {}\n  {}'.format(file1, file2)
        else:
            print 'Different:\n  {}\n  {}'.format(file1, file2)

class Part(object):
    """Represent a part in a multipart messsage"""

    def __init__(self, name, contentType, data, paramName=None):
        super(Part, self).__init__()
        self.name = name
        self.paramName = paramName
        self.contentType = contentType
        self.data = data

    def encode(self):
        body = BytesIO()

        if self.paramName:
            body.write('Content-Disposition: form-data; name="%s"; paramName="%s"\r\n' % (self.name, self.paramName))
        else:
            body.write('Content-Disposition: form-data; name="%s"\r\n' % (self.name,))

        body.write("Content-Type: %s\r\n" % (self.contentType,))
        body.write("\r\n")
        body.write(self.data)
        return body.getvalue()

class Request(object):
    """A handy class for creating a request"""

    def __init__(self):
        super(Request, self).__init__()
        self.parameters = []

    def add_json_parameter(self, name, paramName, data):
        self.parameters.append(Part(name=name, paramName=paramName, contentType="application/json; charset=utf-8", data=data))

    def add_audio_parameter(self, name, paramName, data):
        self.parameters.append(Part(name=name, paramName=paramName, contentType="audio/x-wav;codec=pcm;bit=16;rate=16000", data=data))

    def encode(self):
        boundary = uuid.uuid4().hex
        body = BytesIO()

        for parameter in self.parameters:
            body.write("--%s\r\n" % (boundary,))
            body.write(parameter.encode())
            body.write("\r\n")

        body.write("--%s--\r\n" % (boundary,))
        return body.getvalue(), boundary

def get_tts(required_text, LNG):
    required_text = required_text.strip()
    output_filename = "".join([x if x.isalnum() else "_" for x in required_text[:80]])

    host = "mtldev08.nuance.com"
    port = 443
    uri = "/NmspServlet/"

    if LNG == "ENG":
        parameters = {'lang' : 'eng_GBR', 'location' : '47.4925, 19.0513'}

    if LNG == "GED":
        parameters = {'lang' : 'deu-DEU', 'location' : '48.396231, 9.972909'}

    RequestData = """{
        "appKey": "9c9fa7201e90d3d96718bc3f36ce4cfe1781f2e82f4e5792996623b3b474fee2c77699eb5354f2136063e1ff19c378f0f6dd984471a38ca5c393801bffb062d6",
        "appId": "NMDPTRIAL_AutomotiveTesting_NCS61HTTP",
        "uId": "Alexander",
        "inCodec": "PCM_16_8K",
        "outCodec": "PCM_16_8K",
        "cmdName": "NVC_TTS_CMD",
        "appName": "Python",
        "appVersion": "1",
        "language": "%(lang)s",
        "carrier": "carrier",
        "deviceModel": "deviceModel",
        "cmdDict": {
            "tts_voice": "Serena",
            "tts_language": "%(lang)s",
            "locale": "canada",
            "application_name": "Testing Python Script",
            "organization_id": "NUANCE",
            "phone_OS": "4.0",
            "phone_network": "wifi",
            "audio_source": "SpeakerAndMicrophone",
            "location": "%(location)s",
            "application_session_id": "1234567890",
            "utterance_number": "5",
            "ui_langugage": "en",
            "phone_submodel": "nmPhone2,1",
            "application_state_id": "45"
        }
    }""" % (parameters)

    TEXT_TO_READ = """{
        "tts_type": "text"
    }"""

    TEXT_TO_READ = json.loads(TEXT_TO_READ)
    TEXT_TO_READ["tts_input"] = required_text
    TEXT_TO_READ = json.dumps(TEXT_TO_READ)

    request = Request()
    request.add_json_parameter("RequestData", None, RequestData)
    request.add_json_parameter("TtsParameter", "TEXT_TO_READ", TEXT_TO_READ)

    #ssl._create_default_https_context = ssl._create_unverified_context
    body, boundary = request.encode()
    h = httplib.HTTPSConnection(host, port)
    #h.set_debuglevel(1)

    headers = {
        "Content-Type": "multipart/form-data; boundary=%s" % (boundary,),
        "Connection": "Keep-Alive",
    }

    h.request('POST', uri, body, headers)
    res = h.getresponse()

    data = """MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=--Nuance_NMSP_vutc5w1XobDdefsYG3wq
""" + res.read()

    msg = email.message_from_string(data)

    for part in msg.walk():
        content_type = part.get_content_type()
        payload = part.get_payload()

        if content_type == "audio/x-wav" and len(payload):
            ref_filename = get_filename('pcm', output_filename + '_ref', LNG)
            if not os.path.exists(ref_filename):
                with open(ref_filename, 'wb') as f_pcm:
                    f_pcm.write(payload)

            cur_filename = get_filename('pcm', output_filename, LNG)
            with open(cur_filename, 'wb') as f_pcm:
                f_pcm.write(payload)

            compare_files(ref_filename, cur_filename)

        elif content_type == "application/json":
            with open(get_filename('json', output_filename, LNG), 'w') as f_json:
                f_json.write(payload)

filename = r'input.txt'

with open(filename) as f_input:
    for line in f_input:
        LNG, text = line.strip().split('|')
        print "Getting {}: {}".format(LNG, text)
        get_tts(text, LNG)

rawDescription: How to remove the first four lines and the last 12 lines in to a file in Python?

description: Remove the first four lines and the last 12 lines in to a file in Python


code:
x = ["c~d", "e~f", "g~h"]
a, b = zip(*(s.split("~") for s in x))
print a
print b

rawDescription: pythonic way to split string into two lists

description: Pythonic way to split string into two lists


code:
print [[el.string, el['href'] ]for el in soup('a', href=True)]
# [[u'Go to 1', 'page1.html'], [u'Go to page 2', 'page2.html'], [u'Go to page 3', 'page3.html'], [u'Go to page 4', 'page4.html']]

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


code:
with open('C:\Users\j\Desktop\helloworld.csv', 'r') as datafile:
    data = []
    for row in datafile:
       data.extend(item.strip() for item in row.split())
    print(data)

rawDescription: Create a column from a CSV list in Python 3


code:
# Initiate customer dictionary
customers = dict()

class Customer:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname
        self.address = None
        self.zip = None
        self.state = None
        self.city = None
        self.phone = None

    def add_address(self, address, zp, state, city):
        self.address = address
        self.zip = zp
        self.state = state
        self.city = city

    def add_phone(self, number):
        self.phone = number

# Observe that these functions are not belonging to the class.
def _print_layout(object):
        print object.fname, object.lname
        print '==========================='
        print 'ADDRESS:'
        print object.address
        print object.zip
        print object.state
        print object.city
        print '\nPHONE:'
        print object.phone
        print '\n'

def print_customer(customer_name):
    _print_layout(customers[customer_name])

def print_customers():
    for customer_name in customers.iterkeys():
        _print_layout(customers[customer_name])

if __name__ == '__main__':
    # Add some customers to dictionary:
    customers['Steve'] = Customer('Steve', 'Jobs')
    customers['Niclas'] = Customer('Niclas', 'Nilsson')
    # Add some more data
    customers['Niclas'].add_address('Some road', '12312', 'WeDon\'tHaveStates', 'Hultsfred')
    customers['Steve'].add_phone('123-543 234')

    # Search one customer and print him
    print 'Here are one customer searched:'
    print 'ooooooooooooooooooooooooooooooo'
    print_customer('Niclas')

    # Print all the customers nicely
    print '\n\nHere are all customers'
    print 'oooooooooooooooooooooo'
    print_customers()

rawDescription: How do I assign a variable to an object name?

description: Assign a variable to an object name


code:
xmlstr = """
<?xml version="1.0"?>
<RootElement>
  <PropertySetProperty>
    <Key>KeyNotOfInterest</Key>
    <Value>ValueNotOfInterest</Value>
  </PropertySetProperty>
  <PropertySetProperty>
    <Key>ConnectionFile</Key>
    <Value>THE TEXT I WANT, IN THIS CASE A FILE PATH</Value>
  </PropertySetProperty>
  <PropertySetProperty>
    <Key>KeyAlsoNotOfInterest</Key>
    <Value>ValueAlsoNotOfInterest</Value>
  </PropertySetProperty>
</RootElement>
"""
from lxml import etree

doc = etree.fromstring(xmlstr.strip())
#doc = etree.parse("xmlfilename.xml")

xp = "//PropertySetProperty[Key/text()='ConnectionFile']/Value/text()"
wanted = doc.xpath(xp)[0]
print wanted

rawDescription: parsing single text items from xml with Python

description: Parsing single text items from xml with Python


code:
import fileinput
import sys

print 'list of goodies'
for line in fileinput.input(['./lista.txt'], inplace=True):
    number, item = line.rstrip().split(' ', 1) # strip new line from right, split on first space
    print 'number {0} of {1} for me'.format(number, item)

rawDescription: adding text to a line in a file

description: Adding text to a line in a file


code:
encoded_symbol = '\\u00a5'
print encoded_symbol
print encoded_symbol.decode('unicode_escape')

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


code:
import re

dateRegex = re.compile(r'^\w{3}\s\d\d?\s\d\d:\d\d:\d\d')
                             # ^^^^^^^ I added ? to cater for dates between 1 & 9

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

for line in f.readlines():
    matches = re.findall(dateRegex,line)
    if matches:                # Check if there are matches
        print matches[0]       # Print first element of list returned by findall

rawDescription: printing dates in a log file

description: Printing dates in a log file


code:
import threading
import functools
import time

def synchronized(wrapped):
    lock = threading.Lock()
    print lock, id(lock)
    @functools.wraps(wrapped)
    def _wrap(*args, **kwargs):
        with lock:
            print ("Calling '%s' with Lock %s from thread %s [%s]"
                   % (wrapped.__name__, id(lock),
                   threading.current_thread().name, time.time()))
            result = wrapped(*args, **kwargs)
            print ("Done '%s' with Lock %s from thread %s [%s]"
                   % (wrapped.__name__, id(lock),
                   threading.current_thread().name, time.time()))
            return result
    return _wrap

class AtomicCRUD(object):

    @synchronized
    def create(self):
        #Do stuff that may take a while here.
        time.sleep(1)

    @synchronized
    def delete(self):
        #Do stuff that may take a while here.
        time.sleep(1)

class SyncThread(threading.Thread):

    def __init__(self, crud, name):
        super(self.__class__, self).__init__(name=name)
        self._crud = crud

    def run(self):
        self._crud.create()
        self._crud.delete()

crud = AtomicCRUD()
threads = [SyncThread(crud, "Thread_%d" % i) for i in range(5)]
for t in threads:
    t.start()

for t in threads:
    t.join()

rawDescription: How to create a synchronized function across all instances

description: Create a synchronized function across all instances


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

rawDescription: Python module object without a file

description: Python module object without a file


code:
printer = np.vectorize(lambda x:'{0:5}'.format(x,))
print printer(b).astype(object)

rawDescription: How do I get wider numpy array print output?

description: Get wider numpy array print output


code:
import cPickle
import copy_reg
from types import FunctionType

# data to pickle: note that o['x'] is a lambda and they
# aren't natively picklable (at this time)
o = {'x': lambda x: x, 'y': 1}

# shows that o is not natively picklable (because of
# o['x'])
try:
    cPickle.dumps(o)
except TypeError:
    print "not natively picklable"
else:
    print "was pickled natively"

# create a mechanisms to turn unpickable functions int
# stub objects (the string "STUB" in this case)
def stub_pickler(obj):
    return stub_unpickler, ()
def stub_unpickler():
    return "STUB"
copy_reg.pickle(
    FunctionType,
    stub_pickler, stub_unpickler)

# shows that o is now picklable but o['x'] is restored
# to the stub object instead of its original lambda
print cPickle.loads(cPickle.dumps(o))

rawDescription: cPickle - Ignore stuff it can't serialize instead of raising an exception

description: CPickle - Ignore stuff it can't serialize instead of raising an exception


code:
class Listener:
    def __init__(self, data_to_catch):
        self.data_to_catch = data_to_catch
        self.data = ""
        self.found = False
        self.pos = 0

    def process(self, data):
        if self.found:
            return

        self.data += data
        self.found = self.data_to_catch in self.data[self.pos:]
        self.pos = max(0, len(self.data) - len(self.data_to_catch))

        if self.found:
            print "found!"

l = Listener("hello")
l.process("he")
l.process("llh")
l.process("e")
l.process("ll")
l.process("he")
l.process("llohe")

rawDescription: Listening for a particular pattern

description: Listening for a particular pattern


code:
class MyContextManager(object):
    def __init__(self, data):
        self.data = data

    def __call__(self):
        print 'foo'

    def __enter__(self):
        print "entering... (data=%s)" % self.data
        # this will be bound to the name after the 'as' clause
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        print "exiting... (data=%s)" % self.data

with MyContextManager('bar') as manager:
    print "before"
    manager()
    print "after"

rawDescription: Composing functionality into a method

description: Composing functionality into a method


code:
from traits.api import (HasTraits, Str, Int, Float, on_trait_change,
                        TraitChangeNotifyWrapper)
from traits.trait_notifiers import StaticTraitChangeNotifyWrapper

def get_listeners(h):
    """
    h must be a HasTraits instance.

    Returns a dictionary whose keys are trait names and whose values
    are lists of notifiers.
    """
    listeners = {}
    for name in h.traits():
        notifiers = h.trait(name)._notifiers(0)
        if notifiers is not None:
            # Filter out the static listeners.  Comment this out
            # if you want to keep those.
            notifiers = [notifier for notifier in notifiers
                            if not isinstance(notifier, StaticTraitChangeNotifyWrapper)]
            listeners[name] = notifiers
    return listeners

def print_listeners(listeners):
    """
    Print the dictionary of listeners returned by `get_listeners(h)`.
    """
    for name, notifiers in listeners.items():
        print "trait '%s' has the following listeners:" % (name,)
        for notifier in notifiers:
            if notifier.name is None:
                handler = notifier.handler
                print "    '%s' %r" % (handler.__name__, type(handler))
            else:
                print "    '%s' on object %s" % (notifier.name, notifier.object)

class GenerateEvents ( HasTraits ):
    name   = Str
    age    = Int
    weight = Float

    def _age_changed(self, old):
        print "age changed from ", old, "to", self.age

    @on_trait_change('weight')
    def do_something(self, obj, name, old, new):
        print "do_something: name =", name

class ListenEvents ( HasTraits ):
    def _name_changed ( self, object, name, old, new ):
        print "_name_changed:", object, name, old, new

    def _age_changed ( self, object, name, old, new ):
        print "_age_changed:", object, name, old, new

    def _weight_changed ( self, object, name, old, new ):
        print "_weight_changed:", object, name, old, new

class AnotherListenEvents ( HasTraits ):
    def _name_changed ( self, object, name, old, new ):
        print "Another _name_changed:", object, name, old, new

    def _age_changed ( self, object, name, old, new ):
        print "another _age_changed:", object, name, old, new

    def _weight_changed ( self, object, name, old, new ):
        print "another _weight_changed:", object, name, old, new

def printit(foo):
    print foo

ge = GenerateEvents()
le = ListenEvents()
ale = AnotherListenEvents()
ge.set( name = 'Joe', age = 22, weight = 152.0 )
ge.add_trait_listener( le )
ge.add_trait_listener( ale )
ge.set( name = 'Mike', age = 34, weight = 178.0 )

# Make the function `printit` a listener to ge.name.
ge.on_trait_change(printit, name='name')

rawDescription: Get list of traits listeners -- who's listening to my traits?


code:
import collections

data = [
    {'firstname': 'John', 'lastname': 'Smith'},
    {'firstname': 'Samantha', 'lastname': 'Smith'},
    {'firstname': 'shawn', 'lastname': 'Spencer'},
]

new_data = collections.defaultdict(list)

for d in data:
    new_data[d['lastname']].append(d['firstname'])

print new_data

rawDescription: Python dict question. How to group similar strings?


code:
def fn(*args):
    print str(args)
class MyClass(object):
    myfn = staticmethod(fn)
    def run(self, arg):
      self.myfn(arg)

o = MyClass()
o.run('foo')

rawDescription: Convince Python to run a saved function as static


code:
soup = BeautifulSoup(html)
thetd = soup.find('td', attrs={'class': 'author'})
print thetd.string

rawDescription: Python web scraping involving HTML tags with attributes


code:
class MouseDetector(QtCore.QObject):
    def eventFilter(self, obj, event):
        if event.type() == QtCore.QEvent.MouseButtonPress and obj.count() == 0:
            print 'Clicked'
        return super(MouseDetector, self).eventFilter(obj, event)

rawDescription: PyQt ComboBox widget not signalling when empty


code:
from BeautifulSoup import BeautifulSoup
html = '''

BACKGROUND

Mr. Lewis Carroll has bla bla

    Read Full Background

BACKGROUND

Mr. Paul J. Fribourg has bla bla

    Read Full Background

'''
soup = BeautifulSoup(html)
headings = soup.findAll('h2', text='BACKGROUND')
for section in headings:
    p = section.findNext('p')
    if p:
        print '> ',  p.string
    else:
        print '> ', section.parent.next.next.strip()

rawDescription: problems ... BeautifulSoup Parsing

description: Problems ... BeautifulSoup Parsing


code:
import test
foo = test.Test()
foo.icntl[30] = -654321
print foo.icntl[30]
print foo.icntl[40]

rawDescription: SWIG/python array inside structure

description: SWIG/python array inside structure


code:
import numpy as np
shape = (3, 10, 10, 19, 75, 10, 10)
p = np.arange(np.prod(shape)).reshape(shape)  # this is not symmetric, but not important

ix, iy = np.tril_indices(10)
# In order to index properly, we need to add axes. This can be done by hand or with this
ix1, ix2 = np.ix_(ix, ix)
iy1, iy2 = np.ix_(iy, iy)

p_ltriag = p[:, ix1, iy1, :, :, ix2, iy2]
print p_ltriag.shape  # yields (55, 55, 3, 19, 75), axis order can be changed if needed

q = np.zeros_like(p)
q[:, ix1, iy1, :, :, ix2, iy2] = p_ltriag  # fills the lower triangles on both sides
q[:, ix1, iy1, :, :, iy2, ix2] = p_ltriag  # fills the lower on left, upper on right
q[:, iy1, ix1, :, :, ix2, iy2] = p_ltriag  # fills the upper on left, lower on right
q[:, iy1, ix1, :, :, iy2, ix2] = p_ltriag  # fills the upper triangles on both sides

rawDescription: numpy multidimensional indexing and diagonal symmetries

description: Numpy multidimensional indexing and diagonal symmetries


code:
#init maya
import maya.standalone
maya.standalone.initialize( name='python' )
import maya.cmds as cmd
import glob

def func():
    print cmd.ls(shapes=True)

for file in glob.glob('*.m[ab]'):
    cmd.file( file, o=True )
    func()

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

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


code:
import sys
sys.path = sys.path[1:]
import logging
print dir(logging)

rawDescription: importing module with same name as file

description: Importing module with same name as file


rawDescription: Get load at a moment in time or getloadavg() for a smaller time period in Python in Linux (CentOS)


code:
target = 'Element 1'
with open(path1) as file1:
    for line in file1:
        if target in line:
            print line
            break

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


code:
with open('/Users/kyle/Documents/english words.txt') as word_file:
    english_words = set(word.strip().lower() for word in word_file)

for p1 in itertools.combinations('abcdefghijklmnopqrstuvwxyz', 4):
    word = ''.join(p1)
    print '{} is {}'.format(word, word in english_words)

rawDescription: python program very slow

description: Python program very slow


code:
import re

my_hostname = "testhostname"
is_valid = re.match("^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$", my_hostname)

if is_valid:
    print "%s is a valid hostname" % my_hostname

rawDescription: python: check if IP or DNS

description: Python: check if IP or DNS


code:
import urllib2
from lxml import etree

data = urllib2.urlopen("https://en.wikipedia.org")
tree = etree.parse(data)
element = tree.xpath('//div[@id="p-logo"]/a')[0]
print tree.getpath(element)

rawDescription: How to get an XPath from selenium webelement or from lxml?

description: Get an XPath from selenium webelement or from lxml


code: print convert(input)

rawDescription: Convert a nested dict to a dict that only include list items that has a unique set of keys


code:
class Example(object):
  def method1(self):
    print 1
  def method2(self):
    print 2
  def __getattribute__(self, name):
    def f():
        # todo: check if it's actually a function or not before calling
        r = object.__getattribute__(self, name)()
        print 'hello world'
        return r
    return f

rawDescription: Making sure each method ends with a specific statement


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

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


code:
def my_print(*args):
    print ' '.join(map(str, args))

rawDescription: print tuple as number of arguments

description: Print tuple as number of arguments


code:
x = set(['item1','item2','item3'])
print str(x)[4:-1]

rawDescription: Writing a Set To a File in Python

description: Writing a Set To a File in Python


code:
def log(self):
    print "logging "+self
str.log = log

str("hello")

print "hello".log()

rawDescription: How do I give a string object a custom method in Python?

description: Give a string object a custom method in Python


code: print funct.__doc__

rawDescription: How do I show my argspec/docstring in bpython?

description: Show my argspec/docstring in bpython


code:
print ['{{{0[score]}-{0[numrep]}}}'.format(d) for d in lst]
print '[{}]'.format(', '.join(['{{{0[score]}-{0[numrep]}}}'.format(d) for d in lst]))

rawDescription: List of dictionaries - how to format print output


code:
import rehash
import pickle
sha=rehash.sha256("Hello ")
s=pickle.dumps(sha.ctx)
sha=rehash.sha256()
sha.ctx=pickle.loads(s)
sha.update("World")
print sha.hexdigest()

rawDescription: Persisting sha256 hash objects?

description: Persisting sha256 hash objects


code:
import numpy as np
strarray = ["535.","535.","534.68"]
floatarray = np.array(filter(None,strarray),dtype='|S10').astype(np.float128)
print floatarray

rawDescription: How to set precision of numpy float array when converting from string array?

description: Set precision of numpy float array when converting from string array


code:
from matplotlib.pyplot import plot, ion, show
ion() # enables interactive mode
plot([1,2,3]) # result shows immediatelly (implicit draw())

print 'continue computation'

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

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


code:
import re
regex = re.compile(ur'(?!^)(?=[A-Z])', re.MULTILINE)
str = u"HotelCityClass"

result = re.sub(regex, " ", str)

rawDescription: Format string by adding a space before each uppercase letter


code:
from multiprocessing import Pool
import os

def gitSync(repo):
    print "I am", repo, "and my cwd is:", os.getcwd()
    os.chdir(repo)
    print "I am", repo, "and my cwd is:", os.getcwd()

if __name__ == '__main__':
    dir = os.getcwd()
    repos = [item for item in os.listdir(dir) if os.path.isdir(os.path.join(dir, item))]
    print repos
    pool = Pool(maxtasksperchild=1)
    pool.map(gitSync, repos)
    pool.close()
    pool.join()

rawDescription: working in different directories (os.chdir) in the same time (parallel threading)

description: Working in different directories (os.chdir) in the same time (parallel threading)


code:
#!/usr/bin/env python
from xml.etree import cElementTree as ET
from datetime import datetime
import re

with open('waterservices.usgs.gov.xml','r') as fi:
    waterData = ''.join(fi.readlines())
waterData = re.sub('ns[12]:', '', waterData)
root = ET.fromstring(waterData)
dates = [v.get('dateTime') for v in root.iter('value')]
valus = [float(v.text) for v in root.iter('value')]
units = [v.text for v in root.iter('variableName')]
print 'valus', valus
print 'units', units
print 'dates', dates
dates = [datetime.strptime(t[:-6], '%Y-%m-%dT%H:%M:%S.%f') for t in dates]
print 'dates', dates
a = zip (valus, units, dates)
for v in a:
    print v

rawDescription: How to get time from this xml tag?

description: Get time from this xml tag


rawDescription: Use regular expression to remove contents in brackets in python


code:
def random_walkers(n_walkers, n_steps, p):
    import numpy as np
    from __future__ import division  # to force division of int to float
    step_matrix = np.random.randint(2, size=(n_walkers, n_steps))
    step_matrix[step_matrix < p] = -1
    total_displacement = np.sum(step_matrix, axis=1).reshape(n_walkers, 1)
    positions = np.zeros((n_walkers, n_steps+1))
    for i in range(n_steps):
        positions[:, i+1] = step_matrix[:, i] + positions[:, i]
    mse = np.sum(np.power((step_matrix - total_displacement), 2), axis=1).reshape(n_walkers,1)/n_steps
    return step_matrix, mse, positions

mat, mse, pos = random_walkers(2, 5, 0.5)
print mat
print mse
print pos

rawDescription: General Random Walk generator questions

description: General Random Walk generator questions


code:
import wx

class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Programmatic binding of accelerators in wxPython", size=(450,150))

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)
        bindings = [
                  (wx.ACCEL_CTRL,  wx.WXK_UP, 'up'),
                  (wx.ACCEL_CTRL,  wx.WXK_DOWN, 'down'),
                  (wx.ACCEL_CTRL,  wx.WXK_LEFT, 'left'),
                  (wx.ACCEL_CTRL,  wx.WXK_RIGHT, 'right'),
                  ]

        accelEntries = []

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

            self.Bind(wx.EVT_MENU, lambda evt, temp=binding[2]: self.on_move(evt, temp), id=eventId)

        accelTable  = wx.AcceleratorTable(accelEntries)
        self.SetAcceleratorTable(accelTable )
     #----------------------------------------------------------------------

    def on_move(self, Event, direction):
        print "You pressed CTRL+"+direction

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


code:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

caps = DesiredCapabilities.CHROME
caps['loggingPrefs'] = {'performance': 'ALL'}
driver = webdriver.Chrome(desired_capabilities=caps)

driver.get('https://stackoverflow.com')

for entry in driver.get_log('performance'):
    print entry

driver.close()

rawDescription: Getting chrome performance and tracing logs


code:
class Info(models.Model):
    user_id = models.BigIntegerField(primary_key=True)
    text = models.TextField()

    def save(self,*args,**kwargs):
        print 1
        is_created = False
        if not self.pk:
            print 2
            is_created = True

        self.pk = 20 #set pk before super call to pass validation

        super(Info, self).save(*args, **kwargs)
        print 3
        if is_created:
            print 4

Info.objects.create(text='wew')
#prints 1 2 3 4

rawDescription: Django custom save not working

description: Django custom save not working


code: print format(N|M, 'b')

rawDescription: Taking a binary input from the user in python for bitwise operations


code:
timestamps = filtered['0_1']
diffs = (timestamps.shift() - timestamps).dropna()
print diffs.to_string()

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


code:
def _yourargs(*names):
        "returns a dict with your named local vars"
        alllocs = inspect.stack()[1][0].f_locals
        return {n:alllocs[n] for n in names}

def askformine(val1, val2, val3=None, val4=5):
        "example to show just those args i'm interested in"
        print _yourargs('val1','val2','val3','val4')

class Obj(object):
        "example inserting some named args as instance attributes"
        def __init__(self, arg1, arg2=4):
                 self.__dict__.update(_yourargs('arg1','arg2'))

rawDescription: Is it possible get a dictionary of passed in parameters similar to kwargs(python)?


code:
for entry in data:
    print entry['description']
    for i, inning in enumerate(entry['innings']):
        print 'Innings {}: {} wickets'.format(i + 1, inning['wickets'])

rawDescription: Decoding nested JSON with multiple 'for' loops


code:
# In logging.py
def log(msg):
  print msg

rawDescription: Globals and singletons in Python

description: Globals and singletons in Python


code:
terms
print terms[0] # str
print terms[1]

rawDescription: How to make Python Interactive Shell print cyrillic symbols?

description: Make Python Interactive Shell print cyrillic symbols


code:
df = df.drop( df[ df['Trustworthy'] == 0 ].tail(4).index )

print df

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


code:
reader = csv.reader(cfeature, delimiter=';', skipinitialspace=True)
for row in reader:
    print '\n'.join(row)

rawDescription: Splitting a CSV row into separate strings


rawDescription: sliding window in numpy

description: Sliding window in numpy


code:
for place in session.query(Place).filter(unaccent(Place.name) == "cafe").all():
    print place.name

rawDescription: SQLALCHEMY ignore accents on query

description: SQLALCHEMY ignore accents on query


code:
def sct(from_date=datetime.datetime.now().date()):
    value = getValueByDate(from_date)

print sct.func_defaults

rawDescription: datetime.datetime.now() returns old value

description: Datetime.datetime.now() returns old value


code:
from time import sleep
print "sub1 output: start"
sleep(5)
print "sub1 output: finish"

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


code:
import email.parser
parser = email.parser.FeedParser()

email = '''\
From: Steve <steve@steve.steve>
To: Someone <someone@foo.com>
Subject: Hey what's up
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=_boundary

This is a message with multiple parts in MIME format.

--_boundary
Content-Type: text/plain

This is the body of the message. No mail server involved.

--_boundary
Content-Type: application/octet-stream
Content-Disposition: attachment; filename=test.png;
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ
bWFnZVJlYWR5ccllPAAAA29JREFUeNosU21oW1UYfs459yPJTZY0SZfFtWnXDzs7BMXBRLY/Ey1M
GIp/KlP3cwgOEcThfoj4Z4whMkGnv1ScFoQVRSmr1hWtOh0rY8zRtdOsTZNtTVqTtLlJ7r059/g2
9cLh3nt43+d9Ps5h9vRZgHMwKDCfQ63+1cec+SNcrYxAlocAxZXouK2Q+FEFhr9k2x+9BeXCZwLw
fTB7+n1AmGBSauz+9yeF+PuEtuvJEEs9AQTTIGSgUYQq/QmZnXRabtcHKnX4baUHmpAOWH3mHJQS
guc+OW+m2Ch7+C1gWy/QagCei/aj6YARAjYKUDfPwM2vTcju488zwZtc6TGw0sS7ZrI6yvYcJzYt
3Lr8DYrZKwSQB1WjnL+GmzPjhGSD7X4FRpof4sXx95QeAfftu8O6M/UayxykhiJkdQE/ffURZqcu
kMYVsmAF13/5DpOfnUVzbY5qCmDdB6Gr34/569l9Giq/vaAlQxY5SNP+QbVkI+iX4dcLQD1LHjC4
1RzCvIby/Xmku+MEKiE6Y4KtzRzVhLfwFCzS5y4ROklFHaQNGquRebNtE3Veab9Ntkh7/xIzUhMK
QhSz+wkgPwCRIoB7FKdCOMDQs6cT6XQAkDZNAzIDEbjEJB6hf4eAFaExHULde4A1v4iWzH4r6Ukd
rtuAFaOOsLEVX8vfSoEYtTdqDuwKg2EEoGs+vDvVVc3TM3Mmu3FgcUlg/NsedA1oiFoFWGGHClub
BChNDfWaiYq9E4XbHp57NofBQQmX9y9rdf7QxXDjxoH4Dgm1ox+Rx0+gI7YNVsCAqbM2AcfzoTc8
tKrr8DdOI566Q14ATey+pJXjh8Zi5R/eSAxXOvYOTmH2/FVEe4dgdT6IaLKrzWB9NQ+7uIDq0jz2
7q8g0UXq5kKNcnTkc3b110tIlC682Rv58LTcpVP+PspzcvN6QKr/LaDl03fHkMBjT3OIJQ/L5aOn
ittfPsn++PkiFZrYWTr3cU/y62N+hiOXM1DKSviNLRNFgCPZJ5Dp8cCXJfLFZ8aWO19/kXPps8vT
E2QwhyMNJCqTr/Zrn74TTBcTiBCDTRokQigav0Ga78bXs95Lp0qxw2cMzZOMDtQWwKbTdJWbyoJo
rqYTzpXRuLo2EmaLfWCK1fzMYoU9MrVm7BvzgqmcyeoweKvd958AAwAU0nrZgWbiHgAAAABJRU5E
rkJggg==
--_boundary--
'''

parser.feed(email)
msg = parser.close()

print "To:", msg['to']
print "From:", msg['from']
print "Subject:", msg['subject']
print "Body:", repr(msg.get_payload())
print ""

for payload in msg.get_payload():
    filename = payload.get_filename()
    if filename:
        print "Got an attachment: %r" % filename,
        print "that is %r bytes long." % len(payload.get_payload().decode('base64'))
    else:
        print "Got a message: %r" % payload.get_payload()

rawDescription: Python email.parser in development

description: Python email.parser in development


code:
class Var(object):
    def __init__(self):
        pass

    @property
    def name(self):
        return self._name( )

    @property
    def length(self):
        return len(self.name)

class Actual(Var):
    def __init__(self, name):
        Var.__init__(self)
        self._text = name

    def _name(self):
        return self._text

    def __rshift__(self, other):
        if isinstance(other, Actual):
            return Deferred(self, other)

        return len(self.name)

    @staticmethod
    def NewFromShiftComputation(sequence):
        x = ' >> '.join(reversed(map(lambda actual: actual.name, sequence)))
        return Actual(x)

class Deferred(Var):
    def __init__(self, *args):
        Var.__init__(self)

        self._items  = [ ]
        self._actual = None  #-- cached "actual"

        for item in args:
            self._items.append(item)

    def _name(self):
        self._assure_actual( )
        return self._actual.name

    def __rshift__(self, other):
        self._actual = None  #-- Invalidate the cached "actual"
        self._items.append(other)
        return self

    def _assure_actual(self):
        if self._actual is None:
            self._actual = Actual.NewFromShiftComputation(self._items)

A = Actual('A')
B = Actual('B')
C = Actual('C')
D = Actual('D')
E = Actual('E')

F = A >> B >> C >> D >> E

print F.name
print F.length

rawDescription: Python operator overloading and operator associativity


code: min(zip(*[iter(lst)]*2),key=lambda (x,y): abs(float(x[1]) - 2))

rawDescription: Performing if inside lambda

description: Performing if inside lambda


code:
import re
s = "nothing"
print re.search(r'(\w+\W*){3}', s).group()
# nothing

rawDescription: string mask and offset with regex

description: String mask and offset with regex


code:
for equip in data.dictWayPts.itervalues():
    for waypt in equip.itervalues():
        print waypt.ID, waypt.EquipClassStr

rawDescription: Class Iterator for Nested (2-D) Dictionary?


code:
import re
splitterForIndexing = re.compile(r"(?:[a-zA-Z0-9\-,]+[a-zA-Z0-9\-])|(?:[,.])")
source = "Hello. 1-methyl-4-phenylpyridinium is ultra-bad. However, 1-methyl-4-phenyl-1,2,3,6-tetrahydropyridine is worse."
print "\n".join( splitterForIndexing.findall(source))

rawDescription: Python: question about parsing human-readable text


code:
for item in bases:
    print item
    return get_bases(item)

rawDescription: generate a dependency tree of the inheritance hierarchy

description: Generate a dependency tree of the inheritance hierarchy


code:
import urllib
import re

url = ('http://www.epicurious.com/articlesguides/bestof/'
       'toprecipes/bestchickenrecipes/recipes/food/views/'
       'My-Favorite-Simple-Roast-Chicken-231348')

sock = urllib.urlopen(url)
ch = sock.read()
sock.close()

#----------------------------------------------------------
patingr = re.compile('
(.+?)
\n')
print
print '\n'.join(repr(mat.group()) for mat in patingr.finditer(ch))

ch = ch.replace('
One 2- to 3-pound farm-raised chicken
',
                "
One 2- to 3-pound farm-raised \nchicken
")
print
print '\n'.join(repr(mat.group()) for mat in patingr.finditer(ch))

patingr = re.compile('
(.+?)
\n',re.DOTALL|re.IGNORECASE)
print
print '\n'.join(repr(mat.group()) for mat in patingr.finditer(ch))

rawDescription: Python HTML parsing specific information within tags


code:
import time
import random
from itertools import count

fps = 5
loop_delta = 1./fps

current_time = target_time = time.clock()
for i in count():
    #### loop frequency evaluation
    previous_time, current_time = current_time, time.clock()
    time_delta = current_time - previous_time
    print 'loop #%d frequency: %s' % (i, 1. / time_delta)

    #### processing
    # processing example that sleeps a random time between 0 and loop_delta/2.
    time.sleep(random.uniform(0, loop_delta / 2.))

    #### sleep management
    target_time += loop_delta
    sleep_time = target_time - time.clock()
    if sleep_time > 0:
        time.sleep(sleep_time)
    else:
        print 'took too long'

rawDescription: Limiting Fps in python

description: Limiting Fps in python


code:
ans = 'door'
word = ['_' for _ in ans]

while '_' in word:
    guess = raw_input('Guess a char')
    for i, c in enumerate(ans):
        if guess.lower() == c.lower():
            word[i] = c
    print word

rawDescription: Displaying if there are redundant characters in a string with a list


code:
import string
from itertools import chain

from nltk.corpus import stopwords
from nltk.probability import FreqDist
from nltk.classify import NaiveBayesClassifier as nbc
from nltk.corpus import CategorizedPlaintextCorpusReader
import nltk

mydir = '/home/alvas/my_movie_reviews'

mr = CategorizedPlaintextCorpusReader(mydir, r'(?!\.).*\.txt', cat_pattern=r'(neg|pos)/.*', encoding='ascii')
stop = stopwords.words('english')
documents = [([w for w in mr.words(i) if w.lower() not in stop and w.lower() not in string.punctuation], i.split('/')[0]) for i in mr.fileids()]

word_features = FreqDist(chain(*[i for i,j in documents]))
word_features = word_features.keys()[:100]

numtrain = int(len(documents) * 90 / 100)
train_set = [({i:(i in tokens) for i in word_features}, tag) for tokens,tag in documents[:numtrain]]
test_set = [({i:(i in tokens) for i in word_features}, tag) for tokens,tag  in documents[numtrain:]]

classifier = nbc.train(train_set)
print nltk.classify.accuracy(classifier, test_set)
classifier.show_most_informative_features(5)

rawDescription: Using my own corpus instead of movie_reviews corpus for Classification in NLTK


code:
def roll_wsum(g,w,p):
    rsum = pd.rolling_apply(g.values,p,lambda x: np.dot(w,x),min_periods=p)
    return pd.Series(rsum,index=g.index)

weights = np.array([0.1,0.1,0.2,0.6])
df['wsum'] = df.groupby('ID')['VALUE'].apply(roll_wsum,weights,4)
print df

rawDescription: Is there a way to do a weight-average rolling sum over a grouping?


code:
def fix_docs(cls):
    for name, func in vars(cls).items():
        if not func.__doc__:
            print func, 'needs doc'
            for parent in cls.__bases__:
                parfunc = getattr(parent, name)
                if parfunc and getattr(parfunc, '__doc__', None):
                    func.__doc__ = parfunc.__doc__
                    break
    return cls

class Animal:
    def walk(self):
        'Walk like a duck'

class Dog(Animal):
    def walk(self):
        pass

Dog = fix_docs(Dog)
print Dog.walk.__doc__

rawDescription: Inheriting methods' docstrings in Python


code: print df.loc[df['product']=='a', ['market_penetration_rate', 'success_rate']] * 100

rawDescription: Pandas conditional multiplication

description: Pandas conditional multiplication


rawDescription: Python: Start each newline with a specific character


code:
import binascii
import MySQLdb
import uuid

conn = MySQLdb.connect(host='localhost')

key = uuid.uuid4()
print 'inserting', repr(key.bytes)
r = conn.cursor()
r.execute('INSERT INTO xyz (id) VALUES (%s)', key.bytes)
conn.commit()

print 'selecting', repr(key.bytes)
r.execute('SELECT added_id, id FROM xyz WHERE id = %s', key.bytes)
for row in r.fetchall():
    print row[0], binascii.b2a_hex(row[1])

rawDescription: python inserting and retrieving binary data into mysql

description: Python inserting and retrieving binary data into mysql


code:
results = s.execute()

for row in results:
    print row

rawDescription: SQLAlchemy: Operating on results

description: SQLAlchemy: Operating on results


code:
s = '''

'''

from chameleon import PageTemplate

pt = PageTemplate(s)

print pt(t='
Hi!
')

rawDescription: Python Pyramid & Chameleon templating language escapes html


code:
def bla(*args, **kwargs):
    profile = kwargs.pop('profile', 'system')
    print 'profile', profile
    print 'args', args
    print 'kwargs', kwargs

rawDescription: Mixing default arguments in Python 2.x

description: Mixing default arguments in Python 2.x


code:
import re
p = re.compile(ur'\[span\b[^]]*][^[]*\[/span\]\s*([^[]*)\[div\b')
test_str = u"[span class=\"review-title\"]Wont open[/span] I have the GS5 and the game wont open. I got this game when i got the first droid. The fact that people havent been able to play since almost 2013 is bull. Please fix this or there isnt a point in even having the game on the server. [div class=\"review-link\" "
print re.search(p, test_str).group(1)

rawDescription: Advanced string parsing in python

description: Advanced string parsing in python


code:
import textwrap
input_text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
indentation = ';' + ' ' * 11
result = textwrap.fill(
    input_text, width=80,
    initial_indent=indentation, subsequent_indent=indentation)
print result
# produce a sample with newlines by inserting these around 170 characters

input_text = textwrap.fill(input_text, width=170)
print input_text
params = {
    'width': 80,
    'initial_indent': indentation,
    'subsequent_indent': indentation
}
result = '\n'.join([textwrap.fill(line, **params)
                    for line in input_text.splitlines()])
print result

rawDescription: In Python, is there a way to insert a line break and spaces every 68 characters of a string?


code:
import os
allfiles = os.listdir('C:\\')
onlytxt = [x for x in allfiles if x.endswith('.txt')]
print onlytxt

rawDescription: Put names of .txt files into a list

description: Put names of .txt files into a list


code:
import numpy as np

def bmatrix(a):
    """Returns a LaTeX bmatrix

    :a: numpy array
    :returns: LaTeX bmatrix as a string
    """
    if len(a.shape) > 2:
        raise ValueError('bmatrix can at most display two dimensions')
    lines = str(a).replace('[', '').replace(']', '').splitlines()
    rv = [r'\begin{bmatrix}']
    rv += ['  ' + ' & '.join(l.split()) + r'\\' for l in lines]
    rv +=  [r'\end{bmatrix}']
    return '\n'.join(rv)

A = np.array([[12, 5, 2], [20, 4, 8], [ 2, 4, 3], [ 7, 1, 10]])
print bmatrix(A) + '\n'

B = np.array([[1.2], [3.7], [0.2]])
print bmatrix(B) + '\n'

C = np.array([1.2, 9.3, 0.6, -2.1])
print bmatrix(C) + '\n'

rawDescription: Numpy 2d and 1d array to latex bmatrix

description: Numpy 2d and 1d array to latex bmatrix


code:
import re

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

rawDescription: learnpython.org modules exercise

description: Learnpython.org modules exercise


code:
import module1

class IN(object):
    def __init__(self):
        print "I am the new IN"

class C2(module1.C):
    def __init__(self, x):
        super(C2, self).__init__(x)

print "\n===Before monkey patching==="
C2(1).func()
#monkey patching old In with new In
module1.IN = IN
print "\n===After monkey patching==="
C2(1).func()

rawDescription: How can I override class call inside of an imported class in python?

description: Override class call inside of an imported class in python


code:
import numpy as np
import AgglomerativeClustering # Make sure to use the new one!!!
d = np.array(
    [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
    ]
)

clustering = AgglomerativeClustering(n_clusters=2, compute_full_tree=True,
    affinity='euclidean', linkage='complete')
clustering.fit(d)
print clustering.distance

rawDescription: sklearn agglomerative clustering linkage matrix

description: Sklearn agglomerative clustering linkage matrix


code:
old_raw_input = raw_input

def new_raw_input(prompt):
    result = old_raw_input(prompt)
    if sys.stdin is not sys.__stdin__:
        print result
    return result

raw_input = new_raw_input

rawDescription: How to display the redirected stdin in Python?

description: Display the redirected stdin in Python


code:
import re

pat = ('([2-9])'        # Integer Number 1
       '([p-z])'        # Any Single Word Character (Not Whitespace) 1
       '([a-h])'        # Any Single Word Character (Not Whitespace) 2
       '([2-9])'        # Integer Number 2
       '([a-z]*[p-z]+)' # Word 1
       '([2-9])'        # Integer Number 3
       '([p-z])'        # Any Single Word Character (Not Whitespace) 3
       '(.)'            # Any Single Character 1
       '([p-z])'        # Any Single Word Character (Not Whitespace) 4
       )
rg = re.compile(pat)

txt = 'jiji4pa6fmlgkfmoaz8p#q,,,,,,,,,,'
m = rg.search(txt)
if m:
    text2 = "%s%s%s%s%s%s%s%s%s" % m.groups()
    print text2

# prints 4pa6fmlgkfmoaz8p#q

rawDescription: Python Regex Problems

description: Python Regex Problems


code:
class carInput:
  def __init__(self):
    self.string = 'hi'

car = carInput()

print car.string

rawDescription: Can't access object attribute after instantiating object in console


code:
import argparse

p = argparse.ArgumentParser()
p.add_argument('-l', '--login')
p.add_argument('host')
p.add_argument('cmd', nargs=argparse.REMAINDER)
print p.parse_args('-la server ls'.split())
print p.parse_args('server ls -la'.split())

rawDescription: argparse: require optional arguments to precede positional arguments

description: Argparse: require optional arguments to precede positional arguments


code:
treasure = "Success!"

def primary(input):
    def second():
        print "something"

    def third():
        global treasure
        treasure = 'changed!'
        print treasure

    third()

rawDescription: Access a variable defined in function

description: Access a variable defined in function


code:
for k,v in groupby(lst,key=lambda x:x['date'][:7]):
    print {'date':k+'-01','value':sum(int(d['value']) for d in v)}

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

description: Group this list of dicts by the same month


code:
from pandas import *
left = DataFrame({'url': ['foo.com', 'bar.com', 'foo.com', 'tmp', 'foo.com'], 'action': [0, 1, 0, 2, 4]})
left["klass"] = NaN
right1 = DataFrame({'url': ['foo.com', 'tmp'], 'klass': [10, 20]})
right2 = DataFrame({'url': ['bar.com'], 'klass': [30]})

left["klass"] = left.klass.combine_first(right1.set_index('url').klass[left.url].reset_index(drop=True))
left["klass"] = left.klass.combine_first(right2.set_index('url').klass[left.url].reset_index(drop=True))

print left

rawDescription: What is the equivalent to R's match() for python Pandas/numpy?


code:
def intX_raw(value,sign_bit):
  max_pos=(2**(sign_bit-1))-1  # maximum positive val
  max_neg=(-max_pos)-1
  max_val=(max_pos + (-max_neg))
  print "max positive:",max_pos
  print "max negative:",max_neg
  print "max possible:",max_val
  if value>max_val:
    print"Value too high for int%d"%sign_bit,value
    return 255
  if value<0:
    diff=abs(max_neg+(-value))+1
    result=max_pos+diff
    return result
  else: return value

def intX_val(value,sign_bit):
  max_pos=(2**(sign_bit-1))-1  # maximum positive val
  max_neg=(-max_pos)-1
  max_val=(max_pos + (-max_neg))
  print "max positive:",max_pos
  print "max negative:",max_neg
  print "max possible:",max_val
  if value>max_val:
    print"Value too high for int%d"%sign_bit,value
    return 255

  if value>max_pos:
    print "val over max_pos:",value
    diff=value-max_pos
    result=max_neg+(diff-1)
    return result
  else: return value

rawDescription: How to create or interpret custom data type (4bit signed)

description: Create or interpret custom data type (4bit signed)


code:
freq = defaultdict(int)
for word in text:
    freq[word] += 1

ranks = sorted(freq.keys(), key=lambda k: (-freq[k], text.index(k)))
ranks = dict(zip(ranks, range(1, len(ranks)+1)))

for (a, b), count in counts.iteritems():
    print ranks[a], ranks[b], count

rawDescription: Bigrams and ranks of words

description: Bigrams and ranks of words


code:
import pandas as pd
from glob import glob

path = "/home/nanashi/Documents/Python 2.7/Scrapers/Scrapy/itbooks"
filenames = glob(path + "/*.csv")

for filename in filenames:
    specname = filename.split("/")[-1]
    print filename
    print specname
    dfs = pd.read_csv(filename)
    df = dfs.transpose()
    df.to_csv("transposed/%s" % specname)

rawDescription: Need to write multiple csv files to new folder


code:
import logging

class LogList(list):
   def write(self,data):
       self.append(data)

LL = LogList()
logging.basicConfig(stream = LL,level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.error('Wow, this is bad')
logging.warning('And this, too')
logging.debug('foobar')
logging.warning('baz')

for line in sorted(LL):
   print line[:-1]

rawDescription: Python - Sort Log Messages By Level before Printing


code:
class one:
    def funct1(self):
        print 'class one'

class two(one):
    def funct2(self):
        one.funct1(self)

ob2 = two()
ob2.funct2()

rawDescription: Simple python communication class error?

description: Simple python communication class error


code:
output = '''\
root
  +--sub1
  +--sub2
  |  +--sub2sub1
  +--sub3
     +--sub3sub1
     |  +--sub3sub1sub1
     +--sub3sub2
'''
lines = output.splitlines()[::-1]
width = max(len(l) for l in lines)
reversed = []
for line in lines:
    tree, dashes, label = line.rpartition('--')
    tree = (tree + dashes)[::-1]
    line = '{1:>{0}}{2}'.format(width - len(tree), label, tree).rstrip()
    reversed.append(line)

print '\n'.join(reversed)

rawDescription: How to print a reversed ascii tree?

description: Print a reversed ascii tree


code:
def new(data):
    d1 = pd.melt(data, id_vars=['country','region'], value_vars=['max', 'min','default']).sort_values(['country','region']).reset_index(drop=True)

    #fill NaN in column value method ffill (propagate last valid observation forward to next valid)
    d1['value'] = d1['value'].fillna(method='ffill')

    d1['key'] = 1
    #you can use double d1
    d3 = pd.merge(d1, d1, on='key')
    d3 = d3.drop(['key'],axis=1)

    #filter columns by conditions - boolean indexing
    d3 = d3[(d3['region_x'] == d3['region_y']) & (d3['country_x'] == d3['country_y'])].reset_index(drop=True)

    #if condition is true get value_x else value_y
    #if neccesarry, convert to int by astype(int)
    d3['rate'] = np.where(np.in1d(d3['country_x'], 'A'), d3['value_x'],d3['value_y']).astype(int)

    d3 = d3.drop(['value_x','country_y','region_y','value_y'],axis=1)

    d3.columns = ['country','region','Option1','Option2','measure']
    d3 = d3[['country','region','measure','Option1','Option2']]
    return d3

print old(data)
print new(data1)

rawDescription: How to get columns as row combinations in python?

description: Get columns as row combinations in python