code:
from StringIO import StringIO
from lxml import etree
from lxml.etree import Element

data = """<xml>
   <items>
      <pie>cherry</pie>
      <pie>apple</pie>
      <pie>chocolate</pie>
  </items>
</xml>"""

stream = StringIO(data)
context = etree.iterparse(stream, events=("start", ))

for action, elem in context:
    if elem.tag == 'items':
        items = elem
        index = 1
    elif elem.tag == 'pie':
        item = Element('item', {'id': str(index)})
        items.replace(elem, item)
        item.append(elem)
        index += 1

print etree.tostring(context.root)

rawDescription: python lxml using iterparse to edit and output xml

description: Python lxml using iterparse to edit and output xml


code:
for line in html.splitlines():
    cleaned = line.rsplit('@', 1)[0]
    print cleaned

rawDescription: Python - how to delete all characters in all lines after some sign?


code: print colors.get((255, 255, 255), 0)  # No. of white pixels

rawDescription: Pixel Loading and Evaluating with PIL

description: Pixel Loading and Evaluating with PIL


rawDescription: For Loop Over Dictionaries in List (Python 2.7.6)


code:
import functools
def None_is_dumb_and_does_not_deserve_to_enter_this_function(func):
    @functools.wraps(func)
    def new_func(*args,**kwargs):
        if None in args:
            raise ValueError("None's aren't welcome here")
        return func(*args,**kwargs)
    return new_func

@None_is_dumb_and_does_not_deserve_to_enter_this_function
def foo(a,b,c):
    """Docstring"""
    print a,b,c

foo(1,2,3)
print foo.__doc__
foo(None,'bar','baz')

rawDescription: How do you check if any of the arguments passed to function is None?

description: Check if any of the arguments passed to function is None


code:
from lxml.html import fromstring
from lxml import etree

html = '''

Hello world

Lorem ipsum

Bold Text
'''

l = []
doc = fromstring(html)
for el in doc.xpath(".//h1|.//b"):
    i = etree.Element(el.tag)
    i.text, i.tail = el.text, el.tail
    l.append(etree.tostring(i))

print ''.join(l)

rawDescription: Remove a bad tag completely with html5lib.sanitizer


code:
import urllib
url = 'https://www.google.com/'
page = urllib.urlopen(url)
print page.read()
#page.read is the url's source code, so you would print the source  code here.

rawDescription: Extracting text from webpage, processing with Perl/Python, then rebuilding the page with links added


rawDescription: Print out the whole directory tree

description: Print out the whole directory tree


code:
from itertools import product, islice

letters = 'ABCDE'

def make_generator(first_letter):
    return ((first_letter,) + rest
            for k in range(9)
            for rest in product(letters, repeat=k))

for letter in letters:
    print map(''.join, islice(make_generator(letter), 20)), '...'

rawDescription: fractionate a combination generator to distribute it with celery

description: Fractionate a combination generator to distribute it with celery


code:
from itertools import product

Colour = ['Black','White'] # so Color[0] == 'Black'
Type = ['Car','Bike']

#make that list
nested = product((0,1), repeat=4) # use numbers not strings
print [(Colour[a], Type[b], Colour[c], Type[d]) for (a,b,c,d) in nested]

rawDescription: Listing out binary combinations and mapping to data


code:
for p in Person.objects.all():
    print "person", p
    for h in p.home_address_set.all():
         print " home", h
         for ph in h.phone_set.all():
             print "  phone", ph

rawDescription: printing a list of persons with more than one home, each home with more than one phone number

description: Printing a list of persons with more than one home, each home with more than one phone number


code:
for person in db:
    print "Name:", person[0][0]
    print " ".join(s.strip() for s in person[0][1:])
    print

rawDescription: How can I scrape data from a text table using Python?

description: Scrape data from a text table using Python


code:
import traceback
import inspect
import gc

def giveupthefunc(frame):
    code  = frame.f_code
    globs = frame.f_globals
    functype = type(lambda: 0)
    funcs = []
    for func in gc.get_referrers(code):
        if type(func) is functype:
            if getattr(func, "func_code", None) is code:
                if getattr(func, "func_globals", None) is globs:
                    funcs.append(func)
                    if len(funcs) > 1:
                        return None
    return funcs[0] if funcs else None

def AssertTrue(expr, reason=None):
    print traceback.format_stack()[-2]
    frame = inspect.currentframe().f_back
    func = giveupthefunc(frame)
    if func:
        source = inspect.getsourcelines(func)
        i = source[1]
        for line in source[0]:
            print i, ":", line,
            i += 1

def my_fun():
    AssertTrue(1 == 2,
             reason='One is not equal to two')

my_fun()

rawDescription: Python multi-line expressions and stack trace


code:
for key, value in your_dict.items():
    if key not in your_blacklisted_set:
        print value

rawDescription: Retain all entries except for one key python


code:
for key in combo.viewkeys() & numb:
    print combo[key]

rawDescription: Printing the values of a dictionary when its key is equal to the value of a list element


code:
class ListParser:

 def __init__(self, s):
  self.str = s.split("\n")
  print self.str
  self.answer = []

 def parse(self):
  self.nextLine()
  self.topList()
  return

 def topList(self):
  while(len(self.str) > 0):
   self.topListItem()

 def topListItem(self):
  l = self.nextLine()
  print "TOP: " + l
  l = self.nextLine()
  if l != '':
   raise Exception("expected blank line but found '%s'" % l)
  sub = self.sublist()

 def nextLine(self):
  return self.str.pop(0)

 def sublist(self):
  while True:
   l = self.nextLine()
   if l == '':
    return # end of sublist marked by blank line
   else:
    print "SUB: " + l

parser = ListParser(s)
parser.parse()
print "done"

rawDescription: Parsing multilevel text list

description: Parsing multilevel text list


code:
import csv

with open("output.csv", "w") as f:
    w = csv.writer(f)
    w.writerow([1,2,3,4])
    w.writerow([5,6,7,8])

with open("output.csv", "rb") as f:
    print repr(f.read())

rawDescription: Many tutorials on writing csv files have the mode set to 'wb', why?


code: import numpy; print numpy.get_include()

rawDescription: Reading many values from numpy C API

description: Reading many values from numpy C API


code: for sum_str, sum_val in IterAllSums([1,2,3,4]) : print sum_str, sum_val

rawDescription: Permutations in python 2.5.2

description: Permutations in python 2.5.2


code:
Base = declarative_base()
database='mysql://jgb:XXX@localhost/'
Changes = table_factory (name = "Changes",
                         tablename = "changes",
                         schemaname = 'a')
UPeople = table_factory (name = "UPeople",
                         tablename = "upeople",
                         schemaname = 'b')
engine = create_engine(database)
Base.prepare(engine)
Session = sessionmaker(bind=engine)
session = Session()
q = session.query(UPeople.id).join(Changes,
                                   UPeople.id == Changes.changed_by)
print q

rawDescription: SQLAlchemy: query using tables in two MySQL schemas


code:
myfile = open("readnwrite.txt", "r").read()
myfile
print myfile

rawDescription: Opening and reading txt files

description: Opening and reading txt files


code:
def split_on_parts(s, *parts):
    total = 0
    buildstr = []
    for p in parts:
        buildstr.append(s[total:total+p])
        total += p
    return buildstr

s = 'hello world'
print split_on_parts(s, 3, 3, 3, 3)
print split_on_parts(s, 4, 3, 4)

rawDescription: Split string into array with many char pro items


code:
mylist = [{"greeting":"HELLO WORLD!"}, {"greeting":"Hello Mars"}, {"greeting":"Hello World!!!"}, {"greeting":"hello world"}]
unique(mylist, ['greeting'])

print mylist

rawDescription: How to uniquefy a list of dicts based on percentage similarity of a value in the dicts

description: Uniquefy a list of dicts based on percentage similarity of a value in the dicts


code:
def print_message():
    print "This is my very special script!"

if __name__ == "__main__":
    print_message()

rawDescription: How to distribute my Python/shell script?

description: Distribute my Python/shell script


code:
from xlrd import open_workbook

book = open_workbook('forum.xlsx')
sheet = book.sheet_by_index(3)

# read header values into the list
keys = [sheet.cell(0, col_index).value for col_index in xrange(sheet.ncols)]

dict_list = []
for row_index in xrange(1, sheet.nrows):
    d = {keys[col_index]: sheet.cell(row_index, col_index).value
         for col_index in xrange(sheet.ncols)}
    dict_list.append(d)

print dict_list

rawDescription: XLRD/Python: Reading Excel file into dict with for-loops


rawDescription: removing duplicate content python

description: Removing duplicate content python


code:
class attr_block_meta(type):
    def __new__(meta, cname, bases, dctry):
        def _setattr(self, name, value):
            if not hasattr(self, name):
                raise AttributeError("'" + name + "' not an attibute of " + cname + " object.")
            object.__setattr__(self, name, value)

        cls = type.__new__(meta, cname, bases, dctry)
        cls.__setattr__ = _setattr
        return cls

class ImPoint(object):
    __metaclass__ = attr_block_meta
    def __init__(self, q, z):
        object.__setattr__(self, 'q', q)
        object.__setattr__(self, 'z', z)

point = ImPoint(1, 2)
print point.q, point.z
point.w = 3  # Raises AttributeError

rawDescription: How to write metaclass which would prevent creating new attributes after __init__()?

description: Write metaclass which would prevent creating new attributes after __init__()


code:
#!/usr/bin/python

import sys, urllib
import os

def post_message(url, recipient, message_txt):
    """ post an email message to the given url
    """

    if not url:
        print "Invalid url."
        print "usage: mta2django.py url <recipient>"
        sys.exit(64)

    data = {'mail': message_txt}
    if recipient and len(recipient) > 0:
        data ['recipient'] = recipient

    try:
        result = urllib.urlopen(url, urllib.urlencode(data)).read()
    except (IOError,EOFError),e:
        print "error: could not connect to server",e
        sys.exit(73)

    try:
        exitcode, errormsg = result.split(':')
        if exitcode != '0':
            print 'Error %s: %s' % (exitcode, errormsg)
            sys.exit(int(exitcode))
    except ValueError:
        print 'Unknown error.'
        sys.exit(69)

    sys.exit(0)

if __name__ == '__main__':
    # This gets called by the MTA when a new message arrives.
    # The mail message file gets passed in on the stdin

    # Get the raw mail
    message_txt = sys.stdin.read()

    url = ''
    if len(sys.argv)>1:
        url = sys.argv[1]

    recipient = ''
    # If mta2django is executed as external command by the MTA, the
    # environment variable ORIGINAL_RECIPIENT contains the entire
    # recipient address, before any address rewriting or aliasing
    recipient = os.environ.get('ORIGINAL_RECIPIENT')

    if len(sys.argv)>2:
        recipient = sys.argv[2]

    post_message(url, recipient, message_txt)

rawDescription: Process received email with Python or Django?


code:
mydictvar = {'general': {'UserKey': 'thisisatestkey'}}
print "Your key is: {0[general][UserKey]}".format(mydictvar)

rawDescription: Python 2.7.8: Printing a sub-dictionary value?


code:
u = User.objects.get(username='me')
if u.userprofile.has_role('admin'):
    print 'I have the powah!'

rawDescription: Check for multiple values in a M2M relationship in Django


code:
from itertools import product

axis_labels = ['p','q','r']
dimension = len(axis_labels)

for comb in product([0, 1], repeat=dimension):
    label = ''.join([axis_labels[i] for i in range(dimension) if comb[i]])
    if not label: label = '0'
    print label, list(comb)

rawDescription: For-loop to fill in node positions in an array for a cube


rawDescription: python-taking part of the text stored within a combo box

description: Python-taking part of the text stored within a combo box


code:
bills = raw_input("Enter the value for each seat, separated by a space: ").split()
print sum(map(float, bills))

rawDescription: Addition of raw_input numbers in python

description: Addition of raw_input numbers in python


