code:
#resample and fill missing data
df = df.groupby(df.index).apply(lambda x: x.set_index('Bin_time').resample('100ms', how='first',fill_method='ffill'))

#reset only first level - drop index row
df = df.reset_index(level=0, drop=True)
#resample by 100ms, outside are NaN
df = df.resample('100ms', how='first')
df = df.reset_index()
#convert timedelta to integer back
df['Bin_time'] = (df['Bin_time'] / np.timedelta64(1, 'ms')).astype(int)

print df

rawDescription: Resampling with start and end date columns


code:
#!/usr/local/bin/python
# by Daniel Rosengren, modified by e-satis
"""
Module doctring
"""

import time
from sys import stdout

BAILOUT = 16
MAX_ITERATIONS = 1000

def mandelbrot(dim_1, dim_2):
    """
    function doc string
    """
    cr1 = dim_1 - 0.5
    ci1 = dim_2
    zi1 = 0.0
    zr1 = 0.0

    for i in xrange(MAX_ITERATIONS) :
        temp = zr1 * zi1
        zr2 = zr1 * zr1
        zi2 = zi1 * zi1
        zr1 = zr2 - zi2 + cr1
        zi1 = temp + temp + ci1

        if zi2 + zr2 > BAILOUT:
            return i

    return 0

def execute() :
    """
    func doc string
    """
    print 'Rendering...'
    for dim_1 in xrange(-39, 39):
        stdout.write('\n')
        for dim_2 in xrange(-39, 39):
            if mandelbrot(dim_1/40.0, dim_2/40.0) :
                stdout.write(' ')
            else:
                stdout.write('*')

START_TIME = time.time()
execute()
print '\nPython Elapsed %.02f' % (time.time() - START_TIME)

rawDescription: PyLint, PyChecker or PyFlakes?

description: PyLint, PyChecker or PyFlakes


code:
import re

compiled = re.compile(r"""
    .*?                                # Leading junk
    (?P<ipaddress>\d+\.\d+\.\d+\.\d+)  # IP address
    .*?                                # Trailing junk
    """, re.VERBOSE)
str = "IP 1.2.3.4 is currently trusted in the white list, but it is now using a new trusted certificate."
m = compiled.match(str)
print m.group("ipaddress")

rawDescription: How to extract a word from text in Python

description: Extract a word from text in Python


code:
from lxml import objectify

root = objectify.fromstring("""
 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   1
   1.2
   1
   true
   <c>what?</c>
   <d xsi:nil="true"/>
 </root>
""")

print objectify.dump(root)

rawDescription: Is there any way or any framework in python to create an object model from a xml?


code:
class Mixin(object):
    def talk(self):
        print self.get_message()

class Inheritor(Mixin):
    def get_message(self):
        return "I'm inheriting"

i = Inheritor()
i.talk()
# prints "I'm inheriting"

rawDescription: Constructing Mixins

description: Constructing Mixins


code:
mylogfile = open("test.txt", "w")
print >> mylogfile, 'hola'
mylogfile.close()
mylogfile = open("test.txt", "r")
print mylogfile.read()

rawDescription: Unable to write to a text file

description: Unable to write to a text file


code:
print int("0xdeadbeef", 0)
print int("10", 0)

rawDescription: Convert hex string to int in Python

description: Convert hex string to int in Python


code:
sum(1 for dealt in combinations(deck, 3) for _ in combinations([c for c in deck if c not in dealt], 3))
for hand1 in combinations(deck, 3):
    for hand2 in combinations([c for c in deck if c not in hand1], 3):
        print hand1, hand2

rawDescription: generate two sets of combination with one list in python

description: Generate two sets of combination with one list in python


code:
l=('pizza', 'garlic', 'jam')
p=[]
for i,j,k in permutations(l,3) :
    m=re.search(r'({}.*?{}.*?{})'.format(i,j,k),text)
    if m :
       p.append(m.group(0))

print min(p,key=len)

rawDescription: Regular expression gets 3 words near each other. How to get their context?


code:
class Object(object):
    def __init__(self, values):
        self.values = values
    def __lt__(self, other):
        return [x < other for x in self.values]
    def __gt__(self, other):
        return [x > other for x in self.values]

x = Object([1, 2, 3])
print x < 5 # [True, True, True]
print x > 5 # [False, False, False]
print 0 < x < 5 # [True, True, True]

rawDescription: What is the operator precedence when writing a double inequality in Python (explicitly in the code, and how can this be overridden for arrays?)


code:
my_list = [True, 10.8, [1,2,3], False, True, "Hello", 12, "Sbioer", 2.5]

for i, item in enumerate(my_list):
    if isinstance(item, int) and not isinstance(item, bool):
        print i

rawDescription: Finding the index of the element contains string in a list


code:
print df1.columns.unique()
['cm1' 'cm2']

rawDescription: How to process column names and create new columns

description: Process column names and create new columns


code:
[x for x in range(10)]
def foo(): print 'foo'

[foo() for x in range(10)]

rawDescription: how to use python list comprehensions replace the function invoke inside of "for" stmt?

description: Use python list comprehensions replace the function invoke inside of "for" stmt


code:
result = cur.fetchone()
print result['count']

rawDescription: Get psycopg2 count(*) number of results

description: Get psycopg2 count(*) number of results


code:
dict_dmg = {
    "Normal":{"Rock":0.5,"Dragon":0.5},
    "Rock":{"Fighting":0.5,"Ground":0.5,"Steel":0.5,"Fire":2,"Ice":2,"Fly":2,"Bug":2},
    "Water":{"Water":0.5,"Grass":0.5,"Dragon":0.5,"Fire":2,"Ground":2,"Rock":2},
    "Grass":{"Fire":0.5,"Grass":0.5,"Poison":0.5,"Fly":0.5,"Bug":0.5,"Dragon":0.5,"Steel":0.5,"Water":2,"Ground":2,"Rock":2},
    "Fighting":{"Poison":0.5,"Fly":0.5,"Psychic":0.5,"Bug":0.5,"Fairy":0.5,"Normal":2,"Ice":2,"Rock":2,"Dark":2,"Steel":2},
    "Ground":{"Grass":0.5,"Bug":0.5,"Fire":2,"Electric":2,"Poison":2,"Rock":2,"Steel":2},
    "Electric":{"Electric":0.5,"Grass":0.5,"Dragon":0.5,"Water":2,"Fly":2}
}

def dmg_modifier2(attack_type, receiver_pokemon_type, dict_dmg):
    dmg = dict_dmg[attack_type].get(receiver_pokemon_type, 1)

    print "{0} deals {1}x damage".format(attack_type, dmg)

rawDescription: Concatenating two variable names to automate finding something in list python


code:
print depth_values(vehicles, 0) # >> 58
print depth_values(vehicles, 1) # >> {'Holden': 31, 'Ford': 27}
print depth_values(vehicles, 2) # >> {'Holden': {'Commodore': 31}, 'Ford': {'Falcon': 27}}
print depth_values(vehicles, 3) # exceptions

rawDescription: Python: get a value count at various dict depths


code:
for paired in (dict(p) for v in pleio.itervalues() for p in combinations(v.iteritems(), 2)):
    print paired

rawDescription: Pair combinations of elements in dictionary without repetition


code:
import locale
import re
from lxml import html
import requests

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

page = requests.get('http://twitter.com/aberdeencc')
tree = html.fromstring(page.text)

followers = tree.xpath('//a[@data-nav="followers"]/@title')[0]
followers = re.match(r'^([0-9,]+)\sFollowers$', followers).group(1)
followers = locale.atoi(followers)

print 'Followers:', int(followers)

rawDescription: Grabbing a specific value from a webpage using LXML


code:
q=Queue.PriorityQueue()
q.put((-2,"second"))
q.put((-1,"first"))
q.put((-3,"third"))
print q.get(True)
print q.get(True)
print q.get(True)

rawDescription: getting python priority queue return item of largest priority

description: Getting python priority queue return item of largest priority


code:
primes=[]
input = raw_input("> ")
input2 = raw_input("> ")

for num in range(int(input), int(input2)):
    if all(num%i!=0 for i in range(2,num)):
       primes.append(num)

print primes

rawDescription: Incorrect Prime number list in python

description: Incorrect Prime number list in python


code:
import threading
import multiprocessing as mp
import sys

def ask(stdin):
    print 'Your name? ',
    a = stdin.readline().strip()
    if a == 'Tester':
        print 'Hello'
    else:
        print 'Bye'
    stdin.close()

def writeFiles():
    pass

if __name__ == '__main__':
    p1 = mp.Process(target=writeFiles)
    p1.start()
    t1 = threading.Thread(target=ask, args=(sys.stdin,))
    t1.start()
    p1.join()
    t1.join()

rawDescription: Raw_input inside a Python process

description: Raw_input inside a Python process


code:
# Returns a copy of original_fn with its internal function
# called name replaced with new_fn.
def monkey_patch_fn( original_fn, name, new_fn ):

  #Little helper function to pick out the correct constant
  def fix_consts(x):
    if x==None: return None
    try:
      if x.co_name == name:
        return new_fn.func_code
    except AttributeError, e:
        pass
    return x

  original_code = original_fn.func_code
  new_consts = tuple( map( fix_consts, original_code.co_consts ) )
  code_type_args = [
     "co_argcount", "co_nlocals", "co_stacksize", "co_flags", "co_code",
     "co_consts", "co_names", "co_varnames", "co_filename", "co_name",
     "co_firstlineno", "co_lnotab", "co_freevars", "co_cellvars" ]

  new_code = types.CodeType(
     *[ ( getattr(original_code,x) if x!="co_consts" else new_consts )
        for x in code_type_args ] )
  return types.FunctionType( new_code, {} )

rawDescription: Does an equivalent of override exist for nested functions?


code:
x=5
y=10

def func1():
    global x, y
    x+=1
    y+=1
    print x, y

def func2():
    while True:
        func1()

func2()

rawDescription: python run code that is in a function once

description: Python run code that is in a function once


code:
def height(bReader, dataList):
    print("\nIn height function...")
    # bReader is bores inside polygon
    # dataList BORE_DATA.csv as 2D list
    for row in bReader:
        # initialise measurements for each bore
        avgHeight = 0
        minN = 9e99
        maxPh = 0
        numCount = 0

        for row2 in dataList:
            if row[0] == row2[0]:
                avgHeight += float(row2[3])
                numCount += 1

                print("Water level: {0}".format(row2[3])) # water level

                # Checks the minimum values of Nitrogen
                if float(row2[6]) < minN:
                    minN = float(row2[6])

                 # Checks the maximum values of Phosphorous
                if float(row2[5]) > maxPh:
                    maxPh = float(row2[5])

       # Checks for final value before moving to next bore
        aveHeight = avgHeight / numCount
        print row[0]
        print("Average Water Level: {0}".format(aveHeight))
        print("Minimum Nitrogen: {0}".format(minN))
        print("Maximum Phosphorous {0}".format(maxPh))
        avgHeight = 0
        minN = 9e99
        maxPh = 0
        numCount = 0

# added below for test
import csv

# create file objects and readers
f1, f2 = open('list1.csv', 'rb'), open('list2.csv', 'rb')
r1 = csv.reader(f1, delimiter=',', quotechar='"')
r2 = csv.reader(f2, delimiter=',', quotechar='"')

# create lists from readers
l1 = [[col for col in row] for row in r1]
l2 = [[col for col in row] for row in r2]

# check first couple of rows from lists
print('\nSample data from list 1\n' + '-'*23)
for row in  l1[:2]: print(row)
print('\nSample data from list 2\n' + '-'*23)
for row in l2[:3]: print(row)

# call function without headings from first rows
height(l1[1:], l2[1:])

rawDescription: Skip first line in 2D list and use values


code:
test = True
d = {"a": 23, "b": 42}
d["a" if test else "b"] = 1
print d
#result: {'a': 1, 'b': 42}

rawDescription: Switch between assignments to different variables in python? With ternary operator?


code:
from re import findall, DOTALL

text = '''
title

content here

text...

text...
title

'''
print findall(r'title.*?.*?.*?', text, DOTALL)

