code:
#!/usr/bin/python
import binascii, os.path, urllib, random, Image
from Crypto.Cipher import AES

class ECBPenguin(object):
    '''
    A penguin class
    '''
    def __init__(self, img_clr=""):
        if not img_clr:
            self.__demo_image__()
            self.img_clr = "tux_clear.bmp"
        else:
            self.img_clr = img_clr
        self.__get_header__()

    def __demo_image__(self):
        '''
        Downloads a TUX image compatible for this program: square and with size multiple of 16
        '''
        print "Downloading image..."
        image = urllib.URLopener()
        image.retrieve("http://fp-games.googlecode.com/svn/trunk/CodeWeek1/graviTux/data/tux.bmp","tux_clear.bmp")

    def __get_sizes__(self, dibheader):
        # Get image's dimensions (at offsets 4 and 8 of the DIB header)
        DIBheader = []
        for i in range(0,80,2):
            DIBheader.append(int(binascii.hexlify(dibheader)[i:i+2],16))
        self.width = sum([DIBheader[i+4]*256**i for i in range(0,4)])
        self.height = sum([DIBheader[i+8]*256**i for i in range(0,4)])

    def __get_header__(self):
        '''
        Read BMP and DIB headers from input image and write them to output image
        '''
        f_in = open(self.img_clr, 'rb')
        # BMP is 14 bytes
        bmpheader = f_in.read(14)
        # DIB is 40 bytes
        dibheader = f_in.read(40)
        self.__get_sizes__(dibheader)
        self._bmpheader = bmpheader
        self._dibheader = dibheader
        f_in.close()

    def encrypt(self, img_enc = "tux_enc.bmp", key = '0123456789abcdef'):
        '''
        Encrypt the my_penguin
        '''
        self.img_enc = img_enc
        f_in = open(self.img_clr, 'rb')
        f_out = open(img_enc, 'wb')
        f_out.write(self._bmpheader)
        f_out.write(self._dibheader)
        row_padded = (self.width * self.height * 3)
        image_data = f_in.read(row_padded)
        cleartext =  binascii.unhexlify(binascii.hexlify(image_data))

        # Initialization Vector
        IV = ''.join(chr(random.randint(0, 0xFF)) for i in range(16))
        # AES ECB mode
        mode = AES.MODE_ECB
        # Encryptor
        encryptor = AES.new(key, mode, IV=IV)
        # Perform the encryption and write output to file
        f_out.write(encryptor.encrypt(cleartext))
        f_in.close()
        f_out.close()

    def show_clr(self):
        '''
        Display cleartext penguin
        '''
        im = Image.open(self.img_clr)
        im.show()

    def show_enc(self):
        '''
        Display ciphertext penguin
        '''
        im = Image.open(self.img_enc)
        im.show()

def main():
    my_penguin = ECBPenguin()
    my_penguin.show_clr()
    my_penguin.encrypt()
    my_penguin.show_enc()

if __name__ == "__main__":
    main()

rawDescription: Produce the ECB penguin with AES in python


code:
def pairwise(iterable):
    it = iter(iterable)
    last = next(it)+next(it)
    for curr in it:
        yield last, curr
        last = last[1]+curr

def valid_pair((last, curr)):
    return last[0] in valid and last[1] in valid and curr in valid

rawDescription: Create counter for triples of subsequent letters


code:
import sys
if sys.version_info[0] >= 3:
    import tkinter as tk
else:
    import Tkinter as tk

class App(tk.Frame):

    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.dict = ['a','b','c']
        self.variable_a = tk.StringVar()
        self.optionmenu_a = tk.OptionMenu(self, self.variable_a, *self.dict)
        tk.Button(self, text="Add", command=self.func).pack()
        self.optionmenu_a.pack()
        self.pack()

    def func(self):
        menu = self.optionmenu_a["menu"]
        print self.variable_a.get() + " added"
        menu.delete(self.dict.index(self.variable_a.get()))

if __name__ == "__main__":
    root = tk.Tk()
    app = App(root)
    app.mainloop()

rawDescription: Tkinter Optionmenu-options list to be update(Upon selection, should remove that selection from options)


code:
root = lst = []
levels = 0
while True:
    lst.append([])
    lst = lst[-1]
    levels += 1
    if levels % 1000000 == 0:  # every 1 million
        print levels

rawDescription: Can python list and dictionary be nested infinitely?


code:
import os
for path, dirs, files in os.walk( root ):
    for f in files:
        print path, f, os.path.getsize( os.path.join( path, f ) )

rawDescription: What's the best way to get the size of a folder and all the files inside from python?


code:
import SimpleCV
from SimpleCV import *
display = SimpleCV.Display()
cam = Kinect()
normaldisplay = True

while display.isNotDone():

    if display.mouseRight:
        normaldisplay = not(normaldisplay)
        print "Display Mode:", "Normal" if normaldisplay else "Segmented"

    img = cam.getDepth().flipHorizontal()
    img2 = cam.getImage().flipHorizontal()
    dist = img.colorDistance(SimpleCV.Color.BLACK).dilate(2)
    segmented = dist.stretch(200,255)
    binar = segmented.binarize(200)
    erode = binar.erode(2)
    blobs = erode.findBlobs()
    if blobs:
        circles = blobs.filter([b.isCircle(0.2) for b in blobs])
        if circles:
            img.drawCircle((circles[-1].x, circles[-1].y), circles[-1].radius(),SimpleCV.Color.BLUE,3)

    if normaldisplay:
        img.show()

    else:
        segmented.show()

rawDescription: Simplecv and openkinect blob detection issues


code:
def testfunc():
    print 'Executed'
testdict = {'1': testfunc,}
testdict['1']()
Executed

rawDescription: Can you prevent a function from executing inside a dictionary mapping in python?

description: Prevent a function from executing inside a dictionary mapping in python


code:
from sqlalchemy import Integer, Table, Column, ForeignKey, \
    create_engine, String, select
from sqlalchemy.orm import Session, relationship
from sqlalchemy.ext.declarative import declarative_base

Base= declarative_base()

friendship = Table(
    'friendships', Base.metadata,
    Column('friend_a_id', Integer, ForeignKey('users.id'),
                                        primary_key=True),
    Column('friend_b_id', Integer, ForeignKey('users.id'),
                                        primary_key=True)
)

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    name = Column(String)

    # this relationship is used for persistence
    friends = relationship("User", secondary=friendship,
                           primaryjoin=id==friendship.c.friend_a_id,
                           secondaryjoin=id==friendship.c.friend_b_id,
    )

    def __repr__(self):
        return "User(%r)" % self.name

# this relationship is viewonly and selects across the union of all
# friends
friendship_union = select([
                        friendship.c.friend_a_id,
                        friendship.c.friend_b_id
                        ]).union(
                            select([
                                friendship.c.friend_b_id,
                                friendship.c.friend_a_id]
                            )
                    ).alias()
User.all_friends = relationship('User',
                       secondary=friendship_union,
                       primaryjoin=User.id==friendship_union.c.friend_a_id,
                       secondaryjoin=User.id==friendship_union.c.friend_b_id,
                       viewonly=True)

e = create_engine("sqlite://",echo=True)
Base.metadata.create_all(e)
s = Session(e)

u1, u2, u3, u4, u5 = User(name='u1'), User(name='u2'), \
                    User(name='u3'), User(name='u4'), User(name='u5')

u1.friends = [u2, u3]
u4.friends = [u2, u5]
u3.friends.append(u5)
s.add_all([u1, u2, u3, u4, u5])
s.commit()

print u2.all_friends
print u5.all_friends

rawDescription: How can I achieve a self-referencing many-to-many relationship on the SQLAlchemy ORM back referencing to the same attribute?

description: Achieve a self-referencing many-to-many relationship on the SQLAlchemy ORM back referencing to the same attribute


code:
xtract="""
BT
   /F13 12 Tf
   288 720 Td
   (ABC) Tj
ET
"""
for chunk in xtract.split("ET"):
    if "BT" in chunk:
        for brace in chunk.split(")"):
            if "(" in brace:
                 print brace[brace.find("(")+1:]

rawDescription: Regex matching very slow

description: Regex matching very slow


code:
rect = (slice(0L, 45L, None), slice(460L, 519L, None))

# Find the midpoint of the rectangle:
x,y = [(side.start+side.stop)/2. for side in rect]

rawDescription: How to find the central point of a region segmented by watershed?

description: Find the central point of a region segmented by watershed


rawDescription: Python Pandas Pivot Table

description: Python Pandas Pivot Table


code:
for item in v:
    print v

rawDescription: Set contents to string

description: Set contents to string


code:
from Tkinter import *

master = Tk()

frame = Frame(master)
frame.pack()

s_vars = []

for i in range(5):
    s_vars.append( StringVar() )

    def onChange(a ,b, c, s_var):
        print a, "changed to", s_var.get()

    s_vars[i].trace('w', lambda a, b, c, x=i: onChange(a, b, c, s_vars[x]) )

    en = Entry(frame, textvariable=s_vars[i])
    en.pack()

    s_vars[i].set( str(i) )

mainloop()

rawDescription: Getting values of dynamically generated Entry fields using Tkinter


code:
# p = 1; s = p
s=p=1
#exec"if s%p*s%~-~p:print`p`+','+`p+2`\ns*=p*p;p+=2\n"*999
for i in range(999):
    # s%p = remainder of s/p
    # ~p = 1s complement of p
    if s%p*s%~-~p:
        # `p` = repr(p)
        print`p`+','+`p+2`
    # s = s*p*p
    s*=p*p
    # p = p+2
    p+=2

rawDescription: What does this Python one-liner mean?

description: What does this Python one-liner mean


code:
def chair():

    correct = 1

    a = [1,2,3]
    random.shuffle(a)

    if a[0] == 3:
        correct = 0
        chair()               #run function again without printing
    else:
        print correct

for i in range(5):
    chair()

rawDescription: Python: Unexpected print values from within a simple function


code:
import io
import numpy as np

t=u"""A B C D
      2 3 1 2
      3 1 3 6
      1 5 4 1"""

data = np.genfromtxt(io.StringIO(t),names=True)

print data['A']
# [ 2.,  3.,  1.]

print data['B']
# [ 3.,  1.,  5.]

rawDescription: Storing data to use it with Numpy, with automatic title


code:
class Duck(object):
    # Like in Java, you don't need to write a __init__ if it's empty

    # You don't need to declare fields either - just use them.

    def performFly(self):
        self.flyBehaviour.fly()

    def performQuack(self):
        self.quackBehaviour.quack()

class FlyWithWings(object):
    def fly(self):
        print "I'm flying"

# Example:
d = Duck()
d.flyBehaviour = FlyWithWings()
d.fly() # prints "I'm flying"

rawDescription: Creating and Implementing an interface using Python?


code:
import numpy as np

aGrid = np.arange(0, 10, dtype=float)
mGrid = np.arange(100, 110, dtype=float)
eGrid = np.arange(1000, 1200, dtype=float)

A,M,E = np.meshgrid(aGrid, mGrid, eGrid, indexing='ij')

# contains the grid index
Z = (A + M + E).astype(float)
Z[A < 3] = np.nan

grids = [A, M, E]
grid_bc = np.broadcast_arrays(*grids)
Z2 = np.column_stack([g.ravel() for g in grid_bc])
Z2[np.isnan(Z.ravel())] = np.nan

print Z2[5900], Z2[6000]

rawDescription: Translation between grid index and actual values


code:
your_string = '"text":"string"'
match = re.search(r'(?<=:)("[^"]+")',your_string)
if match:
    print match.group(0) #'"string"'

rawDescription: How to use RE's to find strings that appear after certain text?

description: Use RE's to find strings that appear after certain text


code:
import numpy as np
from numpy import NaN

x = np.array([NaN,NaN,0.1,0.002,0.14,NaN,NaN,NaN])

def getMedian(arr):
    med = np.median(arr)
    if np.isnan(med):
        return np.nanmax(arr)
    else:
        return med

print getMedian(x)

