code:
s = 'output_AU1001-BY.20160502.csv'
date = s.split(".")[1]
print date

rawDescription: How to extract date from the filename using Python?

description: Extract date from the filename using Python


code:
data = ['a']
a, b = data[0], data[1:]
print a, b

rawDescription: How to unpack list into two variables in python2.x

description: Unpack list into two variables in python2.x


code:
poem_init = int(random()*(len(text)-140))
poem = text[poem_init:poem_init+140].split(" ")

if text.find(" "+ poem[0]) < 0:
    del poem[0]

if text.find(poem[-1] + " ") < 0 or text.find(poem[-1]) != len(text):
    del poem[-1]

poem = " ".join(poem)
print poem

rawDescription: Creating a twitter bot - Need help extracting a string


code:
def train_punktsent(trainfile, modelfile):
  """ Trains an unsupervised NLTK punkt sentence tokenizer. """
  punkt = PunktTrainer()
  try:
    with codecs.open(trainfile, 'r','utf8') as fin:
      punkt.train(fin.read(), finalize=False, verbose=False)
  except KeyboardInterrupt:
    print 'KeyboardInterrupt: Stopping the reading of the dump early!'
  ##HACK: Adds abbreviations from rb_tokenizer.
  abbrv_sent = " ".join([i.strip() for i in \
                         codecs.open('abbrev.lex','r','utf8').readlines()])
  abbrv_sent = "Start"+abbrv_sent+"End."
  punkt.train(abbrv_sent,finalize=False, verbose=False)
  # Finalize and outputs trained model.
  punkt.finalize_training(verbose=True)
  model = PunktSentenceTokenizer(punkt.get_params())
  with open(modelfile, mode='wb') as fout:
    pickle.dump(model, fout, protocol=pickle.HIGHEST_PROTOCOL)
  return model

rawDescription: training data format for nltk punkt

description: Training data format for nltk punkt


code:
input_list = ["1", "2", "file1","A","B","C","file2","D","E","F","G","H","I"]
output_list = []

for item in input_list:
    if re.match("file[0-9]+", item) or len(output_list) == 0:
        output_list.append([item])
    else:
        output_list[-1].append(item)

print output_list

rawDescription: Split list into list of lists by regex

description: Split list into list of lists by regex


code:
soup = BeautifulSoup(resp.text)
ads = soup.findAll("div", {"class": "vcard"})
for ad in ads:
    print ad.h2.a.text

rawDescription: Get House Price data

description: Get House Price data


code:
import threading
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

_engine = create_engine('sqlite:///:memory:')
Session = scoped_session(sessionmaker(_engine))

def scoped_session_demo(remove=False):
    ids = []

    def push_ids():
        thread_name = threading.currentThread().getName()
        data = [thread_name]

        data.append(Session())
        if remove:
            Session.remove()
        data.append(Session())

        ids.append(data)

    t = threading.Thread(target=push_ids)
    t.start()
    t.join()

    push_ids()

    sub_thread, main_thread = ids

    sub_name, sub_session_a, sub_session_b = sub_thread
    main_name, main_session_a, main_session_b = main_thread

    print sub_name, sub_session_a == sub_session_b
    print main_name, main_session_a == main_session_b
    print sub_name, '==', main_name, sub_session_a == main_session_b

print 'Without remove:'
scoped_session_demo()
print 'With remove:'
scoped_session_demo(True)

rawDescription: Why is my scoped_session raising an AttributeError: 'Session' object has no attribute 'remove'


code: print integrate.dblquad(func, -pi/2, pi/2, lambda x:-pi/2, lambda x:pi/2)[0]

rawDescription: 2D integrals in SciPy

description: 2D integrals in SciPy


rawDescription: Use regular expression to remove contents in brackets in python


code:
import re
print re.findall(r'\d+', 'shsgd89shs2011%%5swts');

rawDescription: Extracting Numbers from a String Without Regular Expressions


code:
import calendar
print calendar.TextCalendar().formatmonth(2013,2,4)

rawDescription: How do I shift rows of consecutive numbers. Python 3.3.2

description: Shift rows of consecutive numbers. Python 3.3.2


code:
def generate_proto(source):
  """Invokes the Protocol Compiler to generate a _pb2.py from the given
  .proto file.  Does nothing if the output already exists and is newer than
  the input."""

  output = source.replace(".proto", "_pb2.py")

  if (not os.path.exists(output) or
      (os.path.exists(source) and
       os.path.getmtime(source) > os.path.getmtime(output))):
    print "Generating %s..." % output

    if not os.path.exists(source):
      sys.stderr.write("Can't find required file: %s\n" % source)
      sys.exit(-1)

    if protoc == None:
      sys.stderr.write(
          "Protocol buffers compiler 'protoc' not installed or not found.\n"
          )
      sys.exit(-1)

    protoc_command = [ protoc, "-I.", "--python_out=.", source ]
    if subprocess.call(protoc_command) != 0:
      sys.exit(-1)

rawDescription: Python project using protocol buffers, Deployment issues


code:
from bs4 import BeautifulSoup

# Read the input file. I am assuming the above html is part of test.html
html = ""
with open('test.html', 'r') as htmlfile:
    for line in htmlfile:
        html += line
htmlfile.close()

bs = BeautifulSoup(html)
links_to_follow = []

ul = bs.find('ul')
for li in ul.find_all('li'):
    height = int(li.find('div').get_text().strip().split()[1])
    price = int(li.find('div').get_text().strip().split()[3])
    if height > 10 and price > 20: # I am assuming this to be the criteria
        links_to_follow.append(li.find('a').get('href'))

print links_to_follow

rawDescription: mechanize - how to select links based on adjacent tags?

description: Mechanize - how to select links based on adjacent tags


code:
import datetime
import MySQLdb
import MySQLdb.cursors

db = MySQLdb.connect(host=server, user=mysql_user, passwd=mysql_pwd, db=mysql_db, connect_timeout=10,
                     cursorclass=MySQLdb.cursors.DictCursor)
cur = db.cursor()

ago = datetime.datetime.utcnow() - datetime.timedelta(minutes=15)
try:
    cur.execute("SELECT heartbeat_time FROM machines")
    for row in cur:
        if row['heartbeat_time'] <= ago:
            print row['heartbeat_time'], 'more than 15 minutes ago'
finally:
    cur.close()
    db.close()

rawDescription: Python foreach from a MySQLdb

description: Python foreach from a MySQLdb


code:
text = """
TOKYO-BLING.1 H02-AVAILABLE
TOKYO-BLING.1 H02-MIDDLING
TOKYO-BLING.1 H02-TOP
TOKYO-BLING.2 H04-USED
TOKYO-BLING.2 H04-AVAILABLE
TOKYO-BLING.2 H04-CANCELLED
WAY-VERING.1 H03-TOP
WAY-VERING.2 H03-USED
WAY-VERING.2 H03-AVAILABLE
WAY-VERING.1 H03-CANCELLED
"""

T = sorted(t.strip() for t in text.split("\n") if t)

for L in group_by_longest_prefix(T):
  print L

rawDescription: longest common sequence group

description: Longest common sequence group


code:
print df.groupby('UserId')['WordID'].apply(lambda x: x.tolist()).values
[[20, 30, 40] [25, 16] [56, 44, 12]]

rawDescription: Data loading using arrays in Python

description: Data loading using arrays in Python


code:
data = pd.DataFrame({'id' : [50,50,30,10,50,50,30]})
print pd.factorize(data.id)[0]

rawDescription: Unique zero-based id for values in pandas


code:
import inspect
def interruptWithTerminal():
    frame = inspect.currentframe().f_back
    while True:
        interruptchoice = raw_input("print what variable? (Press Enter to continue): ")
        if not interruptchoice.strip(): break
        try:
            print eval(interruptchoice, frame.f_globals, frame.f_locals)
        except:
            print "Error"

rawDescription: pass all current variables to function in python

description: Pass all current variables to function in python


code:
import subprocess

