code:
from hashlib import md5

from werkzeug import check_password_hash, generate_password_hash

from CodernityDB.database_thread_safe import ThreadSafeDatabase
from CodernityDB.database import RecordNotFound

#indexTypes

from CodernityDB.hash_index import HashIndex
from CodernityDB.tree_index import TreeBasedIndex

def get_user_id(username):
    """Convenience method to look up the id for a username."""
    try:
        rv = cdb.get('user', username, with_storage=True)
    except RecordNotFound:
        rv = None
    else:
        rv = rv['user_id']
    return rv

def get_id(username):
    """Convenience method to look up the id for a username."""
    try:
        rv = cdb.get('id', username, with_storage=True)
    except RecordNotFound:
        rv = None
    else:
        rv = rv['id']
    return rv

def get_username(user_id):
    """Convenience method to look up the id for a username."""
    try:
         rv = cdb.get('user', user_id, with_storage=True)
        # rv= user['doc']['pw_hash']
    except RecordNotFound:
        rv = None
    else:
        rv = rv['user_id']
    return rv

def get_allRecords(getRecord):
    """Convenience method to look up the id for a username."""
    try:
        #rv = cdb.get('username', user_id, with_storage=True)
        #cdb.all(getRecord)
        rv="\n"
        for curr in cdb.all(getRecord):
            rv = rv+str(curr)
    except RecordNotFound:
        rv = None
    else:
        rv = "all"
    return rv

# configuration
DATABASE = 'testDB'
SECRET_KEY = 'development key'
cdb = ThreadSafeDatabase(DATABASE)

#main
def main():
    if cdb.exists():
        cdb.open()
        cdb.reindex()
    else:
        #from database_indexes import UserIndex, MessageAllIndex, MessageUserIndex, FollowerRel1Index, FollowerRel2Index, UserIDIndex, FollowerIndex
        print("no database pressent")
        quit()

    print("type'n' to advance through prompts")

    useridRequest_input ="none"
    while useridRequest_input != "n":
        useridRequest_input = raw_input("get ID, search username: ")
        print(get_user_id(useridRequest_input))

    usernameRequest_input ="none"
    while usernameRequest_input != "n":
        usernameRequest_input = raw_input("get username, search ID: ")
        print(get_username(usernameRequest_input))

    print ("counting id tags")
    print cdb.count(cdb.all, 'id')
    print("counting user_id tags")
    print cdb.count(cdb.all, 'user_id')
    for curr in cdb.all('id'):
        print curr

#Run Main
if __name__ == '__main__':
    main()

rawDescription: codernitydb index with string value

description: Codernitydb index with string value


code:
dict_set = {dict1,dicta,dict666}

for name,value in locals().items():
    if value in dict_set:
        print 'the name of the dictionary is ', name
        print 'the dictionary looks like ', value

rawDescription: Get name of dictionary

description: Get name of dictionary


code:
runs = 8
gen = getNum1To4(8)
for n in range(runs): print gen.next()

rawDescription: How do I generate a sequence of integer numbers in a uniform distribution?

description: Generate a sequence of integer numbers in a uniform distribution


code:
from bs4 import BeautifulSoup

data = """

test1
test2
test3
test4

"""
soup = BeautifulSoup(data)
print soup.find_all('a', href=lambda x: not x.startswith('http'))

rawDescription: Get relative links from html page

description: Get relative links from html page


code:
number_of_Widgets = 5
cost = 66.8788
print 'Total cost for %d widgets is $ %5.2f.' % (number_of_Widgets, cost)

rawDescription: Whats does % mean in Python? Have trouble understanding


code:
li=['My name is {}', "My job is {}", "I am {}"]
info=['john','guard','single']

for x, y in zip(li, info):
    print x.format(y)

rawDescription: How do you extract words in a list in python

description: Extract words in a list in python


code:
import requests as rq
from lxml import html

url = "http://www.lun.ua/%D0%BF%D1%80%D0%BE%D0%B4%D0%B0%D0%B6%D0%B0-%D0%BA%D0%B2%D0%B0%D1%80%D1%82%D0%B8%D1%80-%D0%BA%D0%B8%D0%B5%D0%B2"
r = rq.get(url)
tree = html.fromstring(r.text)

divs = tree.xpath("//div[@class='obj-left']")

for div in divs:

    name = div.xpath("./h3/span/a/text()")[0]
    details = div.xpath(".//div[@class='obj-params-col'][1]")[0]
    room = details.xpath("./p[1]/text()[last()]")[0]
    square = details.xpath("./p[2]/text()[last()]")[0]
    floor = details.xpath("./p[3]/text()[last()]")[0]

    print name.encode("utf-8")
    print room.encode("utf-8")
    print square.encode("utf-8")
    print floor.encode("utf-8")

rawDescription: How can I use non-ASCII characters?

description: Use non-ASCII characters


code:
from nltk.util import ngrams
import collections

with open("text.txt", "rU") as f:
    sixgrams = ngrams(f.read().decode('utf8').split(), 2)

result = collections.Counter(sixgrams)
print result
with open("output.txt", "w") as f:
    for item, count in sorted(result.iteritems()):
        if count >= 2:
            text = "{} {}".format(" ".join(item).encode('utf8'), count)
            print text
            print >>f, text

rawDescription: Counting frequency of ngrams

description: Counting frequency of ngrams


rawDescription: Selecting rows by members of a list contained in a cell


code:
def Name(SN):
    while True:
        fname = raw_input('What is your first name? ')
        lname = raw_input('What is your last name? ')
        name = fname+' '+lname
        print name
        check = raw_input('Is this correct?  yes/no')
        if check.upper() in {'YES','Y'}:
            return name

rawDescription: Python27: Checking to see if user input is correct. When select Yes, returns name correctly. When select no, return is 'None'


code:
MAX_LENGTH = 100

results = []
r = ''

for w in words:
    if len(r) + len(w) + 1 > MAX_LENGTH:
        results.append(r)
        r = ''
    r += '{}{}'.format(' ' if r else '', w)

print results

rawDescription: How to split a long string based on character count

description: Split a long string based on character count


code:
class Card:
    def __init__(self, suit = 0, rank = 2):
        self.suit = suit
        self.rank = rank
    #Two class attributes that are helpful in determining suit/rank
    ranklist = ['narf', 'Ace', 'Two', 'Three', 'Four', 'Five', \
    'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King']
    suitlist = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
    def __repr__(self):
        return (self.ranklist[self.rank] + " of " + self.suitlist[self.suit])

class Deck:
    def __init__(self):
        self.cards = []
        for suit in range(4):
            for rank in range(1,13):
                x = Card()
                x.rank = rank
                x.suit = suit
                self.cards.append(x)
    def printDeck(self):
        for card in self.cards:
            print(card)

    def removeCard(self, card):
            if card in self.cards:
                self.cards.remove(card)
                return True
            else:
                return False

d = Deck()
print "The deck has {} cards.".format(len(d.cards))
card = Card(1,1)
print "Removing the {}.".format(card)
d.removeCard(card)
print "The deck has {} cards.".format(len(d.cards))

rawDescription: Card Game Simulation list.remove

description: Card Game Simulation list.remove


code:
from spyne.model.primitive import *
from spyne.model.complex import *

class GpsInfo(ComplexModel):
    UTC = DateTime
    Latitude = Double
    Longitude = Double
    DopplerTime = Double
    Quality = Unicode
    HDOP = Unicode
    Altitude = Double
    Speed = Double
    Heading = Double
    Estimated = Boolean

class Header(ComplexModel):
    Name = Unicode
    Time = DateTime
    SeqNo = Integer

class CTrailData(ComplexModel):
    index = UnsignedInteger
    gpsInfo = GpsInfo
    Header = Header

class CTrail(ComplexModel):
    LastError = AnyXml
    MaxTrial = Integer
    Trail = Array(CTrailData)

from lxml import etree
from spyne.util.xml import *

file_1 = get_xml_as_object(etree.fromstring(open('file1').read()), CTrail)
file_2 = get_xml_as_object(etree.fromstring(open('file2').read()), CTrail)
file_1.Trail.extend(file_2.Trail)
file_1.Trail.sort(key=lambda x: x.index)

elt = get_object_as_xml(file_1, no_namespace=True)
print etree.tostring(elt, pretty_print=True)

rawDescription: Python: Joining and writing (XML.etrees) trees stored in a list


rawDescription: Python: Accessing Values For Dict Key Made Using Variables


code:
def print_file(x):
    with open('/home/me/data/db/test.blast', 'r') as h:
        for line in h:
            print line

rawDescription: Retain formatting

description: Retain formatting


code:
from itertools import groupby

with open('auth.log') as myAuthlog:
    for key, group in groupby(myAuthlog, key = lambda x: x[:9]):
        print "%d attacks in hour %s"%(len(list(group)), key)

rawDescription: Python Count per hour log file

description: Python Count per hour log file


code:
from collections import defaultdict
dc = defaultdict(list)
for key, value in l:
    dc[key].append(value)
print dc
# defaultdict(<type 'list'>, {'foo': [1, 2, 3], 'bar': [2, 3, 4]})

rawDescription: Appending to dictionary value, pythonesque solution


code:
from bs4 import BeautifulSoup, Comment

data = """

cat dog sheep goat

"""

soup = BeautifulSoup(data)

div = soup.find('div', class_='foo')
for element in div(text=lambda text: isinstance(text, Comment)):
    element.extract()

print soup.prettify()

rawDescription: Beautifulsoup 4: Remove comment tag and its content


code:
root = node('grandmother')
root.children = [node('daughter'), node('son')]
root.children[0].children = [node('granddaughter'), node('grandson')]
root.children[1].children = [node('granddaughter'), node('grandson')]
root
str(root)
print root

rawDescription: Printing a Tree data structure in Python


code:
temp1 = []
for boolean in aggregate:
    temp1.append(boolean)
if len(temp1) == len(propositions):
   break
print temp1
print states[0]
if temp1 == list(states[0]):
    print 'True'
else:
    print 'False'

rawDescription: Lists are the same but not considered equal?


code:
#!/usr/bin/env python
from contextlib import closing
from selenium.webdriver import Firefox # pip install selenium

url = "http://busymovies.appspot.com/News.html?id=2965032"

# use firefox to get page with javascript generated content
with closing(Firefox()) as browser:
    browser.get(url)
    link = browser.find_element_by_link_text("Direct Link")
    print link.get_attribute("href")

rawDescription: Python - retrieve dynamic content using urllib2


code:
import numpy as np
from scipy.optimize import leastsq

T = np.arange(5,56,5)

test = np.array([[  3051.11,   2984.85,   3059.17],
                 [  3510.78,   3442.43,   3520.7 ],
                 [  4045.91,   3975.03,   4058.15],
                 [  4646.37,   4575.01,   4662.29],
                 [  5322.75,   5249.33,   5342.1 ],
                 [  6102.73,   6025.72,   6127.86],
                 [  6985.96,   6906.81,   7018.22],
                 [  7979.81,   7901.04,   8021.  ],
                 [  9107.18,   9021.98,   9156.44],
                 [ 10364.26,  10277.02,  10423.1 ],
                 [ 11776.65,  11682.76,  11843.18]])

k0 = 100.
k1 = 100.
k2 = 1.
k3 = 1.
k4 = 1.
k5 = 10.

def residual_x(vars, T, donnees):
    k0 = vars[0]
    k1 = vars[1]
    k2 = vars[2]
    k3 = vars[3]
    k4 = vars[4]
    k5 = vars[5]
    modele = T**5*k0+T**4*k1+T**3*k2+T**2*k3+T*k4+k5
    return donnees-modele

vars = [k0, k1, k2, k3, k4, k5]
coeffs=[]

for i in range(test.shape[1]):
#EDIT: previously appended the complete output of leastsq to coeffs list
#      this is wrong; leastsq returns two outputs (i.e., a tuple)
#      and we're only really interested in the coefficients
    thiscoeffs,_=leastsq(residual_x, vars, args=(T, test[:,i]),epsfcn=0.001)
    coeffs.append(thiscoeffs)

for i in range(test.shape[1]):
    print 'poly[%d] coeffs: %s' % (i,str(coeffs[i]))

