code:
import string, re, timeit

# Precomputed values (for str_join_set and translate)

letter_set = frozenset(string.ascii_lowercase + string.ascii_uppercase)
tab = string.maketrans(string.ascii_lowercase + string.ascii_uppercase,
                       string.ascii_lowercase * 2)
deletions = ''.join(ch for ch in map(chr,range(256)) if ch not in letter_set)

s="A235th@#$&( er Ra{}|?>ndom"

# From unwind's filter approach
def test_filter(s):
    return filter(lambda x: x in string.ascii_lowercase, s.lower())

# using set instead (and contains)
def test_filter_set(s):
    return filter(letter_set.__contains__, s).lower()

# Tomalak's solution
def test_regex(s):
    return re.sub('[^a-z]', '', s.lower())

# Dana's
def test_str_join(s):
    return ''.join(c for c in s.lower() if c in string.ascii_lowercase)

# Modified to use a set.
def test_str_join_set(s):
    return ''.join(c for c in s.lower() if c in letter_set)

# Translate approach.
def test_translate(s):
    return string.translate(s, tab, deletions)

for test in sorted(globals()):
    if test.startswith("test_"):
        assert globals()[test](s)=='atherrandom'
        print "%30s : %s" % (test, timeit.Timer("f(s)",
              "from __main__ import %s as f, s" % test).timeit(200000))

rawDescription: What is the most efficient way in Python to convert a string to all lowercase stripping out all non-ascii alpha characters?


code:
for item in soup.find_all('div', lang=True):
    if not (item.strong and item.small):
        continue
    language = item.strong.string
    pages = item.small.string
    print '{}: {}'.format(language, pages)

rawDescription: How to get objects from div with BeautifulSoup in Python?

description: Get objects from div with BeautifulSoup in Python


code:
if any("1" in str(x) for x in self.users):
   print "True"

rawDescription: Python if Statement not finding value

description: Python if Statement not finding value


code:
filenames = ['c:/batch/a.mb', 'c:/batch/b.mb']
results = []
for eachfile in filenames:
    cmds.file(eachfile, open =True, force=True)
    cmds.setAttr("defaultRenderGlobals.imageFormat", 32)
    # other render settings would go here
    result.append( cmds.render('persp', x=512, y=512))

print results # prints out the names of all the rendered files

rawDescription: how to save multi images by MEL command

description: Save multi images by MEL command


code:
for fileName in glob.glob('*.py'):
    print fileName

rawDescription: Any equivalent of * style in Bash for acting on mutliple files NOT matching the same pattern


rawDescription: How to convert 32-bit binary to float

description: Convert 32-bit binary to float


code:
print dostuff('5.0')
print dostuff('005.00')
print dostuff('.500')
print dostuff('.580')
print dostuff('5.80')

rawDescription: Removing zeros from a numeric string in Python


code:
import unittest

def fancy_attr(*args, **kwargs):
    """Decorator that adds attributes to classes or functions
    for use with the Attribute (-a) plugin. It also renames functions!
    """
    def wrap_ob(ob):
        for name in args:
            setattr(ob, name, True)
            ob.__doc__ = '_'.join([ob.__name__, name])

        return ob

    return wrap_ob

class TestLogins(unittest.TestCase):
    @fancy_attr('slow')
    def test_method(self):
        assert True

def make_name(orig, attrib):
    return '_'.join([orig, attrib])

def rename(cls):
    methods = []
    for key in cls.__dict__:
        method = getattr(cls, key)
        if method:
            if hasattr(cls.__dict__[key], '__dict__'):
                if 'slow' in cls.__dict__[key].__dict__:
                    methods.append(key)

    print methods

    for method in methods:
        setattr(cls, make_name(method, 'slow'),  cls.__dict__[key])
        delattr(cls, method)

rename(TestLogins)

@fancy_attr('slow')
def test_func():
    assert True

rawDescription: Append the nose @attr to the test name

description: Append the nose @attr to the test name


code:
from Tkinter import *

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

def test(x):
    #print "I'm in event:", x
    if x == 1:            # if event on entry e1
        print 'e1 event' # do some thing
    elif x == 2:            # also if event on entry e2
        print 'e2 event'  # do some thing else
    else:
        print 'no event'

def test1(x):
    test(1)

def test2(x):
    test(2)

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

root=Tk()

e1=Entry(root)
e1.pack()

e2=Entry(root)
e2.pack()

root.bind_class(e1, "<FocusOut>", test1) #this can be some thing you suggest to me
root.bind_class(e2, "<FocusOut>", test2) # can be some thing you suggest to me

root.mainloop()

rawDescription: how handle events on every individual entry

description: How handle events on every individual entry


rawDescription: Python a list and a dictionary

description: Python a list and a dictionary


code:
def deep_access(x,keylist):
     val = x
     for key in keylist:
         val = val[key]
     return val

s = 'key2.key21.key211'

print deep_access(x,s.split('.'))

rawDescription: Programmatically accessing arbitrarily deeply-nested values in a dictionary


code:
class AttributeDescriptor(object):
    def __init__(self, attr):
        self.attr = attr

    def __set__(self, inst, value):
        getattr(inst, self.attr).update(value)

    def __get__(self, inst, owner):
        return getattr(inst, self.attr).compute()

class AdjustmentTracker(object):
    def __init__(self, base):
        print("Attribute initialized to %s" % base)
        self.base = base
        self.adjustments = []

    def compute(self):
        return self.base + sum(self.adjustments)

    def update(self, value):
        delta = (value - self.compute())
        print("Adjustment added: %s" % delta)
        self.adjustments.append(delta)

class Creature(object):
    health = AttributeDescriptor('_health')
    armor  = AttributeDescriptor('_armor')

    def __init__(self, health=100, armor=0):
        self._health = AdjustmentTracker(health)
        self._armor =  AdjustmentTracker(armor)

c1 = Creature(50)
c1.health = 60      # Adds a +10 adjustment
print c1.health     # 60
c1.health += 10     # Add a +10 adjustment
print c1.health     # 70
print c1._health.adjustments     # [10, 10]

rawDescription: Creating an Attribute class that allows adjusting a base value?


code:
from gevent.monkey import patch_all; patch_all()
from gevent.socket import create_connection
import gevent
from http_parser.http import HttpStream
from http_parser.reader import SocketReader

def worker(n):
    try:
        s = create_connection(('gunicorn.org', 80))
        s.send("GET / HTTP/1.1\r\nHost: gunicorn.org\r\n\r\n")
        r = SocketReader(s)
        p = HttpStream(r)
        print "Worker {}, headers length: {}".format(n, len(p.headers()))
    finally:
        s.close()

if __name__ == '__main__':
    jobs = [gevent.spawn(worker, job_no) for job_no in range(10)]
    gevent.joinall(jobs)

rawDescription: compatibility of gevent with other packages

description: Compatibility of gevent with other packages


code:
in_text = """City = "Urban Recovery", "Urban Mop-up Operation", "The Oncoming Darkness", "Arks Ship Fire Swirl"
Forest = "Subdue Fang Banther", "With Wind and Rain"
Caves = "Cave", "Volcanic Guerillas"
Desert = "Desert Guerillas"
Tundra =
Skyland = "Rampaging Malice", "Chrome"
Tunnels = "Mega Mecha awakening"
Ruins =
Coast = "Beach Wars!"
Quarry =
Space = "Profound Darkness' Kin: Elder", "Raging Dark Arms", "Approaching Dark Arms", "Utterly Profound", "Falz", "Dark Falz"
Event = "Merry Christmas on Ice", "Trick or Treat", "A Boisterous White Day", "Where's the Chocolate" """

for line in in_text.split("\n"):
    key,vals = line.split("=")
    key = key.strip()
    vals = [x.strip() for x in vals.split(",")]
    print key," : ",vals

rawDescription: Split lines according to regex pattern with 1 or more results


code:
# The inputs are equivalent and should return the same value
print make_canonical([8, 12 | 1]) # returns [8, 13]
print make_canonical([12, 8 | 1]) # returns [12, 9]

rawDescription: Generating all 5 card poker hands

description: Generating all 5 card poker hands


code:
class node(object):
    def __init__(self, *arg_list):
        for (name, arg) in zip('a b c d'.split(), arg_list):
            setattr(self, name, arg)

v = node(1, 20, 300, "Eric")
print v.d  # "Eric"
v.d = "Ajay"  # Works

rawDescription: pack multiple variables of different datatypes in list/array python

description: Pack multiple variables of different datatypes in list/array python


code:
for key in df.columns.values:
    if key in formatters:
        df[key] = df[key].apply(formatters[key])
print df.to_latex()

rawDescription: Export pandas DataFrame to LaTeX and apply formatters by row


code:
def isPrivate(ipaddress):
    if any(ipaddress in IPv4Network(r) for r in ranges):
        print 'private'

rawDescription: How best to iterate over a list when a each list item will be passed to a function?


code:
from sklearn.datasets import load_iris
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVC
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import f1_score

iris = load_iris()

model_to_set = OneVsRestClassifier(SVC(kernel="poly"))

parameters = {
    "estimator__C": [1,2,4,8],
    "estimator__kernel": ["poly","rbf"],
    "estimator__degree":[1, 2, 3, 4],
}

model_tunning = GridSearchCV(model_to_set, param_grid=parameters,
                             score_func=f1_score)

model_tunning.fit(iris.data, iris.target)

print model_tunning.best_score_
print model_tunning.best_params_

rawDescription: GridSearch for an estimator inside a OneVsRestClassifier


code:
def get_section(section):
    marker_line = '#<TITLE>{}'.format(section)
    in_section = False
    section_lines = []
    with open('test.property') as f:
        while True:
            line = f.readline()
            if not line:
                break
            line = line.rstrip()
            if line == marker_line:
                in_section = True
            elif in_section and line.startswith('#<TITLE>'):
                break

            if in_section:
                if not line or line.startswith('#'):
                    continue
                section_lines.append(line)
    return '\n'.join(['{} {}'.format(i, line)
                            for i, line in enumerate(section_lines)])

print get_section('Connection setting')

rawDescription: Print text between two separators?

description: Print text between two separators


code:
with open('data.txt') as inf:
    for line in inf:
        if "Internet(R) explorer" in line:
            print 'found "Internet(R) explorer" in file.'

rawDescription: To search exact word in a file using python


code:
my_matrix = [row1, row2, row3]
print "\n".join(["\t".join(map(str, r)) for r in my_matrix])

rawDescription: Python's pretty printing of matrix

description: Python's pretty printing of matrix


code:
import pygame

pygame.init()
screen = pygame.display.set_mode((400, 400))
image = pygame.image.load('1.png')

sprite = pygame.sprite.Sprite()
sprite.image = image
sprite.rect = image.get_rect()

font = pygame.font.SysFont('Sans', 50)
text = font.render('This is a text', True, (255, 0, 0))

sprite.image.blit(text, sprite.rect)

group = pygame.sprite.Group()
group.add(sprite)
group.draw(screen)

pygame.display.flip()

print 'bits per pixel:'
print 'image', image.get_bitsize()
print 'screen', screen.get_bitsize()

rawDescription: Can't get pygame font to render on image


code:
tick = 1
week = 1
month = 1
year = 1

def new_week():
    global week, tick  # Declare that week and tick are global
    week = week + 1    # Modify the value of the global name week
    tick = tick + 1    # Modify the value of the global name tick
    if tick == 4:
        new_month()
        tick = 1

new_week()
print week, tick
new_week()
print week, tick

rawDescription: Values Not Adding Up

description: Values Not Adding Up


rawDescription: Using python re.compile with beautiful soup to match a string


code:
from multiprocessing.pool import ThreadPool
import time

def fun(arg):
    for i in range (3):
        print 'fun: %s'%i
        time.sleep(1)
    return arg+1

def handle_result(result):
   print("got result {}".format(result))

pool = ThreadPool(2)
pool.map_async(fun, [1,2,3], callback=handle_result)

rawDescription: How to return value from function running by QThread and Queue

description: Return value from function running by QThread and Queue


