code:
import itertools
import operator
import re
import csv
import pprint

class TaggedLine(str):
    """
    Override str to allow a tag to be added.
    """
    def __new__(cls, val, tag):
        return str.__new__(cls, val)

    def __init__(self, val, tag):
        super(TaggedLine, self).__init__(val)
        self.tag = tag

def sections(stream):
    """
    Tag each line of the stream with its [section] (or None)
    """
    section_pattern = re.compile('\[(.*)\]')
    section = None
    for line in stream:
        matcher = section_pattern.match(line)
        if matcher:
            section = matcher.group(1)
            continue
        yield TaggedLine(line, section)

def splitter(stream):
    """
    Group each stream into sections
    """
    return itertools.groupby(sections(stream), operator.attrgetter('tag'))

def parsed_sections(stream):
    for section, lines in splitter(stream):
        yield section, list(csv.reader(lines))

if __name__ == '__main__':
    with open('data.csv') as stream:
        for section, data in parsed_sections(stream):
            print 'section', section
            pprint.pprint(data[:2])

rawDescription: read a multi tier csv file in python

description: Read a multi tier csv file in python


code:
for j in range(3, 50):
        str1 = ''
        str2 = ''
        for k in range(1, j):
                if random.choice([True, False]):
                        str1 += chr(random.randint(97, 122))
                if random.choice([True, False]):
                        str2 += chr(random.randint(97, 122))
        p1 = 0
        p2 = 0
        str3 = ''
        while len(str3) < len(str1)+len(str2):
                if p1 < len(str1) and random.choice([True, False]):
                        str3 += str1[p1]
                        p1 += 1
                if p2 < len(str2) and random.choice([True, False]):
                        str3 += str2[p2]
                        p2 += 1
        a = time.time()
        for i in range(1000000):
                isShuffle2(str1, str2, str3)
        a = (time.time() - a)
        b = time.time()
        for i in range(1000000):
                isinter(str1, str2, str3)
        b = (time.time() - b)

        print "(%s,%s = %s) in %f against %f us" % (str1, str2, str3, a, b)

rawDescription: Algorithm to check the order of substrings in combined string


code:
# app.py (adapted from examples in the Celery Getting Started tutorial
from celery import Celery

app = Celery('tasks', broker='amqp://guest@localhost//')

@app.task
def my_function(a, b):
    return a * b

# main.py
import app

result = app.my_function.delay(4, 5)
print result.get()

rawDescription: How to pass object as commandline argument in python?

description: Pass object as commandline argument in python


code:
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
for elem in tree.iter():
    print elem

rawDescription: how to recursively iterate over XML tags in Python using ElementTree?

description: Recursively iterate over XML tags in Python using ElementTree


code:
import functools

class MyClass(object):
    def create(self, itemType, itemName, *args, **kwargs):
        print "Creating item %s with name %s, args %r and kwargs %r" % (itemType, itemName, args, kwargs)

    def __getattr__(self, attrName):
        return functools.partial(self.create, attrName)

c = MyClass()
bFunc = c.B
bFunc("nameB", 1, 2, foo=3)

rawDescription: Python : Allowing methods not specifically defined to be called ala __getattr__


code:
class Navigator(object):
    def __init__(self, seq):
        self.c = 0
        self.seq = seq

    def next(self):
        self.c +=1
        return self.seq[self.c]

    def prev(self):
        self.c -=1
        return self.seq[self.c]

    def start(self):
        self.c = 0
        return self.seq[self.c]

    def end(self):
        self.c = len(self.seq)-1
        return self.seq[self.c]

n = Navigator(SlicedList(range(100), 5))

print n.start(), n.next(), n.prev(), n.end()

rawDescription: python: how to slice/store iter pointed data in a fixed buffer class?

description: Python: how to slice/store iter pointed data in a fixed buffer class


code:
class A:
    def __init__ (self, b):
        self.b
    def check (self):
        return self.b < 10

l = [A(8), A(40), A(5), A(7), A(33)]
for a in filter(A.check, l):
    print a.b

rawDescription: Python: a more concise way to filter a list


code:
i = 0
s = "string"
while i < len(s):
 print s.__getitem__(i)
 i+=1

rawDescription: find the docs page for function "in"

description: Find the docs page for function "in"


code:
string  = "66,55,66,57"
numbers = [int(x) for x in string.split(",")]
print numbers

rawDescription: extracting numbers in a string

description: Extracting numbers in a string


code:
def part(xs):  # generate all partitions of xs
    xs = tuple(xs)
    n = len(xs)
    def extend(i):
        if i == n:
            yield ()
            return
        this = xs[i]
        for o in extend(i+1):
            yield ((this,),) + o
            for j, p in enumerate(o):
                yield o[:j] + ((this,) + p,) + o[j+1:]
    for o in extend(0):
        yield o

def upart(xs):  # weed out dups, and partitions with a piece bigger than 2
    from collections import Counter
    seen = []
    for p in part(xs):
        if all(len(chunk) <= 2 for chunk in p):
            c = Counter(p)
            if c not in seen:
                seen.append(c)
                yield p

xs = [1,1,1,1,2,2]
for o in upart(xs):
    print o

rawDescription: Partioning non-unique entities into unique sets with Python


code:
def countHits(file):
    f2 = open(file,'rU')
    l2 = f2.readlines()
    f2.close()
    l2 = [l.strip() for l in l2]
    user_input = raw_input(
       "Enter the URL that you wish to chck fr the nmber of Hits"
    )
    print "The number of HITS for the given STRING/URL is : %s"% (
       l2.count(user_input)
    )

rawDescription: Issue with List count method

description: Issue with List count method


code:
covers = soup.select('table.infobox a.image img[src]')
for cover in covers:
    print cover['src']

rawDescription: Getting a particular image from Wikipedia with BeautifulSoup


code:
def loadMaze(ignored):
    maze = '''\
####################################
#S#  ##  ######## # #      #     # #
# #   #             # #        #   #
#   # ##### ## ###### # #######  # #
### # ##    ##      # # #     #### #
#   #    #  #######   #   ###    #E#
####################################
'''
    return [list(m) for m in maze.splitlines()]

directions = (
    (1, 0, '>'),
    (0, 1, 'v'),
    (-1, 0, '<'),
    (0, -1, '^'),
)

def solve(x, y, mazeList=None):
    if mazeList is None:
        mazeList = loadMaze("sample.maze")
    for dx, dy, char in directions:
        nx, ny = x + dx, y + dy
        if mazeList[ny][nx] == "E":
            print '\n'.join([''.join(m) for m in mazeList])
            return "YOU'VE SOLVED THE MAZE!"
        if mazeList[ny][nx] == " ":
            new_maze = [m[:] for m in mazeList]
            new_maze[ny][nx] = char
            result = solve(nx, ny, new_maze)
            if result is not None:
                return result

solve(1, 1)

rawDescription: Runtime Error in python: "Maximum recursion depth exceeded"


code:
class chicken():
    def __init__(self, name, eatable, species):
        self.name=name
        if eatable!="yes" and eatable!="no":
            self.eatable="no"
        else:
            self.eatable=eatable
        self.species=species
    #Print the species when called
    #Doesnt work for some reason
    def print_species(self):
        print"%s" %str(self.species)
    #Print whether or not the bird is eatable
    #This method also doesnt work
    def print_eatable(self):
        print "%s" %str(self.eatable)
    def change_species(self, new_name):
        self.species=new_name
    def change_eatable(self, new_status):
        self.eatable=new_status
        if new_status!="yes" and new_status!="no":
            self.eatable="no"
        else:
            self.eatable=new_status
peep=chicken("Peep", "yes", "Gallus Domesticus")
peep.change_eatable("meatballs")
peep.print_eatable()
peep.print_species()
#This does work
print peep.eatable

rawDescription: How to Create a Printing Method in Python

description: Create a Printing Method in Python


rawDescription: Special Character problem in regexp by python


code:
user_input = "4,8,15,16,23,42"
numbers = [int(d) for d in user_input.split(",")]

print "numbers:", numbers
print "sum of numbers:", sum(numbers)

rawDescription: Changing List Entry Syntax

description: Changing List Entry Syntax


code:
keywords={'int', 'main', 'void', 'return'}

for key, span in ((m.group(1), m.span(1)) for m in re.finditer(r'\b(\w+)\b', src)
                                                          if m.group(1) in keywords):
    print key, span

rawDescription: Find C Keywords inside a String with Python and Regex


code:
import pandas as pd

col = ['open', 'high', 'low', 'close']
index = [1, 2, 3, 4]

df_all = {'df1': pd.DataFrame(columns=col, index=index),
          'df2': pd.DataFrame(columns=col, index=index),
          'df3': pd.DataFrame(columns=col, index=index)}

for key, df in df_all.iteritems():
    df.rename(columns=lambda x: key+x, inplace=True)

print df_all['df1'].columns.tolist()

rawDescription: Renaming columns of dataframe list in Pandas


code:
print which_color(array[0][i]) + "%.3f \t 5" % (array[0][i]) + \
      which_color(array[1][i]) + char + "=%.3f \t10" % (array[1][i]) + char + \
      which_color(array[2][i]) + "=%.3f" % (array[2][i])

rawDescription: Python output complex line with floats colored by value


code:
import sys
import collections

data = collections.deque(line.strip() for line in sys.stdin)
first = data[0]
while data:
    a, b = data.popleft(), data.pop()
    print '{} -> {}'.format(a, b)
    print '{} -> {}'.format(b, data[0] if data else first)

rawDescription: Matching list items to items at the other end of the same list?


code:
d = {i: i * 2 for i in range(10000)}
from timeit import timeit
print timeit("{d[k]: k for k in d}", "from __main__ import d", number = 10000)
# 7.22010397911
print timeit("dict((v, k) for k, v in d.iteritems())", "from __main__ import d", number = 10000)
# 10.6085851192

rawDescription: Reversing dictionary: time and space

description: Reversing dictionary: time and space


code:
from sklearn.feature_extraction.image import extract_patches

patches = extract_patches(x, patch_shape=(2, 2), extraction_step=(2, 2))

xx = patches.mean(-1).mean(-1)

print xx

rawDescription: Interpolation of values when zooming down


code:
#the above same lines

soup1 = BeautifulSoup(html2)
value = soup1.body.extract()

div.append(value)
# replaces a tag with whatever’s inside that tag.
div.body.unwrap()
print div

rawDescription: How do I use BeautifulSoup to move tag contents from one soup to a template soup

description: Use BeautifulSoup to move tag contents from one soup to a template soup


code:
filename = 'TestProc.xml'
import xml.etree.ElementTree as ET

xmldoc = ET.parse(filename)

variables = xmldoc.findall(".//Procedure/Data2/Variable")

varName=[e.get('name') for e in variables]
varType=[e.get('type') for e in variables]

print varName, varType

rawDescription: Issue Getting Nested Elements in xml File using minidom


code:
matching_link = grab_toc.find('a', text=re.compile('one', re.IGNORECASE)).parent
print matching_link.string
print matching_link['href']

rawDescription: Beautiful Soup and searching in results

description: Beautiful Soup and searching in results


code:
from AppKit import *
pb = NSPasteboard.generalPasteboard()
pbstring = pb.stringForType_(NSStringPboardType)
print pbstring.split(" ")

rawDescription: Is there a way to convert the clipboard content into list in the run in Python?


code:
import os
import os.path

for root, dirs, files in os.walk('/path/to/directory'):
    for f in files:
        fname = os.path.join(root, f)
        if os.path.isfile(fname):
            print fname  # or do something else with it...

rawDescription: Python equivalent to "find -type f"

description: Python equivalent to "find -type f"


