code:
import matplotlib.cm as cm
import matplotlib.colors as colors

norm = colors.Normalize(vmin=-14, vmax=86)
f2rgb = cm.ScalarMappable(norm=norm, cmap=cm.get_cmap('RdYlGn'))

def f2hex(f2rgb, f):
    rgb = f2rgb.to_rgba(f)[:3]
    return '#%02x%02x%02x' % tuple([255*fc for fc in rgb])

print f2hex(f2rgb, -13.5)  #   gives #a60126  ie, dark red
print f2hex(f2rgb, 85.5)   #   gives #016937  ie, dark green with some blue

rawDescription: Assign color to value

description: Assign color to value


code:
for dct in mylist:
    for key, val in dct.items():
        print key
        print val

rawDescription: How to loop over a list of dictionaries?

description: Loop over a list of dictionaries


code:
root = ast.parse('''
def foo():
    """the foo function"""
    pass
''')
for node in ast.walk(root):
    if isinstance(node, (ast.FunctionDef, ast.ClassDef, ast.Module)):
        if (node.body and isinstance(node.body[0], ast.Expr) and
            isinstance(node.body[0].value, ast.Str)):
            print node.body[0].value.lineno, node.body[0].value.s

rawDescription: Extract line numbers of all docstrings?

description: Extract line numbers of all docstrings


code:
from itertools import chain

class PrintableList(list): # for a list of dicts
    def __str__(self):
        return '. '.join(' '.join(str(x) for x in
            chain.from_iterable(zip((item[0], 'is', 'and'), item[1])))
                for item in (item.items()[0] for item in self)) + '.'

class PrintableDict(dict): # for a dict
    def __str__(self):
        return '. '.join(' '.join(str(x) for x in
            chain.from_iterable(zip((item[0], 'is', 'and'), item[1])))
                for item in self.iteritems()) + '.'

class Foo:
   def __init__(self):
      self.d = PrintableDict({"Susan": ("Boyle", 50, "alive"),
                              "Albert": ("Speer", 106, "dead")})

class Bar:
   def __init__(self):
      self.l = PrintableList([{"Susan": ("Boyle", 50, "alive")},
                              {"Albert": ("Speer", 106, "dead")}])

foo = Foo()
print self.d
bar = Bar()
print self.l

rawDescription: python __str__ for an object

description: Python __str__ for an object


code:
for i in range(n):
    if n%i == 0:
        print i

rawDescription: Python RSA Brute Force Check

description: Python RSA Brute Force Check


code:
import os

process =os.popen('/usr/bin/zbarcam','r')
while True:
    print 'Got barcode:', process.readline()

rawDescription: Python executing shell command doesn't terminate


code:
import fileinput

translations = {}

with open('file1.txt', 'r') as fileOne:
    trans = fileOne.readlines()

    for line in trans:
        if (line.startswith("\"")):
            key, value = line.strip().split(" = ")
            translations[key] = value

for line in fileinput.input('file2.txt', inplace=True):
    if (line.startswith("\"")):
        key, value = line.strip().split(" = ")
        if key in translations:
            line = "{} = {}".format(key, translations[key])
    print line.strip()

rawDescription: Loop through and copy key value pairs

description: Loop through and copy key value pairs


code:
from random import shuffle
sample_dict = {"M": ["L", "V", "A"], "C": ["N", "K", "W"]}

def get_value(dictionary, key):
    a = dictionary[key]
    shuffle(a)
    return a

b = get_value(sample_dict, "M")
print b

rawDescription: How to get dictionary values in shape of lists with random.shuffle

description: Get dictionary values in shape of lists with random.shuffle


code:
texts = []
def notes():
    global texts
    txt = raw_input("\nPlease enter any notes.\n>>> ")
    texts.append(txt) # Add the entered text inside the list
    print "\Note added to report."
    notes_menu()

def print_note():
    for txt in texts:
        new_report.write("\nNotes: \n%r" % txt)

rawDescription: How to write a repeatable raw_input?

description: Write a repeatable raw_input


code:
from string import Template

name = 'Renata'
place = 'hospital'
job = 'Dr.'
how = 'glad'
header = '\nTo Ms. {name}:'

letter = Template("""
Hello Ms. $name.

I'm glad to inform, you've been
accepted in our $place, and $job Red
will ${how}ly recieve you tomorrow morning.
""")

print header.format(**vars())
print letter.substitute(vars())

rawDescription: Python string interpolation implementation


code:
from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        widget = QtGui.QWidget(self)
        self.setCentralWidget(widget)
        layout = QtGui.QVBoxLayout(widget)
        self.listA = QtGui.QListWidget(self)
        self.textedit = QtGui.QPlainTextEdit(self)
        self.textedit.installEventFilter(self)
        self.listA.setDragEnabled(True)

        for widget in (self.listA, self.textedit):
            widget.setAcceptDrops(True)
            layout.addWidget(widget)

        for item in 'One Two Three Four Five Six'.split():
            self.listA.addItem(item)

    def eventFilter(self, source, event):
        if (event.type() == QtCore.QEvent.DragEnter and source is self.textedit):
            event.accept()
            print 'DragEnter'
            return True
        elif (event.type() == QtCore.QEvent.Drop and source is self.textedit):
            print 'Drop'
            return True
        else:
            return super(Window, self).eventFilter(source, event)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()

rawDescription: Drag&drop from QListWidget to QPlainTextEdit


code:
open('data.csv','w').write(
"""\
13986513,6,6/1/2014 12:00:00 AM,248.7
13986513,6,6/1/2014 12:00:05 AM,248.4
13986513,6,6/1/2014 12:00:10 AM,249
13986513,6,6/1/2014 12:00:15 AM,249.3
13986513,6,6/1/2014 12:00:20 AM,249.3
13986513,6,6/1/2014 12:00:25 AM,249.3
13986513,6,6/30/2014 11:55:00 PM,249.3
13986534,6,6/1/2014 12:00:00 AM,249
13986534,6,6/1/2014 12:00:05 AM,249
13986534,6,6/1/2014 12:00:10 AM,249.3
13986534,6,6/1/2014 12:00:15 AM,249.6
13986534,6,6/30/2014 11:55:00 PM,249.7\
""")

class ReadSensorLines(object):

    def __init__(self, filename):

        sensor_offsets = {}
        sensors = []

        readfp = open(filename, "rb")
        readfp.readline() # skip header

        # find start of each sensor
        # use readline not iteration so that tell offset is right

        offset = readfp.tell()
        sensor = ''

        while True:
            line = readfp.readline()
            if not line:
                break
            next_sensor = line.split(',', 1)[0]
            if next_sensor != sensor:
                if sensor:
                    sensors.append(sensor)
                    next_offset = readfp.tell()
                    sensor_offsets[sensor] = [offset, next_offset - offset]
                    sensor = next_sensor
                    offset = next_offset
                else:
                    # setup for first sensor
                    sensor = next_sensor
        if next_sensor:
            sensors.append(next_sensor)
            sensor_offsets[next_sensor] = [offset, readfp.tell() - offset]

        self.readfp = readfp
        self.sensor_offsets = sensor_offsets
        self.sensors = sensors

    def read_sensor(self, sensorname):
        pos_data = self.sensor_offsets[sensorname]
        self.readfp.seek(pos_data[0])
        line = self.readfp.readline(pos_data[1])
        pos_data[0] += len(line)
        pos_data[1] -= len(line)
        return line

    @property
    def data_remains(self):
        return any(pos_data[1] for pos_data in self.sensor_offsets.itervalues())

    def close(self):
        self.readfp.close()

sensor_lines = ReadSensorLines("data.csv")
while sensor_lines.data_remains:
    row = []
    for sensor in sensor_lines.sensors:
        sensor_line = sensor_lines.read_sensor(sensor)
        if sensor_line:
            _, _, date, volts = sensor_line.strip().split(',')
            row.append(volts)
        else:
            row.append('')
    row.insert(0, date)
    row[0] = str(datetime.datetime.strptime(row[0],'%m/%d/%Y %H:%M'))
    print ','.join(row)

rawDescription: Process a huge .csv file in python

description: Process a huge .csv file in python


code:
import sys

modulename = 'datetime'
if modulename not in sys.modules:
    print 'You have not imported the {} module'.format(modulename)

rawDescription: How to check if a python module has been imported?

description: Check if a python module has been imported


code:
file = raw_input("Enter path to filename :")

text_file= open(file,'r')
for line in text_file.readlines():
    print line[:2]
text_file.close()

rawDescription: How to remove characters from list

description: Remove characters from list


code:
from collections import defaultdict
from pprint import pprint

from hachoir_metadata import metadata
from hachoir_core.cmd_line import unicodeFilename
from hachoir_parser import createParser

# using this example http://archive.org/details/WorkToFishtestwmv
filename = './test_wmv.wmv'
filename, realname = unicodeFilename(filename), filename
parser = createParser(filename)

# See what keys you can extract
for k,v in metadata.extractMetadata(parser)._Metadata__data.iteritems():
    if v.values:
        print v.key, v.values[0].value

# Turn the tags into a defaultdict
metalist = metadata.extractMetadata(parser).exportPlaintext()
meta = defaultdict(defaultdict)
for item in metalist:
    if item.endswith(':'):
        k = item[:-1]
    else:
        tag, value = item.split(': ')
        tag = tag[2:]
        meta[k][tag] = value

print meta['Video stream #1']['Image width'] # 320 pixels

rawDescription: Hachoir - Retrieving data from a group

description: Hachoir - Retrieving data from a group


code:
import urllib2
out = []
for url in ["http://stackoverflow.com/", "http://stackoverflow.com/questions/"]:
    try:
        connection = urllib2.urlopen(url)
        out.append(connection.getcode())
        connection.close()
    except urllib2.HTTPError, e:
        out.append(e.getcode())
print out

rawDescription: Python Printing A List Issue

description: Python Printing A List Issue


code:
class Phonebook:

    def __init__(self):
        self.bok = {'eddie': 1234,
                    'ed': 'eddie',
                    'peter': 4321,
                    'pete': 'peter'}

    def add(self, name, number):
        if name in self.bok.keys():
            print "The name does already exist."
        else:
            self.bok[name] = int(number)
            print self.bok

    def lookup(self, name):
        val = self.bok[name]
        while isinstance(val, str):
            val = self.bok[val]

        print "Name :", name
        print "Telephone number: ", val

    def alias(self, name, nick):
        if nick in self.bok:
            print 'that alias already exists'
            return
        self.bok[nick] = name

    def change(self, name, number):
        while isinstance(self.bok[name], str):
            name = self.bok[name]
        self.bok[name] = int(number)

rawDescription: Python (2.7.8) : Phonebook, Multiple keys with the same value (Alias)


code:
from csv import reader

# test
infile = ['A,B,C,"D12121",E,F,G,H,"I9,I8",J,K']
# real is probably like
# infile = open('filename', 'r')
# or use 'with open(...) as infile:' and indent the rest

for line in reader(infile):
    print line
# for the test input, prints
# ['A', 'B', 'C', 'D12121', 'E', 'F', 'G', 'H', 'I9,I8', 'J', 'K']

rawDescription: Split string on commas but ignore commas within double-quotes?


code:
import numpy as np
import re
def main():
    #sum values from the array if "Flag1"==1, "Flag2"==1, and "ZoneName" includes
    #either "Zone1" or "Zone2"
    myArray = np.array([(1, 1, 1, u'Zone3', 9.223),
    (2, 1, 0, u'Zone2', 17.589),
    (3, 1, 1, u'Zone1_Zone2', 26.95),
    (4, 0, 1, u'Zone2', 19.367),
    (5, 1, 1, u'Zone1_Zone3', 4.395),
    (5, 1, 1, u'Zone15', 8.565),
    (5, 1, 1, u'Zone2', 7.125),
    (5, 1, 0, u'Zone1', 6.395)],
     dtype=[('ID', '<i4'), ('Flag1', '<i4'), ('Flag2', '<i4'), ('ZoneName', '<U15'),
            ('Value', '<f8')])

    doneList = []  #empty list to track which zones have been calc'd
    values = [] #empty list to store values from loop
    criteriaList = ("Zone1", "Zone2")
    zones = myArray["ZoneName"]
    for criteria in criteriaList:
        for zone in zones:
            #only calc if the "ZoneName" value from the array includes the criteria
            #in the current loop and the record has not already been calc'd
            if ((find_word(str(np.char.split(zone,'_')), criteria)) & (zone not in doneList)):
                #the key element from previous question
                value = myArray[((myArray["Flag1"] == 1) & (myArray["Flag2"] == 1))
                    & np.in1d(myArray["ZoneName"], zone)]["Value"].sum()
                values.append(value)
                doneList.append(zone) #Needed to that "Zone1_Zone2" is not double counted
    result = sum(values)
    print result

