code:
import pygame
import sys

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

WHITE = (255,255,255)
BLACK = (0  ,0  ,0  )

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

MAIN_BUTTON = 2

KEYBOARD = True
WORDS = False

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

class Pane():

    def __init__(self, textToDisplay, rect, screen, fgColor=(0,0,0), bgColor=(255,255,255), mouseoverBgColor=(255,255,0) ):

        self.textToDisplay = textToDisplay
        self.rect = pygame.Rect(*rect)
        self.screen = screen

        # colors

        self.fgColor = fgColor
        self.bgColor = bgColor

        self.mouseoverBgColor = mouseoverBgColor

        self.currentFgColor = self.fgColor
        self.currentBgColor = self.bgColor

        self.font = pygame.font.SysFont('Palace Script MT', 25)

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

    def coordinates(self):
        return self.rect

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

    def text(self):
        return self.textToDisplay

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

    def draw(self):
        # background
        self.screen.fill(self.currentBgColor, self.rect)

        # border
        pygame.draw.rect(self.screen, self.currentFgColor, self.rect, 2)

        # text
        self.screen.blit(self.font.render(self.textToDisplay, True, self.currentFgColor), self.rect)

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

    def test_coordinates(self, posx, posy):
        return self.rect.collidepoint(posx, posy)

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

    def event_handler(self, event):

        # standard pane color
        self.currentFgColor = self.fgColor
        self.currentBgColor = self.bgColor

        # if mouse over pane change color

        if event.type == pygame.MOUSEMOTION:
            if self.rect.collidepoint(event.pos):
                self.currentBgColor = self.mouseoverBgColor

        elif event.type == pygame.MOUSEBUTTONDOWN:
            if self.rect.collidepoint(event.pos):
                self.currentBgColor = (255,0,0)

        elif event.type == pygame.MOUSEBUTTONUP:
            if self.rect.collidepoint(event.pos):
                self.currentBgColor = self.mouseoverBgColor

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

class Keyboard():

    Keys = []
    paneLocs = []
    visible = False
    #---------------------------

    def __init__(self, screen):

        self.screen = screen
        self.font = pygame.font.SysFont('Arial', 25)

        self.create()

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

    def create(self):

        letter_code = ord("A")

        for y in range(2):
            for x in range(13):
                lock = (100*x, 100*y, 100, 100)
                self.paneLocs.append(lock)
                self.Keys.append(Pane(chr(letter_code), lock, self.screen, (0,0,0), (0,255,0)))
                letter_code += 1

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

    def draw(self):
        for key in self.Keys:
            key.draw()

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

    def event_handler(self, event):

        textToReturn = None

        # let panes handle event
        for key in self.Keys:
            key.event_handler(event)

        if event.type == pygame.MOUSEBUTTONUP:
            posx, posy = event.pos
            for key in self.Keys:
                #print "debug: keyboard.event_handler", posx, posy
                if key.test_coordinates(posx, posy):
                    textToReturn = key.text()

                    print textToReturn

            # if Q was
            if textToReturn == 'Q':
                self.onKeyQ(textToReturn)

        return textToReturn

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

    def onKeyQ(self, text):

        # hide keyboard
        self.visible = False

        # send event to inform main loop that keyboard was closed
        pygame.event.post(pygame.event.Event(pygame.USEREVENT, {'code': 666, 'key':text}))

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

class Application():

    NoOfPanes = 0
    Panes = []

    paneLocs = [(583, 334, 300, 150),
                (633, 150, 200, 125),
                (633, 600, 200, 125),
                (350, 360, 200, 100),
                (925, 360, 200, 100),
                (1000, 150, 150, 100),
                (275, 150, 150, 100),
                (275, 600, 150, 100),
                (1000, 600, 150, 100),
                (75, 350, 200, 100),
                (0, 668, 200, 100)
                ]

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

    def __init__(self):

        pygame.init()
        pygame.display.set_caption('Box Test')

        self.font = pygame.font.SysFont('Arial', 25)
        self.screen = pygame.display.set_mode((1366,768), 0, 32)

        self.show_panes = True

        self.createPanes()

        self.keyboard = Keyboard(self.screen)

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

    def close(self):
        print "pygame quit"
        pygame.quit()
        sys.exit()

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

    def createPanes(self):
        self.addPane("1", WORDS)
        self.addPane("2", WORDS)
        self.addPane("3", WORDS)
        self.addPane("4", WORDS)
        self.addPane("5", WORDS)
        self.addPane("6", WORDS)
        self.addPane("7", WORDS)
        self.addPane("8", WORDS)
        self.addPane("9", WORDS)
        self.addPane("10", WORDS)
        self.addPane("Keyboard", WORDS)

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

    def addPane(self, textToDisplay, keyboardFlag):
        if (not keyboardFlag) and (self.NoOfPanes > 11):
            print("Limit Reached")
        else:
            print(int(self.NoOfPanes))

            if not keyboardFlag:
                self.Panes.append(Pane(textToDisplay, self.paneLocs[self.NoOfPanes], self.screen))
                self.NoOfPanes += 1

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

    def drawPanes(self):
        for Pane in self.Panes:
            Pane.draw()

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

    def mousePosition(self, event):
            if event.type == MAIN_BUTTON:
                self.Pos = event.pos
                return MAIN_BUTTON
            else:
                return False

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

    def event_handler(self, event):

        textToReturn = None

        # let panes handle events
        for pane in self.Panes:
            pane.event_handler(event)

        if event.type == pygame.MOUSEBUTTONUP:
            posx, posy = event.pos
            for pane in self.Panes:
                #print "debug: app.event_handler", posx, posy
                if pane.test_coordinates(posx, posy):
                    textToReturn = pane.text()

                    if textToReturn == "Keyboard":
                        self.keyboard.visible = True
                        self.show_panes = False

                    print textToReturn
        return textToReturn

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

    def run(self):

        clock = pygame.time.Clock()

        RUNNING = True
        while RUNNING:

            # --- events ---

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    RUNNING = False
                    break

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        self.keyboard.visible = not self.keyboard.visible
                        self.show_panes = not self.show_panes

                if event.type == pygame.USEREVENT:
                    print "USEREVENT:", event.code
                    # event from keyboard
                    if event.code == 666:
                        # keyboar was closed so I have to show panes
                        self.show_panes = True

                if self.show_panes:
                    self.event_handler(event)

                if self.keyboard.visible:
                    self.keyboard.event_handler(event)

            # --- draws ---

            self.screen.fill(WHITE)

            if self.show_panes:
                self.drawPanes()

            if self.keyboard.visible:
                self.keyboard.draw()

            pygame.display.update()

            # --- FPS ---

            clock.tick(25)

        self.close()

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

Application().run()

rawDescription: why is my code still displaying the old panes?

description: Why is my code still displaying the old panes


code:
#!/usr/bin/env python
"""It is a `filename -> filename.ext` filter.

   `ext` is mime-based.
"""
# Mapping of mime-types to extensions is taken form here:
# http://as3corelib.googlecode.com/svn/trunk/src/com/adobe/net/MimeTypeMap.as
mime2exts_list = [
    ["application/andrew-inset","ez"],
    ["application/atom+xml","atom"],
    ["application/mac-binhex40","hqx"],
    ["application/mac-compactpro","cpt"],
    ["application/mathml+xml","mathml"],
    ["application/msword","doc"],
    ["application/octet-stream","bin","dms","lha","lzh","exe","class","so","dll","dmg"],
    ["application/oda","oda"],
    ["application/ogg","ogg"],
    ["application/pdf","pdf"],
    ["application/postscript","ai","eps","ps"],
    ["application/rdf+xml","rdf"],
    ["application/smil","smi","smil"],
    ["application/srgs","gram"],
    ["application/srgs+xml","grxml"],
    ["application/vnd.adobe.apollo-application-installer-package+zip","air"],
    ["application/vnd.mif","mif"],
    ["application/vnd.mozilla.xul+xml","xul"],
    ["application/vnd.ms-excel","xls"],
    ["application/vnd.ms-powerpoint","ppt"],
    ["application/vnd.rn-realmedia","rm"],
    ["application/vnd.wap.wbxml","wbxml"],
    ["application/vnd.wap.wmlc","wmlc"],
    ["application/vnd.wap.wmlscriptc","wmlsc"],
    ["application/voicexml+xml","vxml"],
    ["application/x-bcpio","bcpio"],
    ["application/x-cdlink","vcd"],
    ["application/x-chess-pgn","pgn"],
    ["application/x-cpio","cpio"],
    ["application/x-csh","csh"],
    ["application/x-director","dcr","dir","dxr"],
    ["application/x-dvi","dvi"],
    ["application/x-futuresplash","spl"],
    ["application/x-gtar","gtar"],
    ["application/x-hdf","hdf"],
    ["application/x-javascript","js"],
    ["application/x-koan","skp","skd","skt","skm"],
    ["application/x-latex","latex"],
    ["application/x-netcdf","nc","cdf"],
    ["application/x-sh","sh"],
    ["application/x-shar","shar"],
    ["application/x-shockwave-flash","swf"],
    ["application/x-stuffit","sit"],
    ["application/x-sv4cpio","sv4cpio"],
    ["application/x-sv4crc","sv4crc"],
    ["application/x-tar","tar"],
    ["application/x-tcl","tcl"],
    ["application/x-tex","tex"],
    ["application/x-texinfo","texinfo","texi"],
    ["application/x-troff","t","tr","roff"],
    ["application/x-troff-man","man"],
    ["application/x-troff-me","me"],
    ["application/x-troff-ms","ms"],
    ["application/x-ustar","ustar"],
    ["application/x-wais-source","src"],
    ["application/xhtml+xml","xhtml","xht"],
    ["application/xml","xml","xsl"],
    ["application/xml-dtd","dtd"],
    ["application/xslt+xml","xslt"],
    ["application/zip","zip"],
    ["audio/basic","au","snd"],
    ["audio/midi","mid","midi","kar"],
    ["audio/mpeg","mp3","mpga","mp2"],
    ["audio/x-aiff","aif","aiff","aifc"],
    ["audio/x-mpegurl","m3u"],
    ["audio/x-pn-realaudio","ram","ra"],
    ["audio/x-wav","wav"],
    ["chemical/x-pdb","pdb"],
    ["chemical/x-xyz","xyz"],
    ["image/bmp","bmp"],
    ["image/cgm","cgm"],
    ["image/gif","gif"],
    ["image/ief","ief"],
    ["image/jpeg","jpg","jpeg","jpe"],
    ["image/png","png"],
    ["image/svg+xml","svg"],
    ["image/tiff","tiff","tif"],
    ["image/vnd.djvu","djvu","djv"],
    ["image/vnd.wap.wbmp","wbmp"],
    ["image/x-cmu-raster","ras"],
    ["image/x-icon","ico"],
    ["image/x-portable-anymap","pnm"],
    ["image/x-portable-bitmap","pbm"],
    ["image/x-portable-graymap","pgm"],
    ["image/x-portable-pixmap","ppm"],
    ["image/x-rgb","rgb"],
    ["image/x-xbitmap","xbm"],
    ["image/x-xpixmap","xpm"],
    ["image/x-xwindowdump","xwd"],
    ["model/iges","igs","iges"],
    ["model/mesh","msh","mesh","silo"],
    ["model/vrml","wrl","vrml"],
    ["text/calendar","ics","ifb"],
    ["text/css","css"],
    ["text/html","html","htm"],
    ["text/plain","txt","asc"],
    ["text/richtext","rtx"],
    ["text/rtf","rtf"],
    ["text/sgml","sgml","sgm"],
    ["text/tab-separated-values","tsv"],
    ["text/vnd.wap.wml","wml"],
    ["text/vnd.wap.wmlscript","wmls"],
    ["text/x-setext","etx"],
    ["video/mpeg","mpg","mpeg","mpe"],
    ["video/quicktime","mov","qt"],
    ["video/vnd.mpegurl","m4u","mxu"],
    ["video/x-flv","flv"],
    ["video/x-msvideo","avi"],
    ["video/x-sgi-movie","movie"],
    ["x-conference/x-cooltalk","ice"]]