rawDescription: Return maximum value from the array instead of not a number value


code:
import sys
import math
import winsound

#Define menu function
def menu():
    '''This function prints the main menu''' # this is a docstring for your method
    print "\nMils / Degrees Conversion Calculator"
    print "-" * 38
    print "\nOptions: "
    print "1. Degrees to Mils\n"
    print "2. Mils to Degrees\n"
    print "3. Quit\n"
    print "-" * 20
    print "\n"

#Define mils to degrees function
def mils_to_degrees(a): # use method names that make sense
    '''This function takes mils and converts them into degrees'''
    b = 17.78
    c = round((a / b),0)    #convert and round to nearest degree
    if c > 359 or c < 1:    #change 0 degrees to 360
        c = 360
    return (a,int(c))

#define degrees to mils function
def degrees_to_mils(b):
    '''This method converts degrees to mils'''
    a = 17.78
    if b == 0:
        b = 360
    c = round((b * a),0)        #math, same as above
    if c > 6400:               #set limit to 6400 mils
        c = 6400
    return (b,c)

def check_input(user_input):
    '''Checks if the input is a number'''
    try:
        converted_number = int(user_input)
    except ValueError:
        return -1
    return converted_number

#Begin program
loop = True
while loop:
    menu()
    choice = int(raw_input("Choose your option: "))
    if choice not in [1,2,3]:
        winsound.Beep(440, 500)
        print choice, "is not a valid choice.\n"
        print "Please choose from the available options.\n"
        menu()
        choice = int(raw_input('Choose your option: '))

    if choice == 1:
        print "\nEnter your azimuth in degrees (0 - 360)\n"
        degrees = check_input(raw_input('Degrees: '))
        if degrees > 360 or degrees < 0:
            print 'Please enter a value between 0 and 360'
        else:
            result = degrees_to_mils(degrees) # function call
            print '{} degrees = {} mils'.format(*result)
            print "-" * 38

    elif choice == 2:               #If user chooses mils to degrees:
        print "\nEnter your azimuth in mils (1 - 6400)\n"
        mils = check_input(raw_input('Mils: '))
        if mils > 6400 or mils < 1:
            print 'Please enter a value between 1 and 6400'
        else:
            result = mils_to_degrees(mils)        #function call
            print '{} mils = {} degrees'.format(*result)
            print "-" * 38

    elif choice == 3:
        loop = False

print "\n"
print "-" * 38
print "\nThank you for using Mil-Deg.py\n\n"

rawDescription: Python conversion calculator loop / code efficiency


code:
a = u'Le bébé (de 4 ans) a également un étrange "rire"'
r = unicodedata.normalize('NFKD',a).encode('cp1256','ignore')
r = unicode(re.sub('[^\w\s-]','',r).strip().lower())
r = re.sub('[-\s]+','-',r)
print r

rawDescription: How to convert string to seo-url?

description: Convert string to seo-url


code:
import base64
import time
encoded = base64.urlsafe_b64encode("%d" % int(time.time()))
print encoded
decoded = int(base64.urlsafe_b64decode(encoded))
print decoded
import datetime
datetime.datetime.fromtimestamp(decoded)

rawDescription: Serialize date to url safe string in Python


code:
import pygtk
pygtk.require('2.0')
import gobject

class Car(gobject.GObject):

    __gsignals__ = {
        'engine-started': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
        'engine-stopped': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
    }

    def __init__(self):
        gobject.GObject.__init__(self)
        self._state = 0

    def start(self):
        if not self._state:
            self._state = 1
            self.emit('engine-started')

    def stop(self):
        if self._state:
            self._state = 0
            self.emit('engine-stopped')

gobject.type_register(Car)

def kill_switch(c):
    def callback(*unused, **ignored):
        c.stop()
    return callback

def on_start(*unused, **ignored):
    print "Started..."

def on_stop(*unused, **ignored):
    print "Stopped..."

some_car = Car()
some_car.connect('engine-started', on_start)
some_car.connect('engine-started', kill_switch(some_car))
some_car.connect('engine-stopped', on_stop)
some_car.start()

rawDescription: Is there a generic update notification/pub sub system for GTK?


code:
import multiprocessing as mp

class Thing:
    def __init__(self, y):
        self.attribute = y

def square(thing, given_attribute):
    if thing.attribute == given_attribute:
        return thing

given_attribute = 4
x = [Thing(i) for i in range(10)]  # List of objects to process

if __name__ == '__main__':
    pool = mp.Pool(processes=4)
    results = [pool.apply(square, args=(x[i], given_attribute, )) for i in range(10)]
    r = [i for i in results if i is not None]  # Remove the None results
    print r

rawDescription: Parallelize this list comprehension in Python


code:
from github3 import login
from github3.repos.repo import Repository

import sys, time

if len(sys.argv) != 5:
    print 'Usage: server.exe username=... password=... [[-create -delete -update -fetch] file] [repo=server]'
    sys.exit()

try:
    un, pw, mode, f = sys.argv[1].split('=')[1], sys.argv[2].split('=')[1], sys.argv[3], sys.argv[4]
    me = login(un, pw)
except:
    print 'Usage: server.exe username=... password=... [[-create -delete -update -fetch] file] [repo=server]'
    sys.exit()

try:
    repo = sys.argv[5]
except IndexError:
    repo = 'server'

repo = me.repository(un, repo)

if mode == '-create':
    f = open(f)
    name = f.name
    data = f.read()
    repo.create_file(name, 'Create '+time.asctime(), data.encode('utf-8'))

if mode == '-delete':
    f = open(f)
    repo.file_contents(f.name).delete('Delete '+time.asctime())

if mode == '-update':
    f = open(f)
    name = f.name
    data = f.read()
    repo.file_contents(name).update('Update '+time.asctime(), f.read().encode('utf-8'))

if mode == '-fetch':
    with open(f, mode='w') as f:
        f.write(repo.file_contents(f.name).decoded)

rawDescription: "InvalidSchema" when attempting to create file with github3.py


code:
first, delimiter, second = line.partition('_')
a, s, b = second.partition(' ')
print first + delimiter + 'Normal' + s + b

rawDescription: python replace specific elements in a string

description: Python replace specific elements in a string


code:
a ="2016-05-11 18:33:44"
b= datetime.strptime(a,"%Y-%d-%m %H:%M:%S")
print b.strftime('%s') # returns time in epoch

rawDescription: How to conver datetime to timestamp epoch

description: Conver datetime to timestamp epoch


code:
import inspect

print inspect.getframeinfo(inspect.getouterframes(inspect.currentframe())[1][0])[0]

rawDescription: Where is module being imported from?

description: Where is module being imported from


code:
import pygame as py
py.init()
clock = py.time.Clock()

start_time = py.time.get_ticks()
print "started at:",start_time

for i in xrange(0,30): #wait 1 second
    clock.tick(30)

end_time = py.time.get_ticks()
print "finished at:",end_time

time_taken = end_time-start_time
print "time taken:",time_taken

rawDescription: Pygame, I am trying to make a timer

description: Pygame, I am trying to make a timer


code:
import colorama
colorama.init()
def put_cursor(x,y):
    print "\x1b[{};{}H".format(y+1,x+1)

def clear():
    print "\x1b[2J"

clear()
put_cursor(0,0)
print "hello"
print "huhu"
#return to first line
put_cursor(0,0)
print "noooooo"

rawDescription: Python Terminal Overwrite Many Lines

description: Python Terminal Overwrite Many Lines


code:
from sklearn import linear_model
from sklearn.datasets import make_classification
from sklearn.calibration import CalibratedClassifierCV
# Build a classification task using 3 informative features
X, y = make_classification(n_samples=1000,
                           n_features=10,
                           n_informative=3,
                           n_redundant=0,
                           n_repeated=0,
                           n_classes=2,
                           random_state=0,
                           shuffle=False)

per = linear_model.Perceptron()

clf_isotonic = CalibratedClassifierCV(per, cv=10, method='isotonic')

clf_isotonic.fit(X[:900], y[:900])

preds = clf_isotonic.predict_proba(X[900:])
print preds

rawDescription: Python Scikit-learn Perceptron Output Probabilities


code:
print tree.xpath("//n:text[@id='findme']/n:tspan/text()",
                 namespaces={'n': "http://www.w3.org/2000/svg"})

rawDescription: Finding and replacing XML/SVG element text by id with Python's lxml?


code:
try:

    from itertools import combinations

except ImportError:

    def combinations(l, n):
        if n != 2: raise Exception('This placeholder only good for n=2')
        for i in range(len(l)):
            for j in range(i+1, len(l)):
                yield l[i], l[j]

coords_list = [(0,0), (3,4), (6,8)]

def distance(p1, p2):
    return ( ( p2[0]-p1[0] ) ** 2 + ( p2[1]-p1[1] )**2 ) ** 0.5

largest_distance, (p1, p2) = max([
     (distance(p1,p2), (p1, p2)) for (p1,p2) in combinations(coords_list, 2)
     ])

print largest_distance, p1, p2

rawDescription: Python "round robin"

description: Python "round robin"


code:
import wx

import sys
sys.path.append("..")

from ObjectListView import ObjectListView, ColumnDefn

### 2. Launcher creates wxMenu. ###
menu_titles = [ "Open",
                "Properties",
                "Rename",
                "Delete" ]

menu_title_by_id = {}
for title in menu_titles:
    menu_title_by_id[ wx.NewId() ] = title

class Track(object):
    """
    Simple minded object that represents a song in a music library
    """
    def __init__(self, title, artist, album):
        self.title = title
        self.artist = artist
        self.album = album

def GetTracks():
    """
    Return a collection of tracks
    """
    return [
        Track("Sweet Lullaby", "Deep Forest", "Deep Forest"),
        Track("Losing My Religion", "U2", "Out of Time"),
        Track("En el Pais de la Libertad", "Leon Gieco", "Leon Gieco"),
    ]

class MyFrame(wx.Frame):

    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)

        self.Init()

    def Init(self):
        self.InitModel()
        self.InitWidgets()
        self.InitObjectListView()

    def InitModel(self):
        self.songs = GetTracks()

    def InitWidgets(self):
        panel = wx.Panel(self, -1)
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_1.Add(panel, 1, wx.ALL | wx.EXPAND)
        self.SetSizer(sizer_1)

        self.myOlv = ObjectListView(panel, -1, style=wx.LC_REPORT | wx.SUNKEN_BORDER)
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_2.Add(self.myOlv, 1, wx.ALL | wx.EXPAND, 4)
        panel.SetSizer(sizer_2)

        self.Layout()

    def InitObjectListView(self):
        self.myOlv.SetColumns([
            ColumnDefn("Title", "left", 120, "title"),
            ColumnDefn("Artist", "left", 100, "artist"),
            ColumnDefn("Album", "left", 100, "album")
        ])
        self.myOlv.SetObjects(self.songs)

        self.myOlv.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.RightClick)

    def RightClick(self, event):
        # record what was clicked
        self.list_item_clicked = self.myOlv.GetSelectedObject()

        menu = wx.Menu()
        menu.Bind(wx.EVT_MENU, self.MenuSelectionCb)

        for (id_, title) in menu_title_by_id.items():
            ### 3. Launcher packs menu with Append. ###
            menu.Append(id_, title)

        ### 5. Launcher displays menu with call to PopupMenu, invoked on the source component, passing event's GetPoint. ###
        # self.frame.PopupMenu( menu, event.GetPoint() )
        frame_1.PopupMenu(menu, event.GetPoint())
        menu.Destroy()  # destroy to avoid mem leak

    def MenuSelectionCb(self, event):
        # do something
        operation = menu_title_by_id[ event.GetId() ]
        target = self.list_item_clicked.title
        print 'Perform "%(operation)s" on "%(target)s."' % vars()

