code:
def callback(self, response):
    print response.headers

rawDescription: Scrapy - getting the the file size and type from a URL without downloading the file?


code:
import sys
import os

RESERVED = 40
SENTINEL = '\n### HEADER ENDS ###\n'

def pad(heading):
    free_space = RESERVED - len(heading)
    padding = ('#' * free_space) if free_space > 0 else ''
    return heading + padding

def _write_header_slow(fname, text):
    # Do this in chunks instead if you have large files.
    dest = fname + '.temp'
    with open(fname) as infile:
        content = infile.read()
    with open(dest, 'w') as outfile:
        outfile.write(text)
        outfile.write(SENTINEL)
        outfile.write(content)
    os.rename(dest, fname)

def write_header(fname, text):
    if not text.endswith('\n'):
        text += '\n'
    assert len(text) < RESERVED, 'too much for the header!'
    padded = pad(text)
    with open(fname, 'rb+') as f:
        current_header = f.read(RESERVED + len(SENTINEL))
        if current_header.endswith(SENTINEL):
            f.seek(0)
            print 'fast path!'
            f.write(padded)
        else:
            print 'slow path ):'
            _write_header_slow(fname, text)

if __name__ == '__main__':
    write_header(sys.argv[1], sys.argv[2])

rawDescription: Most efficient way to add a (generated) header to many files?


code:
class bar:
    pass

@DictAccess
class foo(bar):
    x = 1
    y = 2

myfoo = foo()
print myfoo.x # gives 1
print myfoo['y'] # gives 2
myfoo['z'] = 3
print myfoo.z # gives 3

rawDescription: Python class decorator converting element access to attribute access


rawDescription: Creating multiple class instances from a list of names in Python?


code:
a = 'a\xcc\x88'
b = '\xc3\xa4'
a
print a,b   # should look the same, if not it is the browser's fault :)
a==b
[a,b]      # In a list you can see the difference by default.

rawDescription: Python list items encoding

description: Python list items encoding


code:
full_string = 'xxxxxbxxxxx'
if any(s in full_string for s in ('a', 'b')):
    print 'found'

rawDescription: Test if one OR an other substring is in a string, in a DRY way


code:
def findblock( lines, start, stop ):
    it = iter(lines)
    for line in it:
        if start in line:
            # now we are in the block, so yield till we find the end
            for line in it:
                if stop in line:
                    # lets just look for one block
                    return # leave this generator
                    # break # would keep looking for the next block
                yield line

for line in findblock(lines, start="something distinctive",
                             stop="something distinctive"):
    print line

rawDescription: How can I focus on a subset of a list in python

description: Focus on a subset of a list in python


code:
for item1 in np.atleast_2d(data)[:,0]:
    print item1

rawDescription: determine number of rows of an (M,N) array and (M,) array with same function

description: Determine number of rows of an (M,N) array and (M,) array with same function


code: print dict(c)

rawDescription: Sum corresponding elements of multiple python dictionaries


code:
from BeautifulSoup import BeautifulSoup, NavigableString

def strip_tags(html, invalid_tags):
    soup = BeautifulSoup(html)

    for tag in soup.findAll(True):
        if tag.name in invalid_tags:
            s = ""

            for c in tag.contents:
                if not isinstance(c, NavigableString):
                    c = strip_tags(unicode(c), invalid_tags)
                s += unicode(c)

            tag.replaceWith(s)

    return soup

html = "
Good, bad, and ugly
"
invalid_tags = ['b', 'i', 'u']
print strip_tags(html, invalid_tags)

rawDescription: Remove a tag using BeautifulSoup but keep its contents


rawDescription: Comparing values in two list and updating it into the dictionary


code:
import re
pattern = re.compile('.*(RESPOND.*?ALARM\sLEVEL.*?),.*==\s(.*)\s:')

msg = 'MFS: *CFSRES INC0020 01/12/14 07:51 RESPOND TEST ONLY, ALARM LEVEL: 1, 123 TESTSTREET TESTTOWN ,MAP: ,TG 100, == TESTING : UNIT HERE'

match = pattern.match(msg)

if match:
  for group in match.groups():
      print group
else:
    print 'NO MATCH!!'

rawDescription: need to Spilt srting twice

description: Need to Spilt srting twice


code:
class B(object):
    x = 3

CopyOfB = type('CopyOfB', B.__bases__, dict(B.__dict__))

b = B()
cob = CopyOfB()

print b.x   # Prints '3'
print cob.x # Prints '3'

b.x = 2
cob.x = 4

print b.x   # Prints '2'
print cob.x # Prints '4'

rawDescription: How to copy a python class?

description: Copy a python class


code:
L = [([1, 2, 3], 4), ([5, 6, 7], 8)]
element = [5, 6, 7]

D = dict((tuple(a), b) for a, b in L)
# keys must be hashable: list is not, but tuple is
# or you could just build the dict directly:
#D = {(1,2,3): 4, (5,6,7): 8}

v = D.get(tuple(element))
if v is not None:
  print v
else:
  print "not found"

rawDescription: how to get the index or the element itself of an element found with "if element in list"

description: Get the index or the element itself of an element found with "if element in list"


code:
import numpy as np
from scipy.signal import lfilter

np.random.seed(123)

A = np.random.randn(10)
a = 2.0
b = 3.0

# Compute the recursion using lfilter.
# [a] and [1, -b] are the coefficients of the numerator and
# denominator, resp., of the filter's transfer function.
B = lfilter([a], [1, -b], A)

print B

# Compare to a simple loop.
B2 = np.empty(len(A))
for k in range(0, len(B2)):
    if k == 0:
        B2[k] = a*A[k]
    else:
        B2[k] = a*A[k] + b*B2[k-1]

print B2

print "max difference:", np.max(np.abs(B2 - B))

rawDescription: Recursive definitions in Pandas

description: Recursive definitions in Pandas


code:
class cls(object):
    pass

a = "<a>"
b = "<b>"
c = ["", "", "<c>"]
d = cls()
d.s = "<d.s>"
e = cls()
e.s = "<e.s>"

var_list = "a b c[2:7] d.s e.s".split()

for var in var_list:
    exec "%s+='Mod'" % var             # Modify the variables

print a,b,c,d.s,e.s
# >>> <a>Mod <b>Mod ['', '', '<c>', 'M', 'o', 'd'] <d.s>Mod <e.s>Mod

rawDescription: Python: modify strings stored in various objects inplace in a loop


code:
import ply.lex as lex

class LqbLexer:
    # List of token names.   This is always required

    tokens = [
        'QTAG',
        'INT',
        'SPACETAB'
        ]

    # Regular expression rules for simple tokens

    def t_QTAG(self,t):
        # corrected regex
        r'(?m)^\s*[Qq]\.[0-9]+\s+'
        t.value = int(t.value.strip()[2:])
        return t

    # A regular expression rule with some action code
    # Note addition of self parameter since we're in a class
    def t_INT(self,t):
        r'\d+'
        t.value = int(t.value)
        return t

    # Define a rule so we can track line numbers
    def t_newline(self,t):
        r'\n+'
        print "Newline found"
        t.lexer.lineno += len(t.value)

    # A string containing ignored characters (spaces and tabs)
    # Instead of t_ignore  = ' \t'
    def t_SPACETAB(self,t):
        r'[ \t]+'
        print "Space(s) and/or tab(s)"

    # Error handling rule
    def t_error(self,t):
        print "Illegal character '%s'" % t.value[0]
        t.lexer.skip(1)

    # Build the lexer
    def build(self,**kwargs):
        self.lexer = lex.lex(debug=1,module=self, **kwargs)

    # Test its output
    def test(self,data):
        self.lexer.input(data)
        while True:
             tok = self.lexer.token()
             if not tok: break
             print tok

# test it
q = LqbLexer()
q.build()
print "-============Testing some VALID inputs===========-"
q.test("Q.11 ")
q.test("  Q.12 ")
q.test("q.13     ")
q.test("""

   Q.14
""")
q.test("""

qewr
dhdhg
dfhg
   Q.15 asda

""")

# INVALID ones are
print "-============Testing some INVALID inputs===========-"
q.test("asdf Q.16 ")
q.test("Q.  17 ")

rawDescription: Python Lex-Yacc(PLY): Not recognizing start of line or start of string


code:
from yahoo_finance import Share
import csv

def run():
    yahoo = Share('YHOO')

    with open("scripts/hisdata.csv", 'wb') as file1:
        csv_output = csv.writer(file1)

        # First write the header row
        heading = ['Adj_Close', 'Close', 'Date', 'High', 'Low', 'Open', 'Symbol', 'Volume']
        csv_output.writerow(heading)

        h = yahoo.get_historical('2014-04-25', '2014-04-29')[0]
        csv_output.writerow([h[value] for value in heading])

        print h['Volume']
        print h['Date']

        return ([h[value] for value in ['Volume', 'Adj_Close', 'High', 'Low', 'Open']])

print run()

rawDescription: Unknown characters fetching stock data from yahoo in csv format


code:
from lxml import etree as ET

@timing
def parseXMLFromLink():
    parsed = ET.parse(url_link)
    print parsed.getroot()

for n in range(0,100):
    parseXMLFromLink()

rawDescription: Python lxml.etree - Is it more effective to parse XML from string or directly from link?


code:
from datetime import datetime

date_str_format = '%Y-%m-%d'
jobs = []

with open('jobs.txt', 'r') as f:
    for line in f:
        date_due, date_entered, title = line.split(',')
        jobs.append((datetime.strptime(date_due, date_str_format),
                     datetime.strptime(date_entered, date_str_format),
                     title.strip()))

jobs.sort()

for date_due, _, title in jobs:
    print '{} (due {})'.format(title, date_due)

rawDescription: How do I sort through lines of a text document to find a phrase based on a date?

description: Sort through lines of a text document to find a phrase based on a date


code:
import multiprocessing as mp
from time import sleep

def task2():
    sleep(1)
    print "I am doing something important."

def task1():
    for i in range(2):
        process = mp.Process(target=task2)
        process.start()

    print 'task1 done'

def task0():
    process = mp.Process(target=task1)
    process.start()
    process.join()

if __name__ == '__main__':
    task0()
    print 'all done'

rawDescription: Python multiprocessing: where should join() be called, if process' children have grandchildren?


rawDescription: Tokenization using regexp in Python

description: Tokenization using regexp in Python


code:
def numberBowTie(num):
    for i in range(num):

        # the idea is to iterate on i and when it's '1' to print only one time '1'
        # then 2*num - 2 spaces and then to print one time '1' again.
        # now do the same with i=2 only print '2' twice, 2*num - 4 spaces and then '2' twice again
        # or in general:
        #
        # 1) str(i)*i == print a string of the number i -> i times
        # 2) ' '* (2 * (num - i)) == print one space (2 * (num - i)) times
        # 3) do the same as in 1)
        #
        print str(i)*i + ' '* (2 * (num - i)) + str(i)*i

    for i in range(num):
        # in the second loop we do the exact same calculation only in reverse order
        print str(num-i)*(num-i) + ' '* (2 * i) + str(num-i)*(num-i)

numberBowTie(9)

rawDescription: Bow tie shape function

description: Bow tie shape function


code:
tree = ET.fromstring(data)
team = next(team for team in tree.iter('team')
            for number in team.iter('number')
            if number.text == '78')

team.find('.//high_goals_made_tel').text = "I've changed"
print ET.tostring(tree)

rawDescription: Eddited XML File with certain element

description: Eddited XML File with certain element


code:
sampleInput = "01100011100100000\n11100011111111011\n10011111101011011"
inputMatrix = [[int(n) for n in row] for row in sampleInput.split('\n')] #matrix where each row is a list of the numbers from sampleInput

def getConnectedZeros(matrix, x, y, foundIndicies=[]):
    if 0<=y<len(matrix) and 0<=x<len(matrix[y]): #catch out of bounds
        if matrix[y][x] == 1: #catch 1s
            return []
        else:
            if not (x,y) in foundIndicies: #catch 0's we've already "seen"
                foundIndicies.append((x,y))
                return [[y,x]] + getConnectedZeros(matrix, x+1, y, foundIndicies) + getConnectedZeros(matrix, x-1, y, foundIndicies) + getConnectedZeros(matrix, x, y+1, foundIndicies) + getConnectedZeros(matrix, x, y-1, foundIndicies)
            else:
                return []
    else:
        return []