p = subprocess.Popen(["python", "-i"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write("1+3\n")
print p.stdout.readline()
p.stdin.write("2+4\n")
print p.stdout.readline()
p.stdin.write("4+6\n")
print p.stdout.readline()
p.stdin.close()

rawDescription: How can I get return values with subprocess?

description: Get return values with subprocess


code:
for port in range(1, 9):
    edge = G.get_edge('Switch0', 'Switch1', key="{}-{}".format(port, port))
    print edge, edge.attr

rawDescription: pygraphviz: How to get same edges connected on different ports without iterating?

description: Pygraphviz: How to get same edges connected on different ports without iterating


code:
from collections import OrderedDict
import numpy as np

A = [['foo', 123], ['bar', np.array(range(10))], ['baz', 345]]
B = [['foo', 123], ['bar', np.array(range(10))], ['meow', 456]]

seen=OrderedDict((repr(e),0) for e in B)

newA=[]
for e in A+B:
    key=repr(e)
    if key in seen:
        if seen[key]==0:
            newA.append(e)
            seen[key]=1
    else:
        seen[key]=1
        newA.append(e)

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

rawDescription: Python: Intersection of two lists of lists


code:
import re

def get_gap_coordinates(sequence, gapSize=25):
    gapPattern = "N{%s,}?([^N](?:(?!N{%s,}?).)*)" % (gapSize, gapSize)
    p = re.compile(gapPattern)
    m = p.finditer(sequence)
    for gap in m:
        start,stop = gap.start(1), gap.end(1)
        yield(start,stop)

for x in get_gap_coordinates('NNNNNNACTGACGTNNNACTGACNNNNN', 3):
    print x

rawDescription: Match parts of string that are not consecutive stretches of certain character


code:
from bs4 import BeautifulSoup

with open('test.html', 'rb') as html: #My local version of your html file
    soup = BeautifulSoup(html.read())

for td in soup.find_all('td'):
    print td.get_text()

rawDescription: BeautifulSoup Tag Removal

description: BeautifulSoup Tag Removal


code:
list_of_input = map(int,raw_input().split())

print "you input:"
for i,input_val in enumerate(list_of_input):
    print "item %d: %d:" % (i, input_val)

    if input_val < 5:
        pass   # do something for certain items
    elif input_val > 10:
        pass   # do something else for other items
    else:
        pass   # etc

rawDescription: Taking dynamic number of inputs from the same line


code:
for attr in dir(status):
    print attr, getattr(status,attr)

rawDescription: How to "see" the structure of an object in python


code:
import fileinput
import re

# Assume the input file is foo.csv
for line in fileinput.FileInput('foo.csv', inplace=1):
    mm = re.search(r'^(.+?,.+?,.+?,)(\d{1,2})(\d{2})\.0+,(\d{1,2})\.0+',
        line)
    g1, g2, g3, g4 = mm.group(1), int(mm.group(2)), int(mm.group(3)), int(mm.group(4))
    print "%s%02i:%02i:%02i," % (g1, g2, g3, g4)

rawDescription: Use Python to modify specific columns in csv


code:
# Sample Username Client Service Handler.
from socket import *
import threading
import os
import csv

Username = ("ShyGuy")
host = input("Host: ")
port = input("Port: ")
try:
    ss = socket() #AF_INET,SOCK_STREAM)
    ss.connect((host,int(port))) #I was using ("localhost",1234) for testing
    ss.send(Username)
except IOError:
    print("Aw no man")

print("Waiting to Recieve Data")
while True:
    try:
        data = ss.recv(1024)
        if data:
            translated_data = data.decode()
            print(translated_data)
            if translated_data == Username:
                print"Name: {0} has been registered on server!".format(translated_data)
            else:
                Client_Username = translated_data
                print "New client name received: {0}".format(Client_Username)

    except Exception as e:
        print (vars(e))

rawDescription: Client socket not receiving data correctly


code:
import inspect
print inspect.getsource(FreqDist.keys)  # make sure your source code matches the source code below
print inspect.getsource(FreqDist._sort_keys_by_value)  # and matches this source code
text1[:40]  # does the first part of your text list match this one?
text1[-40:]  # and what about the end of your text list?

rawDescription: FreqDist in NLTK not sorting output

description: FreqDist in NLTK not sorting output


rawDescription: Dynamic string formatting in python

description: Dynamic string formatting in python


code:
import matplotlib.font_manager as fmg
for i, item in enumerate(fmg.findSystemFonts()):
    try:
        name=fmg.FontProperties(fname=item).get_name()
        if 'TIMES' in name.upper():
            print name, item, i
    except:
        pass

#Times New Roman /Library/Fonts/Times New Roman Italic.ttf 223
#Times New Roman /Library/Fonts/Times New Roman Bold Italic.ttf 232
#Times New Roman /Library/Fonts/Times New Roman Bold.ttf 336
#Times New Roman /Library/Fonts/Times New Roman.ttf 367

rawDescription: How to set matplotlib font for title, axes,

description: Set matplotlib font for title, axes,


code:
import sys

sys.path.append('C:\Users\mdriscoll\Documents')

import mymath

print mymath.add(4,5)
print mymath.division(4, 2)
print mymath.multiply(10, 5)
print mymath.fibonacci(8)
print mymath.squareroot(48)

rawDescription: Python - why can I import modules without __init__.py at all?


rawDescription: Assigning columns to a pandas dataframe constructed from a string


code:
import requests

URL = "http://wps.aw.com/wps/media/objects/8992/9208383/Data_Sets/Ascii/Chapter1/HTWT1.txt"

response = requests.get(URL)

data = dict()

text = response.content.decode('ISO-8859-1').encode('utf-8').replace('\x00', '').strip()[2:]
for row in text[2:].splitlines()[1:]:
    OBS, x, y = row.split('\t')
    data[int(OBS)] = dict(x=int(x), y=int(y))

print data

rawDescription: Collecting data from .txt url in Python with strange encoding


rawDescription: Python Removing Non Latin Characters

description: Python Removing Non Latin Characters


code:
#!/usr/bin/python2.6

#  You _must_ turn on assistive devices under Accessibility prefpane
# for any of this code to work. Otherwise it won't do anything.

from Cocoa import *
from Foundation import *
from PyObjCTools import AppHelper
import keycode
import string
import sys

class AppDelegate(NSObject):
    def applicationDidFinishLaunching_(self, aNotification):
        NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSKeyDownMask, handler)

def handler(event):
    if event.type() == NSKeyDown and keycode.tostring(event.keyCode()) in string.printable:
        print keycode.tostring(event.keyCode())

def main():
    app = NSApplication.sharedApplication()
    delegate = AppDelegate.alloc().init()
    NSApp().setDelegate_(delegate)
    AppHelper.runEventLoop()

if __name__ == '__main__':
   main()

rawDescription: Python capture Keystrokes values in text file on OS X


code:
# this initializes values in the dictionary d with empty lists
# so that we can directly call .append() without checking "if key in keys"
from collections import defaultdict
d = defaultdict(list)

# your input string
s = "12,23,34,45,56,67,78,81"

# iterate through digit pairs
for pair in s.split(","):
  # get single digits from a pair
  fr = pair[0]
  to = pair[1]

  # add edges in both directions (undirected)
  d[fr].append(to)
  d[to].append(fr)

# see what we got
print d

rawDescription: convert a graph represented by a string of key-value pairs, to a dictionary. Python

description: Convert a graph represented by a string of key-value pairs, to a dictionary. Python


code:
thelist = [[1, 2, 3, 4, 5], 5, 6, 7, 8, [9, 0, 1, 8]]

expected_output =[[1, 2, 3, 4], 5, 6, 7, 8, [9, 0, 1]]

removal_items = []

for item in thelist:
    if not isinstance(item, list):
        removal_items.append(item)

for item in thelist:
    if isinstance(item, list):
        for remove in removal_items:
            if remove in item:
                item.remove(remove)

print thelist

assert thelist == expected_output

rawDescription: Iterate through a list of integer and lists in Python


code:
while response[0] != 'quit':
    response = raw_input("Please enter who you're looking for, or type 'exit' to quit the program: ").split()
    try:
        fn, thing = response[0].capitalize(), response[1].lower()
        print "%s's %s is %s" % (fn, thing, people[fn][thing])
    except KeyError:
        print wrong,

rawDescription: Able to use any case in input to generate the same dict values in output


code:
l1 = [1,2,3,4,5,6,7,8]
l2 = []
for number in l1:
    calc = number*10
    print calc
    l2.append(calc)

rawDescription: Python- How to generate new list from variable in a loop?


code:
from apiclient.discovery import build
DEVELOPER_KEY = '<get your own key here>'
youtube = build('youtube', 'v3', developerKey=DEVELOPER_KEY)
ids = '9bZkp7q19f0,eHvccEXfacM'
results = youtube.videos().list(id=ids, part='snippet').execute()
for result in results.get('items', []):
    print result['id']
    print result['snippet']['description']
    print '-----'

rawDescription: Use BeautifulSoup to get youtube description?


code:
import difflib

s="""R Deep Transverse Metatarsal Ligament 4 GEODE
R Distal JointCapsule 1 GEODE
R Dorsal Calcaneocuboid Ligament GEODE
R Dorsal Carpometacarpal Ligament 2 GEODE
R Dorsal Cuboideavicular Ligament GEODE
R Dorsal Tarsometatarsal Ligament 5 GEODE
R Elbow Capsule GEODE
R F Distal JointCapsule 1 GEODE
R Fibular Collateral Bursa GEODE
R Fibular Collateral Ligament GEODE
R Fibular Ligament GEODE""".split('\n')

qs="""R De Me Li
Fi Colla
bow ODE""".split('\n')

for q in qs:
    print "results for '{}':".format(q)
    matches=difflib.get_close_matches(q,s,3,0.3)
    for i,e in enumerate(matches,1):
        print "\t{}. {}".format(i,e)

rawDescription: regular expression: search multiple words with multiple parts

description: Regular expression: search multiple words with multiple parts


code:
lists = ['A', 'B', 'C', 'D']
nos = [4, 4, 1, 1]

for idx, ln in enumerate(zip(lists,nos)):
    l, n = ln[0], ln[1]
    in_nos = range(1, n+1)
    for indx, in_no in enumerate(in_nos):
        out_no = sum(nos[0:+idx])+indx+1
        print out_no   ##The result should be HERE

rawDescription: A simple list simulation in python

description: A simple list simulation in python


rawDescription: Is it possible to call a Python module from ObjC?


code:
import argparse

def foo(parsed_args):
    print "woop is {0!r}".format(getattr(parsed_args, 'woop'))

def bar(parsed_args):
    print "moop is {0!r}".format(getattr(parsed_args, 'moop'))

parser = argparse.ArgumentParser()

parser.add_argument('--foo', dest='action', action='store_const', const=foo)
parser.add_argument('--bar', dest='action', action='store_const', const=bar)
parser.add_argument('--woop')
parser.add_argument('--moop')

parsed_args = parser.parse_args()
if parsed_args.action is None:
    parser.parse_args(['-h'])
parsed_args.action(parsed_args)

rawDescription: Use argparse to run 1 of 2 functions in my script


code:
f = Fruit('Apple', True, False)
# Or, if you already have a list with the values
params = ['Apple', True, False]
f = Fruit(*params)

print f.seed

rawDescription: Breaking down list of list into dictionaries


code:
import pandas as pd
#df = pd.read_csv('file.csv', dtype={'area': str, 'phone': str})
df=pd.DataFrame(columns=['phone','name','area'],
                data=[['(444) 444-4444, (000) 000-0000', 'Foo', 'cityname, ST'],
                      ['(555) 555-5555',   'Bar', 'othercity, SN']])
print df
df['State'] = df.area.apply(lambda x: x.split(',')[1] if len(x.split(','))>1 else '')
df.area = df.area.apply(lambda x: x.split(',')[0])
df.phone = df.phone.apply(lambda x: x.split(',')[0])
print df

rawDescription: Simple Column split in Pandas

description: Simple Column split in Pandas


code:
import numpy
def BarGraphify3(nums):
    '''
    >>> BarGraphify3([1,2,3])
      *
     **
    ***
    >>> BarGraphify3([1,3,2])
     *
     **
    ***
    '''
    grid = []
    maxVal = max(nums)
    for n in nums:
        space = (maxVal-n)*' '
        asterisks = n*'*'
        grid.append(list(space + asterisks))

    cols = []
    for row in numpy.transpose(grid):
        cols.append(''.join(row))
    print '\n'.join(cols)

rawDescription: Bar Making - Python

description: Bar Making - Python


code:
VariableDefinition = re.compile(r'<%(["\']?)(.*?)\1=(["\']?)(.*?)\3%>', re.LOCALE)
VarRepl = re.compile(r'<%(["\']?)(.*?)\1%>', re.LOCALE)

def processVariables(item):
    vars = {}
    def findVars(m):
        vars[m.group(2).upper()] = m.group(4)
        return ""

    item = VariableDefinition.sub(findVars, item)
    return VarRepl.sub(lambda m: vars[m.group(2).upper()], item)

print processVariables('<%"TITLE"="This Is A Test Variable"%>The Web <%"TITLE"%>')

rawDescription: I'm using Python regexes in a criminally inefficient manner


code:
Demonstration
print f(df)

rawDescription: Operating on pandas dataframes that may or may not be multiIndex


code:
def weight_graph(request):
    print 'weight graph'
    x_axis_list = ['Day 1', 'Day 2', 'Day 3']
    series = [{
            'name': 'Tokyo',
            'data': [7.0, 1.0, 9.5]
        }, {
            'name': 'New York',
            'data': [-0.2, 0.8, 5.7]
        }]

    highchart = LineHighChart(
                              'my chart',
                              'last seven days',
                              x_axis_list,
                              'Weight',
                              ' lbs',
                              series
                              )

    return JsonResponse(highchart.__dict__, safe=False)

rawDescription: populate a class variable with the value of another class variable

description: Populate a class variable with the value of another class variable


code:
d = {}
for k, v in my_dict.items():
    for char in v:
        d.setdefault(char, []).append(k)
print {v[0]:k for k, v in d.items() if len(v) == 1}

rawDescription: Python - Find characters in list of strings that are not duplicates


code:
pqueue = PQueue()

def pqueue_server(sock, addr):
    text = sock.recv(1024)
    cmds = text.split(' ')
    if cmds[0] == 'append':
        result = pqueue.append(cmds[1])
    elif cmds[0] == 'take':
        result = pqueue.take()
    elif cmds[0] == 'promote':
        result = pqueue.promote(int(cmds[1]))
    elif cmds[0] == 'demote':
        result = pqueue.demote(int(cmds[1]))
    else:
        result = ''
    sock.sendall(str(result))
    print 'Request:', text, '; Response:', str(result)

if args.listen:
    server = StreamServer(('127.0.0.1', 4040), pqueue_server)
    print 'Starting pqueue server on port 4040...'
    server.serve_forever()

rawDescription: How could a distributed queue-like-thing be implemented on top of a RBDMS or NOSQL datastore or other messaging system (e.g., rabbitmq)?


code:
ia = imdb.IMDb()
for rec in recs:
  movie = ia.get_movie(rec)
  print movie.movieID, movie.get('title')

rawDescription: IMDB suggestions

description: IMDB suggestions


code:
for i in range(len(entries)):
     print sql + entries[i].get()

rawDescription: printing a "for i in range" command

description: Printing a "for i in range" command


code:
class Example(object):
  def foo(self):
      print "foo"
a=Example()
a.foo()
def new_foo(self):
   Example.foo(self)
   print "new"
funcType = type(Example.foo)
a.foo = funcType(new_foo, a, Example)
a.foo()

rawDescription: How can i add behaviour to a instance of a class without changing the base class

description: Add behaviour to a instance of a class without changing the base class


code:
from lxml.html import fromstring, tostring

html = "
Here is some text
"

doc = fromstring(html)
img = doc.find('.//img')
parent = img.getparent()
parent.text = img.tail
doc.remove(img)

print tostring(doc)

rawDescription: Removing img tag in lxml

description: Removing img tag in lxml


code:
class Node(object):

    def __init__(self, next_node=None):
        self.next_node = next_node

    @property
    def next(self):
        return self.next_node

    def set_next(self, new_next):
        self.next_node = new_next
        return new_next

def loop_size(node):
    onestep = node
    twostep = node.next
    while(onestep != twostep):
        twostep = twostep.next.next
        onestep = onestep.next
    onestep = onestep.next
    size = 1
    while(onestep != twostep):
        size += 1
        onestep = onestep.next
    return size

def test_ll(tail, loop):
    head = Node()
    nodes = [head]
    for i in range(2, tail+loop+1):
        head = head.set_next(Node())
        nodes.append(head)
    nodes[-1].set_next(nodes[tail])
    size = loop_size(nodes[0])
    print "Tail: {}, Loop: {}, Size: {}".format(tail, loop, size)

test_ll(1, 3)
test_ll(999, 1000)
test_ll(1000, 999)
test_ll(50, 3)
test_ll(3, 3)
test_ll(3, 4)

rawDescription: Tail-Loop Puzzle with a Linked list - My Python solution is Failing when tail is longer than loop


code:
X = np.vstack([xx,yy])
V = np.cov(X.T)
VI = np.linalg.inv(V)
print np.diag(np.sqrt(np.dot(np.dot((xx-yy),VI),(xx-yy).T)))

rawDescription: Calculate Mahalanobis distance using NumPy only


code:
with open('two.txt') as f:
    lines = f.readlines() # get lines from second file

b = [] # new names

for line in lines: # iterate once for the new names
    if any(name in line for name in a):
        b.extend(re.findall(r'(?:Dr[.](\w+))', line))

a += b # add new names to original names

for line in lines: # iterate again to display results
    if any(name in line for name in a):
        print line

rawDescription: python reuse code, parse add to list and print match line

description: Python reuse code, parse add to list and print match line


code: [map(itemgetter(1), g) for k, g in groupby(enumerate(seq2), lambda (i,x):i-x)]

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


code:
#imports
command = input()#expecting "filter(...).last()" or so
cleanup(command) #remove ";", "eval()", escape parameter values, strip
command = "MyModel.objects." + command
try:
    result = eval(command)
    prettyprint(result)

except Exception as e:
    print get_traceback()
    print e.message

rawDescription: Allow admins to control Django ORM through a view


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

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:
import csv
import os
from itertools import izip_longest
import sys

try:
    filename = sys.argv[1]
except IndexError:
    print 'Please add a filename'
    exit(-1)
with open(os.path.splitext(filename)[0] + '_t.csv', 'wb') as outfile, open(filename, 'rb') as infile:
    a = izip_longest(*csv.reader(infile))
    csv.writer(outfile).writerows(a)

rawDescription: Transpose csv in python with different row lengths


code:
from lxml import etree

doc = etree.HTML("""

category 1

...

...

...

...

category 2

...

...

...

...

...

category 3

...

...

category 4

...

...

...

...

""")

category = None
for ele in doc.xpath('//h2|//div[contains(@class,"config-entry")]'):
  if ele.tag == 'h2':
    category = str(ele.text)
  else:
    if category:
      print "%s: %s, %r" % (category,ele.tag,ele.attrib)

rawDescription: Select siblings between two nodes

description: Select siblings between two nodes


code:
import numpy as np
import timeit

arr_1D=np.arange(5000,dtype=np.double)
arr_2D=np.arange(500**2,dtype=np.double).reshape(500,500)
arr_3D=np.arange(500**3,dtype=np.double).reshape(500,500,500)

print 'Summation test:'
print timeit.timeit('np.sum(arr_3D)',
                      'import numpy as np; from __main__ import arr_1D, arr_2D, arr_3D',
                      number=5)/5
print timeit.timeit('np.einsum("ijk->", arr_3D)',
                      'import numpy as np; from __main__ import arr_1D, arr_2D, arr_3D',
                      number=5)/5
print '----------------------\n'

print 'Power test:'
print timeit.timeit('arr_3D*arr_3D*arr_3D',
                      'import numpy as np; from __main__ import arr_1D, arr_2D, arr_3D',
                      number=5)/5
print timeit.timeit('np.einsum("ijk,ijk,ijk->ijk", arr_3D, arr_3D, arr_3D)',
                      'import numpy as np; from __main__ import arr_1D, arr_2D, arr_3D',
                      number=5)/5
print '----------------------\n'

print 'Outer test:'
print timeit.timeit('np.outer(arr_1D, arr_1D)',
                      'import numpy as np; from __main__ import arr_1D, arr_2D, arr_3D',
                      number=5)/5
print timeit.timeit('np.einsum("i,k->ik", arr_1D, arr_1D)',
                      'import numpy as np; from __main__ import arr_1D, arr_2D, arr_3D',
                      number=5)/5
print '----------------------\n'

print 'Einsum test:'
print timeit.timeit('np.sum(arr_2D*arr_3D)',
                      'import numpy as np; from __main__ import arr_1D, arr_2D, arr_3D',
                      number=5)/5
print timeit.timeit('np.einsum("ij,oij->", arr_2D, arr_3D)',
                      'import numpy as np; from __main__ import arr_1D, arr_2D, arr_3D',
                      number=5)/5
print '----------------------\n'

rawDescription: Why is numpy's einsum faster than numpy's built in functions?


code:
variable = "/users/me/folder/"
the_list = ["/users/me/folder/file1_001_134543_X5_6MGFS.txt", "/users/me/folder/file2_231_234233_Y5_6MGFFAS.txt"]

print [x.split(variable)[1].split('_')[0] for x in the_list]

rawDescription: Edit a list to remove a defined variable and everything after and including a character


code:
class X(object):
    @setter
    def my_property(self, value):
        self._my_property = value + 1

x = X()
x.my_property = 42
print x.my_property # 43

rawDescription: Defining setter in a shorter way

description: Defining setter in a shorter way


code:
def countLetters(str, ch):
#                      ^ the character you are looking for
    count=0
    index=0
    for ch in str:
#        ^ the string character you are trying to check
        if ch==str[index]:  # ???
            count=count+1
        index=index+1
    print count

rawDescription: Exercise 7.9 in "How to Think Like a Computer Scientist (python)" measuring occurrences of a character in a string


code:
for i, word in enumerate(tmpstr):
    print word, tmpstr[i+1]

rawDescription: The pythonic way of printing a value

description: The pythonic way of printing a value


code:
import zlib, base64
text = 'STACK OVERFLOW'
code =  base64.b64encode(zlib.compress(text,9))
print code

rawDescription: Compressing a file in python and keep the grammar exact when opening it again


code:
import re
pattern = re.compile(ur'(?ui)^\w+$')
pattern.search(u'斗牛')
pattern.search(u'斗牛?') is None

rawDescription: Regex - Expression for matching international characters doesn't work when I add ^ $


code:
#!/usr/bin/env python2.7

from pyparsing import *

# Erlang config file definition:
erlangAtom = Word( alphas + '_')
erlangString = dblQuotedString.setParseAction( removeQuotes )

erlangValue = Forward()
erlangList = Forward()

erlangTaggedTuple = Dict(Group(Suppress('{') + erlangAtom + Suppress(',') +
                           erlangValue + Suppress('}') ))
erlangDict = Suppress('[') + delimitedList( erlangTaggedTuple ) + Suppress(']')
erlangList <<= Suppress('[') + delimitedList( Group(erlangDict|erlangList) ) + Suppress(']')

erlangValue <<= ( erlangAtom | erlangString |
                  erlangTaggedTuple |
                  erlangDict| erlangList )

if __name__ == "__main__":
    working = """
[{foo,"bar"}, {baz, "bar2"}]
"""

    broken = """
[
    [{foo,"bar"}, {baz, "bar2"}],
    [{foo,"bob"}, {baz, "fez"}]
]
"""
    w = erlangValue.parseString(working)
    print w.dump()

    b = erlangValue.parseString(broken)
    print "b[0]:", b[0].dump()
    print "b[1]:", b[1].dump()

rawDescription: pyparsing: grammar for list of Dictionaries (erlang)

description: Pyparsing: grammar for list of Dictionaries (erlang)


code:
with open('datafile') as f:
    for line in f:
        numbers_float = map(float, line.split())
        print function(numbers_float[0],numbers_float[1],numbers_float[2])
        #shorthand:  print function(*numbers_float)

rawDescription: Reading multiple numbers from a text file


code:
import pandas as pd

df = pd.DataFrame({"data": ['a', 'b', 'c']})
df['col1'] = 1
df['col2'] = 2
print df

rawDescription: "Expanding" pandas dataframe by using cell-contained list


code:
import re
s = "87 foo 87 bar"
r = re.compile(r"87\s*")
s = r.sub('', s)
print s

rawDescription: Determining the unmatched portion of a string using a regex in Python


code:
from collections import OrderedDict
result = OrderedDict()
for row in reader:
   if row and row[0]:
      result[row[0]]=[i for i in row[1:] if i]

# print result
for key in result:
   print key,":" ,result[key]

rawDescription: Reading csv file in Python and create a dictionary


code:
import re
string = 'adverb with text verb text from here on'
print re.findall(r'adverb(.*?)verb', string)

rawDescription: Get pattern text that is within search string


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

dct = {}

for d in test:
    if d["id"] in dct:
        dct[d["id"]]["sum"] += d["sum"]
    else:
        dct[d["id"]] = d.copy()

from pprint import pprint as pp

pp(dct.values())

rawDescription: Python Sum dictionary elements (better pythonic way)


rawDescription: Python: string to a list of lists

description: Python: string to a list of lists


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:
for x in l:
    for el in x[8:]:
        print el

rawDescription: How to exclude some indexes in a python loop?

description: Exclude some indexes in a python loop


code:
import subprocess
p = subprocess.Popen( "perl start_workspace.perl", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write('echo "Python: $SOME_VAR"\n')
p.stdin.write("make\n")
(stdoutdata, stderrdata) = p.communicate()
print stdoutdata
print stderrdata

rawDescription: how to have command as input for a shell launched inside a python subprocess

description: Have command as input for a shell launched inside a python subprocess


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

rawDescription: Print sublists in python

description: Print sublists in python


rawDescription: Python - slice a list of list

description: Python - slice a list of list


code:
#!/usr/bin/python

lol = list()
marker = '--->'
with open('txt', 'r') as fh:
    mem = None
    lo = []
    for line in fh.readlines():
        k,v = line.strip().split(marker)
        k, v = [ x.strip() for x in [k,v]]
        if not mem or mem == k:
            lo.append((k,v))
            mem = k
        else:
            lol.append(lo)
            lo = [(k,v)]
            mem = k
    lol.append(lo)

for i in lol:
    k,v = zip(*i)
    print '%s%s %s' % (k[0],marker,' '.join(v))

rawDescription: grouping values for grammar generation in python

description: Grouping values for grammar generation in python


code:
foodweb = {}

with open('AquaticFoodWeb.txt') as f_input:
    for line in f_input:
        animal, prey = line.strip().split(' eats ')
        foodweb.setdefault(animal, []).append(prey)

print "Predators and Prey:"

for animal, prey in sorted(foodweb.items()):
    if len(prey) > 1:
        print "{} eats {} and {}".format(animal, ", ".join(prey[:-1]), prey[-1])
    else:
        print "{} eats {}".format(animal, ", ".join(prey))

rawDescription: Creating a dictionary from txt file Python


code:
x = -16
ans = -1
ans2 = 1
while (ans*ans2 > x):
    ans = ans - 1
    ans2 = ans2 + 1    # changed ans with ans2
print ans, ans2

rawDescription: How do I find the square root of the negative integer -16 or the numbers that it is a multiple of with a while loop

description: Find the square root of the negative integer -16 or the numbers that it is a multiple of with a while loop


code:
class CCollection(object):
  def get_children_strings(self):
    list_of_strings = []
    for attr_name in dir(self):
      if attr_name not in dir(CCollection()):
        attr = getattr(self, attr_name)
        if hasattr(attr, 'get_children_strings'):
          list_of_strings.extend(["." + attr_name + child_string for child_string in attr.get_children_strings()])
        else:
          list_of_strings.append("." + attr_name + " = " + str(attr))
    return list_of_strings

  def print_tree(self, prefix):
    print [prefix + s for s in self.get_children_strings()]

rawDescription: Printing all the attributes in a class

description: Printing all the attributes in a class


code:
def test_append(self):
        start_stats = {'step':250,'offset':13,'inc':113,'danger':0,'input':None}
        gen = StatGenerator(start_stats)
        table = PathTable(gen)

        stats_list = []

        for i in xrange(50):
            stats_list.append(table._row_gen.stats.copy()) # <=== the copy call makes a new copy and solves your problem.
            print stats_list[i]
            table._row_gen.next()

        assert stats_list[0]['step'] == 250

rawDescription: How to stop elements in list from changing?

description: Stop elements in list from changing


code:
sampleguid.append(allguid[1:5])

# for each item, let's call it guid, in the list sampleguid
# do the following
for guid in sampleguid:
  full_url = url + "?" + str(guid)
  print full_url

rawDescription: List to string and for loop

description: List to string and for loop


code:
for index, current_number in enumerate(np.array([0,0,1,1,1,0])):
    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: Python Spaced Triangle

description: Python Spaced Triangle


rawDescription: Replace URL with a link using regex in python


code:
data = [
    'byte string',
    u'unicode string',
    'this byte string has unicode escapes: \\u2122\\u00e6',
    u'this unicode string has non-ascii chars: ©æ™ä',
]

for s in data:
    print repr(s), type(s)
    if isinstance(s, str):
        s = s.decode('unicode-escape')
    z = s.encode('utf8')
    print repr(z), z
    print

rawDescription: Python 2.7: Back to utf-8 after using decode unicode-escape


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

description: Improve the speed of my selection process, python


code:
def hello():\
    print "hello"\
\
\
\

hello()
hello()\
\
\

rawDescription: How do I create a line-break in Terminal?

description: Create a line-break in Terminal


code:
import sys,time
a=1
b=10
while a<b:
    print "yes",
    sys.stdout.flush()
    time.sleep(1)
    a=a+1

rawDescription: Python sleep() inhibit print with comma?

description: Python sleep() inhibit print with comma


code:
A = soup.findAll('strong',{'class':'name fn'})  # Notice there is no [0] slice notation here.
for B in A:
    print B.renderContents()

rawDescription: pls help - error message "no attribute 'renderContents'"

description: Pls help - error message "no attribute 'renderContents'"


code:
import numpy as np

x = np.array([1,2,3,4,5])
y = np.array([100,200,300,400,500])

# b contains true when corresponding value of x is outside 2 < x < 4
b = np.ma.masked_outside(x, 2, 4).mask

# x2 originates from x, but values 2 < x < 4 are stripped (according to the boolean variables contained in b), the same is done with y2
x2 = x[~b]
y2 = y[~b]

print 'x2', x2
print 'y2', y2

rawDescription: select a specific range from a numpy 2d array to plot values

description: Select a specific range from a numpy 2d array to plot values


code:
def __get__(self, obj, objtype):
    print 'Retrieving', self.name
    if obj is None:  # accessed as class attribute
        return self  # return the descriptor itself
    else:  # accessed as instance attribute
        return self.val  # return a value

rawDescription: Inspecting data descriptor attributes in python


code:
cookies = msg.getheaders('Set-Cookie')
for cookie in cookies:
    if cookie.startswith('PREF='):
            print 'Got PREF: ', cookie
    else:
            print 'Got another: ', cookie

rawDescription: Using mimetools.Message in urllib2.urlopen


code:
def bycol_decl(lod, keylist):
    return  [[row[key] for row in lod] for key in keylist]

if __name__ == "__main__":
    lod = [
        {'a':1, 'b':2, 'c':3},
        {'a':4, 'b':5, 'c':6},
        {'a':7, 'b':8, 'c':9},
        ]
    keylist = ['a', 'b', 'c']
    print bycol_decl(lod, keylist)

rawDescription: How to convert list of dictionaries into list of lists

description: Convert list of dictionaries into list of lists


code:
num_students = int(raw_input("Please enter number of students:"))
print "you entered %s students" %num_students
student_info = {}
student_data = ['Math marks : ', 'Physics marks : ', 'Chemistry marks : ']
for i in range(0,num_students):
    student_name = raw_input("Name :")
    student_info[student_name] = {}
    for entry in student_data:
        student_info[student_name][entry] = int(raw_input(entry)) #storing the marks entered as integers to perform arithmetic operations later on.
#print student_info
print"Please enter student name ?"
name = raw_input("Student name : ")
if name in student_info.keys():
    print "Average student marks : ", str(sum(student_info[name].values())/3.0)
else:
    print"please enter valid name"

rawDescription: How to store multiple entries for each student using dictionaries

description: Store multiple entries for each student using dictionaries


code:
## Creating the results list
results = list()

## Casting the string input to int()
amount_of_numbers = int(input("Please enter the amount of numbers: "))

## While lenght of results is lower than the provided amount of numbers
while len(results) < amount_of_numbers:

    ## Casting the string input to int() since we want to store it as an integer and not as a string
    userNumber = int(input("Please input a number: "))

    ## If the input number is not already in results list
    if userNumber not in results:
        ## Appending the input number to results list
        results.append(userNumber)
    else:
        ## Printing a message saying that the number already exists in results list
        print "{} has already been entered".format(userNumber)
## Printing results list
print (results)

rawDescription: Python: How to warn user of duplicate value in list


code:
import win32api
from StringIO import StringIO

datfile = StringIO( win32api.LoadResource(0, u'PROGRAM_DAT', 1))
print datfile.getvalue()

secrets = StringIO( win32api.LoadResource(0, u'CLIENT_SECRETS', 2))
print secrets.getvalue()

rawDescription: py2exe - Create single executable file out of a folder

description: Py2exe - Create single executable file out of a folder


code:
import csv
with open('test.csv', 'rb') as myFile:
    reader = csv.reader(myFile, delimiter=',', quotechar='|')
    for row in reader:
            print row[4] + ' 1:' + row[0] + ' 2:' + row[1] + ' 3:' + row[2] + ' 4:' + row[3]

rawDescription: How can I manipulate data using python?

description: Manipulate data using python


code:
class InfiniteDict(defaultdict):
   def __init__(self):
      defaultdict.__init__(self, self.__class__)

class Counters(InfiniteDict):
   def __init__(self):
      InfiniteDict.__init__(self)
      self.count = 0
      self.total = 0

   def show(self):
      print "%i out of %i" % (self.count, self.total)

rawDescription: Infinitely Nested Dictionary in Python

description: Infinitely Nested Dictionary in Python


code:
def dead(why):
    print why, "Good job!"
    exit(0) #this ends the program (therefor ends loop)

rawDescription: Comprehension difficulties regarding Boolean operators and whileloop


code:
x = open('yourtextfile/text.txt', 'r')
longlist = [line.split(',') for line in x.readlines()]
shortlist = ['D', 'HT', 'LT']
longlist = shortlist[:]
print longlist

rawDescription: How can I use the replace function to change the headers in a .txt file?

description: Use the replace function to change the headers in a .txt file


code:
import inspect
exec inspect.getsource(aaaa.bbbb)

# Now you have your own bbbb function defined in your module.

rawDescription: globals() of imported functions

description: Globals() of imported functions


code:
for num,name in zip(num_list,name_list):
    print str(num) + '\t' + name

rawDescription: Create a dictionary from two lists and then print the data in python


code:
import urlparse

parsed = urlparse.urlparse(data_hovercard)
parsed_dict = urlparse.parse_qs(parsed.query)
hovercard_id = parsed_dict['id']

print hovercard_id
#['671948073']

rawDescription: Select multiple values inside HTML tags with BeautifulSoup


code:
valid = {'Temp': [10, 55], 'rain_percent': [49, 100], 'humidity': [30,50]}
data = {'Temp': 30.45, 'rain_percent': 80.56, 'humidity': 70 }

for key,val in data.items():
    min, max = valid[key]
    if not( min <= val <= max ):
       print "%s=%g is out of valid range (%g-%g)" % (key, val, min, max)
    else:
       print "%s=%g is in the valid range (%g-%g)" % (key, val, min, max)

rawDescription: Comparing elements of one dictionary to ranges of values in another dictionary


code:
import matplotlib.pyplot as plt
from random import randint
from time import time

DATA = [
    (i, randint(5,30), randint(5,30), randint(30,35), randint(1,5)) \
    for i in xrange(1, 401)
]

def mapValues(group):
    ind, open_, close, high, low = group
    if open_ > close: # if open is higher then close
        height = open_ - close # heigth is drawn at bottom+height
        bottom = close
        yerr = (open_ - low, high - open_)
        color = 'r' # plot as a white barr
    else:
        height = close - open_ # heigth is drawn at bottom+height
        bottom = open_
        yerr = (close - low, high - close)
        color = 'g' # plot as a black bar

    return (ind, height, bottom, yerr, color)

#
# Test 1
#
def test1():
    fig = plt.figure()
    ax = fig.add_subplot(111)

    data = map(mapValues, DATA)

    start = time()

    for group in data:

        ind, height, bottom, yerr, color = group

        ax.bar(left=ind, height=height, bottom=bottom, yerr=zip(yerr),
                color=color, ecolor='k', zorder=10,
                error_kw={'barsabove': False, 'zorder': 0, 'capsize': 0},
                alpha=1)

    return time()-start

#
# Test 2
#
def test2():
    fig = plt.figure()
    ax = fig.add_subplot(111)

    # plotData can be serialized
    plotData = zip(*map(mapValues, DATA))

    ind, height, bottom, yerr, color = plotData

    start = time()

    ax.bar(left=ind, height=height, bottom=bottom, yerr=zip(*yerr),
            color=color, ecolor='k', zorder=10,
            error_kw={'barsabove': False, 'zorder': 0, 'capsize': 0},
            alpha=1)

    return time()-start

def doTest(fn):
    end = fn()
    print "%s - Sec: %0.3f, ms: %0d" % (fn.__name__, end, end*1000)

if __name__ == "__main__":
    doTest(test1)
    doTest(test2)

    # plt.show()

rawDescription: Caching Matplotlib with Memcache (Wont Pickle)


code:
data = yaml.load(yaml_str, Loader=yaml.RoundTripLoader)
print yaml.dump(data, Dumper=yaml.RoundTripDumper)

rawDescription: C# parse and change strings in yaml

description: C# parse and change strings in yaml


code:
csv = open("mat.csv")

columns = csv.readline().strip().split(',')[1:]
for i, line in enumerate(csv):
    tokens = line.strip().split(',')
    row = tokens[0]
    for column, cell in zip(columns[i:],tokens[i+1:]):
        print '{},{},{}'.format(row,column,cell)

rawDescription: Python - Convert a matrix to edge list/long form


code: print '\n'.join(str(i) for i in Ticket.objects.get_ticket_available_product('2011-12-29', 5, 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:
for row in reader:
    if row[1] not in ol:
        print row[0],row[1],row[2]

rawDescription: match word list from a file and output remaining words

description: Match word list from a file and output remaining words


code:
cur.execute('select * from ' + tablename1)
for row in cur:
    print row

rawDescription: SQLite3, Python: fetchone() works on table1 but not table2 but fetchall() works on both


code:
t = [
    ["a1","a2"],
    ["a1","a3"],
    ["a1","ax"],
    ["ax","b1"],
    ["ax","bx"],
    ["bx","b3"]
]
class MyGraph:
    def __init__(self,adjacency_table):
        self.table = adjacency_table
        self.graph = {}
        for from_node,to_node in adjacency_table:
            if from_node in self.graph:
                self.graph[from_node].append(to_node)
            else:
                self.graph[from_node] = [to_node]
        print self.graph
    def find_leaves(self,v):
        seen = set(v)
        def search(v):
            for vertex in self.graph[v]:
                if vertex in seen:
                    continue
                seen.add(vertex)
                if vertex in self.graph:
                    for p in search(vertex):
                       yield p
                else:
                    yield vertex
        for p in search(v):
            yield p

print list(MyGraph(t).find_leaves("a1"))#,"a1")

rawDescription: Python - Iterative self cross referencing


code:
from itertools import groupby
from operator import itemgetter
print groupby(my_numpy_array,key = itemgetter(1))

rawDescription: How to group rows in a numpy 2d matrix based on column values?

description: Group rows in a numpy 2d matrix based on column values


code:
for division in response.xpath('//div[@id="content"]//div[contains(@class, "mod-teams-list-medium")]'):
    division_name = division.xpath('.//div[contains(@class, "mod-header")]/h4/text()').extract()[0]
    print division_name
    print

    for team in division.xpath('.//div[contains(@class, "mod-content")]//li'):
        team_name = team.xpath('.//h5/a/text()').extract()[0]

        print team_name
    print "------"

rawDescription: xpath preceding child issue

description: Xpath preceding child issue


code:
for i, item in enumerate(items):
     print i, item

rawDescription: Python - for with counter

description: Python - for with counter


code:
for item in testlist:
    for number1, number2 in reflist:
        if number1 == item[1]:
            item.append(number2)
            break

print testlist
# [['asd', 7, 2], ['bla', 5, 948], ['hi', 3, 123]]

rawDescription: How to compare one value from a list of lists to another value from a list of lists in Python

description: Compare one value from a list of lists to another value from a list of lists in Python


code:
import numpy as np

# Generate some random data
x, y = np.random.random((2, 100))

# Your "U" and "V" arrays, but I'm changing the names for clarity
counts, bins = np.histogram(x, bins=20)

# Rather than iterate over an index, let's use a slightly different trick
for left, right in zip(bins[:-1], bins[1:]):
    # Use boolean indexing to replace the inner loop
    print y[(x > left) & (x < right)]

rawDescription: Arrays in loops

description: Arrays in loops


code:
for i, val in enumerate(range(600), start=1):
    if i % 50 == 0:
        print val

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 itertools
mylist = ['sub_0_a', 'sub_0_b', 'sub_1_a', 'sub_1_b']
for k,v in itertools.groupby(mylist,key=lambda x:x[:5]):
    print k, list(v)

rawDescription: Python split a list into subsets based on pattern


code:
#-------------------------------------------------------------------------------

import numpy
import sympy
import sympy.mpmath
import pprint
import Library_SympyExpressionToPythonFunction
import Library_GenerateChebyShevPolynomial

def Main(
    SympyExpression= None,
    DomainMinimumPoint= None,
    DomainMaximumPoint= None,
    ApproximationOrder= None,
    CheckArguments = True,
    PrintExtra = False,
    ):

    #Tsymb = sympy.Symbol('t')
    Xsymb = sympy.Symbol('x')
    DomainStart = DomainMinimumPoint[0]
    print 'DomainStart', DomainStart
    DomainEnd = DomainMaximumPoint[0]
    print 'DomainEnd', DomainEnd

    #Transform the coefficients and the result to be on arbitrary inverval instead of from 0 to 1
    DomainWidth = DomainEnd - DomainStart
    DomainCenter = (DomainEnd - DomainStart) / 2.
    t = (Xsymb*(DomainWidth) + DomainStart + DomainEnd) / 2.
    x = (2.*Xsymb - DomainStart - DomainEnd) / (DomainWidth)
    SympyExpression = SympyExpression.subs(Xsymb, t)

    #GET THE COEFFICIENTS:
    Coefficients = []
    for CoefficientNumber in range(ApproximationOrder):
        if(PrintExtra): print 'CoefficientNumber', CoefficientNumber

        Coefficient = 0.0
        for k in range(1, ApproximationOrder + 1):
            if(PrintExtra): print '  k', k

            CoefficientFunctionPart = SympyExpression.subs(Xsymb, sympy.cos( sympy.pi*( float(k) - .5 )/ float(ApproximationOrder) )  )
            if(PrintExtra): print '  CoefficientFunctionPart', CoefficientFunctionPart

            CeofficientCosArg = float(CoefficientNumber)*( float(k) - .5 )/ float( ApproximationOrder)
            if(PrintExtra): print '  ',CoefficientNumber,'*','(',k,'-.5)/(', ApproximationOrder ,') == ', CeofficientCosArg

            CoefficientCosPart      =   sympy.cos( sympy.pi*CeofficientCosArg )
            if(PrintExtra): print '  CoefficientCosPart', CoefficientCosPart

            Coefficient += CoefficientFunctionPart*CoefficientCosPart

        if(PrintExtra): print 'Coefficient==', Coefficient

        Coefficient = (2./ApproximationOrder)*Coefficient.evalf(10)

        if(PrintExtra): print 'Coefficient==', Coefficient

        Coefficients.append(Coefficient)

    print '\n\nCoefficients'
    pprint.pprint( Coefficients )

    #GET THE POLYNOMIALS:
    ChebyShevPolynomials = Library_GenerateChebyShevPolynomial.Main(
        ResultType = 'sympy',
        Kind= 1,
        Order= ApproximationOrder-1,
        ReturnAll = True,
        )

    print '\nChebyShevPolynomials'
    pprint.pprint( ChebyShevPolynomials )

    Result = 0.0 -.5*(Coefficients[0])
    for Coefficient, ChebyShevPolynomial in zip(Coefficients, ChebyShevPolynomials):
        Result += Coefficient*ChebyShevPolynomial

    #Transform the coefficients and the result to be on arbitrary inverval instead of from 0 to 1
    Result = Result.subs(Xsymb, x)

    return Result

rawDescription: Python Sympy Arbitrary Approximation to Arbitrary Sympy Expression?


code:
import timeit
def test_no_func():
    (x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19) = range(20)
    for i1 in xrange(100):
            for i2 in xrange(100):
                    for i3 in xrange(100):
                            results = [x0+x1+x2+x3+x4+x5+x6 for _ in xrange(100)]
                            results.extend(x7+x8+x9+x10+x11+x12+x13+x14+x15 for _ in xrange(100))
                            results.extend(x16+x17+x18+x19+x0 for _ in xrange(500))
    for j1 in xrange(100):
            for j2 in xrange(100):
                    for i3 in xrange(100):
                            results = [x0+x1+x2+x3+x4+x5+x6 for _ in xrange(100)]
                            results.extend(x7+x8+x9+x10+x11+x12+x13+x14+x15 for _ in xrange(100))
                            results.extend(x16+x17+x18+x19+x0 for _ in xrange(500))

def your_func(x_vars):
    # of the number is not too big you can simply unpack.
    # 150 is a bit too much for unpacking...
    (x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19) = x_vars

    results = [x0+x1+x2+x3+x4+x5+x6 for _ in xrange(100)]
    results.extend(x7+x8+x9+x10+x11+x12+x13+x14+x15 for _ in xrange(100))
    results.extend(x16+x17+x18+x19+x0 for _ in xrange(500))
    return results

def test_func():
    (x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19) = range(20)
    for i1 in xrange(100):
            for i2 in xrange(100):
                    for i3 in xrange(100):
                            results = your_func(val for key,val in locals().copy().iteritems() if key.startswith('x'))
    for j1 in xrange(100):
            for j2 in xrange(100):
                    for i3 in xrange(100):
                            results = your_func(val for key,val in locals().copy().iteritems() if key.startswith('x'))

print timeit.timeit('test_no_func()', 'from __main__ import test_no_func', number=1)
print timeit.timeit('test_func()', 'from __main__ import test_func, your_func', number=1)

rawDescription: Long argument lists and performance

description: Long argument lists and performance


code:
number = 123456789123456789
from timeit import timeit
print timeit("map(int, str(number))", "from __main__ import number")
print timeit("[int(dig) for dig in str(number)]", "from __main__ import number")

rawDescription: Write the digits of a number into an array


code:
import numpy

fieldTypes = ['i4', 'i4', 'f8', 'S4', 'S4', 'f8'] # data types of each field
fieldNames = ['a', 'b', 'c', 'd', 'e', 'f'] # names of the fields, feel free to give more descriptive names

myType = numpy.dtype(zip(fieldNames, fieldTypes)) # Create a numpy data type based on the types and fields

a = numpy.array([(1, 0, 0.25,'ind1', 'pop2', 0.56),
(2, 0, 0.35,'ind2', 'pop2', 0.58),
(1, 0, 0.23,'ind1', 'pop1', 0.66)], dtype=myType) # Create the array with the right dtype

print numpy.sort(a, order=['c']) # sort based on column 'c'

rawDescription: Sorting numpy mix-typed matrices

description: Sorting numpy mix-typed matrices


code:
for letter in word:
    if letter == 'e':
        continue
    print word

rawDescription: has_no_e() exercise from Think Python

description: Has_no_e() exercise from Think Python


rawDescription: Regular expression for matching latex

description: Regular expression for matching latex


code: print inspect.getdoc(Test.test_method)  # prints "I'm a docstring"

rawDescription: Aceess a methods docstring without instance, Python


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

def main():
    funcs = [square, cube]
    for r in range(5):
        value = map(lambda x: 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 re
import timeit

def timefunc(function, times, *args):
    def wrap():
        function(*args)
    t = timeit.Timer(wrap)
    return t.timeit(times) / times

def question(lines):
    eanic = "D41RP9"
    matcher = re.compile('^(?:'+eanic.strip()+'(?:;|$)|[^;]*;'+eanic.strip()+'(?:;|$))').match
    line=[next(l.split(';') for l in lines if matcher(l))]
    return line

def splitstart(lines):
    eanic = "D41RP9"
    ret = []
    for l in lines:
        s = l.split(';')
        if s[0].startswith(eanic) or s[1].startswith(eanic):
            ret.append(l)
    return ret

def simpler(lines):
    eanic = "D41RP9"
    matcher = re.compile('(^|;)' + eanic)
    return [l for l in lines if matcher.search(l)]

def better(lines):
    eanic = "D41RP9"
    matcher = re.compile('^(?:' + eanic + '|[^;]*;' + eanic + ')')
    return [l for l in lines if matcher.match(l)]

def strin(lines):
    eanic = "D41RP9"
    return [l for l in lines if eanic in l]

def mkdicts(lines):
    ean = {}
    plu = {}
    for l in lines:
        s = l.split(';')
        ean[s[0]] = s
        plu[s[1]] = s
    return (ean, plu)

def searchdicts(ean, plu):
    eanic = "D41RP9"
    return (ean.get(eanic, None), plu.get(eanic, None))

with open('test.txt', 'r') as f:
    lines = f.readlines()

    print "loaded", len(lines), "lines"
    print "question regex\t", timefunc(question, 10, lines)
    print "simpler regex\t", timefunc(simpler, 10, lines)
    print "other regex\t", timefunc(simpler, 10, lines)
    print "startswith\t", timefunc(splitstart, 10, lines)
    print "string in\t", timefunc(strin, 10, lines)
    print "dict construction\t", timefunc(mkdicts, 10, lines)
    ean, plu = mkdicts(lines)
    print "dict lookup\t", timefunc(searchdicts, 10, ean, plu)

rawDescription: Fastest way to find a re.compile match in list


code:
for key, group in groupby(things, lambda x: x[0]):
    listOfThings = " and ".join([thing[1] for thing in group])
    print key + "s:  " + listOfThings + "."

rawDescription: How do I use Python's itertools.groupby()?

description: Use Python's itertools.groupby()


code:
from bs4 import BeautifulSoup as bsoup
import requests as rq

url = "http://rads.stackoverflow.com/amzn/click/B00BRUQ7ZY"
r = rq.get(url)
soup = bsoup(r.content)

title = soup.find("span", id="btAsinTitle")
author = title.find_next("a", href=True)

print title.get_text()
print author.get_text()

rawDescription: Extracting the name of the author from an Amazon page using BeautifulSoup


code:
print 'Convert C-style from linear to multi'
x = box_index_C.reshape(-1,1)
bassign_rev_C = x / b_C % nbins

print 'Convert F-style from linear to multi'
x = box_index_F.reshape(-1,1)
bassign_rev_F = x / b_F % nbins

rawDescription: Numpy interconversion between multidimensional and linear indexing


code: print next(td for td in soup.find_all("td") if td.text.startswith('du'))

rawDescription: Beautiful Soup, searching by content of tag with tag in it


code: print x.rsplit('-', 1)[0]

rawDescription: how to get the last part of a string before a certain character?

description: Get the last part of a string before a certain character


code:
from Tkinter import *
import ImageTk
import time

def scrollToTop(imaget):
    print "I'm in scrollToTop()"
    canvas.move(imaget, 0, -1)
    t.after(1000, lambda:scrollToTop(imaget))

t = Tk()
canvas = Canvas(t,height=256,width=256)
canvas.pack()

arrows = [1]
arrows[0] = ImageTk.PhotoImage(file="arrow.bmp")
image = canvas.create_image(20,100,image=arrows[0],tags="token")
scrollToTop(image)

t.mainloop();

rawDescription: Can't move image with time delay

description: Can't move image with time delay


code:
from bs4 import BeautifulSoup

text = """

      .............................

     text1

     text2

     (222) 123 - 4567

     .........................

"""

soup = BeautifulSoup(text)

# Find divs with class "static" or "summary" and remove them using `extract`
div_nodes = soup.find_all('div', {'class': ['static', 'summary']})
[div.extract() for div in div_nodes]

print soup.text.split()

rawDescription: Strip out html elements using beautiful soup


code:
for name, score in most_common_values(scores, 3):
    print '{}: {}'.format(name, score)

rawDescription: N biggest values of dict with duplicates


code:
import ssl
from socket import *

settimeout(10)

urls = [i.strip().lower() for i in open("urls.txt")]
for urls in url:
    try:
        print ssl.get_server_certificate((url, 443))
    except (error, timeout) as err:
        print "No connection: {0}".format(err)

rawDescription: Adding timeout while fetching server certs via python


code:
import collections
def load_server_config():
        serverList = []
        config.read('deployment.cfg')
        orderedDic = collections.OrderedDict(config.items('nodes'))
        for node, ip in orderedDic.iteritems():
                serverList.append(node)
        print serverList
        return serverList

rawDescription: Python config parser array sort according to file content sequence


code:
def do_something(response):
    print response.status_code

unsentrequests=(grequests.get(u, hooks = {'response' : do_something}) for u in self.urls)
responses=grequests.map(unsentrequests)

rawDescription: Using grequests to send a pool of requests, how can I get the response time of each individual request?


code:
for alarm in alarms:
    for attr in whitelist:
        print getattr(alarm, attr)

rawDescription: get function names from a list python

description: Get function names from a list python


code: print lang.groupdict()

rawDescription: Wikipedia link parsing with regex

description: Wikipedia link parsing with regex


code:
lis=[u' ', u'Address :',u'Sadar Bazaar',u'new Delhi,India',u' ',u'Name :',u'Saun-Jean',u' ',u'Occupation :',u'Developer',u'Hacker',u' ']
strs=' '.join(str(x).strip() for x in lis if str(x).strip())
lis1=strs.split(':')
dic={}
for i,x in enumerate(lis1[:-1]):
    if x.strip():
        temp_lis=x.strip().split()
        if i+1 <len(lis1)-1:
            dic[temp_lis[-1]]=' '.join(lis1[i+1].split()[:-1])
        else:
            dic[temp_lis[-1]]=' '.join(lis1[i+1].split())

print dic

rawDescription: Parse list of certain Format

description: Parse list of certain Format


code:
dist, idx = tree.query(xy, k=8, eps=0, p=1)
columns = ["POINT_X", "POINT_Y", "Elev", "TEMP"]
px, py, elev, tmp = df[columns].values.T[:, idx, None]
tmp = np.squeeze(tmp)
one = np.ones_like(px)

m = np.concatenate((one, px, py, elev), axis=-1)
mtm = np.einsum("ijx,ijy->ixy", m, m)
mty = np.einsum("ijx,ij->ix", m, tmp)
b1,b2,b3 = np.linalg.solve(mtm, mty)[:, 1:].T

px, py, elev = px.squeeze(), py.squeeze(), elev.squeeze()

b1 = b1[:,None]
b2 = b2[:,None]
b3 = b3[:,None]

rdist = (1/dist)**2
t0 = tmp + b1*(xy[:,0,None]-px) + b2*(xy[:,1,None]-py) + b3*(dem[:,None]-elev)
outarr = (t0*rdist).sum(1) / rdist.sum(1)

print outarr

rawDescription: Do I underestimate the power of NumPy.. again?


code:
from mako.template import Template
t = Template("""
% if isinstance(a, int):
    I'm an int
% else:
    I'm a list
% endif
""")

print t.render(a=1)  # prints "I'm an int"
print t.render(a=[1,2,3,4])  # prints "I'm a list"

rawDescription: How to check instanceof in mako template?

description: Check instanceof in mako template


code:
a = "for y in range(2,20):print y"
exec a

rawDescription: Running a function from a string

description: Running a function from a string


code:
p = re.compile(r'^((?P<Given>\w+) (?P<Middle>\w\.) (?P<Family>\w+))$', re.MULTILINE)
str = "Jack A. Smith\nMary B. Miller"
print re.findall(p, str)

rawDescription: basic regex questions in python to help me learn it

description: Basic regex questions in python to help me learn it


code:
class MyClass(object):
    def meth_a(self):
        pass

    def meth_b(self):
        print "I'm meth_b"

rawDescription: How To Use The Pass Statement In Python

description: How To Use The Pass Statement In Python


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

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


code:
def tokengen():
  for c in 'abcdefghijklmnopqrstuvwyxz':
    yield c

def makeexpressiontree(s):
  d=dict()
  tokens = tokengen()
  while 1:
    simplesums=re.findall("\[([^\]\[]*)\]",s)
    if (len(simplesums) == 0):
      break
    for item in simplesums:
      t = tokens.next()
      d[t] = item
      s = s.replace("[%s]"% item,t)
  return d

def evaltree(d):
  """A simple dumb way to show in principle how to evaluate the tree"""
  result=0
  ev={}
  for i,t in zip(range(len(d)),tokengen()):
    ev[t] = eval(d[t],ev)
    result = ev[t]
  return result

s="[9+[7*3+[1+2]]-5]"
print evaluate(s)
tree=makeexpressiontree(s)
print tree
print evaltree(tree)

rawDescription: Storing and evaluating nested string elements


code:
with open('words.txt') as f:
    wordlist = [line.strip().lower() for line in f]

from itertools import permutations
while True:
    userinput = raw_input("Please enter your letters: ").lower()
    # look up any combinations of letters
    for letter_permutation in permutations(userinput):
        s = ''.join(letter_permutation)
        for word in wordlist:
            if word.startswith(s) or word.endswith(s):
                print "'%s' is part of %s" % (s, word)
    if raw_input('Press q to exit, any key to continue... ').lower() == 'q':
        break

rawDescription: Finding words that start/end with input letters (tiles) from the user in a file


code:
import pdb

def calc(i, n):
    j = i * n
    return j

def f(n):
    for i in range(n):
        j = calc(i, n)
        print i, j
    return

if __name__ == '__main__':
    pdb.set_trace()
    f(5)

rawDescription: what is the difference between "next" and "until" in pdb

description: What is the difference between "next" and "until" in pdb


code:
def convert():

    user_input = raw_input('From what do you want to convert?: ')

    if user_input == 'celsius':
        print 'To convert a temperature from Celsius to Fahrenheit:'
        cels = raw_input('CELSIUS: ')
        convert_to_fahrenheit(cels)

    elif user_input == 'farenheit':
        print 'To convert a temperature from Fahrenheit to Celsius:'
        fahr = raw_input('FAHRENHEIT: ')
        convert_to_celsius(fahr)

rawDescription: Simple Python Temperature Converter

description: Simple Python Temperature Converter


code:
import re
text = "^request^ #13#10#13#10^s100^GET http://facebook.com #13#10Host: http://facebook.com #13#10X-Online-Host: http://facebook.com #13#10X-Forward-Host: http://facebook.com #13#10Connection: Keep-Alive#13#10#13#10"
match = re.search(r"\^s(\d+)\^", text)
if match:
    print match.group(1)

rawDescription: Python - How to use regex to find a text?


code:
import numpy as np
import pyopencl as cl

ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
#The kernel uses one workitem per char transfert
prog_str = """kernel void foo(global char *in, global char *out, int size){
                  int idx = get_global_id(0);
                  if (idx < size){
                      out[idx] = in[idx];
                  }
           }"""
prog = cl.Program(ctx, prog_str).build()
#Note that the type of the array of strings is '|S40' for the length
#of third element is 40, the shape is 3 and the nbytes is 120 (3 * 40)
original_str = np.array(('this is an average string',
                         'and another one',
                         "let's push even more with a third string"))
mf = cl.mem_flags
in_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=original_str)
out_buf = cl.Buffer(ctx, mf.WRITE_ONLY, size=str_size)
copied_str = np.zeros_like(original_str)
#here launch the kernel with str_size number of workitems in this case 120
#this mean that some of the workitems won't process any meaningful char
#(not all string have a lenght of 40) but it's no biggie
prog.foo(queue, (str_size,), None, in_buf, out_buf, np.int32(str_size))
cl.enqueue_copy(queue, copied_str, out_buf).wait()
print copied_str

rawDescription: How to pass a list of strings to an opencl kernel using pyopencl?

description: Pass a list of strings to an opencl kernel using pyopencl


code:
from bs4 import BeautifulSoup

data = """

    Title: Point of Sale Threats Proliferate

    Severity: Normal Severity

    Published: Thursday, December 04, 2014 20:27

    Several new Point of Sale malware families have emerged recently, to include LusyPOS,..

    Analysis: Emboldened by past success and media attention, threat actors  ..

"""

soup = BeautifulSoup(data)
print ''.join(text.strip() for text in soup.p.find_all(text=True, recursive=False))

rawDescription: Extract text inside HTML paragraph using BeautifulSoup in Python


code:
servqual = raw_input(">").lower()
while servqual not in ("great", "good", "lacking", "poor"):
    print "I didn't understand that. Please try again."
    servqual = raw_input(">").lower()

rawDescription: Python: Repeating a function when raw_input is not valid


code:
#!/usr/bin/python

dc = {3:['3', '4'], 1:['2', '3'], 2:['3']}
de={}

def do_something(i,k):
    print "first time for '%s' with key '%s'" % (i,k)

def do_somethingelse(i,j,k):
    print "element '%s' seen %i times. Now with key '%s'" % (i,j,k)

for k in sorted(dc):
    for i in dc[k]:
        if i not in de:
            de[i]=1
            do_something(i,k)
        else:
            de[i]+=1
            do_somethingelse(i,de[i],k)

rawDescription: Dictionary Iteration Python

description: Dictionary Iteration Python


code:
import csv
reader = csv.reader(open("yourfile.csv", "rb"), delimiter=';')
for row in reader:
    print row

rawDescription: python split function

description: Python split function


code:
#!/usr/bin/env python

# use "with" to avoid having to close the file
with open("datafile.txt") as textFile:
    # get array containing all values in the file
    v = textFile.read().replace(',','').split()
    # extract the names into a separate tuple
    names = [tuple(filter(str.isalpha, v))]
    # make tuples out of each pair of values, replacing names with zeroes
    values = [(0,b) if a.isalpha() else (a,b) for a,b in zip(v[::2], v[1::2])]
    # concatenate the two
    values = names + values

print values

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


code:
def source(self, *dests):
    for i in xrange(int(1e4)):
        for dest in dests:
            dest.send(i)

def sink():
    while True:
        i = yield

def t3():
    snk = sink()
    snk.next() # activate the coroutine
    source(snk)

if __name__ == '__main__':

    from timeit import Timer
    n = 1000
    t = Timer("t3()", "from __main__ import source, sink, t3")
    print "%.2f usec/pass" % (1000000 * t.timeit(number=n)/n) # 872.99 usec/pass

rawDescription: Multiple clients for a Python generator?

description: Multiple clients for a Python generator


code:
def access_control(correct_pass="test", attempts=4):
    """Control access to the supplied function func."""
    for _ in range(attempts):
        password = raw_input("Password: ")
        if password == correct_pass:
            get_commands()
            break
        else:
            print "Incorrect password."
    else:
        print "Too many tries."

def get_commands():
    """Take user input until they enter 'exit'."""
    while True:
        command = raw_input("Command: ")
        if command == "exit":
            break

rawDescription: Break out of double while loops?

description: Break out of double while loops


code:
from collections import defaultdict

tr = {'critic' : '2_critic',
      'major' : '3_major',
      'all' : ['2_critic','3_major']}

scnd_dict = {'severity': ['all']}

third_dict = {}

for k, v in scnd_dict.iteritems():
    vals = []
    if isinstance(v, list):
        for i in v:
            vals.append(tr.get(i))
    else:
        vals.append(tr.get(v))
    if not vals:
        continue
    third_dict[k] = vals

print third_dict

rawDescription: Python : Match a dictionary value with another dictionary key


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

rawDescription: Append or create and new Dictionary

description: Append or create and new Dictionary


code:
from collections import defaultdict

sentences = ['Hi, friend', 'can you help me?']

start_sentence_map = defaultdict(list)
for sentence in sentences:
    start = sentence.split()[0]
    start_sentence_map[start].append(sentence)

for s in ["can", "you", "Hi,", "friend"]:
    print s,":",start_sentence_map.get(s)

rawDescription: match only exact word/string in python

description: Match only exact word/string in python


code:
combined_d = {key: value for d in mylist for key,value in d.iteritems() }
print combined_d["sort_order"]

rawDescription: Find a key's value from a list of dictionaries python


code:
#!/usr/bin/env python

import mmap
import cPickle

fname = '/tmp/pickle'
infile = open(fname, 'rb')
m = mmap.mmap(infile.fileno(), 0, access=mmap.ACCESS_READ)
start = 0
while True:
    end = m.find('.\n', start + 1) + 2
    if end == 1:
        break
    print cPickle.loads(m[start:end])
    start = end

rawDescription: Reading multiple Python pickled data at once, buffering and newlines?


rawDescription: Python: problems with csvwriter

description: Python: problems with csvwriter


code:
source
import requests, subprocess

def send_beacon(line):
    if line.startswith('ibeacon'):
          line = line.strip()
          a = line.split(' ')[1]
          a = line.split(' ')[2]
          a = line.split(' ')[3]
          a = line.split(' ')[4]
          a = line.split(' ')[5]
          payload = {'uuid': a, 'major': b, 'minor': c, 'power': d, 'rssi': e}
          r = requests.get('http://posttestserver.com/post.php', params=payload)

if __name__=='__main__':
    if 0:
        cmd = ['sudo', '/usr/local/bin/beacon', 'scan', '-b']
    else:                       # testing
        cmd = 'ping -c3 8.8.8.8'.split()
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    for line in iter(proc.stdout.readline, ''):
        print 'X:',line,
        send_beacon(line)

rawDescription: Start a new process and read that process's output


code:
from httpproxy.views import HttpProxy

class DynProxyView(HttpProxy):
    def get_object(self):
        return Fiddle.objects.get(pk=self.kwargs['pk'])
    rewrite = True
    @property
    def base_url(self):
        url= self.request.scheme+"://localhost:" + str(self.get_object().port)
        print url
        return url
    def get_full_url(self, url):
        result = super(DynProxyView, self).get_full_url(url)
        return result[:-1] # To get rid of a pesky redundant slash

rawDescription: Dynamically serving django docker containers


code:
from jinja2 import Environment

HTML = """
<html>
<head>
<title>{{ title }}</title>
</head>
<body>

Hello.

</body>
</html>
"""

def print_html_doc():
    print Environment().from_string(HTML).render(title='Hellow Gist from GutHub')

rawDescription: Replacing section of python string with a designated variable


code:
import urllib

f = [ ('eventName', 'myEvent'),
      ('eventDescription', 'cool event'),
      ('url', 'http://www.google.com')]

print 'POST&%s' % urllib.urlencode(f)

rawDescription: How do I URL encode this

description: URL encode this


code:
key = lambda (k, d): (not d['logins'], d['logins'], d['date_used'] if d['logins'] else d['date_added'])
pprint(sorted(sample.iteritems(), key=key, reverse=True))

rawDescription: Sort a dictionary with custom sorting function


code:
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()))
  print filter(lambda x: x in set1, set2)

rawDescription: Search between two files in Python

description: Search between two files in Python


code:
for i in range(1, 10):
    print "i =", i, ":",                   # Note the comma at the end
    for j in range(1, 10):
        print "{:2d}".format(i * j),       # Note the comma at the end
    print

rawDescription: Multiplication table for double digit numbers using nested loops in Python


code:
@event.listens_for(Comment, "after_insert")
def after_insert(mapper, connection, target):
    thread_table = Thread.__table__
    thread = target.thread
    connection.execute(
            thread_table.update().
             where(thread_table.c.id==thread.id).
             values(word_count=sum(c.word_count for c in thread.comments))
    )
    print "updated cached word count to", thread.word_count

rawDescription: Can SQLAlchemy events be used to update a denormalized data cache?


rawDescription: Calculate average of a column when two other columns values match


code:
print a915.values - a930.values
[-1.]

rawDescription: How do I subtract pandas columns that correspond to different time intervals in python?

description: Subtract pandas columns that correspond to different time intervals in python


rawDescription: Mixed length object type in pandas dataframe


code:
import numpy as np

def isPath(A, threshold):
    for i in range(len(A) - 1):
        print "Finding edges from layer", i, "to", i + 1, "..."
        diffs = np.array(A[i]).reshape((-1, 1)) - np.array(A[i + 1]).reshape((1, -1))
        reached = np.any(np.abs(diffs) <= threshold, axis = 0)
        A[i + 1] = [A[i + 1][j] for j in range(len(reached)) if reached[j]]
        print "Reachable nodes of next layer:", A[i + 1]
    return any(reached)

print isPath([[1, 3, 7], [10, 13], [13, 24]], 3)
print isPath([[1, 3, 7], [10, 13], [13, 24]], 10)

rawDescription: Python: Determine whether each step in path across n arrays falls below threshold value


code:
for i, v1 in enumerate(vect):
    for j in xrange(i + 1, len(vect)):
        a = np.array(v1)
        b = np.array(vect[j])
        space = np.linalg.norm(b - a)
        print space

rawDescription: How to avoid quadratic computation resulting from double 'for loop' when computing distances between vectors

description: Avoid quadratic computation resulting from double 'for loop' when computing distances between vectors


code:
import datetime
a = datetime.datetime(100,1,1,11,34,59)
b = a + datetime.timedelta(0,3) # days, seconds, then other fields.
print a.time()
print b.time()

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


code:
Python
recv_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
send_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
    os.remove(sys.argv[1])
except OSError:
    pass

recv_socket.bind(sys.argv[1])

recv_socket.listen(10)
conn, addr = recv_socket.accept()

send_socket.connect(sys.argv[2])

while 1:
   print "listening ..."
   data = conn.recv(4)
   p = struct.unpack('i',data)
   print 'received ', p

   if p is '9':
      break

   l = int(raw_input(">"))
   a = struct.pack('i', l)
   send_socket.sendall(a)
   print 'sent ', l

send_socket.close()
conn.close()
recv_socket.close()

rawDescription: back-and-forth unix domain sockets lock

description: Back-and-forth unix domain sockets lock


code:
import numpy as np

def shuffle(a, axis=-1):
    """
    Shuffle `a` in-place along the given axis.

    Apply numpy.random.shuffle to the given axis of `a`.
    Each one-dimensional slice is shuffled independently.
    """
    b = a.swapaxes(axis,-1)
    # Shuffle `b` in-place along the last axis.  `b` is a view of `a`,
    # so `a` is shuffled in place, too.
    shp = b.shape[:-1]
    for ndx in np.ndindex(shp):
        np.random.shuffle(b[ndx])
    return

def random_bits(a, n):
    b = (a[..., np.newaxis] > np.arange(n)).astype(int)
    shuffle(b)
    return b

if __name__ == "__main__":
    np.random.seed(12345)

    A = np.random.randint(0, 5, size=(3,4))
    Z = 6

    B = random_bits(A, Z)

    print "A:"
    print A
    print "B:"
    print B

rawDescription: quickly calculate randomized 3D numpy array from 2D numpy array

description: Quickly calculate randomized 3D numpy array from 2D numpy array


code:
def write_data(self, my_dict=None):
    my_dict = my_dict or self.bookmark_data['roots']
    for key, value in my_dict.items():
        if type(my_dict[key]) is dict:
            print my_dict[key]['name']
            for subkey, subvalue in my_dict[key].items():
                if subkey == "children" and len(my_dict[key][subkey]) <> 0:
                    for child in my_dict[key][subkey]:
                        self.write_data(child)
                else:
                    print subkey, ": ",  my_dict[key][subkey]

rawDescription: How can I dynamically build a path/reference to a nested dictionary in python

description: Dynamically build a path/reference to a nested dictionary in python


code:
import csv
import re

def get_values_flexibly(csv_path, keyword):
    def process(func):
        return set([func(cell)] + [func(row[index]) for row in reader])
    # Start fo real!
    kwd_remover = re.compile(
        r'(?:{kw})*\W*(?P<juicy_data>\w+)\W*(?:{kw})*'.format(kw=keyword)
    )
    result = []
    with open(csv_path, 'r') as f:
        reader = csv.reader(f)
        first_row = [kwd_remover.findall(cell)[0] for cell in reader.next()]
        print "Cleaned first_row: %s" % first_row
        for index, row in enumerate(reader):
            print "Before cleaning: %s" % row
            cleaned_row = [kwd_remover.findall(cell)[0] for cell in row]
            result.append(cleaned_row[1])
            print "After cleaning: %s" % cleaned_row
    return result

print "Result: %s" %  get_values_flexibly("sample.csv", 'data')

rawDescription: Skip the first column when reading a csv file Python


code:
import sys

def fmain():
    sys.stdout = open('output','a')
    fsub()
    sys.stdout = sys.__stdout__
    print 'Output of fsub():'
    print open('output').read(),
    # added the coma (,) to avoid a new line

rawDescription: How to catch the stdio from a python subroutine?

description: Catch the stdio from a python subroutine


code:
score_map = {'Tests' : 0, 'Assignments' : 0, 'Quizzes' : 0}
for subject in score_map:
    score_map[subject] = float(raw_input("What percentage do {} make up your grade \n".format(subject)))
# Now you can access each score
for subject in score_map:
    print "{0} makes up {1} percent".format(subject, score_map[subject])

rawDescription: How to Iterate through a list and create/save variables


code:
def segment(string,wset):
    """Segments a string into words prefering longer words givens
    a dictionary wset."""
    # Sort wset in decreasing string order
    wset.sort(key=len, reverse=True)
    result = tokenize(string, wset, "")
    if result:
        result.pop() # Remove the empty string token
        result.reverse() # Put the list into correct order
        return result
    else:
        raise Exception("No possible segmentation!")

def tokenize(string, wset, token):
    """Returns either false if the string can't be segmented by
    the current wset or a list of words that segment the string
    in reverse order."""
    # Are we done yet?
    if string == "":
        return [token]
    # Find all possible prefixes
    for pref in wset:
        if string.startswith(pref):
            res = tokenize(string.replace(pref, '', 1), wset, pref)
            if res:
                res.append(token)
                return res
    # Not possible
    return False

print segment("thisisinsane", ['this', 'is', 'in', 'insane']) # this is insane
print segment("shareasale", ['share', 'a', 'sale', 'as'])     # share a sale
print segment("asignas", ['as', 'sign', 'a'])                 # a sign as

rawDescription: Text segmentation: Algorithm to match input with the longest words from the dictionary


rawDescription: Python: decoding a string that consists of both unicode code points and unicode text


code:
import os
path = "c:\\Python27\\test"

for name in os.listdir(path):
    if name.endswith('.txt'):
        fpath = os.path.join(path, name)
        with open(fpath) as fin:
            print fpath, 'opened'

rawDescription: how to iterate over all files in path?

description: Iterate over all files in path


rawDescription: 3D Array in Python with mutliple data types


code:
print '\n'.join(map(str,xyze))
print

for el in xyze:
    el[1] = map(float,el[1])

print '\n'.join(map(str,xyze))

rawDescription: How do you change a list of integers and strings into a list of integers and floats in python?

description: Change a list of integers and strings into a list of integers and floats in python


code:
for elem in myins.findall('{http://www.w3.org/2005/Atom}entry'):
    print elem.find('{http://www.w3.org/2005/Atom}title').text ## this will be the title
    print elem.find('{http://www.w3.org/2005/Atom}content').text ## this will be the content

rawDescription: Error while parsing xml in Python

description: Error while parsing xml in Python


code:
dic1 = {'A': 25, 'B': 41, 'C': 32}
dic2 = {'A': 21, 'B': 12, 'C': 62}
result = {}
for key in (dic1.viewkeys() | dic2.keys()):
    if key in dic1: result.setdefault(key, []).append(dic1[key])
    if key in dic2: result.setdefault(key, []).append(dic2[key])

print result

rawDescription: Combining two dictionaries into one with the same keys?


code:
import numpy as np
import numba as nb

def func(a, p):
    a = np.asarray(a)
    n = np.arange(0, 10**p)
    a = np.append(a, n**3 + n**2)
    return a

@nb.jit
def func2(a, p):
    a = np.asarray(a)
    n = np.empty(10**p, dtype=np.float64)
    for k in range(10**p):
        n[k] = k*k*(k + 1)

    return np.append(a, n)

p = 6
e = []
E = func(e, p)
E2 = func2(e, p)
print np.allclose(E, E2)

rawDescription: Numba autojit function slower than vectorized Numpy method


code:
class MyFunc(object):
    funcs = []
    def __init__(self, func):
        self.funcs.append(func)

@MyFunc
def foo():
    return 5

@MyFunc
def bar():
    return 10

def quux():
    # Not decorated, so will not be in MyFunc
    return 20

for func in MyFunc.funcs:
    print func()

rawDescription: Is this a good approach to execute a list of operations on a data structure in Python?


code:
# graph is in adjacent list representation
graph = {
        '1': ['2', '3', '4'],
        '2': ['5', '6'],
        '5': ['9', '10'],
        '4': ['7', '8'],
        '7': ['11', '12']
        }

def bfs(graph, start, end):
    # maintain a queue of paths
    queue = []
    # push the first path into the queue
    queue.append([start])
    while queue:
        # get the first path from the queue
        path = queue.pop(0)
        # get the last node from the path
        node = path[-1]
        # path found
        if node == end:
            return path
        # enumerate all adjacent nodes, construct a new path and push it into the queue
        for adjacent in graph.get(node, []):
            new_path = list(path)
            new_path.append(adjacent)
            queue.append(new_path)

print bfs(graph, '1', '11')

rawDescription: How to trace the path in a Breadth-First Search?

description: Trace the path in a Breadth-First Search


code:
from dateutil import tz

print dt.astimezone(tz.tzutc()).isoformat(' ')

rawDescription: Python: time conversion

description: Python: time conversion


rawDescription: Compare values of two arrays in python

description: Compare values of two arrays in python


code: print varBinds[0][1]

rawDescription: Getting Data from an SNMP port in Python


code:
import urllib2
from TorCtl import TorCtl

proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})
opener = urllib2.build_opener(proxy_support)

