code:
#!/usr/bin/env python

import helper

class A(object):
    def __init__(self):
        self.matched_condition_set = set()

    def add_to_matched_condition(self,condition):
        self.matched_condition_set.add(condition)

class B(object):
    def __init__(self, list_of_A):
        self.list_of_a = list_of_A

    def do_stuff(self):
        for a in self.list_of_a:
            if helper.check_for_a(a):
                print(a.matched_condition_set)

a1 = A(); a2 = A()
b = B([a1, a2])

print a1, a2
print b, b.list_of_a

b.do_stuff()

print a1.matched_condition_set
print a2.matched_condition_set

rawDescription: Class instance has no attribute 'function_name' for passing object outside class- python


code:
import cmd

class HelloWorld(cmd.Cmd):
    """Simple command processor example."""

    def do_greet(self, line):
        print "hello"

    def do_EOF(self, line):
        return True

if __name__ == '__main__':
    HelloWorld().cmdloop()

rawDescription: How do you make a console application which accepts commands?

description: Make a console application which accepts commands


code:
def flatten(current_dict, current_key, result_dict):

    # For every key in the dictionary
    for key in current_dict:
        # If the value is of type `dict`, then recurse with the value
        if isinstance(current_dict[key], dict):
            flatten(current_dict[key], current_key + key, result_dict)
        # Otherwise, add the element to the result
        else:
            result_dict[current_key + key] = current_dict[key]
    return result_dict

print flatten(urls, "", {})

rawDescription: python recursive dictionary converting to strings

description: Python recursive dictionary converting to strings


code:
def is_prime(number):
    factors = [candidate_factor for candidate_factor in range(1, number+1) if number % candidate_factor == 0]
    return len(factors) == 2

#generates a list of numbers.
primes = [number for number in range(2, 101) if is_prime(number)]
prime_total = sum(primes)
print prime_total

rawDescription: python sum of primes

description: Python sum of primes


code:
a=['comp','graphics','card','part']
with open('hello.txt', 'r') as f:
    lines = f.readlines()  # loop the file once and store contents in list
    for key in a:
        for line in lines:
            if key in line:
                print line, key

rawDescription: python parse prints only first line from list

description: Python parse prints only first line from list


code:
def test():
    usol = list(set([path_vals(mx, i) for i in matrix_path(5)]))
    print len(usol),"unique results"

cProfile.run("test()")

rawDescription: All possible combinations of numbers of length 5 in a 4X4 matrix


code:
import re

myfile = open(files,'r');
filecontent = myfile.read();

results = re.compile('content(.*?)score', re.DOTALL | re.IGNORECASE).findall(filecontent)
print results

rawDescription: Python finding string between 2 tags

description: Python finding string between 2 tags


code:
a = [5]
b = a
b += [1]
print id(a), id(b) # same

rawDescription: Why equal integers behaves differently to equal lists?


code:
from lxml import etree

doc = etree.XML("""
<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.5/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.5/ http://www.mediawiki.org/xml/export-0.5.xsd" version="0.5" xml:lang="en">
  <siteinfo>
    <sitename>Wiki</sitename>
    <case>first-letter</case>
    <namespaces>
      <namespace key="0" case="first-letter" />
    </namespaces>
  </siteinfo>
  <page>
    <title>Sex</title>
    <title_id>31239628</title_id>
    <revision>
      <id>437708703</id>
      <timestamp>2011-07-04T13:53:52Z</timestamp>
      <text xml:space="preserve" bytes="6830">{{ Hello}}
      </text>
    </revision>
  </page>
</mediawiki>
""")

def first(seq,default=None):
  for item in seq:
    return item
  return default

NSMAP=dict(mw="http://www.mediawiki.org/xml/export-0.5/")

print first(doc.xpath('/mw:mediawiki/mw:page/mw:title/text()',namespaces=NSMAP))
print first(doc.xpath('/mw:mediawiki/mw:page/mw:title_id/text()',namespaces=NSMAP))

rawDescription: Reading XML file tags

description: Reading XML file tags


code:
import datetime

Result = {}

def find_common_key(targetFile):
    with open(targetFile, 'r+') as file_handler:
        for line in file_handler:
            __line = map(int, line.strip().split(','))
            key = (__line[0], __line[1]); value = [__line[2], __line[3]]
            if key in Result:
                Result[key] = Result[key] + value
            else:
                Result[key] = value
    return Result

if __name__ == '__main__':
    files = ['a.txt', 'b.txt']
    print "Started At: ", datetime.datetime.now()
    print
    print 'Initial:'
    for f in files:
        find_common_key(f)

    print
    print "Write to Dic Finished At: ", datetime.datetime.now()

    print
    print 'Result:'
    for k, v in Result.items():
        if len(v) > 2:
            print ','.join(map(str, k)), tuple(v)

    print
    print "Finished At: ", datetime.datetime.now()

rawDescription: PYTHON: Searching two files for common lines and collecting their contents into set


code: print soup.find('tr', 'adr').text

rawDescription: Way to get contents of different children using beautifulSoup?


code:
list_of_pairs = list_of_pairs.append(matchup)
print "ADDED TO LIST: " + str(list_of_pairs)

rawDescription: Python: How to add tuple to an empty list?


code:
def do_something_with(block):

    lines = block.splitlines()

    j = 0
    first_line = lines[j]
    while first_line.strip() == '':
        j += 1
        first_line = lines[j]

    pos = []
    position = first_line.find('*')
    while position != -1:
        pos.append(position)
        position = first_line.find('*', position+1)

    for k, line in enumerate(lines):
        if k > j:
            for p in pos:
                print line[p],
            print

## prints
## z y
## x z
## z y

rawDescription: Do something to line and next lines until a symbol is hit


code: print 'ă'

rawDescription: when to use utf8 as a header in py files

description: When to use utf8 as a header in py files


code:
states = {
     'Alaska' : 'AK',
     'Alabama' : 'AL',}
astring = "I love Alaska"

for state, abbreviation in states.items():
     if state in astring:
        print astring.replace(state, abbreviation)

rawDescription: Search for Dictionary Values in String

description: Search for Dictionary Values in String


code:
import codecs
import urllib

fname = 'input.txt'

with open(fname, 'rb') as f:
    for line in f:
        line = line.strip()
        line = urllib.unquote(line)
        if r'\x' in line:
            line = codecs.unicode_escape_decode(line)[0]
            line = line.encode('latin1')

        line = line.decode('utf-8')
        print repr(line), line

rawDescription: How to normalize Python string encodings

description: Normalize Python string encodings


code:
from camelot.core.orm import Session
import time, schedule

def job():
    session = Session()
    #Use session to access and manipulate model
    print "I'm working..."

    session.flush() #flush session to DB

def main():
    from camelot.core.conf import settings
    settings.setup_model()

    schedule.every(10).seconds.do(job)

    while True:
        schedule.run_pending()
        time.sleep(1)

rawDescription: How do I schedule my desktop application(PyQt) to send emails at some intervals

description: Schedule my desktop application(PyQt) to send emails at some intervals


rawDescription: how to match all 3 digit except a particular number

description: Match all 3 digit except a particular number


rawDescription: How to make json decimal round to three digits

description: Make json decimal round to three digits


code:
with open('test1.csv') as fin:
    csvin = csv.reader(fin)
    lookup = {row[0]: row for row in csvin}

to_find = 'x'
try:
    print '{} = {[4]}'.format(to_find, lookup[to_find])
except (KeyError, IndexError) as e:
    pass # KeyError = no lookup match, IndexError is that CSV file didn't have 5th column...

rawDescription: Python: Use variable as a lookup reference in CSV and return value


code:
for line in get_next_line("Input.txt"):
    print line

rawDescription: Reading from a file: find the new line

description: Reading from a file: find the new line


code:
class Parser1:
    def __init__(self, desc):
        self.parser = argparse.ArgumentParser(description=desc)
        self.actions = []

    def milestone(self, help_='milestone for latest release.', default=None):
        action = self.parser.add_argument('-m', '--milestone', help=help_, default=default)
        self.actions.append(action)
        return self

    def parse(self):
        args = self.parser.parse_args()
        for a in self.actions:
            if getattr(args, a.dest) == a.default:
                print 'Please specify', a.dest
                values = raw_input('>')
                setattr(args, a.dest, values)
        return args

print Parser1('desc').milestone(default='PROMPT').parse()

rawDescription: Cause Python's argparse to execute action for default


code:
import sys

if __name__ == '__main__':
    ticker = sys.argv[3]
    allTypes = bool(int(sys.argv[4])) #argv[4] is a string, you have to convert it to an int, then to a bool

    with open(sys.argv[1]) as ifile, open(sys.argv[2], mode = 'w') as ofile:
        all_timestamps = [] #this is an empty list
        n_rows = 0
        for row in ifile:
            #This splits the line into constituent terms as described earlier
            #SAMPLE LINE:
            #A,1,12884902522,B,B,4900,AAIR,0.1046,28800,390,B,AARCA,
            #After applying this bit of code, the line should be split into this:
            #['A', '1', '12884902522', 'B', 'B', '4900', 'AAIR', '0.1046', '28800', '390', 'B', 'AARCA']
            #NOW, you can make comparisons against those terms. :)

            terms = [term for term in row.split(',') if term.strip() != '']
            current_timestamp = int(terms[2])

            #compare the current against the previous
            #starting from row 2: (index 1)
            if n_rows > 1:
                #Python uses circular indices, hence: -1 means the value at the last index
                #That is, the previous time_stamp. Now perform the comparison and do something if that criterion is met:
                if current_timestamp - all_timestamp[-1] >= 0:
                    pass #the pass keyword means to do nothing. You'll have to replace it with whatever code you want

            #increment n_rows every time:
            n_rows += 1

            #always append the current timestamp to all the time_stamps
            all_timestamps.append(current_timestamp)

            if (terms[6] == ticker):
                # add something to make sure chronological order hasn't been broken
                if (allTypes == 1):
                    ofile.write(row)
            #I don't know if this was a bad indent of not, but you should know
            #where this goes
            elif (terms[0] == "A" or terms[0] == "M" or terms[0] == "D"):
                print row
                ofile.write(row)

rawDescription: write csv row (from within for loop) out to csv file without using python csv module

description: Write csv row (from within for loop) out to csv file without using python csv module


code: print len(odds) <= 1 # prints the value you're looking for

rawDescription: How to check if a sequence could be turned into a palindrome

description: Check if a sequence could be turned into a palindrome


code:
from animals import CAT

print CAT.number_of_legs

rawDescription: Grouping constants in python

description: Grouping constants in python


code:
data = "test, ąśżźć, test2"
data
print data

rawDescription: How to use split with utf8 coding?

description: Use split with utf8 coding


rawDescription: Print 5 items in a row on separate lines for a list?


code:
myList = [687, 687, 683, 683, 677, 662]
unique_sorted_list = sorted(list(set(myList)), reverse = True)
result = []
for i in range(len(unique_sorted_list)):
    if i == 0:
        result.append((unique_sorted_list[i], i))
    else:
        result.append((unique_sorted_list[i], unique_sorted_list[i-1] - unique_sorted_list[i] + result[i-1][1]))

result = [j[1] for i in myList for j in result if i==j[0]]
print result

rawDescription: How to renumber reverse-sorted list of integers?

description: Renumber reverse-sorted list of integers


code:
def parent_function(numbers):
    # do something with numbers
    parent_function.counter += 1
    return sum(numbers)

parent_function.counter = 0

def add_reset_function(parent):

    def reset_counter():
        parent.counter = 0

    setattr(parent, 'reset', reset_counter)

# call parent_function a few times

numbers = [1, 2, 3]
for i in range(4): parent_function(numbers)
print parent_function.counter # 4

add_reset_function(parent_function)

# call the reset function just added
parent_function.reset()

print parent_function.counter # 0
parent_function(numbers)
print parent_function.counter # 1

rawDescription: How to get parent attributes from a child method attribute in Python?

description: Get parent attributes from a child method attribute in Python


