code:
import h5py
import numpy as np

f = h5py.File('yourfile.hdf', 'r')
dataset = f['/M/data']

# Load the entire array into memory, like you're doing for matlab...
data = np.empty(dataset.shape, dataset.dtype)
dataset.read_direct(data)

print data.sum() #Or alternately, "np.sum(data)"

rawDescription: Loading Matlab sparse matrix saved with -v7.3 (HDF5) into Python and operating on it


code:
from sqlalchemy import create_engine, Column, Integer, Unicode, ForeignKey
from sqlalchemy.orm import relationship, scoped_session, sessionmaker
from sqlalchemy.orm.collections import attribute_mapped_collection
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy

# Configure test data SA
engine = create_engine(u'sqlite:///:memory:', echo=False)
session = scoped_session(sessionmaker(bind=engine, autoflush=False))
Base = declarative_base()

class _BaseMixin(object):
    """
    A helper mixin class to set properties on object creation.

    Also provides a convenient default __repr__() function, but be aware that
    also relationships are printed, which might result in loading the relation
    objects from the database
    """
    def __init__(self, **kwargs):
        for k, v in kwargs.items():
            setattr(self, k, v)

    def __repr__(self):
        return "<%s(%s)>" % (self.__class__.__name__,
            ', '.join('%s=%r' % (k, self.__dict__[k])
                      for k in sorted(self.__dict__)
                      if '_' != k[0]
                      #if '_sa_' != k[:4] and '_backref_' != k[:9]
                      )
            )

# relation creator factory functions
def _creator_gr(group, role):
    res = UserGroup(group=group, role=role)
    return res
def _creator_ur(user, role):
    res = UserGroup(user=user, role=role)
    return res

##############################################################################
# Object Model
##############################################################################
class Role(Base, _BaseMixin):
    __tablename__ = 'roles'
    # columns
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(Unicode(16), unique=True)
    # relations
    usergroup = relationship("UserGroup", backref='role')

class User(Base, _BaseMixin):
    __tablename__ = 'users'
    # columns
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(Unicode(16), unique=True)
    # relations
    _rel_usergroup = relationship("UserGroup", collection_class=attribute_mapped_collection('group'),
                                  cascade='all,delete-orphan',
                                  backref='user',
                                  )
    groups = association_proxy('_rel_usergroup', 'role', creator=_creator_gr)

class Group(Base, _BaseMixin):
    __tablename__ = 'groups'
    # columns
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(Unicode(16), unique=True)
    # relations
    _rel_usergroup = relationship("UserGroup", collection_class=attribute_mapped_collection('user'),
                                  cascade='all,delete-orphan',
                                  backref='group',
                                  )
    users = association_proxy('_rel_usergroup', 'role', creator=_creator_ur)

class UserGroup(Base, _BaseMixin):
    __tablename__ = 'user_group'
    # columns
    id = Column(Integer, primary_key=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey('users.id', ondelete='CASCADE'), nullable=False)
    group_id = Column(Integer, ForeignKey('groups.id', ondelete='CASCADE'), nullable=False)
    role_id = Column(Integer, ForeignKey('roles.id', ondelete='CASCADE'), nullable=False)
    # relations
    # (all backrefs)

##############################################################################
# TESTS (showing usages)
#
# Requirements:
#  - list all groups of the user: user.groups (use keys)
#  - list all users of the group: group.users (use keys)
#  - get all users ordered (grouped) by group with the role title
##############################################################################

def _requirement_get_user_groups(user):
    return user.groups.keys()

def _requirement_get_group_users(group):
    return group.users.keys()

def _requirement_get_all_users_by_group_with_role():
    qry = session.query(Group).order_by(Group.name)
    res = []
    for g in qry.all():
        for u, r in sorted(g.users.items()):
            value = (g.name, u.name, r.name)
            res.append(value)
    return res

def _test_all_requirements():
    print '--requirement: all-ordered:'
    for v in _requirement_get_all_users_by_group_with_role():
        print v

    print '--requirement: user-groups:'
    for v in session.query(User).order_by(User.id):
        print v, " has groups: ",  _requirement_get_user_groups(v)

    print '--requirement: group-users:'
    for v in session.query(Group).order_by(Group.id):
        print v, " has users: ",  _requirement_get_group_users(v)

# create db schema
Base.metadata.create_all(engine)

##############################################################################
# CREATE TEST DATA
##############################################################################

# create entities
u_peter = User(name='u_Peter')
u_sonja = User(name='u_Sonja')
g_sales = Group(name='g_Sales')
g_wales = Group(name='g_Wales')
r_super = Role(name='r_Super')
r_minor = Role(name='r_Minor')

# helper functions
def _get_entity(entity, name):
    return session.query(entity).filter_by(name=name).one()
def get_user(name):
    return _get_entity(User, name)
def get_group(name):
    return _get_entity(Group, name)
def _checkpoint():
    session.commit()
    session.expunge_all()
    _test_all_requirements()
    session.expunge_all()
    print '-' * 80

# test: **ADD**
u_peter.groups[g_wales] = r_minor # add
g_wales.users[u_sonja] = r_super # add
g_sales.users[u_peter] = r_minor # add
session.add(g_wales)
#session.add(g_sales)
_checkpoint()

# test: **UPDATE**
u_peter = get_user('u_Peter')
assert u_peter.name == 'u_Peter' and len(u_peter.groups) == 2
assert len(u_peter.groups) == 2
g_wales = get_group('g_Wales')
g_wales.users[u_peter] = r_super # update
_checkpoint()

# test: **DELETE**
u_peter = get_user('u_Peter')
assert u_peter.name == 'u_Peter' and len(u_peter.groups) == 2
g_wales = get_group('g_Wales')
del u_peter.groups[g_wales] # delete
_checkpoint()

rawDescription: Many-to-many declarative SQLAlchemy definition for users, groups, and roles


code:
def rects_bound(rects):
    """ Returns a rectangle that bounds all given rectangles
    """
    def bounding_rect_reducer((ax, ay, aw, ah), (bx, by, bw, bh)):
        return (min(ax,bx),
                min(ay,by),
                max(ax+aw, bx+bw),
                max(ay+ah, by+bh))

    return reduce(bounding_rect_reducer, rects)

rawDescription: Formatting long python lines

description: Formatting long python lines


code:
# Import 3rd Party libraries
from selenium                                       import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class requester_firefox(object):
    def __init__(self):
        self.selenium_browser = webdriver.Firefox()
        self.selenium_browser.set_page_load_timeout(30)

    def __del__(self):
        self.selenium_browser.quit()
        self.selenium_browser = None

    def __call__(self, url):
        try:
            self.selenium_browser.get(url)
            the_page = self.selenium_browser.page_source
        except Exception:
            the_page = ""
        return the_page

test = requester_firefox()
print test("http://www.seek.com.au/jobs/in-australia/#dateRange=999&workType=0&industry=&occupation=&graduateSearch=false&salaryFrom=0&salaryTo=999999&salaryType=annual&advertiserID=&advertiserGroup=&keywords=police+check&page=1&isAreaUnspecified=false&location=&area=&nation=3000&sortMode=Advertiser&searchFrom=quick&searchType=").encode("ascii", "ignore")

rawDescription: Retrieving scripted page urls via web scrape


code:
a = num // dem
b = num % dem
print 'The mixed number is {} and {}/{}'.format(a, b, dem)

rawDescription: How to convert an improper fraction to a mixed number, using python

description: Convert an improper fraction to a mixed number, using python


code:
import random
import ast

def pop(a,accumulated=None):
    print "type q to move on to verification"
    if accumulated is None:
        accumulated = set()
    v = 'I can put any string here, as long as it is not "q" :-)'
    while v.lower() != 'q':
        b = a.pop(0)
        if b <= 15:
            print 'b', b,
        elif b > 15 and b <=30:
            print 'i', b,
        elif b > 30 and b <=45:
            print 'n', b,
        elif b > 45 and b <=60:
            print 'g', b,
        else:
            print 'o', b,
        accumulated.add(b)
        v = raw_input('')
    return accumulated

def verify(numbers):
    new_nums = raw_input("enter numbers separated by ',': ")
    nums = ast.literal_eval(new_nums)
    assert( len(nums) == 5 ) #Need 5 numbers to win
    result = set(nums).issubset(numbers)
    print "Verified? ",result
    return result
    #alternatively, and probably slightly more efficient
    # print numbers.issuperset(nums)
    #I prefer the other though as `subset` is easier for me to remember

def robust_verify(numbers):
   """
   keep trying to verify the numbers until we succeed
   """
   try:
       verify(numbers)
   except (AssertionError,SyntaxError,TypeError):  #other error conditions might go here too, but these are the ones I can think of right now ...
       robust_verify(numbers)

def play_bingo(game_vals=None,accumulated=None):
    if game_vals is None:
        game_vals = list(range(1,76))  #list strictly not necessary here, but why not?
        random.shuffle(game_vals)

    nums = pop(game_vals,accumulated=accumulated)
    if not robust_verify(nums):
        play_bingo(game_vals=game_vals,accumulated=nums)

play_bingo()

rawDescription: bingo number check

description: Bingo number check


code:
import wx

class MainWidget(wx.Frame):

    def __init__(self, parent, title):
        super(MainWidget, self).__init__(parent, title=title)

        self.list = wx.ListCtrl(parent=self)
        for i,j in enumerate('abcdef'):
            self.list.InsertStringItem(i,j)
        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnClick, self.list)

        self.Layout()

    def OnClick(self, event):
        print event.GetText()

if __name__ == '__main__':
    app = wx.App(redirect=False)
    frame = MainWidget(None, "ListCtrl Test")
    frame.Show(True)
    app.MainLoop()

rawDescription: wxPython ListCtrl OnClick Event

description: WxPython ListCtrl OnClick Event


code:
from selenium import selenium
browser = selenium('localhost','4444','*firefox','http://google.com')
version = browser.get_eval('Selenium.version + Selenium.revision')
print version

rawDescription: how to get the version of Selenium RC server

description: Get the version of Selenium RC server


code:
import csv
import multiprocessing
import os
import tempfile

def split(infilename, num_chunks=multiprocessing.cpu_count()):
    READ_BUFFER = 2**13
    in_file_size = os.path.getsize(infilename)
    print 'in_file_size:', in_file_size
    chunk_size = in_file_size // num_chunks
    print 'target chunk_size:', chunk_size
    files = []
    with open(infilename, 'rb', READ_BUFFER) as infile:
        for _ in xrange(num_chunks):
            temp_file = tempfile.TemporaryFile()
            while temp_file.tell() < chunk_size:
                try:
                    temp_file.write(infile.next())
                except StopIteration:  # end of infile
                    break
            temp_file.seek(0)  # rewind
            files.append(temp_file)
    return files

files = split("sample_simple.csv", num_chunks=4)
print 'number of files created: {}'.format(len(files))

for i, ifile in enumerate(files, start=1):
    print 'size of temp file {}: {}'.format(i, os.path.getsize(ifile.name))
    print 'contents of file {}:'.format(i)
    reader = csv.reader(ifile)
    for row in reader:
        print row
    print ''

rawDescription: Python: splitting a CSV file into equal parts


code:
class MyRequest(dict):
    def __init__(self, firstName=None, lastName=None):
        self['firstName'] = firstName
        self['lastName'] = lastName

    @property
    def firstName(self):
        return self['firstName']

    @firstName.setter
    def firstName(self, firstName):
        self['firstName'] = firstName

    @property
    def lastName(self):
        return self['lastName']

    @lastName.setter
    def lastName(self, lastName):
        self['lastName'] = lastName

request = MyRequest('John', 'Connor')
print request.firstName, request.lastName
request.firstName = 'Sarah'
print request.firstName, request.lastName

rawDescription: getters/setters for a python class that inherits dict

description: Getters/setters for a python class that inherits dict