#NOTE: take only the first extension
mime2ext = dict(x[:2] for x in mime2exts_list)

if __name__ == '__main__':
    import fileinput, os.path
    from subprocess import Popen, PIPE

    for filename in (line.rstrip() for line in fileinput.input()):
        output, _ = Popen(['file', '-bi', filename], stdout=PIPE).communicate()
        mime = output.split(';', 1)[0].lower().strip()
        print filename + os.path.extsep + mime2ext.get(mime, 'undefined')

rawDescription: How to add file extensions based on file type on Linux/Unix?

description: Add file extensions based on file type on Linux/Unix


code:
from cookielib import parse_ns_headers
cookies = ["NetflixCookies=true; domain=.netflix.com; path=/", "hcVisitorId=b0ada616-e472-4a89-9bd6-35c6adbdf48a; expires=Sat, 26-Nov-2016 07:30:55 GMT; domain=.help.netflix.com; path=/", "cL=1448609468009%7C144860945956982159%7C144860945964708782%7C%7C5%7C; domain=.www.netflix.com; path=/", "netflixHelpCenterLocale=en-US; domain=.help.netflix.com; path=/", "JSESSIONID=8641E508022EEE2E146073D62A320319; HttpOnly; domain=help.netflix.com; path=/", "memclid=b0ada616-e472-4a89-9bd6-35c6adbdf48a; expires=Sat, 26-Nov-2016 07:31:03 GMT; domain=.netflix.com; path=/"]
for result in parse_ns_headers(cookies):
    print result[0]

rawDescription: passing in cookie python requests

description: Passing in cookie python requests


code:
for match in re.finditer(r'(?s)((?:[^\n][\n]?)+)', DATA):
   print match.start(), match.end()

# Prints:
# 0 214
# 215 298
# 299 589

rawDescription: How match a paragraph using regex

description: How match a paragraph using regex


code:
import random, operator, numpy as np, itertools, timeit

fields = [random.random() for _ in range(3000)]
fields_string = [str(field) for field in fields]
weights = [[random.random() for _ in range(3000)] for c in range(100)]

npw = np.array(weights)
npf = np.array(fields)

num_fields = len(fields)
num_category = len(weights)

def f_original():
  winner = -1
  best = -float('inf')
  for c in range(num_category):
    score = 0
    for i in range(num_fields):
      score += float(fields_string[i]) * weights[c][i]
    if score > best:
      best = score
      winner = c

def f_original_no_string():
  winner = -1
  best = -float('inf')
  for c in range(num_category):
    score = 0
    for i in range(num_fields):
      score += fields[i] * weights[c][i]
    if score > best:
      best = score
      winner = c

def f_original_xrange():
  winner = -1
  best = -float('inf')
  for c in xrange(num_category):
    score = 0
    for i in xrange(num_fields):
      score += fields[i] * weights[c][i]
    if score > best:
      best = score
      winner = c

# Zenon  http://stackoverflow.com/a/10134298/1256624

def f_index_comprehension():
    winner = -1
    best = -float('inf')
    for c in range(num_category):
      score = sum(fields[i] * weights[c][i] for i in xrange(num_fields))
      if score > best:
        best = score
        winner = c

# steveha  http://stackoverflow.com/a/10134247/1256624

def f_comprehension():
  winner = -1
  best = -float('inf')

  for c in xrange(num_category):
    score = sum(f * w for f, w in itertools.izip(fields, weights[c]))
    if score > best:
      best = score
      winner = c

def f_schwartz_original(): # https://en.wikipedia.org/wiki/Schwartzian_transform
    tup = max(((i, sum(t[0] * t[1] for t in itertools.izip(fields, wlist))) for i, wlist in enumerate(weights)),
              key=lambda t: t[1]
             )

def f_schwartz_opt(): # https://en.wikipedia.org/wiki/Schwartzian_transform
    tup = max(((i, sum(f * w for f,w in itertools.izip(fields, wlist))) for i, wlist in enumerate(weights)),
              key=operator.itemgetter(1)
             )

def fweight(field_float_list, wlist):
    f = iter(field_float_list)
    return sum(f.next() * w for w in wlist)

def f_schwartz_iterate():
     tup = max(
         ((i, fweight(fields, wlist)) for i, wlist in enumerate(weights)),
         key=lambda t: t[1]
      )

# Nolen Royalty  http://stackoverflow.com/a/10134147/1256624

def f_numpy_mult_sum():
   np.argmax(np.sum(npf * npw, axis = 1))

# me

def f_imap():
  winner = -1
  best = -float('inf')

  for c in xrange(num_category):
    score = sum(itertools.imap(operator.mul, fields, weights[c]))
    if score > best:
      best = score
      winner = c

def f_numpy():
   np.argmax(npw.dot(npf))

for f in [f_original,
          f_index_comprehension,
          f_schwartz_iterate,
          f_original_no_string,
          f_schwartz_original,
          f_original_xrange,
          f_schwartz_opt,
          f_comprehension,
          f_imap]:
   print "%s: %.2f ms" % (f.__name__, timeit.timeit(f,number=10)/10 * 1000)
for f in [f_numpy_mult_sum, f_numpy]:
   print "%s: %.2f ms" % (f.__name__, timeit.timeit(f,number=100)/100 * 1000)

rawDescription: Python optimisations in this code?

description: Python optimisations in this code


rawDescription: Writing to a specific column of a text file in python


code:
zebra = 'Yout caretak taking care of something'
words = nltk.word_tokenize(zebra)
print " ".join([ correct(i) for i in words ])

rawDescription: Python Converting output into sentence

description: Python Converting output into sentence


code: print utm_sa.union(utm_u)

rawDescription: SQLAlchemy mapping joined tables' columns to one object


rawDescription: perl one-liner to capture multiple matches in the same line

description: Perl one-liner to capture multiple matches in the same line


rawDescription: Solve Singular Value Decomposition (SVD) in Python


code:
from Spotfire.Dxp.Application.Visuals import CrossTablePlot
from Spotfire.Dxp.Data import IndexSet
from Spotfire.Dxp.Data import RowSelection
from Spotfire.Dxp.Data import DataValueCursor
from Spotfire.Dxp.Data import DataSelection

##Get our Data Table from our graph. Data Tables hold marking relations.
##Visuals just point to a marking set you specify
##and interact with it in the UI based on their DataTable.
crossTable = markMe.As[CrossTablePlot]()
crossSource = crossTable.Data.DataTableReference

##Get a Row Count
rowCount = crossSource.RowCount

##Index Set of all our rows
allRows = IndexSet(rowCount,True)

##Empty Index Set to fill with our desired markings
rowsToMark = IndexSet(rowCount,False)

##Pick the column we're interested in examining for values.
##You can create multiple cursors to look at multiple columns.
##Specify the name of your column. Mine is called test1.
colCurs = DataValueCursor.CreateFormatted(crossSource.Columns["test1"])

##Optional: Loop through to determine average value
colTotal = 0
for row in crossSource.GetRows(allRows, colCurs):
    colTotal += int(colCurs.CurrentValue)
colAvg = colTotal/rowCount

##loop through our rows and add what we want to our index.
for row in crossSource.GetRows(allRows, colCurs):
    ##Get the index of our current row in the loop
    rowIndex = row.Index

    ##Compare values and if TRUE then we add this row to our index
    ##Instead of hard coding you can refer to a document property
    ##or any other source of data like the average of your column.
    ##Optional: Print our current value to debug
    #if int(colCurs.CurrentValue) > (2.5):
    if int(colCurs.CurrentValue) > colAvg:
        print colCurs.CurrentValue + " was added to the index! =]"
        rowsToMark.AddIndex(rowIndex)
    else:
        print colCurs.CurrentValue + " was not added to the index... =["

##Set our marking equal to our rowsToMark index
Document.ActiveMarkingSelectionReference.SetSelection(RowSelection(rowsToMark),crossSource)

rawDescription: Spotfire IronPython script: get Cross Table data


code:
pkg = "com.mavdev.focusoutfacebook"
url = "https://play.google.com/store/apps/details?id=" + pkg
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
data = opener.open(url).read()

soup=BeautifulSoup(data)

t=soup.find('span',{'class':'rating-count'})
print t.text

rawDescription: How to get text from inside a TEXT I WANT in Python

description: Get text from inside a TEXT I WANT in Python


code:
#!/usr/bin/env python
import os
import time
from threading import Thread

def follow(filename):
    with open(filename) as file:
        file.seek(0, os.SEEK_END) # goto EOF
        while True:
            for line in iter(file.readline, ''):
                yield line
            time.sleep(1)

def logtail(category, filename):
    print category
    print filename
    for line in follow(filename):
        print line,
        log_entry(category, line)

for args in Config.items('files'):
    Thread(target=logtail, args=args).start()

rawDescription: Using python how to execute this code concurrently for multiple files?


rawDescription: python tuple name same as value name

description: Python tuple name same as value name


code:
# create dummy start/end dataframe
df = pd.DataFrame({'end':pd.Series([50, 100, 120, 150]), 'start':pd.Series([30, 0, 40, 130])})
df = df[['start', 'end']]
df = df.sort('start')

df['roll_end'] = df.end.cummax()
df.roll_end = df.roll_end.shift()

df['new_start'] = df.start
overlap = df.start - df.roll_end < 0
# if start is before rolling max end time then reset start to rolling max end time
df.new_start[overlap] = df.roll_end[overlap]

# if the new start is after end, then completely overlapping
print np.sum([x for x in df.end - df.new_start if x > 0])

rawDescription: Calculate the duration of overlapping time ranges using pandas


code:
import boto
s3 = boto.connect_s3()  # establish connection
bucket = s3.get_bucket('bucket_name')  # get bucket

for key in bucket.list(prefix='key'):  # list objects at a given prefix
    file_contents = key.get_contents_as_string()  # get file contents
    print file_contents.count('\n')  # print the occurrences of the new line character to get the number of lines

rawDescription: Is it possible to loop through Amazon S3 bucket and count the number of lines in its file/key using Python?


code:
import re
a = "x**14 + x**1 + x**0"
c = [int(m.group(0)) for m in re.finditer(r'\d+', a)] #re.finditer returns an iterator
m = max(c)
d = [1 if x in c else 0  for x in xrange(m, -1, -1)]
print d,len(d)

rawDescription: Python read binary polynomial into list

description: Python read binary polynomial into list


code:
import urllib2, httplib
httplib.HTTPConnection._http_vsn = 10
httplib.HTTPConnection._http_vsn_str = 'HTTP/1.0'
print urllib2.urlopen('http://localhost/').read()

rawDescription: How to send HTTP/1.0 request via urllib2?

description: Send HTTP/1.0 request via urllib2


code:
import urllib, re

data = re.search('"([0-9.]*)"', urllib.urlopen("http://ip.jsontest.com/").read()).group(1)
print data

rawDescription: Finding network (external) IP addresses using Python


code:
from __future__ import division
from nltk.corpus import inaugural
inaugural.fileids()
for fileid in inaugural.fileids():
    avg = sum(len(sent) for sent in inaugural.sents(fileids=[fileid])) / len(inaugural.sents(fileids=[fileid]))
    print fileid, avg

rawDescription: Average sentence length for every text in corpus (python3 & nltk)


rawDescription: Find a html tag using BeautifulSoup in Python


code:
user = "Username"
prefix = user + ": "
preferredWidth = 70
wrapper = textwrap.TextWrapper(initial_indent=prefix, width=preferredWidth,
                               subsequent_indent=' '*len(prefix))
message = "LEFTLEFTLEFTLEFTLEFTLEFTLEFT RIGHTRIGHTRIGHT " * 3
print wrapper.fill(message)

rawDescription: Printing with indentation in python

description: Printing with indentation in python


code:
meat = FoodFactory.create_food("meat", energy_value=25)
print meat
print meat._energy

rawDescription: Factory method for objects with many parameters


code:
import xml.etree.ElementTree as ET

data = """
<root>
<ticket >
    <device name="device1"/>
    <detail>
      <name>customer1</name>
      <ip>11.12.13.4/32</ip>
      <blob gid="20" lid="10"/>
    </detail>
    <classification>C1</classification>
  </ticket>

  <ticket >
    <device name="device2"/>
    <detail>
      <name>customer2</name>
    </detail>
    <classification>C2</classification>
  </ticket>
</root>"""

