code:
from bs4 import BeautifulSoup
soup = BeautifulSoup("

My paragraph My link

hello again

","html.parser")

soup.contents[0].unwrap()

print soup
print len(soup.contents)

rawDescription: Strip the first (top level) tag in Beautifulsoup


rawDescription: abstracting the conversion between id3 tags, m4a tags, flac tags

description: Abstracting the conversion between id3 tags, m4a tags, flac tags


code:
import re

def get_cropped(style):
    rex = re.compile(r'.*/%s(?:\b|_)' % style)
    cropped_images = [img for img in cropped if rex.match(img)]
    return cropped_images

for style in styles:
    print '\nStyle:', style
    get_s3_images = get_cropped(style)
    for x in get_s3_images:
        print x

rawDescription: python group file names by exact string

description: Python group file names by exact string


code:
alphabet = {}
for line in input_file:
    line = line.strip()
    starts_with = line[0]
    if starts_with in alphabet:
        alphabet[starts_with]+= 1
    else:
        alphabet[starts_with] = 1

print alphabet

rawDescription: count the words starts with a-z character using dictionary in python

description: Count the words starts with a-z character using dictionary in python


code:
col = range(5)
row = [0, 1, 1, 0, 1]
dat = [1, 1, 1, 1, 1]
S = csr_matrix((dat, (row, col)), shape=(2, 5))
result = S * A
# check that the result is another sparse matrix
print type(result)
# check that the values are the ones we want
print result.toarray()

rawDescription: Sum over rows in scipy.sparse.csr_matrix


code: print data['x'], data['y'], data['z']

rawDescription: Extract variables from string in python

description: Extract variables from string in python


code:
import Tkinter as TK
import tkMessageBox

# [Credit goes to @NadiaAlramli](http://stackoverflow.com/a/1625023/1460057) for the grouping code
def group(seq, groupSize):
    return zip(*(iter(seq),) * groupSize)

Network_Tag, Vertex_Tag, Line_Tag = "network", "vertex", "line"

class Shape:
    def __init__(self, canvas, vertexCoords, vertexDiam):
        self.vertexIDs = []
        self.perimeterID = None
        self.vertexCoords = vertexCoords
        self.vertexRadius = vertexDiam/2
        self.canvas = canvas

    def deleteVertices(self):
        for ID in self.vertexIDs:
            self.canvas.delete(ID)
        self.vertexIDs = []

    def bindClickToVertices(self):
        coordsGrouped = group(self.vertexCoords, 2)
        num = len(coordsGrouped)
        for k in range(len(self.vertexIDs)):
            others = [coordsGrouped[(k-1)%num], coordsGrouped[(k+1)%num]]
            self.canvas.tag_bind(self.vertexIDs[k], '<Button-1>',
                    lambda *args:tkMessageBox.showinfo("Vertex Click", "Neighboring vertices: "+str(others)))

    def drawVertices(self):
        for x, y in group(self.vertexCoords, 2):
            self.vertexIDs.append(self.canvas.create_oval(x-self.vertexRadius, y-self.vertexRadius, x+self.vertexRadius, y+self.vertexRadius, fill='black', tags=(Network_Tag, Vertex_Tag)))
        self.bindClickToVertices()

    def updateVertices(self):
        self.deleteVertices()
        self.drawVertices()

    def deletePerimeter(self):
        if self.perimeterID is not None:
            self.canvas.delete(self.perimeterID)
        self.perimeterID = None

    def drawPerimeter(self):
        print "creating line:", (self.vertexCoords + self.vertexCoords[0:2])
        self.perimeterID = self.canvas.create_line(*(self.vertexCoords + self.vertexCoords[0:2]), fill='black', tags=(Network_Tag, Line_Tag))

    def updatePerimeter(self):
        self.deletePerimeter()
        self.drawPerimeter()

    def deleteShape(self):
        self.deleteVertices()
        self.deletePerimeter()

    def updateShape(self):
        self.updateVertices()
        self.updatePerimeter()

rawDescription: Finding and Connecting Multiple Canvas Items in Python


code:
from dragonfly.all import Grammar, CompoundRule

# Voice command rule combining spoken form and recognition processing.
class ExampleRule(CompoundRule):
    spec = "do something computer"                  # Spoken form of command.
    def _process_recognition(self, node, extras):   # Callback when command is spoken.
        print "Voice command spoken."

# Create a grammar which contains and loads the command rule.
grammar = Grammar("example grammar")                # Create a grammar to contain the command rule.
grammar.add_rule(ExampleRule())                     # Add the command rule to the grammar.
grammar.load()

rawDescription: which module to automating Voice/Speech using python

description: Which module to automating Voice/Speech using python


code:
for week, week_items in week_to_items.iteritems():
    efficiency_average = float(sum(map(lambda x: x['efficiency'], week_items))) / len(week_items)
    print week, efficiency_average

rawDescription: Get matching values from dictionaries in a list then add and average


code:
hard_limit = 6
for i in loop(finite_generator()):
    if hard_limit <= 0:
        break
    print i
    hard_limit -= 1

rawDescription: Why does Python's itertools.cycle need to create a copy of the iterable?


code:
from bs4 import BeautifulSoup
import requests
page = 'http://sbj.speiyou.com/search/index/subject:/grade:12/gtype:time'
r = requests.get(page)
soup = BeautifulSoup(r.text)
div = soup.find('div', class_='pagination')
div
print next(div.stripped_strings, u'')

rawDescription: python beautiful soup get Chinese immediately after class tag

description: Python beautiful soup get Chinese immediately after class tag


code:
# Get the original parameters regardless of argument type
shape1, loc1, scale1 = rv1.dist._parse_args(*rv1.args, **rv1.kwds)
shape2, loc2, scale2 = rv2.dist._parse_args(*rv2.args, **rv2.kwds)

print 'positional and keyword'
print 'frozen parameters: shape={}, loc={}, scale={}'.format(shape1, loc1, scale1)
print
print 'positional only'
print 'frozen parameters: shape={}, loc={}, scale={}'.format(shape2, loc2, scale2)

rawDescription: How to get parameter arguments from a frozen spicy.stats distribution?

description: Get parameter arguments from a frozen spicy.stats distribution


code:
if a_list:
    print "I'm not here"

rawDescription: How to check if a list exists in Python

description: Check if a list exists in Python


code:
s = pd.Series(np.arange(adj_close.shape[0]), index=adj_close.index)
locs = s.resample("M", how="max")
print locs

rawDescription: R-Python: Getting Monthly, weekly index points


code:
lis = [['Mumbai', 98.3], ['London', 23.23], ['Agra', 12.22]]
print sorted(lis, key = lambda x : x[1], reverse = True)[:10] #[:10] returns first ten items

rawDescription: Sort the top ten results

description: Sort the top ten results


code:
def run(self):
        print 'Start Task 2'
        time.sleep(1)
        print 'Completed Task 2'
        self.callb(self)

rawDescription: Python Process completion

description: Python Process completion


code:
import re
mylist = ['name','mem','g1','g2','g3','val1','val2','val3','val1','name','mem','val1','val2','val3','val4']
mj = re.compile(r'^val\d+$')
print [[x[0] for x in g] for k, g in groupby(
                     enumerate(mylist), key=lambda x: bool(mj.search(x[1]))) if k]

rawDescription: Getting index of structured elements of a list in Python


code: print unique_list

rawDescription: Removing repeated characters from a list in python


rawDescription: Is it possible to scrap images from a webpage, convert them to numbers and save them to a file?


code:
class hero():
    def __init__(self, name="Jimmy", prof="Warrior", weapon="Sword"):
        """Constructor for hero"""
        self.name = name
        self.prof = prof
        self.weapon = weapon
        self.herodict = {
            "Name": self.name,
            "Class": self.prof,
            "Weapon": self.weapon
        }
        self.herotext = {
            "Welcome": "Greetings, hero. What is thine name? ",
            "AskClass": "A fine name {Name}. What is thine class? ",
            "AskWeapon": "A {Class}? What shalt thy weapon be? ",
        }

    def setHeroDict(self, textkey, herokey):
        n = raw_input(self.herotext[textkey].format(**self.herodict))
        self.herodict[herokey] = n
        print self.herodict[herokey]

h = hero("Tommy", "Mage", "Staff")
h.setHeroDict("Welcome", "Name")
h.setHeroDict("AskClass", "Class")

rawDescription: Better Method in Python to Call Changed Dict Values Than This?


code:
import re

lines = [
    "CAT One",
    "DOG SILVER FOX Two",
    " KING KONG ",
    "P'OT THEN Mark First",
    "FOO-BAR Second FISH",
    "horsE YELLOW thirD BLUE",
    ]

for line in lines:
    print re.findall(r'\b[A-Z]+(?:\W*[A-Z]+)*\b', line)

rawDescription: Need help to find right Regex match pattern


code:
lst = [1,2,3,4,5,6,7,8]
for x in choice_without_repetition(lst):
    print x

rawDescription: how to pick random items from a list while avoiding picking the same item in a row

description: Pick random items from a list while avoiding picking the same item in a row


code:
from random import shuffle

def scramble(sentence):
   split = sentence.split()  # Split the string into a list of words
   shuffle(split)  # This shuffles the list in-place.
   return ' '.join(split)  # Turn the list back into a string

print scramble("this is a sentence")

rawDescription: return scrambled sentence python

description: Return scrambled sentence python


code:
import time

ion()

tstart = time.time()               # for profiling
x = arange(0,2*pi,0.01)            # x-array
line, = plot(x,sin(x))
for i in arange(1,200):
    line.set_ydata(sin(x+i/10.0))  # update the data
    draw()                         # redraw the canvas

print 'FPS:' , 200/(time.time()-tstart)

rawDescription: matplotlib.pyplot/pylab not updating figure while isinteractive(), using ipython -pylab

description: Matplotlib.pyplot/pylab not updating figure while isinteractive(), using ipython -pylab


code:
class Stats:
    """..."""
    def __init__(self, *args, **kwds):
        # I can't figure out how to explictly specify a stream keyword arg
        # with *args:
        #   def __init__(self, *args, stream=sys.stdout): ...
        # so I use **kwds and sqauwk if something unexpected is passed in.
        self.stream = sys.stdout
        if "stream" in kwds:
            self.stream = kwds["stream"]
            del kwds["stream"]
        if kwds:
            keys = kwds.keys()
            keys.sort()
            extras = ", ".join(["%s=%s" % (k, kwds[k]) for k in keys])
            raise ValueError, "unrecognized keyword args: %s" % extras
        if not len(args):
            arg = None
        else:
            arg = args[0]
            args = args[1:]
        self.init(arg)
        self.add(*args)

rawDescription: Error when attempting to write cProfile information to file


code:
german_text = u"Die Orgellandschaft Südniedersachsen umfasst das Gebiet der Landkreise Goslar, Göttingen, Hameln-Pyrmont, Hildesheim, Holzminden, Northeim und Osterode am Harz sowie die Stadt Salzgitter. Über 70 historische Orgeln vom 17. bis 19. Jahrhundert sind in der südniedersächsischen Orgellandschaft vollständig oder in Teilen erhalten. "
for sent in sent_tokenize(german_text, language='german'):
    print sent
    print '---------'

rawDescription: Use of PunktSentenceTokenizer in NLTK

description: Use of PunktSentenceTokenizer in NLTK


code:
k='john'
k1=['john', 'is', 'sleeping']
print [(i, k) for i in k1]

rawDescription: Concatenating items from two lists to form n-grams using python


code:
day = raw_input("What day were you born?")
month = raw_input("What month were you born?")
year = raw_input("What year were you born?")
print sum(map(int, day+month+year))

rawDescription: Python Sum individual integers

description: Python Sum individual integers


code:
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = ["This is very strange",
          "This is very nice"]
vectorizer = TfidfVectorizer(min_df=1)
X = vectorizer.fit_transform(corpus)
idf = vectorizer.idf_
print dict(zip(vectorizer.get_feature_names(), idf))

rawDescription: tf-idf feature weights using sklearn.feature_extraction.text.TfidfVectorizer

description: Tf-idf feature weights using sklearn.feature_extraction.text.TfidfVectorizer


code:
python_code.py
import os, sys, time
import win32file, win32event, win32con

# stub your functions in my case
def connectToMatlab():
  pass
def getValueFromMatlab():
  return 99

# path to predetermined directory to watch
dirPath = "some_directory"
dirPath = os.path.abspath(dirPath)

# start/connect to a MATLAB session, running the script above
connectToMatlab()

# set up folder watching (notify on file addition/deletion/renaming)
print "Started watching '%s' at %s" % (dirPath, time.asctime())
change_handle = win32file.FindFirstChangeNotification(
  dirPath, 0, win32con.FILE_NOTIFY_CHANGE_FILE_NAME)

# time-out in 10 sec (win32event.INFINITE to wait indefinitely)
timeout = 10000

try:
  # block/wait for notification
  result = win32event.WaitForSingleObject(change_handle, timeout)

  # returned because of a change notification
  if result == win32con.WAIT_OBJECT_0:
    # retrieve final result from MATLAB
    print "MALTAB variable has exceeded threshold at %s" % time.asctime()
    val = getValueFromMatlab()

  # timed out
  elif result == win32con.WAIT_TIMEOUT:
    print "timed-out after %s msec at %s" % (timeout,time.asctime())
    val = None    # maybe to indicate failure

finally:
  # cleanup properly
  win32file.FindCloseChangeNotification(change_handle)

# work with val
print val

rawDescription: Evade polling when accessing Matlab from Python


code:
{'MS_AVERY':
    {'UGRAD':
        {'GENERAL STUDIES': {'PHYS': 1L,
                             'POLS': 53L,
                             'PHIL': 10L,
                             'HIST': 5L,
                             'MELC': 2L,
                             'GEST': 5079L}}}}

rawDescription: Pandas convert Dataframe to Nested Json

description: Pandas convert Dataframe to Nested Json


code:
regex = re.compile("[%s]{5}.*" % "".join(aminoacids))

with open(filename, 'r') as fh:
    s = fh.read()
    aa_sequence = regex.findall(s)
    if len(aa_sequence) > 0:
        # an amino acid sequence was found
        print aa_sequence[0]

rawDescription: Searching a string for a substring of characters in a list


code:
import subprocess

def check_output(command):
    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
    output = process.communicate()
    retcode = process.poll()
    if retcode:
            raise subprocess.CalledProcessError(retcode, command, output=output[0])
    return output

command = "echo this>CON"

print "subprocess -> " + subprocess.check_output(command, shell=True)
print "native -> " + str(check_output(command))

try:
    subprocess.check_output("python output.py", shell=True)
except subprocess.CalledProcessError, e:
    print "subproces CalledProcessError.output = " + e.output

try:
    check_output("python output.py")
except subprocess.CalledProcessError, e:
    print "native CalledProcessError.output = " + e.output

rawDescription: Python: get output from a command line which exits with nonzero exit code


code:
import subprocess

print "Starting process..."
with open("./out.log", "w") as f:
    process = subprocess.Popen(["echo", "a"], shell=False, stdout=f)
    process.wait()
print "Done"

rawDescription: Calling external program through Python and passing the output to stdout


code:
country = countries["USA"]
print "Area is: %s" % (country.area,)

rawDescription: reading file into a dictionary

description: Reading file into a dictionary


code:
import requests

response = requests.post(upload_file_url,
                         files={'filename': open(uploadfile,'rb'),
                                'description': 'upload test'})
print response.content

rawDescription: uploading files with urllib to a FileField model in Django

description: Uploading files with urllib to a FileField model in Django


code:
with open("test2.xls") as w:
    w.next()  # skip over header row
    for row in w:
        (date, time, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t,
         u, LZA, SZA, LAM) = row.split("\t")  # split columns into fields

        A = [(float(a) + float(b) + float(c))/3,
             (float(d) + float(e) + float(f))/3,
             (float(g) + float(h) + float(i))/3,
             (float(j) + float(k) + float(l))/3,
             (float(m) + float(n) + float(o))/3,
             (float(p) + float(q) + float(r))/3,
             (float(s) + float(t) + float(u))/3]
        print ('['+ ', '.join(['{:.6f}']*len(A)) + ']').format(*A)
        avg = sum(A)/len(A)
        print avg

rawDescription: Averaging down a column of averaged data


code:
from __future__ import division
num = [1,2,3,4,5,6,3,31,1,0,120,0,0]
divisor = 10
print [divisor/x for x in num if x != 0]

rawDescription: Ignoring zero by division instances in script


code:
for row in result:
    print row['id']

rawDescription: Result of type long from a SqlProxyRow result in string


code:
# saved as client.py
import Pyro4

name=raw_input("What is your name? ").strip()

greeting_maker=Pyro4.Proxy("PYRONAME:example.greeting")    # use name server object lookup uri shortcut
print greeting_maker.get_fortune(name)

rawDescription: how to connect to local machine using remote client machine

description: Connect to local machine using remote client machine


code:
fi = open('myinput.dat', 'rb')
fo = open('myoutput.dat', 'wb')

out_list = []

bytes_in = fi.read(3)
while bytes_in != '':
    print bytes_in
    out_list.append( bytearray([bytes_in[1], bytes_in[0], bytes_in[2]]) )
    print out_list
    bytes_in = fi.read(3)

out_list.sort()

for bytes_out in out_list:
    fo.write(bytes_out)

fo.close()
fi.close()

rawDescription: convert hex decimal numbers to decimal using python

description: Convert hex decimal numbers to decimal using python


code:
pos = [['det'], ['noun', 'adj'], ['noun'],
       ['vb'], ['det'], ['vb', 'noun', 'adj']]
for x in itertools.product(*pos):
    print " ".join(x)

rawDescription: How to print all possibilities of nested lists in python?

description: Print all possibilities of nested lists in python


code:
import os, random, time

bdir = '/hdd/backup/'
fns = os.listdir(bdir)

while True:
  fn = random.choice(fns)
  if not fn.startswith("duplicity-full."):
    continue
  ts = time.time()
  with open(bdir+fn, 'rb') as f:
    c = f.read()
  print "MB/s: %.1f" %(len(c)/(1000000*(time.time()-ts)))

rawDescription: Memory-Mapping Slows Down Over Time, Alternatives?


code:
import re
def title_except(s, exceptions):
   word_list = re.split(' ', s)       #re.split behaves as expected
   final = [word_list[0].capitalize()]
   for word in word_list[1:]:
      final.append(word in exceptions and word or word.capitalize())
   return " ".join(final)

articles = ['a', 'an', 'of', 'the', 'is']
print title_except('there is a    way', articles)
#There is a    Way
print title_except('a whim   of an elephant', articles)
#A Whim   of an Elephant

rawDescription: Python Titlecase a String with exceptions


code:
c = b[::-1]
# -> '\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'

d = c.encode('hex')
z = d
print z
# -> 00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff

rawDescription: How can I convert a 256-bit big endian integer to little endian in Python?

description: Convert a 256-bit big endian integer to little endian in Python


code:
values = [10,0,0,14,14,123,235,0,0,0,0,0,12,12,1235,23,234,15,15,23,136,34,34,37,45]
number_bins = 10
bins_lower = int(floor(len(values) / float(number_bins)))
bins_upper = int(ceil(len(values) / float(number_bins)))

splits = cycle([bins_lower, bins_upper])
bins = []
count = 0

while count < len(values):
    splitby = splits.next()
    bins.append(values[count:count+splitby])
    count += splitby

print bins

rawDescription: Python - How to generate a binning index for a list?


code:
class MainClass:
    X, Y = 0, 0

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"].X = 11
print ClassInstances["Rectangle1"].X

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

description: Create an indefinite iterations of objects in python


code: for L in itertools.permutations(a_solution): print L

rawDescription: Iterate over lists with a particular sum


code:
class Ball:
    def __init__(self,pos,vel):
        self.pos = Vector(pos.x,pos.y)
        self.vel = Vector(vel.x,vel.y)
    def curx(self):
        return (self.pos.x + self.vel.x)
    def cury(self):
        return (self.pos.y + self.vel.y)
    def forcex(self, velx):
        self.vel.deltax(velx)
    def forcey(self, vely):
        self.vel.deltay(vely)

class Vector:
    def __init__(self,x,y):
        self.x = x
        self.y = y
    def delx(self,deltax):
        self.x = self.x + deltax
    def dely(self,deltay):
        self.y = self.y + deltay
ball = Ball(Vector(0,0),Vector(0,0))
print ball.curx()

rawDescription: Not recognizing object parameter as int

description: Not recognizing object parameter as int


code:
import re
from bs4 import BeautifulSoup

data = 'CHEM-1315-018 - Lab-CHEM 1315-001'

soup = BeautifulSoup(data)

pattern = re.compile(r'^/d2l/lp/ouHome/home\.d2l\?ou=(\d+)')
link = soup.find('a', class_='d2l-link', href=pattern)

print pattern.search(link['href']).group(1)

rawDescription: Extracting href ID from block of code using Python


rawDescription: Reading from JSON file in Python and writing to CSV


code:
container = deque(digits[:13], maxlen=13)
current_product = reduce(mul, map(int, container))
print cal_max_prod(container, current_product)
5576689664895

rawDescription: Largest product in a series in python

description: Largest product in a series in python


code:
class FileSystem():

    def __init__(filePath=None):
        self.children = []
        if files != None:
            try:
                self.name, child = files.split("/", 2)
                self.children.append(FileSystem(filePath))
            except (ValueError):
                 pass

    def addChild(filePath):
        self.children.append(FileSystem(filePath))

    def getChildren():
        return self.children

    def printAllChildren():
        print "Name: "+ self.name
        print "{ Children:"
        for child in self.children:
            child.printAllChildren()
        print "}"

rawDescription: Creating Nested Dictionary from File hierarchy


code:
print scipy.special.kn.__doc__
scipy.special.kn(0, 1)

rawDescription: Zeroth-order Bessel function Python

description: Zeroth-order Bessel function Python


code:
from PyQt4 import QtCore
print QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.TranslationsPath)