def newId():
    conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="your_password")
    conn.send_signal("NEWNYM")

for i in range(0, 10):
    print "case "+str(i+1)
    newId()
    proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})
    urllib2.install_opener(opener)
    print(urllib2.urlopen("http://www.ifconfig.me/ip").read())

rawDescription: How to change Tor identity in Python?

description: Change Tor identity in Python


code:
text = """
EZSTORAGE - PACK IT. STORE IT. WIN -  Nationwide - Restrictions - Ends 6/30/15
"""
from bs4 import BeautifulSoup

soup = BeautifulSoup(text)

for link in soup.find_all('a', id='mylink'):
    link_text = link.text
    print link_text

rawDescription: Extracting text between link tags using BeautifulSoup in Python


code:
from subprocess import Popen, PIPE

# call subprocess
# pass the serial object to subprocess
# read out serial port

# HOW TO PASS SERIAL OBJECT HERE to stdin
p1 = Popen(['python', './ReadCOM.py', "COM1", "9600"], stdin=PIPE, stdout=PIPE, stderr=PIPE) # read COM1 permanently
p2 = Popen(['python', './ReadCOM.py', "COM2", "9600"], stdin=PIPE, stdout=PIPE, stderr=PIPE) # read COM2 permanently

for i in range(10):
    print "received from COM1: %s" % p1.stdout.readline() # print output from ReadCOM.py for COM1
    print "received from COM2: %s" % p2.stdout.readline() # print output from ReadCOM.py for COM2

