code:
keyspace = "fw59eorpma2nvxb07liqt83_u6kgzs41-ycdjh" # Can be anything you like - this was just shuffled letters and numbers, but...
assert len(set(keyspace)) == len(keyspace) # each character must occur only once

def test(v):
    s = int_str(v, keyspace)
    w = str_int(s, keyspace)
    print "OK? %r -- int_str(%d) = %r; str_int(%r) = %d" % (v == w, v, s, s, w)

test(1064463423090)
test(4319193500)
test(495689346389)
test(2496486533)

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


code:
from operator import itemgetter
from itertools import *
seq2 = [1, 2, 4, 5, 6, 8, 9, 10]
list = []
for k, g in groupby(enumerate(seq2), lambda (i,x):i-x):
    list.append(map(itemgetter(1), g))

print list

rawDescription: Python splitting list based on missing numbers in a sequence


code:
query = "You come to a crossroads, left or right?"
lor = raw_input(query).strip().lower()
while lor not in ("left", "right"):
    print "That's not a direction."
    lor = raw_input(query).strip().lower()

if lor == "left":
    print "You went left"
else:
    print "You went right"

rawDescription: Please help me complete my code

description: Please help me complete my code


code:
for (k, v) in user['ban_info'].iteritems():
    print k
    print v

rawDescription: Showing key values Array Items on Django Template


code:
import csv
list_new = []
with open('testingSeta.csv') as csvfile:
    for row in csv.reader(csvfile):
        print row
        row.insert(0, row.pop())
        print row
        list_new.append(row)
        print list_new
        list_new.pop(0)
        print list_new
print list_new

rawDescription: How do I remove all the zero's from a dataset that is in a list?

description: Remove all the zero's from a dataset that is in a list


code:
categories = {}

def get_parent_category(cat_id):
    if len(cat_id) <= 2:
        return '';
    else:
        return cat_id[:-1]

with open('my.csv', "r") as ins:
    for line in ins:
        # remove double quotes: replace('"', '')
        # remove break line : rstrip()
        a = str(line).replace('"', '').rstrip().split('|')

        cat_id = a[0]

        if cat_id != 'CatNo':
            categories[cat_id] = {
                'parent': get_parent_category(cat_id),
                'desc': a[1],
                'long_desc': a[2]
            };

print 'Categories relations:'

for p in categories:

    parent = categories[p]['parent']
    output = categories[p]['desc']

    while parent != '':
        output = categories[parent]['desc'] + ' \\ ' + output
        parent = categories[parent]['parent']

    print '\t', output

rawDescription: Convert a csv into category-subcategory using array


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

xi = np.array([0., 0.5, 1.0])
yi = np.array([0., 0.5, 1.0])
zi = np.array([[0., 1.0, 2.0],
               [0., 1.0, 2.0],
               [-0.1, 1.0, 2.0]])

v = np.linspace(-.1, 2.0, 15, endpoint=True)
plt.contour(xi, yi, zi, v, linewidths=0.5, colors='k')
plt.contourf(xi, yi, zi, v, cmap=plt.cm.jet)
x = plt.colorbar(ticks=v)
print x
plt.show()

rawDescription: Setting the limits on a colorbar in matplotlib


code:
messageCatalogueEntry = x.findall("MessageCatalogueEntry")
for m in messageCatalogueEntry:
    print et.tostring(m[0], encoding='utf8')

rawDescription: Merge two XML files by matching elements by attribute value


code:
from collections import defaultdict
d = defaultdict(dict)

with open('test.txt') as f:
    next(f, None) # skip header

    for line in f:
        group, type_, fraction = line.split()
        d[group][type_] = fraction

types = sorted(set(k for g in d.values() for k in g))

print 'GROUP ' + ''.join('{:<7}'.format('TYPE_' + t) for t in types)
for i, g in sorted(d.items()):
    print '{0:<6}'.format(i) + ''.join('{:<7}'.format(g.get(k, 0.0))
                                       for k in types)

rawDescription: How to modify tables in Python?

description: Modify tables in Python


code:
import requests
from bs4 import BeautifulSoup

page_url = 'http://www.afl.com.au/afl/stats/player-ratings/overall-standings'
token_url = 'http://www.afl.com.au/api/cfs/afl/WMCTok'
player_url = 'http://www.afl.com.au/api/cfs/afl/playerProfile/'

session = requests.Session()
r = session.get(page_url)
soup = BeautifulSoup(r.content)
token = session.post(token_url).json()['token']

for player in soup.find_all('a', {'data-playerid': True}):
    playerid = player['data-playerid']
    data_r = session.get(player_url + playerid, headers={
        'X-media-mis-token': token})
    profile = data_r.json()['playerProfile']
    print profile['firstName'], profile['surname'], profile['position']

rawDescription: Cannot fetch javascript element

description: Cannot fetch javascript element


code:
#!/usr/bin/python

import sys
import os

def calc(console_name):
        f = open("a.txt")

        lines = f.readlines()[1:]
        # skipped first line

        num_people = 0
        num_males = 0
        num_females = 0
        num_married = 0
        num_unmarried = 0
        min_age = 0
        max_age = 0

        for line in lines:
                fields = line.split()

                if (fields[-1] == console_name):
                        num_people += 1

                        # Age
                        age = int(fields[4])
                        if min_age == 0:
                                min_age = age
                        else:
                                if age < min_age:
                                        min_age = age

                        if max_age == 0:
                                max_age = age
                        else:
                                if age >= max_age:
                                        max_age = age

                        # civil status
                        if fields[5] == 'M':
                                num_married += 1
                        elif fields[5] == 'S':
                                num_unmarried += 1

                        # Gender
                        if fields[6] == 'M':
                                num_males += 1
                        else:
                                num_females += 1

        print "For %s there are %s people, which %s are male and %s female, \
                %s are married and %s are unmarried, age range [ %s - %s]" % (console_name, num_people, num_males, num_females, num_married, num_unmarried, min_age, max_age)

c = 'W'
calc(c)

rawDescription: Counter according to other data and a range


rawDescription: Python - sending unicode characters (prefixed with \u) in an HTTP POST request


code:
def do_evil(self, method):
    print "doing evil"
    MyMixin.pre_save(self)
    method(self)

class ConcentratedEvil(type):
    tainted_classes = ['MyView'] # Add others as needed
    def __new__(mcs, name, bases, dict):
        if name in mcs.tainted_classes:
            print "tainting {0} with the power of evil".format(name)
            old_method = dict['pre_save']
            dict['pre_save'] = lambda x: do_evil(x, old_method)
            print dict.keys()
        return type.__new__(mcs, name, bases, dict)

class BaseView(object):
    pass

class MyMixin(object):
    __metaclass__ = ConcentratedEvil
    def pre_save(self):
        print "mixin presave"

class MyView(MyMixin, BaseView):
    def pre_save(self):
        print "view presave"

rawDescription: Python mixin with a method that cannot be overridden (by default)?


code:
import inspect

a = A.NestA

print a in [x[1] for x in inspect.getmembers(A, inspect.isclass)]
print a in [x[1] for x in inspect.getmembers(B, inspect.isclass)]

rawDescription: How to find out the nested class hierarchy?

description: Find out the nested class hierarchy


code:
from itertools import izip, islice
for (op, code) in izip(islice(s, 0, None, 2), islice(s, 1, None, 2)):
    print op, code

rawDescription: Iterate over a string 2 (or n) characters at a time in Python


code:
import requests
import re

sess = requests.Session()
res = sess.get('http://finance.yahoo.com/q/ks?s=MMM+Key+Statistics')
print re.findall('Diluted\s+EPS\s+\(ttm\).*?>([\d.]+)<', res.text)

rawDescription: Using Regular Expressions With Python to Get Value Buried in HTML5


code:
@takes_1dim_arr
def func(arr):
   print arr.shape

rawDescription: Python Passing List or Numpy Array

description: Python Passing List or Numpy Array


rawDescription: Applying the collections.counter() on UTF-8


code:
urls=[]
for x in soup.find_all('a', href=True):
    urls.append(x['href'])
print urls

rawDescription: Assigning the output of a for loop to a variable


code:
import os

def echo():
    print os.environ['var_name']

rawDescription: python global parameters file

description: Python global parameters file


code:
filenames = [
    '1.0.0.0.py',
    '0.0.0.0.py',
    '1.1.0.0.py'
]

print sorted(filenames, key=lambda f: map(int, f.split('.')[:-1]))

rawDescription: Sort list with multiple criteria in python


code:
[{'category': 'BOOK',
  'id': 4217L,
  'information': 'abcdefghijk',
  'name': 'test4',
  'sum': 1},
 {'category': 'BOOK',
  'id': 3937L,
  'information': 'abcdefghijk',
  'name': 'test',
  'sum': 9},
 {'category': 'BOOK',
  'id': 4218L,
  'information': 'abcdefghijk',
  'name': 'test5',
  'sum': 1},
 {'category': 'BOOK',
  'id': 4215L,
  'information': 'abcdefghijk',
  'name': 'test2',
  'sum': 2}]

rawDescription: Python Sum dictionary elements (better pythonic way)


code:
import csv
with open('file.csv') as f:
    csvreader = csv.reader(f)
    for number, what, are, these, entries, called in csvreader:
        print number

rawDescription: Getting the first element in string/list


code:
import json
with open(file.txt) as f:
    text = f.read()
    bookmarks = json.loads(text)
print bookmarks[bookmarks_bar][children][0][name]

rawDescription: Python: Access a dictionary that is located inside of a text file


code:
l = range(10)
for i in (0,1,2):
    print l[-(i + 3):len(l) - i]

rawDescription: Arithmetic in Python slicing, end behaviour


code:
for row in soup('table')[6].findAll('tr'):
    tds = row('td')
    if len(tds) < 2:
        continue
    print tds[0].string, tds[1].string

rawDescription: Python: html table content

description: Python: html table content


code:
list_of_goals = row.xpath('//span[@title="Goal"]')

if list_of_goals:
    list = str(list_of_goals)
    print remove_tags(list).encode('utf-8') + "GOAL" # <-- string

rawDescription: Xpath() method not returning results using Scrapy


code:
for location in latLongList[:sizeOfList]:
    print ' '.join(map(str, location[:-1]))

rawDescription: Print sublists in python

description: Print sublists in python


code:
from bs4 import BeautifulSoup

data = """<test><EXTRACT>extract text</EXTRACT></test>"""
soup = BeautifulSoup(data, 'html.parser')
test = soup.find('test')
print test.find("extract").text  # prints 'extract text'

rawDescription: extracting contents from a tag "extract" from Beautifulsoup

description: Extracting contents from a tag "extract" from Beautifulsoup


code:
def change(C, V, res=None):
    res = [] if res is None else res
    if len(V) == 0:
        return len(res), res
    maxx = max(V)
    V.remove(maxx)
    ans = C//maxx
    if ans == 0 and maxx < C :
        res += [maxx] * ans
        return len(res), res
    else:
        res += [maxx] * ans
        return  change(C % maxx, V, res)

print change(48,[1, 5, 10, 25, 50])
print change(30,[25, 10, 2, 3, 1])

rawDescription: Python Coin change SO CLOSE

description: Python Coin change SO CLOSE


code:
from itertools import starmap, islice, izip

a = [1, 2, 3]
b = [3, 4, 5]
w = [0.5, 1.5] # weights => a*0.5 + b*1.5

products = [m for m in starmap(lambda i,j:i*j, [y for x in zip(a,b) for y in zip(x,w)])]

sums = [sum(x) for x in izip(*[islice(products, i, None, 2) for i in range(2)])]

print sums # should be [5.0, 7.0, 9.0]

rawDescription: Adding two tuples elementwise

description: Adding two tuples elementwise


code:
list = [(1,2),(3,4)]
for a,b in list:
  print a, b

rawDescription: list of Directorys,files using Python Script

description: List of Directorys,files using Python Script


code:
import sys
import curses

curses.initscr()

if not curses.has_colors():
    curses.endwin()
    print "no colors"
    sys.exit()
else:
    curses.start_color()