code:
class Functions(object):
    def __init__(self):
        self.funcs = {}

    def add(self, func):
        self.funcs[func.__name__] = func
        return func

    def dispatch(self, name, *args, **kwargs):
        return self.funcs[name](*args, **kwargs)

a = Functions()

@a.add
def test(foo):
    return foo + 3

@a.add
def hello(bar):
    return bar * 3

print a.dispatch('test', 6)    # returns 9
print a.dispatch('hello', 6)   # returns 18

rawDescription: Python single dispatch functions (PEP 443)


code:
def get_dice_size():
    dice_size = input('Enter the number of sides on the dice: ')
    while dice_size not in ['4','6','12']:
       print 'Sorry, please enter one of 4, 6 or 12:'
       dice_size = input('Enter the number of sides on the dice: ')
    return int(dice_size)

def main():
   dice_size = get_dice_size()
   repeat = True

   while repeat:
       print('Rolling the dice...')
       user_score = random.randint(1,dice_size)
       print("{0} sided dice thrown, score {1}".format(dice_size,user_score))
       roll_again = input("Do you want to roll the dice again? ")
       if roll_again.lower() == 'yes':
           dice_size = get_dice_size()
       else:
           repeat = False

   print('Thank you for playing!')

rawDescription: What is wrong with this Python code? Refuses input


code:
from math import *
from matplotlib import pyplot as plt
from scipy import optimize
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import pprint as pp

dataTupel=zip(xs,ys,zs) #your data from above

# Fitting a plane first
# let the affine plane be defined by two vectors,
# the zero point P0 and the plane normal n0
# a point p is member of the plane if (p-p0).n0 = 0

def distanceToPlane(p0,n0,p):
    return np.dot(np.array(n0),np.array(p)-np.array(p0))

def residualsPlane(parameters,dataPoint):
    px,py,pz,theta,phi = parameters
    nx,ny,nz =sin(theta)*cos(phi),sin(theta)*sin(phi),cos(theta)
    distances = [distanceToPlane([px,py,pz],[nx,ny,nz],[x,y,z]) for x,y,z in dataPoint]
    return distances

estimate = [1900, 700, 335,0,0] # px,py,pz and zeta, phi
#you may automize this by using the center of mass data
# note that the normal vector is given in polar coordinates
bestFitValues, ier = optimize.leastsq(residualsPlane, estimate, args=(dataTupel))
xF,yF,zF,tF,pF = bestFitValues

point  = [xF,yF,zF]
normal = [sin(tF)*cos(pF),sin(tF)*sin(pF),cos(tF)]

# Fitting a circle inside the plane
#creating two inplane vectors
sArr=np.cross(np.array([1,0,0]),np.array(normal))#assuming that normal not parallel x!
sArr=sArr/np.linalg.norm(sArr)
rArr=np.cross(sArr,np.array(normal))
rArr=rArr/np.linalg.norm(rArr)#should be normalized already, but anyhow

def residualsCircle(parameters,dataPoint):
    r,s,Ri = parameters
    planePointArr = s*sArr + r*rArr + np.array(point)
    distance = [ np.linalg.norm( planePointArr-np.array([x,y,z])) for x,y,z in dataPoint]
    res = [(Ri-dist) for dist in distance]
    return res

estimateCircle = [0, 0, 335] # px,py,pz and zeta, phi
bestCircleFitValues, ier = optimize.leastsq(residualsCircle, estimateCircle,args=(dataTupel))

rF,sF,RiF = bestCircleFitValues
print bestCircleFitValues

# Synthetic Data
centerPointArr=sF*sArr + rF*rArr + np.array(point)
synthetic=[list(centerPointArr+ RiF*cos(phi)*rArr+RiF*sin(phi)*sArr) for phi in np.linspace(0, 2*pi,50)]
[cxTupel,cyTupel,czTupel]=[ x for x in zip(*synthetic)]

### Plotting
d = -np.dot(np.array(point),np.array(normal))# dot product
# create x,y mesh
xx, yy = np.meshgrid(np.linspace(2000,2200,10), np.linspace(540,740,10))
# calculate corresponding z
# Note: does not work if normal vector is without z-component
z = (-normal[0]*xx - normal[1]*yy - d)/normal[2]

# plot the surface, data, and synthetic circle
fig = plt.figure()
ax = fig.add_subplot(211, projection='3d')
ax.scatter(xs, ys, zs, c='b', marker='o')
ax.plot_wireframe(xx,yy,z)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
bx = fig.add_subplot(212, projection='3d')
bx.scatter(xs, ys, zs, c='b', marker='o')
bx.scatter(cxTupel,cyTupel,czTupel, c='r', marker='o')
bx.set_xlabel('X Label')
bx.set_ylabel('Y Label')
bx.set_zlabel('Z Label')
plt.show()

rawDescription: python optimize.leastsq: fitting a circle to 3d set of points

description: Python optimize.leastsq: fitting a circle to 3d set of points


code:
sample = """ROUND(MULTIPLICATION(DIVISION(ROUND(SUM(MULTIPLICATION(50.00000,50.00000),MULTIPLICATION(50.00000,50.00000,2),MULTIPLICATION(50.00000,50.00000)),-2),300),DIFFERENCE(DIFFERENCE(41,7),0.3)),0)"""

from pyparsing import *

CALCULATION_TYPES = "ROUND MULTIPLICATION DIVISION SUM DIFFERENCE".split()

functionMap = {
    "ROUND"          : lambda args: round(args[0]),
    "MULTIPLICATION" : lambda args: args[0]*args[1],
    "DIVISION"       : lambda args: args[0]/args[1],
    "SUM"            : lambda args: args[0]+args[1],
    "DIFFERENCE"     : lambda args: args[0]-args[1],
    }

class FormulaParser():
    def __init__(self, formula=None):
        self.formula = formula

        # grammar
        # end result
        expr = Forward()
        formula = Forward()

        #calculation keywords
        calc_keyword = lambda name: Keyword(name)
        calculations = [calc_keyword(calc) for calc in CALCULATION_TYPES]
        calc_name = Group(reduce(lambda y, z: y | z, [x for x in calculations])).setResultsName('calculation_name')

        # a simpler way to create a MatchFirst of all your calculations
        # also, save the results names for when you assemble small elements into larger ones
        calc_name = MatchFirst(calculations)

        #symbols
        oparen, cparen, comma, dot, minus = map(Literal, '(),.-')
        #dnumber = Combine(Optional(minus) + Word(nums) + Optional(dot + Word(nums)))
        # IMPORTANT - convert numbers to floats at parse time with this parse action
        dnumber = Regex(r'-?\d+(\.\d+)?').setParseAction(lambda toks: float(toks[0]))

        #possible formulas
        #expr = Group(formula).setResultsName('formulas', listAllMatches=True) |
        #       Group(dnumber).setResultsName('dnumbers', listAllMatches=True)
        #exprs = expr + ZeroOrMore(comma + expr)

        #entire formula
        #formula << Combine(calc_name + Group(oparen + exprs + cparen).setResultsName('args'))
        #self.parsed_formula = formula

        # define what is allowed for a function arg
        arg_expr = dnumber | formula
        def eval_formula(tokens):
            fn = functionMap[tokens.calculation_name]
            return fn(tokens.args)

        # define overall formula, and add results names here
        formula <<= (calc_name("calculation_name") + oparen
                                        + Optional(delimitedList(arg_expr))('args')
                                        + cparen).setParseAction(eval_formula)
        self.parsed_formula = formula

    def get_parsed_formula(self):
        if self.formula:
            return self.parsed_formula.parseString(self.formula)

        return None

fp = FormulaParser(sample)
print fp.get_parsed_formula()

rawDescription: Refactor recursive calculator into an iterative one


code:
from datetime import datetime
from threading import Timer

x=datetime.today()
y=x.replace(day=x.day+1, hour=1, minute=0, second=0, microsecond=0)
delta_t=y-x

secs=delta_t.seconds+1

def hello_world():
    print "hello world"
    #...

t = Timer(secs, hello_world)
t.start()

rawDescription: Python script to do something at the same time every day


code:
from collections import Counter

class Visit:
    def __init__(self):
        self.date = ''
        self.diagnosis=''
        self.doctor=''
        self.price=0

visits = [Visit() for _ in xrange(10)]

# tuples = [(visit.doctor, visit.diagnosis) for visit in visits]
# counted_visits = Counter(tuples)
counted_visits = Counter((visit.doctor, visit.diagnosis) for visit in visits)
output = [({'Doctor' : doctor, 'Diagnosis' : diagnosis}, k) \
    for (doctor, diagnosis), k in counted_visits.items()]

print output

rawDescription: How to group and count objects by property in a list?

description: Group and count objects by property in a list


code:
import random
from random import randint
random.seed()

NUM_RECTS = 20
REGION = Rect(0, 0, 640, 480)

class Point(object):
    def __init__(self, x, y):
        self.x, self.y = x, y

    @staticmethod
    def from_point(other):
        return Point(other.x, other.y)

class Rect(object):
    def __init__(self, x1, y1, x2, y2):
        minx, maxx = (x1,x2) if x1 < x2 else (x2,x1)
        miny, maxy = (y1,y2) if y1 < y2 else (y2,y1)
        self.min, self.max = Point(minx, miny), Point(maxx, maxy)

    @staticmethod
    def from_points(p1, p2):
        return Rect(p1.x, p1.y, p2.x, p2.y)

    width  = property(lambda self: self.max.x - self.min.x)
    height = property(lambda self: self.max.y - self.min.y)

plus_or_minus = lambda v: v * [-1, 1][(randint(0, 100) % 2)]  # equal chance +/-1

