code:
import nltk
original_grammar = nltk.data.load('grammars/large_grammars/atis.cfg')
original_parser = ChartParser(original_grammar)
sent = ['show', 'me', 'northwest', 'flights', 'to', 'detroit', '.']
for i in original_parser.parse(sent):
    print i
    break

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


code:
import requests

with requests.Session() as session:
    session.get('http://www.justproperty.com/search/uae/apartments/filter__cid/0/sort/score__desc/per_page/20/page/1')

    params = {
        'url': 'filter__cid/0/sort/score__desc/per_page/20/page/1',
        'ajax': 'true'
    }
    response = session.get('http://www.justproperty.com/search/featured-properties/', params=params)
    results = response.json()

    for result in results:
        print result['description']

rawDescription: xpath why i got empty result in this expth

description: Xpath why i got empty result in this expth


code:
var = raw_input("Enter number: ")
print distance_from_zero(var)

rawDescription: Review: Built-In Functions

description: Review: Built-In Functions


code:
from itertools import combinations
from collections import OrderedDict

def sf(k,v,d):
    return (k in d[v]) and (v in d[k])

def lenN(d, n):
    # create a list of unique items
    l = list(OrderedDict.fromkeys(i for x in d for i in d[x]))
    # collect matching groups
    V = list()
    for C in combinations(l, n):
        for P in combinations(C, 2):
            if not sf(P[0], P[1], d): break
        else: V.append(C)
    return V

d = { 1: [2, 3, 5], 2: [3, 1, 5], 3: [1, 2], 5: [2, 1], }

print lenN(d, 3)

rawDescription: Finding a set in a Dictionary fitting a certain criteria


code:
for i in permute3(s):
    print i

rawDescription: Python permutations of both sequence and subsequences


rawDescription: How can I retrieve the TLS/SSL peer certificate of a remote host using python?

description: Retrieve the TLS/SSL peer certificate of a remote host using python


code:
class Entity:
    def action(self):
        print "in Entity::action"

rawDescription: Get a c++ pointer to a Python instance using Boost::Python


code:
Example
#!/usr/bin/env python
from multiprocessing import Process
import sharedmem
import numpy

def do_work(data, start):
    data[start] = 0;

def split_work(num):
    n = 20
    width  = n/num
    shared = sharedmem.empty(n)
    shared[:] = numpy.random.rand(1, n)[0]
    print "values are %s" % shared

    processes = [Process(target=do_work, args=(shared, i*width)) for i in xrange(num)]
    for p in processes:
        p.start()
    for p in processes:
        p.join()

    print "values are %s" % shared
    print "type is %s" % type(shared[0])

if __name__ == '__main__':
    split_work(4)

rawDescription: Share Large, Read-Only Numpy Array Between Multiprocessing Processes


code:
from HTMLParser import HTMLParser

class YoutubeLinkParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        self.data = attrs[2][1].split('/')[-1]

iframe = open('iframe.html').read()
parser = YoutubeLinkParser()
parser.feed(iframe)
print parser.data

rawDescription: return data from HTMLParser handle_starttag

description: Return data from HTMLParser handle_starttag


code:
with open("file1") as f1,open("file2") as f2:
    for x,y in zip(f1,f2):
        if x !=y :
           print " dissimilar lines "

rawDescription: Python: Trouble with comparing two files and making a new file which shows the differences


code:
from xml.etree import ElementTree as ET

tree = ET.fromstring("""
<test>
    <spotter num="0187" report_at="2014-03-15 20:10:25" lat="49.8696518" lng="-80.0973129" callsign="wxman132" active="1" public="" gps="0" phone="" email="addu@nnu.nne" first="" last=""/>
</test>""")
spotter = tree.find('.//spotter')
print spotter.attrib['lat'], spotter.attrib['lng']

rawDescription: Python parse XML variables from a single tag


code:
class myProtocolFactory():
    protocol = MyProtocol

    def doStart(self):
        pass

    def startedConnecting(self, connectorInstance):
        print connectorInstance

    def buildProtocol(self, address):
        print address
        return self.protocol()

    def clientConnectionLost(self, connection, reason):
        print reason
        print connection

    def clientConnectionFailed(self, connection, reason):
        print connection
        print reason

    def doStop(self):
        pass

rawDescription: Twisted client for a send only protocol that is tolerant of disconnects


code:
def split(array):
    n = len(array)
    if n == 1:
        return
    else:
        i = n // 2
        subarray1 = array[:i]
        subarray2 = array[i:]
        yield subarray1
        yield subarray2

        for a in split(subarray1):
            yield a
        for a in split(subarray2):
            yield a

for a in split(range(16)):
    print a

rawDescription: python recursive yield to reduce memory footprint

description: Python recursive yield to reduce memory footprint


code:
for n in range(2,100):
    for x in range(2,n):
        if n%x == 0:
             break
    else:
        print n,'is a prime number'

rawDescription: Prime number in python, prog not working


code:
#!/usr/bin/python
import os.path
import sys

def command1(*args):
    print 'Command1'
    print args

def command2(*args):
    print 'Command2'
    print args

commands = {
    'command1': command1,
    'command2': command2
}

if __name__ == '__main__':
    command = os.path.basename(sys.argv[0])
    if command in commands:
        commands[command](*sys.argv[1:])

rawDescription: Running a python function as a bash command


code:
# This might be slower though if the string is massive
print "\n".join([mystr[:i] for i in range(len(mystr)+1)])

rawDescription: Python iterate over each string?

description: Python iterate over each string


code: print shapes.loc[20958]

rawDescription: Breaking down large dataset into organized index


code:
import numpy as np
a = np.array([[1, 2, 3]])
b = np.array([[1],[2],[3]])
print a == b

rawDescription: Numpy: How to get rid of the minima along axis=1, given the indices - in an efficient way?


code:
def findIt(data, num):
    num = str(num)
    return any(num in str(i) for item in data for i in item)

data = ((1234L,), (23456L,), (3333L,))
print findIt(data, 123)

rawDescription: Check if part of text is in tuple

description: Check if part of text is in tuple


code:
if re.compile('Firma:.*').search(attribute.text):
    firma = attribute.text
    print firma

rawDescription: re.compile does not work properly

description: Re.compile does not work properly


code:
import boto
conn = boto.connect_ec2('asdf','asdfasdfasdfasdf')

tags = conn.get_all_tags()
for tag in tags:
    print tag.name, tag.value

rawDescription: boto aws pulling down list of instances

description: Boto aws pulling down list of instances


code:
import urllib2

for line in urllib2.urlopen(target_url):
    print line

rawDescription: In Python, given a URL to a text file, what is the simplest way to read the contents of the text file?


code:
for roll in sorted(result):
     print '{}: {}'.format(roll, result[roll])

rawDescription: Organize Counter () display in python?

description: Organize Counter () display in python


code:
class yo:
    self.message = "Hello there!"
    def say_message(self):
        print self.message

rawDescription: Where is the best place to put support functions in a class?


code:
import multiprocessing
import subprocess

def work(cmd):
    return subprocess.call(cmd, shell=False)

if __name__ == '__main__':
    count = multiprocessing.cpu_count()
    pool = multiprocessing.Pool(processes=count)
    print pool.map(work, ['ls'] * count)

rawDescription: How to spawn parallel child processes on a multi-processor system?

description: Spawn parallel child processes on a multi-processor system


code:
from scipy import interpolate
f = interpolate.interp2d(y,x,z,kind='linear') #  columns, rows, data
print f([0,.25,.5,.75],[0,.25,.5,.75])

rawDescription: Interpolate values in a 2D Python numerical dictionary


code:
# how many bar charts we expect
numofcharts = len(ser) / 2
# prepare axes for subplots (1 row, numofcharts columns one per bar chart)
fig, axs = plt.subplots(1, numofcharts)
for graphi in range(numofcharts):
    starti = 2*graphi
    # create subseries one for each subchart
    subser = ser[starti:starti+2]
    # print subseries, to see, what we are going to plot
    print subser
    # plot subseries as bar subchart
    subser.plot(ax=axs[graphi], kind="bar")

rawDescription: Plotting bar charts by each two rows

description: Plotting bar charts by each two rows


code:
if __name__ == "__main__":
    print "result is:", b.twomore(5)

rawDescription: Python: how to enable a called file to access functions defined in the calling file?


code:
element = driver.find_element_by_xpath(".//*[@id='toplevel_page_menu']/ul/li[2]/a")
href = element.get_attribute('href')
print href

rawDescription: Extract link from XPath using Selenium Webdriver and Python?


code:
questions = ['Question 1', 'Question 2', 'Question 3']
originals = [['Question 1', 'a1'], ['Question 2', 'b1'], ['Question 3', 'c1']]
answers = [['a1'], ['a2'], ['a3']], [['b1'], ['b2'], ['b3']], [['c1'], ['c2'], ['c3']]        #List of answers for each question
selected_answers = [] #Contains selected answers
random.shuffle(questions)
random.shuffle(answers[0])
random.shuffle(answers[1])
random.shuffle(answers[2])
question = 0
while question < 4:
    if questions[0] == 'Question 1':
        print 'Question 1'
        print answers[0][0], answers[0][1], answers[0][2]
        chosen = raw_input('Enter 1 for the first answer, 2 for the second answer and 3 for the third one.')
        selected_answers.append(chosen)
        del questions[0]
        question += 1
    elif questions[0] == 'Question 2':
        print 'Question 2'
        print answers[1][0], answers[1][1], answers[1][2]
        chosen = raw_input('Enter 1 for the first answer, 2 for the second answer and 3 for the third one.')
        selected_answers.append(chosen)
        del questions[0]
        question += 1
    elif questions[0] == 'Question 3':
        print 'Question 3'
        print answers[2][0], answers[2][1], answers[2][2]
        chosen = raw_input('Enter 1 for the first answer, 2 for the second answer and 3 for the third one.')
        selected_answers.append(chosen)
        del questions[0]
        question += 1

rawDescription: how to randomize order of questions in a quiz in python?

description: Randomize order of questions in a quiz in python


code:
from collections import Counter
# Create frequency table of (C,D) column pairs
file2freq = Counter(zip(C,D))
# Look up frequency value for each row of file 1
for a,b in zip(A,B):
    # and print out the row and frequency data.
    print a,b,file2freq[a,b]

rawDescription: Counting the intersection of equivalent rows in two tables


code:
import sys
print sys.argv

rawDescription: Read argument with spaces in python script from a shell script


code:
for i, row in df.iterrows():
    key = row['lookup_key'].astype(int)
    nNulls = lkup_dict[key]
    startIndex = df.shape[1] - nNulls
    df.loc[i, startIndex:] = np.nan
print df

rawDescription: Apply Across Dynamic Number of Columns

description: Apply Across Dynamic Number of Columns


code:
# Let's use some useful pieces of the awesome standard library
from collections import namedtuple, Counter

# Define a simple structure to hold the properties of each entry in corpus
CorpusEntry = namedtuple('CorpusEntry', ['word', 'lemma', 'tag'])

# Use a context manager ("with...") to automatically close the file when we no
# longer need it
with open('corpus.vert') as c:
    corpus = []
    for line in c:
        if len(line.strip()) > 1 and not line.startswith('<'):
            # Remove the newline character and split at tabs
            word, lemma, tag = line.strip().split('\t')
            # Put the obtained values in the structure
            entry = CorpusEntry(word, lemma, tag)
            # Put the structure in the corpus list
            corpus.append(entry)

# It's practical to wrap the counting in a function
def get_frequencies(lemma):
    # Create a set of indices at which the lemma occurs in corpus. We use a
    # set because it is more efficient for the next part, checking if some
    # index is in this set
    lemma_indices = set()
    # Loop over corpus without manual indexing; enumerate provides information
    # about the current index and the value (some CorpusEntry added earlier).
    for index, entry in enumerate(corpus):
        if entry.lemma == lemma:
            lemma_indices.add(index)

    # Now that we have the indices at which the lemma occurs, we can loop over
    # corpus again and for each entry check if it is either one before or
    # one after the lemma. If so, add the entry's lemma to a new set.
    related_lemmas = set()
    for index, entry in enumerate(corpus):
        before_lemma = index+1 in lemma_indices
        after_lemma = index-1 in lemma_indices
        if before_lemma or after_lemma:
            related_lemmas.add(entry.lemma)

    # Finally, we need to count the number of occurrences of those related
    # lemmas
    counter = Counter()
    for entry in corpus:
        if entry.lemma in related_lemmas:
            counter[entry.lemma] += 1

    return counter

