code:
def compare(a, b):
    a_parts = a.split('.')
    b_parts = b.split('.')
    v = ['major', 'minor', 'build']
    for i in range(3):
        diff = int(a_parts[i]) - int(b_parts[i])
        if diff is 0:
            continue
        if diff > 0:
            direction = 'ahead'
        else:
            direction = 'behind'
            diff = -diff

        return 'pkg is %s %s version %s' %  (diff, v[i], direction)
    return 'version are equal'

print compare('3.2.1', '2.0.0') # pkg is 1 major version ahead
print compare('3.2.1', '3.0.0') # pkg is 2 minor version ahead
print compare('3.2.1', '3.2.0') # pkg is 1 build version ahead
print compare('3.2.1', '3.2.1') # version are equal
print compare('3.2.1', '6.2.1') # pkg is 3 major version behind

rawDescription: Calculate the Difference between Version Numbers


rawDescription: How to delete all the items of a specific key in a list of dicts?

description: Delete all the items of a specific key in a list of dicts


code:
#!/usr/bin/python
import re
import sys
for line in open(sys.argv[1],'r').readlines():
  line = re.sub(r'TargetName=.+',r'TargetName=D:\\new', line)
  line = re.sub(r'FriendlyName=.+',r'FriendlyName=big', line)
  print line,

rawDescription: Replace a whole line in a txt file

description: Replace a whole line in a txt file


code:
import sys

class persistent_locals(object):
    def __init__(self, func):
        self._locals = {}
        self.func = func

    def __call__(self, *args, **kwargs):
        def tracer(frame, event, arg):
            if event=='return':
                self._locals = frame.f_locals.copy()

        # tracer is activated on next call, return or exception
        sys.setprofile(tracer)
        try:
            # trace the function call
            res = self.func(*args, **kwargs)
        finally:
            # disable tracer and replace with old one
            sys.setprofile(None)
        return res

    def clear_locals(self):
        self._locals = {}

    @property
    def locals(self):
        return self._locals

@persistent_locals
def func():
    local1 = 1
    local2 = 2

func()
print func.locals

rawDescription: Python: Is there a way to get a local function variable from within a decorator that wraps it?


rawDescription: How to delete Elements from a list in a dictionary

description: Delete Elements from a list in a dictionary


code:
s = '''1, 2, 3
       [4, 5, 6]
       3, 2, 1. '''

l = re.findall(r'\[.*\]', s)
# number of lists in string
print len(l)

# largest array by length of numbers in each list found
print max([re.findall(r'\d+', i) for i in l])

# number of numbers total in problem
print re.findall(r'\d+', s)

rawDescription: Identifying Lists of Numbers With Regular Expressions in Python


code:
class A (unicode):
    def __new__ (cls, value):
       return unicode.__new__(cls, str(value).upper())

print A('hello')
u'HELLO'

rawDescription: Creating a Python class that holds its value as an upper case string doesn't work


code:
import time

text = """50 Cent     1975-07-06
75 Cents    1933-01-29
9th Wonder  1975-01-15
A Fine Frenzy   1984-12-23"""

for line in text.splitlines():
    dob = line.rsplit(None, 1)[-1]
    dob_new = time.strftime('%B %d, %Y', time.strptime(dob, '%Y-%m-%d'))

    print line.replace(dob, dob_new)

rawDescription: Date conversion in python

description: Date conversion in python


code:
import csv
reader = csv.reader(open('C:/www/stackoverflow.txt'), delimiter='\t')
for row in reader:
    print row

rawDescription: Python File Read + Write

description: Python File Read + Write


code:
#!/usr/bin/python
# -*- mode: python -*-

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite:///webmgmt.db', convert_unicode=True, echo=False)
Base = declarative_base()
Base.metadata.reflect(engine)

from sqlalchemy.orm import relationship, backref

class Users(Base):
    __table__ = Base.metadata.tables['users']

if __name__ == '__main__':
    from sqlalchemy.orm import scoped_session, sessionmaker, Query
    db_session = scoped_session(sessionmaker(bind=engine))
    for item in db_session.query(Users.id, Users.name):
        print item

rawDescription: How to build a flask application around an already existing database?

description: Build a flask application around an already existing database


rawDescription: Selenium- len of text returns zero?

description: Selenium- len of text returns zero


code:
import signal, os

def handler(signum, frame):
    print 'Signal handler called with signal', signum
    raise IOError("Couldn't open device!")

# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGALRM, handler)
signal.alarm(5)

# This open() may hang indefinitely
fd = os.open('/dev/ttyS0', os.O_RDWR)

signal.alarm(0)          # Disable the alarm

rawDescription: What does signal handling EXACTLY mean?

description: What does signal handling EXACTLY mean


code:
import os
import csv
from StringIO import StringIO

scsv = """coupon,    months_to_maturity,    FACE_VALUE,
                0.3758,    12,    100,
                0.7287,    24,    100,
                1.0894,    36,    100,
                1.4019,    48,    100,
                1.6542,    60,    100,
                1.8512,    72,    100,
                2.0034,    84,    100,
                2.1213,    96,    100,
                2.2141,    108,    100,
                2.2891,    120,    100,
                2.3516,    132,    100,
             """

scsv = scsv.replace(' ','')  #remove white spaces
scsv = scsv.replace(',' + os.linesep, os.linesep) #remove commas at the end of the lines

f = StringIO(scsv)
reader = csv.DictReader(f, delimiter=',')
rows = [row for row in reader]

print rows[0]
# {'coupon': '0.3758', 'months_to_maturity': '12', 'FACE_VALUE': '100'}

rawDescription: Converting multi-line String to Dict in Python


code:
from datetime import date

def not_on(dates):
    def noop(): pass
    def decor(fn):
        if date.today() in dates:
            return noop
        else:
            return fn
    return decor

@not_on( ( date(2013, 03, 01), ) )
def job():
    print "yeah"

rawDescription: python script advanced scheduling

description: Python script advanced scheduling


rawDescription: Comparing elements of two lists in jython


code:
import threading
import urllib

maxthreads = 4

sites = ['google.com', 'yahoo.com', ] # etc.

class Download(threading.Thread):
   def run (self):
       global sites
       while sites:
           site = sites.pop()
           print "start", site
           urllib.urlretrieve('http://' + site, site)
           print "end  ", site

for x in xrange(min(maxthreads, len(sites))):
    Download().start()

rawDescription: Scraping landing pages of a list of domains


code:
import threading
import subprocess
import time

class IntervalRunner(threading.Thread):
    def __init__(self, seconds):
        self.seconds = seconds
        threading.Thread.__init__(self)

    def run(self):
        while True:
            p = subprocess.Popen('ls -la'.split(), shell=False,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)

            stdout, stderr = p.communicate()
            print stdout
            time.sleep(self.seconds)

runner = IntervalRunner(10)
runner.start()
runner.join()

rawDescription: Run python subprocess for x seconds every y seconds


code:
import numpy as np

ids = [1,1,1,2,2,2,3]
data = [20,30,0,4,8,9,18]

print np.bincount(ids, weights=data)

rawDescription: Most efficient way to sum huge 2D NumPy array, grouped by ID column?


code:
from array import array
buf = array("d", range(101))
addr, count = buf.buffer_info()
print lib.sum(cast(addr, POINTER(c_double)), count)

rawDescription: Using array.array in Python ctypes

description: Using array.array in Python ctypes


code:
things = set(['Apple', 'Orange', 'Cherry','banana','dog','door','Chair'])
otherThings = set(['Apple', 'Orange','TV' ,'Cherry','banana','Cat','Pen','Computer','Book'])
print things & otherThings

rawDescription: Comparing Lists and Printing the Common

description: Comparing Lists and Printing the Common


code:
for x in ['func_closure','func_code','func_defaults','func_dict']:
     y = getattr(MyFunc.data,x)
     print y

rawDescription: Python: print specific attributes using for loop


code:
class MyHTMLParser(HTMLParser):

    def reset(self):
        HTMLParser.reset(self)
        self.pages    = []
        self.text     = []                     # *****
        self.is_li    = False
        self.num_as   = 0
        self.close_a  = False
        self.close_li = False

    def handle_starttag(self, tag, attrs):
          if tag == 'li':
              self.is_li    = True
              self.close_a  = False
              self.close_li = False

          if tag == 'a' and self.is_li:
              if self.num_as < 7:
                  self.num_as += 1
                  self.close_a = False
              else:
                  self.num_as = 0
                  self.is_li = False

    def handle_endtag(self, tag):
         if tag == 'a':
             self.close_a  = True
         if tag == 'li':
             self.close_li = True
             self.num_as   = 0
             self.pages.append("".join(self.text))      # *****
             self.text = []                             # *****

    def handle_data(self, data):
        if self.is_li:
            if self.num_as == 2 and not self.close_li and not self.close_a:
                print "found data",  data
                self.text.append(data)              # *****

    def handle_charref(self, ref):
        self.handle_entityref("#" + ref)

    def handle_entityref(self, ref):
        self.handle_data(self.unescape("&%s;" % ref))

    def get_pages(self):
        return self.pages

rawDescription: Python HTMLParser dividing data at &

description: Python HTMLParser dividing data at &


code:
import re
pattern = re.compile("html")
query = {"Body": pattern}
r = mo_db.posts.find(query)
print r.count()

rawDescription: MongoDB returns no results in python

description: MongoDB returns no results in python


code:
for status in statuses:

    print "%s\t%s\t%s\t%s" % (status.text,
                              status.author.screen_name,
                              status.created_at,
                              status.source,)

    cur.execute("INSERT INTO tweets VALUES (%s, %s, %s, %s)", (status.text,
                                                               status.author.screen_name,
                                                               status.created_at,
                                                               status.source))

return render_to_response('dashboard.html',{'statuses': statuses},context_instance=RequestContext(request))

rawDescription: Save Tweepy Objects

description: Save Tweepy Objects


code:
import random
import math

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

    def __str__(self):
        return str((self.x, self.y))

class Circle:
    def __init__(self, origin, radius):
        self.origin = origin
        self.radius = radius

origin = Point(0, 0)
radius = 50
circle = Circle(origin, radius)

for i in range(0, 10):
    p = random.random() * 2 * math.pi
    r = circle.radius * math.sqrt(random.random())
    x = math.cos(p) * r
    y = math.sin(p) * r

    print x, y

rawDescription: Can I generate points inside my circle?

description: Generate points inside my circle


code:
import StringIO
import csv
f = StringIO.StringIO(u'''field1,field2,field3
1,2,3
4,5,6
7,8,9
''')
for row in csv.DictReader(f):
    print row

rawDescription: Can you use csv.DictReader without a file?

description: Use csv.DictReader without a file


code:
primes = []

for candidate in range(2, 101):
    can_be_prime = True
    for prime in primes:
        if candidate % prime == 0:
            can_be_prime = False
            break
    if can_be_prime:
        primes.append(candidate)
        print "%d-" % candidate
    else:
        print candidate

rawDescription: Mark prime numbers in a range of numbers


code:
import hashlib

# hash 1 time...
digest = hashlib.sha256(raw).digest()

# then hash 64k-1 times
for x in range(0, 64000-1):
  digest = hashlib.sha256(digest).digest()
print digest.encode("hex")

rawDescription: How to iteratively sha256 in Python using native lib (ie hashlib), using byte[] as input and not hex string

description: Iteratively sha256 in Python using native lib (ie hashlib), using byte[] as input and not hex string


code:
from Microsoft.Win32 import Registry
paths = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\Folders").GetValueNames()
for ipy_path in paths:
    print ipy_path

rawDescription: IronPython windows registry get installation paths


code:
try:
 os.unlink(os.path.join(os.getcwd(),'file.csv'))
except OSError, e:
 print e #file does not exist or you don't have permission

rawDescription: How do you delete a file (located in the same directory that your script is running in) in Python?

description: Delete a file (located in the same directory that your script is running in) in Python


code:
import win32api
import time

def keyWasUnPressed():
    print "enabling joystick..."
    #enable joystick here