rawDescription: Python Pyserial read data form multiple serial ports at same time


code:
dict_content = {'Company':content[0::5], 'Summary':content[1::5], 'Location':content[2::5], 'Year':content[3::5], 'Followers':content[4::5]}
print dict_content

rawDescription: Making columns with data in other column in Pandas


code:
with open("testdata.xls") as inf:
    next(inf)
    lines = [l[:-1].split("\t") for l in inf]

date_array_list = [l[0] for l in lines]
values_matrix = [map(int, l[1:]) for l in lines]

print date_array_list
print values_matrix

rawDescription: How to separate data imported as text in Python into datearray and numeric data

description: Separate data imported as text in Python into datearray and numeric data


code:
print "A set of paired likes and dislikes"
pprint(zip(likes,dislikes),indent=4)

rawDescription: Parsing a pretty printed table into Python objects


code:
import os,shutil
root="/home"
destination="/tmp"
directory = os.path.join(root,"mydir")
os.chdir(directory)
for file in os.listdir("."):
    flag=""
    #check contents of file ?
    for line in open(file):
       if "something" in line:
           flag="found"
    if flag=="found":
       try:
           # or use os.rename() on local
           shutil.move(file,destination)
       except Exception,e: print e
       else:
           print "success"

rawDescription: How to check contents of a folder using Python