code:
l = [i for i in range(1,101) if i % 2 == 0]
print l

rawDescription: How to store a list of numbers generated as output in Python?

description: Store a list of numbers generated as output in Python


code:
import os

os.rename("C:\Users\JohnDoe\Downloads\Report.txt",
          "C:\Users\JohnDoe\Downloads\Report.cvs")

rawDescription: Converting .txt file to .xls in Python

description: Converting .txt file to .xls in Python


code:
from collections import Counter

ix = [0,1,2,0,1]
iy = [0,1,1,0,1]
w = [1,2,3,4,5]

counts = Counter()
for (key, count) in zip(zip(ix, iy), w):
    counts[key] += count
print "Counts:", counts

counts_as_list = [  # Conversion of the counting result (counts) to a list
    [key[0], key[1], total_count] for (key, total_count) in counts.iteritems()]
print "As a list:", counts_as_list

rawDescription: Sum third value in tuple for a given pair of the first two values in the tuple using Python


rawDescription: Reshaping a pandas dataframe into stacked/record/database/long format


code:
def is_ok(x):           #checks if x is a positive integer
    is_int = False
    is_pos = False
    if type(x) == int:
        is_int = True   # if it's an integer, continue to test if >0
        if x > 0:
            is_pos = True
        else:
            pass
    else:
        pass
    return is_int, is_pos

def get_value():
    z = input("insert value: ")
    is_int, is_pos = is_ok(z)
    if (is_int,is_pos) == (True, True): # could also do `if all(is_int,is_pos):`
        print z**2
    elif (is_int,is_pos) == (False, False):
        print "Please enter an integer"
        get_value()
    elif (is_int, is_pos) == (True, False):
        print "Please enter an positive integer"
        get_value()
    else:
        print "something is wrong"

rawDescription: Python: How do I assign 2 values I return from a function with 1 input as values outside the function?


code:
import random

startRow = 3
endRow = 29
row = random.randint(startRow, endRow)
print sh1.cell(column, row).value

rawDescription: Randomize search in excel

description: Randomize search in excel


code:
from PyQt4 import QtCore, QtGui
import time

class Window(QtGui.QDialog):

    def __init__(self):
        super(Window, self).__init__()
        self.resize(200,100)

        layout = QtGui.QVBoxLayout(self)

        layout.addWidget(QtGui.QLineEdit())

        self.button1 = QtGui.QPushButton("Sleep")
        self.button1.clicked.connect(self.go_safe)
        layout.addWidget(self.button1)

        self.button2 = QtGui.QPushButton("Crunch")
        self.button2.clicked.connect(self.go_GIL_buster)
        layout.addWidget(self.button2)

    def go_safe(self):
        t = QtCore.QThread(self)
        heavy = Heavy()
        heavy.moveToThread(t)
        t.started.connect(heavy.safe)
        print "starting thread"
        t.start()
        print "thread started"
        self.t1 = t
        self.heavy1 = heavy

    def go_GIL_buster(self):
        t = QtCore.QThread(self)
        heavy = Heavy()
        heavy.moveToThread(t)
        t.started.connect(heavy.GIL_buster)
        print "starting thread"
        t.start()
        print "thread started"
        self.t2 = t
        self.heavy2 = heavy

class Heavy(QtCore.QObject):

    def safe(self):
        print "Sleeping"
        time.sleep(10)
        print "Done"

    def GIL_buster(self):
        print "Crunching numbers"
        x = 2
        y = 500000000
        foo = x**y
        print "Done"

if __name__ == "__main__":
    app = QtGui.QApplication([])
    win = Window()
    win.show()
    win.raise_()
    app.exec_()

rawDescription: Multithreading Boost Python C++ Code in PySide


code:
import MySQLdb

user_input = raw_input("Please enter barcode and press Enter button: ")

db = MySQLdb.connect(passwd="moonpie",db="thangs")
mycursor = db.cursor()
mycursor.execute("""SELECT name, price FROM Product
                 WHERE barcode = %s""", (user_input,))

# calls fetchone until None is returned (no more rows)
for row in iter(mycursor.fetchone, None):
    print row

rawDescription: Using user input to find information in a Mysql database


code:
text = '''
       I live in SOME_PLACE.
       I am a graduate student.
       My school is in SOME_PLACE.
       '''

text_new = ' '.join('<s> {} </s>'.format(l.strip()) for l in text.splitlines() if len(l.strip()))
print text_new

rawDescription: How to insert html like tag "<s></s>" for a string using regular expression in python?

description: Insert html like tag "<s></s>" for a string using regular expression in python


code:
p = re.compile(r'(http://\S+)|(egg(\d+))')

def repl(match):
    if match.group(2):
        return 'spam{0}'.format(match.group(3))
    return match.group(0)

print p.sub(repl, t)

rawDescription: Python regex not to match http://

description: Python regex not to match http://


code:
k = []
k.append(55.0)
k.append(75.1)
l = [k]
print l

int0 = int(l[0][0])
int1 = int(l[0][1])
print int0
print int1

rawDescription: Getting two separateed integer values

description: Getting two separateed integer values


code:
line = proc.stdout.readline()

while line != '':
    print line

rawDescription: How do you execute a terminal command and then repeatedly check and store the response in Python?

description: Execute a terminal command and then repeatedly check and store the response in Python


code: print [item.lower() for item in [row[index] for index in (1, 0)]]

rawDescription: How to add lower function to this code

description: Add lower function to this code


code:
import psycopg2

query = """
    select array(select * from (values (1), (2)) s);
"""

conn = psycopg2.connect('dbname=cpn user=cpn')
cursor = conn.cursor()
cursor.execute(query)
rs = cursor.fetchall()

for l in rs:
    print l[0]

cursor.close()
conn.close()

rawDescription: Return PosgreSQL UUID array as list with psycopg2


code:
a = A()
try:
    a.d = 89
except AttributeError:
    print "It works!"
else:
    print "It doesn't work."
a.c = 42
print a.a
print a.c
a.freeze = False
a.d = 28
a.freeze = True
print a.d

rawDescription: In Python, count the number of variables in a class or prevent adding new class variables


code:
import random
words = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]

random.shuffle(words)

grouped = [words[i:i+3] for i in range(0, len(words), 3)]
for l in grouped:
    print "".join("{:<10}".format(x) for x in l)

rawDescription: How do you put words into a 3x3 grid using python

description: Put words into a 3x3 grid using python


rawDescription: Given a list of Tuples, return a new list of the first values of the tuples


code:
import sys

orig_stdout = sys.stdout
f = file('out.txt', 'w')
sys.stdout = f

for i in range(2):
    print 'i = ', i

sys.stdout = orig_stdout
f.close()

rawDescription: How to redirect 'print' output to a file using python?

description: Redirect 'print' output to a file using python


code:
import os
import textwrap

filepath = os.getcwd()
def MakeFile(file_name):
    temp_path = filepath + file_name
    with open(file_name, 'w') as f:
        f.write(textwrap.dedent('''\
            def print_success():
                print "sucesss"
                '''))
    print 'Execution completed.'

rawDescription: Whats the best way to write python code into a python file?


code:
for eip_dict in addresses_dict['Addresses']:
    with open('/root/temps', 'w') as f:
      if 'PrivateIpAddress' in eip_dict:
         print eip_dict['PublicIp']
         f.write(eip_dict['PublicIp'])

rawDescription: How to write the list to file?

description: Write the list to file


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

m = []
for e in mainlist:
    l = sum([i for i in e if i < 2.5])
    k = [i for i in e if i >= 2.5] # to distribute smaller values
    j = float(l)/len(k)
    newlist = [x + j for x in k]
    m.append(max(newlist))

print m

rawDescription: How to process sub-lists separately?

description: Process sub-lists separately


code:
for r in session.query(CsvTable).filter(CsvTable.age == '32'):
    print r.name, r.age, r.nickname

rawDescription: Database on the fly with scripting languages


code:
def bounce(num):
    limit, delta = num + 1, -1
    while(num != limit):
        print num
        num += delta
        if num == 0:
            delta = 1

rawDescription: Count to zero then Count up

description: Count to zero then Count up


code:
sint = ignore_exception(ValueError)(int)
print sint("Hello World") # prints none
print sint("1340") # prints 1340

rawDescription: Is there a built-in or more Pythonic way to try to parse a string to an integer


code:
#!/usr/bin/env python
import codecs, itertools, operator, sys
import numpy

filename = sys.argv[1] if len(sys.argv)>1 else '/usr/share/dict/american-english'

# ucs2 or ucs4 python?
dtype = {2: numpy.uint16, 4: numpy.uint32}[len(buffer(u"u"))]

# count ordinals
text = codecs.open(filename, encoding='utf-8').read()
a = numpy.frombuffer(text, dtype=dtype)
counts = numpy.bincount(a)

# pretty print
counts = [(unichr(i), v) for i, v in enumerate(counts) if v]
counts.sort(key=operator.itemgetter(1))
print ' '.join('("%s" %d)' % c for c in counts  if c[0] not in ' \t\n')

rawDescription: Python - Is a dictionary slow to find frequency of each character?


code:
def adv001():
    print("All of a sudden an angry rat jumps out of the sewer right beneath your feet. The small, stinky animal aggressivly flashes his teeth.")
    fight.report(zoo.rat)

allAdv=[]
print locals().keys()  #  ['adv001', '__builtins__', 'random', '__package__', '__name__', '__doc__']
for e in list(locals().keys()):
    if e[:3]=="adv":
        allAdv.append(e)
print(allAdv)
locals()[random.choice(allAdv)]()

rawDescription: Python: random function from locals() with defined prefix


code:
from BeautifulSoup import BeautifulSoup
import urllib2

url = 'http://www.meinpaket.de/de/shopsList.html?page=1'
soup = BeautifulSoup(urllib2.urlopen(url))
datas = soup.findAll('div', {'class':'tsrImg'})
for data in datas:
    link = data.find('a')
    print link.get('href')

rawDescription: Python can not get links from web page

description: Python can not get links from web page


code:
data = [
        ('31.13.73.36', 'google.com'),
        ('31.13.72.35', 'foo.bar'),
        ('31.13.72.36', 'somedomain.com')
]
print sorted(data, key=lambda item: socket.inet_aton(item[0]))

rawDescription: How can I get the list sorted out according to the IP address?

description: Get the list sorted out according to the IP address


code:
# Style 1
abc = lambda: myFunction()

# Style 2
def abc():
    return myFunction()

def myFunction():
    print "123"

rawDescription: Is it possible to use functions before declaring their body in python?


code:
my_list_o_lists = [ [], ["cow", "says", "moo"], [1, 2, 3] ]
m0 = my_list_o_lists[0]
m0 = [ "Where is my cow?", "No idea!" ]
print m0
print my_list_o_lists[0]

rawDescription: Why can't I change a list within a list?

description: Why can't I change a list within a list


code:
L = ['San Francisco', 'Christchurch ', 'Sydney ', 'Bangkok ', 'Copenhagen ']

# Get the maximum length of a string in the list
max_length = len(max(L, key = lambda x : len(x)))
#print max(L, key = lambda x : len(x)) # get the maximum of the list based in length
#print max_length

# Iterate through indices of max_length: 0, 1, 2, 3 ...
for i in range(max_length):
    # Iterate through each city in the list
    for city in L:
        # If the index is < than the length of the city
        if  i < len(city) and city[i] != ' ':
            print city[i],

rawDescription: I have been trying to write a program python


rawDescription: Convert python datetime to epoch with strftime


code:
import collections
import select
import fcntl
import os
import time
import Queue
import sys
import threading
import subprocess
PIPE = subprocess.PIPE

def make_async(fd):
    # http://stackoverflow.com/a/7730201/190597
    '''add the O_NONBLOCK flag to a file descriptor'''
    fcntl.fcntl(
        fd, fcntl.F_SETFL, fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK)

def read_async(fd):
    # http://stackoverflow.com/a/7730201/190597
    '''read some data from a file descriptor, ignoring EAGAIN errors'''
    # time.sleep(1)
    try:
        return fd.read()
    except IOError, e:
        if e.errno != errno.EAGAIN:
            raise e
        else:
            return ''