# try first example with numpy.polyfit
coeffs2 = np.polyfit(T,test[:,0],5)
print 'np.polyfit for case 0: %s',str(coeffs2)

#EDIT: this added in second edit
# if you want coeffs as an array, you could convert the list like this
coeffs_array=np.asarray(coeffs)
# or you could make coeffs a numpy array in the first place

rawDescription: Python minimization leastsq with columns


code:
for i in range(len(max(signs_list)), 0, -1):
    template_string = ""
    input_string = ""
    for z in range(len(test_string_list)):
        string_part = "{" + str(z) + ":<8} "
        template_string += string_part
        input_part = "'x' if len(signs_list[" \
            + str(z) + "]) >= i else '',"
        input_string += input_part
    statement = "('" + template_string + "')"
    statement += ".format"
    statement += "(" + input_string[:-1] + ")"
    print eval(statement)

template_string = ""
input_string = ""
for z in range(len(test_string_list)):
    string_part = "{" + str(z) + ":<8} "
    template_string += string_part
    input_part = "test_list[" + str(z) + "],"
    input_string += input_part
statement = "('" + template_string + "')"
statement += ".format"
statement += "(" + input_string[:-1] + ")"
print eval(statement)

template_string = ""
input_string = ""
for z in range(len(test_string_list)):
    string_part = "{" + str(z) + ":<8} "
    template_string += string_part
    input_part = "test_string_list[" + str(z) + "],"
    input_string += input_part
statement = "('" + template_string + "')"
statement += ".format"
statement += "(" + input_string[:-1] + ")"
print eval(statement)

rawDescription: Basic Python List Excercise

description: Basic Python List Excercise


code:
for a in {1, 'abc', 3.14159}:
    print a
    print type(a)

rawDescription: python strong/weak dynamic/static type language?

description: Python strong/weak dynamic/static type language


code:
createview = CreateView('viewname', t.select().where(t.c.id>5))
engine.execute(createview)

v = Table('viewname', metadata, autoload=True)
for r in engine.execute(v.select()):
    print r

rawDescription: How to create an SQL View with SQLAlchemy?

description: Create an SQL View with SQLAlchemy


code:
# Prints: 1
print result[0]['EXISTS(SELECT 1 FROM episodes WHERE changed > ? LIMIT 1)']

rawDescription: web.py storage object can't access value

description: Web.py storage object can't access value


code:
from itertools import count,islice
a = count(1)
for item in islice(a,2,4):
    print item

rawDescription: How to print from itertools count object?

description: Print from itertools count object


code:
flist = [1.9842, 9.8713, 5.4325, 7.6855, 2.3493, 3.3333]

def listclamp(minn, maxn, nlist):
    return sorted(filter(lambda x: xminn <= x <= maxn, nlist))

print listclamp(3, 8, flist)

rawDescription: Slice List of floats by value in Python

description: Slice List of floats by value in Python


code:
text=["open","funny.","reddit.com"]
print "".join([e for e in text if e.endswith((".",".com",".net",".org",".edu",".gov"))])

rawDescription: Criteria for combining two list items

description: Criteria for combining two list items


code:
from itertools import count

def fast_fib_generator():
    F = [1, 1]
    yield 1
    yield 1
    for k in count(1):
        F.append(F[k] ** 2 + F[k - 1] ** 2)
        yield F[-1]

        F.append(F[k] * (2 * F[k + 1] - F[k]))
        yield F[-1]

for x in fast_fib_generator():
    print x

rawDescription: Fast doubling Fibonacci Python generator sequence


code:
import itertools

l = [(1, 2), (3, 4), (5, 6)]

print list(itertools.chain(*l))

rawDescription: How to collect list of tuples into one tuple in Python?

description: Collect list of tuples into one tuple in Python


rawDescription: How can i edit the json in python and save

description: Edit the json in python and save


rawDescription: Pandas loop for take row and add data back


code:
from functools import wraps

class Logged(object):
    def __init__(self, obj, obj_name):
        self.obj = obj
        self.obj_name = obj_name

    def __getattribute__(self, attr_name):
        obj = object.__getattribute__(self, 'obj')
        obj_name = object.__getattribute__(self, 'obj_name')

        attr = getattr(obj, attr_name)
        # this is not 100% generic, mathematically speaking,
        # but covers all methods and the `__class__` attribute:
        if not callable(attr) or isinstance(attr, type):
            return attr

        @wraps(attr)
        def fn(*args, **kwargs):
            print "%s called on %s with: %s and %s" % (attr_name, obj_name, args, kwargs)
            return attr(*args, **kwargs)

        return fn

    def __repr__(self):
        return repr(object.__getattribute__(self, 'obj'))

rawDescription: Generic object "watcher" proxy to wrap collection objects to log state-changes (add/del)


code:
for entry in json_data['data']['domainSummaryDTOs']:
    print entry['id']

rawDescription: decoding json in python

description: Decoding json in python


code:
import requests
from BeautifulSoup import BeautifulSoup

url = 'http://www.showmeboone.com/sheriff/JailResidents/JailResidents.asp'
response = requests.get(url)
html = response.content

soup = BeautifulSoup(html)
table = soup.find('table', attrs={'class': 'collapse shadow BCSDTable'})

i = 0
for row in table.findAll('tr'):
    print (row.prettify())
    i += 1
    print i
    if i == 6:
        break

rawDescription: Limiting BeautifulSoup output

description: Limiting BeautifulSoup output


code:
import logging
import os

os.makedirs("log")

logger = logging.getLogger('factory')
fh = logging.FileHandler('log/factory.log')
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s %(levelname)s: %(funcName)s:%(lineno)d %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)

# notice that only the warning message is written
logger.debug("DEBUG MESSAGE")
logger.warn("WARNING MESSAGE")
logger.info("INFO MESSAGE")

with open("log/factory.log", "r") as f:
    print f.read()

# now update the level on the Logger to logging.DEBUG:
logger.setLevel(logging.DEBUG)

logger.debug("DEBUG MESSAGE")
logger.warn("WARNING MESSAGE")
logger.info("INFO MESSAGE")

with open("log/factory.log", "r") as f:
    print f.read()

rawDescription: How to use python logging to log to files?

description: Use python logging to log to files


code:
class Handler(object):
    def common_function(self):
        print "Common function"

class HandlerCase1(Handler):
    def handle_stuff(self, *args, **kwargs):
        print "Handling case 1"

class HandlerCase2(Handler):
    def handle_stuff(self, *args, **kwargs):
        print "Handling case 2"

if __name__ == "__main__":
    handlers = []
    handlers.append(HandlerCase1())
    handlers.append(HandlerCase2())
    for h in handlers:
        h.handle_stuff()
        h.common_function()

rawDescription: Encapsulation vs. inheritance, help making a choice


code:
object1 = MyObject()
monitor = Monitor()
print monitor.is_changed(object1)
object1.change_somehow()
print monitor.is_changed(object1)
print monitor.is_changed(object1)

rawDescription: Python object that monitors changes in objects


code:
import pandas as pd

df = {'most_exhibitions' : pd.Series(['USA (1) Netherlands (5)' ,
'United Kingdom (2)','China (3) India (5) Pakistan (8)','USA (11) India (4)'], index=['a', 'b', 'c','d']),
              'name' : pd.Series(['Bob', 'Joe', 'Alex', 'Bill'], index=['a', 'b', 'c','d'])}

df = pd.DataFrame(df)
#cange ordering of columns
df = df[['name', 'most_exhibitions']]
print df
#   name                  most_exhibitions
#a   Bob           USA (1) Netherlands (5)
#b   Joe                United Kingdom (2)
#c  Alex  China (3) India (5) Pakistan (8)
#d  Bill                USA (11) India (4)

#remove '(' and last ')'
df['most_exhibitions'] = df['most_exhibitions'].str.replace('(', '')
df['most_exhibitions'] = df['most_exhibitions'].str.strip(')')

#http://stackoverflow.com/a/34065937/2901002
s = df['most_exhibitions'].str.split(')').apply(pd.Series, 1).stack()
s.index = s.index.droplevel(-1)
s.name = 'most_exhibitions'
print s
#a               USA 1
#a       Netherlands 5
#b    United Kingdom 2
#c             China 3
#c             India 5
#c          Pakistan 8
#d              USA 11
#d             India 4
#Name: most_exhibitions, dtype: object

df = df.drop( ['most_exhibitions'], axis=1)
df = df.join(s)
print df
#   name  most_exhibitions
#a   Bob             USA 1
#a   Bob     Netherlands 5
#b   Joe  United Kingdom 2
#c  Alex           China 3
#c  Alex           India 5
#c  Alex        Pakistan 8
#d  Bill            USA 11
#d  Bill           India 4

#exctract numbers and convert them to integer
df['numbers'] = df['most_exhibitions'].str.extract("(\d+)").astype('int')
#exctract text of most_exhibitions
df['most_exhibitions'] = df['most_exhibitions'].str.rsplit(' ', n=1).str[0]
print df
#   name most_exhibitions  numbers
#a   Bob              USA        1
#a   Bob      Netherlands        5
#b   Joe   United Kingdom        2
#c  Alex            China        3
#c  Alex            India        5
#c  Alex         Pakistan        8
#d  Bill              USA       11
#d  Bill            India        4

#pivot dataframe
df = df.pivot(index='name', columns='most_exhibitions', values='numbers')
#NaN to empty string
df = df.fillna('')

rawDescription: How do I make new columns in dataframe from a row of a different column?

description: Make new columns in dataframe from a row of a different column


rawDescription: Find term in python dictionary

description: Find term in python dictionary


code:
import xml.dom.minidom
filename='abc.xml'
doc = xml.dom.minidom.parse(filename)
print doc.toxml()

c = doc.getElementsByTagName("c")
print c[0].toxml()
c[0].childNodes[0].nodeValue = 'zip'
print doc.toxml()

def replace(tagname, newvalue):
  '''doc is global, first occurrence of tagname gets it!'''
  doc.getElementsByTagName(tagname)[0].childNodes[0].nodeValue = newvalue
replace('c', 'zit')

print doc.toxml()

rawDescription: Setting value for a node in XML document in Python


code:
#!/usr/bin/python

import time,readline,thread,sys

def noisy_thread():
    while True:
        time.sleep(3)
        sys.stdout.write('\r'+' '*(len(readline.get_line_buffer())+2)+'\r')
        print 'Interrupting text!'
        sys.stdout.write('> ' + readline.get_line_buffer())
        sys.stdout.flush()

thread.start_new_thread(noisy_thread, ())
while True:
    s = raw_input('> ')

rawDescription: Reading input from raw_input() without having the prompt overwritten by other threads in Python


code: print k[0], k[1]

rawDescription: How to remove quotes and brackets in tuple in python

description: Remove quotes and brackets in tuple in python


code:
strg="22+11-aa+bb-44-"
arr=[]
i=0
for j in range(len(strg)):
        if strg[j] in '+-':
            arr.append(strg[i:j])
            if strg[j]=='-' and strg[min(j+1,len(strg)-1)].isdigit():
                i=j
            else:
                arr.append(strg[j])
                i=j+1
print arr

rawDescription: How to split a string into a list, taking negative numbers into account?

description: Split a string into a list, taking negative numbers into account


code:
from itertools import cycle

lst = ['a', 'b', 'c']

pool = cycle(lst)

for item in pool:
    print item,

rawDescription: Circular list iterator in Python

description: Circular list iterator in Python


code:
def allOnes(n):
    return ((n+1) & n == 0) and (n!=0)

print allOnes(255)
print allOnes(158)
print allOnes(0)
print allOnes(-1)
print allOnes(-2)

rawDescription: What is the best way to check if all bits are 1?


rawDescription: Python 2to3 tool adds a vowel to my integer


code:
import silentcrawler

wrapped = silentcrawler.wrap(soup)
# just return None on failure
print wrapped.find('div', {'id':'item-pic'}).img["src"].value_

# or
def on_success(value) :
    print 'found value:', value
wrapped = silentcrawler.wrap(soup, success=on_success)
# call on_success if everything will be ok
wrapped.find('div', {'id':'item-pic'}).img["src"].value_

rawDescription: Elegant way to try/except a series of BeautifulSoup commands?


code:
from sortedcontainers import SortedSet

segments = SortedSet([0, 10])