def find_word(text, search):
   result = re.findall('\\b'+search+'\\b', text, flags=re.IGNORECASE)
   if len(result)>0:
      return True
   else:
      return False

if __name__ == '__main__':
    main()

rawDescription: Filter numpy structured array based on partial match to a list


code:
s = 'PRIMARY|SECONDARY'
print '|'.join(['^' + i + '$' for i in s.split('|')])
s = 'PRIMARY'
print '|'.join(['^' + i + '$' for i in s.split('|')])
s = 'PRIMARY|SECONDARY|TERTIARY'
print '|'.join(['^' + i + '$' for i in s.split('|')])

rawDescription: Pipe replacement with character

description: Pipe replacement with character


code:
import urllib2
import urllib

values = dict(state="CA", city="SF") # examine form for actual vars
try:
    req = urllib2.Request("http://example.com/post.php",
                          urllib.urlencode(values))
    response_page = urllib2.urlopen(req).read()
except urllib2.HTTPError, details:
    pass #do something with the error here...

rawDescription: Python Mechanize with Dynamic Dropdown Selection


code:
import sys, os, csv
from xml.sax.saxutils import quoteattr

smil_header = """\
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd">
<smil xmlns="http://www.w3.org/2001/SMIL20/Language">
  <head>
    <meta base="rtmp://cp23636.edgefcs.net/ondemand"/>
  </head>
  <body>
    <switch>
"""
smil_video = """\
      <video src=%s system-bitrate=%s/>
"""
smil_footer = """\
    </switch>
  </body>
</smil>
"""

src_format = 'mp4:soundcheck/%(year)s/%(id)s/%(file_root_name)s_%(suffix)s.mp4'

video_data = (
    ('256', '336000'), ('512', '592000'),
    ('768', '848000'), ('1128', '1208000'),
    )

root = os.getcwd()
if len(sys.argv) > 2:
    root = sys.argv[2]

with open(sys.argv[1], 'rU') as stream:

    for row in csv.DictReader(stream):
        smil = [smil_header]
        for suffix, bitrate in video_data:
            row['suffix'] = suffix
            smil.append(smil_video % (
                quoteattr(src_format) % row, quoteattr(bitrate)
                ))
        smil.append(smil_footer)

        directory = os.path.join(root, '%(year)s-%(id)s' % row)
        try:
            os.makedirs(directory)
        except OSError:
            pass
        path = os.path.join(directory, '%(file_root_name)s.smil' % row)
        print ':: writing file:', path
        with open(path, 'wb') as stream:
            stream.write(''.join(smil))

rawDescription: Node.toprettyxml() adds newlines to DOCTYPE in Python


code:
import numpy as np
import cv2, os.path

allImageIDs = []
allImages = []
countImagesMax = 20
countImages = 0
while countImages < countImagesMax:
  image = np.uint8(np.random.randint(2, size=(144,192)) *255)
  allImages.append(np.reshape(image, (image.shape[0]*image.shape[1])))
  allImageIDs.append(countImages)
  countImages += 1

myArray = [np.array(allImages), np.array(allImageIDs)]

if not os.path.exists("savedFile.npz"):
  np.savez('savedFile.npz', images=myArray[0], ids=myArray[1])
  print  myArray[0].shape, myArray[0].dtype # returns (yyy, xxx) uint8
else:
  npzfile = np.load('savedFile.npz')
  myArray = [npzfile['images'], npzfile['ids']]
  print  myArray[0].shape, myArray[0].dtype # returns (yyy, xxx) uint8

rawDescription: Python Numpy: Saved array is loaded differently


code:
for key, value in dictionary.items():
    if 4 == value:
        print key

rawDescription: How can you print a key given a value in a dictionaty for Python?

description: Print a key given a value in a dictionaty for Python


code:
import time
from threading import Thread

def stupid_work():
    time.sleep(4)

if __name__ == '__main__':
    t = Thread(target=stupid_work)
    t.start()
    while 1:
        if not t.is_alive():
            print 'thread is done'
            break # or whatever
        else:
            print 'thread is working'

        time.sleep(1)

rawDescription: Listen for thread while executing task

description: Listen for thread while executing task


code:
import operator
end = 1000000
N = (1, 10, 100, 1000, 10000, 100000, 1000000)
concatenated_numbers_str = ''.join([str(x) for x in range(1, end + 1)])
print reduce(operator.mul, [int(concatenated_numbers_str[n-1]) for n in N], 1)

rawDescription: The Pythonic way to do this? (multiply certain elements in string)


code:
import numpy as np

def avg_positive_speed(speed):
    s = np.array(speed)
    positives = s > 0
    if positives.any():
        return s[positives].mean()
    else:
        return 0.

speed = [1., 2., 0., 3.]
print avg_positive_speed(speed)
# prints 2.0

print avg_positive_speed([0., 0.])
# prints 0.0

rawDescription: Numpy mean with condition

description: Numpy mean with condition


code:
# Python 2.
# Change xrange to range for Python 3.

import time

start_time = time.time()

limit = 100000000  # 1/10 your limit.
skip  = 1000       # Batch size.

with open('numlistbegin', 'w') as fh:
    for i in xrange(0, limit, skip):
        batch = ''.join('%010d\n' % j for j in xrange(i, i + skip, 1))
        fh.write(batch)

print time.time() - start_time   # 73 sec. (106 sec. without batching).

rawDescription: Writing numbers from 0 to 1000000000

description: Writing numbers from 0 to 1000000000


code:
cursor = arcpy.SearchCursor(my_table)
for _ in cursor:
    count += 1
print count

rawDescription: local variable value is not used - python

description: Local variable value is not used - python


code:
import threading

_f_got_killed = threading.Event()

def f():
    while(True):
        print "hello"
        _f_got_killed.wait(5)
        if _f_got_killed.is_set():
            break

def execute():
    t = threading.Timer(5,f)
    t.start()
    command = ''
    while command != 'exit':
        command = raw_input()
        if command == 'exit':
            _f_got_killed.set()
            t.cancel()

execute()

rawDescription: python thread weird behavior

description: Python thread weird behavior


code:
with open('yourFile') as f:
    lines = f.read().splitlines()

for l in lines:
    if l.startswith('2012-10-23 14'):
        p=1
    elif l.startswith('2012-10-23 16'):
        p=0
        break
    if p: print l

rawDescription: how to filter out lines between two timestamps in python

description: Filter out lines between two timestamps in python


code:
import os

cmd='ps -efww | grep java | grep -v grep'
p = os.popen(cmd)
print p.read()

rawDescription: python executes linux shell, returns only one line

description: Python executes linux shell, returns only one line


code:
from PIL import Image
img = Image.open("filename.png")
pixels = img.load()

#get the B value of the pixel at x=23, y=42
print pixels[23, 42][2]

rawDescription: Analyzing pixel RGB values with PIL

description: Analyzing pixel RGB values with PIL


code:
from BeautifulSoup import BeautifulSoup
html = """ <what you have above> """
Soup = BeautifulSoup(html)
bullets = Soup.findAll('div', {'class': 'productDescriptionWrapper'})
ptags = bullets[0].findAll('p')
print ptags
print ptags[0].text

rawDescription: python finding index of tag in string

description: Python finding index of tag in string


code: print result[0]

rawDescription: How do I get regex to only print the first of an array of information found?

description: Get regex to only print the first of an array of information found


code:
#! /usr/bin/env python

import dbus
import dbus.mainloop.glib
import functools
import glib

class VlcFinder (object):
    def __init__ (self, mainloop):
        self.outstanding = 0
        self.mainloop = mainloop

        bus = dbus.SessionBus ()
        dbus_proxy = bus.get_object ("org.freedesktop.DBus", "/org/freedesktop/DBus")
        names = dbus_proxy.ListNames ()
        for name in dbus_proxy.ListNames ():
            if name.startswith (":"):
                proxy = bus.get_object (name, "/")
                iface = dbus.Interface (proxy, "org.freedesktop.MediaPlayer")
                iface.Identity (reply_handler = functools.partial (self.reply_cb, name),
                                error_handler = functools.partial (self.error_cb, name))
                self.outstanding += 1

    def reply_cb (self, name, ver):
        print "Found {0}: {1}".format (name, ver)
        self.received_result ()

    def error_cb (self, name, msg):
        self.received_result ()

    def received_result (self):
        self.outstanding -= 1
        if self.outstanding == 0:
            self.mainloop.quit ()

if __name__ == "__main__":
    dbus.mainloop.glib.DBusGMainLoop (set_as_default = True)
    mainloop = glib.MainLoop ()
    finder = VlcFinder (mainloop)
    mainloop.run ()

rawDescription: How do I get all instances of VLC on dbus quickly?

description: Get all instances of VLC on dbus quickly


code:
from contextlib import contextmanager
import fcntl

@contextmanager
def file_locked(filename, exclusive=True):
    lockfile = open(filename, 'w')
    if exclusive:
        fcntl.flock(lockfile, fcntl.LOCK_EX)
    else:
        fcntl.flock(lockfile, fcntl.LOCK_SH)
    yield
    fcntl.flock(lockfile, fcntl.LOCK_UN)

if __name__ == '__main__':
    from time import sleep
    from random import random
    from os import getpid
    while True:
        print '%d waiting for lock' % getpid()
        with file_locked('filelock.lck'):
            print '%d got the lock'% getpid()
            sleep(random())
        print '%d released the lock\n' % getpid()

rawDescription: How can I control access to a shared resource when using mod_wsgi?

description: Control access to a shared resource when using mod_wsgi


code:
import time

data = """12425234123|2011-03-19 11:03:44|words words words words
12425234123|2011-03-19 11:04:27|words words words words
12425234123|2011-03-19 11:05:04|words words words words
12482904328|2011-03-19 11:13:31|words words words words
--
12482904328|2011-03-19 15:50:48|More bolder than flow
More cumbersome than pleasure;
Goodbye rocky dump """.splitlines()

def get_haikus(lines):
    haiku = None
    for line in lines:
        try:
            ID, timestamp, txt = line.split('|')
            t = time.strptime(timestamp, "%Y-%m-%d %H:%M:%S")
            ID = int(ID)
            if haiku and len(haiku[1]) ==3:
                yield haiku
            haiku = (timestamp, [txt])
        except ValueError: # happens on error with split(), time or int conversion
            haiku[1].append(line)
    else:
        yield haiku

# now get_haikus() returns tuple (timestamp, [lines])
for haiku in get_haikus(data):
    timestamp, text = haiku
    date = timestamp.split()[0]
    text = '\n'.join(text)
    print """{d}\n{txt}""".format(d=date, txt=text)

rawDescription: Determining a pattern of lines in Python


code:
class SliceMaker(object):
  def __getitem__(self, item):
    return item

make_slice = SliceMaker()

print make_slice[3]
print make_slice[0:]
print make_slice[:-1]
print make_slice[1:10:2,...]

rawDescription: Passing Python slice syntax around to functions


code:
from bs4 import BeautifulSoup
import requests

content = requests.get("TOPShop_URL").content
soup = BeautifulSoup(content)
product_names = soup.findAll("h1")
print product_names

rawDescription: Scraping Product Names using BeautifulSoup


code:
files = ['csv1', 'csv2', 'csv3']
fs = map(open, files)

done = False

while not done:
    chunks = []
    for f in fs:
        try:
            l = next(f).strip()
            chunks.append(l)
        except StopIteration:
            done = True
            break
    if not done:
        print ','.join(chunks)

for f in fs:
    f.close()