print get_frequencies('in')
# Counter({'the': 2, 'union': 1, 'restriction': 1, 'danger': 1})

rawDescription: How to count word frequency from a list in the whole file?

description: Count word frequency from a list in the whole file


code:
p = Popen("myshell.exe", stdout=PIPE, stdin=PIPE, stderr=PIPE, shell=True)
for i in range(3):
    p.stdin.write("run")
    print p.stdout.read(16)
p.terminate()

rawDescription: have a .exe run in the background and type things into that through python

description: Have a .exe run in the background and type things into that through python


code:
txt='''\
1\tDemo
\t\tExample
\t\t\tThis is the description text body that I am
\t\t\ttrying to capture with regex.
\t\tSep
\t\t\tAnd Another Section
\t\t\tOn two lines
'''

cap=[]
buf=[]
for line in txt.splitlines():
    if line.startswith('\t\t\t'):
        buf.append(line.strip())
        continue
    if buf:
        cap.append(' '.join(buf))
        buf=[]
else:
    if buf:
        cap.append(' '.join(buf))

print cap

rawDescription: RegEx to capture multi-line text body

description: RegEx to capture multi-line text body


code:
class Board(tuple):
    def __init__(self, tup):
        for i in tup:
            pass
myList, myTuple = range(10), tuple(xrange(10))

from timeit import timeit
print timeit("Board(myList)", "from __main__ import myList, Board", number = 1000000)
print timeit("Board(myTuple)", "from __main__ import myTuple, Board", number = 1000000)

rawDescription: Inheriting a tuple as a list

description: Inheriting a tuple as a list


code:
def only_positive(x):
    return x[1]['start_diff'] > 0

def min_start(x):
    return x[1]['start_diff']

only_positive_start_diff = filter(only_positive, a.items())
min_start_diff = min(only_positive_start_diff, key=min_start)

print min_start_diff[0]

rawDescription: How do I find the top level KEY of the lowest positive VALUE from a particular SUBKEY?

description: Find the top level KEY of the lowest positive VALUE from a particular SUBKEY


rawDescription: Decoding escaped characters

description: Decoding escaped characters


code:
s = '++*+/+'
l = [3, 8, 9, 110, 26, 38, 1]

result = l[0]
for i in range(1, len(l)):
    # print result,s[i-1],l[i]
    if s[i-1]=='-':
        result = result - l[i]
    elif s[i-1]=='+':
        result = result + l[i]
    elif s[i-1]=='*':
        result = result * l[i]
    elif s[i-1]=='/':
        result = float(result) / l[i]
print result

rawDescription: implementing strange calculator

description: Implementing strange calculator


code:
results.reverse() # put them back in regular order
for value, average in results:
    print value, results

rawDescription: To stop finding running average once the condition fails?


code:
while True:
        choice = raw_input('What do you want? ')
        if choice == 'restart':
                continue
        else:
                break

print 'Break!'

rawDescription: How to restart a simple coin tossing game

description: Restart a simple coin tossing game


code:
@deprecated("function foo is deprecated", "use bar instead")
def foo(bar):
    print 'Hello', bar
foo('World')

rawDescription: Get specific info about calling code in Python decorators


code:
import web

urls = ('/.*', 'hooks')

app = web.application(urls, globals())

class hooks:
    def POST(self):
        data = web.data()
        print
        print 'DATA RECEIVED:'
        print data
        print
        return 'OK'

if __name__ == '__main__':
    app.run()

rawDescription: How do I receive Github Webhooks in Python

description: Receive Github Webhooks in Python


code:
class C(object):
    def __init__(self):
        self._scaling_method = None

    def same(self, u):
        return u

    def double(self, u):
        return u * 2

    scale = same

    @property
    def scaling_method(self):
        return self._scaling_method

    @scaling_method.setter
    def scaling_method(self, value):
        if value.lower() == "double":
            self._scaling_method = "double"
            self.scale = self.double
        elif value.lower() == "same":
            self._scaling_method = "same"
            self.scale = self.same
        else:
            print "Unknown scaling method."

rawDescription: Changing dynamically one method of a class


rawDescription: Catching a selected text from the string in python


code:
from pathos.multiprocessing import ProcessingPool;
import time
import sys;
import datetime
class tester:
    def __init__(self):
        self.pool=ProcessingPool(2);
    def func(self,msg):
        print (str(datetime.datetime.now()));
        for i in xrange(1):
            print msg
            sys.stdout.flush();
        time.sleep(2)
    def worker(self):
        """"""
        pool=self.pool
        for i in xrange(10):
               msg = "hello %d" %(i)
               pool.map(self.func,[i])
        pool.close()
        pool.join()
        time.sleep(40)

datetime.datetime.now()
t = tester()
t.worker()

rawDescription: Pathos multiprocessing can't call any package and function in the class


code: print next((item for item in phonebook if item["name"].lower() == name.lower()),"Does not exist")

rawDescription: Phonebook dictionary

description: Phonebook dictionary


code:
import itertools

cars = [
    {'car': 'X2', 'mods': [1,2]},
    {'car': 'Y2', 'mods': [2]},
    {'car': 'W2', 'mods': [1]},
    {'car': 'X1', 'mods': [1,2]},
    {'car': 'W1', 'mods': [1]},
    {'car': 'Y1', 'mods': [2]},
    {'car': 'Z1', 'mods': [1,2,3]},
    {'car': 'X3', 'mods': [1,2]},
]

cars.sort(key=lambda car: car['mods'])

cars_by_common_mods = {}
for k, g in itertools.groupby(cars, lambda car: car['mods']):
    cars_by_common_mods[frozenset(k)] = [car['car'] for car in g]

print cars_by_common_mods

rawDescription: Django/Python - Grouping objects by common set from a many-to-many relationships


code:
from numpy import floor
import time

def quantile(rdd, p, sample=None, seed=None):
    """Compute a quantile of order p ∈ [0, 1]
    :rdd a numeric rdd
    :p quantile(between 0 and 1)
    :sample fraction of and rdd to use. If not provided we use a whole dataset
    :seed random number generator seed to be used with sample
    """
    assert 0 <= p <= 1
    assert sample is None or 0 < sample <= 1

    seed = seed if seed is not None else time.time()
    rdd = rdd if sample is None else rdd.sample(False, sample, seed)

    rddSortedWithIndex = (rdd.
        sortBy(lambda x: x).
        zipWithIndex().
        map(lambda (x, i): (i, x)).
        cache())

    n = rddSortedWithIndex.count()
    h = (n - 1) * p

    rddX, rddXPlusOne = (
        rddSortedWithIndex.lookup(x)[0]
        for x in int(floor(h)) + np.array([0L, 1L]))

    return rddX + (h - floor(h)) * (rddXPlusOne - rddX)

rawDescription: How to find median using Spark

description: Find median using Spark


code:
import struct

#binary_data = open("your_binary_file.bin","rb").read()

#your binary data would show up as a big string like this one when you .read()
binary_data = '\x44\x69\x62\x65\x6e\x7a\x6f\x79\x6c\x70\x65\x72\x6f\x78\x69\x64\x20\x31\
x32\x30\x20\x43\x20\x30\x33\x2e\x30\x35\x2e\x31\x39\x39\x34\x20\x31\x34\x3a\x32\
x34\x3a\x33\x30'

def search(text):

    #convert the text to binary first
    s = ""

    for c in text:
        s+=struct.pack("b", ord(c))

    results = binary_data.find(s)
    if results == -1:
        print "no results found"
    else:
        print "the string [%s] is found at position %s in the binary data"%(text, results)

search("Dibenzoylperoxid")

search("03.05.1994")

rawDescription: Trying to unpack/decode proprietary data file in Python


code:
data = {
    'a': 15,
    'c': 10,
    'b': 2,
}
# Sort by key:
print [v for (k, v) in sorted(data.iteritems())]
# Output: [15, 2, 10]
# Keys, sorted by value:
from operator import itemgetter
print [k for (k, v) in sorted(data.iteritems(), key = itemgetter(1))]
# Output: ['b', 'c', 'a']

rawDescription: How does one insert a key value pair into a python list?

description: Insert a key value pair into a python list


code:
class Menu:
    def __init__(self):
        self.menu = '1'

    def Do(self):
        if self.menu == '1':
            self.Main()
        elif self.menu == '2':
            self.Sub()

    def Main(self):

        os.system('clear')
        print "main menu"
        test = raw_input()
        if test == '2':
            self.menu = '2'

    def Sub(self):

        os.system('clear')
        print "sub menu"
        test = raw_input()
        if test == '1':
            self.menu = '1'

rawDescription: swapping between menus in python

description: Swapping between menus in python


code:
import csv

with open('test_file.csv', 'rb') as csvfile:
    csv_reader = csv.reader(csvfile)
    for row in csv_reader:
        print row

rawDescription: Python split string(',') issue on a csv field


code:
#!/usr/bin/python

import re

s = """love
lol
loki
loki
ki
loutre
poutre"""

arr = s.split('\n')                                             # sed iterates on each line
arr = map((lambda line: re.sub(r'lo(.*)$', r'\1', line)), arr)  # sed
arr = set(arr)                                                  # uniq
arr = sorted(list(arr))                                         # sort

print '\n'.join(arr)                                            # output it

rawDescription: Converting a sed regular expression to python code


code:
def _finished(self, reply):
    print reply.request().rawHeader("User-Agent")

rawDescription: what is the Default user-Agent of PyQt Web kit Qwebview and how to get it

description: What is the Default user-Agent of PyQt Web kit Qwebview and how to get it


code:
import itertools

def find_correct_operators(seq, total):
    signs = [-1,1]
    for item_signs in itertools.product(*[signs]*len(seq)):
        seq_with_signs_applied = [item*sign for item, sign in zip(seq, item_signs)]
        sum(seq_with_signs_applied)
        if sum(seq_with_signs_applied) == total:
            return item_signs

a = [5,4,3,2,1]
b = 7
signs = find_correct_operators(a,b)
if signs is not None:
    print "{} = {}".format(" ".join("{}{}".format("-" if sign == -1 else "+", item) for sign, item in zip(signs, a)), b)
else:
    print "No solution found"

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


code:
ins = """
    with ins_user as (
        insert into User (
            LastName, FirstName, Gender, DateOfBirth, DateEnrolled
        ) values (%(last_name)s, %(first_name)s, %(gender)s, %(dob)s, now())
        returning UserId
    )
    insert into UserProfile (
        UserId, DateEntered, FaveNumber, Activity
    ) values ((select UserId from ins_user), now(), %(fave_number)s, %(activity)s)
"""

ins_dict = dict (
    last_name = 'Sara',
    first_name = 'Jackson',
    gender = 'F',
    dob = '1941-1-12',
    fave_number = '3',
    activity = 'volleyball'
)
print cur.mogrify(ins, ins_dict) # inspect the rendered command
cur.execute(ins, ins_dict)
connection.commit()

rawDescription: Psycopg2 to create a record using a foreign key


code:
from collections import defaultdict

A = [3,2,1,9,4]
B = [1980,1980,1988,1988,2000]

yearvals = defaultdict(list)
for year, val in zip(B, A):
    yearvals[year].append(val)

for year, vals in yearvals.iteritems():
    print year, sum(vals, 0.0) / len(vals)

rawDescription: mean of items in a list corresponding to same date in another list python

description: Mean of items in a list corresponding to same date in another list python


