code:
import pylab as plt

# event definition
def ontype(event):
    if event.key == '1':
        print "1"
    elif event.key == '2':
        print "2"
    elif event.key == '3':
        print "3"

# main program
plt.plot([1,6,3,8,7])
plt.gcf().canvas.mpl_connect('key_press_event',ontype)
plt.show()

rawDescription: Matplotlib event and replotting

description: Matplotlib event and replotting


code: print hex(new_int)[2:]

rawDescription: How to convert a hex string to hex number

description: Convert a hex string to hex number


code:
Server
from SimpleXMLRPCServer import SimpleXMLRPCServer

class Server(object):
    def __init__(self, hostport):
        self.server = SimpleXMLRPCServer(hostport)

    def register_function(self, function, name=None):
        def _function(args, kwargs):
            return function(*args, **kwargs)
        _function.__name__ = function.__name__
        self.server.register_function(_function, name)

    def serve_forever(self):
        self.server.serve_forever()

#example usage
server = Server(('localhost', 8000))
def test(arg1, arg2):
    print 'arg1: %s arg2: %s' % (arg1, arg2)
    return 0
server.register_function(test)
server.serve_forever()

rawDescription: Using **kwargs with SimpleXMLRPCServer in python


code: print [{"name":dic["name"], "order":tr[dic["name"]]} for dic in l]

rawDescription: Adding a key value pair to a list of dicts based on another list Python


code: print shapes.loc[20958, 45]

rawDescription: Breaking down large dataset into organized index


code:
total = 0
for c in "#car":
    total <<= 7
    total += ord(c)
print total

rawDescription: Obfuscating Strings with ASCII and base 128


code:
def contains_words(prompt, words):

    while True:
        user_input = raw_input(prompt).strip().lower()

        if user_input == "instructions":
            print
            instructions()
            print

        elif user_input == "i" or user_input == "inventory":
            if len(inventory) == 0:
                print
                print "There is nothing in your inventory."
                print
            else:
                print "Your inventory contains: " + inventory

        else:
            if user_input in words:
                return user_input
            else:
                print
                print "I did not understand your answer, consider rephrasing."

rawDescription: Recursive function with same arguments

description: Recursive function with same arguments


code:
a = "  PVT corner         |    TYP_25    |    SLOW_125    SLOW_0_   |    SLOW_M40|    FAST_12 "
b = " Description         |  RD   |  WR | A   |  RD     |  WR     |  RD     |  WR     |  RD     |  WR     |  RD     |  WR     "
head = 0
res = []
for i,(s,t) in enumerate(zip(a,b)):
    if (s,t) == ("|","|"):
        res.append([a[head:i].strip()]+[m.strip() for m in b[head:i].split("|")])
        head = i + 1
res.append([a[head:].strip()]+[m.strip() for m in b[head:].split("|")])

for r in res:
    print r

rawDescription: how to Merge two strings based on some conditions


code:
import xml.etree.ElementTree as ET
root = ET.fromstring("<backup p='5'><u qnt='*' r='-1'></u></backup>")
for child in root:
    print 'qnt=', child.attrib['qnt']
    print 'r=', child.attrib['r']

rawDescription: Parsing XML with Python 2.7

description: Parsing XML with Python 2.7


code:
from listmatching import get_top_sets
my_list = [['cat','bat'],['sat','rat','mat'],['cat','bat','hat'],['pat','mat']]
print get_top_sets(my_list)

rawDescription: python match lists

description: Python match lists


code:
import ast

for elem in doc.findall('Default_Config/Lanestat'):
    if elem.tag == 'Lanestat':
        val = ast.literal_eval(elem.text)
        print type(val), val
        print elem.tag, val[1]

rawDescription: Converting Tuple to Dictionary

description: Converting Tuple to Dictionary


code:
def guess_seq_len(seq):
    guess = 1
    max_len = len(seq) / 2
    for x in range(2, max_len):
        if seq[0:x] == seq[x:2*x] :
            return x

    return guess

list_a = [111,0,3,1,111,0,3,1,111,0,3,1]
list_b = [67,4,67,4,67,4,67,4,2,9,0]
list_c = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,23,18,10]

print guess_seq_len(list_a)
print guess_seq_len(list_b)
print guess_seq_len(list_c)
print guess_seq_len(range(500))   # test of no repetition

rawDescription: Python finding repeating sequence in list of integers?


code:
from collections import namedtuple

Connection=namedtuple("Connection",["source","strength"])
class Unit(object):

# these first two methods just let me set up units and connections:
    def __init__(self,par=1, response=[0], connections=[]):
        self.par=par
        self.response=response
        self.connections=dict()#[]

    def receiveConnection(self,source,strength,label):
        # source is some other variable, strength is probably fixed but
        # i'd like it to be able to change, too
        #cx = Connection(label,source,strength)
        #self.connections.append(cx)
        self.connections[label]=Connection(source,strength)

# stuff that gets done iteratively through the sim
    def update(self):
        netInput=0.
        # based on the value of all the sources and weights right now,
        for item in self.connections.itervalues():
            netInput+= item.source[0] * item.strength
        self.response[0] += self.par * netInput

if __name__=="__main__":
    Unit1 = Unit()
    Unit2 = Unit(response=[1])

    Unit1.receiveConnection(source=Unit2.response,strength=.5,label="U2toU1")
    Unit2.receiveConnection(source=Unit2.response,strength=-.5,label="negativeFeedback")

    for t in xrange(10):
        Unit1.update()
        print 'Unit1:    ' + str(Unit1.response)
        Unit2.update()
        print 'Unit2:    ' + str(Unit2.response)

rawDescription: python: how to create interactions between objects- reference to object doesn't seem to work

description: Python: how to create interactions between objects- reference to object doesn't seem to work


rawDescription: Calculate distance between a point and a line segment in latitude and longitude


code:
X = ['A','B','C']
Y = ['1','2','3']
VALUES_FOR_X = {'A':1, 'B': 2, 'C':3}

class Combination:   # Creates a list of possible position combinations
    def __init__(self,x,y):
        if (x in X) and (y in Y):
            self.x = x
            self.y = y
        else:
            print "WRONG!!"

    def get_value(self):
        return VALUES_FOR_X[self.x]

    def __repr__ (self):
        return self.x+self.y

class Position:     # Makes operation on the chosen position
    def __init__(self):
        self.xy = []
        for i in X:
            for j in Y:
                self.xy.append(Combination(i,j))

    def choose_last(self):
        return self.xy.pop()

    def __str__(self):
        return "List contains: " + str(self.xy)

pos = Position()
print pos
last_item = pos.choose_last()
print "Last item is:", last_item
print  last_item.get_value()

rawDescription: Getting values from a dictionary

description: Getting values from a dictionary


code:
from bs4 import BeautifulSoup, Tag
sample = '''\

    Buying this item will cost you

    silver credits and

    golden credits

'''
html = BeautifulSoup(sample)
result = []
for child in html.find('div', class_='textbox').children:
    if isinstance(child, Tag):
        result.append(child.get('alt', ''))
    else:
        result.append(child.strip())

print ' '.join(result)

rawDescription: Python, BeautifulSoup - <div> text and <img> attributes in correct order


code:
string = 'Bryan'

def caps(word):
    return word.upper()

returning_value = caps(string) #Note that you need to catch the returning the value.
print returning_value

rawDescription: python functions for beginners

description: Python functions for beginners


code:
samples = """\
123.123.This is a string some other numbers
1. This is a string some numbers
12-3-12.This is a string 123
123-12This is a string 1234
""".splitlines()
for sample in samples:
    print 'From: {!r}\nTo:   {!r}\n'.format(
        sample, sample.lstrip('0123456789.- '))

rawDescription: Python: Remove numbers at the beginning of a string


code:
mylist = list("aabbccddeeffgghhii")
for x in reversed(range(len(mylist))):
    if mylist[x] in "bdfh":
        del mylist[x]
print mylist

rawDescription: python - recursively deleting dict keys?

description: Python - recursively deleting dict keys


code:
from multiprocessing import Process, Queue

def myFuncQ(myObj, i, return_queue):
    myObj.x = 'start'
    modObj = deepcopy(myObj)
    modObj.x = 'modified'
    return_queue.put(modObj)

return_queue = Queue()
for i in range(4):
    temp = MyClass()
    p = Process(target=myFuncQ, args=(temp, i, return_queue))
    p.start()
    p.join()
    temp = return_queue.get()
    print i,temp.x

rawDescription: Relink python object (i.e. pass by reference)


code:
import re

target="""
&START   A=23  ... more data ...
                  B=24    &END
#   &START   A=C34  ... more data ...
                  B=C24    &END
&START   .... block 3 of data across multiple lines ....  &END
&START   .... block 4 of data across multiple lines ....  &END
"""

regex = re.compile("^(?!#)&START (.*?)&END",re.MULTILINE|re.DOTALL)

for s in regex.findall(target):
   print s

rawDescription: How to exclude comment lines when searching with regular expression?

description: Exclude comment lines when searching with regular expression


rawDescription: Printing all the values from multiple lists at the same time


code:
import ast
import inspect

def evaluate(constraint):
    print ast.dump(ast.parse(inspect.getsource(constraint)))

evaluate(lambda x: x < 5 and x > -5)

rawDescription: Defining new semantics for expressions in Python


code:
mystring = "0,1,2"
mylist = [int(i) for i in mystring.split(",")]
print mylist

rawDescription: Casting a string containing comma separated values into an integer


code:
s='abc/def/ghi'
t='no slash here'
print s[:s.find("/") % (len(s) + 1)]
print t[:t.find("/") % (len(t) + 1)]
print s[:s.rfind("/") % (len(s) + 1)]
print t[:t.rfind("/") % (len(t) + 1)]
print s[s.find("/")+1:]
print t[t.find("/")+1:]
print s[s.rfind("/")+1:]
print t[t.rfind("/")+1:]

rawDescription: ksh-style left and right string stripping up to matched expression?

description: Ksh-style left and right string stripping up to matched expression


code:
import praw
print praw.__path__

rawDescription: Where can I find packages installed through pip?


code:
#!/usr/bin/python

import sys, json

result = {'success':'true','message':'The Command Completed Successfully'};

myjson = json.load(sys.stdin)
# Do something with 'myjson' object

print 'Content-Type: application/json\n\n'
print json.dumps(result)    # or "json.dump(result, sys.stdout)"

rawDescription: Post JSON to Python CGI

description: Post JSON to Python CGI


code:
class Check(object):
    def __init__(self):
        print "I am initialized"

    def get_name(self):
        return "my name is check"

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

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


code:
from bs4 import BeautifulSoup
import urllib
import re
company = ['Lockheed_Martin', 'Phillips_66', 'ConocoPhillips', 'Sysco', 'Baker_Hughes']

for c in company:
    r = urllib.urlopen('https://en.wikipedia.org/wiki/' + c).read()
    soup = BeautifulSoup(r, "lxml")
    for tr in soup.findAll('tr'):
        trText = tr.text
        if re.search(r"^\bRevenue\b$", trText):
            match = re.search(r"\w+\$(?:\s+)?[\d\.]+.{1}\w+", trText)
            revenue = match.group()
            print c+"\n"+revenue+"\n"

rawDescription: What's the best way to extract specific text from Wikipedia's Infobox using BeautifulSoup and lists?


code:
conditions = [(10, 20), (20, 50)] # min, max as tuples in a list
results = {}
for condition in conditions:
    selection = ar[(ar2 > condition[0]) & (ar2 < condition[1])]
    # do something with the selection ?
    results[condition] = selection
print results

rawDescription: faster way to append value

description: Faster way to append value


code:
from token import NAME
from tokenize import generate_tokens, untokenize
from StringIO import StringIO

source = """def x(y):
    b = 2
    if y == b:
        foo(y)"""
result = []
tokens = generate_tokens(StringIO(source).readline)
for toknum, tokval, _, _, _ in tokens:
    if toknum == NAME and tokval == "foo":
        tokval = "bar"
    result.append((toknum, tokval))

