code:
import cmath

def largest_spoke_angle(centre, peripheral):
     per_from_centre = [complex(z[0]-centre[0], z[1]-centre[1]) for z in peripheral]
     per_angles = [cmath.phase(z) for z in per_from_centre]
     per_angles.sort()

     differences = [ per_angles[n+1]-per_angles[n] for n in range(len(per_angles)-1)] \
                    + [per_angles[0] +2*cmath.pi - per_angles[-1]]

     return max(differences)#in radians

centre = (0.,0.)
peripheral = [(1.,2.),(3.,4.),(3.,5.)]
print largest_spoke_angle(centre, peripheral)

rawDescription: Find the largest angle made by different points at the center


code:
import numpy
import cPickle as pickle

N = 1000
a0 = pickle.dumps(numpy.zeros(N))
a1 = pickle.dumps(numpy.zeros(N), protocol=-1)

print "a0", len(a0)   # 32155
print "a1", len(a1)   #  8133

rawDescription: passing numpy arrays through multiprocessing.Queue

description: Passing numpy arrays through multiprocessing.Queue


code:
syns = collections.defaultdict(list)

for w in wordList:
    hash = tuple(w.index(c) for c in w)
    syns[hash].append(w)

print syns.values()

rawDescription: Conceptual: Collect "synonyms" from a list of "words"


code:
l = ['a','b','c','d']
if 'g' in l:
    print True

rawDescription: Python: Search list and give boolean

description: Python: Search list and give boolean


code:
d={" 'John'": "'car': 2, 'laptop': 4, 'comp': 3"," 'Jim'": "'car':2, 'laptop':3 ,'computer':2"}
new_d = {}
for x in d: #x will be 'John' and 'Jim', notice the single quotes will be remained because it's part of the string while double quotes removed as the mark of string
    code = 'new_d['+x+']={'+d[x]+'}' # new_d['John']={'car': 2, 'laptop': 4, 'comp': 3}
    exec code # This means execute string code as python expression
print new_d

rawDescription: how to remove unnecessary double quote from dictionary

description: Remove unnecessary double quote from dictionary


code:
import os
import sys

def directory_processor(source_dir, out_dir, worker_func, *xs, **kws):
    for root, dirs, files in os.walk(source_dir):
        for f in files:
            in_img = os.path.join(root, f)
            worker_func(in_img, out_dir, *xs, **kws)

# One of your image-processing functions.
def desaturate(in_img, out_dir, *xs, **kws):
    print 'desaturating', in_img, out_dir, xs, kws

# To experiment with this script, pass the source_dir and
# out_dir as command-line arguments.
directory_processor(sys.argv[1], sys.argv[2], desaturate, 'blort', foo = 1)

rawDescription: Python - How to make a general batch function for many similarly-structured functions


code:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base, DeferredReflection

Base = declarative_base(cls=DeferredReflection)

e = create_engine("sqlite://", echo=True)
e.execute("""
    create table "user table" (
            "id col" integer primary key,
            "data col" varchar(30)
    )
""")

from sqlalchemy import event

@event.listens_for(Table, "column_reflect")
def reflect_col(inspector, table, column_info):
    column_info['key'] = column_info['name'].replace(' ', '_')

class User(Base):
    __tablename__ = "user table"

Base.prepare(e)

s = Session(e)
print s.query(User).filter(User.data_col == "some data")

rawDescription: sqlalchemy - reflecting tables and columns with spaces

description: Sqlalchemy - reflecting tables and columns with spaces


code:
def convertVector(numbers):
    d = {}
    for k, c in enumerate(numbers):
        if c:
            d[k] = c
    return d

print convertVector([1, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 4])
print convertVector([1, 0, 1 , 0, 2, 0, 1, 0, 0, 1, 0])
print convertVector([0, 0, 0, 0, 0])

rawDescription: sparse vector in python?

description: Sparse vector in python


code:
import ogr

driver = ogr.GetDriverByName('ESRI Shapefile')

lineshp = driver.Open('U:/My Documents/Tool/shp/line.shp', 0)
linelyr = lineshp.GetLayer()

pointshp = driver.Open('U:/My Documents/Tool/shp/point.shp', 0)
pointlyr = pointshp.GetLayer()

linefeat = linelyr.GetNextFeature()
pointfeat = pointlyr.GetNextFeature()

point_geom = pointfeat.GetGeometryRef()

distlist = []
while linefeat:
    line_geom = linefeat.GetGeometryRef()
    dist = point_geom.Distance(line_geom)
    distlist.append(dist)
    linefeat.Destroy()
    linefeat = linelyr.GetNextFeature()

print min(distlist)

rawDescription: OGR Distance Units

description: OGR Distance Units


code:
data = ((1,), (3,))
print [x[0] for x in data]

rawDescription: Python: Anyway to use map to get first element of a tuple


code:
d = {'Achievement': ['hi', 'hey', 'hello']}
key = 'Achievement'
index = 2
print d[key][index]

rawDescription: Accessing variables names from a list in python


code:
import xml.etree.ElementTree as etree
data = """
<root>
    <person/>
    <person></person>
</root>
"""
tree = etree.fromstring(data)
print etree.tostring(tree, method="html")

rawDescription: do not collapse empty nodes in XML output

description: Do not collapse empty nodes in XML output


code:
#run my operation
for ndvi, ndii, rgr, swir32,  in zip(NDVIraster, NDIIraster,RGRraster,SWIR32raster):
    outpath='F:\Sheyenne\Normalized_Indices\Fuzzy_Membership\\'
    outpath=outpath + ndvi
    outraster= arcpy.gp.FuzzyOverlay_sa([ndvi, ndii, rgr, swir32], outpath, "AND")

rawDescription: Specifying output path with loop on zipped list


code:
import ast

class GetAssignments(ast.NodeVisitor):
    def visit_Name(self, node):
        if isinstance(node.ctx, ast.Store):
            print node.id, node.lineno

with open('testing.py') as fin:
    module = ast.parse(fin.read())
    GetAssignments().visit(module)

rawDescription: get line number which variable is defined in a different file

description: Get line number which variable is defined in a different file


rawDescription: Getting just one MySQL column as a list and not a tuple


code:
class SubclassedDictionary(dict):
    def __init__(self, new_dict, condition=None, *args, **kwargs):
        super(SubclassedDictionary, self).__init__(new_dict, *args, **kwargs)
        self.paths = []
        self.get_paths(condition)

    def _get_paths_recursive(self, condition, iterable, parent_path=[]):
        path = []
        for key, value in iterable.iteritems():
            # If we find an iterable, recursively obtain new paths.
            if isinstance(value, (dict, list, set, tuple)):
                # Make sure to remember where we have been (parent_path + [key])
                recursed_path = self._get_paths_recursive(condition, value, parent_path + [key])
                if recursed_path:
                    self.paths.append(parent_path + recursed_path)
            elif condition(value) is True:
                self.paths.append(parent_path + [key])

    def get_paths(self, condition=None):
        # Condition MUST be a function that returns a bool!
        self.paths = []
        if condition is not None:
            return self._get_paths_recursive(condition, self)

def my_condition(value):
    try:
        return int(value) > 100
    except ValueError:
        return False

my_dict = SubclassedDictionary({"id": {"value1": 144,
                                       "value2": "steve",
                                       "more": {"id": 114}},
                                "attributes": "random"},
                               condition=my_condition)

print my_dict.paths  # Returns [['id', 'value1'], ['id', 'more', 'id']]

rawDescription: How do I create a dictionary that indexes the location of certain keys?

description: Create a dictionary that indexes the location of certain keys


code:
class heap:
     def __init__(self,the_heap):
         self.heap = the_heap
     def getChildren(self,value):
         n = self.heap.index(value)
         return self.heap[2*n+1],self.heap[2*n+2] # i think ...
     def getParent(self,value):
         n = self.heap.index(value)
         if n == 0: return None
         return self.heap[math.floor(n-1/2.0) ] # i think ...
     def traverse(self):
         #do your traversal here just visit each node in the order you want
         pass

the_heap = heap(range(100))

print the_heap.getChildren(2)
print the_heap.getParent(6)

rawDescription: Building a Tree with inorder and preorder traversal in Python3.x


code:
def list_step(max = 268):
    """ split in steps of 120 elements at time
    """
    if max > 120:
        rest_list = [];
        for i in xrange(0,max,120):
            if i <= max:
                rest =  max % xrange(0,max,120)[-1]
                if rest != 120:
                    yield(i)
                    rest_list.append(rest)
                else:
                    yield(i)
                    rest_list.append(0)
        for i in rest_list:
            yield(i)
    else:
        yield(max)

a = list_step()
for i in a: print i;

rawDescription: Python best practices xrange yield

description: Python best practices xrange yield


code:
import sys, traceback

def run_user_code(envdir):
    source = raw_input(">>> ")
    try:
        exec source in envdir
    except:
        print "Exception in user code:"
        print '-'*60
        traceback.print_exc(file=sys.stdout)
        print '-'*60

envdir = {}
while 1:
    run_user_code(envdir)

rawDescription: Django stack traces are awesome. How can I get one outside Django?


code:
def distribute(potential, defender_resources):
    attacker_resources = [0] * len(defender_resources)

    while potential and any(defender_resources):
        for i in range(len(defender_resources)):
            if potential and defender_resources[i]:
                defender_resources[i] -= 1
                attacker_resources[i] += 1
                potential -= 1

    return attacker_resources

print distribute(250, [100,0,1,0])
print distribute(250, [100,25,76,5000])
print distribute(250, [2500,2500,2500,2500])
print distribute(250, [5000])

rawDescription: (Python) Distributing numbers equally

description: (Python) Distributing numbers equally


code:
class ThreadedTCPStreamServer(ThreadingMixin, TCPServer):
    def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True,
                 queue=None):
        self.queue = queue
        TCPServer.__init__(self, server_address, RequestHandlerClass,
                           bind_and_activate=bind_and_activate)

class ThreadedTCPStreamHandler(SocketServer.StreamRequestHandler):
    def __init__(self, request, client_address, server):
        self.queue = server.queue
        StreamRequestHandler.__init__(self, request, client_address, server)

    def handle(self):
        while True:
            self.data = self.rfile.readline().strip()
            if not self.data:
                break
            cur_thread = threading.current_thread()
            command = self.data[0:2]
            if command == "nr":
                info = self.data[2:]
                t1 = info.split("|")
                title = t1[0]
                self.queue.put(info)
                self.finish()

q = Queue.Queue()
server = ThreadedTCPServer((HOST, PORT), ThreadedTCPStreamHandler, queue=q)
ip, port = server.server_address

server_thread = threading.Thread(target=server.serve_forever)
server_thread.daemon = True
server_thread.start()
while True:
        try:
            item = q.get()
            print item
            q.task_done()
            q.join()
        except KeyboardInterrupt:
            server.shutdown()
            sys.exit(0)

rawDescription: Python Queue.Queue wont work in threaded TCP Stream Handler


code:
import itertools
x = ['a','b']
print [p for p in itertools.product(x, repeat=5)]

rawDescription: Permutation of x length of 2 characters

description: Permutation of x length of 2 characters


code:
import numpy as np
a = np.array(['zero', 'one', 'two', 'three'], dtype=object)
a[1] = 'thirteen'
print a

rawDescription: String preallocation in numpy.arrays

description: String preallocation in numpy.arrays


code:
import psutil

pid = 14551

def rss_MB(pid):
    proc = psutil.Process(pid)
    mem_bytes = proc.get_memory_info().rss
    return( float(mem_bytes)/1048576 )

mem_MB = rss_MB(pid)
print "pid: %d has an rss of %f MB" % (pid, mem_MB)

rawDescription: Process memory usage output

description: Process memory usage output


code:
from ctypes import *

l = cdll.LoadLibrary("lib.so")
my_func = l.my_func
my_func.restype = POINTER(c_int)

i = 0;
rv = my_func()
while rv[i]:
    print rv[i]
    i += 1

rawDescription: zero terminated dynamic array as return value in Python

description: Zero terminated dynamic array as return value in Python


