code:
import numpy as np
from nltk import ngrams

doc1 = "Singularity is still a confusing phenomenon in physics".split()
doc2 = "Quantum theory still wins over String theory".split()
_vec1 = list(ngrams(doc1, 3))
_vec2 = list(ngrams(doc2, 3))
# Create a full dictionary of all possible ngrams.
vec_dict = list(set(_vec1).union(_vec2))
print 'Vector Dict:', vec_dict
# Now vectorize the documents
vec1 = [1 if ng in _vec1 else 0 for ng in vec_dict]
vec2 = [1 if ng in _vec2 else 0 for ng in vec_dict]
print 'Vectorzied:', vec1, vec2
print 'Similarity:', np.dot(vec1, vec2)

rawDescription: Normalize ranking score with weights

description: Normalize ranking score with weights


code:
from lxml import etree

root = etree.XML('''
    <root>
      <TEXT1>one</TEXT1>
      <TEXT2>two</TEXT2>
      <TEXT3>three</TEXT3>
      <x-TEXT4>but never four</x-TEXT4>
    </root>''')

result1 = root.xpath(
    "//*[re:test(local-name(), '^TEXT.*')]",
    namespaces={'re': "http://exslt.org/regular-expressions"})

result2 = root.xpath("//*[starts-with(local-name(), 'TEXT')]")

assert(result1 == result2)

for result in result1:
    print result.text, result.tag

rawDescription: lxml find tags by regex

description: Lxml find tags by regex


code:
import sys
import csv

with open(sys.argv[2],"rb") as inputFile:
    #removes null bytes
    reader = csv.reader((line.replace('\0','') for line in inputFile),delimiter="\t")
    outputFile = open(sys.argv[3], "wb")
    leadingRemoved,endingremoved=[int(x) for x in sys.argv[1].split(":")]
    #creates a 2d array of all the elements for each row
    lines = [x for x in reader]
    print lines
    #zips all but the first x number of elements in the first and third row
    zipped = zip(lines[0][leadingRemoved:endingremoved],lines[2][leadingRemoved:endingremoved])
    writer = csv.writer(outputFile)
    writer.writerows(zipped)
    print zipped
    outputFile.close()

rawDescription: How to extract specific data from a downloaded csv file and transpose into a new csv file?

description: Extract specific data from a downloaded csv file and transpose into a new csv file


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 argparse

parser = argparse.ArgumentParser()
parser.add_argument('-d', '--dry-run', action='store_true')
parser.add_argument('input_file', type=argparse.FileType('r'))
parser.add_argument('servername', nargs='?')

args = parser.parse_args()
if args.servername is None and not args.dry_run:
    parser.error("Option 'servername' is required when not in dry-run mode.")

print args

rawDescription: OR function with argparse with two variables on the command line in Python


code:
from multiprocessing import Process, Queue

def f(q):
    q.put([42, None, 'hello'])

if __name__ == '__main__':
    q = Queue()
    p = Process(target=f, args=(q,))
    p.start()
    print q.get()    # prints "[42, None, 'hello']"
    p.join()

rawDescription: Return results to parent process using joblib


code:
for list1_dict in list1:
    for k, v in list1_dict.iteritems():
        try:
            resultDict = {v: dict1[k]}
            print resultDict
        except KeyError:
            pass

rawDescription: combining a list of dictionaries with another dictionary

description: Combining a list of dictionaries with another dictionary


code:
import numpy as np

def d():
    a = [1,2,3,4,5]
    b = [10,20,30,40,50]
    c = [i*j for i,j in zip(a,b)]
    return c

def e():
    a = np.array([1,2,3,4,5])
    b = np.array([10,20,30,40,50])
    c = a*b
    return c

#Warning:  Functions with mutable default arguments are below.
# This code is only for testing and would be bad practice in production!
def f(a=[1,2,3,4,5],b=[10,20,30,40,50]):
    c = [i*j for i,j in zip(a,b)]
    return c

def g(a=np.array([1,2,3,4,5]),b=np.array([10,20,30,40,50])):
    c = a*b
    return c

import timeit
print timeit.timeit('d()','from __main__ import d')
print timeit.timeit('e()','from __main__ import e')
print timeit.timeit('f()','from __main__ import f')
print timeit.timeit('g()','from __main__ import g')

rawDescription: Why is numpy slower than python? How to make code perform better


code:
import numpy as np
from itertools import izip

vector = np.array([2.0, 2.24, 3.1, 4.768, 16.8, 16.9,23.5,24.0])
start = np.array([3.0,4.5,6.5,15.2])
end = np.array([7.3,16.2,17.7,25.8])
start_i = vector.searchsorted(start, 'left')
end_i = vector.searchsorted(end, 'right')
output = [vector[s:e] for s, e in izip(start_i, end_i)]
print output
[array([ 3.1  ,  4.768]), array([ 4.768]), array([ 16.8,  16.9]), array([ 16.8,  16.9,  23.5,  24. ])]

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


code:
from bs4 import BeautifulSoup

soup = BeautifulSoup(html, "html.parser")
for item in soup.find_all("small"):
    print '"{}"'.format(item.text.strip())

rawDescription: Extraction Specific Lines From A Website


code:
import re

x = r'hai! \1 <ops> $1 \' \x \\'
print "want to see: "
print x

print "getting: "
print re.sub(".(.).", x, "###")
print "over escaped: "
print re.sub(".(.).", re.escape(x), "###")
print "could work: "
print re.sub(".(.).", x.replace("\\", "\\\\"), "###")

rawDescription: Python re.sub: ignore backreferences in the replacement string


code:
class Foo(object):
    print "bar"

    def __init__(self):
        print "baz"

rawDescription: What code is executed when a class is being defined?


code:
import shlex

if __name__ == '__main__':
    line = 'abcd xvc  23432 "exampe" 366'
    tokens = shlex.split(line)
    for token in tokens:
        print '>{}<'.format(token)

rawDescription: Parse User Input in Python

description: Parse User Input in Python


code:
from traits.api import on_trait_change
# ...
@on_trait_change('inputs_items')
def something_changed(self, name, new):
    # static event listener for self.inputs
    if len(new.added) > 0:
        print "Check it out, we added %s to self.items" % new.added
    elif len(new.removed) > 0:
        print "Check it out, we removed %s from self.items" % new.removed

rawDescription: How can I fire a Traits static event notification on a List?

description: Fire a Traits static event notification on a List


rawDescription: Numpy's __array_interface__ not returning dict


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


code:
chars = "abcdefghijklmnopqrstuvwxyz"
check_string = "i am checking this string to see how many times each character appears"

for char in chars:
  count = check_string.count(char)
  if count > 1:
    print char, count

rawDescription: Counting repeated characters in a string in Python


code:
for x in sorted(set(my_list)):
    z=my_list.count(x)

    if z>1:
        print x, "is repeated", z, "times."
    else:
        print x, "is repeated", z, "time."

rawDescription: Python item sort

description: Python item sort


code:
import numpy
import os
import random

X = []
Y = []

for filename in os.listdir('input'):
    X.append(numpy.load(os.path.join('input', filename), mmap_mode='r'))

for filename in os.listdir('output'):
    Y.append(numpy.load(os.path.join('output', filename), mmap_mode='r'))

indices = [(chunk, row) for chunk, rows in enumerate(X)
                        for row in range(rows.shape[0])]
random.shuffle(indices)

newchunks = 50
newchunksize = len(indices) / newchunks

for i in range(0, len(indices), newchunksize):
    print i
    rows = [X[chunk][row] for chunk, row in indices[i:i + newchunksize]]
    numpy.save('X_shuffled_' + str(i), numpy.array(rows))
    rows = [Y[chunk][row] for chunk, row in indices[i:i + newchunksize]]
    numpy.save('Y_shuffled_' + str(i), numpy.array(rows))

rawDescription: Uniformly shuffle 5 gigabytes of numpy data


code:
import itertools
alphabet = "abcdefghijklmnopqrstuvwxyz"
l=list(alphabet)

# as a list
genList= ["".join(i) for j in xrange(8) for i in itertools.combinations(l, j+1)]
print genList # to test
# as generator
("".join(i) for j in xrange(8) for i in itertools.combinations(l, j+1))

rawDescription: All possible words until the len 8

description: All possible words until the len 8


code:
def yield_from_l1_l2(l1, l2):
    HALF_OF_L1 = len(l1) / 2
    for i in xrange(len(l2)+1):
        yield l1[:HALF_OF_L1] + l2[:i] + l1[HALF_OF_L1:]

l1 = ['a', 'b', 'c', 'd']
l2 = [1, 2, 3, 4]

for l in yield_from_l1_l2(l1, l2):
    print l

rawDescription: Loop and add with 2 lists

description: Loop and add with 2 lists


code:
for i in tree.xpath('//div[@id="element1"]/text()'):
    print i

rawDescription: How do I parse with LXML recursively in an elegant way?

description: Parse with LXML recursively in an elegant way


code:
import datetime
mdate = "2010-10-05"
rdate = "2010-10-05"
mdate1 = datetime.datetime.strptime(mdate, "%Y-%m-%d").date()
rdate1 = datetime.datetime.strptime(rdate, "%Y-%m-%d").date()
delta =  (mdate1 - rdate1).days
print delta
type(delta)

rawDescription: Integer difference in python between two dates


rawDescription: Using regex for mutliple lines

description: Using regex for mutliple lines


rawDescription: Python/Sympy trigonometric solution of cubic equation


code:
## main.py ##
try:
   import swigpackagefoo.swigsubpackagebar as swigModule
except ImportError:
   import swigMock as swigModule

print swigModule.aFunc("FOO")

rawDescription: how do I create a in-line module

description: Create a in-line module


code:
decimals = 3
print '{0:.{1}f}'.format(some_int / 10.0**decimals, decimals)

rawDescription: format() int as float

description: Format() int as float


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

rawDescription: python only recognizes first line of my input

description: Python only recognizes first line of my input


code:
d = range(0,9)
e = len(d)
for p in itertools.permutations(d, e):
    print p

rawDescription: Python Itertools Permutations

description: Python Itertools Permutations


code:
from collections import OrderedDict

def main( ):
    zip_dictionary = OrderedDict()
    zip_dictionary["CAM"] = "1427"
    zip_dictionary["UofM"] = "1427"
    zip_dictionary["ZIT"] = "1423-503"
    zip_dictionary["MCOC"] = "1423"
    zip_dictionary["NAZARETH"] = "1418"
    zip_dictionary["OCC"] = "1420"

    print "\nContents of dict:"
    for key in zip_dictionary:
        print(key, zip_dictionary[key])

    print "\nPopping items:"
    for i in range(2):
        print(zip_dictionary.popitem( ))

    print "\nRemaining items:"
    for key in zip_dictionary:
        print(key, zip_dictionary[key])

main( )

rawDescription: Python dictionary program output issue

description: Python dictionary program output issue


code:
a = np.array([1,2])

a.shape
#(2,)

b = a[np.newaxis, :]

print b
#array([[1,2]])

b.shape
#(1,2)

rawDescription: How do I get a row of a 2d numpy array as 2d array

description: Get a row of a 2d numpy array as 2d array


code:
class Month:
    def __init__(self, nDays, day1):
        self.nDays = nDays
        self.day1 = day1

    def displayCalendar(self):
        week = ' S  M  T  W  T  F  S '
        print week
        days = 1
        if self.day1 == 2:
            print '  ',
        elif self.day1 == 3:
            print '     ',
        elif self.day1 == 4:
            print '        ',
        elif self.day1 == 5:
            print '           ',
        elif self.day1 == 6:
            print '              ',
        elif self.day1 == 7:
            print '                 ',

        for i in range(self.nDays):
            print '{:2}'.format(days),
            if (self.day1 + i) % 7 == 0:
                print ''
            days = days + 1

print '     APRIL 2014'
april2014 = Month(30,3)
april2014.displayCalendar()

rawDescription: How Do I Make a List of Numbers Line Break at a Certain Point?


code:
class X:
    def __iter__(self):
        return Y()

class Y:
    def __iter__(self):
        return iter('xyz')

a = iter(X())
b = iter(a)
print a is b # False

rawDescription: Using iter on an existing iterator in Python: What happens to the iterator?