class MyPopupMenu(wx.Menu):

    def __init__(self, parent):
        super(MyPopupMenu, self).__init__()

        self.parent = parent

        mmi = wx.MenuItem(self, wx.NewId(), 'Minimize')
        self.AppendItem(mmi)
        self.Bind(wx.EVT_MENU, self.OnMinimize, mmi)

        cmi = wx.MenuItem(self, wx.NewId(), 'Close')
        self.AppendItem(cmi)
        self.Bind(wx.EVT_MENU, self.OnClose, cmi)

    def OnMinimize(self, e):
        self.parent.Iconize()

    def OnClose(self, e):
        self.parent.Close()

if __name__ == '__main__':
    app = wx.App(True)
    wx.InitAllImageHandlers()
    frame_1 = MyFrame(None, -1, "ObjectListView Track Test")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()

rawDescription: Python: Right click on objectlistview not showing item name selected


code:
from selenium import webdriver
# initiate the browser. It will open the url,
# and we can access all its content, and make actions on it.
browser = webdriver.Firefox()
url = 'http://localhost:8080/test.html'
# the page test.html is changing constantly its content by receiving sockets, etc.
#So we need to save its "status" when we decide for further retrieval)
browser.get(url)
# wait until we want to save the content (this could be a buttonUI action, etc.):
raw_input("Press to print web page")
# save the html rendered content in that moment:
html_source = browser.page_source
# display to check:
print html_source

rawDescription: python: how to save dynamically rendered html web page code [SOLVED]

description: Python: how to save dynamically rendered html web page code [SOLVED]


code:
import itertools, operator

data = [
{'LTYPE': 'N', 'RID': '1', 'NAME': 'Jason Smith'},
{'LTYPE': 'A', 'RID': '2', 'NAME': 'DA'},
{'LTYPE': 'A', 'RID': '3', 'NAME': 'B'},
{'LTYPE': 'N', 'RID': '4', 'NAME': 'John Smith'},
{'LTYPE': 'A', 'RID': '5', 'NAME': 'BC'},
{'LTYPE': 'A', 'RID': '6', 'NAME': 'CB'},
{'LTYPE': 'A', 'RID': '7', 'NAME': 'DB'},
{'LTYPE': 'A', 'RID': '8', 'NAME': 'DA'},
]

for k, g in itertools.groupby(data, operator.itemgetter('LTYPE')):
  if k=='N':
    person_name_record = next(g)
  else:
    attribute_records = list(g)
    person_attributes = ' '.join(r['NAME'] for r in attribute_records)
    addfields = dict(PERSON_ATTRIBUTES=person_attributes,
                     PERSON_NAME=person_name_record['NAME'])
    person_name_record.update(addfields)
    for r in attribute_records: r.update(addfields)

for r in data: print r

rawDescription: Python Working with lists based on indexes


code:
def processRecords():
    print ("grade report\n").center(60).upper()
    print "name".upper(),"course".rjust(22).upper(),"grade".rjust(32).upper()
    print "-" * 60

    rec_count = 0
    for line in open("grades.txt","r"):
        name   = line[0:20]
        course = line[20:50]
        grade  = line[50:51]
        print name, course.rjust(3), grade.rjust(3)
        rec_count += 1
    return rec_count

rawDescription: Stuck in a while loop, can you please help?


code:
import os, stat

mode = os.fstat(0).st_mode
if stat.S_ISFIFO(mode):
     print "stdin is piped"
elif stat.S_ISREG(mode):
     print "stdin is redirected"
else:
     print "stdin is terminal"

rawDescription: How do I determine if sys.stdin is redirected from a file vs. piped from another process?

description: Determine if sys.stdin is redirected from a file vs. piped from another process


code:
import calendar

from dateutil.parser import parse
import scrapy
from scrapy.http import Request

class T3Spider(scrapy.Spider):
    name = "t3"
    allowed_domains = ['t3.com']
    start_urls = ['http://www.t3.com/reviews']

    def parse(self, response):
        reviews = response.css('div.listingResult')
        for review in reviews:
            link = review.xpath("a/@href").extract()[0]
            yield Request(url="http://www.t3.com" + link, callback=self.parse_review)

        # TODO: handle exceptions here

        # extract the review date
        time = reviews[-1].xpath(".//time/@datetime").extract()[0]

        # convert a date into a timestamp
        timestamp = calendar.timegm(parse(time).timetuple())

        url = 'http://www.t3.com/more/reviews/latest/%d' % timestamp
        req = Request(url=url,
                      headers={"Referer": "http://www.t3.com/reviews", "X-Requested-With": "XMLHttpRequest"},
                      callback=self.parse)
        yield req

    def parse_review(self, response):
        print response.url

rawDescription: Scrapy crawler not processing XHR Request


code:
query = collection.find({"string": field})
print query.explain()

rawDescription: How to check if a pymongo cursor has query results

description: Check if a pymongo cursor has query results


code:
def sort_with_indexes(data):
    sorted_data = sorted(enumerate(data), key=lambda key: key[1])
    indexes = range(len(data))
    indexes.sort(key=lambda key: sorted_data[key][0])
    return [i[1] for i in sorted_data], indexes

data, indexes = sort_with_indexes([1,3,4,2])
print data, indexes

rawDescription: insertion sort get indices?

description: Insertion sort get indices


code:
import sys
from PySide import QtGui, QtCore

class MyCombo(QtGui.QWidget):
    def __init__(self, *args):
        QtGui.QWidget.__init__(self, *args)
        vLayout=QtGui.QVBoxLayout(self)
        self.setLayout(vLayout)

        self.combo=QtGui.QComboBox(self)
        self.combo.currentIndexChanged.connect(self.currentIndexChanged)
        comboModel=self.combo.model()
        for i in range(3):
            item = QtGui.QStandardItem(str(i))
            comboModel.appendRow(item)
            self.combo.setItemData(i,'MY DATA' + str(i))
        vLayout.addWidget(self.combo)

    def currentIndexChanged(self, index):
        print self.combo.itemData(index)

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

rawDescription: How to get QComboBox Item data

description: Get QComboBox Item data


rawDescription: Python: merge nested lists

description: Python: merge nested lists


code:
## naive recursion
def div(a, b):
    if (a >= b):
        return div(a - b, b) + 1
    else: return 0

try:
    print div(5678, 3)
except Exception as e:
    print e  ## maximum recursion depth exceeded

## less naive recursion with trampolines
## see https://gist.github.com/802557 for details/explanations
def trampoline(f):
    def _(*args):
        result = f(*args)
        while callable(result):
            result = result()
        return result
    return _

def div_func(a, b, acc=0):
    if (a >= b):
        return lambda: div_func(a - b, b, acc + 1)
    else: return acc

div2 = trampoline(div_func)

## ok
print div2(5678, 3)

rawDescription: python recursion function that takes two inputs

description: Python recursion function that takes two inputs


code:
import os
from glob import glob

def readindex(path):
    pattern = '*.txt'
    full_path = os.path.join(path, pattern)
    for fname in sorted(glob(full_path)):
        for line in open(fname, 'r'):
            yield line
# read lines to memory list for using multiple times
linelist = list(readindex("directory"))
for line in linelist:
    print line,

rawDescription: how to search string in a folder of text files using python

description: Search string in a folder of text files using python


code:
lst = ["fairly","blue","car"]
x = "fairly"
try:
   print lst.index(x)
except ValueError:
   print "{0} not in list".format(x)

rawDescription: Is it possible to get the index in list when I use "if string in list:"?


code:
from nltk.corpus import brown
from nltk.data import load

sents = brown.tagged_sents()
# test on last 10% of brown corpus.
numtest = len(sents) / 10
testsents = sents[numtest:]

_POS_TAGGER = 'taggers/maxent_treebank_pos_tagger/english.pickle'

tagger = load(_POS_TAGGER)

print tagger.evaluate(testsents)

rawDescription: What is the accuracy of nltk pos_tagger?

description: What is the accuracy of nltk pos_tagger


code:
var = 5

def printit():
  global var
  threading.Timer(5.0, printit).start()
  print "Hello, World!", var

printit()

import time

for i in range(1000000):
    time.sleep(2)
    var = i

rawDescription: Log value of variable every second in Python


code:
from threading import Thread
tlist = []
for i in range(3):
    t = Thread(target=some_function)
    t.start()
    tlist.append(t)

# wait until threads have finished executing
for t in tlist:
    t.join()

print 'complete!'

rawDescription: How can I set it up so that threads communicate they're complete with their task?

description: Set it up so that threads communicate they're complete with their task


code:
import zipfile
import os.path
import os
zfile = zipfile.ZipFile("test.zip")
for name in zfile.namelist():
  (dirname, filename) = os.path.split(name)
  print "Decompressing " + filename + " on " + dirname
  if not os.path.exists(dirname):
    os.makedirs(dirname)
  zfile.extract(name, dirname)

rawDescription: How to unzip a file with Python 2.4?

description: Unzip a file with Python 2.4


code:
from BeautifulSoup import BeautifulSoup
import urllib2

def searchLinks(url, query_string):
    f = urllib2.urlopen(url)
    soup = BeautifulSoup(f, convertEntities='html')
    for a in soup.findAll('a'):
        if a.has_key('href'):
            idx = a.contents[0].find(query_string)
            if idx is not None and idx > -1:
                yield a['href']

res = list(searchLinks('http://example.com', 'game2'))
print res

rawDescription: Beautiful Soup question

description: Beautiful Soup question


code:
elements = driver.find_elements_by_xpath("//your_path")
counter = 0
for counter in range(counter, len(elements)):
    elements = driver.find_elements_by_xpath("//your_path")
    elements[counter].click()
    time.sleep(2)
    discoverPageTables = driver.find_element_by_xpath("//your_path").text
    print "Tables Found :" + discoverPageTables
    discoverPageInstanceCount = driver.find_element_by_xpath("your_path").text
    print "Instance Count Found :" + discoverPageInstanceCount
    discoverpageWorkload = driver.find_element_by_xpath("//your_path").text
    print "Workload Percentage :" + discoverpageWorkload
    discoverPageHiveCompatible = driver.find_element_by_xpath("//your_path").text
    print "Hive Compatible :" + discoverPageHiveCompatible
    discoverPageComplexity = driver.find_element_by_xpath("your_path").text
    print "Complexity :" + discoverPageComplexity
    discoverPageNormalizedComplexity = driver.find_element_by_xpath("your_path").text
    print "Normalized Complexity :" + discoverPageNormalizedComplexity
    print '\n'
    driver.back()
    time.sleep(5)

rawDescription: How to resolve issue --- Element not found in the cache - perhaps the page has changed since it was looked up

description: Resolve issue --- Element not found in the cache - perhaps the page has changed since it was looked up


code:
class Pet(object):
    @classmethod
    def is_dead(cls):
        return False

class Dog(Pet):
    @classmethod
    def bark(cls):
        print "woof"

if __name__ == "__main__":
    if not Dog.is_dead():
        Dog.bark()

rawDescription: Using an alternative to a Python Module when you wish to create derived Singletons


code:
import re
words = ['You and i', 'everyone else', 'cat and dog', 'We u all']
res = [re.search(r'(.*?) (and|u) (.*?)$', word) for word in words]
for i in res:
    if i is not None:
            print i.group(1)
            print i.group(3)

rawDescription: Identify string values in between a text


code:
import csv
import fileinput
import glob
import os

merged_csv = 'merged.csv'
columns = (1, 2)  # columns used for filtering
pathname = '*.csv'
tmpext = os.extsep + "tmp"
csv_files = glob.glob(pathname)

if merged_csv not in csv_files:
    prev_merged = None
else:
    prev_merged = merged_csv + tmpext
    os.rename(merged_csv, prev_merged)
    csv_files[csv_files.index(merged_csv)] = prev_merged

with open(merged_csv, 'wb') as ofile:
    csv_writer = csv.writer(ofile)
    written = set()  # unique combinations of column values written
    csv_stream = fileinput.input(csv_files, mode='rb')
    for row in csv.reader(csv_stream, delimiter=','):
        combination = tuple(row[col] for col in columns)
        if combination not in written:
            csv_writer.writerow(row)
            written.add(combination)

if prev_merged:
    os.unlink(prev_merged)  # clean up