#Now we can just loop through the inputMatrix and find all of the subsets
foundZeroIndicies = []
subsets = []
y = -1
for row in inputMatrix:
    y += 1
    x = -1
    for val in row:
        x += 1
        if (not [y,x] in foundZeroIndicies) and val==0:
            zerosList = getConnectedZeros(inputMatrix, x, y)
            subsets.append(zerosList)
            foundZeroIndicies.extend(zerosList)
for subset in subsets:
    print "Distinct Subset Found  : ", subset

rawDescription: How do I break out of this recursive loop?

description: Break out of this recursive loop


code:
import inspect
def some_func(a,b,c,d="dee",e="ee"):
    print a,b,c,d,e

some_func("aaa","bbb","ccc",e="EEE")
some_funcspec = inspect.getargspec(some_func)
some_funcspec
defargsstartindex = len(some_funcspec.args) - len(some_funcspec.defaults)
defargsstartindex
namedargsdict = dict(zip([key for key in some_funcspec.args[defargsstartindex:]], list(some_funcspec.defaults)))
namedargsdict

rawDescription: Function arguments inheriting defaults from other function


code:
while_count=0
while consumer(example_list):
    print while_count, consumer(example_list)
    while_count += 1
    if while_count > 10:
        break

rawDescription: Python Loop Behavior with Generators

description: Python Loop Behavior with Generators


code:
for field in self._columns.itervalues():
    print field.string

rawDescription: Get label names for fields in OpenERP

description: Get label names for fields in OpenERP


code:
# server.py
import socket
from time import sleep

def f(s):
        r,a = s.accept()
        print r.recv(100)

s = socket.socket()
s.bind(('localhost',1234))
s.listen(1)

f(s)
# wait around a bit for the client to send it's second packet
sleep(10)

rawDescription: talking between python tcp server and a c++ client

description: Talking between python tcp server and a c++ client


code:
def used(points, n):
  if points[n] is None:
    return { n }
  a, b, operator = points[n]
  return used(points, a) | used(points, b)

print used(points, 10)

rawDescription: Creating formula based on binary tree leaves


code:
for word in words:
    if word.startswith(u"x"):
         print word

rawDescription: selecting unicode characters from text file or webpage

description: Selecting unicode characters from text file or webpage


code:
a = [("http://wwww.ff.com", "msg 1..."),
     ("http://wwww.yy.com", "msg 2..."),
     ("http://wwww.xx.com", "msg 3...")]
print a[0][0]
# http://wwww.ff.com

rawDescription: How to do a multidimensional array in python?

description: Do a multidimensional array in python


code:
body2 = ''

for x in body:
    body += x

print body2

rawDescription: Removing <u> character from text using Scrapy


code:
for row in soup.select('table#table_prefixes4 tr'):
    print row.get_text(' - ', strip=True)

rawDescription: How to extract td HTML tags with Python BeautifulSoup?

description: Extract td HTML tags with Python BeautifulSoup


code:
with My_Context(mode=1):
    print 'Executing block of code ...'

rawDescription: Skipping execution of -with- block

description: Skipping execution of -with- block


code:
s = "key1|key2|key3=value"
keys, value = s.split('=')

base = NestedDict()

base[keys.split('|')] = value

print base # {'key1': {'key2': {'key3': 'value'}}}

rawDescription: Python parse a string into n-depth dictionary


code:
from timeit import Timer
print Timer("0 in L", "L=range(100000)").timeit(number=10000)
print Timer("0 in S", "S=set(range(100000))").timeit(number=10000)

rawDescription: Set is much faster than list while lookup for membership


code:
import itertools
print list(itertools.combinations(range(1, y+1), x))

rawDescription: List All Combinations (Python)

description: List All Combinations (Python)


code:
d = Derived.create_base()
print d

rawDescription: Smart Pointer casting in Boost::Python

description: Smart Pointer casting in Boost::Python


code:
print "Image Manipulation\n"
template = raw_input("Enter the template image: ")
destination = raw_input("Enter the destination image: ")

tempImg = Image.open(template)
destImg = Image.open(destination)

tempWidth,tempHeight = tempImg.size
destWidth,destHeight = destImg.size

if tempWidth >= destWidth and tempHeight >= destHeight:
    print "Error! The template image is larger than the destination image."
    sys.exit()
else:
    print "The template image width and height: ",tempWidth,tempHeight
    print "The destination image width and height: ",destWidth,destHeight

x_loc = raw_input("Enter the X coordinate: ")
y_loc = raw_input("Enter the Y coordinate: ")

x_loc = int(x_loc)
y_loc = int(y_loc)

tempImg = tempImg.convert("RGBA")
destImg = destImg.convert("RGBA")
img = tempImg.load()

for x in range(tempWidth):
    for y in range(tempHeight):
        if img[x,y][1] > img[x,y][0] + img[x,y][2]: #<-- removes green border from image
            img[x,y] = (255,255,255,0)
        else:
            destImg.putpixel((x+x_loc,y+y_loc),tempImg.getpixel((x,y)))

destImg.show()

rawDescription: Python Image Processing - Move an Image

description: Python Image Processing - Move an Image


code:
driver = MyFirefox()
print driver.session_id
driver.quit()
print driver.session_id

rawDescription: Python Selenium: How to check whether the WebDriver did quit()?


code:
no = int (raw_input("Enter a number: "))
space_no = no - 2
print_line = "*"*no
for i in xrange(1,no+1):
    if space_no>0:
        print_line_n = "*"*i+" "*space_no+"*"*i
        space_no -=2
        print print_line_n
    else:
        print print_line

rawDescription: Draw an M-shaped pattern with nested loops


code:
s = 't\u028c\u02c8m\u0251\u0279o\u028a\u032f'  # your string has a unicode-escape encoding but is not unicode
x = unicode(s, encoding='unicode-escape')
print x
x

rawDescription: Decode unicode string in python

description: Decode unicode string in python


code:
def check(n):
    if int(n) != n:
        print("int(n) != n")
    else:
        print("int(n) == n")

def check2(n):
    if not isinstance(n, int):
        print("n is not an int")
    else:
        print("n is an int")

class myInt(int):
    pass

check(1)
check("1")
#check({})      #Will throw error
#check([])      #Will throw error
check(myInt())
print ""
check2(1)
check2("1")
check2({})
check2(myInt())
check2([])

rawDescription: python isinstance(n, int) and int(n) == n

description: Python isinstance(n, int) and int(n) == n


code:
def method1():
  out_str = ''
  for num in xrange(loop_count):
    out_str += `num`
  return out_str

rawDescription: How slow is Python's string concatenation vs. str.join?


code:
import time
import os

class Watcher(object):
    def __init__(self):
        self.dirs = []
        self.snapshots = {}

    def add_dir(self, dir):
        self.dirs.append(dir)

    def check_for_changes(self, dir):
        snapshot = self.snapshots.get(dir)
        curstate = os.listdir(dir)
        if not snapshot:
            self.snapshots[dir] = curstate
        else:
            if not snapshot == curstate:
                print 'Changes: ',
                for change in set(curstate).symmetric_difference(set(snapshot)):
                    if os.path.isdir(change):
                        print "isdir"
                        self.add_dir(change)
                    print change,

                self.snapshots[dir] = curstate
                print

    def mainloop(self):
        if len(self.dirs) < 1:
            print "ERROR: Please add a directory with add_dir()"
            return

        while True:
            for dir in self.dirs:
                self.check_for_changes(dir)
            time.sleep(4) # Don't want to be a resource hog

w = Watcher()
w.add_dir('.')

w.mainloop()

rawDescription: PyGTK/GIO: monitor directory for changes recursively


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


rawDescription: Find lists which together contain all values from 0-23 in list of lists python


code:
def named_property(func):
    return property(lambda *a, **kwa: func(*a, fname=func.__name__, **kwa))

class A(object):
    @named_property
    def b(self, fname):
        print fname
A().b  # will print 'b'

rawDescription: Getting the name of the @property being accessed, set or deleted in Python


code:
import os
print os.environ['HOME']

# using get will return `None` if a key is not present rather than raise a `KeyError`
print os.environ.get('KEY_THAT_MIGHT_EXIST')

# os.getenv is equivalent, and can also give a default value instead of `None`
print os.getenv('KEY_THAT_MIGHT_EXIST', default_value)

rawDescription: Access environment variables from Python


code:
import pygame

def checkifComplete(channel):
    while channel.get_busy():  #Check if Channel is busy
        pygame.time.wait(800)  #  wait in ms
    channel.stop()             #Stop channel

if __name__ == "__main__":

    music_file1 = "sounds/audio1.wav"
    music_file2 = "sounds/audio2.wav"

    #set up the mixer
    freq = 44100     # audio CD quality
    bitsize = -16    # unsigned 16 bit
    channels = 2     # 1 is mono, 2 is stereo
    buffer = 2048    # number of samples (experiment to get right sound)
    pygame.mixer.init(freq, bitsize, channels, buffer)

    pygame.mixer.init() #Initialize Mixer

    #Create sound object for each Audio
    myAudio1 = pygame.mixer.Sound(music_file1)
    myAudio2 = pygame.mixer.Sound(music_file2)

    #Create a Channel for each Audio
    myChannel1 = pygame.mixer.Channel(1)
    myChannel2 = pygame.mixer.Channel(2)

    #Add Audio to  first channel
    myAudio1.set_volume(0.8) # Reduce volume of first audio to 80%
    print "Playing audio : ", music_file1
    myChannel1.play(myAudio1)
    checkifComplete(myChannel1) #Check if Audio1 complete

    #Add Audio to second channel
    myAudio2.set_volume(0.2)    # Reduce volume of first audio to 20%
    print "Playing audio : ", music_file2
    myChannel2.play(myAudio2)
    checkifComplete(myChannel2)

rawDescription: Multiple audio files running on raspberry


code: for k,v in top: print k, v

rawDescription: Top Counts...return tuple format of output back to dictionary format


code:
import subprocess

try:
    output = subprocess.check_output(["Matlab.exe", "/home/user/docs/myapp.mat"])
    # If it doesn't fail, the exitcode equals to 0.
    print output # Prints the output.
except subprocess.CalledProcessError as e:
    print e.returncode # Prints the exitcode.

rawDescription: (Python) Launching an external application and wait for the end of it


code:
import re

input='''\
businessman video demeanor demeanour dishonest acknowledge dvd honor sister opportunity keen labour artistic favourite red definition impatient take behaviour warmth'''

matches=re.findall(r'(demeanour|demeanor)', input)
print matches, len(matches)
# ['demeanor', 'demeanour'] 2

rawDescription: regex is counting only one pattern, when two same patterns are kept consecutive.why?

description: Regex is counting only one pattern, when two same patterns are kept consecutive.why


code:
#!/usr/bin/python

import re

text = "a='one' b='2' c='3' a='two' b='8' c='3'"
pattern = r"a='(.*?)'"

for m in re.findall( pattern, text ):
  print m

rawDescription: How to search and export value with python and re?

description: Search and export value with python and re


code:
bool
bool(1)
bool(1) is bool('true')
True = False
True
True is False
False is bool()
True = bool(1)
True is bool(1)
True is False
True is bool()
bool()
True is bool(2)
True is bool('true')

rawDescription: In Python how to recover from joke statements like True = False


code:
param1 = range(2, 6)
param2 = range(12, 15)
print param1
print param2
print "param1 + param2: %s" % ''.join(str(param1 + param2))

rawDescription: why are layer parameters added for multilayer perceptron example at deeplearning.net

description: Why are layer parameters added for multilayer perceptron example at deeplearning.net


code:
f1_name = "text1.csv"
f2_name = "text2.csv"
f1 = open(f1_name,'r').readlines()
f2 = open(f2_name,'r').readlines()

count1 = 0
for line in f1:
    count1+=1
    if line != "\n" and line in f2:
        print "Line{0} in {1} found in Line{2} in {3} ({4})".format(
            str(count1),
            f1_name,
            str(1+f2.index(line)),
            f2_name,
            line.strip())
f1.close()
f2.close()

rawDescription: Python efficient way of comparing 2 csv files


