code:
subq = self.session.query(DataMeasurement).\
    filter_by(data_source='Sensor1').subquery()
q = self.session.query(
    DataMeasurement.timestamp,
    # Use labels to distinguish between identically named columns.
    # This is optional.
    subq.c.sensor_output.label('output1'),
    DataMeasurement.sensor_output.label('output2')
).filter(
    (DataMeasurement.data_source == 'Sensor2') &
    (DataMeasurement.timestamp == subq.c.timestamp)
)

# Simply get a list of named tuples.
print q.all()
# Or access each column using properties.
for row in q:
    print row.timestamp, row.output1, row.output2

rawDescription: How can I join two queries on the same table with python / sqlalchemy?

description: Join two queries on the same table with python / sqlalchemy


code: print repr(data)

rawDescription: Manipulating binary data in Python

description: Manipulating binary data in Python


code:
from __future__ import division
from itertools import tee, izip
from collections import Counter

text = '''\
chr1 1 A 3
chr1 2 G 3
chr1 3 T 3
chr1 4 C 2
chr1 5 G 1
chr1 6 T 2
chr1 7 G 3
chr1 8 C 3
chr1 9 A 5
chr1 10 A 8
chr2 5 A 1
chr2 6 G 0
chr2 7 G 0
chr2 8 G 0
chr2 9 C 2
chr2 10 T 3
chr2 11 A 3'''

def window(iterable, size):
    iters = tee(iterable, size)
    for i in xrange(1, size):
        for each in iters[i:]:
            next(each, None)
    return izip(*iters)

def get_avg(lists, column):
    return sum(zip(*lists)[column]) / len(lists)

def get_GC_percentage(lists, column):
    counts = Counter(zip(*lists)[column])
    return (counts['C'] + counts['G']) / len(lists)

line_tuples = (line.split() for line in text.split('\n'))
line_tuples_casted = ((a,int(b),c,int(d)) for a,b,c,d in line_tuples)
line_tuples_chunks = window(line_tuples_casted, 2)

for (i,chunk) in enumerate(line_tuples_chunks):
    print 'i: {:2} | avg: {} | GC_content: {:5.0%}'.format(i, get_avg(chunk, 3), get_GC_percentage(chunk, 2))

rawDescription: python sliding window average for letters

description: Python sliding window average for letters


code:
alphabet = 'abcdef1234567890'
import re
regex = re.compile('f*[1-3]+$')
for r in all_matching_strings(alphabet, 5, regex):
    print r

rawDescription: Generating a list of values a regex COULD match in Python


code: print sys.exit.__doc__

rawDescription: Exit while loop in Python

description: Exit while loop in Python


code:
import numpy as np
a = your_array[:,None,:] # replace `your_array` with the name of your array
print a.shape
(119, 1, 80)

rawDescription: Python/Numpy - Turn an array with shape (119,80) into (119,1,80)


code:
import numpy as np
from __future__ import division  # to force division of int to float
num_walkers = 3  # change as needed
num_steps = 10   # change as needed
prob = 0.5

step_matrix = np.random.randint(2, size=(num_walkers, num_steps))
step_matrix[step_matrix < prob] = -1
print step_matrix

rawDescription: General Random Walk generator questions

description: General Random Walk generator questions


code:
import re
regex = re.compile(r"""{{.*?}}\s*('''Papori''')""", re.DOTALL)
print regex.sub(r"\1", rev_data)

rawDescription: get first paragraph from wikipedia article

description: Get first paragraph from wikipedia article


code:
def flatten(listOfLists):
    "Flatten one level of nesting"
    assert isinstance(listOfLists, list)
    if len(listOfLists) > 0:
        assert isinstance(listOfLists[0], list)
    return [x for sublist in listOfLists for x in sublist]

# sequence monad
def seq_unit(x): return [x]
def seq_bind(mval, mf): return flatten(map(mf, mval))

# Decompose ErrorT e m a
def get_value(m_val): return m_val[0]
def get_error(m_val): return m_val[1]

# hard coded "(ErrorT e) List a" instance of throwError, note that seq_unit is hardcoded
def error_throwError(err): return (None, err)
def errorT_list_throwError(err): return seq_unit(error_throwError(err))

# "(ErrorT e) List a" monad
def error_unit(x): return (x,None)
def errorT_list_unit(x): return seq_unit(error_unit(x))

def error_bind(mval, mf):
    assert isinstance(mval, tuple)
    error = get_error(mval)
    if error:
        return error_throwError(error)
    else:
        return mf(get_value(mval))

# Cannot have multi-line lambda
def errorT_list_bind_helper(mval, mf):
    assert isinstance(mval, tuple)
    error = get_error(mval)
    if error:
        return errorT_list_throwError(error)
    else:
        return mf(get_value(mval))

def errorT_list_bind(mval, mf): return seq_bind(mval, lambda v: errorT_list_bind_helper(v, mf))

# combined monad !! (ErrorT e) List a
unit = errorT_list_unit
bind = errorT_list_bind
throwError = errorT_list_throwError

# hard coded "lift :: List a -> (ErrorT e) List a"
def lift(mval):
    assert isinstance(mval, list)
    # return [ (val,None) for val in mval ]
    # return [ errorT_list_unit(val) for val in mval ]
    return seq_bind(mval, lambda v : unit(v))

def get_banks(name):
    if name == "Irek": return lift(["Bank of America", "Wells Fargo"])
    elif name == "John": return unit("PNC Bank")
    elif name == "Alex": return unit("TD Bank")
    else: return throwError("No bank associated with name %s" % name)

def get_accounts(bank, name):
    if   name == "Irek" and bank == "Bank of America": return lift([1, 2])
    elif name == "Irek" and bank == "Wells Fargo": return unit(3)
    elif name == "John" and bank == "PNC Bank": return unit(4)
    elif name == "John" and bank == "Wells Fargo": return lift([5, 6])
    elif name == "Alex" and bank == "TD Bank": return lift([7, 8])
    else: return throwError("No account associated with (%s, %s)" % (bank, name))

def get_balance(bank, account):
    if bank == "Wells Fargo":
        return throwError("Unable to get balance due to technical issue for %s: %s" % (bank, account))
    else:
        return unit(account * 35000)  #right around 200,000 depending on acct number

def get_qualified_amount(balance):
    if balance > 200000:
        return unit(balance)
    else:
        return throwError("Insufficient funds for loan, current balance is %s" % balance)

# monadic business logic
def get_loan(name):

    m_qualified_amounts = (
           bind(get_banks(name), lambda bank:
           bind(get_accounts(bank, name), lambda account:
           bind(get_balance(bank, account), lambda balance:
           bind(get_qualified_amount(balance), lambda qualified_amount:
                    unit(qualified_amount))))))

    assert isinstance(m_qualified_amounts, list)
    assert isinstance(m_qualified_amounts[0], tuple)
    return m_qualified_amounts

names = ["Irek", "John", "Alex", "Fred"]

for name, loans in zip(names, map(get_loan, names)):
    print "%s: %s" % (name, loans)

rawDescription: combining maybe and seq monads: confused at the output

description: Combining maybe and seq monads: confused at the output


code:
class Products(BaseModel):
    id = IntegerField(primary_key=True) # <-- You should specify a Primary Key
    name= CharField()
    inserted = DateTimeField(index=True) # <-- this should be an index

Products.select().where(Products.inserted >datetime(2013, 04, 03, 14, 52, 50))

rawDescription: Python orm datetime issue

description: Python orm datetime issue


code:
num = 20
li = [19,23,24,23,1,2]
li_sort = sorted(x for x in li if x > num)
print [x for x in li_sort  if x == li_sort[0]]

rawDescription: find the closest number in array that is bigger then the number

description: Find the closest number in array that is bigger then the number


code:
import numpy as np
from cffi import FFI

ffi = FFI()
ffi.cdef("void copy(float *in, float *out, int len);")
C = ffi.dlopen("ccode.dll")

a = 42*np.ones(16, dtype=np.float32)
b = np.zeros_like(a)
pa = ffi.cast("float *", a.ctypes.data)
pb = ffi.cast("float *", b.ctypes.data)

C.copy(pa, pb, len(a))
print b

rawDescription: How to pass a Numpy array into a cffi function and how to get one back out?

description: Pass a Numpy array into a cffi function and how to get one back out


code:
def print_counter(val, msg):
    print "%s[%d] %s" % (" "*val, val, msg)

def answer_solve_sudoku(__grid, counter=0):

    res = check_sudoku(__grid)
    if res is None or res is False:
        return res

    grid = copy.deepcopy(__grid)

    for row in xrange(9):
        for col in xrange(9):
            if grid[row][col] == 0:
                for n in xrange(1, 10):
                    grid[row][col] = n
                    print_counter(counter,"test: (row %d, col %d) = %d" % (row,col,n))
                    new = answer_solve_sudoku(grid, counter+1)
                    if new is not False:
                        print_counter(counter, "answer_solve_sudoku() solved: returning")
                        return new
                # backtrack
                print_counter(counter, "backtrack")
                return False

    print_counter(counter, "**SOLVED! Returning back up to top**")
    return grid

from pprint import pprint
solution = answer_solve_sudoku(easy_grid)
pprint(solution)

rawDescription: recursion solution of sudoku in python

description: Recursion solution of sudoku in python


code:
import requests
import lxml.html

# here's some ugly code I've glued together from my IPython %history:
#
# I know it's crap, It's about ~10mins from start to finish, one
# alternative would be to simply generate <img src=""> links for each of
# the images and then rely on Firefox/Chrome to save the whole page...
# this would make prettier file names, and I get the impression this
# is a one-off script...
# --Stuart

def find_images(url):

    root=lxml.html.parse(url).getroot()
    root.make_links_absolute()
    imgs = []
    for i in root.xpath('//div[contains(@id, "post_message")]//img'):
        src = i.attrib.get('src','')
        if 'project1999' not in src:
            imgs.append(i.attrib.get('src',''))
    return imgs

def main():
    nums=[x+1 for x in range(52)]
    urls=['http://www.project1999.org/forums/showthread.php?s=6be291d52837a8ab512858dde188569c&t=37779&page=%d' %num for num in nums]

    todownload = []
    for url in urls:
        for img in find_images(url):
            todownload.append(img)

    todownload = list(set(todownload)) #remove duplicates

    print "downloading %d images" % len(todownload)

    # save all the images without extensions.. (lazy)
    for count, i in enumerate(todownload):
        try:
            print "%d downloading %s" % (count, i)
            open('imgs/%d' % count, 'w').write(requests.get(i).content)
        except:
            print "couldn't download %s" %i

if __name__ == '__main__':
    main()

rawDescription: Python download every image Vbulletin

description: Python download every image Vbulletin


code:
# original
print row.xpath(".//td[@class='previous']/text()").extract()
# ported
print extract_text(row.find(r'.//td[@class="previous"]'))

rawDescription: converting scrapy to lxml

description: Converting scrapy to lxml


rawDescription: Unable to retrieve required indices from multiple NumPy arrays


code:
from collections import defaultdict

class Foo(object):
    QUIT_WORDS = ['exit', 'quit', 'stop', 'finish', 'complete', 'leave']

    def __init__(self):
        self.method_dict = defaultdict(lambda: self.do_exit)

    def __getattr__(self, attr):
        if any([attr.endswith("_{}".format(x)) for x in self.QUIT_WORDS]):
            return self.method_dict[attr]
        else:
            return super(Foo, self).__getattribute__(attr)

    def do_exit(self):
        print "Exit!"

rawDescription: elegant way of setting multiple methods to equal another method