print '{!r} file {}written'.format(merged_csv, 're' if prev_merged else '')

rawDescription: Appending rows to CSV in Python with column-specific duplicate filter


code:
from collections import Counter
values = [1,5,1,1,6,3,3,4,5,5,5,2,5]
counts = Counter(values)
for k, v in sorted(counts.iteritems(), key=lambda x:x[::-1]):
    print k, v * 'x'

rawDescription: Sort the values in a histogram in python and plot them


code:
import threading, time

def wrapper(func, args, res):
    res.append(func(*args))

res = []
t = threading.Thread(target=wrapper, args=(findcluster, (companyid,), res))
t.start()
while t.is_alive():
    # print next iteration of ASCII spinner
    t.join(0.2)
print res[0]

rawDescription: Python: run one function until another function finishes


code:
import random as r
import os
import sys
import time
from subprocess import call
#call('color a', shell=True)
class Entity(object):
    def __init__(self, number):
        self.number = number
        self.chance = 10
        self.age = 0
class Fitness(object):
    def __init__(self, population):
        self.population = population

    def StartPopulation(self, pop, entities):
        for x in range(pop):
            entity = Entity(r.randint(3, 5))
            entities.append(entity)

    def FitnessMethod(self, entities):

        for x in entities:
            if x.number >= 5:
                x.chance += 10
            else:
                x.chance += 0

            if x.chance > 100:
                x.chance = 100

            else:
                x.chance += 0

    def Reproduce(self,entities):
        x = entities[r.randint(0, (len(entities) - 1))]
        if r.randint(0, 100) < x.chance:
            random_children = r.randint(1, 3)
            for a in range(random_children):
                entity = Entity(x.number)
                entity.chance = x.chance
                entities.append(entity)
            x.age += 1

    def Aging(self, entities):
        for x in entities:
            if x.age >= 2:
                entities.remove(x)

pop = r.randint(3, 5)
entities = []
Fitness = Fitness(pop)
Fitness.StartPopulation(pop, entities)
for x in entities:
    print x.number, x.chance
print "~~~~~~~~~~~~~\n"
raw_input()

for x in range(30):
    Fitness.FitnessMethod(entities)
    Fitness.Reproduce(entities)
    Fitness.Aging(entities)
    for x in entities:
        print x.number, x.age, x.chance
    print "Generation_Mutation_Complete"
print "Generation_Mutation_Complete"
raw_input()

rawDescription: Evolutionary algorithm chance of reproduction limit


code:
dd = dict((i, k) for k, i in enumerate(token_list))

for document in clean_tokens:
    print [dd[x] for x in document]

rawDescription: Unique token count speed

description: Unique token count speed


code:
import sys
if sys.platform == 'win32':
  import win32_sysinfo as sysinfo
elif sys.platform == 'darwin':
  import mac_sysinfo as sysinfo
elif 'linux' in sys.platform:
  import linux_sysinfo as sysinfo
#etc

print 'Memory available:', sysinfo.memory_available()

rawDescription: How can I return system information in Python?

description: Return system information in Python


code:
from comtypes.typeinfo import LoadTypeLibEx
lib = LoadTypeLibEx("C:\\path\to\\my.dll")
print lib.GetLibAttr().guid

rawDescription: Extract GUID from ActiveX COM DLL using Python


code:
class FlipkartSpider(BaseSpider):
    name = "flip1"
    allowed_domains = ["flipkart.com"]
    start_urls = [
        "http://www.flipkart.com/tablets/pr?sid=tyy%2Chry&q=mobile&ref=b8b64676-065a-445c-a6a1-bc964d5ff938"
    ]
    '''def is_element_present(self, finder, selector, wait_time=None):
        wait_time = wait_time or self.wait_time
        end_time = time.time() + wait_time
        while time.time() < end_time:
            if finder(selector):
                return True
        return False
        def is_element_present_by_xpath(self, xpath, wait_time=None):
        return self.is_element_present(self.find_by_xpath, xpath, wait_time)
        '''
    def __init__(self):
        self.driver = webdriver.Firefox()

    def parse(self, response):
        sel = Selector(response)
        self.driver.get(response.url)
        block="block"
        hyper="http://www.flipkart.com"
        print hyper
        #i=0
        while True:
            self.driver.execute_script("window.scrollTo(10000000,10000000)")
            self.driver.set_page_load_timeout(10000)
            try:
                show = self.driver.find_element_by_xpath('//div[@id="show-more-results"]').value_of_css_property('display')
                if show==block:
                    self.driver.find_element_by_xpath('//div[@id="show-more-results"]').click()
                no_more = self.driver.find_element_by_xpath('//*[@id="no-more-results" and @class="dont-show"]').value_of_css_property('display')
                if no_more==block:
                    break;
                time.sleep(5)
                self.driver.execute_script("window.scrollTo(10000000,10000000)")

                self.driver.set_page_load_timeout(10000)
                #if i==7:
                #   break
            except NoSuchElementException:
                print "pungi"
                break
        #down = self.driver.find_element_by_xpath('//div[@id="show-more-results"]')
        #location = down.location
        #self.((JavascriptExecutor) driver).executeScript("window.scrollBy(10000,10000);");
        #next = self.driver.find_element_by_xpath('//div[@id="show-more-results"]')
        response = TextResponse(url=response.url, body=self.driver.page_source, encoding='utf-8')
        try:
            #self.driver.set_page_load_timeout(10000)
            #driver.execute_script("window.scrollTo(0, location.get('y')")
            sites = response.xpath('//div[@class="gd-col gu12 browse-product fk-inf-scroll-item"] | //div[@class="pu-details lastUnit"] |  //div[@class="pu-visual-section"]')
            for site in sites:
                item = FlipkartItem()
                item['title'] = site.xpath('div//a[@class="lu-title"]/text() | div[1]/a/text()').extract()
                item['price'] = site.xpath('div//div[@class="pu-price"]/div/text() | div//div[@class="pu-final"]/span/text()').extract()
                item['rating'] = site.xpath('div[@class="pu-rating"]/div/@title').extract()
                item['image'] = site.xpath('a/img/@src').extract()
                data = site.xpath('a/@href').extract()
                print data
                item['link'] = data

                #print rating
                yield item
            '''for site in sites:
                item = FlipkartItem()
                item['title'] = site.xpath('div//a[@class="lu-title"]/text() | div[1]/a/text()').extract()
                item['price'] = site.xpath('div//div[@class="pu-price"]/div/text() | div//div[@class="pu-final"]/span/text()').extract()
                item['rating'] = site.xpath('div[@class="pu-rating"]/div/@title').extract()
                #print rating
                yield item'''
            #next.click()
            #self.driver.execute_script("window.scrollTo(10000000,10000000)")
        except:
            #break
            a=10
        self.driver.close()

rawDescription: How to extract data from dynamic websites like Flipkart using selenium and Scrapy?

description: Extract data from dynamic websites like Flipkart using selenium and Scrapy


code:
for index, (start, end) in enumerate(searchPFAM(fname)):
    with open('newfile-%s.txt' % index,'w') as fileinput:
        print start, end
        for item in lookup[uniprotID]:
            item, start, end = map(int, (item, start, end)) #You shouldn't be doing this here, you should convert these variables to ints when you first store them in "lookup".
            if start <= item <= end:
                print item
                result = str(item - start)
                fileinput.write(">{0} | at {1} position\n".format(uniprotID, result))
                fileinput.write(''.join(makeList[start-1:end]))
                break #exit loop, move onto next file.
        else:
                fileinput.write(">{0} | N/A\n".format(uniprotID))
                fileinput.write(''.join(makeList[start-1:end]))

rawDescription: file output error relating to list dictionary and value

description: File output error relating to list dictionary and value


code:
import wmi
c = wmi.WMI ()

for process in c.Win32_Process ():
    print process.ProcessId, process.Name

rawDescription: I'm trying to get all the processes and applications that are currently running using Python on Windows 7


code:
import urllib2,re
content = urllib2.urlopen("http://somme.url").read()
print re.findall("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}",content)

rawDescription: Python Web Scraping W/O Beautiful Soup or non-default modules?


code:
def make_locals_globals():
    """This is just bad"""
    foo = 1
    bar = 2

    locals_dict = locals()
    globals_dict = globals()

    print 'Locals:', locals_dict

    for varname, varval in locals_dict.items():
        print 'Setting global: %s=%s' % (varname, varval)
        globals_dict[varname] = varval

if __name__ == '__main__':
    make_locals_globals()

    print '\nGlobals:'
    print 'foo=', foo
    print 'bar=', bar

rawDescription: Is there a way to make every variable inside a definition or class to become global automatically?


code:
import threading
from threading import Thread

def func1():
    print 'Working'

def func2():
    print 'Working'

if __name__ == '__main__':
    Thread(target = func1).start()
    Thread(target = func2).start()

rawDescription: Make 2 functions run at the same time

description: Make 2 functions run at the same time


rawDescription: simpler implementation of this string splitting loop

description: Simpler implementation of this string splitting loop


code:
import numpy as np
blocks = np.arange(2*3*4.).reshape((2,3,4))
i,j,k = 0,1,2
print [x for x in blocks[i:i+2, j:j+2, k:k+2].flat]

rawDescription: in numpy what is the multi dimensional equivalent of take

description: In numpy what is the multi dimensional equivalent of take


code:
import itertools

with open('big_file.txt', 'r') as f:
    for line in itertools.islice(f, 1000, None):
        print line

rawDescription: How to start reading from x line in a text file with python

description: Start reading from x line in a text file with python


code:
max = 0
first_no = None
second_no = None

for x in xrange(0,12):
    y = 12 - x
    P = x * pow(y, 3)
    if P > max:
        first_no = x
        first_no = y
        max = P
print "first no is %s" %(first_no)
print "Second no is %s" %(second_no)
print "Maximum number %s" %(max)

rawDescription: How to maximize a simple expression in Python

description: Maximize a simple expression in Python


code:
s1="hello world"
L1=s1.split(" ")
t1=tuple(L1)
print type(t1)
tuple

print(t1)
('hello', 'world')

rawDescription: Python 2.5 string, modified list, into modified tuple


code:
while not done:
    keys = key.get_pressed()
    if keys[K_DOWN]:
        print "DOWN"
    for e in event.get():
        pass # proceed other events.
             # always call event.get() or event.poll() in the main loop

rawDescription: How to efficiently hold a key in Pygame?

description: Efficiently hold a key in Pygame


code:
import itertools

persons = ['person1', 'person2', 'person3']

for p1, p2 in itertools.combinations(persons, 2):
    print "Compare", p1, "and", p2

rawDescription: Comparing similarity between multiple strings with a random starting point


code:
def menu(name=None, location=None):
    if name is None:
        name = raw_input("Enter your name: ")
    if location is None:
        location = raw_input("Enter your location: ")
    print "Name: %s | Location: %s" % (name, location)

rawDescription: Problems of **arg in Python

description: Problems of **arg in Python


rawDescription: How can I filter a numpy array based on another numpy array?

description: Filter a numpy array based on another numpy array


code:
import random
import time

start = time.clock()
while time.clock() - start < 3:
    random_number = random.randint(0,100)

print random_number

rawDescription: Compute random number over certain time interval with Python


code:
def calcIncome(ticketSold,price):
    return price*ticketSold

totalIncome = calcIncome(ticketSold,cPrice)
print totalIncome

rawDescription: Function with boolean input not printing the expected result


code:
screenNames = dict((i, i%100) for i in range(238))
print screenNames

rawDescription: More pythonic way to handle this logic structure


code:
from HTMLParser import HTMLParser

# create a subclass and override the handler methods
class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        print "Encountered a start tag:", tag
    def handle_endtag(self, tag):
        print "Encountered an end tag :", tag
    def handle_data(self, data):
        print "Encountered some data  :", data