rawDescription: StandardContextMenu internationalization in PyQt


code:
file_name = "testdata.txt"
with open(file_name) as f:
      pre_data,post_data =[s.strip() for s in (f.read()).split("[data]")]
post_data_lines = post_data.splitlines()
headers = post_data_lines[0].split()
headers2 = [s.replace("_"," ").strip() for s in headers]
for line in post_data_lines[1:]:
    tmpline  = []
    pos = 0
    for itm in headers:
        tmpline.append(line[pos:pos+len(itm)])
        pos += len(itm)+1

    print dict(zip(headers2,tmpline))

rawDescription: reading files python

description: Reading files python


code:
if re.search(r'javascript|php|windows', input, re.IGNORECASE):
    print 'onoes'

rawDescription: How to find if part of a list is in str

description: Find if part of a list is in str


code:
import config
class test():
    def __init__(self):
        configObj = config.Config()
        configObj.setupValues()
        print configObj.color

rawDescription: How to pass global variables between files in python

description: Pass global variables between files in python


code:
#! /usr/bin/python

import pexpect

cmd = "foo.sh"
thread = pexpect.spawn(cmd)
print "started %s" % cmd
cpl = thread.compile_pattern_list([pexpect.EOF,
                                   'waited (\d+)'])
while True:
    i = thread.expect_list(cpl, timeout=None)
    if i == 0: # EOF
        print "the sub process exited"
        break
    elif i == 1:
        waited_time = thread.match.group(1)
        print "the sub process waited %d seconds" % int(waited_time)
thread.close()

rawDescription: Getting realtime output from ffmpeg to be used in progress bar (PyQt4, stdout)


code:
p1 = Player("Jane")
p2 = Player("John")
print ROSTER["Jane"].name, ROSTER["Jane"].score
print ROSTER["John"].name, ROSTER["John"].score
for p in ROSTER.values():
  print p.name, p.score

rawDescription: Simple OOP query. Python: saving object names in __init__


code:
m = re.search(r"(?s)var\s+FB_DATA\s*=\s*(\{.*?\});", html)
print m.group(1)

