code:
from collections import namedtuple

class SpoofDict(dict):
    def __init__(self, *args, **kwargs):
        self.str_func = kwargs['str_func']
        del kwargs['str_func']

        dict.__init__(self, *args, **kwargs)

    def __str__(self):
        return self.str_func()

class MyObj(object):
    def __init__(self, y, z):
        r = namedtuple('row', 'a b')
        self.row = r(y, z)
        self.arbitrary = True
    def __getattr__(self, attr):
        return getattr(self.row, attr)

    def __dir__(self):
        return list(self.row._fields)

    def str_func(self):
        attrs = list(self.row._fields)
        str_dict = {}
        row = object.__getattribute__(self, 'row')
        for attr in attrs:
            str_dict[attr] = getattr(row, attr)
        return str(str_dict)

    def __getattribute__(self, attribute):
        if attribute == '__dict__':
            spoof_dict = SpoofDict(str_func=self.str_func)
            spoof_dict.update(object.__getattribute__(self, '__dict__'))
            return spoof_dict
        else:
            return object.__getattribute__(self, attribute)

if __name__=='__main__':
    m = MyObj(1, 2)
    print "dir(m) = {0}".format(dir(m))
    print "vars(m) = {0}".format(vars(m))
    print "m.row = {0}".format(m.row)
    print "m.arbitrary = {0}".format(m.arbitrary)

rawDescription: Is it possible to overload a call to vars(object) in Python?


code: print eval(x)

rawDescription: Python convert a string to a logic operator


code:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
from kivy.properties import StringProperty, NumericProperty

class IListItem(Widget):
    idx = NumericProperty(1)
    sid = StringProperty('')
    image = StringProperty('')
    label = StringProperty('')
    pass

class IList(BoxLayout):
    pass

class pplayHome(BoxLayout):

    def init_player(self):
        #global swid

        print self.ids
        ilayout = self.ids['ilist']

        for idx in range (0, 7):
            swid = IListItem(
                    sid = "s" + str(idx),
                    image = 'empty_image.png',
                    label = 'Image' + str(idx),
                    idx=idx
            )
            ilayout.add_widget(swid)
        children_ids = [il.sid for il in ilayout.children]
        print children_ids
        print ilayout.children[children_ids.index('s3')]

class pplayApp(App):
    def build(self):
        # Window.clearcolor = (1,1,1,1)
        Window.borderless = True
        Window.size = 275, 1080
        homeWin = pplayHome()
        homeWin.init_player()
        return homeWin

if __name__ == "__main__":
    pplayApp().run()

rawDescription: converting static widget tree to dynamic using python and .kv file

description: Converting static widget tree to dynamic using python and .kv file


code:
import timeit

NUM_ITERATIONS = 5000
NUM_BENCHMARKS = 50

def a():
    x = 0
    for i in range(NUM_ITERATIONS):
        for j in range(NUM_ITERATIONS):
            x+=1
    return x

def b():
    x = 0
    r = range(NUM_ITERATIONS)
    for i in range(NUM_ITERATIONS):
        for j in r:
            x+=1
    return x

def c():
    x = 0
    r1 = range(NUM_ITERATIONS)
    r2 = range(NUM_ITERATIONS)
    for i in r1:
        for j in r2:
            x+=1
    return x

def d():
    x = 0
    for i in xrange(NUM_ITERATIONS):
        for j in xrange(NUM_ITERATIONS):
            x += 1
    return x

def e():
    x = 0
    r = xrange(NUM_ITERATIONS)
    for i in xrange(NUM_ITERATIONS):
        for j in r:
            x += 1
    return x

print "A: %.4f" % (timeit.timeit(a, number=NUM_BENCHMARKS))
print "B: %.4f" % (timeit.timeit(b, number=NUM_BENCHMARKS))
print "C: %.4f" % (timeit.timeit(c, number=NUM_BENCHMARKS))
print "D: %.4f" % (timeit.timeit(d, number=NUM_BENCHMARKS))
print "E: %.4f" % (timeit.timeit(e, number=NUM_BENCHMARKS))

rawDescription: Should I assign a range outside of a loop in Python?


code:
f = [[0]*21]*21
for nested in f[:3]:
    print id(nested)

f = [[0 for _ in range(21)] for _ in range(21)]
for nested in f[:3]:
    print id(nested)

rawDescription: How do 2 dimentional lists work in Python


code:
def parse_string_to_command(s):
    if re.match('(.*?\(\))',s):
        print "This is a function"
    elif re.match('^\{.*\}$',s):
        print "This is kind of like variables 1"
    elif re.match('^\[.*\]$',s):
        print "This is kind of like variables 2"

rawDescription: building a parser in python

description: Building a parser in python


code:
import pandas as pd
import pylab as pl
import numpy as np

class myClass(object):
    def __init__(self, frameType='All'):
        def method1():
            myFrame = pd.DataFrame(
                {'c1': [1, 2, 3], 'c2': [4, 5, 6], 'c3': [7, 8, 9]})
            return myFrame

        def method2():
            myFrame = pd.DataFrame(
                {'c1': [.1, .2, .3], 'c2': [.4, .5, .6], 'c3': [.7, .8, .9]})
            return myFrame

        def makingChoice(self):
            if self.frameType == 'All':
                variable = method1() + method2()
            elif self.frameType == 'a':
                variable = method1()
            elif self.frameType == 'b':
                variable = method2()
            else:
                variable = pd.DataFrame(
                    {'c1': [0, 0, 0], 'c2': [0, 0, 0], 'c3': [0, 0, 0]})
            # print 'FROM __init__ : %s' % variable
            return variable
        self.frameType = frameType
        self.cObject = makingChoice(self)  # object created by the class

    def __str__(self):
        return str(self.cObject)

    def plotData(self):
        self.fig1 = pl.plot(self.cObject['c1'], self.cObject['c2'])
        self.fig2 = pl.plot(self.cObject['c1'], self.cObject['c3'])
        pl.show()

class myClassAv(myClass):
    def __init__(self, frameType='All'):
        myClass.__init__(self, frameType)

        def addingCol(cObject):
            print 'CURRENT cObject \n%s' % cObject  # the object is visible
            cObject['average'] = cObject.mean(axis=1)
            # creating new column works
            print 'THIS WORKS IN GENERAL\n%s' % str(cObject['average'])
            return cObject

        addingCol(self.cObject)

    def plotData(self):
        # Function to add new plot to already existing plots
        self.fig3 = pl.plot(self.cObject['c1'], self.cObject['average'])

if __name__ == '__main__':
    myObject1 = myClass()
    print 'myObject1 =\n%s' % myObject1
    myObject1.plotData()
    myObject2 = myClass('a')
    print 'myObject2 =\n%s' % myObject2
    myObject3 = myClass('b')
    print 'myObject3 =\n%s' % myObject3
    myObject4 = myClass('c')
    print 'myObject4 =\n%s' % myObject4

    myObject5 = myClassAv('a')
    print 'myObject5 =\n%s' % myObject5
    myObject5.plotData()

rawDescription: creating subclass form class returning pandas dataFrame

description: Creating subclass form class returning pandas dataFrame


code:
from __main__ import *
print x

rawDescription: Pass variable between python scripts

description: Pass variable between python scripts


code:
import datetime

for creation_date in ('2016-02-05', '2016-01-29', '2015-12-31',):
    creation_date = datetime.datetime.strptime(creation_date, '%Y-%m-%d')
    print '\nBefore:', creation_date
    if creation_date.day in (29, 30, 31):
        creation_date = creation_date.replace(day=28)
    print 'After: ', creation_date

rawDescription: Assume date manually - Updating date with own assumption


code:
n = len(mat)
cluster_dict = dict()
for i in range(0, 6):
    new_cluster_id = n+i
    old_cluster_id_0 = linkage_matrix[i, 0]
    old_cluster_id_1 = linkage_matrix[i, 1]
    combined_ids = list()
    if old_cluster_id_0 in cluster_dict:
        combined_ids += cluster_dict[old_cluster_id_0]
        del cluster_dict[old_cluster_id_0]
    else:
        combined_ids += [old_cluster_id_0]
    if old_cluster_id_1 in cluster_dict:
        combined_ids += cluster_dict[old_cluster_id_1]
        del cluster_dict[old_cluster_id_1]
    else:
        combined_ids += [old_cluster_id_1]
    cluster_dict[new_cluster_id] = combined_ids
    print cluster_dict

rawDescription: Python get clustered data-Hierachical Clustering


code: print txt.rsplit('\n',1)[0]

rawDescription: How to not print the last line

description: Not print the last line


code:
sorted_bigrams = sorted(bigrams.items(), key = lambda pair:pair[1], reverse = True)

total_count = 0
for bigram, count in sorted_bigrams:
    if bigram == ("interesting", "news"):
        print count
        total_count += count
print total_count

rawDescription: how can i store the printing result, obtained by "for statement" variable?

description: Store the printing result, obtained by "for statement" variable


code:
def myfunc(n):
    return n**2

def parse_setup(filename):
    globalsdict = {'__builtins__': None, 'myfunc': myfunc}  # put predefined things here
    localsdict = {}  # will be populated by executed script
    execfile(filename, globalsdict, localsdict)
    return localsdict

results = parse_setup('setup.py')
print results  # {'y': 16, 'x': 5, 'z': 25, 'mylist': [1, 2, 3]}

rawDescription: Parse python setup file

description: Parse python setup file


code:
mytuple = ('new','lets python','python 2.7')
print "%s experience, %s with %s" % mytuple

rawDescription: Printing formatted tuple values

description: Printing formatted tuple values


code:
import sys
import operator
import linecache

x=0
y=7  # to skip
z=3  # to print

#g = open('tmp','a+')
while x<23:
    x+=1
    print 'x==',x
    if operator.mod(x, y)==0:
        #curr = g.tell()
        for i in range(x,x+z):
            block=linecache.getline('poem.txt', i)
            print 'block==',repr(block)
            #g.write(block)
            linecache.clearcache()
            #g.seek(curr)
            #lines = g.read()
            #find(lines)

    else:
        pass

#g.close()

rawDescription: Using python to process a chunk of a file every x lines


code:
import argparse

parser=argparse.ArgumentParser()
first_action=parser.add_argument('--foo',dest='cat',default=None)

class BarAction(argparse.Action):
    def __call__(self,parser,namespace,values,option_string=None):
        didfoo=getattr(namespace,first_action.dest,first_action.default)
        if(didfoo == first_action.default):
            parser.error( "foo before bar!")
        else:
            setattr(namespace,self.dest,values)

parser.add_argument('--bar',action=BarAction,
                    help="Only use this if --foo is set")

#testing.
print parser.parse_args('--foo baz'.split())
print parser.parse_args('--foo baz --bar cat'.split())
print parser.parse_args('--bar dog'.split())

rawDescription: python, argparse: enable input parameter when another one has been specified

description: Python, argparse: enable input parameter when another one has been specified


code:
body = MIMEMultipart()
for name, (filename, contents, mimetype) in files.items():
    part = MIMEText(contents, _subtype=mimetype.partition('/')[-1], _charset='utf8')
    part.add_header('Content-Disposition', 'attachment', filename=filename)
    body.attach(part)

post_body = body.as_string().partition('\n\n')[-1]
content_type = body['content-type']
print post_body
print content_type

rawDescription: how to POST multipart list of JSON/xml files using python requests


code:
import itertools as it

def partitions(n, k):
    for c in it.combinations(xrange(n+k-1), k-1):
        yield [b-a-1 for a, b in zip((-1,)+c, c+(n+k-1,))]

def count_subsequences(pool, length):
    if length < 2:
        return pool**length
    return pool * (pool-1)**(length-1)

def count_sequences(length):
    counts = [[count_subsequences(i, j) for j in xrange(length+1)] \
              for i in [10, 26]]

    print 'Partition {:>18}'.format('Sequence count')

    total = 0
    for a, b, c in partitions(length, 3):
        subtotal = counts[0][a] * counts[1][b] * counts[1][c]
        total += subtotal
        print '{} {:18}'.format((a, b, c), subtotal)
    print '\nTOTAL {:22}'.format(total)