description: Elegant way of setting multiple methods to equal another method


code:
x = 0
sum = x

while (sum <= 10):
    x = x + 2
    print x
    sum = x + 2
print ('Good bye!')  # <-- lower case, unindented

rawDescription: Please review my code

description: Please review my code


code:
with open("blurbs.csv","rb") as f:
    blurbs = list(csv.DictReader(f, delimiter="\t"))
for row in blurbs:
    print row
for row in blurbs:
    print row

rawDescription: Python: How do I use DictReader twice?

description: Python: How do I use DictReader twice


code:
for i in range(10):
   print i, i.bit_length()

rawDescription: Python find max number of combinations in binary


code:
numbers = range(1, 59+1)
random.shuffle(numbers)
first5 = numbers[:5]
bonus = random.randint(1, 35)
print first5, bonus

rawDescription: Generating Combinations NOT Permutations. How?


code:
from itertools import chain
items =  iter(sorted(a.items())) #As dicts are unordered
print ' '.join('{} - {} , {} - {}'.format(*chain(x, next(items))) for x in items)

rawDescription: How do I retrieve more than one item pair from a dictionary

description: Retrieve more than one item pair from a dictionary


code:
for k in sorted(a):
    print k, a[k] # or whatever.

rawDescription: Sorting a dictionary having keys as string of numbers in python


code: print [sum(e) for e in izip(*twin)]

rawDescription: Vector algebra in functional

description: Vector algebra in functional


rawDescription: integer gets promoted to long but not vice versa in Python. Why?

description: Integer gets promoted to long but not vice versa in Python. Why


code:
import urlparse
url = "http://127.0.0.1:8000/data/number/"
path = urlparse.urlparse(url).path
val = path.split("/")[2]
print val

rawDescription: strip string from url in Python

description: Strip string from url in Python


rawDescription: How to unpack tuples in nested list?

description: Unpack tuples in nested list


code:
import struct
import numpy as np
np.random.seed(1)
rows = 6
cols = 4

def create_matrix(rows, cols):
    data = (np.random.rand(
        rows, cols) * 100).astype('uint8')  # type for image [0 255] int8?
    return data

def save_matrix(filename, data):
    np.save(filename, data)

def load_matrix(filename):
    data= np.load(filename)
    return data

def load_npy_to_memmap(filename, dtype, shape):
    # npy format is documented here
    # https://github.com/numpy/numpy/blob/master/doc/neps/npy-format.txt
    with open(filename, 'r') as f:
        # skip magic string \x93NUMPY + 2 bytes major/minor version number
        # + 2 bytes little-endian unsigned short int
        junk, header_len = struct.unpack('<8sh', f.read(10))

    data= np.memmap(filename, dtype=dtype, shape=shape, offset=6+2+2+header_len)
    return data

def test_mult_ram():
    A = create_matrix(rows, cols)
    A[1][2] = 42
    save_matrix("A.npy", A)
    A = load_matrix("A.npy")
    print A
    B = create_matrix(cols, rows)
    save_matrix("B.npy", B)
    B = load_matrix("B.npy")
    print B

    fA = load_npy_to_memmap('A.npy', dtype='uint8', shape=(rows, cols))
    fB = load_npy_to_memmap('B.npy', dtype='uint8', shape=(cols, rows))
    print fA
    print fB
    np.testing.assert_equal(A, fA)
    np.testing.assert_equal(B, fB)

test_mult_ram()

rawDescription: numpy.memmap map to save file

description: Numpy.memmap map to save file


code:
excel_file = pd.ExcelFile('test.xlsx')
table = excel_file.parse('Sheet1', header=None)
print table
#            0           1
#0        col1        col2
#1  10-05-2011  31-05-2011
#2  05-10-2011  14-12-2011

rawDescription: Replace index of a DataFrame with another index from another DataFrame


code:
from lxml import etree

parser = etree.HTMLParser(encoding='utf8')
tree = etree.parse('source.html', parser)
print tree.xpath('//a/@href')

rawDescription: Python: Why is Xpath seemingly only processing the first element in this tree?


code:
for link in BeautifulSoup(page, parseOnlyThese=SoupStrainer('a')):
    if link.has_attr('href'):
        print link['href']

rawDescription: pls help: how to use BeautifulSoup picking up texts wanted

description: Pls help: how to use BeautifulSoup picking up texts wanted


code:
def numerize(s):
    nd = 0
    copy = s[::]
    for i  in range(len(s)):
        c = copy[i]
        if c.isdigit(): continue
        nd = nd + 1
        if nd > 9 :
            print "Too many different characters in \"%s\"." % s
            return None # here you may want to throw
        copy = copy.replace(c,repr(nd))
    return copy

print numerize('asdeaswes')

rawDescription: Converting letters to numbers and performing sequence of operations along the length in Python


code:
doc= pxdom.parseString('c')
doc.domConfig.setParameter('format-pretty-print', True)
print doc.pxdomContent

rawDescription: Python xml minidom. generate <text>Some text</text> element


code:
import httplib

try:
    a = httplib.HTTPConnection('google.com')
    a.connect()
except httplib.HTTPException as ex:
    print "not connected"

rawDescription: Python verify url goes to a page

description: Python verify url goes to a page


code: print "{} {}".format(line.split(',')[3], num)

rawDescription: Read line in file, print line if it contains string


code: print m.Runtime

rawDescription: Gurobi: Get optimization runtime

description: Gurobi: Get optimization runtime


code:
import subprocess

class sshcommand(object):
    def __init__(self, command):
        self.result = subprocess.check_output(['ssh', 'localhost', command]).splitlines()

    def __str__(self):
        return self.result

command = raw_input("command> ")
print sshcommand(command)

rawDescription: printing output python subprocess

description: Printing output python subprocess


code:
import uuid
u=uuid.uuid1()
datetime.datetime.fromtimestamp((u.time - 0x01b21dd213814000L)*100/1e9)

rawDescription: Extract the time from a UUID v1 in python


code:
# Take a sequence of values and store them in a tuple called args
def bar1(*args):
    print args[0]

# Take a tuple of values and store it in t
def bar2(t):
    print t[0]

rawDescription: Replacing Positional arguments - calling a function with or without tuple arguments


code:
a = [1,2,3] ; print a
b = a ; print b
a[1] = 42 ; print a
print b   #WTH?

rawDescription: Understanding python's name binding

description: Understanding python's name binding


code:
for value in ic_next_td.strings:
    print value

rawDescription: Regex or other method for returning these values (from BeautifulSoup)


code:
url ="http://aetoys.tumblr.com"
source = requests.get(url)
text = source.text
soup = BeautifulSoup(text)

print [ x["href"]  for div in soup.findAll('li',{'class':'has-sub'}) for x in div.find_all('a',href=True)]

['#', '#', '/onepiece_book', '/onepiece', '#', '/naruto_book', '/naruto', '#', '/bleach_book', '/bleach', '/kingdom', '/tera', '/torico', '/titan', '/seven', '/fairytail', '/soma', '/amsal', '/berserk', '/ghoul', '/kaizi', '/piando', '#', '/onepiece_book', '/onepiece', '#', '/naruto_book', '/naruto', '#', '/bleach_book', '/bleach', '#', '/conan', '/silver', '/hai', '/nise', '/hunterbyhunter', '/baku', '/unhon', '/souleater', '/liargame', '/kenichi', '/dglayman', '/magi', '/suicide', '/pedal', '#', '/dobaku', '/gisei', '/dragonball', '/hagaren', '/gantz', '/doctor', '/dunk', '/susi', '/reborn', '/airgear', '/island', '/crows', '/beelzebub', '/zzang', '/akira', '/tennis', '/kuroco', '/claymore', '/deathnote']

rawDescription: how to select specific words and put them into tuple - list?

description: Select specific words and put them into tuple - list


code:
job_IDs = ['13453', '123', '563456'];
memory_used = ['30', '150.54', '20.6'];
memory_units = ['MB', 'GB', 'MB'];

w1=1
w2=1
w3=1
w4=1
for i in range(len(job_IDs)):
    v=memory_used[i].split('.') if '.' in memory_used[i] else  (memory_used[i], '0')
    item=job_IDs[i]+':'
    units=memory_units[i]
    if len(item)>w1: w1=len(item)
    if len(v[0])>w2: w2=len(v[0])
    if len(v[1])>w3: w3=len(v[1])
    if len(units)>w4: w3=len(units)

for i in range(len(job_IDs)):
    v=memory_used[i].split('.') if '.' in memory_used[i] else  (memory_used[i], '0')
    item=job_IDs[i]+':'
    units=memory_units[i]
    print "Job {item:{w1}} {value[0]:>{w2}}.{value[1]:<{w3}} {units:{w4}}".format(
      item=item,w1=w1, w2=w2,w3=w3,w4=w4, value=v, units=units)

rawDescription: Using Python's Format Specification Mini-Language to specify or automatically determine column widths


code:
import sys
from ConfigParser import SafeConfigParser

parser = SafeConfigParser()
parser.readfp(sys.stdin)

config = dict((section, dict((option, parser.get(section, option))
                             for option in parser.options(section)))
              for section in parser.sections())
print config

rawDescription: Automatically read configuration values in Python


code:
@simple_decorator
def my_simple_logging_decorator(func):
    def you_will_never_see_this_name(*args, **kwargs):
# Added/modified code starts here
        if globalvar:
            print 'calling {}'.format(func.__name__)
# End modified code
        return func(*args, **kwargs)
    return you_will_never_see_this_name

rawDescription: On demand decorator?

description: On demand decorator


code:
import re
D = {"excerpt":"...where we\u00e2\u0080\u0099ll just have to wait and see, I\u00e2\u0080\u0099m sure official announcements with start flowing in the coming months \u2013special\u2013..."}
s = D['excerpt']
print s
s = s.decode('unicode-escape')
print s
print re.sub(ur'[\xc2-\xf4][\x80-\xbf]+',lambda m: m.group(0).encode('latin1').decode('utf8'),s)

rawDescription: converting a string which contains both utf-8 encoded bytestrings and codepoints to utf-8 encoded string

description: Converting a string which contains both utf-8 encoded bytestrings and codepoints to utf-8 encoded string


code:
from collections import defaultdict

d=defaultdict(int,{})
f = open('a.txt', 'r')
for line in f:
     if 'drops' in line:
         data=line.split()
         d[data[1]]=d.setdefault(data[1], 0)+ int(data[3])
f.close()
print d

rawDescription: Python script to generate report of packet drop


code:
import argparse
parser = argparse.ArgumentParser(description='sample parser')
arg1 = parser.add_argument('--foo')
print parser
print arg1

html = """
<form name="%(prog)s action="%(action)s" method="get">
%(description)s
<input type="text" name="%(dest)s"><br>
<input type="submit" value="Submit">
</form>
"""
d = vars(parser)
d.update(vars(arg1))
d['action'] = 'html_form.xxx'
print html%d

rawDescription: HTML forms and argparse

description: HTML forms and argparse


code:
my_timer = Stopwatch()
print my_timer.get_seconds(), "seconds have elapsed since start/reset"
my_timer.reset()

rawDescription: Resetting pygame's timer

description: Resetting pygame's timer


code:
from collections import Counter

def readwords( filename ):
    f = open(filename)
    words = [ line.rstrip() for line in f.readlines()]
    return words

positive = readwords('positive.txt')
negative = readwords('negative.txt')

paragraph = 'this is really bad and in fact awesome. really awesome.'

count = Counter(paragraph.split())

pos = 0
neg = 0
for key, val in count.iteritems():
    key = key.rstrip('.,?!\n') # removing possible punctuation signs
    if key in positive:
        pos += val
    if key in negative:
        neg += val