curses.noecho()    # don't echo the keys on the screen
curses.cbreak()    # don't wait enter for input
curses.curs_set(0) # don't show cursor.

RED_TEXT = 1
curses.init_pair(RED_TEXT, curses.COLOR_RED, curses.COLOR_BLACK)

window = curses.newwin(20, 20, 0, 0)
window.box()
staticwin = curses.newwin(5, 10, 1, 1)
staticwin.box()

staticwin.addstr(1, 1, "test", curses.color_pair(RED_TEXT))

cur_x = 10
cur_y = 10
while True:
    window.addch(cur_y, cur_x, '@')
    window.refresh()
    staticwin.box()
    staticwin.refresh()
    inchar = window.getch()
    window.addch(cur_y, cur_x, ' ')
    # W,A,S,D used to move around the @
    if inchar == ord('w'):
        cur_y -= 1
    elif inchar == ord('a'):
        cur_x -= 1
    elif inchar == ord('d'):
        cur_x += 1
    elif inchar == ord('s'):
        cur_y += 1
    elif inchar == ord('q'):
        break
curses.endwin()

rawDescription: (python) How to create static text in curses


rawDescription: Efficient 1D linear regression for each element of 3D numpy array


code:
from jinja2 import Template

x = Template("""{%- for key, value in tree.iteritems() recursive %}
{{ '--' * (loop.depth-1) }}{{ key }}
{%- if value is mapping -%}/{{ loop(value.iteritems()) }}{%- endif -%}
{%- endfor %}
""")

tree = {'.': {
    'walk.py': None,
    'what.html': None,
    'misc': {},
    'orders': {
        'order1.html': None,
        'more': {
            'stuff.html': None
        }
    }
}}

print x.render(tree=tree)

rawDescription: Flask/Jinja2 - Iterating over nested dictionaries


code:
import random
import itertools as IT
# lst = random.sample(range(100), 30)
lst = range(30)
lst = sorted(lst)

col_size = int(raw_input('How many columns do you want?: '))
sorting = 'Vertical'

if sorting == 'Vertical':
    # vertically sorted
    n = len(lst)//col_size
    lst = iter(lst)
    columns = IT.izip_longest(*[lst]*n, fillvalue='')

    print '\nVertically Sorted:'

    print('\n'.join(
        [''.join(map('{:4}'.format, row))
         for row in IT.izip(*columns)]))

rawDescription: Zip together n number of lists

description: Zip together n number of lists


code:
import hashlib, string, base64

_INT_EFFICIENT_CHARS = string.letters + string.digits + string.punctuation
_L_INT_EFFICIENT_CHARS = len(_INT_EFFICIENT_CHARS)
# http://stackoverflow.com/questions/2267362/convert-integer-to-a-string-in-a-given-numeric-base-in-python
def int_efficient(x):
    rets=''
    while x>0:
        x,idx = divmod(x, _L_INT_EFFICIENT_CHARS)
        rets = _INT_EFFICIENT_CHARS[idx] + rets
    return rets

h = hashlib.md5("foo")
print h.hexdigest()

# Starting in Python 3.2, use int.from_bytes rather than converting to hex
# http://stackoverflow.com/a/9634417/2829764
i = int(h.hexdigest(), 16)
print int_efficient(i)

print base64.b64encode(h.digest())

rawDescription: Dense, human-readable representation of a string of bytes using Python


code:
for index, current_number in enumerate(np.array([0,0,1,1,1,0]).tolist()):
    print index, current_number

rawDescription: putting value of one array into a list in python

description: Putting value of one array into a list in python


rawDescription: Best way to separate data into 3 classes

description: Separate data into 3 classes


code:
import re

text = "Cello is a yellow parakeet who sings with Lillian. Toby is a clown who doesn't sing. Willy is a Wonka. Cello is a yellow Lillian."

for match in re.findall(r'(Cello(?:\W{1,80}\w{1,60}){0,9}\W{0,20}Lillian)', text, flags=re.I):
    print match

rawDescription: Python: Convert tuples from re.findall into string?


rawDescription: How to improve the speed of my selection process, python

description: Improve the speed of my selection process, python


code:
f = open(fileName, "r")
x = f.read()
x = re.sub(r'\n', '', x, re.S) #remove any newline returns
name = re.search('<name>(.*)</name>', x, re.S)
print "Attachment name (as recorded from GPS device): " + name.group(1)

x = re.sub(r'<(.*)<trkseg>', '', x, re.S)  #strip header
x = x.replace("</trkseg></trk></gpx>",""); #strip footer
x = x.replace("<trkpt","\n<trkpt"); #make the file in lines
x = re.sub(r'<speed>(.*?)</speed>', '', x, re.S) #strip speed
x = re.sub(r'<extensions>(.*?)</extensions>', '', x, re.S) # strip out extensions

rawDescription: successively take values between two sub-strings and temporarily store it before writing them in a different order withhin a python fileread

description: Successively take values between two sub-strings and temporarily store it before writing them in a different order withhin a python fileread


code:
import subprocess
cmd = subprocess.Popen('wget --version', shell=True, stdout=subprocess.PIPE)
for line in cmd.stdout:
    if "Wget" in line:
        print line

rawDescription: Parsing a stdout in Python

description: Parsing a stdout in Python


rawDescription: How to find cluster sizes in 2D numpy array?

description: Find cluster sizes in 2D numpy array


code:
f = lambda x: x.upper()
print f("hello")

f = str.upper
print f("hello")

rawDescription: Lambda expression replacement

description: Lambda expression replacement


code:
from xml.dom import minidom

xml = """
<root>
 <x>text</x>
</root>"""

dom = minidom.parseString(xml)
pi = dom.createProcessingInstruction('xml-stylesheet',
                                     'type="text/xsl" href="mystyle.xslt"')
root = dom.firstChild
dom.insertBefore(pi, root)
print dom.toprettyxml()

rawDescription: How to add an xml-stylesheet processing instruction node with Python 2.6 and minidom?

description: Add an xml-stylesheet processing instruction node with Python 2.6 and minidom


code:
result = client.last_received()
print result

rawDescription: Python SOAP document handling

description: Python SOAP document handling


code:
import csv

if SevenZFile.is_7zfile('testing.csv.7z'):
    sevenZfile = SevenZFile('testing.csv.7z')

    if 'testing.csv' not in sevenZfile:
        print 'testing.csv is not a member of testing.csv.7z'
    else:
        reader = csv.reader(sevenZfile.readlines('testing.csv'))
        for row in reader:
            print ', '.join(row)

rawDescription: How to read from a text file compressed with 7z in Python

description: Read from a text file compressed with 7z in Python


code:
us = ''.join(chr(ord(c)) for c in s).decode('utf-8')
print us

rawDescription: Working with unicode in python

description: Working with unicode in python


rawDescription: How to concatenate and output unicode text variables in Python

description: Concatenate and output unicode text variables in Python


code:
# Get now for EST
now = datetime.datetime.now(EST)
print now.strftime('%Y-%m-%d %H:%M:%S')

from pymongo import Connection
# Create a timezone aware connection
connection = Connection('localhost', 27017, tz_aware=True)

# Save your data
db = connection.test_database
db.stackoverflow.save({"Type": "reports", "patId": 'JOHNSONGARY', "lastUpdated": now})

doc = db.stackoverflow.find()[0]
print doc['lastUpdated'].astimezone(EST).strftime('%Y-%m-%d %H:%M:%S')

# Confirm they are the same
assert doc['lastUpdated'].astimezone(EST).strftime('%Y-%m-%d %H:%M:%S') == now.strftime('%Y-%m-%d %H:%M:%S')

rawDescription: Query mongodb datetime output to be for certain timezone


code:
import json

def serialize(data):
    _data = {}
    for k, v in data.items():
        if isinstance(v, int):
            _data[k] = hex(v)
        else:
            _data[k] = v
    return json.dumps(_data, indent=4)

if __name__ == "__main__":
    data = {"a":1, "b":2.0, "c":3}
    print serialize(data)

rawDescription: How to print integers as hex strings using json.dumps() in Python

description: Print integers as hex strings using json.dumps() in Python


code:
from sqlalchemy.dialects import postgresql

class Comment(Base):
    __tablename__ = 'comments'
    id = Column(Integer, primary_key=True)
    path = Column(ARRAY(Integer))

q = Comment.query.filter(Comment.path[1] == 11)
# This is just to demonstrate how compiled query would look like.
print q.statement.compile(dialect=postgresql.dialect())

rawDescription: How to use SQLAlchemy to select value at a position in a PostgreSQL ARRAY?

description: Use SQLAlchemy to select value at a position in a PostgreSQL ARRAY


code:
import re

# Define patterns we want to recognize.

c = r'(\S+)\s+'  # a word we want to capture
s = r'\S+\s+'  # a word we want to skip
mesg = r'(\S.*\S)\s+--Sev\s+'  # mesg to capture; terminated by string '--Sev'
w2 = r'(\S+\s+\S+)\s+'  # two words separated by some white space
w2semi = r'(\S+\s+\S+)\s*;\s+'  # two words terminated by a semicolon
tail = r'(.*\S)\s*;'

# Join together the above patterns to make one giant pattern that parses
# the input.
s_pat = ( r'^\s*' +
    c*2 + s*3 + c*1 + s*10 + c*2 + s*14 + c*1 + s*14 +
    mesg + w2 + w2semi*2 + tail +
    r'\s*$')

# Pre-compile the pattern for speed.
pat = re.compile(s_pat)

# Test string and the expected output result.
s_input = "83b14af0-949b-71e0-18d5-0ad781020000 40ba8352-8dd2-71dc-12b8-0ad781020000 1 -1407714483 20 COLG-GRA-617-RD1.oss 1 181895426 12 oss-ap-1.oss 0 0 48 0 0 0 1307845644 1307845647 0 2 12 0 0 0  0 0 12 0 0 0  0 0 1307845918 3 OpC 6 opcecm 9 SNMPTraps 8 IBB_COLG 4 ATM0 0  0  0  69 Cisco Agent Interface Up (linkUp Trap) on interface ATM0 --Sev Normal 372 Generic: 3; Specific: 0; Enterprise: .1.3.6.1.4.1.9.1.569;"
s_correct = "83b14af0-949b-71e0-18d5-0ad781020000|40ba8352-8dd2-71dc-12b8-0ad781020000|COLG-GRA-617-RD1.oss|1307845644|1307845647|1307845918|Cisco Agent Interface Up (linkUp Trap) on interface ATM0|Normal 372|Generic: 3|Specific: 0|Enterprise: .1.3.6.1.4.1.9.1.569"

# re.match() returns a "match group"
m = re.match(pat, s_input)
# m.groups() returns sequence of captured strings; join with '|'
s_output = '|'.join(m.groups())

# sanity check
if s_correct == s_output:
    print "excellent"
else:
    print "bogus"

# excellent.

rawDescription: Parsing a text log file to extract some fields of data from log messages


code:
html = '

'
soup = BeautifulSoup(html)

div = soup.find('div')
anchor = div.find('a')

data_hovercard = anchor['data-hovercard']

print data_hovercard
#/ajax/hovercard/user.php?id=671948073&extragetparams=%7B%22hc_location%22%3A%22friends_tab%22%7D

rawDescription: Select multiple values inside HTML tags with BeautifulSoup


code:
def ask():
    noun_1=raw_input("Enter a noun:")
    verb=raw_input("Enter a verb:")
    adjective=raw_input("Enter a adjective:")
    noun_2=raw_input("Enter another noun:")
    return noun_1,verb,adjective,noun_2

def main():
    noun_1,verb,adjective,noun_2 = ask()
    print noun_1,verb,adjective,noun_2

main()

rawDescription: In python I am trying to make a simple english sentence. Please help:


rawDescription: Python: Need a hint on reading blocks of data from a text file


code:
a = 'asd'
b = 'dsa'

if all([a,b]):
    print 'All True!!!!'
#All True!!!!

rawDescription: check None in multiples variables

description: Check None in multiples variables