code:
for topmember in mydata:
    print "%s = %s"%(topmember['name'], topmember['value'])
    for member in topmember['members']:
        print '   MEMBER: %s = %s' % (member['name'], member['value'])

rawDescription: Multidimensional List/Dict/whatever?

description: Multidimensional List/Dict/whatever


code:
for d in daterange(date1, date2):
    print d.strftime('%Y.%m.%d')

rawDescription: Creating a loop for two dates

description: Creating a loop for two dates


code:
import sys

if __name__ == '__main__':

    for arg in sys.argv:
        print arg

rawDescription: How to start a Python script several functions in

description: Start a Python script several functions in


code:
def multiple_replace(myString):
    myString = re.sub(ur'(?u)[^\W\d_]', u'A', myString)
    myString = re.sub(ur'[0-9]', u'9', myString)
    return myString

rawDescription: Writing python regex that recognizes all unicode letters


code:
items = [("ITEM01", 100, 10000), ("ITEM02", 24, 576), \
    ("ITEM03", 24, 576), ("ITEM04", 51, 2500), ("ITEM05", 155, 25)]
S = sum([item[1] for item in items])
dp = [None for i in xrange(S + 1)]
dp[0] = []

for item in items:
    for i in xrange(S, -1, -1):
        if dp[i] is not None and i + item[1] <= S:
            dp[i + item[1]] = dp[i] + [item]

desired_sum = 150
i = j = desired_sum

while i >= 0 and j <= S:
    if dp[i] is not None:
        print dp[i]
        break
    elif dp[j] is not None:
        print dp[j]
        break
    else:
        i -= 1
        j += 1

rawDescription: Knapsack prob, but allow over-filling

description: Knapsack prob, but allow over-filling


code:
import pandas as pd
from numpy import array, arange
from numpy.random import randint, seed

seed(42)
n_rows = 1000

d = pd.DataFrame(randint(1,10,(n_rows,8))) #Raw data
dgs = array([3,4,1,8,9,2,3,7,10,8]) #Values we will look up, referenced by index
grps = pd.cut(randint(1,5,n_rows),arange(1,5)) #Grouping

f = lambda x: dgs[x.index].mean() #Works on a grouped Series
g = lambda x: x.groupby(x).apply(f) #Works on a Series
h = lambda x: x.apply(g,axis=1).mean(axis=0) #Works on a grouped DataFrame

print d.groupby(grps).apply(h) #Slow

### my code starts from here ###

def group_process(df2):
    s = df2.stack()
    v = np.repeat(dgs[None, :df2.shape[1]], df2.shape[0], axis=0).ravel()
    return pd.Series(v).groupby([s.index.get_level_values(0), s.values]).mean().mean(level=1)

print d.groupby(grps).apply(group_process)

rawDescription: Need for speed: Slow nested groupbys and applys in Pandas


code:
def run(first, second):
    return [a + b for a, b in zip(first, second)]

print run([1,2,3],[10,20,30])

rawDescription: Turning this simple block to idiomatic Python code


code:
class ListViewModal(ModalView):
    section_dict = cat_dict.SECTION_DICT
    categories = sorted(section_dict)

    def __init__(self, **kwargs):
        super(ListViewModal, self).__init__(**kwargs)

    def callback(self, instance):
        print "HI" + str(instance)
        # Print abbreviation from dictionary lookup
        print ListViewModal.section_dict[instance.text]

rawDescription: How to pass dictionary values to callback function?

description: Pass dictionary values to callback function


code:
seq = ["A", "B", ",", "C", ","]
perm_ii = [i for i,x in enumerate(seq) if x != ","]
for p in permute_some(seq, perm_ii):
    print p

rawDescription: Python - Narrowing down itertools permutations results with "if" statements. How to with repeated element?


code:
import urlparse

params = 'muted=False&class_=Decoder&failures=3&last_orders=Key%28%27ProspectiveSale%27%2C+%27cl1%27%29&last_orderl=Key%28%27ProspectiveSale%27%2C+%27cl2%27%29&hit_per=5.0'
print urlparse.parse_qs(params)

rawDescription: How can I read a task's 'params' value during unit testing in App Engine

description: Read a task's 'params' value during unit testing in App Engine


code:
import netifaces
netifaces.interfaces()
netifaces.ifaddresses('eth0')
for interface in netifaces.interfaces():
  print netifaces.ifaddresses(interface)[netifaces.AF_INET]
for interface in netifaces.interfaces():
  for link in netifaces.ifaddresses(interface)[netifaces.AF_INET]:
    print link['addr']

rawDescription: Why does gethostbyaddr(gethostname()) return my IPv6 IP?


code:
import inspect
class MyClass():
    def display_info(self):
        print inspect.stack()[1][3]

def my_function(my_obj):
    my_obj.display_info()

my_obj = MyClass()
my_function(my_obj)

def another_fn(my_obj):
    my_obj.display_info()

another_fn(my_obj)

rawDescription: How to get a function's name from a object passed as that function's parameter in Python?

description: Get a function's name from a object passed as that function's parameter in Python


code:
def find_usb(self):
    bus = dbus.SystemBus()
    ud_manager_obj = bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks")
    ud_manager = dbus.Interface(ud_manager_obj, 'org.freedesktop.UDisks')

    found = []

    for dev in ud_manager.EnumerateDevices():
        device_obj = bus.get_object("org.freedesktop.UDisks", dev)
        device_props = dbus.Interface(device_obj, dbus.PROPERTIES_IFACE)
        if device_props.Get('org.freedesktop.UDisks.Device', "DriveConnectionInterface") == "usb" and device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsPartition"):
            if device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsMounted"):
                device_file = device_props.Get('org.freedesktop.UDisks.Device', "DeviceFile")
                #print device_file
                found.append(device_file)

            else:
                print "Device not mounted"

    return found

rawDescription: Python - Returning list/ arry from function


code:
with open("commonwords.tsv") as f:
    lines = f.read().split()
    for ind, word in enumerate(lines, 1):
        print "{0} {1}".format(ind, word)

rawDescription: split file with python into new lines with counter

description: Split file with python into new lines with counter


code:
import xlrd

workbook = xlrd.open_workbook(r"input.xlsx")
sheet = workbook.sheet_by_index(0)

col_a = sheet.col_values(0, 1)
col_b = sheet.col_values(1, 1)

my_dict = {a : b for a, b in zip(col_a, col_b)}

print my_dict

rawDescription: Reading excel column 1 into Python dictionary key, column 2 into value


code:
# The first way I had of grabbing the module:
mod = __import__(__name__, fromlist=['nonempty'])

# From Roger's suggestion:
import sys
mod = sys.modules[__name__]

for name in ['A', 'B', 'C']:
    class_ = type(name, (object, ), {})
    setattr(mod, name, class_)

print A, B, C

rawDescription: Populating values in module namespace

description: Populating values in module namespace


code: print [float(15)/x*100 for x in range(15,21)]

rawDescription: Python multiply range

description: Python multiply range


code:
import os # os is a library that gives us the ability to make OS changes

def file_renamer(list_of_files, new_file_name_list):
    for file_name in list_of_files:
        for (new_filename, barcode_infile) in new_file_name_list:
            # as per the mentioned filename pattern -> xxxx.1.xxxx.[barcode]
            barcode_current = file_name[12:19] # extracting the barcode from current filename
            if barcode_current == barcode_infile:
                os.rename(file_name, new_filename)  # renaming step
                print 'Successfully renamed %s to %s ' % (file_name, new_filename)

if __name__ == "__main__":
    path = os.getcwd()  # preassuming that you'll be executing the script while in the files directory
    file_dir = os.path.abspath(path)
    newname_file = raw_input('enter file with new names - or the complete path: ')
    path_newname_file = os.path.join(file_dir, newname_file)
    new_file_name_list = []
    with open(path_newname_file) as file:
        for line in file:
            x = line.strip().split(',')
            new_file_name_list.append(x)

    list_of_files = os.listdir(file_dir)
    file_renamer(list_of_files, new_file_name_list)

rawDescription: Python3 Rename files in a directory importing the new names from a txt file


code: print [ticket for ticket in tickets if ticket['summary'] == issue['title']]

rawDescription: Find a dictionary in a list of dictionaries


code:
for i in range(0, len(position_start)):
    print position_start[i]
    print position_top[i]
    print program_width[i]
    print program_heigh[i]
    print program_title[i]

rawDescription: how to print the list of strings from the arrays

description: Print the list of strings from the arrays


code:
#Calculate average
def calc_average(total):
    return total / 5

#Grading scale
def determine_score(grade):
    if 90 <= grade <= 100:
        return 'A'
    elif 80 <= grade <= 89:
        return 'B'
    elif 70 <= grade <= 79:
        return 'C'
    elif 60 <= grade <= 69:
        return 'D'
    else:
        return 'F'

#Enter 5 test scores
scores = []
for i in range(1, 6):
    score = int(input('Enter score {0}: '.format(i)))
    print 'That\'s a(n): ' + determine_score(score)
    scores.append(score)

total = sum(scores)
avg = calc_average(total)
abc_grade = determine_score(avg)

print('Average grade is: ' + str(avg))
print("That's a(n): " + str(abc_grade))

rawDescription: Test Average and Grade - Python

description: Test Average and Grade - Python


code:
import Tkinter as tk
import thread
from time import sleep
import Queue

request_queue = Queue.Queue()
result_queue = Queue.Queue()

def submit_to_tkinter(callable, *args, **kwargs):
    request_queue.put((callable, args, kwargs))
    return result_queue.get()

t = None
def threadmain():
    global t

    def timertick():
        try:
            callable, args, kwargs = request_queue.get_nowait()
        except Queue.Empty:
            pass
        else:
            print "something in queue"
            retval = callable(*args, **kwargs)
            result_queue.put(retval)

        t.after(500, timertick)

    t = tk.Tk()
    t.configure(width=640, height=480)
    b = tk.Button(text='test', name='button', command=exit)
    b.place(x=0, y=0)
    timertick()
    t.mainloop()

def foo():
    t.title("Hello world")

def bar(button_text):
    t.children["button"].configure(text=button_text)

def get_button_text():
    return t.children["button"]["text"]

if __name__ == '__main__':
    thread.start_new_thread(threadmain, ())

    trigger = 0
    while 1:
        trigger += 1

        if trigger == 3:
            submit_to_tkinter(foo)

        if trigger == 5:
            submit_to_tkinter(bar, "changed")

        if trigger == 7:
            print submit_to_tkinter(get_button_text)

        sleep(1)

rawDescription: Tkinter locks python when Icon loaded and tk.mainloop in a thread


code:
from itertools import groupby
from operator import itemgetter

rows=[
    ('a' , 'b' , 'c' , 'd' , 1),
    ('a' , 'b' , 'c' , 'd' , 2),
    ('z' , 'x' , 'v' , 'n' , 3),
    ('z' , 'x' , 'v' , 'n' , 4)]

out = []
for key,group in groupby(rows, itemgetter(0,1,2,3)):
    new_row = list(key)
    for v in group:
        new_row.append(v[4])
    out.append(new_row)

print out

rawDescription: Issues ith loop is django

description: Issues ith loop is django


code:
s = "N4 AP11 AP7 AP7 N3 AP12 AP8 N3 AP13 AP9 N3 AP14 AP10 AP10 N0 AP15 AP11"

def get_max_by_start(start, string):
    values = re.findall(r"\b{0}\d+\b".format(start), string)
    return max(values, key=lambda x: int(x.replace(start, '')))

print get_max_by_start('N', s)
print get_max_by_start('AP', s)

rawDescription: python for getting big number string form a column

description: Python for getting big number string form a column


code:
{0: [{'distance': 2.1, 'home_id': 2L, 'home_number': 4, 'road_id': 0},
     {'distance': 4.1, 'home_id': 1L, 'home_number': 2, 'road_id': 0}],
 1: [{'distance': 4, 'home_id': 3L, 'home_number': 5, 'road_id': 1}],
 2: [{'distance': 2.1, 'home_id': 4L, 'home_number': 1, 'road_id': 2},
     {'distance': 6, 'home_id': 5L, 'home_number': 3, 'road_id': 2}]}