code:
seq = [5,4,-3,2,1]
goal = 7
limit = 1000
d = {0: []}
for item in seq:
    next_d ={}
    for intermediary_total, path in d.iteritems():
        for candidate in [-item, item]:
            next_total = intermediary_total + candidate
            if abs(next_total) <= limit:
                next_d[next_total] = path + [candidate]
    d = next_d

if goal in d:
    print d[goal]
else:
    print "no solution found"

rawDescription: Python set sequence of numbers to add or subtract for certain result


code:
for i in [1,2,3]:   # imagine this is the event list
    if i == 4:
        sys,exit()

if i == 3:
    print "i==3"

for i in []:        # next time round the while loop, the event list is empty
    if i == 4:
        sys,exit()

if i == 3:          # but you're still processing this event again!
    print "i==3"

rawDescription: Manage keydown in pygame

description: Manage keydown in pygame


code:
s = '\xc2\xa0'    # UTF8 encoded string
s.decode('utf8')
import unicodedata
print unicodedata.name(s.decode('utf8'))    # decode to unicode and lookup name

rawDescription: Most intelligent way of reading some data into Python


code:
import hashlib
import os

def digest_with_progress(filename, chunk_size):
    read_size = 0
    last_percent_done = 0
    digest = hashlib.md5()
    total_size = os.path.getsize(filename)

    data = True
    f = open(filename)
    while data:
        # Read and update digest.
        data = f.read(chunk_size)
        read_size += len(data)
        digest.update(data)

        # Calculate progress.
        percent_done = 100 * read_size / total_size
        if percent_done > last_percent_done:
            print '%d%% done' % percent_done
            last_percent_done = percent_done
    f.close()
    return digest.hexdigest()

rawDescription: Getting percentage complete of an md5 checksum


code:
f1 = ['BLUE GROUSE', 'PLAIN TITMOUSE', 'RUBY-THR. HUMMINGBIRD']
f2 = {'BLUE GROUSE' : 'DUSKY GROUSE',
      'PLAIN TITMOUSE' : ['OAK TITMOUSE', 'JUNIPER TITMOUSE'],
      'RUBY-THR. HUMMINGBIRD' : 'RUBY-THROATED HUMMINGBIRD'
     }

for k,v in zip(f2.keys(), f2.values()):
    if type(v) == str:
        f1[f1.index(k)] = v
    else:
        f1[f1.index(k)] = v[0]
        f1.extend(v[1:])

f1.sort()
print "Standardized list:", f1

rawDescription: Replace value in column with multiple dictionary values


code:
def weakness(self, poketype):
    if poketype == 'fire':
        print 'strong against grass'
        print 'weak against water'

    elif poketype == 'water':
        print 'strong against first'
        print 'weak against grass'

    elif poketype == 'grass':
        print 'strong against water'
        print 'weak against fire'

rawDescription: Error with a simple program, printing error (python)


code:
from graphics import *

clickCounter = 0

def click(event):
    global clickCounter
    clickCounter += 1
    print clickCounter

def sillytask():
    print 'Just waiting around'
    global clickCounter
    if clickCounter >= 10:
        win.quit()
    else:
        win.after(1000, sillytask)

win = GraphWin("My Window", 100, 100)
win.bind('<Button-1>', click)
win.after(1000, sillytask)
win.mainloop()