code:
import numpy as np
import pulp
from itertools import product
threshold = 200
model = pulp.LpProblem('selection', pulp.LpMaximize)
similarity = np.array([[1., 0.08333333, 0.1, 0., 0., 0.0625],
                       [0.08333333, 1., 0.33333333,
                           0., 0.11111111, 0.07692308],
                       [0.1, 0.33333333, 1., 0.2, 0., 0.09090909],
                       [0., 0., 0.2, 1., 0., 0.],
                       [0., 0.11111111, 0., 0., 1., 0.27272727],
                       [0.0625, 0.07692308, 0.09090909, 0., 0.27272727, 1.]])
ingredients = ['var_%d' % i for i in range(6)]

ingredient_pairs = ['var_{}_{}'.format(
    ingredients.index(var[0]), ingredients.index(var[1]))
    for var in product(ingredients, ingredients)
    if ingredients.index(var[0]) > ingredients.index(var[1])]
# Flatten the similarity array
indices = np.triu_indices_from(similarity)
similarity = similarity[indices]

scores = np.random.randint(1, 3, size=len(ingredients))
costs = np.random.randint(20, 60, len(ingredients))
scores = dict(zip(ingredients, scores))
costs = dict(zip(ingredients, costs))
similarity = dict(zip(ingredient_pairs, similarity))
x = pulp.LpVariable.dict(
    'x_%s', ingredients, lowBound=0, upBound=1, cat=pulp.LpInteger)
y = pulp.LpVariable.dict(
    'y_%s', ingredient_pairs, lowBound=0, upBound=1, cat=pulp.LpInteger)
model += sum([scores[i] * x[i] for i in ingredients]) + sum([
    similarity[i] * y[i] for i in ingredient_pairs])
model += sum([costs[i] * x[i] for i in ingredients]) <= threshold
for pair in ingredient_pairs:
    indexes = pair.split('_')[1:]
    for index in indexes:
        # y_{ij} <= x_i and y_{ij} <= x_j Q
        model += y[pair] <= x['var_{}'.format(index)]
    # y_{ij} >= x_i + x_j - 1
    model += y[pair] >= sum(x['var_{}'.format(i)] for i in indexes) - 1
solver = pulp.solvers.PULP_CBC_CMD()
model.solve(solver)
model.writeLP('similarity.lp')
print 'Objective: {}'.format(pulp.value(model.objective))
for v in model.variables():
    if v.varValue > 10e-4:
        print v.name, v.varValue

rawDescription: Python Pulp Integer Linear Program with dynamic constraint


code:
with open('/tmp/test.txt','r') as f:
    data=''.join(f.readlines())

print data[data.index('&'):data.index('!')+1]

rawDescription: Extract string between characters from a txt file in python


code:
def repl(match):
    return eval(match.group(0)[3:-3])

print re.sub(r'<<<\w+>>>', repl, usage)

rawDescription: Python regex, pulling patterns out of a match and using them as input back into the match


code:
from random import randint

class RandomFun(object):
    def __str__(self):
        return str(randomfun())

def randomfun():
    return randint(1, 100)

an_int = RandomFun()
print an_int
print an_int

rawDescription: Variable value is determined by function call


code:
print 'Status: 403 Forbidden'
print

rawDescription: Python CGI returning an http status code, such as 403?


code:
from AppKit import NSScreen

print NSScreen.mainScreen().frame()

rawDescription: How to get the desktop resolution in Mac via Python?

description: Get the desktop resolution in Mac via Python


code:
for n in range(1, N):
    coprimes = set(range(1, N))  # start with all the numbers in the range
    for f in factors[n]:         # eliminate numbers that share a prime factor
        coprimes -= factored[f]
    print "%d is coprime with %r others" % (n, len(coprimes))

rawDescription: Optimizing list comprehension to find pairs of co-prime numbers


code:
for Temperature in c.sensor():
    if "/intelcpu/0/temperature/1" in Temperature.identifier:
        print Temperature.identifier
        print Temperature.value

rawDescription: How to access wmi in python?

description: Access wmi in python


code:
def serverlist(count):
    with open("file.txt", "r") as f:
        for line in f.readlines():
            if "Server[" + str(count) + "]" in line:
                return line.split()[1] # split on spaces
    return False

print serverlist(30)
# ip.ip.ip.ip

print serverlist(";-)")
# False

rawDescription: Slice variable from specified letter to specified letter in line that varies in length


code:
from itertools import product

elements = []
for letter in list1:
    elements.append([''.join(c) for c in product(letter, list2)])
    for combo in product(*elements):
        print combo

rawDescription: lists permutation in python

description: Lists permutation in python


code:
#!/usr/bin/python

import cProfile

verbose=False
def vprint(msg):
    if verbose:
        print msg

def vprint2(msg):
    pass

def testit(fcn):
    for i in xrange(1000000):
        fcn(i)

def testit2():
    testit(vprint2)

def testit1():
    testit(vprint)

if __name__ == '__main__':
    cProfile.run('testit1()')
    cProfile.run('testit2()')

rawDescription: Python: Most efficient way to toggle "verbose" output?


code: print '\n'.join(str(i) for i in [3, 2, 6, 1])

rawDescription: how to return the looped values in a method in python/django that no using array

description: Return the looped values in a method in python/django that no using array


code:
import csv
from itertools import izip
with open('file_.csv','rb') as f :
    csvreader=csv.reader(f,delimiter='\t')
    keys=next(csvreader)
    a=izip(*csvreader)
    d=dict(zip(keys,a))

print d
print d['Salary']

rawDescription: how to split .csv data in python?

description: Split .csv data in python


code:
t = [
    ["a1","a2"],
    ["a1","a3"],
    ["a1","ax"],
    ["ax","b1"],
    ["ax","bx"],
    ["bx","b3"]
]

import itertools
def find_matches(t,key):
    return list(itertools.chain(*[[v] if not v.endswith("x") else find_matches(t,v) for k,v in t if k == key]))

print find_matches(t,"a1")

rawDescription: Python - Iterative self cross referencing


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

Hello world!

This is all

Set to go!

''')
soup
for textelement in soup.find_all(text=True):
    textelement.extract()

print soup.prettify()

rawDescription: Filtering all inner text from a HTML document


code:
for i in range(len(items)):
     print i

rawDescription: Python - for with counter

description: Python - for with counter


code:
import itertools
data = ['0157', '0234', '0467', '0164', '0363', '0341', '0179']
output = []
for key, group in itertools.groupby(sorted(data), lambda x: x[:2]):
    output.append(list(group))
print output

rawDescription: Sorting array into table by first two characters


code:
i = 0
some_iterator = iter(range(600))
while i < 50:
    print some_iterator.next()
    i += 1

rawDescription: How do I return (or print) a maximum number of hits from a statement?

description: Return (or print) a maximum number of hits from a statement


code:
import string
finder3.apply_ngram_filter(lambda w1, w2, w3: w2 == u').' )
print finder3.nbest(trigram_measures.pmi, 10)

rawDescription: finder.apply_ngram_filter in nltk.collocations to classify some ngrams

description: Finder.apply_ngram_filter in nltk.collocations to classify some ngrams


code:
myObject = MyClass('test')
print myObject

rawDescription: Mimicking functionality of built-in types


rawDescription: how to skip a value while printing from dict, if the value is blank

description: Skip a value while printing from dict, if the value is blank


code:
result = p4.run_fstat("//depot/branch1/dir1/file1#4")
print result[0]['headChange']

rawDescription: how to get all change numbers from sync - p4python

description: Get all change numbers from sync - p4python


code:
for line in lines:
    if not comment_line_regex.match(line):
            print reg.findall(line)

rawDescription: Regular expression to parse sfv-format files


code:
import requests

urls = '''http://url.com/r/GFDGDFG
http://url.com/r/A32512F
http://url.com/r/AWDRE25
http://url.com/r/AGBFD35
http://url.com/r/NFG4622'''

for x in urls.splitlines():
    r = requests.get(x)
    print x, '=>', r.url

rawDescription: Crawl URL file to get final destination URL after redirects?


code:
import re
p = re.compile(ur'(.*\.com)\/[^\/]+(\/prod[^\_]*\/p\.prod).*', re.DOTALL | re.IGNORECASE)
test_str = u"http://www.bergdorfgoodman.com/Ippolita-18k-Gold-Rock-Candy-Mini-Single-Square-Pendant-Necklace/prod108010011/p.prod#.U75MVqY-PtS"
subst = ur"\1\2"
result = re.sub(p, subst, test_str)

rawDescription: Perl specific transforms in python

description: Perl specific transforms in python


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

def plot_my_time_based_histogram():
    #Note - changed the 24 values for 0
    time_new=[9, 23, 19, 9, 1, 2, 19, 5, 4, 20, 23, 10, 20, 5, 21, 17, 4, 13, 8, 13, 6, 19, 9, 14, 9, 10, 23, 19, 23, 20, 19, 6, 5, 0, 20, 19, 15, 14, 19, 14, 15, 21]
    fig, ax = plt.subplots()
    hour_list = time_new
    print hour_list
    numbers=[x for x in xrange(0,24)]
    labels=map(lambda x: str(x), numbers)
    plt.xticks(numbers, labels)
    #Make limit slightly lower to accommodate width of 0:00 bar
    plt.xlim(-0.5,24)
    plt.hist(hour_list,bins=np.arange(25)-0.5)

    # Further to comments, OP wants arbitrary labels too.
    labels=[str(t)+':00' for t in range(24)]
    ax.set_xticklabels(labels)
    plt.show()

plot_my_time_based_histogram()

rawDescription: Put value at centre of bins for histogram


code:
def power(r, x):
    return x(r)

def main():
    funcs = [square, cube]
    for r in range(5):
        value = map(lambda x, r=r: power(r, x), funcs)
        print value

rawDescription: map with lambda vs map with function - how to pass more than one variable to function?

description: Map with lambda vs map with function - how to pass more than one variable to function


code:
import pandas
import patsy

dataFrame = pandas.io.parsers.read_csv("salary2.txt")
#salary2.txt is a re-formatted data set from the textbook
#Introductory Econometrics: A Modern Approach
#by Jeffrey Wooldridge
y,X = patsy.dmatrices("sl ~ 1+sx+rk+yr+dg+yd",dataFrame)
#X.design_info provides the meta data behind the X columns
print X.design_info

rawDescription: Python Pandas: how to turn a DataFrame with "factors" into a design matrix for linear regression?


code:
from lxml import etree

data = '''<everything>
<m>Some text before <r/></m>
<m><r/> and some text after.</m>
<m><r/></m>
<m>Text before <r/> and after</m>
<m><b/> Text after a sibling <r/> Text before a sibling<b/></m>
</everything>
'''

f = etree.fromstring(data)
for r in f.xpath('//r'):
  r.tail = 'DELETED' + r.tail if r.tail else 'DELETED'

etree.strip_elements(f,'r',with_tail=False)

print etree.tostring(f,pretty_print=True)

rawDescription: How can one replace an element with text in lxml?


code:
class Node(object):

    def __init__(self, data, next=None):

        self.data = data
        self.next = next

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

class LinkedList(object):

    def __init__(self):

        self.head = None
        self.size = 0

    def append(self, data):
            if not self.head:
                n = Node(data)
                self.head = n
                return
            else:
                n = self.head

                while n.next != None:
                    n = n.next

                new_node = Node(data)
                n.next = new_node;
                return

    def isEmpty(self):
        return not self.head

    def printList(self):
        n = self.head

        while n:
            print str(n)
            n = n.next

ll = LinkedList()
elems = [1, 2, 3, 54, 6]
for elem in elems:
    ll.append(elem)

ll.printList()

rawDescription: Implementing a Linked List in Python

description: Implementing a Linked List in Python


code:
txt='''\
Call me on my mobile anytime: 555-666-1212
The office is best at 555-222-3333
Dont ever call me at 555-666-2345 '''

import re

print re.findall(r'(?:(mobile|office).{0,15}(\+?[2-9]\d{2}\)?[ -]?\d{3}[ -]?\d{4}))', txt)

rawDescription: Regex match following substring in string python


code:
import argparse

def my_stop(args):
    if args.gracefully:
        print "Let's try to stop..."
    else:
        print 'Stop, now!'

parser = argparse.ArgumentParser(prog='mydaemon')