def quadsect(rect, factor):
    """ Subdivide given rectangle into four non-overlapping rectangles.
        'factor' is an integer representing the proportion of the width or
        height the deviatation from the center of the rectangle allowed.
    """
    # pick a point in the interior of given rectangle
    w, h = rect.width, rect.height  # cache properties
    center = Point(rect.min.x + (w // 2), rect.min.y + (h // 2))
    delta_x = plus_or_minus(randint(0, w // factor))
    delta_y = plus_or_minus(randint(0, h // factor))
    interior = Point(center.x + delta_x, center.y + delta_y)

    # create rectangles from the interior point and the corners of the outer one
    return [Rect(interior.x, interior.y, rect.min.x, rect.min.y),
            Rect(interior.x, interior.y, rect.max.x, rect.min.y),
            Rect(interior.x, interior.y, rect.max.x, rect.max.y),
            Rect(interior.x, interior.y, rect.min.x, rect.max.y)]

def square_subregion(rect):
    """ Return a square rectangle centered within the given rectangle """
    w, h = rect.width, rect.height  # cache properties
    if w < h:
        offset = (h - w) // 2
        return Rect(rect.min.x, rect.min.y+offset,
                    rect.max.x, rect.min.y+offset+w)
    else:
        offset = (w - h) // 2
        return Rect(rect.min.x+offset, rect.min.y,
                    rect.min.x+offset+h, rect.max.y)

# call quadsect() until at least the number of rects wanted has been generated
rects = [REGION]   # seed output list
while len(rects) <= NUM_RECTS:
    rects = [subrect for rect in rects
                        for subrect in quadsect(rect, 3)]

random.shuffle(rects)  # mix them up
sample = random.sample(rects, NUM_RECTS)  # select the desired number
print '%d out of the %d rectangles selected' % (NUM_RECTS, len(rects))

#################################################
# extra credit - create an image file showing results

from PIL import Image, ImageDraw

def gray(v): return tuple(int(v*255) for _ in range(3))

BLACK, DARK_GRAY, GRAY = gray(0), gray(.25), gray(.5)
LIGHT_GRAY, WHITE = gray(.75), gray(1)
RED, GREEN, BLUE = (255, 0, 0), (0, 255, 0), (0, 0, 255)
CYAN, MAGENTA, YELLOW = (0, 255, 255), (255, 0, 255), (255, 255, 0)
BACKGR, SQUARE_COLOR, RECT_COLOR = (245, 245, 87), (255, 73, 73), (37, 182, 249)

imgx, imgy = REGION.max.x + 1, REGION.max.y + 1
image = Image.new("RGB", (imgx, imgy), BACKGR)  # create color image
draw = ImageDraw.Draw(image)

def draw_rect(rect, fill=None, outline=WHITE):
    draw.rectangle([(rect.min.x, rect.min.y), (rect.max.x, rect.max.y)],
                   fill=fill, outline=outline)

# first draw outlines of all the non-overlapping rectanges generated
for rect in rects:
    draw_rect(rect, outline=LIGHT_GRAY)

# then draw the random sample of them selected
for rect in sample:
    draw_rect(rect, fill=RECT_COLOR, outline=WHITE)

# and lastly convert those into squares and re-draw them in another color
for rect in sample:
    draw_rect(square_subregion(rect), fill=SQUARE_COLOR, outline=WHITE)

filename = 'square_quadsections.png'
image.save(filename, "PNG")
print repr(filename), 'output image saved'

rawDescription: How can I randomly place several non-colliding rects?

description: Randomly place several non-colliding rects


code:
for row in z:
    print row[1:12]
    for i in row[1:12]:
        k.write(i+'\t')
    k.write('\n')

rawDescription: Python: Help with new line character

description: Python: Help with new line character


code:
for link in links[:3]:
    print link

rawDescription: Loop and traversal in Python

description: Loop and traversal in Python


code:
import time
print time.strftime("%a, %d %b %Y %H:%M:%S")
import locale
locale.setlocale(locale.LC_TIME, "sv_SE") # swedish
print time.strftime("%a, %d %b %Y %H:%M:%S")

rawDescription: Locale date formatting in Python

description: Locale date formatting in Python


code:
from filebrowser import signals

def pre_upload_callback(sender, **kwargs):
    """
    Receiver function called before an upload starts.
    """
    print "Pre Upload Callback"
    print "kwargs:", kwargs
    # your code HERE
signals.filebrowser_pre_upload.connect(pre_upload_callback)

rawDescription: Django filebrowser - resize before upload


code:
import pandas as pd
import numpy as np

#parse data to dataframe df
df = pd.read_table('test/151101.mnd',
                   sep = '\s+',
                   header=None,
                   skiprows = np.arange(0 , 4608, 32),
                   names=['i','z','speed','dir','W','sigW','bck','error','tmp'],
                   usecols=['i','z','speed','dir','W','sigW','bck','error']
                    )

df = df[~df['W'].isin(['dir'])].reset_index(drop=True)
print df.head()

#parse date to df1
df1 = pd.read_table('test/151101.mnd',
                   sep = '\s+',
                   names=['d1','d2','d3'],
                   parse_dates={"Datetime" : [0, 1, 2]}
                     )
print df1.head()

df1 = df1[df1['Datetime'].str.contains(":")].reset_index(drop=True)
print df1.head()

rawDescription: How do I split repeated blocks of data into multiple columns and parse datetime?

description: Split repeated blocks of data into multiple columns and parse datetime


rawDescription: Odd artifacts when using Unicode escapes in Python 2.7


code:
import pandas as pd
import numpy as np

df = pd.DataFrame({'A': [1,1,1,1,1,0,0,1,1,0,0], 'B': [0,0,1,1,0,0,0,1,0,1,0]})
df1 = pd.DataFrame({'A': [1,1,1,1,1,0,0,1,1,0,0], 'B': [0,0,1,1,0,0,0,1,0,1,0]})

#cumulative sum of column B
df1['C'] = df1['B'].cumsum()
df1['lag_C'] = 1
#first 'group' with min value is problematic, copy to column D for latter use
df1.loc[df1['C'] == df1['C'].min() ,'D'] = df1['B']
#cumulative sums of groups to column C
df1['C']= df1.groupby(['C'])['lag_C'].cumsum()
#correct problematic states in column C, use value from D
if (df1['A'].loc[0] == 1):
    df1.loc[df1['D'].notnull() ,'C'] = df1['D']
if ((df1['A'].loc[0] == 1) & (df1['B'].loc[0] == 1)):
    df1.loc[df1['D'].notnull() ,'C'] = 0
del df1['D']
#shifted column lag_C from column C
df1['lag_C'] = df1['C'].shift(1)
print df1
#    A  B  C  lag_C
#0   1  0  0    NaN
#1   1  0  0      0
#2   1  1  1      0
#3   1  1  1      1
#4   1  0  2      1
#5   0  0  3      2
#6   0  0  4      3
#7   1  1  1      4
#8   1  0  2      1
#9   0  1  1      2
#10  0  0  2      1

rawDescription: retain function in python

description: Retain function in python


code:
if not c:
   print "The list is empty"
else:
   print "The list is not empty"

rawDescription: How do you check if list is blank?

description: Check if list is blank


code:
def print_spam():
    print 'spam'

print_spam
type(print_spam)
another_name = print_spam
another_name
another_name is print_spam
another_name()

rawDescription: Confused with function-object usage in Python


code:
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)
def keyPressed(self,*args):
    if args[0] == '\x08':
        self.keyCache = self.keyCache[:-1]
    elif args[0] == '\x1b':
        sys.exit()
    elif args[0] == 'm':
        print "Now meteoring otherwise peaceful teapot"
        # meteor shenanigans
    else:
        self.keyCache += args[0]
    sys.stdout.write(self.keyCache +"                                                  \r")#print "keypress: <",self.keyCache,">"
    sys.stdout.flush()

rawDescription: pyopengl glut input

description: Pyopengl glut input


code:
query = raw_input("enter binary number ")

try:
    is_bin = int(query,2)
    is_bin = True
    print "correct number"
except ValueError:
    is_bin = False
    print "not a binary number"

rawDescription: How do I limit an input in python so it will only allow 0 or 1's?

description: Limit an input in python so it will only allow 0 or 1's


code:
from ctypes import *

buf = create_unicode_buffer(260)
GetLongPathName = windll.kernel32.GetLongPathNameW
rv = GetLongPathName(path, buf, 260)
print buf.value

rawDescription: Is it possible to access the GetLongPathName() Win32 API in Python?


code:
from traits.api import Enum, HasTraits, TraitError

ETEST_VALUES = ['zero', 'one', 'two', 'three']

class Foo(HasTraits):
    eTest = Enum(*ETEST_VALUES)

f = Foo()
assert f.eTest == 'zero'
f.eTest = 'one'
f.eTest = ETEST_VALUES[3]

try:
    f.eTest = 'four'
except TraitError:
    print 'Oops! Bad value!'

rawDescription: Traits Enum values

description: Traits Enum values


code:
# Measure some strings:
words = ['cat', 'window', 'defenestrate']
for w in words:
    print w, len(w)

rawDescription: How would I complete number 2 on the following check list and/or just finish the program?


code:
from pymel.core import *

select("MODELS*")

## Gather our world bounding box and store it in a variable called b
b = general.exactWorldBoundingBox()

## b now contains min and max XYZ world coords
## Name our temporary locators

locName = "tempLoc"
## Create a locator at each min and max point to form a fake bounding box

positions = [[0,1,2], [0,4,2], [0,4,5], [3,4,5], [3,1,5], [3,4,2], [3,1,2], [0,1,5]]

## Create the locators
for position in positions:
    print position
    spaceLocator(p=(b[position[0]],b[position[1]],b[position[2]]), name=locName)

## Once we create the locators, frame locators, delete

tempLocators = select("tempLoc*", r=1)
runtime.FrameSelectedInAllViews()
delete()

rawDescription: Frame bounding boxes within resolution gate


code:
s="""ESC
Here can be something.
Defn: Here is the definition.

Here can also be some text.

TAB
Here can be something.
Defn: Here is the definition.
There can be some more lines.

Here can also be some text."""

if __name__ == '__main__':
    # search =  sys.argv[1]
    search = 'TAB' #Sample search
    index = s.find(search) # Find the index of search term
    def_index = s.find('Defn', index) # Find the index of 'Defn'
    end_index =  s.find('\n\n', def_index) # Find the end of definition
    print index , def_index, end_index
    print s[def_index: end_index][5:]  # 5 is len('Defn:')

rawDescription: Find text after some occurrence

description: Find text after some occurrence


code:
import json
with open("config.json", "r") as e:
    myconfig = json.load(e)
print myconfig
config_string = json.dumps(myconfig)
config_string
json.loads(config_string)
with open("config.json", "w") as e:
   json.dump(myconfig, e)

rawDescription: How to store simple text data in the python program itself?

description: Store simple text data in the python program itself


code:
#!/usr/bin/python
import wx

class BoExplorerPanel(wx.Frame):
  def __init__(self):
     wx.Frame.__init__(self, None)

     self.initComponents() # initialize Window components

  def initComponents(self):
     print "Inizializzo i controlli"
     # controls
     resizeBox = wx.BoxSizer(wx.VERTICAL)

     self.dirBrowser = wx.GenericDirCtrl(self, wx.ID_ANY, style = wx.DIRCTRL_DIR_ONLY)

     resizeBox.Add(self.dirBrowser, 1, wx.EXPAND | wx.ALL)

     self.SetSizerAndFit(resizeBox)

     # events
     tree = self.dirBrowser.GetTreeCtrl()
     self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.dirBrowser_OnItemSelected, tree)
     self.Bind(wx.EVT_TREE_ITEM_RIGHT_CLICK, self.dirBrowser_OnRightClick, tree)
     self.Bind(wx.EVT_TREE_SEL_CHANGED, self.dirBrowser_OnSelectionChanged, tree)

     # panel's properties

  def dirBrowser_OnItemSelected(self, event):
     print "CLicked"
     event.Skip()

  def dirBrowser_OnRightClick(self, event):
     print "Right Click"
     event.Skip()

  def dirBrowser_OnSelectionChanged(self, event):
     print "Selection Changed"
     event.Skip()

app = wx.App(False)
f = BoExplorerPanel()
f.Show()
app.MainLoop

rawDescription: wx.GenericDirCtrl Event's handling

description: Wx.GenericDirCtrl Event's handling


code:
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

url = 'http://www.neighborhoodscout.com/'
username = 'abc@bcas.fulba.com'#change here
password = '12345'#change here
driver = webdriver.PhantomJS(r"C:\phantomjs.exe")
driver.set_window_size(1124, 850)
driver.get(url)
time.sleep(2)
login = driver.find_element_by_xpath("//a[@ id='login']")
login.click()
time.sleep(5)
usr = driver.find_element_by_xpath("//input [@id='user_email' and @class='input-text']")
passw = driver.find_element_by_xpath("//input [@id='user_password' and @class='input-text' ]")
logbtn = driver.find_element_by_xpath("//input [@id='login-button' and  @class='btn']")
usr.send_keys(username)
passw.send_keys(password)
logbtn.click()

time.sleep(4)

if '/users/sign_out' in driver.page_source:
    print "Logged In"
driver.close()

rawDescription: Firefox -> PhantomJs Login to website not working


code:
import copy

# Convenience to work with both normal and mutable numbers
def _get_value(obj):
    try:
        return obj.value[0]
    except:
        return obj

class mutable_number(object):
    def __init__(self, value):
        # Mutable storage because `list` defines a location
        self.value = [value]

    # Define the comparison interface
    def __eq__(self, other):
        return _get_value(self) == _get_value(other)

    def __ne__(self, other):
        return _get_value(self) != _get_value(other)

    # Define the numerical operator interface, returning new instances
    # of mutable_number
    def __add__(self, other):
        return mutable_number(self.value[0] + _get_value(other))

    def __mul__(self, other):
        return mutable_number(self.value[0] * _get_value(other))

    # In-place operations alter the shared location
    def __iadd__(self, other):
        self.value[0] += _get_value(other)
        return self

    def __imul__(self, other):
        self.value[0] *= _get_value(other)
        return self

    # Define the copy interface
    def __copy__(self):
        new = mutable_number(0)
        new.value = self.value
        return new

    def __repr__(self):
        return repr(self.value[0])

x = mutable_number(1)
y = copy.copy(x)
y *= 5
print x

list_1 = [mutable_number(i) for i in [1, 2, 3, 4]]
list_2 = copy.copy(list_1)
list_1[0] *= 100
print list_1
print list_2

rawDescription: Is it possible to make wrapper object for numbers, e.g. float, to make it mutable?


code:
MoveTower(n)
print str(2**n-1), 'moves total'

rawDescription: how to eradicate 'None' from the end of a recursion

description: Eradicate 'None' from the end of a recursion


code:
def log_and_execute(cursor, sql, *args):
    s = sql
    if len(args) > 0:
        # generates SELECT quote(?), quote(?), ...
        cursor.execute("SELECT " + ", ".join(["quote(?)" for i in args]), args)
        quoted_values = cursor.fetchone()
        for quoted_value in quoted_values:
            s = s.replace('?', quoted_value, 1)
    print "SQL command: " + s
    cursor.execute(sql, args)

rawDescription: Python SQLite how to get SQL string statement being executed


code:
import ast

s = 'subtract(4,add(4,times(3,4)))'

# Probably better to use functions from the operator module here :-)
functions = {'subtract': lambda a,b: a-b,
             'add': lambda a, b: a+b,
             'times': lambda a,b: a*b}

def _evaluate(node):
    if isinstance(node, ast.Num):
        return node.n
    elif isinstance(node, ast.Name):
        return functions[node.id]
    elif isinstance(node, ast.Call):
        function = _evaluate(node.func)
        return function(*[_evaluate(n) for n in node.args])
    else:
        raise ValueError('Unknown node type: %s', type(node))

def evaluate(s):
    tree = ast.parse(s)
    node = tree.body[0].value
    return _evaluate(node)

print evaluate(s)

rawDescription: arithmetic expression reader in python

description: Arithmetic expression reader in python


code:
str = hex(5L)
long(str,16)
int(long(str,16))

rawDescription: long hex string to integer in python

description: Long hex string to integer in python


code:
from Queue import Queue
from threading import Thread

class Scheduler():
    def __init__(self):
        self.id=10

    def add(self,q,id):
        self.id+=id
        q.put('added %d' % self.id)

    def mul(self,q,id):
        self.id*=id
        q.put('multiplied : %d' % self.id)

if __name__=='__main__':
    sch1 = Scheduler()
    sch2 = Scheduler()
    q = Queue()
    t1 = Thread(target=sch1.add, args=(q,id,))
    t1.start()
    t2 = Thread(target=sch2.mul, args=(q,id,))
    t2.start()
    print q.get()
    print q.get()

rawDescription: Understanding how to use Multiple threads calling different functions with Queue in python?


code:
with open(SourceFile, 'r') as file:
    data = file.readlines()

print "Your data at line 1" + data[0]
data[1] = "New line to insert instead of the 2nd line\n"

with open(SourceFile, 'w') as file:
    file.writelines( data )

rawDescription: Python: easiest and bulletproof way to read and write a small file


code:
import fileinput

processing_foo1s = False

for line in fileinput.input('1.txt', inplace=1):
  if line.startswith('foo1'):
    processing_foo1s = True
  else:
    if processing_foo1s:
      print 'foo bar'
    processing_foo1s = False
  print line,

rawDescription: Inserting Line at Specified Position of a Text File


code:
from django.db import connection

cursor = connection.cursor()
cursor.execute(" SELECT WEEK(`time`) AS 'week', COUNT(*) AS 'count' FROM %s GROUP BY WEEK(`time`) ORDER BY WEEK(`time`)" % EventOccurrence._meta.db_table, [])

data = []
results = cursor.fetchall()
for i, row in enumerate(results[:-1]):
    data.append(row)

    week = row[0] + 1
    next_week = results[i+1][0]
    while week < next_week:
        data.append( (week, 0) )
        week += 1
data.append( results[-1] )

print data

rawDescription: Grouping by week, and padding out 'missing' weeks


code:
with open('file.text', 'r') as fp:
  for line in fp:
    match = re.search('string=([^,]+)', line)
    if match:
      print match.group(1)

rawDescription: read a file and search for a string, if matches, return the next word in python

description: Read a file and search for a string, if matches, return the next word in python


code:
import lxml.html

tree = lxml.html.fromstring(source)
print tree.xpath('//input[@name="auth"]/@value')[0]

rawDescription: python lxml printing empty list

description: Python lxml printing empty list


code:
import sys
myint = 12
print sys.getsizeof(myint)

rawDescription: How to find number of bytes taken by python variable

description: Find number of bytes taken by python variable


code:
import csv
with open('eggs.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in spamreader:
        print ', '.join(row)

rawDescription: Reading rows of CSV file into string in Python


code:
x="Ae Fond Kiss... (also known as ''Just a Kiss'') is a 2004 romantic [[drama film|film]]\ndirected by [[Ken Loach]], and starring [[Atta Yaqub]] and [[Eva Birthistle]]. The \ntitle is taken from a [[Scotland|Scottish]] song by [[Robert Burns]], the complete\nline  being ''[[Ae Fond Kiss|Ae fond kiss]], and then we sever...'"
print re.sub("([^\[\]]*\|)|(\[\[)|(\]\])","",x)
x="Hero Hitler In Love is a 2011 Punjabi [[romantic]] and [[action film|action]] film directed by [[Sukhjant Dadda]] with story, screenplay & dialogues by [[Babbu Maan]]. Hero Hitler is produced by B S Maan (Khantmanpur) and [[Darshan Singh Grewal]], and stars [[Babbu Maan]], [[Mouni Roy]] and [[Bhagwant Mann]]. ''Hero Hitler In Love'' was released on 18 November in mostly all Punjab theaters. It released to mixed reviews but did have a good opening at the box office."

rawDescription: want to parse paragraph using regex

description: Want to parse paragraph using regex


rawDescription: How to remove certain lists from a list of lists using python?

description: Remove certain lists from a list of lists using python


code:
l = [35,53,[525,6743],64,63,[743,754,757]]

def flatten(xs):
    result = []
    if isinstance(xs, (list, tuple)):
        for x in xs:
            result.extend(flatten(x))
    else:
        result.append(xs)
    return result

print flatten(l)

rawDescription: How to flatten a hetrogenous list of list into a single list in python?

description: Flatten a hetrogenous list of list into a single list in python


code:
s = time.strftime('%Y-%m-%d-%H-%M-%S')
print s

rawDescription: Save date time in filename with valid chars


code:
x = []          # A new list object is bound to x
y = x           # The reference bound to x is bound to y as well
x.append(None)  # None is *appended* to the list object bound to `x`
print y         # y is bound to the same list
x = 5           # x is *rebound* to a different object
print y         # y remains bound to the list

rawDescription: Clarification of python namespaces, specifically between lists and ints


code:
s = "evening"
ans = len([x for x in xrange(len(s)-2) if s[x] == s[x+2]])
print ans

rawDescription: How to make a function that counts how many times each element is equal to 2 elements to its right

description: Make a function that counts how many times each element is equal to 2 elements to its right


code:
import random

x = int(raw_input("Enter desired number: "))

def randoTaker():
    return random.randint(0, 101)

for j in sorted([randoTaker() for i in range(x)]):
    print j

rawDescription: How do I sort the results of this Python program from lowest to highest values?

description: Sort the results of this Python program from lowest to highest values


code:
import numpy as np
a = np.array([1,2,3,4,5])
p = np.percentile(a, 50) # return 50th percentile, e.g median.
print p
3.0

rawDescription: How do I calculate percentiles with python/numpy?

description: Calculate percentiles with python/numpy


code:
import csv

with open('csv_data.csv') as csv_data:
    reader = csv.reader(csv_data)

    # eliminate blank rows if they exist
    rows = [row for row in reader if row]
    headings = rows[0] # get headings

    person_info = {}
    for row in rows[1:]:
        # append the dataitem to the end of the dictionary entry
        # set the default value of [] if this key has not been seen
        for col_header, data_column in zip(headings, row):
            person_info.setdefault(col_header, []).append(data_column)

    print person_info

rawDescription: Convert .csv table to dictionary

description: Convert .csv table to dictionary


code:
import re, sys, md5

def p(s, pos, callBack):
  while pos < len(s):
    m = re.match(r'(d+)[', s[pos:])
    if m:  # repetition?
      number = m.group(1)
      for i in range(int(number)):
        endPos = p(s, pos+len(number)+1, callBack)
      pos = endPos
    elif s[pos] == ']':
      return pos + 1
    else:
      callBack(s[pos])
      pos += 1
  return pos + 1

digest = md5.new()
def feed(s):
  digest.update(s)
  sys.stdout.write(s)
  sys.stdout.flush()

end = p(sys.argv[1], 0, feed)
print
print "MD5:", digest.hexdigest()
print "finished parsing input at pos", end

rawDescription: Hashing same character multiple times

description: Hashing same character multiple times


code:
class Client(object):
    def __init__(self, ident):
        self.ident = ident

    def __repr__(self):
        return '<{0}: {1}>'.format(self.__class__.__name__, self.ident)

def serve(q1, q2, mutex):
    while True:
        client = q1.get()
        print '%s leaved waiting queue for serving' % client
        time.sleep(2)  # Do something with client
        q1.task_done()

        with mutex:
            if not q2.empty() and not q1.full():
                client = q2.get()
                print '%s leaved outside queue' % client
                q1.put(client)
                print '%s is in the waiting queue' % client
                q2.task_done()

def main():
    waiting_queue = Queue(10)
    outside_queue = Queue(20)

    lock = threading.RLock()

    for _ in range(2):
        worker = threading.Thread(target=serve, args=(waiting_queue, outside_queue, lock))
        worker.setDaemon(True)
        worker.start()

    # Every 1-5 seconds 10 clients enter to the waiting room
    i = 1  # Used for unique <int> client's id
    while True:
        delay = randrange(1, 5)
        time.sleep(delay)
        for _ in range(10):
            client = Client(i)
            try:
                lock.acquire()
                if not waiting_queue.full():
                    waiting_queue.put(client)
                else:
                    outside_queue.put_nowait(client)
            except FullQueue:
                # print 'Outside queue is full. Please go out.'
                pass
            finally:
                lock.release()

            i += 1

    waiting_queue.join()
    outside_queue.join()
    print 'Done'

rawDescription: Cannot to line up item from one queue to another


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

fig = plt.figure()
ax = fig.add_subplot(111)

max_height = 100
n_pts = 100
y1 = [0, max_height]
x1 = [0, 0]
y = numpy.random.randn(n_pts) * max_height
x = numpy.arange(0, n_pts)

# draw the data
line1, = ax.plot(x, y, color='black')

# fix the limits of the plot
ax.set_ylim(0, max_height)
ax.set_xlim(0, n_pts)

# draw the plot so that we can capture the background and then use blitting
plt.show(block=False)

# get the canvas object
canvas = ax.figure.canvas
background = canvas.copy_from_bbox(ax.bbox)

# add the progress line.
# XXX consider using axvline
line, = ax.plot(x1, y1, color='r', animated=True)

starttime=time.time()
mytimer=0
mytimer_ref=0

def update(canvas, line, ax):
    # revert the canvas to the state before any progress line was drawn
    canvas.restore_region(background)

    # compute the distance that the progress line has made (based on running time)
    t = time.time() - starttime
    mytimer = t + mytimer_ref
    x1 = [mytimer,mytimer]
    # update the progress line with its new position
    line.set_xdata(x1)
    # draw the line, and blit the axes
    ax.draw_artist(line)
    canvas.blit(ax.bbox)

def onclick(event):
    global starttime
    starttime=time.time()
    global mytimer_ref
    mytimer_ref=event.xdata
    print "starttime",starttime

cid=line1.figure.canvas.mpl_connect('button_press_event',onclick) # when i click the mouse over a point, line goes to that point and start moving from there.
timer=fig.canvas.new_timer(interval=100)
args=[canvas,line,ax]
timer.add_callback(update,*args) # every 100ms it calls update function
timer.start()
plt.show()

rawDescription: Progress Line in Matplotlib Graphs

description: Progress Line in Matplotlib Graphs


code:
import bisect
def f(l1,l2):
    for x in l2:
        ind = bisect.bisect(l1,x)
        # if the index where the item from l2 can fit in l1 is 1,
        # then it's time to return
        if ind - 1 == 0:
            return x
        del l1[ind-1]   #otherwise remove the item from l1

l1 = [0,30,45,55,80,90]
l2 = [35,65,70,75,100,120]
print f(l1,l2)
#75

rawDescription: list manipulation with minimal iteration

description: List manipulation with minimal iteration


code:
for part in email.iterators.typed_subpart_iterator(msg, 'text', 'plain'):
   for body_line in email.iterators.body_line_iterator(part):
       print body_line

rawDescription: Parse email and get number from body

description: Parse email and get number from body


code:
import sys
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebView

class Browser(QWebView):

    def __init__(self):
        QWebView.__init__(self)
        self.loadFinished.connect(self._result_available)

    def _result_available(self, ok):
        frame = self.page().mainFrame()
        print unicode(frame.toHtml()).encode('utf-8')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    view = Browser()
    view.load(QUrl('http://www.google.com'))
    app.exec_()

rawDescription: How to display html using QWebView. Python

description: Display html using QWebView. Python


rawDescription: Transpose nested lists in python

description: Transpose nested lists in python


code:
import numpy,csv

time=[]
data_a=[]
data_b=[]

#--------------------Read in a--------------------
with open('data_a.csv','rb') as csvfile:
    reader=csv.reader(csvfile)
    for row in reader:
        time.append(row[0])
        data_a.append(float(row[1]))

#--------------------Read in b--------------------
with open('data_b.csv','rb') as csvfile:
    reader=csv.reader(csvfile)
    for row in reader:
        data_b.append(float(row[1]))

data_a=numpy.array(data_a)
data_b=numpy.array(data_b)
monthly=numpy.zeros(data_a.shape)

#-----------------Get month means-----------------
for ii in xrange(len(time)):
    tt=time[ii]
    if ii==0:
        month_old=tt[4:6]
        index_start=ii
    else:
        #----------------new month----------------
        month=tt[4:6]
        if month != month_old:
            month_mean=numpy.mean(data_a[index_start:ii])
            print 'mean for month',month_old,month_mean
            monthly[index_start:ii]=month_mean
            month_old=month
            index_start=ii

    #----------------Get the last month----------------
    if ii==len(time)-1:
        month_mean=numpy.mean(data_a[index_start:])
        print 'mean for month',month_old,month_mean
        monthly[index_start:]=month_mean

#-------------------Filter data-------------------
index=numpy.where(monthly>=0.01)
data_a_filtered=numpy.take(data_a,index)
data_b_filtered=numpy.take(data_b,index)
time_filtered=numpy.take(time,index)

rawDescription: Extract monthly data from a list with mean greater than a threshold using Python


rawDescription: how to enter the data inside any loop in python

description: Enter the data inside any loop in python


code:
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.events import subscriber, ContextFound
from pyramid.view import view_config

class RouteName(object):
    '''
    Subscriber predicate to check against route name.
    '''

    def __init__(self, value, config):
        self.value = value
        self.config = config

    def text(self):
        return 'route_name = %s' % self.value

    phash = text

    def __call__(self, event):
        route_name = getattr(event.request.matched_route, 'name', None)
        return route_name == self.value

@subscriber(ContextFound, route_name='home')
def check(event):
    print 'checked'

@view_config(renderer='string', route_name='home')
def home(request):
    return 'home'

@view_config(renderer='string', route_name='another')
def another(request):
    return 'another'

if __name__ == '__main__':
    config = Configurator()
    config.add_subscriber_predicate('route_name', RouteName)
    config.add_route('home', '/')
    config.add_route('another', '/another')
    config.scan()
    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 8080, app)
    server.serve_forever()

rawDescription: Pyramid subscriber predicates not working


code:
f=open(filename,"r")
listOfLines=f.readlines()
for line in listOfLines:
    splitLine=line.split(",")
    print "Day of Month: "+splitLine[4]#this is an example of one piece of info.
    print "Month: "+splitLine[5]#this is an example of one piece of info.

rawDescription: How to turn a comma-delimited list of date attributes into a MySQL date value?

description: Turn a comma-delimited list of date attributes into a MySQL date value


rawDescription: how to xor binary with python

description: Xor binary with python


code:
def value():
    hand_total = 0
    # Add in a dict of "rank values" (excluding the Ace)
    rank_values = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8,
                   '9':9, '10':10, 'J':10, 'Q':10, 'K':10}
    # No need for the while loop
    for i in xrange(len(player1_hand)):
        card = player1_hand[i][0] # the added index will get the card
        if card == 'A':          # without the suit.
            print "Please choose between 1 and 11."
            value_a = input("---> ")
            hand_total += int(value_a) # add a type check before this
        else:
            hand_total += rank_values[card]
    return hand_total

rawDescription: Adding components of a list together for BlackJack


code:
#remove the old way of connecting
#self.connect(self.treeWidget,SIGNAL("itemSelectionChanged()"), self.loadAllMessages)
self.treeWidget.itemSelectionChanged.connect(self.loadAllMessages)
def loadAllMessages(self, folder):
    getSelected = self.treeWidget.selectedItems()
    if getSelected:
        baseNode = getSelected[0]
        getChildNode = baseNode.text(0)
        print getChildNode

rawDescription: PyQt: get the current QTreeWidget item

description: PyQt: get the current QTreeWidget item


code:
import sys

print 'starting'
sys.stdout.flush()

rawDescription: Python multithreaded print statements delayed until all threads complete execution


code:
from itertools import combinations
data = dict(
  list1 = set(list("alphabet")),
  list2 = set(list("fiddlesticks")),
  list3 = set(list("geography")),
  list4 = set(list("bovinespongiformencephalopathy")),
)
variations = {}
for i in range(len(data)):
  for v in combinations(data.keys(),i+1):
    vsets = [ data[x] for x in v ]
    variations[tuple(sorted(v))] = reduce(lambda x,y: x.intersection(y), vsets)
for k,v in sorted(variations.items(),key=lambda x: (len(x[0]),x[0])):
  print "%r\n\t%r" % (k,v)

rawDescription: Venn Diagram up to 4 lists - outputting the intersections and unique sets


code:
import zipfile
with zipfile.ZipFile("zipfile.zip", "r") as f:
    for name in f.namelist():
        data = f.read(name)
        print name, len(data), repr(data[:10])

rawDescription: How to process zip file with Python

description: Process zip file with Python


code:
for i in xrange(0,2):
    playerlist , teamlist = get_things(i) # missing this
    print teamlist    #This is where Im told teamlist doesn't exist
    print playerlist   #This is where I'm told playerlist doesn't exist
    print_sheet(teamlist, playerlist)

rawDescription: Using a list made in a function outside of that function, python


code:
def copyFilesFromServer(server, user, password, remotedir, localdir, filenameRegex = '*', autoTrust=True):
    # Setup ssh connection for checking directory
    sshClient = paramiko.SSHClient()
    if autoTrust:
        sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #No trust issues! (yes this could potentially be abused by someone malicious with access to the internal network)
    sshClient.connect(server,user,password)
    # Setup sftp connection for copying files
    t = paramiko.Transport((server, 22))
    t.connect(user, password)
    sftpClient = paramiko.SFTPClient.from_transport(t)
    fileList = executeCommand(sshclient,'cd {0}; ls | grep {1}'.format(remotedir, filenameRegex)).split('\n')
    #TODO: filter out empties!
    for filename in fileList:
        try:
            sftpClient.get(posixpath.join(remotedir, filename), os.path.join(localdir, filename), callback=None) #callback for showing number of bytes transferred so far
        except IOError as e:
            print 'Failed to download file <{0}> from <{1}> to <{2}>'.format(filename, remotedir, localdir)

rawDescription: Python - FileNotFoundError when dealing with DMZ


code:
p = Bool('p')
x = Real('x')
s = Solver()
s.add(Or(x < 5, x > 10), Or(p, x**2 == 2), Not(p))
s.check()
m = s.model()
print m[p], m[x]
print "is_true(m[p]):", is_true(m[p])
print "is_false(m[p]):", is_false(m[p])
print "is_int_value(m[x]):", is_int_value(m[x])
print "is_rational_value(m[x]):", is_rational_value(m[x])
print "is_algebraic_value(m[x]):", is_algebraic_value(m[x])
r = m[x].approx(20) # r is an approximation of m[x] with precision 1/10^20
print "is_rational_value(r):", is_rational_value(r)
print r.numerator_as_long()
print r.denominator_as_long()
print float(r.numerator_as_long())/float(r.denominator_as_long())

rawDescription: Z3/Python getting python values from model


code:
import json

for myObj in A:
   if myObj['id'] == 102:
      myVal = [myObj]
test = json.dumps(myVal)
B = json.loads(test)
print B

rawDescription: Handling dictionary object in python

description: Handling dictionary object in python


code:
from iniparse import INIConfig

cfg = INIConfig(open('options.ini'))
print cfg.section.option
cfg.section.option = 'new option'

del cfg.section.option
del cfg.section

f = open('options.ini', 'w')
print >>f, cfg
f.close()

rawDescription: Delete Sections and Keys from INI file Using Python Module iniparse?


code:
import facebook
import requests

ACCESS_TOKEN = "my_token"
graph = facebook.GraphAPI(ACCESS_TOKEN)
friends = graph.get_connections("me","friends")

allfriends = []

# Wrap this block in a while loop so we can keep paginating requests until
# finished.
while(True):
    try:
        for friend in friends['data']:
            allfriends.append(friend['name'].encode('utf-8'))
        # Attempt to make a request to the next page of data, if it exists.
        friends=requests.get(friends['paging']['next']).json()
    except KeyError:
        # When there are no more pages (['paging']['next']), break from the
        # loop and end the script.
        break
print allfriends

rawDescription: Python Facebook API - cursor pagination

description: Python Facebook API - cursor pagination


code:
import json
from collections import defaultdict

fh = open("yourfile.csv")
result = defaultdict(dict) #dictionary holding the data
current_key = "" #current category
ignore_next = False #flag to skip header

for line in fh:
    line = line.strip() #throw away newline
    if line == "": #line is empty
        current_key = ""
        continue
    if current_key == "": #current_key is empty
        current_key = line #so the current line is the header for the following data
        ignore_next = True
        continue
    if ignore_next: #we're in a line that can be ignored
        ignore_next = False
        continue
    (a,b) = line.split(",")
    result[current_key][a] = b
fh.close()

#pretty-print data
print json.dumps(result, sort_keys=True, indent=4)

rawDescription: How to convert specific CSV format to JSON using Python

description: Convert specific CSV format to JSON using Python


code:
import time
from datetime import datetime

t = datetime.now()
t1 = t.timetuple()

print time.mktime(t1)

rawDescription: convert date to number python

description: Convert date to number python


code:
from PyQt4 import QtGui
import module
import sys

class ApplicationWindow(QtGui.QMainWindow):
    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self, parent)
        self.statusBar().showMessage("Main window initalized", 6000)
        module.Test(self)

def main():
    app = QtGui.QApplication(sys.argv)
    form = ApplicationWindow()
    form.show()
    app.exec_()

if __name__ == "__main__":
    main()

# module.py

class Test(object):
    def __init__(self, parent):
        print 'hello'
        parent.statusBar().showMessage("Test module called", 6000)

rawDescription: PyQt Confusion with Inheritance

description: PyQt Confusion with Inheritance


code:
class Foo( object ):
    def foo( self ):
    	print 'FOO!'

class Bar( Foo ):
    def foo( self ):
    	raise AttributeError( "'Bar' object has no attribute 'foo'" )

b = Bar()
b.foo()

rawDescription: Python inheritance - how to disable a function


code:
astr = '''var PRO_META_JSON = {
    "attributeDefinition":{
        "defaultSku":305557121,
        "attributeListing":[{
            "label":"Finish",
                    "defaultIndex":0,
                    "options":[
                        "White::f33b4086",
                        "Beige::8e0900fa",
                        "Blue::3c3a4707",
                        "Orange::1d8cb503",
                        "Spring Green::dd5e599a"
                     ]
            }],
            "attributeLookup":[
            [0,305557121],
            [1,305557187],
            [2,305557696],
            [3,305557344],
            [4,305696435]
            ]
        }
    };'''

import re
import json
pat = re.compile('^[^\{]*(\{.*\});.*$', re.MULTILINE|re.DOTALL)
json_str = pat.match(astr).group(1)
d = json.loads(json_str)

for x in d['attributeDefinition']['attributeLookup']:
    print x[1]
# 305557121
# 305557187
# 305557696
# 305557344
# 305696435

rawDescription: How can I use regex in Python to return these numbers?

description: Use regex in Python to return these numbers


code:
from random import randint

foo = ['m1', 'm2', 'm3', 'm4', 'm5','m6','m7','m8']
while foo:
    x = randint(0, len(foo) - 1)
    y = randint(0, len(foo) - 1)
    while x == y and len(foo) > 1:
        y = randint(0, len(foo) - 1)
    if randint(0, 1) == 0 or len(foo) <= 1:
        print foo[x]
    else:
        print foo[y], foo[x]
    foo = foo[:x] + foo[x+1:]

rawDescription: Random Output from a list - Python

description: Random Output from a list - Python


rawDescription: Last decimal digit precision changes in different call of same generator function [python]


code:
class Test:
    i = 3
    y = 3 * i
    def testF(self):
        print Test.y

v = Test()
v.testF()
# >> 9

rawDescription: Is there static block in class in python


code:
import re
print re.sub(r'^[a-zA-Z]:\\.+(\\Data.+)', r'D:\\folder\1', r'C:\Some\Path\Data\File.txt')

rawDescription: Raw string in Python regular expression using Windows folder path


code:
#!/usr/bin/python
import urllib, urllib2, cookielib, re

login_url = "https://minecraft.net/login"
cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
respon = opener.open(login_url).read()

authToken = re.search('name="authenticityToken"[\s]+value="(.+)"', respon).group(1)

data_dict = {
    "username": "USERNAME",
    "password": "PASSWORD",
    "remember": "true",
    "authenticityToken": authToken
}

login = opener.open(login_url, urllib.urlencode(data_dict))
logged_in = login.read()

if login_url in logged_in:
    print 'Login failed'
else:
    print 'Login OK'

rawDescription: Python urllib2 login to minecraft.net

description: Python urllib2 login to minecraft.net


code:
#!/usr/bin/python
# EASY-INSTALL-SCRIPT: 'docutils==0.5','rst2html.py'
"""
A minimal front end to the Docutils Publisher, producing HTML.
"""

try:
    from ulif.rest import directives_plain
    from ulif.rest import roles_plain
    from ulif.rest import pygments_directive

    import locale
    locale.setlocale(locale.LC_ALL, '')
except:
  pass

from docutils.core import publish_doctree, publish_from_doctree
from smartypants import smartyPants
import sys

description = ('Personal docutils parser with extra features.')

doctree = publish_doctree(file(sys.argv[1]).read())
result = publish_from_doctree(doctree, writer_name='html')
result = smartyPants(result)
print result

rawDescription: Can I add "Smartypants" to restructuredText?

description: Add "Smartypants" to restructuredText


code:
ut = 1460505599.0; frac = 0.720321;
ufloat = ut + frac
print str(ufloat)
print repr(ufloat)

rawDescription: Preventing rounding to 2 decimal places

description: Preventing rounding to 2 decimal places


code:
from __future__ import division

print round(27/10)
3.0
print round(21/10)
2.0

rawDescription: Rounding Up a Number in Python

description: Rounding Up a Number in Python


code:
o = urlparse.urlparse('sips:+msisdn@domain?Accept-Contact=+sip.instance="<urn:uuid:2e7671fd-31dd-1393-dkd6-835fe09f314f>";require;explicit')
print o

rawDescription: Parsing telephone number from a SIP URI in Python


code:
class Counter(object): # new style object definition.

    def __init__( self, num ):
        self.value = num
        # this is 32bit int max as well, same as pow(2,32) function.
        self.maximum = 0xFFFFFFFF
    def increase(self):
        self.value += 1

        if self.value > self.maximum:
            self.value -= self.maximum

    def __repr__( self ): # representation function.
        return str(self.value)

    def __str__( self ): # string function
        return str(self.value)

counter = Counter(10)
print counter
counter.increase()
print counter
counter.increase()
print counter

rawDescription: inheritance from int and increase in python

description: Inheritance from int and increase in python


code:
def clues_open():
    try:
        cluesfile = open("clues.txt","r")
        clue_list = cluesfile.readlines()
        #print clue_list   #either print the list here
        return clue_list   # or return the list
    except:
        print("Oops! Something went wrong (Error Code 3)")
        exit()
def display_clues():
    clues_yes_or_no = raw_input("Would you like to see the clues? Enter Y/N: ")
    clues_yes_or_no = clues_yes_or_no.lower()
    if clues_yes_or_no == "y":
        clue_list = clues_open()  # catch list here
        print clue_list

display_clues()

rawDescription: Using .readlines() and struggling to access the list


code:
d = {'foo' : 1, 'bar' : 2}
fname = 'keys.txt'

with open(fname, 'w') as f:
    f.write('\n'.join(d.keys()))
    f.write('\n') # newline at the end of file

print "Keys saved to {}.".format(fname)

rawDescription: How to write dictionary keys to a new text file in python?

description: Write dictionary keys to a new text file in python


code:
def print_table():
    headers = ['One', 'Two', 'Three']
    table = [['test1', 2, 3.0],
            ['test4', 5, 6.0],
            ['test7', 8, 9.0],
            ]
    print ''.join(column.rjust(10) for column in headers)
    for row in table:
        print ''.join(str(column).rjust(10) for column in row)

print_table()

rawDescription: efficient way of printing lists to a table in a .txt file using python

description: Efficient way of printing lists to a table in a .txt file using python


code:
addressbook_xml = """<?xml version="1.0"?>
<addressbook>
    <person>
        <name>Eric Idle</name>
        <phone type='fix'>999-999-999</phone>
        <phone type='mobile'>555-555-555</phone>
        <address>
            <street>12, spam road</street>
            <city>London</city>
            <zip>H4B 1X3</zip>
        </address>
    </person>
</addressbook>"""

note_xml = """<?xml version="1.0"?>

 <note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
 </note>
"""

from lxml import etree

# XML strings to etree
addressbook_root = etree.fromstring(addressbook_xml)
note_root = etree.fromstring(note_xml)

# append the note
addressbook_root.append(note_root)

# print the new addressbook XML document
print etree.tostring(addressbook_root)

rawDescription: Append xml to xml using python.I have two xml files which i need to merge .so is there anyway i can merge both files


code:
def save(self, *args, **kwargs):
    try:
        voucher_type = VoucherTypeMaster.objects.get(
            company=self.company,
            code=self.type.code
            )
        if self.id is None:
            voucher_type.last_number = voucher_type.last_number+1
            self.type = voucher_type
            voucher_type.save()
    except Exception,e:
        print e
    super(Voucher, self).save(*args, **kwargs)

rawDescription: How to update a model instance in another model save method in django?

description: Update a model instance in another model save method in django


code:
image_array = gimp.image_list()
i=0
for image in image_array:
    img = image_array[i]
    layers = img.layers
    last_layer = len(layers)-1
    try:
        disable=pdb.gimp_image_undo_disable(img)
        pdb.gimp_layer_add_alpha(layers[0])
        drw = pdb.gimp_image_active_drawable(img)
        pdb.plug_in_colortoalpha(img,drw,(0,0,0))
        layer = pdb.gimp_image_merge_visible_layers(img, gimpfu.CLIP_TO_IMAGE)#FIXES PROBLEM OF ONLY EXPORTING TRANSPARENCY!
        enable = pdb.gimp_image_undo_enable(img)

    except:
        print "ERROR"

    pdb.file_png_save2(img, layer, "C:\\Users\\jammer\\Desktop\\test.png","test.png",1,9,1,1,1,1,1,0,1)
    i+=1

rawDescription: GIMP Python-fu exporting file only exports transparent layer


code:
new_list = []
for i, row in enumerate(matrix):
    for j, col in enumerate(row):
        new_list.append((col, (i,j)))

sorted_list = sorted(new_list, key=lambda x: x[0], reverse=True)
top_ten = sorted_list[:10]
for i in top_ten:
    print "Value: {0}, Index: {1}".format(i[0], i[1])

rawDescription: How to find the indexes of 10 highest numbers in a 14x14 numpy matrix?

description: Find the indexes of 10 highest numbers in a 14x14 numpy matrix


code:
class MemoryButton(QPushButton):
    def __init__(self, *args, **kw):
        QPushButton.__init__(self, *args, **kw)
        self.last_mouse_pos = None

    def mousePressEvent(self, event):
        self.last_mouse_pos = event.pos()
        QPushButton.mousePressEvent(self, event)

    def mouseReleaseEvent(self, event):
        self.last_mouse_pos = event.pos()
        QPushButton.mouseReleaseEvent(self, event)

    def get_last_pos(self):
        if self.last_mouse_pos:
            return self.mapToGlobal(self.last_mouse_pos)
        else:
            return None

button = MemoryButton("Click Me!")

def popup_menu():
    popup = QMenu()
    menu = popup.addMenu("Do Action")

    def _action(check):
        print "Action Clicked!"

    menu.addAction("Action").triggered.connect(_action)
    popup.exec_(button.get_last_pos())

button.clicked.connect(popup_menu)

rawDescription: PyQt4 dropdownlist with actions

description: PyQt4 dropdownlist with actions


code:
class TableModel(QtCore.QAbstractTableModel):
    def __init__(self, parent=None, *args):
        super(TableModel, self).__init__()
        self.datatable = None

    def update(self, dataIn):
        print 'Updating Model'
        self.datatable = dataIn
        print 'Datatable : {0}'.format(self.datatable)

    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self.datatable.index)

    def columnCount(self, parent=QtCore.QModelIndex()):
        return len(self.datatable.columns.values)

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if role == QtCore.Qt.DisplayRole:
            i = index.row()
            j = index.column()
            return '{0}'.format(self.datatable.iget_value(i, j))
        else:
            return QtCore.QVariant()

    def flags(self, index):
        return QtCore.Qt.ItemIsEnabled

rawDescription: PyQt - Implement a QAbstractTableModel for display in QTableView


code:
maxid = 10000000000000000000
for i in range(0,10):
    with open('output.json','a') as outfile:
        time.sleep(5) # don't piss off twitter
        print 'maxid=',maxid,', twitter loop',i
        results = api.GetSearch('search_term', count=100,max_id = maxid)
        for tweet in results:
            tweet = str(tweet).replace('\n',' ').replace('\r',' ') # remove new lines
            tweet = (json.loads(tweet))
            maxid = tweet['id'] # redefine maxid
            json.dump(tweet,outfile)
            outfile.write('\n') #print tweets on new lines

rawDescription: Does tweets from search api overlap?

description: Does tweets from search api overlap


code:
import pandas as pd
import numpy as np

df = pd.DataFrame({'product': list('aaaabbbbcccc'),
               'week': [1, 2, 3, 5, 1, 2, 3, 5, 1, 2, 3, 4],
               'sales': np.power(2, range(12))})

df = df.set_index('week')

def reindex_by_max_index_of_group(df):
    index = range(1, max(df.index) + 1)
    return df.reindex(index, fill_value=0)

df = df.groupby('product').apply(reindex_by_max_index_of_group)

df.drop(['product'], inplace=True, axis=1)
print df.reset_index()

#   product  week  sales
#0        a     1      1
#1        a     2      2
#2        a     3      4
#3        a     4      0
#4        a     5      8
#5        b     1     16
#6        b     2     32
#7        b     3     64
#8        b     4      0
#9        b     5    128
#10       c     1    256
#11       c     2    512
#12       c     3   1024
#13       c     4   2048

rawDescription: Pandas rolling sum with unevenly spaced index


code:
import random

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors import LinkExtractor
from scrapy.http import Request

class MySpider(CrawlSpider):
    name = 'alexa'
    allowed_domains = ['alexa.com']
    start_urls = ['http://www.alexa.com']
    rules = (
        Rule(LinkExtractor(allow=('topsites', )), callback='parse_page1'),
    )

    _retries = 0

    _random_urls = [
        'http://www.alexa.com/probablydoesnotexist',
        'http://www.alexa.com/neitherdoesthis',
        'http://www.alexa.com/siteinfo/google.com'
    ]

    def parse_page1(self, response):
        print "Got status: ", response.status
        if self._retries == 0 or response.status != 200:
            self._retries += 1
            print 'Retries in 1: ', self._retries
            return Request(random.choice(self._random_urls),
                                 meta={"handle_httpstatus_list": [301, 302, 303, 404]},
                               callback=self.parse_page1)
        else:
            print "Exiting"

rawDescription: Trying / Probing urls with scapy Request


code:
class C(object):
    def foo(self):
         return B()

class B(object):
    def __del__(self):
        print 'deleting %r' %self
    def bar(self):
        print 'I am method bar of %r' %self

c = C()
c.foo().bar()

rawDescription: Are temporary references automatically cleared in Python?


code:
import wx

class TestPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)

        b = wx.Button(self, -1, "Create and Show a ProgressDialog", (50,50))
        self.Bind(wx.EVT_BUTTON, self.OnButton, b)

    def OnButton(self, evt):
        max = 80

        dlg = wx.ProgressDialog("Progress dialog example",
                               "An informative message",
                               maximum = max,
                               parent=self,
                               style = wx.PD_CAN_ABORT
                                | wx.PD_APP_MODAL
                                | wx.PD_ELAPSED_TIME
                                #| wx.PD_ESTIMATED_TIME
                                | wx.PD_REMAINING_TIME
                                )
        for child in dlg.GetChildren():
            if isinstance(child, wx.Button):
                cancel_function = lambda evt, parent=dlg: self.onClose(evt, parent)
                child.Bind(wx.EVT_BUTTON, cancel_function)

        keepGoing = True
        count = 0

        while keepGoing and count < max:
            count += 1
            wx.MilliSleep(250)  # simulate some time-consuming thing...

            if count >= max / 2:
                (keepGoing, skip) = dlg.Update(count, "Half-time!")
            else:
                (keepGoing, skip) = dlg.Update(count)

        dlg.Destroy()

    #----------------------------------------------------------------------
    def onClose(self, event, dialog):
        """"""
        print "Closing dialog!"

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title='Progress')
        panel = TestPanel(self)
        self.Show()