rawDescription: Changing values in a function, but not able to print them outside of the function


code:
hexdump = """\
. a . v a l i d . s t r i n g .
. a n o t h e r . s t r i n g .
. e t c . . . . . . . . . . . .
l i n e . w . a .   s p a c e .
. e t c . . . . . . . . . . . .
"""

from pyparsing import oneOf, printables, delimitedList, White, LineEnd

# expression for a single char or space
dumpchar = oneOf(list(printables)+[' ']).leaveWhitespace()

# convert '.'s to something else, if you like; in this example, '_'
dumpchar.setParseAction(lambda t:'_' if t[0]=='.' else None)

# expression for a whole line of dump chars - intervening spaces will
# be discarded by delimitedList
dumpline = delimitedList(dumpchar, delim=White(' ',exact=1)) + LineEnd().suppress()

# if you want the intervening spaces, use this form instead
#dumpline = delimitedList(dumpchar, delim=White(' ',exact=1), combine=True) + LineEnd().suppress()

# read dumped lines from hexdump
for t in dumpline.searchString(hexdump):
    print ''.join(t)

rawDescription: Pyparsing: space as a valid token

description: Pyparsing: space as a valid token


code:
import a
from c import MyGlobals

def func1():
    MyGlobals.x = 1

if __name__ == "__main__":
    print MyGlobals.x
    func1()
    print MyGlobals.x
    a.func2()
    print MyGlobals.x

rawDescription: make variable global to multiple files in python

description: Make variable global to multiple files in python


code:
erg = {}
allstrings = ['str1','str2','str3','str4']
userkeywords = set(['teststring','awesome','test','string'])
for strs in allstrings:
    strkeywords = params[strs].split()
    #len() works on sets too, no need to convert this to a list
    common = len( userkeywords & set(strkeywords) )
    erg[str] = common

for k in sorted(erg, key=erg.get, reverse=True):
    print k, ':', erg[k]

rawDescription: Python get name in loop

description: Python get name in loop


code: print lines[4]

rawDescription: getting data out of a txt file

description: Getting data out of a txt file


code:
import json

json_data=open('regex.json')
data = json.load(json_data)

for label, regex in data.items():
    print label
    print regex # process your regex here instead print

rawDescription: Execute regex located in an external file in python


code:
class _CharField( ):
   patterns_field = {"max":-1}
   def __init__( self ):
       #remember this calls with self as the child class
       self.max_length1 = self.patterns_field[ 'max' ]
       #or for equivelent of EmailField.patterns_field (assuming you dont know EmailField) refer to self.__class__
       self.max_length2 = self.__class__.patterns_field[ 'max' ]
       #self.__class__ is the Class of the caller(in this case the child)'

class EmailField(_CharField):
    patterns_field = {'max':16}

class NameField(_CharField):
    patterns_field = {'max':12}

c  = _CharField()
print c.max_length1 # prints -1
print c.max_length2 # prints -1
e = EmailField()
print e.max_length1 #prints 16
print e.max_length2 #prints 16

n=NameField()
print n.max_length1 #prints 12
print n.max_length2 #prints 12

rawDescription: How to access a 'static' field of a child class in a base class?

description: Access a 'static' field of a child class in a base class


code:
if x == 0:
  pass
else:
  print "x not equal 0"

rawDescription: What's a standard way to do a no-op in python?


code:
query = datastore.Query(Project._meta.db_table)
for p in query.Run():
    p['id'] = c.key().id() #Convert from app engine key
    print Project.from_json(p)

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


code:
import grequests
from bs4 import BeautifulSoup

def get_urls_from_response(r):
    soup = BeautifulSoup(r.text)
    urls = [link.get('href') for link in soup.find_all('a')]
    return urls

def print_url(args):
    print args['url']

def recursive_urls(urls):
    """
    Given a list of starting urls, recursively finds all descendant urls
    recursively
    """
    while True:
        if len(urls) == 0:
            break
        rs = [grequests.get(url, hooks=dict(args=print_url)) for url in urls]
        responses = grequests.map(rs)
        url_lists = [get_urls_from_response(response) for response in responses]
        urls = sum(url_lists, [])  # flatten list of lists into a list

if __name__ == "__main__":
    recursive_urls(["INITIAL_URLS"])

rawDescription: How to use python-requests and event hooks to write a web crawler with a callback function?

description: Use python-requests and event hooks to write a web crawler with a callback function


code:
for i in range(3):
    print "Your text here"

rawDescription: Python, Printing multiple times,

description: Python, Printing multiple times,


code:
import cherrypy

class Root(object):
    @cherrypy.expose
    def default(self, **kwargs):
        print kwargs
        return '''<form action="" method="POST">
Host Availability:
<input type="checkbox" name="goal" value="cpu" /> CPU idle
<input type="checkbox" name="goal" value="lighttpd" /> Lighttpd Service
<input type="checkbox" name="goal" value="mysql" /> Mysql Service
<input type="submit">
</form>'''

cherrypy.quickstart(Root())

rawDescription: CherryPy - saving checkboxes selection to variables


code:
import xml.etree.ElementTree as ET
tree = ET.parse('a.xml')
root = tree.getroot()
print root
dic={}
for i in root.iter('TeamData'):
    dic[i.attrib['Ref']]=[j.text for j in i]
print dic
#output {'1': ['a', 'b', 'c', 'd'], '2': ['e', 'f', 'g', 'h']}

rawDescription: Parsing Xml with element tree in Python, same tag name


code:
import textwrap
from os.path import join

from lxml import etree

# string to Element
tree = etree.XML(textwrap.dedent('''
  <foo_tag>
    foo text
    <bar_tag some_attr='ok'>bar text</bar_tag>
  </foo_tag>
  '''))
print 'root text: ' , tree.text
print 'pretty_print: '
print etree.tostring(tree, pretty_print=True)
print 'last child: (%s) (%s)' % (tree[-1].tag, tree[-1].text)
print

# filename to ElementTree
tree = etree.parse('some_file.xhtml')

def recurse(root, depth):
  line = '  ' * depth + root.tag + ' {%s}' % ', '.join(root.attrib.keys())
  if root.text:
    line += ' <%s>' % root.text.strip()
  print line
  for child in root:
    recurse(child, depth + 1)
print 'recurse tree:'
recurse(tree.getroot(), 0)
print

print 'find title: ', tree.findtext('html/head/title')
print 'find title again: ', tree.find('html').find('head').find('title').text

rawDescription: Preferred Python XML processor

description: Preferred Python XML processor


code:
nss = extractNavigableStrings(soup)
strings = [unicode(ns.string) for ns in nss]
print strings  # [u'TEXT1 ', u'TEXT2 ', u'TEXT3 ']

for s in strings: print type(s), s
# <type 'unicode'> TEXT1
# <type 'unicode'> TEXT2
# <type 'unicode'> TEXT3

rawDescription: How to scrape between span tags using beautifulsoup

description: Scrape between span tags using beautifulsoup


code:
#!/usr/bin/env python

import json

foo = json.loads('{"123": null, "234": null, "456": null}')
print map(int, foo.keys())

rawDescription: Python get JSON object by index

description: Python get JSON object by index


code:
test_list=['x10','x50','x100']
test_list= sorted(test_list,key = lambda x: len(x), reverse = True)
text='this text has x100n only'
import re
a = re.search("|".join(test_list),text)
if a:
    print "Found",a.group()

rawDescription: python to find sub string inside string

description: Python to find sub string inside string


code:
with open('spectral_data.csv', 'wb') as f:
    writer = csv.DictWriter(f, ('name', 'data'))

    while True:
        name = raw_input("Pigment name [DONE to finish]: ")
        # ....

        print pigment_data

        writer.writerow(data[-1])

rawDescription: Python 2.7 exporting data in a .csv

description: Python 2.7 exporting data in a .csv


code:
import glob
fnames = glob.glob('/opt/rubedo/log/riskserver.'+nowFormat+'*')
print fnames

rawDescription: Wildcard or * for matching a datetime python 2.7


code:
listm = soup.findAll('td',{'class':'thumb'})
for elem in listm:
    print elem('img')[0]['alt']

rawDescription: BeautifulSoup to find text within <img alt tag


rawDescription: Printing Unicode Char inside a List

description: Printing Unicode Char inside a List


code:
a = 1
b = a
a = 2
print b

rawDescription: Pointers in Python on variables with None value


code:
for node in tree.findall('.//{XYZ}grandParent'):
    print node

rawDescription: updating XML attribute value in python

description: Updating XML attribute value in python


code: print sum(dictionary[word] for word in words.split() if word in dictionary)

rawDescription: Keeping values from an if statement

description: Keeping values from an if statement


code:
for words in cleaned_example:
    yes = 0
    no = 0
    for word1, word2 in zip(words, words[1:]):
        if sentiment(word1)[0] < sentiment(word2)[0]:
            yes = yes + 1
        else:
            no = no + 1
    if yes > no:
        print "yes"
    elif yes < no:
        print "no"
    else:
        print "`yes` = `no`"

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

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


code:
import csv
from cStringIO import StringIO

f = StringIO("""item    value   need
key1    153908482   ctaggaacca
key2    115057116   gtaattctga
key3    133381371   ctgaaaagat
key1    143543  atccgatcgg
key2    212321333   accccgta
""")

d = {}
#with open("file.txt", 'r') as f:

data = csv.DictReader(f, delimiter="\t")
for row in data:
#    item = d.get(row["value"], dict())
    item = d.get(row["item"], dict())
    item[row["value"]] = row["need"]
    d[row["item"]] = item

print d

rawDescription: Appending a nested dictionary for values with same key


code:
import xml.etree.ElementTree as ET
def loadfile():
    tree = ET.parse('filename')
    root = tree.getroot()

    for envelope in root.iter('{http://www.opengis.net/gml}Envelope'):
        print "ENV tag", envelope.tag
        print "ENV attrib", envelope.attrib
        print "ENV text", envelope.text

        lCorner = envelope.find('{http://www.opengis.net/gml}lowerCorner').text
        uCorner = envelope.find('{http://www.opengis.net/gml}upperCorner').text

        print "lC",lCorner
        print "uC",uCorner

if __name__== "__main__":
    loadfile()

rawDescription: no results when parsing CityGML in Python

description: No results when parsing CityGML in Python


code:
import zipfile

for filename in [ 'README.txt', 'example.zip',
                  'bad_example.zip', 'notthere.zip' ]:
    print '%20s  %s' % (filename, zipfile.is_zipfile(filename))

rawDescription: Check a .zip archive integrity/filepaths/compression methods from python?


code:
import urlparse
url = urlparse.urlparse('http://localhost:8087/moderate?5649364211118945661=on&23984729386481734=on&456287432349725=on&6753847523429875=off')
query = urlparse.parse_qs(url.query)
print [key for key, value in query.iteritems() if value == 'on']

rawDescription: Getting the Parameter name of a Value in a URL request


code:
import re

names = []

with open('grades.txt', 'r') as f_input:
    for line in f_input:
        if len(line) > 1:
            names.append(re.match(r'(.*?):\s*?(\d+)\s*?\/\s*?(\d+)', line).groups())

for name, mark, total in sorted(names, key=lambda x: float(x[1]) / float(x[2]), reverse=True):
    print "{} - {} out of {}".format(name, mark, total)

rawDescription: Sorting lines by the second word on each line of text file, then displaying it


code:
import ctypes

class FFITuple(ctypes.Structure):
    _fields_ = [("a", ctypes.c_uint32),
                ("b", ctypes.c_uint32)]

class FFIArray(ctypes.Structure):
    _fields_ = [("data", ctypes.c_void_p),
                ("len", ctypes.c_size_t)]

    # Allow implicit conversions from a sequence of 32-bit unsigned
    # integers.
    @classmethod
    def from_param(cls, seq):
        return cls(seq)

    # Wrap sequence of values. You can specify another type besides a
    # 32-bit unsigned integer.
    def __init__(self, seq, data_type = ctypes.c_uint32):
        array_type = data_type * len(seq)
        raw_seq = array_type(*seq)
        self.data = ctypes.cast(raw_seq, ctypes.c_void_p)
        self.len = len(seq)