rawDescription: Python: Counting and printing mouse clicks (Zelle's Graphics)


code:
print out.Sentence.keys()
print out.Sentence['Prefix']

rawDescription: Getting a data structure equivalent to asXML() in pyparsing?


code:
numberPolynomials = 3
F = {}
for n in range(1, numberPolynomials+1):
    F[n] = np.poly1d(f[n-1])
    F[(n, 2)] = np.polyder(F[n])
    print F[(n, 2)]

rawDescription: write a loop to represent each polynomial from an array for n polynomials

description: Write a loop to represent each polynomial from an array for n polynomials


code:
def comp_str(xj, xi):
    if xj == xi:
        return 'equal'
    elif float(xj) < float(xi):
        return 'yes'
    else:
        return 'no'

compare = [ comp_str(xj, xi) for xj, xi in zip(x[:-1], x[1:]) ]
print compare

# -> ['equal', 'equal', 'yes', 'no', 'yes', 'no', 'yes', 'yes', 'yes']

rawDescription: Finding Incrementing value from list

description: Finding Incrementing value from list


code:
import test1

def service_func():
    print 'service func'

if __name__ == '__main__':
    # service.py executed as script
    # do something
    service_func()
    test1.some_func()

rawDescription: What is the best way to call a python script from another python script?


code:
from __future__ import division
from sys import exit
history_begins = 1
history_ends = 16
historyjobs = []
targetjobs = []

def quickzh(zhlistsub,
            historyjobs=historyjobs):
    rev = reversed(zhlistsub)
    i = next(rev)
    num   =  historyjobs[i][1]
    denom = 1
    hfirst  = num
    li = [historyjobs[i][2]]
    for i in rev:
        x = historyjobs[i][1]
        num += x
        denom += 1
        if x > 0.9 * (num / denom):
            li.append(historyjobs[i][2])
        else:
            break
    li.reverse()
    return hfirst,li

def listcreate(hfirst,s,
               historyjobs=historyjobs,targetjobs=targetjobs):
    with open('outputfile.txt','a') as f1:
        j = targetjobs[0][2]
        try:
            a,c,d,g = sum(s)/len(s), max(s), min(s), len(s)
            e1  = abs(j-a)
            er1 = e1/j
            h   = abs(hfirst-targetjobs[0][1])
            # historyjobs[-1][1] is convertsub
            # targetjobs[0][1]   is targetsub
            f1.write('%s\t%s\t%s\t%s\t%s\t%s\t%s\t' %
                     (j, round(a,2), round(er1,3), c, d, g, h))
            f1.write("%s\t%s\t%s\t%s\n" %
                     tuple("good" if er1 < x else "bad" for x in (0.2,0.3,0.4,0.5)))
        except ZeroDivisionError :
            print 'dem 0'
        else:
            print '0'
            f1.close()

def new(inputfile,history_ends,
        historyjobs=historyjobs,targetjobs=targetjobs):
    while 1:

        # checking the file
        with open(inputfile,'r') as f:
            for line in f:
                job = map(int,line.split())
                (historyjobs if job[0]  < history_ends
                 else targetjobs).append(job)

        if len(targetjobs) != 1:
            k = 0
            zhlistsub = [i for i, element in enumerate(historyjobs)
                         if element[3:6] == targetjobs[k][3:6] ]
            if zhlistsub:
                listcreate(*quickzh(zhlistsub))
            history_ends = targetjobs[1][0]
            del targetjobs[:]
            del historyjobs[:]
        else:
            break

new('toto.txt',history_ends)

rawDescription: To make the following code run faster?

description: To make the following code run faster


code:
import numpy as np
import scipy.ndimage

data = 256 * np.random.random((10,10))
data = data.astype(np.uint8)

rotated = scipy.ndimage.rotate(data, 60, cval=255)
print rotated

rawDescription: filling desired color while rotating the image in scipy

description: Filling desired color while rotating the image in scipy


code:
a = [0, np.array([4,5,6])]
store('my_data.h5', 'a', a)
print read('my_data.h5', 'a')

rawDescription: Storing large unstructured list in Python


code:
n = "o"  # you can set this too, but this is optional
def find_letter(lst):
    # do not set a value to lst in here

    if not lst:
        return 0

    elif lst[0] == n:  # You checked first element in here
        return True

    elif find_letter(lst[1:]):  # since you checked the first element, skip it and return the orher elements of the list
        return True

    else:
        return False

lst = ['o','hello', 1]
print find_letter(lst)

rawDescription: Python - check if a letter is in a list

description: Python - check if a letter is in a list


code:
class Meta(type):
    @staticmethod
    def __call__():
        return '__call__'

class Foo(object):
    __metaclass__ = Meta

    @staticmethod
    def bar():
        return 'bar'

print Foo()
#__call__

rawDescription: decorate __call__ with @staticmethod

description: Decorate __call__ with @staticmethod


code:
# I use ntpath because I'm running on Linux.
# This way is more robust if you know you'll be dealing with Windows paths.
# An alternative is to import from os.path then linux filenames will work
# in Linux and Windows paths will work in Windows.
from ntpath import basename, splitext

# Use r"" strings as people rightly point out.
# "\n" does not do what you think it might.
# See here: https://docs.python.org/2.0/ref/strings.html.
docs = [r'C:\Design\dw\file4.doc',
        r'C:\light\PDF\downloads\list.doc',
        r'C:\Design\Dq\file4g.doc',
        r'C:\Design\Dq\file4r.doc',
        r'C:\Design\Dq\file4k.doc',
        r'C:\Design\Dq\ole.doc',
        r'C:\Design/Dq/test1.doc',  # test a corner case
        r'\\some_unc_machine\Design/Dq/test2.doc',  # test a corner case
        r'C:\GE\easy\file\os_references(9).doc',
        r'C:\mate\KLO\Market\BIZ\KP\who\Documents\REF.doc']

# Please use meaningful variable names:
basenames = []

for doc_path in docs:

    # Please don't reinvent the wheel.
    # Use the builtin path handling functions.
    # File naming has a lot of exceptions and weird cases
    # (particularly on Windows).
    file_name = basename(doc_path)
    file_basename, extension = splitext(file_name)
    if extension == ".doc":
        basenames.append(file_basename)

print basenames

rawDescription: Python, string slicing (getting file names from a list of file locations)


code:
from netaddr import IPNetwork
print str(IPNetwork('1.2.3.4/255.255.255.0').cidr)

rawDescription: Get network address and network mask in Python


code:
import socket
from random import random
from time import sleep

class ServerObject:

    def __init__(self,host_address,port):

        self._host_address = host_address
        self._s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self._s.bind((self._host_address,port))

    def handshake(self):

        print "Server Started. Awaiting Connection"

        while True:
            _data, _addr = self._s.recvfrom(1024)
            if str(self._s.recvfrom(1024)[0]) == 'marco':
                break

        print 'marco recieved. sending polo...'

        while True:
            self._s.sendto('polo',_addr)
            if str(self._s.recvfrom(1024)[0]) == 'confirm':
                break
            sleep(.5)

        print 'connection verified'

        self._addr = _addr

        return True

    def send(self,data):

        self._s.sendto(str(data),self._addr)

    def recieve(self,mode = 0):

        _data, _addr = self._s.recvfrom(1024)

        if mode == 0:
            return str(_data)
        if mode == 1:
            return int(_data)
        if mode == 2:
            return float(_data)
        if mode == 3:
            return tuple(_data)

    def change_port(self,port):

        self._s.bind((self._host_address,port))

    def close(self):
        self._s.close()
        print '_socket closed_'

if __name__ == '__main__':

    host = '127.0.0.1'

    talk = ServerObject(host,6003)
    talk.handshake()

rawDescription: How to make multiple python programs communicate in this situation?

description: Make multiple python programs communicate in this situation


code:
PeoplesNames = [];
while len(PeoplesNames) < 3:
    person = input('Enter your name: ')
    PeoplesNames.append(person);
print PeoplesNames

rawDescription: Using a while loop in python until the list is full


code:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from functools import partial

class Box(BoxLayout):
    buttons={}
    def __init__(self, **kw):
        super(Box, self).__init__(**kw)
        for i in range(10):
            self.buttons[str(i)]=Button(text=str(i))
            self.add_widget(self.buttons[str(i)])
            self.buttons[str(i)].bind(on_release=partial(self.ping, str(i)))
    def ping(self, *arg):
        print 'test',str(arg)
class My(App):
    def build(self):
        return Box()
My().run()

rawDescription: Create variables dynamically inside loop in python


code: print 'Country: {}'.format(out[3])

rawDescription: Python splitting values from urllib in string


code:
import random
SEED = 448

myList = [ 'list', 'elements', 'go', 'here' ]
random.seed(SEED)
random.shuffle(myList)

print myList

rawDescription: Python random sequence with seed

description: Python random sequence with seed


code:
import itertools

def hasNoRepeats(matching):
    flattenedList = list(itertools.chain.from_iterable(matching))
    flattenedSet = set(flattenedList)
    return len(flattenedSet) == len(flattenedList)

def getMatchings(users, groupSize=2):
#   Get all possible pairings of users
    pairings = list(itertools.combinations(users, groupSize))
#   Get all possible groups of pairings of the correct size, then filter to eliminate groups of pairings where a user appears more than once
    possibleMatchings = filter(hasNoRepeats, itertools.combinations(pairings, len(users)/groupSize))
#   Select a series of the possible matchings, making sure no users are paired twice, to create a series of matching cycles.
    cycles = [possibleMatchings.pop(0)]
    for matching in possibleMatchings:
        # pairingsToDate represents a flattened list of all pairs made in cycles so far
        pairingsToDate = list(itertools.chain.from_iterable(cycles))
        # The following checks to make sure there are no pairs in matching (the group of pairs being considered for this cycle) that have occurred in previous cycles (pairingsToDate)
        if not any([pair in pairingsToDate for pair in matching]):
            # Ok, 'matching' contains only pairs that have never occurred so far, so we'll add 'matching' as the next cycle
            cycles.append(matching)
    return cycles

# Demo:

users = ["A","B","C","D","E","F"]

matchings = getMatchings(users, groupSize=2)

for matching in matchings:
    print matching

rawDescription: Optimal strategy for choosing pairs from a list of combinations


code:
import sys
f = open("test.out", 'w')
sys.stdout = f
print "test"
f.close()

rawDescription: How to write output of terminal to file

description: Write output of terminal to file


code:
import re

su = '''hello world
xxxx hello world yyyy
<!--
_+]!yuyu*@&^}@?!hello world[@%]^@}$[*a*$&^!@(&bc??,=hello'''

print su

pat = '([a-z]+)(?![a-z])(?<!world)'
print "\nexcluding all the words 'world'\n%s" % pat
print re.findall(pat,su)

pat = '([a-z]+)(?![a-z])(?<!\Ahello world)'
print "\nexcluding the word 'world' of the starting string 'hello world'\n%s" % pat
print re.findall(pat,su)

pat = '([a-z]+)(?![a-z])(?<!hello world)'
print "\nexcluding all the words 'world' of a string 'hello world'\n%s" % pat
print re.findall(pat,su)

print '\n-----------'

pat = '([a-z]+)(?![a-z])(?<!hello)'
print "\nexcluding all the words 'hello'\n%s" % pat
print re.findall(pat,su)

pat = '([a-z]+)(?![a-z])(?<!\Ahello)'
print "\nexcluding the starting word 'hello'\n%s" % pat
print re.findall(pat,su)

pat = '([a-z]+)(?![a-z])(?<!hello(?= world))'
print "\nexcluding all the words 'hello' of a string 'hello world'\n%s" % pat
print re.findall(pat,su)

print '\n-----------'

pat = '([a-z]+)(?![a-z])(?<!hello|world)'
print "\nexcluding all the words 'hello' and 'world'\n%s" % pat
print re.findall(pat,su)

pat = '([a-z]+)(?![a-z])(?<!hello(?= world))(?<!hello world)'
print "\nexcluding all the words of a string 'hello world'\n%s" % pat
print re.findall(pat,su)

pat = '([a-z]+)(?![a-z])(?<!\Ahello(?= world))(?<!\Ahello world)'
print "\nexcluding all the words of the starting string 'hello world'\n%s" % pat
print re.findall(pat,su)

rawDescription: Python Regex: Matching a character on multi line?


code:
from datetime import datetime
from calendar import monthrange
month = '1406'
year, month = int(month[:2]), int(month[2:])
year += 2000  # assume this century
day = monthrange(year, month)[1]
datetime(year, month, day)
print datetime(year, month, day)

rawDescription: Get a datetime format from a string in Python


code:
print result.firstname
print result.hobbies

rawDescription: PyParsing a string representing a function


code:
import operator
def kenobi(l):
    if isinstance(l[1],list):
        l[1] = kenobi(l[1])
    return do_math(l)

def do_math(l):
    #print l
    ops = {'+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.div}
    return ops[l[0]](l[1],l[2])

print kenobi(['+',['-',['*',3,5],8],2])

9

rawDescription: how to get the modified input back in a prefix notation

description: Get the modified input back in a prefix notation


code:
import matplotlib
print matplotlib.is_interactive()

rawDescription: Keep a figure "on hold" after running a script


code:
# !/usr/bin/python

import os

directory_list = list()
for root, dirs, files in os.walk("/path/to/your/dir", topdown=False):
    for name in dirs:
        directory_list.append(os.path.join(root, name))

print directory_list

rawDescription: Python folder names in the directory

description: Python folder names in the directory


rawDescription: Decimal alignment formatting in Python

description: Decimal alignment formatting in Python


rawDescription: How to check if list element is present in array in python

description: Check if list element is present in array in python


code:
query = "ipsum dolor"
corpus = "lorem i psum d0l0r sit amet"
match = get_best_match(query, corpus, step=2, flex=4)
print match
('i psum d0l0r', 0.782608695652174)

rawDescription: Find best substring match

description: Find best substring match


code: print var1 + var2 + "|" + var3

rawDescription: How to add multiple variables on the same line with string in between?

description: Add multiple variables on the same line with string in between


code:
def classproperty_getter_only(f):
    class NonDataDescriptor(object):
        def __get__(self, instance, icls):
            return f(icls)
    return NonDataDescriptor()

class Foo(object):

    @classproperty_getter_only
    def flup(cls):
        return 'hello from', cls

print Foo.flup
print Foo().flup

rawDescription: Implementing class descriptors by subclassing the `type` class


code:
class A(object):

    _x = None

    class __metaclass__(type):

        @property
        def x(cls):
            return A._x

        @x.setter
        def x(cls, val):
            A._x = val

class B(A):
    pass

A.x = 'jim'
B.x = 'joe'

print A.x, B.x

rawDescription: class property that can be accessed and set by all instances (also of subclasses)

description: Class property that can be accessed and set by all instances (also of subclasses)


code:
pk = insp.get_pk_constraint("user")['constrained_columns']
unique = map(lambda x: x['column_names'], insp.get_unique_constraints("user"))

for column in ['name', 'id', 'email', 'login']:
    print "Column %r has an unique constraint: %s" %(column, [column] in [pk]+unique)

rawDescription: SQLAlchemy Determine If Unique Constraint Exists


code:
gen = itertools.permutations(l,3)
s = set()

for i in gen:
    if i not in s:
        print i  # or do anything else
    # if some_cond: break
    s.add(i)

rawDescription: Code for generating all unique permutations recursively?


code:
time/time #nothing

time/time + 1 #nothing

var = time/time  #var becomes result

print time/time #prints the result

rawDescription: Python Variable Division

description: Python Variable Division


code:
TLD = ('.com', '.info', '.org', '.net')
hostName = raw_input(":").lower()
while not hostName.endswith(TLD):
    print "Incorrect input, Try again!"
    hostName = raw_input(":").lower()

rawDescription: Python code to determine if TLD exists, prompt again if not?


code:
f = codecs.open('test', encoding='utf-8', mode='w+')
f.write(u'\u4500 blah blah blah\n')
f.seek(0)
print repr(f.readline()[:1])
f.close()

rawDescription: Character reading from file in Python

description: Character reading from file in Python


rawDescription: Remove duplicate lines from a string

description: Remove duplicate lines from a string


code:
class ModelBase(object):
    def __init__(self, *args, **kwargs):
        serialiser = self.Serialiser(self, *args, **kwargs)

class SerialiserBase(LibrarySerialiser):
    def __init__(self, outer, *args, **kwargs):
        self.outer = outer
        print self.outer.origin
        super(SerialiserBase, self).__init__(*args, **kwargs)

# ...

cm  = ComponentModel()
ser = cm.serialiser

rawDescription: Accessing a parent variable from a nested child class in Python


rawDescription: Efficiently add values to bidimensional array


code: print max(highscores)

rawDescription: I am trying to save high scores with pickle, but how do I add to an already pickled document and then get the max?


rawDescription: Python binary file reading problem

description: Python binary file reading problem


rawDescription: Find the sub-string after nth '|'

description: Find the sub-string after nth '|'


code:
import msvcrt

num = 0
done = False
while not done:
    print num
    num += 1

    if msvcrt.kbhit():
        print "you pressed",msvcrt.getch(),"so now i will quit"
        done = True

rawDescription: Python nonblocking console input

description: Python nonblocking console input


code:
checked = [False] * len(words)
for i, (word, word_s) in enumerate(words):
    if checked[i]:
       continue
    for j, (other, other_s) in enumerate(words[i:]):
        if word_s == other_s:
            print other,
            checked[i+j] = True
    print

rawDescription: How to think in Python after working in C++?

description: Think in Python after working in C++


code:
from django.contrib.auth.decorators import user_passes_test, login_required
@login_required
@user_passes_test(lambda user: user.is_active())
def myView(request):
      print 'in my view'

rawDescription: how to test view with custom decorator relate to user in django?

description: Test view with custom decorator relate to user in django


code:
#!/usr/bin/python

import sys
import os
import glob
from contextlib import contextmanager
import traceback

set_backend = "import matplotlib\nmatplotlib.use('Agg')\n"

@contextmanager
def redirected_output(new_stdout=None, new_stderr=None):
    save_stdout = sys.stdout
    save_stderr = sys.stderr
    if new_stdout is not None:
        sys.stdout = new_stdout
    if new_stderr is not None:
        sys.stderr = new_stderr
    try:
        yield None
    finally:
        sys.stdout = save_stdout
        sys.stderr = save_stderr

def run_exectests(test_dir, log_path='exectests.log'):

    test_files = glob.glob(os.path.join(test_dir, '*.py'))
    test_files.sort()
    passed = []
    failed = []
    with open(log_path, 'w') as f:
        with redirected_output(new_stdout=f, new_stderr=f):
            for fname in test_files:
                print(">> Executing '%s'" % fname)
                try:
                    code = compile(set_backend + open(fname, 'r').read(),
                                   fname, 'exec')
                    exec(code, {'__name__':'__main__'}, {})
                    passed.append(fname)
                except:
                    traceback.print_exc()
                    failed.append(fname)
                    pass

    print ">> Passed %i/%i tests: " %(len(passed), len(test_files))
    print "Passed: " + ', '.join(passed)
    print "Failed: " + ', '.join(failed)
    print "See %s for details" % log_path

    return passed, failed

if __name__ == '__main__':
    run_exectests(*sys.argv[1:])

rawDescription: Suppress matplotlib figures when running .py files via python or ipython terminal


code:
try:
    files = ftp.nlst()
except ftplib.error_perm, resp:
    if str(resp) == "550 No files found":
        print("No files in this directory.")
else:
    raise

rawDescription: Get all filenames inside a directory on FTP [Python]


code:
import subprocess
import threading

outFile = open("qdisc_log.csv","w+")
def printit():
    threading.Timer(1.0, printit).start()
    p = subprocess.Popen(["sudo", "tc", "-s", "qdisc", "show"], stdout=subprocess.PIPE)
    output, err = p.communicate()
    outFile.write(output)
    outFile.write("\n")
    outFile.flush()
    print  output

printit()

rawDescription: Run a command every one second using threads in python


code:
def process_message(self, peer, mailfrom, rcpttos, data):
    # ...
    print 'Message               :', data
    asyncore.close_all()
    return

rawDescription: How to scan python smtpd server for text

description: Scan python smtpd server for text


code:
ticklist = xmldoc.getElementsByTagName('Ticktype')
print "There are currently %d tick types" % len(ticklist)
for s in ticklist:
    print s.firstChild.data

rawDescription: Using minidom to break down an xml file into managable files. Python


code:
soup = BeautifulSoup(st)
for a in soup.findAll('div', {"class":"tab-pane fade in active"}):
    for b in a.findAll('a'):
        print b.get('href')

rawDescription: Python: Extract hrefs inside a div

description: Python: Extract hrefs inside a div


code:
clerk = visit_once_clerk(['start'])
print clerk.next()   # Print: start
print clerk.send('test')  # Print: test
print clerk.next()   # Raise StopIteration

rawDescription: Python coroutine that consumes and produces


rawDescription: How to sort an itertools grouper array in python

description: Sort an itertools grouper array in python


code:
import multiprocessing as mp

timelimit = 3

def f(pipe):
    a = 1/0
    pipe.send('hola')

parent, child = mp.Pipe()

proc = mp.Process(target=f, args=(child,))

proc.start()

print "Grabbing result"
if parent.poll(timelimit):
    print "Result: {0}".format(parent.recv())
else:
    print "No data available after {0} seconds...".format(timelimit)

proc.join()

rawDescription: Dealing with dead processes using multiprocessing.Pipe


code:
# It is a good idea to store the filename into a variable.
# The variable can later become a function argument when the
# code is converted to a function body.
filename = 'data.txt'

# Using the newer with construct to close the file automatically.
with open(filename) as f:
    data = f.readlines()

# Or using the older approach and closing the filea explicitly.
# Here the data is re-read again, do not use both ;)
f = open(filename)
data = f.readlines()
f.close()

# The data is of the list type.  The Python list type is actually
# a dynamic array. The lines contain also the \n; hence the .rstrip()
for n, line in enumerate(data, 1):
    print '{:2}.'.format(n), line.rstrip()

print '-----------------'

# You can later iterate through the list for other purpose, for
# example to read them via the csv.reader.
import csv

reader = csv.reader(data)
for row in reader:
    print row

rawDescription: Is there a way to read a .txt file and store each line to memory?


code:
def onClick(self, event):
    print event
    keys = ""
    if wx.GetKeyState(wx.WXK_CONTROL):
        keys += "ctrl "
    if wx.GetKeyState(wx.WXK_ALT):
        keys += "alt "
    wx.MessageBox("You pressed key '%s' while clicking!" % keys)

rawDescription: matplotlib pick_event returns incorrect pressed keys

description: Matplotlib pick_event returns incorrect pressed keys


code:
#---- This is the third-party module ----#

class Base(object):
  def foo(self):
    print 'original foo'

class One(Base):
  def bar(self):
    self.foo()

class Two(Base):
  def bar(self):
    self.foo()

#---- This is your module ----#

# Test the original
One().bar()
Two().bar()

# Monkey-patch and test
def Base_foo(self):
  print 'monkey-patched foo'

Base.foo = Base_foo

One().bar()
Two().bar()

rawDescription: Python: override base class

description: Python: override base class


code:
# order and selection don't matter
print set(x)

# preserve item order
print dict(zip(x, x)).keys()

# filter, order not preserved
print set(filter(lambda s : True if isinstance(s, str) else False, x))

# filter, preserve order
print (lambda x : [s for s in dict(zip(x,x)).keys() if isinstance(s, str)])(x)

rawDescription: How can I print the elements (non repetitive) in a list

description: Print the elements (non repetitive) in a list


code:
import re

def rpl(mat, d = {'dw11qdwq':'qqqqqq' ,'aaaaa':'wwwwwww'}):
    return mat.group(1) + d[mat.group(2)]

pat = re.compile('(111\s*=\s*)(\S*)')

a = '111 = dw11qdwq\n111 = aaaaa'
print repr(a)
a= pat.sub(rpl,a)
print repr(a)

rawDescription: how to get the two '111 = ' using python Regular Expressions

description: Get the two '111 = ' using python Regular Expressions


code:
print [field for idx, field in enumerate(fieldnames) if values[idx] is not None]
# ['oranges', 'bananas']

rawDescription: Having trouble with pythonic style and list comprehension


code:
#!/usr/bin/env python

import sys

print sys.argv[1]
print '\n'
print sys.argv[2]

rawDescription: Python: Passing value of a string variable as argument in Popen


code:
class AnotherAC(object):
    implements(Interface)
    adapts(A, C)
    def __init__(self, a, c): pass

gsm.registerAdapter(AnotherAC, name=u'another')
for adapter in getAdapters((a, c), Interface):
    print adapter

rawDescription: Issue with zope.component subscriber adapters adapting multiple objects


code:
from urlparse import urlparse
o = urlparse("http://xxx.abcdef.com/fdfdf/")
print o

print o.netloc

rawDescription: python 2 and 3 extract domain from url

description: Python 2 and 3 extract domain from url


code:
for substring, count in counts.most_common():
    print '{:<20} {:>3d} times'.format(substring, count)

rawDescription: Top of the most frequent substrings in file of sequences


code:
for x in range(9):
    print "this works"
print "this doesn't work"

rawDescription: Simple "if" statements in Python

description: Simple "if" statements in Python


rawDescription: How to write lists "one by one" to a binary file in python?

description: Write lists "one by one" to a binary file in python


code:
#import heavy_function
import sys
_data = [1, 2, 3] # heavy_function()

def foo():
    print _data

def bar():
    print 'Call no 1'
    value1 = foo()
    print sys.getrefcount(_data)
    print 'Call no 2'
    value2 = foo()
    print sys.getrefcount(_data)
    print 'call no 3'
    value3 = foo()
    print sys.getrefcount(_data)

    print 'Check referenced values..'
    print value1 is value2

bar()

rawDescription: How does python load global variables?

description: How does python load global variables


code:
for s in all_subsets(['egg','bread','meat','onion']):
    if len(s)>2:
        print s

rawDescription: Django cooking recipes site model structure


code:
cursor.execute("show tables")
for r in cursor.fetchall():
    print r[0]

rawDescription: How to get database name from postgres/mysql cursor object

description: Get database name from postgres/mysql cursor object


code:
dirs = ['D1', 'D2', 'D3']
files = ['F1', 'F2', 'F3', 'F4' , 'F5', 'F6']

for f,d in zip(files, itertools.cycle(dirs)):
   print f, 'write in', d

rawDescription: Writing files to directories in a round robin pattern


code:
class MyFoo(object):
    a = 10

    def __init__(self):
        self.b = 20

    def bar(self):
        print self.a
        print self.b

    def eep(self):
        print self.c

rawDescription: Python Suite, Package, Module, TestCase and TestSuite differences


code:
from prettytable import PrettyTable

table = PrettyTable(["Column 1", "Column 2", "Column 3"])
table.add_row(["A", "B", "C"])
table.add_row(["F", "O", "O"])
table.add_row(["B", "A", "R"])

for row in table:
    row.border = False
    row.header = False
    print row.get_string(fields=["Column 1"]).strip() # Column 1

rawDescription: How to get the value of a cell when using prettytable

description: Get the value of a cell when using prettytable


code:
MASTER = [10,11,12,13,14]

LISTA = [0,1,2]
LISTB = [0,3,4]

for i in LISTA: MASTER[i] += 10
for i in LISTB: MASTER[i] += 10

print MASTER[LISTA[0]]

print MASTER[LISTB[0]]

rawDescription: Multiple references in separate lists; Python


code:
import argparse

class Input_action(argparse._AppendAction):
    def __call__(self, parser, namespace, values, option_string=None):
        # assume attribute has already been set to []
        items = getattr(namespace, self.dest)
        items.append(values)
        setattr(namespace, self.dest, items)

        dest = 'file_rotate'
        values = False
        items = getattr(namespace, dest)
        items.append(values)
        setattr(namespace, dest, items)

class Rotate_action(argparse._StoreTrueAction):

    def __call__(self, parser, namespace, values, option_string=None):
        setattr(namespace, self.dest, self.const)
        dest = 'file_rotate'  # not same as self.dest
        items = getattr(namespace, dest)
        if items:
            items[-1] = True
        setattr(namespace, dest, items)

parser = argparse.ArgumentParser()
parser.add_argument('-i','--input', action=Input_action)
parser.add_argument('-r','--rotate', action=Rotate_action)

parser.print_help()

# simpler to initialize these attributes here than in the Actions
ns = argparse.Namespace(input=[], file_rotate=[])
print parser.parse_args(namespace=ns)

rawDescription: python argparse always set defaults related to an optional argument

description: Python argparse always set defaults related to an optional argument


code:
import re

emailsString = (u'     From:\n'
                '     Lastname, Firstname\n'
                '     Sent:\n'
                '     Monday, June 24, 2013 1:48 PM\n'
                '     To:\n'
                '     Othername, Name\n'
                '     Subject:\n'
                '     RE: Center update\n'
                '    Message message message.\n'
                '    Such a lovely message\n'
                '    Take care,\n'
                '    Firstname Lastname, MS\n'
                '     Long signature\n'
                '     in this email\n'
                '   \n'
                '    E-mail:\n'
                '     email@email.com\n'
                '     Web\n'
                '     my blog\n'
                '     From:\n'
                '     Lastname, Firstname\n'
                '     Sent:\n'
                '     Monday, June 24, 2013 9:33 AM\n'
                '     To:\n'
                '     Othername, Name\n'
                '     Subject:\n'
                '     Center update\n'
                '     Importance:\n'
                '     High\n'
                '    Good Morning Name,\n'
                '    I hope this finds you doing well.\n'
                '    I wanted to inform you of some changes. The Center will be closing August 30\n'
                '     th\n'
                '     .  or September 1\n'
                '     st\n'
                '     .  I\u2019ve enjoyed my experience. ')

allEmailsString = '''
From: FirstLastname, FirstFirstname
Sent: Monday, July 15th, 2011, 9:36 AM
To: TheOne
Subject: blah
Importance: High

Message message message
second line of message

second para of message

From: MidLastname, MidFirstname
Sent: Thursday, July 18th, 2011, 10:45 AM
To: TWOTWO
Subject: once upon

From: LastLastname, LastFirstname
Sent: Saturday, July 20th, 2011, 12:51 AM
To: Mr Three
Subject: blobloblo

Nothing to say. '''

dispat = ("*  from: {from}\n"
          "*  to: {to}\n"
          "*  date: {date}\n"
          "*  subject: {subject}\n"
          "** message (beginning on next line):\n{message}\n"
          "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")

regx = re.compile('From:[ \t\r\n]*(?P<from>.*?[^ \t\r\n])'
                  '[ \t\r\n]*'
                  'Sent:[ \t\r\n]*'
                  '(?P<date>.*?(?:Mon|Tues|Wednes|Thurs|Fri|Satur|Sun)day.*?[^ \t\r\n])'
                  '[ \t\r\n]*'
                  'To:[ \t\r\n]*(?P<to>.*?[^ \t\r\n])'
                  '[ \t\r\n]*'
                  'Subject:[ \t\r\n]*(?P<subject>[^\r\n]+)'
                  '[ \t\r\n]*'
                  '(?:Importance:[ \t\r\n]*(?P<importance>[^ \t\r\n]+))?'
                  '[ \t\r\n]*'
                  '(?P<message>.*?)'
                  '(?=[ \t\r\n]*From:.*?'
                  'Sent:.*?(?:Mon|Tues|Wednes|Thurs|Fri|Satur|Sun)day.*?'
                  'To.*?Subject:|\Z)',
                  re.DOTALL)

for s in (emailsString,allEmailsString):
    print ''.join(dispat.format(**d)
                  for d in (ma.groupdict('') for ma in regx.finditer(s)))
    print '\n#######################################\n'

rawDescription: Python regex: capturing group captures/overrides subsequent matches


code:
with open('data.csv', 'wb') as outfile:
    outfile.write(DELIMITER.join(data) + '\n')

with open('data.csv', 'rb') as infile:
    row = infile.readline().rstrip().split(DELIMITER)
    print row

rawDescription: CSV writing strings of text that need a unique delimiter


rawDescription: Regex for extracting string with interrogation (?) mark in Python


code:
def summer():
  res = 0
  inc = 0
  while True:
    res += inc
    inc = (yield res)

s = summer()
print s.send(None)
print s.send(3)
print s.send(5)
print s.send(2)
print s.send(4)

rawDescription: Returning values from inside a while loop in python


code:
selectedList = []
def printSelected (n, low):

    if len(n) == 1:
        # No need to print because it's
        # already printed at previous recursion level.
        return

    minVal = math.Inf
    minTupleLeft = ()
    minTupleRight = ()
    splitI = 0

    for i in range(1, len(n)):
        tuple1ToI = tuple (n[:i])
        tupleiToN = tuple (n[i:])

        if (cache[tuple1ToI] + cache[tupleiToN]) < minVal:
            minVal = cache[tuple1ToI] + cache[tupleiToN]
            minTupleLeft = tuple1ToI
            minTupleRight = tupleiToN
            splitI = low + i

    print minTupleLeft, minTupleRight, minVal
    print splitI   # OP just wants the split index 'i'.
    selectedList.append(splitI) # or add to the list as requested by OP

    printSelected (list(minTupleLeft), low)
    printSelected (list(minTupleRight), splitI)

rawDescription: More on dynamic programming

description: More on dynamic programming


rawDescription: Convert float64 numpy array to floats not in scientific notation


code:
for word in word_generator():
   print word

rawDescription: Python: How to retrieve second word from the text


code:
def past_window(g,win=70):
    g = g.set_index('dates').resample('D','last')
    g['want'] = pd.rolling_sum(g['x'],win,0).shift(1)
    return g[g.x.notnull()]

df = df.groupby('id').apply(past_window,win=10)
print df.fillna(0)

rawDescription: Pandas temporal cumulative sum by group

description: Pandas temporal cumulative sum by group


code: print 'asdf hjkl'.replace(' ', '\s')

rawDescription: String replace with backslashes in Python


code:
if __name__ == "__main__":
    df = """      Control      Recd_Date/Due_Date                Action        Signature/Requester
0     2000-1703   2000-01-31 00:00:00           OC/OER/OPA/PMS/                 M WEBB
1           NaN   2000-02-29 00:00:00                       NaN              DATA CORP
2     2000-1776   2000-01-02 00:00:00            OC/ORA/OE/DCP/                  G KAN
3           NaN   2000-01-03 00:00:00           OC/ORA/ORO/PNC/              PALM POST
4           NaN                   NaN  FDA/OGROP/ORA/SE-FO/FLA-                    NaN
5           NaN                   NaN                DO/FLA-CB/                    NaN
6     2000-1983   2000-02-02 00:00:00  FDA/OGROP/ORA/CE-FO/CHI-                 M EGAN
7           NaN   2000-02-03 00:00:00                DO/CHI-CB/   BERNSTEIN LIEBHARD &
8           NaN                   NaN                       NaN             LONDON LLP"""
    df =  pd.read_csv(StringIO.StringIO(df),sep = "\s\s+",engine='python')

    groups = df.groupby(df['Control'].apply(rolling_group),as_index=False)
    groupFunct = lambda g: pd.Series([joinFunc(g,col) for col in g.columns],index=g.columns)
    print groups.apply(groupFunct)

rawDescription: Combine rows (under a condition) in a Pandas Dataframe


code:
def find(element, JSON, path, all_paths):
  if element in JSON:
    path = path + element + ' = ' + JSON[element].encode('utf-8')
    print path
    all_paths.append(path)
  for key in JSON:
    if isinstance(JSON[key], dict):
      find(element, JSON[key],path + key + '.',all_paths)

rawDescription: JSON get key path in nested dictionary

description: JSON get key path in nested dictionary


code:
stdin, stdout, stderr = ssh.exec_command(cmd)
lines = stdout.readlines()
errors = stderr.readlines()
for e in errors:
    print 'error', e

for l in lines:
    print 'line', l

rawDescription: Run linux commands from windows in python script


code:
def foo(**new_kwargs):
    print new_kwargs
dic = { 'key' : 'value' }
foo(**dic)
foo(key='value')
foo(dic)

rawDescription: how to show all the methods to a particular class, or functions such as **kwargs?

description: Show all the methods to a particular class, or functions such as **kwargs


code:
def do_the_thing():
    if not "id" in dir():
         print "id not set, cannot continue"
         return
         # exit here!
    # otherwise continue with the rest of the script...
    print "alright..."
    # [ more code ]

# Call the method
do_the_thing()

rawDescription: Exit to command line in Python

description: Exit to command line in Python


code:
log = """\
"ID","PARENT_ID","URI","FILE_PATH","NAME","METHOD","STATUS","SIZE","TYPE","EXT","LAST_MODIFIED","EXTENSION_MISMATCH","MD5_HASH","FORMAT_COUNT","PUID","MIME_TYPE","FORMAT_NAME","FORMAT_VERSION"
"1","","file:/C:/jay/NZ%20Business%20Roundtable_Download_13Feb2013,%20400%20Access/","C:\jay\NZ Business Roundtable_Download_13Feb2013, 400 Access","NZ Business Roundtable_Download_13Feb2013, 400 Access",,"Done","","Folder",,"2013-06-28T11:31:36","false",,"",,"","",""
"2","1","file:/C:/jay/NZ%20Business%20Roundtable_Download_13Feb2013,%20400%20Access/1993/","C:\jay\NZ Business Roundtable_Download_13Feb2013, 400 Access\1993","1993",,"Done","","Folder",,"2013-06-28T11:31:36","false",,"",,"","",""
"""

rawDescription: Python/MySQLdb import from CSV with data encapsulation


code:
name = raw_input("What is your name?")
if name:
    print "Hello %s, Let's be friends!" % name
else:
    print "Sorry, what was that?"

rawDescription: Please help me complete my code

description: Please help me complete my code


code:
x = range(1, 11) #10 players

def round_robin(someIds):
    someIds = someIds[::2] + someIds[1::2]
    first, someIds = [someIds[0]], someIds[1:]
    n = len(someIds)
    for i in range(n):
        top = someIds[:n/2]
        bottom = someIds[n/2:]
        yield zip(first+top, bottom)
        someIds = someIds[-1:] + someIds[:-1]

for thing in round_robin(x):
    print thing

rawDescription: Sorting a list of tuples such that grouping by key is not desired


code:
def filtered_product(args, filter_fn):
    pools = map(tuple, args)
    result = [[]]
    for pool in pools:
        new_result = []
        for x in result:
            for y in pool:
                new_val = x+[y]
                if filter_fn(new_val):
                    new_result.append(x+[y])
        result = new_result
        print 'intermediate result:', result
    for prod in result:
        yield tuple(prod)

rawDescription: How do I convert Python's itertools.product library from a list comprehension to normal for loops?

description: Convert Python's itertools.product library from a list comprehension to normal for loops


code:
import pandas as pd
import numpy as np
df = pd.DataFrame({'SensorA':[1,3,4,5,6], 'SensorB':[5,2,3,6,7], 'SensorC':[7,4,8,1,10], 'group_id':[1,2,1,1,2]})
df = pd.melt(df, id_vars = 'group_id', var_name = 'Sensor')
print df

rawDescription: Reduce number of columns in a pandas DataFrame


code:
def test1(x):
    print x # <Tkinter.Event instance at 0xb74a6fcc>
    test(1)

rawDescription: how handle events on every individual entry

description: How handle events on every individual entry


code:
print "ip path count"
for ip,d in second.iteritems():
     for path, count in d.iteritems():
         print "%s %s %d" % (ip, path, count)

rawDescription: How to approach IIS log parsing in a Pythonic way?

description: Approach IIS log parsing in a Pythonic way


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

W = 512
H = 256

hsls = np.zeros((H, W, 3), dtype=float)

baseCol = np.linspace(0, 1, H)
print baseCol.shape, hsls.shape
# (256,) (256, 512, 3)
hsls[:, :, 0] = baseCol[:, np.newaxis]
hsls[:, :, 1:3] = 0.5 # Use constant saturation and lightness.

vfunc = np.vectorize(colorsys.hsv_to_rgb)
rgbs = np.dstack(vfunc(hsls[:, :, 0], hsls[:, :, 1], hsls[:, :, 2]))

im = plt.imshow(rgbs, interpolation='nearest', aspect='auto')
plt.colorbar(im, orientation='horizontal')
plt.show()

rawDescription: Draw NxMx3 ndarray as image using matplotlib imshow


code:
import threading
from matplotlib import pyplot as p
import time

p.plot([_ for _ in xrange(5)])

t = threading.Thread(target=p.show)
t.start()

for i in xrange(5):
    print "lala %s" % i
    time.sleep(1)

print "Waiting for plot thread to finish..."
t.join()
print "Finished."

rawDescription: CLI program, show matplotlib plot and carry on with program


code:
import lxml.html as LH

xmlstr = """

T1

T2

T3

A1

A3

"""

html_root = LH.fromstring(xmlstr)

eol_table = None
for tbl in html_root.xpath('//table'):
     p_elements = tbl.xpath('.//tr/td/p')
     eol_table = [p_elm.text for p_elm in p_elements]

     print eol_table

rawDescription: lxml: Append 'None' or Null value when html tag text content is None

description: Lxml: Append 'None' or Null value when html tag text content is None


code: print " ".join('%0.2f' % item for item in mylist)

rawDescription: Change default float print format

description: Change default float print format


code:
with open('table.txt', 'r') as table:
    while True:
        try:
            row1 = [i.strip() for i in table.readline().split('\t')] # Split based on number of spaces in btw 2 values or numbers
            row2 = [i.strip() for i in table.readline().split('\t')]
            print row1[0] + ' ' + row1[2] + ' ' + row1[4] + ' ' + row2[2] + ' ' + row2[4]
        except:
            break

rawDescription: Python read txt and print

description: Python read txt and print


code:
nd = NoData()
nd + 5
pow(nd, 1)
math.pow(NoData(), 1)
nd > 5
if nd > 5:
    print "Yes"
else:
    print "No"

"The answer is " + nd
"The answer is %f" % (nd)
"The answer is %s" % (nd)
nd.f = 5
nd.f
nd.f()

rawDescription: Propagation of NaN through calculations

description: Propagation of NaN through calculations


code:
dct = {
    'tick': 1,
    'week': 1,
    'month': 1,
    'year': 1
}

def increment(key):
    dct[key] += 1

increment('week')
print dct['week']
increment('tick')
print dct['tick']

rawDescription: Values Not Adding Up

description: Values Not Adding Up


code:
for js in json_files:
    with open(os.path.join(path_to_json, js)) as json_file:
        # do something with your json; I'll just print
        print json.load(json_file)

rawDescription: Python: Read several json files from a folder


rawDescription: Python increase specific matrix elements


code:
class SimpleThread(QtCore.QThread):
    def __init__(self, queue, result_queue, parent=None):
        QtCore.QThread.__init__(self, parent)
        self.queue=queue
        self.result_queue = result_queue

    def run(self):
        while True:
            arg=self.queue.get()
            self.fun(arg)
            self.queue.task_done()

    def fun(self, arg):
        for i in range (3):
            print 'fun: %s'%i
            self.sleep(1)
        self.result_queue.put(arg+1)

def handle_results(result_queue):
   while True:
       result = result_queue.get()
       print("Got result {}".format(result))

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

description: Return value from function running by QThread and Queue


code:
#LCM Calculator
#Author: Ethan Houston
#Language: Python
#Date: 2013-12-27
#Function: Program takes 2 numbers as input, and finds the lowest number
# that goes into each of them

def lcmCalculator(one, two):
    """ takes two numbers as input, computes a number that evenly
        divides both numbers """
    counter = min_inp = min(one, two)
    while True:
        if counter % one == 0 and counter % two == 0:
            return counter
        else:
            counter += min_inp

print "\nThis program takes two numbers and computes the LCM of them...\n"

first_number = input("Enter your first number: ")
second_number = input("Enter your second number: ")

print lcmCalculator(first_number, second_number)

rawDescription: My 'lowest common multiple' program hangs, and doesn't output an answer


code:
import inspect
import sys

print inspect.getsource(sys.modules[__name__])

rawDescription: How to get the current script's code in Python?

description: Get the current script's code in Python


code:
name = "MyFullName"

print cutit(name, 2)   # prints "FullName"

rawDescription: Remove n characters from a start of a string


code:
import time

print "Starting program."

print "Loading   ",
time.sleep(1) #do some work here...
print "\rLoading.  ",
time.sleep(1) #do some more work here...
print "\rLoading.. ",
time.sleep(1) #do even more work...
print "\rLoading...",
time.sleep(1) #gratuitious amounts of work...
print "\rLoading   ",

rawDescription: Loading animation in python

description: Loading animation in python


code:
class Itemtest(object):
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return self.name
    def __setitem__(self, item, value):
        print "__setitem__", self, item, value
    def __getitem__(self, item):
        print "__getitem__", self, item
        return Itemtest("inner")

a = Itemtest("outer")
a[0:1] = [4]
temp = a[0:1]
temp[0] = 4
a[0:1][0] = 4

rawDescription: slice in python, is a copy or just a pointer

description: Slice in python, is a copy or just a pointer


code:
match = re.search(r'ABC\s*(\d+)', page)
if match:
    print match.group(1)

rawDescription: Finding first non-digit character with .find in Python


code:
def sort_list(my_list):
    print sorted(my_list)

def main():
    arr = [1, 3, 2, 5, 4]
    sort_list(arr)

if __name__ == "__main__":
    main()

rawDescription: Python: creating customer sort method & loop stops before list sorted


code:
with open('bsxlength.txt' , 'r') as string:
    n = 513
    for line in string:
        print [line[i:i+n] for i in range(0, len(line), n)]

rawDescription: Chunk long string into 513 character chunks | Python 3.3


code:
from matplotlib.pyplot import *

def plot():
    print "Hello!"

plot()

rawDescription: What is the difference between importing matplotlib and matplotlib.pyplot?


code:
with open('myfile.txt', 'r') as fo:
    str = fo.readline()
    str = str[7:11]
    print "Read String is : ", str

rawDescription: How to read specific word of a text file in Python

description: Read specific word of a text file in Python


code:
with open('customers.txt','r') as f:
    final_list=[i.split(",") for i in f.read().replace(" ","").splitlines()]

print final_list

rawDescription: Converting text file to list

description: Converting text file to list


code:
for i in content:
    print i.get_text()

rawDescription: Python, remove all html tags from string


code:
import psycopg2, psycopg2.extras

query = """
    select array(
        select *
        from (values
            ('A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11'::uuid),
            ('A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11'::uuid)
        )s
    );
"""

psycopg2.extras.register_uuid()

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:
import socket

try:
    socket.inet_aton(addr)
    print "ipv4 address"
except socket.error:
    print "not ipv4 address"

rawDescription: Checking for IP addresses

description: Checking for IP addresses


code:
import itertools
for i, j in itertools.combinations(range(10), 2):
    print i, j

rawDescription: Managing a computation onlt with iterators Python


code:
import bisect
array = [1.,2.,3.,1.,1.,0.,10.]
n = len(array)
sorted_array = sorted(array)
print [n - bisect.bisect(sorted_array, val) for val in array]

rawDescription: Efficient way to count the greater than operation for each value itself


code:
class NotificationMiddleware(object):
    def process_template_response(self, request, response):
        if request.user.is_authenticated():
            try:
                notifications = Notification.objects.all_for_user(request.user).unread()
                count = notifications.count()
                response.context_data['count'] = count  # I recomend you to use other name instead of 'count'
                return response
            except Exception, e:
                print e  # Fix possible errors
                return response
        else:
            return response

rawDescription: Django middleware process_template_response setup


code:
from selenium import webdriver

url = 'http://board.net/p/ThisIsBob%27sBoard/timeslider'
driver = webdriver.Firefox()
driver.get(url)

element = driver.find_element_by_id('padcontent')
print element.text

rawDescription: How to use python to scrape the text from a page generated by javascript?

description: Use python to scrape the text from a page generated by javascript


code:
def replicateALine(line):
    regex = re.search('\w -> ', line)
    if regex:
        line= line.replace(',', ';\n' + regex.group(0))
    return line

print replicateALine('a -> b,c,d;')
print replicateALine('e -> b;')

rawDescription: python: selectively split and combine alphanumeric strings in a file

description: Python: selectively split and combine alphanumeric strings in a file


code:
def func1(x):
    result = []
    last = None
    for i in x:
        if i != last:
            result.append(i)
        last = i

    return result

a = [1,2,2,3]
print func1(a)

rawDescription: How to have adjacent elements reduced to a single element

description: Have adjacent elements reduced to a single element


code:
from itertools import chain
from collections import Counter

a = [("a",1), ("b",3), ("a",5)]

dict_a = dict(Counter(chain.from_iterable(map(lambda a: a[0] * a[1], a))))

print dict_a
# {'a': 5, 'b': 3}

rawDescription: How to merge value of key when I have a list-tuple object

description: Merge value of key when I have a list-tuple object


code:
totalCount = 0
numberOfPositives = 0

with open('FL%.csv', 'r') as file2:
    reader = csv.reader(file2)
    reader.next() # this skips the first row of the file
    # this iteration will start from the second row of file2.csv
    conditionMet = False
    for row in reader:
        if conditionMet == True:
            if float(row[0].strip('%')) > 0: # change > to >= if you want to count 0 as positive
                print "FA, 1",row[0],',', ','.join(row[1:5]) # print 1 if positive
                numberOfPositives += 1 # add 1 to numberOfPositives only if positive
            else:
                print "FA, 0",row[0],',', ','.join(row[1:5]) # print 0 if not positive
            totalCount += 1 # add 1 to totalCount regardless of sign
            conditionMet = False # or break if you know you only need at most one line
        if row[1:5] == val1:
           conditionMet = True

rawDescription: Python excel number of times a negative and positive number appears (count/frequency)


code:
for i in xrange(3):
    for j in xrange(2):
        print 'i={0} j={1}'.format(i,j)

rawDescription: What is a nested for loop?

description: What is a nested for loop


code:
from fabric.api import env,hide,run

import sys
def hello():
    print "hello world"

def connect(commandInput):
    print "starting to connect"
    env.host_string = 'nms@10.0.0.70'
    env.password = "nms"
    with hide('output','running'):
        p=run(commandInput)
        return p

rawDescription: Python - fabric module missing all of a sudden


code:
validation.py
# Author: Philipp Wagner <bytefish@gmx.de>
# Released to public domain under terms of the BSD Simplified license.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#   * Neither the name of the organization nor the names of its contributors
#     may be used to endorse or promote products derived from this software
#     without specific prior written permission.
#
#   See <http://www.opensource.org/licenses/bsd-license>

import os
import sys
import cv2
import numpy as np

from sklearn import cross_validation as cval
from sklearn.base import BaseEstimator
from sklearn.metrics import precision_score

def read_images(path, sz=None):
    """Reads the images in a given folder, resizes images on the fly if size is given.

    Args:
        path: Path to a folder with subfolders representing the subjects (persons).
        sz: A tuple with the size Resizes

    Returns:
        A list [X,y]

            X: The images, which is a Python list of numpy arrays.
            y: The corresponding labels (the unique number of the subject, person) in a Python list.
    """
    c = 0
    X,y = [], []
    for dirname, dirnames, filenames in os.walk(path):
        for subdirname in dirnames:
            subject_path = os.path.join(dirname, subdirname)
            for filename in os.listdir(subject_path):
                try:
                    im = cv2.imread(os.path.join(subject_path, filename), cv2.IMREAD_GRAYSCALE)
                    # resize to given size (if given)
                    if (sz is not None):
                        im = cv2.resize(im, sz)
                    X.append(np.asarray(im, dtype=np.uint8))
                    y.append(c)
                except IOError, (errno, strerror):
                    print "I/O error({0}): {1}".format(errno, strerror)
                except:
                    print "Unexpected error:", sys.exc_info()[0]
                    raise
            c = c+1
    return [X,y]

class FaceRecognizerModel(BaseEstimator):

    def __init__(self):
        self.model = cv2.createFisherFaceRecognizer()

    def fit(self, X, y):
        self.model.train(X,y)

    def predict(self, T):
        return [self.model.predict(T[i]) for i in range(0, T.shape[0])]

if __name__ == "__main__":
    # You'll need at least some images to perform the validation on:
    if len(sys.argv) < 2:
        print "USAGE: facerec_demo.py </path/to/images> [</path/to/store/images/at>]"
        sys.exit()
    # Read the images and corresponding labels into X and y.
    [X,y] = read_images(sys.argv[1])
    # Convert labels to 32bit integers. This is a workaround for 64bit machines,
    # because the labels will truncated else. This is fixed in recent OpenCV
    # revisions already, I just leave it here for people on older revisions.
    #
    # Thanks to Leo Dirac for reporting:
    y = np.asarray(y, dtype=np.int32)
    # Then we create a 10-fold cross validation iterator:
    cv = cval.StratifiedKFold(y, 10)
    # Now we'll create a classifier, note we wrap it up in the
    # FaceRecognizerModel we have defined in this file. This is
    # done, so we can use it in the awesome scikit-learn library:
    estimator = FaceRecognizerModel()
    # And getting the precision_scores is then as easy as writing:
    precision_scores = cval.cross_val_score(estimator, X, y, score_func=precision_score, cv=cv)
    # Let's print them:
    print precision_scores

rawDescription: Face recognition in OpenCV Python FAR/FRR


code:
import re
import time
import datetime
import calendar

p = re.compile(ur'(?<=\.|_)([A-Z\d+_-]*?([A-Z\d+_-]{0,10}))(?=\.)')
test_str = u"Format            Example Input String  \n\nMMDDYYYY          foo.bar.02242015.txt\nMMDDYY            foo.bar.022415.txt\nMONCCYY           foo.bar.FEB2015.txt\nYYYY-MM-DD        foo_bar_2015-02-01_2015-02-28.txt\nYYYYMMDD          foo_bar_20150224.txt\nMM_YY             foo_bar_02_15.txt\nYYYYMMDD          foo_bar_20150224.txt"
def changedate(date):
    try:
        t = time.strptime(date,'%m%d%Y')
    except:
        pass
    try:
        t = time.strptime(date,'%m%d%y')
    except:
        pass
    try:
        t = time.strptime(date,'%b%Y')
        lastday = calendar.monthrange(int(t.tm_year), int(t.tm_mon))[1]
        t = time.strptime(date + str(lastday),'%b%Y%d')
    except:
        pass
    try:
        t = time.strptime(date,'%m_%y')
        lastday = calendar.monthrange(int(t.tm_year), int(t.tm_mon))[1]
        t = time.strptime(date + str(lastday),'%m_%y%d')
    except:
        pass
    try:
        t = time.strptime(date,'%Y-%m-%d')
    except:
        pass
    try:
        r = time.strftime("%Y%m%d",t)
        return r
    except:
        pass
    return date
test_str = re.sub(p,lambda m: changedate(m.group(2)), test_str)
print test_str

rawDescription: Match and change multiple date formats into one with Python Regex


code: answer = sorted(dd_rf.items(), key=lambda (k,v): v['rates']['correctRate'])

rawDescription: Python: Sort nested dictionary by value

description: Python: Sort nested dictionary by value


code:
class MyMetaClass(type):
    @property
    def my_data(cls):
        if getattr(cls, '_MY_DATA', None) is None:
            print "costly database call executing"
            my_data = 'bar'
            cls._MY_DATA = my_data
        return cls._MY_DATA

class MyClass(object):
    __metaclass__ = MyMetaClass

MyClass.my_data
MyClass.my_data

rawDescription: Python class member lazy initialization

description: Python class member lazy initialization


code:
def myFunction():
    print abc
    abc = 123

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


code:
#!/usr/bin/python

import time
print "Child started"
time.sleep(10)
print "Child finished"

rawDescription: "Fire and forget" a process from a Python script


code:
from urlparse import urljoin

from bs4 import BeautifulSoup
import requests

base_url = 'http://www.ncbi.nlm.nih.gov'
website = 'http://www.ncbi.nlm.nih.gov/pubmed/?term=mtap+prmt'

# parse the main page and grab the link to the side bar
session = requests.Session()
soup = BeautifulSoup(session.get(website).content)

url = urljoin(base_url, soup.select('div#disc_col a.disc_col_ph')[0]['href'])

# parsing the side bar
soup = BeautifulSoup(session.get(url).content)

for a in soup.select('div.portlet_content ul li.brieflinkpopper a'):
    print a.text, urljoin(base_url, a.get('href'))

rawDescription: how to scrape deeply embeded links with python beautifulSoup

description: Scrape deeply embeded links with python beautifulSoup


code:
header = wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'header')))
footer = wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'footer')))