if __name__ == '__main__':
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

rawDescription: How to override the "Cancel" button event of a ProgressDialog?

description: Override the "Cancel" button event of a ProgressDialog


code:
import pandas as pd
rankings = pd.read_html('https://en.wikipedia.org/wiki/Rankings_of_universities_in_the_United_Kingdom', header=0)
university_guide = rankings[0]
df = pd.DataFrame(university_guide.values.reshape((30, 2)), columns=['Rank', 'University'])
df = df.sort('Rank').reset_index(drop=True)
print df

rawDescription: Reshaping Pandas Data Frame

description: Reshaping Pandas Data Frame


code:
get_script = """
local keys = (redis.call('keys', ARGV[1]))
local values={}

for i,key in ipairs(keys) do
    local val = redis.call('GET', key)
    values[i]=val
    i=i+1
end

return values
"""
get_values = rc.register_script(get_script)

print get_values(args=['google:*'])

rawDescription: Getting all the values matching a key pattern from redis through Lua


code:
from zipfile import _ZipDecrypter

fp = open(somefile, 'rb')
zd = _ZipDecrypter(somekey)

fp.read(12)
print ''.join(zd(c) for c in fp.read())

fp.close()

rawDescription: Decrypt VIM encrypted file in Python

description: Decrypt VIM encrypted file in Python