code:
class Worm(pygame.sprite.Sprite): # MODIFIED CONTENTS!
    def __init__(self, surface, x, y, length, (seg_width, seg_height)): # REVISED
        self.surface = surface
        self.x = x
        self.y = y
        self.length = length
        self.dir_x = 0
        self.dir_y = -seg_width # REVISED
        self.body = []
        self.crashed = False

        self.rect = pygame.Rect(x,y,seg_width,seg_height) # NEW/REVISED
        self.segment = pygame.Rect(0,0,seg_width,seg_height) # NEW

    def key_event(self, event):
        """ Handle key events that affect the worm. """
        if event.key == pygame.K_UP:
            self.dir_x = 0
            self.dir_y = -self.segment.height # REVISED
        elif event.key == pygame.K_DOWN:
            self.dir_x = 0
            self.dir_y = self.segment.height # REVISED
        elif event.key == pygame.K_LEFT:
            self.dir_x = -self.segment.width # REVISED
            self.dir_y = 0
        elif event.key == pygame.K_RIGHT:
            self.dir_x = self.segment.width # REVISED
            self.dir_y = 0

    def move(self):
        """ Move the worm. """
        self.x += self.dir_x
        self.y += self.dir_y

        self.rect.topleft = self.x, self.y # NEW/MOVED

        if self.rect.collidelist(self.body) != -1: # REVISED
            self.crashed = True
            print "CRASHED!"

        new_segment = self.segment.copy() # NEW
        self.body.insert(0, new_segment.move(self.x, self.y)) # REVISED

        if len(self.body) > self.length:
            self.body.pop()

    def draw(self): # REVISED
        for SEGMENT in self.body:   # REPLACEMENT
            self.surface.fill((255,255,255), SEGMENT) # REPLACEMENT

rawDescription: colliderect() triggers unexpectedly

description: Colliderect() triggers unexpectedly


code:
class ClassA(object):
    var1 = 0
    var2 = 0
    def __init__(self):
        ClassA.var1 = 1
        ClassA.var2 = 2

    def methodA(self):
        ClassA.var1 = ClassA.var1 + ClassA.var2
        return ClassA.var1

class ClassB(ClassA):
    def __init__(self):
        print ClassA.var1
        print ClassA.var2

object1 = ClassA()
sum = object1.methodA()
object2 = ClassB()
print sum

rawDescription: How would I access variables from one class to another?


code:
import subprocess
import sys

try:
    retcode = subprocess.call(cmd, shell=True)
    if retcode < 0:
        print >>sys.stderr, "Child was terminated by signal", -retcode
    else:
        print >>sys.stderr, "Child returned", retcode
except OSError, e:
    print >>sys.stderr, "Execution failed:", e

rawDescription: Python os.system() automaticly exit when command is not finished yet


code:
from dateutil import parser

dates = ['30th November 2009', '31st March 2010', '30th September 2010']

for date in dates:
    print parser.parse(date).strftime('%Y%m%d')

rawDescription: Convert a date string into YYYYMMDD

description: Convert a date string into YYYYMMDD


code:
base_url = 'https://graph.facebook.com/me'
ACCESS_TOKEN="XXXXX"
params = {
    'fields': 'id,name,accounts',
    'access_token': ACCESS_TOKEN,
}
dat = requests.get(url, params=parames).json()
for entry in dat['accounts']['data']:
    print entry['name']

rawDescription: parsing json data using request function in python.... i am unable to access the objects

description: Parsing json data using request function in python.... i am unable to access the objects


code:
from urllib import urlopen
from BeautifulSoup import BeautifulSoup

url = 'http://www.marketwatch.com/investing/stock/goog/financials'
text_soup = BeautifulSoup(urlopen(url).read()) #read in

titles = text_soup.findAll('td', {'class': 'rowTitle'})
for title in titles:
    if 'EPS (Basic)' in title.text:
        print [td.text for td in title.findNextSiblings(attrs={'class': 'valueCell'}) if td.text]

rawDescription: Parsing html data into python list for manipulation


code:
import random

data = [
    ('mail.yahoo.com', 'CNAME', 'login.yahoo.com'),
    ('foo.com', 'CNAME', 'baz.com'),    # Wooops, watch out!
    ('login.yahoo.com', 'CNAME', 'ats.login.lgg1.b.yahoo.com'),
    ('ats.login.lgg1.b.yahoo.com', 'CNAME', 'ats.member.g02.yahoodns.net'),
    ('baz.com', 'A', '204.236.134.199'), # Wooops, watch out!
    ('ats.member.g02.yahoodns.net', 'CNAME', 'any-ats.member.a02.yahoodns.net'),
    ('any-ats.member.a02.yahoodns.net', 'A', '98.139.21.169')
]

# test 50 times
for x in xrange(50):
    # shuffle the data array
    random.shuffle(data)

    print resolve(data)

rawDescription: Grouping text lines so if A=B and B=C, then A=C


rawDescription: Python - Transpose columns to rows within data operation and before writing to file


code:
files = {'file': ('foo.txt', 'foo\ncontents\n'),
         'file2': ('bar.txt', 'bar contents', 'text/plain'),
         'file3': ('baz.txt', 'baz contents', ('text/plain', {'header': 'value'}))}
print r.prepare().body

rawDescription: How to add per-file custom header to multipart upload with requests?

description: Add per-file custom header to multipart upload with requests


code:
#! /usr/bin/env python

from prettytable import PrettyTable

d1 = {
  "key1":["val1_1", "val1_2"],
  "key2":["val2_1", "val2_2"],
  "key3":["val3_1", "val3_2"],
  "key4":["val4_1", "val4_2"],
}

table = PrettyTable()

for key,val in sorted(d1.iteritems()):
  table.add_column(key, sorted(val))

print table

rawDescription: How to format a dict of lists as a table

description: Format a dict of lists as a table


code:
import fnmatch
import os

print [f for f in os.listdir("C:\Users\UserName\Desktop\New_folder\export") if fnmatch.fnmatch(f, '*.mkv')]

rawDescription: how to get name of a file in directory using python

description: Get name of a file in directory using python


code:
damage = 50
zombie_hp = 100

def attack():
    global zombie_hp #1
    zombie_hp -= damage #2
    print "Zombie health %s/100"%zombie_hp #3
    if zombie_hp <= 0:
        print "Zombie dies"

print "You see a zombie in the distance."
while zombie_hp > 0: #4
    user_input = raw_input("What will you do?: ")
    if user_input == 'shoot':
        attack()

rawDescription: Having trouble saving items

description: Having trouble saving items


code:
testVar = 0

def testFunc():
    global testVar
    testVar += 1

print testVar
testFunc()
print testVar

rawDescription: Local and global variables in python functions


code:
with open(r'<file_path>','r') as file:
        for line in file:
            if  re.match(r'^<', line) == None:
                print line[:line.find(r'<')]

rawDescription: Find words down (and between) string in Python


code:
some_dict = {}
for i in range(2):
    def create_fun(i):
        def fun(self, *args):
            print i
        fun.__doc__ = "I am function {}".format(i)
        fun.__name__ = "function_{}".format(i)
        return fun
    some_dict["function_{}".format(i)] = create_fun(i)

rawDescription: Why can two functions with the same `id` have different attributes?


code: print totals.items()

rawDescription: Algorithm in Python To Solve This Problem


code:
from bs4 import BeautifulSoup

html =  """

            Active

4562256

            Global Finance

2013

            ...

            Active

4588222

            Global Finance

2014
            ...

            Active

International Trade

            4552214

2014

            ...

            Active

8631448

            Law

2012

            ..."""

soup = BeautifulSoup(html)

for tr in soup.find_all('tr'):

    ## a list of all the divs in your tr.
    divs = tr.find_all("div")

    ## the subject is the first...
    subject = divs[0].text

    ## ...and the year the second "div" in divs.
    year = divs[1]["title"]

    if year == "2014":
        print subject

rawDescription: Selectively picking texts from a (webpage) table


code:
from subprocess import Popen, PIPE

def sync(src, dest, passwd):
    cmd = ['sshpass', '-p', passwd, 'rsync', '-avz', '--progress', src, dest]
    p = Popen(cmd, stdout=PIPE)
    line = ''
    while True:
        c = p.stdout.read(1)
        if not c:
            break
        if c in ['\n', '\r']:
            print 'rsync progress: %s' % line
            line = ''
        else:
            line += c

sync('/path/big.txt', 'myserver:/path/', 'mypassword')

rawDescription: How to write a polling function in Python?

description: Write a polling function in Python


rawDescription: Can we pass a environment variable modified by python script to the outer shell?


rawDescription: Python Shift Scheduling Optimization

description: Python Shift Scheduling Optimization


code:
def fact(n):
    """factorial of n, n!"""

    f = 1

    while n > 1:
         f *= n
         n -= 1

    return f

def rrank(s):
    """Back-end to rank for 0-based rank of a list permutation"""

    # trivial case
    if len(s) < 2: return 0

    order = s[:]
    order.sort()

    denom = 1

    # account for multiple occurrences of letters
    for i, c in enumerate(order):
        n = 1
        while i + n < len(order) and order[i + n] == c:
            n += 1

        denom *= n

    # starting letters alphabetically before current letter
    pos = order.index(s[0])

    #recurse to list without its head
    return fact(len(s) - 1) * pos / denom + rrank(s[1:])

def rank(s):
    """Determine 1-based rank of string permutation"""

    return rrank(list(s)) + 1

strings = [
    "ABC", "CBA",
    "ABCD", "BADC", "DCBA", "DCAB", "FRED",
    "QUESTION", "BOOKKEEPER", "JACBZPUC",
    "AAAB", "AABA", "ABAA", "BAAA"
]

for s in strings:
    print s, rank(s)

rawDescription: Word ranking partial completion

description: Word ranking partial completion


code: print s.split('\n')

rawDescription: Opening a file- How to make a .txt file return a list of lists


rawDescription: OOP conditionals for greater specificity - Python


code:
for i in range(c.shape[-1]):
    print c[:,:,i]

rawDescription: Iterating over arbitrary dimension of numpy.array


code:
from urlparse import urlsplit, urlunsplit

data = [["Marino Institute","http://www.mie.ie/","/Library.aspx"],
["TCD", "http://www.tcd.ie/", "/Library/"],
["DIT", "http://www.dit.ie/", "http://dit.ie/library/"],
["IT Tallaght", "http://www.it-tallaght.ie/", "libraryservice3"],
["LYIT", "http://www.lyit.ie/#!prettyPhoto", "http://library1.lyit.ie/"],
["DCU", "http://www.dcu.ie/", "/library/index.shtml"],
["NUIG", "http://www.nuigalway.ie/", "//www.library.nuigalway.ie/"]]

def merge_urls(partial_url1, partial_url2):

    o1 = urlsplit(partial_url1)
    o2 = urlsplit(partial_url2)

    # domain of o2 takes precedence over domain in o1
    if(o2.netloc != ''):
        o3 = o2
        o2 = o1
        o1 = o3

    schemes, netlocs, paths, queries, fragments = zip(o1, o2)
    scheme = schemes[0] if schemes[0] != '' else schemes[1]
    netloc = netlocs[0] if netlocs[0] != '' else netlocs[1]
    path = paths[0] if paths[0] != '/' else paths[1]
    query = queries[0] if queries[0] != '' else queries[1]
    fragment = fragments[0] if fragments[0] != '' else fragments[1]

    return urlunsplit((scheme, netloc, path, query, fragment))

for d in data:
    print d[0], merge_urls(d[1], d[2])

rawDescription: Python advice: Compile list of urls that contain keyword but only include unique valid links


code:
print number_string[:2] # first two digits
print number_string[-3:] # last three digits
print number_string[3] # forth digit

rawDescription: Split an integer into digits to compute an ISBN checksum


code:
from rdflib import Graph, Literal, Namespace, RDF, URIRef

graph = Graph()
skos = Namespace('http://www.w3.org/2004/02/skos/core#')
graph.bind('skos', skos)

graph.add((URIRef('URI'), RDF['type'], skos['Concept']))
graph.add((URIRef('URI'), skos['prefLabel'], Literal('Temp', lang='en')))
graph.add((URIRef('URI'), skos['related'], URIRef('URI-Related')))

print graph.serialize(format='pretty-xml')

rawDescription: create rdf files based on SKOS data model in python

description: Create rdf files based on SKOS data model in python