def keyWasPressed():
    print "disabling joystick..."
    #disable joystick here

def isKeyPressed(key):
    #"if the high-order bit is 1, the key is down; otherwise, it is up."
    return (win32api.GetKeyState(key) & (1 << 7)) != 0

key = ord('A')

wasKeyPressedTheLastTimeWeChecked = False
while True:
    keyIsPressed = isKeyPressed(key)
    if keyIsPressed and not wasKeyPressedTheLastTimeWeChecked:
        keyWasPressed()
    if not keyIsPressed and wasKeyPressedTheLastTimeWeChecked:
        keyWasUnPressed()
    wasKeyPressedTheLastTimeWeChecked = keyIsPressed
    time.sleep(0.01)

rawDescription: pygame capture keyboard events when window not in focus

description: Pygame capture keyboard events when window not in focus


code:
sentence = [(4,'make'),(1,'sentence'),(0,'this'),(3,'now'),(5,'sense'),(2,'should')]
print "[%s]" % ', '.join(word for _,word in sorted(sentence))

rawDescription: Any more elegant solution than provided for sorting tuple?


code:
def GoogPlacDetails(placeid,key):
    print "Fetch data from URL"
    AUTH_KEY = key
    PLACEID = placeid
    fileName = 'data/googleJson.json'
    url = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=',PLACEID,'&sensor=false&key=',AUTH_KEY

    try:
        urllib2.urlopen(url)
        filePut = open(fileName, 'w')
        data = urllib2.urlopen(url).read()

    except urllib2.HTTPError, e:
        print(e.code)

        data = 0
    except urllib2.URLError, e:
        print(e.args)

        data = 0

    if data != 0: #validate url
        try:
            with open(fileName) as jsonGetData:
                #add data
                filePut.write(data)
                filePut.close()

                j = json.load(jsonGetData)
                print "Json processed."
                return 1
        except Exception, e:
            print e

            raise
        else:
            pass
        finally:
            pass
    else:
        print url," Not available."
        return 0
#end GoogPlacDetails

rawDescription: Google API Request in PY Error: "urllib.error.HTTPError: HTTP Error 500: Internal Server Error"


code:
from functools import wraps
class A :
    def __init__(self):
        self.methodName = 'temp1'
    def temp(self, i) :
        print (self.__class__.__name__)
        print("hi" +str(i))
    def temp2(self):
        print "hey hey hey"

class B :
     pass

class C :
    def __call__(self,Func) :
        @wraps(Func)
        def newFunc(self, *args, **kwargs) :
            return Func(*args, **kwargs);
        return newFunc

if __name__ == '__main__' :
    a = A()
    setattr(B, a.methodName, a.temp)
    setattr(B, 'temp1', C().__call__(a.temp))
    b = B()
    b.temp1(5)

rawDescription: Understanding python decorator. why does this not work?


code:
html = """
        Price:
        130  €
        Value:
        90000  €

        Price:
        130  €
        Value:
        90000  €
"""

from bs4 import BeautifulSoup
soup = BeautifulSoup(html)

price_texts = soup.find_all('span', text='Price:')
for element in price_texts:
    # .next_sibling() might work, too, with a parent element present
    price_value = element.find_next_sibling('span')
    print price_value.get_text()

# It prints:
# 130  €
# 130  €

rawDescription: Python beautifulsoup extract value without identifier


code:
import re

range_search = re.compile('''
    (?P<variable>.+)
    (?P<start>\d+):(?P<stop>\d+)
    (?P<k>[^=]+)=(?P<v>.+)
''', re.VERBOSE)

range_replace = '''for k in range(\g<start>, \g<stop>):
    \g<variable>[k].set_\g<k>(expr='\g<v>')'''

print range_search.sub(range_replace, 'f7:10y=x^2')

@StatelessInputTransformer.wrap
def inline_loop(line):
    return range_search.sub(range_replace, line)

rawDescription: How to parse a domain-specific language on top of Python? What architecture with IPython?

description: Parse a domain-specific language on top of Python? What architecture with IPython


code:
def make_thingy(bases):
    new_thingy_class = type("???", bases, {})
    new_thingy_instance = new_thingy_class()
    print new_thingy_instance.some_attribute_in_pet
    print new_thingy_instance.some_attribute_in_species
    return new_thingy_instance

x = new_thingy(Dog)

rawDescription: Changing the base class of a class with __new__


code: if any(s.lower() == "foo" for s in list): print "found"

rawDescription: Generator in if-statement in python

description: Generator in if-statement in python


code:
found = False

for (dirname, dirs, files) in os.walk('/home/user/Documents/mystuff'):
    for filename in files:
        if filename.startswith('autorun'):
            thefile = os.path.join(dirname,filename)
            f = open(thefile)
            print f.read()
            found = True

if not found:
    print "No file matched the criteria"

rawDescription: Searching for files with os.walk if no files are found matching the search criteria- Python


code:
import os, shutil

def file_input(file_name):
    newlist = []                                                #create new list
    for names in os.listdir(file_name):                         #loops through directory
        if names.endswith(".txt") or names.endswith(".doc"):    #returns only extensions required
            full_file_name = os.path.join(file_name, names)     #creates full file path name - required for further file modification
            newlist.append(full_file_name)                      #adds item to list
            dst = os.path.join(file_name + "/target_files")

            if not os.path.exists(dst):
                os.makedirs(dst)

            full_file_name = os.path.join(file_name, names)
            if (os.path.exists(full_file_name)):
                print "Success!"
                shutil.copy(full_file_name, dst)

def find_file():
    file_name = raw_input("\nPlease carefully input full directory pathway.\nUse capitalisation as necessary.\nFile path: ")
    file_name = "/home/praveen/programming/trash/documents"                            #permanent input for testing!
    return file_input(file_name)

find_file()

rawDescription: shutil.copy only works once

description: Shutil.copy only works once


code: print getattr(myobject, obj_name)

rawDescription: Python: Can you use an evaluated expression in an object reference?


code:
from Tkinter import Tk, Entry

root = Tk()

def click(key):
    # print the key that was pressed
    print key.char

entry = Entry()
entry.grid()
# Bind entry to any keypress
entry.bind("<Key>", click)

root.mainloop()

rawDescription: Tkinter, Entry widget, is detecting input text possible?


code:
import time
import datetime

class MySQLWrapper:
  def __init__(self, min_period_seconds):
    self.min_period = datetime.timedelta(seconds=min_period_seconds)
    self.last_calltime = datetime.datetime.now() - self.min_period
  def insert(self, item):
    now = datetime.datetime.now()
    if now-self.last_calltime < self.min_period:
      print "not insert"
    else:
     self.last_calltime = now
     print "insert", item

m = MySQLWrapper(5)
m.insert(1) # insert 1
m.insert(2) # not insert
time.sleep(5)
m.insert(3) # insert 3

rawDescription: Python, function quit if it has been run the last 5 minutes


code:
from BeautifulSoup import BeautifulSoup
import re
import urllib2

page = urllib2.urlopen("http://www.linkpages.com")
soup = BeautifulSoup(page)
for link in soup.findAll('a', attrs={'href': re.compile("^http://")}):
    print link

rawDescription: Trying to grab just absolute links from a webpage using BeautifulSoup


code:
ftp = FTP('webftp.vancouver.ca')
ftp.login()
ftp.cwd('OpenData/xls')
filename = 'new_food_vendor_locations.xls'
ftp.retrbinary('RETR %s' % filename, open('myLovelyNewFile.xls', 'w').write)
ftp.quit()
workbook = xlrd.open_workbook('myLovelyNewFile.xls')
worksheet = workbook.sheet_by_name('Query_vendor_food')
num_rows = worksheet.nrows - 1
curr_row = -1
while curr_row < num_rows:
    curr_row += 1
    row = worksheet.row(curr_row)
    print row

rawDescription: How to download a .xls into a Python object via FTP with Python ftplib

description: Download a .xls into a Python object via FTP with Python ftplib


code:
#!/usr/bin/python

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fsolve

# Define the expression whose roots we want to find

a = 0.5
R = 1.6

func = lambda tau : R - ((1.0 - np.exp(-tau))/(1.0 - np.exp(-a*tau)))

# Plot it

tau = np.linspace(-0.5, 1.5, 201)

plt.plot(tau, func(tau))
plt.xlabel("tau")
plt.ylabel("expression value")
plt.grid()
plt.show()

# Use the numerical solver to find the roots

tau_initial_guess = 0.5
tau_solution = fsolve(func, tau_initial_guess)

print "The solution is tau = %f" % tau_solution
print "at which the value of the expression is %f" % func(tau_solution)

rawDescription: Solve an equation using a python numerical solver in numpy


code:
# called by each thread
def get_url(q, url):
    q.put(urllib2.urlopen(url).read())

theurls = '''http://google.com http://yahoo.com'''.split()

q = Queue.Queue()

for u in theurls:
    t = threading.Thread(target=get_url, args = (q,u))
    t.daemon = True
    t.start()

s = q.get()
print s

rawDescription: Thread to add items in Treeview - pygtk

description: Thread to add items in Treeview - pygtk


code:
import math
import time
from multiprocessing import Queue
import multiprocessing

def factorize_naive(n):
    factors = []
    for div in range(2, int(n**.5)+1):
        while not n % div:
            factors.append(div)
            n //= div
    if n != 1:
        factors.append(n)
    return factors

nums = range(100000)
nprocs = 4

def worker(nums, out_q):
    """ The worker function, invoked in a process. 'nums' is a
        list of numbers to factor. The results are placed in
        a dictionary that's pushed to a queue.
    """
    outdict = {}
    for n in nums:
        outdict[n] = factorize_naive(n)
    out_q.put(outdict)

# Each process will get 'chunksize' nums and a queue to put his out
# dict into
out_q = Queue()
chunksize = int(math.ceil(len(nums) / float(nprocs)))
procs = []

for i in range(nprocs):
    p = multiprocessing.Process(
            target=worker,
            args=(nums[chunksize * i:chunksize * (i + 1)],
                  out_q))
    procs.append(p)
    p.start()

# Collect all results into a single result dict. We know how many dicts
# with results to expect.
resultdict = {}
for i in range(nprocs):
    resultdict.update(out_q.get())

time.sleep(5)

# Wait for all worker processes to finish
for p in procs:
    p.join()

print resultdict

time.sleep(15)

rawDescription: When to call .join() on a process?

description: When to call .join() on a process


code:
import os, sys
import Image

size = 128, 128

for infile in sys.argv[1:]:
    outfile = os.path.splitext(infile)[0] + ".thumbnail"
    if infile != outfile:
        try:
            im = Image.open(infile)
            im.thumbnail(size, Image.ANTIALIAS)
            im.save(outfile, "JPEG")
        except IOError:
            print "cannot create thumbnail for '%s'" % infile

rawDescription: How do I resize an image using PIL and maintain its aspect ratio?

description: Resize an image using PIL and maintain its aspect ratio


code:
from urlparse import urljoin

base_url = "http://website.co.uk"
for link in soup.find_all('a', href=re.compile('sports')):
    print urljoin(base_url, link['href'])

rawDescription: BeautifulSoup web scraping

description: BeautifulSoup web scraping


code:
t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)
print c.fetchone()

rawDescription: using sqlite3 database for flaskr in python without ORM

description: Using sqlite3 database for flaskr in python without ORM


code:
import subprocess

def php(script_path):
    p = subprocess.Popen(['php', script_path], stdout=subprocess.PIPE)
    result = p.communicate()[0]
    return result

# YOUR CODE BELOW:
page_html = "
News and Updates
"
news_script_output = php("news-generator.php")
print page_html + news_script_output

rawDescription: How do I include a PHP script in Python?

description: Include a PHP script in Python


code:
for x in OrderedList:
    if x in ResultList1:
        print x, '1'
        continue
    if x in ResultList2:
        print x, '2'
        continue
    if x in ResultList3:
        print x, '3'
        continue

rawDescription: Incrementing for loop in python when condition matched


code:
import time