code:
import scrapy

class TestSpider(scrapy.Spider):

    name = 'casestatus'
    start_urls = ['https://egov.uscis.gov/casestatus/landing.do']

    def parse(self, response):

        request = scrapy.FormRequest.from_response(
            response,
            formname='caseStatusForm',
            formdata={'appReceiptNum': 'EAC1590674053'},
            callback=self.parse_caseStatus
        )
        print request.body
        yield request

    def parse_caseStatus(self,response):
        sel_current_status = response.xpath('//div[contains(@class,"current-status")]')
        if sel_current_status:
            txt_current_status = sel_current_status.xpath('./text()').extract()
            txt_current_status = " ".join(map(unicode.strip,txt_current_status))
            print txt_current_status
        else:
            print 'NO STATUS FOUND'

# YIELDS THE FOLLOWING OUTPUT FOR ME:
# [casestatus] DEBUG: Crawled (200) <POST https://egov.uscis.gov/casestatus/mycasestatus.do;jsessionid=A19A03FC933A208A2DDF89D98BE9F32E> (referer: https://egov.uscis.gov/casestatus/landing.do)
# Case Rejected Because I Sent An Incorrect Fee

rawDescription: Crawl a webpage where the input come from a textbox


code:
import envoy
p = envoy.connect("python -m SimpleHTTPServer 9999")
print "run here!"
p.kill()