rawDescription: How to generate this custom alpha-numeric sequence?

description: Generate this custom alpha-numeric sequence


code:
f=db.search(Query().time != 0)[-5:]
print ', '.join(str(x['glucose']) for x in f)

rawDescription: How to save information to json as array python

description: Save information to json as array python


code:
def _handler_helper():
   def fna():
      print "a"
      pass

   def fnb():
      print "b"
      pass

   m = {"a":fna,"b":fnb}
   return lambda x:m[x]

get_handler_by_type = _handler_helper()

rawDescription: How to encapsulate handlers in an efficient manner in Python?

description: Encapsulate handlers in an efficient manner in Python


code:
class A(object):
    def __init__(self):
        self.definitions = self.get_all_definitions()

    @classmethod
    def get_all_definitions(cls):
        dct = {}
        while True:
            dct.update(cls.get_definitions())
            if cls == A:
                return dct
            else:
                cls = cls.__bases__[0] # only first parent class is considered
                                       # multiple inheritance is not supported

    @classmethod
    def get_definitions(cls):
        return {'A' : 1}

class B(A):
    @classmethod
    def get_definitions(cls):
        return {'B' : 22}

class CA(B):
    @classmethod
    def get_definitions(cls):
        return {'C' : 333}

class CB(B):
    @classmethod
    def get_definitions(cls):
        return {'C' : 444}

# Test
ca = CA()
print ca.definitions

cb = CB()
print cb.definitions

rawDescription: Inherit dictionary for multiple levels of ancestry


code:
with open('data.txt') as f:
    data = f.readlines()
    diffs = [0] + [float(j) - float(data[i]) for i,j in enumerate(data[1:])]

print diffs

rawDescription: Difference of consecutive float numbers in a column


code:
import re

strings = [
    '}# => 2[1 HMDB00001 ,2 HMDB00002]',
    '}# => 5[1 HMDB00001 ,2 HMDB00002, 3 HMDB00003 ,4 HMDB00004,5 HMDB00005]',
    '}# => 1[1 HMDB00001]',
]

for s in strings:
    mat = re.search(r'\[(.*)\]', s)
    elements = map(str.strip, mat.group(1).split(','))
    print elements

rawDescription: Python string parsing from .txt

description: Python string parsing from .txt


code:
N = int(1e6)
d = np.arange(N, dtype=np.int64)

print np.allclose(nb_sum(d), cy_sum(d))   # True
print np.allclose(nb_sum2(N), cy_sum2(N))  # True

rawDescription: why is numba so much faster on this simple summation?

description: Why is numba so much faster on this simple summation


code:
import re

full_string = 'xxxxxbxxxxx'
if re.search('a|b', full_string):
    print 'found'

rawDescription: Test if one OR an other substring is in a string, in a DRY way


code:
for i in range(10):
   print ''.join(random.sample(lst, 5))

rawDescription: how to get only some of the strings in a list while using random,shuffle? in python

description: Get only some of the strings in a list while using random,shuffle? in python


code:
import pickle
class Restorable(object):
    def __init__(self, a=1, b=1):
        self.a = a
        self.b = b
    def restore(self, serialized_object):
        original = pickle.loads(serialized_object)
        self.__dict__.update(original.__dict__)

r = Restorable()
r.a = 1000
r.b = 2000
saved_state = pickle.dumps(r)
r.a = -1
r.b = -1
r.restore(saved_state)
print r.a, r.b

rawDescription: Storing serialized object in database then restoring object state from key


code:
x = 'january'
if x:
    print x

rawDescription: Python - Novice: Why this simple if-elif (replication of the C case block) always produces the same result?


code:
timer = Timer('print func()', 'from __main__ import func')
print timer.timeit(1)

rawDescription: Python timeit and program output

description: Python timeit and program output


code:
data = {u'City':u'Ciri\xe8'}
data['City']
print data['City']

rawDescription: Transform unicode string in python

description: Transform unicode string in python


code:
stopQue=Queue.Queue()

def listenThread(counter):
    while True:
        if queue.empty()!=True:
            outcome = queue.get()
            print outcome
            counter -=1
            print counter
        if stopQue.empty()!=True:
            break
    print 'Exiting Listening Thread'

rawDescription: Correct usage of multithreading and Queue module in data collection application written in Python


code:
class Bar(object):
    def x(self):
        return 0

setattr(Bar, 'x', property(Bar.x))

print Bar.x
# <property object at 0x04D37270>
print Bar().x
# 0

rawDescription: How do I assign a property to an instance in Python?

description: Assign a property to an instance in Python


code: print max(names.values(), key=lambda x: x if not isinstance(x, str) else float("-inf"))

rawDescription: getting max(dict_name.values()) to ignore strings

description: Getting max(dict_name.values()) to ignore strings


code:
inp_check=["A|1" , "D|41", "F|34", 'F|56']
inp_master=["A","B", "C", "D", "E", "F"]
output=[]
for value in inp_master:
    check=0
    for check_value in inp_check:
        if value in check_value:
            check=1
            output.append(check_value)
    if check ==0:
        output.append("NULL")
print output

rawDescription: Comparing two lists and inserting Nulls where necessary


code: print A.bs.property.mapper.class_

rawDescription: Python+SQLAlchemy: Getting mapper class from InstrumentedList?


code: print re.sub(u'番地$', u'', add.replace(u'丁目', u'－'))

rawDescription: Python - regex with Japanese letters matches only one character


rawDescription: Python iterate over two lists simultaneously


code:
example = ""
for i in range(1,4):
    example = example + str(i)+". " +raw_input("enter the example !") +"\n"
print example

rawDescription: connecting and separating string inputs

description: Connecting and separating string inputs


code:
for i, x in zip(range(index + 1), itertools.cycle(lst)):
    print i, x

rawDescription: Relooping the list

description: Relooping the list


code:
c = [a[index] for index in b]
print c

rawDescription: Getting a sublist of a Python list, with the given indices?


code:
sent_tokenize(train_text[11])
for sent in sent_tokenize(train_text[11]):
    print sent
    print '--------'

rawDescription: Use of PunktSentenceTokenizer in NLTK

description: Use of PunktSentenceTokenizer in NLTK


code:
k=['john']
k1=['john', 'is', 'sleeping']
from itertools import product
print [item for item in product(k1, k)]

rawDescription: Concatenating items from two lists to form n-grams using python


code:
import serial
import numpy as np
from matplotlib import pyplot as plt
from threading import Thread,Event
import time

#A simple ring buffer to keep the plotting data in
class RingBuffer(object):
    def __init__(self,size):
        self.size = size
        self._size = 2*size
        self._buffer = np.zeros(self._size)
        self._idx = 0

    def insert(self,val):
        idx = self._idx%self.size
        self._buffer[idx] = val
        self._buffer[idx+self.size] = val
        self._idx+=1
        if self._idx > self.size:
            self._idx-=self.size

    def get(self):
        start_idx = (self._idx)%self.size
        end_idx = start_idx+self.size
        return self._buffer[start_idx:end_idx]

#A thread to handle plotting and reading data from the serial port
class Plotter(Thread):
    def __init__(self,stream,stop,pause):
        Thread.__init__(self)
        self.stream = stream
        self.fig = plt.figure()
        self.ax = plt.axes()
        self.size = 100
        self.xdata = np.arange(self.size)
        self.ydata = RingBuffer(self.size)
        self.line, = self.ax.plot(self.ydata.get())
        plt.ylim([24,29])
        self.stop = stop
    self.pause = pause

    #The main loop of the thread, invoked with .start()
    def run(self):
        while not self.stop.is_set():
            if self.pause.is_set():
                continue
            raw_data = self.stream.readline().rstrip()
            if len(raw_data) <= 6:
                try:
                    data = float(raw_data)
                except ValueError:
                    print "Not a float"
                else:
                    if data <= 100:
                        self.ydata.insert(data)
                        self.line.set_xdata(self.xdata)
                        self.line.set_ydata(self.ydata.get())
                        plt.draw()

            #sleep statement to reduce processor load
        time.sleep(1)

#this is implemented to facilitate use through something like Ipython easily
class Controller(object):
    def __init__(self):

        #These events control execution of the thread main loop
        self.stop = Event()
        self.pause = Event()
        self.stream = serial.Serial('/dev/tty.usbmodemfa131', 9600)
        self.plotter_thread = Plotter(self.stream,self.stop,self.pause)
        self.plotter_thread.start()

def main():
    con = Controller()

    #Loop over terminal input
    #possible commands are stop, pause and unpause
    while True:
        command = raw_input("cmd >>>")
        if command == "stop":
            con.stop.set()
            con.plotter_thread.join()
            break
        elif command == "pause":
            con.pause.set()
        elif command == "unpause":
            con.pause.clear()
        else:
            print "unrecognised command"

if __name__ == "__main__":
    main()

rawDescription: Plotting Serial data in Python how to pause matplotlib figure?


code:
with open("your_file.csv") as f:
    for x,y in itertools.groupby(sorted(map(str.split, f.read().strip().split("\n"))), key = lambda x:x[0]):
        print x,len(list(y))

rawDescription: Get the count of the each date entry from onr of the raw from CSV file


code:
a = "food, drink, vege"
print {item.strip() for item in a.split(",")}

rawDescription: How to create a list while avoiding duplicate?

description: Create a list while avoiding duplicate


code: exec cc in globals(), dd

rawDescription: Why is an empty function call in python around 15% slower for dynamically compiled python code


code:
import csv

ins = open("file.csv", "rb")
for row in csv.reader(ins):
    print sum(map(int, row))
ins.close()

rawDescription: Import csv into python and sum the row of numbers


code:
from collections import defaultdict
from collections import Counter
from itertools import chain

L = [[['A', 'uni'], [['B', 'uni'], ['C', 'uni'], ['D', 'uni'], ['E', 'uni']]], [['E', 'uni'], [['A', 'uni'], ['F', 'uni'], ['G', 'uni']]]]

d = defaultdict(Counter)
for publication in L:
    authors = [tuple(a) for a in chain([publication[0]], publication[1])]
    for author in authors:
        d[author].update(authors)

for k, v in d.iteritems():
    print k, [(author[0], author[1], counter)
              for author, counter in v.iteritems() if author[0] != k[0]]

rawDescription: author relation analysis with python

description: Author relation analysis with python


code:
from sqlalchemy.ext.orderinglist import ordering_list

# Photo and PhotoInSet stay the same...

class PhotoSet(Base):
    __tablename__ = 'photo_set'
    id = Column(Integer, primary_key=True)
    name = Column(String(256))
    description = Column(Text)
    photo_id = Column(Integer, ForeignKey('photos.id'))
    photos = relationship('PhotoInSet',
       order_by="PhotoInSet.position",
       collection_class=ordering_list('position'),
       backref=backref('set', lazy='select'))

# Sample usage...
session = Session()

# Create two photos, add them to the set...
p_set = PhotoSet(name=u'TestSet')

p = Photo(title=u'Test')
p2 = Photo(title='uTest2')

p_set.photos.append(PhotoInSet(photo=p))
p_set.photos.append(PhotoInSet(photo=p2))
session.add(p_set)

session.commit()
print 'Original list of titles...'
print [x.photo.title for x in p_set.photos]
print ''

# Change the order...
p_set.photos.reverse()
# Any time you change the order of the list in a way that the existing
# items are in a different place, you need to call "reorder". It will not
# automatically try change the position value for you unless you are appending
# an object with a null position value.
p_set.photos.reorder()
session.commit()

p_set = session.query(PhotoSet).first()
print 'List after reordering...'
print [x.photo.title for x in p_set.photos]

rawDescription: Updating a field in an Association Object in SQLAlchemy


code:
from pyparsing import *

LBRACK,RBRACK,LPAREN,RPAREN = map(Suppress,"[]()")
and_op = CaselessKeyword('and')
or_op = CaselessKeyword('or')
word = ~(and_op | or_op) + Word(alphanums + '.-/&')
field_qualifier = Group(LBRACK + OneOrMore(word) + RBRACK)

search_term = (Group(OneOrMore(word) | quotedString)('search_text') +
               Optional(field_qualifier)('field'))
