code:
excel = win32.gencache.EnsureDispatch('Excel.Application')
try:
    wb = excel.Workbooks.Open(file)
except:
    print "Failed to open spreadsheet " + file
    sys.exit(1)
count = wb.Sheets.Count

rawDescription: Count number of sheets in an Excel file using win32com.client


code:
if my_num[0:2] == "0x" or my_num[0] == "x":print "hex"
elif my_num[0:2] == "0b" or my_num[0] == "b" and all(x in "01" for x in my_num):print "bin"
elif my_num[0] in "0O": print "oct"
elif re.match("^[0-9]+$",my_num): print "dec"
else: print "I dont know i guess its just a string ... or maybe base64"

rawDescription: Python Code for Bin, Dec, Hex string Identifier & Converter


code:
import urllib2

user = 'admin'
pwd = 'notmyrealpassword'
host = '192.168.1.1'
url = 'http://' + host + '/traffic_meter_2nd.htm'

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, host, user, pwd)
authhandler = urllib2.HTTPBasicAuthHandler(passman)

opener = urllib2.build_opener(authhandler)
response = opener.open(url)
stuff = response.read()
response.close()

print stuff

rawDescription: How to access netgear router web interface

description: Access netgear router web interface


code:
def remove_items(d, *items):
    """
    Return dictionary copy with some items removed.
    """
    return { a: b for a, b in d.iteritems() if a not in items }

print { x: remove_items(y, 'private') for x, y in out.iteritems() }

rawDescription: Python dict restructure

description: Python dict restructure


code:
print b[(b['Commodity - Transaction'].str.contains('Gas Oil/ Diesel Oil - Production'))  &
        (b['Year'] > 2000) ]

rawDescription: Query with multiple filters on Pandas

description: Query with multiple filters on Pandas


code:
import ctypes
from ctypes import byref
from lcms2consts import *

lcms = ctypes.windll.lcms2

inprof = lcms.cmsCreateLab4Profile(0)
outprof = lcms.cmsCreate_sRGBProfile()
xform = lcms.cmsCreateTransform(inprof, TYPE_Lab_DBL,
    outprof, TYPE_RGB_8,
    INTENT_PERCEPTUAL, 0)
lcms.cmsCloseProfile(inprof)
lcms.cmsCloseProfile(outprof)

DblTriplet = ctypes.c_double * 3
ByteTriplet = ctypes.c_ubyte * 3
inbuf = DblTriplet(60.1,20.2,0.5)
outbuf = ByteTriplet()
lcms.cmsDoTransform(xform, byref(inbuf), byref(outbuf), 1)
print list(outbuf)

lcms.cmsDeleteTransform(xform)

rawDescription: How can one perform color transforms with ICC profiles on a set of arbitrary pixel values (not on an image data structure)?


code:
details={'firstName': 'Test', 'activeSubscriptions': [{'productCode': 'BBB', 'name': 'Bar'}, {'productCode': 'FFF', 'name': 'Foo'}]}
product_code_search_key = 'FFF'
for subscription in details['activeSubscriptions']:
    if subscription and subscription.get('productCode','') and subscription['productCode'] == product_code_search_key:
        print subscription['name']
        break

Foo

rawDescription: Find specific variable in array inside array


code:
from Tkinter import *
import tkSimpleDialog as tks

import cmath,math

root = Tk()

c = Canvas(root,width=200, height=200)
c.pack()

# keypress event
def key(event):
    text.focus_force()
    ch=event.char

    # handle backspace
    if ch=='\x08':
        if len(text.get())>1 :
            entry_text=text.get()[:-1]
            if entry_text=='-': entry_text='0'
        else:
            entry_text='0'
    else:
        entry_text=text.get()+ch

    # we want an integer
    try:
        angle_degrees=int(entry_text)
        cangle = cmath.exp(angle_degrees*1j*math.pi/180)

        offset = complex(center[0], center[1])
        newxy = []
        for x, y in triangle:
            v = cangle * (complex(x, y) - offset) + offset
            newxy.append(v.real)
            newxy.append(v.imag)
        c.coords(polygon_item, *newxy)
    except ValueError:
        print "not integer"

text = Entry(root)
text.bind("<Key>", key)

text.pack()
text.focus_force()

# a triangle
triangle = [(50, 50), (150, 50), (150, 150)]

polygon_item = c.create_polygon(triangle)
center = 100, 100

mainloop()

rawDescription: trying to rotate a triangle with complex numbers in tkinter python