code:
import json
s = '{"data":{"internalName":"value","int":1}}'
data = json.loads(s)
print data
data['data']['internalName']

rawDescription: Having trouble with decoding JSON in Python


code:
contours, hierarchy = cv2.findContours(opening.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
print len(contours)

for contour in contours:
    (x,y),radius = cv2.minEnclosingCircle(contour)
    center = (int(x),int(y))
    radius = int(radius)
    cv2.circle(img,center,radius,(0,255,0),2)
    # labelling the circles around the centers, in no particular order.
    position = (center[0] - 10, center[1] + 10)
    text_color = (0, 0, 255)
    cv2.putText(img, str(i + 1), position, cv2.FONT_HERSHEY_SIMPLEX, 1, text_color, 3)

rawDescription: How to detect bullet holes on the target using python

description: Detect bullet holes on the target using python


code:
new_args = (10,20)

def process(func):
    def new_func(*args, **kw):
        func_defaults = func.func_defaults
        func.func_defaults = new_args
        result = func(*args, **kw)
        func.func_defaults = func_defaults
        return result
    return new_func

@process
def foo(x=1, y =2):
    print x + y

rawDescription: Prepend function call in other function in Python


code: print "\n".join(["%so%s" % (l,l) for l in set(raw_input("type smth: ")).difference('aeiou')])

rawDescription: Getting wrong output when working with tuple and list in Python


code:
@lazyinit
class A(object):
    def __init__(self, a, b):
        print("initializing...")
        self.x = a + b + 2

    def foo(self):
        return self.x

x = A(39, 1)
print x
print x.foo()
print x.foo()

rawDescription: Automatically create (and keep) an object when accessed


code:
def coroutine(func):
    def start(*args,**kwargs):
        cr = func(*args,**kwargs)
        cr.next()
        return cr
    return start

@coroutine
def grep(pattern):
    print "Looking for %s" % pattern
    while True:
        line = (yield)
        if pattern in line:
            print(line)

g = grep("python")
# Notice how you don't need a next() call here
g.send("Yeah, but no, but yeah, but no")
g.send("A series of tubes")
g.send("python generators rock!")
# Looking for python
# python generators rock!

rawDescription: python - support .send() for a class?

description: Python - support .send() for a class


code:
# Save visited boxes. Only for debugging purpose.
visited_boxes = []

def check_for_stores(bounding_box):
    """Function mocking real `ckeck_fo_stores` function by returning
    random list of "stores"
    """
    import random
    randint = random.randint(1, 12)
    print 'Found {} stores for bounding box {}.'.format(randint, bounding_box)
    visited_boxes.append(bounding_box)
    return ['store'] * randint

def split_bounding_box(bounding_box, cell_axis_reduction_factor=2):
    """Returns generator of bounding box coordinates splitted
    from parent `bounding_box`

    :param bounding_box: tuple containing coordinates containing tuples of
          lower-left and upper-right corner coordinates,
          e.g. ((0, 5.2), (20.5, 14.0))
    :param cell_axis_reduction_factor: divide each axis in this param,
                                       in order to produce new box,
                                       meaning that in the end it will
                                       return `cell_axis_reduction_factor`**2 boxes
    :return: generator of bounding box coordinates

    """
    box_lc, box_rc = bounding_box
    box_lc_x, box_lc_y = box_lc
    box_rc_x, box_rc_y = box_rc

    cell_width = (box_rc_x - box_lc_x) / float(cell_axis_reduction_factor)
    cell_height = (box_rc_y - box_lc_y) / float(cell_axis_reduction_factor)

    for x_factor in xrange(cell_axis_reduction_factor):
        lc_x = box_lc_x + cell_width * x_factor
        rc_x = lc_x + cell_width

        for y_factor in xrange(cell_axis_reduction_factor):
            lc_y = box_lc_y + cell_height * y_factor
            rc_y = lc_y + cell_height

            yield ((lc_x, lc_y), (rc_x, rc_y))

def get_stores_in_box(bounding_box, result=None):
    """Returns list of stores found provided `bounding_box`.

    If there are more than or equal to 10 stores found in `bounding_box`,
    recursively splits current `bounding_box` into smaller one and checks
    stores in them.

    :param bounding_box: tuple containing coordinates containing tuples of
          lower-left and upper-right corner coordinates,
          e.g. ((0, 5.2), (20.5, 14.0))
    :param result: list containing found stores, found stores appended here;
                   used for recursive calls
    :return: list with found stores

    """
    if result is None:
        result = []

    print 'Checking for stores...'
    stores = check_for_stores(bounding_box)
    if len(stores) >= 10:
        print 'Stores number is more than or equal 10. Splitting bounding box...'
        for splitted_box_coords in split_bounding_box(bounding_box):
            get_stores_in_box(splitted_box_coords, result)
    else:
        print 'Stores number is less than 10. Saving results.'
        result += stores

    return result

stores = get_stores_in_box(((0, 1), (30, 20)))
print 'Found {} stores in total'.format(len(stores))
print 'Visited boxes: '
print visited_boxes

rawDescription: Web scraping: Expand/contract bounding box depending on results


code:
from unicodedata import name
for c in re.findall(r'\s', chrs, re.UNICODE):
    print repr(c), name(c, '')

rawDescription: In Python, how to list all characters matched by POSIX extended regex `[:space:]`?


code: print sorted(student_tuples, key=lambda student: student[2])

rawDescription: Using sorted() in Python

description: Using sorted() in Python


code:
infoconf = raw_input("Is this information correct? Y/N: ")

#you wrote:  infoconf == "N" or "No" but should be:
if infoconf == "N" or infoconf == "No":
  print "Please try again."
#you wrote: infoconf == "Y" or "Yes" but should be
elif infoconf == "Y" or infoconf == "Yes":
  start()
else:
  print "That is not a yes or no answer, please try again."

rawDescription: If-branch for x == "N" or "No" always runs


code: print response.registers

rawDescription: Reading registers with pymodbus

description: Reading registers with pymodbus


code:
class Validatable(object):
    def __init__(self):
        self.__class__.fieldName = "value for " + self.__class__.__name__

class Demo(Validatable):
    def __init__(self):
        super(Demo, self).__init__()

class Demo2(Validatable):
    def __init__(self):
        super(Demo2, self).__init__()

d = Demo()
d2 = Demo2()
print "Demo.fieldName = " + Demo.fieldName
print "Demo2.fieldName = " + Demo2.fieldName

rawDescription: Inherited but not shared (static) class members in Python


code:
import os
from os.path import join, getsize
size = 0
count = 0
for root, dirs, files in os.walk('.'):
    size += sum(getsize(join(root, name)) for name in files)
    count += len(files)
print count, size

rawDescription: Fast calculation of the size and counting all files in a directory and subdirectories using python (cross platform)


code:
class B(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def replace(self, x):
        self.x.s = self.x.s.replace(self.y, x)
class A(object):
    def __init__(self, s):
        self.s = s
    def __getattr__(self, name):
        return B(self, name)

a = A('abc')
a.b.replace('X')
print a.s

rawDescription: Python List methods

description: Python List methods


code:
import sys

class Base(object):
    def A(self):
        print '  in method A() of a {} instance'.format(self.__class__.__name__)

    def B(self):
        print '  in method B() of a {} instance'.format(self.__class__.__name__)
        if sys._getframe(1).f_code.co_name != 'A':
            print '    caller is not A(), aborting'
            return
        print '    called from A(), continuing execution...'

class Derived(Base):
    def A(self):
        print "  in method A() of a {} instance".format(self.__class__.__name__)
        print '    calling self.B() from A()'
        self.B()

print '== running tests =='
base = Base()
print 'calling base.A()'
base.A()
print 'calling base.B()'
base.B()
derived = Derived()
print 'calling derived.A()'
derived.A()
print 'calling derived.B()'
derived.B()

rawDescription: python: enforce that a class method is only called from within another class method?

description: Python: enforce that a class method is only called from within another class method


code:
infile = open(YourFilename)
for line in infile.read():
    (A, B) = line.split(':')
    As = [eval(a).strip() for a in A.split(',')]
    Bs = [eval(b).strip() for b in B.split(',')]
    Com = [A for A in As if A in Bs]
    print As, ':', Bs, '::', Com

rawDescription: Python Print common values into column

description: Python Print common values into column


code:
for mention in men:
    print mention['user']['screen_name']
    if mention['contributors']:
        print [con['screen_name'] for con in mention['contributors']]

rawDescription: JSON or SimpleJSON and Python with Twython


code:
def hamming(s, k, depth):
    print ' '*(4*depth) + 'hamming(' + s + ', ' + str(k) + ')'
    if k>1:
       c = s[-1]

       if len(s) >= k:
          s1=[]
          for y in hamming(s[:-1], k, depth+1):
              s1.append(y+c)
       else:
          s1=[]
       s2 = [y+flip(c) for y in hamming(s[:-1], k-1, depth+1)]

       r = []
       r.extend(s1)
       r.extend(s2)
       print ' '*(4*depth) + 'r = ' +str( r)
       return r

    else:
       x = [flip_s(s,i) for i in range(len(s))]
       print ' '*(4*depth) + 'x = ' + str(x)
       return x

rawDescription: How does a for loop containing a function work?


code:
import networkx as nx

class myGraph(nx.Graph):
    def __eq__(self, other):
        return nx.is_isomorphic(self, other)
    def __hash__(self):
        return hash(tuple(sorted(self.degree().values())))

if __name__ == '__main__':
    G1 = myGraph([(1,2)])
    G2 = myGraph([(2,3)])
    G3 = myGraph([(1,2),(2,3)])
    print G1.__hash__(), G1.edges()
    print G2.__hash__(), G2.edges()
    print G3.__hash__(), G3.edges()
    print G1 == G2
    print G1 == G3
    graphs = {}
    graphs[G1] = 'G1'
    graphs[G2] = 'G2'
    graphs[G3] = 'G3'
    print graphs.items()

rawDescription: 'Isomorphic' comparison of NetworkX Graph objects instead of the default 'address' comparison


code:
class A:
   class_attribute = 10
   def class_method(self):
       self.instance_attribute = 'I am instance attribute'

print A.__dict__
#{'__module__': '__main__', 'class_method': <function class_method at 0x10978ab18>, 'class_attribute': 10, '__doc__': None}

rawDescription: Difference between Class Attributes, Instance Attributes, and Instance Methods in Python


code:
import os, os.path
print os.chdir(os.path.join(given_dir, os.pardir))

rawDescription: Using chdir to move up a directory in Python


code:
#!/usr/bin/env python

import antlr3
from antlr3 import *
from antlr3.tree import *
from CommentLexer import *
from CommentParser import *

# http://www.antlr.org/wiki/display/ANTLR3/Python+runtime
# http://www.antlr.org/download/antlr-3.1.3.jar

def print_level_order(tree, indent):
  print '{0}{1}'.format('   '*indent, tree.text)
  for child in tree.getChildren():
    print_level_order(child, indent+1)

input = '/*aaa1/*bbb/*ccc*/*/aaa2*/'
char_stream = antlr3.ANTLRStringStream(input)
lexer = CommentLexer(char_stream)
tokens = antlr3.CommonTokenStream(lexer)
parser = CommentParser(tokens)
tree = parser.parse().tree
print_level_order(tree, 0)

rawDescription: ANTLR get and split lexer content

description: ANTLR get and split lexer content


code:
ne_in_sent = []
for subtree in ne_tree:
    if type(subtree) == Tree: # If subtree is a noun chunk, i.e. NE != "O"
        ne_label = subtree.label()
        ne_string = " ".join([token for token, pos in subtree.leaves()])
        ne_in_sent.append((ne_string, ne_label))
print ne_in_sent

rawDescription: Extract list of Persons and Organizations using Stanford NER Tagger in NLTK


code:
class ClassOrder(ast.NodeVisitor):
    identifiers = None
    def visit_ClassDef(self, node):
        self.identifiers = []
        for child in node.body:
            if isinstance(child, ast.Assign):
                for target in child.targets:
                    self.visit(target)
            elif isinstance(child, ast.FunctionDef):
                self.identifiers.append(child.name)
    def visit_Name(self, node):
        if self.identifiers is not None:
            self.identifiers.append(node.id)

tree = ast.parse(sourcecode)
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:
import numpy as np

data = np.array([10, 15, 5, 25])
diff = np.abs(data[:-1] - data[1:])

print diff
array([ 5, 10, 20])

rawDescription: Using Python to create a new Numpy array from an existing one


rawDescription: Calculating change in percentage between two numbers (Python)


code: print names[0]

rawDescription: Basic Python question: referencing original variable inside for loop?


code:
import sys
print sys.version

rawDescription: How to configure ipy_user_conf.py to get IPython to to start with the right IDLE set as editor?

description: Configure ipy_user_conf.py to get IPython to to start with the right IDLE set as editor


code:
for x in my_list:
   print x.get()

rawDescription: python to add variables to list by identifying from a line

description: Python to add variables to list by identifying from a line


code:
for filename, next in zip(filenames, filenames[1:]):
    print filename, next

rawDescription: When walking through files and folders, how do I compare the current filename with the next, in python?


code:
import csv

with open('unload.cmd', 'r') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        print row[0]

rawDescription: searching beautiful soup output without html tags

description: Searching beautiful soup output without html tags


code:
A = [('x1','y1'), ('x2','y2'), ('x3','y3')]
B = [('x1','y1'), ('x2','y5'), ('x4','y4')]

dA = dict(A)
dB = dict(B)

#Intersection, the simple way
print 'Result 1a:', list(set(A) & set(B))

#Intersection using dicts instead of sets
result = [(k, vA) for k, vA in dA.iteritems() if dB.get(k) == vA]
print 'Result 1b:', result

#match on 1st tuple element, ignoring 2nd element
result = {}
for k, vA in dA.iteritems():
    vB = dB.get(k)
    if vB:
        result[k] = (vA, vB) if vB != vA else vA
print 'Result 2a:', result.items()

#match on 1st tuple element only if 2nd elements don't match
result = {}
for k, vA in dA.iteritems():
    vB = dB.get(k)
    if vB and vB != vA:
        result[k] = (vA, vB)
print 'Result 2b:', result.items()

#unique elements of B, ignoring 2nd element
result = [(k, vB) for k, vB in dB.iteritems() if k not in dA]
print 'Result  3:', result

rawDescription: Set theory magic on list of tuples

description: Set theory magic on list of tuples


code:
class B2: # doesn't have to inherit from A
    def __init__(self, a, field2):
        self._a = a # using composition instead
        self.field2 = field2

    @property
    def field(self): # pass accesses to a
        return self._a.field
    # could provide setter, deleter, etc

a = A("spam")
b = B2(a, "ham")

print b.field, b.field2 # prints "spam ham"

rawDescription: Converting an object into a subclass in Python?


code:
def str_to_list(x):
    return x.split(', ')

y = "a, b, c, d, e"
y = str_to_list(y)
print y

rawDescription: Python 2.7 how to turn the string into a list outside the function with str.split()?


code:
result = calculate_coordinates(spam, ham, eggs)
print result.x, result.y

rawDescription: Assign attribute to function

description: Assign attribute to function


code:
def animal_name(animal):
  # here be dragons
  return some_string

cat = 5
print animal_name(cat)  # prints "cat"

rawDescription: how to convert variable into string in python

description: Convert variable into string in python


code:
for t in l:
        print datetime.strptime(t[:t.find(',',t.find(',')+1)],'%d/%m/%Y,%H:%M:%S')

rawDescription: datetime strptime - set format to ignore part of string

description: Datetime strptime - set format to ignore part of string


code:
# Initialize our dictionary with list values for the two special cases.
mydict = {'has_a' : [], 'has_b' : []}
list_of_names = ['aaron', 'boo', 'charlie']
for name in list_of_names:
    if  'a' in name:
        mydict['has_a'].append(name)
    # When not in a special case, just use the dictionary like normal to assign values.
print mydict['has_a']

rawDescription: How do I create a list as a dictionary entry for a specific key in Python?

description: Create a list as a dictionary entry for a specific key in Python


code:
import datetime as dt

dow={d:i for i,d in
         enumerate('Mon,Tue,Wed,Thu,Fri,Sat,Sun'.split(','))}

def next_dow(d,day):
    while d.weekday()!=day:
        d+=dt.timedelta(1)

    return d

d1=min(next_dow(dt.datetime(2013,4,1),day)
                 for day in (dow['Tue'],dow['Thu']))
d2=min(next_dow(dt.datetime(2013,4,2),day)
                 for day in (dow['Mon'],dow['Wed'],dow['Fri']))

for d in d1,d2:
    print d.strftime('%Y-%m-%d')

rawDescription: Find next date by day of week

description: Find next date by day of week


code:
import numpy as np

A = np.array(
[[1, 1],
[2, 2],
[3, 3],])

B = np.array(
[[2, 1],
[3, 2],
[4, 3],
[5, 4]])

C = np.array(
[[2, 2],
[3, 1],
[5, 2],])

I = reduce(
    lambda l,r: np.intersect1d(l,r,True),
    (i[:,0] for i in (A,B,C)))

print A[np.searchsorted(A[:,0], I)]
print B[np.searchsorted(B[:,0], I)]
print C[np.searchsorted(C[:,0], I)]

rawDescription: filtering multiple numpy arrays based on the intersection of one column

description: Filtering multiple numpy arrays based on the intersection of one column


code:
def f(x):
    global firstPoint, iteration, x_history, fx_history

    iteration = iteration + 1
    powerFailure = True
    failedIteration = 10
    previousPoint = False
    eps = 1e-12

    if powerFailure == True:
        if iteration == failedIteration:
            raise Exception("Optimization Ended Early Due to Power Failure")

    for i in range(len(x_history)):
        if abs(x_history[i,0]-x[0])<eps and abs(x_history[i,1]-x[1])<eps:
            previousPoint = True
            firstPoint=False
            fx = fx_history[i,0]
            print "%d: f(%f,%f)=%f (using history)" % (iteration,x[0],x[1],fx)

    if previousPoint == False:
        fx = rosen(x)
        print "%d: f(%f,%f)=%f" % (iteration,x[0],x[1],fx)

    if firstPoint == True:
        x_history = np.atleast_2d([x])
        fx_history = np.atleast_2d([fx])
        firstPoint = False
    else:
        x_history = np.concatenate((x_history,np.atleast_2d(x)),axis=0)
        fx_history = np.concatenate((fx_history,np.atleast_2d(fx)),axis=0)

    return fx

rawDescription: How do you stop the scipy.optimize.fmin function and start it again using the optimization history?

description: Stop the scipy.optimize.fmin function and start it again using the optimization history


code:
import sys
import os
if __name__=="__main__":
 if len(sys.argv) > 1:
   p = sys.argv[1]
 else:
  p = os.path.abspath(os.path.curdir)

 print p

rawDescription: take path of script as an argument python

description: Take path of script as an argument python


code:
import xml.etree.ElementTree as xee
contents='''\
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Ip>74.125.45.100</Ip>
  <Status>OK</Status>
  <CountryCode>US</CountryCode>
  <CountryName>United States</CountryName>
  <RegionCode>06</RegionCode>
  <RegionName>California</RegionName>
  <City>Mountain View</City>
  <ZipPostalCode>94043</ZipPostalCode>
  <Latitude>37.4192</Latitude>
  <Longitude>-122.057</Longitude>
  <TimezoneName>America/Los_Angeles</TimezoneName>
  <Gmtoffset>-25200</Gmtoffset>
  <Isdst>1</Isdst>
</Response>'''

doc=xee.fromstring(contents)
print dict(((elt.tag,elt.text) for elt in doc))

rawDescription: What's an easy and fast way to put returned XML data into a dict?


code:
chapters = ['one', 'two', 'three',]

for x in chapters:
    print x
    if x != chapters[-1]:
        print 'next'

rawDescription: Python for loop break on the last iteration using slicing


code:
import random
import mptest as smt

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

rawDescription: using multiprocessing in a sub process

description: Using multiprocessing in a sub process


code:
#It's good practice to define functions first, too keep definitions in one place

def queuetimer(): #function for resetting the queue every 30 seconds
    global queue
    print 'queue reset'
    queue = 0
    threading.Timer(30,queuetimer).start()

def message(msg): #function for sending messages to the IRC chat
    global queue
    queue = queue + 1
    print queue
    if queue < 20: #ensures does not send >20 msgs per 30 seconds.
        irc.send('PRIVMSG ' + channel + ' :' + msg + '\r\n')
    else:
        print 'Message deleted'

def socialtimer(): #function for announcing social
    global ntimer
    z = open('E:\mIRC\Twitter.txt')
    SOCIAL = z.read()
    message (SOCIAL)
    print 'Social Timers Started!'
    ntimer = threading.Timer(1200,socialtimer)
    ntimer.start()

queue = 0 #sets variable for anti-spam queue functionality

newsmsg = 'whitelist'

irc = socket.socket()
irc.connect((server, 6667)) #connects to the server

#sends variables for connection to twitch chat
irc.send('PASS ' + password + '\r\n')
irc.send('USER ' + nick + ' 0 * :' + bot_owner + '\r\n')
irc.send('NICK ' + nick + '\r\n')
irc.send('JOIN ' + channel + '\r\n')

queuetimer()

while True:
    data = irc.recv(1024) #gets output from IRC server, 1024 is a better number than 1204
    #make sure data isn't an empty string
    if data != '':
        user = data.split(':')[1]
        user = user.split('!')[0] #determines the sender of the messages
        print (data)
    else:
        print ("Nothing to get from the server")

rawDescription: Python: IndexError: list index out of range with IRC Bot


code:
data={}
with open(fn) as f:
    reader=csv.reader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
    header=next(reader)
    for row in reader:
        data.setdefault(row[1], []).append(int(row[2]))

print 'key\tmin\tmax'
for k in data.keys():
    print '{}\t{}\t{}'.format(k, min(data[k]), max(data[k]))

rawDescription: Python max and min of records with same attributes


rawDescription: Can I make this (Python) regular expression any faster?

description: Make this (Python) regular expression any faster


code:
import subprocess
cmd = ['/home/user/workspace/vmlinkedclone.pl', '--server', '192.168.20.2', '--username', 'root', '--password', 'root' ,'--vmbase_id', '2', '--vm_destination_id', '41', '--vmname_destination', 'clone-41-snapname', '--snapname', 'Snapname']
pipe = subprocess.Popen(cmd, shell = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = pipe.communicate()
result = out.decode()
print "Result : ",result

rawDescription: Subprocess.Popen() => No output

description: Subprocess.Popen() => No output


code:
import collections
from collections import Counter
from math import log

corpus = ['the school boy is reading', 'who is reading a comic?', 'the little boy is reading',
          'James the school boy is always busy reading']

txt = corpus[-1]

txt2 = Counter(txt.split())
corpus2 = [Counter(x.split()) for x in corpus]

def tfidf(doc, _corpus):
    dic = collections.defaultdict(int)
    for x in _corpus:
        for y in x:
            dic[y] += 1.
    return {x: doc[x] * log(len(_corpus) / dic[x]) for x in doc}

txt_tfidf = tfidf(txt2, corpus2)
corpus_tfidf = [tfidf(x, corpus2) for x in corpus2]

print txt_tfidf

rawDescription: Implementation of TFIDF weighting scheme


code:
from collections import Counter

line_frequencies = Counter()

with open('data.txt') as data:
    lines = [line.split() for line in data]

for line in lines:
    unique_line = set(line)
    line_frequencies.update(unique_line)

for line in lines:
    for term in line:
        print "%s , count line: %s" % (term, line_frequencies[term])
    print "\n"

rawDescription: Find word in line and count number of line


code:
from BeautifulSoup import BeautifulSoup

def getPrintUnicode(soup):

    body=''
    if isinstance(soup, unicode):
        soup = soup.replace(''',"'")
        soup = soup.replace('"','"')
        soup = soup.replace(' ',' ')
        soup = soup.replace('>','>')
        soup = soup.replace('<','<')
        body = body + soup
    else:
        if not soup.contents:
            return ''
        con_list = soup.contents
        for con in con_list:
            body = body + getPrintUnicode(con)
    return body

print getPrintUnicode(BeautifulSoup('15	 	'))

rawDescription: detecting numbers in an html file using python 3.2

description: Detecting numbers in an html file using python 3.2


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

R, C = 1500, 3000
DATA = np.random.random((R, C))
DATA[::2, :] *= -1  # make every other line negative
Yi, Xi = 1, 10 # increment
CMP = 'seismic'
ImageFormat ='pdf'
Name = 'Image'

DataRange = (np.absolute(DATA)).max() # I want my data centred on 0
EXTENT = [0, Xi*C, 0 ,Yi*R]
NORM = matplotlib.colors.Normalize(vmin =-DataRange, vmax= DataRange, clip =True)

for i in (1,):
    print i
    Fig=plt.figure(figsize=(45, 10), dpi = 100*i, tight_layout=True)
    Fig.suptitle(Name+str(i)+'00DPI')
    ax = Fig.add_subplot(1, 1, 1)
    Plot = ax.imshow(DATA, cmap=plt.get_cmap(CMP), norm = NORM, extent = EXTENT, aspect = 1, interpolation='none')
    ax.set_xlabel('metres')
    ax.set_ylabel('metres')
    vsize=ax.get_position().size[1]  #fraction of figure occupied by axes
    axesdpi= int((Fig.get_size_inches()[1]*vsize)/R)  #(or Yi*R according to what you want to do)
    Fig.savefig(Name+str(axesdpi)+'DPI.'+ImageFormat,  format = ImageFormat, dpi = axesdpi)
    #plt.close()

rawDescription: Plotting at full resolution with matplotlib.pyplot, imshow() and savefig()?


code:
from itertools import chain
print tuple(chain(['a', 'b', 'c'], 'd', 'e'))

rawDescription: How to unpack a list?

description: Unpack a list


code:
from pyparsing import *

kw = Combine(Word(alphas + nums) + Literal(';'))('KEY')
body1 = OneOrMore(~kw + Word(alphas + nums))('Body')
g1 = OneOrMore(Group(kw + body1))

msg = [  """NOW; is the time for a few good ones to come to the aid
of new things to come for it is almost time for
a tornado to strike upon a small hill
when least expected.
lastly; another day progresses and
then we find that which we seek
and finally we will
find our happiness perhaps its closer than 1 or 2 years or not so
    """,
             '',
          ][0]

result = g1.parseString(msg)
# we expect multiple groups, each containing "KEY" and "Body" names,
# so iterate over groups, and dump the contents of each
for res in result:
    print res.dump()

rawDescription: getting a grammar to read more than one keyword in the text

description: Getting a grammar to read more than one keyword in the text


code:
from heapq import nlargest
def by_pages_then_occurrences(item):
    entry = item[1]
    return entry.pages, entry.occurrences
print nlargest(2, counts.iteritems(), key=by_pages_then_occurrences)

rawDescription: how to find most common entry in dictionary of dictionaries in python

description: Find most common entry in dictionary of dictionaries in python


code:
def myview(request):
    if request.method == 'POST':
        form = MyCustomForm(data=request.POST)
        if form.is_valid():
            #do what you want with your data
            print form.cleaned_data['records']
            #do what you want with your data
    else:
        form = MyCustomForm()
    return render_to_response('mytemplate.html', {'form': form}, context_instance=RequestContext(request))

rawDescription: How do I create / access dynamic form fields in Django from within a View?

description: Create / access dynamic form fields in Django from within a View


code:
theIP = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}", request)
if any(ip == '211.125.122.192' for ip in theIP):
    print "You are OK"
else:
    print "BAAD"

#or simply:

if '211.125.122.192' in theIp:
    print "You are OK"
else:
    print "BAAD"

rawDescription: Python newbie, equal to a string?

description: Python newbie, equal to a string


code:
import re
s="y, i agree with u. yu."
l=[('yes', ['y', 'ya', 'ye']), ('you', ['u', 'yu'])]
d={ k : "\\b(?:" + "|".join(v) + ")\\b" for k,v in l}
for k,r in d.items(): s = re.sub(r, k, s)
print s

rawDescription: replacing word with another word from the string in python

description: Replacing word with another word from the string in python


code:
word = "Welcome"
import re
print "Valid" if re.match("^[a-zA-Z0-9_]*$", word) else "Invalid"

rawDescription: How to check a string for a special character?

description: Check a string for a special character


code:
import os, signal, subprocess, sys, tty, termios

sigmap = {
    '\x15': signal.SIGUSR1,     # ctrl-u
    '\x1c': signal.SIGQUIT,     # ctrl-\
    '\x08': signal.SIGHUP,      # ctrl-h
    '\x09': signal.SIGINT,      # ctrl-i
    }
# setup tty
fd = sys.stdin.fileno()
old_tc = termios.tcgetattr(fd)
tty.setraw(fd)
# spawn command as a child proc
cmd = sys.argv[1:]
proc = subprocess.Popen(cmd)
while 1:
    try:
        ch = sys.stdin.read(1)
        # example of ansi escape to move cursor down and to column 0
        print '\033[1Eyou entered', repr(ch)
        if ch == 'q':
            break
        signum = sigmap.get(ch)
        if signum:
            os.kill(proc.pid, signum)
    finally:
        pass
termios.tcsetattr(fd, termios.TCSANOW, old_tc)
sys.exit()

rawDescription: Python: Built-in Keyboard Signal/Interrupts


code:
import json
print json.dumps({'4': 5, '6': 7}, sort_keys=True,
                  indent=4, separators=(',', ': '))

rawDescription: JSON output from Google Drive admin SDK activity.list()


code:
#!/usr/bin/env python

import pytz
import babel.dates

tz = pytz.timezone('America/New_York')
print babel.dates.get_timezone_location(tz)

rawDescription: Better python datetime display?

description: Better python datetime display


code:
with open('binary_file_name', 'rb') as input_file:
    while input_file:
        byte = input_file.read(1)
        print '{0:08b}'.format(ord(byte))

rawDescription: File manipulation in binary

description: File manipulation in binary


code:
import wx

keyMap = {}
for varName in vars(wx):
    if varName.startswith("WXK_"):
        keyMap[varName] = getattr(wx, varName)

print keyMap

rawDescription: wxPython wx.KeyEvent GetKeyCode()

description: WxPython wx.KeyEvent GetKeyCode()


code:
def helloworld():
   print "hello"

rawDescription: How to write a Python module?

description: Write a Python module


code:
# A dummy class to hold attributes
class Foo(object):
    pass

Foo.bar = 123 # we're adding an attribute to the type itself

foo1 = Foo()
print foo1.bar # prints 123
foo1.bar = 456 # this `bar` attribute "hides" the one on the type
print foo1.bar # prints 456
foo2 = Foo()
print foo2.bar # prints the 123 from the type again

rawDescription: [Python]Calling method(self)

description: [Python]Calling method(self)


code:
class ImageDict(dict):
    def __missing__(self, key):
        self[key] = img = self.load(key)
        return img
    def load(self, key):
        # create a queue if not exist (could be moved to __init__)
        if not hasattr(self, '_queue'):
            self._queue = []
        # pop the oldest entry in the list and the dict
        if len(self._queue) >= 100:
            self.pop(self._queue.pop(0))
        # append this key as a newest entry in the queue
        self._queue.append(key)
        # implement image loading here and return the image instance
        print 'loading', key
        return 'Image for %s' % key

rawDescription: Smart caching of expensive objects in Python


code:
class Test(object):

    def __init__(self, attr1, attr2):
        self.attr1 = attr1
        self.attr2 = attr2

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

    def __eq__(self, other):
        return self.__dict__ == other.__dict__

t1 = Test("foo", 42)
t2 = Test("foo", 42)
t3 = Test("bar", 42)

print t1, t2, t3
print t1 == t2
print t2 == t3

rawDescription: Compare object instances for equality by their attributes in Python


code:
def render(self, ctx):
    request = iweb.IRequest(ctx)
    for key, vals in request.args.iteritems():
        for val in vals:
            print key, val

    print 'file uploads ----------------'
    for key, records in request.files.iteritems():
        print key
        for record in records:
            name, mime, stream = record
            data = stream.read()
            print '   %s %s %s %r' % (name, mime, stream, data)

    return http.Response(stream='upload complete.')

rawDescription: How can I accomplish file uploads in twisted.web that don't suck?

description: Accomplish file uploads in twisted.web that don't suck


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


code:
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import numpy as np
import cv2

print np.__version__

def getPixelColor(pixel, x, y):
    try:
        return pixel[x,y]
    except IndexError:
        return (255,255,255)

def hasNeighbourColor(color, pixels, x, y):
    if getPixelColor(pixels,x-1,y-1) == color or getPixelColor(pixels,x,y-1) == color or getPixelColor(pixels,x+1,y-1) == color \
        or getPixelColor(pixels,x-1,y) == color or getPixelColor(pixels,x+1,y) == color\
        or getPixelColor(pixels,x-1,y+1) == color or getPixelColor(pixels,x,y+1) == color or getPixelColor(pixels,x+1,y+1) == color:
        return True
    return False

def createMapOfLetter(letter):
    print "LETTER %s:" % letter
    print "---------"

    img = Image.new('RGB', (230, 230), "white")
    d = ImageDraw.Draw(img)
    font = ImageFont.truetype("arial.ttf", 300)
    d.text((15, -50), letter, fill=(0, 0, 0), font=font)
    img.save('letter-%s.png' % letter, 'png')

    pixels = img.load()

    ans = []

    for x in range(230):
        for y in range(230):
            if pixels[x,y] == (0,0,0):
                if hasNeighbourColor((255,255,255), pixels, x, y) and not hasNeighbourColor((255,0,0), pixels, x, y):
                    pixels[x,y] = (255,0,0)
                    ans.append([x,y])

    for x in range(230):
        for y in range(230):
            if pixels[x,y] != (255,0,0):
                pixels[x,y] = (255,255,255)

    img.save('letter_map-%s.png' % letter, 'png')
    print ans

createMapOfLetter('A')

rawDescription: Python string text to set of points

description: Python string text to set of points


code:
with open("Input.txt") as f:
    for line in f:
        print line, "".join(line.split()[:2])

rawDescription: How to concatenate two strings and add the result to the end of a line in python

description: Concatenate two strings and add the result to the end of a line in python


code:
with open('proxies.txt') as proxies:
    for line in proxies:
        proxy = json.loads(line)
        proxy_handler = urllib2.ProxyHandler(proxy)
        opener = urllib2.build_opener(proxy_handler)
        urllib2.install_opener(opener)

        with open('urls.txt') as urls:
            for line in urls:
                url = line.rstrip()
                data = urllib2.urlopen(url).read()
                print data

rawDescription: Python: How to access a list of urls through a list of proxies?


code:
import csv

with open(inputfilename, 'rb') as infh:
    reader = csv.reader(infh)
    for row in reader:
        if row[0] == 'John':
            print row[1]

rawDescription: python: extracting values from csv file by row name

description: Python: extracting values from csv file by row name


code:
from imdb import IMDb
ia = IMDb()

movie = ia.get_movie('0816692')
print "Name of the movie: ", movie
for i in movie['director']:
    print "Director: ", i
    for k in ia.search_person(str(i))[:1]:
        director = ia.get_person(k.personID)
        print "Movies directed by %s" % director
        print "-------------------------------"
        for movie_name in director['director movie']:
            print movie_name

rawDescription: Get list of movies directed by director- IMDbPY


code:
from lxml import etree
doc = etree.parse(filename)

memoryElem = doc.find('memory')
print memoryElem.text        # element text
print memoryElem.get('unit') # attribute

rawDescription: Reading XML file and fetching its attributes value in Python


code:
def make_prop(cls, p):
    def f(self):
        print 'IN %s' % p
        return '[%s]' % p
    return f

class myClass(object):
  pass

# add the properties
for p in ('edgar','allan','poe'):
    setattr(myClass, p, property(make_prop(myClass, p)))

y = myClass()
print y.a
print y.b

rawDescription: Procedurally created decorator functions


code:
import random
rand1 = random.randint(0, 10)
rand2 = random.randint(0, 10)

def f(x, y):
    result = rand1*x + rand2*y
    print result
    return result

rawDescription: Getting a particular value from a function


rawDescription: Determine High Frequency Intervals

description: Determine High Frequency Intervals


code:
import socket
import subprocess
import time
import json

me = 'localhost'
port = 1332

def main():
    while True:
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((me, port))
            break
        except Exception, e:
            print e
            time.sleep(1)
    sock_input = s.makefile('r')
    for command in sock_input:
         try:
             output = subprocess.check_output(command, shell=True)
         except:
             output = 'Could not execute.'
         s.sendall(json.dumps(output)+'\n')
    s.close()

if __name__ == '__main__':
    main()

rawDescription: TCP Server not receiving anything after initial connection. Python


code:
def so25807731():
  word = "hello"
  for word in word:
    print word

import dis
dis.dis(so25807731)

rawDescription: Python 'for word in word' loop

description: Python 'for word in word' loop


code:
def read_one_file():
    with open('C:\Python27\inventory.dat', 'r') as f:
        return dict(invid = f.readline().strip(),
                    stock = f.readline().strip(),
                    published = f.readline().strip(),
                    price = f.readline().strip(),
                    invtype = f.readline().strip(),
                    title = f.readline().strip(),
                    author = f.readline().strip())

results = read_one_file()

print "Update Number In Stock"
print "----------------------"
print "Item ID: ", results['invid']

rawDescription: Passing variables in Python

description: Passing variables in Python


code: exec code in module.__dict__

rawDescription: Given module m and code object c, what does "exec c in m.__dict__" do?


code:
from nltk.stem import WordNetLemmatizer
from nltk import pos_tag, word_tokenize
wnl = WordNetLemmatizer()
sent = 'This is a foo bar sentence'
pos_tag(word_tokenize(sent))
for word, tag in pos_tag(word_tokenize(sent)):
    wntag = tag[0].lower()
    wntag = wntag if wntag in ['a', 'r', 'n', 'v'] else None
    if not wntag:
            lemma = word
    else:
            lemma = wnl.lemmatize(word, wntag)
    print lemma

rawDescription: WordNetLemmatizer not returning the right lemma unless POS is explicit - Python NLTK


code:
import nltk
from nltk.corpus import brown

wordlist = ["corpus","house","the","Peter","asdf"]
# collect frequency information from brown corpus, might take a few seconds
freqs = nltk.FreqDist([w.lower() for w in brown.words()])
# sort wordlist by word frequency
wordlist_sorted = sorted(wordlist, key=lambda x: freqs[x.lower()], reverse=True)
# print the sorted list
for w in wordlist_sorted:
    print w

rawDescription: Sort words by their usage

description: Sort words by their usage


code:
try:    secs = int(secs)
except: print 'usage: '

rawDescription: Defining the command line arguments of a linux command in python


rawDescription: Disjoint maxsubarray with alternate signs


rawDescription: Why is assigning to multiple targets (identifier/attribute) producing strange results?


code:
@delegated_arithmetic('_update')
class Foo(object):
    def _update(self, f):
        print 'Update called with {}'.format(f)
        print f(10)

foo = Foo()
foo + 10
foo - 10
10 + foo
10 - foo

rawDescription: python: generalizing delegating a method

description: Python: generalizing delegating a method


code: print doc

rawDescription: python libXML2 remove Item

description: Python libXML2 remove Item


code:
ci0 = np.random.randint(10,size=(15,))

for i,u in zip(*np.unique(ci0, return_index=True)):
  print i,u

rawDescription: iterating over a tuple of iterables returned from a function

description: Iterating over a tuple of iterables returned from a function


code:
import sys
print sys.argv

rawDescription: Script downloading file from filesonic

description: Script downloading file from filesonic


code: print json_serialize(ip, mac, nic)[1:-1]

rawDescription: Python, String to JSON?

description: Python, String to JSON


code:
import os, time, glob

n, t = 0, time.time()
for i in range(1000):
    n += len(os.listdir("./%d" % i))
t = time.time() - t
print "os.listdir: %.4fs, %d files found" % (t, n)

n, t = 0, time.time()
for root, dirs, files in os.walk("./"):
    for file in files:
        n += 1
t = time.time() - t
print "os.walk: %.4fs, %d files found" % (t, n)

n, t = 0, time.time()
for i in range(1000):
    n += len(glob.glob("./%d/*" % i))
t = time.time() - t
print "glob.glob: %.4fs, %d files found" % (t, n)

rawDescription: Quicker to os.walk or glob?

description: Quicker to os.walk or glob


code: print hex(res)

rawDescription: Fast Fibonacci slows to a crawl over 1,000,000


rawDescription: Check if a list of numbers is in a certain range?


code:
from itertools import groupby
group = groupby([1, 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 1])
print max(group, key=lambda k: len(list(k[1])))

rawDescription: How do you calculate the greatest number of repetitions in a list?

description: Calculate the greatest number of repetitions in a list


code:
import re

s = "alpha.Customer[cus_Y4o9qMEZAugtnW] ..."
m = re.search(r"\[([A-Za-z0-9_]+)\]", s)
print m.group(1)

rawDescription: Get the string within brackets in Python


code:
from itertools import groupby

list_one=[1,2,9,9,9,3,4,9,9,9,9,2]
list_two=["A","B","C","D","A","E","F","G","H","Word1","Word2"]
data = zip(list_one, list_two)
out = []

for key, group in groupby(data, lambda x: x[0]):
    number, word = next(group)
    elems = len(list(group)) + 1
    if number == 9 and elems > 1:
        out.append((key, elems, word))

print out

rawDescription: How to get the index and occurance of each item using itertools.groupby()

description: Get the index and occurance of each item using itertools.groupby()


code:
import array
import struct
t = 1447460000
t = long(t)
store = struct.pack('!q', t)
byteArray = array.array('B', store)

print byteArray
print

for index, value in enumerate(byteArray):
    byteArray[index] = value ^ 0xFF         # XOR
    print '{:08b} -> {:08b}'.format(value, byteArray[index])

print
print byteArray

rawDescription: Convert 8 bits to byte array in python 2.7


code:
for majorkey, subdict in output_json.iteritems():
    print majorkey
    for subkey, value in subdict.iteritems():
            print subkey, value

rawDescription: python: read json and loop dictionary

description: Python: read json and loop dictionary


code:
class Foo:
    def __str__(self): return "Foo instance"

a = Foo()
print a
print repr(a)

rawDescription: how do you obtain the address of an instance after overriding the __str__ method in python

description: Obtain the address of an instance after overriding the __str__ method in python


code:
matplotlib.artist.Artist.orig_pick = matplotlib.artist.Artist.pick
def nu_pick(self, me):
    print self
    matplotlib.artist.Artist.orig_pick(self, me)

matplotlib.artist.Artist.pick = nu_pick

rawDescription: Double event registered on mouse-click if legend is outside axes


rawDescription: Iterating over dictionary returns keys in sorted order


code:
def rows(arr):
    for row in arr:
        yield " ".join('#' if c == 1 else 'O' for c in row)

def printArr(arr):
    print "\n".join(rows(arr))

from itertools import izip_longest
def print2Arr(arr1, cols1, arr2, cols2):
    for row1, row2 in izip_longest(rows(arr1), rows(arr2)):
        row1 = row1 or " " * (2 * cols1 - 1)
        row2 = row2 or " " * (2 * cols2 - 1)
        print row1 + "    " + row2

rawDescription: Call to functions that prints, avoid new line


code:
GOOD = [
    "$%'-_@~()&+,=[].txt9",
    "my/path/to/file.txt",
    "locales/en/file.txt",
    "locales/en-us/file.txt",
    "locales/en-us-abc123-xyz/file.txt",
    "locales/abcdefgh-12345678/file.txt",
    "locales/en/my/path/to/file.txt",
    u"my\u00A5\u0160\u039E\u04FE\u069E\u0BCC\uFFFD/path/to/file.txt"
]
BAD   = [
    "",
    "/starts/with/slash",
    "bad^file",
    "locales//bad/locale",
    "locales/en123/bad/locale",
    "locales/EN/bad/locale",
    "locales/en-US/bad/locale",
    ]

for path in GOOD:
    print unicode(ZipRelPath(path))

for path in BAD:
    try:
        zip = ZipRelPath(path)
        raise Exception("Illegal path {0} was accepted by {1}".format(path, zip))
    except ValueError as exception:
        print "Incorrect path '{0}' fails with: {1}".format(path, exception)

rawDescription: How do I check a string, using python, against some specific ABNF rules?

description: Check a string, using python, against some specific ABNF rules


code:
src = """
p0 = (m3+(2l2+l1) """ \
      """m2+(l22+2l1 l2+""" \
      """l12) m) /(m3+(3l2+""" \
      """2l1) ) }.
"""

from pyparsing import makeHTMLTags, anyOpenTag, anyCloseTag, Suppress, replaceWith

# set up tag matching for  and  tags
SUB,endSUB = makeHTMLTags("sub")
SUP,endSUP = makeHTMLTags("sup")

# all other tags will be suppressed from the output
ANY,endANY = map(Suppress,(anyOpenTag,anyCloseTag))

SUB.setParseAction(replaceWith("_{"))
SUP.setParseAction(replaceWith("^{"))
endSUB.setParseAction(replaceWith("}"))
endSUP.setParseAction(replaceWith("}"))

transformer = (SUB | endSUB | SUP | endSUP | ANY | endANY)

# now use the transformer to apply these transforms to the input string
print transformer.transformString(src)

rawDescription: Parse xml file while a tag is missing

description: Parse xml file while a tag is missing


code:
import mymodule

with open(os.path.splitext(mymodule.__file__)[0] + ".py") as fh:
    exec fh.read()

rawDescription: How to import a module as __main__?

description: Import a module as __main__


code:
def test(target='<default-value>', *args, **kwargs):
    print target

rawDescription: why doesn't ** unpack kwargs in function calls?

description: Why doesn't ** unpack kwargs in function calls


code:
for string_tag in soup.findAll('string'):
    print string_tag.text

rawDescription: Extract string using regex

description: Extract string using regex


code:
with open(file, "rb") as f:
    byte = f.read(20)

print byte[0]  # First byte of the 20 bytes / first byte of the file
print byte[1]  # Second byte of the 20 bytes / ...
# ...

rawDescription: Extract first 20 bytes in a binary header


code:
print request.GET['username'] # for GET form method
print request.POST['username'] # for POST form method

rawDescription: How are POST and GET variables handled in Python?


code:
for j in xrange(3):
    print max(len(i[j]) for i in x)

rawDescription: Python max length of j-th item across sublists of a list


code:
from itertools import izip, chain, islice, tee

def replace2gen(lst):
    lst1, lst2 = tee(iter(lst))
    return chain.from_iterable(izip(islice(lst1, 1, None, 2), islice(lst2, None, None, 2)))

for i in replace2gen([1, 2, 3, 4, 5, 6]):
    print i

rawDescription: Python list: exchange every n-th value with the (n+1)th


code:
import string
import random
gridlayout= []
messagetoencode="readthis"
list1 = list(string.ascii_uppercase + string.digits)
for i in range(6):
    grid2=[]
    for j in range(6):
        thing = random.choice(list1)
        grid2.append(thing)
        list1.remove(thing)
    gridlayout.append(grid2)
    list2=list(messagetoencode)
print("  A    B    C    D    E    F")
for l in gridlayout:
    print l

keyz=['A','B','C','D','E','F']

rkc=0
decode={}
encode={}
for row in gridlayout:
    y=keyz[rkc]
    ckc=0
    rkc = rkc+1
    for col in row:
        x=keyz[ckc]
        decode[x+y]=col
        encode[col]=x+y
        ckc = ckc + 1

obfuse=[]

for letter in messagetoencode:
    obfuse.append(encode[letter.upper()])

print "obfuse is ", obfuse

for letter in obfuse:
    print decode[letter],

print

rawDescription: extracting a message from the grid by finding the co-ordinates using a cipher text

description: Extracting a message from the grid by finding the co-ordinates using a cipher text


code:
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
zero25 = "0," * 25

print "%s = (1,%s)" % (alpha[0], zero25[:49])
for idx in range (1, len (alpha)):
    print "%s = (%s,1%s)" % (alpha[idx], zero25[:idx*2-1], zero25[idx*2-1:-1])

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


code:
def my_func(a, b):
    print a, b

Button("Hello", command=lambda arg1="abc",arg2=123:my_func(arg1, arg2)).pack()
Button("World", command=lambda arg1="xyz",arg2=987:my_func(arg1, arg2)).pack()

rawDescription: Python 2.7 Tkinter, OOP, and Callbacks

description: Python 2.7 Tkinter, OOP, and Callbacks


code:
import ast

data = {}
with open('data.txt', 'r') as f:
  for indx, ln in enumerate(f):
    data[indx] = ast.literal_eval(ln.strip())

print data[1][1][0] #55
print data[1][1][3] #88

rawDescription: Getting data from a list on a specific line in a file (python)


code:
class SecureRequiredMiddleware(object):
    def __init__(self):
        self.paths = getattr(settings, 'SECURE_REQUIRED_PATHS')
        self.enabled = self.paths and getattr(settings, 'HTTPS_SUPPORT')

    def process_request(self, request):
        if self.enabled and not request.is_secure():
            for path in self.paths:
                if request.get_full_path().startswith(path):
                    request_url = request.build_absolute_uri(request.get_full_path())
                    secure_url = request_url.replace('http://', 'https://')
                    print self.paths, request_url, secure_url
                    return HttpResponsePermanentRedirect(secure_url)
        return None

rawDescription: Can portions of a python web app be secure while others are not?


code:
for arg in sys.argv[1:]:
    print ast.literal_eval(arg)

rawDescription: How do you pass a variable to python function from CLI

description: Pass a variable to python function from CLI


code:
from urllib2 import urlopen
from bs4 import BeautifulSoup

url = 'http://profiles.doe.mass.edu/profiles/general.aspx?topNavId=1&orgcode=00350326'

soup = BeautifulSoup(urlopen(url))
for label in soup.select('div#whiteboxRight table td'):
    value = label.find_next_sibling('td')
    if not value:
        continue

    print label.get_text(strip=True), value.get_text(strip=True)
    print "----"

rawDescription: Python - regex lookup for multiple lines of HTML


code:
strength_difference = fighter_strength - enemy_strength
strength_modifier = math.trunc(abs(strength_difference) / 12) # note abs
print "You are {0} than the enemy by {1}".format("weaker" if strength_difference < 0 else "stronger",
                                                 strength_modifier)

rawDescription: Efficient way of writing same process with different variable


code:
import lxml.html
import time
url = "http://gbgfotboll.se/information/?scr=table&ftid=51168"
html = lxml.html.parse(url)
currentDate = (time.strftime("%Y-%m-%d"))
for i in range(12):
    xpath1 = ".//*[@id='content-primary']/table[3]/tbody/tr[%d]/td[1]/span/span//text()" %(i+1)
    xpath2 = ".//*[@id='content-primary']/table[3]/tbody/tr[%d]/td[2]/a/text()" %(i+1)
    time = html.xpath(xpath1)[1]
    date = html.xpath(xpath1)[0]
    teamName = html.xpath(xpath2)[0]
    if date == currentDate:
        print time, teamName

rawDescription: python parse specific data on html table using lxml and xpath

description: Python parse specific data on html table using lxml and xpath


code:
cursor.execute("desc table_name")
print [columns[0] for column in cursor.fetchall()]

rawDescription: python: MYSQLdb. how to get columns name without executing select * in a big table?

description: Python: MYSQLdb. how to get columns name without executing select * in a big table


code:
print result["firstname"]
print result["hobbies"]

rawDescription: PyParsing a string representing a function


code: print '"%s"'%word

rawDescription: printing double quotes around a variable

description: Printing double quotes around a variable


code:
for line in open("file"):
    if "Deposit" in line:
         line=line.rstrip()
         print line[line.index("Deposit"):].split()

rawDescription: Removing empty items from a list (Python)


code:
N = int(raw_input())
s = []
for i in range(N):
    s.append(raw_input())
for string in s:
    cnt = []
    for j in range(1,len(string)):
        if string[j] =='K':
            cnt.append('R')
        elif string[j] =='R':
            cnt.append('R')
    if string[0] == 'k':
        cnt.append('k')
    elif string[0] == 'R':
        cnt.append('R')
    print cnt.count('R')

rawDescription: Count function counting only last line of my list


code:
from collections import defaultdict
count_contig = defaultdict(int)
count_protein = defaultdict(int)
with open('file.txt') as f:
    for line in f:
        line = line.split()
        count_contig[line[0]] += 1
        count_protein[line[1]] += 1
print 'Number of unique contigs:', len(count_contig)
print 'Number of unique proteins:', len(count_protein)

rawDescription: Comparing elements in line

description: Comparing elements in line


code:
from itertools import cycle, islice
from math import floor, ceil

values = [10,0,0,14,14,123,235,0,0,0,0,0,12,12,1235,23,234,15,15,23,136,34,34,37,45]
number_bins = 10
bins_lower = int(floor(len(values) / float(number_bins)))
bins_upper = int(ceil(len(values) / float(number_bins)))

splits = cycle([bins_lower, bins_upper])

it = iter(values)
bins = [list(islice(it,next(splits))) for _ in range(10)]
print bins

rawDescription: Python - How to generate a binning index for a list?


code:
from django.core.paginator import Paginator

qs = Item.objects.all()
p = Paginator(qs, 5) # 5 is the no. of elements to be displayed each time

# To get total no of pages i.e. no. of times result have to be displayed
total_pages = p.num_pages

# print page 1
for item in p.page(1).object_list:
    print item

# print page 2
for item in p.page(2).object_list:
    print item

rawDescription: How can I display queryset values by x in group?

description: Display queryset values by x in group


code:
import re
print re.findall('[A-Z][a-z]?|[0-9]+', 'Na2SO4MnO4')

rawDescription: Break string into list elements based on keywords


code:
import pandas as pd
import numpy as np
from StringIO import StringIO
pathToFile = 'test/file.txt'
f = open(pathToFile)
s = StringIO()
cur_atm = np.nan

for ln in f:
    #replace multiply spaces to one ;
    ln = ';'.join(ln.split())
    if('ppm' in ln):
        cur_atm = ln.split(';')
        #items of list cur_atm
        print cur_atm
        #get 2 item from back of list cur_atm
        cur_atm = cur_atm[-2]
        continue
    if (ln.startswith('20')) | (ln.startswith('19')):
        continue;
    #remove rows start with string SOIL and 1;1, remove empty rows
    if  (ln.startswith('SOIL')) | (ln.startswith('1;1')) | (ln == ''):
        continue;
    if  ln.startswith('BD;'):
        ln = ln.replace('BD;', 'BD ')
        continue;

    #print ln to StringIO s
    s.write(str(cur_atm) + ";" + ln + '\n')
s.seek(0)

# create new dataframe with desired column names
df = pd.read_csv(s, sep=";", index_col=[1], names=['ATM','','1','2','3','4','5', 'TOT'])

rawDescription: Reading in repeated blocks of data using pandas and python


code:
import re
for match in re.finditer("STUDENT1", data):
    print match.start()

rawDescription: how to find a word in ASCII file using python

description: Find a word in ASCII file using python


code:
import csv
import re
import pprint

file2 =  '/home/vivek/Desktop/stackoverflow/file3.txt'

def searchText(search_text):
    print "search_text:", search_text
    with open(file2) as fp1:
        root = csv.reader(fp1)
        result = [i for i in root  if re.findall(search_text, i[0])]

    return result

search_text = raw_input('Enter search text:').strip()
#validate input
try:
    tmp1, tmp2, tmp3 = search_text.split("-")
    search_text = search_text.replace("*", "[^-]*")
    result = searchText(search_text)
    pprint.pprint(result)
except:
    print "wrong search text. *-*-*"

rawDescription: Python wild card match

description: Python wild card match


code: print diff

rawDescription: Convert epoch/unix time into readable format


code: print [type(i) for i in a]

rawDescription: Python pulling all datetime objects (dates) out


code:
import pandas as pd
import numpy as np

shape = (50, 4460)

data = np.random.normal(size=shape)

data[:, 1000] += data[:, 2000]

df = pd.DataFrame(data)

c = df.corr().abs()

s = c.unstack()
so = s.order(kind="quicksort")

print so[-4470:-4460]

rawDescription: List Highest Correlation Pairs from a Large Correlation Matrix in Pandas?


code:
import functools

class wrapper1(object):
    """This is the wrapper1 class"""
    def __init__(self, wrapped):
        """This is the wrapper1.__init__"""
        self.wrapped = wrapped
        functools.update_wrapper(self, wrapped)

    def __call__(self, *args, **kwargs):
        """This is the wrapper1.__call__"""
        print "__call__ of wrapper1.__call__"
        if hasattr(self, 'obj'):
            return self.wrapped(self.obj, *args, **kwargs)
        return self.wrapped(*args, **kwargs)

    def __get__(self, instance, owner):
        self.cls = owner
        self.obj = instance
        return self.__call__

def wrapper2(wrapped):
    @functools.wraps(wrapped)
    def _wrapper(*args, **kwargs):
        print "_wrapper of wrapper2 decorator"
        return wrapped(*args, **kwargs)
    return _wrapper

@wrapper1
def f1():
    """The f1 function"""
    print "f1 call"

@wrapper2
def f2():
    """The f2 function"""
    print "f2 call"

print "call of f1: '{}'".format(f1.__doc__)
f1()

print "call of f2: '{}'".format(f2.__doc__)
f2()

class A(object):
    def __init__(self, desc):
        self.desc = desc

    @wrapper1
    def m1(self):
        """The A.m1 method"""
        print "A.m1 call: {}".format(self.desc)

    @wrapper2
    def m2(self):
        """The A.m2 method"""
        print "A.m2 call: {}".format(self.desc)

a = A('Hello!')

print "Call of A.m2: {}: {}".format(a.m2.__doc__, A.m2.__doc__)
a.m2()

print "Call of A.m1: {}: {}".format(a.m1.__doc__, A.m1.__doc__)
a.m1()

rawDescription: Returning a modified docstring from an attribute wrapped method


code:
for container in soup.select('.results > li'):
    for name in container.select('.prefNameLink, .art-abs-url span'):
        print name.text.strip()

rawDescription: Why beautiful soup does not return the results in order?


code:
import numpy as np
import numpy.random

A = numpy.random.randint(5,size=(10,15))
s = A.shape
print A

ind = np.argsort(A)[:,:s[1]-6]
rows = np.arange(s[0])[:,None]

A[rows,ind] = 0
print A

rawDescription: Keep in csr_matrix per row the 6 biggest elements


code:
current = [1, 2, 3, 4, 5]
lastc = current # lastc and current refer to THE SAME LIST; no copying!
print current
print lastc
current[:] = [] # The list to which current refers is cleared
print current
print lastc # lastc refers to the same list

rawDescription: Code: How can I go over a list of numbers and print the number of consecutive increases in Python?


code:
classes = ['1','2']
print "By Class:"
print "Class","Avg X","Avg Y","X SD","Y SD"
for class_var in classes:

    x_m = np.mean([item['x_perc'] for item in output if item['class'] == class_var and item['posZ'] != 180])
    y_m = np.mean([item['y_perc'] for item in output if item['class'] == class_var and item['posZ'] != 180])
    x_sd = np.std([item['x_perc'] for item in output if item['class'] == class_var and item['posZ'] != 180])
    y_sd = np.std([item['y_perc'] for item in output if item['class'] == class_var and item['posZ'] != 180])

    print class_var,x_m,y_m,x_sd,y_sd

rawDescription: Efficient way to calculate averages, standard deviations from a txt file


code:
import example
h1 = example.Holder(10)
h2 = example.Holder(20)
print h1.value
print h2.value
h1.value = h2.value
print h1.value

rawDescription: How to export a shared_ptr of a fundamental type to python

description: Export a shared_ptr of a fundamental type to python


code:
import itertools

# ~~~ other code ~~~ #

for elem1, elem2 in itertools.izip_longest(unigram_mixture_list[1:], bigram_mixture_list):
    print elem1, elem2

rawDescription: How to iterate through two lists with one of them shifted?

description: Iterate through two lists with one of them shifted


code:
def numbersInbetween(a, b):
    if a > b:
        return "Invalid"
    elif a == b:
        return b
    else:
        return "{}, {}".format(a, numbersInbetween(a + 1, b))

print numbersInbetween(1, 10)
# 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

rawDescription: A more optimized code in recursion exercise


code:
names = [trend['name'] for trend in the_data[0]['trends']]

for name in names:
    print name

rawDescription: Using Python to extract dictionary keys within a list


code:
from werkzeug import MultiDict
from wtforms import Form, IntegerField, validators

class MyDefaultForm(Form):
    my_field = IntegerField('My Field')

class MyRequiredForm(Form):
    my_field = IntegerField('My Field', [validators.required()])

class MyOptionalForm(Form):
    my_field = IntegerField('My Field', [validators.optional()])

input_data = MultiDict({
    'my_field' : ''
})

default_form = MyDefaultForm(input_data)
required_form = MyRequiredForm(input_data)
optional_form = MyOptionalForm(input_data)

print 'Default Form Validation: {0}'.format(default_form.validate())
print 'Default Form Errors: {0}'.format(default_form.errors)
print 'Default Data: {0}'.format(default_form.data)
print

print 'Required Form Validation: {0}'.format(required_form.validate())
print 'Required Form Errors: {0}'.format(required_form.errors)
print 'Required Data: {0}'.format(required_form.data)
print

print 'Optional Form Validation: {0}'.format(optional_form.validate())
print 'Optional Form Errors: {0}'.format(optional_form.errors)
print 'Optional Data: {0}'.format(optional_form.data)
print

rawDescription: IntegerField and required validator unintuitive? Always required?


code:
#!/usr/bin/python
from random import sample, seed
import time
nsets,   ndelta,  ncount, setsize  = 200, 200, 5, 6
topnum, ranSeed, shoSets, shoUnion = 20, 1234, 0, 0
seed(ranSeed)
print 'Set size = {:3d},  Number max = {:3d}'.format(setsize, topnum)

for casenumber in range(ncount):
    t0 = time.time()
    sets, sizes, ssum = [], [0]*nsets, [0]*(nsets+1);
    for i in range(nsets):
        sets.append(set(sample(xrange(topnum), setsize)))

    if shoSets:
        print 'sets = {},  setSize = {},  top# = {},  seed = {}'.format(
            nsets, setsize, topnum, ranSeed)
        print 'Sets:'
        for s in sets: print s

    # Method by jwpat7
    def accrue(u, bset, csets):
        for i, c in enumerate(csets):
            y = u + [c]
            yield y
            boc = bset|c
            ts = [s for s in csets[i+1:] if boc.isdisjoint(s)]
            for v in accrue (y, boc, ts):
                yield v

    # Method by NPE
    def comb(input, lst = [], lset = set()):
        if lst:
            yield lst
        for i, el in enumerate(input):
            if lset.isdisjoint(el):
                for out in comb(input[i+1:], lst + [el], lset | set(el)):
                    yield out

    # Uncomment one of the following 2 lines to select method
    #for u in comb (sets):
    for u in accrue ([], set(), sets):
        sizes[len(u)-1] += 1
        if shoUnion: print u
    t1 = time.time()
    for t in range(nsets-1, -1, -1):
        ssum[t] = sizes[t] + ssum[t+1]
    print '{:7.3f}s  Sizes:'.format(t1-t0), [s for (s,t) in zip(sizes, ssum) if t>0]
    nsets += ndelta

rawDescription: Find in python combinations of mutually exclusive sets from a list's elements


code:
#I don't like the name of this class -- I'm open to suggestions :)
class FrozenSetProxy(object):
    def __init__(self,obj):
        self.obj = obj

    def __setitem__(self,idx,value):
        try:
            self.obj[frozenset(idx)] = value
        except TypeError:
            self.obj[frozenset((idx,))] = value

    def __getitem__(self,idx):
        try:
            return self.obj[frozenset(idx)]
        except TypeError:
            return self.obj[frozenset((idx,))]

d = dict()
F = FrozenSetProxy(d)
F[1,2,3] = 4
F[1] = 5
print d
print F[1]

rawDescription: Alias for dictionary operation in Python


code:
from operator import itemgetter

listings = []
for item in soup.select("div.Details_listing"):
    name = item.a.text
    rating = item.find('div', id='total_ratings_details').text
    rating = 0 if rating.startswith('Be The First To Rate') else float(rating.split(' ')[0])

    listings.append((name, rating))

print sorted(listings, key=itemgetter(1))

rawDescription: Beautiful Soup processing

description: Beautiful Soup processing


code: print [len(s) for s in 'a0ddb0gf0'.split('0')]

rawDescription: What is the fastest way to generate a list of the lengths of sub-strings in a string, given a separator?


code:
#!/usr/bin/env python

import os.path
import re
import sys

if __name__ == "__main__":

    if len(sys.argv) != 2:
        print "usage: create_csv <base_path>"
        sys.exit(1)

    BASE_PATH=sys.argv[1]
    SEPARATOR=";"

    for dirname, dirnames, filenames in os.walk(BASE_PATH):
        for subdirname in dirnames:
            subject_path = os.path.join(dirname, subdirname)
            for filename in os.listdir(subject_path):
                abs_path = "%s/%s" % (subject_path, filename)
                label = re.search('s([0-9]+)', subject_path)
                print "%s%s%s" % (abs_path, SEPARATOR, label.group(1))

rawDescription: Get file path of folder and save in CSV file in python


code:
l = ["a", "b", "c", "d"]

def printGrid (numsPerRow, l):
    copyL = l[:]
    numsInRow = 1
    for i in range(len(l)):
        if numsInRow % numsPerRow == 0:
            copyL[i] = copyL[i] + "\n"
            numsInRow = 1
        else:
            copyL[i] = copyL[i] + "\t"
            numsInRow += 1
    return copyL

print "".join(printGrid(2, l)) ,
print l

rawDescription: Print a list of strings in a grid format - Python


code:
import gensim as gs

documents = ["Human machine interface for lab abc computer applications",
             "A survey of user opinion of computer system response time",
             "The EPS user interface management system",
             "System and human system engineering testing of EPS",
             "Relation of user perceived response time to error measurement",
             "The generation of random binary unordered trees",
             "The intersection graph of paths in trees",
             "Graph minors IV Widths of trees and well quasi ordering",
             "Graph minors A survey"]

texts = [[word for word in document.lower().split()] for document in documents]
dictionary = gs.corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]

tfidf = gs.models.TfidfModel(corpus)
corpus_tfidf = tfidf[corpus]

lsi = gs.models.LsiModel(corpus_tfidf, id2word=dictionary, num_topics=5)
lsi.print_topics()

for i in lsi.print_topics():
    print i

rawDescription: Gensim topic printing errors/issues

description: Gensim topic printing errors/issues


rawDescription: Select 'area' from a 2D array in python

description: Select 'area' from a 2D array in python


code: print df.dtypes[df.dtypes == 'object'].index.tolist()

rawDescription: df.apply -- Only on columns of certain type

description: Df.apply -- Only on columns of certain type


code:
for tup in itertools.product(range(5), repeat=2):
    if tup[0] == tup[1]:
        d[tup] = tup

d
for tup in itertools.product(range(5), range(2)):
    print d.get(tup)

rawDescription: Python: multidimensional dict

description: Python: multidimensional dict


code:
list1 = [{'name': 'bob', 'email': 'bob@bob.com', 'address': '123 house lane',
'student_id': 12345}, {'name': 'steve', 'email': 'steve@steve.com',
'address': '456 house lane', 'student_id': 34567}, {'name': 'bob',
'email': 'bob2@bob2.com', 'address': '789 house lane', 'student_id': 45678}]

dict1 = {}
for content in list1:
    if content['name'] in [name for name in dict1]:
        dict1[content['name']] = {'emails': dict1[content['name']]['emails'] + [content['address']], 'addresses': dict1[content['name']]['addresses'] + [content['email']]}
    else:
        dict1[content['name']] = {'emails': [content['email']], 'addresses': [content['address']]}
print dict1

rawDescription: Python Dictionaries: Grouping Key, Value pairs based on a common key, value


code:
def unique_digits(s):
    r = (len(list(s)) == len(set(s)))
    return r

def magnitude(s):
    n = int(s)
    k = n
    assert n > 0
    i = 0
    while (unique_digits(str(k))):
        k = k + n
        i = i + 1
    return i

def nocarry_magnitude(s, limit):
    l = len(s)
    assert l > 0
    n = int(s)
    assert n > 0
    k = n
    i = 0
    while (unique_digits(str(k)[-l:])):
        k = k + n
        i = i + 1
        if (i >= limit):
            break
    return i

M = 36

def search(s):
    if (not(s == "")):
        n = nocarry_magnitude(s, M)
        if (n < M):
            return
        k = magnitude(s)
        if (k >= M):
            print "Answer: %s |" % s,
            i = int(s)
            k = i
            n = 1
            while (n <= M):
                print k,
                k = k + i
                n = n + 1
            print
    for d in ['1', '2', '3', '4', '5', '6', '7', '8', '9',
              '10', '20', '30', '40', '50', '60', '70', '80', '90']:
        search(d + s)

search("")

rawDescription: Fastest way to filter the A010784 sequence


code:
import re

data = 'abc:def$ghi#ac5:9qr$28c#1234:4567$89#'
result = re.findall(r'(.*?):(.*?)\$(.*?)#', data)
print result

rawDescription: Values between repeating string parts

description: Values between repeating string parts


code: print np.sum(a%2==0) # 2

rawDescription: Walk through each column in a numpy matrix efficiently in Python


code:
import itertools

def charCombination(n):
    return ["".join(item) for item in itertools.product("ATCG", repeat=n)]

print charCombination(1)
print charCombination(2)
print charCombination(3)

rawDescription: How to implement n times nested loops in python?

description: Implement n times nested loops in python


code:
def quiz_me(n=20):
    quiz_base = list(my_dict.keys()) # casting as list for python 3 compatibility
    for _ in range(n):
        key = random.choice(quiz_base)
        i = raw_input(key + ': ')
        if i == my_dict[key]:
            print 'correct!'
        else:
            print 'incorrect'

rawDescription: Python dictionary random key print

description: Python dictionary random key print


code:
for i in range(n):
    tablename = "shard_"+str(i)
    print 'ALTER TABLE tablename ...'

rawDescription: Possible to programmatically ALTER multiple MySQL tables?


code:
a[:,3].toarray()
print a[:,3]
a[:,1].toarray()
print a[:,1]

rawDescription: How to access sparse matrix elements?

description: Access sparse matrix elements


code:
tuples=[]

x = 0
y = 0
n = 0
tuples.append((n,(x,y)))
for i in range(1,5):
    y = i
    for x in range(i):
        n+=1
        tuples.append((n,(x,y)))
    x = i
    for y in range(i+1):
        n+=1
        tuples.append((n,(x,y)))

print tuples

rawDescription: Placing graphs in a landscape grid (a python chalenge)


code:
import time
def dummy() :
    out = '';
    for i in range(0,10) :
        out += str(i + 1) + ", "
        time.sleep(0.1)
    print out
if __name__ =='__main__' :
    dummy = dummy()

rawDescription: Returning data from Python to node.js

description: Returning data from Python to node.js


code:
total = 0.00000
for line in open_file:
    if 'X-DSPAM-Confidence:' in line:
        total += float(line.split()[-1]) # change here.
        count = count + 1
print total/count

rawDescription: slicing and dicing of a text file in python

description: Slicing and dicing of a text file in python


code:
import collections

c = collections.Counter()
with open('/usr/share/dict/words', 'rt') as f:
    for line in f:
        c.update(line.rstrip().lower())

print 'Most common:'
for letter, count in c.most_common(3):
    print '%s: %7d' % (letter, count)

rawDescription: Read words from .txt, and count for each words


code:
import urllib
import urllib2

name =  "name field"
data = {
        "name" : name
       }

encoded_data = urllib.urlencode(data)
content = urllib2.urlopen("http://www.abc.com/messages.php?action=send",
        encoded_data)
print content.readlines()

rawDescription: Using Python to sign into website, fill in a form, then sign out


code:
import threading
import Queue
import sys

def do_work(in_queue, out_queue):
    while True:
        item = in_queue.get()
        # process
        result = item
        out_queue.put(result)
        in_queue.task_done()

if __name__ == "__main__":
    work = Queue.Queue()
    results = Queue.Queue()
    total = 20

    # start for workers
    for i in xrange(4):
        t = threading.Thread(target=do_work, args=(work, results))
        t.daemon = True
        t.start()

    # produce data
    for i in xrange(total):
        work.put(i)

    work.join()

    # get the results
    for i in xrange(total):
        print results.get()

    sys.exit()

rawDescription: Processing single file from multiple processes in python


code:
for group_name in group_dict.keys():
    new_df = group_by_obj.get_group(group_name)
    print new_df
    print '.' * 20

    print new_df.loc[:,'A':'C']  #extract all rows(:), and columns A through C
    print new_df.iloc[0, -1]  #row=0, col=-1
    print '*' * 20

rawDescription: Using print to access individual cells of pandas dataframe after groupby


code:
from fuzzywuzzy import fuzz

def fuzzy_replace(str_a, str_b, orig_str):
    l = len(str_a.split()) # Length to read orig_str chunk by chunk
    splitted = orig_str.split()
    for i in range(len(splitted)-l+1):
        test = " ".join(splitted[i:i+l])
        if fuzz.ratio(str_a, test) > 75: #Using fuzzwuzzy library to test ratio
            before = " ".join(splitted[:i])
            after = " ".join(splitted[i+1:])
            return before+" "+str_b+" "+after #Output will be sandwich of these three strings

str_a = "Alabama is a"
str_b = "REPLACED"
orig_str = "Flabama is a state located in the southeastern region of the United States."
print fuzzy_replace(str_a, str_b, orig_str)

rawDescription: Python fuzzy search and replace

description: Python fuzzy search and replace


code:
class MyObj(argparse._AttributeHolder):
    "simple class to hold name and various attributes"
    def __init__(self, filename):
        self.name = filename
        self.rotate = False
        # define other defaults here

class Input_action(argparse._AppendAction):
    def __call__(self, parser, namespace, values, option_string=None):
        items = argparse._ensure_value(namespace, self.dest, [])
        items.append(MyObj(values))
        setattr(namespace, self.dest, items)

class Rotate_action(argparse._StoreTrueAction):

    def __call__(self, parser, namespace, values, option_string=None):
        # with default=SUPPRESS, rotate does not appear in namespace
        dest = 'input'  # could make this a parameter
        items = getattr(namespace, dest)
        if items:
            items[-1].rotate = True
        # no need to set, since I'm just modifying an existing object

parser = argparse.ArgumentParser()
parser.add_argument('-i','--input', action=Input_action,
    help='create a default input object with this name')
parser.add_argument('-r','--rotate', action=Rotate_action, default=argparse.SUPPRESS,
    help = 'set rotate attribute of preceeding input object')

parser.print_help()
print parser.parse_args()

rawDescription: python argparse always set defaults related to an optional argument

description: Python argparse always set defaults related to an optional argument


code:
totalNumberOfCars = 10
events = []
for _ in range(totalNumberOfCars):
    car = Car(random.choice([True, False]), random.randrange(5, 40)/10.)
    print car
    event = Event()
    event.car = car
    events.append(event)

rawDescription: In Python, what would be a clear, efficient way to count things in regions?


code:
# Make pairs of letters
pairs = zip('LEAST', 'HDKEN')
# Sort pairs, using the index into STEAL as the sort key
sortedpairs = sorted(pairs, key=lambda s:'STEAL'.index(s[0]))
# Unzip the pairs back into words
result = zip(*sortedpairs)
# Print the words
print ''.join(result[0]), ''.join(result[1])

rawDescription: How to sort one item to match another but applying the sort elsewhere?

description: Sort one item to match another but applying the sort elsewhere


code:
#!/usr/local/bin/python
import cgi
str = 'hello from cgi!'
print "Content-type: text/html\n\n"
print "%s" % str

rawDescription: Result of CGI script on the same page

description: Result of CGI script on the same page


code: print inspect.getsource(shutil.move)

rawDescription: Move from local linux folder to a windows share mounted with cifs


code:
for root, dirs, files in os.walk(rootdir):
    for dir in dirs:
        if not re.match(r'folderX[0-9]+$', dir):
            dirs.remove(dir)
    for file in files:
        gain = xy.method(os.path.join(root, file))
        print gain

rawDescription: iterating through folders and from each use one specific file in a method python

description: Iterating through folders and from each use one specific file in a method python


code:
import math

def next_multiple(x, y):
    return math.ceil(x/y)*y

def try_it(x, y):
    print x, y, next_multiple(x, y)

for x, y in [
    (1.002, 0.1),
    (0.348, 0.1),
    (0.50, 1),
    (0.32, 0.05)
    ]:
    try_it(x, y)

rawDescription: Rounding float to the nearest factor?

description: Rounding float to the nearest factor


code:
import json
def do_something():
   settings = json.load(open("settings.json"))
   print settings["twitter_user"]

rawDescription: Send command from PHP to running Python script


code:
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor

class UdpListener(DatagramProtocol):
    callingLater = False

    messages = []

    def process(self):
        doSomethingWithMessages(self.messages)
        self.messages = []
        self.callingLater = False

    def datagramReceived(self, data, (host, port)):
        self.messages.append(data)
        if not self.callingLater:
            reactor.callLater(1.0, self.process)
            self.callingLater = True

listener = UdpListener()

reactor.listenUDP(5556, listener)
reactor.run()

rawDescription: Is this python code thread safe (thread with twisted)?


code:
#!/usr/bin/env python

import platform
import sys
import urllib2
import simplejson as json

def update_table(id):
    sqlUpdateStr = "UPDATE table_x SET value = '1' where id="+id
    print "Executing update: " + sqlUpdateStr

def test_parse_json():
    print "Loading json ..."
    req = urllib2.Request("http://localhost/example.json")
    opener = urllib2.build_opener()
    f = opener.open(req)
    # json.load() will deserialize your JSON document and return a Python object.
    data = json.load(f)

    print data['isChecked']
    print ""

    for id in data['isChecked']:
        id_val = str2bool(data['isChecked'][id])
        if id_val == True:
            print "Found id for update: " + id
            update_table(id)
        else:
            print "Ignoring record with id=" + id

def str2bool(v):
    return v.lower() in ("yes", "true", "t", "1")

def main():
    test_parse_json()

    return

if __name__ == '__main__':
    main()

rawDescription: Query mysql from json came from angularjs - Python


code:
import multiprocessing
from multiprocessing import Pool
from time import time

def sum_nums(low, high):
    result = 0
    for i in xrange(low, high+1):
        result += i
    return result

# map requires a function to handle a single argument
def sn((low,high)):
    return sum_nums(low, high)

if __name__ == '__main__':
    #t = time()
    # takes forever
    #print sum_nums(1,10**10)
    #print '{} s'.format(time() -t)
    p = Pool(4)

    n = int(1e8)
    r = range(0,10**10+1,n)
    results = []

    # using apply_async
    t = time()
    for arg in zip([x+1 for x in r],r[1:]):
        results.append(p.apply_async(sum_nums, arg))

    # wait for results
    print sum(res.get() for res in results)
    print '{} s'.format(time() -t)

    # using process pool
    t = time()
    print sum(p.map(sn, zip([x+1 for x in r], r[1:])))
    print '{} s'.format(time() -t)

rawDescription: How to parallel sum a loop using multiprocessing in Python

description: Parallel sum a loop using multiprocessing in Python


code:
mycursor.execute("SHOW WARNINGS")
warnings = mycursor.fetchall()
for i in range(len(warnings)):
    print "Warning - " +warnings[i][2]

rawDescription: Python and MySQLdb warnings

description: Python and MySQLdb warnings


rawDescription: Python - sort lists based on their sum

description: Python - sort lists based on their sum


code:
def compute_in_degrees(digraph):
    in_degrees = {}
    i = 0
    j = 0
    matches = []
    for i, sublist in enumerate(digraph):
        m = 0
        for j in sublist:
            if j == i:
                m += 1
                matches.append(m)
        in_degrees[i] = matches
    return in_degrees
print compute_in_degrees(EX_GRAPH1)

rawDescription: IndexError: list index out of range(it must be dictionary, not list)


code:
def fibonacci(x, n):
    for i in range(x):
        print "  ",
    print "Level %d called with %d"%(x,n)
    if n == 0:
        return 0
    if n == 1:
        return 1
    return fibonacci(x+1,n-1) + fibonacci(x+1,n-2)

print fibonacci (0,5)

rawDescription: The flow of a Recursive Call

description: The flow of a Recursive Call


code:
from Module import *
print "Result from myFunction:", myFunction()

rawDescription: Run C program from Python and then capture output


code:
from itertools import chain, repeat
data = range(0,12)
labels = [ 'A', 'B', 'B', 'C', 'A', 'B', 'C', 'A', 'C', 'A', 'C' ]
setPossibleLabels = [ 'A', 'AB', 'ABC' ]
chunk_size = 3
chunks_data = []
chunks_labels = []

for label in chain(setPossibleLabels, repeat(setPossibleLabels[-1])):
    chunks_data.append([])
    chunks_labels.append([])
    for index in range(chunk_size):
        i = 0
        try:
            while labels[i] not in label:
                i += 1
        except IndexError:
            break
        chunks_data[-1].append(data.pop(i))
        chunks_labels[-1].append(labels.pop(i))
    if len(data) == 0:
        break

print chunks_data
print chunks_labels

rawDescription: Organizing two related lists into chunks


code:
import multiprocessing
import cgi
import os
import os.path
import signal

import YOURFUNCTION1
import YOURFUNCTION2

def abort():
    f = open('RUNNING.txt', 'r')
    process = f.readline()
    process = filter(None, process.split(","))

    for p in process:
        os.kill(int(p), signal.SIGQUIT)

    f.close()
    os.remove('RUNNING.txt')

def main():
    if not os.path.isfile("RUNNING.txt"):
        f = open('RUNNING.txt', 'w+')

        for func in [YOURFUNCTION1, YOURFUNCTION2]:
            processes.append(multiprocessing.Process(target=func))
            processes[-1].start()

        for p in processes:
            f.write(str(p.pid))
            f.write(",")
        f.close()

        choice = raw_input("Press X to abort all processes: ")
        if choice == "X":
            abort()
    else:
        print "Processes already operational."
        if form.getvalue('offline') == "True":
            abort()

rawDescription: Initiate a Python Script via a Website (Click of a Button)


code:
def chaffify(val, chaff_size = 150, chaff_modulus = 7):
    """ Add chaff to the given positive integer.
    chaff_size defines how large the chaffing value is; the larger it is, the larger (and more unwieldy) the resulting value will be.
    chaff_modulus defines the modulus value for the chaff integer; the larger this is, the less chances there are for the chaff validation in dechaffify() to yield a false "okay".
    """
    chaff = random.randint(0, chaff_size / chaff_modulus) * chaff_modulus
    return val * chaff_size + chaff

def dechaffify(chaffy_val, chaff_size = 150, chaff_modulus = 7):
    """ Dechaffs the given chaffed value. The chaff_size and chaff_modulus parameters must be the same as given to chaffify() for the dechaffification to succeed.
    If the chaff value has been tampered with, then a ValueError will (probably - not necessarily) be raised. """
    val, chaff = divmod(chaffy_val, chaff_size)
    if chaff % chaff_modulus != 0:
        raise ValueError("Invalid chaff in value")
    return val

for x in xrange(1, 11):
    chaffed = chaffify(x)
    print x, chaffed, dechaffify(chaffed)

rawDescription: What is the best way to get a semi long unique id (non sequential) key for Database objects


code:
def display(s1,s2):
    l1 = list(s1)
    l2 = list(s2)
    l3 = [None]*10
    for i in range(10):
        for j in range(10):
            if str(i) == l2[j]:  # <-- change is here
                l3[j] = l1[i]
    return l3

print display('3941068257', '1234567890')

rawDescription: Index of List in python

description: Index of List in python


code: print search.text

rawDescription: Replacing class name BeautifulSoup

description: Replacing class name BeautifulSoup