expr = Forward()
atom = search_term | (LPAREN + expr + RPAREN)
or_term = atom + ZeroOrMore(or_op + atom)
and_term = or_term + ZeroOrMore(and_op + or_term)
expr << and_term

# define classes for parsed structure
class ParsedObject(object):
    def __init__(self, tokens):
        self.tokens = tokens
    def queryString(self):
        '''Abstract method to be overridden in subclasses'''

class SearchTerm(ParsedObject):
    def queryString(self):
        text = ' '.join(self.tokens.search_text)
        if self.tokens.field:
            return '%s: %s' % (' '.join(f.lower()
                                        for f in self.tokens.field[0]),text)
        else:
            return text
search_term.setParseAction(SearchTerm)

class BinaryOperation(ParsedObject):
    def queryString(self):
        joinstr = ' %s ' % self.op
        return joinstr.join(t.queryString()
                                for t in self.tokens[0::2])
class OrOperation(BinaryOperation):
    op = "OR"
class AndOperation(BinaryOperation):
    op = "AND"
or_term.setParseAction(OrOperation)
and_term.setParseAction(AndOperation)

class Expr(ParsedObject):
    def queryString(self):
        return '(%s)' % self.tokens[0].queryString()
expr.setParseAction(Expr)

test = """("breast neoplasms"[MeSH Terms] OR breast cancer[Acknowledgments]
OR breast cancer[Figure/Table Caption] OR breast cancer[Section Title]
OR breast cancer[Body - All Words] OR breast cancer[Title]
OR breast cancer[Abstract] OR breast cancer[Journal])
AND (prevention[Acknowledgments] OR prevention[Figure/Table Caption]
OR prevention[Section Title] OR prevention[Body - All Words]
OR prevention[Title] OR prevention[Abstract])"""

res = expr.parseString(test)[0]
print res.queryString()

rawDescription: pyparsing one query format to another one

description: Pyparsing one query format to another one


code:
import json
from time import gmtime, strftime

now = str(strftime("%Y-%m-%dT%H:%M:%S", gmtime()))
n = 5000.0

message = {"columns": [["x", now, now, now], ["y", n, n, n]]}
print "message: " + json.dumps(message, indent=4, separators=(',', ': '))

rawDescription: Can we republish messages in PUBNUB

description: Can we republish messages in PUBNUB


code:
class A(object):
    @staticandinstancemethod
    def B(self, x, y):
        print self is None and "static" or "instance"
A.B(1,2)
A().B(1,2)

rawDescription: Can a method be used as either a staticmethod or instance method?


code:
color =['orange', 'green', 'yellow', 'violet', 'blue']
sentences = ['The green monkey age the blue egg', 'How many yellow monkey"s are in the green forest']

for sent in sentences:
  print sent
  for col in color:
    print "", col, sent.count(col)

rawDescription: while loop counter comparing two lists

description: While loop counter comparing two lists


code:
import inspect
from django.db import models
from django.db.models import Manager

class LogManager(Manager):
    def get_queryset(self):
        print inspect.stack()  # in py3.4 the [2] element was the actual caller outside Manager so I used [2:]
        return super(LogManager, self).get_queryset()

class Abc(models.Model):
    objects = LogManager()

rawDescription: get call stack for Model Classes in Django

description: Get call stack for Model Classes in Django


rawDescription: Asterisk art in python

description: Asterisk art in python


code:
for attr in student.studattr:
    print attr, getattr(student, attr)

rawDescription: Python: Concatenate two strings for getting a variable from a class


code:
'가' + 'ㄱ'
u'가' + u'ㄱ'
print u'가' + u'ㄱ'

rawDescription: How to concatenate multiple unicode string?

description: Concatenate multiple unicode string


code:
#!/usr/bin/env python

import sys

with open("test.txt") as file:
    for line in file:
        if "got" in line:
            print "found: {0}".format(line)

rawDescription: Python: Iterate through an html file

description: Python: Iterate through an html file


code:
import collections
items = [['foo',1],['baz',1],['foo',0],['bar',3],['foo',1],['bar',2],['baz',2]]

totals = collections.defaultdict(int)
for key, value in items:
    totals[key] += value

print totals

rawDescription: Algorithm in Python To Solve This Problem


code: print request.form.keys()

rawDescription: Getting information from HTML Form with checkboxes with Python Flask


code:
class Dog(Animal):
    def __init(self, numlegs):
        Animal.__init__(numlegs)

    def bark(self):
        print "Arf!"
#some other code maybe
dog = Dog(4)
#more other code
dog.weapon = Weapon()
#even more code
dog.weapon.fire()

rawDescription: How to add same methods/properties to a set of subclasses derived from common class

description: Add same methods/properties to a set of subclasses derived from common class


code:
import re

regex = [' & ',' / ',' \+ ',' and ',' und ',' ft\. ',' feat\. ']

def splitter(input, inc=0):
    if inc == len(regex):
        print "return point reached"
        return input
    print inc, input
    output = []
    if type(input) is list:
        for i in input:
            o = re.split(regex[inc], i)
            if not o[0] == i:
                for x in o:
                    output.append(x)
            else:
                output.append(i)
    else:
        o = re.split(regex[inc], input)
        if not o[0] == input:
            for x in o:
                output.append(x)
        else:
            output.append(input)
    inc += 1

    # I added the return statement here:
    return splitter(output, inc)

print(splitter("Benga und Welsh ft. Warrior Queen and Skream feat. Benny Ill"))

rawDescription: python recursive function doesn't return anything at all

description: Python recursive function doesn't return anything at all


code:
def Cel2Fah(temp):
    fah = temp * 9.0/5.0 + 32

    return fah

print "{0:.2f}".format(Cel2Fah(29.0))

rawDescription: python 2.7 function to convert temprature

description: Python 2.7 function to convert temprature


code:
prices = soup.find('table', class_='historical_price')
rows = prices.find_all('tr')
print rows[1]

rawDescription: python beautifulsoup4 parsing google finance data

description: Python beautifulsoup4 parsing google finance data


code:
from inspect import stack

print stack()[1][1]

rawDescription: Python : Name of the calling file in different file


code:
import base64
import urllib2
import re

def get_video_url(page_url):
    html = urllib2.urlopen(url).read()
    match = re.search("googleCode = '(.*?)'", html)
    if match is None:
        raise ValueError('googleCode not found')
    googleString = base64.b64decode(match.group(1))
    match = re.search('","(.*?)"', googleString)
    if match is None:
        raise ValueError("didn't find video url")
    return match.group(1)

url = 'http://www.wimp.com/titanicpiano/'
print get_video_url(url)

rawDescription: Rendering multiple webpages in memory using QtWebKit breaks on second page, why?


code:
def main():
    lst = []
    while True:
        data = input("Enter 3 numbers: ")
        if not data:
            break
        lst.append([int(a) for a in data.split()])
    print lst
    # to get more output....
    for row in lst:
        print('{} {} {}'.format(*row))
    print('column 3 sum is {}'.format(sum(row[2] for row in lst)))
main()

rawDescription: Lists and columns in Python

description: Lists and columns in Python


code:
from binascii import hexlify, unhexlify

#Create a string of all possible byte values
allbytes = ''.join([chr(i) for i in xrange(256)])
print 'allbytes'
print repr(allbytes)

print '\nhex list'
print [hex(ord(v))[2:].zfill(2) for v in allbytes]
hexstr = hexlify(allbytes)

print '\nhex string'
print hexstr
newbytes = ''.join([chr(int(hexstr[i:i+2], 16)) for i in xrange(0, len(hexstr), 2)])

print '\nNew bytes'
print repr(newbytes)

print '\nUsing unhexlify'
print repr(unhexlify(hexstr))

rawDescription: python file I/O with binary data

description: Python file I/O with binary data


code:
searchstringsProblem = 'BIG Problem Line'
searchstringsGPSLoc = 'GPS Info'
matches = []

with open("test.txt") as f:
    problem = False
    problem_line = ""

    for line in f:
        if not problem and searchstringsProblem in line:
            problem_line = line.strip()
            problem = True
        elif problem and searchstringsGPSLoc in line:
            latitude = ""
            longitude = ""
            for new_line in f:
                if "Longitude" in new_line:
                    longitude = new_line.split("=")[1].strip()
                elif "Latitude" in new_line:
                    latitude = new_line.split("=")[1].strip()
                if longitude and latitude:
                    break;

            if latitude and longitude:
                matches.append((problem_line, line.strip(), latitude, longitude))
                problem = False

for item in matches:
    print item

rawDescription: Using Python to run nested string searches


rawDescription: Trouble parsing API JSON output with python using 'requests'


code: print '<img src="' + site + '">'

rawDescription: Join string containg quotation marks

description: Join string containg quotation marks


code:
import csv
from StringIO import StringIO

total = 0
for row in csv.reader(StringIO("1,2,3,4")):
    for col in row:
        total += int(col)

print total    # prints 10

rawDescription: Looping through columns in a .csv files in Python


code:
class Tim:
   def speak(self):
       print "Death awaits you with sharp pointy teeth!"

rawDescription: Should I use a class in this: Reading a XML file using lxml


code:
def HeapSort(A,T):
    def heapify(A):
        start = (len(A) - 2) / 2
        while start >= 0:
            siftDown(A, start, len(A) - 1)
            start -= 1

    def siftDown(A, start, end):
        root = start
        while root * 2 + 1 <= end:
            child = root * 2 + 1
            if child + 1 <= end and T.count(A[child]) < T.count(A[child + 1]):
                child += 1
            if child <= end and T.count(A[root]) < T.count(A[child]):
                A[root], A[child] = A[child], A[root]
                root = child
            else:
                return

    heapify(A)
    end = len(A) - 1
    while end > 0:
        A[end], A[0] = A[0], A[end]
        siftDown(A, 0, end - 1)
        end -= 1

if __name__ == '__main__':
    text = "the quick brown fox jumped over the the quick brown quick log log"
    heap = list(set(text.split()))
    print heap

    HeapSort(heap,text)
    print heap

rawDescription: creating a heap in python

description: Creating a heap in python


code:
req = urllib2.Request('http://www.taringa.net/envivo/ajax.php')
req.add_header('User-Agent', 'Mozilla')
resp = urllib2.urlopen(req)
print resp.code  # Gives 200.
print resp.read()  # Gives the HTML of the page.

rawDescription: 403 status code while trying to access webpage from Python


code:
lines = '''\
NA|polymerase|KC545393|Bundibugyo_ebolavirus|EboBund_112_2012|NA|2012|Human|Democratic_Republic_of_the_Congo
NA|VP24|KC545393|Bundibugyo_ebolavirus|EboBund_112_2012|NA|2012|Human|Democratic_Republic_of_the_Congo
NA|VP30|KC545393|Bundibugyo_ebolavirus|EboBund_112_2012|NA|2012|Human|Democratic_Republic_of_the_Congo
'''.splitlines(True)
for line in lines:
    elems = line.split('|', 3)
    print '|'.join(elems[1:3])

rawDescription: How to Print a string to a specific character?

description: Print a string to a specific character


rawDescription: Convert floating point array to integer array?


code:
from BeautifulSoup import BeautifulSoup
import urllib2

page = urllib2.urlopen('http://www.example.com')
soup = BeautifulSoup(page.read())
print soup.find('head')

rawDescription: Read HEAD contents from HTML

description: Read HEAD contents from HTML


code:
import pandas as pd
import numpy as np
A1 = pd.read_table('Test data.tsv', encoding='ISO-8859-1', header = 2)

A1.columns = ['time',2,3,4,5,6,7,'freq',9,10,11,12,13,'temp',15,16,17,18,19]

A1truncated = A1[(A1.temp >= 25) & ( A1.temp <= 350.1)]

A1averaged = A1truncated.groupby(['temp'], as_index=False)['freq'].mean()
A1averaged = np.around(A1averaged, decimals=1)
new_index = pd.Index(np.arange(250, 3500), name='temp')

A1averaged['temp'] = A1averaged['temp'] * 10
A1indexed = A1averaged.set_index('temp').reindex(new_index).reset_index()
A1indexed['temp'] = A1indexed['temp'] / 10
print A1indexed.tail()
#       temp       freq
#3245  349.5  5830065.6
#3246  349.6  5830043.5
#3247  349.7  5830046.3
#3248  349.8  5830025.3
#3249  349.9  5830015.6