rawDescription: Python Envoy won't run threaded

description: Python Envoy won't run threaded


code:
import urllib2
from lxml import etree
rss = urllib2.urlopen("http://dizzy-v.co.kr/test/test.xml").read()
root = etree.fromstring(rss) # <----
for product in root.iter('product'):
    for element in product.iter():
        print element.text

rawDescription: lxml,Python. I can't get content of element: element.text is None

description: Lxml,Python. I can't get content of element: element.text is None


code:
for i in range(len(arr)):
    if arr[i] == "foo":
        if arr[i+1] == 'bar':
            print arr[i] + arr[i+1]
        else:
            print arr[i]

rawDescription: checking next element in for loop

description: Checking next element in for loop


code:
from datetime import datetime

line = '[03/Feb/2013:02:52:05 +0000]'
date = line[1:line.find(' ')]   #  take the string from the second character to the first blank
line_timestamp = datetime.strptime(date, "%d/%b/%Y:%H:%M:%S")

print repr(line_timestamp)
# datetime.datetime(2013, 2, 3, 2, 52, 5)

rawDescription: Subtracting (Apache log) timestamp strings in python?


code:
import os.path
print os.path.abspath(__file__+'/../../..')   # the directory three levels up

rawDescription: Find top-level directory from subdirectory on Linux in Python