rawDescription: Parse JSON from html tag using Python

description: Parse JSON from html tag using Python


code:
coords = {}

def onselect(xmin, xmax):
    indmin, indmax = np.searchsorted(x, (xmin, xmax))
    indmax = min(len(x)-1, indmax)

    thisx = x[indmin:indmax]
    thisy = y[indmin:indmax]
    coords['x'] = thisx
    coords['y'] = thisy
    line2.set_data(thisx, thisy)
    ax2.set_xlim(thisx[0], thisx[-1])
    ax2.set_ylim(thisy.min(), thisy.max())
    fig.canvas.draw()

# set useblit True on gtkagg for enhanced performance
span = SpanSelector(ax, onselect, 'horizontal', useblit=True,
                    rectprops=dict(alpha=0.5, facecolor='red') )

plt.show()

print 'working with x an y'
print coords['x'][:10]
print coords['y'][:10]

rawDescription: Return a portion of a plot using matplotlib


code:
#!/usr/bin/env python

import os
for root, dirs, files in os.walk('notes'):
    print root, dirs, files

rawDescription: How to parse a directory tree in python?

description: Parse a directory tree in python


code:
for k,v in Counter(lst2).iteritems():
    if v == Counter(lst)[k]:
            print k

rawDescription: Compare tuple values in Two lists

description: Compare tuple values in Two lists


code:
class MyMetaClass(type):
    def __new__(cls, class_name, parents, attrs):
        if 'meta_args' in attrs:
            meta_args = attrs['meta_args']
            attrs['args'] = meta_args[0]
            attrs['to'] = meta_args[1]
            attrs['eggs'] = meta_args[2]
            del attrs['meta_args'] # clean up
        return type.__new__(cls, class_name, parents, attrs)

class MyClass(object):
    __metaclass__ = MyMetaClass
    meta_args = ['spam', 'and', 'eggs']

myobject = MyClass()

from pprint import pprint
pprint(dir(myobject))
print myobject.args, myobject.to, myobject.eggs

rawDescription: Python metaclass arguments

description: Python metaclass arguments


code:
import argparse

p = argparse.ArgumentParser(description="My great script")
p.add_argument("sourceDir", type=str, help="source directory")
p.add_argument("destDir", type=str, help="destination directory")
p.add_argument("--match", type=str, dest="match", help="search pattern")

args = p.parse_args()

print args.sourceDir, args.destDir, args.match

rawDescription: Python get string right after argument switch


code:
from celery import Celery, signature, chain
import datetime as dt
import time

app = Celery('test_tasks')
app.config_from_object('celery_config')

@app.task(bind=True)
def test_1(self, prev_endtime=dt.datetime.now(), wait_seconds=5):
    wait = dt.timedelta(seconds=wait_seconds)
    print dt.datetime.now() - prev_endtime
    wait = wait - (dt.datetime.now() - prev_endtime)
    wait = wait.seconds
    print wait
    time.sleep(max(0, wait))
    now = dt.datetime.now()
    print now
    return now

#app.control.rate_limit('test_tasks.test_1', '0.5')
test_task = signature('test_tasks.test_1')

l = [test_task] * 100

chain = chain(*l)
res = chain()

rawDescription: Set delay between tasks in group in Celery


code:
# construct a sample list of lists
list_of_lists = np.array([range(i) for i in range(1000)])
print np.vectorize(len)(list_of_lists)
# [0,1 ... 998,999]

rawDescription: Python: An elegant/efficient way to evaluate function over bi-dimensional indexes?


rawDescription: Python - Pandas: Find largests values off-diagonal


rawDescription: How to covert this binary string to a normal string?

description: Covert this binary string to a normal string


code:
import re
p = re.compile(r'(?s)<script\b((?:(?!src).)*?)>(.*?)</script>')
test_str = "<script type=\"text/javascript\" src=\"python_files/py_dict.js\"></script>\n<script type=\"text/javascript\">9089089089</script>\n<script>9089089089</script>"
print [(x.group(1), x.group(2)) for x in re.finditer(p, test_str)]

rawDescription: Make regular expression Python

description: Make regular expression Python


code:
import collections
import json

decoder = json.JSONDecoder(object_pairs_hook=collections.OrderedDict)

json_string = '{"id":"0","last_modified":"undefined"}'
print decoder.decode(json_string)
json_string = '{"last_modified":"undefined","id":"0"}'
print decoder.decode(json_string)

rawDescription: Converting string to ordered dictionary?

description: Converting string to ordered dictionary


rawDescription: Free up numbers from string

description: Free up numbers from string


code: print unicode(c)

rawDescription: __unicode__() doesn't return a string

description: __unicode__() doesn't return a string


code:
import twisted
from twisted.internet import utils
from twisted.internet import reactor

class A(object):

    def __init__(self):
        self.value = []

    def method_one(self):
        d = utils.getProcessOutput('/bin/sh', ('-c', 'ls /home'))
        def set_self_value(result):
            print 'called set'
            self.value = 100
        d.addCallback(set_self_value)
        d.addCallback(self.method_two)

    def method_two(self, *args): # *args required to handle the result
        print 'called two'
        print self.value

def main():
    a = A()
    a.method_one()

reactor.callWhenRunning(main)
reactor.run()

rawDescription: twisted defer make the assignment of class instance variables is invalid

description: Twisted defer make the assignment of class instance variables is invalid


code:
numbers = [1,2,3,4,5,6]
letters = ["a","b","a","b","c","c"]
state = [False, False, False, False, False, False]

choice1 = input("Enter Your 1st choice: ")
choice2 = input("Enter Your 2nd choice: ")

if letters[choice1] == letters[choice2]:
    state[choice1] = (not state[choice1])
    state[choice2] = (not state[choice2])

List = []

print state
for i in range(len(state)):
    if state[i] == True:
        List.append(letters[i])
    else:
        List.append(numbers[i])

print List

rawDescription: Creating a new list from 2 lists based on the index return from a third list


code:
from bs4 import BeautifulSoup

html_doc = '''

     data1

     data2

'''

soup = BeautifulSoup(html_doc)
mylist = ['addendum 1', 'addendum 2']
for td,item in zip(soup.find_all('td'), mylist):
  td.append(soup.new_tag('br'))
  td.append(item)
print soup.prettify()

rawDescription: How can i append <br> tags after a text element?

description: Append <br> tags after a text element


code:
cat1 = {"james":[1,2,3], "bob":[3,4,5]}
for x in cat1['james']:
    print x

rawDescription: How to add multiple values per key in python dictionary

description: Add multiple values per key in python dictionary


code:
def g(x):
    values = [v for vals in x.values for v in vals[2:]]
    return pd.DataFrame({1: values[:N/2*len(x)], 2: values[N/2*len(x):]})

res = df.groupby(["Activity", "Iteration"]).apply(g).unstack()
r1 = res[1].T.rename(columns={(t,i):t+str(i)+"1" for t,i in res.index})
r2 = res[2].T.rename(columns={(t,i):t+str(i)+"2" for t,i in res.index})
res = pd.concat([r1,r2],axis=1).sort(axis=1)
res = res.rename(columns={t:t[:-2] for t in res.columns})

print df
print res

rawDescription: Creating feature (row) vectors, for SVM, from Pandas GroupBy function (and or other methods suggested)


code:
import xml.etree.ElementTree as etree

english_tree = etree.parse('en.xml')
french_tree = etree.parse('fr.xml')

# Get the root elements, as they support iteration
# through their children (direct descendants)
english_root = english_tree.getroot()
french_root = french_tree.getroot()

# Iterate through the direct descendants of the root
# elements in both trees in parallel.
for en, fr in zip(english_root, french_root):
   assert en.tag == fr.tag # check for the same structure
   if en.tag == 'id':
       assert en.text == fr.text # check for the same id

   elif en.tag == 'string':
       if fr.text is None:
           fr.text = en.text
           print en.text      # displaying what was replaced

etree.dump(french_tree)

rawDescription: Python replace XML content with Etree

description: Python replace XML content with Etree


code:
# Pig Latinify
import re

vowels = list('aeiou')

def pig_latinify():
    state = True
    while state == True:

        user_input = raw_input("Enter a word to be translated: ").lower()

        # If the first character in input is a vowel add 'yay' to input and print.
        if user_input[0] in vowels[0:]:
            print ""
            print "Begins with a vowel."
            pig_output = user_input + "yay"
            print user_input, "becomes:", pig_output
            print ""
        else:
            print ""
            print "Doesn't begin with a vowel."

            r = re.search("(.*?)([aeiou].*)", user_input)

            # Capture the consonants up to the first vowel
            captured_consonants = r.groups()[0]

            # Slice user_input up to the first vowel and create a substring beginng from the first vowel until the end of the string.
            captured_substring = r.groups()[1]

            # Concatenate substring of user_input with captured_consonants and 'ay'
            pig_output = captured_substring + captured_consonants + "ay"
            print user_input, "becomes:", pig_output
            print ""

pig_latinify()

rawDescription: How can I capture characters in a string up to the first vowel and create substring out of it?

description: Capture characters in a string up to the first vowel and create substring out of it


code: print wb.get_named_ranges()

rawDescription: "Worksheet range names does not exist" KeyError in openpyxl


code:
from scipy.spatial.distance import sqeuclidean
a = [1,2,3]
b = [0,0,0]
print sqeuclidean(a,b) # 14

rawDescription: Python - sparse vectors/distance calculation


code:
from pyparsing import (Suppress, Word, nums, alphas, Regex, Forward, Group,
                        Optional, OneOrMore, ParseResults)
from collections import defaultdict

"""
BNF for simple chemical formula (no nesting)

    integer :: '0'..'9'+
    element :: 'A'..'Z' 'a'..'z'*
    term :: element [integer]
    formula :: term+

BNF for nested chemical formula

    integer :: '0'..'9'+
    element :: 'A'..'Z' 'a'..'z'*
    term :: (element | '(' formula ')') [integer]
    formula :: term+

"""

LPAR,RPAR = map(Suppress,"()")
integer = Word(nums)

# add parse action to convert integers to ints, to support doing addition
# and multiplication at parse time
integer.setParseAction(lambda t:int(t[0]))

element = Word(alphas.upper(), alphas.lower())
# or if you want to be more specific, use this Regex
# element = Regex(r"A[cglmrstu]|B[aehikr]?|C[adeflmorsu]?|D[bsy]|E[rsu]|F[emr]?|"
#                 "G[ade]|H[efgos]?|I[nr]?|Kr?|L[airu]|M[dgnot]|N[abdeiop]?|"
#                 "Os?|P[abdmortu]?|R[abefghnu]|S[bcegimnr]?|T[abcehilm]|"
#                 "Uu[bhopqst]|U|V|W|Xe|Yb?|Z[nr]")

# forward declare 'formula' so it can be used in definition of 'term'
formula = Forward()

term = Group((element | Group(LPAR + formula + RPAR)("subgroup")) +
                Optional(integer, default=1)("mult"))

# define contents of a formula as one or more terms
formula << OneOrMore(term)

# add parse actions for parse-time processing

# parse action to multiply out subgroups
def multiplyContents(tokens):
    t = tokens[0]
    # if these tokens contain a subgroup, then use multiplier to
    # extend counts of all elements in the subgroup
    if t.subgroup:
        mult = t.mult
        for term in t.subgroup:
            term[1] *= mult
        return t.subgroup
term.setParseAction(multiplyContents)

# add parse action to sum up multiple references to the same element
def sumByElement(tokens):
    elementsList = [t[0] for t in tokens]

    # construct set to see if there are duplicates
    duplicates = len(elementsList) > len(set(elementsList))

    # if there are duplicate element names, sum up by element and
    # return a new nested ParseResults
    if duplicates:
        ctr = defaultdict(int)
        for t in tokens:
            ctr[t[0]] += t[1]
        return ParseResults([ParseResults([k,v]) for k,v in ctr.items()])
formula.setParseAction(sumByElement)

# run some tests
tests = """\
    H
    NaCl
    HO
    H2O
    HOH
    (H2O)2
    (H2O)2OH
    ((H2O)2OH)12
    C6H5OH
    """.splitlines()