tree = ET.fromstring(data)
for ticket in tree.findall('.//ticket'):
    name = ticket.find('.//name').text
    ip = ticket.find('.//ip')
    ip = ip.text if ip is not None else 'No ip record'
    print "name={name}, ip={ip}".format(name=name, ip=ip)

rawDescription: Python-xml: Verify child element exist for every parent instance


code:
mem={}
def f(x):
    if x == 1:   # base case
        return 20
    if not x in mem:    # if we did not calculate it before - calculate
        mem[x] = f(x-1) * (f(x-1) +1) / 2
    return mem[x]   # otherwise return it

print f(1)
print f(2)
print f(3)

rawDescription: broken memoization code

description: Broken memoization code


code:
eq = 'x^10+6x^4-5x^2+x-11'

patt = r'(?P<sign>[-+]?)(?P<coeff>\d*)(?P<x>x?)(?:\^(?P<exp>\d+))?'
rgx = re.compile(patt)

eq_parts = [m.groupdict() for m in rgx.finditer(eq)][0:-1]

for eqp in eq_parts:
    print eqp

rawDescription: What would be a good way to pull the coefficients out of a string in Python?


rawDescription: Convert series of numbers to array in Python


code:
import numpy as np
N = 11;
n = 3;
x = np.linspace(0,2,N);
pdf = np.exp(-x**2);
pdf = pdf/np.sum(pdf);

#create mask, choose random indices from x according to pdf, set chosen indices to True:
indices = np.full(x.shape, False, bool)
randices = np.random.choice(np.arange(indices.shape[0]), n, replace = False,p = pdf)
indices[randices] = True

x_rand_vals = x[randices]
x_remaining = x[~indices]

print 'original x:\t\t', x
print 'random indices:\t\t',randices
print 'random values:\t\t',x_rands
print 'remaining array:\t',x_remaining

rawDescription: remove randomly chosen elements from array

description: Remove randomly chosen elements from array


code:
def foo(*args):
    ind = args.index('D2')
    lst1 = args[:ind]
    lst2 = args[ind:]
    print lst1
    print lst2

foo('D1', 1, 2, 3, 4, 5, 'D2', 6, 7, 8, 9)

rawDescription: Subiterate over a tuple

description: Subiterate over a tuple


code:
from re import findall

mystr = """
17:25:31;http://example1.com/viewtopic.php?f=8&t=189;example1.com;127.0.0.1 2013-10-19
17:22:32;http://example2.com;example2.com;127.0.0.1 2013-10-19
20:18:28;http://example3.com/threads/example-text-in-url.27304/;example3.com;127.0.0.1  2013-10-19
"""

print findall("(http://.+?);", mystr)

rawDescription: Get links in list from string

description: Get links in list from string


code:
for a_movie in root.xpath('/movies/a_movie'):
    for elem in a_movie:
        print ElementTree.tostring(elem),

rawDescription: Use a variable for an Xpath query in Python


code:
import cv2
image = cv2.imread('foo.jpg')
print image.shape
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
print gray_image.shape
cv2.imwrite('gray.jpg', gray_image)

rawDescription: In OpenCV (Python), why am I getting 3 channel images from a grayscale image?


code: print file.truncate.__doc__

rawDescription: How to delete all content of input.txt file after instance.save()?

description: Delete all content of input.txt file after instance.save()


code:
import urllib2
import time

def report(start, size, text):
    total = time.time() - start
    print "%s reading took %d seconds, transfer rate %.2f KBPS" % (
            text, total, (size / 1024.0) / total)

start = time.time()
url = ('http://itc.conversationsnetwork.org/audio/download/'
       'ITC.SO-Episode69-2009.09.29.mp3')
f = urllib2.urlopen(url)
start = time.time()
data = f.read() # read all data in a single, blocking operation
report(start, len(data), 'All data')
f.close()

f = urllib2.urlopen(url)
start = time.time()
while True:
    chunk = f.read(4096) # read a chunk
    if not chunk:
        break
report(start, len(data), 'Chunked')
f.close()

rawDescription: Reading from sockets and download speed

description: Reading from sockets and download speed


code:
from lxml import etree

data = """<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://apple.com/itunes/importer" version="film5.0">
    <video>
        <products>
            <product>
                <territory>GB</territory>
                <cleared_for_sale>true</cleared_for_sale>
                <wholesale_price_tier>1</wholesale_price_tier>
            </product>
            <product>
                <territory>IE</territory>
                <cleared_for_sale>true</cleared_for_sale>
                <wholesale_price_tier>1</wholesale_price_tier>
            </product>
            <product>
                <territory>US</territory>
                <cleared_for_sale>true</cleared_for_sale>
                <wholesale_price_tier>1</wholesale_price_tier>
            </product>
        </products>
    </video>
</package>
"""

ns = {"x": "http://apple.com/itunes/importer"}
territory = 'IE'

root = etree.fromstring(data)
for product in root.xpath('//x:product[x:territory = "%s"]' % territory, namespaces=ns):
    print product.findtext('x:cleared_for_sale', namespaces=ns)
    print product.findtext('x:wholesale_price_tier', namespaces=ns)

rawDescription: Python lxml - find tag block ammend

description: Python lxml - find tag block ammend


code:
from xml.etree.ElementTree import XML

test_input_xml = '''
<lotsOfStuff>
   <stuff>
   1
   2
   3
   4
   </stuff>

   <stuff>
   5
   7
   8
   9
   </stuff>
</lotsOfStuff>
'''

test_input = XML(test_input_xml)
stuffs = test_input.findall("stuff")

for stuff in stuffs:
   element_text = stuff.text
   print element_text

rawDescription: Looking to pull out data between two string


code:
import tkWorld
tkWorld.drawWorld()
print "end"

rawDescription: Tkinter window to be shut and control flow to be returned automatically


code:
for even in evens:
    print even

rawDescription: Even number check without: if

description: Even number check without: if


code:
mylist = ['first line', 'second line', 'third line', 'fourth line']
preamble = 'My preamble'
postamble = 'My postable'

text = preamble + '\n' + ('\n'.join(mylist)) + '\n' + postamble

print text

rawDescription: Constructing a string with new lines from a dynamic array


code:
def generate_items(inputstring):
    stack = []
    for elem in inputstring.split('|'):
        if not elem: continue
        stack.append(elem)
        yield '|' + '|'.join(stack)

for item in generate_items(inputstring):
    print item

rawDescription: re string position list builder

description: Re string position list builder


code:
for auriga, Sculptor in zip(lista1, list):
    sistema = cmdGen.getCmd(
    cmdgen.CommunityData('public'),
    cmdgen.UdpTransportTarget(( hostname, 161)),auriga
    )
    interfaz = sistema[3][0][1]
    Sextans = cmdGen.getCmd(
    cmdgen.CommunityData('public'),
    cmdgen.UdpTransportTarget(( hostname, 161)),Sculptor
    )
    entrega = Sextans[3][0][1]
    entrega = float(entrega) * 0.1

    print "Estado interfaz " + str(interfaz),
    print "sensor " + str(entrega)

rawDescription: Print 2 values in the same line, from 2 for loops


code:
num1 = 4.87
num2 = 5.03
num3 = 5.50
num4 = 9.45
num5 = 1.29
num6 = 2.19
num7 = 2.29
tax = 0.0565
customer = 0
nextcustomer = "yes"
while nextcustomer != "no":
    amtgiven = 0
    change = 0
    quantity = 0
    foodprice = 0
    totalprice = 0
    billtax = 0
    finalbill = 0
    itemnum = 0
    print "Welcome to Virtual McDonald's!"
    print "Item:                     Meal/item:                          Price:"
    print "1                          Big Mac Meal                        4.87"
    print "2                          Quarter Pounder Meal                5.03"
    print "3                          Chicken Nuggets Meal (5 piece)      5.50"
    print "4                          ChickenNuggets Meal (10 piece)      9.45"
    print "5                          Apple Pie                           1.29"
    print "6                          Large Drink                         2.19"
    print "7                          Large Fries                         2.29"

    customer = raw_input ("Would you like to order? (If not type no)")
    while customer != "no":

        while itemnum != -1:
            itemnum = input("Enter the item you would like to purchase! ")
            if itemnum == -1:
                break
            quantity = input("How many of this item do you want? ")

            if itemnum == 1:
                foodprice = quantity * num1
                totalprice += foodprice

            elif itemnum == 2:
                foodprice = quantity * num2
                totalprice += foodprice

            elif itemnum == 3:
                foodprice = quantity * num3
                totalprice += foodprice

            elif itemnum == 4:
                foodprice = quantity * num4
                totalprice += foodprice

            elif itemnum == 5:
                foodprice = quantity * num5
                totalprice += foodprice

            elif itemnum == 6:
                foodprice = quantity * num6
                totalprice += foodprice

            elif itemnum == 7:
                foodprice = quantity * num7
                totalprice += foodprice

        billtax = totalprice * tax
        finalbill = totalprice + billtax
        print "Your total bill without tax is... ", round(totalprice,2)
        print "Your total tax is... ", round(billtax,2)
        print "Your final bill is... ", round(finalbill,2)
        amtgiven = input("How much do you want to pay with? ")
        change = amtgiven - finalbill
        print "Your change is... ", round(change,2)
        break
    nextcustomer = raw_input("Is there another customer? (yes or no) ")

rawDescription: Nested while loop with trailer values

description: Nested while loop with trailer values


code:
from PIL import Image
im = Image.open(filename)

xMin, yMin, xMax, yMax = im.getbbox()
x = (xMin + xMax) / 2

colors = []
oldColor = None
for y in xrange(yMin, yMax):
    r, g, b = im.getpixel((x, y))

    if r > 240 and g > 240 and b > 240:
        newColor = 'white'
    elif g > 150 and b > 150:
        newColor = 'blue'
    elif r > 150 and g > 150:
        newColor = 'yellow'
    else:
        newColor = 'unknown'

    if newColor != oldColor:
        if newColor != 'white':
            colors.append(newColor)
        oldColor = newColor

print colors

rawDescription: Python, arranging sequences in 2 combined arrays


code:
import json

with open('test.txt', 'r') as datafile:
    data = datafile.read()

json_str = data.split('=', 1)[1].rstrip(';\n\r ')
my_data = json.loads(json_str)

for row in my_data:
    print row

print "Minimum by second value"
print min(my_data, key=lambda x: x[1])

rawDescription: python help separating lists in a text file

description: Python help separating lists in a text file


code:
for date in date_bins:
    print "activations on %s: %s"%(date, len(date_bins[date]))

rawDescription: How to categorize and count imported data

description: Categorize and count imported data


code:
import time
import sys
print 10
sys.stdout.flush()
time.sleep(10)
print 20
sys.stdout.flush()

rawDescription: Getting progress message from a subprocess


code:
import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randn(5,5),columns=['1','2','3','4','5'])
condition = df.values > 2
print np.column_stack(np.where(condition))

rawDescription: Pandas DataFrame group by value and get column & row indexes


code:
if (usr != "None") or (usr != None):
        print usr
        print "Username already taken!"
        pause()

rawDescription: Confusing python values

description: Confusing python values


code:
import re

search_for = "This is an example string"
search_for_re = r"\b" + r"\s+".join(search_for.split()) + r"\b"
pattern = re.compile(search_for_re)
match = lambda s: pattern.match(s) is not None

s = "This is an example string"
print match(s), ":", repr(s)

s = "This is an \n example string"
print match(s), ":", repr(s)

s = "This is \n an unmatching string"
print match(s), ":", repr(s)

rawDescription: Matching a string that's arbitrarily splits over multiple lines


code: print 4

rawDescription: Return Statements in Recursion

description: Return Statements in Recursion


code:
for key, count in counts.most_common():
    print '{}: {}'.format(key, count)

# prints
# 5: pqr
# 3: abc
# 1: xyz

rawDescription: python dictionary count of unique values

description: Python dictionary count of unique values


code:
def posdivisor(n):
    for i in range(1,n+1):
        if n % i == 0:
            print(i)