start = time.time()
in_str = raw_input("Enter the thing:")
end = time.time()
elapsed = end-start
print "That took you " + str(elapsed) + " seconds. Man, you're slow."

rawDescription: Time duration of user input

description: Time duration of user input


code:
#text is the variable with your text
text = text.strip()
questions = re.split(r'\d+\.',text)
questions = [x.strip() for x in questions if x != '']
final = [re.split(r'\(\d+\)',x) for x in questions]

for part in final:
    question = part[0]
    print question
    for answer in part[1:]:
        print answer

rawDescription: Python parse text and group into different parts


code:
import pandas as pd
import numpy as np
import io

#testing df1,df2,df3
temp=u"""ID,Jan,Feb,Mar
1,True,True,False
2,True,True,True
3,False,False,False"""
df3 = pd.read_csv(io.StringIO(temp), sep=",", index_col=[0])
print df3
temp1=u"""ID,Jan,Feb,Mar
1,True,False,False
2,False,True,True
3,False,True,True"""
df1 = pd.read_csv(io.StringIO(temp1), sep=",", index_col=[0])
print df1
temp2=u"""ID,Jan,Feb,Mar
1,False,False,False
2,False,False,True
3,False,True,True"""
df2 = pd.read_csv(io.StringIO(temp2), sep=",", index_col=[0])
print df2

#concat all dataframes by columns
pieces = {'df1': df1, 'df2': df2, 'df3': df3}
df = pd.concat(pieces, axis=1)
print df

#create new dataframe with size as df filled by column names
levels = df.columns.levels
labels = df.columns.labels
xyz = pd.DataFrame( np.array(levels[0][labels[0]].tolist()*len(df.index)).reshape((len(df.index), len(df.index)*len(pieces))), index=df.index, columns = df.columns)
print xyz

#reset multicolumn to column
xyz.columns = levels[1][labels[1]]
df.columns = levels[1][labels[1]]

#use df as mask - output names of df with False
print xyz.mask(df)

#use df as mask - output names of df with True
out_false =  xyz.mask(df)
print out_false

out_true =  xyz.mask(~df)
print out_true

#create output empty df - for False and for True values
result_false = result_true = pd.DataFrame(index = out_false.index)

#group output dataframe by columns and create new df from series - for False and for True values
for name, group in out_false.groupby(level=0, axis=1):
    #print name
    series = pd.Series(group.apply(lambda x: ','.join(map(str, x.dropna())), axis=1), name=name)
    print
    print series
    result_false = pd.concat([result_false, series], axis=1)
print result_false
#        Feb          Jan          Mar
#ID
#1   df1,df2          df2  df1,df2,df3
#2       df2      df1,df2
#3       df3  df1,df2,df3          df3

for name, group in out_true.groupby(level=0, axis=1):
    #print name
    series = pd.Series(group.apply(lambda x: ','.join(map(str, x.dropna())), axis=1), name=name)
    result_true = pd.concat([result_true, series], axis=1)
print result_true
#        Feb      Jan          Mar
#ID
#1       df3  df1,df3
#2   df1,df3      df3  df1,df2,df3
#3   df1,df2               df1,df2

rawDescription: Comparing Boolean Values of Pandas Dataframes- Returning String


code:
handle=Entrez.esummary(db="nucleotide, protein, ...", id="gi or NCBI_ref number")
record=Entrez.read(handle)
handle.close()
description=record[0]["Title"]
print description

rawDescription: How to get sequence description from gi number through biopython?

description: Get sequence description from gi number through biopython


code:
import lxml.html
from lxml.etree import XPath
url = "http://gbgfotboll.se/information/?scr=table&ftid=51168"
date = '2014-09-27'

rows_xpath = XPath("//*[@id='content-primary']/table[3]/tbody/tr[td[1]/span/span//text()='%s']" % (date))
time_xpath = XPath("td[1]/span/span//text()[2]")
team_xpath = XPath("td[2]/a/text()")

html = lxml.html.parse(url)

for row in rows_xpath(html):
    time = time_xpath(row)[0].strip()
    team = team_xpath(row)[0]
    print time, team

rawDescription: Iterate through all the rows in a table using python lxml xpath


code:
# returns a tuple of two lists
def method1(list1, list2):
    list1.append(4)
    list2.append(8)
    return (list1, list2)

# the lists are result[0] and result[1]
result = method1([1, 2, 3], [2, 4, 6])
for l in result:
    print l

# more useful: tuple assignment
(list1, list2) = method1([1, 2, 3], [2, 4, 6])
print list1
print list2

rawDescription: Taking multiple lists as arguments rather than returning them in a dict, is there a better way (python3)?


code:
import sys

listA = []
listB = []
execute = {}

listA.append("A1")
listA.append("A2")
listB.append("B1")
listB.append("B2")

for i in listA:
     z = []
     for j in listB:
        z.append(j)
     execute[i] = z

print execute

rawDescription: Key that maps to List in Python Dictionary


code:
import BeautifulSoup

s = """
<node1>
    <node2>
        <node3>foo</node3>
    </node2>
    <node3>
      <!-- I'm a comment! Leave me be! -->
    </node3>
    <node4>
    <![CDATA[
      I'm CDATA!  Changing me would be bad!
    ]]>
    </node4>
</node1>
"""

soup = BeautifulSoup.BeautifulStoneSoup(s)

for t in soup.findAll(text=True):
   if type(t) is BeautifulSoup.NavigableString: # Ignores comments and CDATA
      t.replaceWith(t.strip())

print soup

rawDescription: Crunching xml with python

description: Crunching xml with python


code:
A = [1, 2, 3]
B = [4, 5, 6]
C = [7, 8, 9]
MasterList = [A, B, C]

whichList = choice(MasterList)
print choice(whichList)

rawDescription: Random List choices in python

description: Random List choices in python


rawDescription: How to convert a string containing unicode escapes into a unicode string containing those characters?

description: Convert a string containing unicode escapes into a unicode string containing those characters


code:
def startSQLHelper(self, sPath, sPassword):
  print '--starting SQL helper--'
  args = [sPath + 'sqlplus.exe', '-L', 'sys/'+sPassword]
  return subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

def checkOraPass (self, sPath, sPassword):
  print '--checkOraPass--'
  p = self.startSQLHelper(sPath, sPassword)
  output = p.communicate()[0]
  print output

  if output.find('ORA-28009') != -1:
     print 'Password passed'
     return True

  return False

rawDescription: Python: how to search a multi line string for specific content


code:
import requests

params = {
    "apikey" : "899..." # Omitted for security
}
barcode = "5060144218476"

response = requests.get(
    "https://api.outpan.com/v2/products/" + barcode,
    params = params)
print response.json()

rawDescription: Retrieving JSON Data Using Flask on Python


code:
import urllib2
import bs4

webpage = urllib2.urlopen('http://facts.randomhistory.com/interesting-facts-about-cats.html')
soup = bs4.BeautifulSoup(webpage, from_encoding='UTF-8',
                         parse_only=bs4.SoupStrainer(attrs='content-td'))
for li in soup('li'):
    print li.text.encode('utf-8')

rawDescription: Using Beautiful Soup, grabbing stuff between <li> and </li>


code:
from Tkinter import *

class Select:
    def __init__ (self, root):
        self.root = root
        root.title ('Title')
        root.geometry ('100x100')

        button1 = Radiobutton (self.root, text = 'Option 1', command = self.option1, value = 1).place (x = 10, y = 10)
        button2 = Radiobutton (self.root, text = 'Option 2', command = self.option2, value = 2).place (x = 10, y = 30)

        test = Button (self.root, text = 'Run', command = self.test).place (x = 10, y = 70)
        self.flag = 0
        self.root.mainloop()

    def option1 (self):
        print ' Option 1'
        self.flag = 1

    def option2 (self):
        print ' Option 2'
        self.flag = 2

    def test (self):
        if self.flag == 1:
            print '1st option selected'
        elif self.flag == 2:
            print '2nd option selected'
        else:
            print 'No option selected'

Select(Tk()).test()

rawDescription: How to identify/use selected radio button by another button in tkinter

description: Identify/use selected radio button by another button in tkinter


code: print repr(message)

rawDescription: Encoding for ZMQ

description: Encoding for ZMQ


code:
# get new values and re-plot
plt.ion()  # make show non-blocking
plt.show() # show the figure
while True:
    print "!"
    XX = inteprolate(u,XX)
    line.set_xdata(XX[:,0])
    line.set_ydata(XX[:,1])
    plt.draw() # re-draw the figure
    plt.pause(.1)  # give the gui time to process the draw events

rawDescription: Dynamically updating a graphed line in python


code:
class Chat(Protocol):
    def connectionMade(self):
        self.factory.clients.append(self)
    def connectionLost(self, reason):
        self.factory.clients.remove(self)
    def dataReceived(self,data):
        for c in self.factory.clients:
            c.message(data)
            print data
    def message(self, data):
        self.transport.write(data + '\n')

factory = Factory()
factory.clients = []
factory.protocol = Chat

reactor.listenTCP(8080,factory)
reactor.run()

rawDescription: Corona Simulator stop working after connecting to server


code:
lists = {
    "list1": ["one","Two","three"],
    "list2": ["1","2","3"],
}

while True:
    choice = raw_input("enter the list name: ")
    try:
        for item in lists[choice]:
            print item
    except KeyError:
        print "I never heard of any list named '{}'! Try again.".format(choice)
    else:
        break

rawDescription: Accessing a local variable whose name comes from user input


code:
def convertSVG(self, file):
    dom = self._getsvgdom(file)
    print dom
    self._switchGlyphsForPaths(dom)
    self._commitSVG(file, dom)
def _commitSVG(self, file, dom):
    f = open(file, 'w')
    dom.writexml(f)
    f.close()
def _getsvgdom(self, file):
    print 'getting DOM model'
    import xml.dom
    import xml.dom.minidom as mini
    f = open(file, 'r')
    svg = f.read()
    f.close()
    dom = mini.parseString(svg)
    return dom
def _getGlyphPaths(self, dom):
    symbols = dom.getElementsByTagName('symbol')
    glyphPaths = {}
    for s in symbols:
        pathNode = [p for p in s.childNodes if 'tagName' in dir(p) and p.tagName == 'path']
        glyphPaths[s.getAttribute('id')] = pathNode[0].getAttribute('d')
    return glyphPaths
def _switchGlyphsForPaths(self, dom):
    glyphs = self._getGlyphPaths(dom)
    use = self._getUseTags(dom)
    for glyph in glyphs.keys():
        print glyph
        nl = self.makeNewList(glyphs[glyph].split(' '))
        u = self._matchUseGlyphs(use, glyph)
        for u2 in u:
            print u2, 'brefore'
            self._convertUseToPath(u2, nl)
            print u2, 'after'

def _getUseTags(self, dom):
    return dom.getElementsByTagName('use')
def _matchUseGlyphs(self, use, glyph):
    matches = []
    for i in use:
        print i.getAttribute('xlink:href')
        if i.getAttribute('xlink:href') == '#'+glyph:
            matches.append(i)
    print matches
    return matches
def _convertUseToPath(self, use, strokeD):
    ## strokeD is a list of lists of strokes to make the glyph
    newD = self.nltostring(self.resetStrokeD(strokeD, use.getAttribute('x'), use.getAttribute('y')))
    use.tagName = 'path'
    use.removeAttribute('xlink:href')
    use.removeAttribute('x')
    use.removeAttribute('y')
    use.setAttribute('style', 'fill: rgb(0%,0%,0%); stroke-width: 0.5; stroke-linecap: round; stroke-linejoin: round; stroke: rgb(0%,0%,0%); stroke-opacity: 1;stroke-miterlimit: 10; ')
    use.setAttribute('d', newD)
def makeNewList(self, inList):
    i = 0
    nt = []
    while i < len(inList):
        start = i + self.listFind(inList[i:], ['M', 'L', 'C', 'Z'])
        end = start + self.listFind(inList[start+1:], ['M', 'L', 'C', 'Z', '', ' '])
        nt.append(inList[start:end+1])
        i = end + 1
    return nt