rawDescription: Inserting rows into dataframe by reindexing


code:
"""fogbugzreminder

Reminds the user to include a FogBugz case reference in their commit message if none is specified
"""

from mercurial import commands, extensions
import re

RE_CASE = re.compile(r'(case):?\s*\d+', re.IGNORECASE)
RE_CASENUM = re.compile(r'\d+', re.IGNORECASE)

def commit(originalcommit, ui, repo, **opts):

    haschange = False
    for changetype in repo.status():
        if len(changetype) > 0:
            haschange = True

    if not haschange and ui.config('ui', 'commitsubrepos', default=True):
        ctx = repo['.']
        for subpath in sorted(ctx.substate):
            subrepo = ctx.sub(subpath)
            if subrepo.dirty(): haschange = True

    if haschange and not opts["nofb"] and not RE_CASE.search(opts["message"]):

        casenumResponse = ui.prompt('*** Please specify a case number, x to abort, or hit enter to ignore:', '')
        casenum = RE_CASENUM.search(casenumResponse)

        if casenum:
            opts["message"] += ' (Case ' + casenum.group(0) + ')'
            print '*** Continuing with updated commit message: ' + opts["message"]
        elif (casenumResponse == 'x'):
            ui.warn('*** User aborted\n')
            return False

    return originalcommit(ui, repo, **opts)

def uisetup(ui):
    entry = extensions.wrapcommand(commands.table, "commit", commit)
    entry[1].append(('', 'nofb', None, ('suppress the fogbugzreminder warning if no case number is present in the commit message')))

rawDescription: How can I set or modify the commit message from a mercurial extension?

description: Set or modify the commit message from a mercurial extension


code:
for x in range(-1,2):
    for y in range(-1,2):
        if (x, y) != (0, 0):
            if theProblem[stateX + x, stateY + y] == '|':
                print "WALL"
            else:
                print "GO"

rawDescription: Cleaner way to compare array values?

description: Cleaner way to compare array values


code:
import re
url = 'http://xyzzy.com?Python=fantastic&paxdiablo=still%20awesome'
args = re.findall(r'[?&]([^?&]*=[^?&]*)', url)
for arg in args:
    (object,property) = arg.split("=")
    print object, "is", property.replace('%20',' ')

rawDescription: passing a variable instead of input file to sed

description: Passing a variable instead of input file to sed


code:
# Read the array from disk
new_data = np.loadtxt('test.txt')

# Note that this returned a 2D array!
print new_data.shape

# However, going back to 3D is easy if we know the
# original shape of the array
new_data = new_data.reshape((4,5,10))

# Just to check that they're the same...
assert np.all(new_data == data)

rawDescription: How to write a multidimensional array to a text file?

description: Write a multidimensional array to a text file


code:
def plot_ellpises(ell_groups):
    print 'GMM Plot Result'
    fig, ax = plt.subplots()
    colors = ax._get_lines.color_cycle
    for ell in ell_groups:
        xy, width, height, angle = ell
        ell = mpl.patches.Ellipse(xy=xy, width=width, height=height,
                                  angle = angle, facecolor=next(colors))
        ell.set_alpha(0.5)
        ax.add_patch(ell)
    ax.autoscale()
    ax.set_aspect('equal')
    return plt.show()

rawDescription: Matplotlib & Python: assign a color when doing iterateive plotting


code:
import inspect
import mymodule
from mymodule import A

def is_subclass(o):
    return inspect.isclass(o) and issubclass(o, A)

print inspect.getmembers(mymodule, predicate=is_subclass)

rawDescription: How to obtain all subclasses (of a specific class) defined by a module?

description: Obtain all subclasses (of a specific class) defined by a module


rawDescription: How to have a single backslash in JSON response using jsonify and flask?

description: Have a single backslash in JSON response using jsonify and flask


rawDescription: String alignment when printing in python


code:
import win32ui
window_name = "Target Window Name" # use EnumerateWindow for a complete list
wd = win32ui.FindWindow(None, window_name)
dc = wd.GetWindowDC() # Get window handle
j = dc.GetPixel (60,20)  # as practical and intuitive as using PIL!
print j
dc.DeleteDC() # necessary to handle garbage collection, otherwise code starts to slow down over many iterations

rawDescription: Most efficient/quickest way to parse pixel data with Python?


code:
def Relay(gpio):
    print "Relay: %s" % gpio.__name__
    # Do stuff with GPIO

rawDescription: Python: How to use variables acros modules


code:
import pyodbc
from pprint import pformat
db = pyodbc.connect(driver='{SQL Server Native Client 10.0}',
                    server='TESTSRVR', database='TESTDB',
                    trusted_connection='yes')
print pformat(db.cursor().execute("select * from dbo.datetest").description)

rawDescription: pyodbc returns SQL Server DATE fields as strings

description: Pyodbc returns SQL Server DATE fields as strings


code:
# print "\033[1;31m"   # this would emit a new line as well
import sys
sys.stdout.write("\033[1;31m")
print "All following prints will be red ..."

rawDescription: How do I print colored output to the terminal in Python?

description: Print colored output to the terminal in Python


code:
import networkx as nx

# build up a graph
G = nx.Graph()
G.add_nodes_from(['A', 'B', 'C', 'D', 'E'])
G.add_edges_from([('A', 'B'), ('B','C'), ('C', 'D'), ('D', 'E')])
bw_centrality = nx.betweenness_centrality(G, normalized=False)

print bw_centrality
# returns {'A': 0.0, 'C': 4.0, 'B': 3.0, 'E': 0.0, 'D': 3.0}

rawDescription: Betweenness centrality in NetworkX: logical error


code: print "i={0}, a.k.a. 'i is equal to {0}.'".format(i)

rawDescription: how do you put a period right next to a variable in python?

description: Put a period right next to a variable in python


code:
rr = r"""
    ^         # begin
    ([^,]+)   # match everything but a comma
    [,\s]+    # a comma and spaces
    (.+?)     # match everything until the next match
    \s*       # spaces
    ([A-Z])   # an uppercase letter
    \.        # a dot
    (\d+)     # some digits
    \.        # a dot
    (\d+)     # some digits
    \s*       # maybe some spaces or newlines
    $         # end
"""

import re
rr = re.compile(rr, re.VERBOSE)
s = 'Wegner, Sven Ake G.15.10\n'
print rr.findall(s)
# [('Wegner', 'Sven Ake', 'G', '15', '10')]

rawDescription: regular expression to split

description: Regular expression to split


code:
def f(x):
    values = [v for vals in x.values for v in vals[2:]]
    return pd.Series(values,name=x.values[0][0])

res = df.groupby(["Activity", "Iteration"]).apply(f)
res = res.T.rename(columns={(t,i):t for t,i in res.index})
print df
print res

rawDescription: Creating feature (row) vectors, for SVM, from Pandas GroupBy function (and or other methods suggested)


code:
#!/usr/bin/env python

# used to parse the json text
import json
with open('json.txt', 'r') as f:
    d = json.load(f)
# define list of what your consider as "missing"
missings = [None, ""]

# recursive function to collect missing paths and alter the aggregator list
# make sure to add cases if needed
def aggregate_missing_paths(aggregator, path, item):
    if isinstance(item, list):
        # check all list items
        map(lambda x: aggregate_missing_paths(aggregator, path, x), item)
    if isinstance(item, dict):
        # check all dict items
        map(lambda kv: aggregate_missing_paths(aggregator, path + '.' + kv[0], kv[1]), item.iteritems())
    if item in missings:
        # alter list to cotain path to missing
        aggregator.append(path)

aggregator = []
aggregate_missing_paths(aggregator, 'root', d)
print aggregator

rawDescription: python parse Json for all null values

description: Python parse Json for all null values


code:
mylist = ["hi_there", "bye_there","hello_there"]
print '\n'.join(s for s in mylist if s.startswith("hi"))

rawDescription: finding a file name from a substring

description: Finding a file name from a substring


code:
from PIL import Image

im = Image.open("image.jpg")

pix = im.load()
print pix[x,y]

rawDescription: Python find rgb

description: Python find rgb


code:
#prints out samenodes
f = open('newerfile.txt')
mylist = list(f)

zippedLists = zip(mylist, mylist[12:], mylist[2:], mylist[13:])
slicedList = zippedLists[::12]

for elt1, elt13, elt2, elt14 in slicedList:
    if elt1 == elt13 and elt2 == elt14:
        print elt1

rawDescription: Loop to Match Parts of List

description: Loop to Match Parts of List


code:
#
#   Hello World client in Python
#   Connects REQ socket to tcp://localhost:5555
#   Sends "Hello" to server, expects "World" back
#
import zmq

context = zmq.Context()

#  Socket to talk to server
print "Connecting to hello world server…"
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")

#  Do 10 requests, waiting each time for a response
for request in range(10):
    print "Sending request %s …" % request
    socket.send("Hello")

    #  Get the reply.
    message = socket.recv()
    print "Received reply %s [ %s ]" % (request, message)

rawDescription: Sending/Receiving data between two different programs


rawDescription: Treat all Unicode characters as single letters


code:
from ctypes import *

class sHeader(Structure):
    _fields_ = [("CC", c_uint32, 4),
        ("AFC", c_uint32, 2),
        ("TSC", c_uint32, 2),
        ("PID", c_uint32, 13),
        ("TP", c_uint32, 1),
        ("PSI", c_uint32, 1),
        ("TEI", c_uint32, 1),
        ("SyncByte", c_uint32, 8)] # note: added 8 here

print sHeader.PID
print sHeader.SyncByte

rawDescription: How Do I Properly Declare a ctype Structure + Union in Python?


code:
import sys
from PyQt4 import QtGui, QtCore

class QCustomTableWidget (QtGui.QTableWidget):
    def __init__ (self, parent = None):
        super(QCustomTableWidget, self).__init__(parent)
        # Setup row & column data
        listsVerticalHeaderItem = ['Device 1', 'Device 2', 'Device 3', 'Device 4', 'Device 5']
        self.setRowCount(len(listsVerticalHeaderItem))
        for index in range(self.rowCount()):
            self.setVerticalHeaderItem(index, QtGui.QTableWidgetItem(listsVerticalHeaderItem[index]))
        listsVerticalHeaderItem = ['Device 1', 'Device 2', 'Device 3', 'Device 4']
        self.setColumnCount(5)
        listsHorizontalHeaderItem = ['Option 1', 'Option 2']
        self.setColumnCount(len(listsHorizontalHeaderItem))
        for index in range(self.columnCount()):
            self.setHorizontalHeaderItem(index, QtGui.QTableWidgetItem(listsHorizontalHeaderItem[index]))

    def dataChanged (self, topLeftQModelIndex, bottomRightQModelIndex):
        row                  = topLeftQModelIndex.row()
        column               = topLeftQModelIndex.column()
        dataQTableWidgetItem = self.item(row, column)
        print '###### Data Changed  ######'
        print 'row    :', row + 1
        print 'column :', column + 1
        print 'data   :', dataQTableWidgetItem.text()
        self.emit(QtCore.SIGNAL('dataChanged'), row, column, dataQTableWidgetItem)
        QtGui.QTableWidget.dataChanged(self, topLeftQModelIndex, bottomRightQModelIndex)

class QCustomWidget (QtGui.QWidget):
    def __init__(self, parent = None):
        super(QCustomWidget, self).__init__(parent)
        self.myQCustomTableWidget = QCustomTableWidget(self)
        self.myQLabel = QtGui.QLabel('Track edited data', self)
        myQVBoxLayout = QtGui.QVBoxLayout()
        myQVBoxLayout.addWidget(self.myQLabel)
        myQVBoxLayout.addWidget(self.myQCustomTableWidget)
        self.setLayout(myQVBoxLayout)
        self.connect(self.myQCustomTableWidget, QtCore.SIGNAL('dataChanged'), self.setTrackData)

    def setTrackData (self, row, column, dataQTableWidgetItem):
        self.myQLabel.setText('Last updated\nRow : %d, Column : %d, Data : %s' % (row + 1, column + 1, str(dataQTableWidgetItem.text())))