rawDescription: Sorting and re-numbering values in Python dictionary


code:
l=range(10000)
from datetime import datetime
start_time = datetime.now()
print reduce(lambda x,y: x+y*y,l)
print('using Reduce numbers: {}'.format(datetime.now() - start_time))

from datetime import datetime
start_time = datetime.now()
print sum(map(lambda x:x*x,l))
print('Sum after map square operation: {}'.format(datetime.now() - start_time))

from datetime import datetime
start_time = datetime.now()
print sum( i*i for i in l)
print('using list comprehension to sum: {}'.format(datetime.now() - start_time))

rawDescription: sum of squares in a list in one line?

description: Sum of squares in a list in one line


code:
def foo(group):
    A = group['A']
    B = group['B']
    r1 = randn()
    r2 = randn()
    return Series({'fancy': r1, 'great': r2})

df = DataFrame(randn(10, 1), columns=['a'])
df['B'] = np.random.choice(['hot', 'cold'], size=10)
df['A'] = np.random.choice(['sweet', 'sour'], size=10)
df['someAttribute'] = np.random.choice(['pretty', 'ugly'], size=10)
print df.groupby('someAttribute').apply(foo)

rawDescription: Pandas: replicating SASs proc means by attribute out=agg


code:
import pygame
print pygame.font.get_default_font()

rawDescription: Changing font in a livewires message

description: Changing font in a livewires message


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

url = 'http://api.cbssports.com/fantasy/pro-teams?version=3.0&SPORT=basketball'
request = urllib2.Request(url)

try:
    response = urllib2.urlopen(request)
    tree = ET.parse(response)
    root = tree.getroot()
    for team in root.findall('.//pro_team'):
        nickname = team.find('nickname').text
        print nickname
except URLError, e:
    print 'error:', e

rawDescription: Python sports XML parsing

description: Python sports XML parsing


code:
soup.p.append(text[:start])
print soup

rawDescription: Python BeautifulSoup - Add Tags around found keyword


code: print 'Value is "' + str(value) + '"'

rawDescription: How to print variables without spaces between values

description: Print variables without spaces between values


code:
import operator
import random
import string

def slices(seq, length):
  unique = {}
  for start in xrange(len(seq) - length, -1, -1):
    unique[seq[start:start+length]] = start
  return unique

def sample_with_repeat(population, length, choice=random.choice):
  return "".join(choice(population) for _ in xrange(length))

word_length = 5*1000*1000
words = [sample_with_repeat(string.lowercase, word_length) for _ in xrange(3)]
slice_length = 20
words_slices_sets = [set(slices(x, slice_length)) for x in words]
unique_words_slices = [reduce(operator.sub,
                              (x for x in words_slices_sets if x is not n),
                              n)
                       for n in words_slices_sets]
print [len(x) for x in unique_words_slices]

rawDescription: Python, Huge Iteration Performance Problem


code:
lock = threading.Lock()

def createstuff(data):
    t= threading.Thread(target=func, args=(data,))
    t.start()

def func(val):
    with lock:
        print 'thread: acquired'
        time.sleep(2)
        print ('Value: %s, : %s'%(val, threading.currentThread().getName()))
        print 'thread released'

ags_list = ['x']

createstuff(ags_list)

with lock:
    print 'thread: acquired'
    rn =random.randint(5,50)
    print 'random no:', rn
    ags_list[0] = rn
    print 'thread: released'

rawDescription: Threading lock in python not working as desired


code:
import numpy

# Populate the lattice
lattice = numpy.concatenate([numpy.ones(90), numpy.zeros(10)])
numpy.random.shuffle(lattice)

# Intialize problem
in_trap = False
steps = 0
pos = int(numpy.random.randint(0,len(lattice),1))
history = []

while in_trap == False:
    # Step of -1 is backward, 1 is forward
    step = numpy.random.permutation([-1,1])[0]

    # Check position for edges and fix if required
    if pos + step > len(lattice) - 1:
        pos = 0
    elif pos + step < 0:
        pos = len(lattice) - 1
    else:
        pos += step

    # Keep track of random walk
    history.append(pos)

    # Check if it's a trap
    if lattice[pos] == 0:
        in_trap = True

    # If not, continue
    steps += 1

print steps
print history
print lattice

rawDescription: python- construction of lattice which traps molecules - doesn't work right

description: Python- construction of lattice which traps molecules - doesn't work right


rawDescription: Convert data in String format to List - python


code:
x = ""
def mutablerange():
    global x
    x += "x"
    return [x + str(i) for i in range(3)]

print list(conjoin([range3, mutablerange]))

rawDescription: Conjoin function made in functional style


code:
class Base(object):
    def __init__(self, *args, **kwargs): pass

class A(Base):
    def __init__(self, *args, **kwargs):
        print "A"
        super(A, self).__init__(*args, **kwargs)

class B(Base):
    def __init__(self, *args, **kwargs):
        print "B"
        super(B, self).__init__(*args, **kwargs)

class C(A):
    def __init__(self, arg, *args, **kwargs):
        print "C","arg=",arg
        super(C, self).__init__(arg, *args, **kwargs)

class D(B):
    def __init__(self, arg, *args, **kwargs):
        print "D", "arg=",arg
        super(D, self).__init__(arg, *args, **kwargs)

class E(C,D):
    def __init__(self, arg, *args, **kwargs):
        print "E", "arg=",arg
        super(E, self).__init__(arg, *args, **kwargs)

print "MRO:", [x.__name__ for x in E.__mro__]
E(10)

rawDescription: correct way to use super (argument passing)

description: Correct way to use super (argument passing)


code:
import itertools
words = ['this', 'is', 'a', 'test']
for p in itertools.permutations(words): print ' '.join(p)

rawDescription: How can I read a file, put contents in an array, shuffle the array, and then write the shuffled array to the file in python 2.7

description: Read a file, put contents in an array, shuffle the array, and then write the shuffled array to the file in python 2.7


code:
class Material(object):
    def __init__(self, name, _type, cons):
        self.name = name
        self.type = _type
        self.cons = cons.tolist() # more compact display
    def __repr__(self):
        return str((self.name, self.type, self.cons))
Ldict = {}
for r in LAMI:
    Ldict[r['name']] = Material(r['name'], r['type'], r['cons'])

dset = np.dtype([('where','a12'),('material','O')])
dtElem = np.dtype([('conn', '<i4', (3,)), ('sets', dset)])
# nested dtypes
# [('conn', '<i4', (3,)), ('sets', [('where', 'S12'), ('material', 'O')])]

ELEM = np.array([ ([47, 49, 36], ('web', Ldict['gelcoat'])),
                  ([48, 30, 43], ('surf', Ldict['balsa']))],
               dtype=dtElem)

print 'ELEM array'
print ELEM
print 'material properties of one element'
print ELEM[0]['sets']['material'].cons
print 'materials of all elements'
print ELEM['sets']['material']

rawDescription: Fast and Clean way to find to find Numpy arrays index


code:
import itertools

hand_dict = {'hand': [['hand','ball_1_r1','cake_1'],
                      ['hand','ball_1_r1','cake_2'],
                      ['hand','ball_1_r1','cake_3'],
                      ['hand','ball_1_r2','cake_1'],
                      ['hand','ball_1_r2','cake_2'],
                      ['hand','ball_1_r2','cake_3'],
                      ['hand','ball_2_r1','cake_1'],
                      ['hand','ball_2_r1','cake_2'],
                      ['hand','ball_2_r1','cake_3'],
                      ['hand','ball_2_r2','cake_1'],
                      ['hand','ball_2_r2','cake_2'],
                      ['hand','ball_2_r2','cake_3']]}

for key, value in hand_dict.iteritems():
    # unflatten the entries in "value"
    for k, v in itertools.groupby(value, lambda row: [row[0], row[1]]):
        k.extend([n[2] for n in v])
        print k

rawDescription: Manipulating dictionary data structures

description: Manipulating dictionary data structures


code:
def twentyone(seq, groups = None, final = None, first_call=True):

    if not groups:
        groups = []

    if first_call:
        final = []

    goal = 21
    s = sum(groups)

    if s == goal:
        final.append(groups)

    if s >= goal:
        return

    for i in range(len(seq)):
        n = seq[i]
        remaining = seq[i+1:]
        twentyone(remaining, groups + [n], final, False)

    return final

seq = [1, 5, 6, 7, 10, 2, 11]
print twentyone(seq)
print twentyone(seq)

rawDescription: trouble appending lists to another list

description: Trouble appending lists to another list


code:
import pandas as pd

df = pd.DataFrame(
    {'correlation':[0.5, 0.1,0.9],
     'p_value':[0.1,0.8,0.01]})
strformat = '%f'
significant = lambda x: strformat % x if x<0.05 else str(x)
formatters = {'p_value': significant}

print df.to_html(formatters=formatters, escape=False)

rawDescription: pandas.to_html() returning None

description: Pandas.to_html() returning None


code:
t = CustomTemplate(source)
t.set_custom_context(Context())
print t.render()

rawDescription: Lazy variable lookup in Jinja templates

description: Lazy variable lookup in Jinja templates


rawDescription: Using regex in python to get episode numbers from file name


code:
from multiprocessing import Pool, Manager

def f(some_list):
    some_list.append(4)
    print 'Child process: new list = ' + str(some_list)
    return True

if __name__ == '__main__':

    my_list = [1, 2, 3]
    m = Manager()
    my_shared_list = m.list(my_list)
    pool = Pool(processes=4)
    result = pool.apply_async(f, [my_shared_list])
    result.get()

    print 'Parent process: new list = ' + str(my_shared_list)

rawDescription: Python multiprocessing: object passed by value?


code:
for y in range(1, 11):
    print ' '.join(board[x, y] for x in range(1, 11))

rawDescription: Print an ordered dict info at specific lines in python


rawDescription: Autocorrelation of a multidimensional array in numpy


code:
def increment_db(self):
    """ Insert the selected favorite star into the database.
    """
    self.chosenartist=self.which_artist(self.v.get())
    print self.chosenartist

    self.config = {
              'user': 'begueradj',
              'passwd': 'bregredj',
              'host': '127.0.0.1',
              'db': 'begueradj',
    }
    try:
        self.connecttodb=MySQLdb.connect(**self.config)
    except MySQLdb.Error:
        print"Connexion error"

    self.cursor=self.connecttodb.cursor()

    self.cursor.execute("""INSERT INTO testtable(starname) VALUES(%s)""",self.chosenartist)

    self.connecttodb.commit()
    self.connecttodb.close()

rawDescription: how to connect to mysql database from gui made in python

description: Connect to mysql database from gui made in python


code:
def p4(base_num):
    numrefs = ['nums/89/202089/4', 'nums/39/205739/2', 'nums/94/203455/6']
    num_ignore = [150362, 147117]
    numrefs_index = dict((int(x.split('/')[2]), x) for x in numrefs)
    for line in file("num_ids.txt"):
        num = int(line)
        if num > base_num and num not in num_ignore and num in numrefs_index:
            print numrefs_index[num]

if __name__ == "__main__":
    p4(203456)

# prints:
# nums/39/205739/2

rawDescription: Matching a variable with lists and printing list value


code:
class FakeModule:
    def __getattr__(self, name):
        print name
        return name
sys.modules['fakeModule'] = FakeModule()
from fakeModule import something
something

rawDescription: Fake out module including attributes

description: Fake out module including attributes


code:
info = {}
with open('scoring_info.txt') as input:
    for line in input:
        player, stats, outcome, date = (item.strip() for item in line.split('-', 3))
        stats = dict(zip(('kills', 'deaths', 'assists'), map(int, stats.split('/'))))
        date = tuple(map(int, date.split('-')))
        info[player] = dict(zip(('stats', 'outcome', 'date'), (stats, outcome, date)))

print 'info:'
for player, record in info.items():
    print '  player %r:' % player
    for field, value in record.items():
        print '    %s: %s' % (field, value)