print untokenize(result)

rawDescription: Python code reflection and modification

description: Python code reflection and modification


code:
num = 5
print globals()

rawDescription: How does CPython match variables to values?


code:
print '\n'.join(food)
for val in food :
     print val

rawDescription: Python: How do I randomly select a value from a dictionary key?


code:
events = """\
1-hammer
1-screwdriver
1-nails
2-hammer
2-nails
3-screws
3-screwdriver
4-nails
4-screws""".splitlines()
events = sorted(map(str.strip,e.split('-')) for e in events)

from collections import defaultdict
from itertools import groupby

# tally each occurrence of each pair of items
summary = defaultdict(int)
for val,items in groupby(events, key=lambda x:x[0]):
    items = sorted(it[1] for it in items)
    for i,item1 in enumerate(items):
        for item2 in items[i+1:]:
            summary[(item1,item2)] += 1
            summary[(item2,item1)] += 1

# now convert raw pair counts into friendlier lookup table
pairmap = defaultdict(dict)
for k,v in summary.items():
    item1, item2 = k
    pairmap[item1][item2] = v

# print the results
for k,v in sorted(pairmap.items()):
    print k,':',v

rawDescription: Optimize algorithm for creating a list of items rated together, in Python


code:
a = 1
globals()['a'] = 2
print a  #2
globals()['a_in'] = 2
print a_in  #2

rawDescription: Python change function input name internaly


code:
def sum_k(n, k):
    if k < 0 or n * 7 < k: return
    if n == 1:
        yield k,
        return
    for d in xrange(8):
        for s in sum_k(n-1, k-d):
            yield (d,) + s

for digs in sum_k(8, 8):
    counts = [0] * 8
    for d in digs:
        counts[d] += 1
    if counts == list(digs):
        print digs

rawDescription: Optimized way for finding the number which satisfies specific conditions


code: print c

rawDescription: Sum corresponding elements of multiple python dictionaries


code:
my_list = [[1,2]]
for e in my_list:
    print 'BEFORE id(e)', id(e)
    e[0] = e[0] + 1
    e[1] = e[1] + e[0]
    print 'AFTER id(e)', id(e)

id(my_list[0])

rawDescription: python behavior of list modification in for loop

description: Python behavior of list modification in for loop


code:
# First, group by picture id (356788, 356849...), which is
# the third column of the data
for group_by_id in itertools.groupby(data_matrix,
                                     lambda x: x[2]):
    # Now, within those groups, group by type, the first column
    # of the data (SD, Image...)
    for group_by_type in itertools.groupby([a for a in group_by_id[1]],
                                           lambda y: y[0]):
        print "%s: %s %s" % (group_by_id[0],
                             len([a for a in group_by_type[1]]),
                             group_by_type[0])
    print ''

rawDescription: counting and storing values in a dictionary using python

description: Counting and storing values in a dictionary using python


code:
from modulefinder import ModuleFinder

finder = ModuleFinder()
finder.run_script('test.py')
print finder.report()

rawDescription: Using Numpy creates a tcl folder when using py2exe


code:
d = { "a":4, "c":3, "b":12 }
d_view = [ (v,k) for k,v in d.iteritems() ]
d_view.sort(reverse=True) # natively sort tuples by first element
for v,k in d_view:
    print "%s: %d" % (k,v)

rawDescription: Print a dict sorted by values

description: Print a dict sorted by values


code:
import subprocess
import select
import time

x=subprocess.Popen(['/bin/bash','-c',"while true; do sleep 5; echo yes; done"],stdout=subprocess.PIPE)

y=select.poll()
y.register(x.stdout,select.POLLIN)

while True:
  if y.poll(1):
     print x.stdout.readline()
  else:
     print "nothing here"
     time.sleep(1)

rawDescription: Python: How to read stdout of subprocess in a nonblocking way


code:
f = open('test_file.txt', 'r')
char_list = []

def read_rec(fh, chars):
    curChar = fh.read(1)
    if curChar: #returns False at the end of the file
        chars.append(curChar) #append to chars
        return read_rec(fh, chars) #recursively read the rest of the file
    return chars

print read_rec(f, char_list)

rawDescription: How to recursively read all characters in a list?

description: Recursively read all characters in a list


code:
import sys
import os

if len(sys.argv) > 1 and sys.argv[1] == 'setenv':
    print 'set FOO=foo'
    print 'set PATH=%s;%s' % ('C:\\my_path_dir', os.environ['PATH'])
else:
    print "I'm gonna do something else with argv=%r" % sys.argv

rawDescription: Launch configured cmd from Python

description: Launch configured cmd from Python


code:
import shutil, sys

print sys.argv # print out the list so you can see what it looks like

a = sys.argv[1]
b = sys.argv[2]
shutil.copy(a, b)    # a & b take their values from the command line

shutil.copy('text1','text2')  # here shutil is using hard coded names

rawDescription: Shutil.copy Won't Copy

description: Shutil.copy Won't Copy


code:
from bs4 import BeautifulSoup
import requests

r=requests.get('http://www.virginiaequestrian.com/main.cfm?action=greenpages&GPType=8')
soup=BeautifulSoup(r.content)

tbl = soup.find("table")
for tag in tbl.find_all("tr")[3:]:
    for td in tag.find_all('td'):
        print td.text

rawDescription: BeautifulSoup Iteration not working

description: BeautifulSoup Iteration not working


code:
hex_str = "0xAD4"
hex_int = int(hex_str, 16)
new_int = hex_int + 0x200
print hex(new_int)

rawDescription: How to convert a hex string to hex number

description: Convert a hex string to hex number


rawDescription: Getting svg stored in a variable in jinja2


code:
value = None
print repr(value)
str(value) if value is not None else ''
value = "None"
print repr(value)
str(value) if value is not None else ''

rawDescription: Python "is not None" returning None

description: Python "is not None" returning None


code:
string = line.split(',')
if string[0] in {'Aa', 'Bb'}:
  print string

rawDescription: Condition with pass statement

description: Condition with pass statement


code:
from Tkinter import *
import tkFileDialog

f_options = dict(filetypes=[('All files','*')])