if __name__ == '__main__':
    myQApplication = QtGui.QApplication(sys.argv)
    myQCustomWidget = QCustomWidget()
    myQCustomWidget.show()
    sys.exit(myQApplication.exec_())

rawDescription: PyQt QTableWidget retrieve edited field

description: PyQt QTableWidget retrieve edited field


code:
for x in range(2, 12, 2): # start at 2, increment by 2, up to but not including 12
    print x
print 'Good bye!'

rawDescription: Please review my code

description: Please review my code


code:
import grequests

def do_something(response, *args, **kwargs):
    print response

payloads = [
    {'search': '51 F ST SW,AUBURN KING 98001,WA,USA', 'app_code': 'xyz', 'app_id': 'xyz', 'lod': '9'},
    {'search': '55 F ST SW,AUBURN KING 98001,WA,USA', 'app_code': 'xyz', 'app_id': 'xyz', 'lod': '9'}]

unsent_request = []

rawDescription: Creat List of URLs for grequest

description: Creat List of URLs for grequest


code:
import time

survivors = 15
survivor_return_seconds = 10.0
time_survivors_left = None

while True:
    action = raw_input("Type 'x' to make survivors leave, ENTER to see how many are left: ")

    #check if survivors returned
    if time_survivors_left is not None:
        if time.time() >= time_survivors_left + survivor_return_seconds:
            survivors += 5
            time_survivors_left = None
            print "Survivors came back!"

    if action == 'x':
        if time_survivors_left is not None:
            print "Survivors already left! Wait a bit!"
        else:
            survivors -= 5
            time_survivors_left = time.time()

    print "There are %s survivors left." % (survivors,)
    if time_survivors_left is not None:
        print "5 survivors will return in %.2fs" % (
            time_survivors_left + survivor_return_seconds - time.time())

rawDescription: Reduce a variable for a set time

description: Reduce a variable for a set time


code:
import datetime
from datetime import timedelta
import time
from datetime import date

yearweekArray = []
today = date.today()
weekday = today.weekday()
start_delta = datetime.timedelta(days=weekday, weeks=10)
startOfWeek = today - start_delta

for i in range(0,10):
    yearweek = startOfWeek + timedelta(weeks=i)
    curDate = time.strptime(str(yearweek), "%Y-%m-%d")
    curYear = int(time.strftime("%Y", curDate))
    curMonth = int(time.strftime("%m", curDate))
    curDay = int(time.strftime("%d", curDate))
    weekNum = "%02d" % (datetime.date(curYear, curMonth, curDay).isocalendar()[1],)
    yearweekArray.append(str(curYear) + str(weekNum))
print yearweekArray

rawDescription: Show week 53 as yearweek in Python

description: Show week 53 as yearweek in Python


code:
import re

from os import sep
sep2 = r'\\' if sep=='\\' else '/'

pot = '^(?:.+?%s)(.+?)=([^=]*$)' % sep2
print 'pot==%s\n' % pot
rogx = re.compile(pot)

pet = '^(?:.+?%s)(.+?(?==[^=]*$))' % sep2
print 'pet==%s\n' % pet
regx = re.compile(pet)

for ss in ('Same\Same2\Foot\Ankle\Joint\Sensor\Value=4.123',
           'Same\Same2\Battery\Name=SomeString',
           'Same\Same2\Ocean\Atlantic\North=',
           'Same\Same2\Maths\Addition\\2+2=4\Simple=ohoh'):
    print ss + '\n' + len(ss)*'-'

    print 'rogx groups  '.rjust(32),rogx.match(ss).groups()

    a,b = ss.split(sep,1)[1].rsplit('=',1)
    print 'split split  '.rjust(32),(a,b)
    print 'split split join upper replace   %s=%s' % (a.replace(sep,'_').upper(),b)

    print 'regx split group  '.rjust(32),regx.match(ss.split(sep,1)[1]).group()
    print 'regx split sub  '.rjust(32),\
          regx.sub(lambda x: x.group(1).replace(sep,'_').upper(), ss)
    print

rawDescription: Regex Python / group quantifiers

description: Regex Python / group quantifiers


rawDescription: How to Print next year from current year in Python


code:
L2 = ['A','B','C','D','A','B']
L3 = [3,2,1,2,2,1]

L4 = zip(L2, L3)

L5 = []
L6 = []
def freduce(l):
    for x, y in l:
        print x , y
        if x in L5:
            k = L5.index(x)
            L6[k] += y
        else:
            L5.append(x)
            L6.append(y)

freduce(L4)
print L5
print L6

rawDescription: Eliminate dups and sum corresponding terms in lists


code: print averages[days-1:]

rawDescription: moving average of 3 elements by C or Python

description: Moving average of 3 elements by C or Python


code:
def power_times(k):
    """use as power_times(k)(x)(y) => k * y^x"""
    return lambda x: lambda y: k * y**x

print power_times(2)(3)(4)  # returns 2 * 4^3 = 128

rawDescription: Function Calling With 3 or More Argument Input Fields - function()()()


code:
import netifaces

for iface in netifaces.interfaces():
    addrs = netifaces.ifaddresses(iface)
    for addr in addrs.get(netifaces.AF_INET6, []):
        print '%-8s %s' % (iface, addr['addr'])

rawDescription: Finding active IPv6 interfaces under Mac OS (using Python)


code:
from datetime import datetime
import operator

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET

tree = ET.ElementTree(file='data.xml')
root = tree.getroot()

mapping = {datetime.strptime(journal.findtext('published'), '%d/%m/%Y'): journal
           for journal in root.iterfind('.//article/journal')}

journal_latest = max(mapping.iteritems(), key=operator.itemgetter(0))[1]
print journal_latest.findtext('name')

rawDescription: How do i get newest date through xpath?

description: Get newest date through xpath


code:
antilog(0.1)
antilog(3)
inputs = [0.1, 1.0, 1.1, 7.9, 21.5]
outputs = [antilog(x) for x in inputs]
print outputs

rawDescription: How to find every integer that is the closest to the antilogarithm (10base) for a series of n*0.1 (in Python)

description: Find every integer that is the closest to the antilogarithm (10base) for a series of n*0.1 (in Python)


code:
from collections import Counter

dicts = [
    {1: 'url1', 3: 'url2', 7: 'url3', 5: 'url4'},
    {1: 'url1', 7: 'url3'},
    {5: 'url4', 10: 'url5'},
]

result = Counter()
for d in dicts:
    result.update(d.keys())

print dict(result)

rawDescription: How to count number of repeated keys in several dictionaries?

description: Count number of repeated keys in several dictionaries


code:
import re
samples = [
    u"magic: 'xfF9i4YBAQP1EgoNhgEBAw',",
    u"magic: 'AZJEXYx.ZsExcTHvjH9mwQ',",
]
magic_regex = re.compile(ur"magic:\s+'([^']+)',")
for sample in samples:
    print magic_regex.search(sample).group(1)

rawDescription: parsing string from request data

description: Parsing string from request data


code:
from collections import Counter
import re

reg = re.compile('\S{4,}')

s = "hello this is hello this is baby baby baby baby hello"
c = Counter(ma.group() for ma in reg.finditer(s))
print c

rawDescription: Counting the Repeating of words in a list PYTHON


code:
words = re.findall('\w+', dataString.lower())
print len([word for word in words if word == 'hello'])

rawDescription: I want to create a program that can analyze downloaded text file in python


code:
l = ['start of paragraph 1','start of paragraph 2','para 3']
first_words = [p[:p.find(' ')] for p in l]
print first_words

rawDescription: Find the first word of a paragraph inside a list


rawDescription: Can someone explain what all the codes mean? PYTHON


code:
l = [{'a': 123, 'b': 1234},
        {'a': 3222, 'b': 1234},
        {'a': 123, 'b': 1234}]

seen = set()
new_l = []
for d in l:
    t = tuple(d.items())
    if t not in seen:
        seen.add(t)
        new_l.append(d)

print new_l

rawDescription: Remove duplicate dict in list in Python

description: Remove duplicate dict in list in Python


code:
#!/usr/bin/python

import cProfile
from timeit import Timer
from faker import Faker

def longest1(lists):
    return max(len(s) for s in lists)

def longest2(lists):
    return len(max(lists, key=len))

def longest3(lists):
    return len(sorted(lists, key=len)[-1])

s = Faker()
seq = [ s.word() for x in range(100) ]

func = [ longest1, longest2, longest3 ]

for f in func:
    t = Timer(lambda: f(seq))
    print f.__name__, cProfile.run('t.timeit(number=1000)')

rawDescription: How to get the length of the longest string in a list (python)?

description: Get the length of the longest string in a list (python)


code:
row = np.array([1,2,3,4,5])
print replaced(row, a, b)

row = np.array([5,4,3,2,1])
print replaced(row, a, b)

row = np.array([4, 5, 1, 3, 2])
print replaced(row, a, b)

rawDescription: Numpy array modifying multiple elements at once


code: print 'Br\"{u}derle'

rawDescription: latexcodec stripping slashes but not translating characters (Python)

description: Latexcodec stripping slashes but not translating characters (Python)


code:
for line in lines:
    search = re.search(r'@([A-Z0-9]+)@\s*\bFAM\b', line)
    if search:
        code = search.group(1)
        print code

rawDescription: Searching for a unique word in a string

description: Searching for a unique word in a string


code:
content = """gi|1168222|sp|P46098.1|5HT3A_HUMAN
gi|1168223|sp|P35563.2|5HT3A_RAT
gi|112809|sp|P23979.1|5HT3A_MOUSE
gi|24211440|sp|O70212.1|5HT3A_CAVPO
gi|113067|sp|P22770|ACHA7_CHICK"""

result = []

for line in content.split("\n"):
    start_index = line.find("sp|")
    if start_index==-1:
        continue

    #- +3 because lenght of sp| is 3
    end_index1 = line.find(".", start_index+3)
    end_index2 = line.find("|", start_index+3)

    if end_index1==-1 and end_index2==-1:
        continue
    elif end_index2==-1:
        end_index = end_index1
    elif end_index1==-1:
        end_index = end_index2
    elif end_index1 < end_index2:
        end_index = end_index1
    else:
        end_index = end_index2

    result.append(line[start_index+3:end_index])

print result

rawDescription: Find string within a string and save it to file on BioPython


code:
from ConfigParser import RawConfigParser
from StringIO import StringIO

START_PROCESS_TOKEN = '[process]'
END_PROCESS_TOKEN = '[export]'

def hacky_parse(stream):
    state = 0
    ini_io = StringIO()
    python_io = StringIO()
    for line in stream.readlines():
        if state == 0:
            if line.strip() == START_PROCESS_TOKEN:
                state = 1
                continue
            ini_io.write(line)
        elif state == 1:
            if line.strip() == END_PROCESS_TOKEN:
                ini_io.write(line)
                state = 2
                continue
            python_io.write(line)
        else:
            ini_io.write(line)

    ini_io.seek(0)
    python_io.seek(0)

    config_parser = RawConfigParser()
    config_parser.readfp(ini_io)

    python_code = python_io.getvalue()

    return config_parser, python_code

cfg = """
[load]
files=a,b,c

[process]
while 1:
    do_stuff()

[export]
files=x,y,z
"""

my_stream = StringIO(cfg)
config_parser, process_code = hacky_parse(my_stream)
print 'The value of "files" in section "load" is...'
print config_parser.get('load', 'files')
print
print 'The raw Python code is...'
print process_code

rawDescription: Reading raw section with ConfigParser

description: Reading raw section with ConfigParser


code:
value1 = 'one'
value2 = 'two'
d = {
        'key1': (value1, value2),
        'key2': (value1, value2),
        'key3': (value1, value2)
    }
CSV ="\n".join([k+','+",".join(v) for k,v in d.items()])
print CSV #You can store this string variable to file as you wish
# with open("filename.csv", "w") as file:
    # file.write(CSV)

rawDescription: Reading/Writing out a dictionary to csv file in python


code:
s = 'module hi(a, b, c)'
regex = re.compile(r'\s(\w+)\(([^\)]+)\)')
try:
    module_name, parameters = regex.search(s).groups()