code:
def listInList(a, list, memory):
    if not list:
        return False
    elif str(list) in memory:
        return memory[str(list)]
    else:
        if list in a:
            memory[str(list)] = True
            return True
        else:
            for i in range(1,len(list)):
                if listInList(a, list[i:], memory) and listInList(a, list[:i], memory):
                    memory[str(list)] = True
                    return True
            memory[str(list)] = False
            return False

def check(a, b):
    res = []
    for elem in b:
        if listInList(a, elem, {}):
            res += [elem]
    return res

w= [
     ['sta'],
     ['co', 'si'],
     ['non'],
     ['si'],
     ['puo'],
     ['ap', 'ri', 're'],
     ['ve', 'dia', 'mo'],
     ['co', 'sa'],
     ['ci'],
     ['pot', 'reb', 'be']
   ]
m=[
     ['sta', 'co'],
     ['puo', 'ap', 'ri', 're'],
     ['ve'],
     ['dia', 'mo'],
     ['co'],
     ['sa'],
     ['ci', 'pot', 'reb', 'be']
  ]
print check(w,m)

rawDescription: Check if list is composed by other lists


code:
formula = "(assignee=cdecker OR due=tomorrow) AND status=open"
tree = expr.parseString(formula, parseAll=True)
print tree.asXML()

rawDescription: Filtering objects matching a given logical formula


code:
def generate(charlist,state,position):
    for i in charlist:
        state[position] = i
        if position == (len(state)-1):
            yield "".join(state)
        else:
            for j in generate(charlist,state,position+1):
                yield j

print list(generate('abc', [None] * 3, 0))

rawDescription: Help understanding how this recursive python function works?


code: print some_instance.__class__.__name__

rawDescription: Convert a python 'type' object to a string


code:
n=1
for match in re.finditer('"id":([^,]+)', subject):
    if n==1:
        print "ignoring the first match"
    else:
        print match.group(1)
    n+=1

rawDescription: Python Regex Match Integer After String

description: Python Regex Match Integer After String


code:
for i in [1,3,5]:
    if i%2==0:
        print i
        break
else:
    print "There are no EVEN #s"

rawDescription: Python For If Else

description: Python For If Else


code:
import re

string = """
<t:latex-object url='%28-3%29%5E%7B2%7D%3D3%5E%7B2%7D'><![CDATA[(-3)^{2}=3^{2}]]></t:latex-object>
""";

for matchObj in re.finditer( r'<t:latex-object\b(?=\s)(?=(?:(?![^>])\'[^\']*\'|"[^"]*"|.)*\surl=\'([^"]*)\').*?<\/t:latex-object>', string, re.M|re.I|re.S):
    print "-------"
    print "matchObj.group(0) : ", matchObj.group(0)
    print "matchObj.group(1) : ", matchObj.group(1)

rawDescription: Replacing xml tag with generated image (python regex)


code:
lookup = dict((s, i) for i, s in enumerate(code_order))
print sorted(l, key=lambda o: lookup[o['code']])

rawDescription: Sort a list of dictionaries by a key, based on the order of keys in a separate list in Python


code:
import sys
import urllib2

# Check command line arguments.
if len(sys.argv) < 3:
    sys.stderr.write("Usage: %s url start [length]\n" % sys.argv[0])
    sys.exit(1)

# Create a request for the given URL.
request = urllib2.Request(sys.argv[1])

# Add the header to specify the range to download.
if len(sys.argv) > 3:
    start, length = map(int, sys.argv[2:])
    request.add_header("range", "bytes=%d-%d" % (start, start + length - 1))
else:
    request.add_header("range", "bytes=%s-" % sys.argv[2])

# Try to get the response. This will raise a urllib2.URLError if there is a
# problem (e.g., invalid URL).
response = urllib2.urlopen(request)

# If a content-range header is present, partial retrieval worked.
if "content-range" in response.headers:
    print "Partial retrieval successful."

    # The header contains the string 'bytes', followed by a space, then the
    # range in the format 'start-end', followed by a slash and then the total
    # size of the page (or an asterix if the total size is unknown). Lets get
    # the range and total size from this.
    range, total = response.headers['content-range'].split(' ')[-1].split('/')

    # Print a message giving the range information.
    if total == '*':
        print "Bytes %s of an unknown total were retrieved." % range
    else:
        print "Bytes %s of a total of %s were retrieved." % (range, total)

# No header, so partial retrieval was unsuccessful.
else:
    print "Unable to use partial retrieval."

# And for good measure, lets check how much data we downloaded.
data = response.read()
print "Retrieved data size: %d bytes" % len(data)

rawDescription: Python - seek in http response stream

description: Python - seek in http response stream


code:
class ListElement:
    def __init__(self, name, value):
        self.name = name
        self.value = value

    def __repr__(self):
        return self.name + "=" + self.value.__str__()

class ChainElement:
    def __init__(self, prev, listElement):
        self.prev = prev
        self.listElement = listElement
        if prev:
            self.value = listElement.value + prev.value
        else:
            self.value = listElement.value

    def __repr__(self):
        if self.prev:
            return self.prev.__repr__() + "," + self.listElement.__repr__()
        else:
            return self.listElement.__repr__()

    def removeAllFrom(self, list):
        list.remove(self.listElement)
        if self.prev:
            self.prev.removeAllFrom(list)

list = []
list.append(ListElement("x", 20))
list.append(ListElement("y", 25))
list.append(ListElement("z", 10))
list.append(ListElement("m", 15))
list.append(ListElement("n",  8))
list.append(ListElement("p", 22))

def find_chain():
    chains = [ChainElement(None, list[0])]

    for le in list[1:]:
        new_chains = []
        for ce in chains:
            new_chain = ChainElement(ce, le)
            if new_chain.value == 50:
                return new_chain
            elif new_chain.value < 50:
                new_chains.append(new_chain)
        chains.extend(new_chains);
    return None

while list:
    tuple = find_chain()
    if tuple:
        print "Found:", tuple
        tuple.removeAllFrom(list)
    else:
        print
        print "Residue:", list
        break

rawDescription: Is it possible to iterate over a list and skip certain elements?


code:
# unstack to wide, fillna as 0s
df_wide = df_indexed.unstack().fillna(0)
# stack back to long
df_long = df_wide.stack()
# change 0s to max using groupby.
df_long['ind_var'] = df_long['ind_var'].groupby(level = 0).transform(lambda x: x.max())
df_long['loc_var'] = df_long['loc_var'].groupby(level = 1).transform(lambda x: x.max())
print df_long

rawDescription: Reindexing and filling NaN values in Pandas


code:
#! /usr/bin/python

import numpy as np

def sigmoid(x):
    return 1.0 / (1.0 + np.exp(-x))

vec_sigmoid = np.vectorize(sigmoid)

# Binesh - just cleaning it up, so you can easily change the number of hiddens.
# Also, initializing with a heuristic from Yoshua Bengio.
# In many places you were using matrix multiplication and elementwise multiplication
# interchangably... You can't do that.. (So I explicitly changed everything to be
# dot products and multiplies so it's clear.)
input_sz = 2;
hidden_sz = 3;
output_sz = 1;
theta1 = np.matrix(0.5 * np.sqrt(6.0 / (input_sz+hidden_sz)) * (np.random.rand(1+input_sz,hidden_sz)-0.5))
theta2 = np.matrix(0.5 * np.sqrt(6.0 / (hidden_sz+output_sz)) * (np.random.rand(1+hidden_sz,output_sz)-0.5))

def fit(x, y, theta1, theta2, learn_rate=.1):
    #forward pass
    layer1 = np.matrix(x, dtype='f')
    layer1 = np.c_[np.ones(1), layer1]
    # Binesh - for layer2 we need to add a bias term.
    layer2 = np.c_[np.ones(1), vec_sigmoid(layer1.dot(theta1))]
    layer3 = sigmoid(layer2.dot(theta2))

    #backprop
    delta3 = y - layer3
    # Binesh - In reality, this is the _negative_ derivative of the cross entropy function
    # wrt the _input_ to the final sigmoid function.

    delta2 = np.multiply(delta3.dot(theta2.T), np.multiply(layer2, (1-layer2)))
    # Binesh - We actually don't use the delta for the bias term. (What would be the point?
    # it has no inputs. Hence the line below.
    delta2 = delta2[:,1:]

    # But, delta's are just derivatives wrt the inputs to the sigmoid.
    # We don't add those to theta directly. We have to multiply these by
    # the preceding layer to get the theta2d's and theta1d's
    theta2d = np.dot(layer2.T, delta3)
    theta1d = np.dot(layer1.T, delta2)

    #update weights
    # Binesh - here you had delta3 and delta2... Those are not the
    # the derivatives wrt the theta's, they are the derivatives wrt
    # the inputs to the sigmoids.. (As I mention above)
    theta2 += learn_rate * theta2d #??
    theta1 += learn_rate * theta1d #??

def train(X, Y):
    for _ in range(10000):
        for i in range(4):
            x = X[i]
            y = Y[i]
            fit(x, y, theta1, theta2)

# Binesh - Here's a little test function to see that it actually works
def test(X):
    for i in range(4):
        layer1 = np.matrix(X[i],dtype='f')
        layer1 = np.c_[np.ones(1), layer1]
        layer2 = np.c_[np.ones(1), vec_sigmoid(layer1.dot(theta1))]
        layer3 = sigmoid(layer2.dot(theta2))
        print "%d xor %d = %.7f" % (layer1[0,1], layer1[0,2], layer3[0,0])

X = [(0,0), (1,0), (0,1), (1,1)]
Y = [0, 1, 1, 0]
train(X, Y)

# Binesh - Alright, let's see!
test(X)

rawDescription: XOR neural network backprop

description: XOR neural network backprop


code:
#!/usr/bin/python -tt

# A dictionary Of Each New SSID
WirelessNetwork = {}
WirelessNetwork['name'] = 'baz'
WirelessNetwork['type'] = 'bar'
WirelessNetwork['pass'] = 'foo'

# A list of all SSIDs
networkAddList = (WirelessNetwork,)

def addWireless(passedDict={}):
  print 'Adding SSID: %s' % passedDict['name']
  print 'Of type: %s' % passedDict['type']
  print 'With Password: %s' % passedDict['pass']

for networkDict in networkAddList:
  addWireless(networkDict)

rawDescription: Iterate a tuple of dictionaries and pass the nested dictionaries to a function


code:
def hms_list(amounts):
    ret = []
    for amount in amounts:
        ret.append(hms(amount))
    return ret

print hms_list([1000, 10000])  # => [(0, 16, 40), (2, 46, 40)]

rawDescription: For loop troubleshooting

description: For loop troubleshooting


code:
def otherfunc(b):
    return b * 7

def myfunc(a):
    for i in range(0, 20):
        print i, otherfunc(i + a + 3)

myfunc(2)

rawDescription: Embedding a Low Performance Scripting Language in Python


code:
number1 = 1
number0 = 0
for i in xrange(M):
    # 0 -> 01
    newnumber1 = number0
    # 1 -> 0 and 0 -> 01
    number0 += number1
    # we replace the number1 with the new number
    number1 = newnumber1
print "%d %d"%(number0,number1)

rawDescription: python - High time consumption and low efficiency in programming challenge

description: Python - High time consumption and low efficiency in programming challenge


code: print dir(tag.lyrics)

rawDescription: Retrieve lyrics from an mp3 file in Python using eyeD3


code:
class BoundedInt(int):
    def __new__(cls, *args, **kwargs):
        lower, upper = bounds = kwargs.pop('bounds')

        val = int.__new__(cls, *args, **kwargs)  # supports all int() args
        val = lower if val < lower else upper if val > upper else val

        val = super(BoundedInt, cls).__new__(cls, val)
        val._bounds = bounds
        return val

    def __add__(self, other):
        return BoundedInt(int(self)+other, bounds=self._bounds)
    __iadd__ = __add__

    def __sub__(self, other):
        return BoundedInt(int(self)-other, bounds=self._bounds)
    __isub__ = __sub__

    def __mul__(self, other):
        return BoundedInt(int(self)*other, bounds=self._bounds)
    __imul__ = __mul__

    # etc, etc...