results = []
while True:
    try:
        wait.until(wait_for_more_than_n_elements((By.CSS_SELECTOR, "div.flightbox"), len(results)))
    except TimeoutException:
        break

    results = self.driver.find_elements_by_css_selector("div.flightbox")
    print len(results)

    # scroll
    for _ in xrange(5):
        self.driver.execute_script("""
            arguments[0].scrollIntoView();
            arguments[1].scrollIntoView();
        """, header, footer)
        sleep(1)

rawDescription: PhantomJS acts differently than Firefox webdriver


code:
import scipy.optimize

def test_lbfgsb():
    def surface(x):
        return (x[0] - 3.0) ** 2 + (factor * x[1] - 4.0) ** 2

    factor = None
    for exponent in xrange(0, 9):
        magnitude = 10 ** exponent
        factors = [x * magnitude for x in [1, 3]]
        for factor in factors:
            optimization_result = scipy.optimize.minimize(surface, [0, 0], method='l-bfgs-b')
            desc = 'at factor %d' % (factor)
            if not optimization_result.success:
                print '%s FAILURE (%s)' % (desc, optimization_result.message)
            else:
                print '%s, found min at %s, after %d evaluations' % (
                    desc, optimization_result.x, optimization_result.nfev)

test_lbfgsb()

rawDescription: Normalization for optimization in python