def listFind(self, x, query):
    for i in range(len(x)):
        if x[i] in query:
            return i
    return len(x)
def resetStrokeD(self, strokeD, x, y):
    nsd = []
    for i in strokeD:
        nsd.append(self.resetXY(i, x, y))
    return nsd
def resetXY(self, nl, x, y): # convert a list of strokes to xy coords
    nl2 = []
    for i in range(len(nl)):
        if i == 0:
            nl2.append(nl[i])
        elif i%2: # it's odd
            nl2.append(float(nl[i]) + float(x))
        elif not i%2: # it's even
            nl2.append(float(nl[i]) + float(y))
        else:
            print i, nl[i], 'error'
    return nl2
def nltostring(self, nl): # convert a colection of nl's to a string
    col = []
    for l in nl:
        templ = []
        for c in l:
            templ.append(str(c))
        templ = ' '.join(templ)
        col.append(templ)
    return ' '.join(col)

rawDescription: SVG Glyphs in Pyqt

description: SVG Glyphs in Pyqt


code: print i/len(some_list)*100," percent complete         \r",

rawDescription: Print to the same line and not a new line in python


code:
for label, vals in zip(list1, list2):
    print label
    print '---'.join(vals)

rawDescription: Best way to print list output in python

description: Print list output in python


code:
from Tkinter import *

root = Tk()
opt_list = ['opt1','opt2','opt3','opt4','opt5']
sel_list = []

def get_sel():
    sel_list.append(Lb1.curselection())
    root.destroy()

def change_opt():
    entry = E.get()
    change = entry.split(" ")
    print change
    Lb1.insert(int(change[0]),change[1])
    root.update()

def cancel():
    root.destroy()
E = Entry(root)
A = Button(root, text ="Change", command = change_opt)
B = Button(root, text ="Submit", command = get_sel)
C = Button(root, text ="Cancel", command = cancel)
Lb1 = Listbox(root, selectmode=MULTIPLE)

for i,j in enumerate(opt_list):
    Lb1.insert(i,j)

Lb1.pack()
B.pack()
C.pack()
E.pack()
A.pack()

root.mainloop()

rawDescription: Tkinter Listbox with Entry

description: Tkinter Listbox with Entry


code:
for r in log:
    host_line = "'",r['host'],"'"
    for line in host_line:
        if not line.startswith("178.255.20.20"):
            if not line == "'":
                print line.strip()

rawDescription: if not line.startswith - give to much single quotes and line bracks

description: If not line.startswith - give to much single quotes and line bracks


code:
from bs4 import BeautifulSoup
import re
import urllib, urllib2

def browse(page):
    url = 'http://bbn.kiwoom.com/bbn.marketConditionMLList.do'
    values = {
    'pageno': page,
    'basePath': '4',
    's_startdate': '20120822',
    's_enddate': '20200222',
    }

    data = urllib.urlencode(values)
    req = urllib2.Request(url, data)
    page=urllib2.urlopen(req)
    soup = BeautifulSoup(page.read())

    aTagAll = soup.find_all('a', {'class': 'file'})

    for aTag in aTagAll:
        downloadFile(getParams( aTag ))
    page+=1

def getParams(aTag):
    params = {}
    m = re.search(r"openFile\('([^']*)','([^']*)','([^']*)", aTag['onclick'])
    params['realname'] = m.group(1)
    params['filename'] = m.group(2)
    params['snMakedate'] = m.group(3)
    return params

def downloadFile(params):
  print 'Downloading : %s' % params['filename']
  url = 'http://bbn.kiwoom.com/bbn.fileDownload.do'
  values = {
    's_realname': params['realname'],
    's_filename': params['filename'],
    's_snMakedate': params['snMakedate'],
        'pageno': '8',
    'basePath': '4'
  }
  data = urllib.urlencode(values)

  req = urllib2.Request(url, data)
  try: response = urllib2.urlopen(req)
  except urllib2.HTTPError as e:
    print e.code
    print e.read()

  file = open(params['filename'], 'w')
  file.write(response.read())
  file.close()

for pagenum in range(1, 58):
    browse(page=pagenum)

rawDescription: How to get webpage using javascript?

description: Get webpage using javascript


code:
from random import randint

network = []

numberofpersons = 3
network = [range(numberofpersons) for i in range(numberofpersons)]

for i in range(numberofpersons):
    for j in range(numberofpersons):
        if randint(0,100)<25 and j!=i:
            network[i][j]="connected"
            network[j][i]="connected"
        else:
            network[i][j]=0
            network[j][i]=0

print network
# [[0, 0, 'connected'], [0, 0, 'connected'], ['connected', 'connected', 0]]

rawDescription: How can I edit an item in a list within a list?

description: Edit an item in a list within a list


code:
import datetime
from dateutil import rrule

holidays = [
    datetime.date(2012, 5, 1,),
    datetime.date(2012, 6, 1,),
    # ...
]

# Create a rule to recur every weekday starting today
r = rrule.rrule(rrule.DAILY,
                byweekday=[rrule.MO, rrule.TU, rrule.WE, rrule.TH, rrule.FR],
                dtstart=datetime.date.today())

# Create a rruleset
rs = rrule.rruleset()

# Attach our rrule to it
rs.rrule(r)

# Add holidays as exclusion days
for exdate in holidays:
    rs.exdate(exdate)

print rs[0]

rawDescription: Datetime Python - Next Business Day

description: Datetime Python - Next Business Day


code:
x = NavigableString('foop')
y = pickle.dumps(x)
z = pickle.loads(y)
print x, z

rawDescription: what does '__getnewargs__' do in this code

description: What does '__getnewargs__' do in this code


code:
# instantiate and configure the worker pool
from pathos.pp import ParallelPythonPool
pool = ParallelPythonPool(nodes=4)
# do a blocking map on the chosen function
print pool.map(pow, [1,2,3,4], [5,6,7,8])
# do a non-blocking map, then extract the results from the iterator
results = pool.imap(pow, [1,2,3,4], [5,6,7,8])
print "..."
print list(results)
# do an asynchronous map, then get the results
results = pool.amap(pow, [1,2,3,4], [5,6,7,8])
while not results.ready():
    time.sleep(5); print ".",
print results.get()

rawDescription: Does Parallel Python PP always get back jobs'results in the order we created them?


code:
import sys;
import email

class Attachement(object):
    def __init__(self):
        self.data = None;
        self.content_type = None;
        self.size = None;
        self.name = None;

def parse_attachment(message_part):
    content_disposition = message_part.get("Content-Disposition", None);
    if content_disposition:
        dispositions = content_disposition.strip().split(";");
        if bool(content_disposition and dispositions[0].lower() == "attachment"):

            attachment = Attachement();
            attachment.data = message_part.get_payload(decode=True);
            attachment.content_type = message_part.get_content_type();
            attachment.size = len(attachment.data);
            attachment.name = message_part.get_filename();

            return attachment;

    return None;

if __name__=='__main__':

    mail = "";
    for line in sys.stdin:
        mail += line;

    msg = email.message_from_string(mail);

    attachements = list();

    if(msg.is_multipart()):
        for part in msg.walk():
            attachement = parse_attachment(part);
            if(attachement):
                attachements.append(attachement);

    for att in attachements:
        file = open(att.name, 'w');
        file.write(att.data);
        file.close();

    print 'OK';

rawDescription: Save email attachment to file in python 2.7


code:
import simpy
import random

LENGTH_SIM = 100.0

class Customer(object):
    def __init__(self, arrive_time, num):
        self.arrive_time = arrive_time
        self.start_service_time = -1.0
        self.finish_service_time = -1.0
        self.served_by = -1.0
        self.num = num
    def print_attributes(self):
        format_string = "#%3d  Arrive: %6.1f  Start Service: %6.1f  "\
        + " End Service: %6.1f" + "   Served By: %3d"
        print format_string %(self.num, self.arrive_time,
                              self.start_service_time,
                                self.finish_service_time, self.served_by)

class Counter(object):
    def __init__(self, number):
        self.number = number
        self.name = "Counter " + str(number)
        self.start_times = []
        self.end_times = []

class Bank(object):
    def __init__(self, env, num_counters):
        self.counters = [Counter(x+1) for x in range(num_counters)]
        self.num_counters = num_counters
        self.counters_resources = [simpy.Resource(env, capacity=1)
                                   for x in range(num_counters)]

def generate_customers(env, customers):
    count = 1
    while 1:
##        wait_time = random.randint(8, 12)
        wait_time = 2
        yield env.timeout(wait_time)
        customers.append(Customer(env.now, count))
        print "Generated Customer: ", count
        count += 1

def select_counter(env, bank):
    '''choose a counter for the customer'''
    for x in range(len(bank.counters)):
        if bank.counters_resources[len(bank.counters) - 1 - x].count == 0 and\
        bank.counters_resources[len(bank.counters) - 1 - x].queue == []:
            print "Counter Selected: ", len(bank.counters) - 1 - x
            return len(bank.counters) - 1 - x
    return -2.0

def gen_service_time():
##    return random.random() * 20.0 + 19.0
    return random.randint(4, 8)

def wait_til_available(env, bank, temp_list):
    while 1:
        yield env.timeout(0.0001)
        for x in range(len(bank.counters)):
            if bank.counters_resources[x].count == 0:
                req = bank.counters_resources[x].request()
                temp_list[0] = x
                temp_list[1] = req
                return

def handle_customer(env, bank, customer, customers, num_process):
    '''handles customer'''
    print "Process ", num_process, " started at:", env.now
    counter = select_counter(env, bank)
    if counter != -2.0:
        with bank.counters_resources[counter].request() as req:
            yield req
            bank.counters[counter].start_times.append(env.now)
            service_time = gen_service_time()
            customer.start_service_time = env.now
            customer.finish_service_time = env.now + service_time
            bank.counters[counter].end_times.append(env.now + service_time)
            customer.served_by = counter + 1
            yield env.timeout(service_time)
    else:
        reqs = []
        got_counter = 0
        for x in range(len(bank.counters)):
            reqs.append(bank.counters_resources[x].request())
        good_req = yield simpy.events.AnyOf(env, reqs)
        req = good_req.keys()[0]
        for x in range(len(bank.counters)):
            if req != reqs[x]:
                bank.counters_resources[x].release(reqs[x])
                reqs[x].cancel(None, None, None)
            else:
                got_counter = x
        bank.counters[got_counter].start_times.append(env.now)
        service_time = gen_service_time()
        customer.start_service_time = env.now
        customer.finish_service_time = env.now + service_time
        bank.counters[got_counter].end_times.append(env.now + service_time)
        customer.served_by = got_counter + 1
        yield env.timeout(service_time)
        print "Process ", num_process, " finished at:", env.now
        bank.counters_resources[got_counter].release(req)

def run_bank(env, bank, customers):
    while 1:
        min_time = -1.0
        min_customer = -1.0
        for x in xrange(len(customers)):
            if customers[x].arrive_time < min_time or min_time == -1.0:
                if customers[x].arrive_time > env.now:
                    min_time = customers[x].arrive_time
                    min_customer = x
                else:
                    continue
        if min_time == -1.0:
            yield env.timeout(LENGTH_SIM - env.now)
        yield env.timeout(min_time - env.now)
        print "Calling Process: ", min_customer+1, "At time:", env.now
        env.process(handle_customer(env,bank,customers[min_customer],customers,
                                    min_customer + 1))

def run_sim():
    env = simpy.Environment()
    customers = []
    env.process(generate_customers(env, customers))
    env.run(until=LENGTH_SIM)
    env2 = simpy.Environment()
    bank = Bank(env2, 3)
    env2.process(run_bank(env2, bank, customers))
    env2.run(until = LENGTH_SIM)
    for x in range(len(customers)):
        customers[x].print_attributes()

if __name__ == '__main__':
    run_sim()

rawDescription: Requesting First available resource in simpy python