def write_output(fds, outmap):
    for fd in fds:
        line = read_async(fd)
        sys.stdout.write(line)
        outmap[fd.fileno()].append(line)

process = subprocess.Popen(
    ['random_print.py'], stdout=PIPE, stderr=PIPE, close_fds=True)

make_async(process.stdout)
make_async(process.stderr)
outmap = collections.defaultdict(list)
while True:
    rlist, wlist, xlist = select.select([process.stdout, process.stderr], [], [])
    write_output(rlist, outmap)
    if process.poll() is not None:
        write_output([process.stdout, process.stderr], outmap)
        break

fileno = {'stdout': process.stdout.fileno(),
          'stderr': process.stderr.fileno()}

print(outmap[fileno['stdout']])
print(outmap[fileno['stderr']])

rawDescription: Subprocess.Popen: cloning stdout and stderr both to terminal and variables


code:
import fileinput
import re

for line in fileinput.input("your_file.txt", inplace=True):
    #remove digits
    result = ''.join(i for i in line if not i.isdigit())
    #remove dollar signs
    result = result.replace("$","")
    #some other regex, removes all y's
    result = re.sub("[Yy]+", "", result)
    print result,

rawDescription: Removing digits from a file

description: Removing digits from a file


code:
c = wmi.WMI()
wql = "Select * From Win32_SerialPort"
for item in c.query(wql):
    print item

rawDescription: How to access wmi in python?

description: Access wmi in python


code:
for i in solution():
    print i,

rawDescription: More elegant/Pythonic way of printing elements of tuple?


code:
with open('file2') as fd:
    for line in fd:
        data = line.strip().split()
        print data1[data[0]], data1[data[1]]

rawDescription: Python to search values in one text file, compare them with values in another text file, then replace values if there is a match


code: print [hotel['sutton'][fname] for fname in headers]

rawDescription: parsing this csv file in python(pylab) and converting it into a dictionary

description: Parsing this csv file in python(pylab) and converting it into a dictionary


code:
import re

ref_fields = ['s-port',
              'time','date',
              's-sitename', 's-ip',
              'cs(User-Agent)', 'sc-status',
              'sc-substatus', 'sc-win32-status',
              'c-ip',
              'cs-username',
              'cs-method', 'cs-uri-stem', 'cs-uri-query']

print 'REF_FIELDS :\n------------\n%s\n' % '\n'.join(ref_fields)

############################################
file_path = 'I:\\sample[1].log'                  # Path to put here
############################################

with open(file_path, 'r') as log_lines:
    line = ''
    while line[0:8]!='#Fields:':
        line = next(log_lines)
    # At this point, line is the line containing the fields keywords
    print 'line of the fields keywords:\n----------------------------\n%r\n' % line

    found_fields = line.split()[1:]
    len_found_fields = len(found_fields)
    regex_extractor = re.compile('[ \t]+'.join(len_found_fields*['([^ \t]+)']))
    print 'list found_fields of keywords in the file:\n------------------------------------------\n%s\n' % found_fields

    print '\nfound_fields == ref_fields  is ',found_fields == ref_fields

    if found_fields == ref_fields:
        print '\nNORMAL ORDER\n------------'
        def line_spliter(line):
            return line.split()

    else:
        the_order = [ found_fields.index(fild) + 1 for fild in ref_fields]
        # the_order is the list of indexes localizing the elements of ref_fields
        # in the order in which they succeed in the actual line of found fields keywords
        print '\nSPECIAL ORDER\n-------------\nthe_order == %s\n\n\n======================' % the_order
        def line_spliter(line):
            return regex_extractor.match(line).group(*the_order)

    for i in xrange(1):
        line = next(log_lines)
        (s_port,
        time, date,
        s_sitename, s_ip,
        cs_user_agent, sc_status, sc_substatus, sc_win32_status,
        c_ip,
        cs_username,
        cs_method, cs_uri_stem, cs_uri_query) = line_spliter(line)
        print ('LINE :\n------\n'
               '%s\n'
               'SPLIT LINE :\n--------------\n'
               '%s\n\n'
               'REORDERED SPLIT LINE :\n-------------------------\n'
               '%s\n\n'
               'EXAMPLE OF SOME CORRECT BINDINGS OBTAINED :\n-------------------------------------------\n'
               'date == %s\n'
               'time == %s\n'
               's_port == %s\n'
               'c_ip == %s\n\n'
               '======================') % (line,'\n'.join(line.split()),line_spliter(line),date,time,s_port,c_ip)

# ---- split each logline into multiple variables, populate dictionaries and db ---- #
def splitLogline(log_line):
        # needs to be dynamic (for different logging setups)
        s_port,
        time, date,
        s_sitename, s_ip,
        cs_user_agent, sc_status, sc_substatus, sc_win32_status,
        c_ip,
        cs_username,
        cs_method, cs_uri_stem, cs_uri_query = line_spliter(line)

rawDescription: Creating a dynamic "variable layout" for split() in Python


rawDescription: How to intialise a Numpy array of numpy arrays

description: Intialise a Numpy array of numpy arrays


code:
d = {
    'a' : {
        'aa': {},
        'ab': {},
    },
    'b' : {
        'ba': {},
        'bb': {},
    }
}
def nested_keys(d):
    yield d.keys()
    for value in d.values():
        if isinstance(value, dict):
            for res in nested_keys(value):
                yield res

for keys in nested_keys(d):
    print keys

rawDescription: python get all keys in subdictorionaries

description: Python get all keys in subdictorionaries


code:
cont = "Y"
if cont == "N" or "n":
    print "cont is N or n"

rawDescription: Python IF Statement not being displayed

description: Python IF Statement not being displayed


code:
prefixes = [u'path', u'folder', u'directory', u'd']

mystrings = [u'path common path and directory', u'directory common path and directory', u'directory folder distinct and directory folder', u'distinct and directory folder', u'd fixable directory or folder']
for s in mystrings:
    for prefix in prefixes:
        if s.startswith(prefix+" "):
            print s[len(prefix)+1:]

rawDescription: Remove a prefix if matches an entry on a list


code:
import pkgutil

# this is the package we are inspecting -- for example 'email' from stdlib
import email

package = email
for importer, modname, ispkg in pkgutil.iter_modules(package.__path__):
    print "Found submodule %s (is a package: %s)" % (modname, ispkg)

rawDescription: List all the modules that are part of a python package?


code:
res = ['mode', "'clip'", 'clipzeros', 'True', 'field', "'R RQT'", 'clipminmax', '[0,1.2]']
mydict = dict(zip(res[::2],[eval(x) for x in res[1::2]]))

print mydict

rawDescription: how to preserve types when using pyparsing and returning the result as a dictionary

description: Preserve types when using pyparsing and returning the result as a dictionary


code:
import numpy as np
z0 = np.ones((2, 3, 4, 5))
za = np.ones(6)

z = za.reshape(za.shape + (1,)*z0.ndim) + z0

print z.shape
# (6, 2, 3, 4, 5)

rawDescription: How can I broadcast between 1D and nD arrays to obtain a (1+n)D array output?

description: Broadcast between 1D and nD arrays to obtain a (1+n)D array output


code:
from collections import Counter
x = [k for k, v in Counter([1, 2, 5, 3, 4, 1, 5]).iteritems() if v == 1]
print x

rawDescription: Removing duplicate element from a list and the element itself


code:
#!/usr/bin/env python

def a(): return 'function a'
def b(): return 'function b'
def c(): return 'function c'

func_list = [a, b, c]
func_names = [f.func_name for f in func_list]
funcs_dict = dict(zip(func_names, func_list))

f = funcs_dict['b']
print f()

rawDescription: How to use argparse arguments as function names

description: Use argparse arguments as function names


code:
import pandas as pd
results = {'Noconst':results_noconstant.summary2(),
           'withcon':results_withconstant.summary2()}
df = pd.DataFrame({'Model':[], 'Param':[], 'Value':[]})
for mod in results.keys():
    for col in results[mod].tables[0].columns:
        if col % 2 == 0:
            df = df.append(pd.DataFrame({'Model': [mod]*results[mod].tables[0][col].size,
                                         'Param':results[mod].tables[0][col].values,
                                         'Value':results[mod].tables[0][col+1].values}))

print df

rawDescription: statsmodels: printing summary of more than one regression models together

description: Statsmodels: printing summary of more than one regression models together


code:
# Hold the frame number and the value for each side
# Where 100 is greater than the expected height and width of the frame
# Assuming top left is (0,0)

frame_top = (0.0, 100.0)
frame_left = (0.0, 100.0)
frame_bottom = (0.0, 0.0)
frame_right = (0.0, 0.0)

for row in data.split("\n"):
    cols = [float(col) for col in row.split(" ") if len(col)]
    icols = iter(cols[1:])

    for x in icols:
        y = next(icols)

        if x and y: # Skip any nan values
            if x < frame_left[1]:
                frame_left = (cols[0], x)
            if x > frame_right[1]:
                frame_right = (cols[0], x)
            if y < frame_top[1]:
                frame_top = (cols[0], y)
            if y > frame_bottom[1]:
                frame_bottom = (cols[0], y)

print "Frame %f has top %f" % frame_top
print "Frame %f has left %f" % frame_left
print "Frame %f has bottom %f" % frame_bottom
print "Frame %f has right %f" % frame_right

rawDescription: How to find the maximum distance between five points?

description: Find the maximum distance between five points


code:
for anchor1 in soup.findAll('div', {"class": "listGrid-price"}):
    textcontent = u' '.join(anchor1.stripped_strings)
    if textcontent:
        print textcontent

rawDescription: Issue in printing data under different classes on html page, together using beautiful soup


code:
[[],
[{'deleted': False, 'user_key': u'landesk522_8', 'account_title': u'logger test',
'ceph_detail_id': 8L, 'ceph_pk': 8L}],
[{'deleted': True, 'user_key': u'landesk522_7', 'account_title': u'test new',
'ceph_detail_id': 7L, 'ceph_pk': 7L}]]

rawDescription: Python: sorting a list of dictionaries

description: Python: sorting a list of dictionaries


code:
prod_list = []
prod1 = Product(3, 3, 3)
prod_list.append(prod1)

prod2 = Product(3, 3, 2)
prod_list.append(prod2)

prod3 = Product(3, 3, 3)
print prod3 in prod_list
True
prod3 = Product(3, 3, 5)
print prod3 in prod_list
False

rawDescription: Check if an object (with certain properties values) not in list


code:
from subprocess import Popen, PIPE

proc = Popen(["/usr/bin/nc", "-l", "9999"], stdout=PIPE)

buffer = []
line = proc.stdout.readline()
while line:
    buffer.append(line)
    print "LINE", line.strip()
    line = proc.stdout.readline()

print "buffer", ''.join(buffer)

rawDescription: How to capture output without preventing it from output in python

description: Capture output without preventing it from output in python


code:
import win32con, win32api, win32gui, ctypes, ctypes.wintypes

class COPYDATASTRUCT(ctypes.Structure):
    _fields_ = [
        ('dwData', ctypes.wintypes.LPARAM),
        ('cbData', ctypes.wintypes.DWORD),
        ('lpData', ctypes.c_void_p)
    ]
PCOPYDATASTRUCT = ctypes.POINTER(COPYDATASTRUCT)

class Listener:

    def __init__(self):
        message_map = {
            win32con.WM_COPYDATA: self.OnCopyData
        }
        wc = win32gui.WNDCLASS()
        wc.lpfnWndProc = message_map
        wc.lpszClassName = 'MyWindowClass'
        hinst = wc.hInstance = win32api.GetModuleHandle(None)
        classAtom = win32gui.RegisterClass(wc)
        self.hwnd = win32gui.CreateWindow (
            classAtom,
            "win32gui test",
            0,
            0,
            0,
            win32con.CW_USEDEFAULT,
            win32con.CW_USEDEFAULT,
            0,
            0,
            hinst,
            None
        )
        print self.hwnd

    def OnCopyData(self, hwnd, msg, wparam, lparam):
        print hwnd
        print msg
        print wparam
        print lparam
        pCDS = ctypes.cast(lparam, PCOPYDATASTRUCT)
        print pCDS.contents.dwData
        print pCDS.contents.cbData
        print ctypes.wstring_at(pCDS.contents.lpData)
        return 1