description: Trying to rotate a triangle with complex numbers in tkinter python


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup("
Something

Something
")
print soup.get_text(separator=u' ')

rawDescription: Replacing <p> and <br> tags in beautifulsoup


code:
#!/usr/bin/env python

import os
import time

def watch_file( filename, time_limit=3600, check_interval=60 ):
    '''Return true if filename exists, if not keep checking once every check_interval seconds for time_limit seconds.
    time_limit defaults to 1 hour
    check_interval defaults to 1 minute
    '''

    now = time.time()
    last_time = now + time_limit

    while time.time() <= last_time:
        if os.path.exists( filename ):
             return True
        else:
            # Wait for check interval seconds, then check again.
            time.sleep( check_interval )

    return False

if __name__ == '__main__':
    filename = '/the/file/Im/waiting/for.txt'
    time_limit = 3600 # one hour from now.
    check_interval = 60 # seconds between checking for the file.

    if watch_file( filename, time_limit, check_interval ):
        print "File present!"
    else:
        print "File not found after waiting:", time_limit, " seconds!"

rawDescription: 'Listening' for a file in Python

description: 'Listening' for a file in Python


code:
import re
Searcher = re.compile("IFCCOLOURRGB\(\$,([\d\.]+),([\d\.]+),([\d\.]+)")

for Line in IfcFile:
    Result = Searcher.search(Line)
    if Result:
        print Result.groups()

rawDescription: Python Regular Expressions Return Floating Points


code:
import bigfloat as bf
b = bf.BigFloat('1e1000', bf.precision(21))
print bf.sqrt(b)

rawDescription: Converting An "Infinite" Float To An Int


code:
import ctypes as ct
class MyCtStructure(ct.Structure):

    def __eq__(self, other):
        for fld in self._fields_:
            if getattr(self, fld[0]) != getattr(other, fld[0]):
                return False
        return True

    def __ne__(self, other):
        for fld in self._fields_:
            if getattr(self, fld[0]) != getattr(other, fld[0]):
                return True
        return False

class Point(MyCtStructure):
    _fields_ = [
        ('x', ct.c_int),
        ('y', ct.c_int),
    ]

p1 = Point(10, 11)
p2 = Point(10, 11)

print p1 == p2

rawDescription: How to compare two ctypes objects for equality?

description: Compare two ctypes objects for equality


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

def delayedFunction(dF):
    print('I was called')
    dF.success(True)

class Counter(object):
    def timeOffsetExecute(self,delay):
        dF = defer.Deferred()
        reactor.callLater(delay,delayedFunction,dF)
        return dF

def onAllResult(val):
    print 'All delayed functions called'
    reactor.stop()

cp = Counter()

dl = defer.DeferredList([cp.timeOffsetExecute(1), cp.timeOffsetExecute(3), cp.timeOffsetExecute(9)], consumeErrors=True)
dl.addCallback(onAllResult)

reactor.run()

rawDescription: How to know reactors all process are completed

description: Know reactors all process are completed


code:
spheres = mc.ls(selection=True)
count=0

for sphere in spheres:
    count += 2
    mc.selectKey(sphere) # only selecting the one sphere!
    mc.keyframe(edit=True, relative=True, timeChange=count)
    print sphere # no need to look up the element
                 # which by the way should have been [i] not (i)

rawDescription: How To Get A For Loop To Affect Each Individual Object


code:
import itertools as it

class ColourGun(object):

    def __init__(self):
        self._colourgen = it.cycle(['predefined_colour1', 'predefined_colour2', 'predefined_colour3'])

    def load(self, colours):
        self._colourgen = it.cycle(colours)

    @property
    def colour(self):
        return next(self._colourgen)

    def __str__(self):
      return self.colour

gun = ColourGun()
print gun
print gun
print gun
gun.load(['potato', 'spam', 'eggs'])
print gun
print gun

rawDescription: Constructing an object which changes its state each time it is accessed and behaves like a variable


code:
con.select("[Gmail]/Spam") # for the label "Sent" use: "[Gmail]/Sent Mail"
status, data = con.search(None, "ALL")
print len(data[0].split()) # prints the number of msgs in your Spam

rawDescription: how to count how many letters in my Junk in gmail?

description: Count how many letters in my Junk in gmail


code:
def setPath(body, path, value):
    brace = path.find('[')
    if brace == 0:
        brace = path.index(']', 1)
        prop = path[1:brace]
        path = path[brace+1:]
    elif brace > 0:
        prop = path[:brace]
        path = path[brace:]
    else:
        path = prop

    if len(path) > 0:
        if type(body) != dict or prop not in body:
            body[prop] = dict()
        return setPath(body[prop], path, value)

    body[prop] = value

body = dict()
setPath(body, "[A][B][C]", "foo")
print body

rawDescription: how to set a string as nested Json key in Python

description: Set a string as nested Json key in Python


code:
fun_string = """In ___0___, crazy ___1___ with ___2___, ate a meal called ___3___ on a grill"""
horror_string = """In ___0___ owned by ___1___, many were killed by ___2___ because of huanted ___3___"""
mystery_string = """The most mysterious place on Earth is ___0___, next to ___1___'s house because of a ___2___ living in ___3___"""

answers = []
blanks = ["___0___", "___1___", "___2___", "___3___"]

def select_level():

    user_input = raw_input("Type in a story you wish to play: ")

    while user_input not in ["Fun", "Horror", "Mystery"]:
        user_input = raw_input("Invalid story. Please try again: ")

    if user_input == "Fun":
        quiz(fun_string, answers, blanks)
    elif user_input == "Horror":
        quiz(horror_string, answers, blanks)
    elif user_input == "Mystery":
        quiz(mystery_string, answers, blanks)

def quiz(quiz_string, answers, blanks):
    print quiz_string

    for blank in blanks:
        answer = raw_input("Type in a word: ")
        quiz_string = quiz_string.replace(blank, answer)
        answers.append(answer)

    print quiz_string

print """Welcome to kis ReMadlibs!!!!"""
print """Select a story you wish to particiate!!"""
print """Fun, Horror, Mystery"""

select_level()

rawDescription: What can you use instead of zip?

description: What can you use instead of zip


code:
import numpy as np
from sklearn.preprocessing import normalize

x = np.random.rand(1000)*10
norm1 = x / np.linalg.norm(x)
norm2 = normalize(x[:,np.newaxis], axis=0).ravel()
print np.all(norm1 == norm2)
# True

rawDescription: how to normalize array numpy?

description: Normalize array numpy


code:
def phone():
    if input("Did you  buy your phone recently? ") == 'yes':
        if input("Did you drop your phone? ") == 'yes':
            print sol1
            return
        else:
            print sol2
            return

    else:
        phone() #this will make you go back to the start of your function, in this case the first question

rawDescription: Linking a Qst to the first Qst asked for the user in Python


code:
def match_class(target):
    target = target.split()
    def do_match(tag):
        try:
            classes = dict(tag.attrs)["class"]
        except KeyError:
            classes = ""
        classes = classes.split()
        return all(c in classes for c in target)
    return do_match

html = """

The actual data is some where here

"""

from BeautifulSoup import BeautifulSoup

soup = BeautifulSoup(html)

matches = soup.findAll(match_class("feeditemcontent cxfeeditemcontent"))
for m in matches:
    print m
    print "-"*10

matches = soup.findAll(match_class("feeditembody"))
for m in matches:
    print m
    print "-"*10

rawDescription: Get class name and contents using Beautiful Soup


code:
submain = main
for key in mylist:
    submain = submain[key]
print submain

rawDescription: Python - "map" a list of keys to a dictionary


code:
my_list = [1,1,2,3,4,5]

def find_pairs(arr, limit):
    arr = sorted(arr)
    res = []

    start = 0
    end = len(arr) - 1

    while start < end:
        if arr[start] + arr[end] >= limit:
            res.append((arr[start], arr[end]))
            end -= 1
        start += 1

    return res

print find_pairs(my_list, 5) # [(1, 5), (1, 4), (2, 3)]

rawDescription: Python - Get sum of pair in a list which is greater than a value with no index being used in multiple pairs


code:
timeLS1 = 0.1
timeLS2 = 0.2
timeLS3 = 0.15
timeLS4 = 0.5
timeLS5 = 0.4
timeLS6 = 0.3

data = {key: value for key, value in locals().iteritems() if 'timeLS' in key}

key, value = max(data.iteritems(), key=lambda x: x[1])
print key, value  # prints 'timeLS4 0.5'

rawDescription: Get name of variable corresponding to maximum value in a list


code:
# you can give k or i a default value here as you like
k = 0
i = 0
while k <= 5:
    for num in range(1, 10):
        print "test"
        i +=1
    k+=1

rawDescription: Python Nested Loop Working Weirdly

description: Python Nested Loop Working Weirdly


code:
from collections import deque # bfs

def bfs(adj_l, i, comp):
    """Performs a Breadth First Search from node i on the graph given by adjacency list, filling the components list
. Returns the nodes of the component."""

    l=[i]
    queue = deque([i])
    comp[i] = i
    while queue:
        j = queue.popleft()
        for neighbor in adj_l[j]:
            if comp[neighbor] is None:
                queue.append(neighbor)
                comp[neighbor] = i # update the component
                l.append(neighbor)
    return l

def connectedcomponents(adj_l):
    """Returns the connected components (list of list of nodes) for the graph given by adjency list."""

    n = len(adj_l)

    comp = [None]*n
    ll=[]
    for i in range(n):
        if comp[i] is None:
            ll.append(bfs(adj_l, i, comp))

    return ll

def graph_operations(L1, L2, L3, P12, P13, P23):
    l=L1+L2+L3
    v=[[] for _ in l]
    for i,j in P12:
        v[i].append(j+len(L1))
        v[j+len(L1)].append(i)
    for i,j in P13:
        v[i].append(j+len(L1)+len(L2))
        v[j+len(L1)+len(L2)].append(i)
    for i,j in P23:
        v[i+len(L1)].append(j+len(L1)+len(L2))
        v[j+len(L1)+len(L2)].append(i+len(L1))

    ll = connectedcomponents(v)

    ll = [[l[i] for i in li] for li in ll]

    return ll

L1 = ["toto", "tata"]
L2 = ["tutu", "vincent"]
L3 = ["georges", "Jean-Eude", "Pikachu"]
P12 = [(0,1)]
P13 = [(0,1)]
P23 = [(1,0),(1,1)]

print graph_operations(L1, L2, L3, P12, P13, P23)

L1 = ["toto", "tata"]
L2 = ["tutu"]
L3 = ["georges"]
P12 = [(1,0)]
P13 = [(0,0)]
P23 = [(0,0)]

print graph_operations(L1, L2, L3, P12, P13, P23)

rawDescription: Fusion of 3 Lists

description: Fusion of 3 Lists


code:
import re

fname = './prec.f90'
f = open(fname)
lines = f.readlines()
f.close()
# If there was no end of the line character (\n) we would need to check if
# this is the end of the line (something like ([^dq\_0-9]|$)
regex = re.compile(r'(\d*\.\d*)([^dq\_0-9])')
for i, line in enumerate(lines):
    search = regex.findall(line)
    if search != []:
        print('Real found in line #%d: ' %i)
        print search
        print('The following line:\n %s' %line)
        print('will be replace by:')
        newline = regex.sub(r'\g<1>d0\g<2>', line)
        print('%s' %newline)

rawDescription: Python regex: replace ignoring empty string


code:
def foo():
    name = 'can one and three be odd numbers'
    name3 = ['one','two','three', 'four',]
    regex1one = re.compile(r'\b(?:%s)\b'%'|'.join(name3))
    regex11one = re.compile(r'\b(?:%s)\b'%'|'.join(keywords))
    name4 = re.findall(regex1one, name)
    print name4
keywords = ['be','are','is',]

foo()

rawDescription: How to match all items in a list using regex

description: Match all items in a list using regex


code:
import os
xml_files.sort(key=os.path.getmtime)
print xml_files[-1] # most recent file

rawDescription: How to grab most recently modified file

description: Grab most recently modified file


code:
class Enum(object):
    @classmethod
    def tostring(cls, val):
        for k,v in vars(cls).iteritems():
            if v==val:
                return k

    @classmethod
    def fromstring(cls, str):
        return getattr(cls, str.upper(), None)

    @classmethod
    def build(cls, str):
        for val, name in enumerate(str.split()):
            setattr(cls, name, val)
class MyEnum(Enum):
    VAL1, VAL2, VAL3 = range(3)

class YourEnum(Enum):
    CAR, BOAT, TRUCK = range(3)

class MoreEnum(Enum):
    pass

print MyEnum.fromstring('Val1')
print MyEnum.tostring(2)
print MyEnum.VAL1

print YourEnum.BOAT
print YourEnum.fromstring('TRUCK')

# Dodgy semantics for creating enums.
# Should really be
# MoreEnum = Enum.build("CIRCLE SQUARE")
MoreEnum.build("CIRCLE SQUARE")
print MoreEnum.CIRCLE
print MoreEnum.tostring(1)
print MoreEnum.tostring(MoreEnum.CIRCLE)

rawDescription: Avoiding magic numbers in Python Flask and probably most other languages


code:
for t in range(0, 2): #Each txt file contains differing amounts

    address = 'http://www.tspc.oregon.gov/lookup_application/LDisplay_Individual.asp?id=' + lines2[t]

    page = requests.get(address)

    tree = html.fromstring(page.text)

    count = 0

    test = tree.xpath(".//tr[td[1] = 'License Type']/following-sibling::tr[1]")
    difference = 15 - len(test)

    for i in range(0, difference):
        test.append('')

    for license_row in test:
        count = count + 1

        try:
            license_data = license_row.xpath(".//td/text()")

        except NameError:
            license_data = ''
            if license_data=='' and count==1:
                ltest1.append('')
            if license_data=='' and count==2:
                ltest2.append('')
            if license_data=='' and count==3:
                ltest3.append('')

        except AttributeError:
            license_data = ''

        if count==1 and True:
            print "True"
            if count==1:
                ltest1.append(license_data)

        if count==2 and True:
            print "True"
            if count==2:
                ltest2.append(license_data)

        if count==3 and True:
            print "True"
            if count==3:
                ltest3.append(license_data)

        del license_data

    for endorse_row in tree.xpath(".//tr[td = 'Endorsements']/following-sibling::tr"):
        endorse_data = endorse_row.xpath(".//td/text()")
        lendorse1.append(endorse_data)

rawDescription: Creating a if/else that appends data from mult. scraped pages if counts differ?


code:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://google.com")
print driver.title
driver.close()

rawDescription: How to integrate Selenium and Python

description: Integrate Selenium and Python


code:
import numpy as np

def in1d_tol(a,b,tol):
    d=np.abs(a-b[:,np.newaxis])
    return np.any(d<=tol, axis=0)

a = np.array([1.011, 2.000, 3.001])
b = np.array([1.01, 3.00])

c = in1d_tol(a,b,0.01)

print c

rawDescription: Python: Change format of np.array or allow tolerance in in1d function


code:
import random

hash = random.getrandbits(128)

print "hash value: %032x" % hash

rawDescription: Random hash in Python

description: Random hash in Python


code:
import urllib
from bs4 import BeautifulSoup
from contextlib import closing

url = 'https://developer.valvesoftware.com/wiki/Hammer_Selection_Tool'
with closing(urllib.urlopen(url)) as h:
    soup = BeautifulSoup(h.read())

print soup.body.get_text()

rawDescription: Scraping text from HTML5 website using Python


code:
from email import mime,message
m1 = message.Message()
t1=email.MIMEText.MIMEText('t1\r\n')
print t1.as_string()
m1.attach(t1)
m1.is_multipart()
m1.get_payload()
t2=email.MIMEText.MIMEText('t2\r\n')
m1.set_payload([t2])
print m1.get_payload()[0].as_string()

rawDescription: Python email lib - How to remove attachment from existing message?


code:
cell = 'text\n'
cell.strip()
rowdata = '|' + cell
print rowdata
rowdata = '|' + cell.strip()
print rowdata

rawDescription: Remove Carriage Return from HTML using BeautifulSoup in Python


code:
from itertools import imap

class OpenUpperCase(object):
    def __init__(self, *args, **kwargs):
        self.file = open(*args, **kwargs)
    def __enter__(self):
        return imap(lambda s: s.upper(), self.file)
    def __exit__( self, type, value, tb ):
        self.file.close()
        return False  # allow any exceptions to be processed normally

if __name__ == '__main__':
    from itertools import islice

    filename1 = 'file1.txt'
    with OpenUpperCase(filename1, "r") as file1:
        for line in islice(file1, 20, None):
            print line,  # will be uppercased

rawDescription: Python Lambda Map

description: Python Lambda Map


code:
from zipfile import ZipFile

with ZipFile('test.zip', 'r') as f:
    directories = [item for item in f.namelist() if item.endswith('/')]
    print directories

rawDescription: Extracting a single file from a zip archive without iterating over the entire name list in Python


code:
import itertools

class Selector(object):
    def __init__(self, iterable, **kwargs):
        if not kwargs:
            self.iterator = iterable
        elif len(kwargs) > 1:
            raise ValueError('only one selector type keyword allowed')
        else:
            selector, target = kwargs.items()[0]

            if selector == 'by_attr':
                self.iterator = itertools.imap(lambda obj: getattr(obj, target), iterable)
            elif selector == 'by_type':
                self.iterator = itertools.ifilter(lambda obj: isinstance(obj, target),
                                                  iterable)
            elif selector == 'by_func':
                self.iterator = itertools.ifilter(target, iterable)
            else:
                raise ValueError('unknown selector type keyword')

    def __iter__(self):
        return self.iterator

if __name__ == '__main__':
    from selector import Selector

    class A(object):
        def __init__(self, a, b):
            self.a, self.b = a, b

    class Flower(object):
        def __init__(self, name):
            self.name = name

    sel = [A(3, 4), A(0, 9), A('test', 3), A(4,22), A(3, 9)]
    print list(Selector(sel, by_attr='a'))

    sel = [42, Flower('Buttercup'), [1,2,3,5,8], A(20, 13), Flower('Rose')]
    print list(Selector(sel, by_type=Flower))

    sel = [4, 9, 3, 22, 9]
    print list(Selector(sel, by_func=lambda i: i%2 == 0))

rawDescription: What is a good way to narrow down a list of elements by their attribute or other conditions?


code:
import subprocess
from os import linesep, stat

with open(r"test.txt","a") as log:
    Process = subprocess.Popen(['dir'],
                               stdout = log, stderr = log,
                               shell=True)
    Process.wait()

with open(r"test.txt","a") as log:
# this prints 0
    print log.tell()
# get the length of the file log
    pos = stat(r"test.txt").st_size
    print pos
# go to the end of log
    log.seek(pos)
    Process = subprocess.Popen(['dir'],
                               stdout = log, stderr = log,
                               shell=True)

rawDescription: Python open+append not working as expected


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
print [a["href"] for a in soup.select('h3.r a')]

rawDescription: use beautiful soup to parse a href from given html structure

description: Use beautiful soup to parse a href from given html structure


code:
document = Document()
document_part = document.part
rels = document_part.rels
for r in rels:
  print r.rId

rawDescription: where is the word/_rels/document.xml.rels in python docx object?

description: Where is the word/_rels/document.xml.rels in python docx object


code:
import logging
from suds.client import Client

# Set logging to DEBUG level to see soap messages
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)