code:
print df['month'].head()
print df['month'].apply(lambda x:x.time()).head()

rawDescription: PANDAS coarse dates (month/year)

description: PANDAS coarse dates (month/year)


code:
import re
import fileinput

for line in fileinput.input("your_file.txt", inplace=True):
    print re.sub("\d+", "", line),

rawDescription: Removing digits from a file

description: Removing digits from a file


code:
print "X"

from y import f1, f2

rawDescription: import behaviour with already loaded modules

description: Import behaviour with already loaded modules


code:
def random_swap(tree1, tree2):
    from random import randrange
    pos_in_1 = randrange(0, count(tree1))
    pos_in_2 = randrange(0, count(tree2))

    parent1, idx1 = find_idx(tree1, pos_in_1)
    parent2, idx2 = find_idx(tree2, pos_in_2)

    # Swap:
    parent1[idx1], parent2[idx2] = parent2[idx2], parent1[idx1]

c = 1
tree1 = ["f:2", c, ["f:1", c]]
tree2 = ["f:2", ["f:2", ["f:2", c, c], ["f:2", c, c]], ["f:3", ["f:4", c, c, c, c], ["f:2", c, c], c]]

while True:
    random_swap(tree1, tree2)
    print tree1
    print tree2

rawDescription: How to split two nested lists and combine the parts to create two new nested lists