class Begueradj(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.title('Begueradj Test All Files')
        menu = self.menubar = Menu(self)
        fmenu = self.filemenu = Menu(menu, tearoff=0)
        menu.add_cascade(label='File', underline=0, menu=fmenu)
        fmenu.add_command(label="Open", underline=0,
                          accelerator='Ctrl+O',
                          command=self.fopendialog)
        self.config(menu=menu)
        self.bind_all('<Control-o>', self.fopendialog)

    def fopendialog(self, event=None):
        print repr(tkFileDialog.askopenfilename(parent=self,
                                                **f_options))

if __name__ == "__main__":
    test = Begueradj()
    test.mainloop()

rawDescription: "tkinter.filedialog" does not recognize files without a suffix in its name


code:
lis = [[(2L,)], [(3L,)]]
[ y[0] for x in lis for y in x]

rawDescription: Convert this list, tuple in list only in Python?


code:
strs = raw_input("Enter a string:")
lis = []
for x in strs:
   if len(lis) != 0:
      if lis[-1][0] == x:
         lis[-1][1] += 1
      else:
         lis.append([x, 1])
   else:
       lis.append([x, 1])
print lis

rawDescription: count letter and show in list

description: Count letter and show in list


rawDescription: How to create Selenium objects?

description: Create Selenium objects


code: print type(body)

rawDescription: Removing <u> character from text using Scrapy


code:
if "0" in myString:
  print "%s contains 0" % myString

rawDescription: python. How to find whether a text contains zero.?

description: Python. How to find whether a text contains zero.


code:
#!/usr/bin/env python

import antlr3
from antlr3 import *
from antlr3.tree import *
from CSSLexer import *
from CSSParser import *

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

input = 'h1 {  a: b;  c: d;}\n\n/*aaa1/*bbb/*ccc*/*/aaa2*/\n\np {x  :  y;}'
char_stream = antlr3.ANTLRStringStream(input)
lexer = CSSLexer(char_stream)
tokens = antlr3.CommonTokenStream(lexer)
parser = CSSParser(tokens)
tree = parser.parse().tree
print_level_order(tree, 0)

rawDescription: ANTLR get and split lexer content

description: ANTLR get and split lexer content


code:
from bs4 import BeautifulSoup

data = """

                                               3.5

                                               3.9

                                               4.2

"""

soup = BeautifulSoup(data)
print [r.get_text(strip=True) for r in soup.find_all('div', attrs={'class': 'rating-div'})]

rawDescription: scrape using xpath and domdocuments

description: Scrape using xpath and domdocuments


code:
import matplotlib.pyplot as plt
import matplotlib.backends.backend_agg as backend
from matplotlib.figure import Figure

# Independent figure/axes
fig = Figure()
canvas = backend.FigureCanvas(fig)
ax = fig.add_subplot(111)
ax.plot(range(10))

# gca() is completely unaware of this axes and will create a new one instead:
ax2 = plt.gca()
print 'Same axes?:', id(ax) == id(ax2)

# And `plt.show()` would show the blank axes of `ax2`

rawDescription: Prevent matplotlib statefulness

description: Prevent matplotlib statefulness


code:
import numpy as np

B = np.random.randint(-100,100,size=100000)

def max_subarray(A):
    max_so_far = max_ending_here = 0
    for x in A:
        max_ending_here = max(0, max_ending_here + x)
        max_so_far      = max(max_so_far, max_ending_here)
    return max_so_far

import time

measurements = np.zeros(100, dtype='float')
for i in range(measurements.size):
    a = time.time()
    max_subarray(B)
    measurements[i] = time.time() - a

print 'non-c:', measurements.min(), measurements.max(), measurements.mean()

rawDescription: Optimizing Kadane's Algorithm for numpy

description: Optimizing Kadane's Algorithm for numpy


code:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivy.animation import Animation
from kivy.properties import NumericProperty

import math

kv = '''
<Dial>:
    circle_id: circle_id
    size: root.size
    pos: 0, 0
    canvas:
        Rotate:
            angle: self.angle
            origin: self.center
        Color:
            rgb: 1, 0, 0
        Ellipse:
            size: min(self.size), min(self.size)
            pos: 0.5*self.size[0] - 0.5*min(self.size), 0.5*self.size[1] - 0.5*min(self.size)
    Circle:
        id: circle_id
        size_hint: 0, 0
        size: 50, 50
        pos: 0.5*root.size[0]-25, 0.9*root.size[1]-25
        canvas:
            Color:
                rgb: 0, 1, 0
            Ellipse:
                size: 50, 50
                pos: self.pos
'''
Builder.load_string(kv)

class Circle(Widget):
    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            print "small circle clicked"

class Dial(Widget):
    angle = NumericProperty(0)

    def on_touch_down(self, touch):
        if not self.circle_id.collide_point(*touch.pos):
            print "big circle clicked"

        y = (touch.y - self.center[1])
        x = (touch.x - self.center[0])
        calc = math.degrees(math.atan2(y, x))
        self.prev_angle = calc if calc > 0 else 360+calc
        self.tmp = self.angle

        return super(Dial, self).on_touch_down(touch) # dispatch touch event futher

    def on_touch_move(self, touch):
        y = (touch.y - self.center[1])
        x = (touch.x - self.center[0])
        calc = math.degrees(math.atan2(y, x))
        new_angle = calc if calc > 0 else 360+calc

        self.angle = self.tmp + (new_angle-self.prev_angle)%360

    def on_touch_up(self, touch):
        Animation(angle=0).start(self)

class DialApp(App):
    def build(self):
        return Dial()

if __name__ == "__main__":
    DialApp().run()

rawDescription: Rotating an object on a touch event in kivy


code:
def register_callback(name):
    def decorator(cls):
        cls._callback = getattr(cls, name)
        return cls
    return decorator

@register_callback('my_func')
class SimpleConsumer(Consumer):
     def my_func(self, body):
         print body

rawDescription: Register unique callbacks per instance using classmethod


code:
import os
import re

r = re.compile(r'[-_ ]+')
def solve(s):
    name, ext = os.path.splitext(s)
    artist, song = name.rsplit('-', 1)
    artist = r.sub(' ', artist).title().strip()
    song = r.sub(' ', song).title().strip()
    return artist + ' - ' + song + ext

if __name__ == '__main__':
    print solve("Alibi- Eternity_ Ft_ Armin Van Buuren and Dj Tiesto_.mp3")
    print solve( "05-orjan_nilsen-so_long_radio__original_mix.mp3")
    print solve( "05 - Orjan Nilsen - So Long Radio (Original Mix).mp3")

rawDescription: Rename file names CAP first character but keep the extention lowercase


code:
from string import ascii_uppercase

print list(ascii_uppercase)[:int(math.ceil(df.scale / 3000))]

rawDescription: Change count into an array of letters, python


code:
print "press enter..."
raw_input()
print "yay!"

rawDescription: How to make python script press 'enter' when prompted on Shell

description: Make python script press 'enter' when prompted on Shell


code:
width = 15
print truncateToApproxArialWidth("the quick brown fox jumps over the lazy dog", width)
print truncateToApproxArialWidth("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG", width)

rawDescription: Roughly approximate the width of a string of text in Python?


code:
print bs1.find('a').contents
print bs2.find('a').contents

rawDescription: Beautifulsoup find element by text using `find_all` no matter if there are elements in it


code:
text = "get|Server ID:Server1,Server ID:Server2,Server ID:Server3...,Query:SELECT * FROM sometable,_xsrf:57bsdkfnklbjhgfhjfghsndfsdfsdf"

pattern = r'Server ID:([^,.]*),?'
result = re.findall(pattern, text)

print result

rawDescription: Extracting values from a string and store them in a list


rawDescription: Different fill methods for different columns in pandas


code:
x = RealVal('3.14')
z = Real('y')
print z > x

rawDescription: z3 const declaration

description: Z3 const declaration


code:
import re
f1=open("file1","r")
f2=open("file2","r")
f=open("output","w")
test_lines=f1.readlines()
correct_lines=f2.readlines()

test_lines = set([l.strip().split("(")[0].replace(" ","").strip() for l in test_lines])
correct_lines = set([l.strip().split("(")[0].replace(" ","").strip() for l in correct_lines])

print "Expected: ", correct_lines-test_lines
print "Got: ", test_lines-correct_lines

rawDescription: Diffing two files while ignoring any missed lines


code:
from mock import patch

# Our class to test
class FooObject():
    def update(self, obj):
        print obj

# Mock the update method
@patch.object(FooObject, 'update')
def test(mock1):
    # test that the update method is actually mocked
    assert FooObject.update is mock1
    foo = FooObject()
    foo.update('foo')
    return mock1

# Test if the mocked update method was called with 'foo' parameter
mock1 = test()
mock1.assert_called_once_with('foo')

rawDescription: Python: using Mock to override class method


code:
foobar = [1, 2, 3]
def main():
    print foobar[0]
def printx():
    print x

rawDescription: How do I make a variable accessible from another python script when accessed from modulename.main() method

description: Make a variable accessible from another python script when accessed from modulename.main() method


code:
import mock

testsocket = mock.Mock()
testsocket.configure_mock(**{'recv.return_value': 'test_recv'})
print testsocket.recv(1024)

rawDescription: How to test loopback socket connections with python?

description: Test loopback socket connections with python


code:
from Tkinter import *

def option_changed(*args):
    print "the user chose the value {}".format(variable.get())
    print a

master = Tk()

a = "Foo"
variable = StringVar(master)
variable.set("Apple") # default value
variable.trace("w", option_changed)

w = OptionMenu(master, variable, "Apple", "Orange", "Grapes")
w.pack()

mainloop()

rawDescription: Getting the choice of optionmenu right after selection Python


code:
comp = n2[:]  # make a copy
for x in n1:
    if x not in comp:
        print x
    else:
        comp.remove(x)
# output: 3

rawDescription: How to find the difference between 3 lists that may have duplicate numbers

description: Find the difference between 3 lists that may have duplicate numbers


code:
from math import sqrt

class Vector(object):

    def __init__(self, x, y, z, normalized=False):
        self.v = [x, y, z]
        if normalized in [True, False]:
            self.normalized = normalized
        else:
            raise Exception("Please set normalized to either True or False")

    def __repr__(self):
        return "Vector ({}, {}, {})".format(*self.v)

    def normalize(self):
        x,y,z = self.v
        norm = sqrt(x**2 + y**2 + z**2)
        self.v = [x/norm, y/norm, z/norm]
        self.normalized = True

    def isNormalized(self):
        return self.normalized

    # other methods follow

v1 = Vector(10, 20, 30)
print v1.isNormalized()
v1.normalize()
print v1.isNormalized()

v2 = Vector(40, 50, 60, normalized=True)
print v2.isNormalized()

rawDescription: Method to convert object from class to subclass in Python


code:
import subprocess

exitcode = subprocess.call(["Matlab.exe", "/home/user/docs/myapp.mat"])
print exitcode

rawDescription: (Python) Launching an external application and wait for the end of it


code:
def _traverse_output_with_list(record, fields, row, field_name=''):
    while len(row) < len(fields):
        row.append('')

    for obj, value in record.iteritems():
        new_field_name = '{}.{}'.format(field_name, obj) if field_name else obj
        print new_field_name
        if not isinstance(value, dict):
            if new_field_name in fields:
                row[fields.index(new_field_name)] = value
        else:
            _traverse_output_with_list(value, fields, row, new_field_name)

rawDescription: Python Recursively Maintain Keyed Depth

description: Python Recursively Maintain Keyed Depth


code:
from matplotlib import pyplot
import numpy, weakref
a = numpy.arange(int(1e3))
fig = pyplot.Figure()
ax  = fig.add_subplot(1, 1, 1)
lines = ax.plot(a)

l = lines.pop(0)
wl = weakref.ref(l)  # create a weak reference to see if references still exist
#                      to this object
print wl  # not dead
l.remove()
print wl  # not dead
del l
print wl  # dead  (remove either of the steps above and this is still live)

rawDescription: How to remove lines in a Matplotlib plot

description: Remove lines in a Matplotlib plot


code:
words_list = string.split()
words_dict = {}

for a in range(len(words_list)):
    for b in range(a):
        phrase = " ".join(words_list[b:a])
        if phrase in words_dict:
            words_dict[phrase] += 1
        else:
            words_dict[phrase] = 1

for i in words_dict:
    print i, words_dict[i]

rawDescription: Being Pythonic: Collecting arbitrary strings - indexer


code:
import itertools

def func1(a, b, c):
    return a+b+c

print list(itertools.starmap(func1, [[1,2,3],[4,5,6],[7,8,9]]))

rawDescription: Using map function with a multi-variable function


rawDescription: Python - How to transform a list of tuple to a tuple


code:
import requests

h = requests.head('http://www.google.com')

print h.headers['content-length']

rawDescription: Is there a better way to retrieve webpage sizes with Python?


code:
for k,v in mydict.iteritems():
  print k, '=', v

rawDescription: concatenate items in dictionary in python using list comprehension

description: Concatenate items in dictionary in python using list comprehension


code:
cows = "111 cows 222 cows "
print cows.split(" cows ") # this prints ['111', '222', '']

rawDescription: 'Int' object is not iterable. But, it is a string


code: print df_list[ df_list['b'].apply(lambda x:test_any_2([2,3,8], x) ) ]

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


code:
from selenium import webdriver

fp = webdriver.FirefoxProfile()
browser = webdriver.Firefox(firefox_profile=fp)
browser.get('http://www.cavirtex.com/orderbook')

lowest_bid = float('inf')
elements = browser.find_elements_by_xpath('//div[@id="orderbook_buy"]/table/tbody/tr/td')

for element in elements:
    text = element.get_attribute('innerHTML').strip('<b>|</b>')
    try:
        bid = float(text)
        if lowest_bid > bid:
            lowest_bid = bid
    except:
        pass

browser.quit()
print lowest_bid

rawDescription: Scrape Data Point Using Python

description: Scrape Data Point Using Python


code:
import numpy as np

example = """
This is a header. The first line is always a header...
  7352.103      26.0      2.61    -8.397                         11.2
...
"""

lines = example.split('\n')[1:]
header = lines[0]
data = []
for line in lines[1:]:
  if len(line):
    data.append([float(x) for x in line.split()])
data = np.array(data)

fmt = '%10.3f %9.1f %9.2f %9.3f %20.1f'  # similar to a FORTRAN format statment
filename = 'stack21865757.txt'

with open(filename,'w') as f:
  np.savetxt(f, data, fmt, header=header)

with open(filename) as f:
  print f.read()

rawDescription: Use python to handle and create input files for external software


code:
a = []
b = a  # the same list
c = [] # another empty one

a.append(3)
print a, b, c # a as well as b = [3], c = [] as it is a different one

d = a[:] # copy it completely
b.append(9)
# now a = b = [3, 9], c = [], d = [3], a copy of the old a resp. b

rawDescription: Why are lists linked in Python in a persistent way?


code:
import bisect
intervals = [6, 7, 8, 9, 10, 11]
for n in (6, 6.1, 6.2, 6.5, 6.8, 7):
  print bisect.bisect_left(intervals, n)

for n in (6, 6.1, 6.2, 6.5, 6.8, 7):
  print bisect.bisect_right(intervals, n)

rawDescription: Fastest way to check which interval index a value is


code:
def second_smallest(numbers):
    m1, m2 = float('inf'), float('inf')
    for x in numbers:
        if x <= m1:
            m1, m2 = x, m1
        elif x < m2:
            m2 = x
    return m2

print second_smallest([1, 2, 3, 4])

rawDescription: Python - Find second smallest number

description: Python - Find second smallest number


code:
import pandas as pd

def f(g):
    return pd.DataFrame({'count': [g.shape[0]], 'sum': [g['Duration'].sum()]})

reader = pd.read_csv('data.csv',chunksize=2)
acc = pd.DataFrame({})
for chunk in reader:
    acc = acc.add(chunk.groupby('Code').apply(f).reset_index(level=1,drop=True),fill_value=0)

acc['avg'] = acc['sum']/acc['count']
print acc

acc.to_csv('avg_codes.csv',cols=['avg'],index_label='Code')

rawDescription: Calculating mean of Pandas dataframe in iterations


code:
from scipy import misc

img = misc.imread("lena.bmp")

print "R:", img[:, :, 0].max()
print "G:", img[:, :, 1].max()
print "B:", img[:, :, 2].max()

rawDescription: get the max of R,G,B of one image by using programming

description: Get the max of R,G,B of one image by using programming


code:
divs = [int(input('Divisible by: ')) for _ in range(int(input('Divisions: ')))]
for beat in range(1, int(input('Number of beats to print: ')) + 1):
    print '%2d:%s' % (beat, ''.join(
        'X' if (beat % div) == 0 else ' ' for div in divs).rstrip())

rawDescription: Advanced Python Code Assistance

description: Advanced Python Code Assistance


code:
queryset = BlahModel.objects.order_by('?')[:5]
print queryset.query

rawDescription: Django Query Performance

description: Django Query Performance


code:
from sqlalchemy import create_engine, Column, Integer, SmallInteger, String, ForeignKey, Table, Index
from sqlalchemy.orm import relationship, scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

# Configure test data SA
engine = create_engine('sqlite:///:memory:', echo=True)
session = scoped_session(sessionmaker(bind=engine))
Base = declarative_base()
Base.query = session.query_property()

class _BaseMixin(object):
    """ Just a helper mixin class to set properties on object creation.
    Also provides a convenient default __repr__() function, but be aware that
    also relationships are printed, which might result in loading relations.
    """
    def __init__(self, **kwargs):
        for k,v in kwargs.items():
            setattr(self, k, v)

    def __repr__(self):
        return "<%s(%s)>" % (self.__class__.__name__,
            ', '.join('%s=%r' % (k, self.__dict__[k])
                for k in sorted(self.__dict__) if '_sa_' != k[:4] and '_backref_' != k[:9])
            )

### AnswerOption hierarchy
class AnswerOption(Base, _BaseMixin):
    """ Possible answer options (choice or any other configuration).  """
    __tablename__ = u'answer_options'
    id = Column(Integer, primary_key=True)
    question_id = Column(Integer, ForeignKey('questions.id'), nullable=False)
    value = Column(Integer, nullable=False)
    type = Column(String(10), nullable=False)
    __mapper_args__ = {'polymorphic_on': type}

class AnswerOptionChoice(AnswerOption):
    """ A possible answer choice for the question.  """
    text = Column(String, nullable=True) # when mapped to single-table, must be NULL in the DB
    __mapper_args__ = {'polymorphic_identity': 'choice'}

class AnswerOptionInput(AnswerOption):
    """ A configuration entry for the input-type of questions.  """
    input = Column(Integer, nullable=True) # when mapped to single-table, must be NULL in the DB
    __mapper_args__ = {'polymorphic_identity': 'input'}

### Question hierarchy
class Question(Base, _BaseMixin):
    """ Base class for all types of questions.  """
    __tablename__ = u'questions'
    id = Column(Integer, primary_key=True)
    text = Column(String, nullable=False)
    type = Column(String(10), nullable=False)
    answer_options = relationship(AnswerOption, backref='question')
    __mapper_args__ = {'polymorphic_on': type}

    def get_answer_value(self, answer):
        """ function to get a value of the answer to the question.  """
        raise Exception('must be implemented in a subclass')

class QuestionChoice(Question):
    """ Single-choice question.  """
    __mapper_args__ = {'polymorphic_identity': 'choice'}

    def get_answer_value(self, answer):
        assert isinstance(answer, AnswerChoice)
        assert answer.answer_option in self.answer_options, "Incorrect choice"
        return answer.answer_option.value

class QuestionInput(Question):
    """ Input type question.  """
    __mapper_args__ = {'polymorphic_identity': 'input'}

    def get_answer_value(self, answer):
        assert isinstance(answer, AnswerInput)
        value_list = sorted([(_i.input, _i.value) for _i in self.answer_options])
        if not value_list:
            raise Exception("no input is specified for the question {0}".format(self))
        if answer.answer_input <= value_list[0][0]:
            return value_list[0][1]
        elif answer.answer_input >= value_list[-1][0]:
            return value_list[-1][1]
        else: # interpolate in the range:
            for _pos in range(len(value_list)-1):
                if answer.answer_input == value_list[_pos+1][0]:
                    return value_list[_pos+1][1]
                elif answer.answer_input < value_list[_pos+1][0]:
                    # interpolate between (_pos, _pos+1)
                    assert (value_list[_pos][0] != value_list[_pos+1][0])
                    return value_list[_pos][1] + (value_list[_pos+1][1] - value_list[_pos][1]) * (answer.answer_input - value_list[_pos][0]) / (value_list[_pos+1][0] - value_list[_pos][0])
        assert False, "should never reach here"

### Answer hierarchy
class Answer(Base, _BaseMixin):
    """ Represents an answer to the question.  """
    __tablename__ = u'answers'
    id = Column(Integer, primary_key=True)
    type = Column(String(10), nullable=False)
    question_id = Column(Integer, ForeignKey('questions.id'), nullable=True) # when mapped to single-table, must be NULL in the DB
    question = relationship(Question)
    test_id = Column(Integer, ForeignKey('tests.id'), nullable=True) # @todo: decide if allow answers without a Test
    __mapper_args__ = {'polymorphic_on': type}

    def get_value(self):
        return self.question.get_answer_value(self)

class AnswerChoice(Answer):
    """ Represents an answer to the *Choice* question.  """
    __mapper_args__ = {'polymorphic_identity': 'choice'}
    answer_option_id = Column(Integer, ForeignKey('answer_options.id'), nullable=True)
    answer_option = relationship(AnswerOption, single_parent=True)

class AnswerInput(Answer):
    """ Represents an answer to the *Choice* question.  """
    __mapper_args__ = {'polymorphic_identity': 'input'}
    answer_input = Column(Integer, nullable=True) # when mapped to single-table, must be NULL in the DB

### other classes (Questionnaire, Test) and helper tables
association_table = Table('questionnaire_question', Base.metadata,
    Column('id', Integer, primary_key=True),
    Column('questionnaire_id', Integer, ForeignKey('questions.id')),
    Column('question_id', Integer, ForeignKey('questionnaires.id'))
)
_idx = Index('questionnaire_question_u_nci',
            association_table.c.questionnaire_id,
            association_table.c.question_id,
            unique=True)

class Questionnaire(Base, _BaseMixin):
    """ Questionnaire is a compilation of questions.  """
    __tablename__ = u'questionnaires'
    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)
    # @note: could use relationship with order or even add question number
    questions = relationship(Question, secondary=association_table)