# instantiate the parser and fed it some HTML
parser = MyHTMLParser()
parser.feed('<title>Test</title>'
            '
Parse me!
')

rawDescription: Python HTML parsing from url

description: Python HTML parsing from url


code:
def COUNT(ind, prev, L):
    if(ind>=len(L)):
        return 0

    val = 0

    val += COUNT(ind+1, prev, L)

    if(L[ind]<=L[prev]):
        val = val+1

    return val

def num_not_increasing(lst):
    val = 0
    for i in range(len(lst)):
        val += COUNT(i+1, i, lst)
    return val

L = [6,1,5,4,2]

print num_not_increasing(L)

rawDescription: Number of Items in a list not in increasing order


code:
from subprocess import Popen
def spawner(cmd_list):
    while True:
        print "Running proc..."
        mon_proc = Popen(cmd_list)
        print "Proc exit: %s" % mon_proc.wait()

spawner(['/bin/sleep', '3'])

rawDescription: python process wrapper

description: Python process wrapper


code:
from selenium import webdriver

browser = webdriver.Firefox()
browser.get('url_goes_here')
list_of_elements = ["a", "button", "li", "nav", "ol", "span", "ul", "header", "footer", "section"]
for tag_name in list_of_elements:
    for element in browser.find_elements_by_tag_name(tag_name):
         print element

rawDescription: Get XPath of an element in DOM tree?

description: Get XPath of an element in DOM tree


code:
from pydub.utils import mediainfo
info = mediainfo("/path/to/file.mp3")
print info['sample_rate']
print info['channels']

rawDescription: pydub accessing the sampling rate(Hz) and the audio signal from an mp3 file

description: Pydub accessing the sampling rate(Hz) and the audio signal from an mp3 file


code:
def reorder(s, word, delta):
  words = s.split()
  oldpos = words.index(word)
  words.insert(oldpos+delta, words.pop(oldpos))
  return ' '.join(words)

print reorder('The quick brown fox jumps over the lazy dog', 'quick', 2)

rawDescription: How can I move a word within a string?

description: Move a word within a string


code:
import re
# matches everything which is between < and > (excluding them)
ptrn = re.compile("<([^>]+)>")
# findall returns ['email','email2']. Join concats them.
print '; '.join(ptrn.findall("fname lname <email>; fname2 lname2 <email2>;"))
# email; email2

rawDescription: How can I extract email addresses from between '<' and '>'?

description: Extract email addresses from between '<' and '>'


code:
#!/usr/bin/python
import lxml.html
import lxml.cssselect
soup = lxml.html.fromstring("""

Hello

        """)

select = lxml.cssselect.CSSSelector("a.yyy.zzz, a.yyy")
print [lxml.html.tostring(s).strip() for s in select(soup)]
print (select(soup) or [None])[0]

rawDescription: lxml equivalent to BeautifulSoup "OR" syntax?

description: Lxml equivalent to BeautifulSoup "OR" syntax


code:
nums = data[0].split()
for num in nums:
   print num
if not nums:
   print "No new emails found"

rawDescription: Python if a var doesn't exist then

description: Python if a var doesn't exist then


code:
lock = threading.RLock()

def func1():
    with lock:
        func2()

def func2():
    with lock: # this does not block even though the lock is acquired already
        print 'hello world'

rawDescription: Python Conditional  "With" Lock Design

description: Python Conditional  "With" Lock Design


code:
from random import *

rankName = ("Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King")

suit = ("hearts", "diamonds", "spades" , "clubs")

users = ("I", "computer", "deck")

# make it weighted and shuffed
users_with_weights = ["I"]*5 + ['computer']*5 + ['deck']*42
shuffle(users_with_weights)

NUMCARDS = 52
DECK = 0
PLAYER = 1
COMP = 2

count = 0
while (count < 52):
   for u in rankName:
       for i in suit:
           count = count + 1
           w = users_with_weights.pop()
           ''' 'computer' and 'I' should only show 5 times, while deck shows 42 cards '''
           print count, '\t| ', u,' of', i, '\t|', w

rawDescription: python only show tuple items in loop x amount of times

description: Python only show tuple items in loop x amount of times


code:
def in_list(small, big):
    l_sml = len(small)
    l_big = len(big)
    return any((big[i:i+l_sml]==small for i in xrange(l_big-l_sml+1)))

print in_list([4,2,1], [1,2,3,4,2,1,0,5]) # True
print in_list([1,2,3], [1,2,4])           # False

rawDescription: Is there a way to know if a list of elements is on a larger list without using 'in' keyword?


code:
from git import Repo, InvalidGitRepositoryError

try:
    Repo(path_to_repo)
    print 'is git repo'
except InvalidGitRepositoryError:
    print 'isn`t git repo'

rawDescription: How could I tell a give directory is git repo(bare or not bare) in Python


code:
import subprocess
retcode = subprocess.call(["/usr/bin/python", "/path/to/test.py"])
print "Return code of test.py is ", retcode

rawDescription: How to run a python script from another python script and get the returned status code?

description: Run a python script from another python script and get the returned status code


code:
import twitter # if this module does not
               # contain OAuth or stream,
               # check if sixohsix' twitter
               # module is used!
auth = twitter.OAuth(
    consumer_key='...',
    consumer_secret='...',
    token='...',
    token_secret='...'
)

stream = twitter.stream.TwitterStream(auth=auth, domain='userstream.twitter.com')

for msg in stream.user():
    if 'direct_message' in msg:
        print msg['direct_message']['text']

rawDescription: Getting mentions and DMs through twitter stream API 1.1? (Using twython)


code:
r = urllib.urlopen('http://www.elnorte.ec/')
x = BeautifulSoup.BeautifulSoup(r.read)
r.close()

print x.prettify('latin-1')

rawDescription: Python and BeautifulSoup encoding issues


code:
import Tkinter as tk
root = tk.Tk()
print "background is", root["background"]

rawDescription: Possible to create single pixel wide horizontal/vertical ttk.Separator's?


code:
test = [
    '=?Windows-1251?B?ICLRLcvu5Obo8fLo6iI?=',
    '=?koi8-r?B?5tLPzM/XwSDtwdLJzsEg98nUwczYxdfOwQ?=',
    '=?Windows-1251?B?1PDu6+7i4CDM4PDo7eAgwujy4Ov85eLt4A?='
]

def decodeStr(s):
    s = s.split('?')
    enc = s[1]
    dat = s[3]

    return (dat+'===').decode('base-64').decode(enc)

for t in test:
    print decodeStr(t)

rawDescription: python : decode strings

description: Python : decode strings


code:
if '':
    print 'empty'
if 'I entered something':
    print 'not empty'
raw_input('just hit enter: ')  # just hinting 'enter' results in the empty string

rawDescription: What is the need of 'if statement' in this python code?


code:
col_idx = -1
for idx, mt in enumerate(sheet.row_values(0)):
    print mt
    if mt=='MT':
        col_idx = idx
        break

rawDescription: how to get column or row number by cell value in python

description: Get column or row number by cell value in python


code:
import threading

class MyThread(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self.sleep_event = threading.Event()
        self.damon = True

    def run(self):
        while True:
            self.sleep_event.clear()
            self.sleep_event.wait(60)
            threading.Thread(target=self._run).start()

    def _run(self):
        print "run"

my_thread = MyThread()
my_thread.start()

while True:
    raw_input("Hit ENTER to force execution\n")
    my_thread.sleep_event.set()

rawDescription: Periodic Python Threads with on demand trigger


code:
import pandas as pd
a = [[1,2], [2,3], [3,4], [4, 5], [1, 6], [2,7], [1,8]]
df = pd.DataFrame(a,columns=['b','c'])
print df
z = df.groupby(['b']).apply(lambda tdf:pd.Series(dict([[vv,tdf[vv].unique().tolist()] for vv in tdf if vv not in ['b']])))
z = z.sort_index()
print z
print z['c'][1]
print z['c'][2]
print z['c'][3]
print z['c'][4]
z['d'] = 0.000
z[['d']] = z[['d']].astype(float)
len_b = len(z.index)
z['d'] = float(len_b)
z['e'] = 1/z['d']
z = z[['c', 'e']]
z.to_csv('your output folder')
print z

rawDescription: Python read a file and make a nth list from the


rawDescription: Curve fitting - monotonically increasing derivative


code:
from Bio import Entrez, Medline

# Always tell NCBI who you are
Entrez.email = "your_address@example.com"

term="((oncology) AND breast cancer) AND resulted in"

handle = Entrez.esearch(db="pubmed", retmax=10, term=term)
record = Entrez.read(handle)

print record['Count']  # see how many hits in your search

for ref in record['IdList']:
    handle = Entrez.efetch(db="pubmed", id=ref,
                           rettype="Medline",
                           retmode="text")
    paper = Medline.read(handle)
    # Medline returns a dict from which we can extract the
    # fields we desire
    print '-' * 30
    print paper['TI']
    print
    print paper['AB']

rawDescription: Navigating to next page using beautiful soup


code:
import asyncore
import socket
import sys

HOST = ''   # Symbolic name meaning all available interfaces
PORT = 9992 # Arbitrary non-privileged port

class ExampleHandler(asyncore.dispatcher_with_send):
    data = ''

    def handle_read(self):
        self.data += self.recv(1024)
        lines = self.data.split('\n')
        if len(lines) > 1:
            self.data = lines[-1]
            for line in lines:
                if not line: continue
                if line == 'CLOSE':
                    global s
                    self.send('You have requested to destroy the connection...\r\n')
                    self.close()
                    # To exit asyncore.loop() immediately,
                    # closing all client connections
                    raise asyncore.ExitNow()
                    # If you want to finish processing the client connections, you
                    # could instead close the server,
                    server.close()
                else:
                    self.send('OK...%s\r\n' % line)

    def handle_write(self):
        self.send('Welcome to the server. Type something and hit enter\r\n')

class ExampleServer(asyncore.dispatcher):

    def __init__(self, host, port):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind((host, port))
        self.listen(5)

    def handle_accept(self):
        pair = self.accept()
        if pair is not None:
            sock, addr = pair
            print 'Connected with ' + addr[0] + ':' + str(addr[1])
            handler = ExampleHandler(sock)
        else:
            print 'socket issue sorry'

server = ExampleServer(HOST, PORT)
try:
    asyncore.loop()
except asyncore.ExitNow:
    pass

rawDescription: How do I close a server socket while it is waiting for a connection?

description: Close a server socket while it is waiting for a connection


code:
def save(self):
    if(self.id):
        foo = Foo.objects.get(pk=self.id)
        if(foo.timestamp > self.timestamp):
            raise Exception, "trying to save outdated Foo"
    super(Foo, self).save()

rawDescription: How to prevent overwriting an object someone else has modified

description: Prevent overwriting an object someone else has modified


code:
print x
print re.sub("^(\D+)0+", r"\1", x)

rawDescription: Regex to remove first set of zeros

description: Regex to remove first set of zeros


code:
import win32com.client, os

wdDoNotSaveChanges = 0
path = os.path.abspath('snippet.txt')

snippet = 'Jon Skeet lieks ponies.  I can haz reputashunz?  '
snippet += 'This is a correct sentence.'
file = open(path, 'w')
file.write(snippet)
file.close()

app = win32com.client.gencache.EnsureDispatch('Word.Application')
doc = app.Documents.Open(path)
print "Grammar: %d" % (doc.GrammaticalErrors.Count,)
print "Spelling: %d" % (doc.SpellingErrors.Count,)

app.Quit(wdDoNotSaveChanges)

rawDescription: How can I use Microsoft Word's spelling/grammar checker programmatically?

description: Use Microsoft Word's spelling/grammar checker programmatically


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

class Robot(object):
    def __init__(self, id, x, y, vx, vy, mass):
        self.id = id
        self.x = x
        self.y = y
        self.vx = vx
        self.vy = vy
        self.ax = 0.
        self.ay = 0.
        self.mass = mass
    def rPrint(self):
        print "Robot ",self.id," at (",self.x,",",self.y,")"
    def interact(self, robot2):
        dx = (self.x-robot2.x)
        dy = (self.y-robot2.y)
        eps = 0.25
        idist3 = numpy.power(numpy.sqrt(dx*dx +dy*dy + eps*eps),-3)
        numerator = -self.mass*robot2.mass
        self.ax += numerator*dx*idist3
        self.ay += numerator*dy*idist3
        robot2.ax -= numerator*dx*idist3
        robot2.ay -= numerator*dy*idist3
    def updatePos(self, dt):
        self.x += 0.5*self.vx*dt
        self.y += 0.5*self.vy*dt
        self.vx += self.ax*dt
        self.vy += self.ay*dt
        self.x += 0.5*self.vx*dt
        self.y += 0.5*self.vy*dt
        self.ax = 0.
        self.ay = 0.

def init(nRobots):
    myRobotList = []
    vx = 0.
    vy = 0.
    mass = 1.
    for i in range(nRobots):
        randpos = numpy.random.uniform(-3,+3,2)
        rx = randpos[0]
        ry = randpos[1]
        myRobotList.append(Robot(i, rx, ry, vx, vy, mass))
    return myRobotList

def selfForces(robotList):
    nRobots = len(robotList)
    for i in range(nRobots-1):
       for j in range (i+1, nRobots):
            robotList[i].interact(robotList[j])

def otherRobotForces(myRobotList, otherRobotList):
    for i in myRobotList:
        for j in otherRobotList:
            i.interact(j)

def plotRobots(robotList):
    xl = []
    yl = []
    vxl = []
    vyl = []
    for i in robotList:
       xl.append(i.x)
       yl.append(i.y)
       vxl.append(i.vx)
       vyl.append(i.vy)
    pylab.subplot(1,1,1)
    pylab.plot(xl,yl,'o')
    pylab.quiver(xl,yl,vxl,vyl)
    pylab.show()

if __name__ == "__main__":
    comm = MPI.COMM_WORLD
    nprocs = comm.Get_size()
    rank   = comm.Get_rank()

    if (nprocs != 2):
        print "Only doing this for 2 for now.."
        sys.exit(-1)
    neigh = (rank + 1) %  nprocs

    robotList = init(50)

    for i in range (10):
        print "[",rank,"] Doing step ", i
        selfForces(robotList)

        request = comm.isend(robotList, dest=neigh, tag=11)
        otherRobotList = comm.recv(source=neigh, tag=11)

        otherRobotForces(robotList,otherRobotList)

        request.Wait()

        for r in robotList:
            r.updatePos(0.05)

    if (rank == 0):
        print "plotting Robots"
        plotRobots(robotList + otherRobotList)

rawDescription: How to parallelize this situation with robots

description: Parallelize this situation with robots


code:
import os.path, time
from datetime import datetime
from time import mktime

fulldate = time.ctime(os.path.getctime("file.xlsx"))
struct = time.strptime(fulldate)
filetime = datetime.fromtimestamp(mktime(struct))
filedate = filetime.replace(hour=0, minute=0, second=0, microsecond=0)

if filetime < datetime.now():
  print "The file is quite old"
else:
  print "The file is not so old"

rawDescription: Checking the created time for the date and comparing to todays date?


code:
html = "blah BAD blah blah blah Time Warner Satan. The blah .."

rx = r"""(?x)          # extended mode - enable comments
    (                  # match a tag
        <em            # tag name
          [^<>]*       # maybe also attributes
        >              # open tag matched
        (              # now match the tag body
            (?<!</em)  # there must be no closing tag before a character
            .          # a body character
        ) *            # some more characters like this
                  # closing tag
        \s*            # maybe some spaces after it
    ){2,}              # repeat the whole thing twice or more
"""

print re.sub(rx, r'{{\g<0>}}', html)
# blah BAD blah blah blah {{Time Warner Satan}}. The blah ..

rawDescription: Python regex match sequential html tags

description: Python regex match sequential html tags


rawDescription: Python: write floats to file, and reduce number of digits after decimal point


code:
def make_hook(f):
    """Decorator to turn 'foo' method into '__foo__'"""
    f.is_hook = 1
    return f

class MyType(type):
    def __new__(cls, name, bases, attrs):

        if name.startswith('None'):
            return None

        # Go over attributes and see if they should be renamed.
        newattrs = {}
        for attrname, attrvalue in attrs.iteritems():
            if getattr(attrvalue, 'is_hook', 0):
                newattrs['__%s__' % attrname] = attrvalue
            else:
                newattrs[attrname] = attrvalue

        return super(MyType, cls).__new__(cls, name, bases, newattrs)

    def __init__(self, name, bases, attrs):
        super(MyType, self).__init__(name, bases, attrs)

        # classregistry.register(self, self.interfaces)
        print "Would register class %s now." % self

    def __add__(self, other):
        class AutoClass(self, other):
            pass
        return AutoClass
        # Alternatively, to autogenerate the classname as well as the class:
        # return type(self.__name__ + other.__name__, (self, other), {})

    def unregister(self):
        # classregistry.unregister(self)
        print "Would unregister class %s now." % self

class MyObject:
    __metaclass__ = MyType

class NoneSample(MyObject):
    pass

# Will print "NoneType None"
print type(NoneSample), repr(NoneSample)

class Example(MyObject):
    def __init__(self, value):
        self.value = value
    @make_hook
    def add(self, other):
        return self.__class__(self.value + other.value)

# Will unregister the class
Example.unregister()

inst = Example(10)
# Will fail with an AttributeError
#inst.unregister()

print inst + inst
class Sibling(MyObject):
    pass

ExampleSibling = Example + Sibling
# ExampleSibling is now a subclass of both Example and Sibling (with no
# content of its own) although it will believe it's called 'AutoClass'
print ExampleSibling
print ExampleSibling.__mro__

rawDescription: What is a metaclass in Python?

description: What is a metaclass in Python


code:
import multiprocessing, time

def p_def():
        print "running p"
        time.sleep(5)

def v_def():
        print "running v"
        time.sleep(5)

v = multiprocessing.Process(target=v_def)

while True:
        time.sleep(0.25)
        p = multiprocessing.Process(target=p_def)
        p.start()
        while p.is_alive() == True:
                time.sleep(0.5)
                print "waiting for it to finish"

rawDescription: Python multiprocessing refuses to loop

description: Python multiprocessing refuses to loop


code:
class AmazonSpider(CrawlSpider):
    name = "amazon"
    allowed_domains = ['amazon.com']
    start_urls = ['http://www.amazon.com/b/ref=br_imp_ara-1?_encoding=UTF8&node=5550342011&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=desktop-hero-2&pf_rd_r=16WPRNKJ91B97JW7TQ27&pf_rd_t=36701&pf_rd_p=1990071642&pf_rd_i=desktop']

    def __init__(self):
        self.driver = webdriver.Firefox()

    def parse(self, response):
        self.driver.get(response.url)
        for element in self.driver.find_elements_by_css_selector('a.titleLink'):
            print element.text

        self.driver.close()

rawDescription: First time scrape amazon deals that is possibly AJAX


code:
import openpyxl

wb = openpyxl.Workbook()
wb = openpyxl.load_workbook(filename='input.xlsx')
ws = wb.active

for row in range(2, ws.get_highest_row() + 1):
    row_label_1 = ws['A%d' % row].value
    row_label_2 = ws['B%d' % row].value

    if row_label_1.find("/") != -1: # Simple test for date
        print row_label_1
    elif row_label_1 in ["Label 1","Label 2","Label 6"]:
        print "%-20s  %s" % (row_label_1, row_label_2)

rawDescription: Python - Read Excel file and print output to another file


code:
import socket
s = socket.socket()
KEY = "ce73007bb263c20268da59eb9fb52b06" # If you want to help me, you can use this key to connect to the server
USERNAME = "Vik2015"
HOST = "68.53.14.13"
PORT = 25566
s.connect((HOST, PORT))
print 'connected'
# login packet (pkt id, protocol version, user name, verification key, pad)
s.sendall('%c%c%-64s%-64s%c' % (0, 7, USERNAME, KEY, 0))
print 'sentall'
print s.recv(4096)
s.close()

rawDescription: Python - Interact with Minecraft Classic server


code:
wordlist = ["paka", "baka", "puki", "paki", "suki", "suku", "buku","baba","nabab"]
for n1,word1 in enumerate(wordlist):
    for word2 in wordlist[n1+1:]:
        if len(word1)==len(word2):
            ndiff=0
            for n,letter in enumerate(word1):
                if word2[n]!=letter:
                    ndiff+=1
            if ndiff==1:
                print word1, word2

rawDescription: Finding (phonological) minimal pairs with python


code:
name = raw_input ("What is your name?: ")
print "Hello, " + name + "!"

rawDescription: How would I take a user's input and reprint it in the Python shell


code:
def solve(fvals_sqrt, x0, debug_solve=True):
    """
    Solves the sqrt function, using newtons methon.
    """
    f, fp = fvals_sqrt(x0) # Get the return values from fvals_sqrt
    x0 = x0 + (f/fp)
    print x0

rawDescription: Python Variable scope with a function as a parameter


code:
def my_source_handler(app, docname, source):
    import re
    print 'test'
    source[0] = re.sub("foo", "bar", source[0])

rawDescription: Source-level transformation in Sphinx

description: Source-level transformation in Sphinx


code:
#Supratika

import scipy.io
import pickle
import gzip
import numpy
numpy.set_printoptions(threshold=numpy.nan)
#mat=scipy.io.loadmat('traffic_patches.mat')
#print mat
'''f=open('mattext1.txt','w+')
pickle.dump(mat,f)
f.close()
'''
dataset='mnist.pkl.gz'
#unpickling..

f = gzip.open(dataset, 'rb')
training_data, validation_data, test_data = pickle.load(f)

print 'we will print'
#print training_data[0], ' ', training_data[1]

g=open("sup_data2.txt","w")
for line in training_data[0]:
    #print type(line) --->    <type 'numpy.ndarray'>

    x=map(str,line.tolist())# makes space separated string frm a list of numbers
    g.write(' '.join(x))

#The above prints serially all the 784 pixel values of all the 60,000 images in mnist.

for val in training_data[1]:
    #y=map(str,val.tolist())
    #g.write(' '.join(y))
    print ' ',training_data[1][val] #class labels

g.close

print 'we printed'
#print training_data

#training_data[0]>file1.txt

f.close()

rawDescription: Viewing all the values from MNIST dataset, on terminal after unpickling


code:
text = r'''"column1a","column2a","column
  3a,",
"column\"this is, a test\"4a"
"column1a2","column2a2","column3a2","column4a2"
"column1b","colu
     mn2b,","column3b",
"column\"this is, a test\"4b"
"column1c,","column2c","column3c",
"column\"this is, a test\"4c"'''

import re

# Number of columns one line is supposed to have
columns = 4
# Temporary variable to hold partial lines
buffer = ""
# Our regex to check for each column
check = re.compile(r'"(?:[^"\\]*|\\.)*"')

# Read the file line by line
for line in text.split("\n"):
    # If there's no stored partial line, this is a new line
    if buffer == "":
        # Check if we get 4 columns and print, if not, put the line
        # into buffer so we store a partial line for later
        if len(check.findall(line)) == columns:
            print matches
        else:
            # use line.strip() if you need to trim whitespaces
            buffer = line
    else:
        # Update the variable (containing a partial line) with the
        # next line and recheck if we get 4 columns
        # use line.strip() if you need to trim whitespaces
        buffer = buffer + line
        # If we indeed get 4, our line is complete and print
        # We must not forget to empty buffer now that we got a whole line
        if len(check.findall(buffer)) == columns:
            print matches
            buffer = ""
        # Optional; always good to have a safety backdoor though
        # If there is a problem with the csv itself like a weird unescaped
        # quote, you send it somewhere else
        elif len(check.findall(buffer)) > columns:
            print "Error: cannot parse line:\n" + buffer
            buffer = ""

rawDescription: regular expressions in python using quotes

description: Regular expressions in python using quotes


code:
from sqlalchemy import create_engine, Column, Integer
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

def create_path(filename):
    import os
    fn = os.path.abspath(os.path.join(os.path.dirname(__file__), 'sqlite_files', filename))
    return fn

def run_script(filename):
    path_to_file = create_path(filename)

    engine = create_engine('sqlite:///{path}'.format(path=path_to_file), echo=True)
    Session = sessionmaker(bind=engine)
    Base = declarative_base(bind=engine)

    # object model
    class MyTable(Base):
        __tablename__ = 'my_table'
        __table_args__ = {'autoload': True}
        id = Column(Integer, primary_key=True) # Monkey patching the id column as primary key

    # script itself
    session = Session()
    for row in session.query(MyTable).all():
        print row
    return "Stop!"

def main(argv):
    assert argv, "must specify a database file name"
    run_script(argv[0])

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))