code:
adj = """1 0 1 0 1
1 0 1 0 0
1 0 1 0 1
0 0 1 0 0
1 1 1 1 0
1 1 1 0 1
1 0 1 0 0"""

for row,line in enumerate(adj.split('\n')):
    for col,val in enumerate(line.split(' ')):
        if val == '1':
            print row,col

rawDescription: Read in matrix from file, make edgelist, and write edgelist to file


code:
import xmlrpclib

#dbname = "my_db"
#username = "admin"
#pwd = "my_pwd"

sock_common = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
#uid = sock_common.login(dbname, username, pwd)
uid = sock_common.login("my_db", "admin", "my_pwd")
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')

#this will search all ids of res_partner class
partner_ids = sock.execute(dbname, uid, pwd, 'res.partner', 'search',[])

for partner_id in partner_ids:
    #res = sock.execute(dbname, uid, pwd, 'res.partner', 'your_function_name', [partner_id])
    res = sock.execute("my_db", uid, "my_pwd", 'res.partner', 'your_function_name', [partner_id])
    print '\n====res=====',res

print " Ending Query ... "

rawDescription: How to call a function of a OpenERP class through openerplib or xmlrpclib?

description: Call a function of a OpenERP class through openerplib or xmlrpclib


code:
import itertools
x = ['a','b','c','d','e']
y = ['1','2','3']
for a, b in itertools.izip(itertools.cycle(x), itertools.cycle(y)):
    print a, b

rawDescription: Cycle of multiple iterables?

description: Cycle of multiple iterables


code:
import scrapy

class TestSpider(scrapy.Spider):

    name = 'quoka'
    start_urls = ['http://www.quoka.de/immobilien/bueros-gewerbeflaechen']

    def parse(self, response):

        request = scrapy.FormRequest.from_response(
            response,
            formname='frmSearch',
            formdata={'classtype': 'of'},
            callback=self.parse_filtered
        )
        print request.body
        yield request

    def parse_filtered(self,response):

        resultList = response.xpath('//div[@id="ResultListData"]/ul/li')
        for resultRow in resultList:
            xpath_Result_Details = './/div[@class="q-col n2"]/a'
            # Check if row has details
            if resultRow.xpath(xpath_Result_Details):
                result_Details = resultRow.xpath(xpath_Result_Details)
                # If YES extract details
                title = result_Details.xpath('./@title').extract()
                href = result_Details.xpath('./@href').extract()[0]
                # Code to request detail pages goes here ...
                print title, href

        # Use this instead of CrawlSpider to follow the pagination links
        xpath_NextPage = '//div[@class="rslt-pagination"]//li[@class="arr-rgt active"]/a'
        if response.xpath(xpath_NextPage):

            nextPage_href = response.xpath(xpath_NextPage + '/@href').extract()[0]
            nextPage_url = 'http://www.quoka.de/immobilien/bueros-gewerbeflaechen' + nextPage_href
            nextPage_num = response.xpath(xpath_NextPage + '/@data-qng-page').extract()[0]

            # request = scrapy.Request(nextPage_url, callback=self.parse_filtered)

            # Create request with formdata ...
            request = scrapy.FormRequest.from_response(
                response,
                formname='frmNaviSearch',
                formdata={'pageno': nextPage_num},
                callback=self.parse_filtered
            )

            yield request

rawDescription: Combine FormRequest and CrawlSpider

description: Combine FormRequest and CrawlSpider


code:
from functools import wraps

