code:
import json
import pyspark
sc = pyspark.SparkContext()

hadoopConf=sc._jsc.hadoopConfiguration()
hadoopConf.get("fs.gs.system.bucket")

conf = {"mapred.bq.project.id": "<project_id>", "mapred.bq.gcs.bucket": "<bucket>",
    "mapred.bq.input.project.id": "publicdata",
    "mapred.bq.input.dataset.id":"samples",
    "mapred.bq.input.table.id": "shakespeare"  }

tableData = sc.newAPIHadoopRDD(
    "com.google.cloud.hadoop.io.bigquery.JsonTextBigQueryInputFormat",
    "org.apache.hadoop.io.LongWritable", "com.google.gson.JsonObject",
    conf=conf).map(lambda k: json.loads(k[1])).map(lambda x: (x["word"],
    int(x["word_count"]))).reduceByKey(lambda x,y: x+y)

print tableData.take(10)

rawDescription: load table from bigquery to spark cluster with pyspark script

description: Load table from bigquery to spark cluster with pyspark script


code:
import Tkinter as tk
import tkMessageBox

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.button = tk.Button(self, text="Push me", command=self.OnButton)
        self.button.pack()

    def OnButton(self):
        result = tkMessageBox.askokcancel(title="File already exists",
                                       message="File already exists. Overwrite?")
        if result is True:
            print "User clicked Ok"
        else:
            print "User clicked Cancel"

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

rawDescription: Python TkMessageBox Question not working!


code:
import json
import urllib2

response = urllib2.urlopen('http://api.formice.com/mouse/stats.json?n=Safwanrockz')
data = json.loads(response.read())

print type(data)  # prints <type 'dict'>

print data['tribe']  # prints "Three Feathers"

rawDescription: Copy Text From an Opened URL in Python

description: Copy Text From an Opened URL in Python


code:
import re
regex = re.compile("(?P<number>[A-Za-z]+)")
print regex.search("Hi This is a Test.").group('number')

rawDescription: python django regular express confused

description: Python django regular express confused


code:
response = urlopen(request)
request_body = response.read()
request_url  = response.geturl()
print 'URL: %s\nRequest_Body: %s' % ( request_url, request_body )

rawDescription: Get content of http request as well as the response url in single request


code:
testString = "She walked the dog to the park and played ball with the dog When she threw the ball to the dog the dog missed the ball and ran to the other side of the park to fetch it"

dic = {}

words = testString.split()

for raw_word in words:
    word = raw_word.lower()
    if word in dic:
        dic[word] += 1
    else:
        dic[word] = 1

print dic

rawDescription: Count number of times each word has repeated in a string?


code:
import smtplib
####import sys#### duplicate
from email.parser import Parser
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import ConfigParser

def mail_man(cfg_file, event_file):
    # Parse email configs from cfg file
    Config = ConfigParser.ConfigParser()
    Config.read(str(cfg_file))

    mail_man_start = Config.get('DC_MS', 'ms')
    security = Config.get('DC_MS', 'ms_security')
    add_attachment = Config.get('DC_MS', 'ms_attach')
    try:
        if mail_man_start == "y" or mail_man_start == "Y":
            fromaddr = Config.get("DC_MS", "ms_from")
            addresses = [Config.get("DC_MS", "ms_sendto")] + [Config.get("DC_MS", "ms_cc")] + [Config.get("DC_MS", "ms_bcc")]

            msg = MIMEMultipart()  # creates multipart email
            msg['Subject'] = Config.get('DC_MS', 'ms_subject')  # sets up the header
            msg['From'] = Config.get('DC_MS', 'ms_from')
            msg['To'] = Config.get('DC_MS', 'ms_sendto')
            msg['reply-to'] = Config.get('DC_MS', 'ms_replyto')
            msg['X-Priority'] = Config.get('DC_MS', 'ms_importance')
            msg['CC'] = Config.get('DC_MS', 'ms_cc')
            msg['BCC'] = Config.get('DC_MS', 'ms_bcc')
            msg['Return-Receipt-To'] = Config.get('DC_MS', 'ms_rrr')
            msg.preamble = 'Event Notification'

            message = '... use this to add a body to the email detailing event. dumpcap.py location??'
            msg.attach(MIMEText(message))   # attaches body to email

            # Adds attachment if ms_attach = Y/y
            if add_attachment == "y" or add_attachment == "Y":
                attachment = open(event_file, "rb")
                # Encodes the attachment and adds it to the email
                part = MIMEBase('application', 'octet-stream')
                part.set_payload(attachment.read())
                encoders.encode_base64(part)
                part.add_header('Content-Disposition', "attachment; filename = %s" % event_file)

                msg.attach(part)
            else:
                print "No attachment sent."

            server = smtplib.SMTP(Config.get('DC_MS', 'ms_smtp_server'), Config.get('DC_MS', 'ms_smtp_port'))
            server.ehlo()
            server.starttls()
            if security == "y" or security == "Y":
                server.login(Config.get('DC_MS', 'ms_user'), Config.get('DC_MS', 'ms_password'))
            text = msg.as_string()
            max_size = Config.get('DC_MS', 'ms_maxattach')
            msg_size = sys.getsizeof(msg)
            if msg_size <= max_size:
                server.sendmail(fromaddr, addresses, text)
            else:
                print "Your message exceeds maximum attachment size.\n Please Try again"
            server.quit()
            attachment.close()
        else:
            print "Mail_man not activated"
    except:
       print "Error! Something went wrong with Mail Man. Please try again."

rawDescription: Python SMTP/MIME Message body

description: Python SMTP/MIME Message body


code:
from bs4 import BeautifulSoup

html="""

This is the first paragraph with some details

user1This is opening contents for user1

This is the contents from user1

This is more content from user1

user2This is opening contents for user2

This is the contents from user2

This is more content from user1

"""

soup = BeautifulSoup(html)
for i in soup.find_all('a'):
  print 'name:', i.text
  for s in [i, i.parent.find_next_sibling()]:
    while s <> None:
      if s.find('a') <> None:
        break
      print 'contents:', s.text
      s = s.find_next_sibling()

rawDescription: Find all the contents between two tags in python


code:
import time
import pytz
import datetime

local_names = []
if time.daylight:
    local_offset = time.altzone
    localtz = time.tzname[1]
else:
    local_offset = time.timezone
    localtz = time.tzname[0]

local_offset = datetime.timedelta(seconds=-local_offset)

for name in pytz.all_timezones:
    timezone = pytz.timezone(name)
    if not hasattr(timezone, '_tzinfos'):
        continue#skip, if some timezone doesn't have info
    # go thru tzinfo and see if short name like EDT and offset matches
    for (utcoffset, daylight, tzname), _ in timezone._tzinfos.iteritems():
        if utcoffset == local_offset and tzname == localtz:
            local_names.append(name)

print local_names

rawDescription: Get the Olson TZ name for the local timezone?


code:
import subprocess
p = subprocess.Popen("some_long_command",stdout=subprocess.PIPE)
for line in iter(p.stdout.readline, ""):
    print line

rawDescription: Need to read from long running os command with Python 2.4


code:
alist = ['ROOT', ['S', ['NP', ['PRP', 'We']], ['VP', ['VBP', 'have'], ['VP', ['VBN', 'received'], ['NP', ['NN', 'information']],
        ['PP', ['IN', 'from'], ['NP', ['NP', ['DT', 'a'], ['NN', 'source']], ['VP', ['VBN', 'entitled'], ['PP', ['TO', 'to'],
        ['NP', ['NN', 'belief']]], [',', ','], ['SBAR', ['IN', 'that'], ['S', ['NP', ['NNP', 'Lincoln']], ['VP', ['VP', ['VBZ', 'has'],
        ['VP', ['VBN', 'paid'], ['NP', ['DT', 'a'], ['JJ', 'hurried'], ['NN', 'visit']], ['PP', ['TO', 'to'],
        ['NP', ['NP', ['DT', 'the'], ['NNP', 'Army']], ['PP', ['IN', 'of'], ['NP', ['DT', 'the'], ['NNP', 'Potomac']]]]],
        [',', ','], ['PRN', ['-LRB-', '-LRB-'], ['ADVP', ['RB', 'now']], ['ADJP', ['JJ', 'burrowing'], ['PP', ['IN', 'on'],
        ['NP', ['NP', ['DT', 'the'], ['NN', 'north'], ['NN', 'bank']], ['PP', ['IN', 'of'], ['NP', ['DT', 'the'], ['NNP', 'James']]],
        [',', ',']]]], ['-RRB-', '-RRB-']]]], ['CC', 'and'], ['VP', ['VBD', 'satisfied'], ['NP', ['PRP', 'himself']],
        [',', ','], ['PP', ['IN', 'by'], ['NP', ['JJ', 'personal'], ['NN', 'observation']]], [',', ','],
        ['PP', ['IN', 'in'], ['NP', ['NN', 'regard']]], ['PP', ['TO', 'to'], ['NP', ['NP', ['DT', 'the'], ['JJ', 'true'], ['NN', 'situation']],
        ['PP', ['IN', 'of'], ['NP', ['NNS', 'affairs']]]]]]]]]]]]]], ['.', '.']]]

def PullNP(NNP, NPLists):
    if NNP in NPLists:
        print "Pulling relevant NP"
        print NNP
        yield NNP
    for thing in NPLists:
        if NNP in thing:
            for nnp in PullNP(thing, NPLists):
                yield nnp
        else:
            for s in thing:
                if isinstance(s, list) and NNP in s:
                    for nnp in PullNP(s, NPLists):
                        yield nnp

def RecurseNNP(alist, pastlists=None, count=None):
    if pastlists is None: pastlists = []
    if count is None: count = 0
    if 'NNP' in alist[0]:
        NNPs = PullNP(alist, pastlists)
        print NNPs
        for np in NNPs:
            print np
    else:
        if str(type(alist)) == "<type 'list'>":
            if alist[0] == 'NP':
                pastlists.append(alist)
            for x in alist[1:]:
                RecurseNNP(x, pastlists, count)

RecurseNNP(alist)

rawDescription: Python generator within a recursive function


code:
query="select 1<2 as val;"
result=plpy.execute(query)
if result[0]["val"]:
   print 'of corse: 1 < 2'
else:
   print 'this will never be printed'

rawDescription: How to get the result of a query executed by plpy

description: Get the result of a query executed by plpy


code:
import re

# Reading File Data
handle = open("input.txt", "r")
input_file_read = "".join(handle.readlines())
handle.close()
# print input_file_read
_PATTERN_ = re.compile('^[\s]*Label[\s]*(\w)+[\s\n\{]*([^\}]+)', re.MULTILINE);
_DEFAULT_VALUE = 1
_Myarray = []
for match in _PATTERN_.finditer(input_file_read):
    Group_name = match.group(1).strip()
    # print Group_name
    elements = match.group(2).replace(',', '').split()
    # print elements
    # _Myarray.append([Group_name, i, _DEFAULT_VALUE] for i in elements)
    for i in elements:
        _Myarray.append([Group_name, i, _DEFAULT_VALUE])

print _Myarray

rawDescription: Track name and list elements and give them default values extracted from a file: Python


code:
from dpkt.ip import IP, IP_PROTO_UDP
from dpkt.udp import UDP
ip = IP('E\x00\x00"\x00\x00\x00\x00@\x11r\xc0\x01\x02\x03\x04\x01\x02\x03\x04\x00o\x00\xde\x00\x0e\xbf5foobar')
#if ip.p == IP_PROTO_UDP:  # checking for protocol field in ip header
if type(ip.data) == UDP :  # checking of type of data that was recognized by dpkg
    udp = ip.data
    print udp.sport
else:
    print "Not UDP"

rawDescription: python / dpkt: Find out if packet is a tcp packet or a udp packet ,

description: Python / dpkt: Find out if packet is a tcp packet or a udp packet ,


code:
from jenkinsapi.jenkins import Jenkins
job_name = 'example_job'
build_number = 123
server = Jenkins('http://localhost:8080', username='user', password='passwd')
build = server.get_job(job_name).get_build(build_number)
rev = build.get_revision_branch()[0].get('name')
print rev
rev.rsplit('/', 1)[1]

rawDescription: python - how to get branch name from a specific build from jenkins?

description: Python - how to get branch name from a specific build from jenkins