# A conversion function that cleans up the result value to make it
# nicer to consume.
def void_array_to_tuple_list(array, _func, _args):
    tuple_array = ctypes.cast(array.data, ctypes.POINTER(FFITuple))
    return [tuple_array[i] for i in range(0, array.len)]

lib = ctypes.cdll.LoadLibrary("./target/debug/libtupleffi.dylib")

lib.convert_vec.argtypes = (FFIArray, FFIArray)
lib.convert_vec.restype = FFIArray
lib.convert_vec.errcheck = void_array_to_tuple_list

for tupl in lib.convert_vec([1,2,3], [9,8,7]):
    print tupl.a, tupl.b

rawDescription: Convert Rust vector of tuples to a C compatible structure


rawDescription: re.split on multiple characters (and maintaining the characters) produces a list containing also empty strings

description: Re.split on multiple characters (and maintaining the characters) produces a list containing also empty strings


code:
for a_tag in results:
    print a_tag.text_content() # prints bthe whol string: "This is a sentence with some italic text-formatting I want to parse."

rawDescription: Correct xpath syntax with pythons lxml library for parsing all the text from arbitrary nested html tags


code:
class Grid:

    def __init__(self):
        self.list = [[1,2], [3,4]]

    def __getitem__(self, index):
        return self.list[index]

g = Grid();

print g[0]
print g[1]
print g[0][1]

rawDescription: Python overloading multiple getitems / index requests


code:
import inspect

def decorate(f):
    def detect(*args, **kwargs):
        try:
            members = inspect.getmembers(args[0])
            members = (x[1].im_func for x in members if 'im_func' in dir(x[1]))
            ismethod = detect in members
        except:
            ismethod = False
        print ismethod

        return f(*args, **kwargs)
    return detect

@decorate
def foo():
    pass

class bar(object):
    @decorate
    def baz(self):
        pass

foo() # prints False
bar().baz() # prints True

rawDescription: Decorator changing function status from method to function


code:
soup=BeautifulSoup.BeautifulSoup(content).findAll('name')
for field in soup:
    print field

rawDescription: How to access a tag called "name" in BeautifulSoup

description: Access a tag called "name" in BeautifulSoup


code:
from django.template import Template, Context
from django.template.engine import Engine

from django.conf import settings
settings.configure(DEBUG=False)

template_string = """
{% for i in the_list %}
  {% if forloop.counter|divisibleby:3 %}3{%else%}0{%endif%}\n
{% endfor %}
"""
print Template(template_string).render(Context({"the_list":[1,2,3,4,5,6,7]}))

rawDescription: How to check whether a number is divisible by another in Jinja Template (Django Framework)

description: Check whether a number is divisible by another in Jinja Template (Django Framework)


code: rdd1.zip(rdd2).map(lambda ((x1, x2), y): (x1, x2, y))

rawDescription: How to flatten tuple created using zip transformation in PySpark

description: Flatten tuple created using zip transformation in PySpark


code:
import requests as rq
from lxml import html

teamstate = ["wsh","phi","nym","mia","atl"]
teamnamelist = ["washington-nationals","philadelphia-phillies","new-york-mets","miami-marlins","atlanta-braves"]
teamlist = ["Washington Nationals","Philadelphia Phillies","New York Mets","Miami Marlins","Atlanta Braves"]

for x, y, z in zip(teamstate, teamnamelist, teamlist):
    url = "http://espn.go.com/mlb/team/_/name/%s/%s" % (x, y)
    r = rq.get(url)
    tree = html.fromstring(r.text)
    val = tree.xpath("//span[@class='stat']/text()")[1]
    print "The batting average of the %s is %s." % (z, str(val))

rawDescription: Python producing two instances of data set


code:
from yum import config, YumBase

config.RepoConf.foo = config.Option()

yum = YumBase()
for repo in yum.repos.listEnabled():
    print repo.id, repo.foo

rawDescription: Yum : How to retrieve option value from repositories configuration using python


code:
a = np.arange(16).reshape((8,2))
b = [2, 6, 7]
mask = np.ones(len(a), dtype=bool)
mask[b,] = False
x, y = a[b], a[mask] # instead of a[b] you could also do a[~mask]
print x
array([[ 4,  5],
       [12, 13],
       [14, 15]])
print y
array([[ 0,  1],
       [ 2,  3],
       [ 6,  7],
       [ 8,  9],
       [10, 11]])

rawDescription: How do I split an ndarray based on array of indexes?

description: Split an ndarray based on array of indexes


code:
from Tkinter import *

main = Tk()

def leftKey(event):
    print "Left key pressed"

def rightKey(event):
    print "Right key pressed"

frame = Frame(main, width=100, height=100)
main.bind('<Left>', leftKey)
main.bind('<Right>', rightKey)
frame.pack()
main.mainloop()

rawDescription: Tkinter - Can't bind arrow key events

description: Tkinter - Can't bind arrow key events


code:
from urllib import urlopen
from bs4 import BeautifulSoup

def crawl(url, seen, to_crawl):
    html = urlopen(url).read()
    soup = BeautifulSoup(html)
    seen.append(url)
    for tag in soup.findAll('a', href=True):
        str = tag['href']
        if 'http' in str:
            if url not in seen and url not in to_crawl:
                to_crawl.append(str)
                print tag['href']
                print "--------------"
    crawl(to_crawl.pop(), seen, to_crawl)

def main ():
    print "$ = " , crawl("http://news.google.ca", [], [])

if __name__ == "__main__":
    main()

rawDescription: Why is this recursion stopping

description: Why is this recursion stopping


code:
import sys
import pydoc

plainSysDoc = pydoc.plain((pydoc.render_doc(sys)))
print plainSysDoc

rawDescription: How to implement man-like help page in python(python shell already has it)

description: Implement man-like help page in python(python shell already has it)


code:
for s, e in perdelta(date(2011, 10, 10), date(2011, 11, 10), timedelta(days=3)):
    print s, e

rawDescription: How to create Python Generator that iterates through date range giving me 3 day segments?

description: Create Python Generator that iterates through date range giving me 3 day segments


code:
import stomp
import sys
from amqplib import client_0_8 as amqp
#read in the exchange name so I can set up multiple recievers for different exchanges to tset
exchange = sys.argv[1]
conn = amqp.Connection(host="localhost:5672", userid="username", password="password",
 virtual_host="/", insist=False)

chan = conn.channel()

chan.access_request('/', active=True, write=True, read=True)

#declare my exchange
chan.exchange_declare(exchange, 'topic')
#not passing a queue name means I get a new unique one back
qname,_,_ = chan.queue_declare()
#bind the queue to the exchange
chan.queue_bind(qname, exchange=exchange)

class MyListener(object):
    def on_error(self, headers, message):
        print 'recieved an error %s' % message

    def on_message(self, headers, message):
        print 'recieved a message %s' % message

conn = stomp.Connection([('0.0.0.0', 61613), ('127.0.0.1', 61613)], 'browser', 'browser')
conn.set_listener('', MyListener())
conn.start()
conn.connect(username="username", password="password")
headers = {}

#subscribe to the queue
conn.subscribe(destination=qname, ack='auto')

while True:
    pass
conn.disconnect()

rawDescription: Stomp Broadcast with Rabbitmq and Python


code:
import PyV8
ctx = PyV8.JSContext()
ctx.enter()

js = """
function escramble_758(){
var a,b,c
a='+1 '
b='84-'
a+='425-'
b+='7450'
c='9'
document.write(a+c+b)
}
escramble_758()
"""

print ctx.eval(js.replace("document.write", "return "))

rawDescription: Executing Javascript from Python

description: Executing Javascript from Python


code:
from Tkinter import *

def toggle_continue():
    if accept.get():
        button1.config(state=NORMAL)        # user agrees, enable button
    else:
        button1.config(state=DISABLED)

def cmd_cancel():
    print "User clicked cancel button"

def cmd_continue():
    print "User clicked continue button"

terms_and_conditions = '''Here are our long and unreadable terms and conditions.

We know that you won't read them properly, if at all, so just click the
"I accept" checkbox below, then click the 'Continue' button.'''

root = Tk()
frame = Frame(root)
label = Label(frame , text='Terms and conditions')

textbox = Text(frame)
textbox.insert(INSERT, terms_and_conditions)
textbox.config(padx=15, pady=15, state=DISABLED)

accept = IntVar()
accept_cb = Checkbutton(frame, text="I accept the terms and conditions",
                        variable=accept, command=toggle_continue)

button1 = Button(frame , text='Continue', command=cmd_continue, state=DISABLED)
button2 = Button(frame , text='Cancel', command=cmd_cancel)

frame.pack()
label.pack( padx=20 , pady=5 )
textbox.pack()
accept_cb.pack()
button1.pack( padx=10 )
button2.pack( padx=10 )
root.mainloop()

rawDescription: How to change contents of a window depending on user input?

description: Change contents of a window depending on user input


code:
from nltk.metrics import ConfusionMatrix
ref  = 'DET NN VB DET JJ NN NN IN DET NN'.split()
tagged = 'DET VB VB DET NN NN NN IN DET NN'.split()
cm = ConfusionMatrix(ref, tagged)
print cm

rawDescription: How to use the confusion matrix module in NLTK? - python

description: Use the confusion matrix module in NLTK? - python


code:
import operator

vector1 = (1, 2, 3)

# get a list of vectors
vectors = [
    (4, 5, 6),
    (7, 8, 9)
]

# for loop through the vectors,
# assignig the current vector to vector2 in every iteration
for vector2 in vectors:
    dotProduct = reduce(operator.add, map(operator.mul, vector1, vector2))
    print dotProduct

rawDescription: How to apply parameters/for loop

description: Apply parameters/for loop


code:
s = "hello, world"
s = s[::-1]
print s  # prints "dlrow ,olleh"

rawDescription: String reversal in Python

description: String reversal in Python


code: print ''.join(map(lambda c:'\\x%02x'% ord(c), a))

rawDescription: Python - string of binary escape sequences as literal


code:
def make_url(ticker_symbol,start_date, end_date):
    print ticker_symbol
    a = start_date
    b = end_date
    dt_url = '%s&a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&g=d&ignore=.csv'% (ticker_symbol, a.month-1, a.day, a.year, b.month-1, b.day,b.year)
    return base_url + dt_url

rawDescription: Get data for specific date range from yahoo finance api via python


code:
import re
samples = '''\
Callable {option-1} {option-2} {option-3} {option-n}
Callable
Callable {option-1}
'''
for line in samples.splitlines():
    print re.findall(r'{([^}]+)}', line)

rawDescription: Python regex parsing string containing braced items


code:
ss = '''    background: #f09 url('images#06F');
    background=#00f url('images #889');
    background:#E4aaa0 url('images#890');
    background: #fff #dddddd; '''

print ss
import re

three = '(?:[0-9A-Fa-f]{3})'

regx = re.compile('^ *background[ =:]*#(%s{1,2})' % three,re.MULTILINE)
print regx.findall(ss)

print '-----------------------------------------------------'

regx = re.compile('(?:(?:^ *background[ =:]*)|(?:(?<=#%s)|(?<=#%s%s)) +)'
                  '#(%s{1,2})' % (three,three,three,three),
                  re.MULTILINE)
print regx.findall(ss)

rawDescription: How to use a negative lookbehind

description: Use a negative lookbehind


code:
for mathched_dict in (cdict for cdict in my_list if cdict["task"] == key):
    print mathched_dict

rawDescription: Python: Find dict in array based on value of key


code:
g = lambda alist: sum(alist)
print g([4,6,7])

rawDescription: Python lambda parameters

description: Python lambda parameters


code:
import pyparsing as par

alphanumsword = par.Word(par.alphanums + "_")

element = "(" + alphanumsword + ")" | alphanumsword
#element = alphanumsword + "(" + alphanumsword + ")" | alphanumsword

row = par.OneOrMore(element)

rows = row + par.OneOrMore("," + row) | row

semicolon = par.Literal(";")

comment1 = par.Literal("--") + par.restOfLine + par.LineEnd()

createtable = par.CaselessLiteral("create") + par.CaselessLiteral("table")  \
              + alphanumsword + "("     + rows + ")" + ~( semicolon | comment1 )

createtable.ignore(comment1)
createtable.ignore(par.cStyleComment)