for t in tests:
    if t.strip():
        results = formula.parseString(t)
        print t, '->', dict(results.asList())

rawDescription: Make outer tokens change inner tokens in a chemical formula using pyparsing


code:
from datetime import timedelta
t= timedelta(seconds=1400536339)
print t.days
16209

rawDescription: Converting mtime To Days In Python

description: Converting mtime To Days In Python


code:
def alphCount(text):
    for character, count in sorted(letterOccurrances(text).iteritems()):
        print "%s: %s" % (character, count)

alphCount("hello, world!")

rawDescription: How do I iterate through a string in Python?

description: Iterate through a string in Python


code:
import mock
import requesocks

class WebScraper:
    def get_content(self, url, proxy):
        session = requesocks.session()
        session.proxies = proxy
        response = session.get(url)
        return response.status_code

@mock.patch('requesocks.session')
def main(mock_class):
    instance = mock_class.return_value
    type(instance.get.return_value).status_code = mock.PropertyMock(return_value=400)

    scraper = WebScraper()
    proxy = {'http': 'socks5://localhost:1080'}
    status_code = scraper.get_content('http://www.purple.com', proxy)
    print "Status Code: ", status_code

if __name__ == "__main__":
    main()

rawDescription: requesocks: Mocking the session objects' get() method

description: Requesocks: Mocking the session objects' get() method


code:
import os
import tarfile
import io
from ctypes import *

class MyStructure(BigEndianStructure):
    _pack_ = True
    _fields_ = [
                ("id", c_uint), # 4 bytes
                ("namefield", c_char * 32), # 32 bytes
                ("timestamp", c_ubyte * 4), # 4 bytes
                ("payload_length", c_ushort), # 2 bytes
                ]

def process_tar(tar_files):
    """
    untar and return file objects to be parsed
    """
    for filepath in tar_files:
        f = os.path.abspath(filepath)
        with tarfile.open(f, 'r:*') as tar_f:
            #tar_f.fileobject = io.BufferedReader
            for tarinfo_member in tar_f.getmembers():
                if tarinfo_member.isfile():
                    yield tar_f.extractfile(tarinfo_member)

# test tar.gz file
open('somefiles.tar.gz', 'wb').write(
'\x1f\x8b\x08\x08\xad\x5c\x34\x52\x02\x00\x66\x6f\x6f\x2e\x74\
\x61\x72\x00\xed\xca\x49\x0a\x83\x40\x14\x04\xd0\x7f\x85\xdc\
\xa0\x43\x92\x6d\xf8\xdd\xa6\xed\x6d\xe6\xc9\x79\x1e\x76\x82\
\x0a\x82\x4b\xef\x8f\xf6\x42\xf0\x02\x6e\xe4\xbf\x4d\x41\x55\
\xb5\x5d\xdf\x9c\xeb\x6a\xa8\x60\x3d\x1c\x51\x29\xc5\x80\x69\
\xb8\xc8\x99\x14\x8c\x0b\xc1\x25\x4a\x53\x5c\x4c\x5d\x28\x9c\
\xfe\x08\x64\x6d\xbb\xc9\xed\xfe\x78\xbe\xde\x9f\xef\xef\x6f\
\xd9\x8e\xeb\xf9\x41\x18\xc5\x49\x9a\xe5\x45\xb9\xbf\x1e\x8e\
\x27\xd0\xbb\x61\x00\x21\x84\x90\x0d\x19\x01\xd4\xe8\x88\xcf\
\x00\x08\x00\x00'
)

f = "somefiles.tar.gz"
for tar_member_fileobj in process_tar([f]):
    mystruct = MyStructure()

    memmove(byref(mystruct),
            tar_member_fileobj.read(sizeof(mystruct)),
            sizeof(mystruct)
    )

    print hex(mystruct.id)
    print mystruct.namefield
    print ''.join(map(chr, mystruct.timestamp))
    print hex(mystruct.payload_length)

rawDescription: Add .readinto(b) method to tarfile's ExFileObject?


code:
class A(object):
    def __eq__(self, other):
        print "Checking equality with __eq__"
        return True

a1 = A()
a2 = A()

rawDescription: Why is (numpy.nan, 1) == (numpy.nan, 1)?

description: Why is (numpy.nan, 1) == (numpy.nan, 1)


code:
chgsgn = dict([('+','-'), ('-','+')])
flag4 = [chgsgn[sgn]+num for (sgn,num) in zip(*[iter(equ2)]*2)]
print flag4

rawDescription: Looping copied list and deleting from original list deletes from copied list


code:
import re
from os import sep

sep2 = r'\\' if sep=='\\' else '/'

pat = '^(?:.+?%s)(.+$)' % sep2
print 'pat==%s\n' % pat

ragx = re.compile(pat)

for ss in ('Same\Same2\Foot\Ankle\Joint\Actuator\Sensor\Temperature\Value=4.123',
           'Same\Same2\Battery\Name=SomeString',
           'Same\Same2\Home\Land\Some\More\Stuff=0.34'):

    print ss
    print ragx.match(ss).group(1).replace(sep,'_')
    print ss.split(sep,1)[1].replace(sep,'_')
    print

rawDescription: Regex Python / group quantifiers

description: Regex Python / group quantifiers


code:
from packageTest import setup
print 'Setup.py version:', setup.__version__
# prints Setup.py version: 1.0

rawDescription: How do I assign a version number for a Python package using SVN and distutils?

description: Assign a version number for a Python package using SVN and distutils


code:
words_set = set(get_next_word())

print "Bob" in words_set
# True
print "the" in words_set
# True
print "thefourtheye" in words_set
# False

rawDescription: Sorting a huge text file and doing a binary search


code:
parsed = etree.fromstring(xml)
for block in parsed.xpath('//PAGE/BLOCK/TEXT'):
    print block.xpath('./TOKEN/text()')

rawDescription: Parsing Data in XML and Storing to DB in Python


rawDescription: 2D numpy array- check to see if all adjacent terms are equal


code:
cur.execute('INSERT INTO DT(Date, Time) Values (?,?)', (z[0],z[1]))
print cur.fetchone()
cur.execute('SELECT Date, Time from DT')
print cur.fetchone()

rawDescription: Trying to log date and time into sqlite3


code:
import re

vowels = {"a", "e", "i", "o", "u", "A", "E", "I", "O", "U"}
pattern = re.compile("[AEIOUaeiou]")

def intersection():
    return bool(vowels.intersection("TWYNDYLLYNGS"))

def any_version():
    return any(char in vowels for char in "TWYNDYLLYNGS")

def re_version():
    return bool(pattern.search("TWYNDYLLYNGS"))

def disjoint():
    return vowels.isdisjoint("TWYNDYLLYNGS")

from timeit import timeit

print timeit("intersection()", "from __main__ import intersection, vowels")
print timeit("any_version()", "from __main__ import any_version, vowels")
print timeit("re_version()", "from __main__ import re_version, vowels")
print timeit("disjoint()", "from __main__ import disjoint, vowels")

rawDescription: Check presence of Vowels in word, python


code:
# your code:
d={}
for word in words:
   if word[0].lower() in 'aeiou':
       d.setdefault(word[0].lower(),[]).append(word)

# a side effect set comprehension:
index={}
r={index.setdefault(word[0].lower(),[]).append(word) for word in words
        if word[0].lower() in 'aeiou'}

print r
print [(k, len(d[k])) for k in sorted(d.keys())]
print [(k, len(index[k])) for k in sorted(index.keys())]

rawDescription: Append to a dict of lists with a dict comprehension


rawDescription: sort a 2D list first by 1st column and then by 2nd column

description: Sort a 2D list first by 1st column and then by 2nd column


code:
su = ur'"/\"' # Raw version
''.join(u'\\u{:04X}'.format(ord(c)) for c in su)
su = u'"/\\"' # Escaped version
''.join(u'\\u{:04X}'.format(ord(c)) for c in su)

rawDescription: Converting a unicode string to see unicode code points


code:
frameMat=cv.GetMat(frameImg)
print "mat ", mat[3,1]
for x in xrange(frameMat.cols):
    for y in xrange(frameMat.rows):
        # just an example, multiply all 3 components by 0.5
        frameMat[y, x] = tuple(c*0.5 for c in frameMat[y, x])
cv.ShowImage( "My Video Window",  frameMat )

rawDescription: What's the fastest way to iterate over a CvMat in Python using OpenCV?


code:
for row in sorted(split_processes[1:], key=itemgetter(10)):
    print " ".join(row[10:])

rawDescription: Python sort string array with subtring

description: Python sort string array with subtring


code:
def paliPrime(n):
    fmt = '%-5d'
    if n >= 1:
        print fmt % 2,
    count = 2
    i = 3
    while count <= n:
        paliPrime=True
        if str(i) == str(i)[::-1]:
            for a in range(2,i):
                if i%a==0:
                    paliPrime=False
                    break
            if paliPrime:
                print fmt % i,
                if count%10 == 0:
                    print
                count += 1
        i += 2
    # add a newline at the end if we haven't done so already
    if count%10 != 1:
        print

rawDescription: Find palindromic primes in Python

description: Find palindromic primes in Python


code:
import fileinput
import re

pattern = re.compile('\d+')
for line in fileinput.input('input.txt', inplace=True):
    if line:
        print pattern.sub(lambda m: str(int(m.group(0)) + 1), line)

rawDescription: Increment all numbers in a file

description: Increment all numbers in a file


code:
for item in selector.xpath('.//*[@itemscope]'):
    print "Item:", item.xpath('@itemtype').extract()
    for property in item.xpath('.//*[@itemprop]'):
        print "Property:",
        print property.xpath('@itemprop').extract(),
        print property.xpath('string(.)').extract()
        for position, attribute in enumerate(property.xpath('@*'), start=1):
            print "attribute: name=%s; value=%s" % (
                property.xpath('name(@*[%d])' % position).extract(),
                attribute.extract())
        print
    print

rawDescription: what would be the right way of doing getallAttributes()

description: What would be the right way of doing getallAttributes()


code:
import os

def dir_or_file_decorator_with_results(concatenateResultFunc):
    def dir_or_file_decorator(func):
        def newFunc(path, *args, **kwargs):
            if os.path.isdir(path):
                results = []
                filenames = os.listdir(path)
                for filename in filenames:
                    filepath = os.path.join(path,filename)
                    results.append(func(filepath, *args, **kwargs))
                return concatenateResultFunc(results)
            else:
                return func(path, *args, **kwargs)
        return newFunc
    return dir_or_file_decorator

#Our function to concatenate the results in case of a directory
def concatenate_results(results):
    return ','.join(results)

#We pass the function used to concatenate the results in case of a directory when we apply to decorator
#What happens is that we create a new dir_or_file_decorator that uses the specified concatenateResultFunc
#That newly created decorator is then applied to our function
@dir_or_file_decorator_with_results(concatenate_results)
def get_file_name_and_args(extra, path):
    return extra + ' -> ' + path

#Test again
print get_file_name_and_args(r'c:\testdir', 'extra for test 1')

#Output:
# c:\testdir\testfile1.txt -> extra for test 1,c:\testdir\testfile2.txt -> extra for test 1

rawDescription: python decorator for directory

description: Python decorator for directory


code:
def censored(sentence, words):
  for word in words:
    if word in sentence:
        sentence = sentence.replace(word, "*" * len(word))
  return sentence
print censored("foo off", ("foo", "bar"))
print censored("foo off", ["foo", "bar"])

rawDescription: How to make an argument in python accept multiple inputs

description: Make an argument in python accept multiple inputs


code:
import Tkinter
import os
import leFunction