description: Split two nested lists and combine the parts to create two new nested lists


code:
if "," and "," in g :
    print "yay"
if "," and "," and "," in g:
    print "nay"

rawDescription: File Inout Output multiple delimiters

description: File Inout Output multiple delimiters


code:
class UserNode(StructuredNode):
    user_id = IntegerProperty(required=True, index=True)
    follow = RelationshipTo('UserNode', 'FOLLOW')

    def get_followers(self):
        results, metadata = self.cypher("START a=node({self}) MATCH a<-[:FOLLOW]-(b) RETURN b")
        return [self.__class__.inflate(row[0]) for row in results]

    def get_following(self):
        results, metadata = self.cypher("START a=node({self}) MATCH b-[:FOLLOW]->(a) RETURN b")
        return [self.__class__.inflate(row[0]) for row in results]

    def follow_person(self, user_id):
        followed_user = self.index.get(user_id=user_id)
        self.follow.connect(followed_user, {'time': str(datetime.datetime.utcnow())})

u1 = UserNode(user_id=1).save()
u2 = UserNode(user_id=2).save()

u1.follow_person(2)
u2.follow_person(1)
print "User 1 follows {}".format(u1.get_following())
print "User 1's followers {}".format(u1.get_followers())

rawDescription: A following models with django neomodel