graceful = argparse.ArgumentParser(add_help=False)
graceful.add_argument('-g', '--gracefully', action='store_true', help='tries to terminate the process gracefully')
sp = parser.add_subparsers()
sp_start = sp.add_parser('start', help='Starts %(prog)s daemon')
sp_stop = sp.add_parser('stop', parents=[graceful],
                    description='Stops the daemon if it is currently running.',
                    help='Stops %(prog)s daemon')
sp_restart = sp.add_parser('restart', parents=[graceful], help='Restarts %(prog)s daemon')

sp_stop.set_defaults(func=my_stop)

args = parser.parse_args()
args.func(args)

rawDescription: How to make python argparse mutually exclusive group arguments without prefix?

description: Make python argparse mutually exclusive group arguments without prefix


code:
import subprocess, datetime, os

start_year, start_month = "2001", "January"

current_month = datetime.date.today().replace(day=1)
possible_month = datetime.datetime.strptime('%s %s' % (start_month, start_year), '%B %Y').date()
while possible_month <= current_month:
    csv_filename = possible_month.strftime('%B %Y') + '.csv'
    month = possible_month.strftime('%B %Y').split(" ")[0]
    year = possible_month.strftime('%B %Y').split(" ")[1]
    if os.path.exists("folder/" + csv_filename):
        print csv_filename
    possible_month = (possible_month + datetime.timedelta(days=31)).replace(day=1)

rawDescription: Load all csv/txt files from one directory and merge them via python


code:
from itertools import groupby, islice

print any(sum(1 for _ in islice(g, 3)) == 3 for k, g in groupby(myList) if k)

rawDescription: find if element appears n times in a row in python list

description: Find if element appears n times in a row in python list


code:
filename = "fifo.tmp"

# Block until writer finishes...
with open(filename, 'r') as f:
    data = f.read()

# Split data into an array
array = [int(x) for x in data.split()]

print array

rawDescription: Send data from c program to python program using pipe?


code:
def runMyODE(yn,tvec,allpara):
    return allpara['para'],transpose(odeint(myODE, yn, tvec, args=(allpara,)))

asyncResults = []

def saveResult(result):
    asyncResults.append((result[0], result[1][2,-1]))

tic = time.time()
for combi in range(numComb):
    pool.apply_async(runMyODE, args=(INIT[combi,:],tval,{'para': PARA[combi,:]}), callback=saveResult)
pool.close()
pool.join()
toc = time.time()

print 'Using apply_async took ', toc-tic, 'seconds!'

for res in asyncResults:
    print res[0], res[1]

rawDescription: Python's multiprocessing: speed up a for-loop for several sets of parameters, "apply" vs. "apply_async"


code:
import logging
from mock import Mock, patch

my_mock = Mock()
my_mock.id = 'i-12341234'

with patch('logging.PlaceHolder', new=my_mock) as mockobj:
    mockobj.return_value = mockobj # <-- added this line.
    print mockobj.id
    newobj = logging.PlaceHolder(None)
    print newobj.id

rawDescription: Python mock library not returning correct mock object for 'patch'


code:
import re
from collections import defaultdict

band_dict = defaultdict(list)
pattern   = re.compile(r"\d+\. (?P<band>.+?) -\s?(?P<album>.+?) \((?P<year>\d+)\)")
with open("musiclist") as f:
    for line in f:
        match = pattern.match(line)
        if match:
            groupdict = match.groupdict()
            band_dict[groupdict['band']].append((groupdict['album'], groupdict['year']))
        else:
            print "Error, no match for line %s" % line

for band in band_dict:
    print band
    for album, year in band_dict[band]:
        print "\t%s: %s" % (album, year)

rawDescription: How to convert a file into a dictionary in python

description: Convert a file into a dictionary in python


code:
for attr in whitelist:
    print "{0}: {1}".format(attr, getattr(alarm, attr, "<Not defined>"))

rawDescription: get function names from a list python

description: Get function names from a list python


code:
from numpy import genfromtxt
from itertools import islice
from time import localtime, time

toKeep = 20
with open('temperature_logging') as fin:
    start = genfromtxt(islice(fin,1), dtype=None, usecols=(0,1))
    hf, mf = map(int, start.tolist()[0].split(':'))
    ti = localtime(time())
    hn, mn = ti.tm_hour, ti.tm_min
    print 'File start: {:02d}:{:02d},  Time Now: {:02d}:{:02d}'.format(hf, mf, hn, mn)
    minutesOld = (hn-hf)*60 + mn-mf
    if minutesOld < 0: minutesOld += 24*60
    data = genfromtxt(fin, dtype=None, usecols=(0,1), skip_header = minutesOld - toKeep)

print data

rawDescription: How to read a file from the bottom with numpy.genfromtxt in python

description: Read a file from the bottom with numpy.genfromtxt in python


code:
r = resource.objects.get(id=701)
langs = r.languages.split(';')
for l in language:
    print l

rawDescription: How do I store multiple copies of the same field in Django?

description: Store multiple copies of the same field in Django


code:
from pyparsing import Word,hexnums,Suppress,OneOrMore

twoDigitHex = Word(hexnums,exact=2)
VERT = Suppress('|')

pattern = VERT + OneOrMore(twoDigitHex) + VERT

# attach parse action to prefix each 2-digit hex with 'x' and join all together
pattern.setParseAction(lambda t: ''.join('x'+tt for tt in t))

# take sample code, and use transformString to apply conversion
sample = "blahblah|0A 4D 5E 43|adfsdasd|92| sgagrewas|12 5E|"
print pattern.transformString(sample)

rawDescription: Need help designing a regex or pyparsing approach to modify all words enclosed within pipes


code:
import json

class Custom(object):
    pass

class CustomEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, Custom):
            return 'TASTY'
        return CustomEncoder(self, o)

print json.dumps( Custom(), cls=CustomEncoder )
print json.dumps( [1, [2,'three'], Custom()], cls=CustomEncoder )

rawDescription: custom object list json serialize in python

description: Custom object list json serialize in python


code: print repr(inp), repr(s)

rawDescription: Test if user has entered a word equal to a word in a text file


code:
def cucumber_veg_2016(cm):
    return float(cm) / 2.54

def podium_place():
    names = OrderedDict([('Competitor Number', int),
                         ('Competitor Name', str),
                         ('Cucumber', cucumber_veg_2016),
                         ('Carrot', float),
                         ('Runner Bean', float)])
    data = []
    with open('veggies_2016.txt') as fobj:
        while True:
            item = OrderedDict()
            try:
                for name, func in names.items():
                    item[name] = func(next(fobj).strip())
                data.append(item)
            except StopIteration:
                break

    sorted_data = sorted(data, key=itemgetter('Cucumber'), reverse=True)
    for entry in sorted_data[:10]:
        for key, value in entry.items():
            print key, value
        print

menu()

rawDescription: Finding the top 10 and converting it from centimeters to inches - Python


code:
conn = imaplib.IMAP4_SSL(imap_server)

try:
    (retcode, capabilities) = conn.login(imap_user, imap_password)
except:
    print sys.exc_info()[1]
    sys.exit(1)

conn.select(readonly=1) # Select inbox or default namespace
(retcode, messages) = conn.search(None, '(UNSEEN)')
if retcode == 'OK':
    for num in messages[0].split(' '):
        print 'Processing :', message
        typ, data = conn.fetch(num,'(RFC822)')
        msg = email.message_from_string(data[0][1])
        typ, data = conn.store(num,'-FLAGS','\\Seen')
        if ret == 'OK':
            print data,'\n',30*'-'
            print msg

conn.close()

rawDescription: Get only NEW Emails imaplib and python

description: Get only NEW Emails imaplib and python


code: print '%-2s'%i, ''.join('%-2s ' % v[1] for v in self.gameBoard.Matrix[i] if v[0] == 'S')

rawDescription: Printing in python with if else conditions and correct spacing


code:
a=ast.parse("i=1")
b=ast.parse("j=2")
a.body += b.body
exec compile(a, "<string>", "exec")
print i
print j

rawDescription: Python AST - merging two ASTs

description: Python AST - merging two ASTs


code:
from xml.dom.pulldom import CHARACTERS, START_ELEMENT, parse

doc = parse("bible.xml")
text_expected = False
for event, node in doc:
    print event, node
    if text_expected:
        text_expected = False
        if event != CHARACTERS:
            # strange .. there should be some
            continue
        print node.data
    else:
        text_expected = (event == START_ELEMENT) and (node.localName == "b")

rawDescription: Python XML Pull Parser

description: Python XML Pull Parser


rawDescription: Working with big data in python and numpy, not enough ram, how to save partial results on disc?


code:
def magicWord(count):
    answer = raw_input("What is the password? ")
    if answer == 'lucas':
        print 'ACESS GRANTED'
    else:
        count += 1
        if count > 2:
            print "na na na you didn\'t say the magic word. "
            return
        else:
            magicWord(count)

rawDescription: How do you make use of a counter with recursion in Python?

description: Make use of a counter with recursion in Python


code:
if ans.haslayer(IP):
    print "Packet has the layer IP"

rawDescription: Getting variables from Scapy using Python


rawDescription: Repeat a scipy csr sparse matrix along axis 0


code:
class MyThread(Thread):
    def __init__(self, event):
        Thread.__init__(self)
        self.stopped = event

    def run(self):
        while not self.stopped.wait(0.5):
            print "my thread"
            # call a function

rawDescription: Python threading.timer - repeat function every 'n' seconds


code:
import re

text = 'hello this #word1# a it #word2# thanks!'
print re.sub('#(\w+)#', lambda m:m.group(1).title(), text)

rawDescription: Replacing words tagged by #word# within a string


code:
lists = {}
for n in nodes:
    b = lists.setdefault(n.id, [])
    lists.setdefault(n.parentid, []).extend([n.name, b])
print lists[0]

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


code:
first_col = np.vstack( (np.array([[1] * data1.shape[0]]).T, np.array([[2] * data2.shape[0]]).T) )
stack = np.vstack( (data1, data2) )
data = np.hstack( (first_col, stack) )
print data

rawDescription: How can I load a csv file and store its contents into an (numpy) array in python?

description: Load a csv file and store its contents into an (numpy) array in python


code:
count = 0
for ip in ips:
    for pwd in open(pass_file, "r"):
        try:
            ssh.connect(ip, port, username=UserName, password=pwd)
        except paramiko.AuthenticationException:
            print '[-] %s:%s fail!' % (UserName, pwd)
        else:
            print '[!] %s:%s is CORRECT for IP %s!' % (UserName, pwd, ip)
            count += 1
            if count == 5:
                return

rawDescription: While loop using text file, only uses last line - Python 2.x


code:
import psutil

for p in psutil.get_process_list():
    if 'python' in p.name:
        print p.cmdline

rawDescription: How can you get a list of running Python scripts?

description: Get a list of running Python scripts


code:
percentages = one_percentage(df)
column_name = 'any_column_name'
print percentages[column_name]

rawDescription: Python Import data dictionary and pattern


code:
from struct import unpack
b = "\x00\x00\x00\x05\x00\x00\x00\x2a"

x, y = unpack('!ii', b)
print x
print y

rawDescription: Python buffer decode

description: Python buffer decode


code:
import multiprocessing

def func_a(q):
    q.put(3)
    q.put(2)
    q.put(1)

def func_b(q):
    v = q.get()
    print 'func_b prints:', v
    v = q.get()
    print 'func_b prints:', v
    v = q.get()
    print 'func_b prints:', v

if __name__ == '__main__':
    q = multiprocessing.Queue()
    p1 = multiprocessing.Process(target = func_a, args = (q,))
    p1.start()
    p2 = multiprocessing.Process(target = func_b, args = (q,))
    p2.start()
    p1.join()
    p2.join()

rawDescription: Functions to be run in parallel with common list to be used in both


code:
import pandas as pd

df = pd.read_json('file.json')
print df.head()

rawDescription: Parsing json files

description: Parsing json files


code:
#!/usr/bin/env python

textFile =open("datafile.txt", "r")

# construct list of non-empty lines
# strip trailing newline and split on ','
lines = [l for l in (m.rstrip('\n').split(',')
                     for m in textFile.readlines()) if len(l) > 1]