# sample usage
print
player = 'Fizz'
print '%s had %s kills in the game' % (player, info[player]['stats']['kills'])

rawDescription: Extract data from lines of text file in Python


code:
import json
import requests
from requests.auth import HTTPBasicAuth

username = "username"
password = "password"

# Base Service Communications URI
baseuri = "https://api.admin.microsoftonline.com/shdtenantcommunications.svc"
headers = {"accept": "application/json;odata=verbose"}
auth = {"username": username, "password": password}
# URI Paths
serviceinfo = "/GetServiceInformation"
register = "/Register"

payload = {'userName': username, 'password': password}
myheaders = {'Content-Type': 'application/json'}
data=json.dumps(payload)
response = requests.post(baseuri+register,data=json.dumps(payload),headers=myheaders)
responsedata = json.loads(response.text)
cookie = responsedata.get("RegistrationCookie")

payload1 = {'lastCookie':cookie,'locale':"en-US"}
response = requests.post(baseuri+serviceinfo,data=json.dumps(payload1),headers=myheaders)
responsedata = json.loads(response.text)
for myobject in responsedata:
   print myobject.get("ServiceName")

rawDescription: Querying Office 365 Service Communications API with Python


code:
for i in range(0, len(myList), 50):
     print myList[i:i+50]

rawDescription: Print item from list in chunks of 50 in Python


code:
file=open("sir_try.txt","r")
list_line=file.readlines()
file.close()
all_text=""

dic={}
sent="ensg"
temp_list=[]
for line in list_line:
    all_text=all_text+line
    name= line.rsplit()[0].strip()
    score=line.rsplit()[1].strip()
    dic[name]=score
for i in dic.keys():
    if sent in i:
        temp_list.append(dic[i])
hiegh_score=max(temp_list)

def check(index):
    reverse_text=all_text[index+1::-1]
    index2=reverse_text.find("\n")
    if sent==reverse_text[:index2+1][::-1][1:len(sent)+1]:
        return False
    else:
        return True

list_to_min=dic.values()
for i in temp_list:
    if i!=hiegh_score:
        index=all_text.find(str(i))
        while check(index):
            index=all_text.find(str(i),index+len(str(i)))
        all_text=all_text[0:index]+all_text[index+len(str(i)):]
        list_to_min.remove(str(i))
#write all text to "sir_try.txt"
file2=open("sir_try.txt","w")
file2.write(all_text)
file2.close()
min_score= min(list_to_min)
for j in dic.keys():
    if min_score==dic[j]:
        print "min score is :"+str(min_score)+" for person "+j

rawDescription: Finding the rating of words using python


code:
n_set = [1.0,3.2,4.5,8.2,1.3,2.2,5.6,9.8,2.4,5.5,6.7]

prev = None
result = []
current = []
for x in n_set:
    if prev is not None and x < prev:
        # Next element is smaller than previous element.
        # The current group is finished.
        result.append(current)

        # Start a new group.
        current = [x]
    else:
        # Add the element to the current group.
        current.append(x)

    # Remember the value of the current element.
    prev = x

# Append the last group to the result.
result.append(current)

print result

rawDescription: Set of float numbers, append numbers to empty list in sets . Python


code:
n = Integer()
print n
m = Integer(7)
m+=5
print m

rawDescription: Creating my own "integer" object in Python


code:
def nDigits(i):
    return len(str(i))
results = [nDigits(i) for i in xrange(1000000)]
print len(results)

rawDescription: the different efficiency for number digits counting with python & C

description: The different efficiency for number digits counting with python & C


code:
def find(q):
    return Pointer(q, 0)

class Pointer(object):
    def __init__(self, alist, index):
        self.alist = alist
        self.index = index

    def get(self):
        return self.alist[self.index]

    def set(self, value):
        self.alist[self.index] = value

list1 = [1, 2, 3]
p = find(list1)
p.set(0)
print list1

rawDescription: How to change the object that is referenced by a variable?

description: Change the object that is referenced by a variable


code:
class O():
    pass

o = O()
o.__dict__.setdefault('some_attr', None)

print o.some_attr

rawDescription: Ways to set default value for class attribute without using hasattr


code:
#!/bin/python
import subprocess
import datetime

IGNORE_FILESYSTEMS = ('Filesystem', 'tmpfs', 'cdrom', 'none')
LIMIT = 70

def main():
  df = subprocess.Popen(['df', '-H'], stdout=subprocess.PIPE)
  output = df.stdout.readlines()
  for line in output:
    parts = line.split()
    filesystem = parts[0]
    if filesystem not in IGNORE_FILESYSTEMS:
      usage = int(parts[4][:-1])  # Strips out the % from 'Use%'
      if usage > LIMIT:
        # Use smtplib sendmail to send an email to the admin.
        print 'Running out of space %s (%s%%) on %s"' % (
            filesystem, usage, datetime.datetime.now())

if __name__ == '__main__':
  main()

rawDescription: converting a shell script into python program

description: Converting a shell script into python program


code:
import re
pat = re.compile('([\w\s]+)\(([\w\s]+)(\d+/\d+)\)')
info = pat.match(s)
print info.groups()

('Michael Schenker Group ', 'House of Blues Dallas ', '3/26')

rawDescription: Python parsing

description: Python parsing


code:
M = np.random.rand(N, N)
M /= M.sum(axis=1).reshape((-1, 1))

r = np.ones(N, dtype=np.float) / N

print solve(r, M)

rawDescription: Theano: Using theano.scan with theano.scan_module.until


code:
from sklearn.utils.extmath import cartesian
print cartesian([a_p1, a_p2])

rawDescription: Combinatoric / cartesian product of Numpy arrays without iterators and/or loop(s)


code:
lst = ['/a/b', '/a/b/c', '/a/b/c/d', '/a/b/c/e', '/a/b/c/f/g', '/a/b/c/f/h', '/a/b/c/f/i']
dct = {}

for item in lst:
    p = dct
    for x in item.split('/'):
        p = p.setdefault(x, {})

print dct

rawDescription: How to parse a directory structure into dictionary?

description: Parse a directory structure into dictionary


code:
from checks import *  # the __init__.py is correct of this one
s="check"
cl=get_class("checks."+s+"."+s.title())
a=cl()
print a.get_name()

rawDescription: python use bound method of a class created by string variable

description: Python use bound method of a class created by string variable


code:
class Wrapper(object):
    def __init__(self, *funcs):
        self.funcs = funcs
    def call_funcs(self, *args):
        for func, args in zip(self.funcs, args):
            func(*args[0], **args[1])

def foo(*args, **kwargs):
    print 'foo(args={}, kwargs={})'.format(args, kwargs)

def bar(*args, **kwargs):
    print 'bar(args={}, kwargs={})'.format(args, kwargs)

def baz(*args, **kwargs):
    print 'baz(args={}, kwargs={})'.format(args, kwargs)

def bork(*args, **kwargs):
    print 'bork(args={}, kwargs={})'.format(args, kwargs)

wrapper = Wrapper(foo, bar, baz, bork)
wrapper.call_funcs(((), {}), ((1,), {}), ((), {'spam':3}), ((42,), {'dent':17}))
print

# to make using it easier and nicer looking, optionally include this:
params = lambda *args, **kwargs: (args, kwargs)

# then the same thing can be written:
wrapper.call_funcs(params(), params(1), params(spam=3), params(42, dent=17))

rawDescription: Passing (grouped) keyword args to wrapped functions


code:
import itertools

with open(fn) as f:
  for line, line2 in itertools.izip_longest(f, f, fillvalue=''):
    print line, line2

rawDescription: reading lines 2 at a time

description: Reading lines 2 at a time


code:
while True:
  try:
    i1 = c1.get_nowait()
    print "received %s from c1" % i1
  except queue.Empty:
    pass
  try:
    i2 = c2.get_nowait()
    print "received %s from c2" % i2
  except queue.Empty:
    pass
  time.sleep(0.1)

rawDescription: Multiplex on queue.Queue?

description: Multiplex on queue.Queue


code:
print {key:row.tolist() for key,row in df.set_index('Label1').iterrows()}

{'key3': ['col1value3', 'col2value3'],
 'key2': ['col1value2', 'col2value2'],
 'key1': ['col1value1', 'col2value1']}

rawDescription: Convert Dataframe to a Dictionary with List Values


code:
b_ele = user.find('b')
print b_ele.get_text()

rawDescription: Extracting certain text from within element


code:
from datetime import datetime, timedelta

d1 = datetime.strptime( "1900-01-01 23:46:00", "%Y-%m-%d %H:%M:%S" )
d2 = datetime.strptime( "1900-01-01 00:21:00", "%Y-%m-%d %H:%M:%S" )

if d2 < d1:
    d2 += timedelta(days=1)

print d2 - d1

# 0:35:00

rawDescription: Add 24 hours to a negative time difference in python


code:
#encrypt.py

import random

msg = raw_input("Enter your message: ")
out = ""
key = ""

for i in xrange(len(msg)):
    key += chr(random.randint(0,255))

k = 0

for char in msg:
    out += chr(ord(msg[k]) ^ ord(key[k]))
    k += 1
out = ":".join("{:02x}".format(ord(c)) for c in out)
key = ":".join("{:02x}".format(ord(c)) for c in key)
print "\nYour output is:", out
print "Your key is:", key

raw_input()

with open("output.txt","w+") as f:
    f.truncate()
    f.write(out+key)

rawDescription: XOR cryptographic algorithm not working for > 54 chars


code:
#! /usr/bin/python

import subprocess
import select

# Critical: all of this must fit inside ONE pipe() buffer
commands = ['echo Start\n', 'date\n', 'sleep 10\n', 'date\n', 'exit\n']