text = \
"""
--- this is comment

CREATE TABLE  PERSON
(
    /* Comment */
    /*
       Comment
    */
   ID VARCHAR(255),
   NAME VARCHAR(255), -- Comment
   --- Comment
   ADDRESS VARCHAR(255) NULL, -- Comment
   CONSTRAINT PK_PERSON PRIMARY KEY (ID)
)

-- Comment

CREATE TABLE A ( B VARCHAR(255) ) --comment
CREATE TABLE A ( B VARCHAR(255) );
CREATE TABLE A ( B VARCHAR(255) )
CREATE TABLE A ( B VARCHAR(255) );

--Commentasfkjaskfj
"""

text_list = list(text)
offset = 0

for t,s,e in createtable.scanString(text):
    print "(***", t, ",", s, ",", e, "**)"

    print "||\n", text[s:e], "\n||"
    print "s: %d, e: %d" % (s, e)

    text_list.insert(e + offset , ';')
    offset += 1

    print "******************************"

print "".join(text_list)

rawDescription: Correct comment scanning with pyparsing

description: Correct comment scanning with pyparsing


code:
def main():
    array = ["susan", "mary", "david", "tom", "richard", "mark"]

    places_moved = int(input("How many places would you like to move?\n" + \
                             "(positive is to the right) "))

    if places_moved >= 0:
        for move in range(places_moved):
            array = [array[-1]] + array[0:-1]
    else:
        for move in range(abs(places_moved)):
            array = array[1:] + [array[0]]

    print array

    return 0

if __name__ == '__main__':
    main()

rawDescription: Python 3.3.4. How to swap values within an array


code:
word = 'holiday'
print change(word)

rawDescription: concatenating strings?

description: Concatenating strings


code:
blacklist = ["olive leaf plucked", "during mating searson"]
finder3.apply_ngram_filter(lambda w1, w2, w3: " ".join([w1,w2,w3]) in blacklist)

print finder3.nbest(trigram_measures.pmi, 10)

rawDescription: finder.apply_ngram_filter in nltk.collocations to classify some ngrams

description: Finder.apply_ngram_filter in nltk.collocations to classify some ngrams


code:
class employee:
    pass

# Some multiline-function.
def whatever(self, a, b):
    c = a + b
    return c

e = employee()

# Make `whatever` an `employee` method with name "add".
setattr(employee, "add", whatever)
print e.add(2, 9)

rawDescription: Python: Dynamically adding function to a class, whose name is contained in a string


code:
result = svm.predict_all(testData)
print result

rawDescription: What features of images will produce good result used in SVM multiclass image classification?


code:
from multiprocessing import Pool
import datetime

def f(x):  # You thumbnail maker function, probably using some module like PIL
    print '%-4d: Started at %s' % (x, datetime.datetime.now())
    return x*x

if __name__ == '__main__':
    pool = Pool(processes=4)              # start 4 worker processes
    print pool.map(f, range(25))          # prints "[0, 1, 4,..., 81]"

rawDescription: Multithreaded thumbnail generation in Python


code:
#do some stuff
print 'tasks done, now sleeping for 10 seconds'
for i in xrange(10,0,-1):
    time.sleep(1)
    print i

rawDescription: Display a countdown for the python sleep function


code:
import sys
import urllib2

from PyQt4 import QtCore, QtGui

class DownloadThread(QtCore.QThread):

    data_downloaded = QtCore.pyqtSignal(object)

    def __init__(self, url):
        QtCore.QThread.__init__(self)
        self.url = url

    def run(self):
        info = urllib2.urlopen(self.url).info()
        self.data_downloaded.emit('%s\n%s' % (self.url, info))

class MainWindow(QtGui.QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.list_widget = QtGui.QListWidget()
        self.button = QtGui.QPushButton("Start")
        self.button.clicked.connect(self.start_download)
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.button)
        layout.addWidget(self.list_widget)
        self.setLayout(layout)

    def start_download(self):
        urls = ['http://google.com', 'http://twitter.com', 'http://yandex.ru',
                'http://stackoverflow.com/', 'http://www.youtube.com/']
        self.threads = []
        for url in urls:
            downloader = DownloadThread(url)
            downloader.data_downloaded.connect(self.on_data_ready)
            self.threads.append(downloader)
            downloader.start()

    def on_data_ready(self, data):
        print data
        self.list_widget.addItem(unicode(data))

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.resize(640, 480)
    window.show()
    sys.exit(app.exec_())

rawDescription: Updating GUI elements in MultiThreaded PyQT


code:
for acid in acids:
    print acid

rawDescription: How can I do the following comparison without having to write 20 if-statements or making 20 lists/dictionaries?

description: Do the following comparison without having to write 20 if-statements or making 20 lists/dictionaries


code:
re = numpy.zeros((a**l, a**l, a**l))
func(re, ws, a, l, r)

re2 = numpy.zeros((a**l, a**l, a**l))
func2(re2, ws, a, l, r)

re3 = numpy.zeros((a**l, a**l, a**l))
func3(re3, ws, a, l, r)

print np.allclose(re, re2)  # True
print np.allclose(re, re3)  # True

rawDescription: Performance issue in python with nested loop


code:
strID = raw_input("Username : ")
strPassWd = getpass.getpass("Password : ")

loggedIn = False

with open('studentsusers.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',')

    for row in spamreader:
        strLoginID, strLoginPasswd = row

        while strLoginID != "":
            if strID == strLoginID and strLoginPasswd == strPassWd:
                print strID, "Logged In"
                loggedIn = True
                return
            else:
                break   # goes on to the next row in spamreader

if loggedIn:
    PupilMenu()
    strContinue = sys.stdin.readline()
else:
    print "Login ID or password incorrect, press enter to continue"
    strContinue = sys.stdin

rawDescription: Reading pairs of rows from a file

description: Reading pairs of rows from a file


rawDescription: Plotting interpolated values using LinearNDInterpolator (Python)


code:
import pickle
from getpass import getpass

login = raw_input('Enter your username')
password = getpass("Enter your password: ")
if pickle.load(open("%s.p"%login, "rb"))[1] == password:
    username = pickle.load(open("%s.p"%login, "rb"))[0]
    pword = pickle.load(open("%s.p"%login, "rb"))[1]
    money_left = pickle.load(open("%s.p"%login, "rb"))[2]
    print 'You have successfully logged in as %s and you have $%d ingame money.' %(login, money_left)

rawDescription: How do I create an account to store information?

description: Create an account to store information


code:
import merge_in_memory as mim_module

str1 = """line 1
line 2"""
str2 = """line 1
line 2 changed"""

merger = mim_module.Merger()
print merger.diff_make(str1, str2)

rawDescription: How can I diff and patch/merge strings instead of files?

description: Diff and patch/merge strings instead of files


code: print 'Hello, %(your_name)s, my name is %(my_name)s' % {'your_name': 'Blender', 'my_name': 'Ken'}

rawDescription: Regex to replace %variables%

description: Regex to replace %variables%


code:
import pandas as pd

# re-create table1
row_index = pd.MultiIndex.from_tuples([(128, 12345, 'AAA')])
row_index.names=['Index_A', 'Index_B', 'Index_C']
table1 = pd.DataFrame(data={'mean': 123, 'std': 2, 'count': 4}, index=row_index)
table1.columns = pd.MultiIndex.from_tuples(zip(['Assay A'] * 3, table1.columns))
print "*** table 1:"
print table1
print ""

# re-create table2
table2 = pd.DataFrame([{'Col_A': 128, 'Col_B': 12345, 'Col_C': 'AAA', 'mean': 456}], index=[1])
table2.index.name = 'Index'
print "*** table 2:"
print table2
print ""

# re-name columns of table2 to match names of respective index columns in table1
table2 = table2.rename(columns={'Col_A': 'Index_A', 'Col_B': 'Index_B', 'Col_C': 'Index_C'})

# Drop 'Assay A' index level on columns of table1;
# without doing that, the following reset_index() will produce a column multi-index
# for Index_A/B/C, so column names will not match the simple column index of table2_renamed.
# If you need to keep the 'Assay A' level here, you will need to also construct a column
# multi-index for table2_renamed (with empty values for the second level).
table1.columns = table1.columns.levels[1]

# Move index columns of table1 back to regular columns
table1 = table1.reset_index()

# Merge the two tables on the now common column names. 'mean' appears in both tables,
# give the column from table2 a suffix '_2'.
joint = pd.merge(table1.reset_index(), table2, on=['Index_A', 'Index_B', 'Index_C'], suffixes={'', '_2'})
print "*** joint, before re-setting index:"
print joint
print ""

# Restore index of the joint table
joint = joint.set_index(['Index_A', 'Index_B', 'Index_C'])

# Compute the 'result'
joint['result'] = joint['mean'] / joint['mean_2']

# drop unused columns
joint = joint.drop(['index', 'mean_2'], axis=1)

# restore column index level
joint.columns = pd.MultiIndex.from_tuples(zip(['Assay A'] * 4, joint.columns))

print "*** final result:"
print joint
print ""

rawDescription: How do I apply a value from a dataframe based on the value of a multi-index of another dataframe?

description: Apply a value from a dataframe based on the value of a multi-index of another dataframe


code:
content = str(packet) # decoded hex string, such as '\xde\xad\xbe\xef'
print content
for byte in content:
    pass # do something with byte

rawDescription: saving hexdump(packet) to list in scapy

description: Saving hexdump(packet) to list in scapy


code: print feedUpdatedTime == lastUpdatedTime

rawDescription: Can somebody explain why is this datetime comparsion showing output the way it is in python?


code:
from restclient import GET

res = GET("http://google.com", async=True, resp=True)

rawDescription: Python & URLLIB2 - Request webpage but don't wait for response


code:
import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

rawDescription: How do I get a Python program to take in parameters as inputs that come from standard input from the UNIX terminal?

description: Get a Python program to take in parameters as inputs that come from standard input from the UNIX terminal


code:
a = 51
if a<0:
  print "Number is negative."
elif a==0:
  print "Number is Zero."
else:
  print "Number is greater than 0"

rawDescription: How do I sort scores and names saved in a .txt file into alphabetical order with python?

description: Sort scores and names saved in a .txt file into alphabetical order with python


code:
class A(object):
  def __init__(self):
    self.var = "1,2,3"

class B(A):
  def __init__(self):
    super(B,self).__init__()
    self.var += ",4,5"

print A().var
print B().var

rawDescription: "Appending" instead of overriding superclass instance variables


code:
print driver.execute_script("""
var element = arguments[0],
    style = element.currentStyle || window.getComputedStyle(element, false);

return style['background-image'];
""", body)

rawDescription: How to get background-url in Python?

description: Get background-url in Python


code:
def makeArchive(fileList, archive, root):
    """
    'fileList' is a list of file names - full path each name
    'archive' is the file name for the archive with a full path
    """
    a = zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED)

    for f in fileList:
        print "archiving file %s" % (f)
        a.write(f, os.path.relpath(f, root))
    a.close()

rawDescription: Python zip a sub folder and not the entire folder path


code:
from collections import defaultdict

adj_list = defaultdict(lambda: defaultdict(lambda: 0))
for start, end in edges:
    adj_list[start][end] += 1

print adj_list['a']

rawDescription: Adjacency List and Adjacency Matrix in Python


code:
from time import sleep
import sys
print 'L',
sys.stdout.flush()
sleep(0.1)

rawDescription: Python: Print to one line with time delay between prints


rawDescription: Digram list manipulation with Python

description: Digram list manipulation with Python


code:
cp_map = "Valid:\nOP_1\nOP_2\nOP_3\nInvalid:\nOP_4\nOP_5\nUnused:\nOP_6\nOP_7"
one_list = cp_map.split('\n')
sub_list, multi_lists = [], []
for item in one_list:
    if 'unused' in item.lower() or 'valid' in item.lower() or 'invalid' in item.lower():
        if sub_list:
            multi_lists.append(sub_list)
        sub_list = []
    sub_list.append(item)
multi_lists.append(sub_list)
print multi_lists

rawDescription: Generating sub-lists of a string containing keywords in python


code:
class Foo(object):
    def Bar1(self, a, b, c):
        print "Bar1", locals()
    def Bar2(self, a, b, c):
        print "Bar2", locals()

import example
cheese = example.Wine()
cheese.do_something(Foo())