class Test(Base, _BaseMixin):
    """ Test is a 'test' - set of answers for a given questionnaire. """
    __tablename__ = u'tests'
    id = Column(Integer, primary_key=True)
    # @todo: add user name or reference
    questionnaire_id = Column(Integer, ForeignKey('questionnaires.id'), nullable=False)
    questionnaire = relationship(Questionnaire, single_parent=True)
    answers = relationship(Answer, backref='test')
    def total_points(self):
        return sum(ans.get_value() for ans in self.answers)

# -- end of model definition --

Base.metadata.create_all(engine)

# -- insert test data --
print '-' * 20 + ' Insert TEST DATA ...'
q1 =  QuestionChoice(text="What is your fav pet?")
q1c1 = AnswerOptionChoice(text="cat", value=1, question=q1)
q1c2 = AnswerOptionChoice(text="dog", value=2, question=q1)
q1c3 = AnswerOptionChoice(text="caiman", value=3)
q1.answer_options.append(q1c3)
a1 = AnswerChoice(question=q1, answer_option=q1c2)
assert a1.get_value() == 2
session.add(a1)
session.flush()

q2 =  QuestionInput(text="How many liters of beer do you drink a day?")
q2i1 = AnswerOptionInput(input=0, value=0, question=q2)
q2i2 = AnswerOptionInput(input=1, value=1, question=q2)
q2i3 = AnswerOptionInput(input=3, value=5)
q2.answer_options.append(q2i3)

# test interpolation routine
_test_ip = ((-100, 0),
            (0, 0),
            (0.5, 0.5),
            (1, 1),
            (2, 3),
            (3, 5),
            (100, 5)
)
a2 = AnswerInput(question=q2, answer_input=None)
for _inp, _exp in _test_ip:
    a2.answer_input = _inp
    _res = a2.get_value()
    assert _res == _exp, "{0}: {1} != {2}".format(_inp, _res, _exp)
a2.answer_input = 2
session.add(a2)
session.flush()

# create a Questionnaire and a Test
qn = Questionnaire(name='test questionnaire')
qn.questions.append(q1)
qn.questions.append(q2)
session.add(qn)
te = Test(questionnaire=qn)
te.answers.append(a1)
te.answers.append(a2)
assert te.total_points() == 5
session.add(te)
session.flush()

# -- other tests --
print '-' * 20 + ' TEST QUERIES ...'
session.expunge_all() # clear the session cache
a1 = session.query(Answer).get(1)
assert a1.get_value() == 2 # @note: will load all dependant objects (question and answer_options) automatically to compute the value
a2 = session.query(Answer).get(2)
assert a2.get_value() == 3 # @note: will load all dependant objects (question and answer_options) automatically to compute the value
te = session.query(Test).get(1)
assert te.total_points() == 5

rawDescription: Mixed content (float || unicode) for database column


code:
a = 5
b = 3
c = a + b
print a, "plus", b, "equals", a+b

rawDescription: How does python know to add a space between a string literal and a variable?


code:
df = pd.DataFrame({"A": [1.0/3]})
df
print df.to_html()

rawDescription: format output data in pandas to_html

description: Format output data in pandas to_html


code:
answer = make_question('Test to correctness:', 'correct', 'not correct')
print answer

rawDescription: raw_input placeholder?

description: Raw_input placeholder


code:
class Test(object):
    @property
    def gx(self):
        print "getting gx"
        return self.__dict__['gx']

    @gx.setter
    def gx(self, value):
        print "setting gx"
        self.__dict__['gx'] = value

    def __setattr__(self, attr, value):
        print "using setattr"
        object.__setattr__(self, attr, value)

t = Test()
t.gx = 4
t.dummy = 5
print t.gx
print t.dummy

print dir(Test)

rawDescription: I need to define __setattr__ for assignment of fields without properties, but use the setter/getter functions for fields with a property defined


code:
for item in users:
    print item, ":\t", findmutual(item, all_users) # << That's why it shows None

rawDescription: Why am I getting a None value, and why is the number still being printed in Python?


code:
from lxml import etree as ET
import urllib2

@timing
def parseXMLFromString():
    xml_string = urllib2.urlopen(url_link).read()
    parsed = ET.fromstring(xml_string)
    print parsed

for n in range(0,100):
    parseXMLFromString()

rawDescription: Python lxml.etree - Is it more effective to parse XML from string or directly from link?


code:
from socket import *
s = socket()
s.connect(('example.com', 80))
s.send('GET / HTTP/1.1\r\nHost: example.com\r\nUser-Agent: MyScript\r\n\r\n')
print s.recv(8192)

rawDescription: How to perform HTTP GET operation in Python?

description: Perform HTTP GET operation in Python


code:
def freqs(list):
    words = {}
    for word in list:
        words[word] = words.get(word, 0) + 1
    return words

def added_and_removed(a, b):
    af = freqs(a.split())
    bf = freqs(b.split())

    removed = []
    added = []

    for key in af:
        num = bf.get(key)
        if num == None:
            if af[key] > 1:
                words = [key]*af[key]
                removed.extend(words)
            else:
                removed.append(key)

    for key in bf:
        num = af.get(key)
        if num == None:
            added.append(key)
        elif num > 1:
            words = [key]*(num-1)
            removed.extend(words)

    return added, removed

a = 'hello hello hello my name is Dave dave bar foo'
b = 'hello my guys is test easy rob dave beef foo'

added, removed =  added_and_removed(a, b)
print added
print removed

rawDescription: Get added and removed words between two strings


code:
list = contents
print list[0]

rawDescription: Need help reading data from input files and displaying it


code:
import itertools
def get_sections(f):
  it = enumerate(f)
  for lno, line in it:
    if line.startswith("/*!"):
      yield itertools.chain(
          [(lno,line.replace("/*!","",1))],
          itertools.takewhile(lambda i: not i[1].startswith('*'), it))

with open('inputfile.txt') as f:
  for secno, section in enumerate(get_sections(f)):
    for lno, line in section:
      print "%04d %04d %s"%(secno, lno,line.rstrip('\n'))

rawDescription: peak next character in a file

description: Peak next character in a file


rawDescription: python regex sub %H:%M:%S to %M:%s or 01:%M:%s

description: Python regex sub %H:%M:%S to %M:%s or 01:%M:%s


code:
for element in root.findall('Category'):
    value = element.find('Data')
    for child in value:
        print child.tag,":",child.text

rawDescription: Parse a *.nfo file with python

description: Parse a *.nfo file with python


code:
def items(lis):
    for l in lis:
        for k, v in l.iteritems():
            yield k, v

for k, v in items(y):
    print k, v

rawDescription: Iterate over a list of objects getting ke/value pairs


rawDescription: Edit Dictionary in python

description: Edit Dictionary in python


code:
import re
a = ["paul Parthipan","paul","sdds","sdsdd",'jony-Parthipan','koala_Parthipan','Parthipan']
lastName = "Parthipan"
myRegex = r"(?mis){0}".format(re.escape(lastName))

