code:
from Tkinter import *

master = Tk()
master.geometry('800x480+0+0')
master.title('Gateway Logger')

names = ['Annette', 'Heather', 'Tracey', 'Amy', 'Josh', 'Chris'
         , 'Lekh', 'Ellie', 'Sarah', 'Richard', 'Paula', 'Michelle'
         , 'Martin', 'Dave', 'Phil']
visitors = ['Visitor 1', 'Visitor2', 'Visitor 3', 'Visitor 4', 'Visitor 5', 'Visitor 6']

name_width = 20
name_height = 5
xpad = 5
ypad = 5
col = 0
roww = 0

sarah='out'

def clock_mast(n):
    iowid=40
    iohi=15
    global clock
    print "This is: " +str(n)
    clock = Toplevel(width=300, height =300)
    clock.title('Clock In/Out')

    staff_name = Label (clock, text = n, font = "Verdana 24")
    staff_name.grid(row=1,column=1,columnspan=2)

    inbut=Button(clock, width=iowid, height=iohi, text='Clock In', bg='green',command=lambda n=n:clock_in(n))
    inbut.grid(row=2, column=1, padx=10,pady=50, columnspan=1)
    outbut=Button(clock, width=iowid, height=iohi, text='Clock Out', bg='red',command=lambda n=n:clock_out(n))
    outbut.grid(row=2, column=2, padx=10,pady=50, columnspan=1)

def clock_in(n):
    global clock
    global button_dict             #This is not really necessary, but I put it there to be explicit.
    button_dict[n].configure(bg='green')     #Choose whatever properties you want to choose to configure
    clock.destroy()

def clock_out(n):
    global clock
    global button_dict             #This is not really necessary, but I put it there to be explicit.
    button_dict[n].configure(bg='red')     #Choose whatever properties you want to choose to configure
    clock.destroy()

button_dict = {}

for n in names:
    button_dict[n] = Button(master,text=n,width = name_width, height = name_height,command=lambda n=n: clock_mast(n))
    button_dict[n].grid(row = roww, column = col,padx = xpad, pady = ypad)
    col += 1
    if col == 3:
        col = 0
        roww += 1

master.mainloop()

rawDescription: Python tkinter change individual button parameters created in for loop


code:
import re
pattern = '[A-Za-z0-9-]+' # pattern for matching all ASCII characters, digits, and repetitions of
                            # them (+)
lines = "property"           # adding input string, raw_input("Enter Combination: ")
ls = re.findall(pattern,lines)
print ls

rawDescription: Regex FindAll occurances of a string inside a text file. How?


code:
class B(object):
    pass

class D(B):
    pass

print issubclass(B, object)
print issubclass(D, B) # true
# order matters:
print issubclass(B, D) # false
# it's transitive:
print issubclass(D, object)
# a class is a subclass of itself:
print issubclass(B, B) # true

rawDescription: Python - isinstance(classInfo, classInfo)


code:
import ast
with open('new.txt') as f:
    d = ast.literal_eval(f.read())
print d['String2']['Value2']['One']

rawDescription: Displaying the text file data in dictionary format


code:
def random_distr(l):
    assert l # don't accept empty lists
    r = random.uniform(0, 1)
    s = 0
    for i in xrange(len(l)):
        item, prob = l[i]
        s += prob
        if s >= r:
            l.pop(i) # remove the item from the distribution
            break
    else: # Might occur because of floating point inaccuracies
        l.pop()
    # update probabilities based on new domain
    d = 1 - prob
    for i in xrange(len(l)):
        l[i][1] /= d
    return item, l

dist = [[1, 0.5], [2, 0.25], [3, 0.05], [4, 0.01], [5, 0.09], [6, 0.1]]
while dist:
    val, dist = random_distr(dist)
    print val

rawDescription: Python: Selecting numbers with associated probabilities


code:
data = 'Field_1 \t Field_2 \t Field_3 \n Additional Text \n More text \t Field_4'
print data.split('\t')

rawDescription: (Python) Parsing tab delimited strings with newline characters


code:
user = User.objects.create_user(username=new_data['username'], email=new_data['email'])
print user.pk #Already works as create_user saves the new instance.
user.first_name = new_data['email'] #Can't assign to first_name in create_user.
user.save()
print user.pk #Will work.

rawDescription: Django User ID fields

description: Django User ID fields


code:
from multiprocessing import Process, Pipe

def f(conn): #This code will be spawned as a new child process
    conn.send([42, None, 'hello']) #The child process sends a msg to the pipe
    conn.close()

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()
    p = Process(target=f, args=(child_conn,)) # prepare to spawn the child
    p.start() # spawn it
    print parent_conn.recv()   # prints "[42, None, 'hello']"
    p.join() #wait for child to exit

rawDescription: Call a function from a running process

description: Call a function from a running process


code: print myvar2['encoding']

rawDescription: Splitting results from chardet output to collect encoding type


code:
from PyQt4 import QtGui, QtCore

class Button(QtGui.QPushButton):
    def __init__(self, *args, **kwargs):
        QtGui.QPushButton.__init__(self, *args, **kwargs)
        self.setAutoRepeat(True)
        self.setAutoRepeatDelay(1000)
        self.setAutoRepeatInterval(1000)
        self.clicked.connect(self.handleClicked)
        self._state = 0

    def handleClicked(self):
        if self.isDown():
            if self._state == 0:
                self._state = 1
                self.setAutoRepeatInterval(50)
                print 'press'
            else:
                print 'repeat'
        elif self._state == 1:
            self._state = 0
            self.setAutoRepeatInterval(1000)
            print 'release'
        else:
            print 'click'

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    button = Button('Test Button')
    button.show()
    sys.exit(app.exec_())

rawDescription: Python: How to distinguish between long, short button press and click


code:
from Tkinter import *
master = Tk()

def getThrottle(event):
    print Throttle.get()

Throttle = Scale(master, from_=0, to=100, orient=HORIZONTAL, command=getThrottle)
Throttle.set(0)
Throttle.pack()

mainloop()

rawDescription: In Tkinter how can I constantly get a slider value


code:
import sys
import tweepy

consumer_key=""
consumer_secret=""
access_key=""
access_secret=""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        if 'manchester united' in status.text.lower():
            print status.text

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream

sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(locations=[-6.38,49.87,1.77,55.81])

rawDescription: How to add a location filter to tweepy module

description: Add a location filter to tweepy module


code:
import os
from itertools import groupby

directory = r'C:\Users\x41\Desktop\Test'
output = []

for basename, group in groupby(sorted(os.listdir(directory)), lambda x: x.rsplit('.')[0]):
    output.extend(sorted(group, key=lambda x: os.stat(os.path.join(directory, x)).st_mtime)[-2:])

print output

rawDescription: Grouping and deleting Files

description: Grouping and deleting Files


code:
def walk(top, topdown=True, onerror=None, followlinks=False):
    islink, join, isdir = path.islink, path.join, path.isdir

    try:
        # Note that listdir and error are globals in this module due
        # to earlier import-*.

        # Should be O(1) since it's probably just reading your filesystem journal
        names = listdir(top)
    except error, err:
        if onerror is not None:
            onerror(err)
        return

    dirs, nondirs = [], []

    # O(n) where n = number of files in the directory
    for name in names:
        if isdir(join(top, name)):
            dirs.append(name)
        else:
            nondirs.append(name)

    if topdown:
        yield top, dirs, nondirs

    # Again O(n), where n = number of directories in the directory
    for name in dirs:
        new_path = join(top, name)
        if followlinks or not islink(new_path):

            # Generator so besides the recursive `walk()` call, no additional cost here.
            for x in walk(new_path, topdown, onerror, followlinks):
                yield x
    if not topdown:
        yield top, dirs, nondirs

rawDescription: Time complexity of os.walk in Python

description: Time complexity of os.walk in Python


code:
a = (7,8,9,10,11,12)   #or any other sequence of integers

print set(y-x for x,y in zip(a,a[1:])) in ({1}, {-1})

rawDescription: check consecutive digits in tuple

description: Check consecutive digits in tuple


code:
with open('pic1.jpg', 'wb') as handle:
        response = requests.get(pic_url, stream=True)

        if not response.ok:
            print response

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

            handle.write(block)

rawDescription: python save image from url

description: Python save image from url


rawDescription: Subset sum for large sums

description: Subset sum for large sums


code:
import re

with open('yourfile') as fin:
    page_source = fin.read()

start_text = re.escape('
')
until_text = re.escape('
')
match_text = re.search('{}(.*?){}'.format(start_text, until_text), page_source, flags=re.DOTALL)
if match_text:
    print match_text.group(1)

rawDescription: Regex multiline- How to grab a portion of a page source


code:
from mock import MagicMock
import __builtin__
from mock import patch
import sys

# Reference to the original open function.
g__test_utils__original_open = open
g__test_utils__file_spec = None

def create_file_mock(read_data):

    # Create file_spec such as in mock.mock_open
    global g__test_utils__file_spec
    if g__test_utils__file_spec is None:
        # set on first use
        if sys.version_info[0] == 3:
            import _io
            g__test_utils__file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO))))
        else:
            g__test_utils__file_spec = file

    file_handle = MagicMock(spec=g__test_utils__file_spec)
    file_handle.write.return_value = None
    file_handle.__enter__.return_value = file_handle
    file_handle.read.return_value = read_data
    return file_handle

def flexible_mock_open(file_map):
    def flexible_side_effect(file_name):
        if file_name in file_map:
            return file_map[file_name]
        else:
            global g__test_utils__original_open
            return g__test_utils__original_open(file_name)

    global g__test_utils__original_open
    return_value = MagicMock(name='open', spec=g__test_utils__original_open)
    return_value.side_effect = flexible_side_effect
    return return_value

if __name__ == "__main__":
    a_mock = create_file_mock(read_data="a mock - content")
    b_mock = create_file_mock(read_data="b mock - different content")
    mocked_files = {
        'a' : a_mock,
        'b' : b_mock,
    }
    with patch.object(__builtin__, 'open', flexible_mock_open(mocked_files)):
        with open('a') as file_handle:
            print file_handle.read() # prints a mock - content

        with open('b') as file_handle:
            print file_handle.read() # prints b mock - different content

        with open('actual_file.txt') as file_handle:
            print file_handle.read() # prints actual file contents

rawDescription: How to mock open differently depending on the parameters passed to open()

description: Mock open differently depending on the parameters passed to open()


code:
def permutateMap(X):
    result = []
    for key, value in X.items():
        splitted = value.split(',')
        if len(splitted) > 1:
            for s in splitted:
                new_dict = X.copy()
                new_dict[key] = s.strip()
                result += [new_dict]
            return splitList(result)
    return [X]

def splitList(X):
    result = []
    for entry in X:
        result += permutateMap(entry)
    return result

my_dicts = {"A": [
            { 'key1 a' : 'value1, value2',
              'key2 a' : 'A, B, C' },
            { 'key1 a' : 'value3',
              'key2 a' : 'D, E' }]}

new_dict = {}
for key, value in my_dicts.items():
    new_dict[key] = splitList(value)

print new_dict

rawDescription: Splitting a dictionary entry in a list to two


code:
while True:
    s.flushInput()
    print s.readline()
    time.sleep(.5)

rawDescription: Reading a Serial Port - Ignore portion of data written to serial port for certain time


code: result = ['{1} - {0}'.format(*pair) for pair in sorted(text_to_count.iteritems(), key = lambda (_,v): v, reverse = True)]

rawDescription: How to sort a dict by values and return a list of formatted strings?

description: Sort a dict by values and return a list of formatted strings


code:
from bs4 import BeautifulSoup
import requests