code:
class SimpleProgressBar(object):
    def __init__(self, maximum, state=0):
        self.max = maximum
        self.state = state

    def _carriage_return(self):
        sys.stdout.write('\r')
        sys.stdout.flush()

    def _display(self):
        stars = ''.join(['*'] * self.state + [' '] * (self.max - self.state))
        print '[{0}] {1}/{2}'.format(stars, self.state, self.max),
        self._carriage_return()

    def update(self, value=None):
        if not value is None:
            self.state = value
        self._display()

spb = SimpleProgressBar(10)
for i in range(0, 11):
    time.sleep(.3)
    spb.update(i)

rawDescription: progress bar for reading lines in text file

description: Progress bar for reading lines in text file


code:
import csv
with open("Schoology_Unique_IDs.csv","rbU") as f1, open("Schoology_Unique_IDs.csv","rbU") as f2:
    r1 = csv.reader(f1)
    r1.next()
    d = {r[1]:r[0] for r in r1}
    r2 = csv.reader(f2)
    print "\n".join([d[i[3]] for i in r2 if i[3] in d])

rawDescription: Need to match values from two different columns and then print out values from a corresponding column


code:
from bs4 import BeautifulSoup

data = """

    Span's text
    link
    B's text

"""
soup = BeautifulSoup(data)

span_texts = [span.text for span in soup.find_all('span')]
a_titles = [a['title'] for a in soup.find_all('a', title=True)]
b_texts = [b.text for b in soup.select('span[class] > b')]

result = span_texts + a_titles + b_texts
print result

rawDescription: How to combine all 3 in 1 re.findall() ??(python 2.7 && Regular Expressions)

description: Combine all 3 in 1 re.findall() ??(python 2.7 && Regular Expressions)


code:
import json

#-----

json_str = '''{"total":100,"items":{"name":"Scarf", "current":{"price":"122.5"}}}'''

data = json.loads(json_str)

if 'name' in data['items'] and data['items']['name'].lower() == 'scarf':
    print 'There is Scarf'
else:
    print 'There is NO Scarf'

#-----

json_str = '''{"total":100,"items":{}}'''

data = json.loads(json_str)

if 'name' in data['items'] and data['items']['name'].lower() == 'scarf':
    print 'There is Scarf'
else:
    print 'There is NO Scarf'

rawDescription: check if json parent value is empty

description: Check if json parent value is empty


code:
class MetaClass(type):
    def __new__(cls, name, bases, dict_):
        print "I am {} called with {}, {}, {}".format(cls, name, bases, dict_)
        return type.__new__(cls, name, bases, dict_)

class MetaObject(object):
    def __call__(self, name, bases, dict_):
        print "I am {} called with {}, {}, {}".format(self, name, bases, dict_)
        return type(name, bases, dict_)

class MyClass(object):
    __metaclass__ = MetaClass

class MyClass2(object):
    __metaclass__ = MetaObject()

rawDescription: Why is my metaclass not behaving as expected


code:
method = client.wsdl.services[0].ports[0].methods["GetCitiesByCountry"]
params = method.binding.input.param_defs(method)
print params

rawDescription: How to extract method using Suds in Python

description: Extract method using Suds in Python


rawDescription: how to re-order bytes in a python hex string and convert to long

description: Re-order bytes in a python hex string and convert to long


code: print list(kmer_count(k,*args))

rawDescription: Python find kmers in large lists

description: Python find kmers in large lists


code:
import json

file_info = ['{"file":"file1", "directory":"dir1"}', '{"file":"file2", "directory":"directory2"}']

for item in file_info:
    item = json.loads(item)
    print item['file'], item['directory']

rawDescription: Extract values from within strings in a list - python


code:
from email.header import Header
print Header('主題', 'utf-8').encode()

rawDescription: Encoding mail subject (SMTP) in Python with non-ASCII characters


code:
from ctypes import *

# Define some symbols
SQLITE_DELETE =  9
SQLITE_INSERT = 18
SQLITE_UPDATE = 23

# Define our callback function
#
# 'user_data' will be the third param passed to sqlite3_update_hook
# 'operation' will be one of: SQLITE_DELETE, SQLITE_INSERT, or SQLITE_UPDATE
# 'db name' will be the name of the affected database
# 'table_name' will be the name of the affected table
# 'row_id' will be the ID of the affected row
def callback(user_data, operation, db_name, table_name, row_id):
    if operation == SQLITE_DELETE:
        optext = 'Deleted row'
    elif operation == SQLITE_INSERT:
        optext = 'Inserted row'
    elif operation == SQLITE_UPDATE:
        optext = 'Updated row'
    else:
        optext = 'Unknown operation on row'
    s = '%s %ld of table "%s" in database "%s"' % (optext, row_id, table_name, db_name)
    print s

# Translate into a ctypes callback
c_callback = CFUNCTYPE(c_void_p, c_void_p, c_int, c_char_p, c_char_p, c_int64)(callback)

# Load sqlite3
dll = CDLL('libsqlite3.so')

# Holds a pointer to the database connection
db = c_void_p()

# Open a connection to 'test.db'
dll.sqlite3_open('test.db', byref(db))

# Register callback
dll.sqlite3_update_hook(db, c_callback, None)

# Create a variable to hold error messages
err = c_char_p()

# Now execute some SQL
dll.sqlite3_exec(db, 'create table foo (id int, name varchar(255))', None, None, byref(err))
if err:
    print err.value
dll.sqlite3_exec(db, 'insert into foo values (1, "Bob")', None, None, byref(err))
if err:
    print err.value

rawDescription: SQLite Data Change Notification Callbacks in Python or Bash or CLI


code:
Random = pp.Group(pp.Keyword("?") + pp.OneOrMore(Modifier))("Random*")
Phrase = pp.Group(Token + pp.OneOrMore(Modifier))("Phrase*")
Collection = pp.Group(pp.delimitedList(Phrase ^ Random, ","))("Collection")
tree = Collection.parseString("hello 12 2, ? 1 2, word 4, ? 3 4, testing 5")
print tree.dump()

rawDescription: Iterating through a pyparsing tree yields only unique items?


code:
from urllib2 import urlopen
from lxml import etree
doc = etree.HTML(urlopen('http://pastebin.com/download.php?i=cg5HHJ6x').read())

for t in doc.xpath('//table/tr/td/table[.//div[@id="title"] and .//td[@class="text"]]'):
    print etree.tostring(t.xpath('.//div[@id="title"]')[0])
    print etree.tostring(t.xpath('.//td[@class="text"]')[0])
    print "--"

rawDescription: Manipulating list from lxml xpath queries


code:
def a(n):
  if n == 0: return 1
  return 1 - 1 / float(a(n-1) + 3)

# limit is sqrt(3) - 1
limit = 3.0 ** 0.5 - 1.0

# get 9 digits' precision
i = 0
while abs(a(i) - limit) > 1.0e-9:
  i += 1

print i

rawDescription: Recurrence sequence in Java / Python / Mathematica


code:
import urllib2
from bs4 import BeautifulSoup

mynumber = '1234567890'
url = "http://www.nccptrai.gov.in/nccpregistry/saveSearchSub.misc?phoneno=" + mynumber

soup = BeautifulSoup(urllib2.urlopen(url))

header = soup.find('td', class_='GridHeader')

result = []
for row in header.parent.find_next_siblings('tr'):
    cells = row.find_all('td')
    try:
        result.append(cells[2].get_text(strip=True))
    except IndexError:
        continue
print result

rawDescription: extract info from a string

description: Extract info from a string


code:
worksheet = workbook.sheet_by_name('Sheet1')
num_rows = worksheet.nrows - 1
curr_row = -1
while curr_row < num_rows:
    curr_row += 1
    row = worksheet.row(curr_row)
    print row

rawDescription: Very basic Python to Excel how to

description: Very basic Python to Excel how to


code:
lists = collections.defaultdict(list)
for n in nodes:
    lists[n.parentid] += [n.name, lists[n.id]]
print lists[0]

rawDescription: Make a python nested list for use in Django's unordered_list


code:
# Extra credit:
# use Dict to auto-define named groups using each '@x' as a key
parser = LBRACK + Dict(OneOrMore(key_value)) + RBRACK
result = parser.parseString(text)

# print the parsed keys
print result.keys()

# print a value for a particular key
print result['c']

# print a value for a particular key using object notation
print result.b

# dump out the whole structure to see just what we got
print result.dump()

rawDescription: Pyparsing sequence of repeating pattern

description: Pyparsing sequence of repeating pattern


code:
from multiprocessing import Pool
import numpy

numToFactor = 976

def isFactor(x):
    result = None
    div = (numToFactor / x)
    if div*x == numToFactor:
        result = (x,div)
    return result

if __name__ == '__main__':
    pool = Pool(processes=4)
    possibleFactors = range(1,int(numpy.floor(numpy.sqrt(numToFactor)))+1)
    print 'Checking ', possibleFactors
    result = pool.map(isFactor, possibleFactors)
    cleaned = [x for x in result if not x is None]
    print 'Factors are', cleaned

rawDescription: Parallel Processing in python

description: Parallel Processing in python


code:
class Base:
  @classmethod
  def registersub(cls, subcls):
    """ Decorator to register subclass types. """
    # replace occurrence(s) of subclass name in 'subs' with the subclass itself
    while subcls.__name__ in cls.subs:
        cls.subs[cls.subs.index(subcls.__name__)] = subcls
    return cls

  subs = ['Sub3','Sub1']  # some subclass names in some special order

@Base.registersub
class Sub1(Base): pass
@Base.registersub
class Sub2(Base): pass
@Base.registersub
class Sub3(Base): pass

print Base.subs
# [<class __main__.Sub3>, <class __main__.Sub1>]

rawDescription: Forward declaration of class in Python

description: Forward declaration of class in Python


code:
from lxml.etree import fromstring, tostring

text_tree = """
<article>
 <pages>
   <list-item>content of page 1</list-item>
   <list-item>content of page 2</list-item>
   <list-item>content of page 3</list-item>
 </pages>
</article>
"""

pages = fromstring(text_tree).find('pages')
pages.tag = 'text'
for list_item in pages.findall('list-item'):
    list_item.tag = 'page'

print tostring(pages)

rawDescription: lxml: Copy-and-Rename a bunch of Elements

description: Lxml: Copy-and-Rename a bunch of Elements


code:
import re

data = "12:05:12.121      O:CLASS (SMS:/xxx.xxx@xxx.xx) R:VOICE/1654354 mid:4312"
result = re.search(r'''
      (?P<time>.*?)
      \s+
      .*?
      \s+
      \(
          (?P<type>.*?):/(?P<source>.*?)
      \)
      \s+
      .*/(?P<destination>\d+)
      \s+
      mid:(?P<mid>\d+)
''', data, re.VERBOSE)

print result.groupdict()

rawDescription: regex find what's after slash without showing the slash

description: Regex find what's after slash without showing the slash


code:
registry = {}

class RegisteringType(type):
    def __init__(cls, name, bases, attrs):
        for key, val in attrs.iteritems():
            properties = getattr(val, 'register', None)
            if properties is not None:
                registry['%s.%s' % (name, key)] = properties

def register(*args):
    def decorator(f):
        f.register = tuple(args)
        return f
    return decorator

class MyClass(object):
    __metaclass__ = RegisteringType
    @register('prop1','prop2')
    def my_method( arg1,arg2 ):
        pass

    @register('prop3','prop4')
    def my_other_method( arg1,arg2 ):
        pass

print registry

rawDescription: Auto-register class methods using decorator


code:
def group(data):
    data = sorted(data)
    it = iter(data)
    a, b = next(it)
    for c, d in it:
        if b >= c:  # Use `if b > c` if you want (1,2), (2,3) not to be
                    # treated as intersection.
            b = max(b, d)
        else:
            yield a, b
            a, b = c, d
    yield a, b

with open('file') as f:
    data = [map(int, line.split()) for line in f]

for a, b in group(data):
    print a, b

rawDescription: Python find continuous interesctions of intervals


code: print json.dumps(u'\xc2\xa9')

rawDescription: Convert Unicode string to UTF-8, and then to JSON


code: print old_im.mode  # This should print "RGBA" if it has an alpha channel.