class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        self.entryVariable = Tkinter.StringVar()
        self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
        self.entry.grid(column=0,row=0,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable.set(u"")

        self.labelVars = []
        for j in range (1,10):
            labelVar = Tkinter.StringVar()
            labelVar.set(j)
            self.labelVars.append(labelVar)
            label = Tkinter.Label(self,textvariable=labelVar,
                              anchor="w",fg="black",bg="white")

            label.bind("<1>", lambda event, text="sup": \
                          self.click_link(event, text))

            label.grid(column=0,row=j,columnspan=2,sticky='EW')

        self.grid_columnconfigure(0,weight=1)
        self.resizable(True,False)
        self.update()
        self.geometry(self.geometry())
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)

    def OnPressEnter(self,event):
        #Result is a list
        Result = leFunction.aFunction(self.entryVariable.get())

        for i in range (0,len(Result)):
            print "result",i,":",Result[i]
            self.labelVars[i].set(Result[i])
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)

    def click_link(self, event, text):
        print "you clicked '%s'" % text
        #os.system("open "+file)

if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('my application')
    app.mainloop()

rawDescription: Set Tkinter label texts as elements of list


code:
import rdflib
from rdflib.Graph import Graph

conj=rdflib.ConjunctiveGraph()

NS=rdflib.Namespace("http://example.com/#")
NS_CTX=rdflib.Namespace("http://example.com/context/#")

alice=NS.alice
bob=NS.bob
charlie=NS.charlie

pizza=NS.pizza
meat=NS.meat
chocolate=NS.chocolate

loves=NS.loves
hates=NS.hates
likes=NS.likes
dislikes=NS.dislikes

love_ctx=Graph(conj.store, NS_CTX.love)
food_ctx=Graph(conj.store, NS_CTX.food)

love_ctx.add( (alice, loves, bob) )
love_ctx.add( (alice, loves, charlie) )
love_ctx.add( (bob, hates, charlie) )
love_ctx.add( (charlie, loves, bob) )

food_ctx.add( (alice, likes, chocolate) )
food_ctx.add( (alice, likes, meat) )
food_ctx.add( (alice, dislikes, pizza) )

print "Full context"
for t in conj:
    print t

print ""
print "Contexts"
for c in conj.contexts():
    print c

print "love context"
for t in love_ctx:
    print t

print "food context"
for t in food_ctx:
    print t

rawDescription: Using contexts in rdflib

description: Using contexts in rdflib


rawDescription: Can someone explain what all the codes mean? PYTHON


code:
import csv

input_file = "dp-input1.csv"

with open(input_file) as fp:
    root = csv.reader(fp, delimiter='|')
    result = [row[3].split(".")[0] for row in root]
    #for row in root:
    #    tmp = row[3].split(".")[0]
    #    result.append(tmp)

print "Final result:-", result

rawDescription: Find string within a string and save it to file on BioPython


code:
from operator import itemgetter

def display(tree, depth=0):
    prepend = "\t" * depth
    for key, val in sorted(tree.items()):
        print "{0}{1}: {2}".format(prepend, key, val['value'])
        if val['children']:
            display(val['children'], depth + 1)

rawDescription: Python tab creation

description: Python tab creation


code:
def acd(val, *vag):
    print '''Given Fixed value:\t{}
    Given Variable Tuple:\t{}
    First Var Value:\t{}
    Second Var value:\t{}
    Len of var:\t{}
    Len of first var element:\t{}
    '''.format(val, vag, vag[0][0], vag[0][1], len(vag), len(vag[0]))
acd(1, (3,4))

rawDescription: Using tuples as inputs in functions

description: Using tuples as inputs in functions


code:
a = zcat_output.split('\n\n')
a.pop(len(a)-1)
c = dict()
for x in [x.splitlines() for x in a]:
    pkg = ""
    for y in [y for y in x if y.startswith('Package:') or y.startswith('Version:')]:
        z = y.split(':')
        if z[0] == 'Package':
            pkg = z[1].strip()
        else:
            c[pkg] = {z[0]: z[1].strip()}

print c.get('whois')

rawDescription: How to parse a zcat output

description: Parse a zcat output


code:
class A(object):
    def a1(self):
        """ This is an instance method. """
        print "Hello from an instance of A"

    @classmethod
    def a2(cls):
        """ This a classmethod. """
        print "Hello from class A"

class B(object):
    def b1(self):
        print A().a1() # => prints 'Hello from an instance of A'
        print A.a2() # => 'Hello from class A'

rawDescription: Python using methods from other classes

description: Python using methods from other classes


code:
for x in root.iterfind('{http://www.w3.org/2005/Atom}entry'):
    print x

rawDescription: Python lxml iterfind w/ namespace but prefix=None


code:
@static(x=1)
def f():
    x += 1

global_x = 1
def test_non_static():
    global global_x
    global_x += 1

print 'Timeit static function: %s' % timeit.timeit(f)
print 'Timeit global variable: %s' % timeit.timeit(test_non_static)

rawDescription: Python: static variable decorator

description: Python: static variable decorator


code:
def f(*args): print args

f(1, 2)
f()
def add(a, b): return a + b

add(*[4, 3])
add(**{'b': 5, 'a': 9})

rawDescription: Unable to understand this python decorator


code:
import gps_data.GpsPoller
import Queue

g = GpsPoller()
g.start()
run = True
while run:
    try:
        # Get item for the GpsPoller queue
        gpsinfo = g.queue.get(True, 1) # blocking with a timeout of 1 sec
        print 'gpsinfo:', gpsinfo
        g.queue.task_done() # tell the queue that the task is done (you are done using gpsinfo)
    except Queue.Empty:
        print 'Queue is Empty'
        run = False

rawDescription: How to make the result from continous script a callable object for another

description: Make the result from continous script a callable object for another


code:
print 'Employee order:'
for k, v in output.items():
    values = ','.join(v)
    print '\t{} controls {}'.format(k, values)

rawDescription: creating dictionaries in python

description: Creating dictionaries in python


code:
import envoy
r = envoy.run('unrar e {0}'.format(zfilename))
if r.status_code > 0:
    print r.std_err
print r.std_out

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

description: Call WinRar in Python on Windows? STILL Problematic


code:
from pprint import pprint
def addBits2Tree( bits, tree ):
    if len(bits) == 1:
        tree.append( {'name':bits[0]} )
    else:
        for t in tree:
            if t['name']==bits[0]:
                addBits2Tree( bits[1:], t['contents'] )
                return
        newTree = []
        addBits2Tree( bits[1:], newTree )
        t = {'name':bits[0], 'contents':newTree}
        tree.append( t )

def addPath2Tree( path, tree ):
    bits = path.split("/")
    addBits2Tree( bits, tree )

tree = []
for p in b:
    print p
    addPath2Tree( p, tree )
pprint(tree)

rawDescription: creating nested dictionary from flat list with python

description: Creating nested dictionary from flat list with python


code:
myDict = {}
with open('a.txt') as f:
    currentList = []
    for line in f:
        if 'Group' in line:
            if currentList:
                myDict[currentGroup] = currentList
                currentList = []
            currentGroup = line[line.index('=')+1:].strip()
        else:
            currentList.append( float(line.strip()) )
    if currentList:
        myDict[currentGroup] = currentList

print myDict

rawDescription: parsing a text file, store a value and create a list and save this into a dictionary python

description: Parsing a text file, store a value and create a list and save this into a dictionary python


code:
my_list = ["hello"] * 100

def v1():
    for x in my_list[::-1]:
        pass

def v2():
    for x in reversed(my_list):
        pass

print timeit.timeit(v1)
print timeit.timeit(v2)

rawDescription: reversed() vs. xrange()

description: Reversed() vs. xrange()


code:
startTime = time.time()
#long running code here
print "Time taken {}".format(time.time() - startTime)

rawDescription: Create a stopwatch (not a countdown or timer) without using Tk python


rawDescription: Diagonal Grid Traverse in Python

description: Diagonal Grid Traverse in Python


code:
config = ConfigParser.RawConfigParser()
config.read('config')

try:
    your_value = config.get("Config", "your_value")
    print your_value
except ConfigParser.NoOptionError:
    print "'your_value' isn't defined in the config file!"

rawDescription: How to Mask Twilio/Python Variable

description: Mask Twilio/Python Variable


code:
print "Least distance: ", data[0][0]
print "Index of kw1 and kw2: ", data[0][1:]
print "Number of occurences: ", len(data)

rawDescription: [tricky]Searching for multiple occurrences of word pairs based on vicinity. Python


code:
foo = MyThing.objects.order_by('?')[0] # get some random object
print 'My object is a ',foo.get_tag_display()

rawDescription: State object in django

description: State object in django


code:
least = min(the_list)
print least[1]

rawDescription: List of tuples with numbers and strings

description: List of tuples with numbers and strings


code:
from pyparsing import makeHTMLTags, withAttribute, SkipTo
import urllib

# get the HTML from your URL
url = "http://www.whitecase.com/Attorneys/List.aspx?LastName=&FirstName="
page = urllib.urlopen(url)
html = page.read()
page.close()

# define opening and closing tag expressions for <td> and <a> tags
# (makeHTMLTags also comprehends tag variations, including attributes,
# upper/lower case, etc.)
tdStart,tdEnd = makeHTMLTags("td")
aStart,aEnd = makeHTMLTags("a")

# only interested in tdStarts if they have "class=altRow" attribute
tdStart.setParseAction(withAttribute(("class","altRow")))

# compose total matching pattern (add trailing tdStart to filter out
# extraneous <td> matches)
patt = tdStart + aStart("a") + SkipTo(aEnd)("text") + aEnd + tdEnd + tdStart

# scan input HTML source for matching refs, and print out the text and
# href values
for ref,s,e in patt.scanString(html):
    print ref.text, ref.a.href

rawDescription: How can I translate this XPath expression to BeautifulSoup?

description: Translate this XPath expression to BeautifulSoup


code:
print divmod(b, test[1])        # ok, though not the element you want
print divmod(b, test[2])        # ok, though not the element you want
print divmod(b, test[5])        # out of range!
print divmod(b, test[10])       # out of range!

rawDescription: Python: list index out of range error; certain list elements work


code:
import wx

import wx.html
def OnClickWord(e):
    print "You Clicked:",e.GetLinkInfo().GetHref()
    return
class MyHtmlFrame(wx.Frame):

    def __init__(self, parent, title):

        wx.Frame.__init__(self, parent, -1, title)

        html = wx.html.HtmlWindow(self)

        #if "gtk2" in wx.PlatformInfo:

        html.SetStandardFonts()

        html.SetPage(
        "" #sorry no css support :/
        "Word1 word 2 wizard of oz.")

app = wx.PySimpleApp()

frm = MyHtmlFrame(None, "Simple HTML")
frm.Bind(wx.html.EVT_HTML_LINK_CLICKED,OnClickWord)
frm.Show()

app.MainLoop()

rawDescription: Mouse events on text in Python using wxPython


code:
ordered = sorted(items, key=lambda (_, value): value) # Not Python 3

# or,
ordered = sorted(items, key=lambda x: x[1])

# or,
import operator
ordered = sorted(items, key=operator.itemgetter(1))

rawDescription: Extract information from defaultdict

description: Extract information from defaultdict


code:
x = [(1, u'Category'), (2, u'Item'), (3, u'SubItem'), (4, u'N/A'), (1, u'Features'), (2, u'Tool Baseline')]
for pair in x:
  print pair[0]*'\t' + pair[1]

rawDescription: File formatting-variable based indentation


code:
import sys

def get_stats(l):
    s = sum(l)
    return (
        s,
        float(s) / len(l),
        min(l),
        max(l)
    )

branches = [i for i in xrange(10000000)]

print get_stats(branches)

rawDescription: Unifying similar functions in one

description: Unifying similar functions in one


code:
import pandas as pd

df = pd.DataFrame({'job': ['a', 'a', 'b', 'b', 'c'], 'gender':['M', 'F', 'M', 'F', 'M'], 'income':[300, 200, 450, 400, 350]})

compare_income_by_gender_df = df.pivot(index='job', columns='gender', values='income')

print compare_income_by_gender_df

rawDescription: Compute difference between rows in pandas dataframe


code:
def dictionary_to_list(data):
    result = {}
    for key, value in data.items():
        if isinstance(value, list):
            for index in value:
                result[index] = key
        else:
            result[value] = key
    return [result[key] for key in sorted(result)]