# make tuple from names
names = tuple(a for a,b in lines if a.isalpha())

# prepend names to list of tuples built from each line
# alphabetical entries are substituted for 0s
value = [names] + [(0,b) if a.isalpha() else (a,b) for a,b in lines]

print value

rawDescription: Print if origin in tuple is [0] (python)


code:
import re
from bs4 import BeautifulSoup

page = """

1

2

3

4

5

6

7
"""

soup = BeautifulSoup(page)
rows = soup.find_all('tr', {'class': re.compile('class1.*')})

for row in rows:
    cls = row.attrs.get("class")
    if not ("class2" in cls or "class3" in cls):
        print row

rawDescription: Selecting specific <tr> tags with BeautifulSoup


code:
with open('input1', 'r') as classified_data:
    for row in classified_data:
        a, b = row.strip().split()
        print a, b, edges.get((a, b), edges.get((b, a)))

rawDescription: Append or create and new Dictionary

description: Append or create and new Dictionary


code:
import itertools

"""Finds all non-negative integer sequences whose sum equals `total`, and who have `size` elements."""
def possible_sums(total, size):
    if total == 0:
        yield [0]*size
        return
    if size == 1:
        yield [total]
        return
    for i in range(total+1):
        left = [i]
        for right in possible_sums(total-i, size-1):
            yield left + right

"""
combines two lists a and b in order like:
[a[0], b[0], a[1], b[1]...]

"""
def interleave(a,b):
    result = []
    for pair in itertools.izip_longest(a,b):
        for item in pair:
            if item is not None:
                result.append(item)
    return result

"""flattens a list of lists into a one dimensional list"""
def flatten(seq):
    return [x for item in seq for x in item]

def permutations(tup, limit):
    one_segments = [[1]*size for size in tup]
    for i in range(len(tup)-1):
        one_segments[i].append(0)
    remaining_zeroes = limit - sum(tup) - len(tup) + 1
    assert remaining_zeroes >= 0, "not enough room to separate ranges!"
    for gap_sizes in possible_sums(remaining_zeroes, len(tup)+1):
        zero_segments = [[0]*size for size in gap_sizes]
        yield flatten(interleave(zero_segments, one_segments))

for seq in permutations((4, 3), 10):
    print seq

rawDescription: fill in list in multiple steps

description: Fill in list in multiple steps


code:
import mechanize
from bs4 import BeautifulSoup
import texttable

med = raw_input("Enter the drugname: ")
br = mechanize.Browser()
br.set_handle_robots(False)
res = br.open("http://www.medindia.net/drug-price/")
br.select_form("frmdruginfo_search")
br.form['druginfosearch'] = med
br.submit()

soup = BeautifulSoup(br.response().read())

tabl = soup.find('table', {'class': 'content-table'})
table = texttable.Texttable()
for tr in tabl.find_all('tr'):
    table.add_row([td.text.strip() for td in tr.find_all('td')])

print table.draw()

rawDescription: parsing html table using beautiful soup

description: Parsing html table using beautiful soup


code:
from StringIO import StringIO
import requests

csv_data = "1, 800 Wilshire Blvd, Los Angeles, CA, 90017"
buffer = StringIO()
buffer.write(csv_data)
buffer.seek(0)

url = "http://geocoding.geo.census.gov/geocoder/locations/addressbatch"
data = {'benchmark': 'Public_AR_Census2010'}
files = {'addressFile': buffer}

response = requests.post(url, data=data, files=files)
print response.content

rawDescription: Mimic curl in python

description: Mimic curl in python


code:
def some_function():
    instance = module.Foo()
    print instance.labels
    return instance.method()

rawDescription: Python mock class instance variable

description: Python mock class instance variable


code:
class UserProfileSerializer(ModelSerializer):
    user = UserSerializer(required=True, many=False)
    games = UserGameProfileSerializer(required=False, many=True)

    class Meta:
        model = UserProfile
        fields = ('premium', 'user', 'games')
        read_only_fields = ('premium', )

    def update(self, instance, validated_data):
        user_data = validated_data.pop('user')
        game_data = validated_data.pop('games')
        username = self.data['user']['username']
        user = User.objects.get(username=username)
        print user
        user_serializer = UserSerializer(data=user_data)
        if user_serializer.is_valid():
            user_serializer.update(user, user_data)
        instance.save()
        return instance

rawDescription: Update user profile and user in one request


code: print aoaoat

rawDescription: Soap Client using Suds

description: Soap Client using Suds


code:
class P(object):
    var = "original"

class PChild(P):
    def func(self):
        if self.var != super(PChild, self).var:
            print "DO A"
        else:
            print "DO B"

class C1(PChild):
    var = "changed"

class C2(PChild):
    pass

c1 = C1()
c2 = C2()

c1.func()
c2.func()

rawDescription: Python method be aware the self.variable changed


code:
C = np.array(
[[9, 2],
[1,6],
[5, 1],
[2, 5],
[3, 2],])

def index_by_first_column_entry(M, keys):
    colkeys = M[:,0]
    sorter = np.argsort(colkeys)
    index = np.searchsorted(colkeys, keys, sorter = sorter)
    return M[sorter[index]]

print index_by_first_column_entry(C, I)

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

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


code:
import bisect

data = [1, 3, 5, 6, 8, 9, 11, 13, 17]
for val in range(19):
    pos = bisect.bisect_right(data, val)
    print val, '->', data[max(0, pos-3):pos]

rawDescription: slicing a list based on values

description: Slicing a list based on values


code:
import re
url_pattern = re.compile(r'.*/(?:www.)?(\w+)')
def get_username_from_url(url):
    match = re.match(url_pattern, url)
    if match:
        return match.group(1)

easy_url = "http://www.httpwwwweirdusername.com/"
hard_url = "http://httpwwwweirdusername.blogger.com/"

print get_username_from_url(easy_url)
print get_username_from_url(hard_url)

rawDescription: How to manipulate a URL string in order to extract a single piece?

description: Manipulate a URL string in order to extract a single piece


code:
ranges = []
for key, group in groupby(enumerate(data), lambda (index, item): index - item):
    group = map(itemgetter(1), group)
    if len(group) > 1:
        ranges.append(xrange(group[0], group[-1]))
    else:
        ranges.append(group[0])

rawDescription: Identify groups of continuous numbers in a list


code:
csv = '''"13:02",10
"13:03",30
"13:04",15
"13:05",12
"13:06",3'''

rates = '''Rate limit: 5 at Thu Jun 12 13:02:00 PDT 2014 (Total missed: 5)
Rate limit: 10 at Thu Jun 12 13:02:01 PDT 2014 (Total missed: 15)
Rate limit: 17 at Thu Jun 12 13:02:06 PDT 2014 (Total missed: 32)
Rate limit: 10 at Thu Jun 12 13:05:50 PDT 2014 (Total missed: 42)
Rate limit: 14 at Thu Jun 12 13:05:53 PDT 2014 (Total missed: 56)
Rate limit: 84 at Thu Jun 12 13:05:21 PDT 2014 (Total missed: 140)
Rate limit: 2 at Thu Jun 12 13:06:30 PDT 2014 (Total missed: 142)
Rate limit: 5 at Thu Jun 12 13:06:34 PDT 2014 (Total missed: 147)'''

# --- example code ---

import re

all_times =  {}

# change csv into dict

for x in csv.splitlines():
    time, value  = x.split(',')
    all_times[time] = int(value)

# print dict

print '--- old ---'
for k,v in all_times.items():
    print k, v

# add rates to dict

for x in rates.splitlines():
    value, time = re.findall('Rate limit: (\d+) .* (\d+:\d+):', x)[0]
    all_times['"%s"' % time] += int(value)

# print dict

print '--- new ---'
for k,v in all_times.items():
    print k, v

rawDescription: Python – sum up/sort range of numbers from file


code:
def split():
  set1 = set(open("a.txt").read().split())
  set2 = set(open("b.txt").read().split())
  lst = filter(lambda x: x in set1, set2)

def generator():

  whitespace = {' ','\n'}
  def word_generator(file_object):
    str_list = []
    for char in file_object:
      if char not in whitespace:
        str_list.append(char)
      else:
        yield "".join(str_list)
        del str_list[:]
  with open("a.txt") as f1, open("b.txt") as f2:
    set1 = set(word_generator(f1.read()))
    set2 = set(word_generator(f2.read()))
    lst = filter(lambda x: x in set1, set2)

if __name__ == "__main__":
  from timeit import timeit
  print timeit("split()", setup="from __main__ import split", number=10000)
  print timeit("generator()", setup="from __main__ import generator", number=10000)

rawDescription: Search between two files in Python

description: Search between two files in Python


code: print df.pivot_table(index='Hr', columns='C', values='A',  aggfunc=len, fill_value=0)

rawDescription: Creating 2d Matrix from dataframe entries


code:
from bs4 import BeautifulSoup

html = """

Additional Info

“R”Web#: 215904

SKU: B7958C02

UPC/EAN/ISBN: 092317112958

Manufacturer #: PAW11295

Product Weight:2.2 pounds

Product Dimensions (in inches):12.7 x 10.1 x 5.4

"""

soup = BeautifulSoup(html)
div = soup.find('div', {'id':'AddnInfo'})
p = div.find_all('p')[4]
label = p.find('label')
manufacturer = label.text
label.extract()
id = p.get_text(strip=True)
print manufacturer
print id

rawDescription: python beautifulsoup extract text

description: Python beautifulsoup extract text


code:
from pyquery import PyQuery

html = """

            Book Details

                Publisher
                HARPER COLLINS INDIA

                ISBN-13
                9789350291924

"""

doc = PyQuery(html)

for td in doc("table.fk-specs-type2").find("td.specs-key"):
    print td.text, td.getnext().text

rawDescription: How to get text content of multiple <td> tags inside a table using PyQuery?

description: Get text content of multiple <td> tags inside a table using PyQuery


rawDescription: Python condition base print color string


code:
def checkCommonNodes(id, rs):
    id_key, id_value = id.split('_')
    for r in rs:
        try:
            if r[id_key] == id_value:
                print "".join('{}_{}'.format(k,v) for k,v in r.iteritems())
        except KeyError:
            continue

rawDescription: Python getting a string (key + value) from Python Dictionary


rawDescription: Distance Calculation Not Working

description: Distance Calculation Not Working


code:
import datetime

def addSecs(tm, secs):
    fulldate = datetime.datetime(100, 1, 1, tm.hour, tm.minute, tm.second)
    fulldate = fulldate + datetime.timedelta(seconds=secs)
    return fulldate.time()

a = datetime.datetime.now().time()
b = addSecs(a, 300)
print a
print b

rawDescription: What is the standard way to add N seconds to datetime.time in Python?


code:
import sys

for fn in sys.argv[1:]:

    with open(fn,'r') as file:

        lines = file.readlines()

        # creates a list or pairs (21st char, line) within a list
        lines = [ [line[21:22], line.strip() ] for line in lines ]

        # sorts lines based on key (21st char)
        lines.sort()

        # brings back list of lines to its original state,
        # but the order is not reverted since it is already sorted
        lines = [ line[1] for line in lines ]

        prev = 0
        for line in lines:
            if line[21:22] != prev:
                # new line separator for each group
                print ''
            print line
            prev = line[21:22]

rawDescription: file separation on the basis of matching character

description: File separation on the basis of matching character


code:
class MyView(View):
    def get(self, request, id):
        print 'requester is {}'.format(self.requester)

rawDescription: daisy-chaining Python/Django custom decorators

description: Daisy-chaining Python/Django custom decorators


code:
import requests

url = 'http://storeland.ru/user/login'
api_url = 'https://api.aminer.org/api/search/people'
with requests.Session() as session:
    session.get(url)

    params = {
        'query': 'jie+tang',
        'size': '20',
        'sort': 'relevance',
        # 'offset': 20  # set offset for pagination
    }
    response = session.get(api_url, params=params)
    for item in response.json()['result']:
        print item['name']

rawDescription: Timeout for webdriver.PhantomJS in python?