code:
class Parent(object):
  def __init__(self):
    print 'Parent.__init__'
  def func(self, x):
    print x
class Child(Parent):
  pass
x = Child()
x.func(1)

rawDescription: Python classes and __init__ method

description: Python classes and __init__ method


code:
from sgmllib import SGMLParser

class URLLister(SGMLParser):
    def reset(self):
        SGMLParser.reset(self)
        self.urls = []

    def start_a(self, attrs):
        href = [v for k, v in attrs if k=='href']
        if href:
            self.urls.extend(href)

import urllib, urllister
usock = urllib.urlopen("http://diveintopython.net/")
parser = urllister.URLLister()
parser.feed(usock.read())
usock.close()
parser.close()
for url in parser.urls: print url

rawDescription: Getting the value of href attributes in all <a> tags on a html file with Python


code:
import itertools
import re

mystring = 'I have this Pig and that pig with a hOrse and coW'

rd = {'pig': 'car', 'horse':'airplane', 'cow':'bus'}

cachedict = {}

def strrep(orig, repdict):
    for k,v in repdict.iteritems():
        if k in cachedict:
            pattern = cachedict[k]
        else:
            pattern = re.compile(k, re.IGNORECASE)
            cachedict[k] = pattern
        orig = pattern.sub(v, orig)
    return orig

print strrep(mystring, rd)

rawDescription: Python Case Insensitive Replace All of multiple strings


code:
import sys
1
"1"
def display_as_hex(item):
    if isinstance(item, (int, long)):
        print hex(item)
    else:
        print repr(item)
sys.displayhook = display_as_hex
1
"1"

rawDescription: Python console default hex display

description: Python console default hex display


code:
from multiprocessing import Process, Queue

def do_sum(q,l):
    q.put(sum(l))

def main():
    my_list = range(1000000)

    q = Queue()

    p1 = Process(target=do_sum, args=(q,my_list[:500000]))
    p2 = Process(target=do_sum, args=(q,my_list[500000:]))
    p1.start()
    p2.start()
    r1 = q.get()
    r2 = q.get()
    print r1+r2

if __name__=='__main__':
    main()

rawDescription: Python: Multicore processing?

description: Python: Multicore processing


code:
from collections import Counter

def main():
    with open('garbledText.txt') as text:
        collection = Counter(text.read())
    print [char for char, times in collection.items() if times < 3]

if __name__=='__main__':
    main()

rawDescription: python 2.7, using Counter on open('file.txt')

description: Python 2.7, using Counter on open('file.txt')


code:
def blahblahblah(input):
    code  = "result = %s\n" % (", ".join(map(repr, input)))  # Map repr instead of str
    print code

blahblahblah(["dasdad", 131])

rawDescription: Formatting elements of a list python

description: Formatting elements of a list python


code:
text = txt.read().decode("utf-16")
print text

rawDescription: Reading txt files in python

description: Reading txt files in python


code:
from threading import Thread
from multiprocessing import Queue

def func1(x):
    return [i*i for i in x]

nums = [1,2,3,4,5,112,32,53,64,25]

def wrapper(func, qIn, qOut):
    for arg in iter(qIn.get, None):
        qOut.put(func(arg))

qIn, qOut = Queue(), Queue()
chunksize = len(nums)/numThreads
for i in xrange(numThreads):
    qIn.put(nums[i*chunksize : (i+1)*chunksize])
numThreads = 2  # or N
for _ in xrange(numThreads):
    qIn.put(None)
for _ in xrange(numThreads):
    Thread(target=wrapper, args=(func1, qIn, qOut)).start()

for _ in xrange(numThreads):
    print qOut.get()

rawDescription: How to run N threads of the same functions with different parameters in parallel? - python

description: Run N threads of the same functions with different parameters in parallel? - python


code: print repr('\x30')

rawDescription: trouble formatting serial data from python enumeration

description: Trouble formatting serial data from python enumeration


code:
def find_column_redundancy(dataset):
    n_columns = dataset.shape[1]
    block_size = 10000000
    n_blocks = int(ceil(float(n_columns) / float(block_size)))

    d = defaultdict(list)
    analysed_column_count = 0
    for block in xrange(n_blocks):
        block_offset = block*block_size
        block_data = dataset[:, block_offset : block_offset+block_size]
        block_data = np.asfortranarray(block_data)
        block_data = np.packbits(block_data, axis=0)
        block_data.flags.writeable = False
        for i in xrange(block_data.shape[1]):
            d[block_data[:, i].data[:]].append(block_offset + i)
            analysed_column_count += 1

        print float(analysed_column_count)/n_columns*100, "% completed. Dictionnary has", len(d), "items."

return d

rawDescription: Finding unique columns in an HDF5 dataset


rawDescription: Pass non-iterable variables to a function evaluated with map()


code:
while True:
    now = datetime.datetime.now()
    if now.second == 1:
        print "One"
        time.sleep(59) # wait 59 seconds after success
    time.sleep(1) # wait 1 second after each fail