someValue = input("Enter an integer: ")

result = posdivisor(someValue)
print result

rawDescription: Why isn't my code returning "None"?

description: Why isn't my code returning "None"


code:
def sayhello():
 print "Hello World!"

globals().get("sayhello")()

rawDescription: Python: How do you call a method when you only have the string name of the method?


code: print "This is a number with {1} places {0:.{1}f}".format(3.14159, 2)

rawDescription: Python string formatting on the fly

description: Python string formatting on the fly


code:
import numpy as np

def cross_diff(A, B):
    return A[:,None] - B[None,:]

vec_a = np.array([1,2,3,4])
vec_b = np.array([3,2,1])

print cross_diff(vec_a, vec_b)

rawDescription: Perform cross-difference in numpy

description: Perform cross-difference in numpy


code:
import numpy
myDtype = numpy.dtype([('name', numpy.str_), ('age', numpy.int32), ('score', numpy.float64)])
myData = numpy.empty(10, dtype=myDtype) # Create empty data sets
print myData['age'] # prints all ages

rawDescription: Python equivalent of MATLAB's dataset array


rawDescription: Two Dimensional Python Array: Sort second by First


rawDescription: Compare two strings in python

description: Compare two strings in python


code:
l = ['fig_1.png', 'fig_3.png', 'fig_4.png', 'fig_2.png']
l.sort() #this will print none as it is in place and does not return anything
print l

rawDescription: sorting python list according to given texts

description: Sorting python list according to given texts


code:
inputNumber = int(raw_input("Please enter the input number: "))
a, b, c = 1, 1, -2 * inputNumber
innerPart = ((b**2) - 4 * a * c)**0.5
N = int(max((-b + innerPart) / (2 * a), (-b - innerPart) / (2 * a)))
result, sumTillN = [i for i in range(1, N + 1)], (N * (N + 1)) // 2
if inputNumber - sumTillN: result.append(inputNumber - sumTillN)
print result

rawDescription: How to generate this list from user entered number using python?

description: Generate this list from user entered number using python


code:
p = 2
sifter = (i*p for i in count(p))
for x in range(3):
   print next(sifter)
# now lets see what happens when we change p
p = 3
for x in range(3):
    print next(sifter)

rawDescription: Merged iterators produce obscure results


code:
class Foo(object):
    def __init__(self, name):
        self.name = name
    @staticmethod
    def bar(argument):
        print argument
foo = Foo('foo')
foo.bar(1)

rawDescription: Making a method a @staticmethod for documentation purposes?


code: print '\n'.join(line.split(',')[0] for line in entireResult.split('\n'))

rawDescription: Can I write this as a two-level list comprehension?

description: Write this as a two-level list comprehension


code:
class SecretYamlObject(yaml.YAMLObject):
     hidden_fields = []
     @classmethod
     def to_yaml(cls,dumper,data):
         new_data = deepcopy(data)
         for item in cls.hidden_fields:
             del new_data.__dict__[item]
         return dumper.represent_yaml_object(cls.yaml_tag, new_data, cls,
                                            flow_style=cls.yaml_flow_style)

class Trivial(SecretYamlObject):
     hidden_fields = ["_ignore"]
     yaml_tag = u'!Trivial'
     def __init__(self):
        self.a = 1
        self.b = 2
        self._ignore = 3

print yaml.dump(Trivial())

rawDescription: How can I ignore a member when serializing an object with PyYAML?

description: Ignore a member when serializing an object with PyYAML


code:
file1_list = []
with open(in_file1, "r") as names:
    for line in names:
        line_items = line.split()
        for item in line_items:
            file1_list.append(item)

matches = []
with open(in_file2, "r") as symbols:
    for line in symbols:
        file2_items = line.split()
        for file2_item in file2_items:
            for file1_item in file1_list:
                if file2_item.startswith(file1_item):
                    matches.append(file2_item)
                    print file2_item
print matches

rawDescription: Matching in Python lists when there are extra characters


rawDescription: Extract fields from the string in python


code:
import a_module
print a_module.__file__

rawDescription: Retrieving python module path

description: Retrieving python module path


code:
import re

def parse(data):
    lines = data.split('\n')  # get the lines by splitting on the newline char
    lines = [line[len("Tue Feb 24 17:51:10.835 SRV02    NOTICE  "):]  for  line in lines]  # remove the number of characters equal to the logging info
    out = []
    header = ''
    for line in lines:
        if line.startswith('   '):
            if line.strip().startswith('hist'):
                out.append(header + ";" + extract_hist_data(line))  # outsource the specific extracting to a function for ease of readability
        else:                      # header/samples line
            if all(i in line for i in ("samples", "avg", "min", "max")):  # if the line contains all these keywords
                out.append(header + ";" + extract_stat_data(line))  # outsource the specific extracting to a function for ease of readability
            else:  # Treat as a header
                header = line
    return '\n'.join(out)

def extract_hist_data(line):
    data = re.findall(r'(hist\[\s*?\d+\]).*?(\d+\.\d+).*?(\d+)',line)
    if len(data) > 0:
        data = data[0]
    else:
        return ""
    return ';'.join(i for i in data)

def extract_stat_data(line):
    data = re.findall(r'(samples).*?(\d+).*?(avg).*?(\d+\.\d+).*?(min).*?(\d+\.\d+).*?(max).*?(\d+\.\d+)',line)
    if len(data) > 0:
        data = data[0]
    else:
        return ""
    return ';'.join(i for i in data)

def parse_log_file(log_file_path):
    with open(log_file_path,'r') as f:
        content = ''.join(i for i in f)
    return parse(content)

print parse_log_file('test.log')

rawDescription: Extract parts of log in Python to import to Excel


code: print formattedList

rawDescription: Format all elements of a list

description: Format all elements of a list


code:
import re

fullstr = "my|name|is|will"
print re.search(r'\|([a-z]+)\|', fullstr).group(1)

rawDescription: Getting Substring in Python

description: Getting Substring in Python


code:
import re

file_names = ["fgmask_frame_20.jpg", "fgmask_frame_40.jpg", "fgmask_frame_2000.jpg"]
file_names = [re.sub(r'(.*?_)(\d+)(\.jpg)', lambda x: '{}{:04}{}'.format(x.group(1), int(x.group(2)), x.group(3)), file_name) for file_name in file_names]
print file_names

rawDescription: python regular expression: script to change "frame_20" to "frame_0020"

description: Python regular expression: script to change "frame_20" to "frame_0020"


code:
if len(stories) >= 2:
    print stories[1]['title']

rawDescription: Python json get specific story and image


rawDescription: Learning loops and lists

description: Learning loops and lists


code:
from ctypes import *
from cv2 import cv

# ctypes IplImage
class cIplImage(Structure):
    _fields_ = [("nSize", c_int),
                ("ID", c_int),
                ("nChannels", c_int),
                ("alphaChannel", c_int),
                ("depth", c_int),
                ("colorModel", c_char * 4),
                ("channelSeq", c_char * 4),
                ("dataOrder", c_int),
                ("origin", c_int),
                ("align", c_int),
                ("width", c_int),
                ("height", c_int),
                ("roi", c_void_p),
                ("maskROI", c_void_p),
                ("imageID", c_void_p),
                ("tileInfo", c_void_p),
                ("imageSize", c_int),
                ("imageData", POINTER(c_char)),
                ("widthStep", c_int),
                ("BorderMode", c_int * 4),
                ("BorderConst", c_int * 4),
                ("imageDataOrigin", c_char_p)]

# load DLL containing needed OpenCV functions
libr = cdll.LoadLibrary("OpenCV_test_DLL.dll")
cvSetData = libr.aux_cvSetData
cvCreateImageHeader = libr.aux_cvCreateImageHeader
cvCvtColor = libr.aux_cvCvtColor
cvCopy = libr.aux_cvCopy
cvReleaseImage = libr.aux_cvReleaseImage
cvReleaseImageHeader = libr.aux_cvReleaseImageHeader
# set return types for library functions
cvSetData.restype = None
cvCreateImageHeader.restype = POINTER(cIplImage)
cvCvtColor.restype = POINTER(cIplImage)
cvCopy.restype = None
cvReleaseImage.restype = None
cvReleaseImageHeader.restype = None
#print "auxlib loaded"

# convert Python PIL to ctypes Ipl
def PIL2Ipl(pil_img):
    """Converts a PIL image to the OpenCV/IplImage data format.

    Supported input image formats are:
        RGB
        L
        F
    """

    # mode dictionary:
    # (pil_mode : (ipl_depth, ipl_channels)
    mode_list = {
        "RGB" : (cv.IPL_DEPTH_8U, 3),
        "L"   : (cv.IPL_DEPTH_8U, 1),
        "F"   : (cv.IPL_DEPTH_32F, 1)
        }

    if not mode_list.has_key(pil_img.mode):
        raise ValueError, 'unknown or unsupported input mode'

    depth = c_int(mode_list[pil_img.mode][0])
    channels = c_int(mode_list[pil_img.mode][1])
    height = c_int(pil_img.size[1])
    width = c_int(pil_img.size[0])
    data = pil_img.tostring()

    ipl_img = cvCreateImageHeader(width, height, depth, channels);
    cvSetData(ipl_img, create_string_buffer(data,len(data)), c_int(width.value * channels.value))
    brg_img = cvCvtColor(ipl_img,cv.CV_RGB2BGR)
    cvReleaseImageHeader(byref(ipl_img))
    return brg_img

rawDescription: Pass python IplImage object as simple struct to shared C library using ctypes


code:
line = 'date time info junk1 junk2 name'
parts = line.split()
parts_I_want = parts[0:3] + parts[5:6]
print ' '.join(parts_I_want)

rawDescription: Only writing to a file certain columns of a line (seperated by spaces)


code:
def main():
    pipeline = [process_raw_text, tag_text, bio_tag, restructure]
    print reduce(apply, pipeline, txt_file)

rawDescription: Structuring Python Code for Data Analysis


code:
from bs4 import BeautifulSoup

data = """<?xml version="1.0" encoding="UTF-8"?>
<note>
    <to> Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>
"""

soup = BeautifulSoup(data, 'xml')
print [tag.name for tag in soup.find_all()]

rawDescription: List of distinct XML element names using BeautifulSoup


code:
List = [A(i) for i in range(5)]
print List

rawDescription: Fastest and pythonic way of deletion of objects in this code


code:
for domain, keys in urls.items():
    print domain
    for key, values in keys.items():
        for value in values:
            print "%s=%s" % (key, value)

        if len(values) > 1:
            print variance(map(len, values))

rawDescription: Determing variance in length

description: Determing variance in length


code:
class MyDescriptor(object):

    def __get__(self, obj, type=None):
        return self._val

    def __set__(self, obj, val):
        self._val = val

    def describe(self):
        return 'inside describe'

class MyClass(object):

    a = MyDescriptor()

    def __init__(self):
        self.a = 42

    def dump(self):
        print 'Inside dump()'
        print self.a.describe()

    def dump2(self):
        print 'Inside dump2()'
        print type(self).__dict__['a'].describe()

MyClass().dump2()
print '-'*20
MyClass().dump()

rawDescription: In Python, how can I call an instance method on a descriptor?


code:
soup = BeautifulSoup(page)
print soup

rawDescription: problems scraping web page using python

description: Problems scraping web page using python


code:
from threading import Lock

class UserBalancedJobQueue(object):

    def __init__(self):
        self._user_jobs = {}
        self._user_list = []
        self._user_index = 0
        self._lock = Lock()

    def pop_user_job(self):
        with self._lock:
            if not self._user_jobs:
                raise ValueError("No jobs to run")

            if self._user_index >= len(self._user_list):
                self._user_index = 0
            user = self._user_list[self._user_index]

            jobs = self._user_jobs[user]
            job = jobs.pop(0)

            if not jobs:
                self._delete_current_user()

            self._user_index += 1
            return user, job

    def _delete_current_user(self):
        user = self._user_list.pop(self._user_index)
        del self._user_jobs[user]

    def add_user_job(self, user, job):
        with self._lock:
            if user not in self._user_jobs:
                self._user_list.append(user)
                self._user_jobs[user] = []
            self._user_jobs[user].append(job)