code:
class GuiWidget():
    def __init__(self, accessFunc):
        self.accessFunc = accessFunc
    def inc(self):
        self.accessFunc(self.accessFunc() + 1)

storage = {"k1":0, "k2":0}

def getAccessFunc(key):
    return lambda v = None: v is not None and storage.update({key: v}) or storage[key]

w1 = GuiWidget(getAccessFunc("k1"))
w2 = GuiWidget(getAccessFunc("k2"))

# calls from gui thread
print storage
w1.inc()
print storage
w2.inc()
print storage

rawDescription: Does it make sense to pass a dict together with a key to pass a storage location? Better way?


rawDescription: Split a dictionary into 2 dictionaries

description: Split a dictionary into 2 dictionaries


code:
class MultithreadParsing(object):

    process = None
    process_flag = True

    def f(self):
        print "\nMultithreadParsing has started\n"
        while self.process_flag:
            ''' get my object from database '''
            legacy = LegacyData.objects.filter(parsed=False)[0:1]

            if legacy:
                print "Processing: %s %s" % (legacy[0].name, legacy[0].disc_number)
                for l in legacy:
                    ''' ... Do what I want it to do ...'''
                sleep(1)
            else:
                self.process_flag = False
                print "Nothing to parse"

    def __init__(self):
        self.process = Process(target=self.f)
        self.process.start()
        print self.process
        a = raw_input("Press any key to stop \n")
        print "\nKILL FLAG HAS BEEN SENT\n"

        if a:
            print "\nKILL\n"
            self.process_flag = False

rawDescription: Creating interruptible process in python


code:
class ReUtils(object):
    new_line_finder = re.compile(r"\s*\n\s*")
    white_space_finder = re.compile(r"\s\s+")

    @classmethod
    def white_space_cleaner(cls, text):
        text = cls.new_line_finder.sub("\n", text)
        text = cls.white_space_finder.sub(" ", text)
        return text

if __name__ == '__main__':
   print ReUtils.white_space_cleaner("the text")

rawDescription: Constructing efficient functions in Python


code:
class DemoClassWhichSolvesProblemButWithoutUnnededFunctions(object):
    def __init__(self, arg1, arg2, arg3):
        self.arg1 = arg1
        self.arg2 = arg2
        self.arg3 = arg3

    def controls(self, attribute_value_pair):
        eval_string = attribute_value_pair[0] + " = " + str(attribute_value_pair[1])
        exec(eval_string)

value1, value2, value3 = (1, 2, 3)
my_object = DemoClassWhichSolvesProblemButWithoutUnnededFunctions(0, 0, 0)

print "original values:", my_object.__dict__

inputs = {"A":["self.arg1", value1],
              "S":["self.arg2", value2],
              "D":["self.arg3", value3]}

my_object.controls(inputs["A"]) #my object, not my class...
my_object.controls(inputs["S"])
my_object.controls(inputs["D"])

#Even this one works:
my_object.controls( ("self.totally_new_attribute", "str('other value')") )

print "modified values:", my_object.__dict__

rawDescription: Python: pass string to be referenced in class (self.string)


code:
import os

path = '~/backup/backup_20121216.log'
path = os.path.expanduser(path)
with open(path, 'rb') as fp:
    text = fp.read()
print text

rawDescription: can't access file

description: Can't access file


code:
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor

from socket import SOL_SOCKET, SO_BROADCAST

class EchoClientDatagramProtocol(DatagramProtocol):
    strings = [
        "Hello, world!",
        "What a fine day it is.",
        "Bye-bye!"
    ]

    def startProtocol(self):
        self.transport.socket.setsockopt(SOL_SOCKET, SO_BROADCAST, True)
        #self.transport.connect("255.255.255.255", 8000) <- not needed
        self.sendDatagram()

    def sendDatagram(self):
        if len(self.strings):
            datagram = self.strings.pop(0)
            self.transport.write(datagram, ('255.255.255.255', 8000)) # <- write to broadcast address here
        else:
            reactor.stop()

    def datagramReceived(self, datagram, host):
        print 'Datagram received: ', repr(datagram)
        self.sendDatagram()

def main():
    protocol = EchoClientDatagramProtocol()
    #0 means any port
    t = reactor.listenUDP(0, protocol)
    reactor.run()

if __name__ == '__main__':
   main()

rawDescription: Twisted Python: UDP Broadcast (simple echo server)


code:
from sqlalchemy.types import PickleType

class Foo(Base):
    __tablename__ = 'foo'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    array = Column(PickleType)

foo = Foo(name=u'bar', array=[1, 2, 3])
session.add(foo)
session.commit()

foo = session.query(Foo).filter_by(name=u'bar').one()
print foo.array

rawDescription: How can I insert arrays into columns on a database table for my pyramid app?

description: Insert arrays into columns on a database table for my pyramid app


code:
import subprocess

print("Message from Python... this should appear at 1st line")
p = subprocess.Popen("c:\src\python\redirect2\hi.bat", stdout=subprocess.PIPE)
out, err = p.communicate()

print out

rawDescription: Python : redirected message order

description: Python : redirected message order


code:
import re

text = 'the f<ox jumpe>d over the l<azy> dog <and the >fence'
list = re.findall('.*?\<(.*?)\>.*?', text)

print list

rawDescription: python, list everything between two tags

description: Python, list everything between two tags


code:
from bs4 import BeautifulSoup
#by taking the `html` variable from the question.
html = BeautifulSoup(html)
firstBigTag = html.find_all('big')[0]
nextATag = firstBigTag.find_next('a')
def loopUntilA(text, firstElement):
    text += firstElement.string
    if (firstElement.next.next == nextATag):
        return text
    else:
        #Using double next to skip the string nodes themselves
        return loopUntilA(text, firstElement.next.next)
targetString = loopUntilA('', firstBigTag)
print targetString

rawDescription: How to get all text between just two specified tags using BeautifulSoup?

description: Get all text between just two specified tags using BeautifulSoup


code:
import itertools

l = range(20)

for i in itertools.islice(l,10,15):
    print i

10
11
12
13
14

rawDescription: Python: iterate over a sublist

description: Python: iterate over a sublist


code:
import wx

class TextEntryDialog(wx.Dialog):
    def __init__(self, parent, title, caption):
        style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER
        super(TextEntryDialog, self).__init__(parent, -1, title, style=style)
        text = wx.StaticText(self, -1, caption)
        input = wx.TextCtrl(self, -1, style=wx.TE_MULTILINE)
        input.SetInitialSize((400, 300))
        buttons = self.CreateButtonSizer(wx.OK|wx.CANCEL)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(text, 0, wx.ALL, 5)
        sizer.Add(input, 1, wx.EXPAND|wx.ALL, 5)
        sizer.Add(buttons, 0, wx.EXPAND|wx.ALL, 5)
        self.SetSizerAndFit(sizer)
        self.input = input
    def SetValue(self, value):
        self.input.SetValue(value)
    def GetValue(self):
        return self.input.GetValue()

if __name__ == '__main__':
    app = wx.PySimpleApp()
    dialog = TextEntryDialog(None, 'Title', 'Caption')
    dialog.Center()
    dialog.SetValue('Value')
    if dialog.ShowModal() == wx.ID_OK:
        print dialog.GetValue()
    dialog.Destroy()
    app.MainLoop()

rawDescription: How to make wx.TextEntryDialog larger and resizable

description: Make wx.TextEntryDialog larger and resizable


code:
#! /usr/bin/env python
import subprocess
import csv
import sys

# These added
import os
import shutil
import glob

if len(sys.argv) != 2:
    # Write error messages to stderr
    print >>sys.stderr, 'Give a number for the folder name as an argument'
    quit()

# Why not make these strings in the first place?  Also might be easier as a list
param1 = 0.0001
param2 = 0.0005
param3 = 0.25
param4 = 0.92
param5 = 0.66
param6 = 0.75
param7 = -1
param8 = 2

arg2 = str(param1)
arg3 = str(param2) #each arg is passed to the program
arg4 = str(param3)
arg5 = str(param4)
arg6 = str(param5)
arg7 = str(param6)
arg8 = str(param7)
arg9 = str(param8)

# sys.argv is a list of strings
new_dir = 'Run' + sys.argv[1]

# Remove folder if there and then make it again
# No need to use a shell or the rm and mkdir programs
#subprocess.call(['rm -r '+new_dir+' ; mkdir '+new_dir], shell=True)

shutil.rmtree(new_dir, True)
os.mkdir(new_dir)
os.chdir(new_dir)
cmd = './main' # executable, removed outside the loop

# This was 'loop_params' and 't'
for xdir in [str(i*0.001) for i in xrange(100,270,10)]:

   arg1 = xdir  # each arg is passed to the program

   print "%s %s %s %s %s %s %s %s %s %s " % (cmd,
        arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)

   # This might be where we specify cwd, but might have to create the directory first
   p = subprocess.Popen([cmd,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9])

   # You are not using these, so why are you capturing them?
   # stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   # out, err = p.communicate()
   p.wait()

   stor_dir = new_dir + '/' + xdir

   # No need to call 3 external programs (shell, mkdir, mv) for this
   #subprocess.call(['mkdir '+stor_dir], shell=True)
   #subprocess.call(['mv *txt '+stor_dir],shell=True)
   os.mkdir(stor_dir)
   for fname in glob.iglob('*txt'):
       shutil.move(fname, stor_dir)

# Info on the parameters
this_run = """
Loop over %s
param1 = %s
param2 = %s
param3 = %s
param4 = %s
param5 = %s
param6 = %s
param7 = %s
param7 = %s
""" % (arg1, arg2 ,arg3 ,arg4, arg5, arg6, arg7, arg8, arg8)

# Are you sure this should be a binary file?
with open("this_run.info", "wb") as ofile:
    ofile.write(this_run)

# No need to call 2 external programs (shell, and mv) for this
#subprocess.call(['mv *.info '+new_dir],shell=True)
for fname in glob.iglob('*.info'):
    shutil.move(fname, new_dir)

rawDescription: Launching executable via Python

description: Launching executable via Python


code:
arrayType = ctypes.c_long * 7
array = arrayType(*[1] * 7)
print list(array)
errorCode = FooFunction(array, len(array))
print list(array)

rawDescription: array output from Python ctypes?

description: Array output from Python ctypes


code:
import wx
import gui

class MainFrame(wx.Frame):
    def __init__( self, parent ):
        wx.Frame.__init__ (self, parent)

        # Layout my frame
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.panel = wx.Panel(self)
        self.panel.Layout()
        self.panel.Bind(wx.EVT_RIGHT_DOWN, self.showPopupMenu)
        sizer.Add( self.panel, 1, wx.EXPAND |wx.ALL, 0 )
        self.SetSizer( sizer )
        self.Layout()

        # Add the menu
        self.popupA = wx.NewId()
        self.popupOne = wx.Menu()

        my_a = self.popupOne.Append(self.popupA, "A", kind=wx.ITEM_CHECK)
        self.popupOne.Check(self.popupA, True)

        self.Bind(wx.EVT_MENU, self.commandA, my_a)

        self.menu = wx.Menu()
        self.menu.AppendMenu(-1, 'One', self.popupOne)

    def showPopupMenu(self, event):
        print "in showPopupMenu(): ",self.popupOne.IsChecked(self.popupA)
        self.PopupMenu(self.menu)

    def commandA(self, event):
        event.Skip()
        print "in commandA(): ", self.popupOne.IsChecked(self.popupA)

class app(wx.App):
    def OnInit(self):
        self.m_frame = MainFrame(None)
        self.m_frame.Show()
        self.SetTopWindow(self.m_frame)
        return True

app = app(0)
app.MainLoop()

rawDescription: wxPython's isChecked() responses are inconsistent- why?

description: WxPython's isChecked() responses are inconsistent- why


code:
class someclass(object):
    def __init__(self):
        self.hope = []
        self.seconds = []
    def appendtoseconds(self):
        self.hope.append(self.date)
        for those in self.hope:
            date = those
            pattern = '%m/%d/%Y'
            epoch = int(time.mktime(time.strptime(date, pattern)))
            self.seconds.append(epoch)
        print self.seconds