except AttributeError as e:
    print 'No match for: {}'.format(s)
    raise
parameters = parameters.split(',')
print module_name, parameters
d = {'module_name':module_name,
     'module_params':parameters[:-1],
     'module_last_param':parameters[-1]}
print d
# {'module_last_param': ' c', 'module_name': 'hi', 'module_params': ['a', ' b']}

rawDescription: Save matches on array

description: Save matches on array


code:
"""
multi_queue.py
"""
from multiprocessing import Process, Queue
import time

def reader(queue):
    while True:
        msg = queue.get()         # Read from the queue and do nothing
        if (msg == 'DONE'):
            break

def writer(count, queue):
    for ii in xrange(0, count):
        queue.put(ii)             # Write 'count' numbers into the queue
    queue.put('DONE')

if __name__=='__main__':
    for count in [10**4, 10**5, 10**6]:
        queue = Queue()   # reader() reads from queue
                          # writer() writes to queue
        reader_p = Process(target=reader, args=((queue),))
        reader_p.daemon = True
        reader_p.start()     # Launch the reader process

        _start = time.time()
        writer(count, queue)    # Send a lot of stuff to reader()
        reader_p.join()         # Wait for the reader to finish
        print "Sending %s numbers to Queue() took %s seconds" % (count,
            (time.time() - _start))

rawDescription: Python multiprocessing - Pipe vs Queue

description: Python multiprocessing - Pipe vs Queue


code:
from bs4 import BeautifulSoup

invalid = """<html>
<html>

</html>
</html>"""

soup = BeautifulSoup(invalid, 'html.parser')
print len(soup.find_all("html"))  # prints 2

valid = """<html>
</html>"""

soup = BeautifulSoup(valid, 'html.parser')
print len(soup.find_all("html"))  # prints 1

rawDescription: Checking for duplicate tags using Beautifulsoup


code:
def foo(*args, **kw):
    a = kw.get('a', MyComplexObject())
    b = kw.get('b', b=Something())
    print a, b, args

rawDescription: Is there a way to access varargs without specifying default value arguments in Python


code:
from numbers import Number
class C(object):
    def __init__(self, value):
        self.value = value
    def __add__(self, other):
        print '__add__ called'
        if isinstance(other, C):
            return self.value + other.value
        if isinstance(other, Number):
            return self.value + other
        return NotImplemented
    def __radd__(self, other):
        print '__radd__ called'
        if isinstance(other, C):
            return other.value + self.value
        if isinstance(other, Number):
            return other + self.value
        return NotImplemented

c = C(123)
c + c
c + 2
2 .__add__(c)
2 + c

rawDescription: How to get __add__ called

description: Get __add__ called


code:
df = pd.read_csv(io.StringIO(temp),header=None,index_col=[1,2], nrows=3, skiprows=[4])
df = df.ix[:, 1:].reset_index(drop=True).T
df.columns = ['Coln1', 'Coln2','Coln3']
print df.reset_index(drop=True)

rawDescription: How to turn selected headers of a CSV file into a Pandas data frame

description: Turn selected headers of a CSV file into a Pandas data frame


code:
from pprint import pprint

name = 'Jeanell'

with open('test_data.txt') as f:
    for line in f:
        try:
            dictionary = eval(line.rstrip())
            if name in dictionary.values():
                email_address1 = dictionary['_chk2m']
                email_address2 = dictionary['_cpzh4']
                print dictionary['_cre1l'], email_address1, email_address2
                pprint(dictionary)
        except SyntaxError as e:
            # a lone newline character ( '\n') at the end of the file
            # is causing this
            print
            print e

rawDescription: Looking up 2nd value in a list based on a user input if first value


code:
print '\n'.join(values.format(date=timestr,
                              rec=rec,
                              rec_count=get_rectype_count(filename, rec))
                for filename in glob.iglob('*.gz')
                for rec in rectypes)

rawDescription: how to join various bits of string and data together using python

description: Join various bits of string and data together using python


code:
import datetime
x = '20140203 00:00:03.132'
timestamp = datetime.datetime.strptime(x, '%Y%m%d %H:%M:%S.%f')
print datetime.datetime.strftime(timestamp, '%Y%m%d %H:%M')  # 20140203 00:00

rawDescription: How to parse CSV file using pandas?

description: Parse CSV file using pandas


code:
import sys
print "Arguments:", sys.argv

rawDescription: Parse Json data to Excel

description: Parse Json data to Excel


code:
import signal

class Reporter(object):
    def __init__(self):
        self.retval = []

    def handler(self,signum,frame):
        print self.retval

r = Reporter()
signal.signal(signal.SIGINT,r.handler)

rawDescription: Python - Print a value without intterupting a loop/function


code:
list_1 = [[1, "BMW", "Boston", "01Jan2013"], [37, "Chevrolet", "Denver", "05Jan2013"],
[854, "BMW", "Boston", "01Jan2013"]]

list_2 = [[1, "Mercedes", "Boston", "01Jan2013"], [37, "Chevrolet", "Denver", "05Jan2013"],
[854, "Toyota", "Boston", "01Jan2013"]]

dict_2 = dict()

for elem in list_2:
    dict_2[elem[0]] = elem[1:]

for item in list_1:
    if dict_2[item[0]][1:] == item[2:]:    # Have to offset the index since dict list only has three elements
        print item

[1, 'BMW', 'Boston', '01Jan2013']
[37, 'Chevrolet', 'Denver', '05Jan2013']
[854, 'BMW', 'Boston', '01Jan2013']

rawDescription: Python: Which is faster: matching lists or matching dictionaries?


code:
#!/usr/bin/env python

import os
import sqlite3
import MySQLdb
from MySQLdb.constants import FIELD_TYPE

user = os.getenv('USER')

def create_mysql_table():
    conn = MySQLdb.connect(user=user, db='foo')
    c = conn.cursor()
    c.execute("DROP TABLE stocks")
    c.execute("CREATE TABLE stocks"
              "(date text, trans text, symbol text, qty real, price Decimal(10,2) UNSIGNED NOT NULL)")
    c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
    conn.commit()

def create_sqlite_table():
    conn = sqlite3.connect('test.db')
    c = conn.cursor()
    c.execute("DROP TABLE stocks")
    c.execute("CREATE TABLE stocks"
              "(date text, trans text, symbol text, qty real, price real)")
    c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
    conn.commit()

def query_dbs(use_type_converters):
    conn = sqlite3.connect('test.db')
    c = conn.cursor()
    for row in c.execute('SELECT * FROM stocks'):
        print 'SQLITE: %s' % str(row)

    type_converters = MySQLdb.converters.conversions.copy()

    if use_type_converters:
        type_converters.update({
            FIELD_TYPE.DECIMAL: float,
            FIELD_TYPE.NEWDECIMAL: float,
        })

    conn = MySQLdb.connect(user=user, db='foo', conv=type_converters)
    c = conn.cursor()
    c.execute('SELECT * FROM stocks')
    for row in c.fetchall():
        print 'MYSQLDB: %s' % str(row)

create_sqlite_table()
create_mysql_table()

print "Without type conversion:"
query_dbs(False)
print "With type conversion:"
query_dbs(True)

rawDescription: Using mysqldb and sqlite3 in the same Python 2.7 script: Should I throw in the towel?


code:
def get_dir(module_name):
    import os,imp
    (file, pathname, description) = imp.find_module(module_name)
    return os.path.dirname(pathname)

print get_dir('os')

rawDescription: Finding a module's directory

description: Finding a module's directory


code:
import datetime as dt

def main(filename, from_str, to_str):
    date_from = dt.datetime.strptime(from_str, "%Y-%m-%d %H:%M")
    date_to = dt.datetime.strptime(to_str, "%Y-%m-%d %H:%M")
    print date_from, date_to
    days = (date_from - date_to).days
    print days

rawDescription: calculate dates in python

description: Calculate dates in python


code:
for label in label_feats:
    for document in label_feats[label]:
        print label, document
        break
    break

rawDescription: My Maxent Classifier works fine with gis algorithm but does not work with iis algorithm. It is not throwing any error, just some warnings


code:
example = 'bar'
str(example)
repr(example)
`example`

rawDescription: Pythonic way of converting integers to string


code:
if nx.path.bidirectional_dijkstra(G,1,2): print "path exists"

if nx.path.bidirectional_dijkstra(G,1,4): print "path exists"

rawDescription: Python: How to find if a path exists between 2 nodes in a graph?


code:
from ctypes import *

_libtest = CDLL('libTest')

_libtest.initialize.argtypes = [POINTER(c_uint32), POINTER(c_int32)]
_libtest.initialize.restype  =  None

def initialize(session,status):
    return _libtest.initialize(byref(session),byref(status))

if __name__ == '__main__':
    session = c_uint32();
    status = c_int32();

    initialize(session, status)

    print status

rawDescription: Use ctypes with typedef

description: Use ctypes with typedef


code:
from classy import ClassName

print ClassName.name
print glob

rawDescription: Per-class constants in Python

description: Per-class constants in Python


code:
for i in test:
    print divmod(b, i)

rawDescription: Python: list index out of range error; certain list elements work


code:
def getLetters(data):
    result = []
    for child in data:
        # use other default values here if more suitable
        c1 = None
        c2 = None
        if child.attrib['c1']:
           c1 = child.attrib['c1']
        if child.attrib['c2']:
           c2 = child.attrib['c2']
        result.append({'c1':c1, 'c2':c2}) # append your next entry as a mini-dictionary
    return result

for entry in getLetters(data):
    print 'c1', entry['c1'], 'c2', entry['c2']

rawDescription: Iterated return in constructors

description: Iterated return in constructors


code:
#!/usr/bin/env python

d = []

def binary_search(a, x, lo=0, hi=None):
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        midval = a[mid]
        if midval < x:
            lo = mid+1
        elif midval > x:
            hi = mid
        else:
            return mid
    return -1

f = open("/etc/hosts","r")
for line in f:
    line=line.rstrip()
    l = len(line)+1
    for i in xrange(1,l):
        x = hash(line[:i])
        d.append(x)
f.close()

d.sort()

while True:
    w = raw_input('> ')
    if not w:
        break

    if binary_search(d, hash(w)) != -1:
        print "match found", w

rawDescription: Fast method to search for a string in a big text file with python


code:
for element in selector.xpath('//element'):
    print element.xpath('count(@*)').extract_first()

for element in selector.xpath('//element'):
    for i in range(1, 2+1):
        print element.xpath('@*[%d]' % i).extract_first()

rawDescription: Getting name of attributes with Scrapy XPATH


code:
class Revision(models.Model):
    # Model junk...

    @staticmethod
    def send_email(sender, instance, created, **kwargs):
        if created:
            print "DO STUFF"

signals.post_save.connect(Revision.send_email, sender=Revision)

rawDescription: How can I have a Django signal call a model method?

description: Have a Django signal call a model method


code:
# Using SELECT BY ATTRIBUTE
arcpy.MakeFeatureLayer_management(fc_In1, "Poly_lyr")     # no need to make the layer each time through the cursor
with arcpy.da.SearchCursor(fc_In1, fc_In1_FieldName) as cursor:
    for row in cursor:
        var_PolygonName = row[0]
        print "Buffering polygon " + var_PolygonName
        arcpy.SelectLayerByAttribute_management("Poly_lyr", "NEW_SELECTION", '"{}" = \'{}\''.format(fc_In1_FieldName, var_PolygonName))
        arcpy.Buffer_analysis("Poly_lyr", var_PolygonName+'_Buff.shp', var_Buffer, "FULL", "ROUND", "ALL", "")

rawDescription: ArcGIS script is buffering all features each time through the loop -- why?


code:
from abc import ABCMeta

class abstractclassmethod(classmethod):

    __isabstractmethod__ = True

    def __init__(self, callable):
        callable.__isabstractmethod__ = True
        super(abstractclassmethod, self).__init__(callable)

class DemoABC:

    __metaclass__ = ABCMeta

    @abstractclassmethod
    def from_int(cls, n):
        return cls()