print pos, neg

rawDescription: How to find total number of positive and negative words from a text?

description: Find total number of positive and negative words from a text


code:
for line in sys.stdin:
    if line.strip():
        city, temperature = line.rsplit(None, 1)
        print '%s\t%s' % (city ,temperature)

rawDescription: get city and temperature values into variables to use in reducer file

description: Get city and temperature values into variables to use in reducer file


rawDescription: Merge pandas DataFrame with MultiIndex

description: Merge pandas DataFrame with MultiIndex


code:
def under_attack(col, queens):
    return col in queens or any(abs(col - x) == len(queens)-i         # (1)
                                for i,x in enumerate(queens))         # (2)

def solve(n):
    solutions = [[]]
    for row in range(n):                                              # (3)
        solutions = [solution+[i] for solution in solutions           # (4)
                                  for i in range(n)                   # (5)
                                  if not under_attack(i, solution)]   # (6)
        print solutions # remove this when using generator!           # (7)
    return solutions

rawDescription: N Queens: backtracking solution implimented by Python generator


code:
start = 0x30D681  # hex literal, gives us a regular integer
end = 0x3227FF

for i in xrange(start, end + 1):
    print format(i, 'X')

rawDescription: Incrementing (iterating) between two hex values in Python


code: print findall(r'title(?:.*?</a>){3}', text, DOTALL)

rawDescription: Regex: match text between 2 items

description: Regex: match text between 2 items


code:
comps={}
for abc in list1:
    a,b,c=abc.split(',')
    if not a in comps:
        comps[a]= [[b,c]]
    else:
        comps[a].append([b,c])

for k,v in comps.iteritems():
    print k,v

rawDescription: How can I create a dynamic dictionaries in Python using a CSV file

description: Create a dynamic dictionaries in Python using a CSV file


code:
r,c = input.shape
output = np.zeros([r, c -1])

for j in range(0, c - 1):
    output[:, j] = np.abs(input[:, j+1] - input[:, j])

print output

rawDescription: Python - sobel x derivative

description: Python - sobel x derivative


code:
thisone_pos = rr.find('JA=THISONE')
range_start = rr.rfind("JA=", 0, thisone_pos) + 3
range_end = rr.find(';', range_start)
print rr[range_start:range_end]

rawDescription: Python regex - faster search

description: Python regex - faster search


rawDescription: Python transition matrix

description: Python transition matrix


code:
for filename in os.listdir("."):
    lines = open(filename).readlines()
    for i, line in enumerate(lines):
        if "foo" in line:
            for x in lines[i-5 : i+6]:
                print x,

rawDescription: `cat filename | grep -B 5 -C 5 foo`

description: `cat filename | grep -B 5 -C 5 foo`


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

Base = declarative_base()

class SomeClass(Base):
    __tablename__ = 'sometable'

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

class TupleElement(Base):
    __tablename__ = 'tuple_data'

    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('sometable.id'), nullable=False)
    name = Column(String)
    col1 = Column(Integer)
    col2 = Column(Boolean)

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

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

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

e = create_engine('postgresql://scott:tiger@localhost/test', echo=True)
Base.metadata.drop_all(e)
Base.metadata.create_all(e)
s = Session(e)

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

s.add(collection)
s.commit()

q = s.query(SomeClass).join(SomeClass.tuple_elements)
assert q.filter(TupleElement.as_tuple == (3, True)).first() is collection
assert q.filter(TupleElement.as_tuple == (5, False)).first() is None
print s.query(TupleElement.as_tuple).all()

rawDescription: Using a tuples of values in sqlalchemy mapped collection


code:
item_iter = iter(item)
for x in i:
    for y in item_iter:
        print y
        break

I
want
this
output

rawDescription: Break out of inner loop in Python

description: Break out of inner loop in Python


code:
import re
import sys

mod_re   = re.compile(r'module\s*([^(]+)\(([^)]*)\);(.*)')
wire_re  = re.compile(r'([^[]+)\[([0-9]+)\]')

def process(line):
    # Get rid of all backslashes. You can make this more selective if you want
    clean = line.replace('\\', '')

    m = mod_re.search(clean)
    if m:
        ports = []
        mod_name, wires, remaining = m.groups()

        for wire in wires.split(','):
            wire = wire.replace(' ', '')

            m = wire_re.search(wire)
            if m:
                # Found a vector
                n = int(m.group(2))
                prev_wire, _ = ports[-1]

                # If previous port was a vector, tack on next value
                if prev_wire == m.group(1):
                    ports[-1][1][1] = n
                else:
                    ports.append((m.group(1), [n, n]))
            else:
                # Found a scalar
                ports.append((wire, None))

        # Stringify ports
        out = []
        for port in ports:
            name, val = port
            if val is None:
                out.append(name)
            else:
                start, end = val
                out.append('%s[%s:%s]' % (name, start, end))

        print 'module %s(%s); %s' % (mod_name, ', '.join(out), remaining)

f = open(sys.argv[1], 'r')
if f:
    for l in f.readlines():
        process(l)
    f.close()

rawDescription: Python - regex parsing file

description: Python - regex parsing file


code:
from lxml import etree

tree = etree.parse("filename.xml")
print tree.xpath('//text()')[0]

rawDescription: Xml with python

description: Xml with python


code:
from ID3 import ID3

mp3_filepath = r'/music/song.mp3'
id3_data = ID3(mp3_filepath)
print 'Artist:', id3_data['ARTIST']
print 'Title:', id3_data['TITLE']

rawDescription: Python scripted mp3 database, with a php front end


code:
class Geometry(object):
    uniqueID = -1
    def __init__(self):
        Geometry.uniqueID += 1
        self.id = Geometry.uniqueID

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

    def __str__(self):
        return '(%.2f, %.2f)' % (int(self.x*100)/100.0, int(self.y*100)/100.0)

    def __eq__(self, point):
        if type(point) is Point:
            if self.x == point.x and self.y == point.y:
                return True
            else:
                return False
        else:
            raise Exception("The input parameter has to be a Point object!")

    def identify(self, other):
        if type(other) is Point:
            if self.id == other.id:
                return True
            else:
                return False
        else:
            raise Exception("The input parameter has to be a Point object!")

    def distance(self, other):
        if type(other) is Point:
            x_square = (other.x - self.x) ** 2
            y_square = (other.y - self.y) ** 2
            d = (x_square + y_square) ** 0.5
            return float(d)
        else:
            raise Exception("The input parameter has to be a Point object!")

    def quadrant(self):
        output = ""
        if self.x > 0 and self.y > 0:
            output = "Quad I"
        elif self.x < 0 and self.y > 0:
            output = "Quad II"
        elif self.x < 0 and self.y < 0:
            output = "Quad III"
        elif self.x > 0 and self.y < 0:
            output = "Quad IV"
        elif self.x == 0:
            output = "Y-axis"
        elif self.y == 0:
            output = "X-axis"
        elif self.x == 0 and self.y == 0:
            output = "Origin"

        return output

def test():

    p1 = Point(0, 3)
    p2 = Point(-3, 7)
    p3 = Point(-3, 7)

    return p1, p2, p3

def collinear(p1, p2, p3):
    p1x = p1.x
    p1y = p1.y

# m1 = ?
# m2 = ?

if __name__ == "__main__":

    p1, p2, p3= test()

    #Either this
    print "p1 is " + p1.__str__()
    print "p2 is " + p2.__str__()
    print "p3 is " + p3.__str__()

    print "\n*******************\n"

    #Or this
    print p1
    print p2
    print p3

rawDescription: Python 2.7: Passing class methods to another function


code:
def text_circle(rad, ch='*'):
    xscale = 4.2

    #Maximum diameter, plus a little padding
    width = 3 + int(0.5 + xscale * rad)

    rad2 = rad ** 2
    for y in range(-rad, rad + 1):
        #Find width at this height
        x = int(0.5 + xscale * (rad2 - y ** 2) ** 0.5)
        s = ch * x
        print s.center(width)

for i in range(4, 15):
    print i
    text_circle(i, '@')

rawDescription: Printing text in form of circle

description: Printing text in form of circle


code:
import re

def name_ends_with_digit(filename):
   return re.match(r'\w*\d+.txt$', filename)

def check(filename):
    if name_ends_with_digit(filename):
        print 'match'
    else:
        print 'no match'

check('test1.txt')
check('1.txt')
check('10.txt')
check('text1.txtyyy')
check('text.txt')

rawDescription: Reading files in directory ending with digit python


code:
@app.route('/query', methods=['GET'])
def query():
    print 'query()'

rawDescription: How to handle web api by flask

description: Handle web api by flask


code:
if "WID 0.12 ;" in x:
  print x

rawDescription: matching only the first variable with the value

description: Matching only the first variable with the value


rawDescription: How to turn a Dataframe into an Array with certain specifications on Python Pandas

description: Turn a Dataframe into an Array with certain specifications on Python Pandas


code:
mytext="""<contextfile concordance=brown>
<context filename=br-a01 paras=yes>
<p pnum=1>
....
<wf cmd=done pos=VB lemma=say wnsn=1 lexsn=2:32:00::>said</wf>
<wf cmd=done pos=NN lemma=friday wnsn=1 lexsn=1:28:00::>Friday</wf>
<wf cmd=ignore pos=DT>an</wf>
....
....
<punc>``</punc>
<wf cmd=ignore pos=DT>no</wf>
<wf cmd=done pos=NN lemma=evidence wnsn=1 lexsn=1:09:00::>evidence</wf>
<punc>''</punc>
....
<wf cmd=done pos=NN lemma=irregularity wnsn=1 lexsn=1:04:00::>irregularities</wf>
<punc>.</punc>
</s>
</p>
</context>
</contextfile>"""

from lxml import html
parsed = html.fromstring(mytext)
for x in parsed.getiterator(): print x.tag, x.attrib, x.text, x.tail

rawDescription: Reading non well-formed XML file without quotation marks in attribute - python


code:
from subprocess import check_output

species_array = ["homo_sapiens", "pan_troglodytes", "pongo_abelii", "gorilla_gorilla", "macaca_mulatta", "callithrix_jacchus", "bos_taurus", "canis_familiaris", "equus_caballus", "felis_catus", "ovis_aries", "sus_scrofa", "oryctolagus_cuniculus", "rattus_norvegicus", "mus_caroli", "mus_pahari", "mus_musculus"]
path = "/homes/varshith/maf_files/1/testmafs/HAL_Files/"
for ele in species_array[1:-5]:
    s = check_output(["find", path, "-name", "*{0}*".format(ele)])
    print s

rawDescription: Listing name of files with part of a string from Python


code:
from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler

port = 9999

def func():
    print 'Hi!'
    print x # error!
    print 'Bye!'

class Handler(SimpleXMLRPCRequestHandler):
     def _dispatch(self, method, params):
         try:
             return self.server.funcs[method](*params)
         except:
             import traceback
             traceback.print_exc()
             raise

if __name__ == '__main__':
    server = SimpleXMLRPCServer(("localhost", port), Handler)
    server.register_function(func)
    print "Listening on port %s..." % port
    server.serve_forever()

rawDescription: How to see traceback on xmlrpc server, not client?

description: See traceback on xmlrpc server, not client


code:
import glob
import datetime

dic={}

for f in glob.glob('*-*.txt'):
    d = datetime.datetime.strptime(f, '%Y%m%d-%H%M%S.txt')
    if d.hour < 2:
        continue
    if not d.day in dic or dic[d.day][0] > d.time:
        dic[d.day] = (d.time, f)