if __name__ == '__main__':
    v = BoundedInt(100, bounds=(0, 100))
    print type(v), v
    v += 10
    print type(v), v
    w = v + 10
    print type(w), w
    x = v - 110
    print type(x), x

rawDescription: keeping a variable inside a range in python

description: Keeping a variable inside a range in python


code:
import numpy as np
a = np.arange(12)**2
b = np.lib.stride_tricks.as_strided(a, shape=(4,3), strides=(a.itemsize*3, a.itemsize))
print np.std(b, axis=1)

rawDescription: Apply function to 3 elements at a time in numpy


code:
class test:
    def get(self, name):
        if name == "valA":
            return {"1":"valA1", "2":"valA2", "3":"valA3"}
        elif name ==  "valB":
            return {"1":"valB1", "2":"valB2", "3":"valB3"}
    def apply(self, function, **kwargs):
        arg_dict = {fun_arg: self.get(sim_args) for fun_arg, sim_args in kwargs.items()}
        result = {}
        for SID in arg_dict[kwargs.keys()[0]]:
            fun_kwargs = {fun_arg: sim_dict[SID] for fun_arg, sim_dict in arg_dict.items()}
            result[SID] = function(**fun_kwargs)
        return result

def joinstrings(string_a, string_b):
    return string_a+string_b

my_test = test()
result = my_test.apply(joinstrings, string_a="valA", string_b="valB")
print result

rawDescription: how to make my own mapping type in python

description: Make my own mapping type in python


code:
import base64
from M2Crypto import RSA

TEMPLATE = """
-----BEGIN RSA PRIVATE KEY-----
%s
-----END RSA PRIVATE KEY-----
"""
raw = open('key.der', 'rb').read()
data = TEMPLATE % base64.encodestring(raw).rstrip()
key = RSA.load_key_string(data)
print key

rawDescription: Loading a DER-encoded RSA key using M2Crypto


code:
import pygame, os
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((640, 480))
keymap = {}

while True:
    event = pygame.event.wait()
    if event.type == KEYDOWN:
        keymap[event.scancode] = event.unicode
        print 'keydown %s pressed' % event.unicode
        if (event.key == K_ESCAPE):
            os._exit(0)

    if event.type == KEYUP:
        print 'keyup %s pressed' % keymap[event.scancode]

rawDescription: Pygame keyboard layouts mixed up

description: Pygame keyboard layouts mixed up


code:
root = 1  # or whatever other representation you may use for root
list = build_tree(root)
print list

rawDescription: Building a tree "backwards"

description: Building a tree "backwards"


code: print codons.count('aaa')

rawDescription: count the number of a certain triplet in a file (DNA codon analysis)

description: Count the number of a certain triplet in a file (DNA codon analysis)


code:
# main.py
import functions

# All of the old stuff that was in main.foo is now in this class
class fooBase(asd.fgh):
    def qwerty(self):
        print 'qwerty'

# Now create a class that has methods and attributes of both classes
class foo(FooBase, functions.foo): # Methods from FooBase take precedence
    pass

rawDescription: Import code directly into script with Python?


code:
import example
from time import sleep

x = range(2)
example.set(x)
example.display()
example.display_in(3)
x[:] = range(7, 10)
print "sleeping"
sleep(6)

rawDescription: boost::python passing reference of python::list

description: Boost::python passing reference of python::list


code:
import requests
import lxml.html

url = 'http://pro-161-70.ib.unicamp.br/~itaraju/cgi-bin/itaraju/bioinf/pimw.cgi'
payload = {
#    'arquivo': '', # remove it
    'opShowTitle': 'ON',
    'opShowSeq': 'ON',
    'opShowStat': 'ON',
    'opShowpimw': 'ON',
    'opGelVirtual': 'ON',
    'opMap': 'gel0.def',
    'opPK': 'Default',
    'tbCt': 3.55,
    'tbNt': 7,
    'tbArg': 12.01,
    'tbAsp': 4.06,
    'tbCys': 9,
    'tbGlu': 4.45,
    'tbHis': 5.985,
    'tbLys': 10.01,
    'tbTyr': 10.01,
    'tbSeq': '',
}

files = {'arquivo': open('sequence.fasta').read()}

#url = 'http://httpbin.org/post' # special portal for tests

# send POST
r = requests.post(url, data=payload, files=files)

#print r.text

# convert HTML string into HTML tree
html = lxml.html.fromstring(r.text)

# get all images
imgs = html.cssselect('img')

# get second image
if len(imgs) > 1:
    url = 'http://pro-161-70.ib.unicamp.br/~itaraju/cgi-bin/itaraju/bioinf/' + imgs[1].attrib['src'].strip()

    print "Downloading ...",  url

    with open('output.gif', 'wb') as handle:
        r = requests.get(url, stream=True)

        if not r.ok:
            # Something went wrong
            pass

        for block in r.iter_content(1024):
            if not block:
                break

            handle.write(block)
            print '.',

        print

# get data
for tr in html.cssselect('tr'):
    for td in tr.cssselect('tr'):
        print td.text_content().strip().replace('\n', ' | '),
    print

rawDescription: Trying to post to a form that uses frames and retrieve the data using python


code:
"""fromstdin - Training and Testing Framework
Usage: fromstdin.py [options] <input>

Options:
    --text=<textmodel>         Text model [default: text.txt]
    --features=<features>      Features model [default: features.txt]
    --test=<testset>           Testing set [default: testset.txt]
    --vectorizer=<vectorizer>  The vectorizec [default: vector.txt]

Read data from <input> file. Use "-" for reading from stdin.
"""
import sys

def main(fname, text, features, test, vectorizer):
    if fname == "-":
        f = sys.stdin
    else:
        f = open(fname)
    process(f, text, features, test, vectorizer)
    print "main func done"

def process(f, text, features, test, vectorizer):
    print "processing"
    print "input parameters", text, features, test, vectorizer
    print "reading input stream"
    for line in f:
        print line.strip("\n")
    print "processing done"

if __name__ == "__main__":
    from docopt import docopt
    args = docopt(__doc__)
    print args
    infile = args["<input>"]
    textfile = args["--text"]
    featuresfile = args["--features"]
    testfile = args["--test"]
    vectorizer = args["--vectorizer"]
    main(infile, textfile, featuresfile, testfile, vectorizer)

rawDescription: Python Read from Stdin with Arguments

description: Python Read from Stdin with Arguments


code:
for i in permute(s):
    print i

rawDescription: Python permutations of both sequence and subsequences


code:
def printitem(id, depth=0):
    print '  '*depth, id
    for child in itemdict[id]:
        printitem(child, depth+1)

rawDescription: Recursive? looping to n levels in Python


code:
for i in range(S.shape[0]):
    M = np.matrix( [S['x'][i], S['center'][i]] )
    print pdist(M, 'euclidean')

rawDescription: Measure distance between data set of 5D

description: Measure distance between data set of 5D


code:
x = []
for i in sorted(np_mwe_nocomma, key=len, reverse=True):
    for j in x:
            if i in j:
                    continue
    print i
    x.append(i)

rawDescription: Get entities from NLTK.tree result

description: Get entities from NLTK.tree result


code:
c = change(455.50)
print c

rawDescription: Work out correct money denominations for change


code:
from itertools import product
for i in product([0,1], repeat=5):
    print i

rawDescription: How do I compute all possibilities for an array of numbers/bits (in python, or any language for that matter)

description: Compute all possibilities for an array of numbers/bits (in python, or any language for that matter)


code: print '\n'.join(' '*p + x[p:] for p, x in eachWithPrefix(v))

rawDescription: How to group strings with similar beginnings in a sorted list?

description: Group strings with similar beginnings in a sorted list


code:
for strKey, dictVal in dictFromJSON.items():
    if strKey == "contacted":
       print dictVal.keys()

rawDescription: Parsing nested key from dict (generated from json)


code:
import operator
import functools

multiadd = functools.partial(map, operator.add)
print multiadd([1,2,3], [2,3,4]) #=> [3, 5, 7]

rawDescription: correct style for element-wise operations on lists without numpy (python)

description: Correct style for element-wise operations on lists without numpy (python)


code:
import os
TDATES = os.getenv('TDATES')
for i in TDATES.split(' '):
    print i

rawDescription: export list from bash script to python

description: Export list from bash script to python


code:
l = ['A', 'AA', 'B', 'BB', 'C', 'CC']
l.sort(key=lambda element: (len(element), element))
print l

rawDescription: Sorting a List of Strings, Ignoring ASCII Ordinal Values


code:
from bs4 import BeautifulSoup
import requests

url = 'http://accessibility.psu.edu/headingshtml/'

respons = requests.get(url).content

soup = BeautifulSoup(respons,'lxml')

h1tags = soup.find_all('h1')

for singleTag in h1tags:
    print singleTag.text

rawDescription: Capturing data between specific tag in python


code:
for k in my_key_order[:2]:
    print dct[k]

rawDescription: How to iterate through dictionary in our preferred keys order in python 2.7?

description: Iterate through dictionary in our preferred keys order in python 2.7


code:
li=tgt.split()
nums=' '.join(li[0:2]+li[-2:])
words=' '.join(li[2:-2])
print words,nums

rawDescription: How do you split a string into quoted sentence and numbers using python

description: Split a string into quoted sentence and numbers using python


code:
dic = {}

dic['MPO']= 'GSTM1 FINC ELN'

dic['HMOX1'] = 'GSTM1 ELN CAV1'

dic['POLD1'] = 'GSTM1 FINC ELN'

dic['MAPK1'] = 'FINC ELN CAV1'

dic['VCAM1'] = 'FINC ELN CAV1'

dic['CTGF']= 'FINC ELN CAV1'

dic['DECR1'] = 'GSTM1 ELN CAV1'

keys = dic.keys()
values = set(dic.values())

for value in values:
    allkeys = ''
    for key in keys:
        if dic[key] == value:
            allkeys = allkeys +" "+str(key)+","
    print value ,'--->', allkeys

rawDescription: Python : Dictionary with one key and multiple values : How to get list of keys having same SET of values?


code:
# phrase_trie.py

from collections import defaultdict

CATEGORIES = {"!DETERMINER": set(["a","an","the"]),
              "!VERB": set(["walked","talked","had"])}

def get_category(word):
    for name,words in CATEGORIES.items():
        if word in words:
            return name
    return None

class PhraseTrie(object):
    def __init__(self):
        self.children = defaultdict(PhraseTrie)
        self.categories = defaultdict(PhraseTrie)

    def insert(self, phrase):
        if not phrase: # nothing to insert
            return

        this=phrase[0]
        rest=phrase[1:]

        if this in CATEGORIES: # it's a category name
            self.categories[this].insert(rest)
        else:
            self.children[this].insert(rest)

    def contains(self, phrase):
        if not phrase:
            return True # the empty phrase is in everything

        this=phrase[0]
        rest=phrase[1:]

        test = False

        # the `if not test` are because if the phrase satisfies one of the
        # previous tests we don't need to bother searching more

        # allow search for ["!DETERMINER", "cat"]
        if this in self.categories:
            test = self.categories[this].contains(rest)

        # the word is literally contained
        if not test and this in self.children:
            test = self.children[this].contains(rest)

        if not test:
            # check for the word being in a category class like "a" in
            # "!DETERMINER"
            cat = get_category(this)
            if cat in self.categories:
                test = self.categories[cat].contains(rest)
        return test

    def __str__(self):
        return '(%s,%s)' % (dict(self.children), dict(self.categories))
    def __repr__(self):
        return str(self)