def add_segment(start, end):
    segments.add(start)
    segments.add(end)

add_segment(1, 6)
add_segment(5, 8)

print segments
# SortedSet([0, 1, 5, 6, 8, 10])

rawDescription: How to make a partition structure

description: Make a partition structure


code:
for line in listofinfo:
    a, b = line.split("=")

    print "LHS:" + a
    print "RHS:" + b

rawDescription: Using split function to split LHS and RHS and store in each of variable accordingly


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

def tell_click_coordinates(event):
    print "X: %.0f, Y: %.0f" % (event.xdata, event.ydata)

fig = plt.figure("Ti Zr")
fig.canvas.mpl_connect("button_press_event", tell_click_coordinates)

ax = plt.subplot(111)
im = plt.imshow(np.flipud(plt.imread('14675002_in.png')),
    origin='lower',
    extent=[0, 800, 1000, 32700])
plt.xticks([10,15, 43, 95, 215,542,800])
plt.yticks([1000, 1860, 2670, 8600, 16600,32700])

plt.axis('normal')
plt.show()

rawDescription: python matplotlib changing default axis for image

description: Python matplotlib changing default axis for image


code:
import matplotlib.pyplot as plt
import seaborn as sns

def add_margin(ax,x=0.05,y=0.05):
    # This will, by default, add 5% to the x and y margins. You
    # can customise this using the x and y arguments when you call it.

    xlim = ax.get_xlim()
    ylim = ax.get_ylim()

    xmargin = (xlim[1]-xlim[0])*x
    ymargin = (ylim[1]-ylim[0])*y

    ax.set_xlim(xlim[0]-xmargin,xlim[1]+xmargin)
    ax.set_ylim(ylim[0]-ymargin,ylim[1]+ymargin)

gammas = sns.load_dataset("gammas")
s = plt.subplot(1, 1, 1)
ax = sns.tsplot(time="timepoint", value="BOLD signal",
                unit="subject", condition="ROI",
                err_style="ci_bars",
                interpolate=False,
                data=gammas)

# Check what the original limits were
x0,y0=s.get_xlim(),s.get_ylim()

# Update the limits using set_xlim and set_ylim
add_margin(s,x=0.05,y=0.01) ### Call this after tsplot

# Check the new limits
x1,y1=s.get_xlim(),s.get_ylim()

# Print the old and new limits
print x0,y0
print x1,y1

plt.show()

rawDescription: setting margins in matplotlib/seaborn with subplots

description: Setting margins in matplotlib/seaborn with subplots


code:
from flask import jsonify

@app.route('/partialupdate/<int:userid>', methods=['POST'])
def partial_update(userid):
    try:
        # fetch the user, perform the updates and commit
        return jsonify(success=1)
    except Exception, e:
        return jsonify(success=0, error_msg=str(e))

rawDescription: Flask flash and url_for with AJAX

description: Flask flash and url_for with AJAX


code:
b.py
import a

class b(a.a):
    def mymethod(self):
        a.a().method()
        print "avariable is ", a.avariable

if __name__ == '__main__':
    print a.avariable
    b().mymethod()
    b().mymethod()

rawDescription: How do I access inherited variables from another module when they are changed in parent?

description: Access inherited variables from another module when they are changed in parent


code:
def run(project_dir, main_class, specific_args):
    classpath = []

    for root, dirs, files in os.walk(project_dir):
        classpath.extend(os.path.join(root, f) for f in files if f.endswith('.jar'))
        if any(f.endswith('.properties') for f in files):
            classpath.append(root)

    classpath_augment = ':'.join(classpath)

    print java_command, classpath_augment, main_class, specific_args

rawDescription: walking directory in python to concatenate a classpath variable

description: Walking directory in python to concatenate a classpath variable


code:
import yaml

sql = ("SELECT DISTINCT p.id_product, "
                      "p.price AS price, "
                      "sp.reduction AS discount, "
                      "sp.reduction_type AS discount_type, "
                      "pl.description_short AS description "
                      "FROM ....")
app_config = dict(sql=sql)
print yaml.dump(app_config)

rawDescription: Save SQL to YAML as is

description: Save SQL to YAML as is


code:
from lxml import html
import requests

def historic_quotes(symbol, stMonth, stDate, stYear, enMonth, enDate, enYear):
    url = 'https://finance.yahoo.com/q/hp?s=%s+Historical+Prices' % symbol

    params = {
        'a': stMonth,
        'b': stDate,
        'c': stYear,
        'd': enMonth,
        'e': enDate,
        'f': enYear,
        'submit': 'submit',
    }
    response = requests.get(url, params=params)

    tree = html.document_fromstring(response.content)
    for amount in tree.xpath('//table[@class="yfnc_datamodoutline1"]//tr[td[@class="yfnc_tabledata1"]]//td[5]/text()'):
        print amount

historic_quotes('baba', '00', '11', '2010', '00', '11', '2015')

rawDescription: Python: Scrape Data from Web after Inputing Info


code: print make_triangle(13,5,2)

rawDescription: Triangle Siblings in python

description: Triangle Siblings in python


code:
import threading

def crawl():
    import urllib2
    data = urllib2.urlopen("http://www.google.com/").read()

    print "Read google.com"

threads = []

for n in range(10):
    thread = threading.Thread(target=crawl)
    thread.start()

    threads.append(thread)

# to wait until all three functions are finished

print "Waiting..."

for thread in threads:
    thread.join()

print "Complete."

rawDescription: Terminate multiple threads when any thread completes a task


code:
order = "l5,l4,l3,l2,q,y,k,k3,d1,z,p,c,b,d3,dt,N,n,a,h,v".split(',')

for key in order:
    print key, infos.get(key)#if infos is your dictionary

rawDescription: python dictionary sorting oddly (want non-alphabetical)

description: Python dictionary sorting oddly (want non-alphabetical)


code:
for row in matrix:
    print ' '.join(row)

rawDescription: How to make matrices in Python?

description: Make matrices in Python


code:
for cols in df.columns:
    df.loc[~df[cols].str.endswith(']'), cols] = df[cols] + ']'
print df

rawDescription: Pandas Conditionally Concatenate

description: Pandas Conditionally Concatenate


code:
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randint(0, 10, size=(100, 2)), columns=["type", "subtype"])
counts = pd.crosstab(df.type, df.subtype)

print counts.loc[0, [2, 3, 5, 6]].sum() + counts.loc[5, [3, 4, 7, 8]].sum()

rawDescription: Pandas OR statement ending in series contains


code:
import time
import sched
s = sched.scheduler(time.time, time.sleep)
counter = 0
def addCount():
    global counter
    counter = counter + 1

def tick():
    print time.time()
    s.enter(5, 0, addCount, ())
    s.run()
    print time.time()
    print counter

tick()

rawDescription: What does action mean for scheduled events in python?


code:
rsp = requests.get('http://google.com', stream=True)
# grab the IP while you can, before you consume the body!!!!!!!!
print rsp.raw._fp.fp._sock.getpeername()
# consume the body, which calls the read(), after that fileno is no longer available.
print rsp.content

rawDescription: How do I get the IP address from a http request using the requests library?

description: Get the IP address from a http request using the requests library


code:
def add_line_dynamic(self, machine_name, sim_port, sim_script, status):
        print machine_name, sim_port, sim_script, status
        self.list_ctrl.InsertStringItem(self.index, machine_name)
        self.list_ctrl.SetStringItem(self.index, 1, sim_port)
        self.list_ctrl.SetStringItem(self.index, 2, sim_script)
        self.list_ctrl.SetStringItem(self.index, 3, status)
        self.list_ctrl.SetStringItem(self.index, 4, "none")
        self.index += 1

rawDescription: Wxpython Listctrl

description: Wxpython Listctrl


code:
mydict={}
mydict["myitem"]=None
mydict["hashfunction"]=None
mydict["python"]=None
print mydict

rawDescription: How does the dictionary add keys in python?


code:
import time
from collections import Counter

from nltk import FreqDist
from nltk.corpus import brown
from nltk import word_tokenize

def time_uniq(maxchar):
    # Let's just take the first 10000 characters.
    words = brown.raw()[:maxchar]

    # Time to tokenize
    start = time.time()
    words = word_tokenize(words)
    print time.time() - start

    # Using collections.Counter
    start = time.time()
    x = Counter(words)
    uniq_words = x.keys()
    print time.time() - start

    # Using nltk.FreqDist
    start = time.time()
    FreqDist(words)
    uniq_words = x.keys()
    print time.time() - start

    # If you don't need frequency info, use set()
    start = time.time()
    uniq_words = set(words)
    print time.time() - start

rawDescription: How to quickly get the collection of words in a corpus (with nltk)?

description: Quickly get the collection of words in a corpus (with nltk)


code:
from gensim import corpora
docs = ["this is a foo bar", "you are a foo"]
texts = [[i for i in doc.lower().split()] for doc in docs]
print texts
dictionary = corpora.Dictionary(texts)
dictionary.save('foobar.txtdic')

rawDescription: Gensim Dictionary Implementation

description: Gensim Dictionary Implementation


code:
@mock.patch('datetime.date.today')
def test():
    datetime.date.today.return_value = date(2010, 1, 1)
    print datetime.date.today()

rawDescription: Python: Trying to mock datetime.date.today() but not working


code:
def numbers_string1(count):
    result = ""
    for i in range(count):
        result += str(i)
    return result

def numbers_string2(count):
    nums = range(count)
    num_strings = map(str, nums)
    return ",".join(num_strings)

def numbers_string3(count):
    num_strings = [str(i) for i in range(count)]
    return ",".join(num_strings)

def num_strings4(count):
    num_strings = [str(i) for i in xrange(count)]
    return ",".join(num_strings)

if __name__ == '__main__':
    import time
    cnts = (10, 100, 1000, 10000)
    funcs = (numbers_string1, numbers_string2, numbers_string3, num_strings4)
    for func in funcs:
        for cnt in cnts:
            tms = []
            for _ in xrange(5):
                begin_time = time.clock()
                func(cnt)
                tms.append(time.clock() - begin_time)
            print "%s, count = %d, min = %s , actuals = %s" % (func.__name__, cnt, min(tms), tms)

rawDescription: How to do profiling for this?

description: Do profiling for this


code:
import regex as re

def tokenize(s):
    words,glyphs = [],[]
    for part in re.split(ur'\P{Letter}+',s):
        if part:
            words.append(part)
    for part in re.split(ur'(\p{Letter}|\p{Separator})+',s)[0::2]:
        glyphs.extend(unicodeCharIterator(part))
    return words,glyphs

s=ur"piñata\U0001f631 \U0001f48bčíslo"
words,glyphs = tokenize(s)

rawDescription: regex to match graphic characters like smileys or word characters from multiple languages

description: Regex to match graphic characters like smileys or word characters from multiple languages


code:
wget = "wget  http://{IPaddress}".format(IPaddress=ipAddr)
print "The command wget is %s" %wget

os.system(wget)

rawDescription: String format function does not parse my slashes character?


code:
import Tkinter as tki # Tkinter -> tkinter in Python 3

class GUI(tki.Tk):
    def __init__(self):
        tki.Tk.__init__(self)

        # create a popup menu
        self.aMenu = tki.Menu(self, tearoff=0)
        self.aMenu.add_command(label="Undo", command=self.hello)
        self.aMenu.add_command(label="Redo", command=self.hello)

        # create a frame
        self.aFrame = tki.Frame(self, width=512, height=512)
        self.aFrame.pack()

        # attach popup to frame
        self.aFrame.bind("<Button-3>", self.popup)

    def hello(self):
        print "hello!"

    def popup(self, event):
        self.aMenu.post(event.x_root, event.y_root)

gui = GUI()
gui.mainloop()

rawDescription: Python tkinter app adding a right click context menu?


code:
import re
test = """
>word
>word2
don't replace
"""
print re.sub('(>).*', r'\1replace!', test)

rawDescription: Python: Regex to find but not include an alphanumeric


code:
li = ['sea','mountain','desert',
      'Emma','Cathy','Kate',
      'HH','VVVVVVV','AAA',
      'round','flat','sharp',
      'blueberry','banana','apple',
      'red','purple','white',
      'hen','tiger']