for t, f in dic.values():
    print f

rawDescription: how to open some specific files using python

description: Open some specific files using python


code:
mask = np.array([[False, False, False],
                 [False, False, True]])

ind2 = np.ma.masked_where(mask, ind)  # these are the indices that we would be interested in (we do not make use of this line later on)

raw_output = X[np.arange(len(ind))[:, np.newaxis], 0] - X[ind, 0]
masked_output = np.ma.masked_where(mask, raw_output)

print masked_output

rawDescription: Using ndarray.flatten() python for array of several arrays, Python


code:
for i in lst:
    print countspace(i)

rawDescription: Finding how many of a certain character in each element in a list


code:
def query(q,epr,f='application/json'):
    try:
        params = {'query': q}
        params = urllib.urlencode(params)
        opener = urllib2.build_opener(urllib2.HTTPHandler)
        request = urllib2.Request(epr+'?'+params)
        request.add_header('Accept', f)
        request.get_method = lambda: 'GET'
        url = opener.open(request)
        return url.read()
    except Exception, e:
        traceback.print_exc(file=sys.stdout)
        raise e

rawDescription: Example python script that uses DBPedia?

description: Example python script that uses DBPedia


code:
import operator
import fractions
from itertools import permutations, product, count
from functools import reduce

vectors = [[87, 125, 209, 312],
           [29, 122, 254, 372],
           [15, 130, 277, 369],
           [142, 197, 282, 383],
           [64, 157, 256, 350]]
vectors = [[fractions.Fraction(x) for x in v] for v in vectors]

operators = {
    '+': operator.add,
    '-': operator.sub,
    '*': operator.mul,
    '/': operator.div,
    }

def create_groupings(n, exclude = ()):
  if n <= 0: yield ()
  for i in range(1, n+1):
    if not '+' in exclude:
      for rest in create_groupings(n - i, ('+',)):
        yield ((i, '+'),) + rest
    if not '-' in exclude:
      for rest in create_groupings(n - i, ('+', '-')):
        yield ((i, '-'),) + rest
    if not '*' in exclude:
      for rest in create_groupings(n - i, ('*',)):
        yield ((i, '*'),) + rest
    if not '/' in exclude:
      for rest in create_groupings(n - i, ('/', '*')):
        yield ((i, '/'),) + rest

def fill_grouping(groups, vectors):
  if len(groups) == 0:
    yield ()
    return

  (group_size, op), grest = groups[0], groups[1:]
  for vv in permutations(vectors):
    vecs, vrest = vectors[:group_size], vectors[group_size:]
    for operands in map(list, product(*vecs)):
      # enforce ascending ordering to avoid collisions
      # like A + B == B + A
      if operands != sorted(operands): continue
      for rest in fill_grouping(grest, vrest):
        yield ((op, operands),) + rest

groupings = create_groupings(5)
for g in groupings:
  for groups in fill_grouping(g, vectors):
    evaluated = ((op, reduce(operators[op], x)) for (op, x) in groups)
    _, value = reduce(lambda (_, x), (op, y): (None, operators[op](x,y)), evaluated)
    if 1000 == value:
      print groups

rawDescription: Find equivalent results under distributive and commutative laws


code:
import re
s = "this is a string #123/234 with other stuff"
t = re.sub('#\d{3}\/\d{3}', '', s)
print t

rawDescription: Python: Ignore a # / and random numbers in a string


code:
a = numpy.random.randint(1000, size=100).reshape(10, 10)
indices = a.ravel().argsort()
print a.ravel()[indices[-10:]]

rawDescription: How to evaluate N largest elements in a multidimensional array?

description: Evaluate N largest elements in a multidimensional array


code:
columns = [2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 2, 1];

ranges = [[i+1, v] for i,v in enumerate(columns[1:]) if columns[i] != columns[i+1]]
ranges.append([len(columns),0]) # special case for last element
for i,v in enumerate(ranges[:-1]):
    if v[1] != columns[0]:
        print "rows", v[0]+1, "-", ranges[i+1][0], ":", v[1]

rawDescription: How to find duplicates in a python list that are adjacent to each other and list them with respect to their indices?

description: Find duplicates in a python list that are adjacent to each other and list them with respect to their indices


code:
from lxml import etree
doc = etree.HTML(open('test.data').read())