class DemoConcrete(DemoABC):

    @classmethod
    def from_int(cls, n):
        return cls(2*n)

    def __init__(self, n):
        print 'Initializing with', n

rawDescription: Python 2.7 Combine abc.abstractmethod and classmethod


code:
for inner_l in l:
    for item in inner_l:
        print item

rawDescription: Nested lists python

description: Nested lists python


code:
data = [['Country', 'Destination', 'Country Code', 'Destination Code', 'Remarks'],
 ['AAA', 'Some Mobile', '111', '12, 23, 34, 46','Some remarks'],
 ['AAA', 'Some city A', '111', '55, 56, 57, 51', 'Some more remarks'],
 ['BBB', 'Some city B', '222', '234, 345, 456', 'Other remarks']]

op=[data[0]]
for i in data[1:]:
   for id,j in enumerate(i.pop(3).split(',')):
      k=i[:]
      k.insert(3,i[2]+j.strip())
      op.append(k)

for i in op:
   print i

rawDescription: How to match phone number prefixes?

description: Match phone number prefixes


code: print the_template.substitute(section_id="theID", first="Sarah" if 0==0 else "John")

rawDescription: Python inline of XML or ASCII string/template?


code:
class Foo(object):
    def barfunc(self, baz):
        self.baz = baz
        print self.baz

    def barbar(self):
        print self.baz

rawDescription: python class methods: parameter as class variables pros and cons

description: Python class methods: parameter as class variables pros and cons


code:
from functools import partial
from itertools import count
def my_pow(x,y):
    return x**y

curried_pow2n = partial(my_pow,x=2)

for i in count(0): #print the 2**i
    print curried_pow2n(i)

rawDescription: What is a _curried method in python and how are they generated?


code:
import multiprocessing
import itertools

def get_chunks(iterable, chunks=1):
    # This is from http://stackoverflow.com/a/2136090/2073595
    lst = list(iterable)
    return [lst[i::chunks] for i in xrange(chunks)]

def someOtherFunction(lower, upper):
    return (lower + upper) / 2

def worker(pairs):
    best_accuracy = best_lower = best_upper = 0
    for lower_threshold, upper_threshold in pairs:
        accuracy = someOtherFunction(lower_threshold, upper_threshold)
        if accuracy > best_accuracy:
            best_accuracy = accuracy
            best_lower = lower_threshold
            best_upper = upper_threshold
    return best_accuracy, best_lower, best_upper

if __name__ == '__main__':
    jobs = []
    pairs = itertools.product(xrange(0, 100), xrange(100, 200))
    chunked_pairs = get_chunks(pairs, chunks=multiprocessing.cpu_count())
    pool = multiprocessing.Pool()
    results = pool.map(worker, chunked_pairs)
    pool.close()
    pool.join()
    print results
    # Now combine the results
    sorted_results = reversed(sorted(results, key=lambda x: x[0]))
    print next(sorted_results)  # Winner

rawDescription: How to use multiprocessing for grid search (parameter optimization) in Python

description: Use multiprocessing for grid search (parameter optimization) in Python


code:
# parse data
data = """Group  Item-1  Item-2
0       7       13
0      10        4
1       2        8
1       3        1
1       4        3
1       6       28
1       8        6"""
data = [[int(x) for x in line.split()] for line in data.splitlines()[1:]]

# get mapping {group_number: [list of pairs]}
groups = defaultdict(list)
for g, x, y in data:
    groups[g].append((x, y))

# for each group, add pairs to union find structure and get groups
for group, links in groups.items():
    union = UnionFind()
    for x, y in links:
        union.union(x, y)
    print group, union.get_groups().values()

rawDescription: Python list comparison to create trees

description: Python list comparison to create trees


code: print '\n'.join(str(p) for p in myList)

rawDescription: Pythonic way to print list items

description: Pythonic way to print list items


code:
s = {u"name": u"испытание"}
import json
print json.dumps(s)
print s['name']
print s['name'].encode('unicode-escape')
print s['name'].encode('utf-8')
s['name'].encode('utf-8')

rawDescription: Non-latin text outputting as nonsense in Python


code:
class Diction(GridLayout):

    def __init__(self, **kwargs):
        super(Diction, self).__init__(**kwargs)
        self.cols=2
        self.add_widget(Label(text="Search"))
        self.search=TextInput(multiline=False)
        self.add_widget(self.search)
        self.add_widget(Button(text="click",on_press=self.show_popup))

    def show_popup(self, b):
        if self.search.text=='A':
            print 'True'
        else:
            print 'False'
        p = CustomPopup()
        p.open()

rawDescription: Python kivy text input

description: Python kivy text input


code:
import re
header =  "Port          Name               Status    Vlan      Duplex  Speed   Type"
row    =  "Eth1/2        trunk to dg-qwu-29 connected trunk     full    1000    1/10g"
# retrieve indices where each header title begins and ends
matches = [(m.group(0), (m.start(), m.end()-1)) for m in re.finditer(r'\S+', header)]
b,c=zip(*matches)
# each text in the row begins in each header title index and ends at most before the index
# of the next header title. strip() to remove extra spaces
items = [(row[j[0]:(c[i+1][0] if i < len(c)-1 else len(row))]).strip() for i,j in enumerate(c)]
print items

rawDescription: Split a string with whitespaces following a template


code:
mystring = 'abccd'
print re.findall(r'(\w)\1', mystring)  # ['c']

rawDescription: How to look for consecutive repeats in regex?

description: Look for consecutive repeats in regex


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

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

browser = webdriver.Chrome()
browser.get("http://fortune.com/fortune500/")

# toggle
toggle = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "filter-toggle")))
toggle.click()

# get all options
industry_button = Select(browser.find_element_by_name("filters[Industry]"))
for option in industry_button.options:
    print option.text

rawDescription: Crawling website using selenium in firefox with python


code:
data = [["date_a", "1a", "2a", "3a"],
        ["date_b", "1b", "2b", "3b"]]

print zip(*(zip(itertools.repeat(ls[0]), ls[1:]) for ls in data))

rawDescription: Using zip() to rotate a list

description: Using zip() to rotate a list


code:
from bs4 import BeautifulSoup

soup = BeautifulSoup(u"""
Signal et Communication

Ingénierie Réseaux et Télécommunications

""")

td = soup.td
print td.string
print td.get_text()

rawDescription: How can I remove all different script tags in BeautifulSoup?

description: Remove all different script tags in BeautifulSoup


code:
aN
bN
if (bN[-1] == "1"):
    print "The binary number was odd so your number will contain 2 decimals"
    print "The number is now", aN[:aN.find(".")+3]
elif (bN[-1] == "0"):
    print "The binary number was even so your number will contain 1 decimal"
    print "The number is now", aN[:aN.find(".")+2]

rawDescription: String Slicing with If?

description: String Slicing with If


code:
import numpy as np
img = np.zeros((3, 4, 3), dtype = np.uint8)
for (x,y), value in np.ndenumerate(img[:,:,0]):
    if x == 1 and y == 2:
        img[x,y,:] = (255,0,255)
print img

rawDescription: cv::MatIterator equivalent in numpy

description: Cv::MatIterator equivalent in numpy


code:
import sys

for arg in sys.argv[1:]:
    print arg

rawDescription: how do i take unlimited sys.argv[] arguments?

description: Take unlimited sys.argv[] arguments


code:
import random

def create_matrix(x,y):
    matrix = []
    for i in range(x):
        matrix.append([])
        for j in range(y):
            matrix[i].append(random.random())
    return matrix

def main():
    matrix = create_matrix(3, 2)
    for row in matrix:
        print row

main()

rawDescription: Populating a matrix with random numbers and computing sums of those numbers


code:
htmls = '''M '''
soup = BeautifulSoup(htmls)
for link in soup.findAll("a", {"href":re.compile("(?<!\.pdf)$")}):
    print link['href']

rawDescription: Regex, Negative Lookback, Prevent Match

description: Regex, Negative Lookback, Prevent Match


code:
import time
from collections import Counter

def string2bytes(data):
    return "".join("{:02x}".format(ord(c)) for c in data)

startTime = time.time()

# get datas from file
f = open("myfile.bin","rb")
filedata = f.read()
size = f.tell()
f.close

# count each data, check percentage and store to a dictionnary if upper than 0.50%
ChkResult = True
r = {}
for k,v in Counter(filedata).items():
    c = float(v) / size * 100
    if c > 0.50:
        ChkResult = False
        tag = string2bytes(k).upper()
        r[tag] = c

# print result
if ChkResult:
    print "OK"
else:
    for x in sorted(r.keys()):
        print "    0x%s is %.2f%%"%((x), r[x])

print "Done in %.2f seconds."%(time.time() - startTime)

rawDescription: Python 2.7 : How to reduce time to get data percentage of a file?


code:
class Point(object):

    __slots__ = ('x', 'y')

    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

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

    def __iadd__(self, other):
        self.x += other.x
        self.y += other.y
        return self

    def __isub__(self, other):
        self.x -= other.x
        self.y -= other.y
        return self

    def __idiv__(self, number):
        self.x /= number
        self.y /= number
        return self

    def abs(self):
        return abs(self.x) + abs(self.y)

    def copy(self):
        return Point(self.x, self.y)

def extrema(moves, jitter=0.1, threshold=1000, sample=16):
    point = Point()
    minpoint = Point()
    maxpoint = Point()
    average = Point()
    average /= 1.0
    turned = False
    for move in moves:
        point += move
        x = point.x
        if x < minpoint.x:
            minpoint.x = x
        elif x > maxpoint.x:
            maxpoint.x = x
        y = point.y
        if y < minpoint.y:
            minpoint.y = y
        elif y > maxpoint.y:
            maxpoint.y = y
        delta = move.copy()
        delta -= average
        delta /= sample
        average += delta
        if average.abs() < jitter:
            if point.abs() > threshold:
                turned = True
            elif turned:
                yield minpoint, maxpoint
                point = Point() # reset (calibrate)
                minpoint = Point()
                maxpoint = Point()
                turned = False

# read data from file
moves = [Point(*map(int, move.split(',')))
    for move in open('data.txt').read().split(';') if move]

# generate extrema
print list(extrema(moves))

rawDescription: Maxima and Minima Point of a Curve in Python


code:
A = np.loadtxt("C:\Users\KEMAL\Desktop\piton\test.txt").T

s = A.max(axis=1).cumsum()
data = np.zeros((int(s[-1]), A.shape[1]))

A[1:] += s[:2, None]
i = (A - 1).astype(int)
j = np.tile(np.arange(A.shape[1]), (A.shape[0], 1))

data[i, j] = 1

rawDescription: how can I covert this to a matrix

description: Covert this to a matrix


code:
for key, value in sorted(d.items(), key=lambda x: random.random()):
    print key, value

rawDescription: How to iterate through dict in random order in Python?

description: Iterate through dict in random order in Python


code:
vals = [1,3,2]
for item in itertools.product(*[range(x+1) for x in vals]):
    print item

rawDescription: Python: how do I create a list of combinations from a series of ranges of numbers


code:
from functools import partial

def partials(*args):
    for arg in args:
        yield partial(str, arg)

one, two, three, four = partials(1, 2, 3, 4)
print one(), two(), three(), four()

rawDescription: Lambda or functools.partial for deferred function evaluation?


rawDescription: Python: Elegantly print all *remaining* elements from list in one line


rawDescription: Deep copy of a np.array of np.array

description: Deep copy of a np.array of np.array


code:
from elps import PyTestClass as TC;

a = TC();
print a.calculate(1);

class B(TC):
#   pass
    def override_me(self):
        return 5

b = B()
print b.calculate(1)

rawDescription: Can I override a C++ virtual function within Python with Cython?

description: Override a C++ virtual function within Python with Cython


rawDescription: How can I iterate over Pandas pivot table? (A multi-index dataframe?)

description: Iterate over Pandas pivot table? (A multi-index dataframe?)


code:
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')

wb = excel.Workbooks.Open(r"E:\Python temp\example.xls")
ws = wb.Worksheets("Sheet 1")
print ws.Range("A1").Value.split('\n')
excel.Application.Quit()

rawDescription: Read multiple lines in a single excel cell using python


code:
import atexit