maxel = max(len(el) for el in li)
a,b = divmod(len(li),3)
itn = iter(li).next
form = '%%-%ds\t%%-%ds\t%%-%ds\n' % (maxel,maxel,maxel)
print ''.join(form % (itn(),itn(),itn())
              for i in xrange(a))\
      + ('%%-%ds\t%%-%ds\t\n' %(maxel,maxel) % (itn(),itn()) if b==2
         else '%%-%ds\t\n' % ma% itn() if b==1
         else '')

rawDescription: Print python list in groups of 3

description: Print python list in groups of 3


code:
class Dog(object):
    def bark(self):
        print "WOOF"

class BobyDog( Dog ):
    def bark( self ):
        super(BobyDog, self).bark()
        print "WoOoOoF!!"

BobyDog().bark()

rawDescription: Python: call parent instancemethod

description: Python: call parent instancemethod


code:
from decimal import Decimal
from operator import itemgetter
survey ='''
BREAK_L,PN1000,LA55.16469813,LN18.15054629
PN1,LA54.16469813,LN17.15054629,EL22.222
BREAK_L,PN2000,LA55.16507249,LN18.15125566
PN6,LA54.16506873,LN17.15115798,EL33.333
PN7,LA54.16507249,LN17.15125566,EL44.444
BREAK_L,PN3000,LA55.16507249,LN18.15125566
PN10,LA54.16507522,LN17.15198405,EL55.555
PN11,LA54.16506566,LN17.15139220,EL44.44
PN12,LA54.16517275,LN17.15100652,EL11.111
'''

def parse_line(line):
  brk = False
  kv = {}
  for part in line.split(','):
    if part == 'BREAK_L':
      brk = True
    else:
      k = part[:2]
      v = part[2:]
      kv[k] = v
  return (brk,kv)

def parse_survey(survey):
  ig1 = itemgetter('PN','LA','LN','EL')
  ig2 = itemgetter('PN','LA','LN')
  brk_data = None
  for line in survey.strip().splitlines():
    brk, data = parse_line(line)
    if brk:
      brk_data = data
      continue
    else:
      yield ig1(data) + ig2(brk_data)

for r in parse_survey(survey):
  print r

rawDescription: How to parse lines in file in sequence/match adequate results using python, pyparsing?

description: Parse lines in file in sequence/match adequate results using python, pyparsing


code:
import string
print list(string.lowercase)
print list(string.uppercase)
print list(string.punctuation)
print list(string.digits)

rawDescription: Strings not joining in Python

description: Strings not joining in Python


code:
L ={(1,2), (1,4), (1,3), (2,3), (3,4), (3,5), (4,5), (6,7)}
s=[set(i) for i in L if i]

def find_intersection(m_list):
    for i,v in enumerate(m_list) :
        for j,k in enumerate(m_list[i+1:],i+1):
           if v & k:
              s[i]=v.union(m_list.pop(j))
              return find_intersection(m_list)
    return m_list

print find_intersection(s)

rawDescription: Combine tuples in a list which have the same value


code:
import re
text = r'Viktor Leis* (Technische Universitt Mnchen) Alfons Kemper (Technische Universitt Mnchen) Thomas Neumann (Technische Universitt Mnchen, Germany)'
pattern = '[^\(\)]* \([^\(]+\)'
result = re.findall(pattern,s)
print result

rawDescription: Python - Split String with Parenthesis based off a Pattern


code:
string =['I', 'love','traveling','all', 'over', 'the', 'world']
word_list =['I love','traveling','all over the world']
count=0
for x in word_list:
    for y in x.split():
        if y in string:
            count+=1
            if count==len(x.split()) and (' ' in x) == True:
                print x
    count=0

rawDescription: how to test if there is any word matched the string in Python

description: Test if there is any word matched the string in Python


code:
my_list = [1,2,3]
for member in my_list:
    member = 42
print my_list

rawDescription: Python List & for-each access (Find/Replace in built-in list)


code:
import Image
import numpy

im = Image.open('Tiff.tif')

imarray = numpy.array(im)
print imarray.shape, im.size   #these agree

rawDescription: Converting an image instance (file) to an array (python)


code:
import re
s = 'LAST LAST2 First First2'
print re.search("[A-Z][a-z].*$",s).group().split()

rawDescription: How to extract non-uppercase string elements for first and last names?

description: Extract non-uppercase string elements for first and last names


code:
grid = [['a' for i in range (0,5)]  for j in range (0,3)]
print '[' + ',\n '.join(str(x) for x in grid) + ']'

rawDescription: Printing List within list in python line by line


code:
import numpy as np
import pandas as pd
start = pd.Timestamp("2014-11-01")
end = pd.Timestamp("2014-11-02")
N = 100000
t = np.random.randint(start.value, end.value, N)
t -= t % 1000000000

start = pd.to_datetime(np.array(t, dtype="datetime64[ns]"))
duration = pd.to_timedelta(np.random.randint(100, 1000, N), unit="s")
df = pd.DataFrame({"start":start, "duration":duration})
df["end"] = df.start + df.duration

print df.head(5)

rawDescription: Pandas TimeSeries With duration of event


code: print "Weekend"

rawDescription: how to find current day is weekday or weekends in Python?

description: Find current day is weekday or weekends in Python


code:
def quickSortPartition(a, l, r):
    if len(a) < 2:
        return a
    else:
        print a, l, r
        p = a[l]
        i = l + 1
        for j in range(l + 1, r+1):
            if a[j] < p:
                temp = a[i]
                a[i] = a[j]
                a[j] = temp
                i = i + 1
        temp = a[l]
        a[l] = a[i - 1]
        a[i - 1] = temp
        firstPartition = a[:i-1]
        pivot = a[i-1]
        secondPartition = a[i:]
        if len(a[:i-1]) > 1:
            a[:i-1] = quickSortPartition(a[:i-1], 0, len(a[:i-1])-1)
        if len(a[i:]) > 1:
            a[i:] = quickSortPartition(a[i:], 0, len(a[i:])-1)
        return a

lines = [3, 8, 2, 5, 1, 4, 7, 6]

# print lines
quickSorted = quickSortPartition(lines, 0, len(lines)-1)

print quickSorted

rawDescription: Mutability of Python list in a recursive algorithm


rawDescription: pandas groupby apply function that take N-column frame and returns object

description: Pandas groupby apply function that take N-column frame and returns object


code:
a = 0.3
print a
a

rawDescription: Another floating point question

description: Another floating point question


code:
# This decorator takes the class/namedtuple to convert any JSON
# data in incoming request to.
def convert_input_to(class_):
    def wrap(f):
        def decorator(*args):
            obj = class_(**request.get_json())
            return f(obj)
        return decorator
    return wrap

@app.route('/doSomething', methods=['POST'])
@convert_input_to(Project)
def do_something(project):
    print project.name
    return "myPage"

rawDescription: Automatically convert JSON to Object on Flask Request


code:
import random

class WordDict(dict):
    def __getitem__(self, key):
        vals = dict.__getitem__(self, key)
        return random.choice(vals)

words = WordDict(
    cold = ["cold", "frigid", "freezing"],
    hot = ["scathing", "burning", "hot"]
)

phrases = ['the water is {word[cold]}', 'the sun is {word[hot]}']

for x in xrange(10):
    for phrase in phrases:
        print phrase.format(word=words)

rawDescription: Python string format using dict of arrays


code:
import re

# find out spaces' positions common to all rows
spaces = sorted(set.intersection(*[
    set(m.end() for m in re.finditer(ur'\s', line))
    for line in data
]))

# split by these positions
for line in data:
    row = []
    p = 0
    for s in spaces:
        row.append(line[p:s])
        p = s
    row.append(line[p:])
    row = filter(len, map(unicode.strip, row))
    print ' | '.join(row) # or whatever you want...

rawDescription: Parsing an non-uniform but CSV-ish textfile


code:
from operator import itemgetter
array = range(1, 12)
indices = itemgetter(0, 3, 10)
print indices(array)
# (1, 4, 11)

rawDescription: How can I get the list slice using a list of indexes in Python?

description: Get the list slice using a list of indexes in Python


rawDescription: Regex help needed for splitting CSV file


code:
matrix=[[1,2,3],
    [4,5,6],
    [7,8,9]]

print matrixmult(matrix, matrix)    # that is your function...

rawDescription: matrix using python

description: Matrix using python


code:
@t.Test_method_2(5, 6, 7, 8, baz='spam', ham='eggs')
def simple_function(x, y, z, n=m):
    print 'this is a simple_function'

rawDescription: Decorating a function with a method (with arbitrary arguments)


code: print l

rawDescription: Is there a function in Python to print all the current properties and values of an object?


code:
big_array = array1 + array2 + array3
import dateutil.parser as p
print sorted(big_array,key=lambda x: p.parse(x[1]))

rawDescription: sort 2d array by date in python 3.3

description: Sort 2d array by date in python 3.3


code:
from timeit import timeit

s1="""
a = "12348546478"
[i for i,j in Counter(a).items() if j>1]

"""
s2="""
def finder(s):
    seen,yields=set(),set()
    for i in s:
      if i in seen:
         if i not in yields:
            yield i
            yields.add(i)
         else :
            yields.add(i)
      else:
          seen.add(i)

a = "12348546478"
list(finder(a))

"""

s3="""
a = "12348546478"
set(i for i in a if a.count(i)>1)
"""

print '1st: ' ,timeit(stmt=s1, number=100000,setup="from collections import Counter")
print '2nd : ',timeit(stmt=s2, number=100000)
print '3rd : ',timeit(stmt=s2, number=100000)

rawDescription: Testing whether a string has repeated characters


code:
vitaminA, calcium, vitaminD = mean()
print vitaminA
print calcium
print vitaminD

rawDescription: Accessing multiple values from a function within a function


code:
from lxml import etree
root = etree.fromstring('''<xml>
<maintag>
<content> lorem
ipsum
  dolor sit  and so on </content>
</maintag>
</xml>''')
for content in root.xpath('.//maintag/content'):
    print etree.tostring(content)

rawDescription: lxml xml parsing with html tags inside xml tags

description: Lxml xml parsing with html tags inside xml tags


code:
a = "\"['BOS']\""
a
print a

rawDescription: Multiple Quotes in String

description: Multiple Quotes in String


code:
def josephus(ls, skip):
    skip -= 1 # pop automatically skips the dead guy
    idx = skip
    while len(ls) > 1:
        print ls.pop(idx) # kill prisoner at idx
        idx = (idx + skip) % len(ls)
    print 'survivor: ', ls[0]

rawDescription: "Josephus-problm" using list in python

description: "Josephus-problm" using list in python


code:
import sys,imp

my_code = 'a = 5'
mymodule = imp.new_module('mymodule')
exec my_code in mymodule.__dict__

rawDescription: python always create a file name "c", why ? (with cs=imp.load_source('cs','',f) function)

description: Python always create a file name "c", why ? (with cs=imp.load_source('cs','',f) function)