if __name__ == "__main__":
    q = UserBalancedJobQueue()
    q.add_user_job("tom", "job1")
    q.add_user_job("tom", "job2")
    q.add_user_job("tom", "job3")
    q.add_user_job("fred", "job4")
    q.add_user_job("fred", "job5")

    for i in xrange(3):
        print q.pop_user_job()

    print "Adding more jobs"
    q.add_user_job("dave", "job6")
    q.add_user_job("dave", "job7")
    q.add_user_job("dave", "job8")
    q.add_user_job("dave", "job9")

    try:
        while True:
            print q.pop_user_job()
    except ValueError:
        pass

rawDescription: Creating a thread-safe queue balancer

description: Creating a thread-safe queue balancer


code:
import collections

def sliding_windows(it):
  fifo = collections.deque()
  # prime the FIFO with the first 10
  for i, line in enumerate(it):
    fifo.append(line)
    if i == 9: break
  # keep yielding 11-line sliding-windows
  for line in it:
    fifo.append(line)
    yield fifo, 5
    fifo.popleft()

for w, i in sliding_windows(open(filename)):
  if "foo" in w[i]:
    for line in w: print line,

rawDescription: `cat filename | grep -B 5 -C 5 foo`

description: `cat filename | grep -B 5 -C 5 foo`


code:
dem0 = 1000
dem = [[[dem0 for t in range(e)] for c in range(r)] for i in range(pdd)]
print dem

rawDescription: how i Can create this matrix with 3 for?

description: How i Can create this matrix with 3 for


code:
f =open('Image Data.txt','r')

R=[]
G=[]
B=[]
for line in f:
    for color_set in line.split():
        r,g,b = color_set.split(',')
        R+=[r]
        G+=[g]
        B+=[b]

print B

rawDescription: Efficient way to split R, G and B values from a file containing RGB values (Without NumPy)


code:
import re

def chunks(l, n):
    for i in range(0, len(l), n):
        # print i
        chunk = l[i:i+n]
        # print chunk
        yield l[i:i+n]

def translate(rna):
    amino_acids = {"UUU":"F", "UUC":"F", "UUA":"L", "UUG":"L",
        "UCU":"S", "UCC":"s", "UCA":"S", "UCG":"S",
        "UAU":"Y", "UAC":"Y", "UAA":"STOP", "UAG":"STOP",
        "UGU":"C", "UGC":"C", "UGA":"STOP", "UGG":"W",
        "CUU":"L", "CUC":"L", "CUA":"L", "CUG":"L",
        "CCU":"P", "CCC":"P", "CCA":"P", "CCG":"P",
        "CAU":"H", "CAC":"H", "CAA":"Q", "CAG":"Q",
        "CGU":"R", "CGC":"R", "CGA":"R", "CGG":"R",
        "AUU":"I", "AUC":"I", "AUA":"I", "AUG":"M",
        "ACU":"T", "ACC":"T", "ACA":"T", "ACG":"T",
        "AAU":"N", "AAC":"N", "AAA":"K", "AAG":"K",
        "AGU":"S", "AGC":"S", "AGA":"R", "AGG":"R",
        "GUU":"V", "GUC":"V", "GUA":"V", "GUG":"V",
        "GCU":"A", "GCC":"A", "GCA":"A", "GCG":"A",
        "GAU":"D", "GAC":"D", "GAA":"E", "GAG":"E",
        "GGU":"G", "GGC":"G", "GGA":"G", "GGG":"G",}
    translated = "".join(amino_acids.get(i, '!') for i in chunks("".join(rna), 3))
    return translated

def validation_check():
    input_rna = raw_input("Type RNA sequence: ").upper()
    if re.match(r"^[AUGCT]+$", input_rna):
        print("Correct! That is a valid sequence.")
        print translate(input_rna)
    else:
        print("That is not a valid RNA sequence, please try again.")
        validation_check()

# in case you ever need to import this, don't always call validation_check
if __name__ == "__main__":
     validation_check()

rawDescription: RNA to PROTEIN program questions

description: RNA to PROTEIN program questions


code:
import sys

class Node(object):
  def __init__(self, title, indent):
    self.title = title
    self.indent = indent
    self.children = []
    self.notes = []
    self.parent = None
  def __repr__(self):
    return 'Node(%s, %s, %r, %s)' % (
        self.indent, self.parent, self.title, self.notes)
  def aspython(self):
    result = dict(title=self.title, children=topython(self.children))
    if self.notes:
      result['notes'] = self.notes
    return result

def print_tree(node):
  print ' ' * node.indent, node.title
  for subnode in node.children:
    print_tree(subnode)
  for note in node.notes:
    print ' ' * node.indent, 'Note:', note

def topython(nodelist):
  return [node.aspython() for node in nodelist]

def lines_to_tree(lines):
  nodes = []
  for line in lines:
    indent = len(line) - len(line.lstrip())
    marker, body = line.strip().split(None, 1)
    if marker == '*':
      nodes.append(Node(body, indent))
    elif marker == '-':
      nodes[-1].notes.append(body)
    else:
      print>>sys.stderr, "Invalid marker %r" % marker

  tree = Node('', -1)
  curr = tree
  for node in nodes:
    while node.indent <= curr.indent:
      curr = curr.parent
    node.parent = curr
    curr.children.append(node)
    curr = node

  return tree

data = """\
* 1
 * 1.1
 * 1.2
  - Note for 1.2
* 2
* 3
- Note for root
""".splitlines()

def main():
  tree = lines_to_tree(data)
  print_tree(tree)
  print
  alist = topython(tree.children)
  print alist

if __name__ == '__main__':
  main()

rawDescription: How can I parse marked up text for further processing?

description: Parse marked up text for further processing


rawDescription: Can pandas.DataFrame have list type column?


code:
import timeit
mem={}
print "Execution time: %s" % timeit.Timer("len(str(f(20)))",
                            setup = "from __main__ import f").timeit(1)

rawDescription: broken memoization code

description: Broken memoization code


code:
import os

def search(path,filename):
    global found
    folders = []
    retval = []
    r.retval = retval #<--- Line added

    try:
        for item in os.listdir(path):
            if not os.path.isfile(os.path.join(path, item)):
                folders.append(os.path.join(path, item))
            else:
                if item == filename:
                    found += 1
                    retval.append(os.path.join(path, item))
    except WindowsError,e:
        print str(e)[10:]

    for folder in folders:
        retval += search(folder,filename)
        r.retval = retval   #<---- Line added
    return retval

found = 0
path = 'C:\\'
filename = 'test.txt'
print search(path,filename)

rawDescription: Python - Print a value without intterupting a loop/function


code:
def foo():
    print 'bar'

foo()

rawDescription: Pythonian structure?

description: Pythonian structure


code:
import json

links = [("Tom","Dick"),("Dick","Harry"),("Tom","Larry"),("Bob","Leroy"),("Bob","Earl")]
parents, children = zip(*links)
root_nodes = {x for x in parents if x not in children}
for node in root_nodes:
    links.append(('Root', node))

def get_nodes(node):
    d = {}
    d['name'] = node
    children = get_children(node)
    if children:
        d['children'] = [get_nodes(child) for child in children]
    return d

def get_children(node):
    return [x[1] for x in links if x[0] == node]

tree = get_nodes('Root')
print json.dumps(tree, indent=4)

rawDescription: recursively build hierarchical JSON tree in python

description: Recursively build hierarchical JSON tree in python


code:
def long_substr(data):
    substr = ''
    if len(data) > 1 and len(data[0]) > 0:
        for i in range(len(data[0])):
            for j in range(len(data[0])-i+1):
                if j > len(substr) and is_substr(data[0][i:i+j], data):
                    substr = data[0][i:i+j]
    return substr

def is_substr(find, data):
    if len(data) < 1 and len(find) < 1:
        return False
    for i in range(len(data)):
        if find not in data[i]:
            return False
    return True

print long_substr(['Oh, hello, my friend.',
                   'I prefer Jelly Belly beans.',
                   'When hell freezes over!'])

rawDescription: Longest common substring from more than two strings - Python


code:
import codecs, re, time
from itertools import chain

import numpy as np

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

trainfile = 'train.txt'

# Vectorizing data.
train = []
word_vectorizer = CountVectorizer(analyzer='word')
trainset = word_vectorizer.fit_transform(codecs.open(trainfile,'r','utf8'))
tags = ['bs','pt','bs','pt']

# Training NB
mnb = MultinomialNB()
mnb.fit(trainset, tags)

print mnb.classes_
print mnb.coef_[0]
print mnb.coef_[1]

rawDescription: How to get most informative features for scikit-learn classifier for different class?

description: Get most informative features for scikit-learn classifier for different class


code:
#!/usr/bin/env python

import time
import smtplib
import threading

if True:
    t = threading.Timer(300, lambda: send_email('Tank temperature Overheat',tank_temp))
    t.start()
print "rest of the script keeps running"
print "keeps running the scrpit and after 300s the email is sent"

rawDescription: Python, threading.timer object will not run function timer?


code:
import numpy as np
import pandas as pd

trains     = [ 1,  1,  1,  2,  2,  2]
thresholds = [10, 20, 30, 10, 20, 30]
data       = [ 1,  0,  1,  0,  1,  0]
df = pd.DataFrame({
    'trains'     : trains,
    'thresholds' : thresholds,
    'C1'         : data,
    'C2'         : data
}).set_index(['trains', 'thresholds'])

print df
df.ix[(2, 30), 0] = 3 # using column index
# or...
df.ix[(2, 30), 'C1'] = 3 # using column name
df.loc[(2, 30), 'C1'] = 3 # using column name
# but not...
df.loc[(2, 30), 1] = 3 # creates a new column
print df

rawDescription: Indexing on DataFrame with MultiIndex

description: Indexing on DataFrame with MultiIndex


code:
def find_where(iterable, dct):
    for item in iterable:
        if all(item[key] == value for key, value in dct.items()):
            yield item

my_list = [
  {'name': 'Thor', 'age': 23},
  {'name': 'Odin', 'age': 42},
  {'name': 'Freya', 'age': 50},
  {'name': 'Skadi', 'age': 23},
]

print list(find_where(my_list, {'age': 23}))

rawDescription: emulating the behavior of findWhere() in python

description: Emulating the behavior of findWhere() in python


code:
from M2Crypto import httpslib, m2urllib2, m2, SSL, Engine
import urllib2

userPin = "123456"
rootCertPath = 'd:/path/to/rootCert.pem'
url = 'https://some.domain.com/index.asp?site=SomeSite'

e = Engine.load_dynamic_engine("pkcs11", "c:/windows/syswow64/engine_pkcs11.dll")
pk = Engine.Engine("pkcs11")
pk.ctrl_cmd_string("MODULE_PATH", "c:/windows/syswow64/opensc-pkcs11.dll")
if len(userPin) > 0: pk.ctrl_cmd_string("PIN", userPin)

m2.engine_init(m2.engine_by_id("pkcs11"))

rootcert = X509.load_cert(rootCertPath)
cert = e.load_certificate("slot_01-id_01")
privatekey = e.load_private_key("slot_01-id_01")

ctx = SSL.Context("sslv23")
ctx.set_cipher_list("HIGH:!aNULL:!eNULL:@STRENGTH")
ctx.set_session_id_ctx("foobar")
ctx.load_verify_locations(cafile=rootcert)
m2.ssl_ctx_use_x509(ctx.ctx, cert.x509)
m2.ssl_ctx_use_pkey_privkey(ctx.ctx, privatekey.pkey)

proxy_support=urllib2.ProxyHandler({'https':'https://proxy:3128'})
opener = m2urllib2.build_opener(ctx, proxy_support)
m2urllib2.install_opener(opener)
req = m2urllib2.Request(url)
try:
    res = m2urllib2.urlopen(req)
    print '\nsuccess'
except urllib2.HTTPError, err:
    print '\nerror'
    print 'err.code: '+str(err.code)
    print 'err.reason: '+str(err.reason)
    print 'err.read(): '+str(err.read())

rawDescription: Error for M2Crypto https get thru a web proxy with cac card authentication


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