description: A following models with django neomodel


code:
def debug(text):
  print text
  return ''

environment.filters['debug']=debug

rawDescription: jinja2 print to console or logging

description: Jinja2 print to console or logging


code:
s1 = "Brendon, Melissa, Jason, , McGuirk"
s2 = "brandon, melissa, jxz, paula, coach"

print [a or b for a, b in zip(s1.split(', '), s2.split(', '))]

rawDescription: Python to filter a comma-separated list on another csv list


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

X = np.array([2,3])

poly = PolynomialFeatures(3)
Y =poly.fit_transform(X)
print Y
#prints [[ 1  2  3  4  6  9  8 12 18 27]]
print poly.powers_

rawDescription: sklearn: how to get coefficients of polynomial features

description: Sklearn: how to get coefficients of polynomial features


code:
import math

list_num = [0.41, 0.093, 0.002, 1.59, 0.0079, 0.080, 0.375, 0, 10.1, -0.061]

def myround(n):
    if n == 0:
        return 0
    sgn = -1 if n < 0 else 1
    scale = int(-math.floor(math.log10(abs(n))))
    if scale <= 0:
        scale = 1
    factor = 10**scale
    return sgn*math.floor(abs(n)*factor)/factor

print [myround(x) for x in list_num]

rawDescription: Round floats down in Python to keep one non-zero decimal only


code:
from itertools import combinations
text = "the big fat cat sits on the mat eating a rat"
lst = text.split()
for start, end in combinations(range(len(lst)), 2):
    print lst[start:end+1]

rawDescription: Printing all possible phrases (consecutive combinations of words) in a given string


code:
bin_edges = V

for left, right in zip(bin_edges, bin_edges[1:]):
    for i, j in zip(x, y):
        if left < i < right:
            print j

rawDescription: Arrays in loops

description: Arrays in loops


code:
import time
NUM_RANGE = 100000000

from multiprocessing  import Process

def timefunc(f):
    t = time.time()
    f()
    return time.time() - t

def multi():
    class MultiProcess(Process):
        def __init__(self):
            Process.__init__(self)

        def run(self):
            # Alter string + test processing speed
            for i in xrange(NUM_RANGE):
                a = 20 * 20

    thread1 = MultiProcess()
    thread2 = MultiProcess()
    thread1.start()
    thread2.start()
    thread1.join()
    thread2.join()

def single():
    for i in xrange(NUM_RANGE):
        a = 20 * 20

    for i in xrange(NUM_RANGE):
        a = 20 * 20

print timefunc(multi) / timefunc(single)

rawDescription: Python, using multiprocess is slower than not using it


code:
import dis

def func1(x, y):
    return min(x, y)

def func2(x, y):
    if x < y:
        return x
    return y

def func3(x, y):
    return x ^ ((y ^ x) & -(x > y))

print "*" * 80
dis.dis(func1)
print "*" * 80
dis.dis(func2)
print "*" * 80
dis.dis(func3)

rawDescription: How is `min` of two integers just as fast as 'bit hacking'?


code:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--use-files', nargs='*', default=['a', 'b', 'c'], help='HI!')
args = parser.parse_args()
print args

rawDescription: Specifying an unlimited number of arguments to a commanline option


rawDescription: How to convert this dictionary into a list of tuples?

description: Convert this dictionary into a list of tuples


code:
for group in sortedList:
    for job in locals()[group]:
        print job

rawDescription: Python: Iterate through a list that refers to other lists


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

class JogadorItem(Item):
    nome = Field()
    time = Field()

class MoneyballSpider(CrawlSpider):
    name = "moneyball"
    allowed_domains = ["esporte.uol.com.br", "click.uol.com.br", "uol.com.br"]
    start_urls = ["http://esporte.uol.com.br/futebol"]

    rules = (Rule(SgmlLinkExtractor(allow=(r'.*futebol/clubes/.*?/jogadores/', )), callback='parse_players', follow=True),
             Rule(SgmlLinkExtractor(allow=(r'.*futebol/clubes/.*', )), follow=True),)

    def parse_players(self, response):
        hxs = HtmlXPathSelector(response)
        jogadores = hxs.select('//div[@id="jogadores"]/div/ul/li')
        items = []
        for jogador in jogadores:
            item = JogadorItem()
            item['nome'] = jogador.select('h5/a/text()').extract()
            item['time'] = hxs.select('//div[@class="header clube"]/h1/a/text()').extract()
            items.append(item)
            print item['nome'], item['time']
        return items

rawDescription: Not able to following links in Scrapy

description: Not able to following links in Scrapy


code: print rec_replace("hello this is hello a simple hello test", "ok", "hello")

rawDescription: Using Recursion to Replace all occurences of a substring with another substring


code:
from requests.utils import unquote

print unquote("EC%2d2EC7")

rawDescription: Converting url encoded string(utf-8) to string in python?


code:
import json
from os import walk, path

def file_to_dict(fpath):
    return {
        'name': path.basename(fpath),
        'type': 'file',
        'path': fpath,
        'tag': 'org',
        }

def folder_to_dict(rootpath):
    return {
            'name': path.basename(rootpath),
            'type': 'folder',
            'path': rootpath,
            'tag': 'org',
            'children': [],
            }

def tree_to_dict(rootpath):
    root_dict = folder_to_dict(rootpath)
    root, folders, files = walk(rootpath).next()
    root_dict['children'] = [file_to_dict(path.sep.join([root, fpath])) for fpath in files]
    root_dict['children'] += [tree_to_dict(path.sep.join([root, folder])) for folder in folders]
    return root_dict

def tree_to_json(rootdir, pretty_print=True):
    root, folders, files = walk(rootdir).next()
    root_dict = [tree_to_dict(path.sep.join([root, folder])) for folder in folders]
    root_dict += [file_to_dict(path.sep.join([root, fpath])) for fpath in files]
    if pretty_print:
        js = json.dumps(root_dict, indent=4, encoding='utf-8')
    else:
        js = json.dumps(root_dict, encoding='utf-8')
    return js

print tree_to_json('/tmp/tree')

rawDescription: Filesytem tree to json

description: Filesytem tree to json


code:
L = [[['a']], [['b']], [['c'], ['d']], [['e'], ['f'], ['g']]]
D = {'adv0' : ('a', 'b'), 'adv1' : ('f', 'c'), 'adv2' : ('d', 'e', 'q')}

l = []
for sublist in L:
    for key in ('adv0','adv1','adv2'):
        if any(item[0] in D[key] for item in sublist):
            l.append(0) # changed to give explicit output you listed.
        else:
            l.append(1) # ditto
print l

rawDescription: Python: nested list and dictionary comparison


rawDescription: Seeding random number generators in parallel programs


code:
soup = BeautifulSoup(data)

for elm in soup.find_all(text="PATTERN:"):
    print elm.find_parent("font").find_next_sibling("font").get_text(strip=True)

rawDescription: Python extract info from a local html file


code:
import subprocess

print subprocess.Popen(['dig', '-t', 'TXT', 'google.com', '+short'], stdout=subprocess.PIPE).communicate()[0]