l = Listener()
win32gui.PumpMessages()

rawDescription: Receiving WM_COPYDATA in Python

description: Receiving WM_COPYDATA in Python


rawDescription: find files more than 100 MB from ls -1hl command's output

description: Find files more than 100 MB from ls -1hl command's output


code:
coords = [(10, 10), (13, 12), (15, 13)]
print "\n".join(str(step) for step in steps(coords))

rawDescription: divide ways on a map in parts of length one

description: Divide ways on a map in parts of length one


code:
def func1(arg):
    arg.append('Hello from Func1!')
    print arg

def func2(arg):
    arg.append(2)
    print arg

main_list = [9,8,7]

func1(main_list)
func2(main_list)

rawDescription: Python 2.7 Local Variables are acting like global variables


rawDescription: how to get the last part of a string before a certain character?

description: Get the last part of a string before a certain character


rawDescription: Using scikit to determine contributions of each feature to a specific class prediction


code: print "The piece is moved to %s%d." % (col,row)

rawDescription: What am I doing wrong? and/or in Python

description: What am I doing wrong? and/or in Python


code:
def build_char_records(phrase):
    phrase = phrase.lower()
    resultList = []
    for character in phrase:
        if character not in resultList:
            resultList.append(character)

    resultList.sort()
    for i in range(len(resultList)):
        tphrase = phrase
        num = phrase.count(resultList[i])
        locs = []

        for j in range(num):
            index = tphrase.find(resultList[i])
            tphrase = tphrase[index+1:]
            if len(locs) != 0:
                index = locs[acc-1] + index + 1
            locs.append(index)

        resultList[i] = [resultList[i], num, locs]

    return resultList

print build_char_records('Hi there bye')

rawDescription: find the index of element the number of occurence in string

description: Find the index of element the number of occurence in string


code:
import readline

class MyCompleter(object):  # Custom completer

    def __init__(self, options):
        self.options = sorted(options)

    def complete(self, text, state):
        if state == 0:  # on first trigger, build possible matches
            if text:  # cache matches (entries that start with entered text)
                self.matches = [s for s in self.options
                                    if s and s.startswith(text)]
            else:  # no text entered, all matches possible
                self.matches = self.options[:]

        # return match indexed by state
        try:
            return self.matches[state]
        except IndexError:
            return None

completer = MyCompleter(["hello", "hi", "how are you", "goodbye", "great"])
readline.set_completer(completer.complete)
readline.parse_and_bind('tab: complete')

input = raw_input("Input: ")
print "You entered", input

rawDescription: How to code autocompletion in python?

description: Code autocompletion in python


code:
for p in permutations(list(x)):
    q = ''.join(p)
    for i in g:
        if i in q:
            print i
    g.seek(0)  # resetting the pointer back to the beginning of the file

rawDescription: searching the strings in a text file in another string

description: Searching the strings in a text file in another string


rawDescription: Sort List in Python by two other lists

description: Sort List in Python by two other lists


code:
import itertools
for x in itertools.count():
    print x

rawDescription: Print numbers from 1 to x where x is infinity in python


rawDescription: Compare two csv file content and store the similar into variable


code:
for i in xrange(1,4):
    print i**2

#prints 1,4,9

rawDescription: How do I generate a list like the one in the example using a non-inclusive range? (Python)

description: Generate a list like the one in the example using a non-inclusive range? (Python)


code: print hand

rawDescription: How to approach implementing 'class Card' as required by Python textbook

description: Approach implementing 'class Card' as required by Python textbook


code:
from subprocess import check_output
size_name = check_output(['gunzip', '-Nlq','othertext.gz'])
size_name = size_name.strip().split("%",1)
print "original filename =",size_name[1].strip()

rawDescription: How to read the contents of a .gz compressed file in Python?

description: Read the contents of a .gz compressed file in Python


code:
import re

x = re.compile(r'^([^\|]*?)\s*\|[^\n]*\n\s*(.*?)\s*$')

s = """NETHERLANDS MUSIC EPA | 36 before
Michael Buble performs in Amsterdam Canadian singer Michael Buble performs during a concert in Amsterdam, The Netherlands, 30 October 2009. Buble released his new album entitled 'Crazy Love'. EPA/OLAF KRAAK"""

mo = x.match(s)

subject, content = mo.groups()

print 'SUBJECT =', repr(subject)
print 'CONTENT =', repr(content)

rawDescription: How can I parse text in Python?

description: Parse text in Python


code:
A = [1,2,3]
B = [4,5,6]
C = [b_item for a_item, b_item in zip(A,B) for _ in range(a_item)]
print C

rawDescription: How to get one number specific times in an array python

description: Get one number specific times in an array python


code:
a = 2
print df.loc[df['s']==2, 'f']

rawDescription: Referencing columns by assigned name in numpy array


rawDescription: Filter Pandas DataFrame using a while loop


code:
occs = letterOccurrances("Hello, world!")
print occs['l']
print occs['h']

rawDescription: How do I iterate through a string in Python?

description: Iterate through a string in Python


code:
import re

def get(st, char_index):
    chars = -1
    for i, char in enumerate(st):
        if char != '-':
            chars += 1
        if char_index == chars:
            return i

def test():
    st = '---------------LL---NE--HVKTHTEEK---PF-ICTVCR-KS----------'
    initial = re.sub('-', '', st)
    for i, char in enumerate(initial):
        print i, char, st[get_1_indexed(st, i)]

def get_1_indexed(st, char_index):
    return 1 + get(st, char_index - 1)

def test_1_indexed():
    st = '---------------LL---NE--HVKTHTEEK---PF-ICTVCR-KS----------'
    initial = re.sub('-', '', st)
    for i, char in enumerate(initial):
        print i+1, char, st[get_1_indexed(st, i + 1) - 1]

rawDescription: How to process character by character in a line

description: Process character by character in a line


code:
class A(object):
    def function1(self, x, y):
        def function2(y):
            print "Function 2 ", y
        print "Function 1 ", x
        return function2

rawDescription: Python Inheritance with closures

description: Python Inheritance with closures


code:
s = 1
def f():
    print s
print f()
s = 1
def g():
    print s
    s = 2
print g()

rawDescription: Python Variables with Inconsistent Scope


code:
from xml.sax import make_parser, handler

class extract_text(handler.ContentHandler):
    def characters(self,data):
        if data.strip():
            print data

parser = make_parser()
parser.setContentHandler(extract_text())
parser.feed(doc)

rawDescription: Remove all XML tags, only keep the text between the tags


code:
from scapy.all import *
import sys

class RawPcapReaderFD(RawPcapReader):
    """A stateful pcap reader. Each packet is returned as a string"""

    def __init__(self, fd):
        self.filename = "dummy"
        try:
            self.f = fd
            magic = self.f.read(4)
        except IOError:
            self.f = fd
            magic = self.f.read(4)
        if magic == "\xa1\xb2\xc3\xd4": #big endian
            self.endian = ">"
        elif  magic == "\xd4\xc3\xb2\xa1": #little endian
            self.endian = "<"
        else:
            raise Scapy_Exception("Not a pcap capture file (bad magic)")
        hdr = self.f.read(20)
        if len(hdr)<20:
            raise Scapy_Exception("Invalid pcap file (too short)")
        vermaj,vermin,tz,sig,snaplen,linktype = struct.unpack(self.endian+"HHIIII",hdr)

        self.linktype = linktype

class PcapReader(RawPcapReaderFD):
    def __init__(self, fd):
        RawPcapReaderFD.__init__(self, fd)
        try:
            self.LLcls = conf.l2types[self.linktype]
        except KeyError:
            warning("PcapReader: unknown LL type [%i]/[%#x]. Using Raw packets" % (self.linktype,self.linktype))
            self.LLcls = conf.raw_layer

print PcapReader(sys.stdin).read_all(-1)

rawDescription: Reading .pcap file from STDIN in Python Scapy


code:
tree = Branch(Branch(Branch(Leaf(4), Leaf(5)), Leaf(6), Branch(Leaf(8))))
print tree
tree.update(10)
print tree

rawDescription: Updating all objects in a class

description: Updating all objects in a class


code:
from multiprocessing import Queue, Process
from socket import socket
import pickle

def handle(q):
    sock = pickle.loads(q.get())
    print 'rest:', sock.recv(2048)

if __name__ == '__main__':
    sock = socket()
    sock.connect(('httpbin.org', 80))
    sock.send(b'GET /get\r\n')
    # first bytes read in parent
    print 'first part:', sock.recv(50)

    q = Queue()
    proc = Process(target=handle, args=(q,))
    proc.start()
    # use the function from above to serialize socket
    q.put(forking_dumps(sock))
    proc.join()

rawDescription: Python - Passing a TCP socket object to a multiprocessing Queue


code:
def store_timedelta_in_database(thingy, duration):
    seconds = dhms_to_seconds(*convert_timedelta(duration))
    db.execute('INSERT INTO foo (thingy, duration) VALUES (?, ?)',
               thingy, seconds)
    db.commit()

def print_timedelta_from_database(thingy):
    cur = db.execute('SELECT duration FROM foo WHERE thingy = ?', thingy)
    seconds = int(cur.fetchone()[0])
    days, hours, minutes, seconds = seconds_to_dhms(seconds)
    print '{} took {} minutes, {} hours, {} days'.format(thingy, minutes, hours, days)

rawDescription: How to convert datetime.timedelta to minutes, hours in Python?

description: Convert datetime.timedelta to minutes, hours in Python


code:
import time
import ctypes
import logging
import Queue
import multiprocessing as mp
import numpy as np

info = mp.get_logger().info

def main():
    logger = mp.log_to_stderr()
    logger.setLevel(logging.INFO)

    data_pipeline = Image_Data_Pipeline(
        num_data_buffers=5,
        buffer_shape=(60, 256, 512))
    start = time.clock()
    data_pipeline.load_buffers(data_pipeline.num_data_buffers)
    end = time.clock()
    data_pipeline.close()
    print "Elapsed time:", end-start

class Image_Data_Pipeline:
    def __init__(self, num_data_buffers, buffer_shape):
        """
        Allocate a bunch of 16-bit buffers for image data
        """
        self.num_data_buffers = num_data_buffers
        self.buffer_shape = buffer_shape
        pix_per_buf = np.prod(buffer_shape)
        self.data_buffers = [mp.Array(ctypes.c_uint16, pix_per_buf)
                             for b in range(num_data_buffers)]
        self.idle_data_buffers = range(num_data_buffers)

        """
        Launch the child processes that make up the pipeline
        """
        self.camera = Data_Pipeline_Process(
            target=child_process, name='Camera',
            data_buffers=self.data_buffers, buffer_shape=buffer_shape)
        self.display_prep = Data_Pipeline_Process(
            target=child_process, name='Display Prep',
            data_buffers=self.data_buffers, buffer_shape=buffer_shape,
            input_queue=self.camera.output_queue)
        self.file_saving = Data_Pipeline_Process(
            target=child_process, name='File Saving',
            data_buffers=self.data_buffers, buffer_shape=buffer_shape,
            input_queue=self.display_prep.output_queue)
        return None

    def load_buffers(self, N, timeout=0):
        """
        Feed the pipe!
        """
        for i in range(N):
            self.camera.input_queue.put(self.idle_data_buffers.pop())

        """
        Wait for the buffers to idle. Here would be a fine place to
        feed them back to the pipeline, too.
        """
        while True:
            try:
                self.idle_data_buffers.append(
                    self.file_saving.output_queue.get_nowait())
                info("Buffer %i idle"%(self.idle_data_buffers[-1]))
            except Queue.Empty:
                time.sleep(0.01)
            if len(self.idle_data_buffers) >= self.num_data_buffers:
                break
        return None

    def close(self):
        self.camera.input_queue.put(None)
        self.display_prep.input_queue.put(None)
        self.file_saving.input_queue.put(None)
        self.camera.child.join()
        self.display_prep.child.join()
        self.file_saving.child.join()