rawDescription: Regex: match text between 2 items

description: Regex: match text between 2 items


code:
for a,b in states.items():
    print '%s is the abbreviation for %s' % (b,a)

rawDescription: How does this python dictionary code actually work?


code:
xmldoc = minidom.parse('sampleXML.xml')
#Grab the first occurence of the "secondSetOfItems" element
second_items = xmldoc.getElementsByTagName("secondSetOfItems")[0]
item_list = second_items.getElementsByTagName("item")

for item in item_list:
    print item.attributes['name'].value

rawDescription: Specific pathing to find XML elements using minidom in Python


code:
file = open('test.txt','r')

start_keyword = 'start'
end_keyword = 'end'
in_range = False
entities = []

lines = file.readlines()

for line in lines:

    line = line.strip()

    if line == start_keyword:
        in_range = True
    elif line == end_keyword:
        in_range = False

    elif in_range:
        entities.append(line)

file.close()

# If you want to include the start/end tags
#entities = [start_keyword] + entities + [end_keyword]

print entities

rawDescription: How to find a substring in a line and append from that line up to the next substring?

description: Find a substring in a line and append from that line up to the next substring


code:
import re

lst = ['data.items.[0].deliveryAddress.region',
'data.items.[0].classification.scheme',
'data.items.[0].classification.id',
'data.items.[0].description']

# no need for re.MULTILINE here
rx = re.compile(r'^((?:.(?!classification))+)$')

matches = [x for x in lst if rx.match(x)]
print matches
# ['data.items.[0].deliveryAddress.region', 'data.items.[0].description']

rawDescription: Regex match key except one

description: Regex match key except one


code:
import urllib2
server = "http://localhost:5000/"
req = urllib2.Request(server)
response = urllib2.urlopen(req)
print response.read()

rawDescription: [Python-Webservice]: How to return and consume on XML format on Flask


code:
import wx
def SomeListener(evt):
    print "Got Event:",evt
    print "My XY:",evt.GetX(),evt.GetY()
    #youll have to figure out which word you clicked using x,y (note x,y relative to static text field)
a= wx.App(redirect=False)
f = wx.Frame(None,-1)
p = wx.Panel(f,-1)
t = wx.StaticText(p,-1,"Some Text")
t.Bind(wx.EVT_LEFT_DOWN,SomeListener)
f.Show()
a.MainLoop()

rawDescription: Mouse events on text in Python using wxPython


code:
import numpy as np

dat = np.array([["Name1", "1,3,4,5", "Description", "2"],
    ["Name2", "5,3", "Description", "4"],
    ["Name3", "2", "Description", "1"],
    ["Name4", "1", "Description", "7,3"],
    ["Name5", "3", "Description", "5,9,4"],
    ["Name6", "2", "Description", "7"],
    ["Name7", "7", "Description", "2"],
    ["Name8", "1", "Description", "4"],
    ["Name9", "4", "Description", "2"]])

#since you only want the 7 initial columns
dat = dat[0:7,:]

#extract second and fourth columns
c1 = dat[:,1]
c2 = dat[:,3]
names = dat[:,0]

#get the start position via string manipulation
startpos = []
for item in c1:
    startpos.append(item.split(',')[0])

startpos = np.array(startpos).astype(np.int32)

#get the end position via string manipulation
endpos = []
for item in c2:
    endpos.append(item.split(',')[-1])

endpos = np.array(endpos).astype(np.int32)
print startpos
print endpos

rawDescription: Python: comparing columns in numpy

description: Python: comparing columns in numpy


code:
from sqlalchemy import Integer, Boolean, String, Column, create_engine, \
    ForeignKey
from sqlalchemy.orm import Session, relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm.collections import attribute_mapped_collection

Base = declarative_base()

class SomeClass(Base):
    __tablename__ = 'sometable'

    id = Column(Integer, primary_key=True)
    tuple_elements = relationship(
                "TupleAssociation",
                collection_class=attribute_mapped_collection("name"),
                cascade="all, delete-orphan"
            )
    items = association_proxy("tuple_elements", "as_tuple")

class TupleAssociation(Base):
    __tablename__ = 'tuple_association'
    parent_id = Column(Integer, ForeignKey('sometable.id'), primary_key=True)
    tuple_id = Column(Integer, ForeignKey("tuple_data.id"), primary_key=True)
    name = Column(String)

    tuple_element = relationship("TupleElement")

    def __init__(self, key, tup):
        self.name = key
        self.tuple_element = TupleElement(tup)

    @property
    def as_tuple(self):
        return self.tuple_element.as_tuple

class TupleElement(Base):
    __tablename__ = 'tuple_data'

    id = Column(Integer, primary_key=True)
    col1 = Column(Integer)
    col2 = Column(Boolean)

    def __init__(self, tup):
        self.col1, self.col2 = tup

    @property
    def as_tuple(self):
        return self.col1, self.col2

e = create_engine('sqlite://')
Base.metadata.create_all(e)
s = Session(e)

collection = SomeClass()
collection.items["item name 1"] = (3, True)
collection.items["item name 2"] = (1, False)
print collection.items

s.add(collection)
s.commit()

collection = s.query(SomeClass).first()
print collection.items

rawDescription: Using a tuples of values in sqlalchemy mapped collection


code:
print [sub for sub in dir(cv2) if sub.startswith('COLORMAP_')]
['COLORMAP_AUTUMN', 'COLORMAP_BONE', 'COLORMAP_COOL', 'COLORMAP_HOT', 'COLORMAP_HSV', 'COLORMAP_JET', 'COLORMAP_OCEAN', 'COLORMAP_PINK', 'COLORMAP_RAINBOW', 'COLORMAP_SPRING', 'COLORMAP_SUMMER', 'COLORMAP_WINTER']

rawDescription: How to get the red channel color space of an image?

description: Get the red channel color space of an image


code:
import numpy as np

img1=np.uint8(np.random.randint(0, 255, (480, 640)))
img2=np.uint8(np.random.randint(0, 255, (480, 640)))

def differenceImageV1(img1, img2):
  diff=np.empty_like(img1)
  h, w=img1.shape
  for y in range(h):
    for x in range(w):
      if img1[y, x]<img2[y, x]: diff[y, x]=img2[y, x]-img1[y, x]
      else:                     diff[y, x]=img1[y, x]-img2[y, x]
  return(diff)

def differenceImageV2(img1, img2):
  return(np.uint8(np.abs(np.int16(img1)-img2)))

def differenceImageV3(img1, img2):  # fast - but wrong result
  return(img1-img2)

def differenceImageV4(img1, img2):
  return np.where(img1>img2, img1-img2, img2-img1)

def differenceImageV5(img1, img2):
  a = img1-img2
  b = img2-img1
  c = img1>img2
  return a*c + b*(~c)

def differenceImageV6(img1, img2):
  a = img1-img2
  b = np.uint8(img1<img2) * 254 + 1
  return a * b

import timeit
def testit():
  for fn in [differenceImageV2, differenceImageV3, differenceImageV4, differenceImageV5, differenceImageV6]:
    print fn.__name__, np.sum(fn(img1, img2).astype('int64')),
    print timeit.timeit("%s(img1, img2)" % fn.__name__, "from test import img1, img2, %s" % fn.__name__, number=1000)

if __name__ == '__main__':
    testit()

rawDescription: Fast absolute difference of two uint8 arrays


code:
import urllib2
from lxml import etree

URL = "http://www.kb.cert.org/vuls/id/628463"
response = urllib2.urlopen(URL)

parser = etree.HTMLParser()
tree = etree.parse(response, parser)

for header in tree.iter('h3'):
    paragraph = header.xpath('(.//following-sibling::p)[1]')
    if paragraph:
        print "%s: %s" % (header.text, paragraph[0].text)

rawDescription: How to select following sibling tag with xpath

description: Select following sibling tag with xpath


code:
create = """
oldlist = range(5000)
newlist = range(5000, 10000)
index = 500
"""
tests = [
    "newlist.extend(oldlist[index:])",
    "newlist.extend(oldlist[i] for i in xrange(index, len(oldlist)))",
    "newlist.extend(islice(oldlist, index, None))",
    """\
while index < len(oldlist):
   newlist.append(oldlist[index])
   index+=1""",
]

import timeit
for test in tests:
    t = timeit.Timer(create + test, setup='from itertools import islice')
    print test, min(t.repeat(number=100000))

rawDescription: Eliminate this unneeded copy in list.extend


code:
class WorkerThread(threading.Thread):

    def run(self):
        while True:
            print 'Working hard'
            time.sleep(0.5)

def runstuff():
    worker = WorkerThread()
    worker.start() #start thread here, which will call run()

rawDescription: How to handle chat client using threading and queues?

description: Handle chat client using threading and queues


code:
tuple_list = [('a', 1), ('b', 3), ('c', 2)]
def flatten_tuple(tuple_list):
    return list(reduce(lambda x,y: x + y, tuple_list))

print flatten_tuple(tuple_list)

rawDescription: Python: convert a list of tuples into a flat list


rawDescription: Splitting a text file into a list of lists


code:
from itertools import chain

for keys, values in my_dict.iteritems():
    fmt_str = ', '.join(map(str, chain(keys, *values)))
    print fmt_str

rawDescription: More pythonic way for dealing with lists and dicts


code: print struct.unpack_from("<l",f)[0]

rawDescription: Start at offset from beginning of file and unpack 4 byte word


rawDescription: Rename row indexes of pandas groupby object


code:
import csv
with open("so.csv",'rU') as f:
    a = list(csv.DictReader(f, dialect=csv.excel_tab, delimiter=','))
print a[2]['Make']

rawDescription: How can I put a CSV file in an array?

description: Put a CSV file in an array


code:
import time
import threading

count = 0

class Screen(threading.Thread):
    def __init__(self, count):
        threading.Thread.__init__(self)
        self.count = count

    def do_something(self):
        while True:
            time.sleep(1)
            self.count += 1
            if self.count >=3:
                print "Stop!"
                break

    def run(self):
        self.do_something()

def test():
    thread1 = Screen(count)
    thread1.start()

test()

rawDescription: How to use thread instance function inside a function?

description: Use thread instance function inside a function


code: print filter(lambda x: not any(y for y in list1 if y in x), list2)

rawDescription: How to remove items from a list if they match any items in another list in Python

description: Remove items from a list if they match any items in another list in Python


rawDescription: how to split a string by characters which occur in another list?

description: Split a string by characters which occur in another list


code:
import ctypes
MOUSEEVENTF_MOVE = 0x0001 # mouse move
MOUSEEVENTF_ABSOLUTE = 0x8000 # absolute move
MOUSEEVENTF_MOVEABS = MOUSEEVENTF_MOVE + MOUSEEVENTF_ABSOLUTE

MOUSEEVENTF_LEFTDOWN = 0x0002 # left button down
MOUSEEVENTF_LEFTUP = 0x0004 # left button up
MOUSEEVENTF_CLICK = MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP

def click(x, y):
    #move first
    x = 65536L * x / ctypes.windll.user32.GetSystemMetrics(0) + 1
    y = 65536L * y / ctypes.windll.user32.GetSystemMetrics(1) + 1
    ctypes.windll.user32.mouse_event(MOUSEEVENTF_MOVEABS, x, y, 0, 0)

    #then click
    ctypes.windll.user32.mouse_event(MOUSEEVENTF_CLICK, 0, 0, 0, 0)

rawDescription: python win32 simulate click

description: Python win32 simulate click


code:
print u"result code %d" % result[u'code']
# doesn't touch 'strings' in comments
u'handles multilines' + u'okay'
u'Unicode is not touched'

rawDescription: How to ensure all string literals are unicode in python

description: Ensure all string literals are unicode in python


code:
print 'Double" quote inside single'
print "Single' quote inside double"

rawDescription: Python: Difference between using ' and "?


code:
from itertools import islice

with open('/tmp/results_nslookup.txt', 'r') as f:
    for line in f:
        if line == 'Non-authoritative answer:\n':
            print ''.join(islice(f, 8))