description: Check contents of a folder using Python


code:
for c in s.cat.categories:
    print c

a
b
c

rawDescription: printing list of categories as a column

description: Printing list of categories as a column


code:
import time
from multiprocessing.dummy import Pool

def run(i):
    print "Going to sleep..."
    time.sleep(i)
    print "Slept .."

p = Pool(3)  # 3 threads in the pool

p.map(run, range(5)) # Calls run(i) for each element i in range(5)

p.close()
p.join()

print "done!!"

rawDescription: How to handle multiple jobs in a queue with fixed number of threads in Python

description: Handle multiple jobs in a queue with fixed number of threads in Python


code:
from prettytable import PrettyTable

# Initialize the object passing the table headers
t = PrettyTable(['A', 'B', 'C'])

t.align='l'
t.border=False

t.add_row([111,222,333])
t.add_row([444,555,666])
t.add_row(['longer text',777,'even longer text here'])

print str(t)

rawDescription: Align column names with data in a CSV file using Python


code:
data = [['Day','Date','Week'], ['Day','Date','Week'], ['Day','Date','Week']]
print data[0]

rawDescription: Is it possible to display only first list element in for loop?


code:
from functools import partial
def f(a,b,c):
    print a,b,c
bound_f = partial(f,1)
bound_f(2,3)