# URL for CA WSDL
url='https://api.channeladvisor.com/ChannelAdvisorAPI/v5/AdminService.asmx?WSDL'

# Initialize client - The extra location is required because CA is https
client = Client(url,location='https://api.channeladvisor.com/ChannelAdvisorAPI/v5/AdminService.asmx')

# Set soap headers to include login information
login = client.factory.create('APICredentials')
login.DeveloperKey = 'YOUR_KEY'
login.Password = 'YOUR_PWD'
client.set_options(soapheaders=login)

# Send Ping to CA
result = client.service.Ping()

print result

rawDescription: Python - SUDS SOAP Invalid namespaces - channel advisor?


code:
from collections import deque
last3 = deque(maxlen=3)

while True:
    last3.append(this_ping)  # <-- insert your ping here, of course
    avg = sum(last3) / len(last3)
    print avg

rawDescription: Python last three items in stream

description: Python last three items in stream


code:
#From http://www.ariel.com.au/a/python-point-int-poly.html
# Modified by Nick ODell
from collections import namedtuple

def point_in_polygon(target, poly):
    """x,y is the point to test. poly is a list of tuples comprising the polygon."""
    point = namedtuple("Point", ("x", "y"))
    line = namedtuple("Line", ("p1", "p2"))
    target = point(*target)

    inside = False
    # Build list of coordinate pairs
    # First, turn it into named tuples

    poly = map(lambda p: point(*p), poly)

    # Make two lists, with list2 shifted forward by one and wrapped around
    list1 = poly
    list2 = poly[1:] + [poly[0]]
    poly = map(line, list1, list2)

    for l in poly:
        p1 = l.p1
        p2 = l.p2

        if p1.y == p2.y:
            # This line is horizontal and thus not relevant.
            continue
        if max(p1.y, p2.y) < target.y <= min(p1.y, p2.y):
            # This line is too high or low
            continue
        if target.x < max(p1.x, p2.x):
            # Ignore this line because it's to the right of our point
            continue
        # Now, the line still might be to the right of our target point, but
        # still to the right of one of the line endpoints.
        rise = p1.y - p2.y
        run =  p1.x - p2.x
        try:
            slope = rise/float(run)
        except ZeroDivisionError:
            slope = float('inf')

        # Find the x-intercept, that is, the place where the line we are
        # testing equals the y value of our target point.

        # Pick one of the line points, and figure out what the run between it
        # and the target point is.
        run_to_intercept = target.x - p1.x
        x_intercept = p1.x + run_to_intercept / slope
        if target.x < x_intercept:
            # We almost crossed the line.
            continue

        inside = not inside

    return inside

if __name__ == "__main__":
    poly = [(2,2), (1,-1), (-1,-1), (-1, 1)]
    print point_in_polygon((1.5, 0), poly)

rawDescription: Python: fill a polygon area in an array

description: Python: fill a polygon area in an array


code:
import random
from scipy.linalg import toeplitz, det
import numpy as np,numpy.random

class si:
    #cache of info for toeplitz matrix construction
    indx = None
    l = None

def xtoeplitz(c,r):
    vals = np.concatenate((r[-1:0:-1], c))
    if si.indx is None or si.l != len(c):
        a, b = np.ogrid[0:len(c), len(r) - 1:-1:-1]
        si.indx = a + b
        si.l = len(c)
    # `indx` is a 2D array of indices into the 1D array `vals`, arranged so
    # that `vals[indx]` is the Toeplitz matrix.
    return vals[si.indx]

def doit():
    for n in xrange(1,25):
        rankzero = 0
        si.indx=None

        for repeats in xrange(5000):

            column = np.random.randint(0,2,n)
            #column=[random.choice([0,1]) for x in xrange(n)] # original code

            row = np.r_[column[0], np.random.randint(0,2,n-1)]
            #row=[column[0]]+[random.choice([0,1]) for x in xrange(n-1)] #origi

            matrix = xtoeplitz(column, row)
            #matrix=toeplitz(column,row) # original code

            #if  (np.linalg.matrix_rank(matrix) < n): # original code
            if  np.abs(det(matrix))<1e-4: # should be faster for small matrices
                rankzero += 1
        print n, (rankzero*1.0)/50000

rawDescription: Speed up random matrix computation

description: Speed up random matrix computation


code:
from locations.models import City
from django.db import transaction
from django.template.defaultfilters import slugify
counter = 0
with transaction.atomic():
    for obj in City.objects.all():
        counter = counter + 1
        if counter % 1000 == 0:
            print counter
        slug = slugify(obj.city_name) + "-" + slugify(obj.region)
        City.objects.filter(pk=obj.pk).update(city_name_slug=slug)

rawDescription: Inserting large amounts of data in Django via the shell


code:
class DictAsMember(dict):
    def __getattr__(self, name):
        value = self[name]
        if isinstance(value, dict):
            value = DictAsMember(value)
        elif isinstance(value, list):
            value = [DictAsMember(element)
                     if isinstance(element, dict)
                     else element
                     for element in value]

        return value

my_dict = DictAsMember()
my_dict["property"] = {'sub': 1}
print my_dict.property.sub    # 1 will be printed

my_dict = DictAsMember()
my_dict["property"] = [{'name': 1}, {'name': 2}]
print my_dict.property[1].name    # 2 will be printed

rawDescription: Overwrite {} in python

description: Overwrite {} in python


code:
path = ""
count = 0
while count < 10 and path == "":
    p1 = subprocess.Popen(["df", "-h"], stdout=subprocess.PIPE)
    p2 = subprocess.Popen(["grep", device_file], stdin=p1.stdout, stdout=subprocess.PIPE)
    p3 = subprocess.Popen(["awk", "{ print $6 }"], stdin=p2.stdout, stdout=subprocess.PIPE)
    path = p3.communicate()[0]
    count += 1
    if path == "":
        time.sleep(1)
print path

rawDescription: problem printing mount point of usb in python

description: Problem printing mount point of usb in python


rawDescription: python iterating through multiple lists and returning a random.choice

description: Python iterating through multiple lists and returning a random.choice


code:
import inspect

def retrieve_name(var):
    local_vars = inspect.currentframe().f_back.f_locals.items()
    return [var_name for var_name, var_val in local_vars if var_val is var]

a = [1,2,3]
b = [2,4,6,4]
c = [4,6,4]

a_list = (a,b,c)
a_names = []

for x in a_list:
    a_names += (retrieve_name(x)[0])

print a_names

rawDescription: How to make list variable names into strings

description: Make list variable names into strings


code: print ''.join([chr(ord(letter) + 5) for letter in word])

rawDescription: Remove whitespace in for loop

description: Remove whitespace in for loop


code:
import threading
import urllib2
import time

start = time.time()
urls = ["http://www.google.com", "http://www.apple.com", "http://www.microsoft.com", "http://www.amazon.com", "http://www.facebook.com"]

def fetch_url(url):
    urlHandler = urllib2.urlopen(url)
    html = urlHandler.read()
    print "'%s\' fetched in %ss" % (url, (time.time() - start))

threads = [threading.Thread(target=fetch_url, args=(url,)) for url in urls]
for thread in threads:
    thread.start()
for thread in threads:
    thread.join()

print "Elapsed Time: %s" % (time.time() - start)

rawDescription: Python - very simple multithreading parallel URL fetching (without queue)


code:
n= input()
plant=map(int,raw_input().split())
count=0

# At each iteration the following loop updates the plant
# list by removing weak plants and count counts no. of steps
# temp temporarily stores surviving plants

while(True):
    temp=[plant[0]]
    for i in range(1,len(plant)):
        if plant[i]<=plant[i-1]:
             temp.append(plant[i])

# If temp and plants have same len, then no plant has
# been removed, so, the process ended.
    if(len(temp)==len(plant)):    break

    plant=temp
    count+=1

print count

rawDescription: HackerRank Challenge : Find total number of days Plants die


code:
UA = 'Mozilla/5.0 (X11; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0'
url = 'https://www.endomondo.com/rest/v1/users/20261627/workouts/526622897'
r = requests.get(url, headers={'User-Agent': UA})
print len(r.content)

rawDescription: Python3 requests module or urllib.request module both retrieving incomplete json