@atexit.register
def goodbye():
    print "You are now leaving the Python sector."

rawDescription: Detect when a Python module unloads

description: Detect when a Python module unloads


code:
#!/usr/bin/env python
from mpi4py import MPI
import collections

def addCounter(counter1, counter2, datatype):
    for item in counter2:
        counter1[item] += counter2[item]
    return counter1

if __name__=="__main__":

    comm = MPI.COMM_WORLD

    if comm.rank == 0:
        myCounter = collections.Counter({'a':1, 'b':2, 'c':3})
    else:
        myCounter = collections.Counter({'b':1, 'c':2, 'd':3})

    counterSumOp = MPI.Op.Create(addCounter, commute=True)

    totcounter = comm.allreduce(myCounter, op=counterSumOp)
    print comm.rank, totcounter

rawDescription: Summing Python Objects with MPI's Allreduce


code:
from collections import defaultdict
word_counts = defaultdict(int)
for item in g_data:
    # You might want to break this out into its own function for modularity.
    for word in item.text.split():
        if word not in STOP_WORDS:
            word_counts[word] += 1
for word, count in word_counts.iteritems():
    print '%s: %d' % (word, count)

rawDescription: Removing words from python lists?

description: Removing words from python lists


code:
RSEM = {key: ERCC.get(key, RSEM[key]) for key in RSEM}
print RSEM

rawDescription: Replace dict key by value of other dict

description: Replace dict key by value of other dict


code:
import os

def cls_changer(name, parents, attrs):
    model_name = os.path.basename(__file__).split('.')[0]
    res = type(model_name+name, parents, attrs)
    gl = globals()
    gl[model_name+name] = res
    return res

class A(object):
    __metaclass__ = cls_changer
    pass

print A.__name__
print untitled0A

rawDescription: Is it possible to change the name of a class (type) with a python decorator?


code:
import textwrap

My_string = "aaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbccccccccccccccccccccddddddddddddddddddddeeeeeeeeeeeeeeeeeeeeffffffffffffffffffff"

print '\n'.join('              {0}'.format(line) for line in [
                '---------------------------------------------------------------',
                'Value of a is'] + textwrap.fill(My_string, 20).split('\n') +
                ['---------------------------------------------------------------'])

rawDescription: Put string in new line after 64 character


code:
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from nltk.corpus import stopwords
import numpy as np
import numpy.linalg as LA

train_set = ["The sky is blue.", "The sun is bright."] #Documents
test_set = ["The sun in the sky is bright."] #Query
stopWords = stopwords.words('english')

vectorizer = CountVectorizer(stop_words = stopWords)
#print vectorizer
transformer = TfidfTransformer()
#print transformer

trainVectorizerArray = vectorizer.fit_transform(train_set).toarray()
testVectorizerArray = vectorizer.transform(test_set).toarray()
print 'Fit Vectorizer to train set', trainVectorizerArray
print 'Transform Vectorizer to test set', testVectorizerArray
cx = lambda a, b : round(np.inner(a, b)/(LA.norm(a)*LA.norm(b)), 3)

for vector in trainVectorizerArray:
    print vector
    for testV in testVectorizerArray:
        print testV
        cosine = cx(vector, testV)
        print cosine

transformer.fit(trainVectorizerArray)
print
print transformer.transform(trainVectorizerArray).toarray()

transformer.fit(testVectorizerArray)
print
tfidf = transformer.transform(testVectorizerArray)
print tfidf.todense()

rawDescription: Python: tf-idf-cosine: to find document similarity


code:
found = False

for n in unformatted_returns:
    if date_new == n[0]:
        print "found date"
        rate_of_return_calc(date, date_new)
        found = True

if not found:
    print "date {0} not found".format(date_new)
    day_accumulater(d, m, y, date_new, date)

rawDescription: Python: for each tuple in list, check if string in tuple


code:
import re
from datetime import datetime

def get_date_time (t):
    """
    t: date time string;
    parse date time string, and return datetime
    """
    time_string = re.findall('\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{4}', t)[0]
    return datetime.strptime(time_string, '%Y-%m-%d %H:%M:%S.%f')

if __name__ == '__main__':
    with open('input.txt') as f:
        src = f.read().split('\n') # split into lines
        print get_date_time(src[7]) -  get_date_time(src[5]) # calculate difference

rawDescription: Read numerical values from a file with lines of strings


rawDescription: How to combine 2d lists elements in python - Taking order in consideration

description: Combine 2d lists elements in python - Taking order in consideration


code: print ''.join(str(x) for x in vars)

rawDescription: How can I create python strings with placeholders with arbitrary number of elements

description: Create python strings with placeholders with arbitrary number of elements


code: for tree in enum_ordered(("a","b","c","d", "e")): print tree

rawDescription: Enumerate all full (labeled) binary tree


code:
import requests
url="http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL"
#headers = {'content-type': 'application/soap+xml'}
headers = {'content-type': 'text/xml'}
body = """<?xml version="1.0" encoding="UTF-8"?>
         <SOAP-ENV:Envelope xmlns:ns0="http://ws.cdyne.com/WeatherWS/" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
            <SOAP-ENV:Header/>
              <ns1:Body><ns0:GetWeatherInformation/></ns1:Body>
         </SOAP-ENV:Envelope>"""

response = requests.post(url,data=body,headers=headers)
print response.content

rawDescription: Python : Soap using requests

description: Python : Soap using requests


code:
messages = []
try:
    pdict = parser.parseProtocol(id)
    messages.append("parsing worked")
except:
    messages.append("parsing failed")

try:
    file = parser.getFile(pdict['filePath'])
    messages.append("file retrieved")
except:
    messages.append("file not found")

print '\n'.join(messages)

rawDescription: Pythonic way to handle updating status string between functions?


code:
# configuration: {label: Column}
column_labels = {
        'Country': User.Country,
        'UT': User.User_Type,
        }

def get_ntile(additional_columns=None):
    """ @return: sqlalchemy expression for selecting a given ntile() using
    predefined as well as *additional* columns.
    """
    partition_by = [
        User.MAUorALL,
        User.User_Type,
        User.Month_ID,
        ]
    label = "Ntile_Objects_Created"
    if additional_columns:
        lbls = []
        for col_name in additional_columns:
            col = column_labels[col_name]
            partition_by.append(col)
            lbls.append(col_name)
        label = "Ntile_{}_Objects_Created".format("_".join(lbls))
    xprs = over(
            func.ntile(10),
            partition_by = partition_by,
            order_by = User.Objects_Created,
            ).label(label)
    return xprs

def get_query(additional_columns=['UT', 'Country']):
    """ @return: a query object which selects a User with additional ntiles
    for predefined columns (fixed) and all possible permutations of
    *additional_columns*
    """
    from itertools import permutations#, combinations
    tiles = [get_ntile(comb)
            for r in range(len(additional_columns) + 1)
            for comb in permutations(additional_columns, r)
            ]
    q = session.query(User, *tiles)
    return q

q = get_query()
print [_c["name"] for _c in q.column_descriptions]
# >>> ['User', 'Ntile_Objects_Created', 'Ntile_UT_Objects_Created', 'Ntile_Country_Objects_Created', 'Ntile_UT_Country_Objects_Created', 'Ntile_Country_UT_Objects_Created']

for tile in q.all():
    print tile

rawDescription: SQLalchemy: quantiles for all permutations of column value combinations


code:
class Dicto(dict):
    def __missing__(self, key):
        self[key]=[]
        return self[key]

d=Dicto()
for word in li:
    d[len(word)].append(word)

result=[d[n] for n in sorted(d, reverse=True)]
print result

rawDescription: Group a list by word length

description: Group a list by word length


code:
for y in xrange( 70, 131, 30):
  for x in xrange( 80, 741, 110):
    print x, y

rawDescription: sequence-pattern based algorithm with numbers

description: Sequence-pattern based algorithm with numbers


code:
import re
regex_strings = ['test1','test2','test3']
regex = re.compile("(" + "|".join(regex_strings) + ")")
strgs = ['This is a test1','This is a test2','This is a test1','This is a test1','This is a test3']
print [strg for strg in strgs if regex.search(strg)]

rawDescription: Python regex list using list

description: Python regex list using list


code:
if guess != 'hint':
  print "Sorry, that's not the answer."

rawDescription: Giving 'hints' problem

description: Giving 'hints' problem


code:
variables = {
   'x' : 'Hello World'
}

with open('b.py') as b_file:
     exec b_file in variables

rawDescription: How can I give an imported library access to current global variables?

description: Give an imported library access to current global variables


code:
reader = csv.DictReader(data.splitlines(), delimiter='\t')
for row in reader:
     print row

rawDescription: Tab-delimited file using csv.reader not delimiting where I expect it to


code:
for k,v in d.items():
    for j,h in v.items():
        print k,j,h

rawDescription: GAE Python - 'Clamp' dates together into a 'tree' structure


code:
import re
data = "asdsaq2323-asds"
if re.match("^[a-zA-Z0-9*]+$", data):
    print "match"
else:
    print "no match"

rawDescription: Python: Data validation using regular expression


code:
clist = []
for i in alist:
    if i in blist:
        clist.append(i)

print clist

rawDescription: List & Loops in python3

description: List & Loops in python3


code: print calendar.timegm(datetime(1970, 1, 1, 0, 0, 0, tzinfo=pytz.utc).utctimetuple())

rawDescription: Convert date to second from a reference - Python


code:
from dis import dis

def foo(a, (b, c) ,d):
    return a + b + c + d

def bar(a, b_c, d):
    b, c = b_c
    return a + b + c + d

def baz(a, b, c, d):
    return a + b + c + d

print '\nfoo:'
dis(foo)
print '\nbar:'
dis(bar)
print '\nbaz:'
dis(baz)

rawDescription: Does the inaccessible `.0` variable in `locals()` affect memory or performance?


code:
import hidden_lib
import inspect
print inspect.getsource(hidden_lib.train_classifier)

rawDescription: Access a hidden library function in Python?


code:
import sys
import re

addresses = set()
pattern = re.compile('^\x20\w{1,}@\w{1,}\.\w{2,3}\x20?')

with open('/Users/me/Desktop/test.txt', 'r') as f:
    for line in f:
        if '->' in line:
            temp = line.split('->')
        elif '=>' in line:
            temp = line.split('=>')
        else:
            # neither '=>' nor '->' present in the line
            continue

        match = pattern.match(temp[1])
        if match is not None:
            addresses.add(match.group())
        else:
            print "nono"

for address in sorted(addresses):
    print(address)

rawDescription: Sorting the unique values from regex match in python


code:
import sys

def main(x):
    print(x)

if __name__ == "__main__":
     print sys.argv #notice this is whatever you put in the arguments
     print sys.argv[0] # this is the name of the file you used
     main(sys.argv[1])

rawDescription: Parsing command line parameters

description: Parsing command line parameters


code:
# Create static member
class Foo:
    bar = 1

# Create and destroy object of type Foo
foo = Foo()
del foo

# Check that static members survive
print Foo.bar # Still prints '1'

rawDescription: Static class members python

description: Static class members python


code:
class Foo:
    def __repr__(self):
            return '__repr__'
bar = Foo()
bar
print bar

rawDescription: How to print a list, dict or collection of objects, in Python

description: Print a list, dict or collection of objects, in Python


code:
import re

x = ["/2.ext", "/4.ext", "/5.ext", "/1.ext"]

print [['/{}.ext'.format(j) for j in range(1,int(re.search(r'\d+',i).group(0))+1)] for i in x]

rawDescription: python list modification to list of lists

description: Python list modification to list of lists


code:
import re

matcher = re.compile(r"""\\plottwo   # The literal \plottwo
                         {           # Opening brace for the first image group
                          (          # Start the first capture group
                           [^}]+     # Match anything OTHER than a closing brace
                          )          # End the first capture group
                         }           # Closing brace
                         {           # Opening brace for the second image group
                          (          # Start the second capture group
                           [^}]+     # Match anything OTHER than a closing brace
                          )          # End the second capture group
                         }           # Closing brace
                      """, re.VERBOSE)

print matcher.findall('\\plottwo{image1}{image2}')

rawDescription: Find two strings next to a keyword with re in python