rawDescription: Python 2.7 While Loop time

description: Python 2.7 While Loop time


code:
def mixrange(s):
    r = []
    for i in s.split(','):
        if '-' not in i:
            r.append(int(i))
        else:
            l,h = map(int, i.split('-'))
            r+= range(l,h+1)
    return r

print mixrange('1-3,6,8-10')

description: Expand a range which looks like: "1-3,6,8-10" to [1,2,3, 6, 8,9,10]


code:
import httplib
from xml.dom import minidom

http.request("POST", "/path/to/my/webservice", body=xml, headers = {
    "Host": "myservername",
    "Content-Type": "text/xml; charset=UTF-8",
    "Content-Length": len(xml)
})

print minidom.parseString(http.getresponse().read())

rawDescription: Can I use HTTP Post Requests for SOAP? - SOAP and Django

description: Use HTTP Post Requests for SOAP? - SOAP and Django


code:
#!/usr/bin/python
import ctypes
import os

class XScreenSaverInfo( ctypes.Structure):
  """ typedef struct { ... } XScreenSaverInfo; """
  _fields_ = [('window',      ctypes.c_ulong), # screen saver window
              ('state',       ctypes.c_int),   # off,on,disabled
              ('kind',        ctypes.c_int),   # blanked,internal,external
              ('since',       ctypes.c_ulong), # milliseconds
              ('idle',        ctypes.c_ulong), # milliseconds
              ('event_mask',  ctypes.c_ulong)] # events

xlib = ctypes.cdll.LoadLibrary('libX11.so')
display = xlib.XOpenDisplay(os.environ['DISPLAY'])
xss = ctypes.cdll.LoadLibrary('libXss.so.1')
xss.XScreenSaverAllocInfo.restype = ctypes.POINTER(XScreenSaverInfo)
xssinfo = xss.XScreenSaverAllocInfo()
xss.XScreenSaverQueryInfo(display, xlib.XDefaultRootWindow(display), xssinfo)

print "idle: %d ms" % xssinfo.contents.idle

rawDescription: In Python on Unix, determine if I am using my computer? or idle?


rawDescription: Python: Sort list by date?

description: Python: Sort list by date


rawDescription: What's the most elegant way to write this for loop in Python?


code:
import struct
s = struct.pack("f", -0.8)
i = struct.unpack("i", s)
print i

rawDescription: how to map float to 32 bit integer in python

description: Map float to 32 bit integer in python


code:
import maya.cmds as cmds
import os

def openFileAndRemapRefs():
    multipleFilters = "Maya Files (*.ma *.mb);;Maya ASCII (*.ma);;Maya Binary (*.mb);;All Files (*.*)"

    # Choose file to open
    filename = cmds.fileDialog2(fileFilter=multipleFilters, dialogStyle=2, fileMode=1)

    # Open file with no reference loaded
    cmds.file( filename[0], open=True, force=True );

    # Dir containing the references
    refDir = 'C:/References'

    # A list of any references found in the scene
    references = cmds.ls(type='reference')

    # For each reference found in scene, load it with the path leading up to it replaced
    for ref in references:
        refFilepath = cmds.referenceQuery(ref, f=True)
        refFilename = os.path.basename( refFilepath )
        print 'Reference ' + ref + ' found at: ' + cmds.referenceQuery(ref, f=True)
        cmds.file( os.path.join(refDir, refFilename), loadReference=ref, options='v=0;')

openFileAndRemapRefs()

rawDescription: script for automatically loading reference files from different destinations in Maya

description: Script for automatically loading reference files from different destinations in Maya


code:
def powerSet(items):
    n = len(items)
    for i in xrange(2**n):
        combo = []
        for j in xrange(n):
            #print i,j,(i >> j) % 2 == 1 # uncomment to see the values through the loop
            # test bit jth of integer i
            if (i >> j) % 2 == 1:
                combo.append(items[j])
        yield combo
print list(powerSet([1,2,3,4]))
[[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3], [4], [1, 4], [2, 4], [1, 2, 4], [3, 4], [1, 3, 4], [2, 3, 4], [1, 2, 3, 4]]

rawDescription: How to access list elements with a binary-patern (bitwise) counting in python

description: Access list elements with a binary-patern (bitwise) counting in python


code: with open(afile,"rb") as f: print f.readlines()

rawDescription: python readlines() does not contain whole file

description: Python readlines() does not contain whole file


code:
#!/usr/bin/env python
# Tested with Python 2.7

# I prefer this style of importing - hides the csv module
# in case you do from this_file.py import * inside of __init__.py
import csv as _csv

# Real comments are more complicated ...
def is_comment(line):
    return line.startswith('#')

# Kind of sily wrapper
def is_whitespace(line):
    return line.isspace()

def iter_filtered(in_file, *filters):
    for line in in_file:
        if not any(fltr(line) for fltr in filters):
            yield line

# A dis-advantage of this approach is that it requires storing rows in RAM
# However, the largest CSV files I worked with were all under 100 Mb
def read_and_filter_csv(csv_path, *filters):
    with open(csv_path, 'rb') as fin:
        iter_clean_lines = iter_filtered(fin, *filters)
        reader = _csv.DictReader(iter_clean_lines, delimiter=';')
        return [row for row in reader]

# Stores all processed lines in RAM
def main_v1(csv_path):
    for row in read_and_filter_csv(csv_path, is_comment, is_whitespace):
        print(row)  # Or do something else with it

# Simpler, less refactored version, does not use with
def main_v2(csv_path):
    try:
        fin = open(csv_path, 'rb')
        reader = _csv.DictReader((line for line in fin if not
                                  line.startswith('#') and not line.isspace()),
                                  delimiter=';')
        for row in reader:
            print(row)  # Or do something else with it
    finally:
        fin.close()

if __name__ == '__main__':
    csv_path = "C:\Users\BKA4ABT\Desktop\Test_Specification\RDBI.csv"
    main_v1(csv_path)
    print('\n'*3)
    main_v2(csv_path)

rawDescription: how to skip blank line while reading CSV file using python

description: Skip blank line while reading CSV file using python


code:
class Garage:
    def __init__(self):
        self.cars = []            # Initialize instance attribute here.

    def add_car(self, car):
        self.cars.append(car)

class Car:
    def __init__(self, color):
        self.color = color        # Ditto.

    def __repr__(self):
        return "Car(color={})".format(self.color)

def main():
    g = Garage()
    for c in 'red green blue'.split():
        c = Car(c)
        g.add_car(c)        # Pass the car to the garage's add_car method.
    print g.cars            # [Car(color=red), Car(color=green), Car(color=blue)]

if __name__ == "__main__":
    main()

rawDescription: How do I pass instance of an object as an argument in a function in python?

description: Pass instance of an object as an argument in a function in python


code:
def readwrite(infilepaths, outfilepath):
    with open(outfilepath, 'w') as outfile:
        outfile.write(threeLinesOfHeader + '\n')
        for infilepath in infilepaths:
            with open(infilepath) as infile:
                skipLines = 4
                for _ in range(skipLines):
                    infile.readline()
                values = itertools.chain.from_iterable(line.strip().split() for line in infile)
                outfile.write('\n'.join(values) + '\n')
    print 'done'

rawDescription: read multiple text files and write to one file in one column

description: Read multiple text files and write to one file in one column


code:
somedict = {'one': 1, 'two': 2, 'doubletwo': 2, 'three': 3}

invert_dict={}
{invert_dict.setdefault(v, []).append(k) for k, v in somedict.items()}

print invert_dict
# {1: ['one'], 2: ['doubletwo', 'two'], 3: ['three']}

rawDescription: Dictionary comprehension for swapping keys/values in a dict with multiple equal values


code:
import Tkinter as tk

result = ['Weekly', 'Monthly', 'Annual']

class Application(tk.Frame):

    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.listbox = dict() # lower case for variable name
        self.label = dict() # lower case for variable name

        for inst in result:
            #textenter = "Select the required output format" + inst
            textenter = inst

            self.label[inst] = tk.Label(self, text=textenter)
            self.label[inst].grid(columnspan=2, sticky=tk.W)

            self.listbox[inst] = tk.Listbox(self, selectmode=tk.MULTIPLE, exportselection=0)
            self.listbox[inst].grid(sticky=tk.W)

            for items in ["Text", "XML", "HTML"]:
                self.listbox[inst].insert(tk.END,items)

        self.submit_button = tk.Button(self, text="Submit", command=self.returns)
        self.submit_button.grid(row=7, column=1, sticky=tk.W)

    def returns(self):
        self.content = []

        for inst in result:
            self.content.append(self.listbox[inst].curselection())

        print self.content

        self.master.destroy()

root = tk.Tk()
app = Application(root)
root.title("Output Formats")
app.mainloop()

rawDescription: Multiple listboxes in python dynamically with Tkinter


code:
import numpy
import multiprocessing as mp
import time
start=time.time()
#Getting the number of CPUs present
num_cpu=mp.cpu_count()
#Creation of random data for testing
data=numpy.random.randint(1,30,size=(200,200))
x,y=data.shape
#Padding is introduced to cope with the border of the dataset.
#Change if you want other behaviour like wrapping, reflection etc.
def pad(data):
    '''Can be made faster, by using numpys pad function
    if present'''
    a=numpy.zeros((x+2,y+2))
    a[1:-1,1:-1]=data
    return a
data=pad(data)
#Kernel to get only the neighbours, change that if you only want diagonals or other shapes.
kernel=numpy.array([[1,1,1],[1,0,1],[1,1,1]])
result_list=[]
#Setting up functions for Parallel Processing
def log_result(result):
    result_list.append(result)
def max_getter(pixel):
    '''As this function is going to be used in a parallel processing environment,
    the data has to exist globally in order not to have to pickle it in the subprocess'''
    temp=data[pixel[0]-1:pixel[0]+2,pixel[1]-1:pixel[1]+2].copy()*kernel
    #Getting the submatrix without the central pixel
    compare=numpy.max(temp)==temp
    coords=numpy.nonzero(compare)
    if len(coords[0])!=1:
        coords=(coords[0][0],coords[1][0])
    #discards every maximum which is not the first. Change if you want.
    #Converting back to global coordinates
    return (pixel,(pixel[0]+(numpy.asscalar(coords[0])-1),pixel[1]+(numpy.asscalar(coords[1])-1)))
    #This assumes, that the maximum is unique in the subset, if this is not the case adjust here
def parallell_max():
    pool = mp.Pool()
#You can experiment using more cores if you have hyperthreading and it's not correctly found by cpu_count
    for i in range(1,x+1):

        for j in range(1,y+1):

            pool.apply_async(max_getter, args = ((i,j),),callback=log_result)
    pool.close()
    pool.join()

#___________START Parallel Processing________
if __name__ == '__main__':
   # directions={}
    parallell_max()
    directions={}
    for i in result_list:
        directions[i[0]]=i[1]
    #Directions is a dictionary-like lookup-table, where every pixel gives the next pixel in the line
    lines=[]
    #The following code can also be easily parallelized as seen above.
    for i in range(1,x+1):
        for j in range(1,y+1):
            line=[]
            first,second=i,j
            for k in range(100):
                line.append((first,second))
                first,second=directions[(first,second)]
            lines.append(line)
    stop=time.time()
    print stop-start

rawDescription: Vectorize nested for loop Python

description: Vectorize nested for loop Python


code:
import pyqtgraph as pg

w = pg.GraphicsWindow()
for i in range(4):
    w.addPlot(0, i)

def onClick(event):
    items = w.scene().items(event.scenePos())
    print "Plots:", [x for x in items if isinstance(x, pg.PlotItem)]

w.scene().sigMouseClicked.connect(onClick)

rawDescription: PyQt mousePressEvent - get object that was clicked on?


code:
import sys

for line in sys.stdin:
    print line

rawDescription: How to receive data from the environment e.g. bash

description: Receive data from the environment e.g. bash