rawDescription: Passing a Python class instance into a C++ function


code:
import __future__, sys
if hasattr(__future__, 'print_function'):
    # Could also check sys.version_info >= __future__. print_function.optional
    import app
    app.main()
else:
    print "instructions for upgrading"

rawDescription: Python graceful future feature (__future__) import


code:
for x in test_list:
    if x.value == value:
        print "i found it!"
        break
else:
    x = None

rawDescription: Find object in list that has attribute equal to some value (that meets any condition)


code:
for i in range(10):
    if i == 5: break;
    print i,
else:
    print "else clause"  # will not execute because of the break

print

for i in range(10):
    print i,
else:
    print "else clause"  # will execute because loop exited normally

rawDescription: Run away array Trying to replace text with vaule in array


code:
def display(l, t = 5):
    length = len(l)
    if   length <= 2:         print " and ".join(l)
    elif length < threshold:  print ", ".join(l[:-1]) + " and " + l[-1]
    elif length == threshold: print ", ".join(l[:-1]) + " and 1 other"
    else: print ", ".join(l[:t-1]) + " and +{} others".format(length - (t - 1))

rawDescription: Python join list of strings with comma but with some conditions - code refractoring


code:
cursor = [(1, u'one'), (2, u'two'), (3, u'three'), (4, u'four')]
for a,b in cursor:
    print a,b

rawDescription: Why do values from SQLite 3 appear with a "u" prefix? How to hide the prefix


code:
L = [([1, 2, 3], 4), ([5, 6, 7], 8)]
element = [5, 6, 7]

for a, b in L:
  if a == element:
    print b
    break
else:
  print "not found"

rawDescription: how to get the index or the element itself of an element found with "if element in list"

description: Get the index or the element itself of an element found with "if element in list"


code:
def ireplace(s, a, b):
    return re.sub("(?i)"+re.escape(a),b,s)
print ireplace(r'c:\Python26\lib\site.py', 'C:\python26', r'image\python26')

rawDescription: How to replace a non-static substring case-insensitively

description: Replace a non-static substring case-insensitively


code:
with open("file1.xml",'r') as data, open("file2.xml",'r') as data2:
    xml = BeautifulSoup(data.read())
    xml2 = BeautifulSoup(data2.read())

    hasAttrBs = xml.document.subdoc.myid
    hasAttrBs2 = xml2.document.subdoc.myid
    print hasAttrBs
    print hasAttrBs2

rawDescription: Test if children tag exists in beautifulsoup


code:
from pprint import pformat

from twisted.internet import reactor
from twisted.internet.defer import Deferred
from twisted.internet.protocol import Protocol
from twisted.web.client import Agent
from twisted.web.http_headers import Headers

class BeginningPrinter(Protocol):
    def __init__(self, finished):
        self.finished = finished
        self.remaining = 1024 * 10

    def dataReceived(self, bytes):
        if self.remaining:
            display = bytes[:self.remaining]
            print 'Some data received:'
            print display
            self.remaining -= len(display)

    def connectionLost(self, reason):
        print 'Finished receiving body:', reason.getErrorMessage()
        self.finished.callback(None)

agent = Agent(reactor)
d = agent.request(
    'GET',
    'http://example.com/',
    Headers({'User-Agent': ['Twisted Web Client Example']}),
    None)

def cbRequest(response):
    print 'Response version:', response.version
    print 'Response code:', response.code
    print 'Response phrase:', response.phrase
    print 'Response headers:'
    print pformat(list(response.headers.getAllRawHeaders()))
    finished = Deferred()
    response.deliverBody(BeginningPrinter(finished))
    return finished
d.addCallback(cbRequest)

def cbShutdown(ignored):
    reactor.stop()
d.addBoth(cbShutdown)

reactor.run()

rawDescription: Twisted: how to know when all the data has been received?


rawDescription: Adding delimeters and removing commas

description: Adding delimeters and removing commas


code:
board = [['0'] for _ in range(5)]
board
print '\n'.join(str(board) for _ in range(5))

rawDescription: Trying to get a nice format output, easy Python


code:
print m; m.shape
print v; v.shape
m  - v

rawDescription: numpy subtract every row of matrix by vector

description: Numpy subtract every row of matrix by vector


rawDescription: Unknown characters fetching stock data from yahoo in csv format


code:
# convert the for loop to a map (still serial)
y = map(doit, x)
y

# build a worker pool for parallel tasks
p = mp.Pool()
# do blocking parallel
y = p.map(doit, x)
y

# use an iterator (non-blocking)
y = p.imap(doit, x)
y
print list(y)
# do asynchronous parallel
y = p.map_async(doit, x)
y
print y.get()
# or if you like for loops, there's always this…
y = p.imap_unordered(doit, x)
z = []
for i in iter(y):
  z.append(i)

z

rawDescription: How to parallelize this piece of code?

description: Parallelize this piece of code


code:
import urllib2

import bs4

def findItem(itemName):
    itemName.replace(" ", "+")
    link = 'http://www.flipkart.com/search/a/all?query= {0}&vertical=all&dd=0&autosuggest[as]=off&autosuggest[as-submittype]=entered&autosuggest[as-grouprank]=0&autosuggest[as-overallrank]=0&autosuggest[orig-query]=&autosuggest[as-shown]=off&Search=%C2%A0&otracker=start&_r=YSWdYULYzr4VBYklfpZRbw--&_l=pMHn9vNCOBi05LKC_PwHFQ--&ref=a2c6fadc-2e24-4412-be6a-ce02c9707310&selmitem=All+Categories'.format(
        itemName)
    r = urllib2.Request(link, headers={"User-Agent": "Python-urlli~"})
    try:
        response = urllib2.urlopen(r)
    except:
        print "Internet connection error"
        return
    thePage = response.read()
    soup = bs4.BeautifulSoup(thePage)

    firstBlockSoup = soup.find('div', attrs={'class': 'product-unit'})
    if not firstBlockSoup:
        firstBlockSoup = soup.find('div', attrs={'class': 'size1of4 fk-medium-atom unit'})
        if not firstBlockSoup:
            print "Item Not Found"
            return

    print "Item found"
    return

findItem("galaxy s advance")
findItem("Giordano Analog Watch")
findItem("nosuchitemfound")

rawDescription: Flipkart.com product scraping using Python


code:
rl = {}
best_range = xrange(0)
for x in s:
    run = rl[x] = rl.get(x-1, 0) + 1
    r = xrange(x-run+1, x+1)
    if len(r) > len(best_range):
        best_range = r
print list(best_range)

rawDescription: In python, how does one efficiently find the largest consecutive set of numbers in a list that are not necessarily adjacent?


code:
from Queue import Queue
q = Queue()
l = []
q.put(d)
while not q.empty():
    d = q.get()
    if d.get("key1") == "value1":
        l.append(d)
        continue
    for k,v in d.items():
        if isinstance(v, dict):
            q.put(v)

print l

rawDescription: Check existence of a key recursively and append to array of dict


code:
import numpy as np

def get_from_csv(fname):
    data = np.loadtxt(fname, dtype=np.object, delimiter=",")
    col = np.array([[ int(fname.rstrip(".txt")) ] * data.shape[0]]).T
    return np.hstack( (col, data) )

files = ["1.txt", "2.txt", "3.txt"]

for f in files:
    try:
        data = np.vstack( (data, get_from_csv(f)) )
    except:
        data = get_from_csv(f)
print data

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

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


code:
import pandas as pd
import numpy as np

a = pd.Series([1, 1, 1, 1, 2, 2])

#for testing len(a) = 6000
a = pd.concat([a]*1000).reset_index(drop=True)

print np.in1d(a,1).sum()
4000
print (a == 1).sum()
4000
print np.sum(a==1)
4000

rawDescription: Pandas, Get count of a single value in a Column of a Dataframe


code:
from itertools import product
for combo in product((None, True, False), repeat=3):
    arguments = {k: v for k, v in zip('abc', combo) if v is not None}
    print arguments

rawDescription: Find all possible combinations of arguments in Python


code:
from itertools import groupby
from operator import itemgetter

mytuple = [('Andrew','10','20'),('Jim',"20",'12'),("Sarah","30",'43'),("Jim","23",'15'),("Andrew","54",'56')]
mytuple = sorted(mytuple)
FULL_HTML = []

for name, rows in groupby(mytuple, itemgetter(0)):
     table = []
     for name, value1, value2 in rows:
        table.append(
            "
{}	{}	{}
".format(
                name, value1, value2))

     table = "
\n{}\n
".format('\n'.join(table))
     FULL_HTML.append(table)

FULL_HTML = "\n{}\n".format('\n'.join(FULL_HTML))

print FULL_HTML

rawDescription: Generating HTML Tables from Tuple Python


code:
class BeginningPrinter(Protocol):
    def __init__(self, finished):
        self.finished = finished
        self.buff = None

    def dataReceived(self, bytes):
        if self.buff is None:
            self.buff = bytes
        else:
            self.buff += bytes

    def connectionLost(self, reason):
        print 'Finished receiving body:', reason.getErrorMessage()
        self.finished.callback(self.buff)

rawDescription: How do I make a GET request and then print out the response body with Python (Twisted)

description: Make a GET request and then print out the response body with Python (Twisted)


code:
tree = [1, [2, [4]], [3, [0]]]
depth, sum = sum_of_longest_branch(tree)
print depth, sum

rawDescription: Finding the maximum height of a tree and returning the sum of that path


code:
from ctypes import CFUNCTYPE, c_int
from array import array
#... bunch of imports

simple = array('B')

# x86 and x64 machine code (MOV eax, 0x10; RET)
simple.extend((0x81, 0xc0) + int32(0x10))
simple.append(0xc3)

program = RawData(len(simple), PROT_READ|PROT_WRITE|PROT_EXEC)
program.upload(simple)

procedure = CFUNCTYPE(c_int)(program.address)
print "result:", procedure()

# alters the first instruction
program.poke(2, int32(123))
print "result:", procedure()

# transforms that first instruction into (NOP,NOP,NOP,NOP,NOP,NOP)
program.poke(0, [0x90]*6)
print "result:", procedure()

rawDescription: position contents of array.array into heap

description: Position contents of array.array into heap


code:
for num in xrange(999, -1, -1):
    print i

rawDescription: iterating over a reversed sequence of numbers

description: Iterating over a reversed sequence of numbers


code:
import functools

def Test_method_2(self, func):
    @functools.wraps(func)
    def wrapper(self, *args, **kw):
        print 'Wrapped function name:', func.__name__
        return func(*args, **kw)
    return wrapper

rawDescription: Decorating a function with a method (with arbitrary arguments)


code:
from types import MethodType

class PrePostCaller:
    def __init__(self, other):
        self.other = other

    def pre(self): print 'pre'
    def post(self): print 'post'

    def __getattr__(self, name):
        if hasattr(self.other, name):
            func = getattr(self.other, name)
            return lambda *args, **kwargs: self._wrap(func, args, kwargs)
        raise AttributeError(name)

    def _wrap(self, func, args, kwargs):
        self.pre()
        if type(func) == MethodType:
            result = func( *args, **kwargs)
        else:
            result = func(self.other, *args, **kwargs)
        self.post()
        return result

#Examples of use
class Foo:
    def stuff(self):
        print 'stuff'

a = PrePostCaller(Foo())
a.stuff()

a = PrePostCaller([1,2,3])
print a.count()

rawDescription: Python: wrapping method invocations with pre and post methods


code:
pushed = False

@post('/button')
def action():
    global pushed  # needed because otherwise assigning to `pushed` will
                   # just create a local variable shadowing the global one
    pushed = True
    redirect("/button")

def method():
    # pushed = False   <----- this line is not needed, and if enabled, will, again,
    #                         create a local variable shadowing the global one
    global pushed  # again, otherwise the `pushed = False` statement will create a local variable
    while True:  # use real booleans, i.e. True/False not 1/0
        if pushed:
            print "push recieved"
            pushed = False

method()

rawDescription: threading a bottle app

description: Threading a bottle app


code:
table = texttable.Texttable()
table.set_cols_dtype(['i',   # integer
                      'a' ]) # automatic
table.add_rows([['int', 'auto'], [330090997180665856, 330090997180665856], [1, 1]])
print table.draw()