rawDescription: How to (re-) arrange this python code without breaking sqlalchemy relevant dependencies?


code:
import subprocess
foo=subprocess.check_output(['nso', 'status', '|', 'awk', '\'{ print $1 }\''], shell=True)
#Thanks bereal for shell=True
print foo

rawDescription: Python test if client exist

description: Python test if client exist


code:
soup = BeautifulSoup(your_old_html)
soup.body.insert(0, your_tag)
print soup

rawDescription: Add content inside html body tag

description: Add content inside html body tag


rawDescription: how to convert list of dictonaries into ....?

description: Convert list of dictonaries into ....


code:
from collections import defaultdict
from multiprocessing.pool import ThreadPool

class MyEvent:
    handlers = defaultdict(list)
    _pool = ThreadPool(5)

    @classmethod
    def setup(cls, notificationname, functionname):
        cls.handlers[notificationname].append(functionname)

    @classmethod
    def runonnotification(cls, notificationname, *args):
        thisfunclist = cls.handlers[notificationname]
        for func in thisfunclist:
            cls._pool.apply_async(func, args=args)

class SimpleExample(object):
    def __init__(self, a=1, b=2):
        SimpleExample.a = a
        SimpleExample.b = b
        MyEvent.setup('greater than 100', self.printerror)
        MyEvent.setup('dont do negative', self.negation)
        MyEvent.setup('many values recieved', self.handlemultipleupdates)

    def updation(self, updateval):
        if updateval > 100:
            MyEvent.runonnotification('greater than 100', updateval)
            self.a = updateval
        if updateval < 0:
            MyEvent.runonnotification('dont do negative')

    def multipleupdates(self, a, b):
        MyEvent.runonnotification('many values recieved', a, b)

    def printerror(self, data):
        print 'something has gone wrong ', data

    def negation(self):
        print 'negation enter'
        self.a = -self.a

    def handlemultipleupdates(self, a, b):
        print 'wow'
        self.a = a
        self.b = b