rawDescription: Python: `paste' multiple (unknown) csvs together


code:
DATA = { u'spam': u'eggs', u'foo': frozenset([u'Gah!']), u'bar': { u'baz': 97 },
         u'list': [u'list', (True, u'Maybe'), set([u'and', u'a', u'set', 1])]}

def convert(data):
    if isinstance(data, basestring):
        return str(data)
    elif isinstance(data, collections.Mapping):
        return dict(map(convert, data.iteritems()))
    elif isinstance(data, collections.Iterable):
        return type(data)(map(convert, data))
    else:
        return data

print DATA
print convert(DATA)
# Prints:
# {u'list': [u'list', (True, u'Maybe'), set([u'and', u'a', u'set', 1])], u'foo': frozenset([u'Gah!']), u'bar': {u'baz': 97}, u'spam': u'eggs'}
# {'bar': {'baz': 97}, 'foo': frozenset(['Gah!']), 'list': ['list', (True, 'Maybe'), set(['and', 'a', 'set', 1])], 'spam': 'eggs'}

rawDescription: Fastest way to convert a dict's keys & values from `unicode` to `str`?


code:
from ctypes import CDLL, c_char_p, c_int, c_long, c_ushort, c_byte, c_char, Structure, POINTER, byref, cast, sizeof, get_errno
from ctypes.util import find_library

class c_dir(Structure):
    """Opaque type for directory entries, corresponds to struct DIR"""
    pass

class c_dirent(Structure):
    """Directory entry"""
    # FIXME not sure these are the exactly correct types!
    _fields_ = (
        ('d_ino', c_long), # inode number
        ('d_off', c_long), # offset to the next dirent
        ('d_reclen', c_ushort), # length of this record
        ('d_type', c_byte), # type of file; not supported by all file system types
        ('d_name', c_char * 4096) # filename
        )
c_dirent_p = POINTER(c_dirent)
c_dirent_pp = POINTER(c_dirent_p)
c_dir_p = POINTER(c_dir)

c_lib = CDLL(find_library("c"))
opendir = c_lib.opendir
opendir.argtypes = [c_char_p]
opendir.restype = c_dir_p

readdir_r = c_lib.readdir_r
readdir_r.argtypes = [c_dir_p, c_dirent_p, c_dirent_pp]
readdir_r.restype = c_int

closedir = c_lib.closedir
closedir.argtypes = [c_dir_p]
closedir.restype = c_int

import errno

def listdirx(path):
    """
    A generator to return the names of files in the directory passed in
    """
    dir_p = opendir(path)

    if not dir_p:
        raise IOError()

    entry_p = cast(c_lib.malloc(sizeof(c_dirent)), c_dirent_p)

    try:
        while True:
            res = readdir_r(dir_p, entry_p, byref(entry_p))
            if res:
                raise IOError()
            if not entry_p:
                break
            name = entry_p.contents.d_name
            if name not in (".", ".."):
                yield name
    finally:
        if dir_p:
            closedir(dir_p)
        if entry_p:
            c_lib.free(entry_p)

if __name__ == '__main__':
    import sys
    path = sys.argv[1]
    max_per_dir = int(sys.argv[2])
    for idx, entry in enumerate(listdirx(path)):
        if idx >= max_per_dir:
            break
        print entry

rawDescription: Python Walk, but Tread Lightly

description: Python Walk, but Tread Lightly


code:
import os
if any(x.startswith('runner') for x in os.listdir('/path/to/runners')):
    print "At least one entry begins with 'runner'"

rawDescription: checking if there is a folder with a name that start with a specific string

description: Checking if there is a folder with a name that start with a specific string


code:
from urllib import urlopen
from BeautifulSoup import BeautifulSoup

handle = urlopen("http://jobsearch.monster.com/search/Engineer_5?q=Software&where=AZ&rad=20&sort=rv.di.dt")
responce = handle.read()
soup = BeautifulSoup( responce )

job_urls = soup.findAll(name = 'a', attrs = { 'class': 'jobTitle fnt11_js' })
for job_url in job_urls:
    print job_url.text
    print

rawDescription: Python, scraping a website for object

description: Python, scraping a website for object


code:
from random import random

# Generate data and keep record of scores
data = []
a_list = []
b_list = []
for i in range(50):
    a = int(random()*500)+1
    b = int(random()*500)+1
    rec = { 's' : 's%s' % i,
            'a' : a,
            'b' : b
             }
    a_list.append(a)
    b_list.append(b)
    data.append(rec)

# divide A and B ranges into five bins

def make_bins(f_list):
    f_min = min(f_list)
    f_max = max(f_list)
    f_step_size = (f_max - f_min) / 5.0
    f_steps = [ (f_min + i * f_step_size,
                 f_min + (i+1) * f_step_size)
                for i in range(5) ]
    # adjust top bin to be just larger than maximum
    top = f_steps[4]
    f_steps[4] = ( top[0], f_max+1 )
    return f_steps

a_steps = make_bins(a_list)
b_steps = make_bins(b_list)

# collect the strings that fit into any of the bins
# thus all the strings in cell[4,3] of your matrix
# would fit these conditions:
# string would have a Score A that is
# greater than or equal to the first element in a_steps[3]
# AND less than the second element in a_steps[3]
# AND it would have a Score B that is
# greater than or equal to the first element in b_steps[2]
# AND less than the second element in a_steps[2]
# NOTE: there is a need to adjust the pointers due to
#       the way you have numbered the cells of your matrix

def query_matrix(ptr_a, ptr_b):
    ptr_a -= 1
    from_a = a_steps[ptr_a][0]
    to_a = a_steps[ptr_a][1]

    ptr_b -= 1
    from_b = b_steps[ptr_b][0]
    to_b = b_steps[ptr_b][1]

    results = []
    for rec in data:
        s = rec['s']
        a = rec['a']
        b = rec['b']
        if (a >= from_a and
            a < to_a and
            b >= from_b and
            b < to_b):
            results.append(s)
    return results

# Print out the results for a visual check
total = 0
for i in range(5):
    for j in range(5):
        print '=' * 80
        print 'Cell: ', i+1, j+1, ' contains: ',
        hits = query_matrix(i+1,j+1)
        total += len(hits)
        print hits
print '=' * 80
print 'Total number of strings found: ', total

rawDescription: Represent a table as a matrix

description: Represent a table as a matrix


code:
mylist = [["a",1],["c",1],["a",1]]
newlist = [["null",0]]

mydict = {}
for i in mylist+newlist:
    if i[0] in mydict:
        mydict[i[0]]+=i[1]
    elif i[0] != 'null':
        mydict[i[0]]=i[1]

print [[x,mydict[x]] for x in mydict]

[['a', 2], ['c', 1]]

rawDescription: Iterating Over List, Combining Quantities


code:
try:
   s1.connect((host1, port1))
   s1.send(data)
except:
   print " s1 not connected"
try:
   s2.connect((host2,port2))
   s2.send(data)
except:
   print"s2 not connected"
s1.close()
s2.close()

rawDescription: how to exit from the server connection if not connected

description: Exit from the server connection if not connected


code:
with open('newfile.txt','w') as fileinput:
    for start, end in searchPFAM(fname):
        print start, end
        if start <= end:
            while start <= end:
                result = makeList[start]
                fileinput.write(result)
                start += 1

rawDescription: data in my outputfile didn't print all result it should have

description: Data in my outputfile didn't print all result it should have


code:
def parse(self,response):
    pattern = re.compile(r"var locations= ({.*?});", re.MULTILINE | re.DOTALL)
    locations = response.xpath('//script[contains(., "var locations")]/text()').re(pattern)[0]
    locations = json.loads(locations)
    for title, data in locations.iteritems():
        print title

rawDescription: How to use re() to extract data from javascript variable using scrapy?

description: Use re() to extract data from javascript variable using scrapy


code:
import wx
import time
from threading import Thread

def print_something_forever(something):
    while True:
        print something
        time.sleep(1)

class Frame(wx.Frame):
    def __init__(self,parent):
        wx.Frame.__init__(self,parent)
        self.panel= wx.Panel(self)
        t= Thread(target=print_something_forever,args=("Thread alive!",))
        t.daemon= True
        t.start()
        self.Show()

if __name__ == "__main__":
    app= wx.App(False)
    Frame(None)
    app.MainLoop()

rawDescription: How to end threads when closing window in wxpy

description: End threads when closing window in wxpy


code:
s = """prefixA_REPLACEME_postfixA
prefixB_SOMETHING_postfixB
prefixA_LLAMAS_postfixC
prefixB_DONTREPLACE_postfixA"""
import re
print re.sub("(?<=prefixA).*(?=postfixA)|(?<=prefixB).*(?=postfixB)|(?<=prefixA).*(?=postfixC)", "_NEWSTR_", s)

rawDescription: Python Regex - Is it possible to use the same group (named or unnamed) in multiple spots?


code:
import HTMLParser
pars = HTMLParser.HTMLParser()
pars.unescape('© €')
print _

rawDescription: Replace html entities with the corresponding utf-8 characters in Python 2.6


code:
import os
import tempfile

#define the location of 'mytemp' parent folder relative to the system temp
sysTemp = tempfile.gettempdir()
myTemp = os.path.join(sysTemp,'mytemp')

#You must make sure myTemp exists
if not os.path.exists(myTemp):
    os.makedirs(myTemp)

#now make your temporary sub folder
tempdir = tempfile.mkdtemp(suffix='foo',prefix='bar',dir=myTemp)

print tempdir

rawDescription: Using tempfile to create a sub-directory for all of my tempfiles


code:
import tornado.ioloop, tornado.web

class Handler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

application = tornado.web.Application([ (r"/", Handler) ])

def startTornado():
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

def stopTornado():
    tornado.ioloop.IOLoop.instance().stop()

if __name__ == "__main__":
    import time, threading
    threading.Thread(target=startTornado).start()
    print "Your web server will self destruct in 2 minutes"
    time.sleep(120)
    stopTornado()

rawDescription: How do I stop Tornado web server?

description: Stop Tornado web server


code:
import zipfile,os;
def unzip(source_filename, dest_dir):
    with zipfile.ZipFile(source_filename) as zf:
        for member in zf.infolist():
            extract_allowed = True;
            path = dest_dir;
            words = member.filename.split('/');
            for word in words:
                if (word == '..'):
                    extract_allowed = False;
                    break;
            if (extract_allowed == True):
                zf.extract(member, dest_dir);
def unzipFiles(dest_dir):
    for file in os.listdir(dest_dir):
        if (os.path.isdir(dest_dir + '/' + file)):
            return unzipFiles(dest_dir + '/' + file);
        if file.endswith(".zip"):
            print 'Found file: "' + file + '" in "' + dest_dir + '" - extracting';
            unzip(dest_dir + '/' + file, dest_dir + '/');
unzipFiles('./MyFolder');

rawDescription: How to decompress zip files across a Windows folder in Python

description: Decompress zip files across a Windows folder in Python


code:
import ast
a = input("Please enter a dictionary: ")
d = ast.literal_eval(a)
print d

rawDescription: Python-How to take a dictionary as input


code:
from collections import namedtuple
import csv

location_info = namedtuple('location_info', 'LCID,CID,LAC,NET,LAT,LON')
input_filename = "input.csv"

def write_header(output_file):
    pass  # left as an excercise

def write_footer(output_file):
    pass  # also left as an exercise

def get_kml(location_info):
    return """    <Placemark>
        <ExtendedData>
            <Data name='CID'>
                <value>
                    {CID}
                </value>
            </Data>
            <Data name='LAC'>
                <value>
                    {LAC}
                </value>
            </Data>
        </ExtendedData>
        <Point>
            <coordinates>{LON},{LAT}</coordinates>
        </Point>
    </Placemark>""".format(**location_info._asdict())

with open(input_filename, 'r') as input_file:
    csv_reader = csv.reader(input_file, delimiter=';')
    print next(csv_reader)  # gets rid of the header line
    all_locations = (location_info(*line[0:6]) for line in csv_reader)  # the slicing is due to the trailing ;

    with open(output_filename, 'w') as output_file:
        write_header(output_file)
        for location in all_locations:
            output_file.write(get_kml(location))
        write_footer(output_file)

rawDescription: Building a KML in Python

description: Building a KML in Python


code:
def cp(target, given):
    'Given is a one or two tuple and target is the letter following'
    g = 0.0
    g_and_t = 0.0
    n = len(given)
    for case, count in model.iteritems():
        if case[:n] == given:
            g += count
            if case[n] == target:
                g_and_t += count
    return g_and_t / g if g else 0.0

print cp(target='r', given=('f', 'o'))

rawDescription: Conditional Probability Calculation

description: Conditional Probability Calculation


code:
h = '''

...

'''
from scrapy.selector import Selector
import re
def normalize(xs):
    text = ''.join(xs)
    text = text.strip()
    return re.sub(r'[\s\xa0]+', ' ', text)
root = Selector(text=h, type='html')
print [normalize(x.xpath('.//text()').extract()) for x in root.xpath('.//td')]

rawDescription: Scrape text from a node with multiple descendants with Scrapy


code:
with open('trash.txt', 'r') as fin:
    for line in sorted(fin, key=lambda x:x.split(',')[1]):
        print line

rawDescription: Sort tuple by last name

description: Sort tuple by last name


code:
from itertools import izip, product

def find_continuous_items(data):
    for p in product(*data):
        if all(b-a==1 for a, b in izip(p, p[1:])):
            yield p

nested = [[1,7,13], [2,5,8], [3, 6, 9]]
print list(find_continuous_items(nested))
#[(1, 2, 3), (7, 8, 9)]

rawDescription: Iterate list of lists and finding consecutive numbers in those lists


code:
def handler(sender=None):
    print "got signal from %r" % sender

iface.connect_to_signal("Hello", handler, sender_keyword='sender')

rawDescription: Python-dbus extra param to add_signal_receiver


code:
#ID      Postal Code
data = [
    ('1001', 'MK18 1TN'),
    ('1001', 'MK18 1TL'),
    ('1002', 'HP17 6DG'),
    ('1002', 'HP17 6DH'),
    ]
d = dict()
for id,pc in data:
    if id not in d:
        d[id] = []
    d[id].append(pc)
idkeys = d.keys()
idkeys.sort()
for k in idkeys:
    print k, ', '.join(d[k])

rawDescription: Copy values from CSV rows and adding to rows where the ID is the same


code:
from simple_currency_helper import get_currency_subunit
print get_currency_subunit('TND')

rawDescription: get the smallest unit of a currency in python

description: Get the smallest unit of a currency in python


code:
MAX = max(val.get('max_width', 0) for val in breakpoints.itervalues()) + 1
# sort by min/max
items = sorted(
    (data['min_width'] or 0, data['max_width'] or MAX, name)
    for name, data in breakpoints.iteritems())
# check if any range overlaps the next higher item
for i in range(len(items)-1):
    if items[i][0] > items[i][1]:
        print "range is incorrect for", items[i][1]
    elif items[i][1] >= items[i+1][0]:
        print items[i+1][2], 'overlaps'

rawDescription: Better way to check config to make sure each rules are unique


code:
import os

def show_progress(file_name, chunk_size=1024):
    fh = open(file_name, "r")
    total_size = os.path.getsize(file_name)
    total_read = 0
    while True:
        chunk = fh.read(chunk_size)
        if not chunk:
            fh.close()
            break
        total_read += len(chunk)
        print "Progress: %s percent" % (total_read/total_size)
        yield chunk

for chunk in show_progress("my_file.txt"):
    # Process the chunk
    pass

rawDescription: Tracking file load progress in Python

description: Tracking file load progress in Python


code:
import datetime
s = '04/21/2013'
d = datetime.datetime.strptime(s, '%m/%d/%Y') + datetime.timedelta(days=1)
print d.strftime('%m/%d/%Y')

rawDescription: How to perform arithmetic operation on a date in Python?

description: Perform arithmetic operation on a date in Python


code:
import smtplib
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders

filePath = "fileName.pdf"

From = 'abc@gmail.com'
To = 'xyz@gmail.com'

msg = MIMEMultipart()
msg['From'] = From
msg['To'] = To
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = 'Sample subject'

msg.attach(MIMEText('Sample message'))

try:
    smtp = smtplib.SMTP('smtp.gmail.com:587')
    smtp.starttls()
    smtp.login('abc@gmail.com', '123456')
except:
    i = 1
else:
    i = 0

if i == 0:
    ctype, encoding = mimetypes.guess_type(filePath)
    if ctype is None or encoding is not None:
        # No guess could be made, or the file is encoded (compressed), so
        # use a generic bag-of-bits type.
        ctype = 'application/octet-stream'
    maintype, subtype = ctype.split('/', 1)
    if maintype == 'text':
        fp = open(filePath)
        # Note: we should handle calculating the charset
        part = MIMEText(fp.read(), _subtype=subtype)
        fp.close()
    elif maintype == 'image':
        fp = open(filePath, 'rb')
        part = MIMEImage(fp.read(), _subtype=subtype)
        fp.close()
    elif maintype == 'audio':
        fp = open(filePath, 'rb')
        part = MIMEAudio(fp.read(), _subtype=subtype)
        fp.close()
    else:
        fp = open(filePath, 'rb')
        part = MIMEBase(maintype, subtype)
        part.set_payload(fp.read())
        fp.close()
        # Encode the payload using Base64
        Encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % filePath)
    msg.attach(part)
    try:
        smtp.sendmail(From, To, msg.as_string())
    except:
        print "Mail not sent"
    else:
        print "Mail sent"
    smtp.close()
else:
    print "Connection failed"

rawDescription: Sending email with Python and Gmail SMTP server doesn't work with attachment


code:
from BeautifulSoup import BeautifulSoup, SoupStrainer
import re
import urllib2

doc = urllib2.urlopen("http://somesite.com").read()
links = SoupStrainer('a', href=re.compile(r'^test'))
soup = [str(elm) for elm in BeautifulSoup(doc, parseOnlyThese=links)]
for elm in soup:
    print elm

rawDescription: How to write a python script to search a website html for matching links

description: Write a python script to search a website html for matching links


code:
if __name__ == "__main__":
  from  optparse import OptionParser

  parser = OptionParser()
  parser.add_option("-g","--gui_mode",
                    dest="guimode",
                    help="start program in gui mode",
                   action="store_true")

  (options,args) = parser.parse_args()

  if (options.guimode):
      print "start wx app"
  else:
      print "start cmd line app"

rawDescription: Application with both console and gui mode


code:
class StratifiedKFold_ByColumn( object ):
    def __init__( self, n_folds, X, y, colname ):
        groupable = pd.concat( [X[colname], y], axis=1 )
        grouped = groupable.groupby( [colname] ).aggregate( max )
        self.column = X[colname]
        self.colname = colname

        # import pdb; pdb.set_trace()

        self.folds = [
            (train,val) for (train,val) in
            sklearn.cross_validation.StratifiedKFold( y=grouped.values[:,0], n_folds=n_folds, shuffle=True )
            ]
        self.n_folds = n_folds
        self.i = 0
        self.y=y

        # self.test()

    def __len__(self):
        return self.n_folds
    def __iter__( self ):
        self.i = 0
        return self

    def test( self ):
        for train,val in self.folds:
            train_mask = self.column.isin( train )
            val_mask = self.column.isin( val )
            print 'train:',self.y[train_mask].sum(), (1-self.y[train_mask]).sum()
            print 'val:',self.y[val_mask].sum(), (1-self.y[val_mask]).sum()

    def next( self ):
        if self.i < self.n_folds:
            train,val = self.folds[self.i]
            self.i += 1

            # import pdb; pdb.set_trace()
            train_mask = self.column.isin( train )
            val_mask = self.column.isin( val )

            y_train = self.y[train_mask]
            X_train = self.column[train_mask]

            n_tr_1 = (y_train!=0).sum()
            n_tr_0 = (y_train==0).sum()
            # import pdb; pdb.set_trace()
            assert n_tr_1 < n_tr_0
            stride = n_tr_0/n_tr_1

            X_train_1 = X_train[y_train!=0]
            y_train_1 = y_train[y_train!=0]
            X_train_0 = X_train[y_train==0]
            y_train_0 = y_train[y_train==0]

            train_idxs = []
            for i_1 in range(0,n_tr_1):
                train_idxs.append( X_train_1[i_1:(i_1+1)].index )
                train_idxs.append( X_train_0[i_1*stride:(i_1+1)*stride].index )
            train_idxs = flatten(train_idxs)

            val_idxs = val_mask[val_mask].index

            return np.array(train_idxs), np.array(val_idxs)
        else:
            raise StopIteration()

rawDescription: Groupped sampling in scikit-learn

description: Groupped sampling in scikit-learn


code:
t1=linspace(-50,50,100)
sig1=sin(t1/2)+np.random.normal(scale=0.1,size=len(t1))
sig2=sin(t1/2)+np.random.normal(scale=0.1,size=len(t1))

f0=interp1d(t1,sig1,kind="cubic",bounds_error=False,fill_value=-10000)
g=interp1d(t1,sig2,kind="cubic",bounds_error=False,fill_value=10000)
#true value that  I would like to estimate
A=2
B=0.8
C=0

def s(t):
    return A+B*t+C*t*t

def inv_s(t):
    return (t-B)/A

def f(t):
    return f0(s(t))

def hat_s(t,a,b):
    return t*b+a

Ig=arange(t1.min(),t1.max(),1)
If=(Ig-A)/B
I_min=max(If.min(),Ig.min())
I_max=min(If.max(),Ig.max())

J=linspace(min(If.min(),Ig.min()),max(If.max(),Ig.max()))
I=linspace(I_min,I_max,1000)

#plot(J,f(J),J,g(J))
#ylim(-1.2,1.2)

def cost(x,T=I):
    a=x[0]
    b=x[1]
    return norm(g(b*T+a)-f(T))/len(T),

import operator
import random

import numpy

from deap import base
from deap import benchmarks
from deap import creator
from deap import tools

creator.create("FitnessMax", base.Fitness, weights=(-1.0,))
creator.create("Particle", list, fitness=creator.FitnessMax, speed=list,
    smin=None, smax=None, best=None)

def generate(size, pmin, pmax, smin, smax):
    part = creator.Particle(random.uniform(pmin, pmax) for _ in range(size))
    part.speed = [random.uniform(smin, smax) for _ in range(size)]
    part.smin = smin
    part.smax = smax
    return part

def updateParticle(part, best, phi1, phi2):
    u1 = (random.uniform(0, phi1) for _ in range(len(part)))
    u2 = (random.uniform(0, phi2) for _ in range(len(part)))
    v_u1 = map(operator.mul, u1, map(operator.sub, part.best, part))
    v_u2 = map(operator.mul, u2, map(operator.sub, best, part))
    part.speed = list(map(operator.add, part.speed, map(operator.add, v_u1, v_u2)))
    for i, speed in enumerate(part.speed):
        if speed < part.smin:
            part.speed[i] = part.smin
        elif speed > part.smax:
            part.speed[i] = part.smax
    part[:] = list(map(operator.add, part, part.speed))

toolbox = base.Toolbox()
toolbox.register("particle", generate, size=2, pmin=-6, pmax=6, smin=-3, smax=3)
toolbox.register("population", tools.initRepeat, list, toolbox.particle)
toolbox.register("update", updateParticle, phi1=2.0, phi2=2.0)
toolbox.register("evaluate", cost)

def main():
    pop = toolbox.population(n=5)
    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", numpy.mean)
    stats.register("std", numpy.std)
    stats.register("min", numpy.min)
    stats.register("max", numpy.max)

    logbook = tools.Logbook()
    logbook.header = ["gen", "evals"] + stats.fields

    GEN = 1000
    best = None

    for g in range(GEN):
        for part in pop:
            part.fitness.values = toolbox.evaluate(part)
            if not part.best or part.best.fitness < part.fitness:
                part.best = creator.Particle(part)
                part.best.fitness.values = part.fitness.values
            if not best or best.fitness < part.fitness:
                best = creator.Particle(part)
                best.fitness.values = part.fitness.values
        for part in pop:
            toolbox.update(part, best)

        # Gather all the fitnesses in one list and print the stats
        logbook.record(gen=g, evals=len(pop), **stats.compile(pop))
        print(logbook.stream)
        print "  Best so far: %s - %s" % (best, best.fitness)

    return pop, logbook, best

pop,logbook,best= main()
print "best=",best,"A,B=",(A,B)

rawDescription: how to minimize a function using Deap?

description: Minimize a function using Deap


code:
part_list = ['ccs', 'fcex', '8-12', '8-15', '8-15b', '80ha3']
items = []
f = open("C:/Users/SilenX/Desktop/python/frys.txt", "r") # Open the file
for line in f:
    for token in line.lower().split(): # Loop over lowercase words in the line
        if token in part_list: # If it's one of the words you're looking for,
            items.append(token) # Append it to your list.
f.close()
print items

rawDescription: lower() and searching for a word in a file using a list

description: Lower() and searching for a word in a file using a list


code: print html2text.html2text(s)

rawDescription: How to get pure text from a string in python?

description: Get pure text from a string in python


code:
def removeNonPDFDirectories(dpath):
    '''Visit 'dpath', removing any subdirectory not containing any PDF
       file. Return True if 'dpath' is removed.
    '''
    import os
    if os.path.isdir(dpath):
        print 'Entering', dpath
        entries = [os.path.join(dpath, entry) for entry in os.listdir(dpath)]
        subdirs = filter(os.path.isdir, entries)
        print '    Subdirectories:', subdirs
        if all(map(removeNonPDFDirectories, subdirs)):
            print '    All subdirectories were removed.'
            files = filter(os.path.isfile, entries)
            pdf_files = [f for f in files if f.endswith('.pdf')]
            print '    PDF files:', pdf_files
            if not pdf_files:
                try:
                    for f in files:
                        os.unlink(f)
                        print '    Removed file', f
                    os.rmdir(dpath)
                    print '    Removed directory', dpath
                except OSError as e:
                    # An error occurred: assume directory is not empty.
                    print '    ERROR:', e
                    print '    Keeping directory', dpath
                    return False
                # Directory was removed: report to caller.
                return True
        # Directory must be kept: report to caller.
        print '    Keeping directory', dpath
        return False
    else:
        return False

rawDescription: In Python, how do I remove subdirectories that do NOT contain a certain file extension (ie .pdf)?


code:
from decimal import Decimal

r1 = Decimal("5.57") * Decimal("2.92")
r2 = Decimal("3.25") * Decimal("5")
epsilon = Decimal("0.01")

if abs(r1 - r2) <= epsilon:
    print "Almost equal!"

rawDescription: Check if sum of two amounts is equal to a certain total with some tolerance


code:
import pandas_datareader.data as web
import datetime
start = datetime.datetime.strptime('1/1/2015', '%m/%d/%Y')
end = datetime.datetime.strptime('2/20/2016', '%m/%d/%Y')
f = web.DataReader(['GOOG','AAPL'], 'yahoo', start, end)
print 'Adjusted Closing Prices'
print f['Adj Close'].describe()
ax = f['Adj Close'].plot(grid=True, fontsize=10, rot=45.)
ax.set_ylabel('Adjusted Closing Price ($)')
plt.legend(loc='upper center', ncol=2, bbox_to_anchor=(0.5,1.1), shadow=True, fancybox=True, prop={'size':10})
plt.show()

rawDescription: how to feed stock market data into python

description: Feed stock market data into python


code:
def spam():
    baz()

def foo():
    def bar():
        print "Hello world!"
    return bar

baz = foo()
spam()

rawDescription: Functions: Where can/must they be declared?


code:
import locale

locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

def addAndReverseSign(number, grouping=True):
    sign, number = locale.currency(number, grouping).replace('$', 'ISK ').split(' ')
    print number, sign

addAndReverseSign(123)

rawDescription: python locale set currency at end of string

description: Python locale set currency at end of string


code:
isstatement= False
try:
    code= compile(s, '<stdin>', 'eval')
except SyntaxError:
    isstatement= True
    code= compile(s, '<stdin>', 'exec')

result= None
if isstatement:
    exec s
else:
    result= eval(s)

if result is not None:
    print result

rawDescription: Python: how to tell if a string represent a statement or an expression?


code:
import xlrd
workbook = xlrd.open_workbook('book.xls')
for sheet in workbook.sheets():
    for row in range(sheet.nrows):
        for column in range(sheet.ncols):
            print "row::::: ", row
            print "column:: ", column
            print "value::: ", sheet.cell(row,column).value

rawDescription: How to get line number in excel sheet using python?

description: Get line number in excel sheet using python


code:
import urlparse
urls = [
    'https://www.google.com',
    'http://stackoverflow.com',
    'http://www.google.co.in',
    'http://domain.com',
    ]
hostnames = [urlparse.urlparse(url).hostname for url in urls]
hostparts = [hostname.split('.') for hostname in hostnames]
domains = [p[0] == 'www' and p[1] or p[0] for p in hostparts]
print domains # ==> ['google', 'stackoverflow', 'google', 'domain']

rawDescription: Fetch a particular part of the url in python


code:
fig1, ax1 = plt.subplots(figsize=(15,10))
#ax1.set_title("Station: "+StaName+','+' Component: '+comp)
ax1.set_xlabel('Time')
ax1.set_ylim(-1, N+2)
ax1.axes.get_yaxis().set_visible(False)
ax1.grid(True)

for sta in filenames:
    N = len(filenames)
    SplitName = sta.split('.', 3)
    StaName =  SplitName[0]
    M = filenames.index(sta)
    comp = SplitName[2]
    print StaName

    f = os.path.join(dirname, sta)
    YYYYJJJ = np.loadtxt(f, dtype='float', unpack=True)
    xdates = [datetime.datetime.strptime(str(int(date)),'%Y%j')
              for date in YYYYJJJ]
    cons = len(YYYYJJJ)
    uno = np.ones((cons), dtype=np.int)
    uno = uno*M
    ax1.scatter(xdates,uno, c='b', alpha=1, marker=(5, 0), s=30)

plt.show()

rawDescription: Plotting scatter figures in for loop, generating just one figure: Python


code:
df = pd.DataFrame([[0,1.0,0],
                   [0,1.0,0],
                   [0,1.0,0]],
                   columns=['a', 'b', 'c'])

def f(x):
    x.values[0] += 1
    return x

df.apply(f, axis=0) #pass every column to f, this will change the original dataframe
print df

df.apply(f, axis=1) #pass every row to f, this will not change the original dataframe
print df

rawDescription: How to apply a function to a mixed type Pandas DataFrame in place?

description: Apply a function to a mixed type Pandas DataFrame in place


code:
global_vars = {}
local_vars = {}

execfile("myFile.txt", global_vars, local_vars)

# local_vars now contains the variables defined in the file.
print local_vars

rawDescription: Read python variables from a file

description: Read python variables from a file


code:
import re

otuid2clusteridlist = dict()
finallist = otuid2clusteridlist
clusterid2denoiseidlist = dict()
known_clusters = dict()

#first block, also = finallist we append all other blocks into.
for line in open('cluster_97.ucm', 'r'):
    lineArray = re.split('\s+',line)
    otuid = lineArray[0]
    clusterid = lineArray[3]
    if otuid in otuid2clusteridlist:
        otuid2clusteridlist[otuid].append(clusterid)
    else:
        otuid2clusteridlist[otuid] = list()
        otuid2clusteridlist[otuid].append(clusterid)

    # remeber the clusters
    known_clusters[clusterid] = otuid2clusteridlist[otuid]

#second block, higher tier needs to expand previous blocks hash
for line in open('denoise.ucm_test', 'r'):
    lineArray = re.split('\s+', line)
    clusterid = lineArray[4]
    denoiseid = lineArray[3]
    if clusterid in clusterid2denoiseidlist:
        clusterid2denoiseidlist[clusterid].append(denoiseid)
    else:
        clusterid2denoiseidlist[clusterid] = list()
        clusterid2denoiseidlist[clusterid].append(denoiseid)

    # match the cluster and update as needed
    matched_cluster = known_clusters.setdefault(clusterid, [])
    if denoiseid not in matched_cluster:
        matched_cluster.append(denoiseid)

#print/return function for testing (will convert to write out later)
for key in finallist:
    print "OTU:", key, "has", len(finallist[key]), "sequence(s) which", "=", finallist[key]

rawDescription: merging dictionarys using keys in one to scan values of another

description: Merging dictionarys using keys in one to scan values of another


code:
import re
print re.findall(r"\|[^|]*\|", text)

rawDescription: Appending various strings between two delimeters in one string. python


code:
from datetime import datetime, timedelta

now = datetime.now()
start_time = now.replace(hour=8, minute=20, second=0)
end_time = start_time + timedelta(minutes=550)

if start_time <= now <= end_time:
    print 'was out'

rawDescription: Most efficient way to check if time lies between start time and end time in Python


code:
def process_mail(mb):
    print "Processing: %s" % q
    if mb.email_box_type == 'pop3':
        if mb.email_box_ssl:
            if not mb.email_box_port: mb.email_box_port = 995
            server = poplib.POP3_SSL(mb.email_box_host, int(mb.email_box_port))
        else:
            if not mb.email_box_port: mb.email_box_port = 110
            server = poplib.POP3(mb.email_box_host, int(mb.email_box_port))
        server.getwelcome()
        server.user(mb.email_box_user)
        server.pass_(mb.email_box_pass)

        messagesInfo = server.list()[1]

        for msg in messagesInfo:
            msgNum = msg.split(" ")[0]
            msgSize = msg.split(" ")[1]
            full_message = "\n".join(server.retr(msgNum)[1])

            # Do something with the message

            server.dele(msgNum)
        server.quit()

    elif mb.email_box_type == 'imap':
        if mb.email_box_ssl:
            if not mb.email_box_port: mb.email_box_port = 993
            server = imaplib.IMAP4_SSL(mb.email_box_host, int(mb.email_box_port))
        else:
            if not mb.email_box_port: mb.email_box_port = 143
            server = imaplib.IMAP4(mb.email_box_host, int(mb.email_box_port))
        server.login(mb.email_box_user, mb.email_box_pass)
        server.select(mb.email_box_imap_folder)
        status, data = server.search(None, 'ALL')
        for num in data[0].split():
            status, data = server.fetch(num, '(RFC822)')
            full_message = data[0][1]

            # Do something with the message

            server.store(num, '+FLAGS', '\\Deleted')
        server.expunge()
        server.close()
        server.logout()

rawDescription: django to send AND receive email?

description: Django to send AND receive email


code:
import os

def set_exit_handler(func):
    import signal
    signal.signal(signal.SIGHUP, func) # on closing the terminal
    signal.signal(signal.SIGTERM, func) # on killing the process

    import atexit
    atexit.register(func) # on Ctrl-C, Ctrl-D and other proper exits

if __name__ == "__main__":
    def on_exit(a=None, b=None):
        print "exit handler triggered"
        os.system("gnome-session-quit --logout --no-prompt")
    set_exit_handler(on_exit)

    print "Enter password:"
    raw_input()
    # ... some verification/timeout code ...

rawDescription: pause system functionality until my python script is done

description: Pause system functionality until my python script is done


code:
class No:
    def __init__(self,a,b):
       self.val = a
       self.next = b
    def __str__(self):
       return "%s->%s"%(self.val,self.next)

def swapandReverse(lista):
   n2 = lista.next #2nd element
   n2.val,n2.next.val = n2.next.val,n2.val #swap 2,3
   n = lista #root node
   v = [] #hold our values
   while n.next:
      v.append(n.val) #add our value to list
      n = n.next #move to next node
   v.append(n.val) #append value of last node in the list
   while len(v): #as long as we have values left in list
      n.next = No(v.pop(-1),None) #set next to new node with our val
      n = n.next

lista = No(3,No(1,No(4,No(2,None))))
print lista
swapandReverse(lista)
print lista

rawDescription: Changing characters and reversing chained lists. (python)


code:
ticks = 197740964
seconds = ticks/100
datetime.timedelta(seconds=seconds)
print datetime.timedelta(seconds=seconds)

rawDescription: Python: convert snmp timeticks to seconds/days, etc


code:
class Class1(object):
    pass

class Class2(object):
    pass

def foo(cls):
    print cls

import random
classes = {'Class1': Class1, 'Class2': Class2}
dynamic_class = classes['Class%d' % random.randint(1, 2)]
foo(dynamic_class())

rawDescription: How to call function iside a module dynamically?

description: Call function iside a module dynamically


code:
for index, value in enumerate(first_list):
    print second_list[index]

rawDescription: How can I loop through an array in an ordered way getting the index?

description: Loop through an array in an ordered way getting the index


code:
import numpy as np
a = np.random.random_integers(0, 9, (3, 4))
a.shape
print a
a.ravel()
list(a.ravel())

rawDescription: Merging ND array into single array and list


code:
def reparent_children(self, parent):
    print "Reparenting"
    for child in self.get_children():
        print "Working on", child.first_name, "to parent", parent.email
        parent = Node.objects.get(id=parent.id)
        current_child = Node.objects.get(id = child.id)
        current_child.move_to(parent, 'last-child')
        current_child.save()

rawDescription: django-mptt: how to successfully move nodes around

description: Django-mptt: how to successfully move nodes around


code:
import urllib2, base64, csv
from bs4 import BeautifulSoup
import MySQLdb
import os
import string
import datetime

request = urllib2.Request("website.com")
base64string = base64.encodestring('%s:%s' % ('username','password')).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request)
soup = BeautifulSoup(result.read())

#print(soup.prettify())
#table=soup.find('table', {"class":"resultsTable,ruler,sortable"})[0]

# Open database connection
db = MySQLdb.connect(host="ipaddress", # your host, usually localhost
                     user="username", # your username
                      passwd="password", # your password
                      db="databsae") # name of the data base

cursor=db.cursor()

table=soup.findAll('table')[6]

#print table

for row in table.findAll('tr'):
    cells = row.findAll('td')
    #For each "tr", assign each "td" to a variable.
    if len(cells) == 19:
        column1 = cells[0].find(text=True)
        column2 = cells[1].find(text=True)
        column3 = cells[2].find(text=True)
        column4 = cells[3].find(text=True)
        column5 = cells[4].find(text=True)
        column6 = cells[5].find(text=True)
        column7 = cells[6].find(text=True)
        column8 = cells[7].find(text=True)
        column9 = cells[8].find(text=True)
        column10 = cells[9].find(text=True)
        column11 = cells[10].find(text=True)
        column12 = cells[11].find(text=True)
        column13 = cells[12].find(text=True)
        column14 = cells[13].find(text=True)
        column15 = cells[14].find(text=True)
        column16 = cells[15].find(text=True)
        column17 = cells[16].find(text=True)
        column18 = cells[17].find(text=True)
        column19 = cells[18].find(text=True)
        TOTAL_AD_CALLS = column6.replace(',','')
        TOTAL_US_AD_CALLS = column7.replace(',','')
        TOTAL_NON_US_AD_CALLS = column8.replace(',','')
        TOTAL_NON_CLEAN_US_AD_CALLS = column10.replace(',','')
        TOTAL_CLEAN_US_AD_CALLS = column12.replace(',','')
        UNUSABLE_AD_CALLS = column13.replace(',','')
        NO_ADS_RETURNED = column14.replace(',','')
        PSAS_RETURNED = column15.replace(',','')
        ADS_RETURNED = column16.replace(',','')
        TOTAL_RETURNED = column17.replace(',','')
        TOTAL_IMPRESSIONS  = column18.replace(',','')
        Engagements = column19.replace(',','')
        NOT_US_RATE = column9.replace('%','')
        NOT_CLEAN_RATE = column11.replace('%','')
        Created = datetime.datetime.now()

print Engagements

cursor.execute ("INSERT INTO PYTHON_TEST (REPORT_TYPE, THE_DATE, PARENT_ID, SITE_ID,SITE_NAME, TOTAL_AD_CALLS, TOTAL_US_AD_CALLS, TOTAL_NON_US_AD_CALLS, NOT_US_RATE, TOTAL_NON_CLEAN_US_AD_CALLS, NOT_CLEAN_RATE, TOTAL_CLEAN_US_AD_CALLS, UNUSABLE_AD_CALLS, NO_ADS_RETURNED, PSAS_RETURNED, ADS_RETURNED, TOTAL_RETURNED, TOTAL_IMPRESSIONS, ENGAGEMENTS, CREATED) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);", (column1, column2, column3, column4, column5, TOTAL_AD_CALLS, TOTAL_US_AD_CALLS, TOTAL_NON_US_AD_CALLS, NOT_US_RATE ,TOTAL_NON_CLEAN_US_AD_CALLS, NOT_CLEAN_RATE, TOTAL_CLEAN_US_AD_CALLS, UNUSABLE_AD_CALLS, NO_ADS_RETURNED, PSAS_RETURNED, ADS_RETURNED, TOTAL_RETURNED, TOTAL_IMPRESSIONS, Engagements, Created))

#cursor.execute ("INSERT INTO PYTHON_TEST (ENGAGEMENTS,434 ) VALUES (%s);", (Engagements))
db.commit()
'''
cursor.execute ("INSERT INTO PYTHON_TEST  VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);", (column1, column2, column3, column4, column5, TOTAL_AD_CALLS, TOTAL_US_AD_CALLS, TOTAL_NON_US_AD_CALLS, NOT_US_RATE ,TOTAL_NON_CLEAN_US_AD_CALLS, NOT_CLEAN_RATE, TOTAL_CLEAN_US_AD_CALLS, UNUSABLE_AD_CALLS, NO_ADS_RETURNED, PSAS_RETURNED, ADS_RETURNED, TOTAL_RETURNED, TOTAL_IMPRESSIONS, Engagements, Created, Created))'''

# disconnect from server
db.close()

rawDescription: Python Data Insert From Beautiful Soup Table to MYSQL


code:
from collections import OrderedDict
stuff=OrderedDict({'age':26,'name':'Freddie Mercury', 'city':'Vladivostok'})
stuff[1]='first'
print stuff
stuff[2]='second'
print stuff
stuff[3]='wtf'
print stuff

rawDescription: how is a dictionary sorted?

description: How is a dictionary sorted


code:
address_counts = {}  # Create an empty dictionary to map addresses to the number of times they occur
for address in IP:
    if address not in address_counts:
        address_counts[address] = 1  # The first time we see an address set the count to 1
    else:
        address_counts[address] += 1  # The next time we see something, add 1 to the count
for address, count in address_count.iteritems():  # Python 2 specific, loops over each key and value in a dictionary. In Python 3, use .items()
    print address + '|' + str(count)

rawDescription: Appending to a list with a str(count) goes wrong


code:
rows = []
r = np.zeros((n,), dtype=np.int)
rows.append(r)
for i in range(n):
    r = np.zeros((n,), dtype=np.int)
    r[i] = 1
    rows.append(r)

rows = np.array(rows)

arr = np.zeros((m,n), dtype=np.int)
idx = [0]*m
base = np.array([(n+1)**j for j in range(m)])
for i in xrange(0, (n+1)**m):
    idx = (i / base) % (n+1)
    print rows[idx]

rawDescription: Iterate over all possible numpy binary arrays with restrictions using python


code:
# Setup Variables
pos1 = 0
pos2 = 0
url_string = '''
Daily News

This is the daily news.

end
'''
i = int(len(url_string)) # the url_string length is 60 characters
print "Setting up Variables with string at ", i, " characters"
print "String is: ", url_string

"""string.find(s, sub[, start[, end]])
Return the lowest index in s where the substring sub is found such that sub is
wholly contained in s[start:end]. Return -1 on failure. Defaults for start and
end and interpretation of negative values is the same as for slices.

Source: http://docs.python.org/2/library/string.html
"""

print "Running through program first time"
pos1 = int(url_string.find('>'))
# This finds the first occurrence of '>', which is at position 6

pos2 = int(url_string.find('<', pos1))
# This finds the first occurrence of '<' after position 3 ('>'),
# which is at position 15
print "Pos1 is at:", pos1, " and pos2 is at:", pos2

url_string = url_string[pos2:] # trimming string down?
print "The string is now: ", url_string
#

This is the daily news.

end

print "The string length is now: ", int(len(url_string)) # string length now 45
i = int(len(url_string)) # updating the length var to the new length

rawDescription: As a beginning Pythoner I don't understand why I get an infinity loop with while?


code:
import random
num_lines = 10

# read the contents of your file into a list
with open('master.txt', 'r') as f:
  lines = [L for L in f if L.strip()]  # store non-empty lines

# get the line numbers of lines that are not marked
candidates = [i for i, L in enumerate(lines) if not L.startswith("*")]

# if there are too few candidates, simply select all
if len(candidates) > num_lines:
  selected = random.sample(candidates, num_lines)
else:
  selected = candidates  # choose all

# print the lines that were selected
print "".join(lines[i] for i in selected)

# Mark selected lines in original content
for i in selected:
  lines[i] = "*%s" % lines[i]  # prepend "*" to selected lines

# overwrite the file with modified content
with open('master.txt', 'w') as f:
  f.write("".join(lines))

rawDescription: Python empty line and text marking

description: Python empty line and text marking


code:
from bs4 import BeautifulSoup as BS
html = """

  some text

   some more text

   and more text

"""
soup = BS(html)
for linebreak in soup.find_all('br'):
    linebreak.extract()

print soup.prettify()

rawDescription: Beautifulsoup sibling structure with br tags


code:
from struct import pack, unpack
for i1, i2 in product(xrange(2**16), repeat=2):
    try:
        f = unpack('>f', pack('>HH', i1, i2))
    except Exception:
        print 'Fails for {}, {}'.format(i1, i2)

rawDescription: convert 2 ints to 32 float which works for all cases

description: Convert 2 ints to 32 float which works for all cases


code:
import time

def myfunc():
    now=time.time()
    timer = 0
    while timer != 10:
        end = time.time()
        timer = round(end-now)

def mynextfunc():
    now=time.time()
    timer = 0
    while timer != 5:
        end = time.time()
        timer = round(end-now)

myfunc()
print "myfunc() exited after 10 seconds"

mynextfunc()
print "mynextfunc() exited after 5 seconds"

rawDescription: How to make a function run for a particular time period?

description: Make a function run for a particular time period


code:
import yaml
from functools import partial

class Persistable(object):
    # simulate a unique id
    _unique = 0

    def __init__(self, *args, **kw):
        Persistable._unique += 1
        self.persistent_id = ("%s.%d" %
                              (self.__class__.__name__, Persistable._unique))

def persistable_representer(dumper, data):
    id = data.persistent_id
    print "Writing to file: %s" % id
    outfile = open(id, 'w')
    outfile.write(yaml.dump(data))
    outfile.close()
    return dumper.represent_scalar(u'!xref', u'%s' % id)

class PersistingDumper(yaml.Dumper):
    pass

PersistingDumper.add_representer(Persistable, persistable_representer)
my_yaml_dump = partial(yaml.dump, Dumper=PersistingDumper)

def persistable_constructor(loader, node):
    xref = loader.construct_scalar(node)
    print "Reading from file: %s" % id
    infile = open(xref, 'r')
    value = yaml.load(infile.read())
    infile.close()
    return value

yaml.add_constructor(u'!xref', persistable_constructor)

# example use, also serves as a test
class Foo(Persistable):
    def __init__(self):
        self.one = 1
        Persistable.__init__(self)

class Bar(Persistable):
    def __init__(self, foo):
        self.foo = foo
        Persistable.__init__(self)

foo = Foo()
bar = Bar(foo)
print "=== foo ==="
dumped_foo = my_yaml_dump(foo)
print dumped_foo
print yaml.load(dumped_foo)
print yaml.load(dumped_foo).one

print "=== bar ==="
dumped_bar = my_yaml_dump(bar)
print dumped_bar
print yaml.load(dumped_bar)
print yaml.load(dumped_bar).foo
print yaml.load(dumped_bar).foo.one

baz = Bar(Persistable())
print "=== baz ==="
dumped_baz = my_yaml_dump(baz)
print dumped_baz
print yaml.load(dumped_baz)

rawDescription: Alternatives to pickle's `persistent_id`?


code:
from Tkinter import *

def proof(event=None):
    print 'ping'

root = Tk()

frame = Frame(root, height=100, width=100)
frame.focus_set()
frame.bind('<Control-Shift-KeyPress-U>', proof)
frame.pack()

root.mainloop()

rawDescription: Tkinter Linux version of "<Control-Shift-u>"


code:
class Player(object):
    """ A player in a shooter game. """

    def __init__(self,ammo):
        self.ammo = ammo
        self.lose = False

    def blast(self, enemy):
        if self.lose:
            print "You were unsuccessful and did not kill the alien!\nNow the Earth was destroyed thanks to you."
        else:
            print "The player blasts an enemy."
            if self.ammo > 0:
                self.ammo -= 1
                print "Your ammunition count is reduced by 1."
                print "The alien took 1 damage! You've done it!\n"
                enemy.die()
            else:
                if enemy.health == 0:
                    print "They're already dead, yo."
                else:
                    self.lose = True
                    print "The alien has more health than you have ammo."
                    print "You run out of ammo and die!"

class Alien(object):
    """ An alien in a shooter game. """

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

    def die(self):
        if self.health > 1:
            self.health -= 1
            print "Is that all you've got??\n"
        elif self.health == 1:
            self.health -= 1
            print "Oh no im gonna die"
        else:
            print "The alien is already dead.  What you're doing is unneccessary."

# main
print "\tThe Death of an Alien\n"

#same health and ammo
print "\n-----You have 6 counts of ammo.-----"
print "-----The alien has 6 health.-----\n"
hero = Player(6)
invader = Alien(6)
for i in range(6):
    hero.blast(invader)

#lower health than ammo
print "\n-----You have 6 counts of ammo.-----"
print "-----The alien has 5 health.-----\n"
hero = Player(6)
invader = Alien(5)
for i in range(6):
    hero.blast(invader)

#lower ammo than health
print "\n-----You have 5 counts of ammo.-----"
print "-----The alien has 6 health.-----\n"
hero = Player(5)
invader = Alien(6)
for i in range(6):
    hero.blast(invader)

raw_input("\n\nPress the enter key to exit.")

rawDescription: Making an Alien Blaster Game for python

description: Making an Alien Blaster Game for python


code:
stmt = session.query(Address).\
                 limit(5).\
                 subquery()
adalias = aliased(Address, stmt)
for user, address in session.query(User, adalias).\
         join(adalias, User.addresses):
     print user
     print address

rawDescription: Eager load partial relationship in SQLAlchemy


code:
import random

x=random.randint(0,255)
randIP = '192.168.0.'
randIP += str(x)
print randIP

rawDescription: Adding a integer at the end of an string in python


code:
def findSubset(alist, targ, i, sumsofar, listsofar):
    if sumsofar == targ:
        return True, listsofar
    if i == len(alist):
        return False, listsofar
    inc, inclistsofar = findSubset(alist, targ, i+1, sumsofar+alist[i], listsofar + [alist[i]])
    noninc, noninclistsofar = findSubset(alist, targ, i+1, sumsofar, listsofar)

    if inc:
        return inc, inclistsofar
    else:
        return noninc, noninclistsofar

alist = [4, 6, 21, 29, 37, 50]
print findSubset(alist, 76, 0, 0, [])

rawDescription: Display subset in recursive algorithm for subset sum


rawDescription: Condense list into string : ['z','y','x'...] -> 'zyx...' ? Python (2.7.1)


code:
## Some shared class that is used all over the place and needs to be patched.

class A(object):
    def __init__(self):
        self.firstname = 'Bob'

    # Print my first name.
    def name(self):
        return self.firstname

    # Use this to allow patching arbitrary methods...
    @classmethod
    def patch(cls, func_name):
        def patch_by_name(new_func):
            old_func = getattr(cls, func_name)
            def patched_func(self):
                return new_func(self, old_func)
            setattr(cls, func_name, patched_func)
        return patch_by_name

## Some other area of the code where you want to throw in a patch

class PatchedA(A):  # doesn't need to subclass, but comes in handy sometimes
    @A.patch('name')
    def name(self, orig_func):
        return 'I am ' + orig_func(self) + 'McWizwaz'

print 'Who are you, A class?'
print A().name()  # prints 'I am Bob McWizwaz'

rawDescription: How to call the original method when it is monkey-patched?

description: Call the original method when it is monkey-patched


code:
for key, value in productDict.iteritems():
     for key2, value2 in value.iteritems():
               print key2, value2

rawDescription: Iterating through a dictionary contains both dict and array


code:
def match_display_hook(self, substitution, matches, longest_match_length):
    print ''
    for match in matches:
        print match
    print self.prompt.rstrip(),
    print readline.get_line_buffer(),
    readline.redisplay()

rawDescription: readline set_completion_display_matches_hook requires return key before displaying prompt

description: Readline set_completion_display_matches_hook requires return key before displaying prompt


code:
from collections import defaultdict

def canonical_form(word):
    return tuple(sorted(word))

anagrams = defaultdict(list)

for word in open("/usr/share/dict/words"):
    word = word.lower().strip()
    anagrams[canonical_form(word)].append(word)

while True:
    print anagrams[canonical_form(raw_input())]

rawDescription: How can i generate the english words from given characters in python

description: Generate the english words from given characters in python


code:
filelist = ['IMG_1.jpg','IMG_2.jpg', 'IMG_3.jpg']

arraysDict = {}
value = {}
con = {}
i = 1

for imagefile in filelist:
      t=Image.open(imagefile).convert('L')
      arr = array(t) #Convert test image into an array
      f = arr + c #Add the corrective factor to the array with the UBM
      f[f > 150] = 0
      value[i] = np.sum(f) #Sum elements in array
      con[i] = np.count_nonzero(f) #Count number of nonzero elements
      i += 1

arraysDict = {}
for i in range(1,4):
    arraysDict['d{0}'.format(i)] = value[i]/con[i]

q = arraysDict.values()
print q

rawDescription: Cycle Through Images in Directory and Assign a Variable to Each result in Python


code:
now = time.time()
twodays_ago = now - 60*60*24*2 # Number of seconds in two days
if fileCreation < twodays_ago:
    print "File is more than two days old"

rawDescription: python comparing date. check for old file

description: Python comparing date. check for old file


code:
foo = [1,2,3]
print max(foo)

rawDescription: Python - Find the greatest number in a set of numbers


code:
def bye():
  print 'goodbye world!!'

import atexit
atexit.register(bye)

rawDescription: Executing a function when the console window closes?


code:
import libtorrent as lt
import urllib2

public_torrent = 'http://releases.ubuntu.com/14.04.3/ubuntu-14.04.3-desktop-amd64.iso.torrent'

def downloadTorrent(torrent_url):
    """
    Download torrent using libtorrent library.
    Torrent will be stored at the current directory.
    """
    ses = lt.session()
    ses.listen_on(6881, 6891)

    # read torrent file as bytes
    torrent = lt.bdecode(urllib2.urlopen(torrent_url, 'rb').read())

    info = lt.torrent_info(torrent)
    h = ses.add_torrent({'ti': info, 'save_path': './'})
    ses.start_dht()
    print 'starting', h.name()

    while (not h.is_seed()):
        s = h.status()

        state_str = ['queued', 'checking', 'downloading metadata', \
          'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
        print '\r%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
          (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
          s.num_peers, state_str[s.state]),
        sys.stdout.flush()

        time.sleep(1)

    print h.name(), 'complete'

downloadTorrent(public_torrent)

rawDescription: I can't download torrent from public tracker libtorrent


code:
import cv2
import numpy as np

world = cv2.imread("world.png") # entire world image
grass = cv2.imread("grass.png") # grass image

result = cv2.matchTemplate(world, grass, cv2.TM_CCOEFF_NORMED)
print np.unravel_index(result.argmax(), result.shape)

rawDescription: Python library for looking patterns in an image


code:
from pytesser import *
image = Image.open('image.jpg')  # Open image object using PIL

text =  image_to_string(image)     # Run tesseract.exe on image

print(repr(text))
result = text.split("\n")

print result

rawDescription: pytesser - next line of text in image?

description: Pytesser - next line of text in image


code:
from bs4 import BeautifulSoup
import urllib2
import re
import json

hdr = {'Accept': 'text/html,application/xhtml+xml,*/*',"user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"}
url='https://www.upwork.com/freelancers/_~013dfabae39ba01678/'
req=urllib2.Request(url,headers=hdr)
html = urllib2.urlopen(req)
soup=BeautifulSoup(html,"lxml")

script = soup.title.find_next('script').get_text()
map_search = re.search('.*var phpVars = (\{.*);', script)
mapData = map_search.group(1)
mapDataObj = json.loads(mapData)

print mapDataObj['profile']['profile']['location']['city']

rawDescription: Beautifulsoup scraping data where there a js text in the middle


code:
# generic Python imports
import datetime
import os
import sched
import sys
import tempfile
import threading
import time

# need PyObjC on sys.path...:
for d in sys.path:
  if 'Extras' in d:
    sys.path.append(d + '/PyObjC')
    break

# objc-related imports
import objc
from Foundation import *
from AppKit import *
from PyObjCTools import AppHelper

# all stuff related to the repeating-action
thesched = sched.scheduler(time.time, time.sleep)

def tick(n, writer):
  writer(n)
  thesched.enter(20.0, 10, tick, (n+1, writer))
  fd, name = tempfile.mkstemp('.txt', 'hello', '/tmp');
  print 'writing %r' % name
  f = os.fdopen(fd, 'w')
  f.write(datetime.datetime.now().isoformat())
  f.write('\n')
  f.close()

def schedule(writer):
  pool = NSAutoreleasePool.alloc().init()
  thesched.enter(0.0, 10, tick, (1, writer))
  thesched.run()
  # normally you'd want pool.drain() here, but since this function never
  # ends until end of program (thesched.run never returns since each tick
  # schedules a new one) that pool.drain would never execute here;-).

# objc-related stuff
class TheDelegate(NSObject):

  statusbar = None
  state = 'idle'

  def applicationDidFinishLaunching_(self, notification):
    statusbar = NSStatusBar.systemStatusBar()
    self.statusitem = statusbar.statusItemWithLength_(
        NSVariableStatusItemLength)
    self.statusitem.setHighlightMode_(1)
    self.statusitem.setToolTip_('Example')
    self.statusitem.setTitle_('Example')

    self.menu = NSMenu.alloc().init()
    menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
        'Quit', 'terminate:', '')
    self.menu.addItem_(menuitem)
    self.statusitem.setMenu_(self.menu)

  def writer(self, s):
    self.badge.setBadgeLabel_(str(s))

if __name__ == "__main__":
  # prepare and set our delegate
  app = NSApplication.sharedApplication()
  delegate = TheDelegate.alloc().init()
  app.setDelegate_(delegate)
  delegate.badge = app.dockTile()
  delegate.writer(0)

  # on a separate thread, run the scheduler
  t = threading.Thread(target=schedule, args=(delegate.writer,))
  t.setDaemon(1)
  t.start()

  # let her rip!-)
  AppHelper.runEventLoop()

rawDescription: Basic cocoa application using dock in Python, but not Xcode and all that extras


code:
for cnt, h3 in enumerate(selector.xpath('.//h3'), start=1):
    print h3.xpath('./following-sibling::node()[count(preceding-sibling::h3)=%d]' % cnt).extract()

for cnt, h3 in enumerate(selector.xpath('.//h3'), start=1):
    print h3.xpath('./following-sibling::node()[count(preceding-sibling::h3)=%d]/descendant-or-self::text()' % cnt).extract()

rawDescription: Xpath select nodes between two nodes in scrapy 0.24.5


code:
import multiprocessing as mp
import time

def build(q):
    print 'I build things'
    time.sleep(5)
    q.put(42)
    return

def run(q):
    while True:
        num = q.get()
        if num == 42:
            print 'I run after build is done'
            return
        else:
            print 'not the right number...'

    def get_number():
        return 41

if __name__ == '__main__':
    queue = mp.Queue()

    run_p = mp.Process(name='run process', target=run, args=(queue,))
    build_p = mp.Process(name='build process', target=build, args=(queue,))

    run_p.start()
    build_p.start()

    print 'waiting on build'
    build_p.join()
    print 'waiting on run'
    run_p.join()
    queue.close()
    print 'waiting on queue'
    queue.join_thread()
    print 'done'

rawDescription: In Python, how do you get data back from a particular process using multiprocessing?


code:
x = [1,2,3,4,5,6]
restart = True
while restart:
    for i in x:
        # add any exit condition!
        # if foo == bar:
        #   restart = False
        #   break
        if i == 4:
           break
        else:
            print i

rawDescription: how to restart "for" loop in python ?

description: Restart "for" loop in python


code:
import pickle

friend = {}
with open('friends.txt') as f:
    while 1:
        try:
            friend.update(pickle.load(f))
        except EOFError:
            break # no more data in the file

for person in friend.values():
    print '{Name}\t{Email}\t{Phone}'.format(**person)

rawDescription: Can't read appended data using pickle.load() method


code:
from subprocess import Popen, PIPE

df = Popen('df -P /Volumes/link/to/some/folder', shell=True, stdout=PIPE)
serverAddress = df.stdout.readlines()[1:][0]
serverAddress = serverAddress.split('@')[1]
serverAddress = serverAddress.split('/')[0]
print serverAddress

rawDescription: Get 'unc' path in OSX of mounted share

description: Get 'unc' path in OSX of mounted share


code:
max_len = 0
max_key = ""
for key in d:
    cur_len = len(d[key])
    if cur_len>max_len:
        max_key = key
        max_len = cur_len
print max_key

rawDescription: How do you find which key in a dictionary has the longest list of values?

description: Find which key in a dictionary has the longest list of values


code:
import sqlite3 as standard
from pysqlite2 import dbapi2 as latest
for m in (standard, latest):
   print m.sqlite_version

rawDescription: How can I upgrade the sqlite3 package in Python 2.6?

description: Upgrade the sqlite3 package in Python 2.6


code:
# Inherit object for new-style Python classes (recommended)
class GraphUtils(object):
    # Override the __init__() magic-method.
    def __init__(self):
        # Initialize the inherited object by executing its __init__() method.
        super(GraphUtils, self).__init__()

    def set_size(self, new_size):
        self.graph_size = new_size

# Inherit GraphUtils
class Graph(GraphUtils):
    def __init__(self):
        # Initialize the inherited GraphUtils object.
        super(Graph, self).__init__()
        # Declare the graph_size variable on creation of a new Graph object.
        self.graph_size = 10

    def print_size(self):
        print self.graph_size

if __name__ == "__main__":
    g = Graph()
    # It is recommended to use str.format() instead of string concatonation
    print "Graph default size: {}".format(g.graph_size) # will print "Graph default size: 10"
    g.set_size(20)
    g.print_size() # This will print 20

rawDescription: How to inherit a class' methods and have them access self member variables in python

description: Inherit a class' methods and have them access self member variables in python


code:
def conversionSolution(utf8StringInput):
    return repr(utf8StringInput)[2:][:-1]

utf8string = u'\u5f00\u80c3\u83dc'
asciistring = conversionSolution(utf8string)
print asciistring

rawDescription: Unicode string to backslash-encoded non-unicode 7-bit ASCII in python?


code:
from multiprocessing import Process, Value

def callback(x):
    return x * 2

def handler(x, fn, a):
    a.value = fn(x.value)

if __name__ == '__main__':
    x = Value('d', 2)
    a = Value('d', 0)
    p1 = Process(target=handler, args=(x, callback, a))
    p2 = Process(target=handler, args=(x, callback, a))
    p1.start()
    p2.start()
    p1.join()
    p2.join()
    print a.value

rawDescription: Can I send a function as a parameter to all processes with multiprocessing?

description: Send a function as a parameter to all processes with multiprocessing


code:
evaluation = 0
n = 0
m = 0
number = 0
check = False

task = input (' Hello! Welcome to Patricia G. Myrons program select task 1, 2 or  3. \n')

         #If the user selects task 1, the program will perform the Simple Divisibility Test
if task == 1  :
    print "Task 1 is here! \n"
    print "I can tell you if n is evenly divisible by m \n"
    print "Enter the following"
    n = input("Integer:")
    m = input("Integer:")
    evaluation = n % m
    if evaluation == 0:
        print n, "/", m, " evenly divides"
    else:
        print n, "/", m, " Sorry, does not evenly divide. Try again!"

#If the user selects task 2, the program will perform the Prime Test
if task == 2 :
    print "Task 2 is here! \n"
    print "I can tell you if the number you enter is prime or not  \n"

    number=int(raw_input("Enter a number "))
    if number <= 1:
        print "Sorry! It is not prime"
    else:
        n=2
        check = True
    while n != m:
        if m%n == 0:
            print "Yeas! The number you entered is prime"
            check = False
            break
        n+=1
    if check == True:
        print "Yeas! The number you entered is prime"

#If the user selects task 3, the program will display a list of factor numbers
if task == 3:
    print "Task 3 is here! \n"
    print "I can tell you all of the factors of the number you enter \n"
    def print_factors(n):

        print("The factors of",n,"are:")
        for i in range(1, n + 1):
            if n % i == 0:
                print(i)
    num = print_factors(int(input("Enter any number: ")))

rawDescription: Having some problems with Task 2

description: Having some problems with Task 2


code:
foo = """

two

three
blabla

"""
import BeautifulSoup as bs
soup = bs.BeautifulSoup(foo)
for table in soup.findAll('table', {'class':'theclass'} ):
    links=table.findAll('a')

print links

rawDescription: How to define findAll for html nested tags using beautifulsoup

description: Define findAll for html nested tags using beautifulsoup


code:
for line in lines:
    m = re.match('^Name[^:]*: present', line)
    if m:
        print re.sub(': present', ': not present', line)
    else:
       print line

rawDescription: pywikipedia (python) regex to add string if lacking

description: Pywikipedia (python) regex to add string if lacking


code:
import simpy

class Timer(object):

    def __init__(self, env, delay, callback):
        self.env      = env
        self.delay    = delay
        self.action   = None
        self.callback = callback
        self.running  = False
        self.canceled = False

    def wait(self):
        """
        Calls a callback after time has elapsed.
        """
        try:
            yield self.env.timeout(self.delay)
            self.callback()
            self.running  = False
        except simpy.Interrupt as i:
            print "Interrupted!"
            self.canceled = True
            self.running  = False

    def start(self):
        """
        Starts the timer
        """
        if not self.running:
            self.running = True
            self.action  = self.env.process(self.wait())

    def stop(self):
        """
        Stops the timer
        """
        if self.running:
            self.action.interrupt()
            self.action = None

    def reset(self):
        """
        Interrupts the current timer and restarts.
        """
        self.stop()
        self.start()

rawDescription: How do I interrupt or cancel a SimPy Timeout event?

description: Interrupt or cancel a SimPy Timeout event


code:
coord = np.where(np.all(img == (0, 0, 255), axis=-1))
print zip(coord[0], coord[1])

rawDescription: How to get x,y coordinates using python?

description: Get x,y coordinates using python


code:
import re
match_dict = {'hello(here)': 'here',
                'Hello   (Hi)': 'Hi',
                "'dfsfds Hello (Hi) fdfd' Hello (Yes)": 'Yes',
                "Hello ('hi)xx')": "hi)xx",
                "Hello  ('Hi')": 'Hi'}
for s, goal in match_dict.iteritems():
    print "INPUT: %s" % s
    print "GOAL: %s" % goal
    m = re.sub(r"(?<!\()'[^']+'", '', s, flags=re.I|re.M)
    paren_quotes = re.findall(r"hello\s*\('([^']+)'\)", m, flags=re.I|re.M)
    output = paren_quotes if paren_quotes else []
    m = re.sub(r"hello\s*\('[^']+'\)", '', m, flags=re.I|re.M)
    paren_matches = re.findall(r"hello\s*\(([^)]+)\)", m, flags=re.I|re.M)
    if paren_matches:
        output.extend(paren_matches)
    print 'OUTPUT: %s\n' % output

rawDescription: Regex Python~Get Everything In Between, If the word is not enclosed by single quotes


code:
def func_one():
    return 2 + 2

def func_two():
    x = func_one()
    print x

func_two()
#output: 4

rawDescription: How to call a previous function in a new function?

description: Call a previous function in a new function


rawDescription: Create objects obj0 through objN in Python, for arbitrary N?


code:
def runInstance(args):
    # NOTE: We don't do the stdout or stderr args
    p = subprocess.call(args,
                        # stdout=subprocess.PIPE,
                        # stderr=subprocess.STDOUT,
                        close_fds=True,
                        shell=True
                        )

def main():
    print "Starting test harness..."
    server_path = os.getcwd() + '/' + 'server.py'
    args0 = [sys.executable, server_path, '-p', '7000', '-t', '8000', '-u', '9000']
    args1 = [sys.executable, server_path, '-p', '7001', '-t', '8001', '-u', '9001']

    # Start server instances
    # NOTE: It is target=runInstance, not target=runInstance()
    p0 = multiprocessing.Process(target=runInstance, args=(' '.join(args0),))
    p1 = multiprocessing.Process(target=runInstance, args=(' '.join(args1),))
    p0.start()
    p1.start()

rawDescription: python subprocesses (of process with subprocesses) on termination hang in OSX activity monitor

description: Python subprocesses (of process with subprocesses) on termination hang in OSX activity monitor


code:
import BeautifulSoup

html = '''<html>
<head>
<meta name="description" content="Free Web tutorials on HTML, CSS, XML" />
<META name="keywords" content="HTML, CSS, XML" />
<title>Test</title>
</head>
<body>
</body>
</html>'''

soup = BeautifulSoup.BeautifulSoup(html)
for x in soup.findAll('meta'):
    print x

rawDescription: Getting BeautifulSoup to catch tags in a non-case-sensitive way


code:
with open('/Users/masongardner/Desktop/pbp-2014.csv', 'rb') as csvfile: #opens the list of descriptions
    reader = csv.reader(csvfile, delimiter=',', quotechar='|')
    reader.next() #skip the title row
    for row in reader:
        desc=row[14] #corresponds with the description of each play
        #print desc
        with open('rbs.csv', 'rb') as rbs:  #open the running back file
           for line in rbs: #each line should be another running back in the running backs CSV
                if re.findall(line[0], desc, re.I):
                    print 'found'
                    print line
                    print desc
                elif re.findall('no play| KICKS | KNEEL | KNEELS | PUNT | PUNTS | Extra point |Field goal|Two-minute|END GAME|timeout|end of quarter|end quarter|no huddle', desc, re.I):
                    pass
                elif re.findall('right guard|right end|right tackle|left guard|left tackle|left end|up the middle', desc,re.I):
                    print "did not find it"
                    print desc
                    rback=raw_input('running back:')
                    with open('rbs.csv','a') as rbfile:
                        rbwriter=csv.writer(rbfile, delimiter=' ', quotechar='|',quoting=csv.QUOTE_MINIMAL)
                        rbwriter.writerow([rback])
                else:
                    print "did not find it"
                    print desc

rawDescription: Searching two csv files for matching variables using Python


code:
import itertools
def truncate_groups(seq, max_group_size, key):
    """yield only the first `max_group_size` elements from each sub-group of `seq`"""
    for key, group in itertools.groupby(seq, key):
        for item in list(group)[:max_group_size]:
            yield item

for row in truncate_groups(summary_results, 10, lambda x: x[0].lower(), x[1].lower()):
    print row

rawDescription: Grabbing certain data from sorted list in Python


rawDescription: Conversion of strings like \\uXXXX in python


code:
for node in response.xpath('//div[@class="articleBody"]//p'):
        print node.xpath('string()').extract()

rawDescription: Matching multiple <p> tags in scrapy

description: Matching multiple <p> tags in scrapy


code:
def checkJson(jsonContents):
    bodyFlag = True if "body" in jsonContents["objects"][0]["data"] and jsonContents["objects"][0]["data"]["body"] == "Present" else False
    codeFlag = True if "code" in jsonContents["objects"][0]["data"] and jsonContents["objects"][0]["data"]["code"] == 200 else False

    return bodyFlag or codeFlag

print checkJson(result)

rawDescription: Check if key/value is in JSON

description: Check if key/value is in JSON


code:
import string
from itertools import combinations_with_replacement

result = itertools.combinations_with_replacement(string.ascii_letters, 5)

#comb = [''.join(n) for n in result] # warning, heavy processing

print [''.join(result.next()) for _ in range(10)]
# > ['aaaaa', 'aaaab', 'aaaac', 'aaaad', 'aaaae', 'aaaaf', 'aaaag', 'aaaah', 'aaaai', 'aaaaj']

rawDescription: Python regex iteration for all combinations


code:
for m in re.finditer(r"\w+ly", text):
    print '%02d-%02d: %s' % (m.start(), m.end(), m.group(0))

rawDescription: Parse Java Source Files with Python

description: Parse Java Source Files with Python