PLT_PAGE_NUM = 39 # default is 39
SUB_PLT_NUM = 20 # default is 20
TIME_LENGTH = 1000

xcor_real_arr = np.random.random((SUB_PLT_NUM*PLT_PAGE_NUM,TIME_LENGTH))
xcor_imag_arr = np.random.random((SUB_PLT_NUM*PLT_PAGE_NUM,TIME_LENGTH))
plt.figure(figsize=(20,12))

ax = {}
line1 = {}
line2 = {}

for pp in xrange(0,PLT_PAGE_NUM):
    start_time = time.time()
    for kk in xrange(0,SUB_PLT_NUM):
        if pp == 0:
            ax[kk] = plt.subplot(5,4,kk+1)
            line1[kk], line2[kk] = ax[kk].plot(np.arange(0,TIME_LENGTH),
                                   xcor_real_arr[SUB_PLT_NUM*pp+kk,0:],'r-',
                                   range(0,TIME_LENGTH),
                                   xcor_imag_arr[SUB_PLT_NUM*pp+kk,0:],'b-')
        else:
            line1[kk].set_ydata(xcor_real_arr[SUB_PLT_NUM*pp+kk,0:])
            line2[kk].set_ydata(xcor_imag_arr[SUB_PLT_NUM*pp+kk,0:])
        plt.title('XCOR of '+ str(SUB_PLT_NUM*pp+kk), color='k')

    plt.savefig('test_imag_real'+str(pp)+'.png',format='png',dpi=100)
    print 'Fig-'+str(pp)+' has been saved'
    print "Excution time:", time.time()-start_time

rawDescription: How to speed up matplotlib when plotting and saving lots of figures?

description: Speed up matplotlib when plotting and saving lots of figures


code:
#groupSeries = df["date_time"].apply(lambda x: pd.to_datetime(x).date() ) #if date_time column isn't already datetime
groupSeries = df["date_time"].apply(lambda x: x.date() ) #if date_time column is datetime obj

grouped =  df.groupby(groupSeries)
for k,g in grouped:
    print k,"\n",g,"\n"*2

rawDescription: Extract daily range from time series using Pandas


rawDescription: Plane fitting to 4 (or more) XYZ points

description: Plane fitting to 4 (or more) XYZ points


code:
from StringIO import StringIO
test = "a,1,2\nb,3,4"
a = np.genfromtxt(StringIO(test), delimiter=",", dtype=None)
print a

rawDescription: How to use numpy.genfromtxt when first column is string and the remaining columns are numbers?

description: Use numpy.genfromtxt when first column is string and the remaining columns are numbers


code:
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--l1', type=str, help="two numbers separated by a hyphen")
parser.add_argument('--l2', type=str)
args = parser.parse_args()
l1_list_range = xrange(*args.l1.split('-')) # xrange(1,50)
for i in l1_list_range:
    print i

rawDescription: Passing integer lists to python

description: Passing integer lists to python


code:
ts1 = TestStep(tag="start", testStepNumber=0)
ts2 = TestStep(tag="do-something-cool")
ts3 = TestStep(tag="do-something-groovy")
ts4 = TestStep(tag="do-nothing")
ts5 = TestStep(tag="finish", testStepNumber=9)

st1 = SubTest(tag="sub-test-01", testSteps=[ts5, ts2, ts3, ts4, ts1])
st2 = SubTest(tag="sub-test-02", testSteps=[ts5, ts3, ts1])
st3 = SubTest(tag="sub-test-03-nosteps")
session.add_all([ts1, ts2, ts3, ts4, ts5, st1, st2, st3])
session.commit()
session.expunge_all()

# query test
print '-' * 80
#engine.echo = False
for _st in session.query(SubTest).all():
    print "SubTest:", _st.tag
    for _ts in _st.testSteps:
        print "  ", _ts.testStepNumber, _ts.tag

rawDescription: Multiple foreign keys in one column referencing the same table


code: print equips.query.as_sql()

rawDescription: Django model with filterable attributes

description: Django model with filterable attributes


code:
results = [(idx, word) for (idx, word) in enumerate(sentence) if word.endswith("GG")]
print results

rawDescription: Searching for Unicode characters in Python


code:
input_file = open('mynewfile.txt', 'r')
with input_file as myFile:
    foundA = False
    for num, line in enumerate(myFile):
        if strA in line:
            foundA = True
            print num

        # Only look for `strB` if `strA` has been seen first
        if foundA and strB in line:
            print num
            break  # stop reading the file.

rawDescription: reading a common string in python

description: Reading a common string in python


code:
from collections import deque

def read_logfile(master_log, linecount=1):
    lastmatches = deque(maxlen=linecount)
    for line in master_log:
        if '[76:Health]:' in line:
            lastmatches.append(line)
    for line in lastmatches:
        print line

rawDescription: python - print last matching line in file

description: Python - print last matching line in file


code:
def create_list(*args):
    return [a for a in args if a is not None]

print create_list(1, 4, None, 'a', None, 'b')

rawDescription: Python: Create list from variables as long as they are not None


code:
for line in sys.stdin:
    arguments = map(ast.literal_eval, line.split())
    print arguments

rawDescription: python sys.argv differentiate int and string

description: Python sys.argv differentiate int and string


code:
def foo(column):
    class Foo(object):
        if column == 'a':
            bar = 'a'
            foo = 'c'
        else:
            bar = 'b'

    return Foo

for x in 'ab':
    cls = foo(x)
    print cls.bar
    print cls.foo

rawDescription: Django passing object to class

description: Django passing object to class