s = SimpleExample()
for x in [-50, 0, 1, 50, 70, 101]:
    s.updation(x)

MyEvent._pool.close()
MyEvent._pool.join()

rawDescription: Raising asynchronous events in python

description: Raising asynchronous events in python


code:
params = """Action: login
Events: off
Username: admin
Secret: mypass"""

s.send(params + '\r\n')
data = s.recv(1024)
print data + '\n'

params = 'Action: status'
s.send(params + '\r\n')
data = s.recv(1024)
print data + '\n'

params = 'Action: Logoff'
s.send(params + '\r\n')
data = s.recv(1024)
print data + '\n'

rawDescription: How to get the status of an Asterisk Server using a Socket - Python

description: Get the status of an Asterisk Server using a Socket - Python


code:
from BeautifulSoup import BeautifulSoup
import requests

# point to output directory
outpath = 'D:/patent_zips/'
url = 'http://www.google.com/googlebooks/uspto-patents-grants.html'
mbyte=1024*1024

print 'Reading: ', url
html = requests.get(url).text
soup = BeautifulSoup(html)

print 'Processing: ', url
for name in soup.findAll('a', href=True):
    zipurl = name['href']
    if( zipurl.endswith('.zip') ):
        outfname = outpath + zipurl.split('/')[-1]
        r = requests.get(zipurl, stream=True)
        if( r.status_code == requests.codes.ok ) :
            fsize = int(r.headers['content-length'])
            print 'Downloading %s (%sMb)' % ( outfname, fsize/mbyte )
            with open(outfname, 'wb') as fd:
                for chunk in r.iter_content(chunk_size=1024): # chuck size can be larger
                    if chunk: # ignore keep-alive requests
                        fd.write(chunk)
                fd.close()

rawDescription: Beautifulsoup download all .zip files from Google Patent Search


code:
"""
Created on 16.08.2010

@author: Jochen Ritzel
"""

import dht

from twisted.names import dns, server, client, cache
from twisted.application import service, internet

class P2PMapping(dht.EntangledDHT):

    def __contains__(self, key):
        return key.endswith('.kad')

class MapResolver(client.Resolver):
    """
    Resolves names by looking in a mapping.
    If `name in mapping` then mapping[name] should return a IP
    else the next server in servers will be asked for name
    """
    def __init__(self, mapping, servers):
        self.mapping = mapping
        client.Resolver.__init__(self, servers=servers)
        self.ttl = 10

    def lookupAddress(self, name, timeout = None):
        # find out if this is a .kad. request
        if name in self.mapping:
            result = self.mapping[name] # get the result
            def packResult( value ):
                return [
                        (dns.RRHeader(name, dns.A, dns.IN, self.ttl, dns.Record_A(value, self.ttl)),), (), ()
                ]
            result.addCallback(packResult) # put it in a A Record
            return result
        else:
            return self._lookup(name, dns.IN, dns.A, timeout)

## this sets up the application

application = service.Application('dnsserver', 1, 1)

## set up the DHT
mapping = P2PMapping(bootstrap=[('127.0.0.1', 4001)])
mapping['jochen.kad'] = '99.99.99.99' # "register" domain with IP

# set up a resolver that uses the mapping or a secondary nameserver
p2presolver = MapResolver(mapping, servers=[('192.168.178.1', 53)])

# create the protocols
f = server.DNSServerFactory(caches=[cache.CacheResolver()], clients=[p2presolver])
p = dns.DNSDatagramProtocol(f)
f.noisy = p.noisy = False

# register as tcp and udp
ret = service.MultiService()
PORT=53

for (klass, arg) in [(internet.TCPServer, f), (internet.UDPServer, p)]:
    s = klass(PORT, arg)
    s.setServiceParent(ret)

# run all of the above as a twistd application
ret.setServiceParent(service.IServiceCollection(application))

# run it through twistd!
if __name__ == '__main__':
    import sys
    print "Usage: twistd -y %s" % sys.argv[0]

rawDescription: Python DNS server with custom backend

description: Python DNS server with custom backend


code:
v = [4, 2, 1, 5]

n = len(v)
s = 0 # this is going to be the sum
for idx, x in enumerate(v):
    s += (n - 1 - 2 * idx) * x
print s

rawDescription: Sum all unique pair differences in a vector


code:
import sys
import asyncio

def got_stdin_data(q):
    asyncio.async(q.put(sys.stdin.readline()))

class EchoServerClientProtocol(asyncio.Protocol):
   def connection_made(self, transport):
       peername = transport.get_extra_info('peername')
       print('Connection from {}'.format(peername))
       self.transport = transport

   def data_received(self, data):
       message = data.decode()
       print('Data received: {!r}'.format(message))
       fut = asyncio.async(q.get())
       fut.add_done_callback(self.write_reply)

   def write_reply(self, fut):
       reply = fut.result()
       print('Send: {!r}'.format(reply))
       self.transport.write(reply.encode())

       #print('Close the client socket')
       #self.transport.close()

q = asyncio.Queue()
loop = asyncio.get_event_loop()
loop.add_reader(sys.stdin, got_stdin_data, q)
# Each client connection will create a new protocol instance
coro = loop.create_server(EchoServerClientProtocol, '127.0.0.1', 8888)
server = loop.run_until_complete(coro)

# Serve requests until CTRL+c is pressed
print('Serving on {}'.format(server.sockets[0].getsockname()))
try:
    loop.run_forever()
except KeyboardInterrupt:
    pass

# Close the server
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()

rawDescription: Prompt for user input using python asyncio.create_server instance


code:
import csv

csvfile = open("SANDoubleSuperMatrix.csv", "rU")
dialect = csv.Sniffer().sniff(csvfile.read(1024))
csvfile.seek(0)
reader = csv.reader(csvfile, dialect)

identity = []

for line in reader:
    identity.append(line[2])

print identity

rawDescription: Extracting BLAST output columns in CSV form with python


code:
import random

# set seed and get state
random.seed(0)
orig_state = random.getstate()

print random.random()
# 0.8444218515250481

# change the RNG seed
random.seed(1)
print random.random()
# 0.13436424411240122

# setting the seed back to 0 resets the RNG back to the original state
random.seed(0)
new_state = random.getstate()
assert new_state == orig_state

# since the state of the RNG is the same as before, it will produce the same
# sequence of pseudorandom output
print random.random()
# 0.8444218515250481

# we could also achieve the same outcome by resetting the state explicitly
random.setstate(orig_state)
print random.random()
# 0.8444218515250481

rawDescription: Python random use both state and seed?

description: Python random use both state and seed


code:
a= 0
if a == 0 :
    print "a==0"
print "hello"

rawDescription: PYTHON - Stop executing function/def

description: PYTHON - Stop executing function/def


code:
class A(object):
    pass
A
del A
A
object.__subclasses__()[-2]
# using weakref
import weakref
weakref.getweakrefs(object.__subclasses__()[-2])
# Another method using gc to see the referrers:
import gc
gc.collect()
gc.get_referrers(object.__subclasses__()[-2])
# See the referents
print '\n'.join(map(str, gc.get_referents(object.__subclasses__()[-2])))

rawDescription: Is it possible to delete (not just unlink) a class?


code: print zip(*squares)

rawDescription: Dynamic arguments for Python functions

description: Dynamic arguments for Python functions


code:
import paramiko
import socket

s = socket.socket()
s.connect(('localhost', 22))
t = paramiko.Transport(s)
t.connect()

try:
    t.auth_none('')
except paramiko.BadAuthenticationType, err:
    print err.allowed_types

rawDescription: get_allowed_auths() in paramiko for authentication types

description: Get_allowed_auths() in paramiko for authentication types


code:
#!/usr/bin/env python

# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

"""
Simple IMAP4 client which displays the subjects of all messages in a
particular mailbox.
"""

import sys

from twisted.internet import protocol
from twisted.internet import ssl
from twisted.internet import defer
from twisted.internet import stdio
from twisted.mail import imap4
from twisted.protocols import basic
from twisted.python import util
from twisted.python import log

class TrivialPrompter(basic.LineReceiver):
    from os import linesep as delimiter

    promptDeferred = None

    def prompt(self, msg):
        assert self.promptDeferred is None
        self.display(msg)
        self.promptDeferred = defer.Deferred()
        return self.promptDeferred

    def display(self, msg):
        self.transport.write(msg)

    def lineReceived(self, line):
        if self.promptDeferred is None:
            return
        d, self.promptDeferred = self.promptDeferred, None
        d.callback(line)

class SimpleIMAP4Client(imap4.IMAP4Client):
    """
    A client with callbacks for greeting messages from an IMAP server.
    """
    greetDeferred = None

    def serverGreeting(self, caps):
        self.serverCapabilities = caps
        if self.greetDeferred is not None:
            d, self.greetDeferred = self.greetDeferred, None
            d.callback(self)

class SimpleIMAP4ClientFactory(protocol.ClientFactory):
    usedUp = False

    protocol = SimpleIMAP4Client

    def __init__(self, username, onConn):
        self.ctx = ssl.ClientContextFactory()

        self.username = username
        self.onConn = onConn

    def buildProtocol(self, addr):
        """
        Initiate the protocol instance. Since we are building a simple IMAP
        client, we don't bother checking what capabilities the server has. We
        just add all the authenticators twisted.mail has.  Note: Gmail no
        longer uses any of the methods below, it's been using XOAUTH since
        2010.
        """
        assert not self.usedUp
        self.usedUp = True

        p = self.protocol(self.ctx)
        p.factory = self
        p.greetDeferred = self.onConn

        p.registerAuthenticator(imap4.PLAINAuthenticator(self.username))
        p.registerAuthenticator(imap4.LOGINAuthenticator(self.username))
        p.registerAuthenticator(
                imap4.CramMD5ClientAuthenticator(self.username))

        return p

    def clientConnectionFailed(self, connector, reason):
        d, self.onConn = self.onConn, None
        d.errback(reason)