url = 'http://www.amazon.com/Best-Sellers-Books-Architecture/zgbs/books/173508/#2'
ajax_url = "http://www.amazon.com/Best-Sellers-Books-Architecture/zgbs/books/173508/ref=zg_bs_173508_pg_2"

def get_books(data):
    soup = BeautifulSoup(data)

    for title in soup.select("div.zg_itemImmersion div.zg_title a"):
        print title.get_text(strip=True)

with requests.Session() as session:
    session.get(url)

    session.headers = {
        'User-Agent': 'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
        'X-Requested-With': 'XMLHttpRequest'
    }

    for page in range(1, 10):
        print "Page #%d" % page

        params = {
            "_encoding": "UTF8",
            "pg": str(page),
            "ajax": "1"
        }
        response = session.get(ajax_url, params=params)
        get_books(response.content)

        params["isAboveTheFold"] = "0"
        response = session.get(ajax_url, params=params)
        get_books(response.content)

rawDescription: Scraping the second page of a website in Python does not work


code:
import wx

WHITE_COLOR = (255,255,255)

class AnimationPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.boxParameters = [10,10,60,60]
        self.SetBackgroundColour(wx.Colour(*WHITE_COLOR))
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
        self.timer.Start(100)

    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        self.paintBox(dc)

    def OnTimer(self, event):
        self.boxParameters[0] += 3
        print self.boxParameters
        self.Refresh()

    def paintBox(self, dc):
        dc.SetBrush(wx.Brush("blue"))
        dc.DrawRectangle(*self.boxParameters)

class MainFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title="Box with far and back movement", size=(300,200))
        AnimationPanel(self)

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MainFrame()
    frame.Show(True)
    app.MainLoop()

rawDescription: wxPython using wxTimer : my box doesn't move

description: WxPython using wxTimer : my box doesn't move


code:
class MC(type):
  def __repr__(self):
    return 'Wahaha!'

class C(object):
  __metaclass__ = MC

print C

rawDescription: How to create a custom string representation for a class object?

description: Create a custom string representation for a class object


code:
import os
import re

all_dirs = []
for root, dirs, files in os.walk("root", topdown=True):
    subset = []
    for d in dirs:
        if not re.match('[dmp]\d{8}$', d):
            # step inside
            subset.append(d)
        else:
            # add to list
            all_dirs.append(os.path.join(root, d))
    dirs[:] = subset

print all_dirs

rawDescription: Python os.walk topdown true with regular expression


code:
import subprocess
import pandas as pd
import numpy as np
import os, sys
from os.path import basename

# CSV IMPORT DEFINED FUNCTION
def csvImport(ftype, fpath):
    try:
       if ftype == 1:
           masterdata = pd.read_csv(fpath)
           return masterdata

       if ftype == 2:
           updateddata = pd.read_csv(fpath)
           updateddata['originfile'] = pd.Series(os.path.basename(fpath), \
                                                 index=updateddata.index)
           return updateddata

    except Exception as e:
       print "\nUnable to import CSV file. Error {}".format(e)
       sys.exit(1)

# EXCEL IMPORT DEFINED FUNCTION
def xlImport(ftype, fpath):
    try:
        if ftype == 1:
           masterdata = pd.read_excel(fpath, 0)
           return masterdata

        if ftype == 2:
           updateddata = pd.read_excel(fpath, 0)
           updateddata['orginfile'] = pd.Series(os.path.basename(fpath), \
                                                index=updateddata.index)
           return updateddata

    except Exception as e:
       print "\nUnable to import Excel file. Error {}".format(e)
       sys.exit(1)

# MASTER FILE USER INPUT DEFINED FUNCTION
def masterfile():
    while True:
       masterfile = raw_input("Enter the path to the master file: ")
       if masterfile.endswith(".csv"):
          return csvImport(1, masterfile)
          break
       elif masterfile.endswith(".xlsx"):
          return xlImport(1, masterfile)
          break
       else:
          print "\nPlease enter a proper CSV format file."

# UPDATED FILE USER INPUT DEFINED FUNCTION
def updatefile():
    while True:
       updatedfile = raw_input("\nEnter the path to the updated file: ")
       if updatedfile.endswith(".csv"):
          return csvImport(2, updatedfile)
          break
       elif updatedfile.endswith(".xlsx"):
          return xlImport(2, updatedfile)
          break
       else:
          print "\nPlease enter a proper Excel file in xlsx format."

# CALLING OPENING FUNCTIONS
masterdata = masterfile()
updateddata = updatefile()

# CONCATENATING DATA FRAMES
combineddata = pd.concat([updateddata, masterdata])

# REMOVING DUPLICATES
finaldata = combineddata.drop_duplicates(['Item'])

# SETTING FINAL PATH BY USER INPUT
while True:
    final = raw_input("\nWhere do you want the file, and what do you want to name it? \
                      (e.g., C:\path_to_file\name_of_file.xlsx): ")
    if final.endswith(".xlsx"):
        break
    else:
        print "\nPlease enter a proper Excel file in xlsx format."

# OUTPUTTING DATA FRAME TO FILE
finaldata.to_excel(final)
print "\nSuccessfully outputted appended data frame to Excel!"

# OPENING OUTPUTTED FILE
# (NOTE: PYTHON STILL RUNS UNTIL SPREADSHEET IS CLOSED)
subprocess.call(final, shell=True)

rawDescription: Combining excel sheets with pandas in python


code:
from lxml import etree
root = etree.fromstring(your_text)
print root.xpath("//td[contains(text(), 'Image')]/following-sibling::td/a/@href")[0]

rawDescription: Extracting XML nodes in Python

description: Extracting XML nodes in Python


code:
con = httplib.HTTPConnection("myweb.com")
while True:
    con.request("GET", "/x.css", headers={"Connection":" keep-alive"})
    result = con.getresponse()
    result.read()
    print result.reason, result.getheaders()

rawDescription: does httplib reuse TCP connections?

description: Does httplib reuse TCP connections


code:
import lxml.html
page = lxml.html.parse('http://www.google.com').getroot()
print page.xpath('//a/@href')

rawDescription: How to run an xpath over html page in python?

description: Run an xpath over html page in python


code:
lis = [3,5,6,2]
for ind, item in enumerate(lis):
    print "{}:{}".format(ind, item)

rawDescription: How to format the output of an array in python

description: Format the output of an array in python


code:
if re.match(r'\A[\w-]+\Z', word):
  print "Single word"

rawDescription: Determine if string in input is a single word in Python


code:
import Image

im = Image.open('image.gif')
rgb_im = im.convert('RGB')
r, g, b = rgb_im.getpixel((1, 1))

print r, g, b
(65, 100, 137)

rawDescription: Image reading for RGB colors?

description: Image reading for RGB colors


rawDescription: Securing Credit Card numbers in python using RE


code:
import datetime

timenow = datetime.datetime.now()

timeonehourago = timenow - datetime.timedelta(hours=1)

url = timenow.strftime("http://nomads.ncep.noaa.gov/pub/data/nccf/nonoperational/com/hrrr/para/hrrr.%Y%m%d/hrrr.t%Hz.wrfnatf04.grib2")

offseturl = timeonehourago.strftime("http://nomads.ncep.noaa.gov/pub/data/nccf/nonoperational/com/hrrr/para/hrrr.%Y%m%d/hrrr.t%Hz.wrfnatf04.grib2")

print url
print offseturl

rawDescription: Python Time 'Lag' Effect in URL

description: Python Time 'Lag' Effect in URL


code:
import sys

def print_et_list (file_handle):
    answer_list = []
    #Question
    question = file_handle.readline()
    print question
    #Choices
    one = file_handle.readline()
    two = file_handle.readline()
    answer_list.append(one)
    answer_list.append (two)
    for item in answer_list:
        print item
    #Solution
    solution = None
    try:
        result = file_handle.readline()
        result.replace("\n","")
        solution = int(result)
    except:
        print "There's an error in the answer"

    ##for the blank line
    file_handle.readline()
    return question, one, two, solution

  ##file_handle for prompting the user for an answer, comparing an answer, keeping score        and printing score.
def hey_user (solution, score=0):
    user_answer = int(raw_input ("Enter what you think the answer is, user.\n"))
    print "you answered '%s'"%user_answer
    if user_answer == solution:
        print "You've got it right!"
        score += 1
    elif user_answer == 0:
        sys.exit()
    else:
        print "You've got it wrong."
    return score

def main ():
        file_handle = open ("modStory.txt","r")
        question, one, two, solution = print_et_list(file_handle)
        scoresofar = hey_user(solution)
        print "\nYour score is now", scoresofar
        while question:
            question, one, two, solution = print_et_list(file_handle)
            if question:
                scoresofar = hey_user(solution, scoresofar)
                print "\nYour score is now", scoresofar
        file_handle.close()

main ()

raw_input ("Hit enter to exit.")

rawDescription: Debugging Trivia game in Python

description: Debugging Trivia game in Python


code:
with open("test.txt", "r") as my_file:
    for line in my_file:
        line = re.findall('(\w+)', line)
        print '{Username:"%s", Application:%s}' % (line[0], line[1])
        print '{Username:"%s", Application:%s}' % (line[2], line[3])

rawDescription: Python Split strings

description: Python Split strings


code:
import mechanize

browser = mechanize.Browser()

browser.open('https://webfiles.duke.edu/?path=/afs/acpub/users/a')

browser.select_form(nr=0)

browser.form['user'] = 'username'
browser.form['pass'] = 'password'
req = browser.submit()

print req.read()

rawDescription: Python auth_handler not working for me

description: Python auth_handler not working for me


code:
import imaplib

#copy from
from_server = {'server': '1.1.1.1',
               'username': 'j@example.com',
               'password': 'pass',
               'box_names': ['Sent', 'Sent Messages']}

#copy to
to_server = {'server': '2.2.2.2',
             'username': 'archive',
             'password': 'password',
             'box_name': 'Sent'}

def connect_server(server):
    conn = imaplib.IMAP4(server['server'])
    conn.login(server['username'], server['password'])
    print 'Logged into mail server @ %s' % server['server']
    return conn

def disconnect_server(server_conn):
    out = server_conn.logout()

if __name__ == '__main__':
    From = connect_server(from_server)
    To = connect_server(to_server)

    for box in from_server['box_names']:
        box_select = From.select(box, readonly = False)  #open box which will have its contents copied
        print 'Fetching messages from \'%s\'...' % box
        resp, items = From.search(None, 'ALL')  #get all messages in the box
        msg_nums = items[0].split()
        print '%s messages to archive' % len(msg_nums)

        for msg_num in msg_nums:
            resp, data = From.fetch(msg_num, "(FLAGS INTERNALDATE BODY.PEEK[])") # get email
            message = data[0][1]
            flags = imaplib.ParseFlags(data[0][0]) # get flags
            flag_str = " ".join(flags)
            date = imaplib.Time2Internaldate(imaplib.Internaldate2tuple(data[0][0])) #get date
            copy_result = To.append(to_server['box_name'], flag_str, date, message) # copy to archive

            if copy_result[0] == 'OK':
                del_msg = From.store(msg_num, '+FLAGS', '\\Deleted') # mark for deletion

        ex = From.expunge() # delete marked
        print 'expunge status: %s' % ex[0]
        if not ex[1][0]: # result can be ['OK', [None]] if no messages need to be deleted
            print 'expunge count: 0'
        else:
            print 'expunge count: %s' % len(ex[1])

    disconnect_server(From)
    disconnect_server(To)

rawDescription: Script to move messages from one IMAP server to another


code:
class C(B):
    def __init__(self):
        print "C init"

    @classmethod
    def _method(cls):
        print "calling super(C)'s classmethod"
        return super(C, cls)._method()

rawDescription: How do I access superclass's classmethod?

description: Access superclass's classmethod


code:
data = []
with open("data.txt") as f:
    for line in f:
        data.append([word for word in line.split("  ") if word])