cmd = subprocess.Popen(['/bin/tcsh'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)

# The list "commands" holds a list of shell commands
for command in commands:
  cmd.stdin.write(command)

  # Must include this to ensure data is passed to child process
  cmd.stdin.flush()

for line in iter(cmd.stdout.readline, b''):
    print line

rawDescription: Displaying output of shell commands with shared environments


code:
class Gallery(object):
    def save(self, *args, **kwargs):
        return self, args, kwargs

class MyGallery(object):
    def save(self, *args, **kwargs):
        return Gallery.save.__func__(self, *args, **kwargs)
    # or
    # save = Gallery.save.__func__

mg = MyGallery()
print mg.save('arg', kwarg='kwarg')
# (<__main__.MyGallery object at 0x04DAD070>, ('arg',), {'kwarg': 'kwarg'})

rawDescription: Use a method from other classes w/o inheritance


code:
import urllib
import urlparse
from bs4 import BeautifulSoup

i = 0
while 1:
    url = "http://www.downloadcrew.com/?act=search&page=%d&cat=51" % i
    pageHtml = urllib.urlopen(url)
    soup = BeautifulSoup(pageHtml)

    has_more = 0
    for a in soup.select("div.productListingTitle a[href]"):
        has_more = 1
        try:
            print (a["href"]).encode("utf-8","replace")
        except:
            print "no link"
    if has_more:
        i += 1
    else:
        break

rawDescription: How can I get all the software link?

description: Get all the software link


code:
from xml.etree import ElementTree

tree = ElementTree.fromstring(example)

for child in tree.findall('.//child'):
    attribute1 = int(child.attrib['attribute1'])
    attribute2 = [int(v) for v in child.attrib['attribute2'].split(',')]
    print attribute1, attribute2

rawDescription: Python. Parsing XML: getting separated values of attributes as a list


code:
with open("Input1.txt") as in_file1, open("Input2.txt") as in_file2:
    line = next(in_file1).rstrip().split(",")
    for row in map(str.split, in_file2):
        print [row.count(item) for item in line]

rawDescription: How to check word in file with list element by using index?

description: Check word in file with list element by using index


code:
from random import sample

print map(chr,sample(xrange(33, 126), 8))

rawDescription: How do I generate 8 different random numbers in python?

description: Generate 8 different random numbers in python


code:
import re
import urllib2
from bs4 import BeautifulSoup as Soup

html = urllib2.urlopen("http://www.99acres.com/property-in-velachery-chennai-south-ffid").read()
soup = Soup(html)

re_digit = re.compile('(\d+)')
for div in soup.find_all('div', {'class': 'sT_disc grey'}):
    try:
        project = div.find('span').find('b').text.strip()
    except:
        project = 'No project'
    area = re.findall(re_digit, div.find('i', {'class': 'blk'}).text.strip())

    print ", ".join([project] + area)

rawDescription: Scraping and stripping off child tags python


code:
import unittest
import os
import copy

def data_dir():
    return os.path.join(os.path.dirname(__file__), 'data')

def get_subdirs(dir_name):
    """ retorna subdirectorios con path completo"""
    subdirs = []
    for f in os.listdir(dir_name):
        f_path = os.path.join(dir_name, f)
        if os.path.isdir(f_path):
            subdirs.append(f)
    return subdirs

def get_data_subdirs():
    return get_subdirs(data_dir())

def data_py_load(file_name):
    """ carga diccionario data desde archivo .py """
    name = file_name.split('.py')[0]
    path_name = 'data.' + name
    exec_str = "from {} import *".format(path_name)
    exec(exec_str)
    return data

class TestDirectories(type):

    def __new__(cls, name, bases, attrs):

        subdirs = get_data_subdirs()

        callables = dict([
            (meth_name, meth) for (meth_name, meth) in attrs.items() if
                meth_name.startswith('_test')
        ])

        data = {}
        for d in subdirs:
            data[d] = {}
            data[d]['name'] = d
            out_path = "{}.dataout.py".format(d)
            data[d]['out'] = data_py_load(out_path)
            var_path = "{}.datain.py".format(d)
            data[d]['in'] = data_py_load(var_path)

        for meth_name, meth in callables.items():
            for d in subdirs:
                new_meth_name = meth_name[1:]
                # name of test to add, _test to test
                test_name = "{}_{}".format(new_meth_name, d)
                # deep copy for dictionaries
                testeable = lambda self, func=meth, args=copy.deepcopy(data[d]): func(self, args)
                attrs[test_name] = testeable

        return type.__new__(cls, name, bases, attrs)

class TestData(unittest.TestCase):

    __metaclass__ = TestDirectories

    def _test_name(self, data):
        in_name = data['in']['name']
        out_name = data['out']['name']
        print in_name, out_name
        self.assertEquals(in_name, out_name)

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

rawDescription: Same tests over many similar data files

description: Same tests over many similar data files


code:
from django.contrib.auth.models import User, Group, Permission
from django.core.management.base import BaseCommand

class Command( BaseCommand ):

    help = "Creates test user"
    args = '<username> <password>'

    def handle( self, username, password ):

        user, created = User.objects.get_or_create( username=username )
        user.set_password( password )
        user.is_staff = True
        user.save()

        custom_perm = Permission.objects.get( codename='custom_perm_name' )
        user.user_permissions.add( custom_perm )

        if created:
            print "%s created" % username
        else:
            print "%s updated" % username

rawDescription: Django : Testing admin user

description: Django : Testing admin user


code:
import pandas as pd

table  = soup.find('table', attrs={'class': 'sortable statsb'})
header = [th.text for th in table.find('thead').select('th')]
header[:2] = ['',' ']
body   = [[td.text for td in row.select('td')]
             for row in table.findAll('tr', attrs = {"onmouseover":"hl(this)"})]
cols   =  zip(*body)
tbl_d  = {name:col for name, col in zip(header,cols)}

print pd.DataFrame(tbl_d, columns = header)

rawDescription: turning beautifulsoup output into matrix

description: Turning beautifulsoup output into matrix


code:
import asyncio
import multiprocessing
from concurrent.futures import ProcessPoolExecutor
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from datetime import datetime
import time

def day_limits():
        variable.value = 90
        print ('Day Variable: ',variable.value)

def night_limits():
        variable.value = 65
        print ('Night Variable: ',variable.value)

def thread_2(variable):
    while True:
        c_hour = int(datetime.now().strftime("%H"))
        c_min = int(datetime.now().strftime("%M"))
        c_sec = int(datetime.now().strftime("%S"))

        print ('%02d:%02d:%02d - Variable: %d ' % (c_hour,c_min,c_sec,variable.value))

        time.sleep(2)

if __name__ == "__main__":

    m = multiprocessing.Manager()
    variable = m.Value('i', 60)

    scheduler = AsyncIOScheduler()
    scheduler.add_job(day_limits, 'cron', hour=7,misfire_grace_time=3600,timezone='GB')
    scheduler.add_job(night_limits, 'cron', hour=19, minute=32,misfire_grace_time=3600,timezone='GB')
    scheduler.start()

    scheduler.print_jobs()

    executor = ProcessPoolExecutor(1)
    loop = asyncio.get_event_loop()
    baa = asyncio.async(loop.run_in_executor(executor, thread_2, variable))  # Need to pass variable explicitly

    try:
        loop.run_forever()

    except (KeyboardInterrupt, Exception):
        loop.stop()
        scheduler.shutdown()

rawDescription: Python 3 - Global Variables with AsyncIO/APScheduler


code:
by_prefix = {}
for x in keyList:
    by_prefix.setdefault(x.rsplit('_', 1)[0], []).append(x)

for prefix, matches in by_prefix.iteritems():
    print 'Matches for {} are {}'.format(prefix, by_prefix[prefix])

rawDescription: check for multiple substring match in a list while iterating list

description: Check for multiple substring match in a list while iterating list


code:
import httplib2
import os
import oauth2client
from oauth2client import client, tools
import base64
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from apiclient import errors, discovery

SCOPES = 'https://www.googleapis.com/auth/gmail.send'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Gmail API Python Send Email'

def get_credentials():
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'gmail-python-email-send.json')
    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        credentials = tools.run_flow(flow, store)
        print 'Storing credentials to ' + credential_path
    return credentials

def SendMessage(sender, to, subject, msgHtml, msgPlain):
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('gmail', 'v1', http=http)
    message1 = CreateMessage(sender, to, subject, msgHtml, msgPlain)
    SendMessageInternal(service, "me", message1)

def SendMessageInternal(service, user_id, message):
    try:
        message = (service.users().messages().send(userId=user_id, body=message).execute())
        print 'Message Id: %s' % message['id']
        return message
    except errors.HttpError, error:
        print 'An error occurred: %s' % error

def CreateMessage(sender, to, subject, msgHtml, msgPlain):
    msg = MIMEMultipart('alternative')
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = to
    msg.attach(MIMEText(msgPlain, 'plain'))
    msg.attach(MIMEText(msgHtml, 'html'))
    return {'raw': base64.urlsafe_b64encode(msg.as_string())}

def main():
    to = "to@address.com"
    sender = "from@address.com"
    subject = "subject"
    msgHtml = "Hi<br/>Html Email"
    msgPlain = "Hi\nPlain Email"
    SendMessage(sender, to, subject, msgHtml, msgPlain)

if __name__ == '__main__':
    main()

rawDescription: Sending email via gmail & python

description: Sending email via gmail & python


code:
# Module: f2.py
# Example 1: functions to store and retrieve global variables
gMyVar = 0
def print_world():
    print "World!"
def get_gMyVar():
    return gMyVar # no need for global statement
def inc_gMyVar():
    global gMyVar
    gMyVar += 1

# Example 2: class methods to store and retrieve properties
class useMyVars(object):
    def __init__(self, myVar):
        self.myVar = myVar
    def getMyVar(self):
        return self.myVar
    def setMyVar(self, myVar):
        self.myVar = myVar
    def print_helloWorld(self):
        print "Hello, World!"

rawDescription: split python source code into multiple files?

description: Split python source code into multiple files


code:
import shlex, subprocess, os, os.path
cmd = 'ls ' + os.path.expanduser('~/Downloads')
print subprocess.check_output(shlex.split(cmd))

cmd = 'ls ~/Downloads'
print subprocess.check_output(cmd, shell=True)

rawDescription: Subprocess call fails

description: Subprocess call fails


code:
import cgi, os, SocketServer, sys, time, urllib
from SimpleHTTPServer import SimpleHTTPRequestHandler
from StringIO import StringIO