code:
def generate_triples(limit):
    # for each number up to the limit
    # Python ranges don't include the max number
    # start from 4 because 1 and 2 are invalid
    # and 3 and 4 give the same triplet
    for i in range(4, limit + 1):
        # if i is odd
        if i % 2:
            # the two larger legs are the integers
            # above and below half of the square of the smallest leg
            # // is floor division
            big = i**2 // 2
            yield i, big, big + 1
        else:
            # the two other legs are the integers
            # above and below half of the smallest leg squared
            big = (i // 2)**2
            yield i, big - 1, big + 1

print list(generate_triples(10))
# [(3, 4, 5), (5, 12, 13), (6, 8, 10),  (7, 24, 25),
#             (8, 15, 17), (9, 40, 41), (10, 24, 26)]

rawDescription: Generating Pythagorean Triples with either leg <= some limit


code:
import os
import sys
from EchoServer_client import *
from ZSI.twisted.wsgi import (SOAPApplication,
                              soapmethod,
                              SOAPHandlerChainFactory)

class EchoService(SOAPApplication):
    factory = SOAPHandlerChainFactory
    wsdl_content = dict(name='Echo',
                        targetNamespace='urn:echo',
                        imports=(),
                        portType='',
                        )

    def __call__(self, env, start_response):
        self.env = env
        return SOAPApplication.__call__(self, env, start_response)

    @soapmethod(EchoRequest.typecode,
                EchoResponse.typecode,
                operation='Echo',
                soapaction='Echo')
    def soap_Echo(self, request, response, **kw):
        # Just return what was sent
        response.Value = request.Value
        return request, response

def main():
    from wsgiref.simple_server import make_server
    from ZSI.twisted.wsgi import WSGIApplication

    application         = WSGIApplication()
    httpd               = make_server('', 7000, application)
    application['echo'] = EchoService()
    print "listening..."
    httpd.serve_forever()

if __name__ == '__main__':
    main()

rawDescription: Are there any working examples of Zolera SOAP Infrastructure (ZSI)?


code:
config_dict = {}

for section in config.sections():
    config_dict[section] = dict(config.items(section))

print config_dict
# {'section2': {'s': 'foo'}, 'section1': {'n': '1'}}

rawDescription: Python: Parsing INI-style configuration files?


code:
def ping(ip):
    packet = IP(dst="192.168.0." + str(ip), ttl=20)/ICMP()
    reply = sr1(packet, timeout=TIMEOUT)
    if not (reply is None):
         print reply.src, "is online"
    else:
         print "Timeout waiting for %s" % packet[IP].src

rawDescription: Speed up Python script's for loop

description: Speed up Python script's for loop


code:
gen = words(f)
while 1:
    try:
        word = gen.next()
        if condition(word):
            print word
    except StopIteration:
        break # we reached the end

rawDescription: Python: read one word per line of a text file


code:
for h in [9,11,16,17]:
    @sched.scheduled_job('cron', hour=h)
    def timed_job_one():
        print h
        print ctime()

rawDescription: How to set Python apscheduler run job at 9,11,14,17,18 clock everyday

description: Set Python apscheduler run job at 9,11,14,17,18 clock everyday


code:
import tornado.ioloop
import tornado.web

print 'This is executed only once and global'
name = 'myname'

class MainHandler(tornado.web.RequestHandler):
    print 'This is executed only once'
    def get(self):
        print 'This is executed for each requests'
        self.write("Hello, world %s " % name)

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

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

rawDescription: Is a single Python executable file shared by all clients when run on a server?


rawDescription: Python breaks parsing json with characters \"


code:
#!/usr/bin/env python
import lxml.etree as etree
import sys

input_filename = sys.argv[1]
output_filename = sys.argv[2]

output_file = open(output_filename, 'w')
for line in open(input_filename, 'r').readlines():
  line = line.rstrip('\n')
  handled = False
  if line and line[0] == '<':
    try:
      xml_el = etree.fromstring(line)
      name = xml_el.find('.//NAME')
      if name is not None:
        print 'Found name:', name.text
      # Add a new XML element under HPORT, and print
      hport_el = xml_el.find('.//HPORT')
      if hport_el is not None:
        new_el = etree.SubElement(hport_el, 'NewElement')
        new_el.text = 'Content Here'
        print >>output_file, etree.tostring(xml_el)
        handled = True
    except lxml.etree.XMLSyntaxError:
      pass # this line wasn't a valid standalone XML document
  if not handled:
    print >>output_file, line

rawDescription: Reading XML from an Existing Non-XML File


code: print string

rawDescription: How to find a non-alphanumeric character and move it to the end of a string in Python

description: Find a non-alphanumeric character and move it to the end of a string in Python


rawDescription: Python: Combining two strings and selecting unique elements out of it


code:
word = word_list[0]      # first tuple
print word[0]            # first entry of the first tuple

rawDescription: Tuple indexing and printing

description: Tuple indexing and printing


code:
import pexpect
passwd = "mypass"
child = pexpect.spawn('su myuser -c "crontab -l"')
child.expect('Password:')
child.sendline(passwd)
child.expect(pexect.EOF)

print child.before

rawDescription: Using pexpect to get output of 'ls' command


code:
from lxml import html

HTML = """
Hi Dervin

This is the way to grab

Different divs with the same prefix in @class attribute
"""

data = html.fromstring(HTML)
print data.xpath('//div[contains(@class,"title_dep")]/text()')

rawDescription: Extracting divs with regular expressions


code:
import itertools
numbers = [1, 2, 3, 7, 7, 9, 10]
result = [seq for i in range(len(numbers), 0, -1) for seq in itertools.combinations(numbers, i) if sum(seq) == 10]
print result

rawDescription: Find all combinations of a list of numbers with a given sum


code:
file_size = check_file_size(OUTPUT)

if job.completed:
    print "Downloading archive"
    with open(OUTPUT, 'wb') as output_file:
        i = 0
        while True:
            response = gv.get_job_output(VAULT_NAME, job_id, (file_size + 1024 * 1024 * i, file_size + 1024 * 1024 * (i + 1)))
            output_file.write(response)
            if len(response) < 1024 * 1024:
                break
            i += 1

rawDescription: Downloading a large archive from AWS Glacier using Boto


code:
for menu in soup.select('div.menu'):
    author = menu.find('span', class_='author').get_text()
    time = menu.find('span', class_='smaller').get_text()
    data = menu.find('br').next_sibling
    print 'Author: {}\nTime: {}\nData: {}'.format(author, time, data)

rawDescription: Getting text from br tags in BeautifulSoup


code:
sorted(a_dict.items(), key=lambda (k, (v1, v2)): v2)
sorted(a_dict.items(), key=lambda item: item[1][1])    # Python 3

rawDescription: How to sort a Python dictionary by value?

description: Sort a Python dictionary by value


code:
class DefaultClassMethods(type):
    def __getattr__(cls, attr):
        def _defaultClassMethod(cls):
            print 'Hi, I am the default class method!'
        setattr(cls, attr, classmethod(_defaultClassMethod))
        return getattr(cls, attr)

rawDescription: Python dynamic class methods

description: Python dynamic class methods


code:
user
print session.query(User).filter_by(name="Edward")
print session.query(User).filter_by(name="Edward").first()
print session.query(User).filter_by(name="Ed").first()

rawDescription: SQLAlchemy: How can I create a hybrid property for the disjunction of columns?


code:
firstDict={'ERITREA': 'ER', 'LAOS': 'LA', 'PORTUGAL': 'PT', "D'IVOIRE": 'CI', 'MONTENEGRO': 'ME', 'NEW CALEDONIA': 'NC', 'SVALBARD AND JAN MAYEN': 'SJ', 'BAHAMAS': 'BS', 'TOGO': 'TG', 'CROATIA': 'HR', 'LUXEMBOURG': 'LU', 'GHANA': 'GH'}
result =({'count': 1L, 'country': 'Eritrea'}, {'count': 1L, 'country': 'Togo'}, {'count': 1L, 'country': 'Sierra Leone'}, {'count': 3L, 'country': 'Bahamas'}, {'count': 1L, 'country': 'Ghana'})
resdict = {r['country'].upper():r['count'] for r in result}
mainRes = {abbrev:resdict.get(country,0) for country, abbrev in firstDict.items()}
mainRes

rawDescription: Python:Comparing Two Dictionaries

description: Python:Comparing Two Dictionaries


code:
import re

def alpha(scanner,token):
    return token, 'a'

def numeric(scanner,token):
    return token,'rn'

def punctuation(scanner,token):
    return token, 'p'

def superscript(scanner,token):
    return token, 'sn'

def unknown(scanner,token):
    return token, 'uk'

scanner = re.Scanner([
    (r"[a-zA-Z]+", alpha),
    (r"[.,:;!?]", punctuation),
    (r"[0-9]+", numeric),
    (r"[\xb9\u2070\xb3\xb2\u2075\u2074\u2077\u2076\u2079\u2078]", superscript),
    (r"[\s\n]+", None), # whitespace, newline
    (r".", unknown)
    ])

tokens, _ = scanner.scan("This is a little test? With 7,9 and 6. \xa0-\xaf")
print tokens

rawDescription: Tokenize and label text

description: Tokenize and label text


code:
cut_weight, partitions = nx.minimum_cut(G, "s", "t")
print "cut edges capacity " + str(cut_weight)
print "Set of nodes in the 's' partition: " + str(partitions[0])
print "Set of nodes in the 't' partition: " + str(partitions[1])
edge_cut_list = [] # Computed by listing edges between the 2 partitions
for p1_node in partitions[0]:
    for p2_node in partitions[1]:
        if G.has_edge(p1_node,p2_node):
            edge_cut_list.append((p1_node,p2_node))
print "Edges of the cut: " + str(edge_cut_list)

rawDescription: Are the NetworkX minimum_cut algorithms correct with the following case?


code:
from class1 import Test

class Test2(Test):

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

if __name__ == '__main__':
    test_instance = Test2()
    test_instance.e() # prints 20 and 30

rawDescription: Class Inheritance

description: Class Inheritance


rawDescription: Numpy arctan2 of multidimensional array

description: Numpy arctan2 of multidimensional array


code:
import csv
with open('c:\us-500.csv', 'rU') as f:
    reader = csv.DictReader(f)
    address_book = {}
    for row in reader:
        address_book[row['phone1']] = row

rawDescription: converting list to dictionary in Python using zip or dict function

description: Converting list to dictionary in Python using zip or dict function


rawDescription: python array manipulation and rearranging

description: Python array manipulation and rearranging


code:
from collections import Counter
print sorted(a, key=Counter(a).get, reverse=True)
# [5, 5, 5, 5, 3, 3, 3, 4, 4, 4, 1, 1, 2]

rawDescription: Sorting a List by frequency of occurrence in a list


code:
class Tests(unittest.TestCase):
    def setUp(self):
        print "The next test will be: " + self.id()

    def test_01(self):
        pass

    def test_02(self):
        pass

if __name__ == '__main__':
    unittest.main()

rawDescription: Does setUp method from unittest.TestCase know the current test case?


code:
ner_output = [(u'Remaking', u'O'), (u'The', u'O'), (u'Republican', u'ORGANIZATION'), (u'Party', u'ORGANIZATION')]

def rechunk(ner_output):
    chunked, pos = [], ""
    for i, word_pos in enumerate(ner_output):
        word, pos = word_pos
        if pos in ['PERSON', 'ORGANIZATION', 'LOCATION'] and pos == prev_tag:
            chunked[-1]+=word_pos
        else:
            chunked.append(word_pos)
        prev_tag = pos

    clean_chunked = [tuple([" ".join(wordpos[::2]), wordpos[-1]])
                    if len(wordpos)!=2 else wordpos for wordpos in chunked]

    return clean_chunked

print rechunk(ner_output)

rawDescription: Chunking Stanford Named Entity Recognizer (NER) outputs from NLTK format


code:
class Test():
    def test_method(self):
        """I'm a docstring"""
        print "test method"

print Test.test_method.__doc__  # prints "I'm a docstring"

rawDescription: Aceess a methods docstring without instance, Python


code:
def update():
    def color(c):
        animal_menu.config(fg=c)
        root.update()
        print c
    def adapt():
        color('white')
        root.after(100, color('#6E6E6E'))
        root.after(100, color('black'))
    adapt() ##attempt no.1

rawDescription: customize tkinter menu while menu is open

description: Customize tkinter menu while menu is open


code:
cursor.execute("select CONCAT(nameid, '_', cityid, '.txt') from mydata")
present = set([r[0] for r in cursor])

for pdffile in os.listdir(filepath):
    nameid, cityid = map(int, pdffile.rsplit('.', 1)[0].split('_'))
    print nameid, cityid,
    print "Found" if pdffile in present else "Not found"

rawDescription: How to do this lookup more elegantly in Python and MySQL?

description: Do this lookup more elegantly in Python and MySQL


code:
choice = 0
while True: # while True will keep going until we break
    print("Welcome to Calculator Function!")
    n = input("How many operations do you want to do?: ")
    for _ in xrange(n): # how many times user want to use the calc
            print("Your options are:")
            print(" ")
            print("1) Addition")
            print("2) Subtraction")
            print("3) Multiplication")
            print("4) Division")
            print("5) Quit CalculatorFunction.py")
            print(" ")

            choice = input("Choose your option: ")
            if choice == 1:
                add1 = input("Add what: ")
                add2 = input("To what: ")
                print add1, "+", add2, "=", add1+add2
            elif choice == 2:
                sub2 = input("Subtract what: ")
                sub1 = input("From what: ")
                print sub1, "-", sub2, "=", sub1-sub2
            elif choice == 3:
                mult1 = input("Multiply what: ")
                mult2 = input("To what: ")
                print mult1, "*", mult2, "=", mult1*mult2
            elif choice == 4:
                div2= input("Divide what: ")
                div1= input("From what: ")
                print div1, "/", div2, "=", div1/div2
            elif choice == 5:
                loop = 0
                print"Thank you for using CalculatorFunction.py have a good day!"
            else:
                print"No selection made, please try again."
    break # end after "n" loops

rawDescription: Creating A Program That terminates After X Amount times


code:
with open("file.txt") as f:
    for line in f:
        if line.strip() == "Hello":
            print "Hello back"

rawDescription: Python Interpreting things from document


code:
np.random.seed(1)
print np.random.normal(0,10)
print np.random.normal(1,11)

16.2434536366
-5.72932055015

rawDescription: Using data from pythons pandas dataframes to sample from normal distributions


code:
class TestClass(object):

    def __init__(self, ctrl_func):
        self.a = 4
        self.ctrl_func = ctrl_func

    def do_stuff(self):
        self.ctrl_func(self)

def test_controller(self):
    self.a = 20

my_thing = TestClass(test_controller)
print my_thing.a         #this prints 4
my_thing.ctrl_func(my_thing)
print my_thing.a         #this prints 4 but I want it to print 20

rawDescription: a function that modifies its class's data objects

description: A function that modifies its class's data objects


code:
print np.allclose(y, y1)
print np.allclose(x, x1)

rawDescription: Getting completely wrong fit from python scipy.optimize.curve_fit


code:
from itertools import izip_longest
print [[i for i in element if i is not None] for element in  list(izip_longest(*input))]

rawDescription: Swap axes of a multidimensional Python list


code:
x = np.linspace(1, 5, 10000)
y = f(x, *popt)
imax = np.argmax(y)
print imax, x[imax]     # 4996 2.99859985999

rawDescription: Finding the maximum of a curve scipy

description: Finding the maximum of a curve scipy


code:
import re
p = re.compile('my regex')
print p

rawDescription: How do I access an object, knowing only its memory address and not a variable that refers to it?

description: Access an object, knowing only its memory address and not a variable that refers to it


code:
for d in data:
    print d
    flowTcpValues = flowTcp.parseString(d)
    print flowTcpValues.dump()
    print flowTcpValues.packets
    print flowTcpValues.eth.src
    print flowTcpValues.eth.dst
    print

rawDescription: extract data from ovs dump-flow using pyparsing

description: Extract data from ovs dump-flow using pyparsing


code:
## [ . . . ]

class RandomSrc(BaseSource):
    def get(self):
        return random.random()

def create_source(choice):
    if choice == 0:
        return ManualSrc()
    elif choice == 1:
        return RandomSrc()
    else:
        raise ValueError('source choice parameter {} not valid'.format(choice))

if __name__ == '__main__':
    for use_src in range(4):
        print 'using source choice {}'.format(use_src)
        src = create_source(use_src)
        print src.get()

rawDescription: Duck-typable classes, ABC, iheritance, __new__


code:
import shelve

document_dict = shelve.open('document_dict', writeback=True)

document_dict.update({"doc1": {1:2, 2:10, 10:2, 100: 1},
                      "doc2": {10:2, 20:10, 30:2, 41: 19},
                      "doc3": {1:2, 2:10, 10:2, 100: 1},})

new_dict = {1:2, 2:10, 10:2, 120: 1}
doc = document_dict.get("doc3", {})  # get current value, if any

for item in new_dict:
    doc[item] = doc.get(item, 0) + new_dict[item]   # update version in memory

document_dict["doc3"] = doc  # write modified (or new) entry to disk
document_dict.sync()  #  clear cache

print document_dict

document_dict.close()

rawDescription: How can I mimic a space efficient version of dict in Python?

description: Mimic a space efficient version of dict in Python


rawDescription: Parsing NOAAPORT feed with RegEx

description: Parsing NOAAPORT feed with RegEx


code:
stations = {'1': "Karratha_Aero",
            '2': "Karratha_Station",
            '10': "Sherlock"}

user_selection = raw_input("Choose number: ")

print stations.get(user_selection) or "No such station"

rawDescription: Selection an item from a python list

description: Selection an item from a python list


code:
df['YYYYMM']=df['YYYYMM'].astype(str).str[:-2]

print t.index
Index([u'2013', u'2014', u'2015', u'2016'], dtype='object', name=u'YYYYMM')

rawDescription: Matplotlib Line Graph with Pandas Pivot Table


code:
w = open("input1.txt", "r").read()
f = open("input2.txt", "r").read()

for number in w.split():
    for letter in f.split():
        print number, letter

f.close()
w.close()

rawDescription: Processing two lists, iterating line by line and join values


rawDescription: Delete list via referent ? > Was : How do you alias variable in Python ? or similar?


code:
import pandas as pd

d2 = {'one' : pd.Series(['us 1','uk 2','china 3','india 4'], index=['a', 'b', 'c','d'])}
df2 = pd.DataFrame(d2)
print df2

df2['two'] = df2['one'].str.split(' ').str[0]
df2['three'] = df2['one'].str.split(' ').str[1]
print df2

#       one    two three
#a     us 1     us     1
#b     uk 2     uk     2
#c  china 3  china     3
#d  india 4  india     4

rawDescription: How do you iterate over each row in a data frame without applying each index to every row?

description: Iterate over each row in a data frame without applying each index to every row


code:
link = q.rsplit("/",1)
images = tree.xpath("//img")

for idx, image in enumerate(images):
    if '.gif' not in image.attrib['src']:
        images[idx].attrib['src'] = link[0]+'/'+image.attrib['src']

for image in images:
    print image.attrib['src']

rawDescription: Python: replace html img src after extraction with xpath


code: print zip(*a)[0]

rawDescription: Get the nth element from the inner list of a list of lists in Python


code: print "x: " + str(x)

rawDescription: Python Beginner. Is this Python code as efficient as it could be?


code:
import itertools

for row_combination in itertools.product(*matrix):
    print row_combination

rawDescription: All matrix row combination

description: All matrix row combination


code:
while True:
   byte = f.read(1)
   if byte == "": break
   print byte

rawDescription: Extract first 20 bytes in a binary header


code:
import numpy as np
values = [1,2,3,6,7,8]
freqs = [2,1,3,2,3,3]

hist, _ = np.histogram(values, bins=[1, 4, 7, 10], weights=freqs)
print hist

rawDescription: Binning frequency distribution in Python


code:
print random.sample(coordinaten, 20)
print [random.choice(coordinaten) for _ in xrange(20)]

rawDescription: Python pick 20 random results from list

description: Python pick 20 random results from list


code:
from itertools import izip
import re
mylist1=['1.alen','N_NN','2.','RD_PUNC','3.abr','N_NNP','4.london','N_NST','5.','RD_PUNC']
newList = []
def pairwise(iterable):
    a = iter(iterable)
    return izip(a, a)

replaceX = False
for x, y in pairwise(mylist1):
    if replaceX:
        x = 'RD_PUNC'
        replaceX = False
    elif re.match(r'\d+\.$', x):
        y = 'RD_PUNC'
    if re.match(r'\d+\.$', y):
        replaceX = True
    newList.append(x)
    newList.append(y)

print newList

rawDescription: Find an item in list and change the next item in list as 'RD_PUNC'


code:
import numpy as np
print np.random.randint(2, size=(3, 3))

rawDescription: Generate a new numpy array of order 2 filling in each element with a random number in a certain range


code:
def search_student(database=None, ID=None, roll=False, email=False):
    if ID is None or ID == 0:
        print 'Provide the arguments properly'
        return False
    get = database.get(ID)
    if get is not None:
            print 'Student Name: {}'.format(get.name)
            if roll:
                print 'Student Roll: {}'.format(get.roll_no)
            if email:
                print("Student email: {}".format(get.email))
    else:
        return "User does not exist in database"

rawDescription: How can I pass the optional parameter of a namedtuple in a function

description: Pass the optional parameter of a namedtuple in a function


code:
M = np.arange(27).reshape((3,3,3))
for face in iter_faces(M): print face

rawDescription: Selecting (n-1)D array from (n)D array in numpy


code:
def test_all_keys( list_of_keys, pressed ):
    return all( (pressed[x] for x in list_of_keys) )

if test_all_keys((K_h, K_e, K_l, K_o), pressed):
    print "all keys are pressed: h, e, l, o"

rawDescription: How to process pygame.get_pressed() to receive input

description: Process pygame.get_pressed() to receive input


code:
print pow.__doc__
print list.__doc__

rawDescription: Displaying python documentation of all methods and attributes with a certain word in a module


code:
from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    pool = Pool(processes=4)              # start 4 worker processes
    result = pool.apply_async(f, [10])    # evaluate "f(10)" asynchronously
    print result.get(timeout=1)           # prints "100" unless your computer is *very* slow
    print pool.map(f, range(10))          # prints "[0, 1, 4,..., 81]"

rawDescription: Python - Using multiprocessing.Process with a maximum number of simultaneous processes


code:
from twisted.internet import reactor
from twisted.internet.protocol import ClientCreator
from twisted.protocols import amp
import time

class Insert(amp.Command):
    arguments = [('data', amp.Integer())]
    response = [('insert_result', amp.Integer())]

class ServerProxy(object):
    def connected(self,protocol):
        self.serverProxy = protocol # assign protocol as instance variable
        reactor.callLater(5,self.startMultipleInsert) #after five seconds start multiple insert procedure

    def remote_insert(self,data):
        return self.serverProxy.callRemote(Insert, data)

    def startMultipleInsert(self):
        for i in range (10): #send data over ten times
            dF = self.remote_insert(i)
            dF.addCallback(self.gotInsertResult)
            dF.addErrback(error)

    def gotInsertResult(self,result):
        print 'insert_result:', str(result)
        tm=time.time()
        print "stop", tm

def error(reason):
    print "error", reason

def main():
    tm=time.time()
    print "start",tm
    serverProxy = ServerProxy()
    ClientCreator(reactor, amp.AMP).connectTCP('127.0.0.1', 1234).addCallback(serverProxy.connected).addErrback(error)
    reactor.run()

if __name__=='__main__':
    main()

rawDescription: Using Twisted AMP with Database insertion


code:
from StringIO import StringIO
from contextlib import contextmanager

@contextmanager
def my_stringio(s):
    print 'creating StringIO'
    sio = StringIO(s)
    yield sio
    print 'calling close()'
    sio.close()

def mygen():
    with my_stringio('abcdefghij') as sio:
        while 1:
            char = sio.read(1)
            if not char:
                break
            yield char

for char in mygen():
    print char
    if char == 'c':
        break

rawDescription: Freeing a generators resources after iterating only over a part of the sequence


code:
st = set(inappropriate)
message = ' '.join(sys.argv[1:]) # the input message already converted to lowercase
message = message.replace(".", "") # to remove the full stop as well

for word in message.split():
    if word in st:
        print "{} is inappropriate.".format(word)

rawDescription: Output the specific word (from a list) that was found in a string


rawDescription: Two Basic Python Dictionary Questions

description: Two Basic Python Dictionary Questions


code: print re.sub('(?:nithorynque)*', '#', 'ornithorynque')

rawDescription: Why does re.findall() find more matches than re.sub()?


code:
import scipy
import numpy as np
from numpy.linalg import inv
import math
import scipy.misc

#from matplotlib import pyplot as plt, cm, colors

S = [0.038,0.194,.425,.626,1.253,2.500,3.740]
rate = [0.050,0.127,0.094,0.2122,0.2729,0.2665,0.3317]
iterations = 5
rows = 7
cols = 2

B = np.matrix([[.9],[.2]]) # original guess for B
print(B)

Jf = np.zeros((rows,cols)) # Jacobian matrix from r
r = np.zeros((rows,1)) #r equations

def model(Vmax, Km, Sval):
   return ((Vmax * Sval) / (Km + Sval))

def partialDerB1(B2,xi):
   return round(-(xi/(B2+xi)),10)

def partialDerB2(B1,B2,xi):
   return round(((B1*xi)/((B2+xi)*(B2+xi))),10)

def residual(x,y,B1,B2):
   return (y - ((B1*x)/(B2+x)))

#
for _ in xrange(iterations):

   sumOfResid=0
   #calculate Jr and r for this iteration.
   for j in xrange(rows):
      r[j,0] = residual(S[j],rate[j],B[0],B[1])
      sumOfResid += (r[j,0] * r[j,0])
      Jf[j,0] = partialDerB1(B[1],S[j])
      Jf[j,1] = partialDerB2(B[0],B[1],S[j])

   Jft =  Jf.T
   B -= np.dot(np.dot( inv(np.dot(Jft,Jf)),Jft),r)

   print B

rawDescription: Python - Gauss Newton Method Wiki example implementation


code:
def check_list(child1, child2):
    child2 = set(child2)
    return all(child in child2 for child in child1)

children1 = ['test1']
children2 = ['test2', 'test5', 'test1']
print check_list(children1, children2)

rawDescription: How to compare two lists in Python efficiently?

description: Compare two lists in Python efficiently


code:
import re
from collections import OrderedDict
from itertools import imap

ch = '>AB0012xxxxaaaaaaaaaaaa'\
     '>CD00192aaabbbllyybbbaaayyyuubbbaaaggggaaabbb'\
     '>ZP0199000012mmmm3m4mmmxxxxaaabbbaaaaaaaaaaaaa'\
     '>QD1547zzzzzzzzjjjiii'\
     '>SE457895ffffaaabbbbbbbgjhgjgjhgjhgbbbbbaaa'

print ch,'\n'

regx = re.compile('((?<=>).{7})|(aaabbb|bbbaaa)')

def collect(ch):
    li = []
    dic = OrderedDict()

    gen = ( (x.start(),x.group(1),x.group(2)) for x in regx.finditer(ch))
    for st,g1,g2 in gen:
        if g1:
            if li:
                dic[(stprec,g1prec)] = li
            li,stprec,g1prec = [],st,g1
        elif g2:
            li.append((st,g2))
    if li:
        dic[(stprec,g1prec)] = li
    return dic

dic = collect(ch)

print '\n'.join('{:>10} {}'.format(headstart,head)+'\n'+\
                '\n'.join(imap('{0[0]:>10}   {0[1]}'.format,li))
                for (headstart,head),li in dic.iteritems())

rawDescription: Regular expression help

description: Regular expression help


code:
import dis

"""Test method returns True if either argument is falsey, else False."""

def and_chk((a, b)):
    if not (a and b):
        return True
    return False

def not_or_chk((a, b)):
    if not a or not b:
        return True
    return False

print("And Check:\n")
print(dis.dis(and_chk))

print("Or Check:\n")
print(dis.dis(not_or_chk))

rawDescription: Why is "if not (a and b)" faster than "if not a or not b"?


code:
def read_logfile(master_log):
    lastmatch = None
    for line in master_log:
        if '[76:Health]:' in line:
            lastmatch = line
    if lastmatch is not None:
        print lastmatch

rawDescription: python - print last matching line in file

description: Python - print last matching line in file


rawDescription: Python pandas idxmax for multiple indexes in a dataframe


code:
def lev(n):
    if n == 0:
        return "F"
    else:
        symbols = lev(n-1)
        return symbols.replace("F", "LFRRFL")

for i in range(4):
    print lev(i)

rawDescription: Trying to understand the Levy Curve (fractal)


code:
c = Counter(myList)
print [x for x in myList if c[x] == 1]

rawDescription: Removing all duplicates from the list - keeping no instance of a duplicate item


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

fig, ax = plt.subplots()
points, = ax.plot(range(10), 'ro')
ax.axis([-1, 10, -1, 10])

# Get the x and y data and transform it into pixel coordinates
x, y = points.get_data()
xy_pixels = ax.transData.transform(np.vstack([x,y]).T)
xpix, ypix = xy_pixels.T

# In matplotlib, 0,0 is the lower left corner, whereas it's usually the upper
# right for most image software, so we'll flip the y-coords...
width, height = fig.canvas.get_width_height()
ypix = height - ypix

print 'Coordinates of the points in pixel coordinates...'
for xp, yp in zip(xpix, ypix):
    print '{x:0.2f}\t{y:0.2f}'.format(x=xp, y=yp)

# We have to be sure to save the figure with it's current DPI
# (savfig overrides the DPI of the figure, by default)
fig.savefig('test.png', dpi=fig.dpi)

rawDescription: How to get pixel coordinates for Matplotlib-generated scatterplot?

description: Get pixel coordinates for Matplotlib-generated scatterplot


code:
class B64String(ModuledString):
    module = base64

if __name__ == '__main__':
    string = B64String('Foo Bar')
    print string
    # 'Foo Bar'
    print string.b64encode()
    # 'Rm9vIEJhcg=='
    print string
    # 'Rm9vIEJhcg=='
    string.string = 'QmFyIEZvbw=='
    print string
    # 'QmFyIEZvbw=='
    print string.b64decode()
    # 'Bar Foo'

rawDescription: How to add abilities to strings in Python

description: Add abilities to strings in Python


code:
a = [(), (1,), (1, 2), (1, 2, 3)]
b = [(), (4,), (4, 5), (4, 5, 6)]

from itertools import product
for item1, item2 in product(a, b):
    print item1 + item2

rawDescription: Generate a lattice using python

description: Generate a lattice using python


code:
mylist = [0, 1, 2]
print sum([2**x for x in mylist])

rawDescription: Sum of 2^x using elements of a list as exponents


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup('''\

   Wed Sep 17, 2014 1:11 am

   Forum:

      Minewind Chat

   Thread:
   # I wish to grab this href link extension:

      1.8

   Replies:

      3

   Views:

      108

''')
topic_links = soup.select('dd a[href*=viewtopic.php]')
for link in topic_links:
    print link['href']

rawDescription: BeautifulSoup Grabbing a certain href link and storing it


code:
start_dir = '/some/starting/dir'
net1 = '/some/network/filesystem'
remote_files = []

for root, dirs, files in os.walk(start_dir):
    for f in files:
        if is_netfile( path.join(root,f), net1):
            remote_files.append( path.join(root,f))

print remote_files

rawDescription: Detect whether file is on a network drive in python


code:
c={}
with open("Sample.txt") as f:
  for line in f:
   t=line.split(' : ')
   c.update({t[0]:t[1].split()})
for i in set(c.keys()) & set(A):  #gives set of matches between keys and A
  print c[1]

rawDescription: Reading Specific Python list from a text file


code:
xmlstr = """

T1

T2

T3

A1

"""
html_root = LH.fromstring(xmlstr)

eol_table = None
for tbl in html_root.xpath('//table'):
    td_elements = tbl.xpath('.//tr/td')
    eol_table = [td_elm.text_content() for td_elm in td_elements]
    print eol_table

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

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


code:
if list_a[2] - list_a[1] == list_a[1] - list_a[0]:
  print True
else:
  print False

rawDescription: Integer Spacing (python)

description: Integer Spacing (python)


code:
import csv
with open('curr.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',')
    print list(spamreader)

rawDescription: Import csv as list in python

description: Import csv as list in python


rawDescription: How to print a string of variables without spaces in Python (minimal coding!)

description: Print a string of variables without spaces in Python (minimal coding!)


rawDescription: Creating new complex index with pandas Dataframe columns


code:
df = pandas.DataFrame.from_csv("data.txt")

print df["Timestamp"] # or whatever your header names might be

rawDescription: Python: creating variable name from header row in csv read


rawDescription: print surface fit equation in python

description: Print surface fit equation in python


code:
question = raw_input(" Welcome to the phone help center, What is the problem?")
for k in  ["screen", "display", "blank"]:
    if k in question:
        for line in searchfile:
            if k in line:             # or maybe  if 'screen' in line ?
                print line
                break
        break

rawDescription: Searching a text file for a line of text matching the text inputted from a user Python


code: print 'Your seed is {}{}{}{}'.format(goblins_defeated+0, lucky+0, princesshelp+0, end+0)

rawDescription: Assigning variables from a list, or "how to make an elegant save function"


code:
values = theano.shared( value = mydata, name = 'values')
f = theano.function([],lr.p_y_given_x ,
                    givens ={x:values},on_unused_input='ignore')
print f()

rawDescription: What is the prupose/meaning of passing "input" to a function in Theano?


code:
def bb_gen(x):
    for n in x:
        if isinstance(n, str):
            yield n
        elif isinstance(n, tuple):
            count, value = n
            for i in range(count):
                yield value

for value in bb_gen(s):
    print value

rawDescription: I need to print/return multiple elements from tuple in Python


code:
def clean(x, regx = re.compile('(?=^( ))?( |(?<! ))+((?<!])\[[^[\]]*\])( )*',re.MULTILINE)):
    def repl(mat):
        return '' if mat.group(1) else mat.group(2)
    while regx.search(x):
        print '------------\n',x,'\n','\n'.join(map(str,regx.findall(x)))
        x = regx.sub(repl,x)
    return x

print '\n==========================\n'+clean(ss)

rawDescription: Using regex to replace object within brackets in a text file


code:
def letter_grade(score):
    if score < 60:
        print 'The grade is E'
    elif score < 70:
        print 'The grade is D'
    elif score < 80:
        print 'The grade is C'
    elif score < 90:
        print 'The grade is B'
    else:
        print 'The grade is A'
    return score

score = int(raw_input('Enter your test score: '))
letter_grade(score)

rawDescription: How do I take a list of integers, take the function letter_grade and print 'The grade is A' for example.

description: Take a list of integers, take the function letter_grade and print 'The grade is A' for example.


code:
import re

string = """
2"' class=Fonzie>text
""";

for matchElementObj in re.finditer( r'<\w+(?=\s|>)(?:[^>=]|=\'[^\']*\'|="[^"]*"|=[^\'"][^\s>]*)*?>', string, re.M|re.I|re.S):
    print "-------"
    print "matchElementObj.group(0) : ", matchElementObj.group(0)

    for matchAttributesObj in re.finditer( r'\s\w+=(?:\'[^\']*\'|"[^"]*"|[^\'"][^\s>]*)(?=\s|>)', string, re.M|re.I|re.S):
        print "matchAttributesObj.group(0) : ", matchAttributesObj.group(0)

rawDescription: Using regex to extract all the html attrs


rawDescription: Calculate area of polygon given (x,y) coordinates


code:
from BeautifulSoup import UnicodeDammit
from lxml import html

def decode_html(html_string):
    converted = UnicodeDammit(html_string, isHTML=True)
    if not converted.unicode:
        raise UnicodeDecodeError(
            "Failed to detect encoding, tried [%s]",
            ', '.join(converted.triedEncodings))
    # print converted.originalEncoding
    return converted.unicode

tag_soup = open('mess.html').read()

# Use BeautifulSoup's UnicodeDammit to detect and fix the encoding
decoded = decode_html(tag_soup)

# Use lxml's HTML parser (faster) to parse the document
root = html.fromstring(decoded)

spans = root.xpath("//span[@style='font-size:14px;']")
wanted_spans = spans[2:]

blocks = []
for span in wanted_spans:
    line = span.text.strip().replace('\n', '')
    blocks.append(line)

description = '\n'.join(blocks)
print description

rawDescription: extracting nested items with beautifulsoup

description: Extracting nested items with beautifulsoup


rawDescription: How to find number of matches in the array or dictionary?

description: Find number of matches in the array or dictionary


code:
#example items
items = [{'width':5, 'height':10, 'cost':200}, {'width':6, 'height':9, 'cost':2}]

#whatever you want your criteria to be
def calculate_criteria(item):
    return item['width']+item['height']+item['cost']

# create subsets based on criterion
subsets = {}
for item in items:
    criteria = calculate_criteria(item)
    subset = subsets.get(criteria, list())
    subset.append(item)
    subsets[criteria] = subset

print subsets
{17: [{'width': 6, 'cost': 2, 'height': 9}], 215: [{'width': 5, 'cost': 200, 'height': 10}]}

rawDescription: Elegant optimization in Python

description: Elegant optimization in Python


code:
def distance_from_zero(a):
    if type(a) == int or type(a) == float:
        return abs(a)
    else:
        return "Nope"

print distance_from_zero(-2)
print distance_from_zero('hi')

rawDescription: My program is not giving the correct output?


code:
@onceEveryXSeconds(3)
def function(foo):
    print foo

while True:
    print "loop"
    function("Hello again")
    time.sleep(1)

rawDescription: Python For Loop List, Function Every 5min


code: print repr(Foo)

rawDescription: Using __repr__() on a ctypes subclass?

description: Using __repr__() on a ctypes subclass


code:
import time

month = int(time.strftime("%m")) - 1 # minus one, so month starts at 0 (0 to 11)
quarter = month / 3 + 1              # add one, so quarter starts at 1 (1 to 4)
quarter_str = "Q" + str(quarter)     # convert to the "Qx" format string
print quarter_str

rawDescription: how to know which quarter does the current month belongs to ? (in python )

description: Know which quarter does the current month belongs to ? (in python )


code:
from itertools import islice

with open(path,'rb') as f_in:
    for line in islice(f_in, N):
        print line.strip()

rawDescription: Iterate over a portion of a list in a list comprehension


code:
def test():
    while True:
        out = get()
        LThumbstickX = out[0]
        if LThumbstickX != 0:
            print 'Left button has been pressed'
            # and so on

rawDescription: Extract value from a list in another function in Python


code:
import itertools
foo = [[1, 2, 3], [4, 5, 6], [7, 8, 8]]
for p in itertools.permutations(foo, 2):
    print zip(*p)

rawDescription: How to Combine Each of the Elements of Two Lists in Python?


code:
print df.index.tolist()

print list(df.index)

rawDescription: how to convert Index into list?

description: Convert Index into list


code:
text = """
<root>
    <entry id="1">
        <headword>go</headword>
        <example>I <hw>go</hw> to school.</example>
    </entry>
</root>
"""

import lxml.etree
import StringIO

data = lxml.etree.parse(StringIO.StringIO(text))
root = data.getroot()

for entry in root.xpath('//example/ancestor::entry[1]'):
    examplegrp = lxml.etree.SubElement(entry,"examplegrp")
    nodes = [node for node in entry.xpath('./example')]
    for node in nodes:
        entry.remove(node)
        examplegrp.append(node)

print lxml.etree.tostring(root,pretty_print=True)

rawDescription: how can I select all descendants of a certain element with ElementTree in Python 3.3?

description: Select all descendants of a certain element with ElementTree in Python 3.3


code:
from bisect import bisect_left
from itertools import islice, takewhile
def optimized_lookup(data, id, age, age_offset=0.1):
    if id in data:
        l = data[id]
        idx = bisect_left(l, age)
        return [a for a,v in takewhile(lambda (x, value): x <= age+age_offset, islice(l, idx, None))]
    else:
        return None

rawDescription: fetching values from dictionary. Range match on keys

description: Fetching values from dictionary. Range match on keys


code:
open('/tmp/test.csv', 'w').write('User Name\tCode\tTrack\tColor\tNote\n\nUser Name2\tCode2\tTrack2\tColor2\tNote2\n')
for row in read_tabbed_file('/tmp/test.csv'):
    print row

rawDescription: Remove multiple EOL in file

description: Remove multiple EOL in file


code:
from itertools import combinations
for a, b in combinations(intList, 2):
   print abs(a - b)

rawDescription: Getting difference from all possible pairs from a list Python


code: print list(mytable.filter(Row.val4.contains_("h"))) #new way to call in (__contains__)

rawDescription: Error implementing __contains__ (in) for a Python class


code:
links = tree.xpath('//li[@class=""]//a/@href')

found = False

for link in links:
    if url in link:
        found = True
        break

print found

rawDescription: Check if url that belongs to the same domain exists in list with python?


code:
def factorial_cap(num):
    print "Input number:", num
    F = 1
    x = 1
    while (F < num):
        x += 1
        F *= x

    print "The answer is:", x
    return x

for t in xrange(0,30):
    factorial_cap(t)
    print

rawDescription: My python program keeps returning the wrong value doubled


code:
import re

text = '''\
ACCT 221 Principles of Accounting II (3) Prerequisite: ACCT 220
ASTD 485 Issues in East Asian Studies (3) (Intended as a final capstone course to be taken in a student's last 15 credits.) Prerequisites: ASTD 284 (or ASTD 150) and 285 (or ASTD 160).
ASTR 100 Introduction to Astronomy (3) (Not open to students who have taken or are taking any astronomy course numbered 250 or higher. For students not majoring or minoring in a science.) Prerequisite: MATH 012 or higher.
ASTD 380 American Relations with China and Japan: 1740 to Present (3) (Fulfills the general education requirement in the social sciences.) A study of American political, economic, and cultural relations with China and Japan from the American colonial era to modern times'''

courses={}
for line in text.splitlines():
    course=re.match(r'([A-Z]{4}\s+\d{3})', line).group(1)
    m=re.search(r'Prerequisites?:\s*(.*)', line)
    if m:
        pre=m.group(1)
    else:
        pre='None'
    courses[course]=pre

print 'COURSE\t\tPREREQUISITE'

for course in sorted(courses.keys()):
    print '{}\t{}'.format(course, courses[course])

rawDescription: Use regular expressions in Python to search for courses converted from PDF


code:
import traceback, sys

class C:
    def __init__(self):
        try:
            raise StopIteration
        except StopIteration:
            tb = sys.exc_info()[2]
            stack = traceback.extract_stack(tb.tb_frame)
            f = stack[-2]
            print "I was called from %s %s (%s:%s)" % (f[2], f[3], f[0], f[1])

class A:
    def __init__(self):
        self.c = C()

def foo():
    A()
    return C()

def main():
    C()
    foo()

if __name__ == '__main__':
    main()

rawDescription: How to get the child class that called the __init__ method

description: Get the child class that called the __init__ method


code:
import requests
import lxml, lxml.html

r = requests.get('http://www.lebanon-lotto.com/lebanese-loto-results/draw-number/1154.php')

html = lxml.html.fromstring(r.text)

imgs =  html.cssselect('img')

for x in imgs:
    src = x.attrib['src']
    #print src
    if 'lotto_balls_gray' in src:
        print src[-6:-4]

rawDescription: Is it possible to scrap images from a webpage, convert them to numbers and save them to a file?


code:
import numpy as np
from sklearn.decomposition import PCA

np.random.seed(0)
my_matrix = np.random.randn(20, 5)

my_model = PCA(n_components=5)
my_model.fit_transform(my_matrix)

print my_model.explained_variance_
print my_model.explained_variance_ratio_
print my_model.explained_variance_ratio_.cumsum()

rawDescription: Python scikit learn pca.explained_variance_ratio_ cutoff


code:
import rdflib

g = rdflib.ConjunctiveGraph("IOMemory",)

#g is made of two sub-graphs or triples gathered in two different contexts.
#the second paramaters identifies the URIRef for each subgraph.
g.parse("dataA.nt",rdflib.URIRef("http://mygraphs.org/names"),format="n3")
g.parse("dataB.nt",rdflib.URIRef("http://mygraphs.org/relations"),format="n3")

print "traverse all contexts and all triples for each context"
for subgraph in g.contexts():
    print "Graph name",subgraph.identifier
    for triple in subgraph.triples((None,None,None)):
        print triple

print "traverse all contexts where a triple appears"
for subgraph in g.contexts(triple=(rdflib.URIRef('http://data.org/inst1'),rdflib.URIRef("http://xmlns.com/foaf/0.1/name"),rdflib.Literal(u'david'))):
    print "Graph name",subgraph.identifier
    for triple in subgraph.triples((None,None,None)):
        print triple

print "traverse a triple pattern regardless the context is in"
for t in g.triples((None,rdflib.URIRef("http://xmlns.com/foaf/0.1/name"),None)):
    print t

rawDescription: how to add comment or label to the graph in RDFLib?

description: Add comment or label to the graph in RDFLib


code:
from itertools import chain

dict_list = [
{'a' : 1, 'b' : 2},
{'c' : 2, 'd' : 3},
{'x' : 4, 'y' : 5, 'z': 0}
]

for item in chain.from_iterable(i.values() for i in dict_list):
    print item

rawDescription: loop through values from a list of dictionaries in Python

description: Loop through values from a list of dictionaries in Python


code:
import itertools

# maxLength = 5
# letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
maxLength = 3
letters = "ABC"

for length in range(1, maxLength + 1):
    string = [""] * (2 * length - 1)
    for combination in itertools.product(letters, repeat=length):
        string[::2] = combination
        for blanks in itertools.product(["", " "], repeat=length-1):
            string[1::2] = blanks
            print "".join(string)

rawDescription: Script to write endless letters combinations (Regex / ASCII)


code:
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
from twisted.internet.task import LoopingCall
import sys, time

class HeartbeatSender(DatagramProtocol):
    def __init__(self, name, host, port):
        self.name = name
        self.loopObj = None
        self.host = host
        self.port = port

    def startProtocol(self):
        # Called when transport is connected
        # I am ready to send heart beats
        self.loopObj = LoopingCall(self.sendHeartBeat)
        self.loopObj.start(2, now=False)

    def stopProtocol(self):
        "Called after all transport is teared down"
        pass

    def datagramReceived(self, data, (host, port)):
        print "received %r from %s:%d" % (data, host, port)

    def sendHeartBeat(self):
        self.transport.write(self.name, (self.host, self.port))

class HeartbeatReciever(DatagramProtocol):
    def __init__(self):
        pass

    def startProtocol(self):
        "Called when transport is connected"
        pass

    def stopProtocol(self):
        "Called after all transport is teared down"

    def datagramReceived(self, data, (host, port)):
        now = time.localtime(time.time())
        timeStr = str(time.strftime("%y/%m/%d %H:%M:%S",now))
        print "received %r from %s:%d at %s" % (data, host, port, timeStr)

heartBeatSenderObj = HeartbeatSender("sender", "127.0.0.1", 8005)

reactor.listenMulticast(8005, HeartbeatReciever(), listenMultiple=True)
reactor.listenMulticast(8005, heartBeatSenderObj, listenMultiple=True)
reactor.run()

rawDescription: UDP client and server with Twisted Python


code: print '\n'.join(ast.literal_eval(s))

rawDescription: Python Parseing String by Single Quotes

description: Python Parseing String by Single Quotes


code:
from datetime import datetime

dt = datetime(2012, 10, 18)
print dt.timetuple()
# time.struct_time(tm_year=2012, tm_mon=10, tm_mday=18, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=292, tm_isdst=-1)

rawDescription: How to complete the information in a python time tuple?

description: Complete the information in a python time tuple


code:
import sys
import re
code = sys.argv[1]
newval= int(sys.argv[2])
f=open("stockcontrol.csv")
data=f.readlines()
print data
for i,line in enumerate(data):
  if re.search('%s,\d+'%code,line): # search for the line with 8-digit code
    data[i]  = '%s,%d\n'%(code,newval) # replace the stock value with new value in the same line
f.close()

f=open("in.csv","w")
f.write("".join(data))
print data
f.close()

rawDescription: String Replace in csv

description: String Replace in csv


code:
counts = {}

for fruit in fruits:
    counts[fruit] = counts.get(fruit, 0) + 1

top_fruits = sorted(counts, key=lambda fruit: counts[fruit], reversed=True)
print top_fruits[:2]

rawDescription: comparing two lists and finding most preferred pairs

description: Comparing two lists and finding most preferred pairs


code:
import os
#hard code the path to the external file
external_file = 'names.txt'
#Ask the user's name
name = raw_input("What's your name?")
#if file exists, use it to load name, else create a new file
if not os.path.exists(external_file):
    with open(external_file, "a") as f: # using "a" will append to the file
        f.write(name)
        f.write("\n")
        f.close()
else:
    #if file exists, use it to load name, else ask user
    with open(external_file, "r+") as f:# r+ open a file for reading & writing
        lines = f.read().split('\n') # split the names
        #print lines
        if name in lines:
            print "Hi {}".format(name)
        else:
            f.seek(0,2) # Resolves an issue in Windows
            f.write(name)
            f.write("\n")
            f.close()

rawDescription: Store input based on computer and change stored input on command


code:
import math
print math.ceil(1.47)

rawDescription: Round up numbers such as 1.23 to 2 in Python