class Data_Pipeline_Process:
    def __init__(
        self,
        target,
        name,
        data_buffers,
        buffer_shape,
        input_queue=None,
        output_queue=None,
        ):
        if input_queue is None:
            self.input_queue = mp.Queue()
        else:
            self.input_queue = input_queue

        if output_queue is None:
            self.output_queue = mp.Queue()
        else:
            self.output_queue = output_queue

        self.command_pipe = mp.Pipe() #For later, we'll send instrument commands

        self.child = mp.Process(
            target=target,
            args=(name, data_buffers, buffer_shape,
                  self.input_queue, self.output_queue, self.command_pipe),
            name=name)
        self.child.start()
        return None

def child_process(
    name,
    data_buffers,
    buffer_shape,
    input_queue,
    output_queue,
    command_pipe):
    if name == 'Display Prep':
        display_buffer = np.empty(buffer_shape, dtype=np.uint16)
    while True:
        try:
            process_me = input_queue.get_nowait()
        except Queue.Empty:
            time.sleep(0.01)
            continue
        if process_me is None:
            break #We're done
        else:
            info("start buffer %i"%(process_me))
            with data_buffers[process_me].get_lock():
                a = np.frombuffer(data_buffers[process_me].get_obj(),
                                  dtype=np.uint16)
                if name == 'Camera':
                    """
                    Fill the buffer with data (eventually, from the
                    camera, dummy data for now)
                    """
                    a.fill(1)
                elif name == 'Display Prep':
                    """
                    Process the 16-bit image into a display-ready
                    8-bit image. Fow now, just copy the data to a
                    similar buffer.
                    """
                    display_buffer[:] = a.reshape(buffer_shape)
                elif name == 'File Saving':
                    """
                    Save the data to disk.
                    """
                    a.tofile('out.raw')
            info("end buffer %i"%(process_me))
            output_queue.put(process_me)
    return None

if __name__ == '__main__':
    main()

rawDescription: Dynamically create a list of shared arrays using python multiprocessing


code:
dist = [
    [0, 74, 4109, 3047, 2266],
    [74, 0, 4069, 2999, 2213],
    [4109, 4069, 0, 1172, 1972],
    [3047, 2999, 1172, 0, 816],
    [2266, 2213, 1972, 816, 0]
]

# Helper function to calculate path length
def path_len(path):
    return sum(dist[i][j] for i, j in zip(path, path[1:]))

# Set of all nodes to visit
to_visit = set(xrange(len(dist)))

# Current state {(node, visited_nodes): shortest_path}
state = {(i, frozenset([0, i])): [0, i] for i in xrange(1, len(dist[0]))}

for _ in xrange(len(dist) - 2):
    next_state = {}
    for position, path in state.iteritems():
        current_node, visited = position

        # Check all nodes that haven't been visited so far
        for node in to_visit - visited:
            new_path = path + [node]
            new_pos = (node, frozenset(new_path))

            # Update if (current node, visited) is not in next state or we found shorter path
            if new_pos not in next_state or path_len(new_path) < path_len(next_state[new_pos]):
                next_state[new_pos] = new_path

    state = next_state

# Find the shortest path from possible candidates
shortest = min((path + [0] for path in state.itervalues()), key=path_len)
print 'path: {0}, length: {1}'.format(shortest, path_len(shortest))

rawDescription: shortest path tsp algorithm

description: Shortest path tsp algorithm


code:
preprocessed = fancy_function(a_directory, a_regex)
print list(preprocessed)

rawDescription: Problems with a python function that return a list of strings?


code:
def performFileManipulation(self, widget, data=None):
        # Create the file chooser dialog:
        dialog = gtk.FileChooserDialog("Open..",
                                       None,
                                       gtk.FILE_CHOOSER_ACTION_OPEN,
                                       (gtk.STOCK_CANCEL,
                                        gtk.RESPONSE_CANCEL,
                                        gtk.STOCK_OPEN, gtk.RESPONSE_OK))
        dialog.set_default_response(gtk.RESPONSE_OK)

        # Display the file selector and obtain the response back
        response = dialog.run()

        # If the user selected a file, then get the filename:
        if response == gtk.RESPONSE_OK:
            dataLocation = dialog.get_filename()

        # If the file was not chosen, then just close the window:
        else:
            print "Closed, no files selected"   # Just for now

        # You'll need to import gobject
        gobject.timeout_add(100, doFileStuff, dataLocation)

rawDescription: PyGTK Window not hiding when told to

description: PyGTK Window not hiding when told to


code: print sum(fruits[name] for name in inputs)

rawDescription: python lists values

description: Python lists values


code:
l = range(10)
for a in l[:]:
    l.remove(a)
print l

rawDescription: Best way to remove controls from a wx panel

description: Remove controls from a wx panel


code:
def change(amount):

    # this can be removed if you pass the amount in pennies
    # rather than dollars
    amount = int(round(amount*100))

    values = [2000, 1000, 500, 100, 25, 10, 5, 1]
    denom = ['twenties', 'tens', 'fives', 'ones', 'quarters', 'dimes', 'nickels', 'pennies']

    for i in range(len(values)):
        num = amount / values[i]
        amount -= num * values[i]
        print str(num) + " " + denom[i]

rawDescription: Rounding up with pennies in Python?

description: Rounding up with pennies in Python


code:
def f(arg):
  print "passed arguemnt is: ", arg
  i = arg * 2
  return i

arguments = (11, 22)
L = map(f, arguments)  # (1) using map()
L
L = [f(arg) for arg in arguments]  # (2) using list compression
L

rawDescription: celery beat schedule arguments

description: Celery beat schedule arguments


code:
ranges = []
for k, g in groupby(enumerate(data), lambda (i,x):i-x):
    group = map(itemgetter(1), g)
    ranges.append((group[0], group[-1]))

rawDescription: Identify groups of continuous numbers in a list


code: print d['Total cells']

rawDescription: Parsing a generic data file in python

description: Parsing a generic data file in python


code:
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(html)
print [t.text for t in soup.find("table", {"class": "list04"}).findAll("div")]

rawDescription: HTML parsing with Beautiful soup

description: HTML parsing with Beautiful soup


code:
import os
for root, dirs, files in os.walk('.'):
    for name in files:
        if name == 'foo':
           name = str(name)
           name = os.path.realpath(os.path.join(root,name))
           print name

rawDescription: Python cant get full path name of file

description: Python cant get full path name of file


code:
for i in reversed(range(len(triangle))):
    getmaxofsub(0, i), getmaxofsub(i//2, i), getmaxofsub(i, i)

print getmaxofsub(0,0)

rawDescription: Max path triangle (Python)

description: Max path triangle (Python)


code:
class Quz(object):
    def __init__(self):
        self.a = "A"
def qux(self):
    print self.a
Quz.qux = qux
q = Quz()
q.qux()

rawDescription: how to add a new function for a class which is created previous?

description: Add a new function for a class which is created previous


code:
import urllib2, time, csv, json, re

def get_url(title):
    base_url = 'http://www.omdbapi.com/?i=&t='
    title = title.strip().replace(' ', '%20')
    return base_url + title

def get_json(lst, filename):
    data = []
    for year, actors, title in lst:
        data.append(urllib2.urlopen(get_url(title)).read())
        time.sleep(5)

    # save data to file
    with open(filename, 'wb') as f:
        json.dump(data, f)

def json_data(filename):
    with open(filename, 'rU') as f:
        return json.load(f)

def counter(filename):
    return len(json_data(filename))

lst = [['2010', 'Colin Firth', "The King's Speech "], ['2009', 'Jeff Bridges', 'Crazy Heart '], ['2008', 'Sean Penn', 'Milk ']]
filename = 'data_fetch.txt'
get_json(lst, filename)
print counter(filename)
print json_data(filename)

rawDescription: Trouble with writing separate JSON entries Python


code:
html = "<tag> </tag>"
from bs4 import BeautifulSoup
print BeautifulSoup(html).encode_contents(formatter='html')

rawDescription: BeautifulSoup and

description: BeautifulSoup and


code:
print 'calling missing_function'
missing_function()

rawDescription: How can I execute a bunch of Python commands before a script is run when using the Python interpreter?

description: Execute a bunch of Python commands before a script is run when using the Python interpreter


code:
for l1, l2 in zip(mentionedlist, mentionedlist[1:]):
    if len(l1) == len(l2) and sum(x != y for x,y in zip(l1, l2)) == 0:
        print l1, l2

rawDescription: How to iterate two successive elements of the lists of lists in python ?

description: Iterate two successive elements of the lists of lists in python


code:
def getLineGenerator(queue, splitOn):
    return_buffer = []
    while True:
        rcv_data = queue.get(block=True) # We can pull any number of characters here.
        for c in rcv_data:
            return_buffer.append(c)
            if c == splitOn:
                yield return_buffer
                return_buffer = []

gen = getLineGenerator(myQueue, "\n")
for line in gen:
    print line.strip()

rawDescription: Reading lines from Python queues

description: Reading lines from Python queues


code:
res = []
for n in range(0, N1):

   for i in range(0,1):
      u[i] = 2*Coefficient*(ui[i+1]+bi*toutside)+(1-2*Coefficient-2*bi*Coefficient)*ui[i]
   for i in range(1,Nx):
      u[i] = Coefficient*(ui[i+1]+ui[i-1])+(1-2*Coefficient)*ui[i]
   for i in range(Nx,Nx+1):
      u[i] = 2*Coefficient*(ui[i-1])+(1-2*Coefficient)*ui[i]
      ui[:]= u #updates matrix for next loop
   print ui
   res.append(ui.copy())

print res

rawDescription: Python Iterative Loop Plot values

description: Python Iterative Loop Plot values


code:
import xlrd

file_location = "/home/myuser/excel.xls"
workbook = xlrd.open_workbook(file_location, formatting_info=True)
sheet = workbook.sheet_by_index(0)

for row in range(sheet.nrows):
    cell = sheet.cell(row, 1)
    style = workbook.xf_list[cell.xf_index]
    color = style.background.pattern_colour_index
    if color == 43:  # on of yellows
        print cell.value

rawDescription: Python / Excel - Conditional cell printing with xlrd


code:
with open('data.csv', 'rt') as f:
    reader = csv.reader(f)
    usernames = {r[0]: r for r in reader}

print usernames[username]

rawDescription: Retrieving a specific row from a CSV file based on input


code:
for candidate in soup.select('.author, .author-name, [rel="author"]'):
    if candidate.text:
        author = candidate.text
        break
else:
    print "No author found"

rawDescription: Parse HTML and look for multiple classes and tags - most elegant way?


code:
import numpy as np
import Image

x = np.arange(256, dtype=np.uint8).reshape((16,16))
print 'Created array'
print x

im = Image.fromarray(x)
print 'Vales in grayscale PIL image using numpy.asarray <-- Works as expected'
print np.asarray(im)

print 'Converted to BW PIL image...'
im_bw = im.convert('1')

print 'Values in BW PIL image, using Image.getdata() <-- Works as expected'
print '  (Not a simple threshold due to dithering...)'
# Dividing by 255 to make the comparison easier
print np.reshape(im_bw.getdata(), (16, 16)) / 255

print 'Values in BW PIL image using numpy.asarray() <-- Unexpected!'
print '   (Same occurs when using numpy.array() to copy and convert.)'
print np.asarray(im_bw).astype(np.uint8)

print 'Workaround, convert back to type "L" before array conversion'
print np.array(im_bw.convert('L')).astype(np.uint8) / 255

rawDescription: Error Converting PIL B&W images to Numpy Arrays


code:
import StringIO
import sys

def macro_PyShell(macro, code):
  code_out = StringIO.StringIO()
  sys.stdout = code_out
  exec code.strip("'").strip('"')
  sys.stdout = sys.__stdout__
  return macro.request.formatter.text(code_out.getvalue())

rawDescription: Is there a way to incorporate python code into moinmoin pages?


code:
from itertools import chain
from collections import defaultdict

result = defaultdict(dict)

for c_dict in chain(L1, L2):
    result[c_dict["id"]].update(c_dict)

print result.values()

rawDescription: How to merge dictionaries within two lists if they have a common key-value pair?

description: Merge dictionaries within two lists if they have a common key-value pair


code:
r = (1 + 5**0.5)/2
wh = {bldg:((area/r)**0.5, (area*r)**0.5) for (bldg, area) in sqft.items()}
print wh

rawDescription: What type of list is best for calling specific items?


code:
from lxml import etree

XML = "your XML document here..."

root = etree.fromstring(XML)
for ns in sorted(root.nsmap.items()):
    print ns

rawDescription: Extract data using lxml in Python from every namespace attribute


code:
import xmltodict
mydict = {}
# I added the contents to a file named 'afile.txt'
with open("afile.txt","r") as f:
    for line in f:
        # ignore lines containing a bracket
        if "{" not in line and "}" not in line:
            # Split string and remove whitespaces
            linesplit = line.split("=")
            mydict[linesplit[0].strip()]=linesplit[1][:-1]
# define xml root tag
root = {
    'body': mydict
}
# parse (or unparce) to xml
print xmltodict.unparse(root, pretty=True)

rawDescription: Parsing text to XML format

description: Parsing text to XML format


code:
import numpy as np

def rolling_window_lastaxis(a, window):
    """Directly taken from Erik Rigtorp's post to numpy-discussion.
    <http://www.mail-archive.com/numpy-discussion@scipy.org/msg29450.html>"""
    if window < 1:
       raise ValueError, "`window` must be at least 1."
    if window > a.shape[-1]:
       raise ValueError, "`window` is too long."
    shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
    strides = a.strides + (a.strides[-1],)
    return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)