code:
for r in db.hello.find(fields=['text', 'timestamp']):
     print '"%s","%s"' % (r['text'], r['timestamp'].strftime('%H:%M:%S'))

rawDescription: Saving entries of Mongo documents into CSV + Formatting ISODate


code:
class C(object):
    def __init__(self, lst):
        self.lst = lst
    def __getitem__(self, key):
        return self.lst[key]
def f(a, b, c):
    print "Arguments: ", a, b, c
c = C([1, 2, 3])
f(*c)

rawDescription: What does a class need to implement in order to be used as an argument tuple?


code:
import re

fp = open("csv.txt", "r")
while True:
    line = fp.readline()
    if '' == line:
            break
    line    = line.strip()
    fields  = re.split("\s+", fp.readline().strip())
    print "\"%s\",%s,%s" % ( line, fields[0], fields[1] )
fp.close()

rawDescription: Python - Parsing Conundrum

description: Python - Parsing Conundrum


code:
m = mannwhitneyu(xx, yy, use_continuity=True)
print m

MannwhitneyuResult(statistic=157.0, pvalue=0.12514839875175593)

rawDescription: What is python's equivalent of Matlab's ranksum?


code:
import asyncore
import socket
import sys
from weakref import WeakValueDictionary

class Soc(asyncore.dispatcher):

    CMDS = {
        'list': 'cmd_list',
        'set': 'cmd_set_addr',
        'send': 'cmd_send',
    }

    def __init__(self, port=8000):
        asyncore.dispatcher.__init__(self)

        self._conns = WeakValueDictionary()
        self._current = tuple()

        self.port = port
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind(('', port))
        self.listen(5)

        self.cmdline = Cmdline(self.handle_input, sys.stdin)
        self.cmdline.prompt()

    def writable(self):
        return False

    def handle_input(self, i):
        tokens = i.strip().split(None, 1)
        cmd = tokens[0]
        arg = ""
        if len(tokens) > 1:
            arg = tokens[1]

        cbk = self.CMDS.get(cmd)
        if cbk:
            getattr(self, cbk)(arg)

        self.cmdline.prompt(self._addr_to_key(self._current))

    def handle_accept(self):
        channel, addr = self.accept()
        c = Conn(channel)
        self._conns[self._addr_to_key(addr)] = c

    def _addr_to_key(self, addr):
        return ':'.join(str(i) for i in addr)

    def cmd_list(self, *args):
        avail = '\n'.join(self._conns.iterkeys())
        print "\n%s\n" % avail

    def cmd_set_addr(self, addr_str):
        conn = self._conns.get(addr_str)
        if conn:
            self._current = conn.addr

    def cmd_send(self, msg):
        if self._current:
            addr_str = self._addr_to_key(self._current)
            conn = self._conns.get(addr_str)
            if conn:
                conn.buffer += msg

class Cmdline(asyncore.file_dispatcher):
    def __init__(self, cbk, f):
        asyncore.file_dispatcher.__init__(self, f)
        self.cbk = cbk

    def prompt(self, msg=''):
        sys.stdout.write('%s > ' % msg)
        sys.stdout.flush()

    def handle_read(self):
        self.cbk(self.recv(1024))

class Conn(asyncore.dispatcher):

    def __init__(self, *args, **kwargs):
        asyncore.dispatcher.__init__(self, *args, **kwargs)
        self.buffer = ""

    def writable(self):
        return len(self.buffer) > 0

    def handle_write(self):
        self.send(self.buffer)
        self.buffer = ''

    def handle_read(self):
        data = self.recv(4096)
        print self.addr, '-', data

server = Soc(8000)
asyncore.loop()

rawDescription: How do i control what socket thread I want to connect to in a asychronous reverse server python?

description: Control what socket thread I want to connect to in a asychronous reverse server python


code:
import multiprocessing as mp
import itertools
import time

def g():
    for el in xrange(50):
        print el
        yield el

def f(x):
    time.sleep(1)
    return x * x

if __name__ == '__main__':
    pool = mp.Pool(processes=4)              # start 4 worker processes
    go = g()
    result = []
    N = 11
    while True:
        g2 = pool.map(f, itertools.islice(go, N))
        if g2:
            result.extend(g2)
            time.sleep(1)
        else:
            break
    print(result)

rawDescription: Python Multiprocessing.Pool lazy iteration


code:
lastdayfrom = pd.to_datetime('8/24/2015')
print lastdayfrom
#2015-08-24 00:00:00

print df
#           Error_Description       Date    Weekend          Type
#0     N17739 Limit switch X- 2015-05-01 2015-05-03      Critical
#1     N17739 Limit switch Y- 2015-05-01 2015-05-03      Critical
#2    N938 Key non-functional 2015-05-01 2015-05-03  Non-Critical
#3   P124 Magazine is running 2015-05-01 2015-05-03  Non-Critical
#4     N17738 Limit switch Z+ 2015-02-01 2015-05-03      Critical
#5    N938 Key non-functional 2015-07-25 2015-05-03  Non-Critical
#6        P873 ENCLOSURE DOOR 2015-07-24 2015-08-30  Non-Critical
#7           N3065 Reset M114 2015-07-21 2015-08-21  Non-Critical
#8          N3065 Reset M114, 2015-08-22 2015-08-22  Non-Critical
#9      N2853 Synchronization 2015-08-23 2015-08-30      Critical
#10            P152 ENCLOSURE 2015-08-24 2015-08-30  Non-Critical
#11         N6236 has stopped 2015-08-24 2015-08-30      Critical

print df.dtypes
#Error_Description            object
#Date                 datetime64[ns]
#Weekend              datetime64[ns]
#Type                         object
#dtype: object

#set index from column Date
df = df.set_index('Date')
#if datetimeindex isn't order, order it
df.index = df.index.sort_values()

#last 30 days of date lastday
df = df.loc[lastdayfrom - pd.Timedelta(days=30):lastdayfrom].reset_index()

rawDescription: Python - Retrieving last 30 days data from dataframe pandas


code:
p_os = os.popen("commands","r")
line = p_os.readline()
print line
re.search('<(\d+)>', line).group(1) #Cheers, Jon Clements

rawDescription: os.system for submitting command

description: Os.system for submitting command


code:
inputfile = outputfile = degres = None

for opt, arg in opts:
    if opt in ("-h", "--help"):
        print usage
        sys.exit()
    elif opt in ("-i", "--ifile"):
        inputfile = arg
        i = 1
    elif opt in ("-o", "--ofile"):
        outputfile = arg
        o = 1
    elif opt in ("-d", "--degres"):
        degres = arg
        d = 1

if inputfile is None or degres is None:
    print usage
    sys.exit(1)

outputfile = outputfile or inputfile

rotate_picture(inputfile, outputfile, degres)

rawDescription: make a test python options mandatory

description: Make a test python options mandatory


code:
o1 = Outer()
o2 = Outer()
i1 = o1.Inner
i2 = o2.Inner
print i1(), i2(), i1(), i2()

rawDescription: Inner Classes: How can I get the outer-class object at construction time?


rawDescription: Binary numbers of N digits

description: Binary numbers of N digits


code:
print '''This is a very long line of text that goes beyond the 80
character limit.'''

rawDescription: Splitting long line printed to screen the right way in python


code:
import socket
import sys

host = 'localhost'
port = 8220
address = (host, port)

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

print "Listening for client . . ."
conn, address = server_socket.accept()
print "Connected to client at ", address
#pick a large output buffer size because i dont necessarily know how big the incoming packet is
while True:
    output = conn.recv(2048);
    if output.strip() == "disconnect":
        conn.close()
        sys.exit("Received disconnect message.  Shutting down.")
        conn.send("dack")
    elif output:
        print "Message received from client:"
        print output
        conn.send("ack")

rawDescription: Repeating/Continuous python server and client


code:
import foo

class bar_mock(object):
    @staticmethod
    def get_data():
        return 'test'

if __name__ == '__main__':
    foo.bar = bar_mock
    d = foo.Data()
    print d.val    # prints 'test'

rawDescription: in python, how to manipulate namespace of an instance

description: In python, how to manipulate namespace of an instance


code:
def load_workbook(filename, use_iterators=False):
    if isinstance(filename, file):
        # fileobject must have been opened with 'rb' flag
        # it is required by zipfile
        if 'b' not in filename.mode:
            raise OpenModeError("File-object must be opened in binary mode")

    try:
        archive = ZipFile(filename, 'r', ZIP_DEFLATED)
    except (BadZipfile, RuntimeError, IOError, ValueError), e:
        raise InvalidFileException(unicode(e))
    wb = Workbook()

    if use_iterators:
        wb._set_optimized_read()

    try:
        _load_workbook(wb, archive, filename, use_iterators)
    except KeyError, e:
        raise InvalidFileException(unicode(e))
    finally:
        archive.close()
    return wb

rawDescription: Closing files in openpyxl

description: Closing files in openpyxl


code:
list_of_goals = row.xpath('//span[@title="Goal"]')

if list_of_goals:
    print "GOAL" # <-- string

rawDescription: Xpath() method not returning results using Scrapy


code:
from ctypes import *

class Foo(Structure):
    _fields_ = [("a", c_uint),
                ("b", c_uint),
                ("c", c_ushort),
                ("d", c_ushort)]

    def __repr__(self):
        return "<Foo: a:%d b:%d c:%d e:%d>" % (self.a, self.b, self.c, self.d)

f = Foo(1,2,3,4)
print repr(f)

# <Foo: a:1 b:2 c:3 e:4>

rawDescription: Using __repr__() on a ctypes subclass?

description: Using __repr__() on a ctypes subclass


code:
with open(PIK, "wb") as f:
    pickle.dump(len(data), f)
    for value in data:
        pickle.dump(value, f)
data2 = []
with open(PIK, "rb") as f:
    for _ in range(pickle.load(f)):
        data2.append(pickle.load(f))
print data2

rawDescription: Saving and loading multiple objects in python pickle file


code:
class StreamListener(tweepy.StreamListener):

    status_wrapper = TextWrapper(width=60, initial_indent='    ',
                                    subsequent_indent='    ')
    conn = mdb.connect('localhost', 'dbUser','dbPass','dbBase')

    def on_status(self, status):
        try:
            cursor = self.conn.cursor()
            cursor.execute('INSERT INTO tweets (text, date) VALUES (%s, NOW())' ,(status.text))
            print self.status_wrapper.fill(status.text)
            print '\n %s  %s  via %s\n' % (status.author.screen_name, status.created_at, status.source)
        except Exception, e:
            # Catch any unicode errors while printing to console
            # and just ignore them to avoid breaking application.
            pass

rawDescription: How to transfer streaming info from stdout to a mySQLdb in python?

description: Transfer streaming info from stdout to a mySQLdb in python


code:
#!/usr/bin/python
#
# Frankenstein-esk amalgam of example code
#   Key of which comes from the Twisted "Chat" example
#   (such as: http://twistedmatrix.com/documents/12.0.0/core/examples/chatserver.py)

import sys # so I can get at stdin
import os # for isatty
import termios, tty # access to posix IO settings
from random import random
from twisted.internet import reactor
from twisted.internet import stdio # the stdio equiv of listenXXX
from twisted.protocols import basic # for lineReceiver for keyboard
from twisted.internet.protocol import Protocol, ServerFactory