for t in doc.xpath('//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:
S = [0, 1, 2]
F = [S[0:i] + S[i+1:] for i in range(len(S))]
print F

rawDescription: Create copy of list and remove element

description: Create copy of list and remove element


code:
from collections import deque
from itertools import islice

def window(iterable, n=3):
    it = iter(iterable)
    d = deque((next(it) for Null in range(n-1)), n)
    for elem in it:
        d.append(elem)
        yield tuple(d)

print [x for x in window([1, 2, 3, 4, 5])]

rawDescription: Iterator with memory?

description: Iterator with memory


code:
for index, entry in enumerate(data, 1):
    print 'Item {}'.format(index)
    for name, value in entry.items():
        print '    {}: {}'.format(name, value)

rawDescription: Display Specific Fields (Through Parse) from JSON response in Python


code:
import random
import numpy as np

#rand creates array between 0,1. multiple by 500 and recast to int to get the range you want
def randomArray(n_Elementi):
    return np.array(np.random.rand(n_Elementi)*500,dtype=int)

#seq[-n:] takes the elements n from the end to the end [-n:]. This is called splicing. similarly [:-n] ...
def npshift(seq, n):
    return np.concatenate((seq[-n:], seq[:-n]))

n_Elementi = 10
my_vector=randomArray(n_Elementi)
print my_vector
print shift (my_vector, 3)

rawDescription: Python- Shifting the position of each object to left or right in a random Array


code:
for key in range(0,nDicts):
    for x in list1[key]:
        if x in dict1:
            resultDict={list1[key][x]:dict1[x]}
            print resultDict

rawDescription: combining a list of dictionaries with another dictionary

description: Combining a list of dictionaries with another dictionary


rawDescription: Converting a unicode string to see unicode code points


code:
for cnt in range(10):
    print str(cnt) + " + 1 = " + str(cnt + 1)

rawDescription: Using a variable as both an integer and a string (Python)


rawDescription: Is there a function in numpy to compare arrays of multiple dimensions similar to numpy.where function?


code:
from Tkinter import *

def show_values(value=None):
    print s1.get(), s2.get()

root=Tk()
s1 = Scale(root, from_=0, to=42, tickinterval=8, command=show_values)
#                                                ^^^^^^^^^^^^^^^^^^^
s1.set(19)
s1.pack()
s2 = Scale(root, from_=0, to=200, length=600, tickinterval=10, orient=HORIZONTAL,
           command=show_values) # <---
s2.set(23)
s2.pack()
Button(root, text='Show', command=show_values).pack()
root.mainloop()

rawDescription: Tkinter scale in realtime

description: Tkinter scale in realtime


code:
soup = BeaufitulSoup (html)
div = soup("div", {"class" : "good"})
children = div[0].findChildren ()
print children[0]   #### desired output

rawDescription: Get HTML subtree from HTMLparser

description: Get HTML subtree from HTMLparser


code:
import re

html = """

        10/12/2015 5:49:00 PM -  Seeking Alpha

"""

for item in re.findall('\\s*(.*?)\s*\<\/small\>', html, re.I+re.M):
    print '"{}"'.format(item)

rawDescription: Extraction Specific Lines From A Website


code:
from math import log10

def sum_digits(n, i=None):
    if i is None:
        i = int(log10(abs(n)))

    e = float(10**i)
    a, b = (n / e), (abs(n) % e)

    if i == 0:
        return int(a)
    else:
        return int(a) + sum_digits(b, (i - 1))

print sum_digits(1234)
print sum_digits(-1234)

rawDescription: How to added up a variable with multiple values together in Python Recursion Function?

description: Added up a variable with multiple values together in Python Recursion Function


code:
#!/usr/bin/python

print "Content-type: text/html\n\n"

import json
from collections import defaultdict

mystuff = list()
mystuff.append( ('1', 'b', 'c', 'd') )
mystuff.append( ('2', 'f', 'g', 'h') )

stufflist = list()

for s in stufflist:
    d = {}
    d['a'] = s[0]
    d['b'] = s[1]
    d['c'] = s[2]
    d['d'] = s[3]
    stufflist.append(d)

print json.write(stufflist)

rawDescription: Convert a JSON string to a Java/Python (Jython) object?


code:
for line in difflib.unified_diff(lines1, lines2, fromfile='file1', tofile='file2', lineterm='', n=0):
    print line

rawDescription: Compare two files report difference in python


code:
from collections import Counter, OrderedDict

fails = [['55','35325235432','log'], ['55','22222222','log'], ['55','3232432','log2'], ['64','55','log3'], ['64','324234324','log2']]

v1_counts = Counter(v[0] for v in fails)
v1 = OrderedDict({v[0] : v for v in fails})
shorten_fails = [v + [v1_counts[k]] for k, v in v1.items()]

print shorten_fails

rawDescription: Compare 1st element in list of lists and write number of repeated values


code:
t = ("hello", "world")   # or
t = ("hello world", )

print t

rawDescription: Python 2.7: What print() does to the string inside of it


code:
print X.__array_interface__
#<bound method Matrix.__array_interface__ of <sprint.Matrix object at 0x107c5c320>>

rawDescription: Numpy's __array_interface__ not returning dict


rawDescription: Check if certain value is contained in pandas column python


code:
for word in sorted(set(my_list)):
    count = my_list.count(word)
    print "{} is repeated {} time{}.".format(word, count, 's' if count > 1 else '')

rawDescription: Python item sort

description: Python item sort


code:
initial = ["Fred-Jim","Fred","Fred-Jim-Bob", "Fred-Jim-Jack", "John", "John-Jim"]

result = {}

for item in initial:
    hierarchy = item.split('-')
    local_result = result
    for node in hierarchy:
        local_result = local_result.setdefault(node, {})

print result

rawDescription: convert delimited string to hierarchical JSON in python

description: Convert delimited string to hierarchical JSON in python


code:
pd.set_option('display.unicode.east_asian_width', True)
print dframe

pd.set_option('display.unicode.east_asian_width', False)
print dframe

rawDescription: Python Pandas: print the csv data in oder with columns


code:
import pandas as pd
import numpy as np

index = pd.date_range('1/1/2000', periods=60, freq='2h')
ts = pd.Series(np.random.randn(60), index = index)

key = lambda x: x.time()
groups = ts.groupby(key)

print pd.DataFrame({k:g for k,g in groups}).resample('D').T

rawDescription: python pandas transform timeseries into multiple column DataFrame

description: Python pandas transform timeseries into multiple column DataFrame


code:
from collections import defaultdict, Counter
from datetime import date
from operator import itemgetter
import os

COLORS = ('blue', 'green', 'yellow', 'red', 'purple')
NUM_LETTERS = 2
path = 'testdir'

date_counters = defaultdict(Counter)

for filename, filepath in ((name, os.path.join(path, name))
                                for name in os.listdir(path)):
    if (os.path.isfile(filepath) and any(color in filename for color in COLORS)):
        mod_date = date.fromtimestamp(os.stat(filepath).st_mtime)
        date_counters[mod_date].update((filename[:NUM_LETTERS],))

for mod_date in sorted(date_counters):  # sort by file group's modification date
    print mod_date.day
    for initials, count in sorted(date_counters[mod_date].iteritems(),
                                  key=itemgetter(1)):
        print initials, count

rawDescription: Count Filenames by name and list by Date Modified


code:
a = column['Home Address']
for index,item in enumerate(a):
    print index,item

rawDescription: Python Column Indexing out of order

description: Python Column Indexing out of order


code:
import threading
mydata = threading.local()
mydata.nesting = 0

class auto_refresh(object):

  def __init__(self, f):
    self.f = f

  def __call__(self, *args, **kwargs):
    mydata.nesting += 1
    try: return self.f(*args, **kwargs)
    finally:
      mydata.nesting -= 1
      if mydata.nesting == 0:
        print 'refreshing'

rawDescription: Refresh decorator

description: Refresh decorator


code:
from datetime import datetime
import re

data = {}
pattern = re.compile('(\w+)\s+(\d+)\s+(\w+\s\d+)\s+(\d+)')
with open('input.txt') as f:
    for line in f:
        try:
            year = datetime.strptime(line.strip(), '%Y-%m-%d').year
        except ValueError:
            item = pattern.match(line.strip()).groups()
            data[str(year) + "_" + item[0]] = item[1:]

print data

rawDescription: How to read specific lines in a file and insert them to a dictionary in python?

description: Read specific lines in a file and insert them to a dictionary in python


code:
from functools import wraps

def fix_url(function):
    @wraps(function)
    def wrapper(*args, **kwarg):
        kwarg['url'] = kwarg['url'].replace(".images", ".servers")
        return function(*args, **kwarg)
    return wrapper

@fix_url
def images_custom_list(args, tenant=None, token=None, url=None):
    url = url + '/' + 'detail'
    output = do_request(url, token)
    output = output[0].json()["images"]
    custom_images_list = [custom_images for custom_images in output
            if custom_images["metadata"].get('user_id', None)]
    temp_image_list = []
    for image in custom_images_list:
        image_temp  = ( { "status": image["status"],
            "links": image["links"][0]["href"],
            "id": image["id"], "name": image["name"]} )
        temp_image_list.append(image_temp)
    print json.dumps(temp_image_list, indent=2)

@fix_url
def image_list_detail(args, tenant=None, token=None, url=None):
    uuid = args['uuid']
    url = url + "/" + uuid
    output = do_request(url, token)
    print output[0]

rawDescription: Python Function Decoration In This Instance


code:
import shelve
t = shelve.open('temp.db')['1']
print t

rawDescription: Writing b-tree in file using python

description: Writing b-tree in file using python


code:
import xml.etree.ElementTree

html = '''
<title></title>

1a	2a	3a	4a	5a	6a

1b	2b	3b	4b	5b	6b

1c	2c	3c	4c	5c	6c

1a	2a	3a	4a	5a	6a

1b	2b	3b	4b	5b	6b

1c	2c	3c	4c	5c	6c

'''

tree = xml.etree.ElementTree.fromstring(html)
results = [ c.text
            for c in tree.findall('body/table[@id="blk-ctnt-2308"]//tr/td[5]') ]
print results

rawDescription: python lxml can't find table

description: Python lxml can't find table


rawDescription: format() int as float

description: Format() int as float


code:
data = []
while True:
    A = raw_input("Please input text here (Q to quit)")
    if len(A) == 1 and A[0].lower() == "q":
        break
    data.append(A)

print data
for A in data:
    print len(A)

rawDescription: python only recognizes first line of my input

description: Python only recognizes first line of my input


code:
import fileinput

for line in fileinput.input('D:\File.txt', inplace = True):
    print line.strip() + ";"

rawDescription: Changing format of output text file

description: Changing format of output text file


code:
class Foo(object):
    def __init__(self):
        self.edit1 = 1
        self.edit2 = 2
        self.edit3 = 3
        self.button1 = 1
        self.button2 = 2
        self.button3 = 3

    def find_attributes(self, name_start):
        return [value for name, value in sorted(self.__dict__.items())
                          if name.startswith(name_start)]

foo = Foo()
print foo.find_attributes('edit')
print foo.find_attributes('button')

rawDescription: pyside / pyqt: simple way to bind multiple buttons that shares the same functionality

description: Pyside / pyqt: simple way to bind multiple buttons that shares the same functionality


code:
from datetime import timedelta

delta = timedelta(hours=3457)
minutes, seconds = divmod(delta.seconds, 60)
hours, minutes = divmod(minutes, 60)
hours += delta.days * 24

print '%sh %sm' % (hours, minutes)

rawDescription: Python convert timedeltas into hours in string format


code:
import json

# 'with' is called a context manager, and it handles closing for you when
# you leave the indented block
with open('data.txt', 'r') as f:
    data = json.load(f)

# When the code leaves the indented block, the file is closed for you

# Print the entire structure
print json.dumps(data)

# Print out a piece of the data
print data['data']['internalName']

rawDescription: Having trouble with decoding JSON in Python


code:
import collections
import re

string = """<author>James Parker</author><year>2008</year><lang>English</lang>
<author>Van Wie</author><year>2002</year>
<year>2012</year><lang>English</lang>
<year>2002</year><lang>French</lang>"""

strings = string.split("\n")
counter = collections.Counter()

tag_re = "\<[^/\>]*\>"
for s in strings:
    tags = re.findall(tag_re, s)
    tags.sort()
    # use name directly
    for tag in tags:
        counter[tag] += 1
    # use set for ngram
    ngram = tuple(tags)
    counter[ngram] += 1

print counter

rawDescription: How to find Term frequency of a particular sets of tags in a document

description: Find Term frequency of a particular sets of tags in a document


code:
element = driver.find_element_by_xpath('//div[@class="d3-tip n"][contains(text(), "select")]')
print element.text

rawDescription: Unable to read text from a div tag in python


code:
def find_all_paths(graph, start, end, path=[]):
        path = path + [start]
        if start == end:
            return [path]
        if start not in graph:
            return []
        paths = []
        for node in graph[start]:
            if node not in path:
                newpaths = find_all_paths(graph, node, end, path)
                for newpath in newpaths:
                    paths.append(newpath)
        return paths

def min_path(graph, start, end):
    paths=find_all_paths(graph,start,end)
    mt=10**99
    mpath=[]
    print '\tAll paths:',paths
    for path in paths:
        t=sum(graph[i][j] for i,j in zip(path,path[1::]))
        print '\t\tevaluating:',path, t
        if t<mt:
            mt=t
            mpath=path

    e1='\n'.join('{}->{}:{}'.format(i,j,graph[i][j]) for i,j in zip(mpath,mpath[1::]))
    e2=str(sum(graph[i][j] for i,j in zip(mpath,mpath[1::])))
    print 'Best path: '+e1+'   Total: '+e2+'\n'

rawDescription: A* Search - least number of hops?

description: A* Search - least number of hops


code:
import subprocess
import sys

cmd = ["ruby", "/Users/smcho/Desktop/testit.rb"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
for line in iter(p.stdout.readline, ''):
    print line,
    sys.stdout.flush()
p.wait()

rawDescription: Print out the output of os.popen() without buffering in python


code:
import timeit

def time_list(x, lst, num):
    for n in xrange(num):
        x in lst

def time_turn_set(x, lst, num):
    s = set(lst)
    for n in xrange(num):
        x in s

for num in range(1, 10):
    size = 1000
    setup_str = "lst = range(%d); from __main__ import %s"
    print num,
    print timeit.timeit("time_list(%d, lst, %d)" % (size / 2, num),
                        setup=setup_str % (size, "time_list"), number=10000),
    print timeit.timeit("time_turn_set(%d, lst, %d)" % (size / 2, num),
                        setup=setup_str % (size, "time_turn_set"), number=10000)

rawDescription: Why would anyone check 'x in list'?

description: Why would anyone check 'x in list'


code:
print square(2)

def pre(x): print 'pre', x
def post(x): print 'post', x

square.add_pre(pre)
print square(2)
print square(3)

square.add_post(post)
print square(4)
print square(5)

rawDescription: allow users to "extend" API functions

description: Allow users to "extend" API functions


code:
eve = Rectangle(3, 5)
kain = ColoredRectangle(eve, color="blue")
abel = ColoredRectangle(eve, color="red")
print eve.color, kain.color, abel.color
#red red red

rawDescription: Subclassing in python of instantiated superclass


code:
def sort_key(student):
    return student[2]

print sorted(student_tuples, key=sort_key)

rawDescription: Using sorted() in Python

description: Using sorted() in Python


code: print response.getRegister(2)

rawDescription: Reading registers with pymodbus

description: Reading registers with pymodbus


code:
class Book:
    pass

class Part:
    book = models.ForeignKey(Book, related_name="parts")

class Chapter:
    part = models.ForeignKey(Part, related_name="chapters")
    number = models.IntegerField()

# fetch a book and all related info w/ only 2 db hits
book = Book.objects.first().prefetch_related("parts","parts__chapters")
print(book.parts.all()) # returns all parts for book
for part in book.parts.all():
    print part.chapters.all()

rawDescription: Django Shortcut nested foreign key

description: Django Shortcut nested foreign key


code: print np.bincount(idx)

rawDescription: Determining the size of cluster after Kmeans in Python


code:
import re
p = re.compile(ur'RT @(\w+):')
test_str = u"RT @ReutersAero: Further pictures from the #MH17 crash site in  in Grabovo, #Ukraine #MH17 - @reuterspictures (GRAPHIC): http://t.co/4rc7Y4…\nRT @AnotherAero: Further pictures from the #MH17 crash site in  in Grabovo, #Ukraine #MH17 - @reuterspictures (GRAPHIC): http://t.co/4rc7Y4…\n"

re.findall(p, test_str)

rawDescription: Repeating regex pattern in Python

description: Repeating regex pattern in Python


code:
from difflib import SequenceMatcher

li = [\
([u'Houston', u'Alabama'],
 [u'Houst', u'Alab']),

([u'Florida State', u'North Carolina State'],
 [u'NCSt', u'FlaSt']),

([u'Penn State', u'Iowa'],
 [u'PnSt', u'Iowa']),

([u'Oklahoma', u'Texas'],
 [u'Texas', u'Okla']),

([u'Florida Atlantic', u'South Florida'],
 [u'SFla', u'FlAtl']),

([u'Georgia', u'Tennessee'],
 [u'Geo', u'Tenn']),

([u'San Jose State', u'Idaho'],
 [u'UI', u'SJSU']),

([u'Washington State', u'Arizona State'],
 [u'ArzSt', u'WshSt']),

([u'Fresno State', u'Nevada'],
 [u'Nevad', u'FrsSt']),

([u'Oregon State', u'Arizona'],
 [u'ARIZ', u'OSU']),

([u'Clemson', u'Virginia Tech'],
 [u'VTech', u'Clem']),

([u'Chattanooga', u'Arkansas'],
 [u'UTC', u'AR']),

([u'USC', u'Stanford'],
 [u'USC', u'Stanf']),

([u'Baylor', u'Colorado'],
 [u'BU', u'CU']),

([u'North Texas', u'Louisiana-Lafayette'],
 [u'NoTex', u'LaLaf']),

([u'Tulane', u'Army'],
 [u'TLN', u'ARMY']),

([u'Troy', u'Florida International'],
 [u'TROY', u'FIU']),

([u'Louisiana-Monroe', u'Arkansas State'],
 [u'ASU', u'ULM']),

([u'Texas Tech', u'Iowa State'],
 [u'TT', u'ISU']),

([u'Akron', u'Western Michigan'],
 [u'AKRON', u'WMU']),

([u'Liberty', u'Toledo'],
 [u'LIBERTY', u'TOLEDO']),

([u'Virginia', u'Middle Tennessee'],
 [u'Virg', u'MTnSt']),

([u'Oklahoma State', u'Texas A&M'],
 [u'TexAM', u'OKSt']),

([u'Notre Dame', u'UCLA'],
 [u'NDame', u'UCLA']),

([u'Rutgers', u'Cincinnati'],
 [u'Cincy', u'Rutgr']),

([u'Ohio State', u'Purdue'],
 [u'Prdue', u'OhSt']),

([u'LSU', u'Florida'],
 [u'Fla', u'LSU']),

([u'Air Force', u'UNLV'],
 [u'AFA', u'UNLV']),

([u'Nebraska', u'Missouri'],
 [u'Misso', u'Neb']),

([u'New Mexico State', u'Boise State'],
 [u'NMxSt', u'BoiSt']),

([u'Pittsburgh', u'Navy'],
 [u'Navy', u'Pitt']),

([u'Wake Forest', u'Florida State'],
 [u'WFrst', u'FlaSt']),

([u'San Jose State', u'Hawaii'],
 [u'Hawa', u'SJSt']),

([u'UCF', u'South Florida'],
 [u'UCF', u'SFla']) ]

def comp(N,D,sq = SequenceMatcher(None)):
    sq.set_seqs(N[0],D[0])
    a = sq.ratio()
    sq.set_seqs(N[1],D[1])
    b = sq.ratio()

    sq.set_seqs(N[0],D[1])
    x = sq.ratio()
    sq.set_seqs(N[1],D[0])
    y = sq.ratio()

    sq.set_seqs(N[0].lower(),D[0].lower())
    al = sq.ratio()
    sq.set_seqs(N[1].lower(),D[1].lower())
    bl = sq.ratio()

    sq.set_seqs(N[0].lower(),D[1].lower())
    xl = sq.ratio()
    sq.set_seqs(N[1].lower(),D[0].lower())
    yl = sq.ratio()

    if ((a>0.5 and b>0.5 and a+b>1.4)
        or (al>0.5 and bl>0.5 and al+bl>1.4)):
        return (N[0],D[0]), (N[1],D[1])
    elif ((x>0.4 and y>0.4 and x+y>1.4)
          or (xl>0.4 and yl>0.4 and xl+yl>1.4)):
        return (N[0],D[1]), (N[1],D[0])
    elif x+y==0.0 and a+b>0.1:
        return (N[0],D[0]), (N[1],D[1])
    elif a+b==0.00 and x+y>0.1:
        return (N[0],D[1]), (N[1],D[0])
    elif a+b > x + y + 0.5:
        return (N[0],D[0]), (N[1],D[1])
    elif x+y > a + b + 0.5:
        return (N[0],D[1]), (N[1],D[0])
    elif a+b > x + y:
        return (N[0],D[0]), (N[1],D[1])
    elif x+y > a + b:
        return (N[0],D[1]), (N[1],D[0])

print '\n'.join('%-30s   %s' % comp(N,D) for N,D in li)

rawDescription: Pythonic Name Matching

description: Pythonic Name Matching


code:
import urllib

BASE_URL = 'http://foobar.com/?%s'
print BASE_URL % urllib.urlencode({
   'foo': 'bar',
})

rawDescription: String inside a string

description: String inside a string


code:
#!/usr/bin/python

import sys

for i in sys.argv:
    print "hi"

rawDescription: How to pass variable arguments from bash script to python script

description: Pass variable arguments from bash script to python script


code:
def userInput(ItemA, ItemB, *args):
    THIS = ItemA
    THAT = ItemB
    MORE = args

    print THIS,THAT,MORE

userInput('this','that','more1','more2','more3')

rawDescription: Iterate over *args?

description: Iterate over *args


code:
sourcecode = '''\
class A(object):
  b = 1
  a = 1
  c = 1
'''
tree = ast.parse(sourcecode)
order = ClassOrder()
order.visit(tree)
print order.identifiers
tree = ast.parse(inspect.getsource(ast.NodeVisitor))
order = ClassOrder()
order.visit(tree)
print order.identifiers

rawDescription: How to read order of declared members in Python class with use reflection or parsing (metaclass replace is forbidden)?

description: Read order of declared members in Python class with use reflection or parsing (metaclass replace is forbidden)


code:
data = """
#include <stdlib.h>
#include "mylib.h"
"""
import re
pattern = re.compile("#\s*include\s*(?:<.*?>|\".*?\")")
print pattern.findall(data)

rawDescription: Python regex matching pair of quotes or "<>"


code: print [{key: val if val is not None else "" for key, val in dct.items()} for dct in lst]

rawDescription: python one liner code for two nested for loop

description: Python one liner code for two nested for loop


code:
content = open('foo').read()
print content
content

rawDescription: dealing with \n characters at end of multiline string in python

description: Dealing with \n characters at end of multiline string in python


code:
arr = [['i', 'dont'], 'hi']
arr = [[item] if isinstance(item, str) else item for item in arr]
#arr is now [['i', 'dont'], ['hi']]
print [item for sublist in arr for item in sublist]

rawDescription: making a flat list out of a list of lists where the elements are strings

description: Making a flat list out of a list of lists where the elements are strings


code:
d = defaultdict(set)

with open("romEdges.txt") as fin:
    for line in fin:
        k, v = line.strip().split()
        d[k].add(v)
print d

rawDescription: Converting text file to dictionary in python


code:
from pyparsing import *

aExpr = Word("A", nums)
bExpr = Word("B", nums)
cExpr = Word("C", nums)

grammar = ZeroOrMore(aExpr.setResultsName("A",listAllMatches=True) |
                     bExpr.setResultsName("B",listAllMatches=True) |
                     cExpr.setResultsName("C",listAllMatches=True) )

results = grammar.parseString("A1 B1 A2 C1 B2 A3")
print results.dump()

rawDescription: Is this technique acceptable for constructing ParseResults in pyparsing?


code:
while True:
  weights = np.zeros( (R_DIMENSION, DIMENSION, DIMENSION))
  for x0,y0 in data['allPoints']:
    s = 0.001
    eta = (x-x0)**2 + (y-y0)**2 - r**2
    weights += 1. / ( sqrt( 2 * sconst.pi ) * s ) * np.exp( -( eta ** 2 )\
                                                            / ( 2 * s ** 2 ) )
  index = np.argmax( weights )
  rr,ii,jj = np.unravel_index( index, (R_DIMENSION, DIMENSION, DIMENSION))
  score = weights[rr][ii][jj]
  if score < 200:
    print 'finished after %s circle(s) found' % circle_counter
    break

rawDescription: Displaying a slice from a 3d ndarray

description: Displaying a slice from a 3d ndarray


code:
import os
def say_hello(input_func):
    name = input_func()
    return "Hello " + name

def prompt_for_name():
    return raw_input("What is your name? ")

print say_hello(prompt_for_name)
# Normally would pass in methods, but lambdas can be used for brevity
print say_hello(lambda: open("a.txt").readline())
print say_hello(lambda: os.environ.get("USER"))

rawDescription: Supply inputs to Python unit tests

description: Supply inputs to Python unit tests


code:
def Arrange_Disks(A):
    length=len(A)
    moves=0
    for i in xrange(length):
        minn=i
        for j in xrange(i+1,length):
            if A[j]<A[minn]:
                minn=j
        moves+=minn-i
        A[i],A[minn]=A[minn],A[i]
    return A,moves                #returns a tuple containing both A and moves

def main():

    n=input("Please enter a number between 2 to 10: ")
    A=[]
    for disk in range (1,(2*n+1)):
        if (disk%2) != 0:
            A.append("1")
        else:
            A.append("0")
    print "This is the original array: ", A

    A,moves=Arrange_Disks(A)

    print "This is the sorted array: ", A
    print "This is the number of moves required: ", moves

main()

rawDescription: python function sorting alternating disks

description: Python function sorting alternating disks


code:
import re

a = " ebrj wjrbw erjwek wekjb rjERJK ABB RAEJKE BWE RWEJBEWJ B KREWBJ BWERBJ32J3B23B J BJ235JK BJJ523 2"

print re.search(ur'(wekjb|ABB)',a).group()
if re.search(ur'(wekjb|ABB)',a):
    print 'success'

rawDescription: Re.match() returns always none

description: Re.match() returns always none


code:
import eventlet
from eventlet.green import urllib2
from eventlet.timeout import Timeout

url5 = 'http://ipv4.download.thinkbroadband.com/5MB.zip'
url10 = 'http://ipv4.download.thinkbroadband.com/10MB.zip'

urls = [url5, url5, url10, url10, url10, url5, url5]

def fetch(url):
    response = bytearray()
    with Timeout(60, False):
        response = urllib2.urlopen(url).read()
    return url, len(response)

pool = eventlet.GreenPool()
for url, length in pool.imap(fetch, urls):
    if (not length):
        print "%s: timeout!" % (url)
    else:
        print "%s: %s" % (url, length)

rawDescription: How to perform time limited response download with python requests?

description: Perform time limited response download with python requests


rawDescription: 2048 get board with python selenium

description: 2048 get board with python selenium


code:
import pandas as pd

name = u'Amiel, Henri-Fr\xe9d\xe9ric'
array = [name]
df = pd.DataFrame({'Names':array})
df.to_csv('names', encoding='utf-8')
uni = pd.read_csv('names', index_col = [0], encoding='utf-8')
print uni  # for me it works with or without print

rawDescription: How to read a dataframe of encoded strings from csv in python

description: Read a dataframe of encoded strings from csv in python


code:
from sqlalchemy import create_engine, Column, Integer, ForeignKey
from sqlalchemy.orm import relation, scoped_session, sessionmaker, AttributeExtension, ColumnProperty
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite:///:memory:', echo=False)
session = scoped_session(sessionmaker(bind=engine, autoflush=True))
Base = declarative_base()
Base.query = session.query_property()

class ValueAttributeExtension(AttributeExtension):
    active_history = True

    def append(self, state, child, initiator):
        assert False, "should not be called"

    def remove(self, state, child, initiator):
        assert False, "should not be called"

    def set(self, state, value, oldvalue, initiator):
        print 'set called', state.obj(), value, oldvalue
        child = state.obj()
        if not(child.parent is None):
            child.parent.sum_of_children += -oldvalue + value
        return value

class ChildrenAttributeExtension(AttributeExtension):
    active_history = True

    def append(self, state, child, initiator):
        print 'append called', state.obj(), child
        parent = state.obj()
        parent.sum_of_children += child.value
        return child

    def remove(self, state, child, initiator):
        print 'remove called', state.obj(), child
        parent = state.obj()
        parent.sum_of_children -= child.value

    def set(self, state, child, oldchild, initiator):
        print 'set called', state, child, oldchild
        parent = state.obj()
        parent.parent.sum_of_children += -oldchild.value + child.value
        #parent.sum_of_children += -oldchild.value + child.value
        return child

class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('parent.id'), nullable=False)
    value = ColumnProperty(Column(Integer, nullable=False, default=0),
                    extension=ValueAttributeExtension())

class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    sum_of_children = Column(Integer, nullable=False, default=0)

    children = relation('Child', backref='parent',
                        extension=ChildrenAttributeExtension())

Base.metadata.create_all(engine)

# Add a parent
p = Parent()
session.add(p)
session.commit()

p = Parent.query.first()
assert p.sum_of_children == 0

# Add a child
c = Child(parent=p, value=5)
session.add(c)
session.commit()

p = Parent.query.first()
assert p.sum_of_children == 5

# Change a child
#c = Child.query.first()
c.value = 3 # fixed bug: = instead of ==
session.commit()  # extension.set() doesn't get called

p = Parent.query.first()
assert p.sum_of_children == 3 # Assertion is OK

rawDescription: Updating a de-normalized attribute automatically with an AttributeExtension


code:
def fuzzyFloor(v):
    """Returns the floor of the given number, unless it is equal to its
    ceiling (within floating point error)."""
    floor = math.floor(v)
    if fAlmostEqual(floor+1, v):
        return floor+1
    return floor

print fuzzyFloor(14.9999999999999)
print fuzzyFloor(15)
print fuzzyFloor(14.99)
print fuzzyFloor(14.5)

rawDescription: Unexpected value of floor() function in python


code:
for i in []:
    print i # This will print nothing since it is iterating over empty list same as yours

rawDescription: Python - How to check if the text is in a file txt?


code:
l1 = [1,1,2,3,5,6]
l2 = [1,2,8,9]
from collections import Counter

print Counter(l1) - Counter(l2)
print Counter(l2) - Counter(l1)

rawDescription: Python list difference with number of ocurrences


code:
import os, os.path, shutil

YUI_COMPRESSOR = 'yuicompressor-2.4.2.jar'

def compress(in_files, out_file, in_type='js', verbose=False,
             temp_file='.temp'):
    temp = open(temp_file, 'w')
    for f in in_files:
        fh = open(f)
        data = fh.read() + '\n'
        fh.close()

        temp.write(data)

        print ' + %s' % f
    temp.close()

    options = ['-o "%s"' % out_file,
               '--type %s' % in_type]

    if verbose:
        options.append('-v')

    os.system('java -jar "%s" %s "%s"' % (YUI_COMPRESSOR,
                                          ' '.join(options),
                                          temp_file))

    org_size = os.path.getsize(temp_file)
    new_size = os.path.getsize(out_file)

    print '=> %s' % out_file
    print 'Original: %.2f kB' % (org_size / 1024.0)
    print 'Compressed: %.2f kB' % (new_size / 1024.0)
    print 'Reduction: %.1f%%' % (float(org_size - new_size) / org_size * 100)
    print ''

    #os.remove(temp_file)

rawDescription: combine javascript files at deployment in python

description: Combine javascript files at deployment in python


code:
import happybase

import json

import csv

file = open('test.json','wb+')

store_file = {}
json_output = []

for key, data in table.scan(row_start='01/01/2009'):
   store_file['Key'] = key
   store_file['Value'] = data
   json_output.append(store_file.copy())
   print json_output

json.dump(json_output,file, indent = 2)

file.close()

rawDescription: Writing contents to json file which already has headers


code:
def do_tasks(keys):
    results = []
    for key in keys:
        try:
            result = perform_scan.delay(key)
            results.append(result)
        except:
           print "failed"
    return results

def main():
    executor = concurrent.futures.ProcessPoolExecutor(10)
    futures = [executor.submit(enqueue_tasks, group) for group in grouper(key_list, 40)]
    print sum(len(results) for results in concurrent.futures.as_completed(futures))

rawDescription: My queue is empty after multiprocessing.Process instances finish


code:
#ef_Tool.py

class MyTools:
    def OnClickPrinting(self,textvalue):
        #Value = self.MyTextCtrl.GetValue()
        print "it is working! ",textvalue

        resultstr = "test successful"
        return resultstr

rawDescription: binding packages with wxpython

description: Binding packages with wxpython


code:
import numpy as np
a = np.array([10,20]).reshape(2,1)
b = np.array([100,200,300]).reshape(1,3)
c = np.arange(1,11).reshape(1,1,10)

print a + b
print a + c
print b[:,:,np.newaxis] + c

rawDescription: Python, numpy : 3 dimensions broadcast

description: Python, numpy : 3 dimensions broadcast


code:
from subprocess import check_output
cmd = '(python-flip RUN-NONINTERACTIVE "/tmp/test.jpg")'
output = check_output(['/usr/bin/gimp', '-i', '-b', cmd, '-b', '(gimp-quit 0)'])
print output

rawDescription: Writing a GIMP python script

description: Writing a GIMP python script


code: print " ".join(hex(ord(n)) for n in my_hex)

rawDescription: Python: print a variable in hex

description: Python: print a variable in hex


code:
from pexpect import replwrap

with open("commands.txt", "r") as f:
    commands = [command.strip() for command in f.readlines()]

repl = replwrap.python()
for command in commands:
   print ">>>", command
   print repl.run_command(command),

rawDescription: Piping commands into the Python REPL

description: Piping commands into the Python REPL


code:
import random
import mptest as smt

if __name__ == '__main__':
    l = []
    for i in range(4):
        l.append(random.randint(1,8))
    print "random numbers generated"
    print smt.mp_factorizer(l, 4)

rawDescription: using multiprocessing in a sub process

description: Using multiprocessing in a sub process


code:
import nltk

lhs = nltk.grammar.Nonterminal('singapore')
rhs = [u'singapore']
singapore_production = nltk.grammar.Production(lhs, rhs)
new_grammar = nltk.data.load('grammars/large_grammars/atis.cfg')
new_grammar._productions.append(singapore_production)
lhs2 = nltk.grammar.Nonterminal('NOUN_NP')
new_grammar._productions.append(nltk.grammar.Production(lhs2, [lhs]))

# Create newer grammar from new_grammar's string
newer_grammar =  nltk.grammar.CFG.fromstring(str(new_grammar).split('\n')[1:])
# Reassign new_grammar's string to newer_grammar !!!
newer_grammar._start = new_grammar.start()
newer_grammar
sent = ['show', 'me', 'northwest', 'flights', 'to', 'singapore', '.']
print newer_grammar.productions()[2091]
print newer_grammar.productions()[-1]
newer_parser = nltk.ChartParser(newer_grammar)
for i in newer_parser.parse(sent):
    print i
    break

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


code:
s = 'Name\nAge\nPosition\nSalary\nJohn Doe\n21\nTest Analyst\n$60,000' #partial list
k = '4'
print '\n'.join([' | '.join(s.split('\n')[i:i+int(k)]) for i in range(0,len(s.split('\n')),int(k))])

rawDescription: How to write the Automator/Applescript Service for "Selected Text" Manipulation converted from working Python "text from file" Manipulation code

description: Write the Automator/Applescript Service for "Selected Text" Manipulation converted from working Python "text from file" Manipulation code


code:
import numpy
import os, sys
import math
import fileinput
from random import random

class generalSim (object):

    def __init__(self,KVDir):
        self.KVDir = KVDir

    def calcIList(self,params):
        n = 0
        iList = numpy.zeros(shape = (1))
        for line in fileinput.input([self.KVDir]):
            iList.resize(n+1)
            kvAs = line.split(",")
            kvAx = {kvA.split('=')[0]:float(kvA.split('=')[1]) for kvA in kvAs}
            iList[n] = intFn(kvAx,params)
            sys.stdout.write(str(n)+"\r")
            sys.stdout.flush()
            n+=1

        return iList

    def simulate(self,nTrueDir,inputKVDir,outputWeightDir,iList,iMax):

        nTrueList = [((1.0/(iList.shape[0]))*(iList.sum(0)))]
        numpy.save(nTrueDir,nTrueList)

        wList=iList[:]/iMax

        wnList=numpy.zeros(shape=(wList.shape[0]))

        for wn in range(len(wList)):
            if wList[wn]>random():
                wnList[wn] = 1

        numpy.save(outputWeightDir,wnList)

def intFn(kVars,params): # Do not change the name of this function
    sinTh = kVars['y']/(numpy.sqrt(kVars['y']**2+(1000.)**2))
    I = 4*((numpy.sin((numpy.pi*.7*sinTh)/.0006)/((.7*sinTh)/.0006))**2)*numpy.cos((numpy.pi*1.2*sinTh)/.0006)**2

    if not math.isnan(I):
        return I
    else:
        print 'Hello'
        return 0.0

'''
sinTh = kVars['y']/(numpy.sqrt(kVars['y']**2+(params['R0'])**2))
A1 = numpy.complex(params['A1r'],params['A1i'])*numpy.e**(((-numpy.pi*params['p']*sinTh)/.0006)*1j)
A2 = numpy.complex(params['A2r'],params['A2i'])*numpy.e**(((numpy.pi*params['p']*sinTh)/.0006)*1j)
return (A1+A2)*numpy.conjugate(A1+A2)
'''

def simFn():
    """
        This is the function that will do the actual simulating. Fill out the filepaths below.
    """
    nTrueDir="./data/nTrueSF.npy"#Location to save the nTrue File
    inputKVDir="./data/flatY.txt"#Loaction of the Generated MC KV text File
    outputWeightDir="./data/wnListSF.npy"#Location where the weighted mask file will be saved

    iList = numpy.load("./data/iListSF.npy")#Location of the list of intensities.

    iMax = iList.max()
    print iMax
    gS.simulate(nTrueDir,inputKVDir,outputWeightDir,iList,iMax)

inputKVDir="./data/FlatY.txt"#Loaction of the Generated MC KV File(Same as above)

gS = generalSim(KVDir = inputKVDir)

if sys.argv[1] == "i":
    numpy.save("./data/iListSF.npy",gS.calcIList({'A2r': 186.55622562665766, 'R0': 1000.0, 'A1r': 186.5549385015023, 'p': 1.2010987369259563, 'A2i': 0.0, 'A1i': 0.4681942082158097}))

elif sys.argv[1] == "s":
    simFn()

rawDescription: Python: Cannot import name/IndexError: List index out of range


code: print MyClass.__dict__

rawDescription: Python Class. key exists as self.key, but doesn't exists in self.__dict__


code:
for song in songdata:
    print song

rawDescription: How to print out __str__ for each instance of a class in a loop

description: Print out __str__ for each instance of a class in a loop


code:
import time

lastResult = 100

def checkNextID(ID, lastResult = lastResult, diff = [8,18,7,17,6,16,5,15]):
    runs = 0
    SEEN = set()

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

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

checkNextID(0)

rawDescription: Python Crawler - need help with my algorithm


code:
for key, value in df_gen:
    value["ROLL_CORR"] = pd.rolling_corr(value["Value1"],value["Value2"], 3)
    print value

rawDescription: Calculate rolling correlation with pandas


code:
import math

def isPower (num, base):
    if base == 1 and num != 1: return False
    if base == 1 and num == 1: return True
    if base == 0 and num != 1: return False
    power = int (math.log (num, base) + 0.5)
    return base ** power == num

print isPower (127,2)       # false
print isPower (128,2)       # true
print isPower (129,2)       # false
print

print isPower (26,3)        # false
print isPower (27,3)        # true
print isPower (28,3)        # false
print isPower (3**10,3)     # ftrue
print isPower (3**129,3)    # true
print

print isPower (5,5)         # true
print isPower (1,1)         # true
print isPower (10,1)        # false

rawDescription: how to check if a number is a power of base b?

description: Check if a number is a power of base b


code:
results = {}
for key in data.keys():
    # key is '20101021', '20101004'...
    # data[key].keys() is '4x4, '4x2'... so let's make sure
    # that the result dictionary contains all those '4x4', '4x2'
    # being zero if nothing better can be calculated.
    results[key] = dict.fromkeys(data[key].keys(), 0)

    for sub_key in data[key].keys():
        # sub_key is '4x4', '4x2'...
        # Also, don't consider a 'valid value' someting that is not a
        # "Central Spectrum" or a "Full Frame"
        valid_values = [
            int(v) for k, v in data[key][sub_key].items()
            if k in ["Central Spectrum", "Full Frame"]
        ]
        # Now add the 'valid_values'
        results[key][sub_key] = sum(valid_values)
print results

rawDescription: summing nested dictionary entries

description: Summing nested dictionary entries


rawDescription: How to get list of the range consisting of floats?

description: Get list of the range consisting of floats


code:
from BeautifulSoup import BeautifulSoup
filename = '/tmp/cyrillic'
fread = open(filename, 'r')
source = fread.read()
source  # Scary
source = source.decode('cp1251')  # Let's try getting this right.
soup = BeautifulSoup(source)
soup  # OK, that's looking right now. Note the </br> was dropped as that's bad HTML with no meaning.
soup.find('span', 'one').findNextSibling(text=True)
soup.find('span', 'two').findNextSibling(text=True)  # This looks a bit daunting ...
print _  # ... but it's not, really. Just Unicode chars.
# Then you may also wish to get things by text:
print soup.find(text=u'Привет').findParent().findNextSibling(text=True)
# You can't get things by attributes and the contained NavigableString at the same time, though. That may be a limitation.

rawDescription: How to solve problem with parsing html file with cyrillic symbol?

description: Solve problem with parsing html file with cyrillic symbol


code:
def parse_the_file():
    lower = str.lower
    split = str.split
    with open('f.txt') as f:
        d = {}
        lines = f.read().split('\n')
        for A,B in [split(l) for l in lines]:
            try:
                d[lower(A)].add(B)
            except KeyError:
                d[lower(A)] = set(B)

        for a in d:
            print "%s - %s" % (a,",".join(list(d[a])))

if __name__ == "__main__":
    parse_the_file()

rawDescription: how to get unique values set from a repeating values list

description: Get unique values set from a repeating values list


code:
#!/usr/bin/python

import grako
import json

grammar = """
final = kvpair;
kvpair = key '=' value;
key = /[^\s=]+/;
value = @:(dict | list | string) [','];
list = '[' @:{ value } ']';
string = '"' @:/[^"]*/ '"';
dict = '{' @:{ kvpair } '}';
"""

class Semantics(object):
    def kvpair(self, arg):
        key, ignore, value = arg
        return { key: value }
    def dict(self, arg):
        d = { }
        for v in arg:
            d.update(v)
        return d

model = grako.genmodel("final", grammar)

with open('out.txt') as f:
    ast = model.parse(f.read(), semantics=Semantics())
    print json.dumps(ast, indent=4)

rawDescription: How can I express this format in EBNF?

description: Express this format in EBNF


code:
import threading
import subprocess
import time

class RunMonitor(threading.Thread):
    def __init__(self, quit_event):
        threading.Thread.__init__(self)
        self.quit_event = quit_event
    def run(self):
        print 'Invoking the command prompt .....\n'
        subprocess.call(["start", "/WAIT", "/DC:\\python27\\sample", "xxx12.bat"], shell=True)
        self.quit_event.set()

class Something:
    def runscript(self):
        print 'Starting the thread...'

        quit_event = threading.Event()
        run_monitor_object = RunMonitor(quit_event).start()

        while True:
            if not quit_event.is_set():
                 print 'The thread is still alive....'
            else:
                 print 'The Thread is not alive anymore'
                 break
            time.sleep(1)

runme = Something()
runme.runscript()

rawDescription: Need Assistance in Python Thread programming


code:
def TryMileToKm():
    attempts = 0

    while attempts < 3:
        attempts += 1
        if MileToKm(): break

    if attempts >= 3:
        print 'Invalid input'
        exit(1)

rawDescription: python 3 - limiting invalid inputs

description: Python 3 - limiting invalid inputs


code:
for enzyme in add_gene(target_enzyme_cogs, gene_cog_dict, cog_gene_dict):
    print enzyme

rawDescription: Python: Find subsets across many-to-many mapping


code:
tests = [
    [None, 1, 2, None, 3, 4, 5, None],
    [1, 2, None, 3, 4, 5, None],
    [None,1, 2, None, 3, 4, 5],
    [1, 2, None, 3, 4, 5],
    [1, 2, None],
    [None, 1, 2],
]

def find_borders(L):
    it = iter(L)

    start_none = False

    for item in it:
        if item is None:
            start_none = True
            continue
        if start_none:
            start = item
        else:
            start = None

        end = item

        end_none = False

        for item in it:
            if item is None:
                end_none = True
                break
            end = item
        if not end_none:
            end = None
        yield (start, end)

        start_none = True

for t in tests:
    print
    print t
    for x in find_borders(t):
        print x

rawDescription: Find borders on a list in python

description: Find borders on a list in python


code:
class MyStr(str):
    def __gt__(self, other):
        print '(was called)',
        return int(self) > int(other)

print MyStr(16) > MyStr(7)   # True

rawDescription: Can you override a magic method when extending a builtin in python?

description: Override a magic method when extending a builtin in python


code:
p=subprocess.Popen(["mybatchfile.bat"])
if p.pid > 0:
   print "Process created with PID:", p.pid

rawDescription: How to start a batch file from within a python script and detach from it

description: Start a batch file from within a python script and detach from it


code:
week = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
days1 = ['Mon', 'Tue', 'Wed', 'Fri', 'Sat']
days2 = ['Mon', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
days3 = ['Mon', 'Wed', 'Thu', 'Sat', 'Sun']

def is_consecutive(d1, d2):
    for i in range(0, len(week) - 1):
        if d1 == week[i] and d2 == week[i + 1]:
            return True
    return False

def recursive_weekdays(days, index, in_range=False):
    # Base case
    if index == len(days) - 1:
        return days[index]

    if is_consecutive(days[index], days[index + 1]):
        if not in_range:
            return days[index] + " - " + recursive_weekdays(days, index + 1, True)
        else:
            return recursive_weekdays(days, index + 1, in_range)

    return days[index] + ", " + recursive_weekdays(days, index + 1)

print recursive_weekdays(days1, 0)
print recursive_weekdays(days2, 0)
print recursive_weekdays(days3, 0)

rawDescription: Converting for loops to recursion [finding consecutive weekdays]


code:
from collections import Counter
lis1 = []
lis2 = []
with open("abc") as f:
    c = Counter(line.split()[0] for line in f)

for key,val in c.items():
    if val == 1:
        lis1.append(key)
    else:
        lis2.extend([key]*val)
print lis1
print lis2

rawDescription: compare file line by line python

description: Compare file line by line python


code:
def parse_crippled_shifted_list(driver, frame, selector, level=1, parent_id=0, path=None):
    """
    Traversal of html list of special structure (you can't know if element has sub list unless you enter it).
    Supports start from remembered list element.

    Nested lists have classes "closed" and "last closed" when closed and "open" and "last open" when opened (on <li>).
    Elements themselves have classes "leaf" and "last leaf" in both cases.
    Nested lists situate in <li> element as <ul> list. Each <ul> appears after clicking <a> in each <li>.
    If you click <a> of leaf, page in another frame will load.

    driver - WebDriver; frame - frame of the list; selector - selector to current list (<ul>);
    level - level of depth, just for console output formatting, parent_id - id of parent category (in DB),
    path - remained path in categories (ORM objects) to target category to start with.
    """

    # Add current level list elements
    # This method selects all but loading. Just what is needed to exclude.
    selector.append(' > li > a:not([class=loading])')

    # Wait for child list to load
    try:
        query = WebDriverWait(driver, WAIT_LONG_TIME).until(
            EC.presence_of_all_elements_located((By.CSS_SELECTOR, ''.join(selector))))

    except TimeoutException:
        print "%s timed out" % ''.join(selector)

    else:
        # List is loaded
        del selector[-1]  # selector correction: delete last part aimed to get loaded content
        selector.append(' > li')

        children = driver.find_elements_by_css_selector(''.join(selector))  # fetch list elements

        # Walk the whole list
        for i, child in enumerate(children):

            del selector[-1]  # delete non-unique li tag selector
            if selector[-1] != ' > ul' and selector[-1] != 'ul.ltr':
                del selector[-1]

            selector.append(' > li:nth-child(%i)' % (i + 1))  # identify child <li> by its order pos
            selector.append(' > a')  # add 'li > a' reference to click

            child_link = driver.find_element_by_css_selector(''.join(selector))

            # If we parse freely further (no need to start from remembered position)
            if not path:
                # Open child
                try:
                    double_click(driver, child_link)
                except InvalidElementStateException:
                        print "\n\nERROR\n", InvalidElementStateException.message(), '\n\n'
                else:
                    # Determine its type
                    del selector[-1]  # delete changed and already useless link reference
                    # If <li> is category, it would have <ul> as child now and class="open"
                    # Check by class is priority, because <li> exists for sure.
                    current_li = driver.find_element_by_css_selector(''.join(selector))

                    # Category case - BRANCH
                    if current_li.get_attribute('class') == 'open' or current_li.get_attribute('class') == 'last open':
                        new_parent_id = process_category_case(child_link, parent_id, level)  # add category to DB
                        selector.append(' > ul')  # forward to nested list
                        # Wait for nested list to load
                        try:
                            query = WebDriverWait(driver, WAIT_LONG_TIME).until(
                                EC.presence_of_all_elements_located((By.CSS_SELECTOR, ''.join(selector))))

                        except TimeoutException:
                            print "\t" * level,  "%s timed out (%i secs). Failed to load nested list." %\
                                                 ''.join(selector), WAIT_LONG_TIME
                        # Parse nested list
                        else:
                            parse_crippled_shifted_list(driver, frame, selector, level + 1, new_parent_id)

                    # Page case - LEAF
                    elif current_li.get_attribute('class') == 'leaf' or current_li.get_attribute('class') == 'last leaf':
                        process_page_case(driver, child_link, level)
                    else:
                        raise Exception('Damn! Alien class: %s' % current_li.get_attribute('class'))

            # If it's required to continue from specified category
            else:
                # Check if it's required category
                if child_link.text == path[0].name:
                    # Open required category
                    try:
                        double_click(driver, child_link)

                    except InvalidElementStateException:
                            print "\n\nERROR\n", InvalidElementStateException.msg, '\n\n'

                    else:
                        # This element of list must be always category (have nested list)
                        del selector[-1]  # delete changed and already useless link reference
                        # If <li> is category, it would have <ul> as child now and class="open"
                        # Check by class is priority, because <li> exists for sure.
                        current_li = driver.find_element_by_css_selector(''.join(selector))

                        # Category case - BRANCH
                        if current_li.get_attribute('class') == 'open' or current_li.get_attribute('class') == 'last open':
                            selector.append(' > ul')  # forward to nested list
                            # Wait for nested list to load
                            try:
                                query = WebDriverWait(driver, WAIT_LONG_TIME).until(
                                    EC.presence_of_all_elements_located((By.CSS_SELECTOR, ''.join(selector))))

                            except TimeoutException:
                                print "\t" * level, "%s timed out (%i secs). Failed to load nested list." %\
                                                     ''.join(selector), WAIT_LONG_TIME
                            # Process this nested list
                            else:
                                last = path.pop(0)
                                if len(path) > 0:  # If more to parse
                                    print "\t" * level, "Going deeper to: %s" % ''.join(selector)
                                    parse_crippled_shifted_list(driver, frame, selector, level + 1,
                                                                parent_id=last.id, path=path)
                                else:  # Current is required
                                    print "\t" * level,  "Returning target category: ", ''.join(selector)
                                    path = None
                                    parse_crippled_shifted_list(driver, frame, selector, level + 1, last.id, path=None)

                        # Page case - LEAF
                        elif current_li.get_attribute('class') == 'leaf':
                            pass
                else:
                    print "dummy"

        del selector[-2:]

rawDescription: Python Selenium wait for several elements to load


rawDescription: How to split a csv file on date using python

description: Split a csv file on date using python