strA=';'.join(a)
se = re.search(myRegex, strA)
ma = re.match(myRegex, strA)
fa = re.findall(myRegex, strA)
fi=[i.group() for i in re.finditer(myRegex, strA, flags=0)]
se = '' if se is None else se.group()
ma = '' if ma is None else ma.group()

print se, 'match' if any(se) else 'no match'
print ma, 'match' if any(ma) else 'no match'
print fa, 'match' if any(fa) else 'no match'
print fi, 'match' if any(fi) else 'no match'

rawDescription: how to find a particular string in an element of array in python

description: Find a particular string in an element of array in python


code:
try:
  a = int(raw_input("Enter Number:"))
except ValueError:
  print "Wrong number. Enters only digit."

rawDescription: Input to list and operate on it

description: Input to list and operate on it


code:
import numpy as np
import shapely.geometry as geom
import matplotlib.pyplot as plt

class NearestPoint(object):
    def __init__(self, line, ax):
        self.line = line
        self.ax = ax
        ax.figure.canvas.mpl_connect('button_press_event', self)

    def __call__(self, event):
        x, y = event.xdata, event.ydata
        point = geom.Point(x, y)
        distance = self.line.distance(point)
        self.draw_segment(point)
        print 'Distance to line:', distance

    def draw_segment(self, point):
        point_on_line = line.interpolate(line.project(point))
        self.ax.plot([point.x, point_on_line.x], [point.y, point_on_line.y],
                     color='red', marker='o', scalex=False, scaley=False)
        fig.canvas.draw()

if __name__ == '__main__':
    coords = np.loadtxt('points.txt')

    line = geom.LineString(coords)

    fig, ax = plt.subplots()
    ax.plot(*coords.T)
    ax.axis('equal')
    NearestPoint(line, ax)
    plt.show()

rawDescription: Find minimum distance from point to complicated curve


code:
myList=[]

myList.append(("a",0))
myList.append(("b",0))
myList.append(("b",0))
myList.append(("a",1))
myList.append(("a",1))

Dict = {}

for Data in myList :
    Dict.setdefault(Data[1], []).append(Data[0])

print [(values[0], key) for key, values in Dict.items()]

rawDescription: Getting unique items from a two dimensional array python


code: print sum(dictionary.get(word, 0) for word in words.split())

rawDescription: Keeping values from an if statement

description: Keeping values from an if statement


code:
for i in my_range(0, 1, 10):
  print i

rawDescription: Is there a built-in Python function to generate 100 numbers from 0 to 1?


code:
import numpy as np, time
from multiprocessing import Pool

def countconvolve(N):
    np.random.seed() # ensure seed is random

    count = 0
    iters = 1000000 # 1million

    l=12
    k=12
    l0=l+k-1

    for n in range(N):
        t = np.random.choice([-1,1], size=l0 * iters)
        v = np.random.choice([-1,1], size = l * iters)

        for i in xrange(iters):
            if (not np.convolve(v[(l*i):(l*(i+1))], t[(l0*i):(l0*(i+1))], 'valid').any()):
                count += 1

    return count

if __name__ == '__main__':
    start = time.clock()

    num_processes = 8
    N = 13

    pool = Pool(processes=num_processes)
    res = pool.map(countconvolve, [N] * num_processes)
    print res, sum(res)

    print (time.clock() - start)

rawDescription: How to get the hardest part to take longest in python

description: Get the hardest part to take longest in python


code:
from bs4 import BeautifulSoup
import requests

url = 'http://www.boxofficemojo.com/movies/?page=daily&view=chart&id=hungergames3.htm'

response = requests.get(url)
soup = BeautifulSoup(response.content)

for tr in soup.find('div', id='chart_container').find_next_sibling('table').find_all('tr')[1:]:
    print [td.text for td in tr('td')]

rawDescription: Python BeautifulSoup Mix Matching items in Table


code:
import os

def findAll(fname, start_dir=os.getcwd()):
  my_files = []
  for root, dirs, files in os.walk(start_dir):
    for name in files:
      if name == fname:
        my_files.append(os.path.join(root, name))
  return my_files

print findAll('file1.txt')
print findAll('file1.txt', '/some/other/starting/directory')

rawDescription: return text file path

description: Return text file path


code:
import rdflib

RDF = rdflib.namespace.RDF

#Parse the file
g = rdflib.Graph()
g.parse("zot.rdf")

#So that we are sure we get something back
print "Number of triples",len(g)

#Couple of handy namespaces to use later
BIB = rdflib.Namespace("http://purl.org/net/biblio#")
FOAF = rdflib.Namespace("http://xmlns.com/foaf/0.1/")

#Author counter to print at the bottom
i=0

#Article for wich we want the list of authors
article = rdflib.term.URIRef("http://www.ncbi.nlm.nih.gov/pubmed/18273724")

#First loop filters is equivalent to "get all authors for article x"
for triple in g.triples((article,BIB["authors"],None)):

    #This expresions removes the rdf:type predicate cause we only want the bnodes
    # of the form http://www.w3.org/1999/02/22-rdf-syntax-ns#_SEQ_NUMBER
    # where SEQ_NUMBER is the index of the element in the rdf:Seq
    list_triples = filter(lambda y: RDF['type'] != y[1], g.triples((triple[2],None,None)))

    #We sort the authors by the predicate of the triple - order in sequences do matter ;-)
    # so "http://www.w3.org/1999/02/22-rdf-syntax-ns#_435"[44:] returns 435
    # and since we want numberic order we do int(x[1][44:]) - (BTW x[1] is the predicate)
    authors_sorted =  sorted(list_triples,key=lambda x: int(x[1][44:]))

    #We iterate the authors bNodes and we get surname and givenname
    for author_bnode in authors_sorted:
        for x in g.triples((author_bnode[2],FOAF['surname'],None)):
            author_surname = x[2]
        for y in g.triples((author_bnode[2],FOAF['givenname'],None)):
            author_name = y[2]
        print "author(%s): %s %s"%(i,author_name,author_surname)
        i += 1

rawDescription: How to access members of an rdf list with rdflib (or plain sparql)

description: Access members of an rdf list with rdflib (or plain sparql)