rawDescription: Python alternative to Javascript function.bind()?


code:
def removestudent(elem,l):
   try :
    l.remove(elem)
    return l
   except ValueError :
    print 'The element is not in list please enter a valid element !!'

removestudent(input("insert the user you wish to remove: "),l)

rawDescription: Python- Function which deletes an element from a list by name


code:
class X:
    def __init__(self):
        self.prop1 = "prop1"

    def __iter__(self):
        yield 1
        yield 2
        yield 3

    def method(self):
        print "method 1"

class Y:
    def __init__(self):
        self.prop2 = "prop2"

    def __iter__(self):
        yield (1,2)
        yield (3,4)
        yield (5,6)

    def another_method(self):
        print "method 2"

class FirstInTuple:
    def __init__(self, obj):
        self.obj = obj

    def __getattr__(self, attr):
        return getattr(self.obj, attr)

    def __iter__(self):
        for each in self.obj:
            if hasattr(each, '__getitem__'):
                yield each[0]
            else:
                yield each

if __name__ == "__main__":
    x = X()
    y = Y()
    f_x = FirstInTuple(x)
    f_y = FirstInTuple(y)

    for each in x:
        print each
    print
    for each in y:
        print each
    print
    for each in f_x:
        print each
    print
    for each in f_y:
        print each
    print
    print f_x.prop1
    f_x.method()
    print
    print f_y.prop2
    f_y.another_method()