rawDescription: Python: Print next x lines from text file when hitting string


code:
for key,count in letters.iteritems():
    print key, count

rawDescription: Python: How can I read the elements

description: Python: How can I read the elements


code:
# attack function
def attack(victim):
    dice20 = d20()
    dice100 = d100()

    print "victim == monster", victim == monster

    print dice20
    print dice100
    print to_hit
    print crit
    print monster_current_hp
    print mod_dmg
    print mod_dmg * 3
    print monster_to_hit
    print monster_crit
    print hit_points
    print monster_dmg
    print monster_dmg * 3

    global monster_current_hp, current_hp, to_hit, crit, hit_points
    if victim == monster:
        if dice20 >= to_hit and dice100 <= crit:
            print "You scored a critical hit against the %s!" % monster
            monster_current_hp = monster_current_hp - (mod_dmg * 3)
            next()
        elif dice20 >= to_hit and dice100 > crit:
            print "You strike the %s!" % monster
            monster_current_hp = monster_current_hp - mod_dmg
            next()
        else:
            print "The %s evades your attack!" % monster
            next()
    else:
        if dice20 >= monster_to_hit and dice100 <= monster_crit:
            print "The %s scored a critical hit against you!" % monster
            hit_points = hit_points - (monster_dmg * 3)
            next()
        elif dice20 >= monster_to_hit and dice100 > crit:
            print "The %s strikes you!"
            hit_points = hit_points - monster_dmg
            next()

rawDescription: Why is this program repeating itself?

description: Why is this program repeating itself


code:
d = []
#no need of two loops here, if the number is present in c then append 1 else 0
for i in range(m, -1, -1): #goes from 14 to 0, -1 is not inclusive
    if i in c:
        d.append(1)
    else:
        d.append(0)

#one-liner : d = [1 if x in c else 0  for x in xrange(14, -1, -1)]
print d,len(d)
#prints [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1] 15

rawDescription: Python read binary polynomial into list

description: Python read binary polynomial into list


code:
import re

# normally, read your input directly from file, but this allows us to test:
input = """The pons also contains the sleep paralysis center of the brain as well as generating the dreams of REM sleep.
The optic tract is a part of the visual system in the brain.
The inferior frontal gyrus is a gyrus of the frontal lobe of the human brain.
The prefrontal cortex (PFC) is the anterior part of the frontallobes of the brain, lying in front of the motor and premotor areas.
There are three possible ways to define the prefrontal cortex as the granular frontal cortex as that part of the frontal cortex whose electrical stimulation does not evoke movements.
This allowed the establishment of homologies despite the lack of a granular frontal cortex in nonprimates.
Modern  tracing studies have shown that projections of the mediodorsal nucleus of the thalamus are not restricted to the granular frontal cortex in primates.
""".split('\n')

patterns = [
    ('(dreams of REM (Geo)? sleep)', '(sleep paralysis)'),
    ('(frontal lobe)',            '(inferior frontal gyrus)'),
    ('(prefrontal cortex)',       '(frontallobes of (leftside )?(the )?brain)'),
    ('(modern tract)',            '(probably mediodorsal nucleus)') ]

# Compile the patterns as regexes
patterns = [ (re.compile(dstr),re.compile(rstr)) for (dstr,rstr) in patterns ]

def longest(t):
    """Get the longest from a tuple of strings."""
    l = list(t) # tuples can't be sorted (immutable), so convert to list...
    l.sort(key=len,reverse=True)
    return l[0]

def custommatch(line):
    for (d,r) in patterns:
        # If got full match to both (d,r), return it immediately...
        (dm,rm) = (d.findall(line), r.findall(line))
        # Slight design problem: we get tuples like: [('frontallobes of the brain', '', 'the ')]
        #... so return the longest match strings for each of dm,rm
        if dm and rm: # must match both dom & rang
            return [longest(dm), longest(rm)]
        # else score any partial matches to (d,r) - how exactly?
        # TBD...
    else:
        # We got here because we only have partial matches (or none)
        # TBD: return the 'highest-scoring' partial match
        return ('TBD... partial match')

for line in input:
    print custommatch(line)

rawDescription: Match longest substring in Python

description: Match longest substring in Python


code: print doc('h1').remove('span')

rawDescription: PyQuery: Get only text of element, not text of child elements


code:
a = 5     # In binary: 0101
b = 3     # In binary: 0011
c = a ^ b # In binary: 0110

print c   # In decimal: 6

rawDescription: How do the bitwise operations work?

description: How do the bitwise operations work


code:
def writeToFile():
    with open("results.txt", "w") as fp:
        fp.write('{}\n'.format('\n\n'.join(['\n'.join(haiku) for haiku in haikuList])))
        print "There are {} lines in your file.".format(len(haikuList)*3 + len(haikuList)-1)

rawDescription: Writing to a File, Adding an Additional Space


code:
import urlparse
s = u'somesite.com/param.php?id=911&param2=<<<<<self.fooz>>>>>&param3=cat'
params = urlparse.parse_qs(s[s.find('?') + 1:])
print params

rawDescription: Get proper output from self var python

description: Get proper output from self var python


code:
server_name = '<server_name>'
rifs = '<rifs>\r'
print 'mco shell run -I {0} "sc qc {1} 5000"'.format(server_name, rifs)
print 'mco shell run -I {0} "sc qc {1} 5000"'.format(server_name, rifs.rstrip('\r'))

rawDescription: Python String Format not working as expected when ran in a script


code:
import numpy as np
import time
data = np.random.random((4000, 4000))

start = time.clock()
np.std(data, axis=0)
print time.clock() - start

start = time.clock()
np.std(data, axis=1)
print time.clock() - start

rawDescription: Any way to optimize numpy stats functions (e.g., via numexpr)?


code:
from bs4 import BeautifulSoup
data = """

This is ABC

 data1_xxx

This is EFG

 data1_xxx

"""
soup = BeautifulSoup(data)
print soup.find("td", {"class":"abc"}).find_parent('table')

rawDescription: How to extract html table by using Beautifulsoup

description: Extract html table by using Beautifulsoup


code:
import time
import math

def timeit1():
    s = time.time()
    for i in xrange(750000):
        z=i**.5
    print "Took %f seconds" % (time.time() - s)

def timeit2(arg=math.sqrt):
    s = time.time()
    for i in xrange(750000):
        z=arg(i)
    print "Took %f seconds" % (time.time() - s)

timeit1()
timeit2()

rawDescription: Which is faster in Python: x**.5 or math.sqrt(x)?


code:
# Get the data from the file
with open('public.txt') as fp:
  v = fp.read()

# The data is base64 encoded. Let's decode it.
v = v.decode('base64')

#  The data is now a string in base-256. Let's convert it to a number
v = v.encode('hex')
v = int(v, 16)

# Now it is a number. I wonder what number it is:
print v
print hex(v)

rawDescription: Reading from a file and assigning content to a variable in Python