class MyClientConnections(basic.LineReceiver):

    def __init__(self):
        self.storedState = "Idle"
        self.connectionpos = None

    def connectionMade(self):
        self.factory.clients.append(self) # <--- magic here :
            # protocol automagically has a link to its factory class, and
            # in this case that is being used to push each new connection
            # (which is in the form of this class) into a list that the
            # factory can then access to get at each of the connections
        self.connectionpos = str(self.factory.clients.index(self)) # figure out
                                      # where I am in the connection array
        print "Got new client! (index:", self.connectionpos + ")"
        self.transport.write("---\nYour connection: " + self.connectionpos + "\n---\n")

    def connectionLost(self, reason):
        print "Lost a client!"
        self.factory.clients.remove(self)

    # used to pretend that something was typed on a telnet connection
    def fakeInput(self, message):
        self.transport.write("FAKING Input: '" + message + "'\n")
        self.lineReceived(message)

    #this is only in a def on its own so I can lump my demo callLater
    def stateUpdate(self, newState, delay):
        self.storedState = newState
        # the following is a hack to fake data coming in this interface
        reactor.callLater(delay, self.fakeInput, newState + " DONE")

    def processInput(self, newState):
        # all the logic in here is junk to make a demo, real code may or may-not look like
        # this.  This junk logic is an example statemachine though
        if self.storedState == "Idle":
            if newState == "start":
                self.stateUpdate("State A", 1)
                # send a message to this connection
                self.transport.write("starting state machine\n")
                # send a message to the term in which the script it running
                print "Connection [" + self.connectionpos + "] starting state machine"
        elif self.storedState == "State A":
            if newState == "State A DONE":
                self.transport.write("Beginning state B\n")
                self.stateUpdate("State B", 2)
        elif self.storedState == "State B":
            if newState == "State B DONE":
                self.transport.write("Beginning state C\n")
                self.stateUpdate("State C", 2)
        elif self.storedState == "State C":
            if newState == "State C DONE":
                self.storedState = "Idle"
                # send a message to this connection
                self.transport.write("Returning to Idle state\n")
                # send a message to the term in which the script it running
                print "Connection [" + self.connectionpos + "] return to Idle state"

    def lineReceived(self, line):
        # print "received '" + line +"' from connection", self.factory.clients.index(self)
        self.processInput(line)

class MyServerFactory(ServerFactory):
    protocol = MyClientConnections

    def __init__(self):
        self.clients = [] # this gets filled from the class above

    def sendToAll(self, message):
      for c in self.clients:  # Read MyClientConnections class for background
        c.transport.write(message)

    def randStart(self, width):
      for c in self.clients:
        startDelay = random() * width
        print "Starting client " + str(c.connectionpos) + " in " +str(startDelay) + " secs"
        reactor.callLater(startDelay, c.processInput, "start")

# to set keyboard into cbreak mode -- just because I like it that way...
class Cbreaktty(object):
    org_termio = None
    my_termio = None

    def __init__(self, ttyfd):
        if(os.isatty(ttyfd)):
            self.org_termio = (ttyfd, termios.tcgetattr(ttyfd))
            tty.setcbreak(ttyfd)
            print '  Set cbreak mode'
            self.my_termio = (ttyfd, termios.tcgetattr(ttyfd))
        else:
          raise IOError #Not something I can set cbreak on!

    def retToOrgState(self):
        (tty, org) = self.org_termio
        print '  Restoring terminal settings'
        termios.tcsetattr(tty, termios.TCSANOW, org)

class KeyEater(basic.LineReceiver):

    def __init__(self, factoryObj):
        self.setRawMode() # Switch from line mode to "however much I got" mode
        # the following is one of the key connecting ideas in twisted, the object
        # that contains another state machine (really all of the tcp statemachines)
        # has been passed into this class via its init.
        self.factoryObj = factoryObj

    def rawDataReceived(self, data):
        key = str(data).lower()[0]
        if key == 's':
            # The following line is going to call (from within the factory object)
            # the random start def
            self.factoryObj.randStart(5)
        elif key == 'd':
            print "State Dump of connections"
            print "-------------------------"
            for c in self.factoryObj.clients:
                print "#" + str(c.connectionpos) + "      " + c.storedState
        elif key == 'q':
            reactor.stop()
        else:
            print "--------------"
            print "  If you haven't already, connect to this script via a"
            print "  'telnet localhost 5000' at least one (multiple connections"
            print "  are better)"
            print "Press:"
            print "      s  - randomly start all clients"
            print "      d  - dump the state of all connected clients"
            print "      q  - to cleanly shutdown"
            print " Note: you can type commands in the connections, things"
            print "       most useful of which is 'start'"
            print "---------------"

# Custom tailored example for SO:30397425
#
# This code is a mishmash of styles and techniques. Both to provide different examples of how
# something can be done and because I'm lazy.  Its been built and tested on OSX and linux,
# it should be portable (other then perhaps termal cbreak mode).  If you want to ask
# questions about this code contact me directly via mail to mike at partialmesh.com
#
# While it isn't directly using serial ports, the tcp connections that its using are a good
# parallel.
#
# It should be used by running the script and then opening up many windows telnet'ing into
# localhost 5000.
#
# Once running press any key in the window where the script was run and it will give
# instructions.
# The normal use case would be to type "s" to queue statemachine
# start-ups, then repeatedly press 'd' to dump the status of all the state machines
#
# 'start' can be typed into any of the telnet connections to start them by hand too.

def main():
    client_connection_factory = MyServerFactory()

    try:
      termstate = Cbreaktty(sys.stdin.fileno())
    except IOError:
      sys.stderr.write("Error: " + sys.argv[0] + " only for use on interactive ttys\n")
      sys.exit(1)

    keyboardobj = KeyEater(client_connection_factory)

    stdio.StandardIO(keyboardobj,sys.stdin.fileno())
    reactor.listenTCP(5000, client_connection_factory)
    reactor.run()
    termstate.retToOrgState()
if __name__ == '__main__':
  main()

rawDescription: How to deal with multiple serial ports for R/W using twisted?

description: Deal with multiple serial ports for R/W using twisted


code:
print capitalize_nested(['this', 'that', ['other']])
# ['This', 'That', ['Other']]

rawDescription: Function that takes a nested list of strings and returns a new nested list with all strings capitalized?


code:
import urllib2
from BeautifulSoup import BeautifulSoup

LINK = "http://investing.businessweek.com/research/stocks/private/snapshot.asp?privcapId=1716794"
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Google Chrome')]

soup = BeautifulSoup(opener.open(LINK))

location = soup.find('div', {'itemprop': 'address'}).findAll('p')[-1].text
founded = soup.find('span', {'itemprop': "foundingDate"}).text

print location, founded

rawDescription: Python Webscraping, Beautiful Soup on Business Week to Find Company Year Founded and Location


code:
import logging
logging.basicConfig(level=logging.DEBUG)
logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)

from spyne.decorator import rpc
from spyne.service import ServiceBase
from spyne.util import xml
from spyne.model import complex, primitive
from spyne.application import Application
from spyne.protocol.soap import Soap11
from spyne.server.wsgi import WsgiApplication
from lxml import etree

class ComplexModel(complex.ComplexModelBase):
    __namespace__ = 'http://xml.candyshop.com/ns/candies/'
    __metaclass__ = complex.ComplexModelMeta

class CandyModel(ComplexModel):
    __type_name__ = 'candy'
    flavor = complex.XmlAttribute(primitive.Unicode)

class BagModel(ComplexModel):
    __type_name__ = 'bag'
    candies = complex.Array(CandyModel)

class Bag(ComplexModel):
    bag = BagModel

class CandyShop(ServiceBase):
    __tns__ = 'http://xml.candyshop.com/ns/shop/'

    @rpc(_returns=Bag)
    def get_my_bag(ctx):
        bag = BagModel()
        bag.candies = [CandyModel(flavor='choco')]
        return Bag(bag=bag)

    @classmethod
    def dispatch(cls):
        application = Application([cls],
            tns=cls.__tns__,
            in_protocol=Soap11(validator='lxml'),
            out_protocol=Soap11(cleanup_namespaces=True)
        )
        return application

# In case you need to extract parts of your response, you can use hooks:

def _on_method_return_document(ctx):
    ns = ctx.app.interface.nsmap
    elt = ctx.out_document.xpath('//s0:bag',namespaces=ns)[0]
    output = etree.tostring(elt, pretty_print=True)

    print output # do what you want with it.

CandyShop.event_manager.add_listener('method_return_document',
                                                _on_method_return_document)

if __name__ == '__main__':
    from wsgiref.simple_server import make_server

    service_app = CandyShop.dispatch()

    application = WsgiApplication(service_app)

    server = make_server('0.0.0.0', 8080, application)
    server.serve_forever()

rawDescription: Overriding namespace in array content

description: Overriding namespace in array content


code:
import ast

print df

#                                           sentiment  tmp
#0  {'compound': 0.4404, 'neg': 0.0, 'neu': 0.919,...   aa
#1  {'compound': 0.4404, 'neg': 0.0, 'neu': 0.256,...  sss

print type(df['sentiment'][0])

#<type 'str'>

df1 = df['sentiment'].apply(lambda x: pd.Series(ast.literal_eval(x)))
print df1

#   compound  neg    neu    pos
#0    0.4404    0  0.919  0.081
#1    0.4404    0  0.256  0.744

rawDescription: Pandas: Applying a function whilst splitting a dict into columns


code:
import operator
def my_eval(nums,ops):

    nums = list(nums)
    ops = list(ops)
    operator_order = ('*/','+-')  #precedence from left to right.  operators at same index have same precendece.
                                  #map operators to functions.
    op_dict = {'*':operator.mul,
               '/':operator.div,
               '+':operator.add,
               '-':operator.sub}
    Value = None
    for op in operator_order:                   #Loop over precedence levels
        while any(o in ops for o in op):        #Operator with this precedence level exists
            idx,oo = next((i,o) for i,o in enumerate(ops) if o in op) #Next operator with this precedence
            ops.pop(idx)                        #remove this operator from the operator list
            values = map(float,nums[idx:idx+2]) #here I just assume float for everything
            value = op_dict[oo](*values)
            nums[idx:idx+2] = [value]           #clear out those indices

    return nums[0]

print my_eval(*parse('3/2*15'))

rawDescription: parsing math expression in python and solving to find an answer

description: Parsing math expression in python and solving to find an answer


code:
from stemming.porter2 import stem
from nltk.corpus import stopwords
stopwords_ =  stopwords.words('english')

def addString(x):
   flag = True
   y = [stem(j).lower() for j in x.split() if j.lower() not in stopwords_]
   for i in section:
      i = [stem(j).lower() for j in i.split() if j.lower() not in stopwords_]
      if y==i:
         flag = False
         break
   if flag:
      section.append(x)
      print "\tNew Section Added"

section = [ "Activity (Last 3 Days)", "Activity (Last 7 days)", "Executable running from disk", "Actions from File"]  # initial Section list
addString("Activity (Last 30 Days)")
addString("Executables running from disk")
addString("Actions from a file")
section

rawDescription: Need String Comparison 's solution for Partial String Comparison in Python


code:
while low<high:
    if l[index] == n:
        print index
    if  l[index] < n:
        low = index + 1
    else:
        high = index
    index = (high + low)/2

rawDescription: Exiting out of loop when not found - Binary Search in Python


code:
text = "foo www.stackoverflow.com bar"

def link(match):
    uri = match.group()
    if ":" not in uri[:7]:
        uri = "http://" + uri
    return r"""{0}""".format(uri)

print uri_re.sub(link, text)

rawDescription: Python -Filtering links from user inputted data


code:
import itertools
primes = [2]
candidate_iter = itertools.count(3, 2)
while len(primes) < 1000:
    candidate = candidate_iter.next()
    if all(candidate % testprime != 0 for testprime in primes):
        primes.append(candidate)
print primes

rawDescription: Please help, this loop isn't working as expected and I don't know how to use booleans in Python


code:
if "$Revision: 1.14" in line:
    print line

rawDescription: Extract Substring from String Python

description: Extract Substring from String Python


code:
import wx

trial_range = range(1, 3)
timer_range = range(7)