if __name__ == '__main__':
    words = PhraseTrie()
    words.insert(["he", "had", "!DETERMINER", "nerve"])
    words.insert(["he", "had", "the", "evren"])
    words.insert(["she", "!VERB", "the", "nerve"])
    words.insert(["no","categories","here"])

    for phrase in ("he had the nerve",
                   "he had the evren",
                   "she had the nerve",
                   "no categories here",
                   "he didn't have the nerve",
                   "she had the nerve more"):
        print '%25s =>' % phrase, words.contains(phrase.split())

rawDescription: Membership testing in a large list that has some wildcards


code:
flip = [[0,1],[1,0]]
tensor4d = np.tensordot(flip, flip, axes=0)
print tensor4d.swapaxes(2, 1).reshape((4,4))

rawDescription: NumPy Tensor / Kronecker product of matrices coming out shuffled


code:
count = 0
largest = None
smallest = None

while True:
    inp = raw_input('Please enter a number: ')
    # Kills the program
    if inp == 'done' : break
    if len(inp) < 1 : break

    # Gets the work done
    try:
        num = float(inp)
    except:
        print 'Invalid input, please enter a number'
        continue
    # The numbers for count, largest and smallest
    count = count + 1
    # Gets largest number
    if largest is None or num > largest:  # Change 1
        largest = num
    print 'Largest',largest
    # Gets smallest number
    if smallest is None or num < smallest:  # Change 2
       smallest = num
    print 'Smallest', smallest

print 'Count:', count, 'Largest:', largest, 'Smallest:', smallest

rawDescription: Iterative Comparison of raw_input

description: Iterative Comparison of raw_input


code:
import numpy as np
strc = np.zeros(3, dtype=[('x', int), ('y', float), ('z', int), ('t', "i8")])

def fields_view(arr, fields):
    dtype2 = np.dtype({name:arr.dtype.fields[name] for name in fields})
    return np.ndarray(arr.shape, dtype2, arr, 0, arr.strides)

v1 = fields_view(strc, ["x", "z"])
v1[0] = 10, 100

v2 = fields_view(strc, ["y", "z"])
v2[1:] = [(3.14, 7)]

v3 = fields_view(strc, ["x", "t"])

v3[1:] = [(1000, 2**16)]

print strc

rawDescription: numpy: how to fill multiple fields in a structured array at once

description: Numpy: how to fill multiple fields in a structured array at once


code:
#!/usr/bin/env python

import numpy as np

file = np.loadtxt('filename', delimiter=',') # Put the delimiter type from your csv file : coma, blank, ..

print file # print the numpy array

rawDescription: Importing csv file into python and creating a table in python


code:
import multiprocessing

def f(name):
    print 'hello', name

if __name__ == '__main__':
    pool = multiprocessing.Pool() #use all available cores, otherwise specify the number you want as an argument
    for i in xrange(0, 512):
        pool.apply_async(f, args=(i,))
    pool.close()
    pool.join()

rawDescription: Python - Using multiprocessing.Process with a maximum number of simultaneous processes


rawDescription: Perl to Python , Match Pattern on a html output


code:
from socket import socket, AF_INET, SOCK_DGRAM

s = socket(AF_INET, SOCK_DGRAM) #create UDP socket
s.bind(('', PORT))

while 1:
    data, addr = s.recvfrom(1024) #wait for a packet
    if data.startswith(MAGIC):
        print "got service announcement from", data[len(MAGIC):]

rawDescription: Python service discovery: Advertise a service across a local network


code:
import re
from itertools import imap
from collections import OrderedDict

ch = '>AB0012xxxxaaaaaaaaaaaa'\
     '>CD00192aaabbbllyybbbaaayyyuubbbaaaggggaaabbb'\
     '>ZP0199000012mmmm3m4mmmxxxxaaabbbaaaaaaaaaaaaa'\
     '>QD1547zzzzzzzzjjjiii'\
     '>SE457895ffffaaabbbbbbbgjhgjgjhgjhgbbbbbaaa'

print ch,'\n'

regx = re.compile('((?<=>)(.{7})[^>]*(?:aaabbb|bbbaaa)[^>]*?)(?=>|\Z)')
rag = re.compile('aaabbb|bbbaaa')

gen = ((mat.groups(),mat.start()) for mat in regx.finditer(ch))

dic = OrderedDict(((headstart,head),
                   [(headstart+six.start(),six.start(),six.group())
                    for six in rag.finditer(chunk)])
                  for (chunk,head),headstart in gen)

print '\n'.join('{:>10} {}'.format(headstart,head)+'\n'+\
                '\n'.join(imap('{0[0]:>10} {0[1]:>6} {0[2]}'.format,li))
                for (headstart,head),li in dic.iteritems())

rawDescription: Regular expression help

description: Regular expression help


code:
from itertools import combinations

for f1, f2 in combinations(sys.argv[1:], 2):
    with open(f1) as inp1, open(f2) as inp2:
        print set(line.rstrip() for line in inp1).intersection(map(str.rstrip,
                                                                           inp2))

rawDescription: Intersection between multiple files

description: Intersection between multiple files


code:
import re
with open("file") as aFile:
    prevline = ""
    for num, line in enumerate(aFile,1):
        m = re.match(r'\d+\.\s*.*', line)                                # stores the match object of the line which starts with a number and a dot
        if m:
            prevline += re.match(r'\d+\.\s*(.*)', line).group()         # If there is any match found then this would append the whole line to the variable prevline. You could also write this line as prevline += m.group()

        searchObj = re.search(r'(\bTotal\b\s+\w+:\s+(\d+\.\d+))', line)  # Search for the line which contains the string Total plus a word plus a colon and a float number
        if searchObj:                                                   # if there is any
            score = float(searchObj.group(2))                           # then the float number is assigned to the variable called score
            if score > 40.0:                                            # Do all the below operations only if the float number we fetched was greater than 40.0
                print "this is the line number: ", num
                print "this is the line", searchObj.group(1)
                print num
                print num-4
                print "found the", prevline
                prevline = ""

rawDescription: Read a line store it in a variable and then read another line and come back to the first line. Python 2


code:
for link in br.links(url_regex="product"):
    follow = br.follow_link(link)
    url = br.response().read()
    find = lh.document_fromstring(url)
    find = find.findall('.//td')
    print find
    results = []  # Create a place to store the text names
    for item in find:
        results.append(item.text)  # Store the text name of the item in the results list.
    output.writerows(results)  # Now, write the results out.  # EDITED to use correct variable here.

rawDescription: Web scraper returning list of elements

description: Web scraper returning list of elements


code:
url = "http://xxx.abcdef.com/fdfdf/"

print url.split('/')[2]

rawDescription: python 2 and 3 extract domain from url

description: Python 2 and 3 extract domain from url


code:
import hello_ext
print hello.greet()

rawDescription: Linking languages

description: Linking languages


code: print repr(''.join(chr(x) for x in rxData.msgBuf))

rawDescription: BYTE to python C-Type structure Conversion Issue


code:
import random
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure

try:
    import Tkinter as Tk
except ImportError:
    import tkinter as Tk
import tkMessageBox

class PlotClassifier(Tk.Tk):
    def __init__(self, plot_generator, arguments, classes, *args, **kwargs):
        Tk.Tk.__init__(self, *args, **kwargs)
        self.title("Plot classifier")
        self._plot_generator = plot_generator
        self._arguments = arguments
        self._classes = [str(x) for x in classes]
        self.class_ = None
        self._setup_gui()

    def _setup_gui(self):
        #self.columnconfigure(0, minsize=100, weight=2)
        #self.columnconfigure(1, minsize=500, weight=8)
        f = Figure()
        self._ax = f.add_subplot(111)
        buttons_frame = Tk.Frame(self)
        buttons_frame.pack(side=Tk.TOP, fill=Tk.X, expand=True)
        buttons_class = []
        for i, cls in enumerate(self._classes):
            buttons_class.append(Tk.Button(master=buttons_frame, text=cls,
                                           command=lambda x=i: self.button_classification_callback(x)))
            buttons_class[-1].pack(side=Tk.LEFT)
        button_quit = Tk.Button(master=buttons_frame, text='Quit', command=self.destroy)
        button_quit.pack(side=Tk.RIGHT) #.grid(row=0,column=0)

        self._canvas = FigureCanvasTkAgg(f, master=self)
        self._canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) #.grid(row=0, column=1, rowspan=3) #
        self._canvas.show()

        toolbar = NavigationToolbar2TkAgg( self._canvas, self )
        toolbar.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) #.grid(row=3, column=1) #
        toolbar.update()

    def button_classification_callback(self, class_idx):
        self.class_ = self._classes[class_idx]
        self.destroy()

    def classify_plot(self):
        self._ax.cla()
        self._plot_generator(self._ax, *self._arguments)
        self._canvas.draw()
        self.mainloop()
        return self.class_

def create_plot(ax, factor):
    ax.plot([(i*factor) % 11 for i in range(100)])

if __name__ == "__main__":
    classes = ["Class %i"%i for i in range(1, 6)]
    arguments_for_plot = [[random.randint(1,10)] for x in range(10)]
    for args in arguments_for_plot:
        classifier = PlotClassifier(create_plot, args, classes)
        class_ = classifier.classify_plot()
        print args, class_
        if class_ is None:
            break

rawDescription: Matplotlib event and replotting

description: Matplotlib event and replotting


code:
for item in self.sizerItems:
    print item.GetLabel()

rawDescription: Generate List of All sizerItems Nested in wxPython Sizers


code:
try:
    inp = raw_input
except NameError:
    inp = input

START, ODD, EVEN = range(3)
state_next = [ODD, EVEN, ODD]
state_str  = ['no zeros yet', 'an odd number of zeros', 'an even number of zeros']

def reduce_fn(state, ch):
    return state_next[state] if ch=='0' else state

state = reduce(reduce_fn, inp('Enter at own risk: '), START)
print "I have seen " + state_str[state]

rawDescription: determinating if the input is even or odd numbers

description: Determinating if the input is even or odd numbers


code:
s = 'ip nat pool-group pool1 pool2 pool3 vrid 0'
t = 'ip nat pool-group pool1 pool2 pool3 pool6 pool7 vrid 0'
m = re.search('^ip nat (.*) vrid 0', s)
print m.groups()
n = re.search('^ip nat (.*) vrid 0', t)
print n.groups()

rawDescription: Regex retrieving a variable string from a string


code:
names = [get_name(n) for n in [name1, name2]]
print names

rawDescription: Extracting part of a string with irregular delimiters in python


code:
tmp_iter = iter(tmp)
[_, prev] = tmp_iter.next()

for [_, x] in tmp_iter:
   print 'seq: ', x, 'prev seq:', prev, 'variance: ', x-prev
   prev = x

rawDescription: Optimal way to access a value from the last iteration in a loop


code:
# Get *all* 'p' tags with the specified class attribute.
elements = driver.find_elements_by_class_name('market_value')
for element in elements:
    print element.text

# or

# Get a *single* 'p' tag with the specified class attribute.
element = driver.find_element_by_class_name('market_value')
print element.text

rawDescription: selenium webdriver in python

description: Selenium webdriver in python


code:
def map_url(url, sm=[None, sitemap]):
    if not url: return sm[0]
    if url[0] not in sm[1]: return False
    return map_url(url[1:], sm[1][url[0]])

print map_url(['section_two', 'c_sect_2_2', 'c_sect_2_2_3']) # => 112
print map_url(['section_two', 'c_sect_2_2'])                 # => 10
print map_url(['section_two', 'notexisting'])                # => False
print map_url([])                                            # => None

rawDescription: In Python with a nested dictionary should I check the type? What other approach can I use?


code: print 'this is \n a test'

rawDescription: What does the "r" in pythons re.compile(r' pattern flags') mean?


code:
import timeit

prepare = """
with open('bible.txt') as fh:
    text = fh.read()
"""

presplit_prepare = """
with open('bible.txt') as fh:
    text = fh.read()
lines = text.split('\\n')
"""