code:
print re.sub(r"\\\[(.*?)\\\]", repl, "frfrfr\nfrrffr
\[something\]
frff\nfrfrr", flags=re.S)
print re.sub("\\\\\\[(.*?)\\\\\\]", repl, "frfrfr\nfrrffr
\[something\]
frff\nfrfrr", flags=re.S)

rawDescription: Matching \[\] in Python regexes

description: Matching \[\] in Python regexes


code:
webpage_text = webpage_text.upper()
for name in ['CARIBOU COFFEE', 'EXPRESS', 'SEARS']:
    if name in webpage_text:
        print name, 'found!'

rawDescription: Fuzzy matching a string within a large body of text in Python (url)


rawDescription: how to encode url in python

description: Encode url in python


code: print 'ĉ'.decode('utf-8').upper()

rawDescription: Convert Unicode/UTF-8 string to lower/upper case using pure & pythonic library


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

Manchester United F.C.

Manchester United Football Club is an English professional football club, based in Old Trafford, Greater Manchester that plays in the Premier League. Founded as Newton Heath LYR Football Club in 1878, the club changed its name to Manchester United in 1902 and moved to Old Trafford in 1910 and is one of the most successful clubs in English football.

Manchester United have won the most League titles (20) of any English club,[3] a joint record 11 FA Cups, four League Cups, and a record twenty FA Community Shields. The club has also won three European Cups, one UEFA Cup Winners' Cup, one UEFA Super Cup, one Intercontinental Cup, and one FIFA Club World Cup. In 1998–99, the club won a continental treble of the Premier League, the FA Cup and the UEFA Champions League.

Early years (1878–1945)

Manchester United was formed in 1878 as Newton Heath LYR Football Club by the Carriage and Wagon department of the Lancashire and Yorkshire Railway (LYR) depot at Newton Heath.[15] The team initially played games against other departments and rail companies, but on 20 November 1880, they competed in their first recorded match; wearing the colours of the railway company – green and gold – they were defeated 6–0 by Bolton Wanderers' reserve team.[16] By 1888, the club had become a founding member of The Combination, a regional football league. Following the league's dissolution after only one season, Newton Heath joined the newly formed Football Alliance, which ran for three seasons before being merged with the Football League. This resulted in the club starting the 1892–93 season in the First Division, by which time it had become independent of the rail company and dropped the "LYR" from its name.[15] After two seasons, the club was relegated to the Second Division.[15]

''')
for header in soup.find_all(['h1', 'h2', 'h3']):
    print header.get_text()
    for elem in header.next_siblings:
        if elem.name and elem.name.startswith('h'):
            break
        if elem.name == 'p':
            print elem.get_text()

rawDescription: Print heading and corresponding paragraph in beautiful soup


code:
gen = enumerate(['a','b','c','d'])
for i, line in gen:
    print str(i) + ', ' + line
    if (i == 1):
        print 'saw 1'
        print 'next: ' + gen.next()[1]

rawDescription: make my enumerated index correspond if I manually peek forward in the generator?

description: Make my enumerated index correspond if I manually peek forward in the generator


code:
class MyHandler(tornado.websocket.WebSocketHandler):

    def check_origin(self, origin):
        return True

    def open(self):
        print "connection opened"
        self.write_message("connection opened")

    def on_close(self):
        print "connection closed"

    def on_message(self,message):
        print "Message received: {}".format(message)
        self.write_message("message received")

rawDescription: tornado 403 GET warning when opening websocket

description: Tornado 403 GET warning when opening websocket


code:
import termcolor

def colorChanger(s, firstSeg, secondSeg, thirdSeg, fourthSeg, hyphen):
    colors = [firstSeg, secondSeg, thirdSeg, fourthSeg]
    frags = s.split('-')
    for ind, color in enumerate(colors):
        frags[ind] = frags[ind].replace(frags[ind], termcolor.colored(frags[ind], color))
    return termcolor.colored('-', hyphen).join(frags)

string = "type-name-function-location"

print colorChanger(string, 'red', 'green', 'yellow', 'magenta', 'cyan')
print colorChanger(string, 'yellow', 'blue', 'cyan', 'red', 'grey')

rawDescription: Color a strings segments in python

description: Color a strings segments in python


code:
def makestruct(name, fields):
    fields = fields.split()
    import textwrap
    template = textwrap.dedent("""\
    class {name}(object):
        __slots__ = {fields!r}
        def __init__(self, {args}):
            {self_fields} = {args}
        def __getitem__(self, idx):
            return getattr(self, fields[idx])
    """).format(
        name=name,
        fields=fields,
        args=','.join(fields),
        self_fields=','.join('self.' + f for f in fields))
    d = {'fields': fields}
    exec template in d
    return d[name]

rawDescription: What is the fastest (to access) struct-like object in Python?


code:
print df
#       name                  most_exhibitions  India  Netherlands  Pakistan  \
#index
#a       Bob           USA (1) Netherlands (5)                   5
#b       Joe                United Kingdom (2)
#c      Alex  China (3) India (5) Pakistan (8)      5                      8
#d      Bill                USA (11) India (4)      4
#
#      China USA United Kingdom
#index
#a             1
#b                            2
#c         3
#d            11

rawDescription: How do I make new columns in dataframe from a row of a different column?

description: Make new columns in dataframe from a row of a different column


code:
import re

from bs4 import BeautifulSoup

page = """
<script type="text/javascript">
            var logged = true;
            var video_id = 59374;
            var item_type = 'official';

            var debug = false;
            var baseUrl = 'http://www.example.com';
            var base_url = 'http://www.example.com/';
            var assetsBaseUrl = 'http://www.example.com/assets';
            var apiBaseUrl = 'http://www.example.com/common';
            var playersData = [{"playerId":"showsPlayer","userId":true,"solution":"flash","playlist":[{"itemId":"5090","itemAK":"Movie"}]];
</script><script type="text/javascript" >
"""
soup = BeautifulSoup(page)

pattern = re.compile(r'"playerId":"(.*?)"', re.MULTILINE | re.DOTALL)
script = soup.find("script", text=pattern)

print pattern.search(script.text).group(1)

rawDescription: how to fetch javascript contents in python

description: Fetch javascript contents in python


code:
def get_data():
    name = raw_input("What is your name: ")
    scores = []
    for i in range(5):
        scores.append(int(input("what are your test scores? ")))
    return name, scores

def drop_low_score(scores):
    sorted_scores = sorted(scores)
    print "Dropping", sorted_scores[0]
    highest_four_scores = sorted_scores[1:]
    return scores

def find_avg(scores):
    avg = sum(scores) / float(len(scores))
    print name, "your average = ", avg
    return avg

name, scores = get_data()
scores = drop_low_score(scores)
avg = find_avg(scores)

rawDescription: Ask for five values, subtract the lowest and take the average


code:
class BankAccount(object):
    def __init__(self, deposit):
        self.amount = deposit

    def withdraw(self, amount):
        self.amount -= amount

    def deposit(self, amount):
        self.amount += amount

    def balance(self):
        return self.amount

# Let me create an instance of 'BankAccount' class with the initial
# balance as $2000.
myAccount = BankAccount(2000)

# Let me check if the balance is right.
print myAccount.balance()

# Let me deposit my salary
myAccount.deposit(10000)

# Let me withdraw some money to buy dinner.
myAccount.withdraw(15)

# What's the balance left?
print myAccount.balance()

rawDescription: Is it necessary to include __init__ as the first function everytime in a class in Python?


code:
from xml.etree import ElementTree as ET

XML = """
<root>

<member>The Minister for Overseas Development (Mr. Neil Marten)
</member><membercontribution>: a policy
<col>16</col>
foobar barforb </membercontribution>

</root>
"""

root = ET.fromstring(XML)

for pid in root.findall(".//p"):
    for cont in pid.findall('membercontribution'):
        for col in cont.findall('col'):
            col_tail = col.tail.strip()          # Get the tail of "col"
            cont.remove(col)                     # Remove "col"
            cont.text = cont.text.strip() + " "  # Replace trailing whitespace with single space
            cont.text = cont.text + col_tail     # Add the tail to "membercontribution"

print ET.tostring(root)

rawDescription: How do I remove XML elements without removing content from the elements' tail?

description: Remove XML elements without removing content from the elements' tail


code:
import numpy as np
from timeit import timeit

N, M = 300, 300

a = np.random.randint(100, size=(N,M))
b = np.random.randint(100, size=(N,))

a2 = a.tolist()
b2 = b.tolist()

s1="""
[[m*n for n in second] for m, second in zip(b2,a2)]
"""

s2 = """
(a.T*b).T
"""

s3 = """
a*b[:,None]
"""

assert np.allclose([[m*n for n in second] for m, second in zip(b2,a2)], (a.T*b).T)
assert np.allclose([[m*n for n in second] for m, second in zip(b2,a2)], a*b[:,None])

print 's1: {:.4f}'.format(
    timeit(stmt=s1, number=10**3, setup='from __main__ import a2,b2'))
print 's2: {:.4f}'.format(
    timeit(stmt=s2, number=10**3, setup='from __main__ import a,b'))
print 's3: {:.4f}'.format(
    timeit(stmt=s3, number=10**3, setup='from __main__ import a,b'))

rawDescription: Why list comprehension is much faster than numpy for multiplying arrays?


code: print "Tmin=", Tmin[0]

rawDescription: Writing to a CSV with python without brackets


code:
# all two digit numbers that are not repeated digits
li=[e for e in range(10,100) if len(set(str(e)))==2]
lucky=[]
while True:
    new_li=[]
    for e in li:
        s=str(e)
        ns=str(abs(int(s[-2])-int(s[-1])))
        if ns not in s:
            new_li.append(int(s+ns))
    if new_li:
        li=new_li
        lucky.extend(new_li)
    else:
        break

print lucky
print max(lucky)

rawDescription: Lucky Number Finder

description: Lucky Number Finder


code:
if options.verbose:
  print ""

rawDescription: My first python program: can you tell me what I'm doing wrong?


code:
import requests
from lxml.cssselect import CSSSelector
from lxml import html

s='''Mobiles Tablets
Fashion
Fashion
Printers Scanners
Audio Visual
Audio Visual
Cameras
Computers Servers
Computers Servers'''

#mainPage = requests.get("http://www.example.com/catalog/seo_sitemap/category/?p=1")
mainTree = html.fromstring(s)

mainTree = html.fromstring(s)
lnks = set([i.get('href') for i in mainTree.cssselect('a')])
for i in lnks:
    print i

rawDescription: Selecting specific anchor tags using python


code:
# ab_worker.py
import os
import time
import boto.swf.layer2 as swf

DOMAIN = 'stackoverflow'
ACTIVITY1 = 'ServerAActivity'
ACTIVITY2 = 'ServerBActivity'
VERSION = '1.0'

class MyBaseWorker(swf.ActivityWorker):

    domain = DOMAIN
    version = VERSION
    task_list = None

    def run(self):
        activity_task = self.poll()
        print activity_task
        if 'activityId' in activity_task:
            # Get input.
            # Get the method for the requested activity.
            try:
                self.activity(activity_task.get('input'))
            except Exception, error:
                self.fail(reason=str(error))
                raise error

            return True

    def activity(self, activity_input):
        raise NotImplementedError

class WorkerA(MyBaseWorker):
    task_list = 'a_tasks'

    def activity(self, activity_input):
        result = str(time.time())
        print 'worker a reporting time: %s' % result
        self.complete(result=result)

class WorkerB(MyBaseWorker):
    task_list = 'b_tasks'

    def activity(self, activity_input):
        result = str(os.getpid())
        print 'worker b returning pid: %s' % result
        self.complete(result=result)

rawDescription: Using Amazon SWF To communicate between servers


code:
def flatten(structure):
    if isinstance(structure, basestring):
        return [structure]
    ret = []
    if isinstance(structure, list):
        for v in structure:
            ret.extend(flatten(v))
    elif isinstance(structure, dict):
        for k, v in structure.items():
            ret.extend(k + '/' + f for f in flatten(v))
    return ret

print sorted(flatten(structure))

rawDescription: Flatten Arbitrary Length of Dictionary Items Into List of Paths in Python


code: print [m.groupdict() for m in re.finditer('toto=(?P<toto>\d+)\,\sbip=(?P<bip>\w+)', my_str)]

rawDescription: How to use symbolic group name using re.findall()

description: Use symbolic group name using re.findall()


code:
import threading
import time
class WaitThread(threading.Thread):
    def __init__(self, time_to_wait, text):
        threading.Thread.__init__(self)
        self.time_to_wait = time_to_wait
        self.text = text
    def run(self):
        # Wait!
        time.sleep(self.time_to_wait)
        print self.text

# Thread will wait for 1 second before it finishes
thread_a = WaitThread(1, "Thread a has ended!")
# Thread will wait for 2 seconds before it finishes
thread_b = WaitThread(2, "Thread b has ended!")

threads = []
threads.append(thread_a)
threads.append(thread_b)

# Start threads
thread_a.start()
thread_b.start()

# Join threads
for t in threads:
    t.join()

print "Both threads have ended!"

rawDescription: How to make mechanize script multthread in python

description: Make mechanize script multthread in python


code:
from itertools import groupby

headers=['Intake Received Date:', 'Intake ID:', 'Allegation Category:', 'Sub Categories:', 'Investigation Finding:']
sep='Intake Received Date:'
compgroup = []
for k, g in groupby(data, key=lambda x: x==sep):
    if not k:
        compgroup.append([sep]+list(g))

print ', '.join(e[0:-1] for e in headers)

for di in [dict(zip(*[iter(c)]*2)) for c in compgroup]:
    line=[]
    for h in headers:
        try:
            line.append(di[h])
        except KeyError:
            line.append('*')
    print ', '.join(line)

rawDescription: Transform Python list into columnar data


code: print data.decode("UTF-8")

rawDescription: python Socket send ascii command and receive response

description: Python Socket send ascii command and receive response


rawDescription: How to parse CSV file using pandas?

description: Parse CSV file using pandas


code:
def main():
    data = [1, 2, 3]
    def foo():
       x = data[2]
       return x + x
    print foo()
    data = [4, 5, 6]
    print foo()
    # if you want the nested function to close over its arguments:
    def bar(data=data):
       x = data[2]
       return x + x
    print bar()
    data = [7, 8, 9]
    print bar()

rawDescription: binding local variables in python

description: Binding local variables in python


code: print file_contents,

rawDescription: remove empty last line from string (output from pipe)

description: Remove empty last line from string (output from pipe)


code:
prime = [2]

odd_list = [x for x in range(100) if x % 2 != 0 and x > 2]
count = 0
for z in odd_list:
    for y in range(2, z):
        if z % y == 0:              # is not prime
            break
    else:
        prime.append(z)
        count += 1

    if len(prime) >= 10:
        break
print 'count = ', count
print 'length of prime =', len(prime)
print prime

rawDescription: Cannot get this loop to stop

description: Cannot get this loop to stop


code:
while 0 != multiprocessing.active_children():
    print q.get()

rawDescription: Producer/Consumer Multi producers and a single Consumer writing to File Python


code:
import json
with open('myfile') as f:
    l = json.load(f)
    print l

rawDescription: Get lists by reading file in Python

description: Get lists by reading file in Python


code:
import os.path
from multiprocessing import Pool
import sys
import time

def process_file(name):
    ''' Process one file: count number of lines and words '''
    linecount=0
    wordcount=0
    with open(name, 'r') as inp:
        for line in inp:
            linecount+=1
            wordcount+=len(line.split(' '))

    return name, linecount, wordcount

def process_files_parallel(arg, dirname, names):
    ''' Process each file in parallel via Poll.map() '''
    pool=Pool()
    results=pool.map(process_file, [os.path.join(dirname, name) for name in names])

def process_files(arg, dirname, names):
    ''' Process each file in via map() '''
    results=map(process_file, [os.path.join(dirname, name) for name in names])

if __name__ == '__main__':
    start=time.time()
    os.path.walk('input/', process_files, None)
    print "process_files()", time.time()-start

    start=time.time()
    os.path.walk('input/', process_files_parallel, None)
    print "process_files_parallel()", time.time()-start

rawDescription: Read txt file with multi-threaded in python


code:
app = Application.Attach(proc)
win = app.GetWindow('Window Caption')
print win.Name
box = win.Get[MultilineTextBox]('textBoxId')
print box.Text

rawDescription: How do I import the white project into an IronPython program?

description: Import the white project into an IronPython program


code:
print 'Processing partition %s' % label
for (col_num, column) in enumerate(zip(*rows)):
    print 'Column number: %d' % col_num
    for (row_num, element) in enumerate(column):
        print '[%d,%d]: %d' % (row_indices[row_num], col_num, element)

rawDescription: Iterate over two nested 2D lists where list2 has list1's row numbers


code:
import re

ss = ('Mr Dotwo bought two gifts for his two sons, two hours ago: two BICYCLES '
      'because his two sons wanted only two BICYCLES')
print ss,'\n\n'

print 'Replacing all "two" before the first "BICYCLES":\n'
reg = re.compile('(\\btwo\\b)|BICYCLES.+')
print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)

rawDescription: Python regular expressions - find a string in a file that occurs somewhere before another string?


code:
import binascii
x = '\x01\x41\x42\x43'
print x
repr(x)
print binascii.hexlify(x)

rawDescription: Print string as hex literal python

description: Print string as hex literal python


code:
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-1")
parser.add_option("-2")
options,args = parser.parse_args()
if options.__dict__["1"] is not None:
    print "Your number was %d." % (int("1"+options.__dict__["1"]))
elif options.__dict__["2"] is not None:
    print "Your number was %d." % (int("2"+options.__dict__["2"]))

rawDescription: Python argument to provide less-like limiting functionality


code:
x = range(10)
for i in x[:]:
    print i
    if i%2==0:
            x.remove(i)

rawDescription: Why is my list out of order consistently?


code:
import sys, time

sys.stdout.write('Content-Type: text/html;charset=utf-8\r\n\r\n')

print '<html><body>'
for i in range(10):
    print '<div>%i</div>'%i
    sys.stdout.flush()
    time.sleep(1)

rawDescription: Display the result on the webpage as soon as the data is available at server


code:
PathwayList = {}
for InFileName in FileList:
    sys.stderr.write("Processing file %s\n" % InFileName)
    InFile = open(InFileName, 'r')
    PathwayList[InFile] = InFile.readlines()
    InFile.close()

for filename, contents in PathwayList.items():
    # do something with contents which is a list of strings
    print filename, contents

rawDescription: python beginner - how to read contents of several files into unique lists?

description: Python beginner - how to read contents of several files into unique lists


code:
from collections import defaultdict

data = {'dog':['milo','otis','laurel','hardy'],'cat':['bob','joe'],'milo':['otis','laurel','hardy','dog'],'hardy':['dog'],'bob':['joe','cat']}

# create symmetric version of data
d = defaultdict(list)
for key, values in data.iteritems():
    for value in values:
        d[key].append(value)
        d[value].append(key)

visited = set()
def connected(key):
    result = []
    def connected(key):
        if key not in visited:
            visited.add(key)
            result.append(key)
            map(connected, d[key])
    connected(key)
    return result

print [connected(key) for key in d if key not in visited]

rawDescription: How to check if a key/value is repeated elsewhere in a dictionary using Python

description: Check if a key/value is repeated elsewhere in a dictionary using Python


code:
list = [ {'comment_id':'51799',   'user_id':'112801710', 'comment_date':'2015-12-07', 'comment_time': '00:03:21',   'comment_likes':'0'},
         {'comment_id':'51761',   'user_id':'112801710', 'comment_date':'2015-12-06',  'comment_time':'19:31:46',   'comment_likes':'3'},
         {'comment_id':'51764',   'user_id':'112801710', 'comment_date':'2015-12-06',  'comment_time':'19:54:19',   'comment_likes':'0'},
         {'comment_id':'51741',   'user_id':'112801710', 'comment_date':'2015-12-06',  'comment_time':'14:17:34',   'comment_likes':'2'},
         {'comment_id':'51768',   'user_id':'52879933',  'comment_date':'2015-12-06',  'comment_time':'20:03:34',   'comment_likes':'0'},
         {'comment_id':'51766',   'user_id':'52879933',  'comment_date':'2015-12-06',  'comment_time':'21:33:34',   'comment_likes':'0'}]

def combine(list):
    result = {}
    for item in list:
        resItem = result.get(item['user_id'], None)
        if not resItem:
            resItem =  {'comment_likes': int(item['comment_likes']), 'comments_left': 1}
        else:
            resItem['comment_likes'] += int(item['comment_likes'])
            resItem['comments_left'] +=1
        result[item['user_id']] = resItem

    print result

combine(list)

rawDescription: For list of dicts: merge same key values, sum another diff. values, count each merge iteration +1


code:
headers = {'Accept': 'application/json', 'Authorization': 'Bearer '+access_token}
json_data = {'rajNames': ['WAR']}
params = {'jsonRequest': json.dumps(json_data)}
url = 'http://258.198.39.215:8280/areas/0.1/get/raj/name'
r = requests.get(url, params=params, headers=headers)
print _r.url

rawDescription: Python requests module url encode

description: Python requests module url encode


code:
from requests.packages.urllib3.connectionpool import HTTPConnectionPool

def _make_request(self,conn,method,url,**kwargs):
    response = self._old_make_request(conn,method,url,**kwargs)
    sock = getattr(conn,'sock',False)
    if sock:
        setattr(response,'peer',sock.getpeername())
    else:
        setattr(response,'peer',None)
    return response

HTTPConnectionPool._old_make_request = HTTPConnectionPool._make_request
HTTPConnectionPool._make_request = _make_request

import requests

r = requests.get('http://www.google.com')
print r.raw._original_response.peer

rawDescription: How do I get the IP address from a http request using the requests library?

description: Get the IP address from a http request using the requests library


code:
# Test.py
import ctypes

lib = ctypes.cdll.TestDLL

# this creates a c-style char pointer, initialized with a string whose
# memory is managed by PYTHON! do not attempt to free it through the DLL!
cstr = ctypes.c_char_p("hello ctypes")

# call the dll function that returns a char pointer
# whose memory is managed by the DLL.
p = lib.stringdup(cstr)

# p is just an integer containing the memory address of the
# char array. therefore, this just prints the address:
print p

# this prints the actual string
print ctypes.c_char_p(p).value

# free the memory through the DLL
lib.stringfree(p)

rawDescription: How to pass pointer back in ctypes?

description: Pass pointer back in ctypes


code:
good_reviews = """This is great!
Wow, it amazes me...
An hour of show, a lifetime of enlightment
"""
bad_reviews = """Comme si, Comme sa.
I just wasted my foo bar on this.
An hour of s**t, ****.
"""
with open('/tmp/good_reviews.txt', 'w') as fout:
    fout.write(good_reviews)
with open('/tmp/bad_reviews.txt', 'w') as fout:
    fout.write(bad_reviews)

reviews = []
with open('/tmp/good_reviews.txt', 'r') as fingood, open('/tmp/bad_reviews.txt', 'r') as finbad:
    reviews = ([(review, 'good_review') for review in fingood.readlines()] + [(review, 'bad_review') for review in finbad.readlines()])

print reviews

rawDescription: How to read and label line by line a text file using nltk.corpus in Python

description: Read and label line by line a text file using nltk.corpus in Python


code:
channels = []
for elem in tv_elem.getchildren():
    if elem.tag == 'channel':
       channels.append( (elem.attrib['id'], self.load_channel(elem)) )
       for channel_value in channels:
           print channel_value[1].get_display_name()

rawDescription: Print dict items in alphabet order

description: Print dict items in alphabet order


code:
a='hi'
b='hi'
print "The number is %d and the text is %s" %(a, b)

rawDescription: printing variables in python using % and variable name

description: Printing variables in python using % and variable name


code:
from collections import OrderedDict

dictclass = OrderedDict

class Foo(dictclass):
    def __getitem__(self,key):
        try:
            return dictclass.__getitem__(self,key)
        except KeyError:
            pass

        data = key*2
        self[key] = data
        return data

    def __contains__(self,whatever):
        print('contains: {}'.format(whatever))
        return dictclass.__contains__(self,whatever) or 'bar' in whatever

    def __setitem__(self, key, value, PREV=0, NEXT=1, dict_setitem=dict.__setitem__):
        'od.__setitem__(i, y) <==> od[i]=y'
        # Setting a new item creates a new link at the end of the linked list,
        # and the inherited dictionary is updated with the new key/value pair.
        print('key not in self: {}'.format(key not in self))
        if key not in self:
            root = self._OrderedDict__root
            last = root[PREV]
            last[NEXT] = root[PREV] = self._OrderedDict__map[key] = [last, root, key]
        dict_setitem(self, key, value)

    def __iter__(self):
        'od.__iter__() <==> iter(od)'
        # Traverse the linked list in order.
        NEXT, KEY = 1, 2

        root = self._OrderedDict__root
        curr = root[NEXT]
        print('curr: {}'.format(curr))
        print('root: {}'.format(root))
        print('curr is not root: {}'.format(curr is not root))

        while curr is not root:
            yield curr[KEY]
            curr = curr[NEXT]

a = Foo()
print a['bar']
# barbar

print a.keys()
# ['bar']

rawDescription: Why does overriding __contains__ break OrderedDict.keys?


code:
x  = range (2)

y = []

for i in range (5):
    y.append(x[:]) #You should append copy of x
    x[0]+=1

print y

rawDescription: Python List of Lists Incrementing

description: Python List of Lists Incrementing


code:
import sys, math
if len(sys.argv) == 3 and sys.argv[1].replace(".","").isdigit() and sys.argv[2].replace(".","").isdigit():
    v, theta = float(sys.argv[1]) , float(sys.argv[2])
    t = (v * math.sin(theta)) / float(9.81)
    print "assuming that g = 9.81"
    print "projectile motion =", t
else:
    print "Usage:", "python %s velocity theta" % sys.argv[0]

rawDescription: How to get input from user and apply calculation in Python?

description: Get input from user and apply calculation in Python


code:
import threading
import time
from Queue import Queue, Empty
from random import choice

class worker(threading.Thread):
  def __init__(self):
    threading.Thread.__init__(self)
    self.q = Queue()
  def run(self):
    while True:
      try:
        r = self.q.get_nowait()
      except Empty:
        r = None
      if r is None:
        time.sleep(1.0)
        continue
      # do something with 'r'
      print '%s: Handled  request %s' % (self, r)
  def push(self, r):
    self.q.put(r)

workers = [worker() for i in range(5)]
for w in workers:
  w.start()

rawDescription: SQLAlchemy + Requests Asynchronous Pattern


code:
import random
ss = ['I am here', 'I like spring', 'My house has two floors', 'Sun is bright']
random.shuffle(ss)
for s in ss:
    print s

rawDescription: Printing sequence of different print commands in python


code:
from lxml import etree as ET

xmlString = """
    <some_root_name>
        <tag_x>bubbles</tag_x>
        <tag_y>car</tag_y>
        <tag...>42</tag...>
    </some_root_name> """

document = ET.fromstring(xmlString)
for elementtag in document.getiterator():
   print "elementtag name:", elementtag.tag

rawDescription: parsing an xml file for unknown elements using python ElementTree

description: Parsing an xml file for unknown elements using python ElementTree


code:
import numpy
import timeit

def makebigfile(outname):
    data = numpy.random.standard_normal((100000, 7))
    numpy.savetxt(outname, data, delimiter=",")

def csvdump(files, original=True):

        date, time, opens, high, low, close, vol = numpy.genfromtxt(str(files)+'.csv', unpack=True, delimiter=',')
        if original:
            for line in high:
                x=str(1/line)
                outr=open(str(files)+"inverse-original.txt", "a")
                outr.write(x)
                outr.write('\n')
        else:
            numpy.savetxt(str(files)+"inverse-savetxt.txt",1/high)

makebigfile('foo.txt')

print timeit.timeit(stmt='__main__.csvdump("foo",True)',setup='import __main__',number=1000)
print timeit.timeit(stmt='__main__.csvdump("foo",False)',setup='import __main__',number=1000)

rawDescription: Fasted Python way to bulk csv convert outside of using pandas


rawDescription: use variable to specify format in python

description: Use variable to specify format in python


code:
from django.contrib.contenttypes.models import ContentType

from django.conf import settings

try:
    #perform this when starting the project
    for definition in settings.DROPPABLE_MODELS:
        #parse contenttype
        parsed=definition.split(':')
        dropcode=parsed[1]
        parsed=parsed[0].split('.')
        appname=parsed[0]
        modelname=parsed[1]

        #get the models class for the specified contenttype
        model_class=ContentType(app_label=appname, model=modelname).model_class()

        #create class Mixin, containing the dropcode property
        class DragToTagable:
            dropcode = dropcode

        #add DragToTagable as a base class to the model class
        model_class.__bases__+=(DragToTagable,)

except AttributeError:
    pass

except:
    import sys
    print "Unexpected error:", sys.exc_info()[0]
    raise

rawDescription: How can i extend a model through a reusable app?

description: Extend a model through a reusable app


rawDescription: How to call python script from java and return long text?

description: Call python script from java and return long text


code:
itm = db.doctors.find_one({"email":doc_mail})
print itm.get('_id')

rawDescription: How do i get id of an object in pymongo?

description: Get id of an object in pymongo


code:
for x in test_list:
    if x.value == value:
        print "i found it!"
        break

rawDescription: Find object in list that has attribute equal to some value (that meets any condition)


code:
x="abc abc abc"
print re.sub(r"abc\s*","",x,count=1)

rawDescription: Regex: Replacing only one occurrence

description: Regex: Replacing only one occurrence


code:
import numpy as np

def perpendicular( a ) :
    b = np.empty_like(a)
    b[0] = -a[1]
    b[1] = a[0]
    return b

def normalize(a):
    a = np.array(a)
    return a/np.linalg.norm(a)

if __name__ == "__main__":
    a = [1,2]
    print perpendicular(normalize(a))
    b = (4,-6)
    print perpendicular(normalize(b))

rawDescription: Normalise and Perpendicular Function in python


code:
ts = [-2, -2, -4,  0, -1,  0, -1,  1, -2,  2, -3, -2,  3,  1, -2,
      -3, -4, -3,  3, -3, -3, -3, -1, -3,  3,  3, -4, -1, -1,  0]

# find positions of 2 consecutive negatives
neg = [i for i in range(len(ts)-1) if ts[i]<0 and ts[i+1] < 0]
print neg

# find locations of 2 consecutive positives + last year
pos = [i for i in range(len(ts)-1) if ts[i]>0 and ts[i+1] > 0] + [len(ts)]
print pos

# find the earliest neg for each pos
draughts = []
for p in pos:
    try:
        draughts.append((neg[0],p))
        neg = [n for n in neg if n > p]
    except IndexError:
        # no more negatives left, no draught in progress
        break

print draughts

rawDescription: Python - Find sequence in array: start when two values below threshold, end when two values above threshold


code:
import re

post_path = re.compile(r'"POST (/\S+) HTTP')

match = post_path.search(apache_log['@message'])
if match:
    print match.group(1)

rawDescription: Is there any way to make this function look nicer?


code:
from bs4 import BeautifulSoup

data = """

                            Expiry Date

                        2016

"""

soup = BeautifulSoup(data)
span = soup.select('tr.odd table.zp > tbody > tr > td > span')[0]
print span.next_sibling.strip()  # prints 2016

rawDescription: Using Python + BeautifulSoup to pick up text in a table on webpage


code:
import multiprocessing

class I(object):
    def __init__(self, maxsize=0):
        self.q = multiprocessing.Queue(maxsize)

    def __getattr__(self, attr):
        if hasattr(self.q, attr):
            return getattr(self.q, attr)

    def __iter__(self):
        return self

    def next(self):
        item = self.q.get()
        if item == 'Done':
            raise StopIteration
        return item

def thisworker(item):
    if item == 1:
        q.put(3)
    if item == 2:
        q.put('Done')
    print 'got this item: %s' % item
    return item

q=I()

q.put(1)
q.put(2)
q.put(5)

the_pool = multiprocessing.Pool(2)  # 2 workers
print list(the_pool.imap(thisworker, q))

rawDescription: Iterable multiprocessing Queue not exiting


code:
import random

cards = {1.1:"Ace of Spades",
     1.2:"Ace of Clubs",
     1.3:"Ace of Diamonds",
     1.4:"Ace of Hearts",
      2.1:"Two of Spades",
     2.2:"Two of Clubs",
     2.3:"Two of Diamonds",
     2.4:"Two of Hearts",

     3.1:"Three of Spades",
     3.2:"Three of Clubs",
     3.3:"Three of Diamonds",
     3.4:"Three of Hearts",

     4.1:"Four of Spades",
     4.2:"Four of Clubs",
     4.3:"Four of Diamonds",
     4.4:"Four of Hearts"}

used_cards = []

def add_card():
    used_cards.append(cards[random.randint(1, 5) + random.randint(1, 5) * 0.1])

add_card()
add_card()

print used_cards

rawDescription: Append one value from a dictionary to a list


code:
for line in shakes:
    if re.search(r"(?i)"+'|'.join(word_lst), line):
        print line,

rawDescription: python search file for a list of words

description: Python search file for a list of words


code:
print A, # note, the trailing comma suppresses the newline
print B,
print C

rawDescription: Unexpected Python print behavior

description: Unexpected Python print behavior


code:
def iterparent(tree):
    for parent in tree.getiterator():
        for child in parent:
            yield parent, child

tree = etree.fromstring(data)
for parent, child in iterparent(tree):
    if child.tag == "letter" and child.attrib.get('name') == "E":
        parent.remove(child)
        parent.extend(child)

print etree.tostring(tree)

rawDescription: Remove a node from etree but leaving child


code:
from PyQt4 import QtGui

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.cb = QtGui.QComboBox(self)
        self.cb.addItem("One")
        self.cb.addItem("Two")
        self.cb.activated[str].connect(self.selected)

    def selected(self, text):
        self.selected_text = text
        print self.selected_text

app = QtGui.QApplication([])
mw = MainWindow()
mw.show()
app.exec_()

rawDescription: PYQT Qcombobox set value selected to a variable


code:
import timeit

ns = [1,2,3, 5, 10, 50, 100]
print 'n', '\t', 'range', '\t', 'xrange'
for n in ns:
    t1 = timeit.timeit("for i in range({}): pass".format(n))
    t2 = timeit.timeit("for i in xrange({}): pass".format(n))
    print n, '\t', t1, '\t', t2

rawDescription: In Python, When yield cost over the return an list?


code: line = line.replace(v, str(int(v) + 00001))

rawDescription: Python appending after updating the line to a file


code:
x="#water #atlantic ocean #sea"
print re.findall(r"(#[^#]*)(?=[^#]*(#[^#]*))",x)

rawDescription: Regex: select all groups of two (hashtag) words next to each other


code:
sum_grad = T.grad(cost=sym2.sum(), wrt=mat)
f_sum_grad = theano.function([mat], sum_grad)

print f_sum_grad(m)
# [[ 1.  2.  2.]
#  [ 0.  1.  2.]
#  [ 0.  0.  1.]]

rawDescription: Initializing a symmetric Theano dmatrix from its upper triangle


code:
import re

def checkio(text):

    words = [w for w in re.split('[,.?!]', text.lower()) if w and not re.search(r'\d', w)]
    print words ## prints [] in this case

rawDescription: Removing words containing digits from a given string


code:
import time

TIMER = None

def start():
    global TIMER
    TIMER = time.time()

def stop(msg=None):
    global TIMER
    msg = msg or "TIMER"
    if TIMER:
        print('%s -- %s' % (msg, time.time() - TIMER))
    else:
        print 'ERROR: TIMER is not set'

rawDescription: Python creating timestamps at different locations/classes to log function block run time


code:
for index in x:
    name = motif[index]
    print name
    for value in column[index]:
        if value == "1":
            print coordinates
            out = open("%s.bed" %name, "a")
            out.write(str(coordinates)+"\n")
        elif value == "0":
            pass

rawDescription: Using index in Python to iterate through columns


code:
curs.execute("SELECT * FROM foo")
print list(curs)

[(1, u'bar'), (2, u'baz')]

rawDescription: Fetch All In APSW

description: Fetch All In APSW


code:
if 'foo' in 'foobar':
    print True

if 'foo' in 'barfoo':
    print True

rawDescription: strncmp in python

description: Strncmp in python


code:
old_values = ('0000}', '0000J', '0000K', '0000L', '0000M', '0000N')
new_values = ('   -0', '   -1', '   -2', '   -3', '   -4', '   -5')
value_map = dict(zip(old_values, new_values))

file_snippet = '00000000000000010000}0000000000000000000200002000000000000000000030000J0000100000000000000500000000000000000000000' # each line is >7K chars long and there are over 6 gigs of text data

result = []
for chunk in [ file_snippet[i:i+5] for i in range(0, len(file_snippet), 5) ]:
    if chunk in value_map:
        result.append(value_map[chunk])
    else:
        result.append(chunk)

result = ''.join(result)
print result

rawDescription: multiple replacement values in python3

description: Multiple replacement values in python3


code:
from collections import namedtuple
from bs4 import BeautifulSoup
import requests

url = "http://www.mortgagenewsdaily.com/mortgage_rates/"
r = requests.get(url)

soup = BeautifulSoup(r.content)

Item = namedtuple('Item', "product,today,yesterday,change,low,high")
for tr in soup.select("table.mtg-rates tr.rate-row"):
    item = Item(*(td.get_text(strip=True) for td in tr.find_all("td")))
    print item

rawDescription: Scraping data with BeautifulSoup issue

description: Scraping data with BeautifulSoup issue


code:
if __name__ == '__main__':

    mgr = Manager()

    # keep strong references to the subjects, because
    # we will delete them explicitly
    a1 = A('a1')
    a2 = A('a2')
    mgr.monitorSubject(a1)
    mgr.monitorSubject(a2)

    # monitor observers directly, and do NOT keep
    # strong references or they will not be deleted
    mgr.monitorObserver( B('b1', a1) )
    mgr.monitorObserver( B('b2', a1) )
    mgr.monitorObserver( B('b3', a2) )
    mgr.monitorObserver( B('b4', a2) )

    # show the starting state
    print mgr

    print "Deleting a1..."
    del a1
    print mgr

    print "Deleting a2..."
    del a2
    print mgr

rawDescription: Using the observer pattern to delete an Observer when a Subject has been "deleted" in Python


code:
for link in soup.find_all('a'):
    print link['href'], ''.join(link.stripped_strings), link.next_sibling

rawDescription: Splitting a BeautifulSoup into 2 Soup-Trees


code:
#fabfile.py
import ast
from fabric.api import run

def getcontroller():
    """Return controller value."""
    cmd = """
import dbus, sys

bus = dbus.SystemBus()
remote_object = bus.get_object('daemon.location', '/daemon')
controller = dbus.Interface(remote_object, 'daemon.path')

print repr(controller.getValue())
""" #NOTE: you must escape all quotation marks
    output = run('python -c "%s"' % cmd)
    tuple_ = ast.literal_eval(output)
    print tuple_[0]

rawDescription: How to get a variable from the shell in python?

description: Get a variable from the shell in python


code:
def count_line_occurrences(ref_list,input_list):
  line_counter = {}

  ref_list = map(lambda x: x.strip(" \t\n"),ref_list)
  input_list = map(lambda x: x.strip(" \t\n"),input_list)

  # Initialization
  for ref_line in ref_list:
    line_counter[ref_line] = 0

  for ref_line in ref_list:
    #print ref_line
    for input_line in input_list:
      #print input_line
      if str(input_line).find(str(ref_line)) != -1:
        print 'found ' + ref_line
        line_counter[ref_line] += 1
  return line_counter

rawDescription: python - find occurrences of strings from a reference file within an input file

description: Python - find occurrences of strings from a reference file within an input file


code:
import glob
for curSurge in glob.glob("/path/to/surges/*.shp"):
    blah, month, year = curSurge.split('.')[0].split('_')
    matches = glob.glob('/path/to/precip/*_%s_%s.shp'%(month, year))
    if len(matches) != 1:
        raise Exception, "Oh No!, We found %d matches instead of 1!"%(len(matches))
    run_near_and_make_tabular_join(curSurge, matches[0])

rawDescription: searching for shapefile within a directory

description: Searching for shapefile within a directory


code:
import json
import sqlite3

db = sqlite3.connect('fluxos.sqlite')
traffic = json.load(open('xxx.json'))

someitem = traffic.itervalues().next()
columns = list(someitem.keys())
columns.remove('medicoes')
columns.remove('regime')

query = "insert into medicoes (timestamp,{0}) values (?{1})"
query = query.format(",".join(columns), ",?" * len(columns))
print query

for timestamp, data in traffic.iteritems():
    keys = (timestamp,) + tuple(data[c] for c in columns)
    c = db.cursor()
    c.execute(query)
    c.close()

rawDescription: Convert JSON to SQLite in Python - How to map json keys to database columns properly?


code:
for img in soup.find_all('img'):
    if img.parent.name == 'a':
        print "Parent is a link"

rawDescription: Get immediate parent tag with BeautifulSoup in Python