class TimersClass(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        panel = wx.Panel(self)
        self.button1 = wx.Button(panel, label = 'Go')
        self.Bind(wx.EVT_BUTTON, self.BuildTimers, self.button1)

        self.timers = {}  # a structure to hold the timers

    def BuildTimers(self, event):
        for trial in trial_range:
            for timer in timer_range:
                new_timer = wx.Timer(self)
                new_timer.Start(1 + timer*1000, oneShot=True)

                key = (trial, timer)
                self.timers[key] = new_timer # to keep the timer in the class
                new_timer.mykey = key  #optional: to know the key of the timer via the event (see Hi())

        self.Bind(wx.EVT_TIMER, self.Hi)

    def Hi(self, event):
        print "timer:",
        print event.GetEventObject().mykey

app = wx.PySimpleApp()
frame = TimersClass(None)
frame.Center()
frame.Show()
app.MainLoop()

rawDescription: wxpython start multiple timers within a for loop

description: Wxpython start multiple timers within a for loop


rawDescription: Comparing values from a multidimentional array using Python


code:
if day == 1:
   print "The temperature ", day, "December was:", December[0], "celsius"
elif day == 2:
   print "The temperature ", day, "December was:", December[1], "celsius"
elif day == 3:
   print "The temperature ", day, "December was:", December[2], "celsius"

rawDescription: Using a for loop while maintaining list object


code:
from nltk.corpus import wordnet as wn

# Concepts to compare
dog_sense = wn.synsets('dog')[0] # It's http://goo.gl/b9sg9X
jb_sense = wn.synsets('James_Baldwin')[0] # It's http://goo.gl/CQQIG9

# To access the hypernym_paths()[0]
# It's weird for that hypernym_paths gives a list of list rather than a list, nevertheless it works.
dog_hypernyms = dog_sense.hypernym_paths()[0]
jb_hypernyms = jb_sense.hypernym_paths()[0]

# Discriminating features in terms of concepts in WordNet
human = wn.synset('person.n.01') # i.e. [+human]
animal = wn.synset('animal.n.01') # i.e. [+animal]

try:
  assert human in jb_hypernyms and animal not in jb_hypernyms
  print "James Baldwin is human"
except:
  print "James Baldwin is not human"

try:
  assert human in dog_hypernyms and animal not in dog_hypernyms
  print "Dog is an animal"
except:
  print "Dog is not an animal"

rawDescription: Wordnet selectional restrictions in NLTK


code:
import os
import wx

wildcard = "Python source (*.py)|*.py|" \
            "All files (*.*)|*.*"

########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY,
                          "File and Folder Dialogs Tutorial")
        panel = wx.Panel(self, wx.ID_ANY)
        self.currentDirectory = os.getcwd()

        saveFileDlgBtn = wx.Button(panel, label="Show SAVE FileDialog")
        saveFileDlgBtn.Bind(wx.EVT_BUTTON, self.onSaveFile)

        # put the buttons in a sizer
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(saveFileDlgBtn, 0, wx.ALL|wx.CENTER, 5)
        panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onSaveFile(self, event):
        """
        Create and show the Save FileDialog
        """
        dlg = wx.FileDialog(
            self, message="Save file as ...",
            defaultDir=self.currentDirectory,
            defaultFile="", wildcard=wildcard, style=wx.SAVE
            )
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            print "You chose the following filename: %s" % path
        dlg.Destroy()

#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

rawDescription: wxPython New, Save, and SaveAs Methods

description: WxPython New, Save, and SaveAs Methods


code:
import csv
import nltk

tweet = []

for t in csv.DictReader(open('test.csv'), delimiter=','):
    tweet.extend(t['text'].lower().split()) # <-----------

tweet_text = nltk.Text(tweet)
print tweet_text.count('the')

rawDescription: count occurrence of a words in a csv file in python using nltk

description: Count occurrence of a words in a csv file in python using nltk


rawDescription: Is there a better way to convert from decimal to binary in python?


code:
import re

text = "Split.this,text in seven.separate,words"

myexp=re.compile(r'[\s.,]')

print myexp.split(text)

rawDescription: How to Tokenize A Text By Regular Expression In Python


code: print "%*s, blah" % (max_title_width,column)

rawDescription: variable length of %s with the % operator in python

description: Variable length of %s with the % operator in python


code:
def print_three_things(a, b, c):
    print 'a = {0}, b = {1}, c = {2}'.format(a,b,c)
mylist = ['aardvark', 'baboon', 'cat']
print_three_things(*mylist)

rawDescription: *args and **kwargs?

description: *args and **kwargs


code:
result = cur.fetchone()
print result[0]

rawDescription: Get psycopg2 count(*) number of results

description: Get psycopg2 count(*) number of results


code:
from xml.dom.minidom import parseString
file = open('test.xml','r')
data = file.read()
file.close()
dom = parseString(data)
print len(dom.getElementsByTagName('out'))

rawDescription: Counting number of xml tags in python using xml.dom.minidom


code:
def c(b, a=a):
  if b is None:
    def a(b):
      print 'local',b
  a(b) #Line 8

rawDescription: What is the pythonic way to have conditional shadowing?


code:
def function1(a):
    print a

def function2(b):
    print b

def function3(c):
    print c

def function4():
    return "hello!"

def main():
    a, b, c = (1, 2, 3)
    function1(a)
    function2(b)
    function3(c)
    d = function4()
    print d

if __name__ == "__main__":
    main()

rawDescription: splitting one program into several smaller and their binding in python?

description: Splitting one program into several smaller and their binding in python


code:
# Needs NumPy
try:
    import numpy.oldnumeric as _Numeric
except:
    msg= """
    This module requires the NumPy module, which could not be
    imported.  It probably is not installed (it's not part of the
    standard Python distribution). See the Numeric Python site
    (http://numpy.scipy.org) for information on downloading source or
    binaries."""
    raise ImportError, "NumPy not found.\n" + msg

rawDescription: wx script can't see numpy, but it's installed

description: Wx script can't see numpy, but it's installed


code:
def printvar(label):
    for frame in inspect.getouterframes(inspect.currentframe())[1:]:
        local_vars = frame[0].f_locals
        if label in local_vars:
            print "%s:\n%s" % (label, frame[0].f_locals[label])
            return
    raise NameError("No such variable")

rawDescription: How to pprint and automatically include the the variable name

description: Pprint and automatically include the the variable name


code:
import re
mystr = 'HelloWorldToYou'
pat = re.compile(r'[A-Z][a-z]*')
print [x.group() for x in pat.finditer(mystr)]

rawDescription: Python regular expression, matching the last word


code:
import re
from collections import defaultdict

search_terms = [['term1',300],['term2',400],['term3',200]] #words searched on our website with number of hits for each
item_description = [[900001,'a string with term1'],[900002,'a string with term2'],[900003,'a string with term1 and term2']]

d = defaultdict(list)
i = 0

for item in search_terms:
    d[item[0]].append(item[1])
    rgx = re.compile(item[0])
    for info in item_description:
        matches = re.findall(rgx, info[1])
        if matches:
            d[item[0]].append(info[0])
        print matches
print d

rawDescription: comparing excel data from two separate sheets with python/xlrd

description: Comparing excel data from two separate sheets with python/xlrd


code:
soup = BeautifulSoup(data, 'lxml')
print soup

rawDescription: Extracting text between <br> with beautifulsoup, but without next tag


rawDescription: Rotation of an array of vectors by an array of rotation matrices


code:
class Details():
    def __init__(self):
        self.name = raw_input("Enter human name> ")
        self.address = raw_input("Human Address> ")
        self.phone = raw_input("Human Phone Number> ")
        self.email = raw_input("Human Email> ")
        self.info = [self.name, self.address, self.phone, self.email]

go = Details() # create instance
# print details by accessing the instance attributes
print go.name,go.address,go.email,go.phone,go.info

rawDescription: Print function attribute

description: Print function attribute


code:
@gen.coroutine
def get(self):
    print 'a'
    yield self.process('data') # I don't want to wait here
    print 'b'
    self.write('page')

@gen.coroutine
def process(self, arg):
    print 'c'
    d = yield gen.Task(self.otherFunc, arg)
    print 'd'
    raise gen.Return(None)

rawDescription: Don't wait for an async function to finish


code:
from psutil import disk_partitions

def diskinfo():
    for i, disk in enumerate(disk_partitions()):
        print "Disk #{0:d} {1:s}".format(i, disk.device)
        print " Mount Point: {0:s}".format(disk.mountpoint)
        print " File System: {0:s}".format(disk.fstype)
        print " Options: {0:s}".format(disk.opts)

diskinfo()

rawDescription: Python output format

description: Python output format


code:
import facebook
graph = facebook.GraphAPI()
page = graph.get_object('6127898346')
print '{} has {} likes.'.format(page['name'], page['likes'])

rawDescription: Get information for Facebook page in Facebook API graph


code: print chunked_topsort(['ac', 'bc', 'bde', 'be', 'fbcg'])

rawDescription: More efficient algorithm to compute an integer mapping for a set of relations


code:
import re
samplestr = u'\xa9 Copyright 2009-10 \n\t\t\t\t All Rights Reserved. (Best viewed in 1024x768 \n\t\t\t\tresolution & IE 6.0)                    break\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0 \nChief Engineer'
re.sub(ur'[\s\xa0]+', u' ', samplestr)

rawDescription: python regular expression works in sample code but not the code required

description: Python regular expression works in sample code but not the code required


code:
from StringIO import StringIO
import pycurl

url = 'http://www.google.com/'

storage = StringIO()
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.WRITEFUNCTION, storage.write)
c.perform()
c.close()
content = storage.getvalue()
print content

rawDescription: Getting HTML with Pycurl

description: Getting HTML with Pycurl


code:
a = 'hello'
b = 'world'
c = 'i have spaces in me'

d = ' '.join((a,b,c)) # <- correct way
# hello world i have spaces in me
print ' '.join(d.split(' ', 2)) # take joined, split out again making sure not to split `c`, then join back again!?

rawDescription: How to split a line but keep a variable in the line unsplit in python

description: Split a line but keep a variable in the line unsplit in python


code:
for v in f.f_locals.keys():
    if not v.startswith("_"):
        print v

rawDescription: View Variables in Python

description: View Variables in Python


code:
l = 'there are no builtins for that'.split()
print [" ".join(l[n:n+3]) for n in xrange(len(l)-2)]
['there are no', 'are no builtins', 'no builtins for', 'builtins for that']

rawDescription: Is there a built-in function in Python or MySQL which returns a set of 3 frequently occuring words?


code:
import pycountry

country = pycountry.countries.get(name='Norway')
currency = pycountry.currencies.get(numeric=country.numeric)

print currency.letter
print currency.name

rawDescription: django countries currency code

description: Django countries currency code


code:
import tensorflow as tf

image = tf.fill([8,8], 0)
sess = tf.Session()
sess.run(tf.initialize_all_variables())
image_row = tf.slice(image, [1,0], [1, -1])
total = tf.reduce_sum(image_row)
is_all_zero = tf.equal(total, 0)
print sess.run([total, is_all_zero, image_row])

rawDescription: Tensorflow how to check if a tensor row is only zeroes?


code:
import socket
import ping

ip_address_up = []

addr_range = "192.168.1.%d"

for i in range(1, 254):
   try:
       ip = addr_range % i
       delay = ping.do_one(ip, timeout=2)
       ip_address_up.append(ip)
   except (socket.herror, socket.timeout) as ex:
       pass

print ip_address_up

rawDescription: How to find out how many clients are on a certain address range?

description: Find out how many clients are on a certain address range


code:
def cround(v):
    """
    Round number down at 1st decimal place when the digit in the
    3rd decimal place is <= 6, up when >= 7
    """
    v *= 10
    q = str(round(v, 2))
    if int(q[-1]) <= 6:
        return int(v) / 10.0
    return round(v) / 10.0

NUMS = [
    10.666, 10.667, 0.1, 1.0, 10.11, 10.22, 10.06, 10.006, 11.6, 11.7,
    10.666123, 10.667123, 10.888, 10.999 ]

for num in NUMS:
    print str(num).ljust(11), cround(num)

rawDescription: Rounding Rules in Python

description: Rounding Rules in Python


code:
a = rolling_window(matrix, 2)
print np.all(rolling_window(matrix, 2) == [0,1], axis=2)

rawDescription: Rolling window or occurrences for 2D matrix in Numpy per row?


code:
from bs4 import BeautifulSoup

data = """

User Name

"Data to be extracted"

"""

soup = BeautifulSoup(data)
print soup.find('p', class_='class_label').find_next_sibling('p').text

rawDescription: Python : Extract HTML content

description: Python : Extract HTML content


code:
import atexit
@atexit.register
def save_cache():
    print 'save cache'

rawDescription: Is there a way to detect when a python program is going to end?


rawDescription: How do I limit the number of digits in a list?

description: Limit the number of digits in a list