longsearch = """
'hello' in text
"""

splitsearch = """
for line in text.split('\\n'):
    if 'hello' in line:
        break
"""

presplitsearch = """
for line in lines:
    if 'hello' in line:
        break
"""

benchmark = timeit.Timer(longsearch, prepare)
print "IN on big string takes:", benchmark.timeit(1000), "seconds"

benchmark = timeit.Timer(splitsearch, prepare)
print "IN on splitted string takes:", benchmark.timeit(1000), "seconds"

benchmark = timeit.Timer(presplitsearch, presplit_prepare)
print "IN on pre-splitted string takes:", benchmark.timeit(1000), "seconds"

rawDescription: Python string search efficiency

description: Python string search efficiency


code:
#!/usr/bin/env python

solution = {}

orientations = [(1,1),(1,-1),(-1,1),(-1,-1)]    # 4 possible orientations

def solve( avl, step ) :
    if not len(avl) :
        print 'done!'
        return True

    for i,j in avl :
        for oi,oj in orientations :
            if (i+oi,j) in avl and (i,j+oj) in avl :
                occupied = [(i,j),(i+oi,j),(i,j+oj)]
                # remove occupied from available, increase step and keep solving
                if solve( [a for a in avl if a not in occupied], step + 1 ) :
                    for occ in occupied :
                        solution[occ] = step
                    return True

# initial conditions for this configuration
#
# . . x . . .
# . . . . . .
# . . x . . x

X_SIZE, Y_SIZE = 6, 3
forbidden_cells = [(0,2),(2,2),(2,5)]

if __name__ == '__main__' :
    available = []

    # fill the available cells list
    for i in range(Y_SIZE) :
        for j in range(X_SIZE) :
            if (i,j) not in forbidden_cells :
                available.append( (i,j) )

    # print the original problem
    for i in range(Y_SIZE) :
        for j in range(X_SIZE) :
            print '.' if (i,j) in available else 'x',
        print

    # solve away!
    if solve( available, 1 ) :
        for i in range(Y_SIZE) :
            for j in range(X_SIZE) :
                print solution[(i,j)] if (i,j) in available else 'x',
            print
    else :
        print 'sorry, no solution found'

rawDescription: Python algorithm to solve triomines puzzle with backtracking


code:
def changeLabel(*args): # require *args for trace
    print "changeLabel", args

    custname_int = 0 # default value when error
    try:
        custname_int = int( custname.get() )
    except:
        pass # if error do nothing

    if 2 < custname_int <= 6:
        if relStatus.get() == 'HOSTS':
            label4Text.set("255.255.255.248")
        else: #if relStatus.get() == 'NETWORKS':
            label4Text.set("224.0.0.0")
    else:
        label4Text.set("-")

rawDescription: need assistance with Entry Tkinter in Python 2.5

description: Need assistance with Entry Tkinter in Python 2.5


code:
import unittest
import contextlib
import sys

@contextlib.contextmanager
def context_mgr():
    print "setting up context"
    try:
        yield
    finally:
        print "tearing down context"

class TestSomeClass(unittest.TestCase):
    def test_normal(self):
        with context_mgr() as c:
            print "normal task"

    def test_raise(self):
        with context_mgr() as c:
            print "raise task"
            raise RuntimeError

    def test_exit(self):
        with context_mgr() as c:
            print "exit task"
            sys.exit(1)

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

rawDescription: Testing methods each with a different setup/teardown


code:
import re
txt = ''' foo
bar
baz '''
for found in re.finditer('(.*)', txt):
    print found.groups()

for found in re.finditer('.*', txt):
    print found.groups()

for found in re.finditer('.*', txt, re.DOTALL):
    print found.groups()

for found in re.finditer('(.*)', txt, re.DOTALL):
    print found.groups()

rawDescription: regular expressions : (.*), (.*?) and .*

description: Regular expressions : (.*), (.*?) and .*


code:
with open("miles.txt","r") as fin:
    cities, distances = parse_cities(fin)
print cities['Waycross, GA'] # (3122, 8235, 19371)
print distances['Youngstown, OH', 'Yakima, WA'] # 1513

rawDescription: So How could I get the coordinates and population of the city (retun them to a list) by calling the city names that are already created?


code: print type(f.addToList)

rawDescription: Understanding python variable scope within class


code:
def yahooscores():
    results = {}

    for table in soup.find_all('table', class_='scores'):

        for row in table.find_all('tr'):
            scores = []
            name = None
            for cell in row.find_all('td', class_='yspscores'):
                link = cell.find('a')
                if link:
                    name = link.text
                elif cell.text.isdigit():
                    scores.append(cell.text)
            if name is not None:
                print ('%s: %s' % (name, ', '.join(scores)) + '.')

        print ""

yahooscores()

rawDescription: Taking These Tables and Associating Their Appropriate Teams Together


code:
from pyparsing import *

keywords = ["select", "show", "team", "from", "to", "commits", "and", "or"]
[select, show, team, _from, _to,  commits, _and, _or] = [ CaselessKeyword(word) for word in keywords ]

# define an expression to prevent matching keywords as user names - used below in users expression
keyword = MatchFirst(map(CaselessKeyword, keywords))

user = Word(alphas+"."+alphas)  # ??? what are you trying to define here?
user2 = Combine(user + "'s")
# must not confuse keywords like commit with usernames - and use ungroup to
# unpack single-element token lists
users = ungroup(~keyword + (user|user2))

#~ bnf = (show|select)+Group(users).setResultsName("users")+Optional(team)+(commits).setResultsName("stats") \
    #~ + Optional(_from + quotedString.setParseAction(removeQuotes)('from') +
                    #~ _to + quotedString.setParseAction(removeQuotes)('to'))

def convertToDatetime(tokens):
    # change this code to do your additional parsing/conversion to a Python datetime
    return tokens[0]
timestamp = quotedString.setParseAction(removeQuotes, convertToDatetime)

# similar to your expression
# - use delimitedList instead of OneOrMore to handle comma-separated list of items
# - add distinction of "xxx team" vs "xxx"
# - dropped expr.setResultsName("name") in favor of short notation expr("name")
# - results names with trailing '*' will accumulate like elements into a single
#   named result (short notation for setResultsName(name, listAllValues=True) )
# - dropped setResultsName("stats") on keyword "commits", no point to this, commits must always be present
#
bnf = ((show|select)("command") + delimitedList(users("team*") + team | users("user*")) + commits +
            Optional(_from + timestamp('from') + _to + timestamp('to')))

test = 'show abc, def team, xyz commits from "Jan 10,2015" to "27/1/2015"'

print bnf.parseString(test).dump()

rawDescription: Parse and group multiple items together using Pyparse


code:
s = '\\x70f=l\\x26hl=en\\x26geocode=\\x26q\\x3c'
print s

rawDescription: How to convert hex string "\x89PNG" to plain text in python

description: Convert hex string "\x89PNG" to plain text in python


code:
gen = base3sequence()
for i in range(10):
    print next(gen)

rawDescription: Improve efficiency of modules

description: Improve efficiency of modules


code: print pos_tag.func_code.co_consts[0]

rawDescription: Probing a python function

description: Probing a python function


code:
def machine(request, ip):
    print machine_ip
    ip = Machine.objects.get(ip=ip)
    context_dict['machine_ip'] = machine_ip.replace('.', '-')
    return render(request, 'topology/machine.html', context_dict)

rawDescription: Replacing dots in URL for Django

description: Replacing dots in URL for Django


code:
with open('data.csv') as f:
    cf = csv.DictReader(f, fieldnames=['city'])
    for row in cf:
        print row['city']

rawDescription: Python csv without header

description: Python csv without header


code:
import collections
tree = lambda: collections.defaultdict(tree)
root = tree()
add_element(root, ['toplevel', 'secondlevel', 'thirdlevel'], 1)
add_element(root, ['toplevel', 'anotherlevel'], 2)
print root

rawDescription: How do I build a tree dynamically in Python

description: Build a tree dynamically in Python


code:
s = "31/12/9999"
L = s.split('/') # L now contains ['31', '12', '9999']
print '/'.join(L[::-1]) # Reverse the list, then print all the content in the list joined by a /

rawDescription: Invert the order of a string, while preserving the original contents of the string


code:
import sys
import inspect
from functools import wraps
from bs4 import BeautifulSoup