class DirectoryHandler(SimpleHTTPRequestHandler):
    def list_directory(self, path):
        try:
            list = os.listdir(path)
        except os.error:
            self.send_error(404, "No permission to list directory")
            return None
        list.sort(key=lambda a: a.lower())
        f = StringIO()
        displaypath = cgi.escape(urllib.unquote(self.path))
        f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
        f.write("\n<title>Directory listing for %s</title>\n" % displaypath)
        f.write("\n
Directory listing for %s
\n" % displaypath)
        f.write("
\n
\n")
        for name in list:
            fullname = os.path.join(path, name)
            displayname = linkname = name
            date_modified = time.ctime(os.path.getmtime(fullname))
            # Append / for directories or @ for symbolic links
            if os.path.isdir(fullname):
                displayname = name + "/"
                linkname = name + "/"
            if os.path.islink(fullname):
                displayname = name + "@"
                # Note: a link to a directory displays with @ and links with /
            f.write('
%s - %s\n'
                    % (urllib.quote(linkname), cgi.escape(displayname), date_modified))
        f.write("
\n
\n\n\n")
        length = f.tell()
        f.seek(0)
        self.send_response(200)
        encoding = sys.getfilesystemencoding()
        self.send_header("Content-type", "text/html; charset=%s" % encoding)
        self.send_header("Content-Length", str(length))
        self.end_headers()
        return f

httpd = SocketServer.TCPServer(("", 8000), DirectoryHandler)
print "serving at port", 8000
httpd.serve_forever()

rawDescription: python: how to override SimpleHTTPServer to show timestamp in directory listing?

description: Python: how to override SimpleHTTPServer to show timestamp in directory listing


code:
x = 1.23456789
# length of the number before the decimal
left_sigdigs = len(str(x).partition(".")[0])
format_string = "{0:0." + str(4 + left_sigdigs) + "g}"
print format_string.format(x)

rawDescription: is there built-in custom numeric formating in python?

description: Is there built-in custom numeric formating in python


code:
from slimit import ast
from slimit.parser import Parser
from slimit.visitors import nodevisitor

data = """
var defaultTeamStatsConfigParams = {
        data:{
            url: 'stage-team-stat'
        },
        defaultParams: {
            stageId : 9155,
            field: 2,
            teamId: 32
        }
    };

    DataStore.prime('stage-team-stat', defaultTeamStatsConfigParams.defaultParams, [{"RegionId":252,"RegionCode":"gb-eng","TournamentName":"Premier League","TournamentId":2,"StageId":9155,"Field":{"Value":2,"DisplayName":"Overall"},"TeamName":"Manchester United","TeamId":32,"GamesPlayed":4,"Goals":6,"Yellow":7,"Red":0,"TotalPasses":2480,"Possession":247,"AccuratePasses":2167,"AerialWon":61,"AerialLost":49,"Rating":7.01,"DefensiveRating":7.01,"OffensiveRating":6.79,"ShotsConcededIBox":13,"ShotsConcededOBox":21,"TotalTackle":75,"Interceptions":71,"Fouls":54,"WasFouled":46,"TotalShots":49,"ShotsBlocked":9,"ShotsOnTarget":19,"Dribbles":44,"Offsides":3,"Corners":17,"Throws":73,"Dispossesed":36,"TotalClearance":78,"Turnover":0,"Ranking":0}]);

    var stageStatsConfig = {
        id: 'team-stage-stats',
        singular: true,
        filter: {
                instanceType: WS.Filter,
                id: 'team-stage-stats-filter',
                categories: { data: [{ value: 'field' }] },
                singular: true
        },
        params: defaultTeamStatsConfigParams,
        content: {
            instanceType: TeamStageStats,
            view: {
                renderTo: 'team-stage-stats-content'
            }
        }
    };

    var stageStats = new WS.Panel(stageStatsConfig);
    stageStats.load();
"""

parser = Parser()
tree = parser.parse(data)
fields = {getattr(node.left, 'value', ''): getattr(node.right, 'value', '')
          for node in nodevisitor.visit(tree)
          if isinstance(node, ast.Assign)}

print fields['stageId'], fields['field'], fields['teamId']

rawDescription: Obtaining correct data using Regex/List

description: Obtaining correct data using Regex/List


code:
from random import random

class Rectangle(object):
    def __init__(self, x1, y1, x2, y2):
        self._p1 = (x1, y1)
        self._p2 = (x2,y2)
        self._children = []

    def __str__(self):
        return "Rectangle defined by %s, %s, %i children" % (self._p1, self._p2, len(self._children))

    def is_child_of(self, other):
        return (self is not other and
            self._p1[0] > other._p1[0] and
            self._p2[0] < other._p2[0] and
            self._p1[1] > other._p1[1] and
            self._p2[1] < other._p2[1])

    def add_child(self, other):
        self._children.append(other)

    def check_relation_and_connect(self, other):
        if self.is_child_of(other):
            other.add_child(self)
        elif other.is_child_of(self):
            self.add_child(other)

if __name__=="__main__":
    rectangles = [Rectangle(random()*5, random()*5, random()*5+5, random()*5+5) for i in range(10)]

    for i in range(len(rectangles)):
        for j in range(i):
            rectangles[i].check_relation_and_connect(rectangles[j])

    for rectangle in rectangles:
        print rectangle

rawDescription: Data structure(s) for storing nested boxes?


code:
import itertools

def generate_replacements(lo, hi, alphabet, text):
    for count in range(lo, hi + 1):
        for indexes in itertools.combinations(range(len(text)), count):
            for letters in itertools.product(alphabet, repeat=count):
                new_text = list(text)
                actual_count = 0
                for index, letter in zip(indexes, letters):
                    if new_text[index] == letter:
                        continue
                    new_text[index] = letter
                    actual_count += 1
                if actual_count == count:
                    yield ''.join(new_text)

for text in generate_replacements(0, 2, 'BGCX', 'BBBX'):
    print text

rawDescription: Python - build new string of specific length with n replacements from specific alphabet


code:
from itertools import groupby
print ["".join(grp) for char, grp in groupby(s)]

rawDescription: Getting a list with new line characters

description: Getting a list with new line characters


code:
import socket
socket.RAND_add("this is a random string", 75.0) #Note: do not use in production

import urllib2
response = urllib2.urlopen('https://twitter.com') #Note: 'https'
html = response.read()
print html

rawDescription: Python 2.7 Test_SSL fails - RAND_status is 0 (insufficient randomness)


code:
import urllib2
content = urllib2.urlopen(some_url).read()
print content

rawDescription: How do you send an HTTP Get Web Request in Python?

description: Send an HTTP Get Web Request in Python


code:
t = Thing(a=1, b=2, c=3)
print t.a, t.b, t.c # prints 1, 2, 3

rawDescription: Python 3 object construction: which is the most Pythonic / the accepted way?


code:
import pandas as pd
import calendar
import sys

if len(sys.argv) != 3:
    print "Usage: {0} <month> <year>".format(sys.argv[0])
month, year = sys.argv[1:]

if not month in calendar.month_name:
    print "Invalid month! Month must be one of:{0}".format(str(calendar.month_name))
if not year.isdigit():
    print "Invalid year! Year must be a number."

nextmonth = calendar.month_name[1:][(calendar.month_name[1:].index(month) + 1) % 12]

df1 = pd.read_csv("data csv 1\{0} {1} 1 UUedit1.csv".format(month, year), delimiter = ",")
df2 = pd.read_csv("data csv 1\{0} {1} 2 UUedit2.csv".format(month, year), delimiter = ",")
merged = df1.merge(df2, on="Unique Element")
delcols = "Element_y", "number_y", "date_y", "title_y", "name_y"

for delcol in delcols:
    del merged[delcol]

merged.rename(columns={"name_x": "name", "rdate_x": "date", "title_x": "title", "number_x": "number", "Element_x": "Element"}, inplace = True)
merged = merged.sort("Element").reset_index(drop=True)
merged = merged.sort("date").reset_index(drop=True)

if month == calendar.month_name[-1]: year = str(int(year + 1))

merged.to_csv("{0} {1} MRG.csv".format(nextmonth, year), index=False, sep = ",")

rawDescription: Dynamically naming edited csv, to include part of the old csv name


code:
import os
import pwd
import subprocess
import sys

def main(my_args=None):
    if my_args is None: my_args = sys.argv[1:]
    user_name, cwd = my_args[:2]
    args = my_args[2:]
    pw_record = pwd.getpwnam(user_name)
    user_name      = pw_record.pw_name
    user_home_dir  = pw_record.pw_dir
    user_uid       = pw_record.pw_uid
    user_gid       = pw_record.pw_gid
    env = os.environ.copy()
    env[ 'HOME'     ]  = user_home_dir
    env[ 'LOGNAME'  ]  = user_name
    env[ 'PWD'      ]  = cwd
    env[ 'USER'     ]  = user_name
    report_ids('starting ' + str(args))
    process = subprocess.Popen(
        args, preexec_fn=demote(user_uid, user_gid), cwd=cwd, env=env
    )
    result = process.wait()
    report_ids('finished ' + str(args))
    print 'result', result

def demote(user_uid, user_gid):
    def result():
        report_ids('starting demotion')
        os.setgid(user_gid)
        os.setuid(user_uid)
        report_ids('finished demotion')
    return result

def report_ids(msg):
    print 'uid, gid = %d, %d; %s' % (os.getuid(), os.getgid(), msg)

if __name__ == '__main__':
    main()

rawDescription: Run child processes as different user from a long running process


code:
sel = selenium.selenium('localhost', 4444, '*firefox', 'http://www.google.com')
sel.start(True)
sel.open('')
print sel.captureNetworkTraffic('json')

rawDescription: Selenium-rc: How do you use CaptureNetworkTraffic in python


code:
import pipes

original = "/users/experiment_in14"
output2 = subprocess.check_output("find {} -empty".format(pipes.quote(original)), shell=True)
print output2

rawDescription: replace path in subprocess.check_output()

description: Replace path in subprocess.check_output()


code:
def diff_folder(folder1, folder2):
    for path1 in glob.glob(folder1+"/*.png"):
        filename = os.path.basename(path1)
        path2 = os.path.join(folder2, filename)
        if not os.path.exists(path2):
            continue
        im1 = Image.open(path1)
        im2 = Image.open(path2)
        diff = rmsdiff(im1, im2)
        if diff > threshold:
           print "different"

rawDescription: Comparing Image Files

description: Comparing Image Files


code:
import re
p = re.compile(ur'\b[0-9]{6}\b')
test_str = ur"C:\\Users\\416938\\AppData\\Roaming\\ESRI\\Desktop10.0\\ArcCatalog"
match = re.search(p, test_str)
if match:
    print(match.group(0))

rawDescription: How to search for data divided by backslash using regular expressions in Python

description: Search for data divided by backslash using regular expressions in Python


code:
with open('my_file_name') as in_file:
    for line in in_file:
        print line

rawDescription: Python3 Print First File Line

description: Python3 Print First File Line


code:
import re

def ask_user(answer):
    user_ans = raw_input("> ").strip()
    if user_ans == answer:
        print "Correct!\n"
        result = True
    else:
        print "Wrong!\n"
        result = False

    return result

question = None

with open('test.txt') as test:
    for line in test:
        if line[0] == 'Q':
            if question:
                ask_user(answer)
            question = line
        else:
            m = re.match(r'(.*?)\*(\d+)', line)
            if m:
                line, answer = m.groups()
                line = "%s%s\n" % (line,answer)

        print line,

# Final question
ask_user(answer)

rawDescription: How to print a line from a text file without a certain character

description: Print a line from a text file without a certain character


code:
import thread
import time

def read_key():
    import termios
    import sys
    fd = sys.stdin.fileno()
    old = termios.tcgetattr(fd)
    new = termios.tcgetattr(fd)
    new[3] &= ~(termios.ICANON | termios.ECHO) # c_lflags
    c = None
    try:
        termios.tcsetattr(fd, termios.TCSANOW, new)
        c = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSANOW, old)
    return c

def input_thread():
    read_key()
    thread.interrupt_main()

def countup():
    try:
        thread.start_new_thread(input_thread, ())
        for i in range(1000000):
            print i
            time.sleep(1)
    except KeyboardInterrupt:
        Z = raw_input("restart timer? ")
        if Z == 'y' or Z == 'Y':
            countup()

rawDescription: Making An "Any Key" Interuptable Python Timer


code:
x = {"foo": "bar"}
a = x
x.clear()
print a

rawDescription: Python : Variable in a different module doesn't get changed


code:
import numpy as np
a = np.random.rand(50,25,36) # Create a fake array
print a.shape
a.shape = -1 # Flatten the array in place
print a.shape

rawDescription: Python, from matrix to array

description: Python, from matrix to array


code:
def selfDecorator(func):
    def wrap(*args, **kw):
        return SelfGenerator(func, args, kw)
    return wrap

class SelfGenerator(object):
    """This class implements the generator interface"""
    def __init__(self, generator, args, kw):
        self.generator = generator(self, *args, **kw)
    def __iter__(self):
        return self
    def __next__(self):
        return next(self.generator)
    next = __next__
    def send(self, value):
        return self.generator.send(value)

@selfDecorator
def gen(self, x): # your generator function with self
    for i in range(x):
        yield self

for x in gen(5):
    print x # prints <__main__.SelfGenerator object at 0x02BB16D0>

rawDescription: Is there anything similar to "self" inside a Python generator?


code:
def test():
    print myList.__len__()
    for i in range(0, myList.__len__()):
        print i + myList.__len__()

import dis
dis.dis(test)

rawDescription: Is it better to save the length of a list that I use several time?


code:
import MySQLdb.converters
conv = MySQLdb.converters.conversions.copy()

class CustomList(list):
    def __init__(self, *items):
        super(CustomList, self).__init__(items)

conv[CustomList] = lambda lst, conv: "(%s)" % ', '.join(str(item) for item in lst)

con = MySQLdb.connect(db='test', conv=conv)
cur = con.cursor()
cur.execute('select * from test where id in %(foo)s', {'foo': CustomList(0, 1, 2)})
print cur._executed

rawDescription: Mysql named placeholders in python used in the IN clause


code:
@curry
def f(a, b, c, flag_foo=True, flag_bar=False):
    return 'horray!'

print f
print f(1, 2, flag_bar=True)
print f(1, 2, flag_bar=True)(3, flag_foo=False)
print f(1, 2, flag_bar=True)(3, flag_foo=False)()

rawDescription: Is there any way to lazy evaluate function.func_name?


code:
list=[]
f=open('list.dat','r')
for line in f.readlines():
    list.append(line.strip('\n').rsplit(','))

f.close()

print list

rawDescription: Python reads \n as string

description: Python reads \n as string


code:
print outplus, outdivide
-2.016 -8.06666666667

rawDescription: How to compute integrals dependent upon two variables with SciPy?

description: Compute integrals dependent upon two variables with SciPy


code:
import pty
from os import waitpid, execv, read, write

class ssh():
    def __init__(self, host, execute='echo "done" > /root/testing.txt',
                 askpass=False, user='root', password=b'SuperSecurePassword'):
        self.exec_ = execute
        self.host = host
        self.user = user
        self.password = password
        self.askpass = askpass
        self.run()

    def run(self):
        command = [
                '/usr/bin/ssh',
                self.user+'@'+self.host,
                '-o', 'NumberOfPasswordPrompts=1',
                self.exec_,
        ]

        # PID = 0 for child, and the PID of the child for the parent
        pid, child_fd = pty.fork()

        if not pid: # Child process
            # Replace child process with our SSH process
            execv(command[0], command)

        ## if we havn't setup pub-key authentication
        ## we can loop for a password promt and "insert" the password.
        while self.askpass:
            try:
                output = read(child_fd, 1024).strip()
            except:
                break
            lower = output.lower()
            # Write the password
            if b'password:' in lower:
                write(child_fd, self.password + b'\n')
                break
            elif b'are you sure you want to continue connecting' in lower:
                # Adding key to known_hosts
                write(child_fd, b'yes\n')
            else:
                print('Error:',output)

        # See if there's more output to read after the password has been sent,
        # And capture it in a list.
        output = []
        while True:
            try:
                output.append(read(child_fd, 1024).strip())
            except:
                break

        waitpid(pid, 0)
        return ''.join(output)

if __name__ == "__main__":
    s = ssh("some ip", execute="ls -R /etc", askpass=True)
    print s.run()

rawDescription: Enter an ssh password using the standard python library (not pexpect)


code:
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule

class MySpider(CrawlSpider):
    name = "craigs"
    allowed_domains = ["careers-cooperhealth.icims.com"]
    start_urls = ["https://careers-cooperhealth.icims.com"]

    deny = ['login', 'intro', 'candidate', 'referral', 'reminder', 'search']
    allow = ['jobs']

    rules = (Rule (SgmlLinkExtractor(deny = deny,
                                     allow=allow,
                                     restrict_xpaths=('*')),
                                     callback="parse_items",
                                     follow= True),)

    def parse_items(self, response):
        print "YAHOO!"

rawDescription: scrapy spider bypass deny my rules

description: Scrapy spider bypass deny my rules


code:
def list_append(count, id, res):
    #                      ^^^
    out_list = []
    for i in range(count):
        out_list.append(random.random())
    res[id] = out_list  # <------

if __name__ == "__main__":
    size = 10000000
    procs = 2
    manager = multiprocessing.Manager()  # <---
    res = manager.dict()                 # <---
    jobs = []
    for i in range(0, procs):
        process = multiprocessing.Process(target=list_append,args=(size, i, res))
        #                                                                   ^^^^
        jobs.append(process)

    for j in jobs:
        r = j.start()

    for j in jobs:
        j.join()

    print "List processing complete."
    # now `res` will contain results

rawDescription: How to access the result of a function called in a multiprocessing in Python?

description: Access the result of a function called in a multiprocessing in Python


code:
# generate 2d data
a = np.array((np.random.rand(5,5)) * 10, dtype=np.uint8)

# check all neighbours
for ax in range(len(a.shape)):
    for i in [-1,1]:
        print a == np.roll(a, i, axis=ax)

rawDescription: Numpy/Python: Array iteration without for-loop


code:
import re
file = open( "Textfile.txt", "r" ).readlines()
var=raw_input("enter the value")
for line in file:
    if line.find('Block1:') != -1:
        if re.search(var,line,re.IGNORECASE):
            print "value=",line

rawDescription: Finding the "a" string in file within a block for one time using python


code:
def visualize(self, imin, imax, typ_ = "wireframe"):
    """
    3d plot signal between imin and imax
    . typ_: type of plot, "wireframce", "surface"
    """

    times = self.retT[imin:imax]
    nrows = (int)((times[(len(times)-1)] - times[0])/self.mod) + 1

    self.modulate(imin, imax)

    fig = plt.figure('3d view')
    ax = fig.gca(projection='3d')

    x = []
    for i in range(nrows):
        x.append(self.matRetT[i][0] + self.mod * i)

    y = []
    for i in range(len(self.matRetT[0])):
        y.append(self.matRetT[0][i])
    y = y[:-1]

    X,Y = np.meshgrid(x,y)

    z = [tuple(self.matGC2D[i]) for i in range(len(self.matGC))] # matGC a matrix

    zzip = zip(*z)

    for i in range(len(z)):
        print len(z[i])

    if(typ_ == "wireframe"):
        ax.plot_wireframe(X,Y,zzip)
        plt.show()
    elif(typ_ == "contour"):
        cset = ax.contour(X, Y, zzip, zdir='z', offset=0)
        plt.show()
    elif(typ_ == "surf_contours"):
        surf = ax.plot_surface(X, Y, zzip, rstride=1, cstride=1, alpha=0.3)
        cset = ax.contour(X, Y, zzip, zdir='z', offset=-40)
        cset = ax.contour(X, Y, zzip, zdir='x', offset=-40)
        cset = ax.contour(X, Y, zzip, zdir='y', offset=-40)
        plt.show()

rawDescription: matplotlib wireframe plot / 3d plot howTo

description: Matplotlib wireframe plot / 3d plot howTo


code:
def binaryDecomposition(value):
  return [1 << i for i in xrange(len(FLAGS)) if value & (1 << i)]
print binaryDecomposition(22)

rawDescription: Standard Solution for Decoding Additive Numbers


code: print dir(B.__class__)

rawDescription: Why does a class have the attributes of its metaclass?


code:
sizes = []
for i in a:
        dest = '/home/zurelsoft/my_files'
        fullname = os.path.join(dest, i) #Get the file_full_size to calculate size
        st = int(os.path.getsize(fullname))
        f_size = size(st)
        sizes.append(f_size)
print sum(sizes)

rawDescription: How to add all the elements inside loop in Python?

description: Add all the elements inside loop in Python


code:
regex = re.compile(r'_(.*)\.')
print regex.search('10.10.10.17_super-micro-100-13.txt').group(1)
# super-micro-100-13

rawDescription: Python: RegEx assistance

description: Python: RegEx assistance


code:
def fallback(elapsed, total):
    tx_pc = lambda chunk, full: chunk * 100 / full
    print str(tx_pc(elapsed, total)) + "% operation completed"

    if tx_pc(elapsed, total) == 100:
        print "Operation complete: 100%"

rawDescription: How can I make a progress bar for Inkscape extension?

description: Make a progress bar for Inkscape extension


code:
cursor = db.cursor()
max_age = 42
cursor.execute("""SELECT name FROM employees WHERE age < %s""", (max_age,))
print cursor.fetchone()

rawDescription: What is the process for using MySQL from Python in Windows?


code:
list1 = ["well", "antifederalist", "mooooooo"]

print next((s for s in list1 if len(s) == 3), None)

rawDescription: Get string of certain length out of a list


code:
import hello_ext
print hello_ext.greet()

rawDescription: Running algorithms in compiled C/C++ code within a Java/PHP/Python framework?


code:
import argparse

class Server:
    def check(self):
        return self.__class__.__name__

class FooServer(Server):
    pass

class BarServer(Server):
    pass

def get_server(server):
    try:
        klass = globals()[server.capitalize()+'Server']
        if not issubclass(klass, Server):
            raise KeyError

        return klass()
    except KeyError:
        raise argparse.ArgumentTypeError("%s is not a valid server." % server)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(dest='command')

    check = subparsers.add_parser('check')
    check.add_argument('server', type=get_server)

    args = parser.parse_args()

    print getattr(args.server, args.command)()

rawDescription: Python ArgParse Subparsers and linking to the correct function


code: print maxvalpos([4, 2, 5, 10])

rawDescription: Find the sum and position of the maximum value in a list


code:
import glob
import os

path = 'D:\HeadFirstPython\chapter3'
os.chdir(path)
with open("temp.txt","w") as f: # with automatically closes your files
   my_l = [x.replace(".jpg","").rsplit("_",1) for x in glob.glob("*.jpg")] # list of lists

with open("temp.txt", "w") as f:
    for x in glob.glob("*.jpg"):
        print x.replace(".jpg", "").rsplit("_", 1) # each list

rawDescription: Save file list into text file using python


code: print ser.readline()

rawDescription: Python Noob - Silly Question? Works in Python Interpreter, not from CLI


code:
abc = {"type":"insecure","id":"1","name":"peter"}
black_list = {"type"}
rename ={"id":"identity"}  #use a mapping dictionary in case you want to rename multiple items
dic = {rename.get(key,key) : val for key ,val in abc.items() if key not in black_list}
print dic

rawDescription: create a new dict based on old dict

description: Create a new dict based on old dict


code:
from decimal import Decimal as D
for x in 1.0, 2, 10.010, D("1000.0000"), D("10.5"), D("9.0"):
    print str(x), "->", normalize(x)

rawDescription: How can I "trim" significant figures of a Decimal in Python to only those that are non-zero?


code:
def split_str(s, n):
  if n == 1:
     yield (s,)
  else:
    for i in range(len(s)):
      left, right = s[:i], s[i:]
      for substrings in split_str(right, n - 1):
        yield (left,) + substrings

for substrings in split_str('house', 3):
  print substrings

rawDescription: Creating an exhaustive list of `n` substrings


code:
#!/usr/bin/env python

import sys

def nifty_thing(*names):
    frame = sys._getframe(1)
    locals = frame.f_locals

    for name in names:
        locals[name] = None

class foo(object):

    nifty_thing("foo", "bar", "baz")

f = foo()

print f.foo, f.bar, f.bar

rawDescription: Is there a Python 'shortcut' to define a class variable equal to a string version of its own name?


code:
@countcalls
def f():
    print 'f called'

rawDescription: Unable to understand this python decorator


code:
import functools
import time

def memoize(meth):
    @functools.wraps(meth)
    def wrapped(self, *args, **kwargs):

        # Prepare and get reference to cache
        attr = "_memo_{0}".format(meth.__name__)
        if not hasattr(self, attr):
            setattr(self, attr, {})
        cache = getattr(self, attr)

        # Actual caching
        key = args, tuple(sorted(kwargs))
        try:
            return cache[key]
        except KeyError:
            cache[key] = meth(self, *args, **kwargs)
            return cache[key]

    return wrapped

def network_call(user_id):
    print "Was called with: %s" % user_id
    return 1

class NetworkEngine(object):

    @memoize
    def search(self, user_id):
        return network_call(user_id)

if __name__ == "__main__":
    e = NetworkEngine()
    for v in [1,1,2]:
        e.search(v)
    NetworkEngine().search(1)

rawDescription: How can memoized functions be tested?

description: How can memoized functions be tested


code:
import poppler, os.path

path = 'file://%s' % os.path.realpath('annotations.pdf')
doc = poppler.document_new_from_file(path, None)
pages = [doc.get_page(i) for i in range(doc.get_n_pages())]

for page_no, page in enumerate(pages):
    items = [i.annot.get_contents() for i in page.get_annot_mapping()]
    items = [i for i in items if i]
    print "page: %s comments: %s " % (page_no + 1, items)

rawDescription: Extracting PDF annotations/comments

description: Extracting PDF annotations/comments


code:
result = testing(0,10)
print result

rawDescription: How to import a module of functions and pass variables between modules

description: Import a module of functions and pass variables between modules


code:
from functools import partial
def foo(x, y):
    print x+y

partial(foo, y=3)
f = partial(foo, y=3)
f(2)

rawDescription: Python lambda with regex

description: Python lambda with regex


code:
def flatten(LoL):
    for el in LoL:
        if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
            for sub in flatten(el):
                yield sub
        else:
            yield el
newA=[]
seen=set()
for e in A+B:
    fset=frozenset(flatten(e))
    if fset not in seen:
        newA.append(e)
        seen.add(fset)

print newA
# [['foo', 123], ['bar', array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])], ['baz', 345], ['meow', 456]]

rawDescription: Python: Intersection of two lists of lists


code:
def lessThan4(aList):
    new_list = [i for i in aList if len(i) < 4]
    return new_list

print lessThan4(['Wrt', 'XPZG', 'ZPvwkHLN', 'pC', '', 'pZStoOc', ''])

rawDescription: Write a Python function that returns the sublist of strings that contain fewer than 4 characters


code:
from bs4 import BeautifulSoup

data = """
Your html here
"""

soup = BeautifulSoup(data)
for p in soup.find('div', attrs={'class':'InternaTesto'}).find_all("p"):
    print p.text, [li.text for li in list(p.next_siblings)[2].find_all('li')]

rawDescription: Parse bullet list in correct order with beautifulsoup


code:
string = raw_input("Input here:")
def token(string):
    start = 0
    i = 0
    token_list = []
    for x in range(0, len(string)):
        if " " == string[i:i+1][0]:
            token_list.append(string[start:i+1])
            #print string[start:i+1]
            start = i + 1
        i += 1
    token_list.append(string[start:i+1])
    return token_list

print token(string)

rawDescription: Converting a string to array of words - Python


code:
import shelve

def main(argv):
    """Main of script 1
    Due to the  internal structure of the script this
    main function must always be called with the flag -d
    and a corresponding argument.
    """

    settings = shelve.open('mySettings')

    global now
    now = datetime.datetime.now()

    settings['vroot_directory'] = commands.getoutput("pwd")

    settings['testcase_list_file'] = 'no_argument'

    try:
        opts, args = getopt.getopt(argv, "d:t:",
            ["directory_path=", "testcase_list="])
    except getopt.GetoptError, err:
        print command_syntax
        sys.exit()
    for opt, arg in opts:
        if opt in ("-d", "--directory"):
            settings['vroot_directory'] = arg
        if opt in ("-t", "--testcase"):
            settings['testcase_list_file'] = arg

    def function1():
        pass

    def function2():
        if testcase_list_file == 'no_argument':
            function1()
        else:
            function2()

if __name__ == "__main__":
    main(sys.argv[1:])

rawDescription: Importing python variables of a program after its execution


code:
class ADryRow(DryBase):
    __slots__ = ['seeds']

    ripeness = slot()
    colour = slot()
    juiciness = slot()

    @memo(alias='seeds?')
    def has_seeds(self):
        print "Expensive!!!"
        return self.seeds > 0

rawDescription: OO design: an object that can be exported to a "row", while accessing header names, without repeating myself


code:
def specialmethod(f):
    @functools.wraps(f)
    def wrapper(arg1, arg2):
        print arg1, arg2
        f(arg1, arg2)
    return wrapper

rawDescription: Python decorate method with callable outside parent class


code:
while True:
    try:
            print 'Hit ^C to add 5 to score, and print score, bonus'
            time.sleep(1)
    except KeyboardInterrupt:
            score+=5
            bonus = score/5
            print score, bonus

rawDescription: Increment a variable based on another variable


code:
import argparse
import socket
import threading
import thread
import time

class server(threading.Thread):
    def __init__(self, port):
        threading.Thread.__init__(self)
        self.s = socket.socket()
        self.host = socket.gethostname()
        self.port = port

    def run(self):
        print 'Server started!'
        print 'Waiting for clients...'

        self.s.bind((self.host, self.port))
        self.s.listen(5)
        c, addr = self.s.accept()
        print 'Got connection from', addr
        while True:
            time.sleep(2)
            msg = c.recv(1024)
            if len(msg) == 0 :  break
            print addr, ' >> ', msg

class client(threading.Thread):
    def __init__(self, port):
        threading.Thread.__init__(self)
        self.s = socket.socket()
        self.host = socket.gethostname()
        self.port = port

    def run(self):
        while True:
            time.sleep(5)
            print 'Connecting to ', self.host, self.port
            try:
                self.s.connect((self.host, self.port))
                break
            except Exception as err:
                print "Waiting", err
        print "Connectd"
        while True:
            time.sleep(2)
            msg = raw_input('CLIENT >> ')
            if len(msg) == 0:break
            self.s.send(msg)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--server_port', type = int, default = 8888)
    parser.add_argument('--client_port', type = int, default = 7777)
    args = parser.parse_args()

    thread1 = server(args.server_port)
    thread2 = client(args.client_port)

    thread1.start()
    thread2.start()

    thread1.join()
    thread2.join()

rawDescription: python threading between two classes not working properly

description: Python threading between two classes not working properly


code:
ids={ sec:config_instance.get(sec,'id') for sec in sections }
print ids['sec1']

rawDescription: config parser python

description: Config parser python


code:
import re
eqns = ('y=eexp(xyz)', 'y=pifrexp(yt)', 'y=sin(x)exp(y)')
for equation in eqns:
    equation = re.sub(r"([)xyzuvet]|pi|exp|frexp)([xyzuvet]|pi|exp|frexp)\b",r"\1*\2", equation)
    equation = re.sub(r"([)xyzuvet]|pi|exp|frexp)([xyzuvet]|pi|exp|frexp)\b",r"\1*\2", equation)
    print equation

rawDescription: Using Python's Regular Expression to Insert Symbol Between Certain Characters


code:
from collections import namedtuple
Fruit = namedtuple("Fruit", ["name", "color"])
f = Fruit(name="banana", color="red")
print f
f.name
f.color

rawDescription: Python: Tuples/dictionaries as keys, select, sort


code:
mytext = get_data_text(9, 2012)  # this time let's get the text for September, 2012
print mytext[0]

rawDescription: Formatting data from Text (.prn) file

description: Formatting data from Text (.prn) file


code: print "I'm sorry to hear that {} ".format(name)

rawDescription: Python2 word input if-statement script

description: Python2 word input if-statement script


rawDescription: How to sort integer list in python descending order

description: Sort integer list in python descending order


code:
u2 = s.query(User).first()
print u2.name
u2 is u

rawDescription: In Sqlalchemy, if i add an object using session.add() and flush it, session.query() does not give that object, why?


code:
weights = np.linspace(1, 2, N)
Xw = X * np.sqrt(weights)[:, None]
yw = y * np.sqrt(weights)
print linalg.lstsq(Xw, yw)[0]

rawDescription: Understanding scipy's least square function with IRLS


code:
import re
from bs4 import BeautifulSoup

data = """your html here"""

soup = BeautifulSoup(data)

s = soup.find('p').br.previous_sibling  # find "p" element and get the part before the 1st br
match = re.search('string\-1\.string2 \d+ (\w+) string3\.string', s)
print match.group(1)

rawDescription: How to extract html to text in BeautifulSoup?

description: Extract html to text in BeautifulSoup


code:
total = 0

with open('input.txt') as f:
    for line in f:
        finded = line.find('California')
        if finded != -1 and finded != 0:
            total += 1

print total

rawDescription: Python: Count how many times a word occurs in a file


code:
i =len(range (0, 5))
print i

rawDescription: How to get the count of output in python

description: Get the count of output in python


code:
import urllib2
print urllib2.urlopen('http://bit.ly/1cPIdPg').url

rawDescription: Using python to expand a bit.ly link

description: Using python to expand a bit.ly link


code:
import numpy as np
d = {}
d['1/2/15'] = {'Project 1' : [1,4,7], 'Project 2' : [1,5, 11]}

for key, value in d.iteritems():
        d_avg = {}
        for k, v in value.iteritems():
            avg =  np.mean([x for x in d[key][k] if x < 6])
            d_avg[str(k) + ' Average'] = avg
        d[key].update(d_avg)

print d

rawDescription: Adding key for average values in nested dictionary lists


code:
word = "llamas"
for character in word:
  word = None
  print character

rawDescription: Python 'for word in word' loop

description: Python 'for word in word' loop


code:
dice = [2, 3, 5, 1, 4]
if any(ls.issubset(dice) for ls in largeStraight):
    print 'Large!'
elif any(ss.issubset(dice) for ss in smallStraight):
    print 'Small!'

dice = [2, 3, 5, 1, 6]
if any(ls.issubset(dice) for ls in largeStraight):
    print 'Large!'
elif any(ss.issubset(dice) for ss in smallStraight):
    print 'Small!'

dice = [2, 3, 6, 4, 1]
if any(ls.issubset(dice) for ls in largeStraight):
    print 'Large!'
elif any(ss.issubset(dice) for ss in smallStraight):
    print 'Small!'

rawDescription: Python - Comparing lists

description: Python - Comparing lists


code:
import urllib
from multiprocessing import Pool

URL = "http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=sl1t1v&e=.csv"
symbols = ('GGP', 'JPM', 'AIG', 'AMZN','GGP', 'JPM', 'AIG', 'AMZN')

def fetch_quote(symbols):
    url = URL % '+'.join(symbols)
    fp = urllib.urlopen(url)
    try:
        data = fp.read()
    finally:
        fp.close()
    return data

def main():
    for symbol in symbols:
        data_fp = fetch_quote((symbol,))
        print data_fp

if __name__ =='__main__':
    pool = Pool(processes=5)
    for symbol in symbols:
        result = pool.apply_async(fetch_quote, [(symbol,)])
        print result.get(timeout=1)

rawDescription: python multi threading/ multiprocess code

description: Python multi threading/ multiprocess code


code:
import datetime as dt

def  sim(startdate, enddate):
    time_format_one = "%b %d, %Y"
    time_format_two = "%Y,%m,%d"

    try:
        dt_start = dt.datetime.strptime(startdate, time_format_one)
        dt_end = dt.datetime.strptime(enddate, time_format_one)
    except ValueError:
        dt_start = dt.datetime.strptime(startdate, time_format_two)
        dt_end = dt.datetime.strptime(enddate, time_format_two)

    print 'Start Date: ', dt_start.date()
    print 'End Date: ', dt_end.date()

# in String format
sim('Jan 1, 2011', 'Dec 31, 2011')

# in interger in string format
sim('2011,1,1', '2011,12,31')

rawDescription: Date String to Time tuple (python)

description: Date String to Time tuple (python)


code:
from time import sleep

def printit():
    cpt = 1
    while True:
        if cpt < 3:
            number = ("images[" + str(cpt) + "].jpg")
            yield number #here is the return
            sleep(5)
            cpt+=1
        else:
            for number in printit():
                yield number

for number in printit():
    print number

rawDescription: How can I return a changing variable without ending the function in Python?

description: Return a changing variable without ending the function in Python


code:
from jinja2 import Template
t = '''{% set name=name or "John Doe" %}Hello {{ name }}'''
template = Template(t)
print template.render(name='Jonnie Doe')
print template.render()

rawDescription: Sane way to define default variable values from within a jinja template?


code:
def flatten(items):
    for elem in items:
        if isinstance(elem[0],list):
            for sub_elem in elem:
                yield sub_elem
        else:
            yield elem

print list(flatten(DictofCoordinates.values()))
[[1, 1], [[3, 3], [3, 3], [3, 3]], [2, 2], [2, 2], [2, 2], [[41, 41], [41, 41]], [[42, 42], [42, 42]]]

rawDescription: recursively exploding a list of list to "atomic list"

description: Recursively exploding a list of list to "atomic list"


code:
from collections import Counter
mylist = [u'retw', u'folivi_jochan', u':', u'rt', u'newsycombinator', u':', u'uber', u'is', u'taking', u'millions', u'of', u'manhattan', u'rides', u'away', u'from', u'taxis', u'http', u':', u'//t.co/zluyq3f6cc', u'retw', u'chr1sa', u':', u'rt', u'newsycombinator', u':', u'uber', u'is', u'taking', u'millions', u'of', u'manhattan', u'rides', u'away', u'from', u'taxis', u'http', u':', u'//t.co/zluyq3f6cc', u'retw', u'olutosinfashusi', u':', u'rt', u'newsycombinator', u':', u'uber', u'is', u'taking', u'millions', u'of']
c = Counter(mylist)
print dict(c)
[(u':', 8),
 (u'rt', 3),
 (u'uber', 3),
 (u'newsycombinator', 3),
 (u'of', 3),
 (u'is', 3),
 (u'retw', 3),
 (u'taking', 3),
 (u'millions', 3),
 (u'from', 2),
 (u'//t.co/zluyq3f6cc', 2),
 (u'manhattan', 2),
 (u'away', 2),
 (u'http', 2),
 (u'taxis', 2),
 (u'rides', 2),
 (u'olutosinfashusi', 1),
 (u'chr1sa', 1),
 (u'folivi_jochan', 1)]

rawDescription: Regex: creating a dictionary from a list of strings


code:
import numpy as np
numrows, numcols = 10,3
x = np.random.random((numrows, numcols)) > 0.5
x = x.view(','.join(numcols * ['i1'])) # <- View the rows as a 1D structured array...

unique_vals, indicies = np.unique(x, return_inverse=True)
counts = np.bincount(indicies)

print unique_vals, counts

rawDescription: comparing row in numpy array

description: Comparing row in numpy array