def decorate(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper

@decorate
def test(a=1,b=2):
    return a+b

print test.__name__

rawDescription: how should I user python decorator without changing the function name?

description: How should I user python decorator without changing the function name


code:
print your_qs.filter(league_pk__in=[1,2,3]).query
print your_qs.filter(league=[1,2,3]).query

rawDescription: In Django filter statement what's the difference between __in and equal sign (=)?


rawDescription: In Python, when iterating through items in list, how to start at 0 if end is reached?


code:
snap, err = epoch.communicate()
print "\nEpoch\t\tTime"
for stamp,_,_,_,snaptime,_ in zip(*[iter(snap.split(' '))]*6):
    print('{}\t{}'.format(stamp, snaptime))

rawDescription: Formatting text from a subprocess.Popen command


code:
process = subprocess.Popen(['nginx', '-t'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out,err = process.communicate()
print out
print err

rawDescription: Catch output from python subprocess

description: Catch output from python subprocess


code:
def center_print(s):
    import os,sys
    command = "mode con ^ | findstr Columns" if sys.platform == 'win32' else "stty size"
    ignore, columns = os.popen(command).read().split()
    print " "*((int(columns)-len(s))/2)+s

center_print("aaaaaaaaaaaaaaaa")

rawDescription: python print in center

description: Python print in center


code:
work = [('January', 2, [('curly', 30, 5), ('larry',10, 5), ('moe',20, 10)])]

workers = dict()
month = ""
section = ""
for w in work:
    month = w[0]
    section = w[1]
    for worker, time, overtime in w[2]:
        workers[worker] = (time, overtime)
    print "Month: {0}\nSection: {1}".format(month, section)
    print "".join("%s has worked %s hours, overtime %s\n" % (worker, time[0], time[1]) for worker, time in workers.items())

rawDescription: break string and arrays down intro separate items

description: Break string and arrays down intro separate items


code:
import twill.commands
import BeautifulSoup

class browser:
   def __init__(self, url="http://www.google.com",log = None):
      self.a=twill.commands
      self.a.config("readonly_controls_writeable", 1)
      self.b = self.a.get_browser()
      self.b.set_agent_string("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14")
      self.log = log
      self.b.clear_cookies()
      self.url=url
   def googleQuery(self, query="python code"):
      self.b.go(self.url)
      #self.b.showforms()
      f = self.b.get_form("f")
      #print "form is %s" % f
      f["q"] = query
      self.b.clicked(f, "btnG")
      self.b.submit()
      pageContent = self.b.get_html()
      soup=BeautifulSoup.BeautifulSoup(pageContent)
      ths = soup.findAll(attrs={"class" : "l"})
      for a in ths:
         print a

t=browser()
t.googleQuery("twill queries")

rawDescription: how to use python to run a google search and print out the results

description: Use python to run a google search and print out the results


code:
cursor.execute('''
    SELECT *,
        6371*1000 * acos(cos(radians(%s)) *
        cos(radians(lat)) * cos(radians(lon) - radians(%s)) +
        sin(radians(%s)) * sin(radians(lat))) as distance
    FROM
        poi_table
    WHERE distance < %s
    ORDER BY distance ASC
    LIMIT 0, 50''', (latitude, longitude, latitude, radius,))
results = cursor.fetchall()
print results

rawDescription: Python and MySQLdb

description: Python and MySQLdb


code:
importances = rf.feature_importances_
important_names = feature_names[importances > np.mean(importances)]
print important_names

rawDescription: How does sklearn random forest index feature_importances_


code:
if x % 3 == 0:
    print 'x is divisible by 3'

rawDescription: Determining whether an value is a whole number in Python


code:
for c in cols:
    if not re.match(r'^[A-Za-z][A-Za-z0-9$_-]+$', c):
            print "Argument is not a valid column name :", c
            sys.exit()

rawDescription: Proper SQL request with psycopg2 in Python


code:
import re

def scale(fact):
    """This function returns a lambda which will scale a number by a
    factor 'fact'"""
    return lambda val: fact * val

def find_and_scale(file, fact):
    """This function will find all the numbers (integers) in a file and
    return a list of all such numbers scaled by a factor 'fact'"""
    num = re.compile('(\d+)')
    scaling = scale(fact)
    f = open(file, 'r').read()
    numbers = [int(m.group(1)) for m in num.finditer(f)]
    return map(scaling, numbers)

if __name__ == "__main__":
    import sys
    if len(sys.argv) != 3:
        print "usage: %s file factor" % sys.argv[0]
        sys.exit(-1)
    numbers = find_and_scale(sys.argv[1], int(sys.argv[2]))
    for number in numbers:
        print "%d " % number

rawDescription: Multiply with find and replace

description: Multiply with find and replace


code:
c=[0.3, 0.2, 0.3, 0.6, 0.9, 0.1, 0.2, 0.5, 0.3, 0.5, 0.7, 0.1]

def num_bigger(threshold):
    count = 0
    for num in c:
        if num > threshold:
            count +=1

    return count

thresholds = [x/10.0 for x in range(10)]

for thresh in thresholds:
    print thresh, num_bigger(thresh)

rawDescription: python; counting elements of vectors

description: Python; counting elements of vectors


code:
import re
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.http import Request
from scrapy.item import Item, Field
from scrapy.selector import HtmlXPathSelector

class GigItem(Item):
    gig_link = Field()

class PrinzSpider(CrawlSpider):
    name = "prinz"
    allowed_domains = ["prinzwilly.de"]
    start_urls = ["http://www.prinzwilly.de/"]

    rules = (Rule(SgmlLinkExtractor(allow=(r'veranstaltungen-(.*)',)), callback='parse'),)

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        startlinks = hxs.select("//ul[@id='mainNav2']/li/a")
        for link in startlinks:
            item = GigItem()
            item['gig_link'] = link.select('@href').extract()[0]
            yield Request(item['gig_link'], callback=self.parse_gig_page, meta={'item': item})

    def parse_gig_page(self, response):
        hxs = HtmlXPathSelector(response)
        item = response.meta['item']
        gig_content = hxs.select("//div[@class='n']/table/tbody").extract()[0]
        fb_link = re.findall(r'(?:www.facebook.com/)(.*)', gig_content)
        print '********** FB LINK ********', fb_link
        return item

rawDescription: Scraper not finding pages

description: Scraper not finding pages


code:
text =  "Test Message"
message = client.messages.create(
    body= text,
    to="+12125551234",
    from_="+15105551234",
)
print message.sid

rawDescription: Twillio Restclient in python

description: Twillio Restclient in python


code: print public_ip() != connect_tor()

rawDescription: Best Way to Structure Functions in Python


code:
pid = os.fork()
if pid:
    # we are the parent, continue on
    print "This runs in a separate process from the else clause."

else:
    #child runs browser then quits.
    webbrowser.open_new(url)
    print "Exiting..."
    sys.exit()

rawDescription: How to resume program (or exit) after opening webbrowser?

description: Resume program (or exit) after opening webbrowser


rawDescription: python read .csv file - access list elements in a field

description: Python read .csv file - access list elements in a field


code:
with open('/Users/jessieinchauspe/Dropbox/Smesh/TMT/zipf.csv') as text:
    print ','.join('"{0}"'.format(word.strip()) for word in text)

rawDescription: CSV File to list

description: CSV File to list


code:
#!/usr/bin/python
import datetime
import os
import sys
import shutil

SOURCE_PATH = "/home/customers/"
TARGET_PATH = "/home/oldcustomers/"
TIME_THRESHOLD = datetime.timedelta(365)    #days

def get_old_dirs(source_path, time_threshold):
    old_dirs = []
    for root, dirs, files in os.walk(source_path):
        for d in dirs:
            full_path = os.path.join(root, d)
            now = datetime.datetime.now()
            last_modified = datetime.datetime.fromtimestamp(os.stat(full_path).st_mtime)
            delta = now - last_modified
            if (delta) >= time_threshold:
                old_dirs.append((full_path, delta))
        break
    return old_dirs

def move_old_dirs(target_path, source_path, time_threshold, confirm=True):
    dirs = get_old_dirs(source_path, time_threshold)
    print '"old" dirs: %d' % len(dirs)
    if dirs:
        if confirm:
            print "pending moves:"
            for (d, delta) in dirs:
                print "[%s days] %s" % (str(delta.days).rjust(4), d)
            if not raw_input("Move %d directories to %s ? [y/n]: " % (len(dirs), target_path)).lower() in ['y', 'yes']:
                return
        if not os.path.exists(target_path):
            os.makedirs(target_path)
        for (d, delta) in dirs:
            shutil.move(d, target_path)
            print "%s -> %s" % (d, target_path)
        print "moved %d directories" % len(dirs)

def cmdline(args):
    from optparse import OptionParser
    usage = "move_old_dirs [options] <source_dir> <target_dir>"
    default_desc = "%s -> %s [%s]" % (SOURCE_PATH, TARGET_PATH, TIME_THRESHOLD)
    parser = OptionParser(usage)
    parser.add_option("-d", "--days",
                      action="store", type="int", dest="days", default=365,
                      help="How many days old the directory must be to move")
    parser.add_option("--default", default=False,
                    action="store_true", dest="default",
                    help="Run the default values set in the script: (%s)" % default_desc)
    parser.add_option("-f", "--force", default=False,
                    action="store_true", dest="force",
                    help="Dont ask for confirmation")
    (options, args) = parser.parse_args(args)
    if len(args) == 1 and options.default:
        print "running default: %s" % default_desc
        return move_old_dirs(TARGET_PATH, SOURCE_PATH, TIME_THRESHOLD, confirm=(not options.force))
    elif len(args) == 3:
        return move_old_dirs(args[2], args[1], datetime.timedelta(options.days), confirm=(not options.force))
    print usage
    print "incorrect number of arguments, try -h or --help"
    return 1

if __name__ == "__main__":
    cmdline(sys.argv)

rawDescription: Easy way of moving directories containing old files away


rawDescription: Doing math with Python lists

description: Doing math with Python lists


code:
import mylist

def someFunc():
    mylist = reload(mylist) # this may raise syntaxerrors in wrong moments
    list = mylist.list
    print list

rawDescription: How to dynamically import and reimport a file containing definition of a global variable which may change anytime

description: Dynamically import and reimport a file containing definition of a global variable which may change anytime


code:
from collections import Counter
l = [2L, 14L, 14L, 14L, 11L, 2L, 2L, 11L, 14L, 11L, 14L, 2L, 2L, 14L]
c = Counter(l)
c
c[14L]

rawDescription: Count number of occurrences

description: Count number of occurrences


code:
from itertools import combinations

Allusers={
    'User1': {'Film1': 'Vote1', 'Film2': 'Vote2'},
    'User2': {'Film1': 'Vote1', 'Film2': 'Vote2'}
}

for comb in combinations(Allusers, 2):
    if Allusers[comb[0]] == Allusers[comb[1]]:
        print '{0} is same with {1}'.format(comb[0], comb[1])

rawDescription: How to compare items in a python dictionary

description: Compare items in a python dictionary


code:
list_a = ['an378j', 'an378jijm', 'fg453h', 'fg453hbrd']
list_b = ['fg453h2564677sakjh', 'an378jijm564456464ewf', 'fg453hbrd8968764ewf', 'an378j86764edwf']

sorted_b = []
grouped_b = {}
for item_a in list_a:
    for item_b in list_b:
        if item_b.startswith(item_a):
            if item_a not in grouped_b:
                grouped_b[item_a] = [item_b]
            else:
                grouped_b[item_a].append(item_b)

    for item in sorted(grouped_b[item_a]):
        if item not in sorted_b:
            sorted_b.append(item)

print sorted_b

rawDescription: rearrange a list using contents of another list in python

description: Rearrange a list using contents of another list in python


code:
CRlist = [['CR', 'FA', 'CL', 'TITLE'],
          ['', '409452', 'WLAN', '656885\tAge out RSSI values from buffer in Beacon miss scenario'],
          ['', '415560', 'WLAN', '656886\tTo Record SMD Event Logging'],
          ['', '413064', 'wlan', '654661\tTo Record HAL and SLM FW Event Logging'],
          ['', '379104', 'BT', '656928\tCR379104: BT doesn\xe2\x80\x99t work that Riva neither sends HCI Evt for HID ACL data nor response to HCI_INQUIRY after entering into pseudo sniff subrating mode.']]
CRstring = ''
INFO = """

{CR}
{FA}
{CL}
{Title}

"""
for item in CRlist[1:]:
    print item
    CRstring += INFO.format(
        CR=item[0],
        FA=item[1],
        CL=item[2],
        Title=item[3],
        )
print CRstring

rawDescription: Appending a hyperlink

description: Appending a hyperlink


code:
print id([])
print id([])
print id({})
print id({})

rawDescription: Explain this behaviour

description: Explain this behaviour


code:
#!/usr/bin/python
import cgitb
cgitb.enable()

import MySQLdb, cgi, os, sys

db = MySQLdb.connect(host="localhost", user="root", passwd="", db="test")

form = cgi.FieldStorage()
name = form.getvalue('temp')
passwd = form.getvalue('temp2')

with db as query:
    query.execute("select * from cred where uname=%s and %s", (name, passwd))
    result = query.fetchone()

if result is None:
    # no such user, redirect
    print 'Status: 302 Found'
    print 'Location: http://localhost:8000/'
    print

else:
    print 'Content-type: text/html'
    print
    print 'Hello {}'.format(name)

rawDescription: Python Redirect not working

description: Python Redirect not working


code:
import Tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        button = tk.Button(text="Save", command=self.save)
        button.pack(side="top")
        self.widgets = []
        for line in ["one","two","three","four"]:
            widget = tk.Entry(self)
            widget.insert(0, line)
            widget.pack(side="top", fill="x")
            self.widgets.append(widget)

    def save(self):
        for widget in self.widgets:
            print widget.get()

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

rawDescription: Store entry values from previous Tkinter session in a loop


code:
import subprocess

p = subprocess.Popen(r'start cmd /c C:\test.bat', shell=True)

p.wait()

print 'done'

rawDescription: How to run a DOS batch file in background using Python?

description: Run a DOS batch file in background using Python


code:
import math, sys
from PyQt4.QtCore import Qt, QTimer
from PyQt4.QtGui import *

class MainWindow(QMainWindow):
    def __init__(self, parent = None):
        QMainWindow.__init__(self, parent)

        #Create Basic UI
        self.mainWidget = QWidget(self)
        self.table = QTableWidget()
        self.table.setRowCount(160)
        self.table.setColumnCount(10)

        self.button = QPushButton("Print stuff")

        layout = QVBoxLayout(self.mainWidget)
        layout.addWidget(self.table)
        layout.addWidget(self.button)

        self.setCentralWidget(self.mainWidget)

        self.button.clicked.connect(self.printStuff)
        #################

        #Fill the table
        self.rowRange = range(0, self.table.rowCount(), 2)
        for i in self.rowRange:
            chkBoxItem = QTableWidgetItem()
            chkBoxItem.setFlags(Qt.ItemIsUserCheckable|Qt.ItemIsEnabled)
            chkBoxItem.setCheckState(Qt.Unchecked)
            self.table.setItem(i, 9, chkBoxItem)
        ###############

    def printStuff(self): #You can remove this, this is for testing purpose only
        print [(i+1, self.table.item(i, 9).checkState()) for i in self.rowRange]

if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

rawDescription: Inserting multiple QCheckBox into QTableWidget odd rows


code:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_string('''
<Root>:
    MainScreen:
        name: 'main'
    AnotherScreen:
        name: 'another'
<MainScreen>:
    BoxLayout:
        Button:
            text: 'next screen'
            on_release: root.parent.current='another'
        Button:
            text: 'ping!'
            on_release: root.ping()
<AnotherScreen>:
    BoxLayout:
        Button:
            text: 'previous screen'
            on_release: root.parent.current='main'
        Button:
            text: 'ping!'
            on_release: root.ping()
''')

class MainScreen(Screen):
    def __init__(self, **kw):
        super(MainScreen, self).__init__(**kw)
        self.a = App.get_running_app()
    def ping(self):
        print self.a.big_dict['hi']

class AnotherScreen(Screen):
    def ping(self):
        b = App.get_running_app()
        print b.big_dict['hi']

class Root(ScreenManager):
    pass
class SimpleKivy(App):
    big_dict={'hi':'hi there!'}
    def build(self):
        return Root()
SimpleKivy().run()

rawDescription: How to interact with Checkbox in kivy python?

description: Interact with Checkbox in kivy python


code:
G = nx.Graph()
# ... build or do whatever to the graph
nodes = filter(lambda (n, d): d['color'] == 'red', G.nodes_iter(data=True))
subgraph = G.subgraph(nodes)

rawDescription: partition graph into sungraphs based on node's attribute NetworkX

description: Partition graph into sungraphs based on node's attribute NetworkX


code:
import random

playerList = ["Player 1", "Player 2", "Player 3", "Player 4"]
player1, player2 = random.sample(playerList, 2)
print '{} vs. {}'.format(player1, player2)

rawDescription: Two different random.choices from a list (python)


code:
from itertools import dropwhile, takewhile
input1 = [['aaaaa', 'bbbbb', 'cccc', 'specified string', 'ddddd', 'eeeeee', 'ffffff']]
input2 = [['aaaaa', 'bbbbb', 'cccc', 'specified string1', 'ddddd', 'eeeeee', 'ffffff', 'specified string2', 'qqqq', 'wwww', 'sssss']]
input3 = [['aaaaa', 'bbbbb', 'cccc', 'specified string', 'ddddd', 'eeeeee', 'ffffff']]
f = lambda x: x <> 'specified string'
f1 = lambda x: x <> 'specified string1'
f2 = lambda x: x <> 'specified string2'
list(dropwhile(f, input1[0]))[1:]
list(takewhile(f2, dropwhile(f1, input2[0])))[1:]
list(takewhile(f, input3[0]))

rawDescription: fillter a list in python

description: Fillter a list in python


rawDescription: How to compare all items in a list with an integer without using for loop

description: Compare all items in a list with an integer without using for loop


code:
#!/usr/bin/env python

import dpkt
from socket import inet_ntoa

with open("sample.pcap") as f:
    pcap = dpkt.pcap.Reader(f)
    for ts, buf in pcap:
        ip = dpkt.ethernet.Ethernet(buf).data
        print "{} --> {} {}".format(inet_ntoa(ip.src), inet_ntoa(ip.dst), ip.len)

rawDescription: Find how much data has been transferred from pcap data


code:
import maya.api.OpenMaya as om
import maya.cmds as cmds
om.MEventMessage.getEventNames()

class CallbackHandler(object):

    def __init__(self, cb, fn):
        self.callback = cb
        self.function  = fn
        self.id = None

    def install(self):
        if self.id:
            print "callback is currently installed"
            return False
        self.id = om.MEventMessage.addEventCallback(self.callback, self.function)
        return True

    def uninstall(self):
        if self.id:
            om.MEventMessage.removeCallback(self.id)
            self.id = None
            return True
        else:
            print "callback not currently installed"
            return False

    def __del__(self):
        self.uninstall()

def test_fn(arg):
    print "callback fired 2", arg

cb = CallbackHandler('NameChanged', test_fn)
cb.install()
# callback is active
cb.uninstall()
# callback not active
cb.install()
# callback on again
del(cb) # or cb = None
# callback gone again

rawDescription: Using callbacks to run function using the current values in a class


code:
from itertools import izip
from operator import itemgetter

# Ignoring the outer levels of the dict and shortening the numbers.
data = {
    'Class1' : [-138, -133,  33, 999],
    'Class2' : [ -20,  -19, 100, 777],
    'Class3' : [-338, -345, 200, 111],
}

lookup    = dict(enumerate(data.keys()))
max_index = lambda ns: max(enumerate(ns), key = itemgetter(1))[0]

# Zip the arrays together.
# For each zipped-set of numbers, find the index of the max.
# Use that index to look up the class name.
classes = [lookup[max_index(ns)] for ns in izip(*data.values())]

print classes    # ['Class2', 'Class2', 'Class3', 'Class1']

rawDescription: Python: Comparing values between more than 2 arrays


code:
def dump_keys(d, lvl=0):
    for k, v in d.iteritems():
        print '%s%s' % (lvl * ' ', k)
        if type(v) == dict:
            dump_keys(v, lvl+1)

mydict = {'key1':'value1',
          'key2':{'subkey1':'subvalue1',
                  'subkey2':'subvalue2',
                  'subkey3': {'subsubkey1' : 'subsubkeyval1'}}}

dump_keys(mydict)

rawDescription: Concise way to print dictionary keys in a nice format


code:
import lxml.html

doc = lxml.html.parse(your_html)
for i in range(1,7):
    for h in doc.xpath('//h%d' % i):
        h.text = h.text.capitalize()

print lxml.html.tostring(doc)

rawDescription: Match html tags in python

description: Match html tags in python


code:
for line in text.splitlines():
    line = line.split()
    if line:
        print '|'.join((' '.join(line[:-3]), '|'.join(line[-3:])))

rawDescription: How can I place delimiters between numbers and text in Python?

description: Place delimiters between numbers and text in Python


code:
from wsgiref.simple_server import demo_app
from wsgiref.simple_server import make_server

httpd = make_server('0.0.0.0', 8000, demo_app)
print "Serving on port 8000..."
httpd.serve_forever()

rawDescription: How to determine that pythons urllib2 has queried content through a given proxy?

description: Determine that pythons urllib2 has queried content through a given proxy


code:
data = """
1 A 3 B 2 C 0
        D 0
    E 1 F 0
    G 2 H 0
      I 0"""

def construct_nodes(data):
    return dict((next(data), construct_nodes(data))
                for _ in xrange(int(next(data))))

print construct_nodes(iter(data.split()))

rawDescription: Python: Read tree like data from text file and store in dictionary


code:
import cherrypy
import os.path
import struct
from auth import AuthController, require, member_of, name_is

class Server(object):
    led_power=0
    led_switch=1 #Initial LED on

_cp_config = {
    'tools.sessions.on': True,
    'tools.auth.on': True
}
auth = AuthController()
@cherrypy.expose
@require()
def index(self,  switch='', power=''):
    if switch:
        self.led_switch = int(switch)
    if power:
        self.led_power = int(power)

    html = open('led.html','r').read()

    if self.led_switch:
        print "ON"
    else:
        print "OFF"

    if self.led_power:
        print "Logout"
        AuthController().logout()

        return html
    index.exposed = True

conf = {
    'global' : {
        'server.socket_host': '0.0.0.0', #0.0.0.0 or specific IP
        'server.socket_port': 8080 #server port
    },

    '/images': { #images served as static files
        'tools.staticdir.on': True,
        'tools.staticdir.dir': os.path.abspath('images')
    },

    '/favicon.ico': {  #favorite icon
        'tools.staticfile.on': True,
        'tools.staticfile.filename': os.path.abspath("images/bulb.ico")
    }
}
cherrypy.quickstart(Server(), config=conf)

rawDescription: How to logout from a simple web appl. in CherryPy, Python

description: Logout from a simple web appl. in CherryPy, Python


code:
user_input = raw_input("Please enter a company name: ")
if user_input in dictionary:
    print "Ticker symbol for ", user_input, " is: ", dictionary[user_input]
else:
    print user_input, " not a valid company name"

rawDescription: Write a function ticker() that takes as input a company name and returns the ticker symbol


code: a = 1; b = 2; c = a + b; print c

rawDescription: Are there any unused symbols in python?

description: Are there any unused symbols in python


code:
import sys

print sys.maxsize
# prints some system-dependent number representing the maximum
# size most containers can hold.

rawDescription: How meny items cn contain tuple or list in python?


code:
def derp(theta, size):
    w = math.ceil(float(size)*float(sigma))

    # If the dimension is an even number, make it uneven
    if(w%2 == 0):
        print "even number detected, incrementing"
        w = w + 1

    # Now make the mask
    x_mask = []

    w_range = int(math.floor(w/2))
    print "Going from " + str(-w_range) + " to " + str(w_range)
    for i in range_inc(-w_range, w_range):
        x_mask = a*i^2

rawDescription: Laplacian of Gaussian Edge Detector Being Affected by Change of Mask Size


code:
s = 'hello, {person}'
s
s.format(person='james')
print s                       # but you did not assign it
x = s.format(person='james')  # now assign it
print x

rawDescription: str.format() option is not working

description: Str.format() option is not working


code:
class Thing(object):

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

    def __eq__(self, other):
        return self.value == other.value

    def __repr__(self):
        return str(self.value)

    def __hash__(self):
        return hash(self.value)

a = Thing(5)
b = Thing(5)

s = set()
s.add(a)
s.add(b)
print s

rawDescription: Set multiplicity for user defined classes


code:
#!/usr/bin/env python

"""Reconstruct sessions: Take a space-delimited web server access log
including IP addresses, timestamps, and User Agent,
and output a list of the IPs, and the number of inferred sessions for each."""

## Input looks like:
# Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Referer) sc-status sc-substatus sc-win32-status
# 2010-09-21 23:59:59 172.21.1.119 GET /graphics/foo.gif - 80 - 128.123.114.141 Mozilla/5.0+(Windows;+U;+Windows+NT+5.1;+en-US;+rv:1.9.2)+Gecko/20100115+Firefox/3.6+(.NET+CLR+3.5.30729) http://www.site.org//baz.htm 200 0 0

import datetime
import operator

infileName = "ex100922.log"
outfileName = "visitor-ips.csv"

ipDict = {}

def inputRecords():
    infile = open(infileName, "r")

    recordsRead = 0
    progressThreshold = 100
    sessionTimeout = datetime.timedelta(minutes=30)

    for line in infile:
        if (line[0] == '#'):
            continue
        else:
            recordsRead += 1

            fields = line.split()
            # print "line of %d records: %s\n" % (len(fields), line)
            if (recordsRead >= progressThreshold):
                print "Read %d records" % recordsRead
                progressThreshold *= 2

            # http://www.dblab.ntua.gr/persdl2007/papers/72.pdf
            #   "a new request is put in an existing session if two conditions are valid:
            #    * the IP address and the user-agent are the same of the requests already
            #      inserted in the session,
            #    * the request is done less than fifteen minutes after the last request inserted."

            theDate, theTime = fields[0], fields[1]
            newRequestTime = datetime.datetime.strptime(theDate + " " + theTime, "%Y-%m-%d %H:%M:%S")

            ipAddr, userAgent = fields[8], fields[9]

            if ipAddr not in ipDict:
                ipDict[ipAddr] = {userAgent: [1, newRequestTime]}
            else:
                if userAgent not in ipDict[ipAddr]:
                    ipDict[ipAddr][userAgent] = [1, newRequestTime]
                else:
                    ipdipaua = ipDict[ipAddr][userAgent]
                    if newRequestTime - ipdipaua[1] >= sessionTimeout:
                        ipdipaua[0] += 1
                    ipdipaua[1] = newRequestTime
    infile.close()
    return recordsRead

def outputSessions():
    outfile = open(outfileName, "w")
    outfile.write("#Fields: IPAddr Sessions\n")
    recordsWritten = len(ipDict)

    # ipDict[ip] is { userAgent1: [numSessions, lastTimeStamp], ... }
    for ip, val in ipDict.iteritems():
        # TODO: sum over on all keys' values  [(v, k) for (k, v) in d.iteritems()].
        totalSessions = reduce(operator.add, [v2[0] for v2 in val.itervalues()])
        outfile.write("%s\t%d\n" % (ip, totalSessions))

    outfile.close()
    return recordsWritten

recordsRead = inputRecords()

recordsWritten = outputSessions()

print "Finished session reconstruction: read %d records, wrote %d\n" % (recordsRead, recordsWritten)

rawDescription: free implementation of counting user sessions from a web server log?

description: Free implementation of counting user sessions from a web server log


code:
print Permutation.__invert__.__doc__
~Permutation(1, 2, 0)

rawDescription: Inverting permutations witn sympy

description: Inverting permutations witn sympy


code:
from PIL import Image

r, g, b = 255, 240, 227
intcolor = (b << 16 ) | (g << 8 ) | r
print intcolor # 14938367
ImgRGB = Image.new("RGB", (200, 200), intcolor)
ImgRGB.show()

rawDescription: PIL Image mode I is grayscale?

description: PIL Image mode I is grayscale


code:
regex_dict={1:r'\w{1,2}',2:r'\w{1,2}\s+(\d+)\s+\d+\.+\d+'}
with open('ch4_int_coord.txt') as f:
    for index,line in enumerate(f,1):
        print re.search(regex_dict[index],line)

rawDescription: How can I use a different form of regex for each line in a file?

description: Use a different form of regex for each line in a file


code:
f = open('raw_data.txt', 'rt')
data = f.read()

students = data.split('Start\n')[1:]

for s in students:
    lines = s.split('\n')
    id = lines[0]
    name = lines[1]

    nrcourses = int(lines[2])

    line = 2
    courses = []
    for n in range(nrcourses):
        number = lines[line+1]
        credit = lines[line+2]
        score = lines[line+3]
        courses.append((number, credit, score))
        line += 3

    print 'id: %s; name %s; course list %s' % (id, name, courses)

f.close()

rawDescription: Reading from raw_data.txt and writing to a results.txt file with processing in Python


code:
for item in data:
    print ', '.join(item['brand'])

rawDescription: How to remove square brackets and u char from json data

description: Remove square brackets and u char from json data


rawDescription: Python - Regular Expression replace all non alpha-numerics EXCEPT periods/decimal points?


code:
for item in parsed_result:
    time.sleep(0.01)
    txt = TextInput(id=item['id'], size_hint=(.5, None), height=32, text='IMSI', multiline=False)
    btn = Button(text=item['id'], size_hint=(.5, None), height=32,
                     on_release=lambda ti=txt: self.click_device_id(ti.text, txt.text))
    add_widget_target.add_widget(btn)
    add_widget_target.add_widget(txt)

def click_device_id(self, button_text, txt_text):
    print button_text, txt_text
    self.go_next_screen()

rawDescription: How to select widget by id?

description: Select widget by id


code:
for key in phonebook:
    if key.startswith("Sm"):
        print "Phone number of %s is %d" % (key, phonebook[key])

rawDescription: Dictionaries in Python and different ways of accessing it


code:
def dir():
    file = open('config','r+')
    cont = file.readlines()
    print "file contents are %s" % cont + '\n'
    elements = []
    for i in range(len(cont)):
        rowElems = cont[i].split(' ')
        elements.append({ 'dir' : rowElems[0], 'ext' : rowElems[1], 'days' : rowElems[2] })

for e in elements:
    print "Dir = " + e['dir']
    print "Fileext = " + e['ext']
    print "days = " + e['days']

dir()

rawDescription: python: read file and split the data

description: Python: read file and split the data


code:
def foo(): print 'Hello world!'
foo
foo()
bar = foo
bar()

rawDescription: Howto pass a function to a function in Python?


code:
import datetime
d = datetime.datetime(2009, 10, 5, 18, 00)
print d.year, d.month, d.day, d.hour, d.second
print d.isoformat(' ')

rawDescription: How to specify date and time in python?

description: Specify date and time in python


code:
def reorder(seq, extractor, iterable_or_mapping):
    if hasattr(iterable_or_mapping, 'get'): # dict?
        rank = iterable_or_mapping
    else:
        rank = dict((v,i) for i,v in enumerate(iterable_or_mapping))
    return sorted(seq, key=lambda v: rank.get(extractor(v),-1))

# Sample application
filenames = '''
    cld_for_April_EISopt_1000.png     cld_for_August_EISopt_1000.png
    cld_for_December_EISopt_1000.png  cld_for_February_EISopt_1000.png
    cld_for_January_EISopt_1000.png   cld_for_July_EISopt_1000.png
    cld_for_June_EISopt_1000.png      cld_for_March_EISopt_1000.png
    cld_for_May_EISopt_1000.png       cld_for_November_EISopt_1000.png
    cld_for_October_EISopt_1000.png   cld_for_September_EISopt_1000.png
'''.split()

months = ['January', 'February', 'March', 'April', 'May', 'June',
          'July', 'August', 'September', 'October', 'November', 'December']

def get_month(filename):
    return filename.split('_')[2]

for file in reorder(filenames, get_month, months):
    print file

# cld_for_January_EISopt_1000.png
# cld_for_February_EISopt_1000.png
# cld_for_March_EISopt_1000.png
# ...

rawDescription: ordering a list of string according to name (month)

description: Ordering a list of string according to name (month)


code:
import itertools
import pickle  #for restoring last data session
fname = "restore.pck"

reals = [line.strip() for line in open("real.txt", 'r')]
imags = [line.strip() for line in open("imag.txt", 'r')]

seed_count = 0 #number last processing cycle
count_ses = 0  #number current session cycle
count_tot = 0  #number total cycle processing so far
it_tot=len(reals)*len(imgs)

ret = raw_input("recover last session?: y/n")
if ret =="y":
    with open(fname,"rb") as f:
        rec= pickle.load(f)
        print  "last session data {0} {1} {2}".format(rec[0],rec[1],rec[2])
        seed_count=rec[2]
        seed_img=rec[1]
        seed_rea=rec[0]
try:
 print "total iterations", it_tot

 for r in itertools.product(reals,imgs):
    # do nothing until you get last interrupted cycle
    count_tot+=1
    if count_tot >= seed_count:
    # and now start processing
     count_ses +=1
     print "processing: {0}, {1}, (total : {2}), (this session: {3}) {4}% )".format(r[0], r[1], count_tot, count_ses,(count_tot/len_tot*100))

except Exception as e:
    print str(e)
    #save data if an error occurred
    store = r[0], r[1], count_tot
    with open(fname,"wb") as f:
     pickle.dump(store,f,pickle.HIGHEST_PROTOCOL)
     f.close()

rawDescription: Python restoring an interrupt iteration of two lists


code:
# start with an empty output dictionary
out = {}

# iterate through the keys in the dictionary
for key in p:
   # iterate through the values in the sublist
   for val in p[key]:
      # check to see if we've seen this one before
      if not out.has_key(val):
         # if not, start it out at 0
         out[val] = 0

      # increment by one because we've seen it once more
      out[val] += 1

print out

rawDescription: How can I create a histogram of appearances of values in a dictionary?

description: Create a histogram of appearances of values in a dictionary


code:
from bs4 import BeautifulSoup as bsoup
import re

markup = """

This house believes that society benefits when we share personal information online.

Oregon-Oxford, Cross Examination

On the affirmative: Foo Debate Club

On the opposition: Bar Debate Club

"""
soup = bsoup(markup)

# Explicitly define the tag and class.
motion = soup.find("h2", class_="debateHeaderProp").get_text()
# Or just use the class.
d_format = soup.find(class_="debateFormat").get_text()
# And even use regex for more power.
teams = [t.get_text() for t in soup.find_all("div", class_=re.compile(r".*debate.*Side.*"))]

print "Our Debate for Today"
print "Motion:", motion
print "Format:", d_format
print teams[0]
print teams[1]

# Prints the following:
# Our Debate for Today
# Motion: This house believes that society benefits when we share personal information online.
# Format: Oregon-Oxford, Cross Examination
# On the affirmative: Foo Debate Club
# On the opposition: Bar Debate Club

rawDescription: How to remove HTML tags from python strings?

description: Remove HTML tags from python strings


code:
def noofways(firstnumchosen, sum, numofnum):
    global count
    # don't set count to 0 here
    if sum<0:
        return
    if sum==0 and not(numofnum==0):
        return
    if sum==0 and numofnum==0:
       count+=1
       return
    if numofnum<=0:
       return
    if firstnumchosen>sum:
       return
    noofways(firstnumchosen+1,sum,numofnum)
    noofways(firstnumchosen+1,sum-firstnumchosen,numofnum-1)

# set count to 0 here
count = 0
res=noofways(1,8,3);
print count

rawDescription: Using global variables in python

description: Using global variables in python


code:
from subprocess import Popen, PIPE
toolLocation = "C:\\Program Files\\Electric Cloud\\ElectricCommander\\bin\\ectool"
parameters = ["--server", "server.domain.com", "login", "username", "password"]
output = Popen([toolLocation] + parameters, stdout=PIPE)
print output.stdout.read()

rawDescription: Run an exe from python as a subprocess

description: Run an exe from python as a subprocess


code:
#!/usr/bin/env python2
import fileinput
import subprocess

process = subprocess.Popen(['sort'], stdin=subprocess.PIPE)
with process.stdin as pipe, fileinput.FileInput() as file:
    for line in file:
        if file.isfirstline(): # print header
            print line,
        else: # pipe tails
            pipe.write(line)
process.wait()

rawDescription: streaming data into command with subprocess.Popen

description: Streaming data into command with subprocess.Popen


code:
import Tkinter as tk

def select():
    title=var.get()
    print "selected string :"+title
    print "corresponing integer value :"+str(choices[Dic[title]])

choices = [1,2,3]
Dic={'title 1':0,'title 2':1,'title 3':2}
GUI = tk.Tk()
var = tk.StringVar(GUI)
var.set('title 1')
op=tk.OptionMenu(GUI, var, *Dic.keys())
op.pack(side='left',padx=20,pady=10)
bt=tk.Button(GUI,text='check value',command=select)
bt.pack(side='left',padx=20,pady=10)
GUI.mainloop()

rawDescription: How to change the labels of the choices in an option menu?

description: Change the labels of the choices in an option menu


code:
val = 1 # abs(1) is the same as 1
while True: # get user input
    num = raw_input("Enter a number: ")
    try: # indented
        num = float(num) # goes to except clause if convertion fails
        if not 0 <= num <= val: # check against val, not "val",moved to try block
            print 'try again between 0 to 1' # indented
        else:
            break # input ok, get out of while loop
    except ValueError: # indented, only excepting ValueErrors, not processor is burning errors
        print "Invalid input"

if .5 <= num <= 1:
    print 'Good'
else: # 0 <= num < 0.5
    print 'Fair'

rawDescription: Basic Program Control Flow & Exceptions Try


code:
from BeautifulSoup import BeautifulStoneSoup
xml = '<item><title>New Blog Entry: Common Sense Guide to Mitigating Insider Threats - Best Practice 16 (of 19)</title><link>http://www.cert.org/blogs/insider_threat/2013/02/common_sense_guide_to_mitigating_insider_threats_-_best_practice_16_of_19.html</link><description>This sixteenth of 19 blog posts about the fourth edition of the Common   Sense Guide to Mitigating Insider Threats describes Practice 16: Develop a formalized insider threat program.</description><pubDate>Wed, 06 Feb 2013 06:38:07 -0500</pubDate></item>'

soup = BeautifulStoneSoup(xml)
item = soup('item')[0]
print item('pubdate'), item('link')

rawDescription: Parsing XML RSS feed byte stream for <item> tag


code:
def optimize(myList):
    score_tup = lambda tup_a, tup_b: sum(1.0 if a > b else 0.5 if a == b else 0 for a, b in zip(tup_a, tup_b))
    scores = ((tup_a, [score_tup(tup_a, tup_b) for tup_b in myList]) for tup_a in myList)
    scores = ((tup, score.count(2), score.count(1.5)) for tup, score in scores)
    return [tup for tup, win, draw in scores if (win * 1.0 / (len(myList) - win - draw)) > 1.0]

a = 5
myList = [(i, j, a-i-j) for i in range(a + 1) for j in range(a + 1 - i)]
print myList
print optimize(myList)

rawDescription: Iterating Lists in Python, Ruby, Haskell (or whatever)


code:
import sqlite3 as lite
import time

con = lite.connect('common.db')
cur = con.cursor()
cur.execute(
    "CREATE TABLE IF NOT EXISTS restaurant (orderId INT primary key, inventory TEXT, sales INT);")

for i in range(5):
    print "About to insert on ID: %s" % i
    cur.execute("INSERT INTO restaurant VALUES(%d, 'burger', 1)" % i)
    time.sleep(1)

con.close()

rawDescription: Multiple Instantiation of Programs interacting with one another (Python2.7 & Tkinter)


code:
from datetime import datetime
from datetime import timedelta

now = datetime.now()
time_changed_state = now - timedelta(hours=2)
time_delay = 0
time_arrival = time_changed_state + timedelta(minutes=time_delay)

if now < time_arrival:
   print 'something'
elif now >= time_arrival:
   print 'something different'

rawDescription: Datetime comparison behavior in python

description: Datetime comparison behavior in python


code:
import random

def RandomNo(upper=50):
    Random = random.randint(1, min(50, upper))
    return Random

Food = 1
FGlasgow = 100

while FGlasgow > 0:
    Random = RandomNo(FGlasgow)
    Food += Random
    FGlasgow -= Random
    print "You have found" , Random , "units of food"

rawDescription: Undoing a function while requirements met


code:
import re
s = ['55967', '56003', '56113', '56081', '56333', '56093', '58091']
patt_s = re.compile('(56\d\d3)')
for s_nr in s:
    matched_re = re.search(patt_s, s_nr)
    if matched_re:
        print matched_re.group(0)

rawDescription: Searching a list of numbers for a specific pattern


code:
class bar(object):
    def __init__(self, x):
        self.x = x
    def __repr__(self):
        return "<bar "+ str(self.x) +">"

def foo(x=None, add_to_d=None, d = {}):
    if add_to_d:
        d.update(add_to_d)
    if x is None:
        return
    if x in d:
        print "returning cached result, d is %s, x is %s"%(d, x)
        return d[x]
    d[x] = bar(x)
    return d[x]

def finit(cacheval):
    foo(x=None, add_to_d=cacheval)

def parafn():
    from multiprocessing import Pool
    arglist = []
    foo(1)
    pool = Pool(processes=2, initializer=finit, initargs=[foo.func_defaults[2]])
    arglist = range(4)
    results = []
    r = pool.map_async(foo, iterable=arglist, callback=results.append)
    r.get()
    r.wait()
    pool.close()
    pool.join()
    return results

print parafn()

rawDescription: Sharing state across multiple processes using the Python multiprocessing module


code:
alphabet = ' abcdefghijklmnopqrstuvwxyz'
print alphabet.find('b')
# prints '2'
print alphabet[alphabet.find('g') + alphabet.find('h')]
# prints 'o' as expected.

rawDescription: Adding the values of each chr(ord() character in a string?


code:
import lxml.etree

tree = lxml.etree.parse("http://someurl.com")
sites = tree.xpath("//site")

for site in sites:
    siteid = site.find("siteid").text
    print siteid

rawDescription: How do I pull data from XML document from between XML tags in Django/Python?

description: Pull data from XML document from between XML tags in Django/Python


code:
from twisted.internet import protocol, reactor
from twisted.protocols.basic import LineReceiver

class Echo(LineReceiver):
    def dataReceived(self, data):
        self.transport.write(data)

    def connectionLost(self, reason):
        print 'Client connection lost. Reason:\n{r}\n'.format(r=reason)
        LineReceiver.connectionLost(self, reason)
        reactor.stop()

class EchoFactory(protocol.Factory):
    def buildProtocol(self, addr):
        return Echo()

reactor.listenTCP(8000, EchoFactory())
reactor.run()

rawDescription: How to break a while true statement

description: Break a while true statement


code:
def myrepl(match):
    num = int(match.group(1))
    # Do something here
    return str(num) # this will replace the $url[2]

mystr = "this is $url[2] a string"

print re.sub(r'\$url\[([0-9])\]', myrepl, mystr, flags=re.IGNORECASE)

rawDescription: RegEx replace in Python

description: RegEx replace in Python


rawDescription: system of nonlinear equations can be solved in Maple, but not in sympy

description: System of nonlinear equations can be solved in Maple, but not in sympy


code:
list1 = []
list2 = []
howmany = int(input("How many words will you write?"))
n = int(input("What will the n number be?"))
for i in range(0, howmany, 1):
        word = raw_input("Write the word ")
        list1 = list1 + [word]
        if len(word) > n:
            list2 = list2 + [word]
print list2

rawDescription: Functions, lists, python

description: Functions, lists, python


code:
while True:
    try:
        up_key.click()
        item = WebDriverWait(driver, 5).until(lambda driver: driver.find_element_by_xpath(element_xpath))
        if item.text != 'awesome text':
            continue
        driver.close()
    except:
        print "Not found"

rawDescription: Continue when element is not found in selenium


code:
import sys
from PyQt4.QtGui import QDialog, QProgressBar, QPushButton, \
                    QLabel, QApplication, QVBoxLayout
from PyQt4.QtCore import QThread, pyqtSignal

def someprocess():
    bignumber = 1000000
    for i in range((bignumber+1)):
        if float(i)/bignumber in [float(count)/10 for count in range(11)]:
            yield i, float(i)/bignumber

class WorkerThread(QThread):
    progress = pyqtSignal(int)

    def run(self):
        it = someprocess()
        try:
            value, percentage = it.next()
            while value != None:
                value, percentage = it.next()
                self.progress.emit(percentage*100)
        except StopIteration:
            print "Completed!"
        except Exception, e:
            print "Unknown exception: ", repr(e)
            raise e

if __name__ == "__main__":
    it= someprocess()
    app = QApplication(sys.argv)
    mydialog = QDialog()
    myprogress = QProgressBar()
    mylabel = QLabel("Uninitiated.")
    mylayout = QVBoxLayout()
    mylayout.addWidget(mylabel)
    mylayout.addWidget(myprogress)
    mydialog.setLayout(mylayout)
    mydialog.show()
    w = WorkerThread()
    w.start()
    w.progress.connect(myprogress.setValue)
    sys.exit(app.exec_())

rawDescription: Is using PyQT's QProgressbar in combination with generators efficient?


code:
import random

def WeightedPick(d):
    r = random.uniform(0, sum(d.itervalues()))
    s = 0.0
    for k, w in d.iteritems():
        s += w
        if r < s: return k
    return k

def Test():
    k = {'A': 68, 'B': 62, 'C': 47, 'D': 16, 'E': 81}
    results = {}
    for x in xrange(10000):
        p = WeightedPick(k)
        results[p] = results.get(p, 0) + 1
    print results

Test()

rawDescription: (Python) algorithm to randomly select a key based on proportionality/weight


code:
query = session.query(Email.address)
print [str(column['expr']) for column in query.column_descriptions]  # should print ["Email.address"]

data = query.filter(Email.id==str(myId)).one()

rawDescription: SQLAlchemy : is there any good automated way to rename columns


rawDescription: Average of two consecutive elements in the list in Python


code:
text = """hello what is your name
my name is david"""
print text.split()

#output: ['hello', 'what', 'is', 'your', 'name', 'my', 'name', 'is', 'david']

rawDescription: Making a list of strings with 2 parameters. Python 3.3


code:
from bs4 import BeautifulSoup
import requests
url = "http://www.ssa.gov/cgi-bin/popularnames.cgi"
payload = {
    'year': 2010,
    'top': 50
}
r = requests.post(url, data=payload)
soup = BeautifulSoup(r.text)
table = soup.find('table', summary='Popularity for top 50')
for row in table.find_all('tr')[1:4]:
    print [td.text for td in row.find_all('td')]

rawDescription: Requests post on form not returning the generated page


code:
for entry in soup2:
    if entry.findAll(text=re.compile("Today")):
        print entry

rawDescription: Find 2 attributes in BeautifulSoup

description: Find 2 attributes in BeautifulSoup


code:
import numpy as np
x = np.array([[10, 1], [2, 12], [3, 5]])
multi_cols = x[:,0] * x[:,1]
print x[multi_cols.argmin()]

rawDescription: How can I query a numpy array to find the minimum multiplication of the first two columns?

description: Query a numpy array to find the minimum multiplication of the first two columns


rawDescription: Mapping result of psycopg2 into dataframe for R with RPY2


code:
def search(self, index=None, **kwargs):
    """
    This will use the & syntax with all the kwargs provided.
    For example: es.search(name="margin", id=4) ==> ?name=margin&id=4.

    Use as a simplified search to gut check very basic things.
    """
    index = index or self.index
    qs = urllib.urlencode(kwargs)
    try:
        res = self.es.search(index=index, q=qs)
    except TransportError, e:
        print '>>>', e
        res = None
    return res

rawDescription: Simplest ElasticSearch query

description: Simplest ElasticSearch query


code:
for i in range(0,a.shape[0]):
    if (a[i,0] in b and a[i,1] in b and a[i,2] in b):
        print a[i]

rawDescription: Double loop with multiple "if" conditionals


code:
mylist=['a','b','c','d','e']
myorder=[3,2,0,1,4]
mylist = [ mylist[i] for i in myorder]
print mylist

rawDescription: How can I reorder a list in python?

description: Reorder a list in python


code:
from inspect import getmembers, isfunction, getsource

import your_module
print getmembers(your_module, isfunction)
# [('anotherfile', <function anotherfile at 0x028129B0>), ('ihavefile', <function ihavefile at 0x027F3570>)]
for name, func in getmembers(your_module, isfunction):
    print getsource(func)

rawDescription: Extract information from Python file

description: Extract information from Python file


code:
import re
import sys

with open('filea.html') as originalFile:
    originalFileContents = originalFile.read()

pattern = re.compile(r'[0-9a-f]{32}_se')
originalMatches = pattern.findall(originalFileContents)

counter = 0

def replaceId(match):
    global counter
    value = match.group()
    newValue = originalMatches[counter]
    print counter, '=> replacing', value, 'with', newValue
    counter = counter + 1
    return newValue

with open('fileb.html') as targetFile:
    targetFileContents = targetFile.read()

changedTargetFileContents = pattern.sub(replaceId, targetFileContents)
print changedTargetFileContents

new_file = open("Output.html", "w")
new_file.write(changedTargetFileContents)
new_file.close()

rawDescription: Compare a regex match from two separate files and replace with values from one of them


code:
def main():
    my_list = [1,2,3,4,5,6,3,4,63,3]
    sort(my_list)

def sort(my_list):
    size = len(my_list)
    for i in range(size):
        for j in range(size-i-1):
            if(my_list[j] > my_list[j+1]):
                tmp = my_list[j]
                my_list[j] = my_list[j+1]
                my_list[j+1] = tmp
    print my_list

if __name__ == '__main__':
    main()

rawDescription: Sorting A List From Least to Greatest Without list.sort( )


code:
import csv
with open('agGDPpct.csv','r') as f:
  inputfile = csv.DictReader(f)
  list = []

  for line in inputfile:
    list.append([line[1], line[0], line[53], line[54], line[55], line[56], line[57]])
print list

rawDescription: List of lists (not just list) in Python

description: List of lists (not just list) in Python


code:
from Tkinter import *

oneThing = {"Name:": "Guido", "Tel.:":"666-6969", "Email:":"foobar@lol.com"}
another = {"Name:": "Philler", "Tel.:":"111-1111", "Email:":"philler@lol.com"}
z = [oneThing, another]

root = Tk()
l = Listbox(root)
l.pack(fill = "both")
l.pack_propagate(True)
[l.insert(END, item) for item in z]

def createPerson(index):

    #This is whatever function that creates stuff

    def edit():

        for i in range(len(labels)):
            z[index][labels[i]] = entries[i].get()
        print z
        top.destroy()

    top = Toplevel()
    labels = ["Name:", "Tel.:", "Email:"]
    i = 0
    for text in labels:
        Label(top, text = text).grid(column = 0, row = i)
        i += 1

    e1 = Entry(top)
    e1.grid(column = 1, row = 0)
    e2 = Entry(top)
    e2.grid(column = 1, row = 1)
    e3 = Entry(top)
    e3.grid(column = 1, row = 2)
    Button(top, text = "Submit", command = edit).grid(column = 1, row = 3)
    entries = [e1, e2, e3]

    #Return reference to toplevel so that root can wait for it to run its course
    return top

def edit():
    global l, z, root
    # Get dictionary from listbox
    sel = l.curselection()
    if len(sel) > 0:
        indexToEdit = z.index(eval(l.get(sel[0])))
        l.delete(sel)
        root.wait_window(createPerson(indexToEdit))
        print z[indexToEdit]
        l.insert(sel, z[indexToEdit])

Button(root, text = "Edit", command = edit).pack()
root.mainloop()

rawDescription: Editing an object in a Listbox and the list where it comes from


code:
#!/usr/bin/env python

with open('f1') as  fd:
    f1 = [map(int,i.strip().split()) for i in fd]
with open('f2') as  fd:
    f2 = [map(float,i.strip().split()) for i in fd]

for n, f in enumerate(f2):
    if f1[n][0] == 1 and f1[n][1] == -2:
        print f[0], f[1], 8
    else:
        print f

rawDescription: Read multiple files and change fields

description: Read multiple files and change fields


code:
import sys
import re

if __name__ == "__main__":
    instr = sys.argv[1]
    subs = []
    success = False
    while not success:
        try:
            indict = eval(instr)
            success = True
        except NameError, e:
            defname = re.search("name '([^\']*)' is not defined", str(e)).group(1)
            subs.append(defname)
            locals()[defname] = '//substitute{%d}' % (len(subs) - 1)
    outlist = indict.items()
    outstr = str(outlist)
    for i, sub in enumerate(subs):
        outstr = outstr.replace("'//substitute{%d}'" % i, sub)
    print outstr

rawDescription: Using eval() with Undefined Variables

description: Using eval() with Undefined Variables


code:
text = ["123456","12342","234567"]
with open("indput.txt") as f,open("output.txt","w") as out:
    lines = f.readlines()
    print lines
    for ind,line in enumerate(lines):
        if line.strip().startswith("Number"):
            lines[ind] = (line+"     "+'\n     '.join(text) + '\n')
            break
    for line in lines:
        out.write(line)

rawDescription: python get index value and write text

description: Python get index value and write text


code:
from scipy import misc
image_name = 'Birds.jpg'
calc_mean = misc.imread(image_name)
print calc_mean.mean()

rawDescription: Calculating the mean of an image with pyFITS in Python


code:
import cv2

cap = cv2.VideoCapture("./out.mp4")
while not cap.isOpened():
    cap = cv2.VideoCapture("./out.mp4")
    cv2.waitKey(1000)
    print "Wait for the header"

pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
while True:
    flag, frame = cap.read()
    if flag:
        # The frame is ready and already captured
        cv2.imshow('video', frame)
        pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
        print str(pos_frame)+" frames"
    else:
        # The next frame is not ready, so we try to read it again
        cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos_frame-1)
        print "frame is not ready"
        # It is better to wait for a while for the next frame to be ready
        cv2.waitKey(1000)

    if cv2.waitKey(10) == 27:
        break
    if cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT):
        # If the number of captured frames is equal to the total number of frames,
        # we stop
        break

rawDescription: How to process images of a video, frame by frame in video streaming using Opencv python

description: Process images of a video, frame by frame in video streaming using Opencv python


code:
from multiprocessing import Process, Queue

def my_function(q, x):
    q.put(x + 100)

if __name__ == '__main__':
    queue = Queue()
    p = Process(target=my_function, args=(queue, 1))
    p.start()
    p.join() # this blocks until the process terminates
    result = queue.get()
    print result

rawDescription: Is it possible to run function in a subprocess without threading or writing a separate file/script


code:
import random

class DECK():
    def __init__(self):
        suite = ('Spades', 'Hearts', 'Diamonds', 'Clubs')
        rank = ('2', '3', '4', '5', '6', '7', '8', '9', '10', "Jack", "Queen", "King", "Ace")
        self.full_deck = []
        for s in suite:
            for r in rank:
                self.full_deck.append("%s of %s" % (r, s))

    def pick_item(self):
        card_key = random.randint(0, len(self.full_deck)-1)
        new_card = self.full_deck[card_key]
        del self.full_deck[card_key]
        return new_card

    def missing_card(self):
        print self.full_deck

d1 = DECK()
card1 = d1.pick_item()

print card1

d1.missing_card()

rawDescription: python: Class error __main__.foo instance at 0x

description: Python: Class error __main__.foo instance at 0x


code:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
h3 = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h3.one")))
print h3.text

rawDescription: Selenium code to wait until CSS class is available and extract text in Python


code:
#!/usr/bin/python

import pycurl
import sys

class Storage:
    def __init__(self):
        self.contents = ''
        self.line = 0

    def store(self, buf):
        self.line = self.line + 1
        self.contents = "%s%i: %s" % (self.contents, self.line, buf)

    def __str__(self):
        return self.contents

retrieved_body = Storage()
retrieved_headers = Storage()
c = pycurl.Curl()
c.setopt(c.URL, 'http://www.demaziere.fr/eve/')
c.setopt(c.WRITEFUNCTION, retrieved_body.store)
c.setopt(c.HEADERFUNCTION, retrieved_headers.store)
c.perform()
c.close()
print retrieved_headers
print retrieved_body

rawDescription: How to read the header with pycurl

description: Read the header with pycurl


code:
x = 1
with open(loc_path + 'SAMPLEDATA.TXT', 'rb') as textin:
    for line in textin:
        if line[3:].startswith(str(x).zfill(2)):
            print '%r' % line
        else:
            x = 1 # Restart counter
        x += 1
        if x >= 13:
            break # Stop reading

rawDescription: python iterate though text file until condition is met

description: Python iterate though text file until condition is met


code:
from collections import defaultdict
import six

def calc_token_frequencies(doc_list):
    frequencies = defaultdict(int)  # Each dict item will start off as int(0)
    for token_set in doc_list:
        for token in token_set:
            frequencies[token] += 1
    return frequencies

if __name__ == '__main__':
    # Use a list of sets here in order to leverage set features
    tokenized_document_list = [{"egg", "apple", "bread", "milk", "pear"},
                               {"egg", "apple", "bread", "milk"},
                               {"egg", "apple", "bread", "milk"}]

    # Count the number of documents each token was in.
    token_frequencies = calc_token_frequencies(tokenized_document_list)

    # I used 50% here instead of the example 5% so that it would do something useful.
    token_min_docs = 0.5*len(tokenized_document_list)

    # Calculate the black list via set comprehension.
    token_blacklist = {token for token, doc_count in six.iteritems(token_frequencies)
                       if doc_count < token_min_docs}

    # Remove items on the black list
    for doc_tokens in tokenized_document_list:
        doc_tokens.difference_update(token_blacklist)

    print tokenized_document_list

rawDescription: Removing words in a list that appear infrequently


code:
import time, calendar

# Get the current time in a localtime manner (we need it for the day and year)
today = time.localtime(time.time())
# We put the day, month and year into a strftime -> localtime converter
# giving us the localtime version of 00:00:00 of today (there's cleaner version for this process)
today_strftime = time.strptime(str(today.tm_mday) + " " + time.strftime("%b") + " " + str(today.tm_year)[-2:], "%d %b %y")
# Then we take that localtime of 00:00:00 and put it into timegm which gives us the unix
# timestamp of 00:00:00 today, which we can subtract from time.time() to give us how many
# seconds of today has passed since 00:00:00.. Which we can use to compare integers with eachother (more efficient, and easy to compare)
unixtime_beginning_of_day = calendar.timegm(today_strftime)
time_passed_today = time.time() - unixtime_beginning_of_day

if time_passed_today > 3600:
    # 60sec per min * 60 minutes per hour == 3600 seconds in one hour.
    print "One hour has passed since the start of this day"

rawDescription: Scheduler to run certain tasks during certain times not working correctly


code:
verbs = {"dislikes":"blue", "knows":"black", "likes":"red"}
for s in open("/tmp/infile"):
  s = s.strip()
  for verb in verbs.keys():
    if (s.count(verb) > 0):
      print s.replace(verb,"->")+" "+verbs[verb]+";"
      break

rawDescription: Put bar at the end of every line that includes foo


code:
def player():
    card1 = deck()
    card2 = deck()
    hand = card1 + card2
    print "Cards dealt: %d and %d" % (card1, card2)
    while hand <= 21:
        choice = input_with_timeout("Would you like to hit or stand?: ", 3, "stand")
        print choice
        if choice == "hit":
            hand = hand + deck()
            print "Current Total: %d" % hand
        elif choice == "stand":
            return hand

rawDescription: python integrating code with raw_input timeout

description: Python integrating code with raw_input timeout


code:
for country in x:
    for city, pop in x[country].iteritems():
        if 'Ecuador' in city:
            print ''.join(city.split('_')[1:3])

rawDescription: For items in a dictonary, split the name of one item, add and remove some info python


code:
input_binary_string = "110010111111111111101010" ## must be a multiple of 8
out = []
while len(input_binary_string) >= 8:
    byte = input_binary_string[:8]
    input_binary_string = input_binary_string[8:]
    b = int(byte,2)
    c = chr(b)
    out.append(c)
## Better not have a bits left over
assert len(input_binary_string) == 0
outString = "".join(out)

print [ ord(c) for c in out ]

rawDescription: Python Fixed Length Packet

description: Python Fixed Length Packet


code:
from textblob import Blobber
from textblob.sentiments import NaiveBayesAnalyzer
tb = Blobber(analyzer=NaiveBayesAnalyzer())

print tb("sentence you want to test")

rawDescription: TextBlob NaiveBayesAnalyzer extremely slow (compared to Pattern)


code:
def codes(lst, cods):
    lst1=lst[:]
    lst[:] = []

    for element in lst1:
        cccc = element[0:4]
        yyyy = element[4:8]
        mm = element[8:10]
        dd = element[10:12]

        if cccc in cods:
            lst += [cccc, int(dd), int(mm), int(yyyy)]
        else:
            lst += [cccc + yyyy + mm + dd]

    return lst

first = ['Cara20130716', 'Tara20080601', 'PALA19961231', 'Melo19601023']
secnd = ['PALA', 'CARA', 'Tara']
print codes(first, secnd)

rawDescription: Python: if arrived to end of list

description: Python: if arrived to end of list


code:
from socket import inet_aton
from struct import unpack

def atol(a):
    return unpack(">L", inet_aton(a))[0]

addr = atol("30.44.230.0")
mask = atol("255.255.0.0")
lo = atol("32.44.230.0")
hi = atol("32.44.230.255")
prefix = addr & mask

print lo <= prefix <= hi

rawDescription: Subnet is in an ip range in Python

description: Subnet is in an ip range in Python


code:
for l in itertools.product(*letters):
    print ''.join(l)

rawDescription: Python Cartesian Product

description: Python Cartesian Product


code: print '\n'.join('\t'.join(x) for x in L)

rawDescription: Export list as .txt (Python)

description: Export list as .txt (Python)


code:
import os
import sys
import io

try:
    if os.isatty(sys.stdin.fileno()):
        print "msvcrt.getch() will work."
    else:
        print "msvcrt.getch() will fail."
except (AttributeError, io.UnsupportedOperation):
    print "msvcrt.getch() will fail."

rawDescription: How to tell if you're running through Pythonwin or console

description: Tell if you're running through Pythonwin or console


code:
for x,y,z in itertools.product(range(2), range(2), range(3)):
    print x,y,z

rawDescription: Python nested looping Idiom

description: Python nested looping Idiom


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

def mypie(slices,labels,colors):

    colordict={}
    for l,c in zip(labels,colors):
        print l,c
        colordict[l]=c

    fig = plt.figure(figsize=[10, 10])
    ax = fig.add_subplot(111)

    pie_wedge_collection = ax.pie(slices, labels=labels, labeldistance=1.05)#, autopct=make_autopct(slices))

    for pie_wedge in pie_wedge_collection[0]:
        pie_wedge.set_edgecolor('white')
        pie_wedge.set_facecolor(colordict[pie_wedge.get_label()])

    titlestring = 'Issues'

    ax.set_title(titlestring)

    return fig,ax,pie_wedge_collection

slices = [37, 39, 39, 38, 62, 21, 15,  9,  6,  7,  6,  5,  4, 3]
cmap = plt.cm.prism
colors = cmap(np.linspace(0., 1., len(slices)))
labels = [u'TI', u'Con', u'FR', u'TraI', u'Bug', u'Data', u'Int', u'KB', u'Other', u'Dep', u'PW', u'Uns', u'Perf', u'Dep']

fig,ax,pie_wedge_collection = mypie(slices,labels,colors)

plt.show()

rawDescription: Assign specific colours to data in Matplotlib pie chart


code:
import fileinput

def report(first,last,count):
  if first == last:
    print first, count
  else:
    print first, "-", last, count

first = None
last = None
count = 0
for line in fileinput.input():
  line = line.strip()
  if line == "--":
    if last:
      report(first, last, count)
    last = first = None
    count = 0
  else:
    words = line.split()
    i = words[0]
    n = int(words[1])
    if not first:
      first = i
      count = 0
    last = i
    count = count + n
if first:
  report(first, last, count)

rawDescription: Clustering data in a file

description: Clustering data in a file


code:
for i in [float(j) / 100 for j in range(0, 100, 1)]:
    print i

rawDescription: How do get more control over loop increments in Python?


code:
fname = raw_input("Enter file name: ")
with open(fname) as fh: #  use with to automatically close the file
    final = []
    new = [x.rstrip().split() for x in fh] # strip and split into sublists of words
    for ele in new:
        for word in ele: # for every word in each sublist
            if not word in final: # if word not already in final then add word
                final.append(word)
    final.sort()  # sort
    print final

rawDescription: how to sort and append the list of words to an empty list in python

description: Sort and append the list of words to an empty list in python


code:
def run_all(path):
    import glob, os
    print "Exploring %s" % path
    for filename in glob.glob(path + "/*.py"):
        # modulename = "bot_paperino"
        modulename = os.path.splitext(os.path.split(filename)[-1])[0]
        # classname = "Paperino"
        classname = modulename.split("bot_")[-1].capitalize()
        # package = "path.bot_paperino"
        package = filename.replace("\\", "/").replace("/", ".")[:-3]
        mod = __import__(package)
        if classname in mod.__dict__[modulename].__dict__.keys():
            obj = mod.__dict__[modulename].__dict__[classname]()
            if hasattr(obj, "go"):
                obj.go()

if __name__ == "__main__":
    import sys
    # Run on each directory passed on command line
    for path in sys.argv[1:]:
        run_all(sys.argv[1])

rawDescription: Best way to create a "runner" script in Python?

description: Create a "runner" script in Python


code:
from xml.etree import ElementTree as ET

NS = "urn:ebay:apis:eBLBaseComponents"

tree = ET.parse('response.xml')

for elem in tree.iter("*"):    # Use tree.getiterator("*") in Python 2.5 and 2.6
    if elem.tag == '{%s}OrderID' % NS:
        print elem.text

rawDescription: Element Tree: How to parse subElements of child nodes


code:
getDemos = picam.Picam_GetAvailableDemoCameraModels
getDemos.argtypes = [ctypes.POINTER(ctypes.POINTER(PicamCameraID)) , ctypes.POINTER(piint)]
mem = ctypes.POINTER(PicamCameraID)()
size = ctypes.c_int(0)
getDemos(ctypes.byref(mem), ctypes.byref(size))
print size.value

rawDescription: python ctypes to pass a pointer to a pointer to a struct dynamically allocating

description: Python ctypes to pass a pointer to a pointer to a struct dynamically allocating


code:
from mako.template import Template
from mako.lookup import TemplateLookup

template_path = "c:/Users/username/Desktop/xampp/htdocs/blog"
mylookup = TemplateLookup(directories=[path])

def serve_template(templatename, **kwargs):
    templatename = "scripts/templates/index.html"
    mytemplate = mylookup.get_template(templatename)
    print mytemplate.render(**kwargs)

rawDescription: Changing the root directory Python and mod_wsgi


code:
import numpy as np
import cv2

im = cv2.imread('tricky.png')

# For better visibility, resize image to better fit screen
#im= cv2.resize(im, dsize=(0,0),fx=0.25, fy=0.25)

#convert to gray value for blob analysis
imgray= cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

#### Blob analysis to find inner white leaves
# SimpleBlobDetector will find black blobs on white surface, this is why type=cv2.THRESH_BINARY_INV is necessary
ret,imthresh = cv2.threshold(imgray,160, 255,type=cv2.THRESH_BINARY_INV)

# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()

# Filter by Area.
params.filterByArea = True
params.minArea = 15000
params.maxArea = 150000

# Create a detector with the parameters
detector = cv2.SimpleBlobDetector_create(params)

# Detect blobs.
keypoints = detector.detect(imthresh)

# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures
# the size of the circle corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(imthresh, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show blobs
cv2.imshow("Keypoints", im_with_keypoints)

####floodfill inner white leaves with blue
#http://docs.opencv.org/3.0-beta/modules/imgproc/doc/miscellaneous_transformations.html

#Create a black mask for floodfill. Mask needs to be 2 pixel wider and taller
maskborder=imgray.copy()
maskborder[:] = 0
bordersize=1
maskborder=cv2.copyMakeBorder(maskborder, top=bordersize, bottom=bordersize, left=bordersize, right=bordersize, borderType= cv2.BORDER_CONSTANT, value=[255,255,255] )
print imgray.shape[:2]
print maskborder.shape[:2]

#Create result image for floodfill
result = im.copy()

#fill white inner segments with blue color
for k in keypoints:
    print int(k.pt[0]),int(k.pt[1])
    seed_pt = int(k.pt[0]),int(k.pt[1])
    cv2.floodFill(result, maskborder, seed_pt, (255,0, 0))

#### Blob analysis to find small triangles
# SimpleBlobDetector will find black blobs on white surface, this is why type=cv2.THRESH_BINARY_INV is necessary
ret,imthresh2 = cv2.threshold(imgray,150, 255,type=cv2.THRESH_BINARY)
ret,imthresh3 = cv2.threshold(imgray,140, 255,type=cv2.THRESH_BINARY_INV)
imthresh4 = cv2.add(imthresh2,imthresh3)

# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()

# Filter by Area.
params.filterByArea = True
params.minArea = 20
params.maxArea = 1000
params.maxArea = 50000 #Using this line includes the outer dark leaves. Comment out if necessary

# Don't filter by Circularity
params.filterByCircularity = False

# Don't filter by Convexity
params.filterByConvexity = False

# Don't filter by Inertia
params.filterByInertia = False

# Create a detector with the parameters
detector = cv2.SimpleBlobDetector_create(params)

# Detect blobs.
keypoints = detector.detect(imthresh4)

# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures
# the size of the circle corresponds to the size of blob
im_with_keypoints2 = cv2.drawKeypoints(imthresh4, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show blobs
cv2.imshow("Keypoints2", im_with_keypoints2)

####floodfill triangles with green
#http://docs.opencv.org/3.0-beta/modules/imgproc/doc/miscellaneous_transformations.html

#Create a black mask for floodfill. Mask needs to be 2 pixel wider and taller
maskborder=imgray.copy()
maskborder[:] = 0
bordersize=1
maskborder=cv2.copyMakeBorder(maskborder, top=bordersize, bottom=bordersize, left=bordersize, right=bordersize, borderType= cv2.BORDER_CONSTANT, value=[255,255,255] )
print imgray.shape[:2]
print maskborder.shape[:2]

#Create result image for floodfill
result2 = result.copy()

#fill triangles with green color
for k in keypoints:
    print int(k.pt[0]),int(k.pt[1])
    seed_pt = int(k.pt[0]),int(k.pt[1])
    cv2.floodFill(result2, maskborder, seed_pt, (0,255, 0))

#cv2.imshow('main',im)
#cv2.imshow('gray',imgray)
#cv2.imshow('borders',maskborder)
#cv2.imshow('threshold2',imthresh2)
#cv2.imshow('threshold3',imthresh3)
#cv2.imshow('threshold4',imthresh4)
cv2.imshow("Result", result2)
cv2.imwrite("result.png",result2)

cv2.waitKey(0)
cv2.destroyAllWindows()

rawDescription: How can I fill arbitrary closed regions in Matplotlib?

description: Fill arbitrary closed regions in Matplotlib


code:
import re

text = "this is my desc abc-3456"
m = re.findall("\w+\\-\d+", text)
print m[0]

rawDescription: How to extract just the characters "abc-3456" from the given text in python

description: Extract just the characters "abc-3456" from the given text in python


code:
import re
pattern = re.compile(r'^[0-9]+\.[0-9]+')

# ... later ...

version = '1.2.3.4.5.6.7'

def get_version(s):
    m = pattern.search(s)
    if m:
        return m.group()

print get_version(version)

rawDescription: Get sub-string for major version out of full version string


code:
A=int("100",2)
B=int("010",2)
C=int("001",2)
print A,B,C
movie = A|B
print movie
print bin(movie)

rawDescription: one hot encoder: how to encode multible value of same category?

description: One hot encoder: how to encode multible value of same category


code:
import abc
class B(object):
    __metaclass__ = abc.ABCMeta
    @abc.abstractmethod
    def foo(self):
            print 'In B'

class C(B):
    def foo(self):
            super(C, self).foo()
            print 'In C'

C().foo()

rawDescription: Python's super(), abstract base classes, and NotImplementedError


code:
import time
import sys
for i in range(100):
    print i,
    sys.stdout.flush()
    time.sleep(1)
    print "\r",

rawDescription: How to rewrite output in terminal

description: Rewrite output in terminal


code:
import os
import random
import shutil

def copy_sample(src, dst, size=500):
    files = [os.path.join(src, i) for i in random.sample(os.listdir(src), size)]
    count = len(files)
    for index, afile in enumerate(files):
        try:
           shutil.copy(afile, dst)
           print 'Copied file %s (%d/%d)' % (afile, index + 1, count)
        except Exception, msg:
           print 'Failed file %s (%d/%d) -- %s' % (afile, index + 1, count, msg)

src = '/home/mjanja/Desktop/Leaves'
dst = '/home/mjanja/Desktop/Positive Leaves'

copy_sample(src, dst)

rawDescription: shutil and copying files

description: Shutil and copying files


code:
import wx

class MainWindow(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(900, 600) )

        SubmissionsPanel(self)
        self.Show()

class SubmissionsPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        #Create some sizers:
        main_sizer = wx.BoxSizer(wx.VERTICAL)
        top_row_sizer = wx.BoxSizer(wx.HORIZONTAL);
        radio_static_box = wx.StaticBox(self, label='') #You have to add this widget to parent BEFORE
                                                        #contained widgets otherwise buttons won't operate.
        radio_static_box_sizer = wx.StaticBoxSizer(radio_static_box, wx.HORIZONTAL)

        job_static_box = wx.StaticBox(self, label='')  #You have to add this widget to parent BEFORE
                                                       #contained widgets otherwise buttons won't operate.
        job_static_box_sizer = wx.StaticBoxSizer(job_static_box, wx.HORIZONTAL)

        '''
        radio_labels = ['Verify Mode', 'Upload Mode']
        self.radio_box = wx.RadioBox(
                self, -1, "",
                choices=radio_labels,
        )
        '''
        #I couldn't get the radio buttons inside a RadioBox to line up vertically
        #with the checkbox, so I did the following:
        radio1 = wx.RadioButton(self, label='Verify Mode', style=wx.RB_GROUP)
        radio2 = wx.RadioButton(self, label='Upload Mode')
        radio1.Bind(wx.EVT_RADIOBUTTON, self.on_radio_click)
        radio2.Bind(wx.EVT_RADIOBUTTON, self.on_radio_click)

        radio_static_box_sizer.Add(radio1, flag=wx.BOTTOM, border=4) #To make outline line up with the outline of checkbox,txtctrl
        radio_static_box_sizer.Add(radio2, flag=wx.LEFT, border=10)

        top_row_sizer.Add(radio_static_box_sizer, flag=wx.LEFT, border=10)

        #Create Job Title widgets:
        self.check_box = wx.CheckBox(self, label="Job Title")
        self.check_box.SetValue(False)
        self.check_box.Bind(wx.EVT_CHECKBOX, self.ShowOrHideTVTitle)

        self.txtTitle = wx.TextCtrl(self, style=wx.SUNKEN_BORDER, value="Enter job title here...", size=(200, -1))
        self.txtTitle.Show(False)

        job_static_box_sizer.Add(self.check_box, flag=wx.LEFT, border=10)
        job_static_box_sizer.Add(self.txtTitle, flag=wx.RESERVE_SPACE_EVEN_IF_HIDDEN | wx.LEFT, border=10)

        top_row_sizer.Add(job_static_box_sizer, flag=wx.LEFT, border=10);

        main_sizer.Add(top_row_sizer);
        main_sizer.Add((-1, 20))  #Add 20 pixels of vertical space

        #Drag n Drop widgets:
        drag_drop_text = wx.StaticText(self, -1, 'Drag n Drop itmsp folder')
        main_sizer.Add(drag_drop_text, flag=wx.LEFT, border=10)
        main_sizer.Add((-1, 3))  #Add 3 pixels of vertical space

        third_row_sizer = wx.BoxSizer(wx.HORIZONTAL);
        self.tc_files = wx.TextCtrl(self, size=(375, 25))
        self.buttonGo = wx.Button(self, label='Submit')
        third_row_sizer.Add(self.tc_files, flag=wx.LEFT, border=10)
        third_row_sizer.Add(self.buttonGo, flag=wx.LEFT, border=10)
        main_sizer.Add(third_row_sizer)

        #Create ListCtrl:
        self.index = 0
        self.log_text22 = wx.ListCtrl(self,size=(800,275),
                         style=wx.LC_REPORT | wx.BORDER_SUNKEN | wx.LC_SINGLE_SEL | wx.LC_VRULES | wx.LC_HRULES)

        font = wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
        self.log_text22.SetFont(font)

        self.log_text22.InsertColumn(1, 'Job ID', width=50, format=wx.LIST_FORMAT_CENTRE)
        self.log_text22.InsertColumn(2, 'Mode  ', width=50, format=wx.LIST_FORMAT_CENTRE)
        self.log_text22.InsertColumn(3, 'Title', width=183)
        self.log_text22.InsertColumn(4, 'Package', width=227)
        self.log_text22.InsertColumn(5, 'Submitted By', width=100)
        self.log_text22.InsertColumn(6, 'Sumbit Time', width=85)
        self.log_text22.InsertColumn(7, 'Process', width=50, format=wx.LIST_FORMAT_CENTRE)
        self.log_text22.InsertColumn(8, 'Status  ', width=50, format=wx.LIST_FORMAT_CENTRE)

        main_sizer.Add((-1, 20)) #Add 20px of vertical space
        list_sizer = wx.BoxSizer(wx.VERTICAL)
        list_sizer.Add(self.log_text22, flag=wx.LEFT, border=10)
        main_sizer.Add(list_sizer)

        #Create Quit button:
        list_sizer.Add((-1, 10))  #Add 15px of vertical space
        self.buttonClose = wx.Button(self, -1, "Quit Application")
        list_sizer.Add(self.buttonClose, flag=wx.ALIGN_CENTER | wx.TOP | wx.LEFT, border=10)

        self.SetSizer(main_sizer)

    def on_radio_click(self, evt):
        print "You clicked?"

    def ShowOrHideTVTitle(self, evt):
        element = evt.GetEventObject()
        isChecked = element.GetValue()

        if isChecked:
            self.txtTitle.Show(True)
            self.txtTitle.SetFocus()
        else:
            self.txtTitle.Show(False)
            self.tc_files.SetFocus()

    def OnSubmit(self):
        pass

    def OnClose(self):
        pass

    def showLogFile(self):
        pass

    def right_click(self):
        pass

app = wx.App(redirect=False) ##Instead of seeing just a window flash, show errors in terminal.
MainWindow(None, -1, 'Test App')
app.MainLoop()

rawDescription: Wxpython - convert static positioning to sizer


code:
with open("file.txt") as f:
    for i in range(9):
        f.next()
    print f.readline()

rawDescription: How do I pull text from a specific text line in Python?

description: Pull text from a specific text line in Python


code:
class valueContainer(object):
    def __init__(self, value):
        self.value = value
    def __repr__(self):
        return self.value.__repr__()

v1 = valueContainer(1)

myDict = {'myvar': v1}
print myDict
#{'myvar': 1}

v1.value = 2
print myDict
#{'myvar': 2}

rawDescription: Variables as hash values in Python

description: Variables as hash values in Python


code:
import networkx as nx
import matplotlib.pyplot as plt

graph = nx.Graph()
with open('graphdata.txt') as f:
    for line in f:
        firm_id_1, firm_id_2 = line.split()
        graph.add_edge(firm_id_1, firm_id_2)

paths_from_1 = nx.shortest_path(graph, "1")
for path in paths_from_1:
    if len(paths_from_1[node]) > 9:
        print "Shortest path from 1 to", node, "is longer than 9"

pos = nx.spring_layout(graph, iterations=200)
nx.draw(graph, pos)
plt.savefig("mynetwork.png")
plt.show()

rawDescription: Python: Graph a Network from Data

description: Python: Graph a Network from Data