code:
from datetime import datetime
light_on = datetime.strptime('0500', '%H%M')
light_off = datetime.strptime('2300', '%H%M')
print light_off - light_on

rawDescription: Subtracting times taken from a .csv in python


code:
import Tkinter as tk # python 2.7

class Example(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        button = tk.Button(self, text="Open window", command=self.on_button)
        button.pack()

    def on_button(self):
        var, note = Update()
        a = var.get()
        b = note.get()
        print "a: '%s' b: '%s'" % (a,b)

def Update():
    Up = tk.Toplevel()
    Up.title("Update")
    tk.Label(Up, text ="Update", font=('Times', 20)).grid(row=0)

    var = tk.IntVar()
    tk.Radiobutton(Up, text="Fully", variable=var, value=1).grid(row=2, sticky="w")
    tk.Radiobutton(Up, text="Partly", variable=var, value=2).grid(row=3, sticky="w")
    tk.Radiobutton(Up, text="Un", variable=var, value=3).grid(row=4, sticky="w")

    evar = tk.StringVar()
    note = tk.Entry(Up, width=30, font=('Arial', 12,), textvariable=evar)
    note.grid(row=6)

    Button = tk.Button(Up, text ="Task Complete and Send Invoice", command = Up.destroy).grid(row =7)

    Up.wait_window()
    print "done waiting..."
    return (var, evar)

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

rawDescription: How to get the value from tkinter toplevel in side a 0 attribute function?

description: Get the value from tkinter toplevel in side a 0 attribute function


code:
import sys
from gevent import server
from gevent.baseserver import _tcp_listener
from gevent import pywsgi
from gevent.monkey import patch_all; patch_all()
from multiprocessing import Process, current_process, cpu_count

def hello_world(env, start_response):
    if env['PATH_INFO'] == '/':
        start_response('200 OK', [('Content-Type', 'text/html')])
        return ["hello world"]
    else:
        start_response('404 Not Found', [('Content-Type', 'text/html')])
        return ['
Not Found
']

listener = _tcp_listener(('127.0.0.1', 8001))

def serve_forever(listener):
    pywsgi.WSGIServer(listener, hello_world).serve_forever()

number_of_processes = 5
print 'Starting %s processes' % number_of_processes
for i in range(number_of_processes):
    Process(target=serve_forever, args=(listener,)).start()

serve_forever(listener)

rawDescription: Gevent Pywsgi Server - Multiprocessing?

description: Gevent Pywsgi Server - Multiprocessing


code:
feeds = {}
for item in whatever:
    feeds[item[0]] = lw.twitterWeather(*item[1:])

def getWeather(feeds, accountList):
    for item in accountList:
        print '%s: %s' % (item, feeds[item].parseFeed())

rawDescription: Pass instance as function argument

description: Pass instance as function argument


code:
objlist = range(10)
for j in range(1,3):
  print objlist[1+ j*3:4+ j*3]

rawDescription: is python list comprehension possible inside a for loop?

description: Is python list comprehension possible inside a for loop


code:
s = "33.2 + 6 * 0xFF - 0744"
from StringIO import StringIO
import tokenize
t = list(tokenize.generate_tokens(StringIO(s).readline))
t
nums = [eval(i[1]) for i in t if i[0] == tokenize.NUMBER]
nums
print map(type, nums)

rawDescription: Regular expression to match a Python integer literal


code:
import Queue
import threading

class StuffDoer(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.setDaemon(True)
        self.max_n_times = 0
        self.total_n_times = 0
        self.do_queue = Queue.Queue()

    def run(self):
        # this part is outside of my control
        while True:
            self._do_stuff()
            # do other stuff

    def _do_stuff(self):
        # this part is under my control
        if self.total_n_times >= self.max_n_times:
            try:
                self.max_n_times += self.do_queue.get(block=False)
            except Queue.Empty, e:
                pass
        if self.max_n_times > self.total_n_times:
            # do stuff that must execute in the background thread
            self.total_n_times += 1
            if self.total_n_times >= self.max_n_times:
                self.do_queue.task_done()

sd = StuffDoer()
sd.start()

def do_stuff(n_times):
    sd.do_queue.put(n_times)
    sd.do_queue.join()
    assert (sd.total_n_times == sd.max_n_times)

rawDescription: Block main thread until python background thread finishes side-task


code:
resp = mechanize.Request(url, data)
cj.add_cookie_header(resp)
res1 = mechanize.urlopen(resp)
print res1.read()

rawDescription: How can I see the output HTML

description: See the output HTML


code:
import sys,  traceback

filename = 'tempfile'

def tail(filename):
    fd = open(filename)
    while True:
        try:
            line = fd.readline()
        except KeyboardInterrupt:
            print 'keyboard interrupt here'
            yield fd
        if not line:
            continue
        else:
            yield line

try:
    genObj = tail(filename)
    valfromgen= genObj.next()
    while valfromgen:
        if isinstance(valfromgen, file):
            print 'Closing this file now as `tail` yielded a file descriptor'
            valfromgen.close()
            genObj.close()
            break
        print 'Yielded line: ', valfromgen
        valfromgen= genObj.next()

    print 'Just in order to check that things are in order, the following line will raise StopIteration. If it raises, it means we are good.'
    print genObj.next()
except:
    traceback.print_exc()

rawDescription: Exception while handling generator.send() in try...except block


code:
import wx
import wx.lib.delayedresult as delayedresult

class FrameSimpleDelayedBase(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        pnl = wx.Panel(self)
        self.checkboxUseDelayed = wx.CheckBox(pnl, -1, "Using delayedresult")
        self.buttonGet = wx.Button(pnl, -1, "Get")
        self.buttonAbort = wx.Button(pnl, -1, "Abort")
        self.slider = wx.Slider(pnl, -1, 0, 0, 10, size=(100,-1),
                                style=wx.SL_HORIZONTAL|wx.SL_AUTOTICKS)
        self.textCtrlResult = wx.TextCtrl(pnl, -1, "", style=wx.TE_READONLY)

        self.checkboxUseDelayed.SetValue(1)
        self.checkboxUseDelayed.Enable(False)
        self.buttonAbort.Enable(False)

        vsizer = wx.BoxSizer(wx.VERTICAL)
        hsizer = wx.BoxSizer(wx.HORIZONTAL)
        vsizer.Add(self.checkboxUseDelayed, 0, wx.ALL, 10)
        hsizer.Add(self.buttonGet, 0, wx.ALL, 5)
        hsizer.Add(self.buttonAbort, 0, wx.ALL, 5)
        hsizer.Add(self.slider, 0, wx.ALL, 5)
        hsizer.Add(self.textCtrlResult, 0, wx.ALL, 5)
        vsizer.Add(hsizer, 0, wx.ALL, 5)
        pnl.SetSizer(vsizer)
        vsizer.SetSizeHints(self)

        self.Bind(wx.EVT_BUTTON, self.handleGet, self.buttonGet)
        self.Bind(wx.EVT_BUTTON, self.handleAbort, self.buttonAbort)

class FrameSimpleDelayed(FrameSimpleDelayedBase):
    """This demos simplistic use of delayedresult module."""

    def __init__(self, *args, **kwargs):
        FrameSimpleDelayedBase.__init__(self, *args, **kwargs)
        self.jobID = 0
        self.abortEvent = delayedresult.AbortEvent()
        self.Bind(wx.EVT_CLOSE, self.handleClose)

    def setLog(self, log):
        self.log = log

    def handleClose(self, event):
        """Only needed because in demo, closing the window does not kill the
        app, so worker thread continues and sends result to dead frame; normally
        your app would exit so this would not happen."""
        if self.buttonAbort.IsEnabled():
            self.log( "Exiting: Aborting job %s" % self.jobID )
            self.abortEvent.set()
        self.Destroy()

    def handleGet(self, event):
        """Compute result in separate thread, doesn't affect GUI response."""
        self.buttonGet.Enable(False)
        self.buttonAbort.Enable(True)
        self.abortEvent.clear()
        self.jobID += 1

        self.log( "Starting job %s in producer thread: GUI remains responsive"
                  % self.jobID )
        delayedresult.startWorker(self._resultConsumer, self._resultProducer,
                                  wargs=(self.jobID,self.abortEvent), jobID=self.jobID)

    def _resultProducer(self, jobID, abortEvent):
        """Pretend to be a complex worker function or something that takes
        long time to run due to network access etc. GUI will freeze if this
        method is not called in separate thread."""
        import time
        count = 0
        while not abortEvent() and count < 50:
            time.sleep(0.1)
            count += 1
        return jobID

    def handleAbort(self, event):
        """Abort the result computation."""
        self.log( "Aborting result for job %s" % self.jobID )
        self.buttonGet.Enable(True)
        self.buttonAbort.Enable(False)
        self.abortEvent.set()

    def _resultConsumer(self, delayedResult):
        jobID = delayedResult.getJobID()
        assert jobID == self.jobID
        try:
            result = delayedResult.get()
        except Exception, exc:
            self.log( "Result for job %s raised exception: %s" % (jobID, exc) )
            return

        # output result
        self.log( "Got result for job %s: %s" % (jobID, result) )
        self.textCtrlResult.SetValue(str(result))

        # get ready for next job:
        self.buttonGet.Enable(True)
        self.buttonAbort.Enable(False)

class FrameSimpleDirect(FrameSimpleDelayedBase):
    """This does not use delayedresult so the GUI will freeze while
    the GET is taking place."""

    def __init__(self, *args, **kwargs):
        self.jobID = 1
        FrameSimpleDelayedBase.__init__(self, *args, **kwargs)
        self.checkboxUseDelayed.SetValue(False)

    def setLog(self, log):
        self.log = log

    def handleGet(self, event):
        """Use delayedresult, this will compute result in separate
        thread, and will affect GUI response because a thread is not
        used."""
        self.buttonGet.Enable(False)
        self.buttonAbort.Enable(True)

        self.log( "Doing job %s without delayedresult (same as GUI thread): GUI hangs (for a while)" % self.jobID )
        result = self._resultProducer(self.jobID)
        self._resultConsumer( result )

    def _resultProducer(self, jobID):
        """Pretend to be a complex worker function or something that takes
        long time to run due to network access etc. GUI will freeze if this
        method is not called in separate thread."""
        import time
        time.sleep(5)
        return jobID

    def handleAbort(self, event):
        """can never be called"""
        pass

    def _resultConsumer(self, result):
        # output result
        self.log( "Got result for job %s: %s" % (self.jobID, result) )
        self.textCtrlResult.SetValue(str(result))

        # get ready for next job:
        self.buttonGet.Enable(True)
        self.buttonAbort.Enable(False)
        self.jobID += 1

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

class TestPanel(wx.Panel):
    def __init__(self, parent, log):
        self.log = log
        wx.Panel.__init__(self, parent, -1)

        vsizer = wx.BoxSizer(wx.VERTICAL)
        b = wx.Button(self, -1, "Long-running function in separate thread")
        vsizer.Add(b, 0, wx.ALL, 5)
        self.Bind(wx.EVT_BUTTON, self.OnButton1, b)

        b = wx.Button(self, -1, "Long-running function in GUI thread")
        vsizer.Add(b, 0, wx.ALL, 5)
        self.Bind(wx.EVT_BUTTON, self.OnButton2, b)

        bdr = wx.BoxSizer()
        bdr.Add(vsizer, 0, wx.ALL, 50)
        self.SetSizer(bdr)
        self.Layout()

    def OnButton1(self, evt):
        frame = FrameSimpleDelayed(self, title="Long-running function in separate thread")
        frame.setLog(self.log.WriteText)
        frame.Show()

    def OnButton2(self, evt):
        frame = FrameSimpleDirect(self, title="Long-running function in GUI thread")
        frame.setLog(self.log.WriteText)
        frame.Show()

rawDescription: Thread safety in wxpython

description: Thread safety in wxpython


code:
buf = np.memmap(fname, mode="readwrite", dtype="uint32", shape=(n,n))
buf[:] = 0
img = Image.frombuffer('RGBA', (n, n), buf)
img.readonly = 0
dr = ImageDraw.Draw(img)
dr.rectangle(((0,0), (n,n)), fill="red")
print buf[0,0]
# 4278190335

rawDescription: numpy.memmap to Image.frombuffer - without copying

description: Numpy.memmap to Image.frombuffer - without copying


code:
y = x**3 +  x**2 + 3*x +5
def foo(x):
 print 'got',x
foo(y)

rawDescription: Sympy type symbols to Python code

description: Sympy type symbols to Python code


code:
# Compute "a op b", returning None if the result
# is no good (eg: 9/0 or too big).
def do_op(a, op, b):
    if op == '+':
        return a + b
    if op == '-':
        return a - b
    if op == '*':
        return a * b
    if op == '/':
        if b == 0 or a % b != 0:
            return None
        return a // b
    if op == '**':
        # Disallow arguments that would result
        # in fractions or huge numbers, being careful
        # to allow valid results.
        if a == 1:
            return a
        if a == -1:
            return -1 if b % 2 else 1
        if a == 0 and b == 0:
            return None
        if b < 0 or b > 20 or a > 10000 or a < -10000:
            return None
        return a ** b
    assert False

# Generates expressions that result in the given target.
# ops is the a record of the operations applied so far,
# stack is the evaluation stack, and num is the first
# digit that we've not pushed yet.
def sums(ops, stack, num, target):
    if not num and len(stack) == 1:
        if stack[0] == target:
            yield ops
        return

    # If num is 7, say, try pushing 7, 76, 765, 7654, ..., 7654321.
    k = num
    for i in xrange(num, 0, -1):
        for s in sums(ops + [k], stack + [k], i-1, target):
            yield s
        k = 10 * k + (i - 1)

    # If we've less that 2 things on the stack, we can't apply
    # any operations.
    if len(stack) < 2:
        return

    # Try each of the possible ops in turn.
    for op in ['+', '-', '*', '/', '**']:
        result = do_op(stack[-2], op, stack[-1])
        if result is None:
            continue
        for s in sums(ops + [op], stack[:-2] + [result], num, target):
            yield s

# Convert a list of operations that represent an expression in RPN
# into infix notation. Every operation is bracketed, even when
# that's redundant.
def to_infix(ops):
    stack = []
    for p in ops:
        if isinstance(p, int):
            stack = stack + [p]
        else:
            stack = stack[:-2] + ['(%s%s%s)' % (stack[-2], p, stack[-1])]

    assert len(stack) == 1
    return stack[0]

# Starting with an empty stack (and no operations), with 9 as the first
# unused digit, generate all expressions that evaluate to 2016.
for s in sums([], [], 9, 2016):
    print to_infix(s)

rawDescription: How to write a program to get all methods to get 2016 using numbers from 9 to 1?

description: Write a program to get all methods to get 2016 using numbers from 9 to 1


code:
import networkx as nx
G = nx.path_graph(5)
mapping_dict = {0: 'A', 1: 'B', 2: 'C', 3: 'A', 4: 'B', 5: 'C'}
H = nx.MultiGraph()
for (u,v) in G.edges():
    H.add_edge(mapping_dict[u],mapping_dict[v])
for edge in H.edges():
     print edge
#('A', 'C')
#('A', 'B')
#('A', 'B')
#('C', 'B')

rawDescription: Aggregating nodes in networkx

description: Aggregating nodes in networkx


code:
class Substitutions():
    def __init__(self, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

sub = Substitutions(name='sam', number=7467, marks=80)
details = '-n ${sub.name} -p ${sub.number} -m ${sub.marks}'
details = details.replace('${', '{') # Remove all the dollar signs

result = details.format(sub=sub)
print result

rawDescription: Python search and replace in a string

description: Python search and replace in a string


code:
for i in range(0, len(df_one.index)):
    for j in range(0, len(df_one.columns)):
        print df_one.values[i,j],df_two.value[i,j],i,j

rawDescription: Pandas: How could I iterate two dataframes which have exactly same format?


code:
def find():
    n = 0
    for a in xrange(999, 100, -1):
        for b in xrange(a, 100, -1):
            x = a * b
            if x > n:
                s = str(a * b)
                if s == s[::-1]:
                     n = a * b
    print n

find()

rawDescription: Project Euler prob. 4 in Python

description: Project Euler prob. 4 in Python


code:
args = {
    'name1': "Alice",
    'name2': "Bob",
    'date': datetime.datetime.now()
}
print "Hi, my name is {name1}. Today is {date}".format(**args)

rawDescription: Extract substring that starts and ends with symbol and replace with variable in Python


code:
def eachStraightPath(g):
  straightNodes = { node for node in g.node if len(g.edge[node]) == 2 }
  print straightNodes
  while straightNodes:
    straightNode = straightNodes.pop()
    straightPath = [ straightNode ]
    neighborA, neighborB = g.edge[straightNode].keys()
    while True:  # break out later if node is not straight
      straightPath.insert(0, neighborA)
      if neighborA not in straightNodes:
        break
      newNeighborA = (set(g.edge[neighborA]) ^ { straightPath[1] }).pop()
      straightNodes.remove(neighborA)
      neighborA = newNeighborA
    while True:  # break out later if node is not straight
      straightPath.append(neighborB)
      if neighborB not in straightNodes:
        break
      newNeighborB = (set(g.edge[neighborB]) ^ { straightPath[-2] }).pop()
      straightNodes.remove(neighborB)
      neighborB = newNeighborB
    yield straightPath

g = nx.lollipop_graph(5, 7)

for straightPath in eachStraightPath(g):
  print straightPath

rawDescription: How to isolate these (image included) paths in a graph?

description: Isolate these (image included) paths in a graph


code:
import pulp

prob = pulp.LpProblem("example", pulp.LpMinimize)

# Variable represent number of times device i is used
n1 = pulp.LpVariable("n1", 0, 5, cat="Integer")
n2 = pulp.LpVariable("n2", 0, 5, cat="Integer")
n3 = pulp.LpVariable("n3", 0, 5, cat="Integer")
n4 = pulp.LpVariable("n4", 0, 5, cat="Integer")
n5 = pulp.LpVariable("n5", 0, 5, cat="Integer")

# Device params
Device1=[8,8,4,4,200]
Device2=[16,0,16,0,250]
Device3=[8,0,4,4,300]
Device4=[16,8,4,4,300]
Device5=[8,8,2,2,150]

# The objective function that we want to minimize: the total cost
prob += n1 * Device1[-1] + n2 * Device2[-1] + n3 * Device3[-1] + n4 * Device4[-1] + n5 * Device5[-1]

# Constraint that we use no more than 5 devices
prob += n1 + n2 + n3 + n4 + n5 <= 5

Target = [24, 12, 16, 8]

# Constraint that the total I/O for all devices exceeds the target
for i in range(4):
    prob += n1 * Device1[i] + n2 * Device2[i] + n3 * Device3[i] + n4 * Device4[i] + n5 * Device5[i] >= Target[i]

# Actually solve the problem, this calls GLPK so you need it installed
pulp.GLPK().solve(prob)

# Print out the results
for v in prob.variables():
    print v.name, "=", v.varValue

rawDescription: Optimization problem in Python

description: Optimization problem in Python


code:
def do_quit(self, line):
    print 'bye bye !'
    reactor.stop() # <------
    return True

rawDescription: Using Cmd module inside a Python Twisted thread


code:
import numpy as np

vec_in = np.array([1*np.tan(np.pi/180*45), 1*np.tan(np.pi/180*45), 1])
vec_in /= np.linalg.norm(vec_in) #normalize, so we can use np.dot as scalar product
print vec_in

#find angles between vec_in and our x and y axis
#avoid inverse trigonometric functions, thereby increase accuracy

cosy = vec_in[0]/np.sqrt((vec_in[2]**2+vec_in[0]**2))
siny = np.sqrt(1-cosy**2)

#turn around y to align z to vector
Ry = np.array([[cosy,0,siny], [0,1,0], [-siny,0,cosy]])
Ryi = np.linalg.inv(Ry)
print 'accuracy Ry\n', np.dot(Ry, Ryi)

v1 = Ry.dot(vec_in) #z = 0
cosx = v1[0]/np.sqrt((v1[1]**2+v1[0]**2))
sinx = np.sqrt(1-cosx**2)

#turn around z to bring x fraction to zero
Rz = np.array([[cosx,-sinx,0], [sinx,cosx,0], [0,0,1]])
Rzi = np.linalg.inv(Rz)
print 'accuracy Rz\n', np.dot(Rz, Rzi)

print np.dot(Rzi, np.dot(Ry, vec_in)), '\tshould be [1,0,0]'

rawDescription: numpy huge error in rotational coordinate transformation

description: Numpy huge error in rotational coordinate transformation


code:
#!/usr/bin/python
from subprocess import *
import os

def fork_and_input(input, handle):
    """Send input to handle in a child process."""
    # Make sure input is iterable before forking
    input = iter(input)
    if os.fork():
        # Parent
        handle.close()
    else:
        # Child
        try:
            handle.writelines(input)
            handle.close()
        # An IOError here means some *other* part of the program
        # crashed, so don't complain here.
        except IOError:
            pass
        os._exit()

# A really big iterator
input_iterator = ("hello %s\n" % x for x in xrange(100000000))

subproc = Popen("cat", stdin=PIPE, stdout=PIPE)
fork_and_input(input_iterator, subproc.stdin)

for line in subproc.stdout:
    print line,

rawDescription: How can I feed a subprocess's standard input from a Python iterator?

description: Feed a subprocess's standard input from a Python iterator


rawDescription: Querying web API in python 2.7

description: Querying web API in python 2.7


code:
#!/usr/bin/env python
import traceback
def asdf():
    (filename,line_number,function_name,text)=traceback.extract_stack()[-1]
    print function_name
asdf()

rawDescription: Python equivalent of ruby's __method__?

description: Python equivalent of ruby's __method__


code:
import csv

with open("filepath", 'rb') as f:
    reader = csv.DictReader(f)
    rows = [row for row in reader if row['Total_Depth'] != '0']

for row in rows:
    print row

rawDescription: Returning a row from a CSV, if specified value within the row matches condition


code:
input_dict = {1: [1,2,3,4], 2: [2,3,4], 3:[1,2,3]}
#Get key/length(list) type tuples
helper = [(key, len(input_dict[key])) for key in input_dict.keys()]
#Sort list by the second element of the tuple(the length of the list)
helper.sort(key=lambda x: x[1])

#Now the first position hold the key to the shortest list from the dicitonary and the length
print input_dict[helper[0][0]]

rawDescription: Trying to find the list with fewer elements


code:
#Function
def on_mouse(event, x, y, flags,(cPts,overlayImage,resetImage)):
    if event==cv.CV_EVENT_LBUTTONUP:
        cPts[0].append([x,y])
        cv2.circle(overlayImage,(x,y),5,(255),-1)
    elif event==cv.CV_EVENT_RBUTTONUP:
        cPts[0]=[]
        print cPts
        overlayImage[:]=resetImage[:]

#Main Program
cvImage=cv2.imread(inputImageFilePath)
grayscaleImage=cv2.cvtColor(cvImage,cv.CV_BGR2GRAY)
overlayImage=np.copy(grayscaleImage)

cv2.namedWindow('preview')
cPts=[[]]
cv2.setMouseCallback('preview',on_mouse,(cPts,overlayImage,grayscaleImage))
opacity=0.4
while True:
    displayImage=cv2.addWeighted(overlayImage,opacity,grayscaleImage,1-opacity,0)
    cv2.imshow('preview',displayImage)
    keyPressed=cv2.waitKey(5)
    if keyPressed==27:
        break
    elif keyPressed==32:
        print cPts
        cv2.drawContours(overlayImage,np.array(cPts),0,255)
        maskImage=np.zeros_like(grayscaleImage)
        cv2.drawContours(maskImage,np.array(cPts),0,255,-1)
        extractedImage=np.bitwise_and(grayscaleImage,maskImage)
        cv2.imshow('extractedImage',extractedImage)
cv2.destroyAllWindows()

rawDescription: extracting a subimage in shape of a polygon

description: Extracting a subimage in shape of a polygon


code:
class fan_details:
    #you need to initialize and define the object:
    def __init__(self, name="", previous_address = None, age = 0):
        self.name = name
        if previous_address is None:
            self.previous_addresses = []
        else:
            self.previous_addresses = [previous_address]
        self.age = age

fans = []
x = fan_details()
x.name = "Joe Blow"
x.previous_addresses.append("4, seaview rd")
x.age = 42;
fans.append(x)
#a python object is mutable!!! This means that "x" and fans[0] point to the same
#python object. Lets create a new python object:
x = fan_details(name = "Jimmy Flag", previous_address="21, Main Street", age=33)
#this line above is really important! Note that now we can initialize an object on one line :-)
fans.append(x)
print fans[0].previous_addresses
print fans[1].previous_addresses

rawDescription: insert a list into a list of objects

description: Insert a list into a list of objects


code:
from xml.parsers.expat import ParserCreate

open_elements = {}
result_dict = {}

def start_element(name, attrs):
    open_elements[name] = True

def end_element(name):
    del open_elements[name]

def char_data(data):
    for element in open_elements:
        cur = result_dict.setdefault(element, '')
        result_dict[element] = cur + data

if __name__ == '__main__':
    p = ParserCreate()

    p.StartElementHandler = start_element
    p.EndElementHandler = end_element
    p.CharacterDataHandler = char_data

    p.Parse(u'<_133_3><_135_3><_116_2>The other system worked for about 1 month</_116_2> got some good images <_137_3>on it then it started doing the same thing as the first one</_137_3> so then I quit using either camera now they are just sitting and collecting dust.</_135_3></_133_3>', 1)

    print result_dict

rawDescription: Parsing a string for nested patterns

description: Parsing a string for nested patterns


code:
for membership in redmine.project.get('my_project').memberships:
    print membership.user_id

rawDescription: Can I get a list of users assigned to a project?

description: Get a list of users assigned to a project


code:
import nltk
from nltk.corpus import brown

# http://thetokenizer.com/2013/05/09/efficient-way-to-extract-the-main-topics-of-a-sentence/

# This is our fast Part of Speech tagger
#############################################################################
brown_train = brown.tagged_sents(categories='news')
regexp_tagger = nltk.RegexpTagger(
    [(r'^-?[0-9]+(.[0-9]+)?$', 'CD'),
     (r'(-|:|;)$', ':'),
     (r'\'*$', 'MD'),
     (r'(The|the|A|a|An|an)$', 'AT'),
     (r'.*able$', 'JJ'),
     (r'^[A-Z].*$', 'NNP'),
     (r'.*ness$', 'NN'),
     (r'.*ly$', 'RB'),
     (r'.*s$', 'NNS'),
     (r'.*ing$', 'VBG'),
     (r'.*ed$', 'VBD'),
     (r'.*', 'NN')
])
unigram_tagger = nltk.UnigramTagger(brown_train, backoff=regexp_tagger)
bigram_tagger = nltk.BigramTagger(brown_train, backoff=unigram_tagger)
#############################################################################

# This is our semi-CFG; Extend it according to your own needs
#############################################################################
cfg = {}
cfg["NNP+NNP"] = "NNP"
cfg["NN+NN"] = "NNI"
cfg["NNI+NN"] = "NNI"
cfg["JJ+JJ"] = "JJ"
cfg["JJ+NN"] = "NNI"
#############################################################################

class NPExtractor(object):

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

    # Split the sentence into singlw words/tokens
    def tokenize_sentence(self, sentence):
        tokens = nltk.word_tokenize(sentence)
        return tokens

    # Normalize brown corpus' tags ("NN", "NN-PL", "NNS" > "NN")
    def normalize_tags(self, tagged):
        n_tagged = []
        for t in tagged:
            if t[1] == "NP-TL" or t[1] == "NP":
                n_tagged.append((t[0], "NNP"))
                continue
            if t[1].endswith("-TL"):
                n_tagged.append((t[0], t[1][:-3]))
                continue
            if t[1].endswith("S"):
                n_tagged.append((t[0], t[1][:-1]))
                continue
            n_tagged.append((t[0], t[1]))
        return n_tagged

    # Extract the main topics from the sentence
    def extract(self):

        tokens = self.tokenize_sentence(self.sentence)
        tags = self.normalize_tags(bigram_tagger.tag(tokens))

        merge = True
        while merge:
            merge = False
            for x in range(0, len(tags) - 1):
                t1 = tags[x]
                t2 = tags[x + 1]
                key = "%s+%s" % (t1[1], t2[1])
                value = cfg.get(key, '')
                if value:
                    merge = True
                    tags.pop(x)
                    tags.pop(x)
                    match = "%s %s" % (t1[0], t2[0])
                    pos = value
                    tags.insert(x, (match, pos))
                    break

        matches = []
        for t in tags:
            if t[1] == "NNP" or t[1] == "NNI":
            #if t[1] == "NNP" or t[1] == "NNI" or t[1] == "NN":
                matches.append(t[0])
        return matches

# Main method, just run "python np_extractor.py"
Summary="""

Verizon has not honored this appointment or notified me of the delay in an appropriate manner. It is now 1:20 PM and the only way I found out of a change is that I called their chat line and got a message saying my appointment is for 2 PM. My cell phone message says the original time as stated here.

"""
def main(Topic):
    facebookData=[]
    readdata=csv.reader(open('fb_data1.csv','r'))
    for row in readdata:
        facebookData.append(row)
    relevant_sentence=[]
    for status in facebookData:
        summary=status.split('.')
        for sentence in summary:
            np_extractor = NPExtractor(sentence)
            result = np_extractor.extract()
            if Topic in result:
                relevant_sentence.append(sentence)
            print sentence
            print "This sentence is about: %s" % ", ".join(result)
        return relevant_sentence

if __name__ == '__main__':
    result=main('Verizon')

rawDescription: How to detect aboutness with python pos tagger

description: Detect aboutness with python pos tagger


code:
class Main:
    def __init__(self):
        self.i = [5, 5]
        self.o = []

        self.getset_pos()

    def getset_pos(self):
        """ Takes the coord of an item and sets it equal to another item """
        self.o = []
        for numb in input:
            self.o.append(numb)
        print output

rawDescription: How do I take the value of one variable and set it equal to another variable in a class?

description: Take the value of one variable and set it equal to another variable in a class


code:
def row(x, transitions):
    """ generator spitting all cells in a row given a list of transition (in/out) columns."""

    i = 1
    in_poly = True
    y = transitions[0]
    while i < len(transitions):
        if in_poly:
            while y < transitions[i]:
                yield (x,y)
                y += 1
            in_poly = False
        else:
            in_poly = True
            y = transitions[i]
        i += 1

def get_same_row_vert(i, vertices):
    """ find all vertex columns in the same row as vertices[i], and return next vertex index as well."""

    vert = []
    x = vertices[i][0]
    while i < len(vertices) and vertices[i][0] == x:
        vert.append(vertices[i][1])
        i += 1
    return vert, i

def update_transitions(old, new):
    """ update old transition columns for a row given new vertices.

    That is: merge both lists and remove duplicate values (2 transitions at the same column cancel each other)"""

    if old == []:
        return new
    if new == []:
        return old
    o0 = old[0]
    n0 = new[0]
    if o0 == n0:
        return update_transitions(old[1:], new[1:])
    if o0 < n0:
        return [o0] + update_transitions(old[1:], new)
    return [n0] + update_transitions(old, new[1:])

def polygon(vertices):
    """ generator spitting all cells in the polygon defined by given vertices."""

    vertices.sort()
    x = vertices[0][0]
    transitions, i = get_same_row_vert(0, vertices)
    while i < len(vertices):
        while x < vertices[i][0]:
            for cell in row(x, transitions):
                yield cell
            x += 1
        vert, i = get_same_row_vert(i, vertices)
        transitions = update_transitions(transitions, vert)

# define a "strange" polygon (hook shaped)
vertices = [(0,0),(0,3),(4,3),(4,0),(3,0),(3,2),(1,2),(1,1),(2,1),(2,0)]

for cell in polygon(vertices):
    print cell
    # or do whatever you need to do

rawDescription: Find irregular region in 4D numpy array of gridded data (lat/lon)


code:
def closed(self, reason):
    for k,v in self.slain.iteritems():
        print "Slain by %s: %d" % (k,v)

rawDescription: Aggregate data from list of Requests in a crawler


code:
a = r"\x50\x79\x74\x68\x6f\x6e"
print a
a.decode('string_escape')

rawDescription: Python equivalent to echo -e?

description: Python equivalent to echo -e


code:
import csv

combination = '1,0,1,0,0'.split(',')

with open('pythonbigDATA.txt') as infile:
    for row in csv.reader(infile):
        if row[1:] == combination:
            print row[0], ','.join(row[1:])

rawDescription: Python to extract specific numbers from text file


code:
import datetime

now = datetime.datetime.now()

for h in xrange(24):
    for m in xrange(60):
        for s in xrange(60):
           time_string = '%02d:%02d:%02d' % (h,m,s)
           if time_string == now.strftime('%H:%m:%S'):
               print 'you found it! %s' % time_string

rawDescription: Getting a lists of times in Python from 0:0:0 to 23:59:59 and then comparing with datetime values


code:
def twiddle(objFunction, args, init=0.5, tolerance=0.00001, domain=(float("-inf"), float("inf"))):
  """Optimize a single parameter given an objective function.

  This is a local hill-climbing algorithm. Here is a simple description of it:
  https://www.youtube.com/watch?v=2uQ2BSzDvXs

  @param args       (tuple)   Arguments necessary for the objective function.

  @param tolerance  (float)   Number used to determine when optimization has
                              converged to a sufficiently good score.

  @param objFunction(function)Objective Function used to quantify how good a
                              particular parameter choice is.

  @param init       (float)   Initial value of the parameter.

  @param domain     (tuple)   Domain of parameter values, as (min, max).

  @return (dict) Contains:
        "parameter" (float)   Threshold that returns the largest score from the
                              Objective function.

        "score"     (float)   The score from the objective function given the
                              threshold.
  """
  pastCalls = {}
  x = init
  delta = 0.1
  bestScore = objFunction(x, args)

  pastCalls[x] = bestScore

  while delta > tolerance:

    # Keep x within bounds
    if x+delta > domain[1]:
      delta = abs(domain[1] - x) / 2
    x += delta

    if x not in pastCalls:
      score = objFunction(x, args)
      pastCalls[x] = score

    score = pastCalls[x]

    if score > bestScore:
      bestScore = score
      delta *= 2

    else:
      # Keep x within bounds
      if x-delta < domain[0]:
        delta = abs(domain[0] - x) / 2
      x -= 2*delta

      if x not in pastCalls:
        score = objFunction(x, args)
        pastCalls[x] = score

      score = pastCalls[x]

      if score > bestScore:
        bestScore = score
        delta *= 2
      else:
        x += delta
        delta *= 0.5

    print "Parameter:", x
    print "Best score:", bestScore
    print "Step size:", delta
    print

  return {"parameter": x,
          "score": bestScore}

rawDescription: Domain for twiddle optimizer?

description: Domain for twiddle optimizer


code:
import os
os.linesep
print "line one" + os.linesep + "line two"

rawDescription: Python, string format, newline (C++ - std::endl)


code:
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi

def voronoi_finite_polygons_2d(vor, radius=None):
    """
    Reconstruct infinite voronoi regions in a 2D diagram to finite
    regions.

    Parameters
    ----------
    vor : Voronoi
        Input diagram
    radius : float, optional
        Distance to 'points at infinity'.

    Returns
    -------
    regions : list of tuples
        Indices of vertices in each revised Voronoi regions.
    vertices : list of tuples
        Coordinates for revised Voronoi vertices. Same as coordinates
        of input vertices, with 'points at infinity' appended to the
        end.

    """

    if vor.points.shape[1] != 2:
        raise ValueError("Requires 2D input")

    new_regions = []
    new_vertices = vor.vertices.tolist()

    center = vor.points.mean(axis=0)
    if radius is None:
        radius = vor.points.ptp().max()

    # Construct a map containing all ridges for a given point
    all_ridges = {}
    for (p1, p2), (v1, v2) in zip(vor.ridge_points, vor.ridge_vertices):
        all_ridges.setdefault(p1, []).append((p2, v1, v2))
        all_ridges.setdefault(p2, []).append((p1, v1, v2))

    # Reconstruct infinite regions
    for p1, region in enumerate(vor.point_region):
        vertices = vor.regions[region]

        if all(v >= 0 for v in vertices):
            # finite region
            new_regions.append(vertices)
            continue

        # reconstruct a non-finite region
        ridges = all_ridges[p1]
        new_region = [v for v in vertices if v >= 0]

        for p2, v1, v2 in ridges:
            if v2 < 0:
                v1, v2 = v2, v1
            if v1 >= 0:
                # finite ridge: already in the region
                continue

            # Compute the missing endpoint of an infinite ridge

            t = vor.points[p2] - vor.points[p1] # tangent
            t /= np.linalg.norm(t)
            n = np.array([-t[1], t[0]])  # normal

            midpoint = vor.points[[p1, p2]].mean(axis=0)
            direction = np.sign(np.dot(midpoint - center, n)) * n
            far_point = vor.vertices[v2] + direction * radius

            new_region.append(len(new_vertices))
            new_vertices.append(far_point.tolist())

        # sort region counterclockwise
        vs = np.asarray([new_vertices[v] for v in new_region])
        c = vs.mean(axis=0)
        angles = np.arctan2(vs[:,1] - c[1], vs[:,0] - c[0])
        new_region = np.array(new_region)[np.argsort(angles)]

        # finish
        new_regions.append(new_region.tolist())

    return new_regions, np.asarray(new_vertices)

# make up data points
np.random.seed(1234)
points = np.random.rand(15, 2)

# compute Voronoi tesselation
vor = Voronoi(points)

# plot
regions, vertices = voronoi_finite_polygons_2d(vor)
print "--"
print regions
print "--"
print vertices

# colorize
for region in regions:
    polygon = vertices[region]
    plt.fill(*zip(*polygon), alpha=0.4)

plt.plot(points[:,0], points[:,1], 'ko')
plt.xlim(vor.min_bound[0] - 0.1, vor.max_bound[0] + 0.1)
plt.ylim(vor.min_bound[1] - 0.1, vor.max_bound[1] + 0.1)

plt.show()

rawDescription: Colorize Voronoi Diagram

description: Colorize Voronoi Diagram


code:
from Tkinter import Tk, Button

def goodbye_world():
    print "Goodbye World!\nWait, I changed my mind!"
    button.configure(text = "Hello World!", command=hello_world)

def hello_world():
    print "Hello World!\nWait, I changed my mind!"
    button.configure(text = "Goodbye World!", command=goodbye_world)

root = Tk()
button = Button(root, text="Hello World!", command=hello_world)
button.pack()

root.mainloop()

rawDescription: Change command Method for Tkinter Button in Python


code:
nlist = [['Chris', 'Davids', 21], ['Rob', 'Croft', 26]]

for i, v in enumerate(nlist):
   if v[0] == 'Chris':
       nlist.remove(nlist[i])

print nlist

rawDescription: Python: Remove entry in a nested list

description: Python: Remove entry in a nested list


code:
import socket
import httplib
from encryption import GetSSOTicket
import re
from xml.sax import saxutils

username = "login@hotmail.com"
password = "password"

def ReadAll(sock):
    data = ""
    while 1:
        try:
            r = sock.recv(4096)
            data += r
        except socket.timeout:
            break
    return data

def SOAPRequest(xml):
    url = "login.live.com"
    con = httplib.HTTPSConnection(url)

    headers = {"Host": "login.live.com",
                "Accept": "text/plain"}

    con.request("POST", "/RST.srf", xml, headers)

    response = con.getresponse().read()

    con.close()

    return response

def GetTicket(policy, nonce):
    xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    xml += "<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:wsse=\"http://schemas.xmlsoap.org/ws/2003/06/secext\" xmlns:saml=\"urn:oasis:names:tc:SAML:1.0:assertion\" xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2002/12/policy\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/03/addressing\" xmlns:wssc=\"http://schemas.xmlsoap.org/ws/2004/04/sc\" xmlns:wst=\"http://schemas.xmlsoap.org/ws/2004/04/trust\">";
    xml += "<ps:AuthInfo xmlns:ps=\"http://schemas.microsoft.com/Passport/SoapServices/PPCRL\" Id=\"PPAuthInfo\">";
    xml += "<ps:HostingApp>{7108E71A-9926-4FCB-BCC9-9A9D3F32E423}</ps:HostingApp>";
    xml += "<ps:BinaryVersion>4</ps:BinaryVersion>";
    xml += "<ps:UIVersion>1</ps:UIVersion>";
    xml += "<ps:Cookies></ps:Cookies>";
    xml += "<ps:RequestParams>AQAAAAIAAABsYwQAAAAxMDMz</ps:RequestParams>";
    xml += "</ps:AuthInfo>";
    xml += "<wsse:Security><wsse:UsernameToken Id=\"user\">";
    xml += "<wsse:Username>" + username + "</wsse:Username>";
    xml += "<wsse:Password>" + password + "</wsse:Password>";
    xml += "</wsse:UsernameToken></wsse:Security>";
    xml += "<ps:RequestMultipleSecurityTokens xmlns:ps=\"http://schemas.microsoft.com/Passport/SoapServices/PPCRL\" Id=\"RSTS\">";
    xml += "<wst:RequestSecurityToken Id=\"RST0\">";
    xml += "<wst:RequestType>http://schemas.xmlsoap.org/ws/2004/04/security/trust/Issue</wst:RequestType>";
    xml += "<wsp:AppliesTo><wsa:EndpointReference><wsa:Address>http://Passport.NET/tb";
    xml += "</wsa:Address></wsa:EndpointReference></wsp:AppliesTo></wst:RequestSecurityToken>";
    xml += "<wst:RequestSecurityToken Id=\"RST1\">";
    xml += "<wst:RequestType>http://schemas.xmlsoap.org/ws/2004/04/security/trust/Issue</wst:RequestType><wsp:AppliesTo><wsa:EndpointReference>";
    xml += "<wsa:Address>messengerclear.live.com</wsa:Address></wsa:EndpointReference></wsp:AppliesTo>";
    xml += "<wsse:PolicyReference URI=\"" + policy + "\"></wsse:PolicyReference></wst:RequestSecurityToken>";
    xml += "<wst:RequestSecurityToken Id=\"RST2\">";
    xml += "<wst:RequestType>http://schemas.xmlsoap.org/ws/2004/04/security/trust/Issue</wst:RequestType>";
    xml += "<wsp:AppliesTo>";
    xml += "<wsa:EndpointReference>";
    xml += "<wsa:Address>contacts.msn.com</wsa:Address>";
    xml += "</wsa:EndpointReference>";
    xml += "</wsp:AppliesTo>";
    xml += "<wsse:PolicyReference URI=\"MBI\">";
    xml += "</wsse:PolicyReference>";
    xml += "</wst:RequestSecurityToken>";
    xml += "</ps:RequestMultipleSecurityTokens></Envelope>";

    xmlDoc = SOAPRequest(xml)

    m = re.search(r'<wsse:BinarySecurityToken Id="Compact1">(.*?)</wst:BinarySecret>', xmlDoc)
    secret = saxutils.unescape(m.group(1))
    secret = secret.split("<wst:BinarySecret>")[1]

    print "secret:"
    print secret

    m = re.search(r'<wsse:BinarySecurityToken Id="Compact1">(.*?)</wsse:BinarySecurityToken>',xmlDoc)
    ticket = saxutils.unescape(m.group(1))

    SSOTicket = GetSSOTicket(secret, nonce)

    return ticket + " " + SSOTicket

def LogIntoNS():
    address = ("64.4.61.219", 1863)

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(address)
    s.settimeout(3)

    s.send("VER 1 MSNP15 CVR0\r\n")

    ReadAll(s)

    s.send("CVR 2 0x0407 win 6.0 i386 MSNMSGR 8.5 MSMSGS " + username + "\r\n")

    ReadAll(s)

    s.send("USR 3 SSO I " + username + "\r\n")

    recvbuf = ReadAll(s)

    recvbuf = recvbuf[recvbuf.find("USR"):]

    policy = recvbuf.split()[4]

    print "policy:"
    print policy

    nonce = recvbuf.split()[5]
    print "nonce:"
    print nonce

    ticket = GetTicket(policy, nonce)

    print "ticket:"
    print ticket

    s.send("USR 4 SSO S " + ticket + "\r\n")

    print "Sent"

    print s.recv(4096)

LogIntoNS()

rawDescription: Implementing MSNP15 in Python - 911 error but all tickets are correct


code:
class MyDict(dict):
   def __getitem__(self, item):
       return dict.__getitem__(self, item) % self

dictionary = MyDict({

    'user' : 'gnucom',
    'home' : '/home/%(user)s',
    'bin' : '%(home)s/bin'
})

print dictionary["home"]
print dictionary["bin"]

rawDescription: What do I do when I need a self referential dictionary?


code: print tree.identify_row(event.y)

rawDescription: Getting the value of the Id Column in Tkinter Treeview


code:
#!/usr/bin/python

import os, csv, sys, Tkinter, tkFileDialog as fd

# stop tinker shell from opening as only needed for file dialog
root = Tkinter.Tk()
root.withdraw()

def crop_file(in_path):
    # read file into memory
    file_obj = open(in_path, 'rb')
    reader = csv.reader(file_obj, delimiter='\t')
    data = []
    for row in reader:
        if not row or not any(row):
            break #stop at empty row
        else:
            data.append(row)
    file_obj.close()

    print 'Found', len(data), 'rows of data without empty lines.'
    conf = raw_input('delete remaining lines? (Y|N): ').upper()[0]

    if conf == 'Y':
        # write data to file
        file_obj = open(in_path, 'wb')
        writer = csv.writer(file_obj)
        writer.writerows(data)
        file_obj.close

def main():
    in_path = None
    prog_name = sys.argv[0]

    # check if in_path are inlcuded as cmd line args...
    if len(sys.argv) > 1:
        in_path = sys.argv[1]
        if not os.path.exists(in_path):
            print 'Usage:', prog_name, '[file_path>]'
            print 'cannot find the file provided for file_path:\n', in_path
            sys.exit("Error - invalid excel_file_path arg")
    else:
        try:
            # set current working directory to user's my documents folder
            os.chdir(os.path.join(os.getenv('userprofile'),'documents'))
        except:
            pass

    # ask user for path to file...
    while not in_path:
        print "Please select the file to read data from ..."
        try:
            in_path = fd.askopenfilename()
        except:
            print 'Error selecting file.'
        if not in_path:
            cont = raw_input('Do you want to continue? (Y|N): ').upper()[0]
            if cont == 'N':
                sys.exit("Error - unable to select input file")

    crop_file(in_path)

if __name__ == '__main__':
    main()

rawDescription: Find and delete after 1st occurance of empty row in csv with python


code:
import Tkinter
import tkMessageBox

print "Sample #1"
tkMessageBox.showinfo("Message", "Insert sample and press OK")
print "Sample #2"

rawDescription: Pause for an event in python tkinter

description: Pause for an event in python tkinter


code:
import sys
from PySide import QtGui
app = QtGui.QApplication.instance()
standalone = app is None
if standalone:
    app = QtGui.QApplication(sys.argv)
wid = QtGui.QWidget()
wid.resize(250,150)
wid.setWindowTitle('Simple')
wid.show()
if standalone:
    sys.exit(app.exec_())
else:
    print "We're back with the Qt window still active"

rawDescription: Getting PySide Hello App to run under Canopy


code:
import atexit
#defining function to run on shutdown
def close_running_threads():
    for thread in the_threads:
        thread.join()
    print "Threads complete, ready to finish"
#Register the function to be called on exit
atexit.register(close_running_threads)
#start your process
app.run()

rawDescription: Python Flask shutdown event handler

description: Python Flask shutdown event handler


code:
import os
import sys
from threading import Thread, Lock
from Queue import Queue
def report(message):
     mutex.acquire()
     print message
     sys.stdout.flush()
     mutex.release()
class Compressor(Thread):
     def __init__(self, in_queue, out_queue):
         Thread.__init__(self)
         self.in_queue = in_queue
         self.out_queue = out_queue
     def run(self):
         while True:
             path = self.in_queue.get()
             sys.stdout.flush()
             if path is None:
                 break
             report("Compressing %s" % path)
             os.system("bzip2 %s" % path)
             report("Done %s" %  path)
             self.out_queue.put(path)
in_queue = Queue()
out_queue = Queue()
mutex = Lock()
THREAD_COUNT = 4
worker_list = []
for i in range(THREAD_COUNT):
     worker = Compressor(in_queue, out_queue)
     worker.start()
     worker_list.append(worker)
for roots, dirlist, filelist in os.walk(os.curdir):
     for file in [os.path.join(roots, filegot) for filegot in filelist]:
         if "bz2" not in file:
             in_queue.put(file)
for i in range(THREAD_COUNT):
     in_queue.put(None)
for worker in worker_list:
     worker.join()

rawDescription: multicpu bzip2 using a python script

description: Multicpu bzip2 using a python script


code:
#!/usr/bin/env python

import sys
import ftplib

filename = sys.argv[1]
ftp = ftplib.FTP('servername', 'username', 'password')
print ftp.sendcmd('SITE CHMOD 644 ' + filename)
ftp.quit()

rawDescription: Change permissions via ftp in python

description: Change permissions via ftp in python


code:
current = datetime.date.today().replace(day=1)

for i in xrange(24):
    new_month = current.month % 12 + 1
    new_year = current.year + current.month // 12
    current = current.replace(month=new_month, year=new_year)
    print current

rawDescription: How to list next 24 months' start dates with python?

description: List next 24 months' start dates with python


rawDescription: How I can calculate big numbers in python


code:
class Parent(object):
    ID = None

    @classmethod
    def getId(cls):
        return cls.ID

class Child(Parent):
    ID = "Child Class"

print Child.getId() # "Child Class"

rawDescription: Python How to override a class member in the child and access it from parent?


code:
import inspect, re

def varname(p):
  for line in inspect.getframeinfo(inspect.currentframe().f_back)[3]:
    m = re.search(r'\bvarname\s*\(\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)', line)
    if m:
      return m.group(1)

if __name__ == '__main__':
  spam = 42
  print varname(spam)

rawDescription: How can you print a variable name in python?

description: Print a variable name in python


code:
import inspect

def none2defaultdecorator(thisfunc):
    (specargs, _, _, defaults) = inspect.getargspec(thisfunc)

    def wrappedfunc(*instargs):
        callargs = inspect.getcallargs(thisfunc, *instargs)

        if len(specargs) != len(callargs):
             print "Strange - different argument count in this call to those in spec"
             print specargs
             print callargs

        for i,argname in enumerate(specargs[-len(defaults):]):
            try:
                if callargs[argname] == None:
                    callargs[argname] = defaults[i]
            except KeyError:
                # no local value for this argument - it will get the default anyway
                pass

        return thisfunc(**callargs)

    return wrappedfunc

#Decorate the funtion with the "None replacer".  Comment this out to see difference
@none2defaultdecorator
def test(binarg = True, myint = 5):
    if binarg == True:
        print "Got binarg == true"
    elif binarg == False:
        print "Got binarg == false"
    else:
        print "Didn't understand this argument - binarg == ", binarg

    print "Argument values - binarg:",binarg,"; myint:",myint

test()
test(False)
test(True)
test(None)
test(None, None)

rawDescription: What is a good way to get a function to use its default value for an argument when the argument is specified as None in Python?


code:
import wmi

c = wmi.WMI()
t = wmi.WMI(moniker = "//./root/wmi")

batts1 = c.CIM_Battery(Caption = 'Portable Battery')
for i, b in enumerate(batts1):
    print 'Battery %d Design Capacity: %d mWh' % (i, b.DesignCapacity or 0)

batts = t.ExecQuery('Select * from BatteryFullChargedCapacity')
for i, b in enumerate(batts):
    print ('Battery %d Fully Charged Capacity: %d mWh' %
          (i, b.FullChargedCapacity))

batts = t.ExecQuery('Select * from BatteryStatus where Voltage > 0')
for i, b in enumerate(batts):
    print '\nBattery %d ***************' % i
    print 'Tag:               ' + str(b.Tag)
    print 'Name:              ' + b.InstanceName

    print 'PowerOnline:       ' + str(b.PowerOnline)
    print 'Discharging:       ' + str(b.Discharging)
    print 'Charging:          ' + str(b.Charging)
    print 'Voltage:           ' + str(b.Voltage)
    print 'DischargeRate:     ' + str(b.DischargeRate)
    print 'ChargeRate:        ' + str(b.ChargeRate)
    print 'RemainingCapacity: ' + str(b.RemainingCapacity)
    print 'Active:            ' + str(b.Active)
    print 'Critical:          ' + str(b.Critical)

rawDescription: Getting Battery Capacity Windows with Python


code:
class TweepyFileListener(tweepy.StreamListener):
    def on_data(self, data):
        print "on_data called"
        # Twitter returns data in JSON format - we need to decode it first
        decoded = json.loads(data)
        msg = '@%s: %s\n' % (
            decoded['user']['screen_name'],
            decoded['text'].encode('ascii', 'ignore'))

        #you should really open the file in __init__
        #You should also use a RotatingFileHandler or this guy will get massive
        with open("Output.txt", "a") as tweet_log:
            print "Received: %s\n" % msg
            tweet_log.write(msg)

rawDescription: Python issue with saving the output

description: Python issue with saving the output


code:
import subprocess
import select

cmd = subprocess.Popen(['bash'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)

poll = select.poll()
poll.register(cmd.stdout.fileno(),select.POLLIN)

# Write the first command
command = "export greeting=hello\n"
cmd.stdin.write(command)
cmd.stdin.flush() # Must include this to ensure data is passed to child process
ready = poll.poll(500)
if ready:
   result = cmd.stdout.readline()
   print result

# Write the second command
command = "echo $greeting world\n"
cmd.stdin.write(command)
cmd.stdin.flush() # Must include this to ensure data is passed to child process
ready = poll.poll(500)
if ready:
   result = cmd.stdout.readline()
   print result

rawDescription: Sending multiple commands to a bash shell which must share an environment


code:
str1 = "Start"
fp = open("C:...test.txt")
check = 0
for i,line in enumerate(fp):
    if str1 in line:
        check = i
        continue
    if "contain" in line and (i == check + 3):
        print "OK"
    else:
        print "NOK"

rawDescription: Python : select a specific line from text


code:
import re,sys,time
from Tkinter import *
import Tkinter
import threading
import traceback
import Queue

class ReaderThread(threading.Thread):
    def __init__(self, root, queue):
        print "Thread init"
        threading.Thread.__init__(self)
        self.root = root
        self.running = True
        self.q = queue

    def stop(self):
        print "Stopping thread"
        running = False

    def run(self):
        print "Thread started"
        time.sleep(5)

        try:
            while(self.running):
                # emulating delay when reading from serial interface
                time.sleep(0.05)
                curline = "the quick brown fox jumps over the lazy dog\n"

                try:
                    self.q.put(curline)
                    self.root.event_generate('<<AppendLine>>', when='tail')
                # If it failed, the window has been destoyed: over
                except TclError as e:
                    print e
                    break

        except Exception as e:
            traceback.print_exc(file=sys.stdout)
            print "Exception in receiver thread, stopping..."
            pass
        print "Thread stopped"

class Transformer:
    def __init__(self):
        self.q = Queue.Queue()
        self.lineIndex = 1
        pass

    def appendLine(self, event):
        line = self.q.get_nowait()

        if line == None:
            return

        i = self.lineIndex
        curIndex = "1.0"
        lowerEdge = 1.0
        pos = 1.0

        # get cur position
        pos = self.scrollbar.get()[1]

        # Disable scrollbar
        self.text.configure(yscrollcommand=None, state=NORMAL)

        # Add to text window
        self.text.insert(END, str(line))
        startIndex = repr(i) + ".0"
        curIndex = repr(i) + ".end"

        # Perform colorization
        if i % 6 == 0:
            self.text.tag_add("warn", startIndex, curIndex)
        elif i % 6 == 1:
            self.text.tag_add("debug", startIndex, curIndex)
        elif i % 6 == 2:
            self.text.tag_add("info", startIndex, curIndex)
        elif i % 6 == 3:
            self.text.tag_add("error", startIndex, curIndex)
        elif i % 6 == 4:
            self.text.tag_add("fatal", startIndex, curIndex)
        i = i + 1

        # Enable scrollbar
        self.text.configure(yscrollcommand=self.scrollbar.set, state=DISABLED)

        # Auto scroll down to the end if scroll bar was at the bottom before
        # Otherwise allow customer scrolling

        if pos == 1.0:
            self.text.yview(END)

        self.lineIndex = i

    def start(self):
        """starts to read linewise from self.in_stream and parses the read lines"""
        count = 1
        self.root = Tk()
        self.root.title("Tkinter Auto-Scrolling Test")#
        self.root.bind('<<AppendLine>>', self.appendLine)
        self.topPane = PanedWindow(self.root, orient=HORIZONTAL)
        self.topPane.pack(side=TOP, fill=X)
        self.lowerPane = PanedWindow(self.root, orient=VERTICAL)

        self.scrollbar = Scrollbar(self.root)
        self.scrollbar.pack(side=RIGHT, fill=Y)
        self.text = Text(wrap=WORD, yscrollcommand=self.scrollbar.set)
        self.scrollbar.config(command=self.text.yview)
        # Color definition for log levels
        self.text.tag_config("debug",foreground="gray50")
        self.text.tag_config("info",foreground="green")
        self.text.tag_config("warn",foreground="orange")
        self.text.tag_config("error",foreground="red")
        self.text.tag_config("fatal",foreground="#8B008B")
        # set default color
        self.text.config(background="black", foreground="gray");
        self.text.pack(expand=YES, fill=BOTH)

        self.lowerPane.add(self.text)
        self.lowerPane.pack(expand=YES, fill=BOTH)

        t = ReaderThread(self.root, self.q)
        print "Starting thread"
        t.start()

        try:
            self.root.mainloop()
        except Exception as e:
            print "Exception in window manager: ", e

        t.stop()
        t.join()

if __name__ == "__main__":
    try:
        trans = Transformer()
        trans.start()
    except Exception as e:
        print "Error: ", e
        sys.exit(1)

rawDescription: Python Tkinter Text Widget with Auto & Custom Scroll


code:
import magic

MAGIC_PARAM_NAME_MAX = 1                    # definition from magic.h
name_max = magic.c_void_p(40)               # new use count, can also be c_size_t
name_max_ref = magic.ctypes.byref(name_max)

s = magic.Magic()
magic.libmagic.magic_setparam(s.cookie, MAGIC_PARAM_NAME_MAX, name_max_ref)
print s.from_file('file.jpg')

rawDescription: Get MP3 MIME type using python

description: Get MP3 MIME type using python


code:
# prints recursive count of lines of python source code from current directory
# includes an ignore_list. also prints total sloc

import os
cur_path = os.getcwd()
ignore_set = set(["__init__.py", "count_sourcelines.py"])

loclist = []

for pydir, _, pyfiles in os.walk(cur_path):
    for pyfile in pyfiles:
        if pyfile.endswith(".py") and pyfile not in ignore_set:
            totalpath = os.path.join(pydir, pyfile)
            loclist.append( ( len(open(totalpath, "r").read().splitlines()),
                               totalpath.split(cur_path)[1]) )

for linenumbercount, filename in loclist:
    print "%05d lines in %s" % (linenumbercount, filename)

print "\nTotal: %s lines (%s)" %(sum([x[0] for x in loclist]), cur_path)

rawDescription: Lines of Code in Eclipse PyDev Projects

description: Lines of Code in Eclipse PyDev Projects


code:
x = []
y = x
x.append('SPAM')
print y
# Returns: ['SPAM']

rawDescription: Python Class Memory Usage

description: Python Class Memory Usage


code:
import urllib2
from BeautifulSoup import BeautifulSoup

page = urllib2.urlopen("http://www.icc-ccs.org/prc/piracyreport.php")
soup = BeautifulSoup(page)
for incident in soup('td', width="90%"):
    where, linebreak, what = incident.contents[:3]
    print where.strip()
    print what.strip()
    print

rawDescription: Scraping web pages with Python vs PHP?

description: Scraping web pages with Python vs PHP


code:
db.mytable.myfield.requires = IS_IN_SET(['apple', 'banana', 'orange'])
print db.mytable.myfield.requires.options()

rawDescription: Creating a custom options widget in web2py


code:
import json
url_list = ['http://www.google.com', 'http://www.yahoo.com']
json.dumps({'entry': [{'url': v} for v in url_list]})
print json.dumps({'entry': [{'url': v} for v in url_list]}, indent=4)

rawDescription: how do I convert array to JSON

description: Convert array to JSON


code:
A1 = {'b1': ['X', '0'], 'b2': ['S', 'T'], 'b3': ['X', 'Y']}

item = 'X'

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

rawDescription: How to match a string value with dictionary value stored as list?

description: Match a string value with dictionary value stored as list


code:
l1 = [1,2,3,4,5,6,7,8,10]
l2 = ['a','b']

from itertools import cycle

for number, letter in zip(l1, cycle(l2)):
    print number, letter

rawDescription: Python Double Loop

description: Python Double Loop


code:
import heapq
res = heapq.nlargest(2, some_sequence)
print res[1] # to get 2nd largest

rawDescription: python find the 2nd highest element

description: Python find the 2nd highest element


code:
import csv
import json

class Node(dict):
    def __init__(self, (nid, name, ndescr)):
        dict.__init__(self)
        self['id'] = nid
        self['name'] = name.lstrip() # you have badly formed csv....
        self['description'] = ndescr.lstrip()
        self['children'] = []

    def add_node(self, node):
        for child in self['children']:
            if child.is_parent(node):
                child.add_node(node)
                break
        else:
            self['children'].append(node)

    def is_parent(self, node):
        if len(self['id']) == 4 and self['id'][-1] == '0':
            return node['id'].startswith(self['id'][:-1])
        return node['id'].startswith(self['id'])

class RootNode(Node):
    def __init__(self):
        Node.__init__(self, ('Root', '', ''))

    def is_parent(self, node):
        return True

def pretty_print(node, i=0):
    print '%sID=%s NAME=%s %s' % ('\t' * i, node['id'], node['name'], node['description'])
    for child in node['children']:
        pretty_print(child, i + 1)

def main():
    with open('input.csv') as f:
        f.readline() # Skip first line
        root = RootNode()
        for node in map(Node, csv.reader(f)):
            root.add_node(node)

    pretty_print(root)
    print json.dumps(root)

if __name__ == '__main__':
    main()

rawDescription: How to Create a JSON Tree from a Tabulated Hierarchy in Python


code:
import random
for i in range (10):
    ang = random.randrange(100000000,999999999)
    print int("1"+str(ang))

rawDescription: How to generate multiple 9 digit random numbers in one go and add a prefix number to them in python

description: Generate multiple 9 digit random numbers in one go and add a prefix number to them in python


rawDescription: Adding even values to new list Python

description: Adding even values to new list Python


code:
class Storage(object):
    def __init__(self):
        self.data = []

class Dialogue(object):
    def __init__(self):
        self.msg = 'hello world'

def main():
    d = Dialogue()
    s = Storage()
    s.data.append(d.msg)
    print s.data

if __name__ == '__main__':
    main()

rawDescription: Python pass string to class using *args in __init__


code:
import sys

def run(filename):
    with open(filename) as f:
        for line in f:
            print ">>> ", line
            exec line

if __name__ == "__main__":
    try:
        run(sys.argv[1])
    except KeyError:
        print "You should pass one filename as parameter."

rawDescription: Python script & stdout

description: Python script & stdout


code:
with open('your_file', 'rt') as filehandle:
  lines = filehandle.readlines()[4:]

for line in lines:
    print line
    #do something

rawDescription: Reading from a certain line onwards in python


code:
for line in f:
    value = line.rstrip('\n')
    url = "http://www.link.com/whatever/%s" % value
    print url
    urllib.urlretrieve(url, value)  # Not sure why you want to send value (your a) here

rawDescription: Why does my code keep stopping?

description: Why does my code keep stopping


code:
import jinja2
env = jinja2.Environment()
t = env.from_string("""
{% for field in fields %}
<td> {{ item[field] }} </td>
{% endfor %}""")
print t.generate(item={'key':'value'}, fields=['key']).next()

rawDescription: Jinja2 dictonary lookup using a variable key


code:
number = 7
def magicnumber (guess):
    magicnumber.counter += 1
    if(magicnumber.counter <= 5):
         if number<guess:
            print ("too high")
         elif number>guess:
            print ("too low")
         elif number == guess:
             print ("well done")
             magicnumber.counter = 0#If you want to reset the counter
         return
    else:
        print "Out of trials!"
        return

magicnumber.counter = 0

rawDescription: How to insert loop counter into my definition

description: Insert loop counter into my definition


code:
media = MediaFileUpload('image.png', mimetype='image/png', chunksize=1000, resumable=True)
request = drive.files().update(id='fileId', media_body=media)

response = None
while response is None:
  status, response = request.next_chunk()
  if status:
    print status.progress()

rawDescription: Google Drive SDK: MediaUploadProgress - not sure how to use this


code:
h4_tags = soup.find_all('h4', attrs = {'style' : True}) # Get all H4 tags with style attribute
for result in h4_tags:
    if len(result.attrs) == 1:                          # Print if it is the only attribute
        print result.contents                           # Print tag text contents

rawDescription: Regex Search after a variable length text


rawDescription: How to sort list of lists according to length of sublists

description: Sort list of lists according to length of sublists


code:
myxml=ET.parse('path_to_yourxml')
for elem in myxml.getroot().findall('pi/pe/i'):
    if elem.find('css').text!='-0':
        for elem1 in elem.findall('m'):
            if eval(elem1.find('rs/r/bsa').text)>0:
                print elem1.find('rs/r/name').text

rawDescription: parse xml with python - select children based on grandparent's siblings

description: Parse xml with python - select children based on grandparent's siblings


code:
from collections import defaultdict
d = defaultdict(int)
a = map(int, [x**0.5 for x in range(20)])
for i in a:
    d[i] += 1

print d

rawDescription: Count the number of elements of same value in Python


code:
import csv

input = ['abc,"a string, with a comma","another, one"']
parser = csv.reader(input)

for fields in parser:
  for i,f in enumerate(fields):
    print i,f    # in Python 3 and up, print is a function; use: print(i,f)

rawDescription: How to quickly parse a list of strings

description: Quickly parse a list of strings


code:
import re
import html2text

def convert_bbcode_fonts(html):
    flags = re.IGNORECASE | re.MULTILINE
    # replace start font tags
    html = re.sub(r'\[font\s*([^\]]+)\]', '', html, flags=flags)
    # replace end font tags
    html = re.sub(r'\[/font\s*\]', '', html, flags=flags)
    return html

def extract_text(html):
    html = convert_bbcode_fonts(html)
    h = html2text.HTML2Text()
    h.ignore_links = True
    h.ignore_images = True
    h.ignore_emphasis = True
    return h.handle(html)

INPUT = """
[font='Times New Roman']THIS[/font][font='Times New Roman'] IS [/font][font='Times New Roman']A TEST[/font][font='Times New Roman'] USING [/font][font='Times New Roman']SOME[/font][font='Times New Roman'] BEAUTIFUL [/font][font='Times New Roman']SOUP[/font][font='Times New Roman'] | [/font][font='Times New Roman']96786[/font][font='Times New Roman'] AND [/font][font='Times New Roman']HTML2TEXT[/font][font='Times New Roman'] TO LEARN [/font][font='Times New Roman']NEW THING[/font]
"""

if __name__ == '__main__':
    print extract_text(INPUT)

rawDescription: How to remove [font] tag using html2text/BeautifulSoup python

description: Remove [font] tag using html2text/BeautifulSoup python


code:
from lxml.html import fromstring
from lxml.html.clean import Cleaner
import requests

url = "http://stackoverflow.com/questions/2165943/removing-html-tags-from-a-text-using-regular-expression-in-python"
html = requests.get(url).text

doc = fromstring(html)

tags = ['h1','h2','h3','h4','h5','h6',
       'div', 'span',
       'img', 'area', 'map']
args = {'meta':False, 'safe_attrs_only':False, 'page_structure':False,
       'scripts':True, 'style':True, 'links':True, 'remove_tags':tags}
cleaner = Cleaner(**args)

path = '/html/body'
body = doc.xpath(path)[0]

print cleaner.clean_html(body).text_content().encode('ascii', 'ignore')

rawDescription: Removing html tags from a text using Regular Expression in python


code:
a=[]
b=[1]
a.append(b[:])
a.append(b[:])
a[0].append(2)
a[1].append(3)
print a

rawDescription: Why does foo.append(bar) affect all elements in a list of lists?


code:
import os
print os.path.normpath(os.path.join(os.getcwd(), '../AnotherBook/Chap2.txt'))

rawDescription: How do I tell a Python script (cygwin) to work in current (or relative) directories?

description: Tell a Python script (cygwin) to work in current (or relative) directories


code:
for name, digits in e_list:
    shortest = sorted([d for l, d in a_list if digits.startswith(d)], key=len)[0]
    print name, digits, shortest

rawDescription: Python: matching values from one list to the sequence of values in another list


code:
class Basic(object):
    def __init__(self, val):
        self.val = val

    def current_ins(self):
        #???
        print self.val    # one can use self to access all the member of it's class.

rawDescription: How to get current instance in class?

description: Get current instance in class


code:
for i in x:
    print i

rawDescription: Understanding iterables and generators in Python


code:
import datetime, xlrd
book = xlrd.open_workbook("myexcelfile.xls")
sh = book.sheet_by_index(0)
a1 = sh.cell_value(rowx=0, colx=0)
a1_as_datetime = datetime.datetime(*xlrd.xldate_as_tuple(a1, book.datemode))
print 'datetime: %s' % a1_as_datetime

rawDescription: How to read dates using xlrd?

description: Read dates using xlrd


code:
import feedparser
d = feedparser.parse('http://www.reddit.com/r/Python/.rss')
for entry in d.entries:
  date = entry.published_parsed
  if date.tm_year == 2015 and date.tm_mon == 4 and date.tm_mday >= 15 and date.tm_mday <= 16:
    print entry.title

rawDescription: Parse entries that is within specified date using feedparser


code:
import datetime
tote = datetime.timedelta(0, 25200), datetime.timedelta(0, 25200)
brac = sum(tote, datetime.timedelta(0))
brac
print brac
repr(brac)

rawDescription: how to access output from sum() before it changes

description: Access output from sum() before it changes


code:
print "Quiz Program"
print "----------------------------------------------"
print "This program stores quiz questions and allows you to take the quiz"
print "----------------------------------------------"
size = input("Enter number of questions: ")
i = 0
new_list = []
for i in xrange(size):
    question = raw_input("Please enter a question, enter to exit: ")
    question_dictionary =  {"question" : question }
    count = input("How many possible answers  do you want this question to have: ")
    answer_list = []
    for j in xrange(count):
        answer = raw_input("Please enter the next answer: ")
        answer_list.append(answer)
    print "------------------------------------------------"
    for j in xrange(count):
        print j + 1,"." ,answer_list[j]
    print "------------------------------------------------"
    question_dictionary["answers"] = answer_list
    correct_answer = input("Please enter the value of the correct answer: ")
    question_dictionary["correct_answer"] = correct_answer
    new_list.append(question_dictionary)
print "------------------------------------------------"
print new_list
true_answer = 0
for i in xrange(size):
    print "question" ,new_list[i]['question'],".\n",  # problem to print the question
    print "------------------------------------------------"
    print "Possible Answers: "
    for j in xrange(len(new_list[i]['answers'])):
        print j + 1,"." ,new_list[i]['answers'][j]
    print "------------------------------------------------"
    user_answer = input("Please select an answer: ")
    if(user_answer == new_list[i]['correct_answer']): # problem to get the right answer
        print "well done, you answered the question correctly!"
        true_answer += 1
    else:
        print "sorry, you didn't get the correct answer."
    print "------------------------------------------------"
print "Your final score was", true_answer," out of", size

rawDescription: print value of a dictionary when the dictionary is in list

description: Print value of a dictionary when the dictionary is in list


code:
import Image
from scipy import *
from scipy.optimize import leastsq

# Load the picture with PIL, process if needed
pic         = asarray(Image.open("band2.png"))

# Average the pixel values along vertical axis
pic_avg     = pic.mean(axis=2)
projection  = pic_avg.sum(axis=0)

# Set the min value to zero for a nice fit
projection /= projection.mean()
projection -= projection.min()

# Fit function, two gaussians, adjust as needed
def fitfunc(p,x):
    return p[0]*exp(-(x-p[1])**2/(2.0*p[2]**2)) + \
        p[3]*exp(-(x-p[4])**2/(2.0*p[5]**2))
errfunc = lambda p, x, y: fitfunc(p,x)-y

# Use scipy to fit, p0 is inital guess
p0 = array([0,20,1,0,75,10])
X  = xrange(len(projection))
p1, success = leastsq(errfunc, p0, args=(X,projection))
Y = fitfunc(p1,X)

# Output the result
print "Mean values at: ", p1[1], p1[4]

# Plot the result
from pylab import *
subplot(211)
imshow(pic)
subplot(223)
plot(projection)
subplot(224)
plot(X,Y,'r',lw=5)
show()

rawDescription: Correct method and Python package that can find width of an image's feature


code:
rally = Rally(server, user, password)
rally.enableLogging('rallyConnection.log')
rally.setProject("RallyTestPrj")

defectID = 'DE9221'
discussion_text = "Adding discussion from Python"

# get the defect entity so we can grab the defect oid which we'll need when
# creating the new ConversationPost
defect = rally.get('Defect', query='FormattedID = %s' % defectID, instance=True)

discussion_data = {"Artifact": defect.oid, "Text": discussion_text}

# create the discussion
try:
    discussion = rally.create('ConversationPost', discussion_data)
except Exception, details:
    sys.stderr.write('ERROR: %s \n' % details)
    sys.exit(1)
print "Defect updated with a new discussion entry"

rawDescription: Rally Python Api: Add discussion to defect


code:
import signal

def sigint_handler(signum, frame):
     print "my_variable =", frame.f_locals.get("my_variable", None)

signal.signal(signal.SIGINT, sigint_handler)

rawDescription: Can I make use of an interrupt to print a status while still continue process?

description: Make use of an interrupt to print a status while still continue process


code:
import pygame
pygame.init()
i = 0
clock = pygame.time.Clock()
while i < 10: # Just run a few cycles to check the output
    res = clock.tick(1) # Caps framerate to 1 fps
    print res # This should print out 1000
    i += 1

rawDescription: Pygame clock function returns a strange value


code:
data = urllib2.urlopen(link)
content = data.read()
# replace the script and style tags with html comments, so the bs4 just skips them
content = content.replace("<script", "<!--")
content = content.replace("</script>", "-->")
content = content.replace("<style", "<!--")
content = content.replace("</style>", "-->")
soup = BeautifulSoup(content, "lxml") # assuming you've imported lxml and bs4
comments = soup.findAll(text=lambda text:isinstance(text, Comment))
Comments = [comment.extract() for comment in comments] # remove the commenys
words = []
for i in soup.stripped_strings:
    print i
# i will print most of the text of the page line by line

rawDescription: Scraping majority of text in body

description: Scraping majority of text in body


code:
import datetime

def getFiletime(dt):
    microseconds = int(dt, 16) / 10
    seconds, microseconds = divmod(microseconds, 1000000)
    days, seconds = divmod(seconds, 86400)

    return datetime.datetime(1601, 1, 1) + datetime.timedelta(days, seconds, microseconds)

print format(getFiletime(hex(13024882639633631*10)[2:17]), '%a, %d %B %Y %H:%M:%S %Z')

rawDescription: How to parse the date_added field in Chrome Bookmarks file?

description: Parse the date_added field in Chrome Bookmarks file


code:
import requests
from bs4 import BeautifulSoup

with requests.Session() as r:
     url = 'https://www.youtube.com/user/ProgrammingKnowledge/videos'
     page = r.get(url)
     html = page.content
soup = BeautifulSoup(html, 'html.parser')
ren = soup.find_all('h3', 'yt-lockup-title')

for item in ren:
    item = item.text
    if 'Mongo' in item:
        print item

rawDescription: Web Scraping using BeautifulSoup4

description: Web Scraping using BeautifulSoup4


code:
with open('path/to/file/sites.csv', 'rU') as datafile:
    for line in datafile:
        print line.strip()

rawDescription: CSV Import with Python - Why brackets?

description: CSV Import with Python - Why brackets


code:
import re
from urlparse import urljoin

from bs4 import BeautifulSoup
import requests

BASE_URL = 'http://www.un.org/en/ga/search/'
URL = "http://www.un.org/en/ga/search/view_doc.asp?symbol=A/RES/68/278"
BASE_ACCESS_URL = 'http://daccess-ods.un.org'

# start session
session = requests.Session()
response = session.get(URL, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'})

# get frame links
soup = BeautifulSoup(response.text)
frames = soup.find_all('frame')
header_link, document_link = [urljoin(BASE_URL, frame.get('src')) for frame in frames]

# get header
session.get(header_link, headers={'Referer': URL})

# get document html url
response = session.get(document_link, headers={'Referer': URL})
soup = BeautifulSoup(response.text)

content = soup.find('meta', content=re.compile('URL='))['content']
document_html_link = re.search('URL=(.*)', content).group(1)
document_html_link = urljoin(BASE_ACCESS_URL, document_html_link)

# follow html link and get the pdf link
response = session.get(document_html_link)
soup = BeautifulSoup(response.text)

# get the real document link
content = soup.find('meta', content=re.compile('URL='))['content']
document_link = re.search('URL=(.*)', content).group(1)
document_link = urljoin(BASE_ACCESS_URL, document_link)
print document_link

# follow the frame link with login and password first - would set the important cookie
auth_link = soup.find('frame', {'name': 'footer'})['src']
session.get(auth_link)

# download file
with open('document.pdf', 'wb') as handle:
    response = session.get(document_link, stream=True)

    for block in response.iter_content(1024):
        if not block:
            break

        handle.write(block)

rawDescription: How to get contents of frames automatically if browser does not support frames + can't access frame directly

description: Get contents of frames automatically if browser does not support frames + can't access frame directly


code:
from pprint import pprint
from itertools import count
import json
counter = count(1)
def verbose_object_hook(dct):
    print 'Object #{}'.format(next(counter))
    pprint(dct)
    return dct

sample = '''\
{
  "list_of_x": {
      "type": "x",
      "items" : [
        { "key" : "value" },
        { "key" : "value" }
      ]
  }
}
'''
json.loads(sample, object_hook=verbose_object_hook)

rawDescription: How do I make a graph of custom json deserializers in Python

description: Make a graph of custom json deserializers in Python


code:
total_lines = 0
matched_lines = 0
for line in open("filename"):
    total_lines += 1
    matched_lines += bool(re.search(r"\b[A-Z]{3}\b", line))
print "%f%%" % (float(matched_lines) / total_lines * 100)

rawDescription: Counting three letter acronyms in a line with Regex Python


code:
def keyPressEvent(self, event):
    if (event.modifiers() & QtCore.Qt.ShiftModifier):
        self.shift = True
        print 'Shift!'
    # call base class keyPressEvent
    QtGui.QLineEdit.keyPressEvent(self, event)

rawDescription: Properly handling a keyPressEvent in a Subclassed PyQT LineEdit


code:
import re

s = "Idaho Ave N,,Crystal,Mn,55427-1463,US,,610839124763,Expedited"

def upcase(match):
    return match.group().upper()

print re.sub("[A-Z][a-z],", upcase, s)

rawDescription: Python RE question - proper state initial formatting


code:
import Tkinter as tk
import ttk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        part_num = '1234'
        self.newPNVar = tk.StringVar()
        newPN = ttk.Entry(self, width=13, textvariable=self.newPNVar)
        self.newPNVar.set(part_num)
        newPN.pack()
        #newPN.insert(0, part_num)  also didn't work

        print self.newPNVar.get()

app = SampleApp()
app.mainloop()

rawDescription: ttk.Entry not behaving the same way that tk.Entry does

description: Ttk.Entry not behaving the same way that tk.Entry does


code:
user_id = '00000000000'
print api.show_friendship(target_id=user_id)

rawDescription: Tweepy ( Twitter ) change to exists_friendship -> lookup_friendship ( Python )


rawDescription: connected components using Breadth first search

description: Connected components using Breadth first search


code:
import re
a="BILLY: The sky is blue. SALLY: It really is blue. SAM: I think it looks like this: terrible."
print re.split(r"\s(?=[A-Z]+:)",a)

rawDescription: Split Text in Python

description: Split Text in Python


code:
import random
user_input = 'this is a test string'.split()
n = len(user_input)
i = random.randint(0, n-2)
words = user_input[i:i+2]
print words

['is', 'a']

rawDescription: trying to match match two consecutive words in a list

description: Trying to match match two consecutive words in a list


code:
for items in groupList:
    setFolder = []
    os.chdir(items) ## creates a list of lists for each drive
    print 'Scanning drive:',os.getcwd()
    for line in os.listdir(items):
        if len(line)==3 and line != 'sec':
            setFolder.append(int(line)) ##appends new dataset to old one
    setFolder.sort()
    dataList.append(setFolder)

rawDescription: Seperate data from different sources into lists of lists for analysis


code:
from pylab import *
import netCDF4

# open NetCDF input files

f = netCDF4.MFDataset('/usgs/data2/rsignell/models/ncep/narr/air.2m.19??.nc')
# print variables
f.variables.keys()

atemp = f.variables['air']
print atemp

ntimes, ny, nx = shape(atemp)
cold_days = zeros((ny,nx),dtype=int)

# create output NetCDF file

nco = netCDF4.Dataset('/usgs/data2/notebook/cold_days.nc','w',clobber=True)
nco.createDimension('x',nx)
nco.createDimension('y',ny)
nco.createDimension('time',ntimes)

cold_days_v = nco.createVariable('cold_days', 'i4',  ( 'time', 'y', 'x'))
cold_days_v.units='days'
cold_days_v.long_name='total number of days below 0 degC'
cold_days_v.grid_mapping = 'Lambert_Conformal'

timeo = nco.createVariable('time','f8',('time'))
lono = nco.createVariable('lon','f4',('y','x'))
lato = nco.createVariable('lat','f4',('y','x'))
xo = nco.createVariable('x','f4',('x'))
yo = nco.createVariable('y','f4',('y'))
lco = nco.createVariable('Lambert_Conformal','i4')

# copy all the variable attributes from original file
for var in ['time','lon','lat','x','y','Lambert_Conformal']:
    for att in f.variables[var].ncattrs():
        setattr(nco.variables[var],att,getattr(f.variables[var],att))

# copy variable data for time, lon,lat,x and y
timeo[:] = f.variables['time'][:]
lato[:] = f.variables['lat'][:]
xo[:] = f.variables['x'][:]
yo[:] = f.variables['y'][:]

for i in xrange(ntimes):
    cold_days += atemp[i,:,:].data-273.15 < 0
    #  write the cold_days data
    cold_days_v[i,:,:]=cold_days

# copy Global attributes from original file
for att in f.ncattrs():
    setattr(nco,att,getattr(f,att))

nco.Conventions='CF-1.6'
nco.close()

rawDescription: Efficient netCDF analysis when looping through data


code:
def contact(request):
    if request.method == 'POST': # If the form has been submitted...
        form = ContactForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...

            print form.cleaned_data['my_form_field_name']

            return HttpResponseRedirect('/thanks/') # Redirect after POST
    else:
        form = ContactForm() # An unbound form

    return render_to_response('contact.html', {
        'form': form,
    })

rawDescription: How to get value from form field in django framework?

description: Get value from form field in django framework


rawDescription: Format number in python with leading zeros and fixed decimal places


code:
import uuid
if val in valdict: # We have seen this reference before
    print '<value href="#%s"/>' % valdict[val]
else:              # We need to add this reference
    valdict[val] = str(uuid.uuid4()).upper()
    print '<value id="%s">%s</value>' % (valdict[val], val)

rawDescription: Simplest way to find and link repeating strings in an XML with Python


code:
line = '1 0:2.0 3:4.0 123:1.0\n'

def parse_line(line):
   data = line.split(" ")
   return(dict((i.split(":")) for i in data[1:]),data[0])

print parse_line(line)

rawDescription: Mapping feature and feature value from a line to a dictionary in python


code:
stuff=urllib2.urlopen("http://httpbin.org/headers").read()

print stuff
{
  "headers": {
    "Accept-Encoding": "identity",
    "Connection": "close",
    "Host": "httpbin.org",
    "User-Agent": "Python-urllib/2.7",
    "X-Request-Id": "43jhc13b-3dj4-4eb5-8780-ad7cfs4790cd"
  }
}

rawDescription: Custom user-agent with urllib2, python 2.7