rawDescription: Python converts long number to float I believe ( example: x.xxxe+yy)


code:
class Subject(object):
    def __init__(self):
        self.observers = []

    def __del__(self, ):
        ''' on deletion, notify the observers '''
        print "{0}.__del__".format(self)

        # copy the observer list, then remove all references to the observers
        # NEW - use weakrefs here, or they will not be properly deleted later
        obs = [weakref.ref(o) for o in self.observers]

        # clear all strong references to the observers
        self.observers = []

        # notify all observers that we were deleted
        # ** only if they are not already deleted **
        for o in obs:
            if not o() == None:
                o().deleting(self)

        # NEW - second pass to allow the Manager to cleanup
        # ** only if they are not already deleted **
        for o in obs:
            if not o() == None:
                o().deleted(self)

    def registerObserver(self, observer):
        if observer not in self.observers:
            self.observers.append(observer)

    def removeObserver(self, observer):
        self.observers.remove(observer)

    def notifyObservers(self, message = None):
        for observer in self.observers:
            observer.update(self,message)

rawDescription: Using the observer pattern to delete an Observer when a Subject has been "deleted" in Python


code:
from BeautifulSoup import BeautifulSoup

html = '''

dvgbkfbnfd

dsvdfvd

fvjdfnvjundf

'''

soup = BeautifulSoup(html)
for match in soup.findAll('div'):
    match.replaceWithChildren()

print soup

rawDescription: How do I use BeautifulSoup to replace a tag with its contents?

description: Use BeautifulSoup to replace a tag with its contents


code:
def lsum(l, depth=0):
    print depth, l
    if type(l) is int:
        return l
    elif type(l) is list and len(l) > 0:
        return lsum(l[0], depth+1) + lsum(l[1:], depth+1)
    return 0

rawDescription: How to understand the recursion in a particular nested list?

description: Understand the recursion in a particular nested list


code:
import scripttwo
def test():
    print "Hello World"

scripttwo.scriptone_test = test
scripttwo.test()

rawDescription: Running function from script it was imported by


code:
from itertools import tee, izip_longest
in1, in2 = tee(sys.stdin, 2)
next(in2)
pairs = izip_longest(in1, in2, fillvalue='')
cond = lambda pair: not pair[0].startswith('---(more') and not pair[1].startswith('---(more')
res = filter(cond, pairs)
for line in res:
    print line

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


code:
from javax.swing import JButton, JFrame

class MyFrame(JFrame):
    def __init__(self):
        JFrame.__init__(self, "Hello Jython")
        button = JButton("Hello", actionPerformed=self.hello)
        self.add(button)

        self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
        self.setSize(300, 300)
        self.show()

    def hello(self, event):
        print "Hello, world!"

if __name__=="__main__":
    MyFrame()

rawDescription: Event handling with Jython & Swing

description: Event handling with Jython & Swing


code:
import subprocess

def logcommunicate(self, s):
    self.logfilehandle.write("Input "+s)
    std = self.oldcommunicate(s)

    self.logfilehandle.write("Output "+std[0])
    return std

subprocess.Popen.oldcommunicate = subprocess.Popen.communicate
subprocess.Popen.communicate = logcommunicate
logfh = open("/tmp/communicate.log", "a")