def list_to_dictionary(data):
    result = {}
    for index, char in enumerate(data):
        result.setdefault(char, [])
        result[char].append(index)
    return dict((key, value[0]) if len(value) == 1 else (key, value) for key, value in result.items())

dictData = {'a':3, 'b':0, 'c':[2,4], 'd':[1,5]}
listData = ['b', 'd', 'c', 'a', 'c', 'd']

print dictionary_to_list(dictData)
print list_to_dictionary(listData)

rawDescription: Converting dictionaries to list sorted by values, with multiple values per item


rawDescription: How can I read a text file and replace numbers?

description: Read a text file and replace numbers


code:
for name, group in df.groupby(["Cluster_id", "User"]):
    print 'group name:', name
    print 'group rows:'
    print group
    print 'counts of Quality values:'
    print group["Quality"].value_counts()
    raw_input()

rawDescription: Counting particular occurences in python in csv file


rawDescription: comparing strings and calculating occurance

description: Comparing strings and calculating occurance


code:
df2['Currency'] = df.set_index('Date')['Currency']
df2['Date'] = df2.index
df2 = df2.reset_index(drop=True)
df2 = df2[['Currency','Date','1Y','2Y','3Y']]
print df2

rawDescription: Subtracting rows in panda having different column types


code:
import my_module
my_module.fun('foo')
print mykey

rawDescription: save variable to interactive namespace from python debugger

description: Save variable to interactive namespace from python debugger


code:
if __name__ == '__main__':
  root = Directory('/home/plg')
  root.refresh()
  root.helper.join()

  for d in [root] + root.dirs:
    for f in d.files:
      print f

rawDescription: updating file path list in a fast changing directory

description: Updating file path list in a fast changing directory


code:
import sympy
from sympy.abc import d,f,r,l
equation = sympy.Eq( ((1-d)**2*d*r)/(2*f), l )

print sympy.solve(equation,"f")
print sympy.solve(equation,"r")
print sympy.solve(equation,"d")

rawDescription: Solve 2 sides of equation with SymPy

description: Solve 2 sides of equation with SymPy


code:
try:
    # Notice that I got rid of str(Lookup)
    # raw_input always returns a string
    print Guide[Lookup]
# KeyErrors are generated when you try to access a key in a dict that doesn't exist
except KeyError:
    print 'Key not found.'

rawDescription: Python dictionary failsafe

description: Python dictionary failsafe


code:
from math import atan2, cos, pi, sin

class GetMinimumAreaRectangle(object):
    """ functor to find length, width, and area of the smallest rectangular
        area of the given convex hull """
    def __call__(self, hull):
        self.hull = hull
        mostfar = self._mostfar  # local reference
        n = len(hull)
        min_area = 10**100  # huge value
        iL = iR = iP = 1  # indexes left, right, opposite
#        print '    {:>2s} {:>2s} {:>2s} {:>2s} {:>9s}'.format(
#                   'i', 'iL', 'iP', 'iR', 'area')
        for i in xrange(n-1):
            dx = hull[i+1][0] - hull[i][0]  # distance on x axis
            dy = hull[i+1][1] - hull[i][1]  # distance on y axis
            theta = pi-atan2(dy, dx)   # get orientation angle of the edge
            s, c = sin(theta), cos(theta)
            yC = hull[i][0]*s + hull[i][1]*c
            xP, yP, iP = mostfar(iP, n, s, c, 0, 1)
            if i==0: iR = iP
            xR, yR, iR = mostfar(iR, n, s, c,  1, 0)
            xL, yL, iL = mostfar(iL, n, s, c, -1, 0)
            l, w = (yP-yC), (xR-xL)
            area = l*w
#            print '    {:2d} {:2d} {:2d} {:2d} {:9.3f}'.format(i, iL, iP, iR, area)
            if area < min_area:
                min_area, min_length, min_width = area, l, w
        return (min_length, min_width, min_area)

    def _mostfar(self, j, n, s, c, mx, my):
        """ advance j to extreme point """
        hull = self.hull  # local reference
        xn, yn = hull[j][0], hull[j][1]
        rx, ry = xn*c - yn*s, xn*s + yn*c
        best = mx*rx + my*ry
        while True:
            x, y = rx, ry
            xn, yn = hull[(j+1)%n][0], hull[(j+1)%n][1]
            rx, ry = xn*c - yn*s, xn*s + yn*c
            if mx*rx + my*ry >= best:
                j = (j+1)%n
                best = mx*rx + my*ry
            else:
                return (x, y, j)

if __name__ == '__main__':

    hull= [(560023.44957588764, 6362057.3904932579),
           (560023.44957588764, 6362060.3904932579),
           (560024.44957588764, 6362063.3904932579),
           (560026.94957588764, 6362068.3904932579),
           (560028.44957588764, 6362069.8904932579),
           (560034.94957588764, 6362071.8904932579),
           (560036.44957588764, 6362071.8904932579),
           (560037.44957588764, 6362070.3904932579),
           (560037.44957588764, 6362064.8904932579),
           (560036.44957588764, 6362063.3904932579),
           (560034.94957588764, 6362061.3904932579),
           (560026.94957588764, 6362057.8904932579),
           (560025.44957588764, 6362057.3904932579),
           (560023.44957588764, 6362057.3904932579)]

    gmar = GetMinimumAreaRectangle()  # create functor object
    print "dimensions and area of smallest enclosing rectangular area:"
    print "  {:.3f}(L) x {:.3f}(W) = {:.3f} area".format(*gmar(hull))  # use it

rawDescription: Python: implement a script in a function. Some suggestions


code:
import csv
from itertools import groupby, dropwhile

goal = 'bob'
ids = {}

with open('my_data.csv') as ifile:
    reader = csv.reader(ifile)
    for key, rows in groupby(reader, key=lambda r: r[0]):
        matched_rows = list(dropwhile(lambda r: r[2] != goal, rows))
        if len(matched_rows) > 1:
            ids[key] = [row[1] for row in matched_rows[1:]]

print ids

rawDescription: Getting values from file if match found for lines with same id in Python


rawDescription: What is the best way to sort this list?

description: What is the best way to sort this list


code:
from dis import dis

class A(object):
  def __pick(self):
      print "1"

  def doitinA(self):
      self.__pick()

class B(A):
  def __pick(self):
      print "2"

  def doitinB(self):
      self.__pick()

b = B()
b.doitinA() # prints 1
b.doitinB() # prints 2

dis(A.doitinA)
print
dis(B.doitinB)

rawDescription: How do I override a parent class's functions in python?

description: Override a parent class's functions in python


code:
list = [['apples', 'oranges', 'cherries', 'banana'],
        ['Alice', 'Bob', 'Carol', 'David'],
        ['dogs', 'cats', 'moose', 'goose']]

def swaplist(list):
    result_list = []
    for index in range(0, len(list[0])):
        temp_list = []
        for i in list:
            temp_list.append(i[index])
        result_list.append(temp_list)
    return result_list

print swaplist(list)

rawDescription: Print List Of List. Go from list[x][y] to list[y][x]


code:
env.hosts = [ 'localhost', '127.0.0.1']

def remoteRun():
    print "ENV %s" %(env.hosts)
    out = run('uname -r')
    print "Output %s"%(out)

rawDescription: No hosts found: Fabric

description: No hosts found: Fabric


code:
def y(x):
    return x*x

def offset_function(f, desired=0):
    def newf(x):
        return f(x) - desired
    return newf

y9 = offset_function(y, 9)

for x in range(5):
    print x, y(x), y9(x)

rawDescription: Solve for the argument to a function that will produce a particular return value


code:
for s in l:
    if not isinstance(s, unicode):
        print s.decode('unicode_escape')

rawDescription: Converting a latin string to unicode in python


code:
A=np.loadtxt("C:\Users\KEMAL\Desktop\piton\test.txt")
n=3
arrs = []
for i in range(0,n):
    b=[row[i] for row in A]
    D=np.array(b)
    size=[len(D),np.max(D)]
    B=np.zeros(size)
    for i in range(len(D)):
        idx=D[i]-1
        B[i,idx]=1
        U=B.transpose(1, 0)

    arrs.append(U)

np.vstack(arrs)

rawDescription: how can I covert this to a matrix

description: Covert this to a matrix


code:
# original
print row.xpath(".//td[@class='currency']/text()").extract()
# ported
print extract_text(row.find(r'.//td[@class="currency"]'))

rawDescription: converting scrapy to lxml

description: Converting scrapy to lxml


code:
for item in h['PC3']:
    if isinstance(item, dict):
        for key in item.keys():
            if item[key][1] < 1390618305.478:
                item.pop(key, None)
            else:
                print item[key][0], key

rawDescription: How to access values inside a tuple which is inside nested dictionary

description: Access values inside a tuple which is inside nested dictionary


code:
import Tkinter as tk

class newdemo:
    def __init__(self, root):
        root.wm_title("Calendar Demo")
        self.myentry = tk.Entry(root, text="Choose date")
        self.myentry.pack()

        self.button = tk.Button(root, text="Choose date", command=self.onclick)
        self.button.pack()

    def onclick(self):
        cd = CalendarDialog(root)
        print cd.result
        self.myentry.delete(0, tk.END)
        self.myentry.insert(0, cd.result)

root = tk.Tk()
newdemo(root)
root.mainloop()

rawDescription: return function value as entry text

description: Return function value as entry text


code:
from scipy.sparse import lil_matrix, rand
from time import time as timer
from numpy import array, concatenate, empty

### sparse appending ####
def sparse_append(A):
    dim = A.shape[1]
    mat = lil_matrix(A.shape, dtype = A.dtype)

    sparse_addtime = 0
    i = 0
    for vector in A:
        st = timer()

        mat[i] = vector
        i += 1
        et = timer()
        sparse_addtime += et-st

    return sparse_addtime

#### dense append ####
def dense_append(A):
    dim = A.shape[1]
    mat = empty([0,dim])

    dense_addtime = 0

    for vector in A:
        st = timer()
        mat = concatenate((mat,vector))
        et = timer()
        dense_addtime += et-st

    return dense_addtime

### main ####
if __name__ == '__main__':
    dim = 400
    n = 200

    A = rand(n, dim, density = 0.1, format='lil')
    B = A.todense() #numpy.ndarray

    t1 = sparse_append(A)
    t2 = dense_append(B)

    print t1, t2

rawDescription: Adding a sparse vector to a sparse array in python is too slow


code:
import requests
from bs4 import BeautifulSoup

resp = requests.get('http://www.youtube.com/user/sachinabey/videos')

soup = BeautifulSoup(resp.text)
for title in soup.findAll('h3', attrs={'class': 'yt-lockup-title'}):
    print title.find('a').text.strip()

rawDescription: Extract attribute value in beautiful soup


code:
print [5] * 3
print "abc" * 3
print [1.1] * 3
print (8,) * 3

rawDescription: Star operator (*) applied to lists and integers


code:
from pyramid.request import Request

req = Request.blank('http://gdzies.w.pl/ala/ma/kota?q=1&q=2&w=3&w=4&q=5&a=0')
print req.params.items()

rawDescription: Ordering of query parameters in pyramid.request object


code:
a_dict = {'key1': 1, 'key2': 2, 'key3missing': 3}

print set([a_dict[key] for key in ['key1','key2','key3'] if key in a_dict])

# prints: set([1, 2])

rawDescription: What's the most pythonic way to write this filtered list comprehension?


rawDescription: Removing one or multiple keys from a dictionary


code:
def scrolled(scrollbar, value):
    if value == scrollbar.maximum():
        print 'reached max' # that will be the bottom/right end
    if value == scrollbar.minimum():
        print 'reached min' # top/left end

scrollBar = listview.verticalScrollBar()
scrollBar.valueChanged.connect(lambda value: scrolled(scrollBar, value))

rawDescription: Python PyQt scrollbar in the end position event


code:
def extract_delimited_data(data, begin_delimiter, end_delimiter):
    s = data.find(begin_delimiter)

    while s != -1:
        e = data.find(end_delimiter, s + len(begin_delimiter))

        s += len(begin_delimiter)
        yield data[s:e]

        s = data.find(begin_delimiter, e + len(end_delimiter))