code:
from Tkinter import *
class App(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.makeMenu(master)

    def makeMenu(self, parent):
        menuBar = Menu(parent)
        parent.configure(menu=menuBar)
        fileMenu = Menu(menuBar)
        sourceMenu = Menu(menuBar)
        menuBar.add_cascade(label="File", menu=fileMenu)
        fileMenu.add_command(label="Load")
        fileMenu.add_cascade(label="Source", menu=sourceMenu)
        self._listi = ["who","what","where","how"]
        for l in self._listi:
            sourceMenu.add_checkbutton(label=l, command=lambda arg0=l: self.test(arg0))
        fileMenu.add_command(label="Save")
        fileMenu.add_command(label="Exit", command=self.quit)

    def test(self, arg0):
        print "you clicked on", arg0

# create app
root = Tk()
myApp = App(root)

# start program
myApp.mainloop()

rawDescription: Python Tkinter Submenu Radio Buttons

description: Python Tkinter Submenu Radio Buttons


code:
data=''.join(sys.stdin)
print data

rawDescription: How does raw_input treat ENTER or \n

description: How does raw_input treat ENTER or \n


code:
TheCurrentDate = TheCurrentDateTime.date()
if eventDateTime.date() == TheCurrentDate:
    print "Your event is today."

rawDescription: Time Checking in Python

description: Time Checking in Python


code:
import subprocess

s = """/usr/local/sbin/kamctl fifo profile_get_size myprofile | awk -F ':: ' '{print $2}'"""

p = subprocess.Popen(s, shell=True, stdout=subprocess.PIPE)

out, err = p.communicate()

print 'out: %r' % out
print 'err: %r' % err
print 'status: %r' % p.returncode

rawDescription: Processing output from cmdline via a Python script


code:
#!/usr/bin/env python
import csv
afile = open('<directory>/*.csv', 'r+')
csvReader1 = csv.reader(afile)
for i in range(2):
    print csvReader1.next()

rawDescription: Print the first two rows of a csv file to a standard output


code:
from gnupg import GPG

s = '''-----BEGIN PGP PUBLIC KEY BLOCK ... END PGP PUBLIC KEY BLOCK-----'''
gpg_key = GPG().import_keys(s)
gpg_msg = GPG().encrypt('message', gpg_key.results[0]['fingerprint'])

print gpg_msg

rawDescription: How to import PGP and GPG keys and export encrypted text in Python?

description: Import PGP and GPG keys and export encrypted text in Python


code:
import wmi
c = wmi.WMI ()

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

rawDescription: List running processes on 64-bit Windows


code:
class Action(object):
    pass

class HealthAction(Action):
    triggers = ("life", "health")

    def perform(self):
        print health

class Room(object):
    actions = [HealthAction()] # this list contains actions applicable to all rooms
    room_actions = [] # this list contains room-specific actions

    def enter(self):
        while True:
            response = raw_input("What would you like to do? ")
            for action in self.actions + self.room_actions:
                if any(x in response for x in action.triggers):
                    action.perform()

class RoomOne(Room):
    room_actions = [] # you can add room-specific actions here

rawDescription: How to add particular raw_input responses to subclasses in Python

description: Add particular raw_input responses to subclasses in Python


code:
# items in section 'NODE': key, value pairs
for key, value in config.items('NODE'):
  if value == WHAT_I_NEED:
    print key

rawDescription: ConfigParser Get Key on given Value

description: ConfigParser Get Key on given Value


code:
d = { 'd1': 5, 'd4': 4 }
print min(d, key=d.get)

rawDescription: using the min function

description: Using the min function


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

xi = np.array([0., 0.5, 1.0])
yi = np.array([0., 0.5, 1.0])
zi = np.array([[0., 1.0, 2.0],
               [0., 1.0, 2.0],
               [-0.1, 1.0, 2.0]])

v = np.linspace(-.1, 2.0, 15, endpoint=True)
plt.contour(xi, yi, zi, v, linewidths=0.5, colors='k')
plt.contourf(xi, yi, zi, v, cmap=plt.cm.jet)
x = plt.colorbar(ticks=v)
print x
plt.show()

rawDescription: Python/Matplotlib - Colorbar Range and Display Values


code:
while True:
    filename = raw_input(' filename: ')
    try:
        lines = [line.strip() for line in open(filename)]
        print lines
        break
    except IOError as e:
        print 'No file found'
        continue

rawDescription: inputting a words.txt file python 3

description: Inputting a words.txt file python 3


code:
import pygame

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

class Background():

    def __init__(self, screen):
        self.screen = screen

        self.timer = 0
        self.color = 0
        self.up = True # up or down

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

    def change(self):

        if self.timer == 15: # 15 frames for UP and 15 frames for DOWN
            self.timer = 0
            self.up = not self.up

        self.timer += 1

        if self.up:
            self.color += 10
        else:
            self.color -= 10

        print self.up, self.color

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

    def draw(self):
        self.screen.fill( (self.color, self.color, self.color) )

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

class Game():

    def __init__(self):
        pygame.init()

        self.screen = pygame.display.set_mode((800,600))

        self.background = Background(self.screen)

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

    def run(self):

        clock = pygame.time.Clock()

        RUNNING = True

        while RUNNING:

            # ----- events -----

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    RUNNING = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        RUNNING = False

            # ----- changes -----

            self.background.change()

            # ----- draws ------

            self.background.draw()

            pygame.display.update()

            # ----- FPS -----

            clock.tick(30)

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

        pygame.quit()

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

Game().run()

rawDescription: How to make surface/image blink 2 times a second with pygame Python

description: Make surface/image blink 2 times a second with pygame Python


code:
import operator

# custom function

def diff_squares(a, b):
    return a ** 2 - b ** 2

operators = {'add' : operator.add,
             'sub' : operator.sub,
             'mul' : operator.mul,
             'div' : operator.div,
             'dos' : diff_squares}

data = raw_input('Question: ')
data = data.split()

a = int(data[0]) # one number
b = int(data[1]) # two number

answer = operators[data[2]](a, b)
print 'Solution:', answer

rawDescription: What pattern is best suited for executing different actions (depending on user input) in python


code:
for field in SettingsForm().fields:
    print field.initial

rawDescription: Get initial data of form fields

description: Get initial data of form fields


code:
import gtk

def new_button_pressed(widget, data=None):
    ## data is a reference to the dialog widget
    ## do some validation, in this case the folder has to start with "/home"
    if not(data.get_filename().startswith("/home")):
        ## force cancel response
        print "failed validation..."
        data.response(gtk.RESPONSE_CANCEL)
    else:
        print "success validation..."
        data.response(gtk.RESPONSE_OK)

newDialog = gtk.FileChooserDialog(
title="Create new folder", parent=None,
action=gtk.FILE_CHOOSER_ACTION_CREATE_FOLDER,
buttons= (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL), backend=None)
new_button = newDialog.add_button(gtk.STOCK_NEW, gtk.RESPONSE_OK)
new_button.connect("pressed", new_button_pressed, newDialog)
newDialog.run()

rawDescription: pyGTK/GTK filechooser - Is it possible to validate user entry before file or folder is written to disc?

description: PyGTK/GTK filechooser - Is it possible to validate user entry before file or folder is written to disc


code:
import numpy as np
grid = np.random.random((5,5)) * 100
z = np.array([0, 10, 20, 60, 100.])
replace_value = np.array([np.nan, 5., 15., 40., 80.])
grid = replace_value[z.searchsorted(grid)]
print grid

rawDescription: replace floats from value a to b with value c in an large array

description: Replace floats from value a to b with value c in an large array


rawDescription: How can I create a numpy .npy file in place on disk?

description: Create a numpy .npy file in place on disk


code:
import time
from multiprocessing import Pool, Value, Lock
import decimal
import random

number = Value('i', 0)
lock = Lock()
lists = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h','i', 'j', 'k']

def thefunction(listi):
    time.sleep(decimal.Decimal(random.random()))
    with lock:
        number.value += 1
        print time.strftime('%H:%M:%S'), number.value, listi

def initializer(*args):
    global number, lock
    number, lock = args

pool = Pool(4, initializer, (number, lock))
pool.map(thefunction, lists)
print number.value

rawDescription: Python Increment a number in a function called with multiprocessing.pool.map()


code:
import datetime

t1 = '12:00:00'
t2 = '13:00:00'

print datetime.strptime(t2, "%H:%M:%S") - datetime.strptime(t1, "%H:%M:%S")
# datetime.timedelta(0, 3600)

rawDescription: Calculating times between two datetime strings


code:
import threading
import Queue
import datetime
import time

class myThread(threading.Thread):
    def __init__(self, in_queue, out_queue):
        threading.Thread.__init__(self)
        self.in_queue = in_queue
        self.out_queue = out_queue

    def run(self):
        while True:
            item = self.in_queue.get() #blocking till something is available in the queue
            #run your lines of code here
            processed_data = item + str(datetime.now()) + 'Processed'
            self.out_queue.put(processed_data)

IN_QUEUE = Queue.Queue()
OUT_QUEUE = Queue.Queue()

#starting 10 threads to do your work in parallel
for i in range(10):
    t = myThread(IN_QUEUE, OUT_QUEUE)
    t.setDaemon(True)
    t.start()

#now populate your input queue
for i in range(3000):
    IN_QUEUE.put("string to process")

while not IN_QUEUE.empty():
    print "Data left to process - ", IN_QUEUE.qsize()
    time.sleep(10)

#finally printing output
while not OUT_QUEUE.empty():
    print OUT_QUEUE.get()

rawDescription: Running two lines of code in python at the same time?


code:
from StringIO import StringIO

from selenium import webdriver
import lxml.etree

#
# Make sure that your chromedriver is in your PATH, and use the following line...
#
driver = webdriver.Chrome()
#
# ... or, you can put the path inside the call like this:
# driver = webdriver.Chrome("/path/to/chromedriver")
#

parser = lxml.etree.HTMLParser()

driver.get("http://google.com")

# We get this element only for the sake of illustration, for the tests later.
input_from_find = driver.find_element_by_id("gbqfq")
input_from_find.send_keys("foo")

html = driver.execute_script("return document.documentElement.outerHTML")
tree = lxml.etree.parse(StringIO(html), parser)

# Find our element in the tree.
field = tree.find("//*[@id='gbqfq']")
# Get the XPath that will uniquely select it.
path = tree.getpath(field)

# Use the XPath to get the element from the browser.
input_from_xpath = driver.find_element_by_xpath(path)

print "Equal?", input_from_xpath == input_from_find
# In JavaScript we would not call ``getAttribute`` but Selenium treats
# a query on the ``value`` attribute as special, so this works.
print "Value:", input_from_xpath.get_attribute("value")

driver.quit()

rawDescription: Need to dump entire DOM tree with element id from selenium server


code:
import numpy as np
import itertools

def cartesian_chunked(n, n_items=4, chunk_dim=3):
    if n > chunk_dim:
        p = n - chunk_dim
        q = chunk_dim
        outer = itertools.product(*([range(n_items)] * (n - chunk_dim)))
    else:
        p = 0
        q = n
        def outer_iter():
            yield ()
        outer = outer_iter()

    chunk = np.zeros([n_items**q, n], dtype=int)
    chunk[:,p:] = np.indices(np.repeat([n_items], q)).reshape(q, -1).T
    for seq in outer:
        chunk[:,:p] = seq
        yield chunk

def compute_energy(indices):
    base_energies = np.array([-1, 4, 8, 2.4])
    return (base_energies[indices]).sum(axis=1)

max_energy = 0
max_config = None

# try out 4**10 ~ 1e6 combinations, in chunks of 4**8
for chunk in cartesian_chunked(n=10, n_items=4, chunk_dim=8):
    energies = compute_energy(chunk)
    j = np.argmax(energies)
    if energies[j] > max_energy:
        max_energy = energies[j]
        max_config = chunk[j].copy() # copy! the chunk is modified

print max_energy
print max_config

rawDescription: How can I quickly run a function over an array of every possible array of length L with given possible elements?

description: Quickly run a function over an array of every possible array of length L with given possible elements


code:
def izpisi(polje):
    return '\n'.join([ # inserts '\n' between the lines
        ''.join(map(str, sublst)) # converts list to string of numbers
        for sublst in zip(*polje) # zip(*...) transposes your matrix
    ][::-1]) # [::-1] reverses the list

polje = [[1, 2, 4, 4], [4, 1, 4, 2], [2, 1, 4, 3], [2, 4, 2, 3], [1, 2, 3, 4]]
print izpisi(polje)

rawDescription: Returning for loop inside a while loop in python


code:
d = {1: {1: '[]', 2: '[1]'}, 2: {1: '[]', 3: '[1]'}, 3: {1: '[]'}}
p = {}
for from_, subdict in d.iteritems():
    for to, value in subdict.iteritems():
        p.setdefault(to, {})
        p[to][from_] = value
print p

rawDescription: Invert dictionary/directed graph with edge labels


code:
import requests
from bs4 import BeautifulSoup

url = "http://uswest.ensembl.org/Gallus_gallus/Gene/Summary?db=core;g=ENSGALG00000016955;r=1:165302186-165480795;t=ENSGALT00000027404"
r = requests.get(url, timeout=5)
html = BeautifulSoup(r.text)
description = html.find("div", {'class': "rhs"})
print description.text

rawDescription: BeautifulSoup returning different html than view source


code:
from urllib import unquote

def unquote_u(source):
    result = unquote(source)
    if '%u' in result:
        result = result.replace('%u','\\u').decode('unicode_escape')
    return result

print unquote_u('Tan%u0131m')

rawDescription: Encode unicode special character (symbol) in Python?


code:
class MenuScreen(Screen):
    def val(self):
        Clock.schedule_once(self.my_callback_timer1, 5)

    def my_callback_timer1(self, interval):
        print "5 sec code executed"
        Clock.schedule_interval(self.my_callback_timer2, 1)
    def my_callback_timer2(self, interval):
        print "1 sec code executed"

rawDescription: Issue in controlling 2 clock schedules in Kivy


code:
import numpy as np
import scipy.spatial.distance as distance

points = np.array([(472765.09, 6191522.78, 13.0), (472764.82, 6191524.09, 9.0), (472763.8, 6191525.68, 8.0), (472764.07, 6191524.39, 16.0)])

dist = distance.pdist(points)
print dist.max()
print dist.min()
print np.median(dist)
print np.average(dist)

rawDescription: Minimum, mean and maximum distance between points 3-D in Python


code:
filename=raw_input("enter the file name: ")
with open(filename) as f :
    for line in f:
       for word in line.split() :
           if word == word[::-1]:
               print word

rawDescription: Reversing a text and find same words - python


code:
import glob

fname = glob.glob("*4564536_01*/xyz.txt")[0]
with open(fname) as fp:
    print fp.read()

rawDescription: How to open a file through python with folder name a regular expression?

description: Open a file through python with folder name a regular expression


code:
def myfunc():
    for i in range(1, 1001):
        for i2 in range(i, 1001):
            for i3 in range(i2, 1001):
                if i*i + i2*i2 == i3*i3 and i + i2 + i3 == 1000:
                    print i*i2*i3
                    return # Exit the function (and stop all of the loops)
myfunc() # Call the function

rawDescription: Python - `break` out of all loops

description: Python - `break` out of all loops


code:
def new_task():
    while True:
        temp = open(filename, 'a')
        new_entry = raw_input('Enter New Task: ')
        if new_entry == 'exit' or new_entry == 'quit':
            break
        if new_entry == 'preview':
            print '\n'
            preview()
            break
        temp.write(new_entry + '\n')
        temp.close()

rawDescription: Show updated text file in python

description: Show updated text file in python


code:
chr = "*"
size = 5
row = 1
while row <= size:
    col = size - row + 1
    while col <= size:
        print ' ',
        col = col + 1
    col = 0
    while col <= size-row:
        print chr,
        col = col + 1
    row = row + 1
    print ''
print ''

rawDescription: How do I draw a reverse triangle using characters and nested while loops in Python code?

description: Draw a reverse triangle using characters and nested while loops in Python code


code:
class Human(object):
    def __init__(self, name):
        self.name = name

    def __add__(self, other):
        return '{0} {1}'.format(self.name, other.name)

    def __mul__(self, other):
        return self.name * len(other.name)

    def __str__(self):
        return self.name

bob = Human('Bob')
sam = Human('Sam')

print sam + bob # calls __add__
print sam * bob # calls __mul__

print bob # calls __str__

rawDescription: How do I get the possible "self attributes" of a Python class?

description: Get the possible "self attributes" of a Python class


code:
for track in soup.findAll('track'):
    print track.title.string
    print track.location.string

rawDescription: Beautiful soup how print a tag while iterating over it


code:
#!/usr/bin/python

import sys

print 'Argument List:', str(sys.argv)

rawDescription: Python passing values from command line

description: Python passing values from command line


code:
class User(object):
    GENDER_MALE = 'mr'
    GENDER_FEMALE = 'ms'
    def __init__(self, **kwargs):
        valid_keys = ["title", "first_name", "last_name", "is_guest", "company_name", "mobile", "landline", "email", "password", "fax", "wants_sms_notification", "wants_email_notification", "wants_newsletter","street_address"]
        for key in valid_keys:
            self.__dict__[key] = kwargs.get(key)

x = User(first_name="Kevin", password="hunter2")
print x.first_name, x.password, x.mobile

rawDescription: Is passing too many arguments to the constructor considered an anti-pattern?


code:
import cv2
import sys
from PySide import QtGui, QtCore
from threading import Thread

class MainWindow(QtGui.QMainWindow):
    def __init__(self, cam=0, parent=None):
        super(MainWindow, self).__init__(parent)

        self.title = "Image"

        widget = QtGui.QWidget()
        self.layout = QtGui.QBoxLayout(QtGui.QBoxLayout.LeftToRight)

        self.image = QtGui.QLabel()

        self.layout.addWidget(self.image)
        self.layout.addStretch()

        self.setCentralWidget(widget)
        widget.setLayout(self.layout)

        self.setMinimumSize(640, 480)
        self.frame = cv2.imread("image.jpg")

        try:
            self.height, self.width = self.frame.shape[:2]

            img = QtGui.QImage(self.frame, self.width, self.height, QtGui.QImage.Format_RGB888)
            img = QtGui.QPixmap.fromImage(img)
            self.image.setPixmap(img)

        except:
            pass

    def closeEvent(self, event):
        cv2.destroyAllWindows()
        event.accept()

if __name__ == "__main__":
    print cv2.__version__
    print (sys.version)
    app = QtGui.QApplication(sys.argv)
    window = MainWindow(0)
    window.show()
    sys.exit(app.exec_())

rawDescription: I used opencv to convert a picture to grayscale，how to display the picture on pyqt4？


code:
import os
print os.path.abspath(os.curdir)
os.chdir("..")
print os.path.abspath(os.curdir)

rawDescription: Moving up one directory in Python

description: Moving up one directory in Python


code:
separators = [u"।", u",", u"."]
text = open("indiantext.txt").read()
#This converts the encoded text to an internal unicode object, where
# all characters are properly recognized as an entity:
text = text.decode("utf-8")

#this breaks the text on the white spaces, yielding a list of words:
words = text.split()

counter = 1

output = ""
for word in words:
    #if the last char is a separator, and is joined to the word:
    if word[-1] in separators and len(word) > 1:
        #word up to the second to last char:
        output += word[:-1] + u"(%d) " % counter
        counter += 1
        #last char
        output += word[-1] +  u"(%d) " % counter
    else:
        output += word + u"(%d) " % counter
    counter += 1

print output

rawDescription: C++ - How to read Unicode characters( Hindi Script for e.g. ) using C++ or is there a better Way through some other programming language?


code:
class A(object):

    def __init__(self):
        self.a = 1
        self.b = 1

    @property
    def a_plus_b(self):
        return self.a + self.b

foo = A()
print foo.a_plus_b # prints "2"
foo.a = 3
print foo.a_plus_b # prints "4"
foo.b = 4
print foo.a_plus_b # prints "7"

rawDescription: Python - Set class property to depend on values of other properties in the same class


code:
import sys
import zipfile

from bs4 import BeautifulSoup

with zipfile.ZipFile(sys.argv[1], 'r') as zfp:
    with zfp.open('word/document.xml') as fp:
        soup = BeautifulSoup(fp.read(), 'xml')

print soup

rawDescription: How to efficiently remove table from docx/xml and extract text

description: Efficiently remove table from docx/xml and extract text


code:
def stream_session(gpsData, session, lock):
    for report in session:
        if report.get("class") == "TPV":
            print ":"
            lock.acquire()
            fill_gpsdata(gpsData, session, report)
            lock.release()   # better to call the method
            print "::"

rawDescription: Python threads and Lock : Deadlock

description: Python threads and Lock : Deadlock


code:
def perm_generator(lst):
    res = []
    if len(lst) == 1:
        return [lst]
    else:
        for i in range(len(lst)):
            for perm in perm_generator(lst[:i] + lst[i+1:]):
                res.append([lst[i]] + perm)
    return res

gen = perm_generator([1,2,3])
print gen # prints [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]

rawDescription: How does 'yield' work in this permutation generator?


code:
for line in itertools.islice(infile, 9, None):
    print line

rawDescription: Open with 'rU' not recognizing endlines

description: Open with 'rU' not recognizing endlines


code:
import sys

class VirtualModule(object):

   def hello(self):
      return 'Hello World!'

class CustomImporter(object):

   virtual_name = 'my_virtual_module'

   def find_module(self, fullname, path):
      """This method is called by Python if this class
         is on sys.path. fullname is the fully-qualified
         name of the module to look for, and path is either
         __path__ (for submodules and subpackages) or None (for
         a top-level module/package).

         Note that this method will be called every time an import
         statement is detected (or __import__ is called), before
         Python's built-in package/module-finding code kicks in."""

      if fullname ==  self.virtual_name:

         # As per PEP #302 (which implemented the sys.meta_path protocol),
         # if fullname is the name of a module/package that we want to
         # report as found, then we need to return a loader object.
         # In this simple example, that will just be self.

         return self

      # If we don't provide the requested module, return None, as per
      # PEP #302.

      return None

   def load_module(self, fullname):
      """This method is called by Python if CustomImporter.find_module
         does not return None. fullname is the fully-qualified name
         of the module/package that was requested."""

      if fullname != self.virtual_name:
         # Raise ImportError as per PEP #302 if the requested module/package
         # couldn't be loaded. This should never be reached in this
         # simple example, but it's included here for completeness. :)
         raise ImportError(fullname)

      # PEP#302 says to return the module if the loader object (i.e,
      # this class) successfully loaded the module.
      # Note that a regular class works just fine as a module.
      return VirtualModule()

if __name__ == '__main__':

   # Add our import hook to sys.meta_path
   sys.meta_path.append(CustomImporter())

   # Let's use our import hook
   import my_virtual_module
   print my_virtual_module.hello()

rawDescription: Lazy-loading modules in python

description: Lazy-loading modules in python


code:
# Snippet imported from snippets.scrapy.org (which no longer works)

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request

from selenium import selenium

class SeleniumSpider(CrawlSpider):
    name = "SeleniumSpider"
    start_urls = ["http://www.domain.com"]

    rules = (
        Rule(SgmlLinkExtractor(allow=('\.html', )),
        callback='parse_page',follow=True),
    )

    def __init__(self):
        CrawlSpider.__init__(self)
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*chrome", "http://www.domain.com")
        self.selenium.start()

    def __del__(self):
        self.selenium.stop()
        print self.verificationErrors
        CrawlSpider.__del__(self)

    def parse_page(self, response):
        item = Item()

        hxs = HtmlXPathSelector(response)
        #Do some XPath selection with Scrapy
        hxs.select('//div').extract()

        sel = self.selenium
        sel.open(response.url)

        #Wait for javscript to load in Selenium
        time.sleep(2.5)

        #Do some crawling of javascript created content with Selenium
        sel.get_text("//div")
        yield item

rawDescription: Executing Javascript Submit form functions using scrapy in python


code:
def func(my_list, z):

    if z == len(my_list):
        return something
    else:
        # do something else
        return func(my_list, z+1)

z = someValue
print func(my_list, z)

rawDescription: How do I turn a while loop into a recursive method?

description: Turn a while loop into a recursive method


code:
class CommonMeasurement(models.Model):
    timestamp = models.DateTimeField()
    value = models.FloatField()
    run = models.ForeignKey(Run)

    objects = models.Manager()
    analyzes = managers.MeasureStatManager()

    def save(self, **kwargs):
        self.clean()
        return super(CommonMeasurement, self).save(**kwargs)

    def clean(self):
        super(CommonMeasurement, self).clean()
        print 'here we go'
        if self.timestamp < self.run.timestamp_start or self.timestamp > self.run.timestamp_end:
            raise django_excetions.ValidationError('Measurement is outside the run')

rawDescription: Django doesn't call model clean method

description: Django doesn't call model clean method


code:
cell = tbl.cell(3, 3)
paragraphs = cell.textframe.paragraphs
for paragraph in paragraphs:
    for run in paragraph.runs:
        print run.text

rawDescription: Reading text values in a PowerPoint table using pptx?


code:
class Node(object):
  def __init__(self, value, left=None, right=None):
    self.value = value
    self.left = left
    self.right = right

def traverse(rootnode):
  thislevel = [rootnode]
  while thislevel:
    nextlevel = list()
    for n in thislevel:
      print n.value,
      if n.left: nextlevel.append(n.left)
      if n.right: nextlevel.append(n.right)
    print
    thislevel = nextlevel

t = Node(1, Node(2, Node(4, Node(7))), Node(3, Node(5), Node(6)))

traverse(t)

rawDescription: Printing BFS (Binary Tree) in Level Order with _specific formatting_


code:
class EchoHandler(SocketServer.StreamRequestHandler):
    def handle(self):
        # I have no idea why you would print this but this is an example
        print( self.server.conn );
        print self.client_address, 'connected'

if __name__ == '__main__':
    SocketServer.ForkingTCPServer.allow_reuse_address = 1

    server = SocketServer.ForkingTCPServer(('10.0.0.6', 4242), EchoHandler)
    server.conn = MySQLdb.connect (host = "10.0.0.5",
                     user = "user", passwd = "pass", db = "database")
    # continue as normal

rawDescription: With python socketserver how can I pass a variable to the constructor of the handler class


code:
class DictWrapper(object):

    def __init__(self, x_names, y_names):
        self._dict = {}
        for x in x_names:
            self._dict[x] = {}
            for y in y_names:
                self._dict[x][y] = {}

    def __setitem__(self, key, val): # single key
        self._dict[key[0]][key[1]] = val

    def __getitem__(self, key): # single key
        return self._dict[key[0]][key[1]]

results_holder = DictWrapper(['x1', 'x2', 'x3'], ['y1', 'y2'])
results_holder[('x1','y2')] = 11
print results_holder[('x1','y2')]

# without () works too
results_holder['x1','y2'] = 11
print results_holder['x1','y2']

rawDescription: Python - wrapper for nested dictionary

description: Python - wrapper for nested dictionary


code:
from multiprocessing import Process
import os
import sys

def info(title):
    print title
    print 'module name:', __name__
    print 'parent process:', os.getppid()
    print 'process id:', os.getpid()

def f(name):
    sys.stdout = open(str(os.getpid()) + ".out", "w")
    info('function f')
    print 'hello', name

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    q = Process(target=f, args=('fred',))
    q.start()
    p.join()
    q.join()

rawDescription: Log output of multiprocessing.Process

description: Log output of multiprocessing.Process


code:
def foo():
    x = 'hello world'
    return x  # return 'hello world' would do, too

foo()
print x    # NameError - x is not defined outside the function

y = foo()
print y    # this works

x = foo()
print x    # this also works, and it's a completely different x than that inside
           # foo()

z = bar(x) # of course, now you can use x as you want

z = bar(foo()) # but you don't have to

rawDescription: Python - output from functions?

description: Python - output from functions


code:
import collections

def add_output(fct, value):
    def wrapped(*args, **kwargs):
        result = fct(*args, **kwargs)
        if isinstance(result, collections.Sequence):
            return tuple(result) + (value,)
        elif result is None:
            return value
        else: # non-None and non-sequence
            return (result, value)
    return wrapped

def f1(a): return a,a+1
def f2(a): pass
g1 = add_output(f1, 42)
g2 = add_output(f2, 42)
print g1(12)            # (12,13,42)
print g2(12)            # 42

rawDescription: automatic wrapper that adds an output to a function

description: Automatic wrapper that adds an output to a function


code:
class ObservableDict( dict ):
    def __init__( self, *args, **kw ):
        self.observers= []
        super( ObservableDict, self ).__init__( *args, **kw )
    def observe( self, observer ):
        self.observers.append( observer )
    def __setitem__( self, key, value ):
        for o in self.observers:
            o.notify( self, key, self[key], value )
        super( ObservableDict, self ).__setitem__( key, value )
    def update( self, anotherDict ):
        for k in anotherDict:
            self[k]= anotherDict[k]

class Watcher( object ):
    def notify( self, observable, key, old, new ):
        print "Change to ", observable, "at", key

w= Watcher()
a= ObservableDict( {'a':'hamburger', 'b':'fries', 'c':'coke'} )
a.observe( w )
b = {'b':'fries', 'c':'pepsi'}
a.update( b )

rawDescription: python dict update diff

description: Python dict update diff


code:
from random import randint
l=[]
l = [randint(0,3) for i in xrange(100)]
for i in xrange(0,100,10):
    print l[i:i+10].count(2)*10

10
10
20
20
10
0
30
20
30
20

rawDescription: Proportion of a certain number in a vector with 1 hundred position


code:
from collections import namedtuple

import random

Prize = namedtuple("Prize", ["left", "right" ])

this_prize = Prize("FirstPrize", "SecondPrize")

if random.random() > .5:
    choice = "left"
else:
    choice = "right"

#retrieve the value of "left" or "right" depending on the choice

print "You won", getattr(this_prize,choice)

rawDescription: Python: Accessing an attribute using a variable


code:
from scipy import misc
import glob

for image_path in glob.glob("/home/adam/*.png"):
    image = misc.imread(image_path)
    print image.shape
    print image.dtype

rawDescription: Importing PNG files into Numpy?

description: Importing PNG files into Numpy


code:
result = summaryStats()
print result

rawDescription: Python return issues

description: Python return issues


code:
#!/usr/bin/env python
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.sendto('\xff'*6+'\x00\x11\x22\x33\x44\x55'*16, ('192.168.0.255', 9))
print "Attempted to wake host"

rawDescription: program that will send a magic packet to a certian ip

description: Program that will send a magic packet to a certian ip


code:
import subprocess as sp

try:
    sp.check_output(['python', 'child.py'], stderr=sp.STDOUT)
except sp.CalledProcessError as err:
    print 'Child terminated abnormally with error %d, log:' % err.returncode
    print err.output

rawDescription: launching python script process from another script on Ubuntu: how to track status?

description: Launching python script process from another script on Ubuntu: how to track status


code:
msg = email.message_from_string(data[0][1])
addr = email.utils.parseaddr(msg['From'])[1]
domain = addr.split('@')[1]
if domain == "example.com":
    print "Verified requester"

rawDescription: Verify the domain of an e-mail address

description: Verify the domain of an e-mail address


code:
import re

sequence = 'i have -0.03 dollars in my hand'

m = re.search(r'(have )(-?\d+\.\d+)( dollars)', sequence)

print m.group(1) # captured group start from `1`.
print m.group(2)
print m.group(3)

rawDescription: Regex search to extract float from string. Python


code:
import datetime
(y,w,d ) = date.isocalendar()
#w will be the iso week number, d the day of week
(y2,wb,d2) = datetime.datetime(date.year,date.month,1).isocalendar()
wkcount = 1 if d>=d2 else 0
# you need to account whether your date is a weekday after the one of the first of the month or not
print "BYDAY=%d%s"%(w-wb+wkcount,date.strftime("%a").upper()[0:2])

rawDescription: How format date into BYDAY (iCalendar specification)


code:
import time
import numpy as np
from sklearn.ensemble import BaggingClassifier, RandomForestClassifier
from sklearn import datasets
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVC

iris = datasets.load_iris()
X, y = iris.data, iris.target

X = np.repeat(X, 100, axis=0)
y = np.repeat(y, 100, axis=0)
start = time.time()
clf = OneVsRestClassifier(SVC(kernel='linear', probability=True, class_weight='auto'))
clf.fit(X, y)
end = time.time()
print "Single SVC", end - start, clf.score(X,y)
proba = clf.predict_proba(X)

n_estimators = 10
start = time.time()
clf = OneVsRestClassifier(BaggingClassifier(SVC(kernel='linear', probability=True, class_weight='auto'), max_samples=1.0 / n_estimators, n_estimators=n_estimators))
clf.fit(X, y)
end = time.time()
print "Bagging SVC", end - start, clf.score(X,y)
proba = clf.predict_proba(X)

start = time.time()
clf = RandomForestClassifier(min_samples_leaf=20)
clf.fit(X, y)
end = time.time()
print "Random Forest", end - start, clf.score(X,y)
proba = clf.predict_proba(X)

rawDescription: Making SVM run faster in python

description: Making SVM run faster in python


code:
for x in xrange(cv.GetSize(image)[0]):
  for y in xrange(cv.GetSize(image)[1]):

    a = Point(x, y, heights_top[x / 2] * (cv.GetSize(image)[1] - y) + heights_bottom[x / 2] * y)
    b = Point()

    d.x = costy * (sintz * (a.y - c.y) + costz * (a.x - c.x)) - sinty * (a.z - c.z)
    d.y = sintx * (costy * (a.z - c.z) + sinty * (sintz * (a.y - c.y) + costz * (a.x - c.x))) + costx * (costz * (a.y - c.y) - sintz * (a.x - c.x))
    d.z = costx * (costy * (a.z - c.z) + sinty * (sintz * (a.y - c.y) + costz * (a.x - c.x))) - sintx * (costz * (a.y - c.y) - sintz * (a.x - c.x))

    mapX[y, x] = x + 100.0 * (d.x - e.x) * (e.z / d.z)
    mapY[y, x] = y + 100.0 * (d.y - e.y) * (e.z / d.z)

print
print 'Remapping original image using map...'

remapped = cv.CreateImage(cv.GetSize(image), 8, 3)
cv.Remap(image, remapped, mapX, mapY, cv.CV_INTER_LINEAR)

rawDescription: Distorting an image using a height map?

description: Distorting an image using a height map


code:
from shutil import copytree
from os.path import join

dst = '/Users/name/Desktop/Core/TEST'
with open('/Users/name/Desktop/Core/Core_List.txt') as f:
    for src in f:
       print src
       copytree(src, join(dst, src))

rawDescription: Python: read a text file and copy/paste directories listed in it to a new directory


code:
def _get_all_subclasses(cls):
  for scls in cls.__subclasses__():
    yield scls
    for scls in _get_all_subclasses(scls):
      yield scls

class Animal(object):

  @staticmethod
  def from_string(s):
    for cls in _get_all_subclasses(Animal):
      # Somehow pick the class based on the string... This is a really simple example...
      if cls.__name__ in s:
        return cls()
    raise ValueError('Bummer.  Animal has not been discovered.')

class Dog(Animal):
  pass

class Cat(Animal):
  pass

class Lion(Cat):
  pass

print Animal.from_string('is a Dog')
print Animal.from_string('is a Cat')
print Animal.from_string('Lions!!!')
print Animal.from_string('Lockness Monster')

rawDescription: Python - Getting subclass from input. Do I have to write a separate function?


code:
from mechanize import Browser, Item
br = Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent',
                  'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1)'
                  ' Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
url = 'http://www.matrixscience.com'\
      '/cgi/search_form.pl?FORMVER=2&SEARCH=MIS'
br.open(url)
br.select_form('mainSearch')

# get the actual control object instead of its contents
mods = br.find_control('MODS')
# add an item
item = Item(mods, {"contents": "Acetyl (N-term)", "value": "Acetyl (N-term)"})
# select it. if you don't, it doesn't appear in the output
# this is probably why MASTER_MODS appears empty
item.selected = True
print br['MODS']
# outputs: ['Acetyl (N-term)']

rawDescription: Mechanize how to add to a select list?

description: Mechanize how to add to a select list


code:
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 3001
MESSAGE = "asd"

sock = socket.socket(socket.AF_INET, # Internet
                 socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    print "received message:", data

rawDescription: UDP in Jython 2.7

description: UDP in Jython 2.7


code:
import subprocess
bat_text = "dir" # (text to write in bat file)
bat_file = open("program.bat","w") # (open file in write mode)
bat_file.write(bat_text) # (write bat_text on bat_file)
bat_file.close() # (close file)
command = "C:\program.bat" # (the location of bat file)
execute = subprocess.Popen(command, stdout=subprocess.PIPE)  # (exec the command)
output = execute.stdout.read() # (read output)
print output  # (print output)

rawDescription: Example of python script calling batch, then python script recieving batch script result?


code:
import xml.etree.ElementTree as ET
import sys
path = sys.argv[1]
xml_file = sys.argv[2]
tree = ET.parse(xml_file)
root = tree.getroot()
notes = root.find('notes')
for note in notes:
    found_any_path = False
    for place in note[:]:
        if place.attrib['file'] == path:
            found_any_path = True
    else:
        if not found_any_path:
            notes.remove(note)

print ET.tostring(root, encoding="UTF-8", method="xml")

rawDescription: bash/python script to retrieve nodes with given tag property

description: Bash/python script to retrieve nodes with given tag property


code:
from django.db.models import Count
most_common = Ingredient.objects.annotate(num_recipes=Count('recipe')).order_by('-num_recipes')[0]
print most_common.name

rawDescription: Django how to order by many to many occurences


code:
class Base(object):
    def m(self):
        return self.__class__.PARAM
    def __getstate__(self):
        global AutoSub
        AutoSub = self.__class__
        return (self.__dict__,self.__class__.PARAM)
    def __setstate__(self, state):
        self.__class__ = make_parameterized(state[1])
        self.__dict__.update(state[0])

def make_parameterized(param_value):
    class AutoSub(Base):
        PARAM = param_value
    return AutoSub

class AutoSub(Base):
    pass

if __name__ == "__main__":

    from pickle import dumps, loads

    a = make_parameterized("a")()
    b = make_parameterized("b")()

    print a.PARAM, b.PARAM, type(a) is type(b)
    a_p = dumps(a)
    b_p = dumps(b)

    del a, b
    a = loads(a_p)
    b = loads(b_p)

    print a.PARAM, b.PARAM, type(a) is type(b)

rawDescription: Pickle a dynamically parameterized sub-class


rawDescription: Dynamic Keyword Arguments in Python?

description: Dynamic Keyword Arguments in Python


code:
import re
x="""a, b, c
  #a, b, c"""

def repl(m):
    if m.group(1):
        return m.group(1)
    else:
        return ""

print re.sub(r"^(#.*)$|,",repl,x,flags=re.MULTILINE)

rawDescription: remove all comma characters from a line unless line starts with # using python regex

description: Remove all comma characters from a line unless line starts with # using python regex


code:
def foo():
    print "Hello, world!"
from StringIO import StringIO
import uncompyle2
out = StringIO()
uncompyle2.uncompyle("2.7", foo.func_code, out=out)
print s.getvalue()

rawDescription: Lost important .py file (overwritten as 0byte file), but the old version still LOADED IN IPYTHON as module -- can it be retrieved?


code:
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import *
import time

@pyqtSlot(dict)
def callback(param):
    print "in callback"
    print param

class Test(QThread):

    mySignal =  pyqtSignal(dict,name="mySignal")

    def __init__(self):
        QThread.__init__(self)

    def run(self):
        while 1:
            print "Test running"
            self.mySignal.emit({'a': 'b'})
            time.sleep(1)

if __name__ == "__main__":
    t = Test()
    t.start()
    t.mySignal.connect(callback)
    app = QApplication([])
    app.exec_()

rawDescription: Python QT emit SIGNAL is not invoking callback function


code:
class A:
    @classmethod
    def interfaceBasedMethod(cls):
        print "Want to delegate to overriding method in subclass"

    @classmethod
    def a(cls):
        cls.interfaceBasedMethod()

class B(A):
    @classmethod
    def interfaceBasedMethod(cls):
        print "Class B processes."

if __name__ == "__main__":
    b=B
    b.a()

rawDescription: Interface-Based Programming in Python?

description: Interface-Based Programming in Python


code:
my_dict = {'Mark': 2, 'Susan': 1, 'Billy': 3}
strings = 'Mark + (Susan * 4 * Billy)'

for k, v in my_dict.items():
    strings = strings.replace(k, str(v))

print eval(strings)

rawDescription: Parse a string expression

description: Parse a string expression


code:
buffer('foobar')
buffer('foobar')[3:5]
for c in buffer('foobar'):
  print c

rawDescription: Python 2.6 read from buffer

description: Python 2.6 read from buffer


code:
from twisted.web.client import getPage
from twisted.internet import reactor

class Getter(object):

    def __init__(self):
        self._sequence = 0
        self._results = []
        self._errors = []

    def add(self, url):
        d = getPage(url)
        d.addCallbacks(self._on_success, self._on_error)
        d.addCallback(self._on_finish)
        self._sequence += 1

    def _on_finish(self, *narg):
        self._sequence -= 1
        if not self._sequence:
            reactor.stop()

    _on_success = lambda self, *res: self._results.append(res)
    _on_error = lambda self, *err: self._errors.append(err)

    def run(self):
        reactor.run()
        return self._results, self._errors

g = Getter()
for url in ('http://www.google.com', 'http://www.yahoo.com', 'idontexist'):
    g.add(url)
results, errors = g.run()
print results
print errors

rawDescription: Twisted reactor starting multiple times in a single program?


code:
import subprocess
import sys

process=subprocess.Popen(["canCHandleIt.exe"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
res=process.communicate('2')
print res[0]

rawDescription: Why my scanf catch the order that I give from my Popen


code:
import pycurl
import sys
import StringIO

o = StringIO.StringIO()
h = StringIO.StringIO()

c = pycurl.Curl()
c.setopt(c.URL, 'http://stackoverflow.com/questions/21444891')
# c.setopt(c.HTTPPOST, values)
c.setopt(c.WRITEFUNCTION, o.write)
c.setopt(c.HEADERFUNCTION, h.write)
c.setopt(c.CONNECTTIMEOUT, 30)
c.setopt(c.AUTOREFERER,1)
c.setopt(c.FOLLOWLOCATION, 1)
c.setopt(c.COOKIEFILE, '')
c.setopt(c.TIMEOUT, 30)
c.setopt(c.USERAGENT, '')
c.perform()

h.seek(0)

location = ""

for l in h:
    if "Location" in l:
        location = l.split(": ")[-1]

print location

rawDescription: python pycurl get final url redirect

description: Python pycurl get final url redirect


code:
import urllib, urllib2
import time
import sys

query = ' '.join(sys.argv)

# encode params as a list of 2-tuples
params = ( ('from','ACC'), ('to', 'P_REFSEQ_AC'), ('format','tab'), ('query', query))
# url encode them
data = urllib.urlencode(params)
url = 'http://www.uniprot.org/mapping/'

# fetch the data
try:
    foo = urllib2.urlopen(url, data)
except urllib2.HttpError, e:
    if e.code == 503:
        # blah blah get the value of the header...
        wait_time = int(e.hdrs.get('Retry-after', 0))
        print 'Sleeping %i seconds...' % (wait_time,)
        time.sleep(wait_time)
        foo = urllib2.urlopen(url, data)

# foo is a file-like object, do with it what you will.
foo.read()

rawDescription: How can I talk to UniProt over HTTP in Python?

description: Talk to UniProt over HTTP in Python


code:
import string
import sys

def main():
    # get some stop words
    stopf = open('stop_words.txt', "r")
    stopwords = {}
    for s in stopf:
        stopwords[string.strip(s)] = 1

    file = open(sys.argv[1], "r")
    filedata = file.read()
    words=string.split(filedata)
    histogram = {}
    count = 0
    for word in words:
        word = string.strip(word, string.punctuation)
        word = string.lower(word)
        if word in stopwords:
            continue
        histogram[word] = histogram.get(word, 0) + 1
        count = (count+1) % 1000
        if count == 0:
            print '*',
    flist = []
    for word, count in histogram.items():
        flist.append([count, word])
    flist.sort()
    flist.reverse()
    for pair in flist[0:100]:
        print "%30s: %4d" % (pair[1], pair[0])

main()

rawDescription: How can I remove all instances of a single set item in set A from set B?

description: Remove all instances of a single set item in set A from set B


code:
data = list(csv.DictReader(open(file, 'rU')))
print "keys", data[0].keys()

rawDescription: Best data structure: arrays of dictionaries, arrays of objects?


code:
import time
from threadpool import ThreadPool
pool = ThreadPool(max_threads=25, catch_returns=True)

# decorate any functions you need to aggregate
# if you're pulling a function from an outside source
# you can still say 'func = pool(func)' or 'pool(func)()
@pool
def data(ID, start):
    for i in xrange(start, start+4):
        yield ID, i
        time.sleep(1)

# each of these calls will spawn a thread and return immediately
# make sure you do either pool.finish() or pool.iter()
# otherwise your program will exit before the threads finish
data("generator 1", 5)
data("generator 2", 10)
data("generator 3", 64)

for value in pool.iter():
    # this will print the generators' return values as they yield
    print value

rawDescription: How to efficiently iterate over multiple generators?

description: Efficiently iterate over multiple generators


code: map(lambda (i,v) : A[i].update(v), enumerate(map((lambda i: {'x':i['x'] + 1}), A)))

rawDescription: Update list of dictionaries elementwise in Python


code:
def unique_ips():
    f = open('log_file.txt','r')
    ips = set()
    for line in f:
        ip = line.split()[0]
        ips.add(ip)
    return ips

if __name__=='__main__':
    print unique_ips()

rawDescription: how to get unique ip address from list of ip address present in log file using python?

description: Get unique ip address from list of ip address present in log file using python


code:
input_file = csv.DictReader(open("contacts.csv"))
for row in input_file:
    for column in row.keys():
        if column.startswith("E-mail"):
           print row[column]

rawDescription: Python row.get(regex)

description: Python row.get(regex)


code:
import threading
import time

def compute_stuff(number):
   print str(number) + "start"
   time.sleep(10)
   print str(number) + "end"
   return [number*2, number*4]

threads = []
for i in range(5):
    t = threading.Thread(target=compute_stuff, args=(i,))
    threads.append(t)
    t.start()

rawDescription: Python - simplest way possible to run functions in parallel


code:
input_string = "Parenthesis: () Brackets: []"
temp_string = ''
odd_chars = ["[", "]", "(", ")", " "]
for odd_char in odd_chars:
    temp_string = input_string.replace(odd_char, "_")
    input_string = temp_string

print input_string

rawDescription: How do I cycle through a list of characters and replace them with "_" in Python?

description: Cycle through a list of characters and replace them with "_" in Python


rawDescription: Index of row where maximum value is located


code:
first = [{'a':'aaa','b':'ccc','c':'bbb','d':'ddd'},
         {'a':'bb','b':2,'c':1,'d':3},
         {'a':'cc','b':22,'c':11,'d':33}]

second = [{'a':'aaa','b':'bbb','c':'ccc','d':'ddd'},
          {'a':'bb','b':1,'c':2,'d':3},
          {'a':'cc','b':11,'c':22,'d':33}]

def compare(first,second):
    for a,b in zip(first, second):
        vals_a = list(a.values())
        vals_b = list(b.values())

        vals_a.sort()
        vals_b.sort()

        if vals_a != vals_b:
            return False

    return True

print compare(first,second)

rawDescription: Compare python list of dictionaries with tolerance on column order


code:
import signal
import time
import sys

please_stop = False
def setup_signal():
    def handler(x,y):
        global please_stop
        please_stop = True
    signal.signal(signal.SIGUSR1, handler)

def main_task():
    for i in range(10):
        print "Working hard on iteration #%d"%i
        time.sleep(1)
        if please_stop:
            print "Stopping now"
            sys.exit(0)

setup_signal()
main_task()

rawDescription: Efficient way to ask python to stop (controlled) from bash (/tmp/stop?)


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)
images = list(soup.findAll('img'))
print images[0]['src']

rawDescription: python : easy substring/parsing

description: Python : easy substring/parsing


code:
import xlrd
xls = xlrd.open_workbook(r'<path_to_your_excel_file>', on_demand=True)
print xls.sheet_names() # <- remeber: xlrd sheet_names is a function, not a property

rawDescription: How to obtain sheet names from XLS files without loading the whole file?

description: Obtain sheet names from XLS files without loading the whole file


code:
from fortran_code import fortran_function
import os

print "will run fortran function!"

# open 2 fds
null_fds = [os.open(os.devnull, os.O_RDWR) for x in xrange(2)]
# save the current file descriptors to a tuple
save = os.dup(1), os.dup(2)
# put /dev/null fds on 1 and 2
os.dup2(null_fds[0], 1)
os.dup2(null_fds[1], 2)

# *** run the function ***
fortran_function()

# restore file descriptors so I can print the results
os.dup2(save[0], 1)
os.dup2(save[1], 2)
# close the temporary fds
os.close(null_fds[0])
os.close(null_fds[1])

print "done!"

rawDescription: Redirecting FORTRAN (called via F2PY) output in Python


rawDescription: wxPython: Ensure only one instance of a panel is open

description: WxPython: Ensure only one instance of a panel is open


code:
import glob

for filepath in glob.glob('/home/data/*.dat'):
    b = np.genfromtxt(filepath).T[1] #luminsity
    q = getmax(b)
    print filename, 'max b=', q

rawDescription: how to execute a command for all .dat files in a directory

description: Execute a command for all .dat files in a directory


code:
import os
import json

def path_to_dict(path):
    d = {'name': os.path.basename(path)}
    if os.path.isdir(path):
        d['type'] = "directory"
        d['children'] = [path_to_dict(os.path.join(path,x)) for x in os.listdir\
(path)]
    else:
        d['type'] = "file"
    return d

print json.dumps(path_to_dict('.'))

rawDescription: Represent directory tree as JSON

description: Represent directory tree as JSON


code:
result = [c[1] for b in z for c in b]
print result

rawDescription: Deriving a unique and ordered list from a complex dictionary


code:
today = datetime.datetime.today()#.strftime("%Y-%m-%d")
foo = (session.query(Foo)
        .options(joinedload(Foo.Bars))
        .filter(Foo.code=='MY_STRING')
        .one()
        )
prev_bars = [bar for bar in foo.Bars if bar.end_date < today]
curr_bars = [bar for bar in foo.Bars if bar.end_date >=today]
print foo
print prev_bars
print curr_bars

rawDescription: Two sqlalchemy queries return the same result even though they have different filters


code:
import re

lsta = ['The Fake Company','Fake Company Inc.','The Fake Company Store','Another.','Irrelevant','Not Included']
lstb = [['Fake','PersonA'], ['BCompany','PersonB'],['Another','PersonC'],['DCompany','PersonC'],['The Another Inc.','PersonC'], ['thisisareallylongstringandwontmatch', 'yeaaaaaaaah']]

Desired_ListA, Desired_ListB = [], []
for sublist in lstb:
    for company in lsta:
        if re.search(sublist[0], company):
            Desired_ListA.append(sublist)
        else:
            Desired_ListB.append(sublist)
print Desired_ListA
print Desired_ListB

rawDescription: How to return the sublists in lstb if they exist in lsta, while excluding certain words in the match?

description: Return the sublists in lstb if they exist in lsta, while excluding certain words in the match


code:
DIR_EXCLUDES = set(['.', '..'])
MASK = win32con.FILE_ATTRIBUTE_DIRECTORY | win32con.FILE_ATTRIBUTE_SYSTEM
REQUIRED = win32con.FILE_ATTRIBUTE_DIRECTORY
FindFilesW = win32file.FindFilesW

def get_dir_size(path):
    total_size = 0
    try:
        items = FindFilesW(path + r'\*')
    except pywintypes.error, ex:
        return total_size

    for item in items:
        total_size += item[5]
        if (item[0] & MASK == REQUIRED):
            name = item[8]
            if name not in DIR_EXCLUDES:
                total_size += get_dir_size(path + '\\' + name)

    return total_size

rawDescription: fast folder size calculation in Python on Windows

description: Fast folder size calculation in Python on Windows


code:
for row in table.findAll('tr', attrs = {'class':'odd'}) + table.findAll('tr', attrs = {'class':'even'}):
    for cell in row.findAll('td'):
        print cell

rawDescription: Pythonic way to iterate through loops

description: Pythonic way to iterate through loops


code:
my_list = [('key1', 'topic1', 'content1'), ('key2', 'topic1', 'content2'), ('key3', 'topic2', 'content3')]

result={}
for t in my_list:
    result.setdefault(t[1],[]).append(t[2])

print result

rawDescription: Dictionary with Multiple Values from a Nested List


rawDescription: how would i separate a string based on some pattern without loosing the match parameter in python?

description: How would i separate a string based on some pattern without loosing the match parameter in python


code:
d = {'a':['2', 'ova', 'no'], 'b':['23', 'movie', 'yes'], 'c':['5', 'show', 'yes'], 'd':['17', 'ova', 'yes'], 'e':['1', 'movie', 'no']}
sorted_items = sorted(d.items(), key=itemgetter(1))
for i,v in sorted_items:
    print i, v

rawDescription: Python Dictionary: Sorting Trouble

description: Python Dictionary: Sorting Trouble


code:
def p_decorate(func):
   def func_wrapper(name):
       return "
{0}
".format(func(name))
   return func_wrapper

@p_decorate
def get_text(name):
   return "lorem ipsum, {0} dolor sit amet".format(name)

print get_text("John")

# Outputs
lorem ipsum, John dolor sit amet

rawDescription: Is inheritance in functions possible in Python?


rawDescription: Forward slash not found in last string in line


code:
import ast

with open('module.py') as f:
    data = f.read()
    tree = ast.parse(data)
    elements = [el for el in tree.body if type(el) in (ast.Assign, ast.FunctionDef, ast.ClassDef)]

result = {}

for el in elements:
    if type(el) == ast.Assign:
        for t in el.targets:
            if type(el.value) == ast.Call:
                result[t.id] = el.value.func.id + '()'
            else:
                for attr in ['id', 'i', 's']:
                    try:
                        result[t.id] = getattr(el.value, attr)
                        break
                    except Exception as e:
                        pass
    elif type(el) == ast.FunctionDef:
        result[el.name] = '<function %s>' % el.name
    else:
        result[el.name] = '<class %s>' % el.name

print result
#

rawDescription: How to get a list of all non imported names in a Python module?

description: Get a list of all non imported names in a Python module


code:
from multiprocessing import Process,Queue

def do_threading(q,function,argument, timeout_in=1):

    # Start function as a process
    p = Process(target=function, args=(argument,q,))
    p.start()

    # Wait for 10 seconds or until process finishes
    p.join(timeout_in)
    print "time out"
    # If thread is still active
    if p.is_alive():
        print("running... let's kill it...")

        # print(q.get())

        # Terminate
        p.terminate()
        print "terminate"
        p.join()

def do_big_job(argument, q):

    # Do something with passed argument
    print(argument)

    # heavy computation
    result = 2**123
    # print("in child thread ",result)

    # Putting result in the queue for exchange
    q.put(result)

if __name__ == '__main__':
    q = Queue() # Creating the queue in the main allows you to access it anytime
    print("Main thread starting...")
    do_threading( q, do_big_job, "Child thread starting...", timeout_in=10)
    if q.empty():
        pass
    else:
        print(q.get()) # get your result here.

rawDescription: Not able to exchange object/ timeout a child process using multiprocessing.Process() Python


code: print unescape('at √s = 7 TeV with')

rawDescription: Python encoding - Nothing works

description: Python encoding - Nothing works


code:
class EchoHandler(asyncore.dispatcher_with_send):
    def setAddr(self, addr):
        self.addr = addr

    def handle_read(self):
        data = self.recv(8192)
        print '%s from %s' % (data, self.addr)
        self.send(data)

class EchoServer(asyncore.dispatcher):

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

    def handle_accept(self):
        pair = self.accept()
        if pair is None:
            pass
        else:
            sock, addr = pair
            print 'Incoming connection from %s' % repr(addr)
            handler = EchoHandler(sock)
            handler.setAddr(addr) #Set the address of the sender in the EchoHandler

rawDescription: python asyncore keep track of clients

description: Python asyncore keep track of clients


code:
import theano
import theano.tensor as T

x = T.dscalar('x')

def g():
    y1 = T.sqr(x)
    return y1

def composition():
    input = g()
    yComp = x * input
    return  theano.function([x], yComp)

tfunc = composition()
print tfunc(4)

rawDescription: How can I form a composite Function with Theano?

description: Form a composite Function with Theano


code:
import subprocess
p = subprocess.Popen(['snmptable', '-v', '2c', '-c', 'public', '-Ob', '172.16.67.240',    '.1.3.6.1.2.1.2.2'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print out

rawDescription: How to get SNMP table in Python

description: Get SNMP table in Python


code:
from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    pool = Pool(processes=4)
    n = 10000000
    inputs = xrange(n)
    # calc f output using 4 processes
    out_list = pool.map(f, inputs)
    # average
    print sum(out_list) / float(n)

rawDescription: parallel python: just run function n times

description: Parallel python: just run function n times


code:
import pwd, grp

user = ''
users = pwd.getpwall()
i=0
for p in users:
    print str(i) + ')' + str(p[0])
    i+=1

chose = int(raw_input("Select user from this list > "))
user = users[chose]
print
print '1) show user groups \n2) show user id'
print
chose = int(raw_input("Your choice > "))

if chose == 1:
    print grp.getgrgid(user.pw_gid).gr_name
else:
    print user.pw_uid

rawDescription: Choose specific user from list using `os.system()`


code:
import yaml
import yaml.constructor

try:
    # included in standard lib from Python 2.7
    from collections import OrderedDict
except ImportError:
    # try importing the backported drop-in replacement
    # it's available on PyPI
    from ordereddict import OrderedDict

class OrderedDictYAMLLoader(yaml.Loader):
    """
    A YAML loader that loads mappings into ordered dictionaries.
    """

    def __init__(self, *args, **kwargs):
        yaml.Loader.__init__(self, *args, **kwargs)

        self.add_constructor(u'tag:yaml.org,2002:map', type(self).construct_yaml_map)
        self.add_constructor(u'tag:yaml.org,2002:omap', type(self).construct_yaml_map)

    def construct_yaml_map(self, node):
        data = OrderedDict()
        yield data
        value = self.construct_mapping(node)
        data.update(value)

    def construct_mapping(self, node, deep=False):
        if isinstance(node, yaml.MappingNode):
            self.flatten_mapping(node)
        else:
            raise yaml.constructor.ConstructorError(None, None,
                'expected a mapping node, but found %s' % node.id, node.start_mark)

        mapping = OrderedDict()
        for key_node, value_node in node.value:
            key = self.construct_object(key_node, deep=deep)
            try:
                hash(key)
            except TypeError, exc:
                raise yaml.constructor.ConstructorError('while constructing a mapping',
                    node.start_mark, 'found unacceptable key (%s)' % exc, key_node.start_mark)
            value = self.construct_object(value_node, deep=deep)
            mapping[key] = value
        return mapping

rawDescription: In Python, how can you load YAML mappings as OrderedDicts?


code:
try :
    if my_array[i][j] : #Checks if the array contains something not empty
        if isinstance(my_array[i][j], YourObjectType) :
            print "We have a type YourObjectType at position %d, %d" % (i, j)
except :
    print "Ouch, nothing in the position %d,%d" % (i, j)

rawDescription: How to check if there is an object in a 2D list

description: Check if there is an object in a 2D list


code:
int('0x'+'2a',16)
packet=["2a","19","00","00"]
packet=[int(p,16) for p in packet]
packet
print ", ".join(map(hex,packet))

rawDescription: Interpret 0x string as hex in Python

description: Interpret 0x string as hex in Python


code:
class myStack:
     def __init__(self):
         self.container = []  # You don't want to assign [] to self - when you do that, you're just assigning to a new local variable called `self`.  You want your stack to *have* a list, not *be* a list.

     def isEmpty(self):
         return self.size() == 0   # While there's nothing wrong with self.container == [], there is a builtin function for that purpose, so we may as well use it.  And while we're at it, it's often nice to use your own internal functions, so behavior is more consistent.

     def push(self, item):
         self.container.append(item)  # appending to the *container*, not the instance itself.

     def pop(self):
         return self.container.pop()  # pop from the container, this was fixed from the old version which was wrong

     def size(self):
         return len(self.container)  # length of the container

s = myStack()
s.push('1')
s.push('2')
print(s.pop())
print s

rawDescription: Implementing Stack with Python

description: Implementing Stack with Python


code:
from matlib import transpose, mattmat, vec2colmat, mat2vec, matdim, matprint
from qr import  qr

def readdat():
    f = open('dat','r')
    x, y = [], []
    f.next()
    for line in  f:
        val = line.split()
        y.append(float(val[1]))
        x.append([float(p) for p in val[2:]])
    return x, y

def bsub(r, z):
    """ solves "R b = z", where r is triangular"""
    m, n = matdim(r)
    p, q = matdim(z)
    b = [[0] * n]
    pp, qq = matdim(b)
    for j in range(n-1, -1, -1):
        zz = z[0][j] - sum(r[j][k]*b[0][k] for k in range(j+1, n))
        b[0][j] = zz / r[j][j]
    return b

def linreg(y, x):

    # prepend x with 1
    for xx in x:
        xx.insert(0, 1.0)

    # QR decomposition
    q, r = qr(x)

    # z = Q^T y
    z = mattmat(q, vec2colmat(y))

    # back substitute to find b in R b = z
    b = bsub(r, transpose(z))
    b = b[0]

    return b

def tester():
    # read test data
    x, y = readdat()

    # calculate coeff
    b = linreg(y, x)

    for i,coef in enumerate(b):
        print 'coef b%d: %f' % (i, coef)

if __name__ == "__main__":
    tester()

rawDescription: pure python code for multivariate linear regression

description: Pure python code for multivariate linear regression