def rolling_window(a, window):
    if not hasattr(window, '__iter__'):
        return rolling_window_lastaxis(a, window)
    for i, win in enumerate(window):
        if win > 1:
            a = a.swapaxes(i, -1)
            a = rolling_window_lastaxis(a, win)
            a = a.swapaxes(-2, i)
    return a

filtsize = (3, 3)
a = np.zeros((10,10), dtype=np.float)
a[5:7,5] = 1

b = rolling_window(a, filtsize)
blurred = b.mean(axis=-1).mean(axis=-1)

rawDescription: Using strides for an efficient moving average filter


code:
s = "ABCDEFGHIJKL"
sl = slice(0,4)
print s[sl]

rawDescription: Python slice how-to, I know the Python slice but how can I use built-in slice object for it?


code:
from itertools import product
print [zip(A, item) for item in product(B, repeat=len(A))]

rawDescription: How to get all mappings between two lists?

description: Get all mappings between two lists


code:
import re
test = "a: 1 b: 2 c { d: 3 e: 4 } f: 5"
pattern = re.compile(r"[^ ]+(?:: [^ ]+| \{[^}]+\})")
for match in pattern.findall(test):
    print match

rawDescription: Tokenize string in python using regular expression


code:
res = df.merge(df.shift(-1), left_index=True, right_index=True).iloc[:-1]
res.columns = ['A1', 'B1', 'A2', 'B2']
res = res[['A1', 'A2', 'B1', 'B2']]
print res

rawDescription: pandas long to wide windowing

description: Pandas long to wide windowing


code:
import pybtex.database.input.bibtex
from StringIO import StringIO

def bibtex_string_to_data(s):
    parser = pybtex.database.input.bibtex.Parser()
    return parser.parse_stream(StringIO(s))

print bibtex_string_to_data("""
@article{article,
  author  = {Peter Adams},
  title   = {The title of the work},
  journal = {The name of the journal},
  year    = 1993,
  number  = 2,
  pages   = {201-213},
  month   = 7,
  note    = {An optional note},
  volume  = 4
}
""")

rawDescription: Pybtex does not recogonize bibtex entry

description: Pybtex does not recogonize bibtex entry


code:
class MyBase(object):
    def p(self):
        print [(attr, value) for attr, value in vars(self).items()
            if not callable(value) and not attr.startswith('__')]

rawDescription: How to access a class field values in its base class?

description: Access a class field values in its base class


code:
pyDes.des(key, [mode], [IV], [pad], [padmode])

from pyDes import *

data = "Please encrypt my data"
k = des("DESCRYPT", ECB, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
d = k.encrypt(data)
print "Encrypted: %r" % d
print "Decrypted: %r" % k.decrypt(d)

rawDescription: Encrypting Datas using python

description: Encrypting Datas using python


rawDescription: how to delete the "u and ' ' " before the of database table display by python

description: Delete the "u and ' ' " before the of database table display by python


code:
#!/usr/bin/python

result = raw_input("Enter some string: ")
print result.replace("J","")

rawDescription: What Python Module extracts certain chars from raw input?


code:
page_source = driver.page_source
driver.close()

soup = BeautifulSoup(page_source)
for tweet in soup.select("div.tweet div.content"):
    print tweet.p.text

rawDescription: Parse the html code for a whole webpage scrolled down


code:
some_list = [1,2,6,7]

is_in_the_list = False
for x in some_list:
  if x == 7: # 7 plays the role of the user input here
    is_in_the_list = True

if is_in_the_list:
  print "It is in the list"

rawDescription: Using Python, how can I tell the user they already entered a number in the list?


code:
lis = ['ALRAGUL','AKALH', "AL", 'H','ALH' ,'to7a','ALRAGULH']

def solve(lis):
    for x in lis:
        if x.startswith("AL") and x.endswith("H"):
            yield x[:2]
            if len(x)>4:
                yield x[2:-1]
            yield x[-1]
        elif x.startswith("AL"):
            yield x[:2]
            if len(x)>2:
                yield x[2:]
        elif x.endswith("H"):
            if len(x)>1:
                yield x[:-1]
            yield x[-1]
        else:
            yield x

new_lis = list(solve(lis))
print new_lis

rawDescription: Modify list in Python

description: Modify list in Python


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

driver = webdriver.Firefox()
driver.get('http://www.koovs.com/only-onlall-stripe-ls-shirt-59554.html?from=category-651&skuid=236376')

wait = WebDriverWait(driver, 10)
chart = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "a[size_chart]")))
chart.click()

driver.switch_to.window(driver.window_handles[-1])

print driver.current_url

driver.close()

rawDescription: Get JavaScript function call value using Selenium


code:
def predicateA(v):
    """
    some logic that eventually returns a True/False value
    """
    return True

print "Name:", predicateA.__name__
print "Doc:", predicateB.__doc__

rawDescription: how to obtain a function's name and/or a description given a pointer

description: Obtain a function's name and/or a description given a pointer


rawDescription: Extracting % occurrences in a string

description: Extracting % occurrences in a string


code:
def coocurrence (common_entities):

    com = defaultdict(int)

    # Build co-occurrence matrix
    for w1, w2 in combinations(sorted(common_entities), 2):
        com[w1, w2] += 1

    result = defaultdict(dict)
    for (w1, w2), count in com.items():
        if w1 != w2:
            result[w1][w2] = {'weight': count}
    return result

print coocurrence('abcaqwvv')

rawDescription: Python: creating undirected weighted graph from a co-occurrence matrix


code:
import copy
class A:
  def foo(self, x = [] ):
    x = copy.copy(x)
    x.append(1)
    self.x = x

a = A()
a.foo()
print a.x
# prints: [1]
a.foo()
print a.x
# prints: [1]   # !!! Much better =)
y = [1,2,3]
a.foo(y)
print a.x
# prints: [1, 2, 3, 1]
print y
# prints: [1, 2, 3]  #  !!!! My list is how I made it

rawDescription: Undesired python feedparser instantiation relic


code:
from pyparsing import White, Keyword, nestedExpr, lineEnd, Suppress

insource = """
annotation (
  foo1(k=3),
  bar(
    x=0.29,
    y=0,
    bar1(
    x=3, y=4),
    width=0.71,
    height=0.85),
  foo2(System(...))
"""

barRef = White(' \t') + Keyword('bar') + nestedExpr() + ',' + lineEnd

out = Suppress(barRef).transformString(insource)
print out

rawDescription: Removing/replacing multi-line code sections with python


code:
count = {}
no_match = []
domains = ["examplecartrading.com", "examplepensions.co.uk",
    "exampledeals.org", "examplesummeroffers.com"]

# Assume domains is the list of domain names ["examplecartrading.com", ...]
for domain in domains:
    # Extract the part in front of the first ".", and make it lower case
    name = domain.partition(".")[0].lower()
    found = set()
    for split in substrings_in_set(name, words):
        found |= set(split)
    for word in found:
        count[word] = count.get(word, 0) + 1
    if not found:
        no_match.append(name)

print count
print "No match found for:", no_match

rawDescription: Breaking a string into individual words(Python)


code:
from Queue import PriorityQueue

class MyPriorityQueue(PriorityQueue):
    def __init__(self):
        PriorityQueue.__init__(self)
        self.counter = 0

    def put(self, item, priority):
        PriorityQueue.put(self, (priority, self.counter, item))
        self.counter += 1

    def get(self, *args, **kwargs):
        _, _, item = PriorityQueue.get(self, *args, **kwargs)
        return item

queue = MyPriorityQueue()
queue.put('item2', 1)
queue.put('item1', 1)

print queue.get()
print queue.get()

rawDescription: How to put items into priority queues?

description: Put items into priority queues


code:
from xml.etree import ElementTree as ET

xml_string = '''<Books>
<Booktype List= "Story > Fiction > Young">
#here the rest of book text
</Booktype>
<Booktype List= "Science > Math > Young">
#here the rest of book text 2
</Booktype>
</Books>
'''

xml = ET.fromstring(xml_string)
for booktype in xml.findall('Booktype'):
    types = map(lambda x: x.strip(), booktype.get('List').split('>'))
    current = booktype
    for t in types:
        current = ET.SubElement(current, t)
    current.text = booktype.text
    booktype.text = ''
    del booktype.attrib['List']
print ET.tostring(xml,'utf-8')

rawDescription: variable in XML subelement

description: Variable in XML subelement


code:
for instance in instances:
    print instance['networkInterfaces'][0]['accessConfigs'][0]['natIP']

rawDescription: What's the simplest way to get the IP address of a Google Compute Engine Instance in Python?


code:
import pandas as pd
import os

Source = r'F:\backup\finalized 2'
OutputCSV = r'F:\Tom\Python\output.csv'

normSource = os.path.normpath(Source)

#find column names and delete 1,2,4, and more as 90th columns
#read one csv
names = pd.read_csv(os.path.join(normSource,'header.csv'), sep=",")
#column names to list
cols = names.columns.tolist()
print cols
#the first item has index 0, so you need delete 0, 1, 3, 89, 90, 91.. item

#delete 3 item
del cols[3]

#get 2,4,5,...89 item
cols = cols[2:89]
print cols

rawDescription: Concatenating multiple CSV files into a Dataframe and outputing to Master CSV


code:
for inner in mlist:
    print ''.join(inner)

rawDescription: get all elements from list of lists in rows of 5

description: Get all elements from list of lists in rows of 5


code:
with open('Text.txt', 'r+') as text_file:
    print 'The file content BEFORE writing content:'
    print text_file.read()
    text_file.write(' add this text to the file')
    print 'The file content AFTER writing content:'
    text_file.seek(0)
    print text_file.read()

rawDescription: Should we open the file more than once to read it?


code:
for ch in root:
    print next((x for x in ch.xpath('text()')), None)

rawDescription: Parsing XML using lxml, unable to get text when there is another child node


code:
for character in range(ord('a'), ord('z')):
    if "(++info++)" not in decoded_str:
        break
    decoded_str = decoded_str.replace("(++info++)", "(++info {0}++)".format(chr(character)), 1)

print decoded_str