data = 'foo<bar>hello<world>'

print list(extract_delimited_data(data, '<', '>'))

rawDescription: Trying to pull useful info blocks from text file


rawDescription: Why is python rounding this value?

description: Why is python rounding this value


code:
from graph_tool import GraphView, generation

g = generation.complete_graph(5)

# select some vertices
vfilt = g.new_vertex_property('bool');
vfilt[0] = True
vfilt[1] = True
vfilt[3] = True

sub = GraphView(g, vfilt)

print [(g.vertex_index[e.source()], g.vertex_index[e.target()])
           for e in sub.edges()]

rawDescription: Filtering a subgraph in graph-tool

description: Filtering a subgraph in graph-tool


code:
import struct

f=open('myfile.binary', 'rb')
contents=f.read(5)
data=list(contents)
data[0:4]=struct.pack(">i", 0x12345678)
print data

rawDescription: Python, how to put 32-bit integer into byte array


code:
import socket

HOST = ''
PORT = 12349
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(5)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    if not data: break
    conn.sendall(data)
conn.close()

rawDescription: How to use client socket as a server socket python

description: Use client socket as a server socket python


rawDescription: Regex and Octal Characters

description: Regex and Octal Characters


code:
from sklearn.feature_extraction.text import TfidfVectorizer
all_lines = ["This is an example doc", "Another short example document .", "Just a third example"]

tf = TfidfVectorizer(analyzer='word')
tfidf_matrix =  tf.fit_transform(all_lines)
query_string = "This is a short example string"
print "Query String:"
print tf.transform([query_string])
print "Example doc:"
print tf.transform(["This is a short example doc"])

rawDescription: Calculating the TF-IDF of a query string over a trained set of documents


code:
#!/usr/bin/env python
from mpi4py import MPI

def addCounter(counter1, counter2, datatype):
    for item in counter2:
        if item in counter1:
            counter1[item] += counter2[item]
        else:
            counter1[item] = counter2[item]
    return counter1

if __name__=="__main__":

    comm = MPI.COMM_WORLD

    if comm.rank == 0:
        myDict = {'a':1, 'c':"Hello "}
    else:
        myDict = {'c':"World!", 'd':3}

    counterSumOp = MPI.Op.Create(addCounter, commute=True)

    totDict = comm.allreduce(myDict, op=counterSumOp)
    print comm.rank, totDict

rawDescription: Summing Python Objects with MPI's Allreduce


code: print sorted(example_dict.items(),key=lambda x: len(set(x[1])))

rawDescription: Python: Sorted representation of dict of lists, sorted by number of elements in list, after removing duplicates


code:
import re

def next_tag(s, tag):
    i = -1
    while True:
        try:
            i = s.index(tag, i+1)
        except ValueError:
            return
        yield i

a = "<list><list-item>First level<list><list-item>Second level</list-item></list></list-item></list>"

a = a.replace("<list-item>", "* ")

for LEVEL, ind in enumerate(next_tag(a, "<list>")):
    a = re.sub("<list>", "\n" + LEVEL * "\t", a, 1)

a = a.replace("</list-item>", "")
a = a.replace("</list>", "")

print a

rawDescription: Converting HTML list (<li>) to tabs (i.e. indentation)


code:
l2=[]
with open('list_data2.txt') as f:
  for line in f:
   l=line.strip().split(',')
   l2.append(sorted(l[:2])+sorted(map(int,l[2:]),reverse=True))

print l2

rawDescription: How do I sort a numerical list with names attached?

description: Sort a numerical list with names attached


code:
class turn(object):
    def __init__(self):
        self.currentTurn = self.TURNS[self.curr]

    def getTurn(self):
        return self.TURNS[self.curr]

    def switch(self):
        cls = type(self)
        cls.curr = 1 - cls.curr

        if self.curr == 0:
            print "It's your move! \n"

        self.currentTurn = self.TURNS[self.curr]

rawDescription: Variable naming in Python classes

description: Variable naming in Python classes


code:
with SkipContext():
    with SomeContext(1):
        print "body"

rawDescription: How to stop statements within a Python with context from executing?

description: Stop statements within a Python with context from executing


code:
for item in MusicTag.objects.all():
    print item.tag , item.useritem_set.count()

rawDescription: How to design models.py when I want to store the complex data like this?

description: Design models.py when I want to store the complex data like this


code:
a_list  = re.sub("[^a-zA-Z ]","",s).split()#4957 words (lorum ipsum generated)
search_space = set("dog cat fish bear walrus".split())

def joranbeasley():
    return search_space.intersection(a_list)

def stephenPochmann():
    for needle in search_space:
        if needle in s: print needle

import timeit
print "Stephen Timeit:",timeit.timeit(stephenPochmann,number=1000)
print "joran Timeit:",timeit.timeit(joranbeasley,number=1000)

rawDescription: Optimize runtime of calling 'in' many times on same string


code:
import os
import urllib2
from csv import DictReader

def moo(DocIn, DocOut, WebAddy):
    os.rename(DocIn, DocOut)
    file(DocIn, "wb").write(urllib2.urlopen(WebAddy).read())
    hosts0 = open(DocIn,"r")
    hosts1 = open(DocOut,"r")
    lines1 = hosts0.readlines()
    for i,lines2 in enumerate(hosts1):
        if lines2 != lines1[i]:
            print "line ", i, " in hosts1 is different \n"
            print lines2

with open('Infodoc 2.txt','r') as infile:
    incsv = DictReader(infile)
    for row in incsv:
        print row['Publisher']
        DocIn = row['DocIn']
        DocOut = row['DocOut']
        WebAddy = row['WebAddy']
        moo(DocIn, DocOut, WebAddy)

rawDescription: Python, need to input csv file into a loop


code:
for xindex, xvalue in enumerate(xarr):
  darr = {}
  for pindex, pvalue in enumerate(parr):
    dvp = math.fabs(xvalue - pvalue)
    darr[dvp] = {'xindex': xindex, 'pindex': pindex}
  mini = min(darr.keys())
  minix = darr[mini]['xindex']
  minip = darr[mini]['pindex']
  minindex = darr.keys().index(mini)

  print "minimum_index> {0}, is the difference of xarr[{1}] and parr[{2}]".format(minindex, minix, minip)
  darr.clear()

rawDescription: find value of forloop at which event occurred Python

description: Find value of forloop at which event occurred Python


code:
for n in unformatted_returns:
    if date_new == n[0]:
        print "found date"
        rate_of_return_calc(date, date_new)
        break
else:
    print "date {0} not found".format(date_new)
    day_accumulater(d, m, y, date_new, date)

rawDescription: Python: for each tuple in list, check if string in tuple


code:
class URL_Cleaner(object):
    def __init__(self, old_strings, new_strings, term):
        """These are all instance objects"""
        self.old_strings = old_strings
        self.new_strings = new_strings
        self.term = term

    new_strings = [] # this is a class object

    def delete_term(self, new_strings):
        """You never actually call this function! It never does anything!"""
        for self.string in self.old_strings:
            if self.term in string:
                new_string = string.replace(term, "")
                self.new_strings.append(new_string)
            else:
                self.new_strings.append(string)
        return self.new_strings

    print "\n" .join(new_strings) #for checking; will be removed later
    # this is referring the class object, and will be evaluated when
    # the class is defined, NOT when the object is created!

rawDescription: Class returns empty list

description: Class returns empty list


code:
from graphics import *

def example_callback():
    print "I love unicorns!"

def main():
    # win is a Canvas with a setMouseHandler method
    win = GraphWin("My Circle", 100, 100)
    c = Circle(Point(50,50), 10)
    c.draw(win)

    #Add a callback to the canvas
    c.cavas.setMouseHandler(example_callback)
    # or win.setMouseHandler(example_callback)

    win.getMouse() # Pause to view result
    win.close()    # Close window when done

main()

rawDescription: Creating two button methods Python

description: Creating two button methods Python


code:
from xml.dom import minidom

XML = """
<root>
   <x>abc</x>
   <y>123</y>
</root>"""

dom = minidom.parseString(XML)
x = dom.getElementsByTagName('x')[0]

print x
print x.ownerDocument

rawDescription: Get Document from Node or Element objects with minidom


code:
def _previewMode(command):
    print command

def _updateMode(command):
    exec(command)

rawDescription: Python scripting strategies: running script twice vs excec


code:
from suds.client import Client
url="http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL"
client = Client(url)
print client ## shows the details of this service

result = client.service.GetWeatherInformation()
print result

rawDescription: Python : Soap using requests

description: Python : Soap using requests


code:
tests = Tests()
tests.parsetest()
if condition:
    tests.retrievetest()
print '\n'.join(tests.messages)

rawDescription: Pythonic way to handle updating status string between functions?


code:
li=['sight', 'first', 'love', 'was', 'at', 'It']

d={}
for word in li:
    d.setdefault(len(word), []).append(word)

result=[d[n] for n in sorted(d, reverse=True)]

print result
# [['sight', 'first'], ['love'], ['was'], ['at', 'It']]

rawDescription: Group a list by word length

description: Group a list by word length


code:
import re

s = "There was a cow at home. home=mary cowname=betsy date=10-jan-2013"

d = {}

def add(m):
    d[m.group(1)] = m.group(2)

s = re.sub(r'(\w+)=(\S+)', add, s)
d['theSentence'] = s.strip()

print d

rawDescription: Python tokenize sentence with optional key/val pairs


code:
import threading, time, random

class Key(object):
    results={}
    def __init__(self,refresh,name):
        self.refresh=refresh
        self.name=name
        self.t0=time.time()
        self.t=threading.Timer(refresh,self.now_what)
        self.t.start()

    def now_what(self):
        s='{}: {:6.4f}'.format(self.name,time.time()-self.t0)
        Key.results.setdefault(self.refresh,[]).append(s)
        # do the thing you want at this time ref with the Key...

    def time_left(self):
        return max(self.t0+self.refresh-time.time(),0)

keys=[Key(random.randint(2,15),'Key {}'.format(i)) for i in range(1,1001)]
t=time.time()
while any(key.time_left() for key in keys):
    if time.time()-t > 1:
        kc=filter(lambda x: x, (key.time_left() for key in keys))
        if kc:
            tmpl='{} keys; max life: {:.2f}; average life: {:.2f}'
            print tmpl.format(len(kc),max(kc),sum(kc)/len(kc))
            t=time.time()

for k in sorted(Key.results):
    print '\nKeys with {} secs life:'.format(k)
    for e in Key.results[k]:
        print '\t{}'.format(e)

rawDescription: Best way to reset keys which expires in few minutes in python

description: Reset keys which expires in few minutes in python


code:
def jsonp(func):
    """Wraps JSONified output for JSONP requests."""
    @wraps(func)
    def decorated_function(*args, **kwargs):
        callback = request.args.get('callback', False)
        if callback:
            data = str(func(*args, **kwargs).data)
            content = str(callback) + '(' + data + ')'
            mimetype = 'application/javascript'
            return current_app.response_class(content, mimetype=mimetype)
        else:
            return func(*args, **kwargs)
    return decorated_function

@app.route('/ping/')
@jsonp
def ping():

    if 'user_id' in session:
        print "session ping: %s" % session
        return jsonify(logged_in='true')

    else:
        print "session ping: not logged in"
        return jsonify(logged_in="false")

rawDescription: Access Flask session cookies from custom domain


code: print '{:<10} {:<10} {:>2}\' {:>2}" {:>6}'.format(player["firstname"], player["lastname"], player["h_feet"], player["h_inches"], player["weight"])

rawDescription: Can I create an aligned table with an if statement in python?

description: Create an aligned table with an if statement in python


code:
import os
import re
res = [f for f in os.listdir() if re.search(r'(abc|123|a1b).*\.txt$', f)]
for f in res:
    print f

rawDescription: regular expression using in glob.glob of python

description: Regular expression using in glob.glob of python


code:
number = 0x2080 # = 8320 in decimal
unicode_char = unichr(number)
print unicode_char
# output: ₀

rawDescription: Generate python unicode character from int value


code:
#! /usr/bin/env python

sample_interval = 10
filename="capture.cap"