rawDescription: Changing a python iterator while preserving the object


code:
def memoized(func):
   cache = {}
   def wrapper(*args):
      if not isinstance(args, collections.Hashable):
         # uncacheable. a list, for instance.
         # better to not cache than blow up.
         return func(*args)
      if args in cache:
         print 'get cached version{}'.format(args)
         return cache[args]
      else:
         print 'compute {}'.format(args)
         value = func(*args)
         cache[args] = value
         return value
   return wrapper
@memoized
def fibonacci(n):
   "Return the nth fibonacci number."
   if n in (0, 1):
      return n
   return fibonacci(n-1) + fibonacci(n-2)
fibonacci.__closure__
fibonacci.__closure__[1].cell_contents

rawDescription: How can I see the bytecode of a decorated function?

description: See the bytecode of a decorated function


code:
#right fql query, but prints only part of it
posts_query = "SELECT created_time, post_id, actor_id, type, updated_time, attachment FROM stream WHERE post_id in (select post_id from stream where ('video') in attachment AND source_id IN ( SELECT uid2 FROM friend WHERE uid1=me()) limit 100)"
users_query = "SELECT uid, first_name FROM user WHERE uid IN (SELECT actor_id FROM (#posts_query))"

queries = {'posts_query': posts_query, 'users_query': users_query}
fql_var = "https://api.facebook.com/method/fql.query?access_token=" + token['access_token'] + "&q=" + json.dumps(queries_json) + "&format=json"
data = urllib.urlopen(fql_var)
fb_stream = json.loads(data.read())
print ast.literal_eval(json.dumps(fb_stream))

rawDescription: How do I execute a multiquery fql statement?

description: Execute a multiquery fql statement


code:
from collections import Counter
from itertools import izip

with open('test.txt') as f:
    print [(x, Counter(x)) for x in izip(*(line.strip() for line in f))]

rawDescription: python split all lines in columns, no spaces in lines

description: Python split all lines in columns, no spaces in lines


rawDescription: numpy merge upper and lower triangular

description: Numpy merge upper and lower triangular


code:
patterns = {'quality':'Quality\=(\d+/\d+)',
            'level': 'level\=([+-]?\d+)',
            'key':'key\:(.+)',
            'channel':'Channel (.+)\)'}
body_of_text = open("somefile.txt").read()
results = dict([(key,re.search(regex,body_of_text).group(1)) for  key,regex in patterns.items()])
print results

rawDescription: Python regex match issue and grouping multiple groups