print data
import xlwt
wb = xlwt.Workbook()
sheet = wb.add_sheet("New Sheet")
for row_index in range(len(data)):
    for col_index in range(len(data[row_index])):
        sheet.write(row_index, col_index, data[row_index][col_index])
wb.save("newSheet.xls")

rawDescription: Python text file strings into columns in spreadsheet


code:
from urlparse import urlparse

o = urlparse("http://127.0.0.1:5000/")
print o.hostname  # will display '127.0.0.1'

rawDescription: Get Host Name Without Port in Flask

description: Get Host Name Without Port in Flask


code:
h = 0
phase = 0
while True:
    try:
        h = int(raw_input("Please Enter your altitude in metres > "))
        if h > 0 and h < 11000:
            phase = 'Troposphere'
            break
    except ValueError:
            print 'Your entered value contained letters or punctuation. Please enter a numerical value.'

rawDescription: Variable defined in while loop cannot be defined


code:
def xl_rowcol_to_cell(row_num, col_num):

    # Removed these 2 lines if your row, col is 1 indexed.
    row_num += 1
    col_num += 1

    col_str = ''

    while col_num:
        remainder = col_num % 26

        if remainder == 0:
            remainder = 26

        # Convert the remainder to a character.
        col_letter = chr(ord('A') + remainder - 1)

        # Accumulate the column letters, right to left.
        col_str = col_letter + col_str

        # Get the next order of magnitude.
        col_num = int((col_num - 1) / 26)

    return col_str + str(row_num)

print xl_rowcol_to_cell(3, 15) # print P4

rawDescription: Converting XY Position Notation to Spreadsheet Position Notation


code:
keys = ['lastname', 'firstname', 'email', 'id', 'phone']
dicts = []
with open("oldFile.txt") as f:
    for line in f:
        # Split each line.
        line = line.strip().split()
        # Create dict for each row.
        d = dict(zip(keys, line))
        # Print the row dict
        print d
        # Store for future use
        dicts.append(d)

rawDescription: Text File to Dictionary with multiple columns


code:
import SimpleHTTPServer, SocketServer
import urlparse

PORT = 80

class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
   def do_GET(self):

       # Parse query data & params to find out what was passed
       parsedParams = urlparse.urlparse(self.path)
       queryParsed = urlparse.parse_qs(parsedParams.query)

       # request is either for a file to be served up or our test
       if parsedParams.path == "/test":
          self.processMyRequest(queryParsed)
       else:
          # Default to serve up a local file
          SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self);

   def processMyRequest(self, query):

       self.send_response(200)
       self.send_header('Content-Type', 'application/xml')
       self.end_headers()

       self.wfile.write("<?xml version='1.0'?>");
       self.wfile.write("<sample>Some XML</sample>");
       self.wfile.close();

Handler = MyHandler

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

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

rawDescription: Having Trouble Getting SimpleHTTPRequestHandler to respond to AJAX


code:
import fileinput

for line in fileinput.input('filename', inplace=True):
    # do what you need to do and then just print:
    print line.replace('1234', '24')

rawDescription: Replacing string, integer, float in a file txt


code:
file_list = []

parser = argparse.ArgumentParser()
parser.add_argument('file', nargs='*')
args = parser.parse_args()

for filepath in args.file:
    x = os.path.abspath(filepath)
    with open(filepath, 'r') as f:
        data = f.read()
        print data
        file_list.append(x)

rawDescription: Using argparse, how can I put the user input into a list?


code:
string ="someText:someValue~"
print re.split("(:|~)",string,1)

rawDescription: Python split a string w/ multiple delimiter and find the delimiter used


code:
part1 = pd.DataFrame({'id'    :[100,200,300,400,500],
                   'amount': np.arange(5)
                    })

part2 = pd.DataFrame({'id'    :[700,100,800,500,300],
                   'amount': np.random.randn(5)
                    })

concatenated = pd.concat([part1, part2], axis=0)
print concatenated.drop_duplicates(cols="id")

rawDescription: Pandas: concatenating conditioned on unique values


code: print ",".join(row)

rawDescription: Print elements of a list to a .csv file

description: Print elements of a list to a .csv file


code:
import os
for root, dirs, files in os.walk('/root'):
    for basename in files:
        filename = os.path.join(root, basename)
        status = os.stat(filename)
        if status.st_mtime > timestamp:
            print filename

rawDescription: Quickly find recently changed files

description: Quickly find recently changed files


code:
import nltk
from random import choice, shuffle, random

# Sometimes i find reading terminals as values into a dict of POS helps.
vocab={
'Det':['a','an','the','my'],
'N':['man','dog','cat','telescope','park'],
'V':['saw','ate','walked'],
'P':['in','on','by','with'],
'NP':['John','Mary','Bob']
}

vocab2string = [pos + " -> '" + "' | '".join(vocab[pos])+"'" for pos in vocab]

# Rules are simpler to be manually crafted so i left them in strings
rules = '''
S -> NP VP
VP -> V NP
VP -> V NP PP
PP -> NP P
NP -> Det N
'''

mygrammar = rules + "\n".join(vocab2string)
grammar = nltk.parse_cfg(mygrammar) # Loaded your grammar
parser =  nltk.ChartParser(grammar) # Loaded grammar into a parser

# Randomly select one terminal from each POS, based on infinite monkey theorem, i.e. selection of words without grammatical order, see https://en.wikipedia.org/wiki/Infinite_monkey_theorem
words = [choice(vocab[pos]) for pos in vocab if pos != 'P'] # without PP
words = [choice(vocab[pos]) for pos in vocab] + choice(vocab('NP')) # with a PP you need 3 NPs

# To make sure that you always generate a grammatical sentence
trees = []
while trees != []:
  shuffle(words)
  trees = parser.nbest_parse(words)

for t in trees:
  print t

rawDescription: Randomly generated Parse Tree using a fix set of vocabulary


code:
string = 'File "/home/path/to/dir/filename.py", line 888, in function_name'

search = re.search('File (.*), line (.*), in (.*)', string, re.IGNORECASE)
print search.group(1)
print search.group(2)
print search.group(3)

rawDescription: How to extract various parts of a string into separate strings

description: Extract various parts of a string into separate strings


code:
D = { 'h' : 'hh' , 'e' : 'ee'}
str = 'hello'
returns = []  # create list for storing return value from function
def myLambda(x): # function does lambda
    return D.get(x,x)

for x in str: #map==> pass iterable
    returns.append(myLambda(x)) #for each element get equivalent string from dictionary and append to list

print ''.join(returns) #join for showing result

rawDescription: How does Map work

description: How does Map work


code:
import csv
from collections import defaultdict
from datetime import datetime

posters = defaultdict(list)
with open('datetouched.csv','rU') as fin:
    csvin = csv.reader(fin)
    items = ((row[0], datetime.strptime(row[1], '%m/%d/%y')) for row in csvin)
    for uid, dt in items:
        posters[uid].append(dt)

for uid, dates in posters.iteritems():
    # print uid, list of datetime objects, and max date in same format as input
    print uid, dates, '{0.month}/{0.day}/%y'.format(max(dates))

rawDescription: Python string to date, date to string

description: Python string to date, date to string


code:
parts=[]
FirstLetter='M'
fp=open('textfile.txt')
for line in fp:
    if 'spare_parts' in line:
        part =line.rstrip().split('/')[-1].strip('.htm')
        if part.startswith(FirstLetter):
            parts.append(part)

print sorted(parts)

rawDescription: Find especific links in files

description: Find especific links in files


code:
from lxml import html
import requests

urls = ['http://whiskey.de/shop/Aktuell/']

for url in urls:
    for number in range(1, 15):
        page_url = url + str(number) if number > 1 else url
        page = requests.get(page_url)

        tree = html.fromstring(page.text)

        prices = tree.cssselect('div#content div.price')
        names = tree.cssselect('div#content div.title a')

        print 'Whiskey Preis: ', [price.text for price in prices]
        print 'Whiskey Names: ', [name.text for name in names]

rawDescription: Python Web Scraping title in a special div & Page 1 + 15


code:
class C(object):
    my_var = 'a'

    @classmethod
    def t(cls):
        print cls.my_var

C.t()

rawDescription: In Python, how do I reference a class generically in a static way, like PHP's "self" keyword?


code:
import re

html = '
Step1Carefully transfer the biscuits to a rimmed baking sheet, spacing them an inch or so apart
'

print re.sub(r'<[^>]*?>', '', html)

rawDescription: Web Scraping using re:How to neglect html tags within the text we want to extract?


code:
import subprocess
subprocess.call(["C:\Users\Public\Videos\Sample Videos\New Folder\ksp-win-0-21-1\KSP_win cosmetic mods\KSP.exe"])

rawDescription: How to open a .exe file?

description: Open a .exe file


code:
import requests

r_sanfran = requests.get("https://api.angel.co/1/tags/1664/jobs").json()
num_pages = r_sanfran['last_page']

for page in range(2, num_pages + 1):
    r_sanfran = requests.get("https://api.angel.co/1/tags/1664/jobs", params={'page': page}).json()
    print r_sanfran['page']
    # TODO: extract the data

rawDescription: Python requests arguments/dealing with api pagination


code:
with open('file1.txt') as f1:
    fd1 = {}
    for ln in f1:
        if ln.startswith('access-list'):
            vals = ln.split()
            fd1[vals[3]] = vals[2]

with open('file2.txt') as f2:
    for ln in f2:
        vals = ln.split()
        if vals[0] in fd1:
            print vals[0], fd1[vals[0]], vals[1]

rawDescription: How to compare between values of different files in python

description: Compare between values of different files in python


code:
# Make a queue with (url, filename) tuples
queue = Queue.Queue()
for url in urls:
    url = url.strip()
    if not url or url[0] == "#":
        continue
    filename = "doc_%03d.dat" % (len(queue.queue) + 1)
    queue.put((url, filename))

# Check args
assert queue.queue, "no URLs given"
num_urls = len(queue.queue)
num_conn = min(num_conn, num_urls)
assert 1 <= num_conn <= 10000, "invalid number of concurrent connections"
print "PycURL %s (compiled against 0x%x)" % (pycurl.version, pycurl.COMPILE_LIBCURL_VERSION_NUM)
print "----- Getting", num_urls, "URLs using", num_conn, "connections -----"