from scapy.all import *
from collections import Counter

traffic = Counter()
hosts = {}

def human(num):
    for x in ['', 'k', 'M', 'G', 'T']:
        if num < 1024.: return "%3.1f %sB" % (num, x)
        num /= 1024.
    return  "%3.1f PB" % (num)

def traffic_monitor_callback(pkt):
    if IP in pkt:
        pkt = pkt[IP]
        traffic.update({tuple(sorted(map(atol, (pkt.src, pkt.dst)))): pkt.len})

# A trick I like: don't use rdpcap() that would waste your memory;
# iterate over a PcapReader object instead.
for p in PcapReader("capture.cap"):
    traffic_monitor_callback(p)

for (h1, h2), total in traffic.most_common(10):
    h1, h2 = map(ltoa, (h1, h2))
    for host in (h1, h2):
        if host not in hosts:
            try:
                rhost = socket.gethostbyaddr(host)
                hosts[host] = rhost[0]
            except:
                hosts[host] = None
    h1 = "%s (%s)" % (hosts[h1], h1) if hosts[h1] is not None else h1
    h2 = "%s (%s)" % (hosts[h2], h2) if hosts[h2] is not None else h2
    print "%s/s: %s - %s" % (human(float(total)/sample_interval), h1, h2)

rawDescription: Calculate bandwidth usage per IP with scapy, iftop-style


code:
import re
symbolList=[',' , '&' , '8' , '9' , '.']
regex = '|'.join(map(re.escape, symbolList))

words = ['ha,di' , 'dea&r','1.2']
for i in words:
    print re.split(regex, i)

rawDescription: how to split a string by characters which occur in another list?

description: Split a string by characters which occur in another list


code:
from mpl_toolkits.mplot3d import Axes3D    # @UnusedImport
from PySide import QtGui, QtCore

import matplotlib
import matplotlib.animation as animation
import sys
# specify the use of PySide
matplotlib.rcParams['backend.qt4'] = "PySide"

# import the figure canvas for interfacing with the backend
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg \
                                                            as FigureCanvas
from matplotlib.figure import Figure

import numpy as np
from math import pi, cos, sin

class Ui_MainWindow(object):
    def setupUi(self, MainWindow, direction, maxRadius):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 500)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Maximum,
                                       QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(
                            self.pushButton.sizePolicy().hasHeightForWidth())
        self.pushButton.setSizePolicy(sizePolicy)
        self.pushButton.setMaximumSize(QtCore.QSize(150, 16777215))
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setText("Change Direction")
        self.horizontalLayout.addWidget(self.pushButton)
        self.frame = QtGui.QFrame(self.centralwidget)
        self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtGui.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.gridLayout = QtGui.QGridLayout(self.frame)
        self.gridLayout.setObjectName("gridLayout")
        self.horizontalLayout.addWidget(self.frame)
        # ------
        self.mplWidget = MplWidget(None, direction, maxRadius)
        self.gridLayout.addWidget(self.mplWidget)
        # ------
        MainWindow.setCentralWidget(self.centralwidget)

        QtCore.QMetaObject.connectSlotsByName(MainWindow)

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.direction = 'up'
        self.maxRadius = 0.3
        self.ui.setupUi(self, self.direction, self.maxRadius)
        self.ui.pushButton.clicked.connect(self.changeStuff)
        self.animation = self.ui.mplWidget.animate()

    def changeStuff(self):
        self.ui.mplWidget.close_event()  # mpl clean up
        self.ui.mplWidget.deleteLater()  # QT cleanup
        self.ui.gridLayout.removeWidget(self.ui.mplWidget)

        dirs = {'up': 'down', 'down': 'up'}
        rads = {0.3: 1, 1: 0.3}
        self.direction = dirs[self.direction]
        self.maxRadius = rads[self.maxRadius]
        self.ui.mplWidget = MplWidget(self.ui.frame, self.direction,
                                      self.maxRadius)
        self.ui.gridLayout.addWidget(self.ui.mplWidget)
        self.animation = self.ui.mplWidget.animate()
        print self.ui.frame.children()
        print 'finished change stuff'

class MplWidget(FigureCanvas):
    def __init__(self, parent=None, direction='up', maxRadius=0.3):
        self.figure = Figure()
        super(MplWidget, self).__init__(self.figure)
        self.setParent(parent)
        self.axes = self.figure.add_subplot(111, projection='3d')
        self.axes.set_xlabel("x label")
        self.axes.set_ylabel("y label")
        self.axes.set_zlabel("z label")
        self.axes.set_xlim3d([-1, 1])
        self.axes.set_ylim3d([-1, 1])
        self.axes.set_zlim3d([-1, 1])
        self.axes.set_aspect('equal')
        if direction == 'up':
            self.c = 1
        elif direction == 'down':
            self.c = -1
        else:
            self.c = 1
        self.maxRadius = maxRadius
        self.frames = 50
        self.plot_handle = self.func_plot(self.frames)

    def func_plot(self, z):
        z /= float(self.frames) * self.c
        theta = np.arange(0, 2 * pi + pi / 50, pi / 50)
        xdata = self.maxRadius * z * np.array([cos(q) for q in theta])
        ydata = self.maxRadius * z * np.array([sin(q) for q in theta])
        zdata = z * np.ones(np.shape(xdata))
        if not hasattr(self, 'plot_handle'):
            plot_handle = self.axes.plot(xdata, ydata, zdata)[0]
        else:
            plot_handle = self.plot_handle
        plot_handle.set_data(xdata, ydata)
        plot_handle.set_3d_properties(zdata)
        return plot_handle

    def animate(self):
        return animation.FuncAnimation(
                    fig=self.figure, func=self.func_plot, frames=self.frames,
                    interval=1000.0 / self.frames, blit=False)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())

rawDescription: Deleting and Redrawing Matplotlib Animation


code:
import datetime
import traceback
from functools import partial

def db_log(*args, **kwargs): print args, kwargs # Mock

class BaseWrapper(object):
    def __init__(self, instance):
        self._instance = instance

    def __getattr__(self, attr):
        return getattr(self._instance, attr)

class DBLogWrapper(BaseWrapper):

    @staticmethod
    def _time_method(method):
        name = "{0}.{1}.{2}".format(
            method.im_class.__module__,
            method.im_class.__name__,
            method.__name__
        )

        def timed_method(*args, **kwargs):
            begin = datetime.datetime.now()
            return_val = method(*args, **kwargs)
            end = datetime.datetime.now()

            trace = traceback.format_stack()

            db_log(
                name,
                begin,
                end,
                info={
                    'args': args,
                    'kwargs': kwargs,
                    'trace': trace
                }
            )
            return return_val

        return timed_method

    def __init__(self, instance, methods):
        super(DBLogWrapper, self).__init__(instance)
        for method in methods:
            class_method = getattr(instance, method)
            wrapped_method = DBLogWrapper._time_method(class_method)
            setattr(self, method, wrapped_method)

rawDescription: How to wrap python instance methods at runtime

description: Wrap python instance methods at runtime


code: exec exp in globals()

rawDescription: Boost python, calling function objects with a namespace


code:
from sys import argv
from os.path import exists

script, filenamefrom, filenameto = argv

print "We're going to erase %r." % filenamefrom
print "If you don't want that, hit CTRL-C (^C)."
print "If you do want that, hit RETURN."

raw_input("?")

print "Now I'm going to ask you for three lines."

line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")

print "Opening the file..."
with open(filenamefrom, 'w+') as target_file:
    print "Truncating the file. Goodbye!"
    target_file.truncate()
    print "I'm going to write these to the file."
    target_file.write('%r\n%r\n%r\n' % (line1, line2, line3))

print "Now we are going to copy %r to %r!" % (filenamefrom, filenameto)

with open(filenamefrom, 'r') as target_file:
    readdata = target_file.read()
    with open(filenameto, 'w+') as to_file:
        to_file.write(readdata)

rawDescription: Python - Trying to truncate, write to and then copy the written-to file to another file


code:
from itertools import ifilter
filtered_generator = ifilter(lambda x: x.get('slug') == 'my-slug', data)

filtered_generator

for d in filtered_generator:
    print d['slug']

rawDescription: python django - search and get dictionary

description: Python django - search and get dictionary


code:
def editItem(item):
    print 'editing', item.text()

# initialize a table widget somehow
table = QTableWidget(parent)
table.setRowCount(1)
table.setColumnCount(1)

# create an item
item = QTableWidgetItem('12/1/12')
table.setItem(0, 0, item)

# if you don't want to allow in-table editing, either disable the table like:
table.setEditTriggers( QTableWidget.NoEditTriggers )

# or specifically for this item
item.setFlags( item.flags() ^ Qt.ItemIsEditable)

# create a connection to the double click event
table.itemDoubleClicked.connect(editItem)

rawDescription: Adding widgets to qtablewidget pyqt

description: Adding widgets to qtablewidget pyqt


code:
# the directory where we have all our HTML files
dir = "myDir"

# our csv file
csvFile = "silkroad.csv"

# insert the column titles to csv
csvfile = open(csvFile, 'wb')
writer = csv.writer(csvfile)
writer.writerow(["Time", "Author", "Title", "Post"])
csvfile.close()

# get a list of files in the directory
fileList = os.listdir(dir)

# define variables we need for status text
totalLen = len(fileList)
count = 1

# iterate through files and read all of them into the csv file
for htmlFile in fileList:
    path = os.path.join(dir, htmlFile) # get the file path
    processData(path) # process the data in the file
    print "Processed '" + path + "'(" + str(count) + "/" + str(totalLen) + ")..." # display status
    count = count + 1 # increment counter

rawDescription: BeautifulSoup - scraping a forum page

description: BeautifulSoup - scraping a forum page


rawDescription: Python getting all links from a google search result page


code:
from pytz import timezone, utc
eastern = timezone('US/Eastern')
utctime = utc.localize(datetime.strptime(s, "%Y-%m-%d %H:%M:%S"))
local_tz = utctime.astimezone(eastern)
print mytime.strftime("%Y.%m.%d %H:%M:%S")

rawDescription: Python datetime add

description: Python datetime add


code:
for n, obj in enumerate(objects):
    print n, obj, colors[n % len(colors)]

rawDescription: Python: Associate for loop with a list

description: Python: Associate for loop with a list


code:
a = 2
print "A.a:", a
import B
print "B.b:", B.b
from B import *
print "b in our namespace:", b

rawDescription: passing value to other module python

description: Passing value to other module python


code:
for k in l:
    try:
        del d[k]
    except KeyError:
        print 'No such key', k

rawDescription: deleting items from a dictionary with a given list

description: Deleting items from a dictionary with a given list


code:
SERVICE_NAME = 'confluence_api'

def get_login_cli(username = None, prompt = False):
    '''
    Get the password for the username out of the keyring.  If the password
    isn't found in the keyring, ask for it from the command line.
    '''

    disp_username = False

    if username is None or prompt:
        username = getpass.getuser()
        disp_username = True

    passwd = keyring.get_password(SERVICE_NAME, username)

    if passwd is None or prompt:

        if disp_username:
            print 'login: %s' % username

        passwd = getpass.getpass()
        set_password(username, passwd)

    return (username, passwd)

def set_password(username, passwd):
    '''
    Writes the password to the keyring.
    '''

    keyring.set_password(SERVICE_NAME, username, passwd)

rawDescription: Cache user remote credentials for command line tool


code:
import bs4

def my_handle_entityref(self, name):
     character = bs4.dammit.EntitySubstitution.HTML_ENTITY_TO_CHARACTER.get(name)
     if character is not None:
         data = character
     else:
         #the original code mishandles unknown entities (the following commented-out line)
         #data = "&%s;" % name
         data = "&%s" % name
     self.handle_data(data)

bs4.builder._htmlparser.BeautifulSoupHTMLParser.handle_entityref = my_handle_entityref
soup = bs4.BeautifulSoup('AT&T announces new plans')
print soup.text
soup = bs4.BeautifulSoup('AT&TOP announces new plans')
print soup.text

rawDescription: Beautiful Soup turns S&P into S&P; AT&T into AT&T; ?