rawDescription: Producing a list that can be sorted

description: Producing a list that can be sorted


code:
import bisect

qtys = [50, 100, 200, 500, 1000]

def sack(amt, qtys=qtys):
  res = set()
  while amt > 0:
    pivot = bisect.bisect(qtys, amt)
    if sum(qtys[:pivot]) >= amt:
      amt -= qtys[pivot - 1]
      res.add(pivot - 1)
    else:
      if sum(qtys[:pivot + 1]) < amt:
        raise ValueError('Not enough items to fill the sack')
      res.add(pivot)
      amt -= qtys[pivot]
  return res

print sack(150)
print sack(210)
print sack(1777)
print sack(530)

rawDescription: Fetching data from the list

description: Fetching data from the list


code: print repr(a).decode("unicode-escape")

rawDescription: Python print unicode strings in arrays as characters, not code points


code:
for elements in fileListing:
    #add this just to ensure file location
    print os.path.abspath(elements)
    for line in open(elements, 'r'):  #this does a line by line read of the file
        print line

rawDescription: Opening a file in Python given the dirrectory


code:
from subprocess import Popen
Popen('gdb a.out', shell=True).communicate()
print 'test'

rawDescription: Passing control of console window to subprocess


code:
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk, cairo, gio, pango, pangocairo, atk, gobject

class form:
    def typeentry(self, widget, data):
        print "hi"
        if data=="num" and widget.get_active()==True:
            self.buttonS.set_visible(True)
            self.buttonI.set_visible(True)
            self.buttonF.set_visible(True)
            self.buttonL.set_visible(True)
        if data=="num" and widget.get_active()==False:
            self.buttonI.set_visible(False)
            self.buttonS.set_visible(False)
            self.buttonF.set_visible(False)
            self.buttonL.set_visible(False)
    def __init__(self):
        self.win = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.win.set_position(gtk.WIN_POS_CENTER)
        self.win.set_size_request(530, 340)
        self.win.set_resizable(False)

        evbox = gtk.VBox(False, 0)
        frame = gtk.AspectFrame("Select", 1.0, 1.0,3, False)
        frame.set_border_width(10)
        vbox = gtk.VBox(False, 5)
        vbox.set_border_width(5)
        frame.add(vbox)
        vbox.set_uposition(90,165)
        button1 = gtk.CheckButton("Number")
        vbox.pack_start(button1, False, False, 2)
        button1.set_active(True)

        button2 = gtk.CheckButton("String")
        vbox.pack_start(button2, False, False, 0)
        button2.set_active(True)

        self.buttonI = gtk.CheckButton("int")

        vbox.pack_start(self.buttonI, False, False, 0)
        self.buttonI.set_active(True)
        self.buttonI.set_uposition(300,155)

        self.buttonF = gtk.CheckButton("float")

        vbox.pack_start(self.buttonF, False, False, 0)
        self.buttonF.set_active(True)
        self.buttonF.set_uposition(300,170)

        self.buttonL = gtk.CheckButton("long")

        vbox.pack_start(self.buttonL, False, False, 0)
        self.buttonL.set_active(True)
        self.buttonL.set_uposition(300,185)

        self.buttonS = gtk.CheckButton("short")
        vbox.pack_start(self.buttonS, False, False, 0)
        self.buttonS.set_active(True)
        self.buttonS.set_uposition(300,200)

        button1.connect("clicked", self.typeentry, "num")
        self.buttonI.connect("clicked", self.typeentry, "int")
        self.buttonF.connect("clicked", self.typeentry, "float")
        self.buttonL.connect("clicked", self.typeentry, "long")
        self.buttonS.connect("clicked", self.typeentry, "short")
        evbox.add(frame)
        self.win.add(evbox)

        hbox = gtk.HBox(False, 0)
        evbox.pack_start(hbox, False, True, 5)
        hbox.set_uposition(0,295)

        button = gtk.Button("Previous")
        button.connect("clicked", lambda w: gtk.main_quit())
        button.set_size_request(90,35)
        hbox.pack_start(button, False, False,10)

        dbox = gtk.HBox(False, 0)
        button = gtk.Button("Next")
        button.connect("clicked", lambda w: gtk.main_quit())
        button.set_size_request(90,35)
        dbox.pack_end(button, True, True, 5)

        button = gtk.Button("Exit")
        button.set_size_request(90,35)
        dbox.pack_end(button, True, True,5)

        hbox.pack_end(dbox, False,True, 5)

        self.win.show_all()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    first = form()
    first.main()

rawDescription: AttributeError: form instance has no attribute 'buttonI' in pygtk


code: print '\n%s %s' % (row[0].strftime('%Y-%m-%d'), row[1])

rawDescription: Formatting SQL query output in Python/SQLite 3


code:
def print_table(lines, col_num, col_width):
    for line_ix in range(0, len(lines), col_num):
        print ' -- '.join([line.strip().ljust(col_width) for line in lines[line_ix:line_ix+col_num]])

rawDescription: How do I format text from a file in Python?

description: Format text from a file in Python


code:
import urllib

# After you get the code
response = salesforce_oauth_2.get_token(code)
print urllib.urlopen(response['id'])

rawDescription: How to get Salesforce username using REST api?

description: Get Salesforce username using REST api


code:
# Echo server program
import socket

HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 50007              # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    if not data: break
    conn.sendall(data)
conn.close()

rawDescription: When I import socket in python, why does it skip socket.bind?


code:
import twain

index = 0;

def next(ss):
    try:
        print ss.GetImageInfo()
        return True
    except:
        return False

def capture(ss):
    global index
    rv = ss.XferImageNatively()
    fileName = str(index) + '_image.bmp';
    index+=1;
    print rv;
    if rv:
        (handle, count) = rv
        twain.DIBToBMFile(handle, fileName)

sm = twain.SourceManager(0)
ss = sm.OpenSource('Plustek MobileOffice D600')

try:
    ss.SetCapability( twain.CAP_DUPLEXENABLED, twain.TWTY_BOOL, True)
except:
    print "Could not set duplex to True"

print "acquire image"
ss.RequestAcquire(0,0)

while next(ss):
    capture(ss)

print('Done')

rawDescription: python-twain library in duplex mode get separate image from each side

description: Python-twain library in duplex mode get separate image from each side


code:
import boto
import boto.s3
import sys
from boto.s3.key import Key

AWS_ACCESS_KEY_ID = ''
AWS_SECRET_ACCESS_KEY = ''

bucket_name = AWS_ACCESS_KEY_ID.lower() + '-dump'
conn = boto.connect_s3(AWS_ACCESS_KEY_ID,
        AWS_SECRET_ACCESS_KEY)

bucket = conn.create_bucket(bucket_name,
    location=boto.s3.connection.Location.DEFAULT)

testfile = "replace this with an actual filename"
print 'Uploading %s to Amazon S3 bucket %s' % \
   (testfile, bucket_name)

def percent_cb(complete, total):
    sys.stdout.write('.')
    sys.stdout.flush()

k = Key(bucket)
k.key = 'my test file'
k.set_contents_from_filename(testfile,
    cb=percent_cb, num_cb=10)

rawDescription: how to upload a file to directory in S3 bucket using boto

description: Upload a file to directory in S3 bucket using boto


code:
#!/usr/bin/env python

import re
import mechanize

from bs4 import BeautifulSoup

def select_form(form):
    return form.attrs.get('id', None) == 'form1'

def get_state_items(browser):
    browser.select_form(predicate=select_form)
    ctl = browser.form.find_control('ctl00$ContentPlaceHolder1$ddlState')
    state_items = ctl.get_items()
    return state_items[1:]

def get_city_items(browser):
    browser.select_form(predicate=select_form)
    ctl = browser.form.find_control('ctl00$ContentPlaceHolder1$ddlCity')
    city_items = ctl.get_items()
    return city_items[1:]

br = mechanize.Browser()
br.open('http://www.marutisuzuki.com/Maruti-Price.aspx')
br.select_form(predicate=select_form)
br.form['ctl00$ContentPlaceHolder1$ddlmodel'] = ['AK'] # model = Maruti Suzuki Alto K10

for state in get_state_items(br):
    # 1 - Submit form for state.name to get cities for this state
    br.select_form(predicate=select_form)
    br.form['ctl00$ContentPlaceHolder1$ddlState'] = [ state.name ]
    br.submit()

    # 2 - Now the city dropdown is filled for state.name
    for city in get_city_items(br):
        br.select_form(predicate=select_form)
        br.form['ctl00$ContentPlaceHolder1$ddlCity'] = [ city.name ]
        br.submit()

        s = BeautifulSoup(br.response().read())
        t = s.find('table', id='ContentPlaceHolder1_dtDealer')
        r = re.compile(r'^ContentPlaceHolder1_dtDealer_lblName_\d+$')

        header_printed = False
        for p in t.findAll('span', id=r):
            tr = p.findParent('tr')
            td = tr.findAll('td')

            if header_printed is False:
                str = '%s, %s' % (city.attrs['label'], state.attrs['label'])
                print str
                print '-' * len(str)
                header_printed = True

            print ' '.join(['%s' % x.text.strip() for x in td])

rawDescription: Web Scraper for dynamic forms in python

description: Web Scraper for dynamic forms in python


code:
import theano
import numpy as np
v = np.arange(10)
var = theano.tensor.vector()
out = theano.tensor.eq(var, 2).nonzero()[0]
print out.eval({var: v})

rawDescription: Returning the index of a value in Theano vector


code:
def link_strings(xs):
        string1 = ''
        for i in range(len(xs)):
            try:
                string1 += str(xs[i])
            except:
                print "not a string"
        return string1

rawDescription: try, except block concatenate each spot in a list to some combined string variable

description: Try, except block concatenate each spot in a list to some combined string variable


code:
def closureMaker():
    x = [0]
    def closure():
        x[0] += 1
        print x[0]
    return closure

rawDescription: Python closure with side-effects

description: Python closure with side-effects


code:
import os

path = "C:\\WorkingFolder\\"

# Check current working directory.
retval = os.getcwd()

print "Current working directory %s" % retval

# Now change the directory
os.chdir( path )

# Check current working directory.
retval = os.getcwd()

print "Directory changed successfully %s" % retval

rawDescription: Python reading files in different local drive locations


rawDescription: Difficulty in using sympy solver in python


code:
def parsefile(filename):
  ret = {}
  cur = None
  for line in ( x.strip() for x in open(filename,'r')):
    if line.startswith('d.complex'):
      cur = set()
      ret[line] = cur
    if not cur or not line.isdigit():
      continue
    cur.add(int(line))
  return ret

def compareStructures(first,second):
  # Iterate through key,value pairs in first
  for firstcmplx, firstmembers in first.iteritems():
    # Iterate through key,value pairs in second
    for secondcmplx, secondmembers in second.iteritems():
      notinsecond = firstmembers- secondmembers
      if notinsecond:
        # There are items in first that aren't in second
        print firstcmplx
        print secondcmplx
        print "\n".join([ str(x) for x in notinsecond])

first = parsefile("myFirstFile.txt")
second = parsefile("mySecondFile.txt")

compareStructures(first,second)

rawDescription: More efficient solution to loop nesting required


code:
CBase = namedtuple('C', ['x', 'y'])

class C(CBase):
    __dict__ = property(CBase._asdict)

print C(1, 2).__dict__

rawDescription: Dynamically add a class __dict__ property in Python


code:
import os
import time

os.system("...../abc.bat")

while True:
    try:
        o = open("test.txt", "r")
        break
    except:
        print "waiting the bat.."
        time.sleep(15) # Modify (seconds) to se sufficient with your bat to be ready
        continue

rawDescription: wait statement in python

description: Wait statement in python


code:
contours, _ = cv2.findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
    rect = cv2.boundingRect(c)
    if rect[2] < 5 or rect[3] < 5: continue
    print cv2.contourArea(c)

rawDescription: Blob detection with python opencv

description: Blob detection with python opencv