rawDescription: Python: Combining images using paste with overlapping pixels and areas with alpha channel=0


code:
from collections import Counter

counts = Counter(lst)

for word, count in counts.most_common():
    print '{} occurs {} times'.format(word, count)

rawDescription: I have a list in python which contains many names, I want to print out how many times each name occurs


code:
from pyparsing import Literal,CaselessLiteral,Word,Combine,Group,Optional,\
    ZeroOrMore,Forward,nums,alphas
from numpy import *
import operator

exprStack = []

def pushFirst( strg, loc, toks ):
    exprStack.append( toks[0] )
def pushUMinus( strg, loc, toks ):
    if toks and toks[0]=='-':
        exprStack.append( 'unary -' )
        #~ exprStack.append( '-1' )
        #~ exprStack.append( '*' )

bnf = None
def BNF():
    """
    expop   :: '^'
    multop  :: '*' | '/'
    addop   :: '+' | '-'
    integer :: ['+' | '-'] '0'..'9'+
    atom    :: PI | E | real | T | fn '(' expr ')' | '(' expr ')'
    factor  :: atom [ expop factor ]*
    term    :: factor [ multop factor ]*
    expr    :: term [ addop term ]*
    """
    global bnf
    if not bnf:
        point = Literal( "." )
        e     = CaselessLiteral( "E" )
        fnumber = Combine( Word( "+-"+nums, nums ) +
                           Optional( point + Optional( Word( nums ) ) ) +
                           Optional( e + Word( "+-"+nums, nums ) ) )
        ident = Word(alphas, alphas+nums+"_$")

        plus  = Literal( "+" )
        minus = Literal( "-" )
        mult  = Literal( "*" )
        div   = Literal( "/" )
        lpar  = Literal( "(" ).suppress()
        rpar  = Literal( ")" ).suppress()
        addop  = plus | minus
        multop = mult | div
        expop = Literal( "^" )
        pi    = CaselessLiteral( "PI" )
        t     = CaselessLiteral( "T" )

        expr = Forward()
        atom = (Optional("-") + ( pi | e | t | fnumber | ident + lpar + expr + rpar ).setParseAction( pushFirst ) | ( lpar + expr.suppress() + rpar )).setParseAction(pushUMinus)

        # by defining exponentiation as "atom [ ^ factor ]..." instead of "atom [ ^ atom ]...", we get right-to-left exponents, instead of left-to-righ
        # that is, 2^3^2 = 2^(3^2), not (2^3)^2.
        factor = Forward()
        factor << atom + ZeroOrMore( ( expop + factor ).setParseAction( pushFirst ) )

        term = factor + ZeroOrMore( ( multop + factor ).setParseAction( pushFirst ) )
        expr << term + ZeroOrMore( ( addop + term ).setParseAction( pushFirst ) )
        bnf = expr
    return bnf

# map operator symbols to corresponding arithmetic operations
epsilon = 1e-12
opn = { "+" : operator.add,
        "-" : operator.sub,
        "*" : operator.mul,
        "/" : operator.truediv,
        "^" : operator.pow }
fn  = { "sin" : sin,
        "cos" : cos,
        "tan" : tan,
        "abs" : abs,
        "trunc" : lambda a: int(a),
        "round" : round,
        "sgn" : lambda a: abs(a)>epsilon and cmp(a,0) or 0}

def evaluateStack( s ):
    op = s.pop()
    if op == 'unary -':
        return -evaluateStack( s )
    if op in "+-*/^":
        op2 = evaluateStack( s )
        op1 = evaluateStack( s )
        return opn[op]( op1, op2 )
    elif op == "PI":
        return math.pi # 3.1415926535
    elif op == "E":
        return math.e  # 2.718281828
    elif op == "T":
        return linspace(-4 * pi, 4 * pi, 200)
    elif op in fn:
        return fn[op]( evaluateStack( s ) )
    elif op[0].isalpha():
        return 0
    else:
        return float( op )

def compute(s):
    global exprStack
    exprStack = []
    results = BNF().parseString( s )
    val = evaluateStack( exprStack[:] )
    return val

fun=raw_input("Please input a function to be evaluated: ")
print compute(fun)

rawDescription: Input function from user matplotlib in python


code:
def f():
 print "Worker"
dis.dis(f)

rawDescription: Why there is space in the code example in python threading


code:
import override
print "(o.o)" * 10
import sys
sys.stdout.toggle(False)
print "(o.o)" * 10

rawDescription: Is there any way to hack IDLE (or Python itself) to cut off large prints when working with large text files?


code:
from abc import ABCMeta
from functools import wraps

def virtualmethod(method):
    method.__isabstractmethod__ = True
    @wraps(method)
    def wrapper(self, *args, **kwargs):
        return getattr(self, method.__name__)(*args, **kwargs)
    return wrapper

class IBase(object):
    __metaclass__ = ABCMeta
    @virtualmethod
    def my_method(self, x, y):
        pass

class AddImpl(IBase):
    def my_method(self, x, y):
        return x + y

class MulImpl(IBase):
    def my_method(self, x, y):
        return x * y

items = [AddImpl(), MulImpl()]

for each in items:
    print IBase.my_method(each, 3, 4)

b = IBase()  #  <-- crash

rawDescription: Polymorphism with callables in Python

description: Polymorphism with callables in Python


code:
from collections import Counter

a =[('Volozhyn AI', 2007),
 ('Lam KL', 2010),
 ('Boudreau NG', 2006),
 ('Tsuchitani M', 1997),
 ('Zheng LP', 1997)]

b = (Counter(i[1] for i in a)).items()
print b

rawDescription: Python counting in a tuple list

description: Python counting in a tuple list


code:
from dateutil import rrule
from datetime import datetime, timedelta

now = datetime.now()
tenYearsLater = now + timedelta(years=10)

for dt in rrule.rrule(rrule.DAILY, dtstart=now, until=tenYearsLater):
    print dt

rawDescription: How to set wxpython Grid top headers?

description: Set wxpython Grid top headers


code:
import lxml.etree as ET

root = ET.fromstring(statsTF2)
for text in root.xpath('./stats/stat[name[contains(text(), ".accum.iPlayTime")]]/value/text()'):
    print text

rawDescription: Extract text value in xml using python

description: Extract text value in xml using python


code:
import re
from functools import partial
replacements = (
    partial(re.compile(r'\s*#define ([A-Z_]+) (.*)').sub, r'\1 = \2'),
    partial(re.compile(r'\s*/\*\s*(.*?)\s*\*/').sub, r'# \1'),
)
for line in inputsequence:
    for transform in replacements:
        line = transform(line)
    print line

rawDescription: converting include file in C to python

description: Converting include file in C to python


code:
class Foo(object):
    def MethodName():
        pass

print type(Foo()).__name__
# Foo

rawDescription: How to get class name when there is attribute attached?

description: Get class name when there is attribute attached


code:
import networkx as nx
from networkx.algorithms.cycles import simple_cycles
g = nx.DiGraph()
g.add_edges_from([(28, 36), (17, 43), (36, 28), (43, 69), (69, 17), (8, 94)])
print list(simple_cycles(g))

rawDescription: Search for loops of linked numbers

description: Search for loops of linked numbers


code:
from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np

lectures = ["this is some food", "this is some drink"]
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(lectures)
indices = np.argsort(vectorizer.idf_)[::-1]
features = vectorizer.get_feature_names()
top_n = 2
top_features = [features[i] for i in indices[:top_n]]
print top_features

rawDescription: How to see top n entries of term-document matrix after tfidf in scikit-learn

description: See top n entries of term-document matrix after tfidf in scikit-learn


code:
def test_all(data, expected):
    return all( x in expected for x in data )

print df_list['b'].apply(lambda x:test_all(x,[2,3,8]) )
print df_list[ df_list['b'].apply(lambda x:test_all(x,[2,3,8]) ) ]

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


code:
import threading
import gc

mydata = threading.local()

class x:
    def __del__(self):
        print "x got deleted!"

def run():
    mydata.foo = x()

t = threading.Thread(target=run)
print "t created"
gc.collect()
t.start()
print "t started"
gc.collect()
del mydata
print "mydata deleted"
gc.collect()
t.join()
print "t joined"
gc.collect()
print "Done!"

rawDescription: What's the life-time of a thread-local value in Python?


code:
from bs4 import BeautifulSoup

html = """
Element Values

01 = 01<o:p></o:p>

02<o:p></o:p>

20[true]

<o:p> </o:p>

Element Notes
"""

soup = BeautifulSoup(html)
elements = soup.find_all(name='span', attrs={'style' : 'mso-no-proof:yes'})
print ','.join(e.text for e in elements)

rawDescription: Python scraping from a complex HTML file


code:
import argparse

parser= argparse.ArgumentParser(prog='Test', usage='%(prog)s -f Filename Option1 Option2 ')
parser.add_argument ('-f', '--file', nargs=3, action='append')

files = parser.parse_args().file

for f in files:
    print tuple(f)

rawDescription: Getting command line arguments as tuples in python


code:
list = ['one' , 'two' , 'three']
newlist = list
list.append('four')
print newlist

rawDescription: Why do copied dictionaries point to the same directory but lists don't?


code:
import timeit
import numpy as np

L = range(1000)
M = np.arange(1000)

def f0():
    sum([val**2 for val in L])

def f1():
    total = 0
    for val in L:
        total+=val**2

def f2():
    np.sum(M*M)

print timeit.timeit("f0()", setup="from __main__ import f0, f1, f2, L, M", number=100)
print timeit.timeit("f1()", setup="from __main__ import f0, f1, f2, L, M", number=100)
print timeit.timeit("f2()", setup="from __main__ import f0, f1, f2, L, M", number=100)

# 0.015289068222
# 0.00959897041321
# 0.000958919525146

rawDescription: looping for a sum versus summing a list comprehension

description: Looping for a sum versus summing a list comprehension


code:
routes = [[967]]
finished_routes = []
for n in range(20):
    routes, finished_routes = add_steps(routes, finished_routes)
    if not routes:
        print finished_routes
        break

rawDescription: Python compare list and store in new variable


code:
from itertools import groupby

with open('auth.log') as myAuthlog:
    myAuthlog = (line for line in myAuthlog if "Failed password for" in line)
    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


rawDescription: storing record arrays in object arrays

description: Storing record arrays in object arrays


code: print df.to_html(float_format=lambda x: '%10.2f' % x)

rawDescription: format output data in pandas to_html

description: Format output data in pandas to_html


code:
try:
    pos = my_list.index('pear')
    print my_list[pos + 1]
    # orange
except IndexError as e:
    pass # original value didn't exist or was end of list, so what's +1 mean?

rawDescription: Is there a way to get the next item in a list by knowing the current item's value?


code:
import os
import sys,time
import socket

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", port))
server_socket.listen(5)

client_socket, address = server_socket.accept()
print "Conencted to - ",address,"\n"

while (files):
    print '\nMaybe, pending work'
    for au in files:
        if (au.find('d')>-1):
            os.chdir(au)
            imgFiles = os.listdir(au)
            if (imgFiles[i].endswith('.jpg')):
                imgs=imgFiles[i]
                file_name = open(imgs,'rb')
                print file_name
                while True:
                    strng = file_name.read()
                    client_socket.sendall(strng)
                    if not strng:  break
                file_name.close()
                #client_socket.send("done")
        else:
            time.sleep(2)
time.sleep(5)
exit()

rawDescription: How do I move files from mobile SD card to PC using python scripts?

description: Move files from mobile SD card to PC using python scripts


code:
a2 = sm.add_constant(a)
model = sm.RLM(b, a2, M=sm.robust.norms.HuberT()).fit()
model.params
print model.summary()

rawDescription: Python statsmodels return values missing


code:
import sys
import os
import socket
import uuid
import time

# Options
TASKS_DIR = "."