code:
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(25, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(4, GPIO.OUT)
GPIO.output(4, GPIO.HIGH)

def callback_func(pin):
    while not GPIO.input(pin):
        GPIO.output(4, GPIO.LOW)
    GPIO.output(4, GPIO.HIGH)

GPIO.add_event_detect(25, GPIO.FALLING, callback=callback_func, bouncetime=100)

def main():
    while True:
        print "Not blocking! You're free to do other stuff here"
        time.sleep(5)

if __name__ == "__main__":
    main()

rawDescription: GPIO Momentary button controls relay

description: GPIO Momentary button controls relay


code:
class DeviceListener:
  def __init__(self, device):
    LOCK.acquire() # first you lock it up
    self.device = device
    self.start_listener()
    LOCK.release()

  def start_listener(self):
    while True: # because of the loop, the lock wouldn't get release even if the LOCK below doesn't exist
      LOCK.acquire() # it end up blocking here and oops, it locked up xD
      data = self.device.read(255) # so it wouldn't be able to read here
      LOCK.release()
      if data:
        print data
      else:
        pass

rawDescription: Two threads, one object

description: Two threads, one object


code:
import subprocess

def getmac(command):
    return subprocess.check_output(command, shell=True)

command = "ifconfig -a | grep HWaddr"
print "%s" %(getmac(command).split()[9])
# or print out the entire list to see which index your HWAddr corresponds to
# print "%s" %(getmac(command).split())

rawDescription: Using console commands in python

description: Using console commands in python


code:
with open("file_path","r") as f:
    lines = f.read()
    lines = lines.replace("http://","")
    lines = lines.replace("www.", "") # May replace some false positives ('www.com')
    urls = [url.split('/')[0] for url in lines.split()]
    print '\n'.join(urls)

rawDescription: Find http:// and or www. and strip from domain. leaving domain.com


code:
import time

wakeup = time.time()

while True:
    wakeup += 6
    print "start",

    for i in range(20):
        time.sleep(0.5)   # simulate work
        print i,

        if time.time() > wakeup:
            break

    print "finished"

    while time.time() < wakeup:
        time.sleep(1)

rawDescription: Restart a script after 6 minutes

description: Restart a script after 6 minutes


code:
for line in data:
    if re.match("(.*)(background-color:red)(.*)", line):
        print line
        break

rawDescription: End of for loop options in Python?

description: End of for loop options in Python


code:
from bs4 import BeautifulSoup

data = """

        Do not try to get me with a regex, please.

"""

soup = BeautifulSoup(data)
print soup.find('div', {'class': 'id-app-orig-desc'}).text.strip()

rawDescription: Pattern match not working as expected python


code:
import Tkinter as tk

class App(object):
    def __init__(self):
        self.right = False
        self.left = False
        self.up = False

    def keyPressed(self,event):
        print "HERE"
        if event.keysym == 'Escape':
            root.destroy()
        elif event.keysym == 'Right':
            self.right = True
        elif event.keysym == 'Left':
            self.left = True
        elif event.keysym == 'Up':
            self.up = True

    def keyReleased(self,event):
        if event.keysym == 'Right':
            self.right = False
        elif event.keysym == 'Left':
            self.left = False
        elif event.keysym == 'Up':
            self.up = False

    def task(self):
        if self.right:
            print 'Right'
        elif self.left:
            print 'Left'
        elif self.up:
            print 'Forward'
        root.after(20,self.task)

application = App()
root = tk.Tk()
print( "Press arrow key (Escape key to exit):" )

root.bind_all('<Key>', application.keyPressed)
root.bind_all('<KeyRelease>', application.keyReleased)
root.after(20,application.task)

root.mainloop()

rawDescription: Arrow key input code not working in tkinter


code:
x = tf.constant([[1, 220, 55], [4, 3, -1]])
x_max = tf.reduce_max(x, reduction_indices=[1])
print sess.run(x_max)  # ==> "array([220,   4], dtype=int32)"

rawDescription: TensorFlow: Max of a tensor along an axis


code:
data = []
for day in soup.find_all('td', class_='events'):
    data.append([div.text for div in day.find_all('div', class_='econoevents')])

print data

rawDescription: Python 2.7 : Can't figure out how to parse a tree with BeautifulSoup4


code:
for indx, ele in enumerate(a):
    if ele == 2:
        a[indx] = 10
print a

rawDescription: Manually replace elements in a list

description: Manually replace elements in a list


code:
# Import "csv.DictReader" and put it in the name "dr".
from csv import DictReader as dr

# These are the columns we will be working with.
cols = "TitleB", "TitleRNum", "TitlePNum", "TitleBF", "TitleCheck"

# This is a variable to hold a previous row for future processing.
# It severs the same purpose as the "entries" dict in Sudipta Chatterjee's answer.
# I set it to 0 simply so its value is false.  You could also set it to "False" or "None".
mark = 0

# Open the CSV file in binary mode.
with open("exampledata.csv", "rb") as f:

    # This loops through what is returned by "DictReader".
    #
    # The expression "f.readline().strip().split(",")" reads the first line of the file,
    # (which is the column names), strips off the newline at the end,
    # and then gets the column names by splitting the line on commas.
    for row in dr(f, f.readline().strip().split(",")):

        # This checks if "mark" is true.
        # If it is, then that means "mark" contains a previous row to be processed.
        if mark:

            # This line takes the row stored in "mark" as well as the current row
            # and puts them together, separating the values with commas using "str.join".
            print ",".join([",".join([mark[c], row[c]]) for c in cols])

        # This is a compact statement equivalent to:
        #
        #    if row["TitlePNum"] == row["TitleRNum"] == "1":
        #        mark = row
        #    else:
        #        mark = 0
        #
        # It sees if the "TitlePNum" and "TitleRNum" columns in the current row are both "1".
        # If so, it saves that row in "mark" for future processing.
        #
        # It is basically the same thing as the
        #
        #    if TitleRNum == 1 and TitlePNum == 1:
        #        entries[row['TitleIndicator']] = row
        #
        # part in Sudipta Chatterjee's answer.
        mark = row if row["TitlePNum"]==row["TitleRNum"]=="1" else 0

rawDescription: Matching keys in csv file and producing desired output


code:
from os.path import dirname

def parents(p):
    while len(p) > 1:
        p = dirname(p)
        yield p

pathsManaged = ['/a/b', '/c/d', '/e']

currPath = '/e/a'

if any(p in pathsManaged for p in parents(currPath)):
    print 'subdir of already managed path'

rawDescription: python - check if list entry is contained in string

description: Python - check if list entry is contained in string


code:
import sys

class Logger(object):
    def __init__(self, *files):
        self.files = files

    def write(self, obj):
        for f in self.files:
            f.write(obj)

f = open('file', 'w')
sys.stdout = Logger(f, sys.stdout)

print "Python Magic"

rawDescription: How to automatically direct print statements' outputs to a file in python?

description: Automatically direct print statements' outputs to a file in python


code:
shipUrl = 'http://www.veristar.com/portal/veristarinfo/generalinfo/registers/seaGoingShips?portal:componentId=p_efff31ac-af4c-4e89-83bc-55e6d477d131&interactionstate=JBPNS_rO0ABXdRAAZudW1iZXIAAAABAAYwODkxME0AFGphdmF4LnBvcnRsZXQuYWN0aW9uAAAAAQAYc2hpcFNlYXJjaFJlc3VsdHNTZXRTaGlwAAdfX0VPRl9f&portal:type=action&portal:isSecure=false'
shipPage = urlopen(shipUrl)

soup = BeautifulSoup(shipPage)
table = soup.find_all("table", { "class" : "table1" })
for mytable in table:
    table_body = mytable.find('tbody')
    try:
        rows = table_body.find_all('tr')
        for tr in rows:
            cols = tr.find_all('td')
            for td in cols:
                print td.text
    except:
        print "no tbody"

rawDescription: Python BeautifulSoup to scrape tables from a webpage


code:
result = []
previous_keyword = None
with open('A.pdb') as f:
    for line in f:
        # use these five lines if keyword is fixed at 3rd column, and columns are separated by whitespace
        try:
            keyword = line.split()[2]
        except:
            print('Line with unknown format: ' + line)
            continue

        # use this one if the keyword is fixed at position[13:16]
        #keyword = line[13:16]

        if keyword != previous_keyword:
            result.append(line)
            #result.append(line.rstrip())     use this one if you don't want trailing 'newline'
            previous_keyword = keyword

for x in result:
    print x

rawDescription: Removing repeated lines from text file

description: Removing repeated lines from text file


code:
def say_hello():
    print 'Hello'

say_hello()  # behavior before wrapping
say_hello = wrapper(say_hello)
say_hello()  # behavior after wrapping

rawDescription: Python Method Definition Using Decorators


code:
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from numpy import arange, sin, pi
import threading

class Dummy():

    def plotme(self, iteration = 1):

        print "%ix plotting... " % iteration,
        t = arange(0.0, 2.0, 0.01)
        s = sin(2*pi*t)

        fig, ax = plt.subplots()
        ax.plot(t, s)
        ax.set_xlabel('time (s)')
        ax.set_ylabel('voltage (mV)')
        ax.set_title('About as simple as it gets, folks (%i)' % iteration)
        fig.savefig("19110942_%i_test.png" % iteration)

    def threadme(self, iteration = 1):

        thread_plot = threading.Thread(target=self.plotme,
                                      args=(iteration,))
        thread_plot.start()
        thread_plot.join()

dummy = Dummy()
dummy.threadme(1)
dummy.threadme(2)

rawDescription: matplotlib plotting freezing in simple thread

description: Matplotlib plotting freezing in simple thread


code:
class C:
    def __init__(self, n):
        self.num = n
    def __repr__(self):
        return str(self.num)

my_list = [C(i) for i in xrange(3)]
for member in my_list:
    member.num += 42
print my_list

rawDescription: Python List & for-each access (Find/Replace in built-in list)


code:
import io
data = """dateA; item1; item2; item3; item4; itemN
dateB; item5; item2; item3; item6; itemN
"""

df = pd.read_csv(io.BytesIO(data), sep=";", header=None, skipinitialspace=True, index_col=0)

from itertools import combinations, chain
df2 = df[list(chain.from_iterable(combinations(df.columns, 2)))]

df2.columns = pd.MultiIndex.from_product([range(df2.shape[1]//2), ["A", "B"]])

print df2.stack(level=0)

rawDescription: Transform dataframe rows with lists of items into rows with pairs of items


code:
for lyric in tag.lyrics:
  print lyric

rawDescription: Retrieve lyrics from an mp3 file in Python using eyeD3


code:
def do_op(x,y,fn):
   return fn(x,y)

def add(a,b):
   return a+b

print do_op(5,4,add)

rawDescription: How to pass a function as a parameter to a class in python

description: Pass a function as a parameter to a class in python


code:
kernel = stats.gaussian_kde(np.vstack([x_data, y_data]))
print kernel([0.5, 1.3])

bw = kernel.covariance_factor()
kernel2 = stats.gaussian_kde(np.vstack([x_data, y_data]), bw_method=bw)
print kernel2([0.5, 1.3])

rawDescription: Getting bandwidth used by SciPy's gaussian_kde function


code:
f1 = open('a', 'r').readlines()
f2 = open('b', 'r').readlines()
out = []
count = 1
for i in f1:
    flag = False
    for j in f2:
        if i == j:
            flag = True
    if not flag:
        out.append(count)
    count+=1
for o in out:
    print o

rawDescription: Compare two files in python and save line differences in a new file


code:
import multiprocessing as mp
import time

def calculate(num):
    l = [num*num for num in range(num)]
    s = sum(l)
    del l       # delete lists as an  option
    return s

if __name__ == "__main__":

    # fix is in the following line #
    pool = mp.Pool(processes=2,maxtasksperchild=1000)

    time.sleep(5)
    print "launching calculation"
    num_tasks = 1000
    tasks =  [pool.apply_async(calculate,(i,)) for i in range(num_tasks)]
    for f in tasks:
        print f.get(5)
    print "calculation finished"
    time.sleep(10)
    print "closing  pool"
    pool.close()
    print "closed pool"
    print "joining pool"
    pool.join()
    print "joined pool"
    time.sleep(5)

rawDescription: High Memory Usage Using Python Multiprocessing


code: print ''.join([unicode(t).strip() for t in x.findAll(text=True)])

rawDescription: Parsing HTML page using beautifulsoup

description: Parsing HTML page using beautifulsoup


code:
sent = "The ninja turtles fight Shredder."
for i in ne_chunk(pos_tag(word_tokenize(sent))):
    print i

sent = "The Ninja Turtles fight Shredder."
for i in ne_chunk(pos_tag(word_tokenize(sent))):
    print i

rawDescription: How do I tag a sentence with the Brown or CoNLL2000 tagger / chunker?

description: Tag a sentence with the Brown or CoNLL2000 tagger / chunker


code:
for i in sorted(items, key = lambda x: (order.index(x[1][0]), x[0])):
    print i

rawDescription: Complex sort in python

description: Complex sort in python


code:
dict_options = {1: 'Development', 2: 'Certification', 3: 'Production'}
print " " + str(dict_options).strip('{}').replace(',', '\n')

rawDescription: python - list a dictionary using raw_input

description: Python - list a dictionary using raw_input


code:
import pandas as pd

result = { 'meta': {'some': 'meta info'} }

df = pd.DataFrame([[1,2,3], [.1,.2,.3]], columns=('a','b','c'))

df_dict = df.to_dict()

df_dict['extra_data'] = result

print pd.json.dumps( df_dict )

rawDescription: Combine DataFrame as JSON with additional data


code:
class IntermediateObj(object):

    def __init__(self, raw):
        self.raw = raw
        self.sections = []
        self.fields = []
        self.elements = []

    def recurse(self, items):
        # this works ok, but passing the same arguments to the recurse function
        # which were passed to the constructor as well ,and then stored as a class
        # attribute, why, seems like self.raw is not even needed?
        for k,v in items.iteritems():
            if isinstance(v, list):
                getattr(self, k).append(v)
                [self.recurse(i) for i in v]
            else:
                setattr(self, k, v)

class MyClass(IntermediateObj):

    def __init__(self, attribute=None):
        self.attribute = attribute
        super(MyClass, self).__init__(attribute)

ii = IntermediateObj({'large': 'nested_dictionary',
                      'sections': [{}, {}, {}]
                    })
ii.recurse(ii.raw)
print ii.raw
print ii.sections

# passing an instance of IntermediateObj is not the same as passing a dict (logically)
# yet the constructor of MyClass just forwards that instance object to the
# baseclasses constructor, while you initially passed a dict to the IntermediateObj
# constructor.
tada = MyClass(ii)
# MyClass inherits the recurse method, but it won't magically be executed unless
# you call it, so just instantiating MyClass won't copy those values recursively
tada.recurse(ii.raw)
# now tada, it copied everything, and notice, I called recurse with ii.raw, which is the
# source dictionary, but I'm not even sure if you wanted it that way, it's not clear
# define your question more precisely
print tada.sections

rawDescription: Python class inheritance (multiple) : Why are attributes empty? i.e. doing it right


code:
def comparisons(value):
    """Only change here is using x <= y < z syntax."""
    if value < 5:
        status = 'nill'
        total = 0
    elif 5 <= value < 8:
        status = 'valid'
        total = 1
    elif 8 <= value < 12:
        status = 'superb'
        total = 5
    else:
        status = 'over-qualified'
        total = 10
    # Here we return the status and the total for each item
    # This may not be what you want, so this can be adjusted
    return status, total

# Create a list that will contain your 15 items
items = ['Engineering', 'Carpentry']

# Create a container to hold the results.
# Note that we don't use different variables names each time -
# instead, we create an entry in a dictionary corresponding to
# our 'items' values.
results = {}

# Now we iterate through each item in our items list, running
# our function and storing the results. Note that this is a guess
# as to how you want to handle 'status' - a more useful solution
# could be to use a nested dictionary where each item has a dictionary
# with two sub-fields: 'status' and 'total'.
for item in items:
    status, total = comparisons(int(raw_input('{0}: '.format(item))))
    results[item] = [status, total]

# Print out each item
print '\nIndividual items:'
for item, values in results.iteritems():
    print item, values

# Example of how you could sum the totals
print '\nTotal score:'
print sum((results[x][1] for x in results))

rawDescription: Creating functions with different variables


code:
print df.groupby('one').apply(net_func).xs(0, level=1)
#       one  two  dailyTrue  dailyFalse
#one
#AAL    AAL    1          1           0
#AAPL  AAPL    1          1           0

print df.groupby('one').apply(net_func).xs(1, level=1)
#       one  two  dailyTrue  dailyFalse
#one
#AAL    AAL    2          1           0
#AAPL  AAPL    2          1           0

rawDescription: Python Pandas, slice rows from group in .groupby().apply()


code:
def PrintFruiteListSentence(list_of_fruits):
    sentence = 'You would like to eat '
    for i, item in enumerate (list_of_fruits):
        if i != (len(list_of_fruits) - 1):
            sentence += item + ' as fruit ' + str(i+2) + ' and '
        else:
            sentence += item + ' as fruit ' + str(i+2)
    sentence += ' in your diet'
    print sentence

rawDescription: concatenate print stmt inside and outside of for loop into a sentence in python

description: Concatenate print stmt inside and outside of for loop into a sentence in python


code:
if 5 in di:
    print "has a five"

rawDescription: Find sequences of digits in long integers efficiently


code:
def check_section(self, section):
    for attr in getattr(self, '%s_attrs' % section):
        print attr

rawDescription: How can I dynamically refer to a variable in Python

description: Dynamically refer to a variable in Python


code:
#! /usr/bin/env python

''' Split DNA sequences and save to separate files

    Sequences consist of random subsequences of the letters 'ACGT'
    as well as short sequences of 'N', of random length up to 200.
    Exactly 1000 'N's separate sequence blocks.
    All sequences may contain newlines chars

    Written by PM 2Ring 2015.03.23
'''

import sys
from linebuffer import LineBuffer

#Output line width
linewidth = 120

#Sequence separator
nsepblock = 'N' * 1000

def main():
    iname = 'DNA_sequence.txt'
    outbase = 'contig'

    with open(iname, 'rt') as f:
        data = f.read()

    #Remove all newlines
    data = data.replace('\n', '')

    sequences = data.split(nsepblock)

    #Save each sequence to a series of files
    for i, seq in enumerate(sequences, 1):
        outname = '%s%05d' % (outbase, i)
        print outname

        #Write sequence data, with line breaks
        lb = LineBuffer(outname, linewidth)
        lb.write(seq)
        lb.close()

if __name__ == '__main__':
    main()

rawDescription: Python splitting with string as delimiter


code:
import dependencies_groups
for group in sortedList:
    for job in getattr(dependencies_groups, group):
        print job

rawDescription: Python: Iterate through a list that refers to other lists


code: print"\n".join("Wow{}".format('!'*i) for i in range(1,4))

rawDescription: Shortest way to add more characters to a string with each successive line


rawDescription: Filter numpy array by comparing elements to elements in prior row without looping


code:
"""
Dependencies:
    1) before executing this script, the script 1_script.py
        has to have been successfully run

This script:
    1) reads the output generated by 1_script.py
    2) modifies the read data with a user-supplied input
    3) prints the modified data to the screen
"""
def post_input():
    # reads the output generated by 1_script.py
    f = open('output_from_1_script.txt', 'r')
    pregenerated_data = f.readline()
    f.close()

    # modifies the read data with a user-supplied input
    user_input = raw_input('Enter an input with which to modify the output     generated by 1_script.py: ')
    modified_data = pregenerated_data + user_input

    print modified_data

rawDescription: How to automate the execution of several inter-dependent Python scripts

description: Automate the execution of several inter-dependent Python scripts


code:
import math

def distance(argv):
    comp_diff = []
    for comp_1, comp_2 in argv:
        comp_diff.append(comp_1-comp_2)
        print comp_2
    return math.sqrt(sum([math.pow(comp,2) for comp in comp_diff]))

if __name__ == '__main__':
    components = [(0,4), (0,5), (0,4)]
    d = distance(components)
    print d

rawDescription: dictionary not storing multiple values for same key

description: Dictionary not storing multiple values for same key


code:
import random

#----------------------------------------------#
def main():
    my_array = create_list_and_find_max_and_min(10)
    print the_smart_way(my_array)
#----------------------------------------------#
def create_list_and_find_max_and_min(n):
    my_array = []
    n = input("How many numbers do you want in your array?:")
    for i in range(n):
        my_array.append(random.randint(1,n))
    print "My array is:", my_array
    #----------------------------------------------#
    min = my_array[0]
    for number in my_array:
        if min > number:
            min = number
    print "The minimum value in the array is:", min
    #----------------------------------------------#
    max = my_array[0]
    for number in my_array:
        if max < number:
            max = number
    print "The maximum value in the array is:", max
    return my_array
#----------------------------------------------#
def the_smart_way(my_array):
    # "This one uses the built-in Python functions for min/max..."
    min_my_array = min(my_array)
    max_my_array = max(my_array)
    return min_my_array, max_my_array
#----------------------------------------------#
if __name__ == '__main__':
    main()

rawDescription: Python Alternatives to Global Variables

description: Python Alternatives to Global Variables


rawDescription: How to write a regex to match multiple file path

description: Write a regex to match multiple file path


code:
def negpos (lst):
    (neg, pos) = (0, 0)
    for elem in lst:
        if elem < 0:
            neg = neg + elem
        else:
            pos = pos + elem
    return [neg, pos]

print negpos ([-5,5,6,-8,9])

rawDescription: negatives and positives in lists

description: Negatives and positives in lists


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

import re
regex = re.compile(r"(?P<minus>-)?((?P<minutes>\d+):)?(?P<seconds>\d+)(,(?P<centiseconds>\d{2}))?")

def parse_time(time_str):
    parts = regex.match(time_str)
    if not parts:
        return
    parts = parts.groupdict()
    time_params = {}
    for (name, param) in parts.iteritems():
        if param and (name != 'minus'):
            time_params[name] = int(param)
    time_params['milliseconds'] = time_params['centiseconds']*10
    del time_params['centiseconds']
    return (-1 if parts['minus'] else 1) * datetime.timedelta(**time_params)

df = pd.DataFrame({'Time':['-1:10,10', '1:16,32', '3:03,04']})
df['timedelta'] = df.Time.apply(lambda x: parse_time(x))
df['secs'] = df['timedelta'].apply(lambda x: x / np.timedelta64(1, 's'))
print df

rawDescription: Pandas; transform column with MM:SS,decimals into number of seconds


code:
# Convert 7F back to an integer:
print int(hex_list[0], 16)

rawDescription: process a string of ascii Hex values into a numerical hex value

description: Process a string of ascii Hex values into a numerical hex value


code: print map(bin,parities)

rawDescription: Python XOR on hex and string (parity check function)


code:
class BaseFile(object):
    """Inherit these common methods and properties"""
    def __init__(self, fname):
        self.fname = fname

    def is_pretty(self):
        NotImplemented

class TextFileClass(BaseFile):
    def is_pretty(self):
        return False

class RTFFileClass(BaseFile):
    def is_pretty(self):
        return True

def createFileClass(fname):
    if fname.endswith('.txt'):
        return TextFileClass(fname)
    elif fname.endswith('.rtf'):
        return RTFFileClass(fname)
    else:
        raise Exception, 'unknown file format'

FileClass = createFileClass

rawDescription: How to create a hidden factory?

description: Create a hidden factory


code:
def pytest_funcarg__res(request):
    def setup():
        print "res-setup"
    def teardown(val):
        print "res-teardown"
    return request.cached_setup(setup, teardown)

def test_hello(res):
    raise KeyboardInterrupt()

rawDescription: py.test: get KeyboardInterrupt to call teardown

description: Py.test: get KeyboardInterrupt to call teardown


code:
import inspect
import parser

# this flatten function is by mike c fletcher
def flatten(l, ltypes=(list, tuple)):
    ltype = type(l)
    l = list(l)
    i = 0
    while i < len(l):
        while isinstance(l[i], ltypes):
            if not l[i]:
                l.pop(i)
                i -= 1
                break
            else:
                l[i:i + 1] = l[i]
        i += 1
    return ltype(l)

# function we're interested in
def a():
    current_func = eval(inspect.stack()[0][3])
    last_frame = inspect.stack()[1]
    calling_code = last_frame[4][0]
    syntax_tree = parser.expr(calling_code)
    syntax_tree_tuple = parser.st2tuple(syntax_tree)
    flat_syntax_tree_tuple = flatten(syntax_tree_tuple)
    list_of_strings = filter(lambda s: type(s)==str,flat_syntax_tree_tuple)
    list_of_valid_strings = []
    for string in list_of_strings:
        try:
            st = parser.expr(string)
            list_of_valid_strings.append(string)
        except:
            pass
    list_of_candidates = filter(lambda s: eval(s)==current_func, list_of_valid_strings)
    print list_of_candidates

# other function
def c():
    pass

a()
b=a
a(),b(),c()
a(),c()
c(),b()

rawDescription: Aliased name of a Function in Python

description: Aliased name of a Function in Python


rawDescription: python class [] function

description: Python class [] function


code:
for a in xrange(5+1): # Note xrange(n) produces 0,1,2...(n-1) and does not include n.
    for b in xrange (3+1):
        for c in xrange (8+1):
            print a,b,c

rawDescription: Why is only the innermost of these nested while loops working?


code: print "If there was a birth every 7 seconds, there would be: {} births".format(births)

rawDescription: How can I print variable and string on same line in Python?

description: Print variable and string on same line in Python


code:
import urllib,re
sock = urllib.urlopen("http://ipchicken.com")
htmlSource = sock.read()
sock.close()
print htmlSource
ips = re.findall('(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})', htmlSource)
print "IPs in page", ips

rawDescription: How to Get a line of text from html to Python

description: Get a line of text from html to Python


code:
# smuggle.py
def smuggle(url, name):
    code = urllib.urlopen(url).read()
    module = imp.new_module(name)
    exec code in module.__dict__
    return module

hello = smuggle("http://127.0.0.1:1234/hello.py", "hello")
hello.world()
# prints out "hello world!"

rawDescription: How should a global (module) object be created in a Python function?


code:
#!/usr/bin/python
import threading
import socket

class connection(threading.Thread):
    def __init__(self, sock, addr, parent):
        self.sock = sock
        self.addr = addr
        self.parent = parent
        threading.Thread.__init__(self)
        self.sock.settimeout(None)
        self.closed = False # will be set to True on thread end
    def run (self):
        while not self.parent._stopped:
            buffer = self.sock.recv(1024)
            if buffer == "disconnected\r\n":
                self.sock.send("bye")
                break
            elif buffer:
                self.sock.send(buffer)
        self.sock.close()
        self.closed = True

class echoserver(threading.Thread):
    def __init__(self, port=12119):
        threading.Thread.__init__(self)
        self.running = False
        self._port = port
        self._socket = None
        self._stopped = False
        self._conns = [] # list of active connections

    def isRunning(self):
        return self.running

    def port(self):
        return self._port

    def run(self):
        self.running = True
        self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self._socket.bind(("0.0.0.0", self.port()))
        self._socket.listen(5)
        self._socket.settimeout(5) # use a timeout to respond to stop()
        while not self._stopped:
            try:
                conn, addr = self._socket.accept()
                c = connection(conn, addr, self)
                self._conns.append(c) # add child the the list
                c.start()
            except Exception as e:
                # print e # in debug
                pass
            self._conns = self.child_list() # remove closed child from list
        self._socket.close()
        print "Server is closing..."
        for connect in self._conns: # join active children
            connect.join()
        print "Server is closed"

    def stop(self):
        self._stopped = True

    def child_list(self):
        l = []
        for conn in self._conns:
            if conn.closed:
                conn.join()
            else:
                l.append(conn)
        return l

rawDescription: python stop multithreaded echo server

description: Python stop multithreaded echo server


code:
#!/usr/bin/env python

from ctypes import c_int, byref, cdll

a = c_int(3)
b = c_int(4)
c = c_int()

addlib = cdll.LoadLibrary('./add.so')
addlib.add_(byref(a),byref(b),byref(c))
print c

rawDescription: Wrapping a f2py-compiled Fortran .so with Cython (without passing by python)


code:
import inspect

def c():
    def a():
        frame = inspect.currentframe()
        print frame.f_back.f_locals

    def b():
        pass

    a()

c()

rawDescription: Access parent namespace in python

description: Access parent namespace in python


code:
import datetime
import csv

A = ((248500353L,
    11,
    '4',
    '248741302',
    633,
    7321L,
    7321L,
    'EAD4083003',
    0,
    datetime.datetime(2011, 4, 19, 23, 0, 42),
    datetime.datetime(2011, 4, 19, 23, 1, 39)),)

with open('output.csv', 'wb') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(A[0])

rawDescription: How to extract values from tuple in Python?

description: Extract values from tuple in Python


code: print re.sub(r'\..*\.', '.', data_file)

rawDescription: Python regex to extract substring at start and end of string


code:
import os
sdir=os.path.dirname(os.path.abspath(__file__)).split(os.sep)[-1]
print sdir

rawDescription: copy directory name and placing it into a variable (python)

description: Copy directory name and placing it into a variable (python)


code:
# #1
a = range(3)
b = a
a = range(5)
print b

# #2
a = range(3)
b = a
a[:] = range(5)
print b

rawDescription: What is the correct way to reassign a list in Python?


code:
from bs4 import BeautifulSoup

data = """<form method="post" enctype="multipart/form-data" action="http://localhost/wp-admin/update.php?action=upload-plugin">
        <input type="hidden" id="_wpnonce" name="_wpnonce" value="e2e315cd8f"><input type="hidden" name="_wp_http_referer" value="/wp-admin/plugin-install.php?tab=upload">     <label class="screen-reader-text" for="pluginzip">Plugin zip file</label>
        <input type="file" id="pluginzip" name="pluginzip">
        <input type="submit" class="button" value="Install Now">
    </form>"""

soup = BeautifulSoup(data)
print soup.find("input", {'id': "_wpnonce"}).attrs['value']

rawDescription: Scrape input value from html

description: Scrape input value from html


code:
import re
a = ("as-df-- as-df- as-df (as-df) 'as-df' asdf-asdf) (asd-f"
     "asdf' asd-f' -asdf- %asdf%s asdf& $asdf$ +asdf+ asdf++ asdf''")
b = re.sub(r"((?<=\s)|\A)(?P<chr>[-()+%&'$])([^\s]*)(?P=chr)((?=\s)|\Z)",r"\3",a)
print b

rawDescription: special character at the begin who match with the end from every word [only regex]

description: Special character at the begin who match with the end from every word [only regex]