rawDescription: Replace a pattern in python

description: Replace a pattern in python


code:
import mymath
m = mymath.math()
print '1 + 2 = %d' % m.add(1, 2)

rawDescription: C++ Python import class; call methods

description: C++ Python import class; call methods


code:
def permNums(inp, N=4):

    if len(inp[0]) == N: return inp # stopping criterion

    newInp = []
    for i in inp:
        for j in range(N):
            if j not in i: newInp.append( i+[j] )

    return permNums(newInp, N) # keep calling itself

print permNums([[]])

rawDescription: How to write a lot of nested 'for' loops (Recursively)

description: Write a lot of nested 'for' loops (Recursively)


code:
noBreak = True
i = 0
for i in range(0,29):
    if 6 < i < 8:   #1st if loop
        if i in [1,3,5,7]:        #2nd if loop
            print "here"
            noBreak = False
        else:
            print "there"
            noBreak = True      #line1
        print "Going to break out of loop"
        break
print i

rawDescription: break statement in for loop not working

description: Break statement in for loop not working


code: print all(set(e[0]).issubset(e[1]) for e in zip(list1, list2))

rawDescription: subset-by-subset list comparison in python

description: Subset-by-subset list comparison in python


code:
import urllib2
from bs4 import BeautifulSoup

urls = ['https://jobs-challp.icims.com/jobs/search?ss=1&searchKeyword=gis&searchCategory=&searchLocation=&latitude=&longitude=&searchZip=&searchRadius=20&in_iframe=1',
        'https://www.smartrecruiters.com/SpectraForce1/']

for url in urls:
    soup = BeautifulSoup(urllib2.urlopen(url))
    print 'Scraping {}'.format(url)
    for link in soup.find_all('a'):
        if 'GIS' in link.text:
            print '--> TEXT: ' + link.text.strip()
            print '--> URL:  ' + link['href']
            print ''

rawDescription: BeautifulSoup for job searching

description: BeautifulSoup for job searching


code:
import json
from wtforms import Field
import geojson
from shapely.geometry import asShape
from geoalchemy2.shape import to_shape, from_shape
from wtforms.widgets import html_params, HTMLString
from geoalchemy2.elements import WKTElement, WKBElement
from flask import render_template
class WTFormsMapInput(object):
    def __call__(self, field, **kwargs):
        options = dict(name=field.name, value=field.data, height=field.height, width=field.width,
                       geometry_type=field.geometry_type)

        return HTMLString(render_template("admin/admin_map.html", height=options['height'], width=options['width'],
                                          geolayer=self.geolayer(field.data), preview=False))

    def geolayer(self, value):
        if value is not None:
            html = ""
            subme = """var geojson = JSON.parse('%s');
                       editableLayers.addData(geojson);
                       update()
                       map.fitBounds(editableLayers.getBounds());"""
            # If validation in Flask-Admin fails on somethign other than
            # the spatial column, it is never converted to geojson.  Didn't
            # spend the time to figure out why, so I just convert here.
            if isinstance(value, (WKTElement, WKBElement)):
                html += subme % geojson.dumps(to_shape(value))
            else:
                html += subme % geojson.dumps(value)
            return html

class WTFormsMapField(Field):
    widget = WTFormsMapInput()

    def __init__(self, label='', validators=None, geometry_type=None, width=500, height=500,
                 **kwargs):
        super(WTFormsMapField, self).__init__(label, validators, **kwargs)
        self.width = width
        self.height = height
        self.geometry_type = geometry_type

    def _value(self):
        """ Called by widget to get GeoJSON representation of object """
        if self.data:
            return self.data
        else:
            return json.loads(json.dumps(dict()))

    def process_formdata(self, valuelist):
        """ Convert GeoJSON to DB object """
        if valuelist:
            geo_ob = geojson.loads(valuelist[0])
            self.data = from_shape(asShape(geo_ob.geometry))
        else:
            self.data = None

    def process_data(self, value):
        """ Convert DB object to GeoJSON """
        if value is not None:
            self.data = geojson.loads(geojson.dumps(to_shape(value)))
            print self.data
        else:
            self.data = None

rawDescription: AdminModelConvertor for Geometry Field (LON/LAT)


code:
with open('input.txt') as f:
    for line in f:
        line = line.strip()
        print line + ' ' + str(sum(map(int, line)))

rawDescription: how can i make a sum in a line of numbers in python?

description: Make a sum in a line of numbers in python


code:
from xml.dom.minidom import parse, parseString

data = '''<root>
<host starttime="1404053959" endtime="1404054014">
    <status state="up" reason="reset" reason_ttl="254"/>
    <address addr="ip" addrtype="ipv4">127.0.0.1</address>
</host>
<host starttime="1404053959" endtime="1404054014">
    <status state="up" reason="reset" reason_ttl="254"/>
    <address addr="ip" addrtype="ipv4">192.168.0.1</address>
</host>
</root>'''

#dom = parse('abc.xml')
dom = parseString(data)

for node in dom.getElementsByTagName('address'):
    print ' xml:', node.toxml()
    print 'type:', node.getAttribute('addrtype')
    print 'addr:', node.childNodes[0].data

rawDescription: How to create proper xml attribute with python parser

description: Create proper xml attribute with python parser


code:
text = {('idA','id2'): 'text',
        ('idB','id1'): 'text',
        ('idC','id3'): 'text',}

print sorted(text.iteritems(), key=lambda item: item[0][1])

rawDescription: sort Python dictionary with two keys

description: Sort Python dictionary with two keys


rawDescription: Python remove tabs and carriage lines from unicode list


rawDescription: How could I check if a number is a perfect square?


code:
for data in lineexpr.searchString(sample):
    print data.dump()
    print data.qty
    print

rawDescription: How to parse the following multi-dimensional xml-like data into a dataframe

description: Parse the following multi-dimensional xml-like data into a dataframe


code:
from django.conf import settings
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'my_django_project.settings'
print settings.SECRET_KEY

rawDescription: List quickly all settings of your Django project


code:
test = "somestring 363 1 46 17,363 1 34 17,401 3 8 14,"
from pyparsing import *
integer = Word(nums).setParseAction(lambda tokens:int(tokens[0]))
patt = Word(alphas)("desc") + \
    OneOrMore(Group(integer*4 + Suppress(',')))("numgroups")

print patt.parseString(test).asList()
print patt.parseString(test).dump()
res = patt.parseString(test)
print res.desc
print res.numgroups
for ng in res.numgroups:
    print sum(ng)

rawDescription: How can I build a regular expression which has options part

description: Build a regular expression which has options part


code:
import string

myFilename = "countries.txt"

myDict = {}

myFile = open (myFilename, 'r')

for line in myFile:
    splitLine = string.split (line)
    code = splitLine [-3]
    country = " ".join(splitLine[:-3])
    myDict[country] = code

print myDict

rawDescription: Easiest way to convert two columns to Python dictionary


code:
for story in json_obj["list"]["story"]:
   # each iteration story become this
   # story = {'title': {'$text': 'some title2'}}
   print story["title"]["$text"]

rawDescription: Python for loop syntax

description: Python for loop syntax


rawDescription: chunk of data into fixed lengths chunks and then add a space and again add them all as a string

description: Chunk of data into fixed lengths chunks and then add a space and again add them all as a string


code:
def s(): print "a" + chr(98)
dis.dis(s)
s()

rawDescription: Python parser/compiler vs. interpreter, and string concatenation compile-time vs. run-time?


code:
import re
data="""[{
  "tr": [{
      "name": "#For5Rs",
      "promoted_content": null,
      "query": "%23For5Rs",
      "events": null
  }, {
      "name": "Javed Bashir",
      "promoted_content": null,
      "query": "%22Javed+Bashir%22",
      "events": null
  }, {
      "name": "Milan Luthria",
      "promoted_content": null,
      "query": "%22Milan+Luthria%22",
      "events": null
  }, {
      "name": "Vidya Balan",
      "promoted_content": null,
      "query": "%22Vidya+Balan%22",
      "events": null
  }],
  "as_of": "2013-08-16T10:31:35Z",
  "created_at": "2013-08-16T10:20:41Z",
  "locations": [{
      "name": "India",
      "woeid": 23424848
  }]
"""

names=re.findall(r'(?<="name":\s").+(?=",[\s\S]+"promoted_content")',data)
print names

rawDescription: How to get list of values of a single key from JSON using Python

description: Get list of values of a single key from JSON using Python


code:
UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
LOWERCASE = "abcdefghijklmnopqrstuvwxyz"

def to_lower(s, trans=dict(zip(UPPERCASE, LOWERCASE))):
    return ''.join(trans.get(ch,ch) for ch in s)

print to_lower('This Is a TEST')   # => 'this is a test'

rawDescription: Creating my own string functions

description: Creating my own string functions


code:
sentinel = range(6)

def anything_but(values):
    values = map(str, values)
    i = raw_input()
    while i not in values:
        yield i
        i = raw_input()

lines = []
for i in anything_but(sentinel):
    lines.append(i)

print lines

rawDescription: Error in input: Python

description: Error in input: Python


code:
for i in range(1, 6):
    for c1 in itertools.combinations(c, i):
            c1 = set(c1)
            c2 = set(c) - c1
            print c1, c2

rawDescription: Two Combination Lists from One List

description: Two Combination Lists from One List


code:
for i, d in enumerate(records):
    print i, d

rawDescription: pymongo: possible to iterate result use next() and prev()?

description: Pymongo: possible to iterate result use next() and prev()


code: print self.elf.lbl.pos  #<-- ISSUE: This not working.

rawDescription: Issues with Scatter size and position

description: Issues with Scatter size and position


code:
def fun(list_1):
    data_frames = []
    for each_value in list1:
        # perform some operation and create a new data frame(pandas) named "new_data_frame" for each value in the list
        print each_value
        data_frames.append(new_data_frame)

return data_frames

rawDescription: what should I change in my function

description: What should I change in my function


code:
import argparse
parser = argparse.ArgumentParser(description = 'A simple description')

parser.add_argument('-f', action = 'store', dest =  'filename', required = False)
parser.add_argument('-n', action = 'store', dest = 'n', required = True, type=int)

arguments = parser.parse_args()

print arguments.n
print arguments.filename

rawDescription: Filtering integers out of python command line arguments


code:
def f(s): print s

f.__code__

rawDescription: What is a Python code object?

description: What is a Python code object


code:
stages = root.findall("./ep_150/stage")

print '\n\nreviews for id=p4645\n'

for stage in stages:
    for rev in stage.findall('review'):
        if rev.attrib['id']=='p4645':
            print stage.attrib['name'], rev.attrib['name']

rawDescription: How to get parent tag while using XPaths in xml.etree.elementree with python 2.6.4

description: Get parent tag while using XPaths in xml.etree.elementree with python 2.6.4


code:
import sys
from PyQt4 import QtCore, QtGui, QtWebKit

class WebPage(QtWebKit.QWebPage):
    def javaScriptConsoleMessage(self, msg, line, source):
        print '%s line %d: %s' % (source, line, msg)

url = 'http://localhost/test.html'
app = QtGui.QApplication([])
browser = QtWebKit.QWebView()
page = WebPage()
browser.setPage(page)
browser.load(QtCore.QUrl(url))
browser.show()
sys.exit(app.exec_())

rawDescription: Print Javascript Exceptions In A QWebView To The Console


code:
from pyparsing import makeHTMLTags, originalTextFor, SkipTo, Combine

# makeHTMLTags defines HTML tag patterns for given tag string
aTag,aEnd = makeHTMLTags("A")

# makeHTMLTags by default returns a structure containing
# the tag's attributes - we just want the original input text
aTag = originalTextFor(aTag)
aEnd = originalTextFor(aEnd)

# define an expression for a full link, and use a parse action to
# combine the returned tokens into a single string
aLink = aTag + SkipTo(aEnd) + aEnd
aLink.setParseAction(lambda tokens : ''.join(tokens))

# extract links from the input html
links = aLink.searchString(html)