def main():

    sessionId = str(uuid.uuid4())

    print 'Session ID: '+ sessionId
    sys.stdout.write ('
Send test command')
    sys.stdout.flush()

    address = os.path.join(TASKS_DIR, '%s.socket' % sessionId)

    sock = socket.socket(socket.AF_UNIX)
    sock.setblocking(0)
    sock.settimeout(.1)
    sock.bind(address)
    sock.listen(1)

    fakeTasks = [foo_task, foo_task, foo_task]

    try:
        for task in fakeTasks:

            # pretend we started a task
            runningTask = task()
            # runningTask = Thread(target=task)
            # runningTask.start()

            # while runningTask.is_alive():
            while runningTask:
                conn = None
                try:
                    conn, addr = sock.accept()
                    data = conn.recv(100).strip()

                except socket.timeout:
                    # nothing ready from a client
                    continue

                except socket.error, e:
                    print "
Connection Error from client"

                else:
                    print "
"+ data
                    sys.stdout.flush()
                    conn.close()

                    # for the thread version, you will need some
                    # approach to kill or interrupt it.
                    # This is just simulating.
                    if data == "CANCEL":
                        # temp way to cancel our task
                        print "
Cancelling current task."
                        runningTask = False

                    elif data == "QUIT":
                        print "
Quitting entire process."
                        runningTask = False
                        fakeTasks[:] = []

                finally:
                    if conn:
                        conn.close()

    finally:
        sock.close()
        os.remove(address)

def foo_task():
    print 'foo task'
    return True

if __name__ == '__main__':
    sys.stdout.write("Content-type:text/html;charset=utf-8\r\n\r\n")
    sys.stdout.write('<!DOCTYPE html>\n<title>Test</title>')

    main()

    print ''
    sys.exit()

rawDescription: Python - User input to CGI via. Threading and reading file


rawDescription: Summing Attribute in a list of Python Objects


code: print re.findall('[a-zA-Z]+|\\d+', "my t0kens")

rawDescription: Separate number/letter tokens in Python

description: Separate number/letter tokens in Python


code:
for k, v in {k:v for l in y for k, v in l.iteritems()}.iteritems():
    print k, v

rawDescription: Iterate over a list of objects getting ke/value pairs


code: models=[pd.ols(y=df[ref_col],x=df[col]) for col in df if col<>ref_col]

rawDescription: Pairwise regressions in Pandas

description: Pairwise regressions in Pandas


code:
statutes={}
pat=re.compile(r'^(\d+\w+-\d+-\d+(?:[,.\-0-9A-Z]+)?[ \t]+.*?(?=\n\n|\n+\Z|\Z))', re.S | re.M)
for block in pat.finditer(txt):
    m=re.search(r'^.*(Superseded|Repealed|Transferred|Obsolete|Reserved|Rejected|Omitted|Not|Executed)', block.group(1))
    if m:
        statutes.setdefault(m.group(1), []).append(block.group(1))
    else:
        statutes.setdefault('Enacted', []).append(block.group(1))

for status in sorted(statutes):
    print '{} ============\n{}\n'.format(status, '\n\n'.join(statutes[status]))

rawDescription: At a standstill parsing some statutes with regex


code:
from sqlalchemy import Table
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.expression import Executable, ClauseElement

class CreateView(Executable, ClauseElement):
    def __init__(self, name, select):
        self.name = name
        self.select = select

@compiles(CreateView)
def visit_create_view(element, compiler, **kw):
    return "CREATE VIEW %s AS %s" % (
         element.name,
         compiler.process(element.select, literal_binds=True)
         )

# test data
from sqlalchemy import MetaData, Column, Integer
from sqlalchemy.engine import create_engine
engine = create_engine('sqlite://')
metadata = MetaData(engine)
t = Table('t',
          metadata,
          Column('id', Integer, primary_key=True),
          Column('number', Integer))
t.create()
engine.execute(t.insert().values(id=1, number=3))
engine.execute(t.insert().values(id=9, number=-3))

# create view
createview = CreateView('viewname', t.select().where(t.c.id>5))
engine.execute(createview)

# reflect view and print result
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:
from BeautifulSoup import BeautifulSoup

html = '.....	\
        .......	\
        .....	'

soup = BeautifulSoup(html)
results = soup.findAll("td", {"valign" : "top"})

for result in results :
    if len(result.attrs) == 1 :
        print result

rawDescription: How to find tags with only certain attributes - BeautifulSoup

description: Find tags with only certain attributes - BeautifulSoup


rawDescription: Advanced string parsing in python

description: Advanced string parsing in python


code:
from itertools import count
a = count(1)

next(a)
next(a)
print next(a)

rawDescription: How to print from itertools count object?

description: Print from itertools count object


code:
from fabric.api import task, execute, run, runs_once

@task
def workhorse():
    return run("get my infos")

@task
@runs_once
def go():
    results = execute(workhorse)
    print results

rawDescription: How can I collect fabric task output and print a summary for multiple hosts?

description: Collect fabric task output and print a summary for multiple hosts


code:
import win32api, win32con
import time
import win32com.client
import Tkinter

def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

shell = win32com.client.Dispatch("WScript.Shell")

master = Tkinter.Tk()
Tkinter.Label(master, text="Starting Frequency").grid(row=0)
Tkinter.Label(master, text="Ending Frequency").grid(row=1)
Tkinter.Label(master, text="Increment").grid(row=2)

line1 = Tkinter.Entry(master)
line2 = Tkinter.Entry(master)
line3 = Tkinter.Entry(master)

line1.grid(row=0, column=1)
line2.grid(row=1, column=1)
line3.grid(row=2, column=1)

def callback():

    global line1, line2, line3, master, lowf, highf, step
    inputlowf = line1.get()
    inputhighf = line2.get()
    inputstep = line3.get()

    try:

        lowf = int(float(inputlowf) * 10)
        highf = int(float(inputhighf) * 10)
        step = int(float(inputstep) * 10)

        print lowf
        print highf
        print step

        master.destroy()

    except ValueError:

        print "Error with input"

myButton = Tkinter.Button(master, text='OK', command=callback).grid(row=3, column=1, sticky=Tkinter.W, pady=4)

Tkinter.mainloop()

rawDescription: Tkinter get() function not saving values as float or int variables


code:
#!/usr/bin/env python

import sys
import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('image.jpg',0)
print img.shape
h, w = img.shape[:2]

# Drop top and bottom area of image with black parts.
img= img[100:h-100, :]
h, w = img.shape[:2]

# Threshold image
ret,th1 = cv2.threshold(img,50,255,cv2.THRESH_BINARY)

# get rid of thinner lines
kernel = np.ones((5,5),np.uint8)
th1 = cv2.dilate(th1,kernel,iterations = 3)

# Determine contour of all blobs found
_, contours0, hierarchy = cv2.findContours( th1.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
contours = [cv2.approxPolyDP(cnt, 3, True) for cnt in contours0]

# Draw all contours
vis = np.zeros((h, w, 3), np.uint8)
cv2.drawContours( vis, contours, -1, (128,255,255), 3, cv2.LINE_AA)

# Draw the contour with maximum perimeter (omitting the first contour which is outer boundary of image
# Not necessary in this case
vis2 = np.zeros((h, w, 3), np.uint8)
perimeter=[]
for cnt in contours[1:]:
    perimeter.append(cv2.arcLength(cnt,True))
print perimeter
print max(perimeter)
maxindex= perimeter.index(max(perimeter))
print maxindex

cv2.drawContours( vis2, contours, maxindex +1, (255,0,0), -1)

# Show all images
titles = ['Original Image','Threshold','Contours', 'Result']
images=[img, th1, vis, vis2]
for i in xrange(4):
    plt.subplot(2,2,i+1)
    plt.imshow(images[i],'gray')
    plt.title(titles[i]), plt.xticks([]), plt.yticks([])
plt.show()

rawDescription: Longest line detection - python - opencv


code:
from scapy.all import *
import tempfile
import sys

if __name__=="__main__":
  ftmp = tempfile.NamedTemporaryFile(delete=True)
  ftmp.write(sys.stdin.read())
  ftmp.flush()
  print rdpcap(ftmp.name)
  ftmp.close()

rawDescription: Reading .pcap file from STDIN in Python Scapy


code:
print NA & NA
print NA & True
print NA & False
print NA | True
print NA | False
print NA | NA
print NA ^ True
print NA ^ NA
if NA: print 3
if NA & False: print 3
if NA | True: print 3

rawDescription: How to do boolean algebra on missing values?

description: Do boolean algebra on missing values


code:
from pathos.multiprocessing import ProcessingPool as Pool
def simFunction(x,y):
  import time
  time.sleep(2)
  return x**2 + y

x,y = range(100),range(-100,100,2)
res = Pool().imap(simFunction, x,y)
with open('results.txt', 'w') as out:
  for i in x:
    out.write("%s\n" % res.next())
    if i%10 is 0:
      print "%s of %s simulated" % (i, len(x))

rawDescription: Python multiprocessing - tracking the process of pool.map operation


code:
import os
import subprocess as subp
p=subp.Popen('cmd.exe', shell=True, stdin=subp.PIPE)
p.stdin.write("""dir
cd "\\program files"
dir
""")
p.stdin.write('exit' + os.linesep)
p.wait()
del p
print 'done'

rawDescription: How to send a list of commands using 'os.system('command')' to CMD in python

description: Send a list of commands using 'os.system('command')' to CMD in python


code:
from time import sleep
from multiprocessing import Process, Pipe

def test_proc(name, conn):
    x = 0
    while True:
            #print x
            x += 1
            conn.poll()

def main():
    proc_name= ['a', 'b', 'c']
    procs = [Process(target=test_proc, args=Pipe()) for p in proc_name]
    for p in procs:
            p.start()
    while True:
            print [(p.is_alive(), 'Pid %s' %(p.pid)) for p in procs]
            sleep(1)

main()

rawDescription: Problem with multiprocessing python 2.6

description: Problem with multiprocessing python 2.6


code:
import numpy as np
from numpy import array

ypoints = np.r_[array([0, 2.1, 2.4]),    # first dataset, 3 points
           array([0.1, 2.1, 2.9]),  # second dataset
           array([-0.1, 1.4])]      # only 2 points

xpoints = [array([0, 2, 2.5]),      # first dataset
           array([0, 2, 3]),        # second, also x coordinates are different
           array([0, 1.5])]         # the first coordinate is always 0

xp = np.hstack(xpoints)
indicator = []
for i,a in enumerate(xpoints):
    indicator.extend([i]*len(a))

indicator = np.array(indicator)

x = xp[:,None]*(indicator[:,None]==np.arange(3)).astype(int)
x = np.hstack((np.ones((xp.shape[0],1)),x))

print np.dot(np.linalg.pinv(x), ypoints)
# [ 0.01947973  0.98656987  0.98481549  0.92034684]

rawDescription: scipy smart optimize

description: Scipy smart optimize


code:
load_profile = open('output.txt', "r")
read_it = load_profile.read()
myLines = [ ]
for line in read_it.splitlines():
    for splt in line.split():
        if "./testSuites/" in splt:
            myLines.append(splt)
print myLines

rawDescription: How can I strip the first 14 characters in an list element using python?

description: Strip the first 14 characters in an list element using python


code:
#add boolean columns to df1 from df
df1['bool1'] = df['15/12/2015'] < df['15/11/2015']
df1['bool2'] = df['15/11/2015'] < df['15/10/2015']
print df1
#       15/12/2015  15/11/2015  15/10/2015  bool1  bool2
#Stock
#AA              2          11          22   True  False
#BB              4          14          33  False  False
#CC              3          12          30   True  False
#DD              5          16          40   True  False

#remove column 15/10/2015, add columns to index
df1 = df1.drop(['15/10/2015'], axis = 1)
df1 = df1.set_index(['bool1', 'bool2'], append=True)
print df1
#                   15/12/2015  15/11/2015
#Stock bool1 bool2
#AA    True  False           2          11
#BB    False False           4          14
#CC    True  False           3          12
#DD    True  False           5          16

rawDescription: Pandas loop for take row and add data back


rawDescription: Python - sys.argv special symbol (€, ¢, ₪, etc…)


code:
print df
#most_exhibitions  name  India  Netherlands  Pakistan China USA United Kingdom
#0                 Alex      5                      8     3
#1                 Bill      4                               11
#2                  Bob                   5                   1
#3                  Joe                                                      2
print df1
#  index  name                  most_exhibitions
#0     a   Bob           USA (1) Netherlands (5)
#1     b   Joe                United Kingdom (2)
#2     c  Alex  China (3) India (5) Pakistan (8)
#3     d  Bill                USA (11) India (4)
df = pd.merge(df1,df, on=['name'])
df = df.set_index('index')

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


code:
class MCLidarActions( object ):
    li = []

    tcp_send = 'Event'

    def object__updated_changed( self, info ):
        print info;
        pass;

    def _tcp_send_changed( self ):
        print( "Click" )
        for x in self.li:
            x._tcp_send_changed()

class MCUDPActions( MCLidarActions ):
    def __init__(self):
        self.li.append(self)
    def object__updated_changed( self, info ):
        pass;
    def _tcp_send_changed( self ):
        print( "Click UDP" )

class MC_uuuuuuuuuuuuuutp_Actions( MCLidarActions ):
    def __init__(self):
        self.li.append(self)
    def object__updated_changed( self, info ):
        pass;
    def _tcp_send_changed( self ):
        print( "Click _uuuuuuuuuuuuuutp_" )

M = MCLidarActions()
print 'M, instance of MCLidarActions, created ------------'
print '  executing  M._tcp_send_changed():'
M._tcp_send_changed()

a = MCUDPActions()
print '\na, instance of MCUDPActions, created ------------'
print '  executing  M._tcp_send_changed():'
M._tcp_send_changed()
print
print '  executing  a._tcp_send_changed():'
a._tcp_send_changed()

b = MCUDPActions()
print '\nb, instance of MCUDPActions, created ------------'
print '  executing  M._tcp_send_changed():'
M._tcp_send_changed()
print
print '  executing  a._tcp_send_changed():'
a._tcp_send_changed()
print
print '  executing  b._tcp_send_changed():'
b._tcp_send_changed()

v = MC_uuuuuuuuuuuuuutp_Actions()
print '\nv, instance of MC_uuuuuuuuuuuuuutp_Actions, created ------------'
print '  executing  M._tcp_send_changed():'
M._tcp_send_changed()
print
print '  executing  a._tcp_send_changed():'
a._tcp_send_changed()
print
print '  executing  b._tcp_send_changed():'
b._tcp_send_changed()
print
print '  executing  v._tcp_send_changed():'
v._tcp_send_changed()

rawDescription: Python function calling

description: Python function calling


code:
# this randomly selects the character.
character = random.choice(not_e.keys())

# this prints the powers of the randomly selected characters.
print not_e[character]

rawDescription: Find term in python dictionary

description: Find term in python dictionary


code:
bigrams={}
while True:
    print "Enter some text or enter `break` keyword to stop:"
    line = raw_input()
    if line.lower() == 'break': break
    line = line.split()
    for i,j in zip(line[:-1],line[1:]):   # Keep taking two consecutive words (bigrams) until end of line
        bigrams.setdefault((i,j),0)
        bigrams[(i,j)]+=1

rawDescription: Bigram Counter Almost Complete Python

description: Bigram Counter Almost Complete Python


code:
class A(object):
    def f():
        print 'foo'

rawDescription: Is it necessary to include __init__ as the first function everytime in a class in Python?


code:
def maximize_nonoverlapping_count(intervals):
    # sort by the end-point
    L = sorted(intervals, key=lambda (start, end): (end, (end - start)),
               reverse=True) # O(n*logn)
    iv = build_interval_tree(intervals) # O(n*log n)
    result = []
    while L: # until there are intervals left to consider
        # pop the interval with the smallest end-point, keep it in the result
        result.append(L.pop()) # O(1)
        # remove intervals that overlap with the popped interval
        overlapping_intervals = iv.pop(result[-1]) # O(log n + m)
        remove(overlapping_intervals, from_=L)
    return result

rawDescription: Python - Removing overlapping lists

description: Python - Removing overlapping lists


code:
dicts = [
 { "Annot1": 1, "NAME": "name", "TEXT": "text" },
 { "Annot1": 2, "NAME": "name", "TEXT": "text" },
 { "Annot1": 3, "NAME": "name", "TEXT": "text" },
 { "Annot1": 4, "NAME": "name", "TEXT": "text" }
]

for d in dicts:
    print "Annot{}".format(d["Annot1"])
    for item in ["NAME", "TEXT"]:
        print '{}: {}'.format(item, d[item])
    print

rawDescription: List of dictionaries: print readable output in python 2.7


code:
def get_number(element):
    re_number = re.match("(-?\d+\.?\d*)", element)

    if re_number:
        return float(re_number.group(1))
    else:
        return 1.0

def getvalues(equation):
    elements = re.findall("(-?[a-z0-9.]+)", equation)
    return [get_number(element) for element in elements]

eqn = []
eqn_no = int(raw_input("Enter the number of equations: "))

for i in range(eqn_no):
    eqn.append(getvalues(str(raw_input("Enter Equation %d: " % (i+1)))))

print

for line in eqn:
    print "|" + "  ".join(["%3.1f" % value for value in line]) + "|"

rawDescription: How to access every element of a multi-dimensional Python list through a loop?

description: Access every element of a multi-dimensional Python list through a loop


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


code: print balls.get_offsets()

rawDescription: Set properties for specific patch in matplotlib.collections.PathCollection


code:
read_file = open('inp.txt')
for line in read_file:  # reads entire contents of file
    print line
read_file.read()  # trying to read again gives an empty string
out = read_file.read()
int(out)  # empty string cannot be converted to an int
read_file.seek(0)  # moves to beginning of file
read_file.read()   # now the content can be read again

rawDescription: ValueError: invalid literal for int() with base 10: ' '


code:
print segments[2]
# 5

pos = segments.bisect(7)
print [segments[pos - 1], segments[pos]]
# [6, 8]

rawDescription: How to make a partition structure

description: Make a partition structure


code:
a_list = []
b_list = []
for line in listofinfo:
    a, b = line.split("=")
    a_list.append(a)
    b_list.append(b)

print "LHS:", ','.join(a_list)
print "RHS:", ','.join(b_list)

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


code:
score = 0
word = 'tact'
for letter in word:
    score += SCRABBLE_LETTER_VALUES[letter]
print score

rawDescription: dictionaries in python: summing the values returned from searching the keys

description: Dictionaries in python: summing the values returned from searching the keys


code:
from timeit import Timer as T
import random

def timsort(x):
    return sorted(x)

def countsort(x):
    m = max(x)
    buckets = [0] * (m+1)
    for a in x: buckets[a] += 1
    return [ b for c in ( [a] * buckets[a] for a in xrange(m+1) ) for b in c ]

for t,n in [ (1000,1000), (1000,10000), (10000,10000), (10000,100000), (100000,100000), (100000,1000000) ]:

    print 't=',t,'n=',n

    X = [ random.randint(0,t) for _ in xrange(n) ]

    t = T( lambda: countsort(X) )
    print 'count',t.timeit(number=1000)

    t = T( lambda: timsort(X) )
    print 'timso',t.timeit(number=1000)

rawDescription: Is there a more efficient way to sort a list than list.sort() in Python?


code:
for cell in set:
    print "stage1" if cell.getchildren() else "stage2"

rawDescription: how to express `there is a child node in current node` in lxml

description: Express `there is a child node in current node` in lxml


code:
def get_callable_name(X):
    """
    Returns the best available display name for the given function/callable.
    """
    print '- inside get_callable_name()'
    print '  object X arriving in get_callable_name() :\n    ',X
    f_self = getattr(X, '__self__', None) or getattr(X, 'im_self', None)
    print '  X.__call__ ==',X.__call__
    print '  X.__name__ ==',X.__name__
    print '\n  X.__self__== X.im_self ==',f_self
    print '  isinstance(%r, type)  is  %r' % (f_self,isinstance(f_self, type))
    if f_self and hasattr(X, '__name__'): # it is a method
        if isinstance(f_self, type):
            # class method
            clsname = getattr(f_self, '__qualname__', None) or f_self.__name__
            return '%s.%s' % (clsname, X.__name__)
        # bound method
        print '\n  f_self.__class__          ==',f_self.__class__
        print '  f_self.__class__.__name__ ==',f_self.__class__.__name__
        return '%s.%s' % ('t', X.__name__)
    if hasattr(X, '__call__'):
        if hasattr(X, '__name__'):
            # function, unbound method or a class with a __call__ method
            return X.__name__
        # instance of a class with a __call__ method
        return X.__class__.__name__
    raise TypeError('Unable to determine a name for %s -- '
                    'maybe it is not a callable?' % repr(X))

def obj_to_ref(obj):
    """
    Returns the path to the given object.
    """
    print '- obj arriving in obj_to_ref :\n  %r' % obj

    ref = '%s:%s' % (obj.__module__, get_callable_name(obj))

    return ref

def ref_to_obj(ref):
    """
    Returns the object pointed to by ``ref``.
    """
    print '- ref arriving in ref_to_obj == %r' % ref

    if not isinstance(ref, basestring):
        raise TypeError('References must be strings')
    if not ':' in ref:
        raise ValueError('Invalid reference')
    modulename, rest = ref.split(':', 1)

    try:
        obj = __import__(modulename)
    except ImportError:
        raise LookupError('Error resolving reference %s: '
                          'could not import module' % ref)

    print '  we start with dictionary obj == ',obj
    try:
        for name in modulename.split('.')[1:] + rest.split('.'):
            print '  object of name ',name,' searched in',obj
            obj = getattr(obj, name)
            print '  got obj ==',obj
        return obj
    except Exception:
        raise LookupError('Error resolving reference %s: '
                          'error looking up object' % ref)

class Test:
    def TestFunc(self):
        print 'this is Test::TestFunc method'

t = Test()

print 't ==',t

print '\nt.TestFunc ==',t.TestFunc

print "getattr(t,'TestFunc') ==",getattr(t,'TestFunc')

print ('\nTrying to obtain reference of t.TestFunc\n'
       '----------------------------------------')

print '- REF = obj_to_ref(t.TestFunc)  done'
REF = obj_to_ref(t.TestFunc)
print '\n- REF obtained: %r' % REF

print ("\n\nVerifying what is ref_to_obj(REF)\n"
       "---------------------------------")
try:
    print '- obj2 = ref_to_obj(REF)  done'
    obj2 = ref_to_obj(REF)
    if obj2 != t.TestFunc:
        raise ValueError
except Exception:
        raise ValueError('Cannot determine the object of reference %s' % REF)
print '\n- object obtained : ',obj2

rawDescription: Textual reference of a method

description: Textual reference of a method


code:
import numpy

def dms_string_to_radians(dms_string):
    degrees = numpy.sum(numpy.fromstring(dms_string, sep='-') *
            [1.0, 1/60.0, 1/3600.0])

    return numpy.radians(degrees)

print dms_string_to_radians('47-39-28')

rawDescription: How to convert DMS to Radian with a given format : DD-MM-SS in python

description: Convert DMS to Radian with a given format : DD-MM-SS in python


rawDescription: How to Create a table with data from JSON output in Python


rawDescription: In Python, how can I naturally sort a list of alphanumeric strings such that alpha characters sort ahead of numeric characters?


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

engine = create_engine('sqlite:///')
session = sessionmaker(bind=engine)()
Base = declarative_base()

class MarkdownText(object):

    def __init__(self, text):
        self._text = text
        self._html = "<html>%s</html>" % text

    @classmethod
    def _from_db(cls, text, html):
        mt = MarkdownText(text)
        mt._html = html
        return mt

    def __composite_values__(self):
        return (self._text, self._html)

    def __str__(self):
        return self._text

    @property
    def __html__(self):
        return self._html

class Foo(Base):
    __tablename__ = 'foo'

    id = Column(Integer, primary_key=True)
    a = composite(MarkdownText._from_db,
                     Column('_text', Text),
                     Column('_html', Text))

    def __init__(self, a):
        self.a = MarkdownText(a)

    def __repr__(self):
        return '(%s)' % (self.a)

Base.metadata.create_all(engine)

session.add_all([Foo('test'), Foo('nips')])
session.commit()
x = session.query(Foo).all()
print x
print x[0].a.__html__
print x[0].a

rawDescription: How do I make compound columns with SQLAlchemy declarative?

description: Make compound columns with SQLAlchemy declarative


code:
"""
The most basic (working) Windows service possible.
Requires Mark Hammond's pywin32 package.
Most of the code was taken from a  CherryPy 2.2 example of how to set up a service
"""
import pkg_resources
import win32serviceutil
from paste.script.serve import ServeCommand as Server
import os, sys
import ConfigParser

import win32service
import win32event

SCRIPT_DIR          = os.path.abspath(os.path.dirname(__file__))
INI_FILE            = 'celeryd.ini'
SERV_SECTION        = 'celery:service'
SERV_NAME           = 'service_name'
SERV_DISPLAY_NAME   = 'service_display_name'
SERV_DESC           = 'service_description'
SERV_LOG_FILE       = 'service_logfile'
SERV_APPLICATION    = 'celeryd'
SERV_LOG_FILE_VAR   = 'CELERYD_LOG_FILE'

# Default Values
SERV_NAME_DEFAULT           = 'CeleryService'
SERV_DISPLAY_NAME_DEFAULT   = 'Celery Service'
SERV_DESC_DEFAULT           = 'WSCGI Windows Celery Service'
SERV_LOG_FILE_DEFAULT       = r'D:\logs\celery.log'

class DefaultSettings(object):
    def __init__(self):
        if SCRIPT_DIR:
            os.chdir(SCRIPT_DIR)
        # find the ini file
        self.ini = os.path.join(SCRIPT_DIR,INI_FILE)
        # create a config parser opject and populate it with the ini file
        c = ConfigParser.SafeConfigParser()
        c.read(self.ini)
        self.c = c

    def getDefaults(self):
        '''
        Check for and get the default settings
        '''
        if (
            (not self.c.has_section(SERV_SECTION)) or
            (not self.c.has_option(SERV_SECTION, SERV_NAME)) or
            (not self.c.has_option(SERV_SECTION, SERV_DISPLAY_NAME)) or
            (not self.c.has_option(SERV_SECTION, SERV_DESC)) or
            (not self.c.has_option(SERV_SECTION, SERV_LOG_FILE))
            ):
            print 'setting defaults'
            self.setDefaults()
        service_name = self.c.get(SERV_SECTION, SERV_NAME)
        service_display_name = self.c.get(SERV_SECTION, SERV_DISPLAY_NAME)
        service_description = self.c.get(SERV_SECTION, SERV_DESC)
        iniFile = self.ini
        service_logfile = self.c.get(SERV_SECTION, SERV_LOG_FILE)
        return service_name, service_display_name, service_description, iniFile, service_logfile

    def setDefaults(self):
        '''
        set and add the default setting to the ini file
        '''
        if not self.c.has_section(SERV_SECTION):
            self.c.add_section(SERV_SECTION)
        self.c.set(SERV_SECTION, SERV_NAME, SERV_NAME_DEFAULT)
        self.c.set(SERV_SECTION, SERV_DISPLAY_NAME, SERV_DISPLAY_NAME_DEFAULT)
        self.c.set(SERV_SECTION, SERV_DESC, SERV_DESC_DEFAULT)
        self.c.set(SERV_SECTION, SERV_LOG_FILE, SERV_LOG_FILE_DEFAULT)
        cfg = file(self.ini, 'wr')
        self.c.write(cfg)
        cfg.close()
        print '''
you must set the celery:service section service_name, service_display_name,
and service_description options to define the service
in the %s file
''' % self.ini
        sys.exit()

class CeleryService(win32serviceutil.ServiceFramework):
    """NT Service."""

    d = DefaultSettings()
    service_name, service_display_name, service_description, iniFile, logFile = d.getDefaults()

    _svc_name_ = service_name
    _svc_display_name_ = service_display_name
    _svc_description_ = service_description

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        # create an event that SvcDoRun can wait on and SvcStop
        # can set.
        self.stop_event = win32event.CreateEvent(None, 0, 0, None)

    def SvcDoRun(self):
        os.chdir(SCRIPT_DIR)
        s = Server(SERV_APPLICATION)
        os.environ[SERV_LOG_FILE_VAR] = self.logFile
        s.run([self.iniFile])
        win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        #win32event.SetEvent(self.stop_event)
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)
        sys.exit()

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(CeleryService)

rawDescription: How to create Celery Windows Service?

description: Create Celery Windows Service


code:
import itertools
a = [range(0,8)]*4
print tuple(itertools.product(*a))

rawDescription: Any better way to generate a tuple of all possible tuples in form of (x1,y1,x2,y2)?


code:
class Config(object):
    def __init__(self):
        self.config = ConfigParser.ConfigParser()
        self.config.read('config.py')

    def get_path(self):
        return self.config.get('templates', 'path')

if __name__ == '__main__':
    c = Config()
    print c.get_path()

rawDescription: Python ConfigParser wrapper

description: Python ConfigParser wrapper


code:
Order = list(range(len(myList)))
# Order is a list having the same number of items as myList,
# where each position's value equals its index

random.seed(SEED)
random.shuffle(Order)
# Order is now shuffled in the same order as myList;
# so each position's value equals its original index

originalList = [0]*len(myList)   # empty list, but the right length
for index,originalIndex in enumerate(Order):
    originalList[originalIndex] = myList[index]
    # copy each item back to its original index

print originalList

rawDescription: Python random sequence with seed

description: Python random sequence with seed


code:
import pandas as pd

data = [['yellow',1,'02/01/2015'],
        ['yellow',2,'04/01/2015'],
        ['green',3,'03/01/2015'],
        ['red',4,'01/01/2015']]
df = pd.DataFrame(data, columns=['colour','number','date'])
#simulate datetime column date
df['date'] = pd.to_datetime(df['date'])
#set index from columns colour and date
df = df.set_index(['colour', 'date'])
print df
#                   number
#colour date
#yellow 2015-02-01       1
#       2015-04-01       2
#green  2015-03-01       3
#red    2015-01-01       4

#set old index to variable idx
idx = df.index
print df.index

#pivot table, it doesn't work with test data
df.pivot_table(index=['number','date'])

#reindex by old multiindex
df1 = df.reindex(idx)
print df1
#                   number
#colour date
#yellow 2015-02-01       1
#       2015-04-01       2
#green  2015-03-01       3
#red    2015-01-01       4

rawDescription: How to sort pandas pivot_table based on newest date within level?

description: Sort pandas pivot_table based on newest date within level


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

rawDescription: How to make matrices in Python?

description: Make matrices in Python


code:
import hoovers as hv5
from robobrowser import RoboBrowser

browser = RoboBrowser()

hv5.testlogin(browser)

browser.open('http://subscriber.hoovers.com/H/company360/overview.htmlcompanyId=26082233')
print browser.parsed

rawDescription: Login function in separate file using RoboBrowser


code:
elements = `soup.findAll('td', attrs={X : Y})`
titlelist=[]
for elem in elements:
    titlelist.append(elem['title'])

rawDescription: Extract information within tag

description: Extract information within tag


code:
while 1: # infinite loop
    name = raw_input("Name: ")
    if name == "":
        print "Ops! Retry"
    else:
        print "Hello", name
        break  # this will break the loop

rawDescription: How to get user input within a Method (python)

description: Get user input within a Method (python)


rawDescription: delete items at the same position (index) from two synchronized lists?

description: Delete items at the same position (index) from two synchronized lists


code:
def Test(value):
    # Local Scope #1
    def innerFunc():
        # Local Scope #2
        print value
        # No immediate local in Local Scope #2 - check up the chain
        # First find value in outer function scope (Local Scope #1).
        # Use that.
    innerFunc()

def TestWithAssignment(value):
    # Local Scope #1
    def innerFunc():
        # Local Scope #2
        print value
        # Immediate local variable found in Local Scope #2.
        # No need to check up the chain.
        # However, no value has been assigned to this variable yet.
        # Throw an error.
        value = "Changed value"
    innerFunc()

rawDescription: Confusing change of scope - what's going on?


code:
for x in range(0, 100, 2):
    print x

rawDescription: How to count by twos with Python's 'range'

description: Count by twos with Python's 'range'


code:
byte_array = map(ord, str(packet)) # list of numbers, such as [0xDE, 0xAD, 0xBE, 0xEF]
print byte_array
for byte in byte_array:
    pass # do something with byte

rawDescription: saving hexdump(packet) to list in scapy

description: Saving hexdump(packet) to list in scapy


code:
rs = ','.join(result).split(',')
idx = [i for i,j in enumerate(rs) if j.strip().startswith('Mr')]

if len(idx) == 1:  # we found "Mr" in the list
    idx = idx[0]
    final_result = (rs[idx] +',' + ','.join(rs[1:idx] + rs[idx+1:])).strip()
else: # We didn't find "Mr" in the list .. adjust output to your needs
    final_result = (','.join(rs).strip())

print final_result

rawDescription: Arranging the string of a list in a required format in python


code:
a = raw_input("Enter option:")
a
int(a)
try:
  int(a)
except ValueError:
  print "Enter digit only.."

rawDescription: How do I sort scores and names saved in a .txt file into alphabetical order with python?

description: Sort scores and names saved in a .txt file into alphabetical order with python


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

rawDescription: Python: call parent instancemethod

description: Python: call parent instancemethod


rawDescription: Finding Median in 13 rows in CSV

description: Finding Median in 13 rows in CSV


code:
import time
import sys
import threading

# lock the sys.stdout stream when a thread is printing.
slowshow_lock = threading.Lock()

def slowshow(text, delay=0.2):
    slowshow_lock.acquire()
    for c in text:
        time.sleep(delay)
        sys.stdout.write(c)
    print ""
    slowshow_lock.release()

# they will show when needed
threading.Thread(target=slowshow, args=("Hello, I am Reut Sharabani!", )).start()
threading.Thread(target=slowshow, args=("Hello, I am Albus Dumbledore!",)).start()

# this should show immediately, even though we've sent text to printing!
print "VERY COMPLEX BACKGROUND CALCULATION"

rawDescription: How to code writing type python script

description: Code writing type python script


code:
import inspect
import random

class TracingDict(object):

  def __init__(self, loc, glob):
    self.loc = loc
    self.glob = glob
    self.vars = []

  def __getitem__(self, name):
    try: v = self.loc[name]
    except KeyError: v = self.glob[name]
    self.vars.append((name, v))
    return v

class Evaluator(object):

  def __init__(self):
    f = inspect.currentframe()
    f = inspect.getouterframes(f)[1][0]
    self.d = TracingDict(f.f_locals, f.f_globals)

  def __call__(self, expr):
    return eval(expr, {}, self.d)

def f(A, B, C, D, E):
  e = Evaluator()
  res = e('A or B and ((C or D and E) or F) or G and H')
  print 'R=%r from %s' % (res, e.d.vars)

for x in range(20):
  A, B, C, D, E, F, G, H = [random.randrange(2) for x in range(8)]
  f(A, B, C, D, E)

rawDescription: Python: Analyzing complex statements during execution


code:
import sys
import time

from daemon import Daemon

class YourCode(object):
    def run(self):
        while True:
            time.sleep(1)

class MyDaemon(Daemon):
    def run(self):
        # Or simply merge your code with MyDaemon.
        your_code = YourCode()
        your_code.run()

if __name__ == "__main__":
    daemon = MyDaemon('/tmp/daemon-example.pid')
    if len(sys.argv) == 2:
        if 'start' == sys.argv[1]:
            daemon.start()
        elif 'stop' == sys.argv[1]:
            daemon.stop()
        elif 'restart' == sys.argv[1]:
            daemon.restart()
        else:
            print "Unknown command"
            sys.exit(2)
        sys.exit(0)
    else:
        print "usage: %s start|stop|restart" % sys.argv[0]
        sys.exit(2)

rawDescription: How to make Python script run as service?

description: Make Python script run as service


code:
must = {"from", "to"}
print len(d) > len(must) and all(key in d for key in must)
# True

rawDescription: python dictionary check if any key other than given keys exist

description: Python dictionary check if any key other than given keys exist


code:
my_list = [1,2,3]
for ndx, member in enumerate(my_list):
    my_list[ndx] += 42
print my_list

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