code:
import csv
for row in csv.reader(['num1, 123456789', 'num2, 987654321', 'num3, +23456789']):
    phoneNumber = row[1].strip()
    if not phoneNumber.startswith('+'):
        phoneNumber = '+' + phoneNumber
    print phoneNumber

rawDescription: adding '+' to all the numbers as a prefix (numbers are stored in a csv file) using a python script

description: Adding '+' to all the numbers as a prefix (numbers are stored in a csv file) using a python script


code:
# Save the string (e.g. '12.5,67.4,78.5')
number_string = raw_input("Enter any positive floats: ")

# Initialize a variable to hold the sum of the numbers
tot = 0

# Iterate over all numbers in the string. The string is splitted
# at each comma, thus resulting in a list like `['12.5', '67.4', '78.5']`
for num in number_string.split(','):
    # But every element in the list is a string, so you need to
    # convert them to floats, and add each of them to the total sum
    tot += float(num)

# Print the total sum
print tot

rawDescription: Extracting Multiple floating points from string in python 2.7


code:
import difflib
file1 = "PATH OF FILE 1"
file1 = open(file1, "r")
file2 = "PATH OF FILE 2"
file2 = open(file2, "r")
diff = difflib.ndiff(file1.readlines(), file2.readlines())
file1.close()
file2.close()
delta = ''.join(x[2:] for x in diff if x.startswith('- '))
print delta

rawDescription: Calculation of Set A - Set B in Python

description: Calculation of Set A - Set B in Python


code:
import sys, re

annot = file("example.embl", "r")
embl = ""
annotation = []

for line in annot:
    embl += line

embl_list = embl.split("FT   gen")

for item in embl_list:
    if "e            " in item:
        split_item = item.split("\n")
        for l in split_item:
            if "e            " in l:
                if not "complement" in l:
                    coordinates = l[13:len(l)]
                    C = coordinates.split("..")
                    genestart = C[0]
                    geneend = C[1]
                    strand = "+"
                if "complement" in l:
                    coordinates = l[24:len(l)-1]
                    C = coordinates.split("..")
                    genestart = C[0]
                    geneend = C[1]
                    strand = "-"

            if "/locus_tag" in l:
                L = l.split('"')
                locus = L[1]

            if "/product" in l:
                P = l.split('"')
                product = P[1]

        annotation.append({
            "locus": locus,
            "genestart": genestart,
            "geneend": geneend,
            "product": product,
        })
    else:
        print "Finished!"

rawDescription: From one File to Multiple Dictionaries in Python


code:
import unittest

class CommonTests(object):
    def testCommon(self):
        print 'Calling BaseTest:testCommon'
        value = 5
        self.assertEquals(value, 5)

class SubTest1(unittest.TestCase, CommonTests):

    def testSub1(self):
        print 'Calling SubTest1:testSub1'
        sub = 3
        self.assertEquals(sub, 3)

class SubTest2(unittest.TestCase, CommonTests):

    def testSub2(self):
        print 'Calling SubTest2:testSub2'
        sub = 4
        self.assertEquals(sub, 4)

if __name__ == '__main__':
    unittest.main()

rawDescription: Python unit test with base and sub class


code:
import xlrd

wb = xlrd.open_workbook("links.xls")
sh = wb.sheet_by_index(0)

count = 1
for row in range(1, sh.nrows):
    val = sh.row_values(row)

    if (val[4]) != 2:
        print 'id',str(int(count)), 'value', str(int(val[1])) + "-" + str(int(val[2]))
    else:
        print 'id',str(int(count)), 'value', str(int(val[1])) + "-" + str(int(val[2]))
        count+=1
        print 'id',str(int(count)), 'value', str(int(val[2])) + "-" + str(int(val[1]))
    count+=1

rawDescription: Serialize the outcome of a loop

description: Serialize the outcome of a loop


code:
def printMaze(maze, x, y):
    for i in range(9):
        for z in range(4):
            if i == x and z == y:
                print 'X',
            else:
                print maze[i][z],
        print ''

rawDescription: Python: Why isn't this working?

description: Python: Why isn't this working


code:
import unicodedata
if unicodedata.combining(u'a'):
    print "is combining character"
else:
    print "is not combining"

rawDescription: tell whether a character is a combining diacritic mark

description: Tell whether a character is a combining diacritic mark


rawDescription: How to convert an H:MM:SS time string to seconds in Python?

description: Convert an H:MM:SS time string to seconds in Python


code:
import threading
import webbrowser

def start_browser(server_ready_event, url):
    print "[Browser Thread] Waiting for server to start"
    server_ready_event.wait()
    print "[Browser Thread] Opening browser"
    webbrowser.open(url)

url = "someurl"
server_ready = threading.Event()
browser_thread = threading.Thread(target=start_browser, args=(server_ready, url))
browser_thread.start()

print "[Main Thread] Starting server"
httpd = make_server('', 80, server)
print "[Main Thread] Server started"
server_ready.set()

httpd.serve_forever()
browser_thread.join()

rawDescription: python webbrowser.open(url)

description: Python webbrowser.open(url)


code:
from lxml import etree
root = etree.fromstring(xml)

replace_set = {}
for node in root.iter("Node"):
    if 'NodeRef' in [c.tag for c in node]:
        # This is a <Node> type with child element <NodeRef>. So it will
        # be referenced by a <Node> with <NodeName>. Let's keep it, and then
        # remove it from the tree.
        ref = node.find("NodeRef").text
        inner = node.find("InnerNode")
        replace_set[ref] = inner
        # Remove NodeRef element, as we've saved it in dict
        node.getparent().remove(node)

# Cleanup where we've removed NodeRefs.
for node in root.iter("Paths"):
    if len(node.find("Path")) == 0:
        node.getparent().remove(node)

# Replace references to NodeRefs
for node in root.iter("NodeRef"):
    if node.text in replace_set:
        node.getparent().replace(node, replace_set[ref])

print etree.tostring(root)

rawDescription: How to merge two different paths in a XML file?

description: Merge two different paths in a XML file


code:
availLicense = []

if arcpy.CheckExtension("3D") == "Available":
    availLicense.append("ArcGIS 3D Analyst")
if arcpy.CheckExtension("Network") == "Available":
    availLicense.append("ArcGIS Network Analyst")
if arcpy.CheckExtension("Spatial") == "Available":
    availLicense.append("ArcGIS Spatial Analyst")

print "The following extensions are available: " + str(availLicense).strip('[]')

rawDescription: How can I check multiple extensions in arcpy and print an interesting statement?

description: Check multiple extensions in arcpy and print an interesting statement


code:
from functools import wraps

def outer(a):
    @wraps(outer)
    def inner(x):
        return x*a
    return inner
p = outer(3)
print p  # <function outer at ...>

rawDescription: Get name of outer function from inner function object in python


code:
string = 'Hello There'
print ' '.join([string[:5]] * 2)

rawDescription: Python repeat string

description: Python repeat string


code:
import re
# Sample input for the following code.
blogs = ["This is a blog.","This is another blog.","Cats? Cats are awesome."]
# This is a list that will contain dictionaries counting the wordcounts for each blog
wordcount = []
# This is a list of all unique words in all blogs.
wordlist = []
# Consider each blog sequentially
for blog in blogs:
    # Remove all the non-alphanumeric, non-whitespace characters,
    # and then split the string at all whitespace after converting to lowercase.
    # eg: "That's not mine." -> "Thats not mine" -> ["thats","not","mine"]
    words = re.sub("\s+"," ",re.sub("[^\w\s]","",blog)).lower().split(" ")
    # Add a new dictionary to the list. As it is at the end,
    # it can be referred to by wordcount[-1]
    wordcount.append({})
    # Consider each word in the list generated above.
    for word in words:
        # If that word has been encountered before, increment the count
        if word in wordcount[-1]: wordcount[-1][word]+=1
        # Else, create a new entry in the dictionary
        else: wordcount[-1][word]=1
        # If it is not already in the list of unique words, add it.
        if word not in wordlist: wordlist.append(word)

# We now have wordlist, which has a unique list of all words in all blogs.
# and wordcount, which contains len(blogs) dictionaries, containing word counts.
# Matrix is the table that you need of wordcounts. The number of rows will be
# equal to the number of unique words, and the number of columns = no. of blogs.
matrix = []
# Consider each word in the unique list of words (corresponding to each row)
for word in wordlist:
    # Add as many columns as there are blogs, all initialized to zero.
    matrix.append([0]*len(wordcount))
    # Consider each blog one by one
    for i in range(len(wordcount)):
        # Check if the currently selected word appears in that blog
        if word in wordcount[i]:
            # If yes, increment the counter for that blog/column
            matrix[-1][i]+=wordcount[i][word]

# For printing matrix, first generate the column headings
temp = "\t"
for i in range(len(blogs)):
    temp+="Blog "+str(i+1)+"\t"

print temp
# Then generate each row, with the word at the starting, and tabs between numbers.

for i in range(len(matrix)):
    temp = wordlist[i]+"\t"
    for j in matrix[i]: temp += str(j)+"\t"
    print temp

rawDescription: How to create a two-dimensional array in Python?

description: Create a two-dimensional array in Python


code:
fig = plt.figure()
maxRange = 0
for file_name in os.listdir(source):
    print 'Processing file : ' + str(file_name)
    with open(os.path.join(source,file_name), 'rb') as pkl_file:
        lst = pickle.load(pkl_file)
    x,y = zip(*[(dt.datetime.strptime(x_i,'%Y-%m-%d'), y_i) for (x_i, y_i) in lst])
    if max(y) < 5 :
        print 'The word #' +str(file_name[0]) + ' has a frequency of ' +str(maxRange) + '.'
    else :
        maxRange=max(maxRange, max(y))
        plt.plot(x, y , 'm')
plt.ylim(0,maxRange)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
plt.gca().xaxis.set_major_locator(mdates.YearLocator())
plt.xlabel('Time')
plt.ylabel('Frequency')
fig.savefig("Plots/allfiles.pdf")

rawDescription: how to plot multiple lines while reading x and y from files in a for loop?

description: Plot multiple lines while reading x and y from files in a for loop


code: print binascii.hexlify('012')

rawDescription: network data in the form of hex convertion to ascii in python issue

description: Network data in the form of hex convertion to ascii in python issue


code:
self.menu_xml_id = 'base.menu_action_res_users'

module_name, menu_xml_id = self.menu_xml_id.split('.')

dummy, menu_id = self.registry('ir.model.data').get_object_reference(self.cr, self.uid, module_name, menu_xml_id)

ir_ui_menu = self.registry('ir.ui.menu').browse(self.cr, self.uid, menu_id, context=None)

print "action_id==============", ir_ui_menu.action

rawDescription: How to find action ids of menu items in odoo?

description: Find action ids of menu items in odoo


code:
from struct import Struct

handshake = Struct('B19s8B20s20s')

pstrlen = 19
pstr = 'BitTorrent protocol'
info_hash = 'x' * 20
peer_id = 'y' * 20

pkg = handshake.pack(pstrlen, pstr, 0,0,0,0,0,0,0,0, info_hash, peer_id)
print len(pkg)

rawDescription: Why doesn't the packed length of my data correspond to what was declared in the struct?


code:
import matplotlib.pyplot as plt
Blues = plt.get_cmap('Blues')
print Blues(0)
print Blues(0.5)
print Blues(1.0)

rawDescription: How to convert data values into color information for matplotlib?

description: Convert data values into color information for matplotlib


code:
import math
import random

def distL2((x1,y1), (x2,y2)):
    """Compute the L2-norm (Euclidean) distance between two points.

    The distance is rounded to the closest integer, for compatibility
    with the TSPLIB convention.

    The two points are located on coordinates (x1,y1) and (x2,y2),
    sent as parameters"""
    xdiff = x2 - x1
    ydiff = y2 - y1
    return int(math.sqrt(xdiff*xdiff + ydiff*ydiff) + .5)