rawDescription: How can I check the value of a DNS TXT record for a host?

description: Check the value of a DNS TXT record for a host


code:
class First:
    def __init__(self):
        self.text = "This is a text in First class"
        self.second = Second(self.text)

class Second:
    def __init__(self, text):
        self.text = text
        print "How to show self.text from First?"
        print self.text

rawDescription: Python: Calling a method from the importing class


code: for i, j in enumerate(u'Сон'): print '%d: %s' % (i+1, j)

rawDescription: Easiest way to enumerate utf-8 string

description: Easiest way to enumerate utf-8 string


code:
@coroutine
def PlatinumCustomer(successor=None):
    while 1:    #   <---- this is missing from your coroutines
        cust = (yield)
        if cust.custtype == 'platinum':
           print "Platinum Customer"
        elif successor is not None:
           successor.send(cust)

rawDescription: Implementing Chain of responsibility pattern in python using coroutines


code:
import jinja2

template_string = '''

{% if x == 3 %}
{{ x }}
{% endif %}

'''
# create templates
template1 = jinja2.Template(template_string)
template2 = jinja2.Template(template_string, trim_blocks=True)

# render with and without settings
print template1.render(x=3)
print '\n\n'.format('-' * 32)
print template2.render(x=3)

rawDescription: Stripping whitespace in jinja2 & flask...why do I still need the minus sign?


code:
class fancytype(type):
    def __str__(self):
        return self.__name__
class ham(object):
    __metaclass__ = fancytype
print ham

rawDescription: How to define a __str__ method for a class?

description: Define a __str__ method for a class


code:
from datetime import datetime
import pandas as pd
def date():
    dates = []
    while True:
        start = raw_input("Please put start date with format yy.mm.dd or 'end': ")
        if start == "end":
            break
        end = raw_input("Please put end date with format yy.mm.dd or 'end': ")
        try:
            start = datetime.strptime(start,"%y.%m.%d")
            end = datetime.strptime(end,"%y.%m.%d")
            dates.append(pd.date_range(start, end))
        except ValueError:
            print "Use yy.mm.dd format or write 'end'"
    print 'Dates:'
    return dates

rawDescription: How to input a range of dates and turn it into a list?

description: Input a range of dates and turn it into a list


code:
import lxml.etree as et

xmltext = """
<root>
    <fruit>apple</fruit>
    <fruit>pear</fruit>
    <fruit>mango</fruit>
    <fruit>kiwi</fruit>
</root>
"""

tree = et.fromstring(xmltext)

for fruit in tree.xpath('//fruit'):
    fruit.text = 'rotten %s' % (fruit.text,)

print et.tostring(tree, pretty_print=True)

rawDescription: Is there an easy way to manipulate XML documents in Python?


code:
for y in range(1,int(N/2)):
   x = N - (y<<1)
   print x, y

rawDescription: Finding solution set of a Linear equation?


code:
x = 1,2,3
print x, type(x)
x = (1)
print x, type(x)
x = 1,
print x, type(x)
x = (1,)
print x, type(x)

rawDescription: Use parentheses or not for multiple variable assignment in Python?


code:
import pandas as pd
df = pd.DataFrame({'var': ['id', 'author', 'title', 'id', 'author', 'title'],
                   'info': [1, 'A', 'B', 2, 'C', 'D']})
df['id'] = df['info'].ix[::3].repeat(3).values
df_reshaped = df.pivot(index='id', columns='var', values='info')
# Discard the redundant 'id' column
df_reshaped = df_reshaped[['author', 'title']]
print df_reshaped

rawDescription: Reshaping Dataframe without ID variable

description: Reshaping Dataframe without ID variable


rawDescription: ElementTree, how to find neighbor element


code:
import csv
from StringIO import StringIO

i = """"Equipment","LNKEQP","METAST","METSER","MODSTA","METEOD"
"HLL_POS_00098",1,1,0,0,0
"TOY_GAT_00003",0,0,0,3,0
"NAT_POS_00010",0,3,0,3,0
"NAT_GAT_00002",0,0,0,0,0
"NAT_GAT_00001",0,0,0,4,0
A machine A is unavailable
"""

# Take only those lines that contain a comma.
j = "".join([line for line in StringIO(i).readlines() if ',' in line])

# Parse the taken lines as CSV.
reader = csv.reader(StringIO(j))
for line in reader:
    print line

rawDescription: How to get non-csv lines in csv file

description: Get non-csv lines in csv file


code:
mystr=""
mystr+=hex(dt.year)
mystr+=hex(dt.month)
mystr+=hex(dt.day)
mystr+=hex(dt.hour)
mystr+=hex(dt.minute)
mystr+=hex(dt.second)
print mystr

rawDescription: Convert time-string into individual hex bytes


code:
from sqlalchemy import Column, String, Integer, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Person(Base):
    __tablename__ = 'person'
    id = Column(Integer, primary_key=True)
    name = Column(String(250), nullable=False)
    job = Column(String(250), nullable=False)

engine = create_engine('sqlite:///persons.sqlite')
Base.metadata.create_all(engine)

session = sessionmaker(bind=engine)
s = session()

dct = {'John': 'doctor', 'Alice': 'typist'}

for key, value in dct.items():
    np = Person(name=key, job=value)
    s.add(np)

s.commit()

db_result = s.query(Person).all()
for row in db_result:
    print row.id, row.name, row.job

rawDescription: How to add all elements from python dictionary to database?

description: Add all elements from python dictionary to database


code:
def main():
    beanstalk = beanstalkc.Connection(host='localhost', port=11300)
    while True:
        job = beanstalk.reserve()
        print job.body
        job.delete()

if __name__ == '__main__':
    main()

rawDescription: How can I schedule an email to be sent at some point in the future in django?

description: Schedule an email to be sent at some point in the future in django


code:
import string
print ''.join(getAllTheLetters()) == string.lowercase

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

description: Iterate through a string in Python


code:
fulladdress = "213 house rd apt 1011"
apts = ['apt','apartment','unit','spc','space','trlr','lot','A','B','C','D','house']
first_part = ""

for search in apts:
    index = fulladdress.find(" %s " % search)

    if index != -1:
        first_part = fulladdress[:index]
        break

print first_part

rawDescription: Python split string with a list of words


code:
from bs4 import BeautifulSoup as bs

c_addr = []
id_addr = []
data = \
"""

Primary Location

       No. 4

       Private Drive,

       Sri Lanka ON  K7L LK

"""
soup = bs(data)

for i in soup.find_all('div'):
    # get data using "class" attribute
    addr = ""
    if i.get("class")[0] == u'address': # unicode string
        text = i.get_text()
        for line in text.splitlines(): # line-wise
            line = line.strip() # remove whitespace
            addr += line # add to address string
        c_addr.append(addr)

    # get data using "id" attribute
    addr = ""
    if int(i.get("id")) == 10: # integer
        text = i.get_text()
        # same processing as above
        for line in text.splitlines():
            line = line.strip()
            addr += line
        id_addr.append(addr)

print "id_addr"
print id_addr
print "c_addr"
print c_addr

rawDescription: Python extracting data from HTML using split


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

    def __iter__(self):
        return (self.__dict__[item] for item in sorted(self.__dict__))

def printer(x, y):
    print x, y

printer(*Point(2, 3))

rawDescription: Properly Implementing Python Star Operator for a Custom Class


code:
class MyThread(Thread):

    def __init__(self, sf):
        Thread.__init__(self)
        self.sf = sf

    def run(self):

        subprocess.call('notepad ' + self.sf)

def main():
    myThread = MyThread('test.txt')
    myThread.start()
    while myThread.isAlive():
        print "Still Alive"

if __name__ == '__main__':
    main()

rawDescription: Check for open text file

description: Check for open text file


code:
# Increment using timedelta
print 'This works:'
d = start_datetime
for i in range(5):
    print str(d) + ' ' + tz.tzname(d, is_dst=is_dst(d, tz))
    d += timedelta(minutes=30)  # Add 30 minutes
    d = tz.normalize(tz.localize(d)).replace(tzinfo=None)

# Increment by adding seconds to epoch
print 'This works too:'
epoch = datetime.utcfromtimestamp(0)
timestamp = (start_datetime - epoch).total_seconds()
for i in range(5):
    d = tz.normalize(datetime.fromtimestamp(timestamp, pytz.utc).astimezone(tz)).replace(tzinfo=None)
    print str(d) + ' ' + tz.tzname(d, is_dst=is_dst(d, tz))
    timestamp += 30 * 60  # Add 30 minutes

rawDescription: Dates and time zone codes around a DST change


code:
result = re.findall(r'Data\s+:\s+(.*)', output, re.M)[0]
print result

rawDescription: Create python map based on bash output

description: Create python map based on bash output


code:
import requests

# Variation 1
r = requests.post(url, data={'username': 'exampleuser'})

# Variation 2
r = requests.post(url, params={'username': 'exampleuser'})

# Followed by these lines
print r.status_code
print r.text

rawDescription: Generating Tableau Trusted Tickets with curl or Python?


code:
tag_dict = {}

for tag in request.POST.getlist('tags[]'):
    tag_dict[tag] = request.POST.getlist('creation_tags['+ tag +']')

print tag_dict

rawDescription: How to send the javascript dictionary object to django view?

description: Send the javascript dictionary object to django view


code:
from sklearn.preprocessing import OneHotEncoder
import itertools

# two example documents
docs = ["A B", "B B"]

# split documents to tokens
tokens_docs = [doc.split(" ") for doc in docs]

# convert list of of token-lists to one flat list of tokens
# and then create a dictionary that maps word to id of word,
# like {A: 1, B: 2} here
all_tokens = itertools.chain.from_iterable(tokens_docs)
word_to_id = {token: idx for idx, token in enumerate(set(all_tokens))}

# convert token lists to token-id lists, e.g. [[1, 2], [2, 2]] here
token_ids = [[word_to_id[token] for token in tokens_doc] for tokens_doc in tokens_docs]

# convert list of token-id lists to one-hot representation
vec = OneHotEncoder(n_values=len(word_to_id))
X = vec.fit_transform(token_ids)

print X.toarray()

rawDescription: One Hot Encoding for representing corpus sentences in python


code:
class Hint(object):
    def __init__(self, n, msg):
        self.n = n
        self.msg = msg

    def __call__(self, f):
        def wrap(*args, **kw):
            count = 1
            while True:
                ret = f(*args, **kw)
                if ret == False:
                    count += 1
                    if count > self.n:
                        print self.msg
                        count = 0
                else:
                    break
            return ret
        return wrap

def start_at(place):
    print "start at %d" % place
    return "start"

@Hint(3, "You may choose from 1, 2, 3.")
def start():
    print "What place will you start?"
    cmd = raw_input("> ")
    try:
        place = int(cmd)
        if place not in (1,2,3):
            return False
        else:
            return start_at, (place,)
    except ValueError:
        return False

def quit():
    print "I will quit"
    return "quit"

@Hint(4, "You may choose from yes and no.")
def begin():
    print "Would you like to begin?"
    cmd = raw_input("> ")
    if "yes" in cmd:
        print "Great!\n"
        return start, ()
    elif "no" in cmd:
        print "Bad!\n"
        return quit, ()
    else:
        return False

call_func, args = begin, ()
while True:
    ret = call_func(*args)
    if isinstance(ret, tuple):
        call_func, args = ret
    else:
        break

rawDescription: Infinite while loop reset when a function is run


code:
def propagate_focus_in_event(parent, event, *data):
    print 'focus-in', parent, event
    child = parent.get_child()
    if child.get_property('can-focus'):
        child.grab_focus()
    else:
        if not child.child_focus(gtk.DIR_TAB_FORWARD):
            parent.get_toplevel().child_focus(gtk.DIR_TAB_FORWARD)
    return True

rawDescription: PyGTK Entry widget in TreeViewColumn header


code:
for p in my_gen("ABC", range(4)):
   print p

rawDescription: Python Cartesian power generator over a list of powers