def cbServerGreeting(proto, username, password):
    """
    Initial callback - invoked after the server sends us its greet message.
    """
    # Hook up stdio
    tp = TrivialPrompter()
    stdio.StandardIO(tp)

    # And make it easily accessible
    proto.prompt = tp.prompt
    proto.display = tp.display

    # Try to authenticate securely
    return proto.authenticate(password
        ).addCallback(cbAuthentication, proto
        ).addErrback(ebAuthentication, proto, username, password
        )

def ebConnection(reason):
    """
    Fallback error-handler. If anything goes wrong, log it and quit.
    """
    log.startLogging(sys.stdout)
    log.err(reason)
    return reason

def cbAuthentication(result, proto):
    """
    Callback after authentication has succeeded.

    Lists a bunch of mailboxes.
    """
    return proto.list("", "*"
        ).addCallback(cbMailboxList, proto
        )

def ebAuthentication(failure, proto, username, password):
    """
    Errback invoked when authentication fails.

    If it failed because no SASL mechanisms match, offer the user the choice
    of logging in insecurely.

    If you are trying to connect to your Gmail account, you will be here!
    """
    failure.trap(imap4.NoSupportedAuthentication)
    return proto.prompt(
        "No secure authentication available. Login insecurely? (y/N) "
        ).addCallback(cbInsecureLogin, proto, username, password
        )

def cbInsecureLogin(result, proto, username, password):
    """
    Callback for "insecure-login" prompt.
    """
    if result.lower() == "y":
        # If they said yes, do it.
        return proto.login(username, password
            ).addCallback(cbAuthentication, proto
            )
    return defer.fail(Exception("Login failed for security reasons."))

def cbMailboxList(result, proto):
    """
    Callback invoked when a list of mailboxes has been retrieved.
    """
    result = [e[2] for e in result]
    s = '\n'.join(['%d. %s' % (n + 1, m) for (n, m) in zip(range(len(result)), result)])
    if not s:
        return defer.fail(Exception("No mailboxes exist on server!"))
    return proto.prompt(s + "\nWhich mailbox? [1] "
        ).addCallback(cbPickMailbox, proto, result
        )

def cbPickMailbox(result, proto, mboxes):
    """
    When the user selects a mailbox, "examine" it.
    """
    mbox = mboxes[int(result or '1') - 1]
    return proto.examine(mbox
        ).addCallback(cbExamineMbox, proto
        )

def cbExamineMbox(result, proto):
    """
    Callback invoked when examine command completes.

    Retrieve the subject header of every message in the mailbox.
    """
    return proto.fetchSpecific('1:*',
                               headerType='HEADER.FIELDS',
                               headerArgs=['SUBJECT'],
        ).addCallback(cbFetch, proto
        )

def cbFetch(result, proto):
    """
    Finally, display headers.
    """
    if result:
        keys = result.keys()
        keys.sort()
        for k in keys:
            proto.display('%s %s' % (k, result[k][0][2]))
    else:
        print "Hey, an empty mailbox!"

    return proto.logout()

def cbClose(result):
    """
    Close the connection when we finish everything.
    """
    from twisted.internet import reactor
    reactor.stop()

def main():
    hostname = raw_input('IMAP4 Server Hostname: ')
    port = raw_input('IMAP4 Server Port (the default is 143, 993 uses SSL): ')
    username = raw_input('IMAP4 Username: ')
    password = util.getPassword('IMAP4 Password: ')

    onConn = defer.Deferred(
        ).addCallback(cbServerGreeting, username, password
        ).addErrback(ebConnection
        ).addBoth(cbClose)

    factory = SimpleIMAP4ClientFactory(username, onConn)

    from twisted.internet import reactor
    if port == '993':
        reactor.connectSSL(hostname, int(port), factory, ssl.ClientContextFactory())
    else:
        if not port:
            port = 143
        reactor.connectTCP(hostname, int(port), factory)
    reactor.run()

if __name__ == '__main__':
    main()

rawDescription: Trigger script upon email receipt

description: Trigger script upon email receipt


code:
#!/usr/bin/env python

import sys

def generate_log_report(logfile):
        for line in logfile:
                line_split = line.split()
                list = [line_split[0], line_split[1], line_split[2], line_split[4]]
                if "exim" in list[3]:
                        l = [line_split[0], line_split[1], line_split[2], line_split[4]]
                else:
                        li = [line_split[0], line_split[1], line_split[2], line_split[4]]
        return l

if __name__ == "__main__":
        if not len(sys.argv) > 1:
                print __doc__
                sys.exit(1)
        infile_name = sys.argv[1]
        try:
                infile = open(infile_name, "r")
        except IOError:
                print "you must specify a valid file"
                print __doc__
                sys.exit(1)
        log_report = generate_log_report(infile)
        print log_report
        infile.close()

rawDescription: python logparse search specific text

description: Python logparse search specific text


code:
char_size = 80
pieces = [my_long_str[x:x+char_size] for x in range(0,len(my_long_str),char_size)]

for piece in pieces:
    print piece

rawDescription: Sublime Os X output window word-wrap

description: Sublime Os X output window word-wrap


code:
s = set([u'first.last@domain.com',u'first1.last1@domain.com'])
s = {str(item) for item in s}
print s

rawDescription: Retrieving email Ids for a group using Python


code:
from bs4 import BeautifulSoup
import urllib2

url = "http://example.com/"
soup = BeautifulSoup(urllib2.urlopen(url).read())

sources = []
scripts = []

for script in soup('script', {'type': 'text/javascript'}):
    src = script.get('src')
    if src:
        sources.append(src)
    else:
        scripts.append(script.text)

print scripts
print sources

rawDescription: Downloading all external and internal javascript files in python


code:
import multiprocessing as mp

class Worker(mp.Process):
    def __init__(self):
        print "Init"
        mp.Process.__init__(self)
        self.num = mp.Value('d', 0.0)

    def run(self):
        print "Running"
        self.num.value = 1

p = Worker()
p.start()
p.join()
print p.num.value

rawDescription: Python class inheriting multiprocessing, trouble with accessing class members


code:
DOC = """<sbml xmlns="http://www.sbml.org/sbml/level2/version4" xmlns:celldesigner="http://www.sbml.org/2001/ns/celldesigner" level="2" version="4">
  <model metaid="untitled" id="untitled">
    <annotation>...</annotation>
    <listOfUnitDefinitions>...</listOfUnitDefinitions>
    <listOfCompartments>...</listOfCompartments>
    <listOfSpecies>
      <species metaid="s1" id="s1" name="GenA" compartment="default" initialAmount="0">
        <annotation>
          <celldesigner:extension>...</celldesigner:extension>
        </annotation>
      </species>
      <species metaid="s2" id="s2" name="s2" compartment="default" initialAmount="0">
        <annotation>
           <celldesigner:extension>...</celldesigner:extension>
        </annotation>
      </species>
    </listOfSpecies>
    <listOfReactions>...</listOfReactions>
  </model>
</sbml>"""

from lxml import etree
from StringIO import StringIO
NS = "http://this.is.some/custom_namespace"
tree = etree.ElementTree(element=None, file=StringIO(DOC))
root = tree.getroot()
nsmap = root.nsmap
nsmap['kjw'] = NS
new_root = etree.Element(root.tag, nsmap=nsmap)
new_root[:] = root[:]
new_root.append(etree.Element('{%s}%s' % (NS, 'test')))
new_root.append(etree.Element('{%s}%s' % (NS, 'test')))
print etree.tostring(new_root, pretty_print=True)

rawDescription: lxml: add namespace to input file

description: Lxml: add namespace to input file


code:
import random

num = random.randint(7, 14)
print num

message = ""
prevnum = -1
for n in range(num):
    newnum = random.randint(100, 103)
    while newnum == prevnum:
        newnum = random.randint(100, 103)
    prevnum = newnum
    message += chr(newnum)

rawDescription: While Loop and if statement

description: While Loop and if statement


code:
def getcolumn(n,data):
    return (i[n] for i in data) # if this dosent work replace () with []

for i in getcolumn(1,data):
    print i

rawDescription: Python output entire column

description: Python output entire column


code:
# First, create a dictionary for each column, that maps each
# value (eg colorfront2) to a list of house names.
results = [{}, {}, {}, {}]
for filename in os.listdir('/wherever'):
    s = open(os.path.join('/wherever', filename), 'rb').read()
    for line in s.split('\n'):
        if not line.strip(): continue # skip blank lines
        house, values = line.split(',', 1)
        values = values.split(',')
        assert len(values) == len(results) # sanity check
        for value, result in zip(values, results):
            if value not in result:
                result[value] = []
            result[value].append(house)

# Then, do something with it -- e.g., show them in order
for i, result in enumerate(results):
    print 'COLUMN %d' % i
    def sortkey(pair): return len(pair[1]) # number of houses
    for (val, houses) in sorted(result.items(), key=sortkey, reverse=True):
        print '   %20s occurs for %d houses' % (val, len(houses))

rawDescription: load text file then load different words into different lists

description: Load text file then load different words into different lists


code:
import os
import urllib2

input_file = "urls.txt"
debug = True

good_urls = []
bad_urls = []

bad, good = range(2)

def track(url, good_flag, code):
    if good_flag == good:
        good_str = "good"
    elif good_flag == bad:
        good_str = "bad"
    else:
        good_str = "ERROR! (" + repr(good) + ")"
    if debug:
        print("DEBUG: %s: '%s' code %s" % (good_str, url, repr(code)))
    if good_flag == good:
        good_urls.append(url)
    else:
        bad_urls.append(url)

with open(input_file) as f:
    for line in f:
        url = line.strip()
        try:
            r = urllib2.urlopen(url)
            if r.code in (200, 401):
                print '[{0}]: '.format(url), "Up!"
            if r.code == 404:
                # URL is bad if it is missing (code 404)
                track(url, bad, r.code)
            else:
                # any code other than 404, assume URL is good
                track(url, good, r.code)
        except urllib2.URLError as e:
            track(url, bad, "exception!")

# if any URLs were bad, rewrite the input file to remove them.
if bad_urls:
    # simple way to get a filename for temp file: append ".tmp" to filename
    temp_file = input_file + ".tmp"
    with open(temp_file, "w") as f:
        for url in good_urls:
            f.write(url + '\n')
    # if we reach this point, temp file is good.  Remove old input file
    os.remove(input_file)  # only needed for Windows
    os.rename(temp_file, input_file)  # replace original input file with temp file

rawDescription: How to delete a line from a file after it has been used

description: Delete a line from a file after it has been used


code:
def timeit(method):
    def timed(self, *args, **kw):
        result = method(self, *args, **kw)

        className = type(self).__name__
        print '%s.%s'%(className, method.func_name)
        #want printed: Test.run

        return result

    return timed

class Test(object):
    @timeit
    def run(self):
        print 'run'

a = Test()
a.run()

rawDescription: get class name in decorator python 2.7

description: Get class name in decorator python 2.7


code:
import re

myString = 'myPERLPythonJavaScriptJavaTextSample'
myString = re.sub(r'((?<=[a-z])[A-Z]|(?<!\A)[A-Z](?=[a-z]))', r' \1', myString)
result = myString.split()
print result

rawDescription: String Splitting on upper case but retain upper case substrings


code:
from multiprocessing import Pool

foo = {1: []}

def f(x):
    return x

def main():
    pool = Pool()
    foo[1] = pool.map(f, range(100))
    pool.close()
    pool.join()
    print foo

if __name__ == '__main__':
    main()

rawDescription: Python multiprocessing pool, join; not waiting to go on?