def distL1((x1,y1), (x2,y2)):
    """Compute the L1-norm (Manhattan) distance between two points.

    The distance is rounded to the closest integer, for compatibility
    with the TSPLIB convention.

    The two points are located on coordinates (x1,y1) and (x2,y2),
    sent as parameters"""
    return int(abs(x2-x1) + abs(y2-y1)+.5)

def mk_matrix(coord, dist):
    """Compute a distance matrix for a set of points.

    Uses function 'dist' to calculate distance between
    any two points.  Parameters:
    -coord -- list of tuples with coordinates of all points, [(x1,y1),...,(xn,yn)]
    -dist -- distance function
    """
    n = len(coord)
    D = {}      # dictionary to hold n times n matrix
    for i in range(n-1):
        for j in range(i+1,n):
            (x1,y1) = coord[i]
            (x2,y2) = coord[j]
            D[i,j] = dist((x1,y1), (x2,y2))
            D[j,i] = D[i,j]
    return n,D

def read_tsplib(filename):
    "basic function for reading a TSP problem on the TSPLIB format"
    "NOTE: only works for 2D euclidean or manhattan distances"
    f = open(filename, 'r');

    line = f.readline()
    while line.find("EDGE_WEIGHT_TYPE") == -1:
        line = f.readline()

    if line.find("EUC_2D") != -1:
        dist = distL2
    elif line.find("MAN_2D") != -1:
        dist = distL1
    else:
        print "cannot deal with non-euclidean or non-manhattan distances"
        raise Exception

    while line.find("NODE_COORD_SECTION") == -1:
        line = f.readline()

    xy_positions = []
    while 1:
        line = f.readline()
        if line.find("EOF") != -1: break
        (i,x,y) = line.split()
        x = float(x)
        y = float(y)
        xy_positions.append((x,y))

    n,D = mk_matrix(xy_positions, dist)
    return n, xy_positions, D

def mk_closest(D, n):
    """Compute a sorted list of the distances for each of the nodes.

    For each node, the entry is in the form [(d1,i1), (d2,i2), ...]
    where each tuple is a pair (distance,node).
    """
    C = []
    for i in range(n):
        dlist = [(D[i,j], j) for j in range(n) if j != i]
        dlist.sort()
        C.append(dlist)
    return C

def length(tour, D):
    """Calculate the length of a tour according to distance matrix 'D'."""
    z = D[tour[-1], tour[0]]    # edge from last to first city of the tour
    for i in range(1,len(tour)):
        z += D[tour[i], tour[i-1]]      # add length of edge from city i-1 to i
    return z

def randtour(n):
    """Construct a random tour of size 'n'."""
    sol = range(n)      # set solution equal to [0,1,...,n-1]
    random.shuffle(sol) # place it in a random order
    return sol

def nearest(last, unvisited, D):
    """Return the index of the node which is closest to 'last'."""
    near = unvisited[0]
    min_dist = D[last, near]
    for i in unvisited[1:]:
        if D[last,i] < min_dist:
            near = i
            min_dist = D[last, near]
    return near

def nearest_neighbor(n, i, D):
    """Return tour starting from city 'i', using the Nearest Neighbor.

    Uses the Nearest Neighbor heuristic to construct a solution:
    - start visiting city i
    - while there are unvisited cities, follow to the closest one
    - return to city i
    """
    unvisited = range(n)
    unvisited.remove(i)
    last = i
    tour = [i]
    while unvisited != []:
        next = nearest(last, unvisited, D)
        tour.append(next)
        unvisited.remove(next)
        last = next
    return tour

def exchange_cost(tour, i, j, D):
    """Calculate the cost of exchanging two arcs in a tour.

    Determine the variation in the tour length if
    arcs (i,i+1) and (j,j+1) are removed,
    and replaced by (i,j) and (i+1,j+1)
    (note the exception for the last arc).

    Parameters:
    -t -- a tour
    -i -- position of the first arc
    -j>i -- position of the second arc
    """
    n = len(tour)
    a,b = tour[i],tour[(i+1)%n]
    c,d = tour[j],tour[(j+1)%n]
    return (D[a,c] + D[b,d]) - (D[a,b] + D[c,d])

def exchange(tour, tinv, i, j):
    """Exchange arcs (i,i+1) and (j,j+1) with (i,j) and (i+1,j+1).

    For the given tour 't', remove the arcs (i,i+1) and (j,j+1) and
    insert (i,j) and (i+1,j+1).

    This is done by inverting the sublist of cities between i and j.
    """
    n = len(tour)
    if i>j:
        i,j = j,i
    assert i>=0 and i<j-1 and j<n
    path = tour[i+1:j+1]
    path.reverse()
    tour[i+1:j+1] = path
    for k in range(i+1,j+1):
        tinv[tour[k]] = k

def improve(tour, z, D, C):
    """Try to improve tour 't' by exchanging arcs; return improved tour length.

    If possible, make a series of local improvements on the solution 'tour',
    using a breadth first strategy, until reaching a local optimum.
    """
    n = len(tour)
    tinv = [0 for i in tour]
    for k in range(n):
        tinv[tour[k]] = k  # position of each city in 't'
    for i in range(n):
        a,b = tour[i],tour[(i+1)%n]
        dist_ab = D[a,b]
        improved = False
        for dist_ac,c in C[a]:
            if dist_ac >= dist_ab:
                break
            j = tinv[c]
            d = tour[(j+1)%n]
            dist_cd = D[c,d]
            dist_bd = D[b,d]
            delta = (dist_ac + dist_bd) - (dist_ab + dist_cd)
            if delta < 0:       # exchange decreases length
                exchange(tour, tinv, i, j);
                z += delta
                improved = True
                break
        if improved:
            continue
        for dist_bd,d in C[b]:
            if dist_bd >= dist_ab:
                break
            j = tinv[d]-1
            if j==-1:
                j=n-1
            c = tour[j]
            dist_cd = D[c,d]
            dist_ac = D[a,c]
            delta = (dist_ac + dist_bd) - (dist_ab + dist_cd)
            if delta < 0:       # exchange decreases length
                exchange(tour, tinv, i, j);
                z += delta
                break
    return z

def localsearch(tour, z, D, C=None):
    """Obtain a local optimum starting from solution t; return solution length.

    Parameters:
      tour -- initial tour
      z -- length of the initial tour
      D -- distance matrix
    """
    n = len(tour)
    if C == None:
        C = mk_closest(D, n)     # create a sorted list of distances to each node
    while 1:
        newz = improve(tour, z, D, C)
        if newz < z:
            z = newz
        else:
            break
    return z

def multistart_localsearch(k, n, D, report=None):
    """Do k iterations of local search, starting from random solutions.

    Parameters:
    -k -- number of iterations
    -D -- distance matrix
    -report -- if not None, call it to print verbose output

    Returns best solution and its cost.
    """
    C = mk_closest(D, n) # create a sorted list of distances to each node
    bestt=None
    bestz=None
    for i in range(0,k):
        tour = randtour(n)
        z = length(tour, D)
        z = localsearch(tour, z, D, C)
        if z < bestz or bestz == None:
            bestz = z
            bestt = list(tour)
            if report:
                report(z, tour)

    return bestt, bestz

if __name__ == "__main__":
    """Local search for the Travelling Saleman Problem: sample usage."""

    #
    # test the functions:
    #

    # random.seed(1)    # uncomment for having always the same behavior
    import sys
    if len(sys.argv) == 1:
        # create a graph with several cities' coordinates
        coord = [(4,0),(5,6),(8,3),(4,4),(4,1),(4,10),(4,7),(6,8),(8,1)]
        n, D = mk_matrix(coord, distL2) # create the distance matrix
        instance = "toy problem"
    else:
        instance = sys.argv[1]
        n, coord, D = read_tsplib(instance)     # create the distance matrix
        # n, coord, D = read_tsplib('INSTANCES/TSP/eil51.tsp')  # create the distance matrix

    # function for printing best found solution when it is found
    from time import clock
    init = clock()
    def report_sol(obj, s=""):
        print "cpu:%g\tobj:%g\ttour:%s" % \
              (clock(), obj, s)

    print "*** travelling salesman problem ***"
    print

    # random construction
    print "random construction + local search:"
    tour = randtour(n)     # create a random tour
    z = length(tour, D)     # calculate its length
    print "random:", tour, z, '  -->  ',
    z = localsearch(tour, z, D)      # local search starting from the random tour
    print tour, z
    print

    # greedy construction
    print "greedy construction with nearest neighbor + local search:"
    for i in range(n):
        tour = nearest_neighbor(n, i, D)     # create a greedy tour, visiting city 'i' first
        z = length(tour, D)
        print "nneigh:", tour, z, '  -->  ',
        z = localsearch(tour, z, D)
        print tour, z
    print

    # multi-start local search
    print "random start local search:"
    niter = 100
    tour,z = multistart_localsearch(niter, n, D, report_sol)
    assert z == length(tour, D)
    print "best found solution (%d iterations): z = %g" % (niter, z)
    print tour

rawDescription: Python Traveling Salesman Greedy Algorithm


code:
import os
import multiprocessing

def create_file(e):
    with open('files/file%d.txt'%e, 'w') as f:
        print f.name
        f.write("hi")

if __name__ == '__main__':
    if not os.path.exists('files'):
            os.makedirs('files')
    pool = multiprocessing.Pool(processes=4)
    pool.map(create_file, range(10))

rawDescription: Writing files with a pool of workers

description: Writing files with a pool of workers


code:
import simplejson as json
import sys
import urllib
import traceback, csv, string

# "base" API URL
URL_BASE = 'Some URL'
# set user agent string
urllib.version = "Data Collection Fix it"

page_ids = []

def divide_list(list_, n):
    for i in range(0, len(list_), n):
        yield list_[i:i + n]

def issue_query():

    iFile = open('ReadFromThisFile.txt', "r")
    lines = iFile.readlines()
    #print len(lines)

    for line in lines:
        ids = string.split(line)
        ids = ids[0]
        page_ids.append(ids)

    url = URL_BASE
    indicies = range(len(page_ids))
    File = open("WriteToThisFile.csv", "w")
    for indicies in divide_list(page_ids, 50):
        count = 0
        fiftyIds =[]
        url = URL_BASE
        for id in indicies:
            str(id).strip
            url += str(id) + '|'
            print url
            fiftyIds.append(str(id))
            count += 1
        print count
        rv = urllib.urlopen(url)
        j = rv.read().decode("utf-8")
        #sys.stderr.write(j + "\n")
        data = json.loads(j)
        for id in fiftyIds:
            try:
                s = int(data["query"]["pages"][id]["revisions"][0]["size"])
                sys.stderr.write("%d\t%d\n" % (int(id), s))
                File.write("%d\t%d\n" % (int(id), s))
                #print ("%d\t%d\n" % (int(id), s))
                # do something interesting with id and s
            except Exception, e:
                traceback.print_exc()

    File.close()
    iFile.close()

issue_query()

rawDescription: Python: divide an array into section

description: Python: divide an array into section


code:
myString = 'Join us!https://t.co/Fe0oTahdom'

(head, sep, tail) = myString.partition('https')

print head  #Prints Join us!
print sep + tail #Prints the rest

rawDescription: splitting a string if a substring has occured

description: Splitting a string if a substring has occured


code:
import sys
import threading
import time
import select

def timeout_read(n_chars):
  r, _, _ = select.select([sys.stdin], [], [], 1)
  return r[0].read(n_chars) if r else ""

class MyThread (threading.Thread):
    def run (self):
        try:
            self.wantQuit = 0
            while not self.wantQuit:
                print "want input"
                button = timeout_read(1)
                if button == "q":
                    self.wantQuit=1
        except KeyboardInterrupt:
            self.wantQuit = 1
            print "abort with KeyboardInterrupt"
        print "done mythread"

myThread = MyThread ()
myThread.start()

a=5
while not myThread.wantQuit:
    print "hey"
    if (a == 0):
        break;
    a = a-1;
    time.sleep(1)
myThread.wantQuit=1
print "main thread done"

rawDescription: python, funny business with threads and IDEs?

description: Python, funny business with threads and IDEs