proc = subprocess.Popen(['cat'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
proc.logfilehandle = logfh

result = proc.communicate("hello there\n")
print result

rawDescription: Logging Process' STDIN and STDOUT

description: Logging Process' STDIN and STDOUT


code: print a['href'][-6:]

rawDescription: search a specific word in BeautifulSoup python

description: Search a specific word in BeautifulSoup python


code:
def func1():
   print "func1"

func2 = func1

func2()       # prints "func1"

rawDescription: How does this Python Lambda recursion expression work?


code:
for match in repeat.finditer("aaaaabbbbbbbbbbbbbbccccccccccc"):
    print match.group(), match.start(), match.end()

rawDescription: Python split string by pattern

description: Python split string by pattern


code:
f = open("file.txt", "r")
searchlines = f.readlines()
f.close()
for i, line in enumerate(searchlines):
    if "searchphrase" in line:
        for l in searchlines[i:i+3]: print l,
        print

rawDescription: Search a text file and print related lines in Python?


code:
## pickle version
# For pickle, you need a file-like object.
# StringIO is the native python one, whie cStringIO
# is the c implementation of the same.
# cStringIO is faster
# see http://docs.python.org/library/stringio.html and
# http://www.doughellmann.com/PyMOTW/StringIO/ for more information
import pickle
# Find the best implementation available on this platform
try:
    from cStringIO import StringIO
except:
    from StringIO import StringIO

import redis

# set up the redis client
r = redis.StrictRedis(host = '', port = 6379, db = 0)

# sample parent dicts
parent0 = {'child0': {0:'a', 1:'b', 2:'c',}, 'child1':{5:'e', 6:'f', 7:'g'}}
parent1 = {'child0': {0:'h', 1:'i', 2:'j',}, 'child1':{5:'k', 6:'l', 7:'m'}}

# define a class with a reusable StringIO object
class Pickler(object):
    """Simple helper class to use pickle with a reusable string buffer object"""
    def __init__(self):
        self.tmpstr = StringIO()

    def __del__(self):
        # close the StringIO buffer and delete it
        self.tmpstr.close()
        del self.tmpstr

    def dump(self, obj):
        """Pickle an object and return the pickled string"""
        # empty current buffer
        self.tmpstr.seek(0,0)
        self.tmpstr.truncate(0)
        # pickle obj into the buffer
        pickle.dump(obj, self.tmpstr)
        # move the buffer pointer to the start
        self.tmpstr.seek(0,0)
        # return the pickled buffer as a string
        return self.tmpstr.read()

    def load(self, obj):
        """load a pickled object string and return the object"""
        # empty the current buffer
        self.tmpstr.seek(0,0)
        self.tmpstr.truncate(0)
        # load the pickled obj string into the buffer
        self.tmpstr.write(obj)
        # move the buffer pointer to start
        self.tmpstr.seek(0,0)
        # load the pickled buffer into an object
        return pickle.load(self.tmpstr)

pickler = Pickler()

# save the parents as hashfields, with the children as pickled strings,
# pickled using our helper class
r.hmset('parent0', {key: pickler.dump(parent0[key]) for key in parent0})
r.hmset('parent1', {key: pickler.dump(parent1[key]) for key in parent1})

# Get a child dict from a parent
# say child1 of parent0
childstring = r.hget('parent0', 'child1')
# this could be done in a single line...
childdict = pickler.load(childstring)

# we don't need to do any str to int conversion on the keys.

print childdict

rawDescription: Data model and datastore technology for fast multidimensional data lookup


code:
def magic(s):
    res, indent = [], 0
    for part in s.split('\magic'):
        res.append(' ' * indent + part)
        indent += len(part)
    return '\n'.join(res)

print magic('Hello!\magicHello!')

rawDescription: Print with automatic tab (Python)

description: Print with automatic tab (Python)


code:
import x
def t():
    x.a()
    print "we still in parent"
t()

rawDescription: How can I stop a .py script that is triggered by some parent .py script without kiiling parent .py?

description: Stop a .py script that is triggered by some parent .py script without kiiling parent .py


code:
#!/usr/bin/env python

import mechanize

br = mechanize.Browser()
br.set_handle_redirect(False)
with open('urls.txt') as urls:
    for url in urls:
        stripped = url.strip()
        print '[{}]: '.format(stripped),
        try:
            br.open_novisit(stripped)
            print 'Funfando!'
        except Exception, e:
            print e

rawDescription: Check if many URLs exists from a txt file (Python)


code:
my_list = [("car", 2), ("plane", 3), ("bike", 1)]
a = itertools.product(*([name + str(i + 1) for i in range(length)]
                        for name, length in my_list))
for x in a:
    print x

rawDescription: Python: all possible combinations of "dynamic" list


code:
interesting_rows = ['Bills', 'Notes', 'Total Public Debt Outstanding']  # add the initial part of any row you are interested at (without leading spaces)

mytext = get_data_text(2, 2013)  # gets the text for feb, 2013 as rows
data = keep_only_interesting_rows(mytext, interesting_rows)  # filter rows
final_data = convert_data_to_dict(data)  # convert remaining rows into a dict

print final_data
print final_data['Bills'][2]  # this gives you the third column

# >>> {'Notes': (7416574, 5888, 7422462), 'Bills': (1738404, 3546, 1741950), 'Total Public Debt Outstanding': (11822436, 4864853, 16687289)}
# >>> 1741950

rawDescription: Formatting data from Text (.prn) file

description: Formatting data from Text (.prn) file


code:
t = PrettyTable(['key', 'value'])
for key, val in dictionary.items():
   t.add_row([key, val])
print t

rawDescription: Is there's any python library to output dictionary in beautiful ascii table?


code:
import subprocess
# ...
subprocess.call(['python', 'config.py'])
try:
  sys.exit(1)
except SystemExit:
  try:
    subprocess.call(['python', 'config.py',  "-user", "user1"])  # Extra args
  except:
    print "errr"

rawDescription: is it possible to use import after sys.exit(1)?

description: Is it possible to use import after sys.exit(1)


code: print sorted(A, key=lambda n: n if isinstance(n, numbers.Number) else sys.float_info.min, reverse=False)

rawDescription: Sorting numbers in a list - Python

description: Sorting numbers in a list - Python


code:
col={'Car','Car sales','Cars','Sports cars','Sports foo','Car bar','Statistics'}
s='Find sports car sales statistics in Manhattan.'
for i in col:
   g=re.search('{}'.format(i),s,re.IGNORECASE)
   if g:
     print g.group(0)

rawDescription: Searching for Phrase Keywords in MySQL

description: Searching for Phrase Keywords in MySQL


code:
class Book(object):
    def __repr__(self):
        return 'I am a book'

class Library(object):
    def __init__(self,*books):
        self.books = books
    def __repr__(self):
        return ' | '.join(repr(book) for book in self.books)

b1, b2 = Book(), Book()
print Library(b1,b2)

#prints I am a book | I am a book

rawDescription: print nested objects with python and __repr__

description: Print nested objects with python and __repr__


code: print list(words)

rawDescription: Removing Set Identifier when Printing sets in Python


code:
import os
if os.path.splitext(link)[1] not in ['.jpg', '.pdf', '.mp3']:
    print link

rawDescription: Excluding web links with specific extensions in web scraper


code:
with open("blurbs.csv","rb") as f:
    blurbs = csv.DictReader(f, delimiter="\t")
    for row in blurbs:
        print row
    f.seek(0)
    for row in blurbs:
        print row

rawDescription: Python: How do I use DictReader twice?

description: Python: How do I use DictReader twice


rawDescription: Parsing slice information from a slice object?


code:
import itertools

l1 = [1,2,3,4]
l2 = [5,10,15]
l3 = [10, 20, 30, 40, 50]

for a, b, c in itertools.product(l1, l2, l3):
    print a,b,c

rawDescription: Algorithm to calculate total unique permutations of set of lists that contain some items


code:
from bs4 import BeautifulSoup
import urllib2
import requests

url="http://blog.jang.com.pk/blog_details.asp?id=11058"
r  = requests.get(url)
# Update encoding to match source
r.encoding = "utf-8"

data = r.text
soup = BeautifulSoup(data,'lxml')
print str(soup.title)

rawDescription: How to extract Urdu text from a webpage using beautifulsoup

description: Extract Urdu text from a webpage using beautifulsoup


code: print [[int(item) for item in items] for items in array]

rawDescription: Convert floating point array to integer array?


code:
class Player:
    def __init__(self, name, points=0):
        self.name = name
        self.points = points
    def gain_points(self, n):
        self.points += n
    def next_player(self):
        i = players.index(self)
        return players[i+1]
    def __repr__(self):
        return '%s(%r, %r)' % (self.__class__.__name__, self.name, self.points)

if __name__ == '__main__':
    players = [Player('Manny'), Player('Mo'), Player('Jack')]
    players[1].next_player().gain_points(5)
    print players

rawDescription: How to call functions of next/previous object in list in Python

description: Call functions of next/previous object in list in Python


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

LISTS = []

#Generate data
for _ in range(3):
    LISTS.append(nprnd.randint(100, size=100))

#Find the maximum y value of every data set
maxYs = [i[0].max() for i in map(plt.hist,LISTS)]
print "maxYs:", maxYs

#Find the largest y
maxY = np.max(maxYs)
print "maxY:",maxY

for LIST in LISTS:
    plt.figure()
    #Set that as the ylim
    plt.ylim(0,maxY)
    plt.hist(LIST)
    plt.title('Title')
    plt.xlabel("x-axis [unit]")
    plt.ylabel("Frequency")
    #Got rid of the safe function
plt.show()

rawDescription: Python matplotlib histogram: edit x-axis based on maximum frequency in bin


code:
s="19920101 00:00"
format = "%Y%m%d %H:%M"
d = datetime.datetime.strptime(s, format)
print d

rawDescription: parse using dateutil

description: Parse using dateutil


code:
def dump(obj):
  out = {}

  for attr in dir(obj):
    out[attr] = getattr(obj, attr)

  from pprint import pformat
  return pformat(out)

class myclass(object):
  foo = 'foo'

  def __init__(self):
    self.bar = 'bar'

  def __str__(self):
    return dump(self)

c = myclass()
print c

rawDescription: Is there a python module that provides deeper analysis of a data structure than pprint?


code:
import re

# read all the text of the file, and clean it up
with open('counts.txt', 'rt') as f:
    alltext = '\n'.join(line.strip() for line in f)

# find all occurrences of the script line followed by the launch line
cont = re.findall('^script(\d)\nlaunch(\d+)\.VBS\n(?mi)',alltext)

# accumulate the counts of each launch number for each script number
# into nested dictionaries
scriptcounts = {}
for scriptnum,launchnum in cont:
    # if we haven't seen this scriptnumber before, create the dictionary for it
    if scriptnum not in scriptcounts:
        scriptcounts[scriptnum]={}
    # if we haven't seen this launchnumber with this scriptnumber before,
    # initialize count to 0
    if launchnum not in scriptcounts[scriptnum]:
        scriptcounts[scriptnum][launchnum] = 0
    # incremement the count for this combination of script and launch number
    scriptcounts[scriptnum][launchnum] += 1

# produce the output in order of increasing scriptnum/launchnum
for scriptnum in sorted(scriptcounts.keys()):
    for launchnum in sorted(scriptcounts[scriptnum].keys()):
        print "script%s\nlaunch%s.VBS\n# count %d\n"%(scriptnum,launchnum,scriptcounts[scriptnum][launchnum])

rawDescription: Python counting occurrences across multiple lines using loops


code:
from time import time
from datetime import datetime
import sqla_lib

t = time()

DBC = sqla_lib.DataBase(sqla_lib.Base)

data = {'make':'Chevy',
    'model' : 'Corvette',
    'year' : 1964,
    'price' : 50000}
DBC.Add_Record(data)

data = {'make':'Chevy',
    'model' : 'Camaro',
    'year' : 1967,
    'doors' : 2,
    'wheels_rwd' : 4.1,
    "test" : "bladads"}
DBC.Add_Record(data)
DBC.Commit()
DBC.Close()

print str(time() - t) + " s."

rawDescription: Wrap SQLAlchemy in Classes in Python

description: Wrap SQLAlchemy in Classes in Python


code:
from win32pdh import *
import time

while True:
    path = MakeCounterPath((None,"PhysicalDisk","_Total",None,0,"Avg. Disk Queue Length"),0)
    query_handle = OpenQuery()
    counter_handle = AddCounter(query_handle, path)
    query_handle = OpenQuery()
    counter_handle = AddCounter(query_handle, path)
    CollectQueryData(query_handle)
    time.sleep(2)
    CollectQueryData(query_handle)
    (counter_type, value) = GetFormattedCounterValue(counter_handle, PDH_FMT_DOUBLE)
    print value
    CloseQuery(query_handle)

rawDescription: Python collect System Stats

description: Python collect System Stats


code:
print all_compare_tables['CompareTables1']
print all_compare_tables['CompareTables2']

rawDescription: Python list Merging

description: Python list Merging


code:
from itertools import groupby

class A(object):

    def __init__(self, b, c):
        self.b = b
        self.c = c

items = [A(1, 2), A(1, 2), A(1, 2), A(2, 2), A(3, 4), A(3, 4), A(5, 6)]

groups = groupby(items, lambda a: (a.b, a.c))

for key, group in groups:
    print "Key: %s, Number of items: %s" % (key, len(list(group)))

rawDescription: group python list of objects by 2 attributes

description: Group python list of objects by 2 attributes


code:
sum = 0
for line in sys.stdin:
    item = int(line.strip())
    sum += item
print sum

rawDescription: How to process raw data (in python)?

description: Process raw data (in python)


code:
from multiprocessing import Process, Queue

def test_1(q):
    for i in range(10):
        q.put('test_1: ' + str(i))

def test_2(q):
    for i in range(10):
        q.put('test_2: ' + str(i))

q = Queue()
p1 = Process(target=test_1, args=(q,))
p2 = Process(target=test_2, args=(q,))
p1.start()
p2.start()

with open('test.txt', 'w') as file:
    while p1.is_alive() or p2.is_alive() or not q.empty():
        try:
            value = q.get(timeout = 1)
            file.write(value + '\n')
        except Exception as qe:
            print "Empty Queue or dead process"
p1.join()
p2.join()

rawDescription: Writing to file in multi-threading

description: Writing to file in multi-threading


code:
probs_y2 = probs[:, T.arange(targets.shape[1])[np.newaxis, :], targets]

print probs_y2.eval()
print (probs_y2.dimshuffle(0, 2, 1) - probs_y).eval()

rawDescription: Theano advanced indexing for tensor, shared index


code:
from itertools import imap,compress
with open('templ.txt') as templ, open('in.txt') as f:
    print list(imap(str.strip,compress(f,imap(lambda x: '=?' in x,templ))))

rawDescription: Python how to match files to templates

description: Python how to match files to templates


code:
conditions = ["foo","bar"]

s = "foo bar"
print all(x in s.split()  for x in conditions)
True

l = ["fo","bar"]

s = "foo bar"
print all(x in s.split() for x in conditions)
False

rawDescription: Check if all list items exist in line(regex)


code:
with open("inputfile.txt") as myfile:
    print myfile.readines()[l].split()[w]

rawDescription: Python: printing a specific word on a specific line


code: print out.readlines()

rawDescription: SSH a linux box using subprocess module

description: SSH a linux box using subprocess module


code:
import urllib2
f = urllib2.urlopen('http://www.python.org/')
print f.read(100)

rawDescription: How to perform HTTP GET operation in Python?

description: Perform HTTP GET operation in Python


code:
def dosomething():
    print "Doing something..."

def myfunc(conn, command):
    output = StringIO()
    result = ""
    with std_redirector(stdout=output, stderr=output):
        try:
            eval(command)
            result = output.getvalue()
        except Exception, err:
            result = repr(err)

    conn.send(result)
    conn.close()

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()
    command = "dosomething()"
    p = Process(target=myfunc, args=(child_conn, command))
    p.start()
    print parent_conn.recv()
    p.join()

rawDescription: How can I send python multiprocessing Process output to a Tkinter gui

description: Send python multiprocessing Process output to a Tkinter gui


code:
def detectCropFile(localPath):
    fpath = "/xxx/xx/Desktop/Crop_detect/videos/USUV.mp4"
    print "File to detect crop: %s " % fpath
    p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", "cropdetect=24:16:0", "-vframes", "500", "-f", "rawvideo", "-y", "/dev/null"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    infos = p.stderr.read()
    print infos
    allCrops = re.findall(CROP_DETECT_LINE + ".*", infos)
    print allCrops
    mostCommonCrop = Counter(allCrops).most_common(1)
    print "most common crop: %s" % mostCommonCrop
    print mostCommonCrop[0][0]
    global crop
    crop = mostCommonCrop[0][0]
    video_rename()

rawDescription: ffmpeg crop detect python using subprocess

description: Ffmpeg crop detect python using subprocess


code:
buff += "This is a) test of i) one ii) two iii) three a. four and b- five"
print re.sub(" \({0,1}(I+|i+|[a-zA-Z0-9])(\)|\.|-)","\n\g<0>",buff)

rawDescription: regex for ordering/numbering

description: Regex for ordering/numbering


code:
def generate_print_callback():
    """
    Generate a callback that prints

        iteration number | parameter values | objective function

    every tenth iteration.
    """
    saved_params = { "iteration_number" : 0 }
    def print_callback(xs):
        if saved_params["iteration_number"] % 10 == 0:
            print "{:3} | {} | {}".format(
                saved_params["iteration_number"], xs, objective_function(xs))
        saved_params["iteration_number"] += 1
    return print_callback

rawDescription: print currently evaluated params during scipy minimization

description: Print currently evaluated params during scipy minimization


code:
# Your script.
import sys

import foo

bar = 999
sys.argv = ['blah', 'fubb']

execfile( 'foo.py', globals() )

# The file being exec'd.
if __name__ == "__main__":
    print bar
    print sys.argv

rawDescription: Python dynamically importing a script, need to have its __name__ == "__main__" code to be called


code:
with open("dvd.csv") as f:
    for num, line in enumerate(f, 1):
        if " arnold " in line:
            num = str(num)
            print line.split(',')[3] + '' + num

rawDescription: Read line in file, print line if it contains string


code:
class MainClass:
    def __setattr__(self, name, value):
        self.__dict__[name] = value

def getRectangleNameGenerator(N = 10):
    X = 0
    while X <= N:
        X += 1
        yield "Rectangle" + str(X)
RectangleName = getRectangleNameGenerator()

ClassInstances = {next(RectangleName) : MainClass}
ClassInstances[next(RectangleName)] = MainClass

ClassInstances["Rectangle1"].Temp = 10
print ClassInstances["Rectangle1"].Temp

rawDescription: How can I create an indefinite iterations of objects in python?

description: Create an indefinite iterations of objects in python


code:
def findword(objf, word):
    lcount = 1
    found = False
    with open(objf, "r") as f:
        for line in f:
            if word in line: # If word is in line
                testline = line.split()
                ind = testline.index(word) # This is the index, starting from 0
                found = True
                break
            else:
                lcount += 1
        if found:
            print "Word %s found at line %d, word %d" % (word, lcount, ind+1)
        else:
            print "Not found"

rawDescription: How to know a position (.txt)

description: Know a position (.txt)


code:
from numpy import sqrt, log

def sigma2Gamma(sigma):
    '''Function to convert standard deviation (sigma) to FWHM (Gamma)'''
    return sigma * sqrt(2 * log(2)) * 2 / sqrt(2)

Gamma = sigma2Gamma(10)
print Gamma
# prints 16.651092223153956, which is what you saw in your graph

def Gamma2sigma(Gamma):
    '''Function to convert FWHM (Gamma) to standard deviation (sigma)'''
    return Gamma * sqrt(2) / ( sqrt(2 * log(2)) * 2 )

sigma = Gamma2sigma(10)
print sigma
# prints 6.0056120439322491, which is the standard deviation that will
# give a FWHM of 10

rawDescription: Making Gaussians Constrained by the FWHM


code:
def if_extractable(fn):
    # this expects to be wrapping a Pamplemousse object
    def wrapped(self, *args):
        print "wrapper(): Calling %s with" % fn, args
        result = None
        with PDFMinerWrapper(self.pdf_doc) as doc:
            if doc.is_extractable:
                result = fn(self, doc, *args)
        return result
    return wrapped

class Pamplemousse(object):

    def __init__(self, inputfile):
        self.pdf_doc = inputfile

    # get_toc will only get called if the wrapper check
    # passes the extractable test
    @if_extractable
    def get_toc(self, doc, *args):
        print "get_toc():", self, doc, args

rawDescription: Replacing macro-style class method with a decorator?


code:
s = r'foo(bar, "Value of \"s\" is: %s", "foobar");'
m = re.search(r'".*?[^\\]"', s)
result = m.group(0)
print result

rawDescription: Python RE - Regular expression for matching a printf-like format string with escaped quotation marks


code:
def countdown(n, m):
    for i in range(n, 0, -m):
        print i
    print "Blastoff!"

rawDescription: Countdown function with two parameters

description: Countdown function with two parameters