# build list of strings for output
out = []
out.append(pre)
out.extend(['  '+lnk[0] for lnk in links])
out.append(post)

print '\n'.join(out)

rawDescription: Cannot prettify scraped html in BeautifulSoup


rawDescription: Python: Efficient workaround for multiprocessing a function that is a data member of a class, from within that class


code: print '\n'.join(row['hostname'] for row in config_params)

rawDescription: Get value from dict

description: Get value from dict


code:
from ftplib import *
from os import listdir
from os.path import getsize

ftp_dt = FTP(host,user,password)
ftp_pi = FTP(host,user,password)
print '\n- Ouverture de connection et logging : OK'

ftp_dt.cwd('public_html/soleil')
ftp_pi.cwd('public_html/soleil')

def func(content, li = [0], la = [], si = [0], memname = ['']):
    if name!=memname[0]:
        memname[0],li[0:1],la[:],si[0:1] = name,[0],[],[0]
    li[0] = li[0] + 1
    si[0] = si[0] + len(content)
    la.append(str(len(content)))
    if li[0]%8==0:
        print '  '.join(la) +\
              '   total: '+str(li[0])+' chunks,  '+str(si[0])+' bytes'
        la[:] = []
    f.write(content)

li_files = []
for name in ftp_dt.nlst():
    try:
        ftp_dt.size(name)
        if name not in ('.','..') and name[-4:]=='.pdf':
            li_files.append(name)
    except:
        pass

if li_files:
    for name in li_files:
        print '\n- Downloading  '+name

        with open('E:\\PDF\\DOWNS\\'+name,'wb') as f:
            ftp_pi.retrbinary('RETR '+name,func)

        if getsize('E:\\PDF\\DOWNS\\'+name)==ftp_dt.size(name):
            print '   OK !  Download of complete  '+repr(name)+'  SUCCEEDED'
        else:
            print '   FAILURE !! :  '+name+'  only partially downloaded'
else:
    print '\nThere is no PDF file in this FTP directory'

ftp_dt.quit()
ftp_pi.quit()

rawDescription: Downloading .pdf file from FTP using a Python Script


code:
name_company = soup.findAll("div", {"class" : "title"})
for name in name_company:
    print name.find('h2').string

rawDescription: Python: BeautifulSoup Findall Skip to the Next Tag


code:
today = datetime.datetime(2012, 1, 31, 15, 20)
for m in minutes(today, today + datetime.timedelta(minutes = 5)):
    print m

rawDescription: Python generator to return series of times


code:
somedict = {'foo': 'bar', 42: 'Life, the Universe and Everything', 'monty': 'python'}
for count, (key, value) in enumerate(somedict.iteritems(), 1):
    print key, value, count

rawDescription: how to get item index or number along with key,value in dict

description: Get item index or number along with key,value in dict


code:
c= [x[:] for x in [[None]*3]*4]
c= [x[:] for x in [[None]*3]*4]
print id(c[0])
c[0] = [0 for x in c[0]]
print id(c[0])              #c[0] is a new list object

rawDescription: Updating elements in nested list using 'for' won't work in python list


code:
from socket import *
s = socket()
s.connect(('127.0.0.1', 1234))

s.send('version\n')
print 'Client got:', s.recv(8192)
s.close()

rawDescription: Python: Passing Multiple Variables Across TCP Client/Server


code:
for k,d in enumerate(data):
    first = data[k].strip().replace(' ',',').strip()
    try:
        second = data[k+1].strip().replace(' ',',')
    except IndexError:
        # this is the last line, use the first value as second (maybe?)
        second = first
    print "%s,%s" % (first, second)

rawDescription: Combine results from regex to make x,y pairs


code:
import re
files = ["noi100k_0p55m0p3_fow71f",\
     "fnoi100v5_71f60s",\
     "noi100k_0p55m0p3_151f_560s",\
     "noi110v25_560s"]
for i in files:
    keyws = i.split("_")
    for j in keyws:
        match = re.match(r"noi(\w+)k|fnoi(\w+)v(\w+)|noi(\w+)v(\w+)",j)
        if match:
            results = match.group(1) or match.group(2) or match.group(4)
            print "Results :", results

rawDescription: Regular expression match a or b pattern

description: Regular expression match a or b pattern


code:
from multiprocessing.pool import ThreadPool

def foo(word, number):
    print (word * number)
    r[(word,number)] = number
    return number

words = ['hello', 'world', 'test', 'word', 'another test']
numbers = [1,2,3,4,5]
pool = ThreadPool(5)
results = []
for i in range(0, len(words)):
    results.append(pool.apply_async(foo, args=(words[i], numbers[i])))

pool.close()
pool.join()
results = [r.get() for r in results]
print results

rawDescription: How to obtain the results from a pool of threads in python?

description: Obtain the results from a pool of threads in python


code:
from weakref import ref
from time import sleep

class Person4:
    '''Represents a person'''
    population = 0

    def __init__(self, name):
        '''Initialize the person's data'''
        self.name = name
        print 'Initializing %s'% self.name

        #When the person is created they increase the population
        Person4.population += 1

        self._wr = ref(self, lambda wr, name=self.name: Person4_finalizer(name))

def Person4_finalizer(name):
        '''I am dying'''
        print '%s says bye' % name

        Person4.population -= 1

        if Person4.population == 0:
            print 'I am the last one'
        else:
            print 'There are still %d left' % Person4.population

p1 = Person4("one")
p2 = Person4("two")
p3 = Person4("three")

del p2
del p3
sleep(5)

rawDescription: __del__ method being called in python when it is not expected


code: print 0x0225DF30

rawDescription: What is the second number in a python weak reference's repr?


code:
def fun(a, b, c):
    d = locals()
    e = d
    print e
    print locals()

fun(1, 2, 3)

rawDescription: How to iterate over function arguments

description: Iterate over function arguments


code:
for i, n in enumerate(shoestring):
    if n.isalpha():
        continue
    else:
        print n, i

rawDescription: Python - Why is this function not returning the index for white space?


code:
def print_sorted(d, depth=0):
    if isinstance(d, dict):
        for k in sorted(d):
            print "  " * depth, k
            print_sorted(d[k], depth+1)
    else:
        print "  " * depth, d
print_sorted(result)

rawDescription: Sorting/Combining a list of lists

description: Sorting/Combining a list of lists


code:
prompt = "> "

print "Welcome to the converter. What would you like \
to convert? (temp or distance)"
choice = raw_input(prompt)

while (choice != "temp" and choice != "distance"):
    print "Sorry, that's not an option"
    choice = raw_input(prompt)
if choice == "temp":
    print "temp"
elif choice == "distance":
    print "distance"

rawDescription: Python - Using a while loop with an if/elif statement


code:
import json

# Load sh from JSON file or create a new dictionary if it doesn't exist
try:
    sh = json.load( open("storage.json","r") )
except IOError:
    sh = {}

sh["foo"] = (1,2,3,4)
sh["bar"] = "I like spam"

# Save sh to JSON file
json.dump( sh, open("storage.json","w") );

# Reload it
sh = json.load( open("storage.json","r") )

print sh["foo"]
print sh.keys()

rawDescription: Modify variables in a different module

description: Modify variables in a different module


code:
from collections import OrderedDict
counter = OrderedDict()
words = ['cat', 'apple', 'apple', 'boat', 'cat', 'apple', 'cat']
for word in words:
    counter[word] = counter.get(word, 0) + 1
print [key for key, count in counter.items() if count == 3]

rawDescription: Printing items in a list that occur a certain number of times?


code:
import numpy as np

arr1 = np.arange(8).reshape(2, 4)
arr2 = np.arange(10).reshape(2, 5)
np.savez('mat.npz', name1=arr1, name2=arr2)

data = np.load('mat.npz')
print data['name1']
print data['name2']

rawDescription: NumPy save some arrays at once

description: NumPy save some arrays at once


code:
class Room:
    def __init__(self, name):
        self.name = name
        self.furniture = []

    def add_chair(self):
        self.furniture.append(Chair(self))

    def __str__(self):
        return '{} with {}'.format(self.name, self.furniture)

class Chair:
    def __init__(self, room):
        self.room = room
    def __str__(self):
        return 'Chair in {}'.format(self.room.name)

r = Room('Kitchen')
r.add_chair()
r.add_chair()

print r
print r.furniture[0]

rawDescription: Creating an object with a reference to the object that created it


code:
print mm[24:]
mm[24:] = "sea.  "
print mm[:]

rawDescription: Python Random Access File

description: Python Random Access File


code:
def pasteEvent(self, text):
    # this is your paste event, whenever any text is pasted in the
    # 'lineEdit_URL', this method gets called.
    # 'text' is the newly pasted text.

    print text

rawDescription: PyQt QLineEdit and 'paste' event?

description: PyQt QLineEdit and 'paste' event


code:
for name, number, weight in elements:
    print name, ':', number, weight

rawDescription: Using for loops with nested loops in python


code:
print re.search(r'(\s)\w+', words).groups()
print re.search(r'(\s)\w+', words).group(1)

rawDescription: Difference between re.search() and re.findall()


code:
def get_biggest_c_value(key_a):
    values = []
    for group_b in key_a.itervalues():
        for key_b in group_b.itervalues():
            for group_c in key_b.itervalues():
                for value in group_c.itervalues():
                    values.append(value)
    return max(values)

filtered = sorted(data["GROUP-A"].items(), key=lambda kv: get_biggest_c_value(kv[1]))
print filtered

rawDescription: Challenge: create a list by sorting a bunch of nested dict


code:
l = map(str,range(0,10))
for i in range(10):
    print ' '.join(l[:len(l)-i]).rjust(20)

rawDescription: How can I print this?

description: Print this


code:
from pyPdf import PdfFileReader
import pyPdf
def _setup_page_id_to_num(pdf, pages=None, _result=None, _num_pages=None):
    if _result is None:
        _result = {}
    if pages is None:
        _num_pages = []
        pages = pdf.trailer["/Root"].getObject()["/Pages"].getObject()
    t = pages["/Type"]
    if t == "/Pages":
        for page in pages["/Kids"]:
            _result[page.idnum] = len(_num_pages)
            _setup_page_id_to_num(pdf, page.getObject(), _result, _num_pages)
    elif t == "/Page":
        _num_pages.append(1)
    return _result
def outlines_pg_zoom_info(outlines, pg_id_num_map, result=None):
    if result is None:
        result = dict()
    if type(outlines) == list:
        for outline in outlines:
            result = outlines_pg_zoom_info(outline, pg_id_num_map, result)
    elif type(outlines) == pyPdf.pdf.Destination:
        title = outlines['/Title']
        result[title.split()[0]] = dict(title=outlines['/Title'], top=outlines['/Top'], \
        left=outlines['/Left'], page=(pg_id_num_map[outlines.page.idnum]+1))
    return result

# main
pdf_name = 'document.pdf'
f = open(pdf_name,'rb')
pdf = PdfFileReader(f)
# map page ids to page numbers
pg_id_num_map = _setup_page_id_to_num(pdf)
outlines = pdf.getOutlines()
bookmarks_info = outlines_pg_zoom_info(outlines, pg_id_num_map)
print bookmarks_info

rawDescription: How to get bookmark's page number

description: Get bookmark's page number


code:
import server

@server.data_hander
def on_acc_data(alpha, beta, gamma):
    print alpha, beta, gamma

server.go()

rawDescription: How can I read the accelerometer in my windows tablet with python?

description: Read the accelerometer in my windows tablet with python


code:
import file
# ...
try:
    v = file.varName
except AttributeError:
    print 'var not found'

rawDescription: how to determine if a variable exists in another python file

description: Determine if a variable exists in another python file


code:
import os

folder = 'zeFolder'
brothers = ['brotherOne', 'brotherTwo']

for dirpath, dirnames, filenames in os.walk('/'):
    if folder in dirnames and all(brother in dirnames for brother in brothers):
        print 'matches on %s' % os.path.join(dirpath, 'zeFolder')

rawDescription: How can I get a specific folder with a specific folders sibling in python

description: Get a specific folder with a specific folders sibling in python