# Decorator with returning None and trace info if
# soup.find or soup.find_all fails at a certain point
def robust_soup(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except AttributeError:
            # just an example without formatting
            print inspect.getinnerframes(sys.exc_info()[2])
    return wrapper

rawDescription: How to avoid (nested) if/else statements by using BeautifulSoup with a decorator?

description: Avoid (nested) if/else statements by using BeautifulSoup with a decorator


code:
from bs4 import BeautifulSoup

data = """

    xxxxxxx
    e3aa8247-354b-e311-b6eb-005056b42341
    yyyyyy
    e3aa8247-354b-e311-b6eb-005056b42342
    zzzz

"""

soup = BeautifulSoup(data)

print soup.find("td", text="xxxxxxx").find_next_sibling('td').text

rawDescription: How to write python regex to find guid in html?

description: Write python regex to find guid in html


code:
from compiler import parse
from compiler.ast import *

def literal_eval(node_or_string):
    """
    Safely evaluate an expression node or a string containing a Python
    expression.  The string or node provided may only consist of the
    following Python literal structures: strings, numbers, tuples,
    lists, dicts, booleans, and None.
    """
    _safe_names = {'None': None, 'True': True, 'False': False}
    if isinstance(node_or_string, basestring):
        node_or_string = parse(node_or_string, mode='eval')
    if isinstance(node_or_string, Expression):
        node_or_string = node_or_string.node
    def _convert(node):
        if isinstance(node, Const) and isinstance(node.value,
                (basestring, int, float, long, complex)):
             return node.value
        elif isinstance(node, Tuple):
            return tuple(map(_convert, node.nodes))
        elif isinstance(node, List):
            return list(map(_convert, node.nodes))
        elif isinstance(node, Dict):
            return dict((_convert(k), _convert(v)) for k, v
                        in node.items)
        elif isinstance(node, Name):
            if node.name in _safe_names:
                return _safe_names[node.name]
        elif isinstance(node, UnarySub):
            return -_convert(node.expr)
        raise ValueError('malformed string')
    return _convert(node_or_string)

print literal_eval("(1, [-2, 3e10, False, True, None, {'a': ['b']}])")

rawDescription: Converting Tuple to Dictionary

description: Converting Tuple to Dictionary


code:
#!/usr/bin/python

import threading
import time

globalVar = 1;

def sub1  ( self ):
  global globalVar ;

  print str(globalVar) + " in der 1. subfunktion von " +str(self.name)
  print "teste self"  + str(self.counter) + " - " + str(globalVar) + " " +str(self.name) ;
  globalVar += 1 ;
  time.sleep(1);
  sub2 (self.name) ;
  return None ;

class myThread (threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
    def run(self):
        print "Starting " + self.name + " mit zaehler " + str(self.counter)
        sub1 (self);

threadLock = threading.Lock()
threads = [] ;

# Create new threads
count =0;
while count < 10 :
        count += 1;
        threadX = myThread(count, "Thread-" + str(count), count)
        threadX.start()
        threads.append(threadX)

for t in threads:
    t.join()
print "Exiting Main Thread"

rawDescription: python : multithreading : self as global variable

description: Python : multithreading : self as global variable


code:
def wrapper(func):
    def wrapped():
        print 'Enter'
        result = func()
        print 'Exit'
        return result
    return wrapped

rawDescription: Python Method Definition Using Decorators


code:
# test.py
from app2 import *

c = Class1()
d = Class2()
try:
    e = ClassX()
except NameError:
    print "Working as intended!"

rawDescription: Python, could I import * without Imported classes


code:
import datetime
import logging
import socket

def doit():
    size = 1024 * 4
    start = datetime.datetime.now()
    for _ in range(10):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect(('216.58.216.4', 80))
        sock.send('GET / HTTP/1.0\n\r\n\r')
        data = ''
        newData = 'dummy'
        while newData != '' and not newData.endswith('\r\n'):
            newData = sock.recv(size)
            #print newData
            data += newData
        print '.'
        sock.close()
    msg = 'timespent:{} for:{}'.format(
        datetime.datetime.now() - start, len(data))
    logging.info(msg)
    print('<p>{}</p>'.format(msg))

doit()

rawDescription: App Engine Socket API factor 8 slower than native python


code:
with open("Test.txt", 'r') as f:
    for line in f:
        a, b = getStartEnd(line.rstrip())
        print a, b

rawDescription: Function does not work properly in for loop - Python


code:
foos = Foo.objects.filter(bar='baz')

if foos:   # nonzero() calls exists() which causes extra query
           # even though the QS is already going to be evaluated
           # which in my projects is a common pattern.
   print "Yay for foos!"
   for foo in foos:
       print foo

rawDescription: Django: Any difference between `QuerySet.__nonzero__` and `QuerySet.exists`?


code:
def perform_function_checks(function_list):
    for afunc in function_list:
        if getattr(afunc, 'takes_args'):
            print "function '%s' takes args! how unusual!" % (afunc.__name__,)
        if getattr(afunc, 'number_of_args'):
            print "function '%s' takes %s args." % (afunc.__name__, afunc.number_of_args)
        if getattr(afunc, 'type_of_args'):
            print "function '%s' takes %s args: %s" (afunc.__name__, len(afunc.type_of_args), [", and ".join(str(item)) for item in afunc.type_of_args])
        if getattr(afunc, 'uses_black_magic'):
            print "oh no! function '%s' uses black magic!" % (afunc.__name__,)

perform_function_checks([add, subtract, get_attribute_value])

# prints:
# function 'add' takes args! how unusual!
# function 'add' takes 2 args.
# function 'add' takes 2 args: <type 'int'>, and <type 'int'>
# function 'subtract' takes args! how unusual!
# function 'subtract' takes 2 args.
# function 'subtract' takes 2 args: <type 'int'>, and <type 'int'>
# function 'get_attribute_value' takes args! how unusual!
# function 'get_attribute_value' takes 2 args.
# function 'get_attribute_value' takes 2 args: <type 'object'>, and <type 'str'>
# oh no! function 'get_attribute_value' uses black magic!

rawDescription: What method attributes are used in Django?


code:
if spam:
    do_eggs() # We arrive here if spam is true
elif ham:
    do_sausages() # Here if spam was false but ham is true
else:
    print "Camelot!" # And here if both were false

rawDescription: Quick question regarding format of if-elif statements


code:
from itertools import product
list(product(range(3), range(4)))
def print_array(l):
    for p in product(*map(range, l)):
        print p

print_array([3, 4])

rawDescription: Python specific format output with itertools.product


code:
with io.open( file_dir + file_name, 'w', encoding='utf-8' ) as file:
    file.write(u'[')
    for count, small_group in enumerate(self.small_groups.values()):
        if count:  # not the first
            file.write(u',')
        json_object = json.dumps(small_group, ensure_ascii=False)
        file.write(unicode(json_object))
    file.write(u']')

print '{} small group JSON objects successfully written to the file {}.'.format(
    len(self.small_groups), file.name)

rawDescription: Python - formatting JSON output to file

description: Python - formatting JSON output to file


code:
class Weapons(object):
    def __init__(self, base):
        for weapon in ["saber", "sword", "axe"]:
            setattr(self, weapon, base[:])

w = Weapons([100, 100, 100])
print w.__dict__

#output: {'sword': [100, 100, 100],
#         'saber': [100, 100, 100],
#         'axe': [100, 100, 100]}

w.axe[0] = 10
print w.axe       # output: [10, 100, 100]
print w.sword     # output: [100, 100, 100]

rawDescription: Define variables with the same list data but different objects using python


code:
for x in range(1, len(row)):
    print row[x]

rawDescription: Building list of lists from CSV file

description: Building list of lists from CSV file


code:
from selenium import webdriver

browser = webdriver.Firefox()
#code to get you to the page
select_box = browser.find_element_by_name("countries") # if your select_box has a name.. why use xpath?..... this step could use either xpath or name, but name is sooo much easier.
options = [x for x in select_box.find_elements_by_tag_name("option")] #this part is cool, because it searches the elements contained inside of select_box and then adds them to the list options if they have the tag name "options"
for element in options:
    print element.get_attribute("value") # or append to list or whatever you want here

rawDescription: Listing select option values with Selenium and Python


code:
import pymongo
from datetime import datetime

class AccountsDB():
    def __init__(self):
        pass
    def store_info(self, user_info=None):
        try:
            conn = pymongo.Connection('localhost', 27017)
            db_name = 'accountsdb'
            coll_name = 'user_info'
            db = conn[db_name]
            coll = db[coll_name]
            print "Successfully connected to '%s'" % db_name
            if user_info is not None:
                user_info['created_time'] = datetime.now()
                print user_info
                coll.insert(user_info)
            else:
                print 'No user_info'
            print "Data where stored in database"
        except:
            print "Some Error Occured"

a = AccountsDB()
a.store_info({})

rawDescription: insert current time into mongo using pymongo

description: Insert current time into mongo using pymongo


code:
class Foo:
  pass # dummy class

Foo.bar = lambda self: 42

x = Foo()
print x.bar()

rawDescription: Can you monkey patch methods on core types in python?

description: Monkey patch methods on core types in python


code:
def reroute_ajaxy(ajax_handler):
    def wrap(f):
        def decorate(*args, **kwargs):
            if args[0].is_ajax():
                return ajax_handler(args)
            else:
                return f(*args, **kwargs)
        return decorate
    return wrap

def score_ajax_handler(request):
    print "score ajax handler"

@reroute_ajaxy(score_ajax_handler)
def score_handler(request):
    print "score handler"

rawDescription: Python separating Ajax requests from normal page views


code:
b = d.setdefault('b', [])
if val not in b:
  b.append(val)

print d

rawDescription: Efficient way to either create a list, or append to it if one already exists?


code:
import xml.dom.minidom as md
dom = md.parse('wu.xml')
strings = dom.getElementsByTagName('string')
for string in strings:
    print get_text(string)

def get_text(el):
    """get_text
    For text nodes, returns the text. For element nodes, recursively call the
    function to aggregate all the text nodes into a string"""
    msg = ''
    for n in el.childNodes:
        if n.nodeType == n.TEXT_NODE:
            msg += n.nodeValue
        elif n.nodeType == n.ELEMENT_NODE:
            msg += get_text(n)
    return msg

rawDescription: How to read strings with tag <xliff:g> from xml file by python3

description: Read strings with tag <xliff:g> from xml file by python3


code:
import numpy as np

B = np.random.rand(3, 32, 128)
a = np.random.rand(32, 32)

def f1(a, B):
    Y   = dot(a,B[0])
    dY  = dot(a,B[1])
    ddY = dot(a,B[2])
    return array([Y,dY,ddY])

def f2(a, B):
    result = np.empty((B.shape[0], a.shape[0], B.shape[-1]))
    for i in xrange(B.shape[0]):
        np.dot(a, B[i], result[i])
    return result

r1 = f1(a, B)
r2 = f2(a, B)
print np.allclose(r1, r2)

rawDescription: Numpy dot() and array casting performace optimization


code:
import threading
import time

def func_1():
    i = 0
    while i<5:
        i += 1
        time.sleep(1.5) # Do some work for 1.5 seconds
        print 'func_1'

def func_2():
    i = 0
    while i<5:
        i += 1
        time.sleep(0.5) # Do some work for 0.5 seconds
        print 'func_2'

thread1 = threading.Thread(target=func_1)
thread1.start()
thread2 = threading.Thread(target=func_2)
thread2.start()

rawDescription: Two while loops at one?

description: Two while loops at one


code:
import mechanize

br = mechanize.Browser()
br.open(URL)
br.select_form(nr=0)
for i in range(0, len(br.find_control(type="checkbox").items)):
    if "modify" not in str(br.find_control(type="checkbox").items[i]):
        br.find_control(type="checkbox").items[i].selected =True
reponse = br.submit()
print reponse.read()

rawDescription: Python mechanize checkboxes

description: Python mechanize checkboxes


code:
#!/usr/bin/python

class example():
    def doMath(self, x, y):
        z = x + y
        z = self.square(z)
        return z

    def square(self, z):
        z = z * z
        return z

p = example()
print p.doMath(5,4)

rawDescription: Python: Calling Methods from a Method

description: Python: Calling Methods from a Method


code:
a = 1
b = 2
do_something_with_globals(a=a,b=b)
print a_in
print b_in

rawDescription: Python change function input name internaly


code:
#!/usr/bin/python

# Import modules for CGI handling
import cgi, cgitb
import urllib2
import json

# Create instance of FieldStorage
form = cgi.FieldStorage()

#download data from request parameter 'url'
print "Content-type:text/javascript\r\n\r\n"
url = form.getvalue("url")
callback = form.getvalue("callback")
req = urllib2.Request(url)
response = urllib2.urlopen(req)
try:
    data = response.read()
    print callback + "(" + json.dumps(data)+ ")"
finally:
    response.close()

rawDescription: Trouble retrieving JSON from python script via $.getJSON in javascript


code:
if start_timestamp < start_created_at < end_timestamp:
    print 'yay!'

rawDescription: Converting Twitter time zone and performing searches


code:
import re
print re.sub(r'[\xc2\x99]'," ","Hello\xc2There\x99")

rawDescription: efficiently replace bad characters

description: Efficiently replace bad characters


code:
for item1 in np.column_stack([data])[:,0]:
    print item1

rawDescription: determine number of rows of an (M,N) array and (M,) array with same function

description: Determine number of rows of an (M,N) array and (M,) array with same function


code:
import re
mystring = '* Text1'
print re.sub(r'\* (.*)',r'\\begin{itemize} \n\\item \1 \n\\end{itemize}',mystring)

rawDescription: Regex - Replace tags and special characters, saving the text within the tags


code:
def foo():
    bar = []
    for spam in ('ham', 'eggs', 'salad'):
        bar.append(lambda: spam)
    return bar

for bar in foo():
    print bar()

rawDescription: What exactly is contained within a obj.__closure__?


code:
import re
s = 'neighbors= {5 7 9 11 13 14 15 16 17 }'
r = re.compile('(\d+)')
print re.findall(r,s)

rawDescription: python regex to capture a pattern which appear an unknown number of times

description: Python regex to capture a pattern which appear an unknown number of times


code:
# AFFECTS everything reachable from the directory named in "top",
# assuming there are no symbolic links.
# CAUTION:  This is dangerous!  For example, if top == '/', it
# could affect all your disk files.

import os, arcpy, sys, datetime
top = os.getcwd()
RootOutput = top
FileTypes=['shp']
SearchStrings=['Temp_Pol', 'Spatial_Ex']

filecount=0
#successcount=0
#errorcount=0

print "Working in: "+os.getcwd()

list =[]
f = open(RootOutput+'\\Success_LOG.txt', 'a')
f.write("Log of files Succesfully processed. RESULT of process run @:"+str(datetime.datetime.now())+"\n")
f.close()

for root, dirs, files in os.walk(top, topdown=False):
    for fl in files:
      currentFile=os.path.join(root, fl)
      for FileType in FileTypes:
          status= str.endswith(currentFile,FileType)
          if str(status) == 'True':
              for SearchString in SearchStrings:
                  if str(SearchString in currentFile) == 'True':
                    #print str(currentFile)+str(status)
                    filecount=filecount+1
                    list.append(currentFile)

print 'Merging: ' + str(list)

rawDescription: Python: Current directory in an os.walk

description: Python: Current directory in an os.walk


code:
import wx

class Frame(wx.Frame):
    def __init__(self, parent=None, id=-1, title='Title', pos=wx.DefaultPosition, size=(300, 300)):
        wx.Frame.__init__(self, parent, id, title, pos, size)
        self.panel = wx.Panel(self, -1, pos=(0, 0), size=(300, 300))
        self.b = wx.Button(self.panel, -1, "Click me!", (50,50))
        self.Bind(wx.EVT_BUTTON, self.OnButton, self.b)
        result = self.b.IsShown()
        print "Button visible? : %s" % (result)

    def OnButton(self, evt):
        self.b.Hide( )
        result = self.b.IsShown()
        print "Button visible? : %s" % (result)

class App(wx.App):
    def OnInit(self):
        self.frame = Frame()
        self.frame.Show()
        return True

def main():
    app = App()
    app.MainLoop()

if __name__ == '__main__':
    main()

rawDescription: WXPYTHON: How to check If a frame is already visible?


code:
print "hello world"
three = 1+2
In[len(In)-1]

rawDescription: IPython notebook read string from raw text cell


code:
class C(object):
    x = []

CopyOfC = type('CopyOfC', C.__bases__, dict(C.__dict__))

c = C()
coc = CopyOfC()

c.x.append(1)
coc.x.append(2)

print c.x   # Prints '[1, 2]' (!)
print coc.x # Prints '[1, 2]' (!)

rawDescription: How to copy a python class?

description: Copy a python class


code:
for row in data['store']['rows']:
    print row

rawDescription: reading Unicode string as json object in python

description: Reading Unicode string as json object in python


code:
# limit number of retries to
# avoid infinite looping
retries = 30
while not 'key' in my_dict:
    my_dict = external_service.query_for('my_dict')
    time.sleep(0.5)  # wait a bit between retries
    retries -= 1
    if retries == 0:
        # if no data after all retries, give up
        print 'Failed getting data from the server!'
        break

# if everything was ok, the data should be here,
# if not, all retries have failed and nothing we can do
image = requests.get(my_dict['key'])

rawDescription: python while loop to poll server for x amount of time for specific dictionary key

description: Python while loop to poll server for x amount of time for specific dictionary key


code:
dct_vals = [1,2,3,4]
dict2 = dict(zip(dct_keys, dct_vals))
print dict2

rawDescription: How to create unique key(keys defined in some list) for dictionary and assign value to it in for loop in python 2.7?

description: Create unique key(keys defined in some list) for dictionary and assign value to it in for loop in python 2.7


code:
server = ServerThread()
server.start() # The server is now running
print "I want to continue my code after this..."

rawDescription: Python Multithreading XMLRPC server (?)

description: Python Multithreading XMLRPC server (?)


code:
for item in Post.objects:
    print item.title

rawDescription: How to retrieve records in a different collection using python's mongoengine?

description: Retrieve records in a different collection using python's mongoengine


code:
for name, number, weight in elements:
    print '{:>10}: {:3d} {:7.3f}'.format(name, number, weight)

rawDescription: Using for loops with nested loops in python


rawDescription: How do I sort through lines of a text document to find a phrase based on a date?

description: Sort through lines of a text document to find a phrase based on a date


code:
import re
parts = re.split("([\+\-])", c)
parts.insert(3, ',')
print "".join(parts[1:])

rawDescription: Python- insert a character into a string


code:
from selenium import webdriver

login = "mylogin"
password = "mypassword"

driver = webdriver.Firefox()
driver.get('http://elogbook.ofdc.org.tw/')

# fill the form
driver.find_element_by_id('Login1_UserName').send_keys(login)
driver.find_element_by_id('Login1_Password').send_keys(password)
driver.find_element_by_id('Login1_RadioButtonList_Type_0').click()

# submit
driver.find_element_by_id('Login1_LoginButton').click()

driver.get('http://elogbook.ofdc.org.tw/Member/CatchReportTuna.aspx')
print driver.page_source

rawDescription: Login to a website with a radio button using python's request module


code:
import re

from bs4 import BeautifulSoup
import requests

url = 'https://zkillboard.com/kill/44998359/'

pattern = re.compile(r"(?s)Name: (.*?)Security: (.*?)Corp: (.*?)Alliance: (.*?)")

with requests.Session() as session:
    session.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36'}

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

    data = soup.select('form.form textarea#eft')[0].text

    for name, security, corp, alliance in pattern.findall(data):
        print name.strip()

rawDescription: Unsure how to web-scrape a specific value that could be in several different places


code:
result = []
while total:
    result.append(chr(total % 128))
    total >>= 7
print ''.join(reversed(result))

rawDescription: Obfuscating Strings with ASCII and base 128


code:
from lxml import etree
from lxml.etree import XMLParser

data = """..."""

tree = etree.fromstring(data, parser=XMLParser())
team = tree.xpath('.//number[text()="78"]/..')[0]
team.find('high_goals_made_tel').text = "I've changed"

print etree.tostring(tree)

rawDescription: Eddited XML File with certain element

description: Eddited XML File with certain element


code:
out=[]
for r in result:
    s  = str(result[r]).replace(', ', ',').replace("'",'')
    if s.endswith(",)"): s = s[1:-2]
    out.append(r+':'+s)

print ",".join(out)

rawDescription: how to insert multiple values to a single unique key in python

description: Insert multiple values to a single unique key in python


code:
filePath = r'E:\dir1\Logs\dir2\1998-12-23\message.txt'
splitFilePath = filePath.rsplit ('\\', 1)
directory = splitFilePath [0]
fileName = splitFilePath [1]
print directory
print fileName

rawDescription: Extract Directory Names from Dynamic File Paths in Python


code:
a = {'name': 'foo', age: 12}
print '{name} is {age} old'.format(**a)

rawDescription: Can I express named Python string arguments more succintly?

description: Express named Python string arguments more succintly


code:
class TACRG(QtGui.QMainWindow, design.Ui_MainWindow):
    def __init__(self, parent=None):
        # Your init code
        self.report_start, self.report_end, self.accs = [None] * 3

    def r_chosen(self, text):
        self.report_start, self.report_end = report_period(text)
        self.other_method()

    def c_chosen(self, text):
        self.accs = get_ucs(text)
        self.other_method()

    def other_method(self):
        #validate if your fields have valid values
        if self.report_start and self.report_end and self.accs:
            print self.report_start, self.report_end, self.accs

rawDescription: How to use QComboBox selection in function

description: Use QComboBox selection in function


code:
a = [{"url": "http://wwww.ff.com", "msg": "msg 1..."},
     {"url": "http://wwww.yy.com", "msg": "msg 2..."},
     {"url": "http://wwww.xx.com", "msg": "msg 3..."}]
print a[0]["url"]
# http://wwww.ff.com

rawDescription: How to do a multidimensional array in python?

description: Do a multidimensional array in python


code:
should_restart = True
while should_restart:
  should_restart = False
  for i in xrange(10):
    print i
    if i == 5:
      should_restart = True
      break

rawDescription: Python - Way to restart a for loop, similar to "continue" for while loops?


code:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

PORT_NUMBER = 8080
DATA = '
test
'  # supposed to come from BeautifulSoup

class MyHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write(DATA)
        return

try:
    server = HTTPServer(('', PORT_NUMBER), MyHandler)
    print 'Started httpserver on port ', PORT_NUMBER
    server.serve_forever()
except KeyboardInterrupt:
    print '^C received, shutting down the web server'
    server.socket.close()

rawDescription: Can beautiful soup output be sent to browser?


code:
response = '''BEST SHOW EVER http://www.myradio.foo/xml/logos/showlogo.jpg Avicii Hey Brother'''

## parsing using named group
m = re.match("(?P<showtitle>.*?)\s+(?P<image>https?://\S+)\s+(?P<song>.+)", response);
mystring = m.groupdict()
print mystring['song']

rawDescription: Auto completing song titles and parsing a string


code:
body2 = "".join(body)

print body2

rawDescription: Removing <u> character from text using Scrapy


code:
def is_row_zero(row):
  parts = row.split(":")
  value = parts[1].strip()
  if value == "0":
    print "%s is 0, not allowed" % parts[0]
    return True
  return False

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

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


code:
import sys
import inspect

class My_Context(object):
    def __init__(self,mode=0):
        """
        if mode = 0, proceed as normal
        if mode = 1, do not execute block
        """
        self.mode=mode
    def __enter__(self):
        if self.mode==1:
            print 'Met block-skipping criterion ...'
            # Do some magic
            sys.settrace(lambda *args, **keys: None)
            frame = inspect.currentframe(1)
            frame.f_trace = self.trace
    def trace(self, frame, event, arg):
        raise
    def __exit__(self, type, value, traceback):
        print 'Exiting context ...'
        return True

rawDescription: Skipping execution of -with- block

description: Skipping execution of -with- block


code:
print capwords("This is an IRS test.")    # Produces: "This Is An IRS Test."
print capwords("This is an I.R.S. test.") # Produces: "This Is An I.R.S. Test."

rawDescription: is there a string method to capitalize acronyms in python?

description: Is there a string method to capitalize acronyms in python


code:
if __name__ == '__main__':
    hid = '282'
    b = (hid)+"\n"
    hidtext = open("foo.txt").readlines()
    r = str (b) in hidtext
    print hidtext
    print r
    print (hid)
    print b

rawDescription: python search through text files composed of lines

description: Python search through text files composed of lines


code:
@max_execs(5)
def f():
    print "hi!"

rawDescription: How are these type of python decorators written?


code:
import xml.etree.cElementTree as ET

tree = ET.parse("filexml")
root = tree.getroot()

for child_root in root.iter():
    print  child_root.tag, child_root.attrib

rawDescription: Rewrite SOAP xml before sen it as request


code:
a = input("Enter word: ")
a = a.strip()
total = 0
for b in a:
  c = SCORES[b]
  total += c
print total

rawDescription: I need to add all my output numbers from my loop?


code:
class AttributeObject(object):
    def __init__(self, base):
        self.base = base
        self.adjustments = []
        print("Attribute initialized to %s" % base)

    def __compute(self):
        return self.base + sum(self.adjustments)

    def __int__(self):
        return self.__compute()

    def __iadd__(self, delta):
        print("Adjustment added: %s" % delta)
        self.adjustments.append(delta)
        return self

    def __isub__(self, delta):
        print("Adjustment added: %s" % -delta)
        self.adjustments.append(-delta)
        return self

class Creature(object):
    def __init__(self, health=100, armor=0):
        self.health = AttributeObject(health)
        self.armor =  AttributeObject(armor)

c1 = Creature(50)
#c1.health = 60         # Can't do this, because it will override the AttributeObject
print int(c1.health)    # 60
c1.health += 10         # Add a +10 adjustment
print int(c1.health)    # 70
print c1.health.adjustments  # [10]

rawDescription: Creating an Attribute class that allows adjusting a base value?


code:
a = ['agency[15]','assignment72,','you’11','america’s']
import re
b = re.compile('\w+')
for item in a:
    print b.search(item).group(0)
b = re.compile('[a-z]+')
for item in a:
    print b.search(item).group(0)

rawDescription: How do I use rstrip to remove trailing characters?

description: Use rstrip to remove trailing characters


code:
class MyClass:
    def __init__(self):
        print "I'm in a compiled class (I hope)"

    def somemethod(self):
        print "in some method"

    def isodd(self, n):
        return 1 == n % 2

rawDescription: Build Python scripts and call methods from C#


code:
def askForNumber():
    while True:
        try:
            return int(raw_input('> '))
        except ValueError:
            print "Not a number, please try again"

def guessNumber():
    user = askForNumber()
    while user != computer:
        if user > computer:
            print "Your number is too big"
            user = askForNumber()
        else:
            print "Naa! too small. Try a bit higher number"
            user = askForNumber()
    print "Now the numbers are equal"

rawDescription: Python simple number comparison

description: Python simple number comparison


code: print ''.join(x.stripped_strings)

rawDescription: Parsing HTML page using beautifulsoup

description: Parsing HTML page using beautifulsoup


rawDescription: Decode unicode string in python

description: Decode unicode string in python


code:
def method4():
  str_list = []
  for num in xrange(loop_count):
    str_list.append(`num`)
  return ''.join(str_list)

rawDescription: How slow is Python's string concatenation vs. str.join?


code:
string = "PLEASE"
if (any(x.isalpha() for x in string)
    and any(x.isspace() for x in string)
    and all(x.isalpha() or x.isspace() for x in string)):
    print "match"
else:
    print "no match"

string = "PLEASE "
if (any(x.isalpha() for x in string)
    and any(x.isspace() for x in string)
    and all(x.isalpha() or x.isspace() for x in string)):
    print "match"
else:
    print "no match"

rawDescription: Checking if string is only letters and spaces - Python


code:
from bs4 import BeautifulSoup

html = '\

\

Hindi and \
      English

	'

soup = BeautifulSoup(html)
print soup.find('a',{'class':''}).string
print soup.find('a',{'class':''}).nextSibling.nextSibling.string

rawDescription: accessing values in subgroups in html code

description: Accessing values in subgroups in html code


rawDescription: How to intialise a Numpy array of numpy arrays

description: Intialise a Numpy array of numpy arrays


code:
import socket
import time

listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener.bind(("localhost", 12346))
listener.listen(1)
sock,_ = listener.accept()

interval = 0.5
while 1:
  interval += 1 # increase interval by 1 second
  time.sleep(interval)
  # Get 1MB but will be really limited by the buffer
  data = sock.recv(1000000)
  print interval, len(data)
  if not data:
    break

rawDescription: How to prevent blocking forever on urlopen

description: Prevent blocking forever on urlopen


code:
from bs4 import BeautifulSoup

html = """
PC1	zz:zz:zz:zz:zz:ce	10.0.0.244	23 hours, 55 minutes, 25 seconds

PC2	zz:zz:zz:zz:zz:cf	10.0.0.245	23 hours, 23 minutes, 27 seconds
"""

soup = BeautifulSoup(html)
mac = []
ip = []

for tr in soup.find_all('tr'):
    cols = [td.text for td in tr.find_all('td')]
    mac.append(cols[1])
    ip.append(cols[2])

print mac
print ip

rawDescription: regex/beautifulsoup how to extract the all values of column from html table?

description: Regex/beautifulsoup how to extract the all values of column from html table


code:
print d['supplier A'].projects
print [ float(bid)/winbid for bid,winbid in d['supplier A'].bids ]

rawDescription: search large number of text files python

description: Search large number of text files python


code:
reduce(lambda (bcount, ccount), (b, c): (bcount + bool(b), ccount + bool(c)),
       bullsandcows(given, number), (0, 0))

rawDescription: Reduce list of tuples

description: Reduce list of tuples


code:
import inspect
class A(object):
    @property
    def b(self):
        print inspect.stack()[0][3]

A().b

rawDescription: Getting the name of the @property being accessed, set or deleted in Python


code:
now = datetime.datetime.now()
minute = timedelta(days=0,seconds=60,microseconds=0)

print now-minute

rawDescription: Python: creating list of timestamps by minute


code:
import sys
print sys.prefix

rawDescription: Access environment variables from Python


code:
import ephem

az = 3.30084818 #rad
el = 0.94610742 #rad
lat = 34.64 #deg
lon = -103.7 #deg
alt = 35800.26 #m
ut = 2455822.20000367 #julian date

# Which Julian Date does Ephem start its own count at?
J0 = ephem.julian_date(0)

observer = ephem.Observer()
observer.lon = str(lon)  # str() forces deg -> rad conversion
observer.lat = str(lat)  # deg -> rad
observer.elevation = alt
observer.date = ut - J0

print observer.date
print observer.radec_of(az, el)

rawDescription: Convert topocentric coordinates (Azimuth, Elevation) to equatorial coordinates (RA, Dec) with PyEphem


code: print top

rawDescription: Top Counts...return tuple format of output back to dictionary format


code:
dir()
True is __builtins__.True
True = 'redefined'
__builtins__.True is True
del True
__builtins__.True is True

rawDescription: In Python how to recover from joke statements like True = False


code:
import bs4
import re

data = '''

today is a sunny day

I love when it's sunny outside

Call me sunny

sunny is a cool word sunny

'''

searched_word = 'sunny'

soup = bs4.BeautifulSoup(data, 'html.parser')
results = soup.body.find_all(string=re.compile('.*{0}.*'.format(searched_word)), recursive=True)

print 'Found the word "{0}" {1} times\n'.format(searched_word, len(results))

for content in results:
    words = content.split()
    for index, word in enumerate(words):
        # If the content contains the search word twice or more this will fire for each occurence
        if word == searched_word:
            print 'Whole content: "{0}"'.format(content)
            before = None
            after = None
            # Check if it's a first word
            if index != 0:
                before = words[index-1]
            # Check if it's a last word
            if index != len(words)-1:
                after = words[index+1]
            print '\tWord before: "{0}", word after: "{1}"'.format(before, after)

rawDescription: How to find a particular word in html page through beautiful soup in python?

description: Find a particular word in html page through beautiful soup in python


code:
from pkg_resources import resource_string, resource_listdir

# Itemize data files under proj/resources/images:
print resource_listdir('proj.resources.images', '')
# Get the data file bytes:
print resource_string('proj.resources.images', 'pic2.png').encode('base64')

rawDescription: Managing resources in a Python project

description: Managing resources in a Python project


code: print a[3 % len(a)]

rawDescription: Circular array indices in Python

description: Circular array indices in Python


code:
class HandlerCase1(object):
    def handle_stuff(self, *args, **kwargs):
        print "Handling case 1"

class HandlerCase2(object):
    def handle_stuff(self, *args, **kwargs):
        print "Handling case 2"

if __name__ == "__main__":
    handlers = []
    handlers.append(HandlerCase1())
    handlers.append(HandlerCase2())
    for h in handlers:
        h.handle_stuff()

rawDescription: Encapsulation vs. inheritance, help making a choice


code:
import numpy as np
import scipy as sp
import scipy.ndimage.morphology

# The array you gave above
data = np.array(
        [
           [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
        ])

# Fill holes to make sure we get nice clusters
filled = sp.ndimage.morphology.binary_fill_holes(data)

# Now seperate each group of contigous ones into a distinct value
# This will be an array of values from 1 - num_objects, with zeros
# outside of any contigous object
objects, num_objects = sp.ndimage.label(filled)

# Now return a list of slices around each object
#  (This is effectively the tuple that you wanted)
object_slices =  sp.ndimage.find_objects(objects)

# Just to illustrate using the object_slices
for obj_slice in object_slices:
    print data[obj_slice]

rawDescription: Finding blank regions in image

description: Finding blank regions in image


code:
from BeautifulSoup import BeautifulSoup

xml = """<xml>
  <Web>
    <Total>4000</Total>
    <Offset>0</Offset>
  </Web>
</xml>"""

doc = BeautifulSoup( xml )
print doc.xml.web.total.string
print doc.xml.web.offset.string

rawDescription: How can I access namespaced XML elements using BeautifulSoup?

description: Access namespaced XML elements using BeautifulSoup


code:
for checkActor in tree.xpath('//ns:Actor', namespaces=namespaces):
    checkActor.getparent().remove(checkActor)

print etree.tostring(tree, pretty_print=True, xml_declaration=True)

rawDescription: python remove element containing namespace

description: Python remove element containing namespace


code:
import urllib2, bs4, re

opener = urllib2.build_opener()
opener.addheaders = [("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36")]
# I omitted the cookie here and you need to copy and paste your own
opener.addheaders.append(('Cookie', 'act-bg-i...eat_uuniq=1; criteo=; pl=true'))
soup = bs4.BeautifulSoup(opener.open("http://www.heraldsun.com.au/help/rss"))
div = soup.find('div', {"id":"content-2"}).find('div', {"class":"group-content"})

for a in div.find_all('a'):
    try:
        if 'feeds.news' in a['href']:
            print a
    except:
        pass

rawDescription: It is not possible to parse a part of a webpage that is visible when open with browser


code:
for n in (6, 6.1, 6.2, 6.5, 6.8, 7):
    iwidth = intervals[1] - intervals[0]
    print np.ceil((n - intervals[0]) / iwidth)

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


code:
#!/usr/bin/python

from datetime import date

def is_consecutive(datelist):
    for idx, my_date in enumerate(datelist[:-1]):
        if ((datelist[idx + 1].month - my_date.month == 1 and
             datelist[idx + 1].year == my_date.year) or
            (datelist[idx + 1].month == 1 and
             my_date.month == 12 and
             datelist[idx + 1].year - my_date.year == 1)):
            continue
        else:
            return False
    return True

print is_consecutive([date(2010, 10, 3),
                      date(2010, 11, 8),
                      date(2010, 12, 1),
                      date(2011, 1, 11)])

print is_consecutive([date(2010, 7, 6),
                      date(2010, 8, 24),
                      date(2010, 3, 5),
                      date(2010, 10, 25)])

rawDescription: Determine consecutive dates

description: Determine consecutive dates


code: print '\n'.join( ''.join('#' for column in range(10)) for row in range(10))

rawDescription: How can I properly join these strings together (by column then row)?

description: Properly join these strings together (by column then row)


code:
host = 'stackoverflow.com'
whois = pythonwhois.net.get_whois_raw(host)
for item in whois:
    print item

rawDescription: Iterate over a list in python

description: Iterate over a list in python


code:
t = timeit.timeit(stmt="signal.fftconvolve(x, y, mode='same')", number=1,
setup="""
import numpy as np
from scipy import signal
x = np.random.random((2048, 2048)).astype(np.float32)
y = np.random.random((32, 32)).astype(np.float32)
""")
print t

rawDescription: Fastest 2D convolution or image filter in Python


code: print [i for i in dir(a) if not i.endswith('__')]

rawDescription: How can I get a full list of class members without instantiating a class?

description: Get a full list of class members without instantiating a class


code:
import pexpect

p = pexpect.spawn('unbuffer',['-p','./guess'])
l = 1
r = 100
m = 0
while (p.isalive()):
    p.expect('')
    p.expect(p.buffer[-1])
    print p.before
    print m
    if (p.before.find('small')>0):
        l = m
    elif (p.before.find('big')>0):
        r = m
    m = (l+r)/2;
    p.sendline(str(m))
print p.buffer
print 'Program ended!'

rawDescription: How to INTERACT between a program on the server and the user via a web page REAL-TIME


code:
while out['acknowledged'] is 0:
    print "not yet" #placed for debugging
    time.sleep(5)
    v = requests.get("https://api.pushover.net/1/receipts/"+ d + ".json?token=" + app)
    out = v.json() #update the out, so you can check again

rawDescription: python acknowlage in pushover using dict

description: Python acknowlage in pushover using dict


code:
temp1 = []
for boolean in aggregate:
    temp1.append(boolean)
if len(temp1) == len(propositions):
   break
print temp1
print states[0]
if tuple(temp1) == states[0]:
    print 'True'
else:
    print 'False'

rawDescription: Lists are the same but not considered equal?


code:
import requests
r = requests.get(url='http://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg',stream=True)
print r.headers['last-modified']

rawDescription: python keep-alive connection and download image

description: Python keep-alive connection and download image


code:
soup.find('p', class_='scroll-down').extract()
print soup

rawDescription: How to select everything but a certain html element using beautifulsoup4?

description: Select everything but a certain html element using beautifulsoup4