class WorkerThread(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue

    def run(self):
        while 1:
            try:
                url, filename = self.queue.get_nowait()
            except Queue.Empty:
                raise SystemExit
            fp = open(filename, "wb")
            curl = pycurl.Curl()
            curl.setopt(pycurl.URL, url)
            curl.setopt(pycurl.FOLLOWLOCATION, 1)
            curl.setopt(pycurl.MAXREDIRS, 5)
            curl.setopt(pycurl.CONNECTTIMEOUT, 30)
            curl.setopt(pycurl.TIMEOUT, 300)
            curl.setopt(pycurl.NOSIGNAL, 1)
            curl.setopt(pycurl.WRITEDATA, fp)
            try:
                curl.perform()
            except:
                import traceback
                traceback.print_exc(file=sys.stderr)
                sys.stderr.flush()
            curl.close()
            fp.close()
            sys.stdout.write(".")
            sys.stdout.flush()

# Start a bunch of threads
threads = []
for dummy in range(num_conn):
    t = WorkerThread(queue)
    t.start()
    threads.append(t)

# Wait for all threads to finish
for thread in threads:
    thread.join()

rawDescription: Python: multiple files download by turn

description: Python: multiple files download by turn


code:
import re

posts = ['1test', '23test', '15test']

r = re.compile('(^\d)')
print sum([float(r.match(post).group(1)) for post in posts])/len(posts)

rawDescription: python How would I divide this?

description: Python How would I divide this


code:
class base(object):
    def create_attrib_a(self):
        self.a = 1
class derived(base):
    def create_attrib_b(self):
        self.b = 1
def create_attrib_c(obj):
   obj.c = 1

import inspect

o = derived()
o.create_attrib_a()
o.create_attrib_b()
create_attrib_c(o)
o.d = 1

# The objects attributes are relatively anonymous
print o.__dict__

# But the class's methods have lots of information available
for name, value in inspect.getmembers(o, inspect.ismethod):
    print 'Method=%s, filename=%s, line number=%d'%(
        name,
        value.im_func.func_code.co_filename,
        value.im_func.func_code.co_firstlineno)

rawDescription: python how to find which parent classes define methods of a child object

description: Python how to find which parent classes define methods of a child object


code:
def f(number):
    ones    =   [
                    "",
                    "one",
                    "two",
                    "three",
                    "four",
                    "five",
                    "six",
                    "seven",
                    "eight",
                    "nine",
                ]
    teens   =   {
                    10:"ten",
                    11:"eleven",
                    12:"twelve",
                    13:"thirteen",
                    14:"fourteen",
                    15:"fifteen",
                    16:"sixteen",
                    17:"seventeen",
                    18:"eighteen",
                    19:"nineteen",
                }
    tens    =   [
                    "",
                    "teen",
                    "twenty",
                    "thirty",
                    "fourty",
                    "fifty",
                    "sizty",
                    "seventy",
                    "eighty",
                    "ninety",
                ]
    hundred =   [ "%shundred" % one for one in ones ]
    place_value   = {1:ones,2:tens,3:hundred}
    number_string = ""
    number        = list(str(number))
    number.reverse()
    number        = [(place+1, int(digit)) for place, digit in enumerate(number)]
    number.reverse()
    number_iter = number.__iter__()
    for place, digit in number_iter:
        print place, digit
        if place == 2 and digit == 1:
            place, digit = number_iter.next()
            number_string += teens[10 + digit]
        else:
            number_string += place_value[place][digit]
        print number_string

rawDescription: Short Form Number into Words Python

description: Short Form Number into Words Python


code:
import glob
for files in glob.glob("*.csv"):
    # print all files that contain only .csv
    if files.count('.') == 1:
       print files

rawDescription: read only csv files python

description: Read only csv files python


code:
import os
os.linesep
with open('test.txt', 'w') as f:
    f.write(os.linesep)
with open('test.txt', 'rb') as f:
    print repr(f.read())

rawDescription: Why you shouldn't use os.linesep when editing on text mode?


rawDescription: How to extract some numbers from a character string with comma seperating some numbers using python?

description: Extract some numbers from a character string with comma seperating some numbers using python


code:
from collections import Counter
with open('full_megalog.txt') as f:
    data = [d.split() for d in f]

sum1, sum2 = Counter(), Counter()

for d in data:
    sum1[d[0]] += int(d[1])
    sum2[d[0]] += int(d[2])

for date_ip in sum1.keys():
    print date_ip, sum1[date_ip], sum2[date_ip]

rawDescription: Python sum of the array elements

description: Python sum of the array elements


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

# The new Stream Object which replaces the default stream associated with sys.stdout
# This object just puts data in a queue!
class WriteStream(object):
    def __init__(self,queue):
        self.queue = queue

    def write(self, text):
        self.queue.put(text)

# A QObject (to be run in a QThread) which sits waiting for data to come through a Queue.Queue().
# It blocks until data is available, and one it has got something from the queue, it sends
# it to the "MainThread" by emitting a Qt Signal
class MyReceiver(QObject):
    mysignal = pyqtSignal(str)

    def __init__(self,queue,*args,**kwargs):
        QObject.__init__(self,*args,**kwargs)
        self.queue = queue

    @pyqtSlot()
    def run(self):
        while True:
            text = self.queue.get()
            self.mysignal.emit(text)

# An example QObject (to be run in a QThread) which outputs information with print
class LongRunningThing(QObject):
    @pyqtSlot()
    def run(self):
        for i in range(1000):
            print i

# An Example application QWidget containing the textedit to redirect stdout to
class MyApp(QWidget):
    def __init__(self,*args,**kwargs):
        QWidget.__init__(self,*args,**kwargs)

        self.layout = QVBoxLayout(self)
        self.textedit = QTextEdit()
        self.button = QPushButton('start long running thread')
        self.button.clicked.connect(self.start_thread)
        self.layout.addWidget(self.textedit)
        self.layout.addWidget(self.button)

    @pyqtSlot(str)
    def append_text(self,text):
        self.textedit.moveCursor(QTextCursor.End)
        self.textedit.insertPlainText( text )

    @pyqtSlot()
    def start_thread(self):
        self.thread = QThread()
        self.long_running_thing = LongRunningThing()
        self.long_running_thing.moveToThread(self.thread)
        self.thread.started.connect(self.long_running_thing.run)
        self.thread.start()

# Create Queue and redirect sys.stdout to this queue
queue = Queue()
sys.stdout = WriteStream(queue)

# Create QApplication and QWidget
qapp = QApplication(sys.argv)
app = MyApp()
app.show()

# Create thread that will listen on the other end of the queue, and send the text to the textedit in our application
thread = QThread()
my_receiver = MyReceiver(queue)
my_receiver.mysignal.connect(app.append_text)
my_receiver.moveToThread(thread)
thread.started.connect(my_receiver.run)
thread.start()

qapp.exec_()

rawDescription: Redirecting stdout and stderr to a PyQt4 QTextEdit from a secondary thread


code:
import msvcrt
while True:
    pressedKey = msvcrt.getch()
    if pressedKey == 'q':
       print "Q was pressed"
    elif pressedKey == 'x':
       sys.exit()
    else:
       print "Key Pressed:" + str(pressedKey)

rawDescription: Python Windows `msvcrt.getch()` only detects every 3rd keypress?


code:
from pkg_resources import WorkingSet , DistributionNotFound
working_set = WorkingSet()

# Printing all installed modules
print tuple(working_set)

# Detecting if module is installed
try:
    dep = working_set.require('paramiko>=1.0')
except DistributionNotFound:
    pass

# Installing it (anyone knows a better way?)
from setuptools.command.easy_install import main as install
install(['django>=1.2'])

rawDescription: Installing packages and plugins when script is run--Python?


code:
for key, value in d.items():
    print key, value

rawDescription: can we access key and value in the ordereddict in python.?

description: Can we access key and value in the ordereddict in python.


code:
ptags_with_span = content.xpath('//*[@id="ciHomeContentlhs"]/div[4]/div[2]/div[1]/p[span]')
no_of_p_tags = len(ptags_with_span)
for ptag in ptags_with_span:
    print ptag.xpath('./span/text()')

rawDescription: find number of 'p' tags in a div and iterate through them to scrape the underlying text using python xpath

description: Find number of 'p' tags in a div and iterate through them to scrape the underlying text using python xpath


code:
from sklearn.ensemble import BaseEnsemble, RandomForestClassifier

old_init = BaseEnsemble.__init__

def new_init(*args, **kwargs):
    print 'called'
    old_init(*args, **kwargs)

BaseEnsemble.__init__ = new_init

RandomForestClassifier()

rawDescription: Is it possible to completely monkey-patch a class from a library in Python?


code:
import zipfile
import io

buffer = b'PK\x03\x04\n\x00\x00\x00\x00\x00\n\\\x88Gzzo\xed\x03\x00\x00\x00\x03\x00\x00\x00\x07\x00\x1c\x00foo.txtUT\t\x00\x03$\x14gV(\x14gVux\x0b\x00\x01\x041\x04\x00\x00\x041\x04\x00\x00hi\nPK\x01\x02\x1e\x03\n\x00\x00\x00\x00\x00\n\\\x88Gzzo\xed\x03\x00\x00\x00\x03\x00\x00\x00\x07\x00\x18\x00\x00\x00\x00\x00\x01\x00\x00\x00\xb4\x81\x00\x00\x00\x00foo.txtUT\x05\x00\x03$\x14gVux\x0b\x00\x01\x041\x04\x00\x00\x041\x04\x00\x00PK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00M\x00\x00\x00D\x00\x00\x00\x00\x00'

z = zipfile.ZipFile(io.BytesIO(buffer))

# The following three lines are alternatives. Use one of them
# according to your need:
foo = z.read('foo.txt')        # Reads the data from "foo.txt"
foo2 = z.read(z.infolist()[0]) # Reads the data from the first file
z.extractall()                 # Copies foo.txt to the filesystem

z.close()

print foo
print foo2

rawDescription: Unzip buffer with Python?

description: Unzip buffer with Python


code:
a = b'12341234A12341234B12341234C12341234'
def chunkify(buf, length):
   while len(buf) > length:
      # get the next chunk from the buffer
      retval = buf[:length]
      # ...and move along to the next interesting point in the buffer.
      buf = buf[length+1:]
      yield retval
for chunk in chunkify(a, 8):
   print chunk
''.join(chunk for chunk in chunkify(a, 8))

rawDescription: Dechunking a string in python

description: Dechunking a string in python


code:
last_value = None #Set to None so it will always be different the first time.
while True:
    current_value = GPIO.input(23)
    if current_value != last_value:
        if current_value:
            GPIO.output(24,True)
            GPIO.output(25,False)
            #LOG STUFF
        else:
            GPIO.output(24,False)
            GPIO.output(25,True)
            print "button false"
    last_value = current_value
    time.sleep(0.1)

rawDescription: Python time.sleep script

description: Python time.sleep script


code:
import csv

data_heading = {'Date': [(3,2)],
            'Rate': [(3,3)],
            'Product': [(3,7)]}

rdr = csv.reader(open(csvpath, 'rb'))
columns = [{key:row[pos[0][1]] for key,pos in data_heading.items()} for row in rdr]
# finally trim to desired row startpoints:
data = {key:[col[key] for col in columns[pos[0][0]:]] for key,pos in  data_heading.items()}
print data

rawDescription: How to write a new csv by reading the data from another csv using python?

description: Write a new csv by reading the data from another csv using python


code:
for line in open("file"):
    if "|" in line:
        whatiwant_1=line.split("|")[1]
        if "[" in line:
            whatiwant_2=line.split("[")[1].split("]")[0]
print whatiwant_1 , whatiwant_2

rawDescription: How to extract a couple marked strings from a line (python)

description: Extract a couple marked strings from a line (python)


code:
if found.strftime('%I:%M:%S') == "05:29:00":
    print 'the same'
else:
    print 'not the same'

rawDescription: Comparing time object with literal string in Python


code:
s = "Some ABC text followed by XYZ followed by multiple ABC, more ABC, more ABC"

pattern = re.compile(r'(?<=XYZ)(.*?)ABC')
while pattern.search(s):
   s = pattern.sub(r'\1REPLACED', s)

print s

rawDescription: Regex for matching any occurrence of ABC following XYZ anywhere in the string


code:
concatenated = concat([df,df], keys=['first', 'second'])
print concatenated.T
print concatenated.T.to_dict().values()

rawDescription: Pandas transpose concat()

description: Pandas transpose concat()


code:
g = globals() # get a reference to the globals dict
g[raw_input("Name Please")] = raw_input("Value Please")
print foo

rawDescription: Create a new variable as named from input in Python?


code:
#Initialize a dictionary with the appropriate values
color_lookup = {
    'a': libtcod.Color(0, 176, 240),
    'b': libtcod.Color(100, 155, 200),
    'c': libtcod.Color(80, 055, 100)
}

#Then call create_room like this
create_room(x, y, color_lookup[color])

rawDescription: Reading strings from text file as variable in python


code:
def lineReceived(self, line):
   print "received", repr(line)
   for c in self.factory.clients:
       c.transport.write(message + '\n')
   for server in self.factory.otherServers:
       server.otherServerMessage(self, line)

rawDescription: Sending message from one server to another in Twisted


code:
#!/usr/bin/env python

from scrape import s
import sys, re

if len(sys.argv) < 2:
    print "Usage: words.py url"
    sys.exit(0)

s.go(sys.argv[1]) # fetch content
text = s.doc.text # extract readable text
text = re.sub("\W+", " ", text) # remove all non-word characters and repeating whitespace
print text

rawDescription: Bash/Python : open url & print top 10 words


code:
file = open("test.txt", "U")
test = []
for line in file:
   line = line.replace("\r\n","").replace("\n","")
   test.append(line)
   print test

rawDescription: List strings have "\n" at the end of them, when individually printed "\n" is gone


code:
class XYZ(object):
    def __init__(self, cameraId):
        self.cameraId = cameraId
        self.index = ""

    def get_test(self):
        print "Index: " + self.index + " CameraID: " + self.cameraId
        return self.cameraId

    def set_test(self, value):
        # Require value to be a tuple!
        assert(isinstance(value, tuple))

        self.cameraId = value[0]

        try:
            self.index = value[1]

        except IndexError:
            self.index = ""

        return True

    TEST_XYZ = property(get_test, set_test)

rawDescription: Python Property, How do I get this to work correctly


code:
for x in train['ingredients'][0]:
     print x

rawDescription: Extra "u" when converting from json to csv in python


rawDescription: accessing html parsed data in python using lists

description: Accessing html parsed data in python using lists


code:
for iteration in xrange(50):
    print "iter", iteration,
    omega = next_omega(omega)
    err = 0
    print "omega", omega,
    for x in range(1, m1):
        for y in range(1, m1):
            if (x%2+y)%2 == 0: # Only update the 'red' grid cells
                diff = 0.25 * omega * \
                    (grid[x - 1][y] + \
                     grid[x + 1][y] + \
                     grid[x][y - 1] + \
                     grid[x][y + 1] - 4.0 * grid[x][y])

                grid[x][y] = grid[x][y] + diff
                err += diff

    for x in range(1, m1):
        for y in range(1, m1):
            if (x%2+y)%2 == 1: # Only update the 'black' grid cells
                diff = 0.25 * omega * \
                    (grid[x - 1][y] + \
                     grid[x + 1][y] + \
                     grid[x][y - 1] + \
                     grid[x][y + 1] - 4.0 * grid[x][y])

                grid[x][y] = grid[x][y] + diff
                err += diff

    print err

rawDescription: SOR method does not converge

description: SOR method does not converge


code:
class Math(object):
    def __init__(self, *args, **kwargs):
        self.list = list(args)

    def __getitem__(self, i):
        return MathSlice(self, i)

class MathSlice(object):
    def __init__(self, math, slice):
        self.math = math
        self.slice = slice

    def add_one(self):
        for i in xrange(*self.slice.indices(len(self.math.list))):
            self.math.list[i] += 1

instance = Math(1,2,3,4,5)
instance[2:4].add_one()

print instance.list

rawDescription: Overload [] python operator and chaining methods using a memory reference


code:
import requests
import json

r = requests.get("http://www.nseindia.com/live_market/dynaContent/"
                 "live_analysis/gainers/niftyGainers1.json")
data_as_json = json.loads(r.content)

for stock_info in data_as_json['data']:
    for key, value in stock_info.items():
        print key, value

rawDescription: Full html code is not coming

description: Full html code is not coming


code:
lev=8
def t():
     global lev
     print 1+1
     lev = lev+1
t()
lev

rawDescription: function without args that uses elements precreated

description: Function without args that uses elements precreated


code:
from Queue import Empty, Queue
from subprocess import PIPE, Popen
from threading import Thread

def start_thread(target, *args):
    t = Thread(target=target, args=args)
    t.daemon = True
    t.start()
    return t

def signal_completion(queue, stderr):
    for line in iter(stderr.readline, ''):
        if 'Finished' in line:
           queue.put(1) # signal completion
    stderr.close()

def print_stdout(q, stdout):
    """Print stdout upon receiving a signal."""
    text = []
    for line in iter(stdout.readline, ''):
        if not q.empty():
           try: q.get_nowait()
           except Empty:
               text.append(line) # queue is empty
           else: # received completion signal
               print ''.join(text),
               text = []
               q.task_done()
        else: # buffer stdout until the task is finished
            text.append(line)
    stdout.close()
    if text: print ''.join(text), # print the rest unconditionally

queue = Queue()
proc = Popen("foo -config x.ini -threads 4".split(), bufsize=1,
             stdin=PIPE, stdout=PIPE, stderr=PIPE)
threads =  [start_thread(print_stdout, queue, proc.stdout)]
threads += [start_thread(signal_completion, queue, proc.stderr)]

mylist = ['this','is','my','test','app','.']
for line in mylist:
    proc.stdin.write(line.strip()+'\n')
proc.stdin.close()
proc.wait()
for t in threads: t.join() # wait for stdout

rawDescription: How can Python continuously fill multiple threads of subprocess?


code:
import ast

class Py2Neko(ast.NodeVisitor):
    def generic_visit(self, node):
        print type(node).__name__
        ast.NodeVisitor.generic_visit(self, node)

    def visit_Name(self, node):
        print 'Name :', node.id

    def visit_Num(self, node):
        print 'Num :', node.__dict__['n']

    def visit_Str(self, node):
        print "Str :", node.s

    def visit_Print(self, node):
        print "Print :"
        ast.NodeVisitor.generic_visit(self, node)

    def visit_Assign(self, node):
        print "Assign :"
        ast.NodeVisitor.generic_visit(self, node)

    def visit_Expr(self, node):
        print "Expr :"
        ast.NodeVisitor.generic_visit(self, node)

if __name__ == '__main__':
    node = ast.parse("a = 1 + 2")

    print ast.dump(node)

    v = Py2Neko()
    v.visit(node)

rawDescription: Visiting nodes in a syntax tree with Python ast module


code:
with open(class_name + ".csv", 'rt') as f:
  for line in f:
    print line.strip().replace(',', ' ')

rawDescription: How to stop python from printing commas from a csv file

description: Stop python from printing commas from a csv file


code:
import difflib

a='abcd'
b='ab123'
seq=difflib.SequenceMatcher(None, a,b)
d=seq.ratio()*100
print d

rawDescription: How to use SequenceMatcher to find similarity between two strings?

description: Use SequenceMatcher to find similarity between two strings


code: print"\n".join(['Test{0:04}'.format(num) for num in xrange(0, 10000)])

rawDescription: Adding text to loop in python

description: Adding text to loop in python


code:
def subfunc(L,sublist):
    sublen = len(sublist)
    for i in xrange(len(L)-sublen+1):
        if L[i:i+sublen] == sublist:
            return True
    return False

L = [0,1,2,3,4,5]
print subfunc(L, [0,1])  # --> True
print subfunc(L, [5,1])  # --> False
print subfunc(L, [4,5])  # --> True

rawDescription: Check for specific sequences in list

description: Check for specific sequences in list


code:
import Image
import glob
name = raw_input('What is the file name (excluding the extension) of your video that was converted using FreeVideoToJPGConverter?\n')
x_res = int(raw_input('What do you want the height of your image to be (in pixels)?\n'))
y_res = int(raw_input('What do you want the width of your image to be (in pixels)?\n'))
rows = int(raw_input('How many rows do you want?\n'))
columns = int(raw_input('How many columns do you want?\n'))
images = glob.glob('../' + name + '*.jpg')
new_im = Image.new('RGB', (x_res,y_res))
x_cntr = 0
y_cntr = 0
for x in xrange(0,len(images),1):
    if x%(rows*columns) == 0:
        new_im.save('Output' + str(x) + '.jpg')
        new_im = Image.new('RGB', (x_res,y_res))
        y_cntr = 0
        x_cntr = 0
        print str(round(100*(float(x)/len(images)), 1)) + "% Complete"
    elif x%rows == 0:
        x_cntr = 0
        y_cntr = y_cntr + y_res/columns
    elif x%1 == 0:
        x_cntr = x_cntr + x_res/rows
    im = Image.open(images[x])
    im = im.resize((x_res/rows + x_res%rows, y_res/columns + y_res%columns), Image.ANTIALIAS)
    new_im.paste(im, (x_cntr, y_cntr))

rawDescription: Python PIL - Joining images in an array

description: Python PIL - Joining images in an array


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

class ZeroSpinBox(QSpinBox):
    zeros = 0

    def __init__(self, parent=None):
        super(ZeroSpinBox, self).__init__(parent)
        self.valueChanged.connect(self.checkzero)

    atzero = Signal(int)

    def checkzero(self):
        if self.value() == 0:
            self.zeros += 1
            self.atzero.emit(self.zeros)

class Form(QDialog):
    def __init__(self, parent= None):
        super(Form, self).__init__(parent)

        dial = QDial()
        dial.setNotchesVisible(True)
        spinbox = ZeroSpinBox()
        spinbox.setRange(0,200)
        dial.setRange(0,200)

        layout = QHBoxLayout()
        layout.addWidget(dial)
        layout.addWidget(spinbox)
        self.setLayout(layout)

        dial.valueChanged.connect(spinbox.setValue)
        spinbox.valueChanged.connect(dial.setValue)
        spinbox.atzero.connect(self.announce)

        self.setWindowTitle("Signals and Slots Part 2")

    @Slot(int)
    def announce(self, zeros):
        print "ZeroSpinBox has been at zero %d times" % zeros

if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = Form()
    form.show()
    app.exec_()

rawDescription: PySide emit signal causes python to crash


code:
import numpy as np
A=[2, 3, 4, 2, 1, 3, 4, 1, 3, 2 ]
size=[len(A),np.max(A)]
B=np.zeros(size)
for i in range(len(A)):
    idx=A[i]-1
    B[i,idx]=1
print B

rawDescription: Numpy Matrix extension such as below:

description: Numpy Matrix extension such as below:


code:
with open('firstfile.txt') as f:
    file1 = f.read().splitlines()

with open('secondfile.txt') as f:
    file2 = f.read().splitlines()

for x in file1:
    for y in file2:
        if x == y:
            print "Found matching: " + x
            #do what you want here

rawDescription: Difference between 2 text files with uknown size


code:
class RestoreStruct(Structure):

    _fields_ = [
        ("version_bin", c_uint32),
        ("cwd", c_char*256),
        ("argc", c_uint32),
        ("argv", POINTER(POINTER(c_char))),
        ("pid", c_uint32),
        ("devices_cnt", c_uint32),
        ("dictpos", c_uint32),
        ("maskpos", c_uint32),
        ("pw_off", POINTER(c_uint64)),
        ("pw_NUM", POINTER(c_uint64)),
        ("pw_CUR", c_uint64),
        ("digests_cnt", c_uint32),
        ("digests_done", c_uint32),
        ("digests_shown", POINTER(c_uint32)),
        ("salts_cnt", c_uint32),
        ("salts_done", c_uint32),
        ("salts_shown", POINTER(c_uint*30)),
        ("ms_running", c_float)

        ]

with open("cudaHashcat.restore", "rb") as restore_file:

    status = []
    struct = RestoreStruct()
    restore_file.readinto(struct)
    rest = restore_file.read()

    print struct.version_bin
    # Print rest of variables that are not pointers

    print rest.splitlines()[0:struct.argc]    # Prints a list structure of argv values

rawDescription: Access to ctypes **argv from binary file through Python


code:
f = "2.5"
i = "9"
ci = int(i)
ci
cf = float(f)
cf
g = "dsa"
cg = float(g)
try:
  cg = float(g)
except:
  print "g is not a float"

rawDescription: Method for guessing type of data represented currently represented as strings in python


code:
for (path, dirs, files) in os.walk(PATH):
  for z in files:
    filename = z.replace(" ","_").replace("-","").replace("__","_")

    # explode and transform number
    parts = filename.split('_', 2)
    parts[1] = parts[1].rjust(2, '0')

    # rejoin the transformed parts
    print '_'.join(parts)

rawDescription: rename strings / rename files in python

description: Rename strings / rename files in python


code:
import urllib2
import time
import contextlib
def timeit():
  s = time.time()
  try:
    yield
  except urllib2.URLError:
    pass
  print 'took %.3f secs' % (time.time() - s)
timeit = contextlib.contextmanager(timeit)
with timeit():
  r = urllib2.urlopen('http://loc:8080', None, 2)
with timeit():
  r = urllib2.urlopen('http://loc:8080', None, 5)

rawDescription: python urllib2 timeout

description: Python urllib2 timeout


code:
for Z in ip_results:
    matching = 0
    for X in host_file:
        if Z in X:
            print Z, X
            matching = 1
    if matching == 0:
        print Z

rawDescription: python compare ip range to host file

description: Python compare ip range to host file


code:
import subprocess
process = subprocess.Popen(["C:\Users\Ben\Desktop\youtube-dl\youtube-dl.exe", "--max-quality", "FORMAT", "--extract-audio", "songUrl"], shell=True, stdout = subprocess.PIPE)

while process.poll() is None:
    result = process.stdout.read()
    # do some calculations for percentage done

rawDescription: python: getting a response from check_output before the command is finished

description: Python: getting a response from check_output before the command is finished


code:
directory_service = authenticate()

all_groups = []
all_members = []
page_token = None
new_page_token = None
params = {'domain': 'company.com', 'customer': 'my_customer'}

while True:
  try:
    if page_token:
      params['pageToken'] = page_token
    current_page = directory_service.groups().list(**params).execute()

    all_groups.extend(current_page['groups'])
    page_token = current_page.get('nextPageToken')
    if not page_token:
      break
  except errors.HttpError as error:
    print 'An error occurred: %s' % error
    break

for group in all_groups:
  group_key = group['email']
  new_params = {'groupKey': '%s' % group_key, 'maxResults': '500'}

  if group['directMembersCount'] == '0':
    continue
  else:
    while True:
      try:
        if new_page_token:
          new_params['pageToken'] = new_page_token
        new_current_page = directory_service.members().list(**new_params).execute()
        all_members.extend(new_current_page['members'])
        new_page_token = new_current_page.get('nextPageToken')

        if not new_page_token:
          break
      except errors.HttpError as error:
        print 'An error occurred: %s' % error
        break

  print ''
  print '%s: %s member(s)' % (group['email'], group['directMembersCount'])

  for member in all_members:
    print '    %s - %s' % (member['email'], member['role'])
  del all_members[:]

rawDescription: Google Admin API pageToken loop help python


code:
DATA = ['Hello', 'Python', 'World']
SEARCH_STRING = 'n'
print [s for s in DATA if SEARCH_STRING in s]
# Prints ['Python']

rawDescription: Python string find

description: Python string find


code: print line,

rawDescription: Replace text in file with Python

description: Replace text in file with Python


code:
import math
def str_with_err(value, error):
    digits = -int(math.floor(math.log10(error)))
    return "{0:.{2}f}({1:.0f})".format(value, error*10**digits, digits)

print str_with_err(17.969860, 5.966e-05)
# 17.96986(6)
print str_with_err(0.01202, 0.001749)
# 0.012(2)

rawDescription: Is there any Python function that will format a measurement and an uncertainty parenthetically?


code:
import arcpy

from arcpy import env
env.workspace = 'C:\Users\Testing'
env.overwriteOutput = True

inFeatures = 'ras2point.shp'
arcpy.AddField_management(inFeatures, 'X', 'DOUBLE', 8, 3)
arcpy.AddField_management(inFeatures, 'Y', 'DOUBLE', 8, 3)
with arcpy.da.UpdateCursor(inFeatures, ['SHAPE@X', 'SHAPE@Y', 'X', 'Y']) as cursor:
    for row in cursor:
        row[2] = round(row[0], 3)
        row[3] = round(row[1], 3)
        cursor.updateRow(row)

print 'FINISHED'

rawDescription: Writing lat/long geometries to a feature class field


code:
from multiprocessing import Process, Queue

import random
import time

def firstresult(func1, func2):
    queue = Queue()
    proc1 = Process(target=func1,args=(queue,))
    proc2 = Process(target=func2, args=(queue,))
    proc1.start();proc2.start()
    result = queue.get()
    proc1.terminate(); proc2.terminate()
    return result

def algo1(queue):
    time.sleep(random.uniform(0,1))
    queue.put("algo 1")

def algo2(queue):
    time.sleep(random.uniform(0,1))
    queue.put("algo 2")

print firstresult(algo1, algo2)

rawDescription: Return whichever expression returns first


rawDescription: Include '-' in regex search with \w+ . Python


code:
def parse(self, response):
    print "enter parse"
    self.password+=1
    with open("try" + str(self.password), "wb") as f:
         f.write(response.body)
    yield Request(url="http://www.heibanke.com/lesson/crawler_ex01/",
                  callback=self.parse,
                  cookies={'username':str(1), "password":str(self.password)},
                  dont_filter=True)  # HERE

rawDescription: spider won't follow link

description: Spider won't follow link


code:
f1 = lambda x: "a" + x
f2 = lambda x: x + "a"
l2 = map(f1,lst) + map(f2,lst)
print l2

rawDescription: make a total list of a returned map (python)

description: Make a total list of a returned map (python)


code:
from xml.dom.minidom import parse
from xml.dom.minidom import Node

domData = parse("XMLFile.xml")
lanes = domData.getElementsByTagName("lane")
for lane in lanes:
    #print lane id
    laneId = lane.getAttribute("id")
    print "LaneID: " + laneId

    #print vehicle id, position and speed
    vehicles = lane.getElementsByTagName("vehicle")
    for vehicle in vehicles:
        vehicleId = vehicle.getAttribute("id")
        vehiclePos = vehicle.getAttribute("pos")
        vehicleSpeed = vehicle.getAttribute("speed")
        print "\tVehicleID: " + vehicleId, "VehiclePos: " + vehiclePos, "VehicleSpeed: " + vehicleSpeed

domData.unlink()

rawDescription: Feeding Blender an XML road file

description: Feeding Blender an XML road file


code:
with open("c:\\newgood.csv", "r") as fo:
    for line in fo:
        match = re.search('first_name"(.*?)"(.*?)"', line)
        if match:
            results = match.group(2)
        else:
            results = 'None'
        print results

rawDescription: Loop order issue

description: Loop order issue


code:
def lbuild( start, inc, test ):
    rslt = [start]
    while not test(start,inc):
        start += inc
        rslt.append( start )
    return rslt

n = '22'
a = 4

nval = int(n)
print lbuild( nval, -10, lambda(x,y): (x<10 and x>=0) )
print lbuild( nval, 1, lambda(x,y): x%10 == a-1 )

rawDescription: Python - Create a group of lists using numbers


code:
from PyQt4.QtGui import QApplication, QMainWindow, QWidget, QLabel

class MyWidget(QWidget):
     def enterEvent(self, evt):
        print 'a'

if __name__ == "__main__":
    app = QApplication([])
    window = QMainWindow()
    window.resize(200, 200)
    widget1 = MyWidget(window)
    widget1.resize(100, 100)
    widget1.setStyleSheet("background-color:#FFFFFF")
    window.show()
    app.exec_()

rawDescription: How to inherit from QWidget in PyQt?

description: Inherit from QWidget in PyQt


code:
import re

for word in re.split(r'\s+', line):
    print word

rawDescription: Reading columns from text file without a clear delimiter


code:
for p in psutil.get_process_list():
  if p.cmdline[0].endswith('pythonw.exe') and p.cmdline[1] == 'myscript.py':
    print p.pid

rawDescription: Identifying python processes

description: Identifying python processes


code:
#! /usr/bin/python

import xml
import xml.sax
import StringIO

class NIHXMLparser:
  class XMLContentHandler(xml.sax.ContentHandler):
    def __init__(self):
        self.name = False
        self.content = ''
    def startElement(self, name, attrs):
        if name == 'name':
            self.name = True
    def characters(self,content):
        self.content += content
    def endElement(self,name):
        if self.name and name == 'name':
            self.name = False
            print self.content
            self.content = ''

xmlText = r'<name>D & C YELLOW NO. 10</name>'
xmlFile = StringIO.StringIO(xmlText)

XMLContentHandler=NIHXMLparser.XMLContentHandler()
xml.sax.parse(xmlFile,XMLContentHandler)

rawDescription: Python XML sax parser drops everything including &


code:
import sys

if __name__ ==  "__main__":
    if len(sys.argv) == 1:
        t = raw_input("Lets begin. Type something!!!: ")
        while t != "quit":
            print "You just typed " + str(t)
            t = raw_input("Type something: ")
            #do something
        exit()
    a = sys.argv[1]
    print "The system argument supplied is:" + str(a)
    #do something
    exit()

rawDescription: Run program from terminal with either stdinput or program input


code:
mylist = ["Mahon Point retail park", "Finglas","Blackpool Mahon", "mahon point  blanchardstown"]
for item in mylist:
    print len(item.split())

rawDescription: Python - Count number of words in a list strings


code:
x = [1,2,2,2,3,4,5,6,6,7]

from itertools import groupby
print [key for key,group in groupby(x) if len(list(group)) > 1]

rawDescription: Keep duplicates in a list in Python

description: Keep duplicates in a list in Python


code:
def listenToClient(self, c):
    while self.server_running:
        try:
            recvd_data = c.client.recv(self.size)
            print "received:", c, recvd_data
            if recvd_data == "" or recvd_data == "close\n":
                print "Client " + str(c.address) + (" disconnected...")
                return

            print recvd_data

        except socket.error, (value, message):
            if value == 35:
                continue
            else:
                print "Error:", value, message

rawDescription: Thread synchronization, Python

description: Thread synchronization, Python


code:
import urllib
from BeautifulSoup import BeautifulSoup as BS

soup = BS(urllib.urlopen("http://item.taobao.com/item.htm?id=13015989524"))

a = []
for textarea in soup.findAll("textarea"):
    textsoup = BS(textarea.text)  # parse the contents as html
    a.extend(textsoup.findAll("a", attrs={"href":"http://item.taobao.com/item.htm?id=13015989524"}))

for tag in a:
    print tag

# outputs
# <img ...
# <a href="http://item.taobao.com/item.htm?id=13015989524" title="901 ...

rawDescription: Beautiful Soup - Cannot find the tags

description: Beautiful Soup - Cannot find the tags


code:
user_input = (input("give me a phone number: "))

key_alph='abcdefghijklmnopqrstuvwxyz'

key_num= '22233344455566677778889999'

counter = len(user_input[:6])

total=user_input[:6]    #Stores the part of string which is not going to be changed ("1-800-")

while (counter>0):
    alpha = user_input[-counter]
    if alpha.isalpha():   #checks if each character in the input is a valid alphabet and not a number.
        total+=key_num[key_alph.index(alpha)]  #Appending the new characters after the "1-800-"
    else:
        total+=alpha     #This will preserve if any numeric character is encountered and keeps it same as in the input
    counter -= 1
print total

rawDescription: python convert letter to telephone number

description: Python convert letter to telephone number


rawDescription: python - most recent file in list

description: Python - most recent file in list


code:
class Foo:
    sentinel = {}
    def bar(self, arg=sentinel):
        if arg is self.sentinel:
            print "You didn't supply an argument!"
        else:
            print "The argument was", arg

f = Foo()
f.bar(123)
f.bar()
f.bar(None)
f.bar({})

rawDescription: passing self data into a recursive function

description: Passing self data into a recursive function


code:
import ConfigParser

# New instance with 'bar' and 'baz' defaulting to 'Life' and 'hard' each
config = ConfigParser.SafeConfigParser({'bar': 'Life', 'baz': 'hard'})
config.read('example.cfg')

print config.get('Section1', 'foo') # -> "Python is fun!"
config.remove_option('Section1', 'bar')
config.remove_option('Section1', 'baz')
print config.get('Section1', 'foo') # -> "Life is hard!"

rawDescription: What is the equvalent of python command-line argument 'default' when using config file?


code:
import redis

r = redis.Redis(host='localhost', port=6379, db=0)
p = r.pipeline()
for key in keys:
    p.hgetall(key)

for h in p.execute():
    print h

rawDescription: Most efficient way to get several hashes in Redis?


code:
csv_file = csv.DictReader(open(sys.argv[1], 'rU'), delimiter=':')
uniq_rows = set()
for column in csv_file:
    X = column[' IP'].split(':')[-1]
    row = X + '\t'
    uniq_rows.add(row)

for row in list(uniq_rows):
    print row

rawDescription: Parse delimited csv file using Python, output to terminal or file


code:
def foo():
    filedir = os.path.dirname(__file__)
    files = [f for f in (os.path.join(filedir, fil) for fil in os.listdir(filedir)) if os.path.isfile(f)]
    print files
    #do stuff here....

rawDescription: python: file paths no longer work with imp

description: Python: file paths no longer work with imp


rawDescription: How to correct one unicode string like "\u8bf8\u845b\u4eae" in python?

description: Correct one unicode string like "\u8bf8\u845b\u4eae" in python


code:
class A:
    @staticmethod
    def optionA():
        return "A"

    @staticmethod
    def optionB():
        return "B"

    @staticmethod
    def run(opt):
        if not hasattr(A, opt):
            raise SystemExit(1)
        else:
            f = getattr(A, opt)
            return f()

print A.run('optionA')

rawDescription: Pattern for a class to return different methods based on options?


code:
# create new dataframe for selected data
new_df = pd.DataFrame()

# placeholder for six selections (i = 1...6)
for i, df in enumerate(dataframes):
    colname = 'Run {}'.format(i+1)
    selected_data = i['Unnamed: 3'].ix[12:16]
    new_df[colname] = selected_data
    print new_df

rawDescription: How to select row and column from dataframe using pandas

description: Select row and column from dataframe using pandas


code:
import BeautifulSoup
soup = BeautifulSoup.BeautifulSoup('Fun Text')
print soup.html.body.span['title']
print soup.find('span', {"class": "thisClass"})['title']

rawDescription: Pull Tag Value using BeautifulSoup

description: Pull Tag Value using BeautifulSoup


code:
import json
f = open("file", 'r')

leafArray = []

def parseTree(obj):
    if len(obj["children"]) == 0:
        leafArray.append(obj)
    else:
        for child in obj["children"]:
            parseTree(child)

for line in f:
    global leafArray
    leafArray = []
    tree = json.loads(line.strip())
    parseTree(tree)
    #somehow walk through the tree and find leaves
    print ""
    for each in leafArray:
        print each

rawDescription: access leaves of json tree

description: Access leaves of json tree


code:
for key,value in os.environ.items():
    print key + ":"+ str(type(value.decode("utf-8")))

rawDescription: Python 2.6.6 Ubuntu Server os.environ not unicode


code:
words = ["hello", "world"]
ascii = []
for word in words:
    ascii_word = []
    for char in word:
        ascii_word.append(ord(char))
    ascii.append(ascii_word)  # Change this line

print ascii # and only print this.

rawDescription: Python: changing string values in lists into ascii values using append


code:
for item in names:
    print len(item.split())

rawDescription: python counting words in JSON

description: Python counting words in JSON


code:
from multiprocessing import Process, Queue

class Result(object):
    '''Result from start to end.'''
    def __init__(self, start, end, data):
        self.start = start
        self.end = end
        self.data = data

class Worker(Process):
    '''Joins two results into one result.'''
    def __init__(self, result_queue, pair):
        self.result_queue = result_queue
        self.pair = pair
        super(Worker, self).__init__()

    def run(self):
        left, right = self.pair
        result = Result(left.start, right.end,
                        '(%s, %s)' % (left.data, right.data))
        self.result_queue.put(result)

class WorkerManager(Process):
    '''
    Takes results from result_queue, pairs them
    and assigns workers to process them.
    Returns final result into final_queue.
    '''
    def __init__(self, result_queue, final_queue, start, end):
        self._result_queue = result_queue
        self._final_queue = final_queue
        self._start = start
        self._end = end
        self._results = []
        super(WorkerManager, self).__init__()

    def run(self):
        while True:
            result = self._result_queue.get()
            self._add_result(result)
            if self._has_final_result():
                self._final_queue.put(self._get_final_result())
                return
            pair = self._find_adjacent_pair()
            if pair:
                self._start_worker(pair)

    def _add_result(self, result):
        self._results.append(result)
        self._results.sort(key=lambda result: result.start)

    def _has_final_result(self):
        return (len(self._results) == 1
                and self._results[0].start == self._start
                and self._results[0].end == self._end)

    def _get_final_result(self):
        return self._results[0]

    def _find_adjacent_pair(self):
        for i in xrange(len(self._results) - 1):
            left, right = self._results[i], self._results[i + 1]
            if left.end == right.start:
                self._results = self._results[:i] + self._results[i + 2:]
                return left, right

    def _start_worker(self, pair):
        worker = Worker(self._result_queue, pair)
        worker.start()

if __name__ == '__main__':
    DATA = [Result(i, i + 1, str(i)) for i in xrange(6)]
    result_queue = Queue()
    final_queue = Queue()
    start = 0
    end = len(DATA)
    man = WorkerManager(result_queue, final_queue, start, end)
    man.start()
    for res in DATA:
        result_queue.put(res)
    final = final_queue.get()
    print final.start
    # 0
    print final.end
    # 6
    print final.data
    # For example:
    # (((0, 1), (2, 3)), (4, 5))

rawDescription: Implementing a special type of multiprocessing queue in Python


code:
import os

Export_Dir = os.path.dirname(Export)
Export_Name = os.path.basename(Export)

Output = Export_Dir + "\Output_" + Export_Name
print "\nOutput: " + Output + "\n"

cmd = grep_loc + " --file=" + Serial_List + " " + Export + " > " + Output
print "grep usage: \n" + cmd + "\n"
os.system(cmd)
print "Output created\n"

rawDescription: Use grep on file in Python

description: Use grep on file in Python


code:
from igraph import *
g = Nexus.get("karate")
weight = g.es.select(_source=0, _target=1)['weight']
print weight

rawDescription: Get edge weight by vertices id

description: Get edge weight by vertices id


code:
def find(x):
    for root in range(x + 1):
        for pwr in range(1, 6):
            y = pow(root, pwr)
            if y > x:
                break
            if y == x:
                return root, pwr
    return None

x = int(raw_input('Enter an integer: '))
result = find(x)
if result is None:
    print 'No combinations within parameters exist'
else:
    root, pwr = result
    print root, "**", pwr, "=", x

rawDescription: Simpler way to test a list using an incrementing value (Python 2.5.X)


code:
from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    p = Pool(5)
    print(p.map(f, [1, 2, 3]))

    print "External"

rawDescription: Python Multiprocessing: How do I keep it within the specified function?


code:
s = """|[nothing detected] www.neopets.com/
\t|status: (referer=http://www.google.com)saved 55189 bytes /fetch_d4cd213a56276ca726ddd437a1e75f1024ab7799
\t\t|file: fetch_d4cd213a56276ca726ddd437a1e75f1024ab7799: 55189 bytes
\t\t|file: decoding_367af53b4963986ecdeb9c09ce1a405b5b1ecd91: 68 bytes
\t\t|[nothing detected] (script) images.neopets.com/js/common.js?v=6
\t\t\t|status: (referer=http://www.google.com)saved 1523 bytes /fetch_8eeadcc08d4cb48c02dedf18648510b75190d1d7failure: [Errno 13] Permission denied: '/tmp/tmpsha1_8d7fb3ff1ef087c7ea2bf044dee294735a76ed4b.js'
\t\t|file: fetch_8eeadcc08d4cb48c02dedf18648510b75190d1d7: 1523 bytes""".splitlines()

class MyList(list):
    def __init__(self, parent):
        self.parent = parent
        list.__init__(self)

top = current = MyList(None)
ntab_old = 0
for line in s:
    ntab = line.count('\t') #may need to be more robust to get tabs only at beginning
    if(ntab_old > ntab):
        #go up ntab_old - ntab levels.
        for _ in range(ntab_old - ntab):
            current = current.parent
    elif(ntab_old < ntab):
        #increased indentation means we want a new list
        current.append(MyList(current))
        current = current[-1]
    current.append(line)
    ntab_old = ntab

print top

rawDescription: Python; nested '\t' parsing

description: Python; nested '\t' parsing


code:
data = {
    "Mrs Dodgson": {
        "Rosie": [1,123.8,5],
        "James": [2,122.6,7],
        "Chris": [3,120.4,12]
    },
    "Dr Clement": {
        "Hannah": [1,126.9,2.56],
        "Gill": [2,124.54,6.89],
        "Jack": [3,122.04,15.62],
        "Jamie": [4,121.09,20.91]
    }
}

to_remove = ["Jamie", "Rosie"]

# Mrs. Dodgson's class data, initially.
print data["Mrs Dodgson"]

# Now remove the student data.
for cls_data in data.values():
    for student in to_remove:
        try:
            del cls_data[student]
        except KeyError:
            pass

print data["Mrs Dodgson"]

rawDescription: Dynamically named sets or other method?

description: Dynamically named sets or other method


code:
# Here I try to make the first 10 steps.
for i in range(10):
    print [x,y]
    if [x,y] in Lred:
        Lred.remove([x,y])
        Lwhite.append([x,y])
        direct-=1
    elif [x,y] in Lwhite:
        print "white square"
        Lwhite.remove([x,y])
        Lred.append([x,y])
        direct+=1
    Move(direct)
print Lwhite
print Lred

rawDescription: How to check if a set of coordinates is in a list

description: Check if a set of coordinates is in a list


code:
import multiprocessing

class My_Class(multiprocessing.Process):
    def __init__(self, initial_value):
        super(My_Class, self).__init__()
        self.v = initial_value

    def run(self):
        print 'Hello from a class ' + str(self.v)
        return

if __name__ == '__main__':
    for x in range(100):
        process = My_Class(x)
        process.start()

rawDescription: How to I instantiate a class with an initial value?


code:
import httplib2
from BeautifulSoup import BeautifulSoup, SoupStrainer

http = httplib2.Http()
status, response = http.request('http://www.nytimes.com')

for link in BeautifulSoup(response, parseOnlyThese=SoupStrainer('a')):
    if link.has_attr('href'):
        print link['href']

rawDescription: retrieve links from web page using python and BeautifulSoup

description: Retrieve links from web page using python and BeautifulSoup


code:
master = Tk.Tk() # Open up GUI connection
master.title('Program Application')
print "VSM activated input range first (X2 must be larger than X1)"
#Declare button and respective method
button = Tk.Button(text='VSM', command=VSM, fg="red")
button.config( height = 10, width = 80 )
button.pack() #pack is needed to display the button
bluebutton = Tk.Button(text="AGM Folder",command= lambda: Folder(0), fg="blue").pack(side = LEFT)
bluebutton = Tk.Button(text="VSM Folder",command= lambda: Folder(1), fg="blue").pack(side = RIGHT)
Label(text='Correct Range for Gradient\nX2                                                                                X1').pack(side=TOP,padx=10,pady=10)

################### Use 3 frames to contains entries, buttons.
frameX2 = Frame(master)
frameX2.pack(side=LEFT, expand=1, anchor=E)
entryX2 = Entry(frameX2, width=10)
entryX2.pack(side=TOP,padx=10,pady=10)
entryX2IP = Entry(frameX2, width=10)
entryX2IP.pack(side=TOP,padx=10,pady=10)
frameButton = Frame(master)
frameButton.pack(side=LEFT)
Tk.Button(frameButton, text='Input Range OP',command= lambda: Folder(2), fg="red").pack(padx=10, pady=10)
Tk.Button(frameButton, text='Input Range IP',command= lambda: Folder(2), fg="red").pack(padx=10, pady=10)
frameX1 = Frame(master)
frameX1.pack(side=LEFT, expand=1, anchor=W)
entryX1 = Entry(frameX1,width=10)
entryX1.pack(side=TOP,padx=10,pady=10)
entryX1IP = Entry(frameX1,width=10)
entryX1IP.pack(side=TOP,padx=10,pady=10)
#####################

master.mainloop() # Continue loop till user close tab

rawDescription: Unable to acheive wanted entry textBox alignment


code:
from __future__ import with_statement
try:
    import cPickle as pickle
except ImportError:
    import pickle

class cached(object):
    """Decorator/context manager for caching function call results.
    All results are cached in one dictionary that is shared by all cached
    functions.

    To use this as a decorator:
        @cached
        def function(...):
            ...

    The results returned by a decorated function are not cleared from the
    cache until decorated_function.clear_my_cache() or cached.clear_cache()
    is called

    To use this as a context manager:

        with cached(function) as function:
            ...
            function(...)
            ...

    The function's return values will be cleared from the cache when the
    with block ends

    To clear all cached results, call the cached.clear_cache() class method
    """

    _CACHE = {}

    def __init__(self, fn):
        self._fn = fn

    def __call__(self, *args, **kwds):
        key = self._cache_key(*args, **kwds)
        function_cache = self._CACHE.setdefault(self._fn, {})
        try:
            return function_cache[key]
        except KeyError:
            function_cache[key] = result = self._fn(*args, **kwds)
            return result

    def clear_my_cache(self):
        """Clear the cache for a decorated function
        """
        try:
            del self._CACHE[self._fn]
        except KeyError:
            pass # no cached results

    def __enter__(self):
        return self

    def __exit__(self, type, value, traceback):
        self.clear_my_cache()

    def _cache_key(self, *args, **kwds):
        """Create a cache key for the given positional and keyword
        arguments. pickle.dumps() is used because there could be
        unhashable objects in the arguments, but passing them to
        pickle.dumps() will result in a string, which is always hashable.

        I used this to make the cached class as generic as possible. Depending
        on your requirements, other key generating techniques may be more
        efficient
        """
        return pickle.dumps((args, sorted(kwds.items())), pickle.HIGHEST_PROTOCOL)

    @classmethod
    def clear_cache(cls):
        """Clear everything from all functions from the cache
        """
        cls._CACHE = {}

if __name__ == '__main__':
    # used as decorator
    @cached
    def fibonacci(n):
        print "calculating fibonacci(%d)" % n
        if n == 0:
            return 0
        if n == 1:
            return 1
        return fibonacci(n - 1) + fibonacci(n - 2)

    for n in xrange(10):
        print 'fibonacci(%d) = %d' % (n, fibonacci(n))

    def lucas(n):
        print "calculating lucas(%d)" % n
        if n == 0:
            return 2
        if n == 1:
            return 1
        return lucas(n - 1) + lucas(n - 2)

    # used as context manager
    with cached(lucas) as lucas:
        for i in xrange(10):
            print 'lucas(%d) = %d' % (i, lucas(i))

    for n in xrange(9, -1, -1):
        print 'fibonacci(%d) = %d' % (n, fibonacci(n))

    cached.clear_cache()

    for n in xrange(9, -1, -1):
        print 'fibonacci(%d) = %d' % (n, fibonacci(n))

rawDescription: How to put variables on the stack/context in Python

description: Put variables on the stack/context in Python


code:
def makebold(fn):
    def wrapped():
        return "" + fn() + ""
    return wrapped

def makeitalic(fn):
    def wrapped():
        return "" + fn() + ""
    return wrapped

@makebold
@makeitalic
def hello():
    return "hello world"

print hello() ## returns "hello world"

rawDescription: How can I make a chain of function decorators in Python?

description: Make a chain of function decorators in Python


code:
from tarfile import TarFile, TarError
from StringIO import StringIO

sio = StringIO(data)
try:
    tf = TarFile(fileobj=sio)
    # process the file....
except TarError:
    print "Not a tar file"

rawDescription: How to determine if data is valid tar file without a file?

description: Determine if data is valid tar file without a file


code:
class Foo:
  def __init__(self): print "Created a Foo!"
globals()['Foo']()

rawDescription: Reflection in python

description: Reflection in python


code:
listed = []
inputs = None

while inputs != '':
    inputs = input()
    listed.append(inputs)

listed = [i for i in listed if i != '']
print listed

rawDescription: Python - I need a specific way of making multi-line input


rawDescription: Best way to return False for list of empty tuples

description: Return False for list of empty tuples


code:
import re

words_list = [word for word in keywords if re.search(word, user_input )]
print "These words have been found: %s" % str(words_list)

rawDescription: Output matching keyword from a string in python


code:
#!/usr/bin/python

from mininet.net import Mininet
from mininet.node import Controller, RemoteController
from mininet.cli import CLI
from mininet.log import setLogLevel, info

def myNet():

    #OpenDayLight controller
    ODL_CONTROLLER_IP='10.0.0.4'

    #Floodlight controller
    FL_CONTROLLER_IP='10.0.0.5'

    net = Mininet( topo=None, build=False)

    # Create nodes
    h1 = net.addHost( 'h1', mac='01:00:00:00:01:00', ip='192.168.0.1/24' )
    h2 = net.addHost( 'h2', mac='01:00:00:00:02:00', ip='192.168.0.2/24' )

    # Create switches
    s1 = net.addSwitch( 's1', listenPort=6634, mac='00:00:00:00:00:01' )
    s2 = net.addSwitch( 's2', listenPort=6634, mac='00:00:00:00:00:02' )

    print "*** Creating links"
    net.addLink(h1, s1, )
    net.addLink(h2, s2, )
    net.addLink(s1, s2, )

    # Add Controllers
    odl_ctrl = net.addController( 'c0', controller=RemoteController, ip=ODL_CONTROLLER_IP, port=6633)

    fl_ctrl = net.addController( 'c1', controller=RemoteController, ip=FL_CONTROLLER_IP, port=6633)

    net.build()

    # Connect each switch to a different controller
    s1.start( [odl_ctrl] )
    s2.start( [fl_ctrl] )

    s1.cmdPrint('ovs-vsctl show')

    CLI( net )
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    myNet()

rawDescription: how to connect different switches to different remote controllers in mininet?

description: Connect different switches to different remote controllers in mininet


code:
fin = open('E:\KGG 375 - GIS Advanced\Assignment 2 - Python\TIR043109gpxpos.txt')

for line in fin: # no need to read these into a list first
  info = line.split(',')
  blurry = float(info[1])
  print info[0],
  if blurry > 0.3:
    print ' is blurry'
  else:
    print ' is not blurry'

rawDescription: I need to write a Python stub to print names of image files and whether they are blurry or not


code:
column = 3
message = '{'+str(column)+'}'
print message.format(0,0,0,'spam')

rawDescription: print formatting in python

description: Print formatting in python


code:
file = open("user.txt", "r")
for login in file:
    login = login.split('#')
    if user == login[2] and password == login[3]:
        print 'Correct username/password'
        break
else:
    print 'Invalid username/password'

rawDescription: Read line by line from text file

description: Read line by line from text file


code:
activationWords = ['cactus', 'cacti', 'rofl']
s = "Wow, rofl I found a cactus in a cacti pile."
temp = [(s.find(i), i) for i in activationWords if i in s]
print temp
print min(temp)[1]

rawDescription: Grab the first word in a list that is found in a string. ( Python )


code:
ar = np.array([0, 1])

for _ in range(10):
    print ar
    np.random.shuffle(ar)

rawDescription: Multiple Numpy random shuffles does not add up


rawDescription: How to convert the following list of lists into list of dictionaries in Python

description: Convert the following list of lists into list of dictionaries in Python


code:
import re

start_ln = re.compile(r'\[\d+\] ppt: \(.*?\) \d+ \d+ (?P<ivar>\d+\.\d+\.\d+\.\d+)')
tstamp_ln = re.compile(r'timestamp: \d+')
state_ln = re.compile(r'states: (?P<pcount>\d+) (?P<ggroup>(\[\d+\(average \d+ \d+\)\])+)')
group_p = re.compile(r'\[(?P<st>\d+)\(average (?P<avg>\d+) \d+\)\]')

f = open('pfile', 'r')

state = 'WAIT'
llist = []
ldict = {}
cvar = None

for ln in f:
    if state == 'WAIT':
        mtch = start_ln.match(ln)
        if mtch is not None:
            cvar = mtch.groupdict()['ivar']
            ldict = {}
            state = 'LINE#1'
            continue
    elif state == 'LINE#1':
        mtch = tstamp_ln.match(ln)
        if mtch is not None:
            ldict.update(mtch.groupdict())
            state = 'LINE#2'
            continue
    elif state == 'LINE#2':
        mtch = state_ln.match(ln)
        if mtch is not None:
            groupline = mtch.groupdict()['ggroup']
            mtch2 = group_p.findall(groupline)
            ldict[cvar] = dict(mtch2)
            cvar = None
            state = 'WAIT'
        llist.append(ldict)

for i in llist:
    print i

rawDescription: Parsing a specific file using python

description: Parsing a specific file using python


code:
import zlib

a = "this string needs compressing"
a = zlib.compress(a)
print zlib.decompress(a) #outputs original contents of a

rawDescription: How to compress or compact a string in Python

description: Compress or compact a string in Python


code:
import requests

url = 'www.targetsite.com/page/i/need/'
headers = { 'Connection' : 'keep-alive',
            'Header' : 'header-value',
            'Cookie' : 'CookieFieldName=CookieValue'
          }
r = requests.get(url, headers=headers)
print r.content

rawDescription: Is there a python web browser that supports user login?


code:
import requests
from bs4 import BeautifulSoup
import re
url = "http://www.webconfs.com/domain-age.php"
domain = 'youtube.com'
r = requests.post(url, {'domains': domain, 'submit': 'submit'})
soup = BeautifulSoup(r.content)
for item in soup.find_all('a', href=re.compile('website-history')):
    print item.text

rawDescription: Calculation of age of domain by passing value in URL using POST method


code:
camAngle = 140

# The context manager closes the file automatically when you leave the block
with open(camFileLoc, 'r') as handle:
    next(handle)  # Skips the header

    for line in handle:
        # Splits the line on the whitespace and converts each string
        # into an integer. Then, you unpack it into the two variables (a tuple)
        angle, displacement = map(int, line.split())

        if angle == camAngle:
            print displacement
            break  # Exits the `for` loop
    else:
        # We never broke out of the loop, so the angle was never found
        print 'This angle is not in the file'

rawDescription: In python, how do I search for a number in a text file and print a corresponding value when it's found?


code:
file1=open("f1.txt","r")
file2=open("f2.txt","r")

list1 = file1.readlines()
list2 = file2.readlines()
for line1 in list1:
  for line2 in list2:
    if line1.strip() in line2.strip().split(' '):
      print line2

rawDescription: file comparision using python

description: File comparision using python