code:
with open("your_file.csv") as f:
    my_list = []
    for line in f:
        if line:
            my_list.append(line.strip().split())
    for x,y in itertools.groupby(sorted(my_list, key=lambda x:x[0]), 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


rawDescription: Unicode - Just print the string

description: Unicode - Just print the string


code: print " ".join(l)

rawDescription: python expand a list to print

description: Python expand a list to print


code:
l = list(enumerate(text))
chars = groupby(l, lambda x: x[1].isalpha())
list(chars.next()[1])
for k,v in list(chars): print list(v)

rawDescription: itertools.groupby returning wrong result (this is not about sorting)

description: Itertools.groupby returning wrong result (this is not about sorting)


code:
class TestClass(object):
    variable = 1

var_1, var_2 = TestClass(), TestClass()
print var_1.variable is var_2.variable
# True
print TestClass.variable is var_1.variable
# True

rawDescription: python using __init__ vs just defining variables in class - any difference?

description: Python using __init__ vs just defining variables in class - any difference


code:
inds = np.where(np.diff(x))[0]
out = np.split(x,inds[np.diff(inds)==1][0::2]+2)
for n in out:
    print n

rawDescription: Split list based on when a pattern of consecutive numbering stops


rawDescription: how to read from an array without a particular column in python

description: Read from an array without a particular column in python


code:
d={}
for data in power_set(l):
    d[data]="your_calc_prob"
print d
{(0, 1): 'your_calc_prob', (1, 0): 'your_calc_prob', (0, 0): 'your_calc_prob', (1, 1): 'your_calc_prob'}

rawDescription: Creating dictionary dynamically in Python


code:
#!/usr/bin/env python

reservations = [
    # [DHCP SERVER, IP ADDRESS, MAC ADDRESS, HOSTNAME, DESCRIPTION]
    ['server1','172.16.0.120','31872fcefa33','wks120.domain.net','Description of client'],
    ['server1','172.16.0.125','4791ca3d7279','wks125.domain.net','Description of client'],
    ['server1','172.16.0.132','6035a71c930c','wks132.domain.net','Description of client'],
]

def reservationlist(reservations, serverpattern, addresspattern, hostpattern,
        start, end):
    result = []
    for i in range(start, end + 1):
        result.append([
            serverpattern % i,
            addresspattern % i,
            '[no mac]',
            hostpattern % i,
            'Unregistered'])

    for reservation in reservations:
        index = int(reservation[1].split('.')[3]) - start
        result[index] = reservation

    return result

print reservationlist(
    reservations,
    'server%d',
    '172.16.0.%d',
    'wks%d.domain.net',
    120,
    132)

rawDescription: How do I fill in unused ip addresses in a python sublist?

description: Fill in unused ip addresses in a python sublist


code:
class genericParser(HTMLParser):
   def __init__ (self):
       HTMLParser.__init__ (self)
       self.divFound = False
       self.buff = ""
       self.level = 0

   def computeRecord (self, tag, attrs):
        mystr = "<" + tag + " "
        for att, val in attrs:
            mystr += att+"='"+val+ "' "
        mystr += ">"
        return mystr

   def handle_starttag (self, tag, attrs):
       if tag == "div" and ("class", "good") in attrs:
           self.divFound = True
       elif self.divFound:
          self.level += 1
          self.buff += self.computeRecord (tag, attrs)

   def handle_data (self, data):
       if self.divFound:
          self.buff += data

   def handle_endtag (self, tag):
      if self.divFound:
         self.buff += "</" + tag + ">"
         self.level -= 1
         if (self.level == 0):
            self.divFound = False
            print self.buff

rawDescription: Get HTML subtree from HTMLparser

description: Get HTML subtree from HTMLparser


code:
d = json_data['data']
for f in d:
    print d['field1']

rawDescription: Using python requests module to hit API, how do I filter what's returned when using 'print r.json'?


code:
import data
import someOtherModule

print data.formations['Arrowhead']
someOtherModule.function()

rawDescription: I can't but help get the idea I'm doing it all wrong (Python, again)


code:
import sys, os
from pocketsphinx import *
import pyaudio

modeldir = "../../../model"

# Create a decoder with certain model
config = Decoder.default_config()
config.set_string('-hmm', os.path.join(modeldir, 'en-us/en-us'))
config.set_string('-dict', os.path.join(modeldir, 'en-us/cmudict-en-us.dict'))
config.set_string('-kws', 'keyphrase.list')

p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
stream.start_stream()

# Process audio chunk by chunk. On keyword detected perform action and restart search
decoder = Decoder(config)
decoder.start_utt()
while True:
    buf = stream.read(1024)
    decoder.process_raw(buf, False, False)
    if decoder.hyp() != None:
        print "Detected keyword", decoder.hyp(), "restarting search"
        decoder.end_utt()
        decoder.start_utt()

rawDescription: Raspberry Pi Asynchronous/Continuous Speech Recognition in Python


code:
from itertools import islice
for index, i in enumerate(islice(xrange(1, 100), 20)):
    print index, i

rawDescription: Restrict for loop in Python for specific iterations


code:
suits = ('Hearts', 'Diamonds', 'Clubs', 'Spades')

while True:
    guess = raw_input("Make a guess:")
    guesses = {'{} of {}'.format(guess, suit) for suit in suits}
    if guesses.intersection(PH):
        # correct guess!
        break
    print "You do not have a card like that in your hand."

rawDescription: "While not in" error - python

description: "While not in" error - python


code:
with open("data1.txt") as f:
    head,sub,auth = [f.readline().strip() for i in range(3)]
    data=f.read()
    print head,sub,auth,data

rawDescription: python read text file into array

description: Python read text file into array


code:
class Parent(object):
   def __init__(self, name, number):
       self.name = name
       self.number = number

class Child(object):
    def __init__(self, parent, other):
        self.parent = parent
        self.other = other

    def __getattr__(self, name):
        try:
            return getattr(self.parent, name)
        except AttributeError, e:
            raise AttributeError("Child' object has no attribute '%s'" % name)

p = Parent("Foo", 42)
c = Child(p, "parrot")
print c.name, c.number, c.other
p.name = "Bar"
print c.name, c.number, c.other

rawDescription: How to copy all attributes of one Python object to another?

description: Copy all attributes of one Python object to another


code: print ' '.join([x.strip() for x in ele.xpath('//text()')])

rawDescription: How to parse text from html using lxml?

description: Parse text from html using lxml


code:
def v():
    response = urllib2.urlopen('https://api.gosquared.com/v2/concurrents?api_key=xxxxx&site_token=xxxx&presenter=old')
    data = json.load(response)
    #comment the below line if you don't want to print data and simply want to return it
    print data
    return data

rawDescription: get value from function using python

description: Get value from function using python


code:
import re
m = re.search("|".join(map(re.escape, lst)), string)
if m:
   print "some word from the list is in the string"

rawDescription: Determining if a query is in a string

description: Determining if a query is in a string


code:
import sys

def is_abundant(num):
    max_divisor = int(num / 2) + 1
    sum = 0
    for x in range(1, max_divisor):
        if num % x == 0:
            sum += x
    return sum > num

try:
    n = int(raw_input("Enter a number: "))
except ValueError:
    print "Invalid number"
    sys.exit(1)

if is_abundant(n):
    print ("Your number is abundant")
else:
    print ("Your number isn't abundant")

rawDescription: Determining a Abundant Number when user inputs a nmber


code:
from collections import Counter
for word, count in Counter(my_list).most_common():
    print "{} is repeated {} time{}.".format(word, count, 's' if count > 1 else '')

rawDescription: Python item sort

description: Python item sort


code:
def is_new_style(cls):
    return hasattr(cls, '__class__') \
           and \
           ('__dict__' in dir(cls) or hasattr(cls, '__slots__'))

class new_style(object):
    pass

class old_style():
    pass

print is_new_style(int)
print is_new_style(new_style)
print is_new_style(old_style)

rawDescription: Identifying that a variable is a new-style class in Python?


code:
import re
def decomposeLogEntry(text):
    r""" Decompose log file entry into its various components.

    If text is a valid log entry, return regex match object of
    log entry components strings. Otherwise return None."""
    return re.match(r"""
        # Decompose log file entry into its various components.
        ^                            # Anchor to start of string
        (?P<time>\d\d:\d\d:\d\d)     # Capture: time
        \s+
        (?P<modname>\w+?)            # Capture module name
        \s-\s\[
        (?P<msgtype>[^]]+)           # Capture message type
        \]
        (?P<message>[^!]+)           # Capture message text
        !!\sftp_site=
        (?P<ftpsite>\S+?)            # Capture ftp URL
        \sfile_dir=
        (?P<filedir>\S+?)            # Capture file directory?
        \sinput\sfile=
        (?P<infile>                  # Capture input path and filename
          (?P<infilepath>\S+)\\      # Capture input file path
          (?P<infilename>[^\s\\]+)   # Capture input file filename
        )
        \soutput\sfile=
        (?P<outfile>                 # Capture input path and filename
          (?P<outfilepath>\S+)\\     # Capture output file path
          (?P<outfilename>[^\s\\]+)  # Capture output file filename
        )
        \s*                          # Optional whitespace at end.
        $                            # Anchor to end of string
        """, text, re.IGNORECASE | re.VERBOSE)

# Demonstrate decomposeLogEntry function. Print components of all log entries.
f=open("testdata.log")
mcnt = 0
for line in f:
    # Decompose this line into its components.
    m = decomposeLogEntry(line)
    if m:
        mcnt += 1
        print "Match number %d" % (mcnt)
        print "  Time:             %s" % m.group("time")
        print "  Module name:      %s" % m.group("modname")
        print "  Message type:     %s" % m.group("time")
        print "  Message:          %s" % m.group("message")
        print "  FTP site URL:     %s" % m.group("ftpsite")
        print "  Input file:       %s" % m.group("infile")
        print "  Input file path:  %s" % m.group("infilepath")
        print "  Input file name:  %s" % m.group("infilename")
        print "  Output file:      %s" % m.group("outfile")
        print "  Output file path: %s" % m.group("outfilepath")
        print "  Output file name: %s" % m.group("outfilename")
        print "\n",
f.close()

# Next pick out only the desired data.
f=open("testdata.log")
mcnt = 0
matches = []
for line in f:
    # Decompose this line into its components.
    m = decomposeLogEntry(line)
    if m:
        # See if this record meets desired requirements
        if re.search(r"ABC$|DEF$", m.group("infilepath")):
            matches.append(line)
f.close()
print "There were %d matching records" % len(matches)

rawDescription: Python regular expression to get filename in a long path


code:
import time, random
start = time.time()
time.sleep(random.randrange(20))  # random amount of time to wait
print time.time() - start

rawDescription: How can I upload/download a file with python and log the time it takes in each operation?

description: Upload/download a file with python and log the time it takes in each operation


code:
class OurAttr(object):
    def __init__(self):
        self._log = False

    @property
    def log(self):
        return self._log

    @log.setter
    def log(self, value):
        raise AttributeError("Cannot set 'log' attribute directly.")

    @log.deleter
    def log(self):
        raise AttributeError("Cannot delete 'log' attribute directly.")

    def setLog(self):
        self._log = True
        print "Log is", self._log

    def clearLog(self):
        self._log = False
        print "Log is", self._log

class OurClass(object):
    def __init__(self):
        self.OurAttr = OurAttr()

oc = OurClass()
oc.OurAttr.setLog()
oc.OurAttr.clearLog()
oc.OurAttr.log = False   # Raises exception

rawDescription: nesting of properties vs setters and getters in python

description: Nesting of properties vs setters and getters in python


code:
class Logger(object): # boilerplate code
    def log(self,level,msg,*args,**kwargs): # *args, **kwargs = flexible arguments
        self._log(level,msg,*args,**kwargs) # call with flexible argments

    def _log(self,level,msg,*args,**kwargs):
        # override me at runtime :)
        # I think Java people call this Dependency Runtime Injection
        if level>1:
            print msg,args,kwargs

logger = Logger() # boilerplate code

def logged(level): # what pattern do you call this?
    def logged_decorator(function): # and this?
        def func(*args,**kwars):
            name = func.__name__ # look ma, reflective metaprogramming!
            logger.log(level,name,*args,**kwargs)
            return func(*args,**kwargs)
        return func # boilerplate code
    return logged_decorator # boilerplate code

rawDescription: What kind of things can be done with Java but not Python?


code:
#===============================================================================
# 1. MSSQL QUERY WITH PYODBC
#===============================================================================
import pyodbc
url = 'DSN=myserver;UID=myusername;PWD=mypassword;PORT=1433;DATABASE=mydatabase;'
pyodbccon = pyodbc.connect(url); cursor = pyodbccon.cursor()

userdataQ = "SELECT users.userid, users.client, users.industry FROM users"
cursor.execute(userdataQ); userdata = cursor.fetchall()

##===============================================================================
## 2. convert tuple list to key-value dictionary
## 3. export to mongodb
##===============================================================================
from pymongo import Connection; conmongo = Connection('localhost')
db = conmongo.mypymongodb #mypymongodb = dbname
headers = ['userid','client','industry']

for tup in userdata:
    nextdoc = dict(zip(headers, tup))
    db.usercollection.save(nextdoc)
print "usercollection in mypymongodb updated with " + str(db.usercollection.count()) + " docs"

rawDescription: Issues converting python list for mongodb import


code:
for root, dirs, files in os.walk(here):
    if not dirs:
        print '%s is a leaf' % root

rawDescription: Printing final (leaf?) nodes in directory listing Python


code:
from collections import Counter

first = Counter({'id': 1, 'age': 23})
second = Counter({'id': 4, 'out': 100})

first_plus_second = first + second
print first_plus_second

rawDescription: sum value of two different dictionaries which is having same key

description: Sum value of two different dictionaries which is having same key


code:
import numpy as np
from scipy.ndimage import map_coordinates

#-- Setup ---------------------------
z = np.array([ [3.6, 6.5, 9.1, 11.5, 13.8],
               [3.9, 7.3, 10.0, 13.1, 15.9],
               [4.5, 9.2, 12.2, 14.8, 18.2] ])
ny, nx = z.shape
xmin, xmax = 1, 5
ymin, ymax = 10000, 20000

# Points we want to interpolate at
x1, y1 = 1.3, 25000
x2, y2 = 0.2, 50000
x3, y3 = 2.5, 15000

# To make our lives easier down the road, let's
# turn these into arrays of x & y coords
xi = np.array([x1, x2, x3], dtype=np.float)
yi = np.array([y1, y2, y3], dtype=np.float)

# Now, we'll set points outside the boundaries to lie along an edge
xi[xi > xmax] = xmax
xi[xi < xmin] = xmin

# To deal with the "hard" break, we'll have to treat y differently,
# so we're ust setting the min here...
yi[yi < ymin] = ymin

# We need to convert these to (float) indicies
#   (xi should range from 0 to (nx - 1), etc)
xi = (nx - 1) * (xi - xmin) / (xmax - xmin)

# Deal with the "hard" break in the y-direction
yi = (ny - 2) * (yi - ymin) / (ymax - ymin)
yi[yi > 1] = 2.0

# Now we actually interpolate
# map_coordinates does cubic interpolation by default,
# use "order=1" to preform bilinear interpolation instead...
z1, z2, z3 = map_coordinates(z, [yi, xi])

# Display the results
for X, Y, Z in zip((x1, x2, x3), (y1, y2, y3), (z1, z2, z3)):
    print X, ',', Y, '-->', Z

rawDescription: Scipy interpolation on a numpy array

description: Scipy interpolation on a numpy array


code:
class Mixin(object):
    def __getattribute__ (self, attr):
        print self, "getting", attr
        return object.__getattribute__(self, attr)
Mixin().__str__
Mixin().foobar

rawDescription: Mixin class to trace attribute requests - __attribute__ recursion


code:
x = range(0,10)
for i in x:
    print 'i = ',x[i]
    for k in x[i::-1]:
        print 'k = ', k
    print("\n")

rawDescription: Python Nested For Loop Counting Backwards


code:
from collections import defaultdict
from itertools import combinations

R = [1,3,1,6,9,6,1,2,3,0]
d = defaultdict(list)
for idx,item in enumerate(R,1):
    d[item].append(idx)

result = []
for indices in d.itervalues():
    result.extend(combinations(indices, 2))

print result

rawDescription: Algorithm to extract edge list efficiently from a vector


code:
import re

with open("data.txt") as f:
    content = re.split(' |\n', f.read())
    print "\n".join([" ".join(content[i::10]) for i in range(1, 10)])

rawDescription: Python: Create ndarray from a custom file format fast


code:
inputdata = '(1 ((st 8) (pitch 67) (dur 4) (keysig 1) (timesig 12) (fermata 0))((st 12) (pitch 67) (dur 8) (keysig 1) (timesig 12) (fermata 0)))'

from pyparsing import OneOrMore, nestedExpr

data = OneOrMore(nestedExpr()).parseString(inputdata)
print data

# [['1', [['st', '8'], ['pitch', '67'], ['dur', '4'], ['keysig', '1'], ['timesig', '12'], ['fermata', '0']], [['st', '12'], ['pitch', '67'], ['dur', '8'], ['keysig', '1'], ['timesig', '12'], ['fermata', '0']]]]

rawDescription: Parsing a lisp file with Python

description: Parsing a lisp file with Python


code:
dic_1 = {'x': [0.0005697499999999999, 0.00056825,0.00056825,0.00056825,0.00056825],'y': [-1.1e-05, 548],'z': [653,  672,150,590],'w': [653, 672,150,590]}

def tester(key,value):
    if 'e' in str(value):
        return 0
    #elif key in ('x'):     # This was what you wanted last time.
    #    return value       # I have commented it out to match output
    else:
        return (10**6) * value

newdic = {key:[tester(key,i) for i in value] for key,value in dic_1.items()}
print newdic

rawDescription: Exponential not working in Python

description: Exponential not working in Python


code:
p = re.compile(ur'(?<!Mr|Dr|Ms|Jr|Sr)[.?!]+\s(?=[A-Z0-9])')
#                                          ^^^^^        ^
test_str = u"Mr. Smith bought cheapsite.com for 1.5 million dollars i.e. he paid a lot for it. Did he mind? Adam Jones Jr. thinks he didn't. In any case, this isn't true... Well, with a probability of .9 it isn't.23 is the ish. My name is! Why wouldn't you... this is.\nAndrew"
print(p.split(test_str))

rawDescription: How do I delimit my input by this capture group?

description: Delimit my input by this capture group


code:
import ruamel.yaml as yaml

from ruamel.yaml.loader import Reader, Scanner, Composer, SafeConstructor, \
     Resolver, Parser

class TimeConstructor(SafeConstructor):
    def __init__(self):
        SafeConstructor.__init__(self)

    def construct_yaml_time(self, node):
        x = SafeConstructor.construct_yaml_timestamp(self, node)
        return x.time()

TimeConstructor.add_constructor(
    u'tag:yaml.org,2002:timestamp',
    TimeConstructor.construct_yaml_time)

class TimeLoader(Reader, Scanner, Parser, Composer, TimeConstructor, Resolver):
    def __init__(self, stream):
        Reader.__init__(self, stream)
        Scanner.__init__(self)
        Parser.__init__(self)
        Composer.__init__(self)
        TimeConstructor.__init__(self)
        Resolver.__init__(self)

yaml_str = """\
MyDateTime: 2015-08-11 10:08:37
x: 23423
"""

data = yaml.load(yaml_str, Loader=TimeLoader)
print data

rawDescription: Create datetime.time() object in yaml?

description: Create datetime.time() object in yaml


code:
print e.eval({"element": "Apple"})
print e.eval({"element": "Duck"})
print e.eval({"element": "Banana"})

rawDescription: Create python functions from expression trees


code:
def report(x):
    print x
    return True
df
df.groupby('source').filter(report)

rawDescription: pandas group filter issue

description: Pandas group filter issue


code:
common = set(s1w).intersection(s2w)
# returns set(['c'])

import difflib
common_ratio = difflib.SequenceMatcher(None, s1w, s2w).ratio()
print '%.1f%% of words common.' % (100*common_ratio)

rawDescription: Python: how many similar words in string?


rawDescription: Calculating element with most occurrences in list


code:
def tests(path):
    print not bool(path.startswith('/test'))
    print not bool(path.startswith('/api/'))
tests('/api/')
tests('/test')
tests('')  # or any other string not starting with /test or /api/

rawDescription: or in if statement - one condition of the two met

description: Or in if statement - one condition of the two met


code:
import datetime

print datetime.datetime.strptime('2010-08-27', '%Y-%m-%d')
print datetime.datetime.strptime('2010-15-27', '%Y-%m-%d')

rawDescription: match dates using python regular expressions

description: Match dates using python regular expressions


code:
from collections import defaultdict
L=['a','b','a','b']
d=defaultdict(int)
for item in L:
    d[item]+=1

print d
print d.items()

rawDescription: Most Efficient way to calculate Frequency of values in a Python list?


code:
data = [range(100)[::4], range(100)[::3], range(100)[::2], range(100)[::1]]
sets = map(set, data)
print set.intersection(*sets)

rawDescription: Take the intersection of an arbitrary number of lists in python


code: print pd.read_sql_query("SELECT * FROM stu", conn)

rawDescription: Printing a properly formatted SQLite table in Python


code:
foos = ['\x01\x02\x03\x00', 'regular string']
for f in foos:
    if ord(f[0]) is 1:
        print f

rawDescription: Checking for Hex Code Type

description: Checking for Hex Code Type


rawDescription: Convert data in String format to List - python


code:
for k in d1:
    if k not in d2:
        continue
    v1, v2 = d1[k], d2[k]
    for i in xrange(min(len(v1), len(v2))):
        if v1[i] != v2[i]:
            print k, i, v1[i]
            print k, i, v2[i]

rawDescription: Comparing Python dictionaries at identical indices


code:
s = '''PAIR 1MFK 1 URANIUM 82 HELIUM 112 3.6997
PAIR 2JGH 2 PLUTONIUM 98 POTASSIUM 88 5.3003
PAIR 345G 3 SODIUM 23 CARBON 14 1.664
PAIR 4IG5 4 LITHIUM 82 ARGON 99 2.5506'''
ll = [x.split() for x in s.split('\n')]
for row in ll:
  print ''.join(x.ljust(10) for x in row)

rawDescription: writing column entry just one below another in python

description: Writing column entry just one below another in python


rawDescription: Python Parallel Matrix Vector Multiplication


code: print "".join(uppers)

rawDescription: how can i check if a letter in a string is capialized using python?

description: Check if a letter in a string is capialized using python


code:
r = re.compile(r'([A-Z])\1{2}(?P<middle>[a-z])\1{3}')
m = r.match(gibberish)
if m is not None:
    print m.group('middle')

rawDescription: Find a lowercase letter surronded by three uppercase letters


code:
BYTES
print BYTES
abberation = BYTES.decode('utf-8')
abberation
print abberation

rawDescription: UnicodeDecodeError: 'ascii' codec can't decode


code:
import random

n=5
ran=[int(n*random.random()) for i in xrange(n)]
print ran

def selection_sort(li):
    l=li[:]
    sl=[]
    i=1
    while len(l):
        lowest=l[0]
        for x in l:
            if x<lowest:
                lowest=x
        sl.append(lowest)
        l.remove(lowest)
        print i
        i+=1
    return sl

print selection_sort(ran)

rawDescription: Sorting 5 elements with minimum element comparison


code:
if re.search(regex, testtweet) is not None:
    print "Is true"

rawDescription: Regex Python not matching, but does on regexpal


code:
A, B = [None,None, None, None, None, None, 1, 1, 3, 4], [1, 1, 1]

def replace(input_list, element, replacement):
    try:
        input_list[input_list.index(element)] = None
    except ValueError, e:
        pass
    return input_list

def replace_all(input_list, element, replacement):
    input_list[:] = [replacement if item == element else item for item in input_list]
    return input_list

print replace(A, 1, None)
print replace_all(B, 1, None)

rawDescription: Delete elements in a list and retaining the size of a list in Python?


code:
for i in result:
    print i

rawDescription: parse text using bash/python

description: Parse text using bash/python


code:
for num in range(1000, -1, -1): # count down from 1000
    if primeFinder(num):
        print "The largest prime under 1000 is:"
        print num
        break # Break out of loop after we've found largest result, as per question

rawDescription: how to use the break clause in this specific case

description: Use the break clause in this specific case


code:
from pandas import DataFrame
import numpy as np

class MyWrapper(DataFrame):
    def __init__(self, *args, **kwargs):
        super(MyWrapper,self).__init__(*args,**kwargs)

    def myapply(self,label, func):
        self[label]= super(MyWrapper,self).__getitem__(label).apply(func)

df =  frame = MyWrapper(np.random.randn(4, 3), columns=list('bde'), index=['Utah', 'Ohio', 'Texas', 'Oregon'])
print df
df.myapply('b', lambda x: x+1)
print df

rawDescription: Pandas map column in place

description: Pandas map column in place


code:
import xml.etree.ElementTree as ET

data = """<?xml version="1.0" encoding="UTF-8" ?>
<body>
    <shortName>Trigger:Digital Edge:Source</shortName>
</body>"""

tree = ET.fromstring(data)
for element in tree.findall('shortName'):
    items = element.text.split(':')
    if not items:
        continue

    element.text = items[0]
    for item in items[1:]:
        colon = ET.Element('colon')
        colon.tail = item
        element.append(colon)

print ET.tostring(tree)

rawDescription: Python: How to replace a character in a XML file with a new node?


code:
import re
import xml.etree.ElementTree as ET

data = """I will meet you at 1st.
5th... OK, 5th?
today is 2nd
Aug.3rd"""

endings = ['st', 'th', 'nd', 'rd']
pattern = re.compile('(%s)' % "|".join(endings))

root = ET.Element('root')
for line in data.split('\n'):
    items = []
    for item in re.split(pattern, line):
        if item in endings:
            items.append('%s' % item)
        else:
            items.append(item)
    element = ET.fromstring("""<Text VAlign="top" VPosition="85.00">%s</Text>""" % ''.join(items))
    root.append(element)

print ET.tostring(root)

rawDescription: Make sentences with 1st|2nd|3rd|4th to XML according to a special specification


code:
def random_letter(letters):
    while True:
        x = raw_input('Press enter for a random letter...')
        if x == 'done':
            break
        print random.choice(letters)

random_letter('abcdef')

rawDescription: Running a function in Python every time any key is pressed?


code:
def is_alternating_signs(a):
    return numpy.all(numpy.abs(numpy.diff(numpy.sign(a))) == 2)

print is_alternating_signs([1, -1, 1, -1, 1])
print is_alternating_signs([-1, 1, -1, 1, -1])
print is_alternating_signs([1, -1, 1, -1, -1])

rawDescription: Detect alternating signs

description: Detect alternating signs


rawDescription: Python - csv.DictWriter() or writerow() rounding my floats?


code:
sent = ['show', 'me', 'northwest', 'flights', 'to', 'singapore', '.']
print new_grammar.productions()[2091]
print new_grammar.productions()[-1]
new_parser = nltk.ChartParser(new_grammar)
for i in new_parser.parse(sent):
    print i

rawDescription: NLTK: can I add terminal to grammar that is already generated


code:
ewList = []
a = [{"one": "green", "two": "blue", "three": "red"},
     {"two": "blue", "four" : "green", "five" : "yellow"},
     {"two": "yellow", "six": "white", "seven" : "black"}]

for d in a:
    if "yellow" in d.values():
        ewList.append(d)

print ewList

rawDescription: iterate list of dictionary and append python

description: Iterate list of dictionary and append python


code:
for z in range(len(results)):
    sub =  df.iloc[results[z]]
    print sub['three'].mean()

rawDescription: Get statistics from subgroups in pandas

description: Get statistics from subgroups in pandas


code:
class X(object):
    def sum(self, a, b):
        print self
        return a+b
types = [type(x['name'], (object,), {'title': x['title'], 'sum': X.__dict__['sum']}) for x in d]
types[0]().sum(1,2)

rawDescription: Generating class dynamically from a list of values


code:
m = numpy.ones(1000, dtype=numpy.float32) / 1000
print repr(m.sum())

rawDescription: Python 32/64-bit machine float summing of transposed matrix not correct?


code:
import time

lastResult = 100

def checkNextID(ID, lastResult = lastResult, diff = [8,18,7,17,6,16,5,15]):
    runs = 0
    maxdiff = max(diff)
    others = [x for x in xrange(1,maxdiff) if x not in diff]
    lastothers = others[-1]
    SEEN = set()

    while True:
        if ID>lastResult:
            print ('\n=========================='
                   '\nID==%s'
                   '\n   ID>lastResult is %s : program STOPS')\
                  % (ID,ID>lastResult,)
            break
        runs += 1
        if runs % 10 == 0:  time.sleep(0.5)
        if ID in SEEN:
            print '-----------------\nID=='+str(ID)+'  already seen, not examined'
            ID += 1
        else:
            curRes = isValid(ID)
            if curRes:
                print '------------------------------------\nID=='+str(ID)+'  vaaaalid'
                while True:
                    for i in diff:
                        runs += 1
                        if runs % 10 == 0:  time.sleep(0.5)
                        curRes = isValid(ID+i)
                        SEEN.add(ID+i)
                        if curRes:
                            print '   i==%2s   ID+i==%s   valid' % (i,ID+i)
                            ID += i
                            print '--------------------------\nID==%s' % str(ID)
                            break
                        else:
                            print '   i==%2s   ID+i==%s   not valid' % (i,ID+i)
                    else:
                        for j in others:
                            if ID+j>lastResult:
                                print '\n   j==%2s   %s+%s==%s>lastResult==%s is %s' \
                                      % (j,ID,j,ID+j,lastResult,ID+j>lastResult)
                                ID += j
                                print '\n--------------------------\nnow ID==',ID
                                break
                            runs += 1
                            if runs % 10 == 0:  time.sleep(0.5)
                            curRes = isValid(ID+j)
                            SEEN.add(ID+j)
                            if curRes:
                                print '   j==%2s   ID+j==%s   valid' % (j,ID+j)
                                ID += j
                                print '--------------------------\nID=='+str(ID)
                                break
                            else:
                                print '   j==%2s   ID+j==%s   not valid' % (j,ID+j)

                        if j==lastothers:
                            ID += maxdiff + 1
                            print '   ID += %s + 1 ==> ID==%s' % (maxdiff,ID)
                            break
                        elif ID>lastResult:
                            print '   ID>lastResult==%s>%s is %s ==> WILL STOP' % (ID,lastResult,ID>lastResult)
                            break

            else:
                print '-------------------------\nID=='+str(ID)+'  not valid'
                ID += 1

def isValid(ID, valid_ones = (1,9,17,25,50,52,60,83,97,98)):
    return ID in valid_ones

checkNextID(0)

rawDescription: Python Crawler - need help with my algorithm


code:
import pandas as pd

df = pd.read_csv("color.csv")
df_gen = df.copy().groupby("Color")

for key, value in df_gen:
    print "key: {}".format(key)
    print value.rolling_corr(value["Value1"],value["Value2"], 3)

rawDescription: Calculate rolling correlation with pandas


code:
def getInt(msg, lo=None, hi=None):
    while True:
        try:
            val = int(raw_input(msg))
            if lo is None or lo <= val:
                if hi is None or val <= hi:
                    return val
        except ValueError:
            pass

class GameBoard(object):
    def __init__(self, width=4, height=4):
        self.width  = width
        self.height = height
        self.rows   = [['x']*width for _ in xrange(height)]

    def getMove(self):
        row = getInt('Please enter row (1-{0}): '.format(self.height), 1, self.height)
        col = getInt('Please enter column (1-{0}): '.format(self.width), 1, self.width)
        return row,col

    def move(self, row, col, newch='a'):
        if 1 <= row <= self.height and 1 <= col <= self.width:
            self.rows[row-1][col-1] = newch

    def __str__(self):
        return '\n'.join(' '.join(row) for row in self.rows)

def main():
    bd = GameBoard()

    row,col = bd.getMove()
    bd.move(row,col)

    print bd

if __name__=="__main__":
    main()

rawDescription: Change a specific element of a list in python?


rawDescription: PYTHON: How do I create a list of every possible letter mapping using a dictionary that stores every possible letter mapping combination?


code:
f1=  open('PUT/PUT_SELLING.csv')
file_reader = csv.reader(f1)

spyy_values = []
mins = []
maxes = []

# just saying 'for x in file_reader' is all you need to iterate through the rows
# you don't need to use file_reader.next()
# here I'm also using the enumerate() function
# which automatically returns an index for each row
for row_index, row in enumerate(file_reader):
    # get the value
    spyy_values.append( float(row[1]) )

    if row_index >= 249:
        # get the min of the last 250 values,
        # including this line
        this_min = min(spyy_values[-250:])
        mins.append(this_min)
        # get the max of the last 250 values,
        # including this line
        this_max = max(spyy_values[-250:])
        maxes.append(this_max)

print "total max:", max(maxes)
print "total min:", min(mins)
print "you have %s max values" % len(maxes)
print "you have %s min values" % len(mins)
print "here are the maxes", maxes
print "here are the mins", mins

rawDescription: Manipulating csv files in python

description: Manipulating csv files in python


code:
import subprocess

output = subprocess.check_output(['chkconfig', '--list'])

for line in output.splitlines():
    if line.startswith('httpd_'):
        print line.split()[0]

rawDescription: python parse file and put in for loop

description: Python parse file and put in for loop


code:
words = text.split()
print words.count('cat')

rawDescription: A regular expression for finding a word within a string exactly "n" times


code:
import threading
import Queue

def main():
    c1 = Queue.Queue(maxsize=0)
    c2 = Queue.Queue(maxsize=0)
    quit = Queue.Queue(maxsize=0)

    def func1():
        for i in range(10):
            c1.put(i)
        quit.put(0)

    threading.Thread(target=func1).start()

    def func2():
        for i in range(2):
            c2.put(i)

    threading.Thread(target=func2).start()

    combined = Queue.Queue(maxsize=0)

    def listen_and_forward(queue):
        while True:
            combined.put((queue, queue.get()))

    t = threading.Thread(target=listen_and_forward, args=(c1,))
    t.daemon = True
    t.start()
    t = threading.Thread(target=listen_and_forward, args=(c2,))
    t.daemon = True
    t.start()
    t = threading.Thread(target=listen_and_forward, args=(quit,))
    t.daemon = True
    t.start()

    while True:
        which, message = combined.get()
        if which is c1:
            print 'Received value from c1'
        elif which is c2:
            print 'Received value from c2'
        elif which is quit:
            print 'Received value from quit'
            return
main()

rawDescription: Python equivalent of Golang's select on channels


code:
class Digit:
    def __init__(self, modulus):
        self.modulus = modulus
        self.value = 0
    def __str__(self):
        return str(self.value)
    def __nonzero__(self):
        return bool(self.value)
    def increment(self):
        self.value += 1
        self.value %= self.modulus
        return self.value == 0

class Number:
    def __init__(self, moduli):
        self.digits = [Digit(m) for m in moduli]
    def __str__(self):
        return "".join(str(d) for d in self.digits)
    def __nonzero__(self):
        return any(d for d in self.digits)
    def increment(self):
        carryover = True
        for d in reversed(self.digits):
            if carryover:
                carryover = d.increment()

n = Number([3,2,2])
while True:
    print n
    n.increment()
    if not n:
        break

rawDescription: Recursive generator in C++

description: Recursive generator in C++


code:
monsterDamage = monster.attack()
health -= monsterDamage
print "The monster does " + str(monsterDamage) + " damage to you."

rawDescription: random.randint function isnt working the way i would like it to

description: Random.randint function isnt working the way i would like it to


code:
import subprocess, shlex
strs = subprocess.check_output(shlex.split('ip r l'))
gateway = strs.split('default via')[-1].split()[0]
ip  = strs.split('src')[-1].split()[0]
print gateway, ip
#10.64.64.64 106.205.92.100

rawDescription: Find IP and Gateway using Python Script

description: Find IP and Gateway using Python Script


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

rawDescription: Best way to remove controls from a wx panel

description: Remove controls from a wx panel


code:
names = []
numbers = []
emails = []

with open('nameOfYourSaveFile', 'r') as inFile:
    for line in inFile:
        #get rid of EOL
        line = line.rstrip()

        #random example
        names.append(line[0])
        numbers.append(line[1])
        emails.append(line[2])

        #Or another approach if we want to simply print each token on a newline
        for word in line:
            print word

rawDescription: Python- Saving Results to a File and Recalling Them


code:
import numpy as np
sums=np.array([50,51,52,53,54,55,56,57,58,59,60,61])
print sums
print sums % 10
print 10 - sums % 10  # all results right except for 10s which should be zeros
print 10 - (sums-1) % 10   -1

rawDescription: Check a variable is a multiple of 10

description: Check a variable is a multiple of 10


code:
from collections import defaultdict
lis1 = []
lis2 = []

with open("abc") as f:
    dic = defaultdict(list)
    for line in f:
        spl =line.split()
        dic[spl[0]].append(spl[1:])

for key,val in dic.items():
    if len(val) == 1:
        lis1.append(key)
    else:
        lis2.append(key)
print lis1
print lis2

print dic["Meni32_2"]  #access columns related to any key from the the dict

rawDescription: compare file line by line python

description: Compare file line by line python


code:
TeamAScore = 0
TeamBScore = 0

TeamAWins = 0
TeamBWins = 0
NoWins = 0

for i in range(100):
    LuckA = random.randrange(0,25)
    LuckB = random.randrange(0,25)

    ScoreA = (PowerRankA + WinPercA + LuckA) / 3
    ScoreB = (PowerRankB + WinPercB + LuckB + HomeBonus) / 3

    TeamAScore += ScoreA
    TeamBScore += ScoreB

    if ScoreA > ScoreB:
        TeamAWins += 1
    elif ScoreA < ScoreB:
        TeamBWins += 1
    else:
        NoWins += 1

TeamAScore /= 100.0
TeamBScore /= 100.0

print "Avg. Score for TeamA:", TeamAScore, " number of Wins:", TeamAWin
print "Avg. Score for TeamB:", TeamBScore, " number of Wins:", TeamBWin
print "No Wins:", NoWins

rawDescription: Python loop while collecting data

description: Python loop while collecting data


rawDescription: How to find set of points in x,y grid using KDTree.query_ball_tree

description: Find set of points in x,y grid using KDTree.query_ball_tree


code:
def make_printer(msg):
    def printer():
        print msg
    return printer

printer = make_printer('Foo!')
printer()

rawDescription: Why aren't python nested functions called closures?


code:
import numpy as np

def log_bounded(x, verylow=-10**10):
    y = np.log(x)
    y[x <= 0] = verylow
    return y

print log_bounded(np.arange(-2, 3))

rawDescription: Define boudned ufunc for log

description: Define boudned ufunc for log


code:
import numpy
import mahotas.polygon

def run(n, reverse=0):
    canvas = numpy.zeros((n, n), dtype=int)
    lim = n-2
    print '\n%d x %d, lim=%d reverse=%d' % (n, n, lim, reverse)
    pts = [(1, 1), (1, lim), (lim, lim), (lim, 1), (1, 1)]
    if reverse:
        pts.reverse()
    mahotas.polygon.fill_polygon(pts, canvas)
    return canvas

for rev in (0, 1):
    for n in range(3, 14):
        print run(n, rev)

rawDescription: Drawing polygons in numpy arrays

description: Drawing polygons in numpy arrays


code:
import numpy
print numpy.float32(1.0 + 1e-7)
print "%.10f" % numpy.float32(1.0 + 1e-7)
print "%.10f" % numpy.float32(1.0 + 1e-8)

rawDescription: numpy float32 truncating decimal

description: Numpy float32 truncating decimal


code:
data = """cookie_id     interaction
1234          did_something
1234          viewed_banner*
1234          did_something
1234          did_something
1234          viewed_and_clicked_banner*
"""

import pandas as pd
import io

df = pd.read_csv(io.BytesIO(data), delim_whitespace=True)
flag = df.interaction.str.endswith("*")
group_flag = flag.astype(float).mask(~flag).ffill(limit=1).fillna(0).cumsum()
df["interaction"] = df.interaction.str.rstrip("*")
interest_df = df[flag]

def f(s):
    return s.value_counts()

df2 = df.groupby(group_flag).interaction.apply(f).unstack().fillna(0).cumsum()
result = df2[::2].reset_index(drop=True)
result["clicked"] = interest_df.interaction.str.contains("clicked").reset_index(drop=True)
print result

rawDescription: Aggregating overlapping "all-previous-events" features from time series data - in Python


code:
from datetime import datetime
def date():
    dates = []
    user_input =''
    while user_input != 'end':
        user_input = raw_input("Please put folder with format yy.mm.dd or 'end': ")
        try:
            dte = datetime.strptime(user_input,"%y.%m.%d")
            dates.append(dte)
        except ValueError:
            print "Use yy.mm.dd format or write 'end'"
    print 'Dates:'
    return dates

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

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


code: serial_result = map(lambda (x,y):(x**2,y**3), product(range(20), range(10)))

rawDescription: Is it possible to include mutiple equations in a lambda function


code:
My_Table = {
    'Table1' : {'Results': [["pineapple", 5], ["cherry", 7], ["kumquat", None], ["plum", None]]},
    'Table2' : {'Results': [["Orange", 5], ["Apple", 7], ["Pear", None], ["cantelope", None]]},
}
for tbl, values in My_Table.viewitems():
     values['Results'][:] = [x for x in values['Results'] if x[1] is not None]
     print values['Results']

rawDescription: Removing an list from a list of list

description: Removing an list from a list of list


code:
import atexit
import sys
import time

def cleanup():
    timeout_sec = 5
    for p in all_processes: # list of your processes
        p_sec = 0
        for second in range(timeout_sec):
            if p.poll() == None:
                time.sleep(1)
                p_sec += 1
        if p_sec >= timeout_sec:
            p.kill() # supported from python 2.6
    print 'cleaned up!'
atexit.register(cleanup)
sys.exit()

rawDescription: Ensuring subprocesses are dead on exiting Python program


code:
for i in range(0,30):
    while i: # while 0 == while False, so it just skips to next value
        print i
        break

rawDescription: Range(0,30) does not start with 0 if using while . Python


code:
str = r'''
<select name="somelist">
   <option value="234234234239393">Some Text</option>
   <option value="42">Other text</option>
</select>
'''

soup = BeautifulSoup(str)
select_node = soup.findAll('select', attrs={'name': 'somelist'})

if select_node:
    for option in select_node[0].findAll('option'):
        print option

rawDescription: name and value lookup collection, loaded from a dropdown list using beautifulsoup

description: Name and value lookup collection, loaded from a dropdown list using beautifulsoup


code:
alist = ['a', 'man', 'ran', 'at', 'a', 'catamaran', 'boat']
result = set()
for word1 in alist:
    if word1 in result:
        continue # shortcut for performance reasons only
    to_remove = []
    for word2 in result:
        if word1 in word2:
            break
        if word2 in word1:
            to_remove.append(word2)
    else:
        result.add(word1)
    for word in to_remove:
        result.remove(word)
print result

rawDescription: How to obtain a list of strings such that it represents all the strings in a given list?

description: Obtain a list of strings such that it represents all the strings in a given list


code:
words = {X[i:i+kmer] for i in range(len(X)-kmer+1)}
for i in range(len(Y)-kmer+1):
    if Y[i:i+kmer] in words:
        print Y[i:i+kmer]

rawDescription: An efficient way to search similar words (with specified length) in two strings using python


code:
import requests
from bs4 import BeautifulSoup

# Configure this to be your first request URL
r = requests.get("http://www.mywebsite.com/search/")
soup = BeautifulSoup(r.content)

for a_tag in soup.find_all('a', class_='listing-name', href=True):
    print 'href: ', a_tag['href']

# Configure this to the root of the above website, e.g. 'http://www.mywebsite.com'
base_url = "http://www.mywebsite.com"

for a_tag in soup.find_all('a', class_='listing-name', href=True):
    print '-' * 60      # Add a line of dashes
    print 'href: ', a_tag['href']
    request_href = requests.get(base_url + a_tag['href'])
    print request_href.content

rawDescription: Python and BeautifulSoup Opening pages

description: Python and BeautifulSoup Opening pages


code:
import functools
def trace(f):
    '''This decorator shows how the function was called'''
    @functools.wraps(f)
    def wrapper(*arg,**kw):
        arg_str=','.join(['%r'%a for a in arg]+['%s=%s'%(key,kw[key]) for key in kw])
        print "%s(%s)" % (f.__name__, arg_str)
        return f(*arg, **kw)
    return wrapper

@trace
def foo(*args):
    pass

for n in range(3):
    foo(n)

rawDescription: In Python, what are some examples of when decorators greatly simplify a task?


code:
if number % 2:
    print "Odd!"
else:
    print "Even!"

rawDescription: Boolean evaluation in a lambda

description: Boolean evaluation in a lambda


code:
try:
    re
except NameError, e:
    print "name 're' is not yet defined in the module's namespace"
    print "module namespace : %s" % globals()

import re
print "name 're' is now defined in the module's namespace"
print "module namespace : %s" % globals()

def foo():
    try:
        os
    except NameError, e:
        print "name 'os' is not yet defined in the function's namespace"
        print "function namespace : %s" % locals()
        print "name 'os' is not defined in the module's namespace neither"
        print "module namespace : %s" % globals()

    import os
    print "name 'os' is now defined in the function's namespace"
    print "function namespace : %s" % locals()
    print "name 'os' is still not defined in the module's namespace"
    print "module namespace : %s" % globals()

foo()

print "After calling foo(), name 'os' is still not defined in the module's namespace"
print "module namespace : %s" % globals()

class Foo(object):
    try:
        os
    except NameError, e:
        print "name 'os' is not yet defined in the class namespace"
        print "but we cannot inspect this namespace now so you have to take me on words"
        print "but if you read the code you'll notice we can only get there if we have a NameError, so we have an indirect proof at least ;)"
        print "name 'os' is not defined in the module's namespace neither obvisouly"
        print "module namespace : %s" % globals()

    import os
    print "name 'os' is now defined in the class namespace"
    print "we still cannot inspect this namespace now but wait..."
    print "name 'os' is still not defined in the module's namespace neither"
    print "module namespace : %s" % globals()

print "class namespace is now accessible via Foo.__dict__"
print "Foo.__dict__ is %s" % (Foo.__dict__)
print "'os' is now an attribute of Foo - Foo.os = %s" % Foo.os
print "name 'os' is still not defined in the module's namespace"
print "module namespace : %s" % globals()

rawDescription: Where to import?

description: Where to import


code:
# generation code - for cut and paste

import operator
def fixtures(teams):
    if len(teams) % 2:
        teams.append('Day off')  # if team number is odd - use 'day off' as fake team

    rotation = list(teams)       # copy the list

    fixtures = []
    for i in range(0, len(teams)-1):
        fixtures.append(rotation)
        rotation = [rotation[0]] + [rotation[-1]] + rotation[1:-1]

    return fixtures

# demo code
teams = ["Team1", "Team2", "Team3", "Team4", "Team5"]

# for one match each - use this block only
matches = fixtures(teams)
for f in matches:
    print f

# if you want return matches
reverse_teams =  [list(x) for x in zip(teams[1::2], teams[::2])]
reverse_teams = reduce(operator.add,  reverse_teams)    # swap team1 with team2, and so on ....

#then run the fixtures again
matches = fixtures(reverse_teams)

print "return matches"
for f in matches:
    print f

rawDescription: League fixture generator in python

description: League fixture generator in python


code:
from numpy.lib.stride_tricks import as_strided as ast

def block_view(A, block= (3, 3)):
    """Provide a 2D block view to 2D array. No error checking made.
    Therefore meaningful (as implemented) only for blocks strictly
    compatible with the shape of A."""
    # simple shape and strides computations may seem at first strange
    # unless one is able to recognize the 'tuple additions' involved ;-)
    shape= (A.shape[0]/ block[0], A.shape[1]/ block[1])+ block
    strides= (block[0]* A.strides[0], block[1]* A.strides[1])+ A.strides
    return ast(A, shape= shape, strides= strides)

if __name__ == '__main__':
    from numpy import arange
    A= arange(144).reshape(12, 12)
    print block_view(A)[0, 0]
    #[[ 0  1  2]
    # [12 13 14]
    # [24 25 26]]
    print block_view(A, (2, 6))[0, 0]
    #[[ 0  1  2  3  4  5]
    # [12 13 14 15 16 17]]
    print block_view(A, (3, 12))[0, 0]
    #[[ 0  1  2  3  4  5  6  7  8  9 10 11]
    # [12 13 14 15 16 17 18 19 20 21 22 23]
    # [24 25 26 27 28 29 30 31 32 33 34 35]]

rawDescription: How can I efficiently process a numpy array in blocks similar to Matlab's blkproc (blockproc) function

description: Efficiently process a numpy array in blocks similar to Matlab's blkproc (blockproc) function


code:
import sys
from PyQt4.QtGui import QPixmap, QApplication, QColor
app = QApplication(sys.argv)

# img is QImage type
img = QPixmap.grabWindow(
        QApplication.desktop().winId(),
        x=00,
        y=100,
        height=20,
        width=20,
        ).toImage()

for x in range(0,20):
    for y in range(0,20):
        c = img.pixel(x,y)
        colors = QColor(c).getRgbF()
        print "(%s,%s) = %s" % (x, y, colors)

rawDescription: How to get RGB values of QPixmap or QImage pixel - Qt, PyQt

description: Get RGB values of QPixmap or QImage pixel - Qt, PyQt


code:
d={}
f=open("file")
for line in f:
    a,b=map( int, line.split() )
    d[a]=b
f.close()
print d

rawDescription: How to create a dictionary of integer pairs from a file in python

description: Create a dictionary of integer pairs from a file in python


code: print ' '.join([s.rstrip('\r').rsplit('<br />')[0].rsplit('°C')[0] for s in section_heading])

rawDescription: Removing part of a string that has been obtained by a regular expression using strip functions


code:
print "\bhello\b"
print r"\bhello\b"
len("\bhello\b"), len(r"\bhello\b")

rawDescription: Regular Expressions working differently in Python and Ruby


code:
origFields = (u'NLCDV1', u'NLCDV2', u'NLCDV3')
addedFields = ('KM2_LC1', 'KM2_LC2', 'KM2_LC3')
calcs = ('!NLCDV1! * 900.0', '!NLCDV2! * 900.0', '!NLCDV3! * 900.0')

for orig, added, calc in zip(origFields, addedFields, calcs):
    print '{!r}, {!r}, {!r}'.format(orig, added, calc)

rawDescription: Python for-loop with if-statements

description: Python for-loop with if-statements


code: [{'startDate': 123L, 'campaignCfid': 789L, 'endDate': 456L, 'budgetImpressions': 60L, 'campaignName': 'abc'}, {'startDate': 456L, 'campaignCfid': 123L, 'endDate': 789L, 'budgetImpressions': 80L, 'campaignName': 'def'}]

rawDescription: Group By & Aggregate List of Dictionaries in Python


code:
od[1]
od[3]
for k, v in od.iteritems(): print k, v

rawDescription: How can I sort a dictionary by key?

description: Sort a dictionary by key


code:
def userSelection():
    userSelection = raw_input('New \nSaved \nRemove \nAlerts')
    userSel = userSelection.lower()
    if userSel == 'new':
        getDesc()
    elif userSel == 'saved':
        savedBills()
    elif userSel == 'remove':
        removeBills()
    elif userSel == 'alerts':
        setAlerts()
    else:
        print 'Please enter a valid command'
        userSelection()

def getDesc():
    desc = raw_input('\nHow do you want to describe this bill?\n')

def getAmnt():
    try:
        billAmnt = raw_input('How much is this bill?')
        amnt = float(billAmnt.strip('$'))
        return amnt
    except ValueError:
        print 'Please enter an amount that is at least 0'
        getAmnt()

def getDate():
    try:
        strDate = raw_input('What date of the month is this bill due?\nExample: 3rd\n')
        date = strDate.strip('abcdefghijklmnopqrstuvqxyz')
        numDate = int(date)
        print numDate
    except ValueError:
        print 'Please enter the day of the month, for example. 3rd or 17th.'

def getFreqy():
    strFreqy = raw_input('How often do you have to pay this bill? \nMonthly, Weekly, or Once\n')
    freqy = strFreqy.lower()
    if freqy == 'monthly':
        print 'monthly'
    elif freqy == 'weekly':
        print 'weekly'
    elif freqy == 'once':
        print 'once'
    else:
        print 'Please enter either monthly, weekly, or once.'
        getFreqy()

def savedBills():
    print 'Here is a list of all of your bills.'
    # retrieve bills from database

def removeBills():
    print 'You wish to remove a bill from your list, very well, let\'s get started.',
    print 'Here\'s a list of all of your bills.'
    # retrieve a numbered list of all bills
    billNum = raw_input('What number bill would you like to remove?')
    if int.billNum:
        print 'We will remove that bill from your list.'
        waiting()
    else:
        print 'Please type in a corresponding number.'

def setAlerts():
    # pulls the current bill alert from database
    print 'Your bill alerts are currently set to %s.' % currentAlert
    change = raw_input("Would you like to change this?")
    if change == 'yes':
        # change bill alert in database
        pass
    elif change == 'no':
        print 'Your bill alerts will continue as scheduled'
    else:
        print 'Please enter either yes or no.'

rawDescription: Having trouble importing functions in python from my directory


code:
import datetime
print datetime.datetime.now()
print datetime.datetime.now() - datetime.timedelta(minutes=2)
print datetime.datetime.now() - datetime.timedelta(seconds=10)
print datetime.datetime.now() - datetime.timedelta(milliseconds=400)

rawDescription: python time subtraction

description: Python time subtraction


code:
import xmlrpclib

s = xmlrpclib.ServerProxy('http://localhost:8000')
print s.myfunction(2, 4)

rawDescription: convention to represent the exit status and actual result in XMLRPC

description: Convention to represent the exit status and actual result in XMLRPC


code: print hash('hello') % 8   # 5

rawDescription: Iterating over dictionary returns keys in sorted order


code: print [(each, i) for i, each in enumerate(calendar.month_name[1:])]

rawDescription: list comprehension for multiple variables generate month name and number

description: List comprehension for multiple variables generate month name and number


code:
import geopy
import geopy.distance
import scipy.optimize

def north(startpoint, distance_km):
    """Return target function whose argument is a positive latitude
    change (in degrees) relative to `startpoint`, and that has a root
    for a latitude offset that corresponds to a point that is
    `distance_km` kilometers away from the start point.
    """
    def target(latitude_positive_offset):
        return geopy.distance.distance(
            startpoint, geopy.Point(
                latitude=startpoint.latitude + latitude_positive_offset,
                longitude=startpoint.longitude)
            ).km - distance_km
    return target

start = geopy.Point(48.853, 2.349)
print "Start: %s" % start

# Find the root of the target function, vary the positve latitude offset between
# 0 and 2 degrees (which is for sure enough for finding a 1 km distance, but must
# be adjusted for larger distances).
latitude_positive_offset = scipy.optimize.bisect(north(start, 1),  0, 2)

# Build Point object for identified point in space.
end = geopy.Point(
    latitude=start.latitude + latitude_positive_offset,
    longitude=start.longitude
    )

print "1 km north: %s" % end

# Make the control.
print "Control distance between both points: %.4f km." % (
     geopy.distance.distance(start, end).km)

rawDescription: Calculate point based on distance and direction


code:
def replace_string():
    found = False
    for line in fileinput.input(filename, inplace=1): # <-
        if re.search("CVS Header", line):
            #print line

        ####################################################################################
        #  Below logic:                                                                    #
        #  if length of revision number is 4 characters (e.g. 1.15) then increment by 0.01 #
        #  else if it is 3 characters (e.g. 1.5) then increment by 0.1                     #
        ####################################################################################

            if len(line.split("$Revision: ")[1].split()[0]) == 4:
                new_line = str.replace(line, line.split("$Revision: ")[1].split()[0], str(float(line.split("$Revision: ")[1].split()[0]) + 0.01))
            elif len(line.split("$Revision: ")[1].split()[0]) == 3:
                new_line = str.replace(line, line.split("$Revision: ")[1].split()[0],     str(float(line.split("$Revision: ")[1].split()[0]) + 0.1))
            ###
            ###
            newer_line = str.replace(new_line, line.split("$Author: ")[1].split()[0], username)
            newest_line = str.replace(newer_line, line.split("$Date: ")[1].split()[0], today)
            current_line = str.replace(newest_line, line.split("$Date: ")[1].split()[1], time)
            print current_line[:-1] # <-
            found = True
        else:
            print line[:-1] # <- keep original line otherwise
    fileinput.close()  # <-
    if not found:
        print "No CVS Header exists in %s" % filename

rawDescription: Python Replace String in File in With clause


code:
with open('file.txt') as f:
    for line in f:
        print ",".join(sorted(line.rstrip().split(',')))

rawDescription: Howto sort alphanumerical values (a1,a2,a3,...) in a CSV line after line with Python


code: class_<A>("A", init<>()[ incref_return_value_policy<>() ] );

rawDescription: Incrementing the reference count on an object created by a constructor exposed to boost::python


code:
import csv, collections

with open("IPconnections.csv") as f:
    r = csv.reader(f, skipinitialspace=True)    # regular reader, strip spaces
    next(r)                                     # skip header row
    ips = set()                                 # holds unique IPs
    transferred = collections.defaultdict(int)  # holds sums of transf. bytes
    for src, tgt, transf in r:
        ips.add(src)                            # add to unique IPs
        ips.add(tgt)
        transferred[(src, tgt)] += int(transf)  # add to transferred
    print ips
    print transferred

rawDescription: How to print the number of unique values in a dictionary colomn and add values in a column

description: Print the number of unique values in a dictionary colomn and add values in a column


code:
import threading

class PrimeNumber(threading.Thread):
    def __init__(self, number):
        threading.Thread.__init__(self)
        self.Number = number

    def run(self):
        counter = 2
        while counter*counter < self.Number:
            if self.Number % counter == 0:
                print "%d is not a prime number, because %d = %d * %d" % ( self.Number,      self.Number, counter, self.Number / counter)
                return
            counter += 1
        print "%d is a prime number" % self.Number

threads = []
while True:
    input1 = long(raw_input("Enter first number: "))
    if input1 > 1:
        break

thread1 = PrimeNumber(input1)
threads += [thread1]

while True:
    input2 = long(raw_input("Enter second number: "))
    if input2 > 1:
        break

thread2 = PrimeNumber(input2)
threads += [thread2]

for x in threads:
    x.start()
    x.join()

rawDescription: I have a program to check if five numbers are prime but it is not working correctly


rawDescription: Whats the easiest way to pad an alphabet index both sides with zeroes?


code:
#!/usr/bin/env python2
"""Dummy controller."""
import json
import sys

for line in iter(sys.stdin.readline, b''):
    request = json.loads(line)
    # square data field in the request and send it back
    print json.dumps(dict(request, data=int(request['data'])**2))

rawDescription: Reading and writing from a single serial port simultaneously from multiple threads


code:
'''
Created on Dec 9, 2012

@author: Daniel Ng
'''

import urllib

def fetch_structure(structureid, filetype='pdb'):
  download_url = 'http://www.rcsb.org/pdb/download/downloadFile.do?fileFormat=%s&compression=NO&structureId=%s'
  filetypes = ['pdb','cif','xml']
  if (filetype not in filetypes):
    print "Invalid filetype...", filetype
  else:
    try:
      urllib.urlretrieve(download_url % (filetype,structureid), '%s.%s' % (structureid,filetype))
    except Exception, e:
      print "Download failed...", e
    else:
      print "Saved to", '%s.%s' % (structureid,filetype)

if __name__ == "__main__":
  fetch_structure('1AW0')
  fetch_structure('1AW0', filetype='xml')
  fetch_structure('1AW0', filetype='png')

rawDescription: how do i write a program to click a particular link in Python

description: Write a program to click a particular link in Python


code:
import csv
from itertools import izip_longest

# Creating a CSV file
with open(r'Data.csv','wb') as f:
    fw = csv.writer(f)
    fw.writerows( (('Heading 1', 'Heading 2'),
                   ('1'),
                   ('1'),
                   ('0'),
                   ('2'),
                   ('1'),
                   ('0'))   )

print "The CSV file at start, read by a csv.reader :\n"
with open(r'Data.csv','rb') as f:
    fr = csv.reader(f)
    print '\n'.join(map(repr,fr))

print '\n------------------------------------------'
newdata = [10,0,10,20,10,20,10,10]

with open(r'Data.csv','rb') as f:
    fr = csv.reader(f)
    newrows = [fr.next()]
    newrows += (a+[b] for a,b in izip_longest(fr, newdata,
                                              fillvalue=[0]))

print 'newrows\n',newrows

with open(r'Data.csv', 'wb') as f:
     csv.writer(f).writerows(newrows)
print '------------------------------------------\n'

print "The new CSV file created, read by a csv.reader :\n"
with open(r'Data.csv','rb') as f:
    fr = csv.reader(f)
    print '\n'.join(map(repr,fr))

rawDescription: Python inserting list of integers into CSV column after other data


code:
for fruit in (elem for elem in array
              if not elem.startswith(('a','e','i','o','u'))):
    print fruit

rawDescription: Can I add a condition to a for loop?

description: Add a condition to a for loop


rawDescription: How to Speed up with Bulk Insert to MS Server from Python with Pyodbc from CSV


code:
text = """
When in the course of human Events, it becomes necessary for one People to dissolve the Political Bands which have connected them with another, and to assume among the Powers of the Earth, the separate and equal Station to which the Laws of Nature and of Nature?s God entitle them, a decent Respect to the Opinions of Mankind requires that they should declare the causes which impel them to the Separation.

We hold these Truths to be self-evident, that all Men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty, and the pursuit of Happiness?-That to secure these Rights, Governments are instituted among Men, deriving their just Powers from the Consent of the Governed, that whenever any Form of Government becomes destructive of these Ends, it is the Right of the People to alter or abolish it, and to institute a new Government, laying its Foundation on such Principles, and organizing its Powers in such Form, as to them shall seem most likely to effect their Safety and Happiness. Prudence, indeed, will dictate that Governments long established should not be changed for light and transient Causes; and accordingly all Experience hath shewn, that Mankind are more disposed to suffer, while Evils are sufferable, than to right themselves by abolishing the Forms to which they are accustomed. But when a long Train of Abuses and Usurpations, pursuing invariably the same Object, evinces a Design to reduce them under absolute Despotism, it is their Right, it is their Duty, to throw off such Government, and to provide new Guards for their future Security. Such has been the patient Sufferance of these Colonies; and such is now the Necessity which constrains them to alter their former Systems of Government. The History of the Present King of Great-Britain is a History of repeated Injuries and Usurpations, all having in direct Object the Establishment of an absolute Tyranny over these States. To prove this, let Facts be submitted to a candid World.
"""

words = text.split()
subs = []
n = 4
for i in range(0, len(words), n):
    subs.append(" ".join(words[i:i+n]))
print subs[:10]

rawDescription: Split a large string into multiple substrings containing 'n' number of words via python


code:
def double_quote(word):
    double_q = '"' # double quote
    return double_q + word + double_q
print double_quote(word), double_quote(word2)

rawDescription: printing double quotes around a variable

description: Printing double quotes around a variable


code:
re1 = re.compile('''^(D.*?)\d''', flags)
print re.findall(re1, txt)

rawDescription: Python Regex matching choice between "\*" (literal Asterisk char) OR "\s" (Space)


code:
user_to_count_map = {}
for username in list_of_usernames:
    x = user_to_count_map.get(username, 0)
    x += 1
    user_to_count_map[x] = x
for username, counter in sorted(user_to_count_map.items()):
    print username, counter[0]

rawDescription: Using setdefault, but not wanting to reassign back to map/dict


code:
from Tkinter import *

root = Tk()
root.title("TkApp")

var = StringVar()

def varWriter(*args):
    print var.get()
    print type(var.get())
    print int(var.get())
    print type(int(var.get()))
    print float(var.get())
    print type(float(var.get()))
var.trace("w", varWriter)

label = Label(root, textvariable=var)
label.pack()

text_entry = Entry(root, textvariable=var)
text_entry.pack()

root.mainloop()

rawDescription: how to make an entry into a string, and then into an input so I can put 4 entries into a formula?

description: Make an entry into a string, and then into an input so I can put 4 entries into a formula


rawDescription: Processing XML file with networkx in python