code:
from mechanize import Browser

br = Browser()
r = br.open("http://www.example.com/")

if r.code == 200:
    for link in br.links():
        print link
else:
    print "Error loading page"

rawDescription: How can I set a code for users when they enter a valud URL or not with PYTHON/Flask?

description: Set a code for users when they enter a valud URL or not with PYTHON/Flask


code:
toss = np.random.randint(0,2,100)

steps = np.diff(np.cumsum(toss) * toss)
edges = steps < 0
edges_shift = np.append(False, edges[:-1])
init_step = steps[edges][0]
toss2 = np.array(toss)
toss2[edges_shift] = np.append(init_step, np.diff(steps[edges]))
bets = 2 ** np.cumsum(toss2)

fmt_dict = {1:'l', 0:'w'}
for t, b in zip(toss, bets):
    print fmt_dict[t] + '-> {0:d}'.format(b)

rawDescription: Implement a classic martingale using Python and Pandas


code:
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
from game.models import *
from mongoengine import *
#from django.contrib.auth import authenticate
from mongoengine.django.auth import User

def login(request):
    connect('reborn')
    from django.contrib.auth import login
    from mongoengine.django.auth import User
    from mongoengine.queryset import DoesNotExist
    from django.contrib import messages
    try:
        user = User.objects.get(username='bob')#request.POST['username'])
        if user.check_password('bobpass'):#request.POST['password']):
            user.backend = 'mongoengine.django.auth.MongoEngineBackend'
            print login(request, user)
            request.session.set_expiry(60 * 60 * 1) # 1 hour timeout
            print "return"
            return HttpResponse("LOGUEJAT")#redirect('index')
        else:
            print "malament"
            messages.add_message(request,messages.ERROR,u"Incorrect login name or password !")
    except DoesNotExist:
        messages.add_message(request,messages.ERROR,u"Incorrect login name or password !")
    return render(request, 'login.html', {})

def logout(request):#NOT TESTED
    from django.contrib.auth import logout
    logout(request)
    return redirect('login')

def createuser(request):
    connect('reborn')
    User.create_user('boba','bobpass','bobsaget@fullhouse.gov')
    return HttpResponse("SAVED")

rawDescription: Django - Auth with mongoengine DB

description: Django - Auth with mongoengine DB


code:
if a_dict:
    print "I'm not here"

rawDescription: How to check if a list exists in Python

description: Check if a list exists in Python


code:
import pyparsing as pp

src0 = '''
"Hello
 World
 Goodbye and go"
'''

pat = pp.QuotedString('"', multiline=True)
print pat.parseString(src0)

rawDescription: Python/Pyparsing - Multiline quotes

description: Python/Pyparsing - Multiline quotes


code:
import re
# assuming input_file to be a file-like object
header = input_file.next()

borders = [match.start() for match in re.finditer(r'\s+', header)]
second_to_third_border = borders[1]
borders = borders[1:-1] # delete the first and last because not right-aligned

for line in input_file:
    first_to_second_border = line[:second_to_third_border].rfind(' ')
    actual_borders = [0, first_to_second_border] + borders + [len(line)]
    dset = []
    for (s, e) in zip(actual_borders[:-1], actual_borders[1:]):
        dset.append(line[s:e].strip())
    print dset

rawDescription: Split a variable table in python

description: Split a variable table in python


code:
import re

s = '''
A1a _A_1 A_ A_1_a A-1-a _A_1_a_ _A1a _A-1-A_ a1_a  A-_-5-a
_A-_-5-A a1_-1 XMDC_A1a or XMDC-A1a or XMDC_A1-a XMDC_A_1_a_ _A-1-A_
'''

def find_this(text):
    matches = re.findall(r'([_-]?[a-z][_-]?[1-9][_-]?[a-z][_-]?)', text, re.I)
    records = [m.replace('-', '').replace('_', '') for m in matches]
    print records

find_this(s)

rawDescription: Python Regular Expression to find all combinations of a Letter Number Letter Designation


code:
def galPrice(commodity, values):
    comdata = get_commodity_data(commodity)
    return dict(zip(values, [comdata[value] for value in values]))

print galPrice('Explosives', ['meanPrice', 'buyPrice', 'sellPrice'])

rawDescription: Retrieve multiple values from JSON

description: Retrieve multiple values from JSON


code:
#The egg and basket game

import pygame
from pygame.locals import *
import time
import random
clock = pygame.time.Clock()
x=260
y=500
#Screen initialize
pygame.init()
pygame.font.init()
screen=pygame.display.set_mode((600,600))
pygame.display.set_caption("egg")

#Background
cloud=pygame.image.load("clouds.png").convert_alpha()
cloud=pygame.transform.scale(cloud,(600,600))
screen.blit(cloud,(0,0))

#Basket
basket=pygame.image.load("basket.png").convert_alpha()
basket=pygame.transform.scale(basket,(80,80))
screen.blit(basket,(x,y))
pygame.display.update()

#egg
egg=pygame.image.load("eggs.png").convert_alpha()
egg=pygame.transform.scale(egg,(20,20))

#screen.blit(egg,(290,20))
pygame.display.update()

#Movement of basket
ychange=0
xchange=0
exiting=False

xegg = random.randrange(50,550)
yegg = 20
while not exiting:
#xegg=random.randrange(50,550)
#for yegg in range(20,550):
    print yegg
    if yegg<550:
        yegg += 5
        pygame.display.update()
        clock.tick(60)
        screen.blit(egg,(xegg,yegg))

    else:
        yegg=20
        xegg = random.randrange(50,550)
        yegg=yegg+ychange
        pygame.display.update()
        clock.tick(60)
        screen.blit(egg,(xegg,yegg))
    #yegg=20
    pygame.display.update()
    clock.tick(60)
    #yegg=20

    for event in pygame.event.get():
        print(event)
        if(event.type==pygame.QUIT):
            exiting=True
            pygame.quit()
            quit()
        if(event.type==pygame.KEYDOWN):
            if(event.key==pygame.K_LEFT):
                xchange=-5
            if(event.key==pygame.K_RIGHT):
                xchange=5
        screen.blit(basket,(x,y))
        if(event.type==pygame.KEYUP):
            if(event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT):
                xchange=0

        x=x+xchange
        print(x)
        screen.blit(cloud,(0,0))

        screen.blit(basket,(x,y))

        pygame.display.update()
        clock.tick(60)

i=i+1
ychange=0
    #random position of eggs

    #MOVEMENT OF egg

rawDescription: Egg and basket game in python

description: Egg and basket game in python


code:
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(raw)

for x in soup.findAll("html:td"):
   if x.string == "Equity share capital":
       VALS = [y.string for y in x.parent.findAll() if y.has_key("class")]

print VALS

rawDescription: Extract data / parse from HTML in PHP / Python


rawDescription: How to format a dictionary of lists as a table in Python?

description: Format a dictionary of lists as a table in Python


code:
class Programs:

    def __init__(self):  # class constructur (called at creation time)
        self.name = ""   # the default name is the empty string

    def ask(self):
        while 1: # infinite loop
            name = raw_input("Name: ")
            if name == "":
                print "Ops! Retry"
            else:
                print "Hello ", name
                break  # this will break the loop

        self.name = name  # assign to self.name the value name

if __name__ == '__main__':
    prog = Programs()
    prog.ask()
    raw_input("\n\nPress enter to exit.")

rawDescription: How to get user input within a Method (python)

description: Get user input within a Method (python)


code:
def reservoir(it, k):
    ls = [next(it) for _ in range(k)]
    for i, x in enumerate(it, k + 1):
        j = random.randint(0, i)
        if j < k:
            ls[j] = x
    return ls

xs = range(0, 3)
ys = range(0, 4)
zs = range(0, 5)

size = 4

print reservoir(product_no_repeats(xs, ys, zs), size)

rawDescription: Randomly generating 3-tuples with distinct elements in python


code:
class Foo(object):

    def __init__(self, a):
        self.a = a
        self.cls_method()

    @classmethod
    def cls_method(cls):
        print 'class %s' % cls.__name__

rawDescription: Is there a possibility in python to call a classmethod out of the constructor?


code:
print ", ".join(k for k, v in occurrences.items() if k != "consonants" and v > 0),
print "were inserted"
print ", ".join(str(v) for k, v in occurrences.items() if k != "consonants" and v > 0),
print "times respectively"

rawDescription: Join two for loops, separated by a print statement into one


code: print re.sub(r'([^\\])', escape_repl, '[^$]')

rawDescription: Python Regex: escaping a back-reference

description: Python Regex: escaping a back-reference


code: result = reduce(lambda data, (aname, p): algo_by_name(aname)(p, data), workflow)

rawDescription: Processing a simple workflow in Python

description: Processing a simple workflow in Python


code:
import gzip
with gzip.GzipFile("/path/to/some_log_file.gz") as input_plaintext_filehandle:
     for line in input_plaintext_filehandle:
         print line

rawDescription: BRO IDS searches

description: BRO IDS searches


code:
def get_files(directory):
    files = []
    for file in [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory,f))]:
        da = datetime.fromtimestamp(os.path.getmtime(file))
        files.append( '{0}/{1}-{2}'.format(directory,file,da)) # add time from timestamp
    return files

print sorted(get_files("."),key=lambda x: float(x.rsplit(":",1)[-1]),reverse = True)[:5] # sort based on seconds/milisecs

rawDescription: How to retrieve the most recently created files in a directory

description: Retrieve the most recently created files in a directory


code:
from bs4 import BeautifulSoup

data = """

Header1

      block of text that I want.

      another block of text that I want.

      still more text that I want.

Header2

"""

soup = BeautifulSoup(data)
header1 = soup.find('h4', text='Header1')
for item in header1.next_siblings:
    if getattr(item, 'name') == 'h4' and item.text == 'Header2':
        break

    print item

rawDescription: Getting multiple blocks of text between tags


code:
def get_next_word():
    with open("Input.txt") as in_file:
        for line in in_file:
            for word in line.strip().split():
                yield word

print any(word == "Bob" for word in get_next_word())
# True

rawDescription: Sorting a huge text file and doing a binary search


code:
for i,keypoint in enumerate(kp):
    print "Keypoint %d: %s" % (i, keypoint.pt)

rawDescription: OpenCV - After feature points detection, how can I get the x,y-coordinates of the feature points


code:
from pygments import highlight
from pygments.lexers import guess_lexer
from pygments.formatters import HtmlFormatter

code = '#!/usr/bin/python\nprint "Hello World!"'
lexer = guess_lexer(code) # or just pygments.lexers.PythonLexer()
formatter = HtmlFormatter(noclasses=True, nowrap=True, lineseparator="<br>\n")
print highlight(code, lexer, formatter)

rawDescription: Python equivalent to "php -s"

description: Python equivalent to "php -s"


code:
from bs4 import BeautifulSoup
import urllib2
url="http://rediff.com/"
page=urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
sources=soup.findAll('script',{"src":True})
for source in sources:
 print source['src']

rawDescription: Getting attribute's value using BeautifulSoup


code:
t=[foo(c) for c in l]
for c in t: print c.ID,
for c in l: print c.ID,

rawDescription: How to assign value inside list comprehension

description: Assign value inside list comprehension


rawDescription: How can i know if there are white pixels in a binary image in some coordinates (stored in a list) in python?

description: Know if there are white pixels in a binary image in some coordinates (stored in a list) in python


code:
import os
directoryPath=raw_input('Directory for csv files: ')
for i,file in enumerate(os.listdir(directoryPath)):
    if file.endswith(".csv"):
        print os.path.basename(file)

rawDescription: Python: get the names of specific file types in a given folder


code:
from django.utils.safestring import mark_safe

t = loader.get_template("sometemplate")
c = Context({
   'foo': 'bar',
   'url': mark_safe('http://127.0.0.1/test?a=1&b=2'),
})
print t.render(c)

rawDescription: How to disable HTML encoding when using Context in django

description: Disable HTML encoding when using Context in django


code:
while True:
    x = raw_input("enter > ")
    xl = []
    xl.append(x)
    print xl

rawDescription: How to keep adding inputs into a list

description: Keep adding inputs into a list


code:
def resizeHandler(signum, frame):
    print "resize-window signal caught"

signal.signal(signal.SIGWINCH, resizeHandler)

rawDescription: Want to resize terminal windows in python, working but not quite right


code:
def __import__(name, globals={}, locals={}, fromlist=[], level=-1, oldfunc=__import__):
    checklist = fromlist[:]
    checklist.extend(name.split('.'))
    for check in checklist:
        if check in ('os', 'sys'):
            print 'Uh-uh I\'m better than that...'
            return
    oldfunc(name, globals, locals, fromlist, level)

rawDescription: Is there a way to traverse all modules that can be imported


code:
if(len(upperCharacter) == 1):
    finalString = originalString.replace(upperCharacter, upperCharacter.upper())
print finalString

rawDescription: uppercasing a character in string with a letter of users choosing

description: Uppercasing a character in string with a letter of users choosing


code:
done = False
while done == False:
    with open("q0.txt", 'r') as fr, open("q0.txt", 'w') as fw:
        lines = fr.readlines()
        if len(lines) > 0:
            print lines[0] # This would be your processing
            del lines[0]
            fw.writelines(lines)
        else:
            done = True

rawDescription: Removing the line from file once processed


code:
arr7 = ctypes.c_double * `n`
x = arr7()

rawDescription: How do I wrap this C function, with multiple arguments, with ctypes?

description: Wrap this C function, with multiple arguments, with ctypes


code:
import numpy as np

n = 20
#also works for non-square A
A = np.random.randint(2, size=(n*6,n)).astype(np.int8)
##A = np.array( [[0, 0, 0], [1, 1, 1], [1, 1 ,1]], np.uint8)
##A = np.zeros((6,6))
#force the inclusion of some hits, to keep our algorithm on its toes
##A[0] = A[1]

def base_pack_lazy(a, base, dtype=np.uint64):
    """
    pack the last axis of an array as minimal base representation
    lazily yields packed columns of the original matrix
    """
    a = np.ascontiguousarray( np.rollaxis(a, -1))
    packing = int(np.dtype(dtype).itemsize * 8 / (float(base) / 2))
    for columns in np.array_split(a, (len(a)-1)//packing+1):
        R = np.zeros(a.shape[1:], dtype)
        for col in columns:
            R *= base
            R += col
        yield R

def unique_count(a):
    """returns counts of unique elements"""
    unique, inverse = np.unique(a, return_inverse=True)
    count = np.zeros(len(unique), np.int)
    np.add.at(count, inverse, 1)        #note; this scatter operation requires numpy 1.8; use a sparse matrix otherwise!
    return unique, count, inverse

def voidview(arr):
    """view the last axis of an array as a void object. can be used as a faster form of lexsort"""
    return np.ascontiguousarray(arr).view(np.dtype((np.void, arr.dtype.itemsize * arr.shape[-1]))).reshape(arr.shape[:-1])

def has_identical_row_sums_lazy(A, combinations_index):
    """
    compute the existence of combinations of rows summing to the same vector,
    given an nxm matrix A and an index matrix specifying all combinations

    naively, we need to compute the sum of each row combination at least once, giving n^3 computations
    however, this isnt strictly required; we can lazily consider the columns, giving an early exit opportunity
    all nicely vectorized of course
    """

    multiplicity, combinations = combinations_index.shape
    #list of indices into combinations_index, denoting possibly interacting combinations
    active_combinations = np.arange(combinations, dtype=np.uint32)
    #keep all packed columns; we might need them later
    columns = []

    for packed_column in base_pack_lazy(A, base=multiplicity+1):       #loop over packed cols
        columns.append(packed_column)
        #compute rowsums only for a fixed number of columns at a time.
        #this is O(n^2) rather than O(n^3), and after considering the first column,
        #we can typically already exclude almost all combinations
        partial_rowsums = sum(packed_column[I[active_combinations]] for I in combinations_index)
        #find duplicates in this column
        unique, count, inverse = unique_count(partial_rowsums)
        #prune those combinations which we can exclude as having different sums, based on columns inspected thus far
        active_combinations = active_combinations[count[inverse] > 1]
        #early exit; no pairs
        if len(active_combinations)==0:
            return False

    """
    we now have a small set of relevant combinations, but we have lost the details of their particulars
    to see which combinations of rows does sum to the same value, we do need to consider rows as a whole
    we can simply apply the same mechanism, but for all columns at the same time,
    but only for the selected subset of row combinations known to be relevant
    """
    #construct full packed matrix
    B = np.ascontiguousarray(np.vstack(columns).T)
    #perform all relevant sums, over all columns
    rowsums = sum(B[I[active_combinations]] for I in combinations_index)
    #find the unique rowsums, by viewing rows as a void object
    unique, count, inverse = unique_count(voidview(rowsums))
    #if not, we did something wrong in deciding on active combinations
    assert(np.all(count>1))

    #loop over all sets of rows that sum to an identical unique value
    for i in xrange(len(unique)):
        #set of indexes into combinations_index;
        #note that there may be more than two combinations that sum to the same value; we grab them all here
        combinations_group = active_combinations[inverse==i]
        #associated row-combinations
        #array of shape=(mulitplicity,group_size)
        row_combinations = combinations_index[:,combinations_group]

        #if no duplicate rows involved, we have a match
        if len(np.unique(row_combinations[:,[0,-1]])) == multiplicity*2:
            print row_combinations
            return True

    #none of identical rowsums met uniqueness criteria
    return False

def has_identical_triple_row_sums(A):
    n = len(A)
    idx = np.array( [(i,j,k)
        for i in xrange(n)
            for j in xrange(n)
                for k in xrange(n)
                    if i<j and j<k], dtype=np.uint16)
    idx = np.ascontiguousarray( idx.T)
    return has_identical_row_sums_lazy(A, idx)

def has_identical_double_row_sums(A):
    n = len(A)
    idx = np.array(np.tril_indices(n,-1), dtype=np.int32)
    return has_identical_row_sums_lazy(A, idx)

from time import clock
t = clock()
for i in xrange(1):
##    print has_identical_double_row_sums(A)
    print has_identical_triple_row_sums(A)
print clock()-t

rawDescription: Find two disjoint pairs of pairs that sum to the same vector


code:
# Try to use the C implementation first, falling back to python
try:
    from xml.etree import cElementTree as ElementTree
except ImportError, e:
    from xml.etree import ElementTree

rawDescription: How to consume XML from RESTful web services using Django / Python?

description: Consume XML from RESTful web services using Django / Python


rawDescription: how to delete the "u and ' ' " before the of database table display by python

description: Delete the "u and ' ' " before the of database table display by python


code:
import random
import itertools

def generate_sequence(alphabet, num_repeats):

    # generate the permutations of the list of size 2
    # repeated `num_repeats` times
    perms = [(letter1, letter2) for letter1, letter2 in itertools.product(alphabet, alphabet) for i in xrange(num_repeats)]

    # save the original length of `perm` for later
    perm_len = len(perms)

    # randomly choose a starting point and add it to our result
    curr_s, curr_e = random.choice(perms)
    result = [curr_s, curr_e]

    # remove the starting point from the list... on to the next one
    perms.remove((curr_s, curr_e))

    # while we still have pairs in `perms`...
    while len(perms):

        # get all possible next pairs if the end of the current pair
        # equals the beginning of the next pair
        next_vals = [(s,e) for s,e in perms if s == curr_e]

        # if there aren't anymore, we may have exhausted the pairs that
        # start with `curr_e`, in which case choose a new random pair
        if len(next_vals) != 0:
            next_s, next_e = random.choice(next_vals)
        else:
            next_s, next_e = random.choice(perms)

        # remove the next pair from `perm` and append it to the `result`
        perms.remove((next_s, next_e))
        result.append(next_e)

        # set the current pair to the next pair and continue iterating...
        curr_s, curr_e = next_s, next_e

    return result

alphabet = ('x', 'y', 'z')
num_repeats = 2
print generate_sequence(alphabet, num_repeats)

rawDescription: how to create a list sequence in python containing all possible pairs an equal number of times?

description: Create a list sequence in python containing all possible pairs an equal number of times


code: print round(x)

rawDescription: Round to whole numbers without using conditional statements in Python - Logic


code:
from scrapy.selector import HtmlXPathSelector
from scrapy.spider import BaseSpider
from scrapy.http import Request

DOMAIN = 'example.com'
URL = 'http://%s' % DOMAIN

class MySpider(BaseSpider):
    name = DOMAIN
    allowed_domains = [DOMAIN]
    start_urls = [
        URL
    ]

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        for url in hxs.select('//a/@href').extract():
            if not ( url.startswith('http://') or url.startswith('https://') ):
                url= URL + url
            print url
            yield Request(url, callback=self.parse)

rawDescription: How do I use the Python Scrapy module to list all the URLs from my website?

description: Use the Python Scrapy module to list all the URLs from my website


code:
print "something"
exit('ohoh')

rawDescription: store return value of a Python script in a bash script

description: Store return value of a Python script in a bash script


code:
import subprocess

numberImages = 3
urlin=raw_input("input the url: ")
for i in range(1,numberImages+1):
    print urlin%i
    subprocess.call(["wget",urlin%i])

rawDescription: how to make a variable take a html link without formatting in python?

description: Make a variable take a html link without formatting in python


code:
import pyparsing as pyp

comma = pyp.Literal(',').suppress()
# use parse actions to do type conversion at parse time, so that results fields
# can immediately be used as ints or floats, without additional int() or float()
# calls
floatnum = pyp.Regex(r'([\d\.]+)').setParseAction(lambda t: float(t[0]))
integer = pyp.Word(pyp.nums).setParseAction(lambda t: int(t[0]))

# define the command keyword - I assume there will be other commands too, they
# should follow this general pattern (define the command keyword, then all the
# options, then define the overall command)
aperture_defn_command_keyword = pyp.Literal('AD')

# define a results name for the matched integer - I don't know what this
# option is, wasn't in your original post
d_option = 'D' + integer.setResultsName('D')

# shortcut for defining a results name is to use the expression as a
# callable, and pass the results name as the argument (I find this much
# cleaner and keeps the grammar definition from getting messy with lots
# of calls to setResultsName)
circular_aperture_defn = 'C' + comma + floatnum('diameter') + 'X'

# define the overall command
aperture_defn_command = aperture_defn_command_keyword("command") + d_option + pyp.Optional(circular_aperture_defn)

# use searchString to skip over '%'s and '*'s, gives us a ParseResults object
test = "%ADD22C,0.35X*%"
appData = aperture_defn_command.searchString(test)[0]

# ParseResults can be accessed directly just like a dict
print appData['command']
print appData['D']
print appData['diameter']

# or if you prefer attribute-style access to results names
print appData.command
print appData.D
print appData.diameter

# convert ParseResults to an actual Python dict, removes all unnamed tokens
print appData.asDict()

# dump() prints out the parsed tokens as a list, then all named results
print appData.dump()

rawDescription: In pyparsing, how to assign a "no match" key value?


code:
print "     A   B   C   D   E"
for row_label, row in zip(row_labels, x):
    print '%s [%s]' % (row_label, ' '.join('%03s' % i for i in row))

rawDescription: How do I print an aligned numpy array with (text) row and column labels?

description: Print an aligned numpy array with (text) row and column labels


code:
class DummyManager(object):
    def __enter__(self):
        print 'entering with block!'
        return 'foo'
    def __exit__(self, exc_type, exc_val, exc_tb):
        print 'exiting with block!'
        print 'this is exception info, if an exception was raised:'
        print exc_type, exc_val, exc_tb

with DummyManager() as dummy:
    print dummy

rawDescription: How to consume a File / Cursor line by line from another object that has no access to them

description: Consume a File / Cursor line by line from another object that has no access to them


code:
for d in b:
    print d['id']
    for desc in d['desc']:
        print desc

rawDescription: python list of dictionaries find duplicates based on value

description: Python list of dictionaries find duplicates based on value


code:
headers = [item.text for item in table('th')]
for row in table('tr')[1:]:
    params = [item.text.strip() for item in row('td')]
    print dict(zip(headers, params))

rawDescription: Scaping a table using mechanize & beautiful when some rows contain additional formatting


code:
re1 = re.compile(
  r'''(?:^|\n) # noncapturing, assert start of string or newline
      (D.*?)   # capture D and everything after it
      (?=\n[A-Z]|$) #lookahead, newline cap char or end of string?
  ''', flags)

for i in  re.findall(re1, txt):
    print i

rawDescription: Python Regex matching choice between "\*" (literal Asterisk char) OR "\s" (Space)


code:
guard = object()
result = return_generator()
if next(result, guard) is not guard:
    print 'Yes, the generator did generate something!'

rawDescription: Check if a generator really generate something in one line


code: print list(partitions("4824"))

rawDescription: python all combinations of subsets of a string

description: Python all combinations of subsets of a string


code:
import libxml2

DOC = """<elements>
    <parent name="CategoryA">
        <child value="a1"/>
        <child value="a2"/>
        <child value="a3"/>
    </parent>
    <parent name="CategoryB">
        <child value="b1"/>
        <child value="b2"/>
        <child value="b3"/>
    </parent>
</elements>"""

doc = libxml2.parseDoc(DOC)

def getValues(cat):
    return [attr.content for attr in doc.xpathEval("/elements/parent[@name='%s']/child/@value" % (cat))]

print getValues("CategoryA")

rawDescription: Get list of XML attribute values in Python


rawDescription: turn string to array python

description: Turn string to array python


code:
heroes = soup.find_all('span', {'style': 'white-space:nowrap'})
for hero in heroes:
    print hero.getText()

rawDescription: BeautifulSoup Scraping Returning Duplicate Results


rawDescription: NumPy - Set values in structured array based on other values in structured array


code:
#!/usr/bin/python
import cgi

form = cgi.FieldStorage()
print 'Content-Type: text/html\n'
if 'uploadFile' in form and form['uploadFile'].filename:
    print form['uploadFile'].value
else:
    print 'no file'

rawDescription: having problems getting a python cgi script to open file passed to it from AJAX

description: Having problems getting a python cgi script to open file passed to it from AJAX


code:
import sys, StringIO

class Supress_print(object):

    def __init__(self):
        pass

    def __enter__(self):
        self.stdout = sys.stdout
        sys.stdout = StringIO.StringIO()

    def __exit__(self, *args):
        sys.stdout = self.stdout

def f1():
    print 'foo'

def f2(boo=True):
    with Supress_print():
        f1()
    if boo:
        print 'bar'
    else:
        print 'black sheep'

def f3():
    with Supress_print():
        f2()
    print 'shh!!!'

f2(True)
print
f2(False)
print
f3()

rawDescription: How to silence a function's output to console?

description: Silence a function's output to console


code:
for x, y in dis:
    for k in dct.keys():
        if x == k:
            print x, " ".join(dct[k])

rawDescription: Find closest location from a list of locations in csv file Python


code:
class A(object):
    def __getattr__(self, attr):
        val = 'Attribute set to string'
        setattr(self, attr, val)
        print 'Assigned attribute'
        return val

rawDescription: Python __getattr__ behavior? IN ECLIPSE/PyDev console?


code:
import sys

def estimate_chars():
    "size in bytes"
    s = ""
    for idx in range(100):
        print idx * 10, sys.getsizeof(s), len(s)
        s += '1234567890'

estimate_chars()

rawDescription: Getting number of characters through it's memory size in python


code:
from binascii import unhexlify
import zlib
from cStringIO import StringIO
import skimage.io, skimage.color
from colormath.color_objects import RGBColor
import numpy as np

def mask1(img, treshold=80, color=[255,20,147]):
    res=img.copy()
    for x in range(img.shape[0]):
        for y in range(img.shape[1]):
            RGBimg=RGBColor(img[x,y,0],img[x,y,1],img[x,y,2])
            LABimg=RGBimg.convert_to('lab', debug=False)
            if (LABimg.lab_l > treshold):
                res[x,y,:]=color
            else:
                res[x,y,:]=img[x,y,:]
    return res

def mask2(rgb, threshold=80, color=[255,20,147]):
    res = rgb.copy()
    lab = skimage.color.rgb2lab(rgb)
    mask = lab[:,:,0] > threshold
    res[mask] = np.array(color).reshape(1,-1).repeat(mask.sum(),axis=0)
    return res

sdata = "789C9D953D56C52010856363696D49E90AAC73ECDD4396C25228B210CE711B2CC2CAC622CECC9D0C0321313A27E411123EEEFCC07B7BFF7A9CC45EA9BD507BD6F620F769CAF4FEE3096DB76DDACEAEE9865D4CF79C6DAB34F46D441F7F23F88F6F728E6AD794724EDD5CBB9B790EF53FBF1595D9524C517E93CDEA3A433D984E83440327B318B633BF867A4C12734A5654CE26F24F29AB28704A067685363C665B0582D30ADF0F39A2717F3979C9412A6108A1D731C6992C04BD96252ECB9A2AC4A60F2B07904AA8166C84B51545D172C3C8D02B4CA3D51D841F7584B5CD2E17E2698A5DDE991302AD6240189666558242122D68F1C0F19F99475104D0F7C6216D5A6665AFAED62F8A27730A57E3BC4858669D25716B387BA04E39B41059BCC7E99CEAF4B05F971C75AAB0181AE938111CA9DB9A71C9B5443EA000D4231183A4F8ECEF79E7E5B40E2DEF647BDEA9AB6250EA59F70B6AC90E9FAABFB7D040E43C010107D4F1086A4ADA6D8DA66C8AEDD9C10E3514196A0F060220B59825C843883F5D71A67586809FEDF17FFCD75C4CFC012B43550B"
fh = StringIO(zlib.decompress(unhexlify(sdata)))
rgb = skimage.io.imread(fh)

res1 = mask1(rgb)
res2 = mask2(rgb)

import timeit
t1 = timeit.timeit("mask1(rgb)", setup="from __main__ import rgb, mask1", number=100)
t2 = timeit.timeit("mask2(rgb)", setup="from __main__ import rgb, mask2", number=100)

print t1, t2, t1/t2

rawDescription: python code slow skin detection

description: Python code slow skin detection


code:
def disemvowel(string):

    # Letters to remove & the new, vowel-free string
    vowels_list = 'aeiouAEIOU'
    new_string = ""

    # Go through each character in the string
    for character in string:

        # Skip over vowels, include everything else
        if character not in vowels_list:
            new_string += character

    return new_string

print disemvowel('Nasty Comment: Stack exchange sucks!')

rawDescription: Unusual behaviour when using string concatenation inside for loop


code:
d = datetime.datetime(2013, 2, 1, 10, 15) # day of the month is 1
# since the target day is the 29th and that is 28 days after the first
# subtract 1 before creating the timedelta.
d = d + datetime.timedelta(days=28)
print d
# datetime.datetime(2013, 3, 1, 10, 15)

rawDescription: datetime.datetime - day field out of range

description: Datetime.datetime - day field out of range


rawDescription: How to merge list to become string without adding any character in python?

description: Merge list to become string without adding any character in python


code:
import xml.dom.minidom

d = xml.dom.minidom.parseString('<head>hello</head>')
d.getElementsByTagName('head')[0].setAttribute('joe', '2')
print d.toxml()

rawDescription: Can I edit XML loaded by xml.dom.minidom.parse?

description: Edit XML loaded by xml.dom.minidom.parse


code:
##################################################
# file: EchoServer_client.py
#
# client stubs generated by
# "ZSI.generate.wsdl2python.WriteServiceModule"
#
##################################################

from EchoServer_types import *
import urlparse, types
from ZSI.TCcompound import ComplexType, Struct
from ZSI import client
from ZSI.schema import GED, GTD
import ZSI
from ZSI.generate.pyclass import pyclass_type

# Locator
class EchoServerLocator:
    EchoServer_address = "http://localhost:7000"
    def getEchoServerAddress(self):
        return EchoServerLocator.EchoServer_address
    def getEchoServer(self, url=None, **kw):
        return EchoServerSOAP(
            url or EchoServerLocator.EchoServer_address,
            **kw)

# Methods
class EchoServerSOAP:
    def __init__(self, url, **kw):
        kw.setdefault("readerclass", None)
        kw.setdefault("writerclass", None)
        # no resource properties
        self.binding = client.Binding(url=url, **kw)
        # no ws-addressing

    # op: Echo
    def Echo(self, request, **kw):
        if isinstance(request, EchoRequest) is False:
            raise TypeError, "%s incorrect request type" % \
                (request.__class__)
        # no input wsaction
        self.binding.Send(None, None, request, soapaction="Echo", **kw)
        # no output wsaction
        response = self.binding.Receive(EchoResponse.typecode)
        return response

EchoRequest = GED("urn:ZSI", "Echo").pyclass

EchoResponse = GED("urn:ZSI", "Echo").pyclass

rawDescription: Are there any working examples of Zolera SOAP Infrastructure (ZSI)?


code:
import cfg
from cfg import *   # bind objects referenced by variables in cfg to
                    # like-named variables in this module. These objects
                    # from 'cfg' now have an additional reference here

if __name__ == '__main__':
    print cfg.var, var
    var = 20        # rebind this module's 'var' to some other value.
                    # this does not affect cfg's 'var'
    print cfg.var, var

    print '----'
    print cfg.somelist, somelist
    somelist.append(1)    # update this module's 'somelist'
                          # since we updated the existing object, all see it
    print cfg.somelist, somelist

    print '----'
    var2 = 2
    print var2, cfg.var2, show_var2() # show_var2 is in cfg.py and uses its
                                      # namespace even if its been imported
                                      # elsewhere

rawDescription: Global Variables between different modules


code: print chr(157).decode('CP437')

rawDescription: encrypting a string via binary manipulation

description: Encrypting a string via binary manipulation


code:
sentence = "My screen is smashed"

solution_dict = {}
solution_dict[("screen", ("broken", "smashed"))] = "use a repair kit"

#If value is a tuple, run function on every value and return if there are any matches
#If not, check the word is part of the sentence
def check_match(sentence_words, keyword):
    if isinstance(keyword, tuple):
        return any([check_match(sentence_words, i) for i in keyword])
    return keyword in sentence_words

#Make sure each value in the main tuple has a match
sentence_words = [i.lower() for i in sentence.split()]
for k,v in solution_dict.iteritems():
    if all(check_match(sentence_words, i) for i in k):
        print v

rawDescription: Search multiple keywords in string

description: Search multiple keywords in string


code:
a = MyClass()
a.x = 5
print a._x, a._y
a.y = 8
print a._x, a._y

rawDescription: How can I add properties to a class using a decorator that takes a list of names as argument?

description: Add properties to a class using a decorator that takes a list of names as argument


code: print re.match(pattern, "10\\xbd") # the extra \ 'escapes' the '\' so it is no longer special

rawDescription: The same regular expression doesn't work on my local environment


code:
from collections import Counter
with open("/path/to/file.log", "wb") as f:
  counter = Counter(''.join(line.split()[11:14:2]) for line in f)

# print top 10 (leave argument out to list all)
for url, count in counter.most_common(10):
  print "%d %s" % (count, url)

rawDescription: Log analysis (top URLs accessed) with Python


code:
import urllib
from bs4 import BeautifulSoup

url = "http://www.nba.com/gameline/20160323/"
page = urllib.urlopen(url).read()
soup = BeautifulSoup(page)
indicateGameDone = soup.find("p", {"class": "nbaFnlStatTx"})
print indicateGameDone.text

rawDescription: Beautiful Soup not returning everything in HTML file?


code:
import multiprocessing
import logging
import time

class gener(object):
    def __init__(self, m):
        self.m = m
        self.c = multiprocessing.Value('i', 0)

    def __iter__(self):
        return self

    def next(self):
        print "n"
        time.sleep(3)
        with self.c.get_lock():
            ret = self.m[self.c.value]
            self.c.value += 1
        return ret

def gen(func):
    t = func()
    itst[t] = t

if __name__ == "__main__":
    m = [0,1,2,3]
    iter_state = 0

    tt  = gener(m)
    itst = multiprocessing.Array('i', 3)
    multiprocessing.log_to_stderr(logging.DEBUG)

    tm = time.time()

    jobs = [ multiprocessing.Process(target=gen, args=(tt.next,)) for _ in range(3)]
    for j in jobs:
        j.start()
    for j in jobs:
        j.join()

    for i in itst:
        print i

    tm = time.time() - tm
    print tm

rawDescription: Python multiprocessing doesn't nest subprocess?


rawDescription: Common items between all the keys of a dictionary


code:
class A:
    def __init__(self):
        time.sleep(0.1)

    def a1(self):
        print 'a1'

def a2():
    print 'a2'

def b2():
    a2()
    A().a1()

def x():
    t = time.clock()
    for i in xrange(100):
        b2()
    print 'elapsed: %0.1fs'%(time.clock()-t)

rawDescription: Python: performance issue implemenating nested classes with staticmethod


code:
# Print name
print "".join([b[i][0] for i in suit_sels])
# Print suit symbols
print "".join([b[i][1] for i in suit_sels])

rawDescription: Card generator game printing in a column, not a row


code:
# OPTIONAL for performance testing -------------# ||||||||||||||||||||||||||||||||
from zmq import Stopwatch                       # _MICROSECOND_ timer
#                                               # timer-resolution step ~ 21 nsec
#                                               # Yes, NANOSECOND-s
# OPTIONAL for performance testing -------------# ||||||||||||||||||||||||||||||||
arr        = np.zeros( ( height, width, 4 ), dtype = int )
aStopWatch = zmq.Stopwatch()                    # ||||||||||||||||||||||||||||||||
# /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\# <<< your original code segment
#  aStopWatch.start()                           # |||||||||||||__.start
#  for i in range(     width  ):
#      for j in range( height ):
#          ambient_mod  = ambient[i,j][0] / 255.0
#          arr[j, i, :] = [ color[i,j][0] * ambient_mod, \
#                           color[i,j][1] * ambient_mod, \
#                           color[i,j][2] * ambient_mod, \
#                           shine[i,j][0]                \
#                           ]
#  usec_for = aStopWatch.stop()                 # |||||||||||||__.stop
#  print 'Converting Color Map to image'
#  print '           FOR processing took ', usec_for, ' [usec]'
# /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\# <<< proposed alternative
aStopWatch.start()                              # |||||||||||||__.start
# reduced numpy broadcasting one dimension less # ref. comments below
arr[:,:, 0]  = color[:,:,0] * ambient[:,:,0]    # MUL ambient[0]  * [{R}]
arr[:,:, 1]  = color[:,:,1] * ambient[:,:,0]    # MUL ambient[0]  * [{G}]
arr[:,:, 2]  = color[:,:,2] * ambient[:,:,0]    # MUL ambient[0]  * [{B}]
arr[:,:,:3] /= 255                              # DIV 255 to normalise
arr[:,:, 3]  = shine[:,:,0]                     # STO shine[  0] in [3]
usec_Vector  = aStopWatch.stop()                # |||||||||||||__.stop
print 'Converting Color Map to image'
print '           Vectorised processing took ', usec_Vector, ' [usec]'
return Image.fromarray( arr.astype( np.uint8 ) )

rawDescription: Python - Quick batch modification of PNGs


code:
def place(bin, row, column, block):
    if isSpaceFree(bin, row, column, block):
        for r in range(row, row+block):
            for c in range(column, column+block):
                bin[r][c] = block

x = [
[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],
]

place(x, 0, 0, 4)

print "\n".join(str(row) for row in x)

rawDescription: 2D binary list python

description: 2D binary list python


code:
d = json.load(open('C:\Users\Joshua\Desktop\Database.txt'))
print d['user2123']

rawDescription: How to read a dictionary from a file in Python 2.7?

description: Read a dictionary from a file in Python 2.7


code:
print np.array(list(mystr), dtype=int)
# [1 0 0 1 1 0]

rawDescription: Convert string to numpy array

description: Convert string to numpy array


code:
from itertools import groupby
li=[]
for k, l in groupby(s):
    li.append((k, len(list(l))))

print li

rawDescription: Splitting a string into consecutive counts?


code:
# method decorator -- makes undeclared 'self' argument available to method
injectself = lambda f: lambda self: eval(f.func_code, dict(self=self))

class methodclass:
    def __call__():
        print 'in methodclass::__call__(): self.attr = %r' % self.attr
        return 42

class TestClass:
    def __init__(self, thing):
        self.attr = thing

    method = injectself(methodclass.__call__)

test = TestClass("attribute's value")
ret = test.method()
print 'return value:', ret

# output
# in methodclass::__call__(): self.attr = "attribute's value"
# return value: 42

rawDescription: How to get self into a Python method without explicitly accepting it

description: Get self into a Python method without explicitly accepting it


code:
import datetime
import multiprocessing
from multiprocessing import Queue

def worker(myid, work_queue, done_queue):
    if work_queue.get() == "Start":
        for i in range(1,10):
                if i == 5:
                    # do something
                    print myid, datetime.datetime.now()
        done_queue.put(myid)

def main():
    worker_queues = {}
    for i in xrange(1, 6):
        q = Queue()
        done_q = Queue()
        worker_queues[i] = (q, done_q)
        d = multiprocessing.Process(target = worker, args = (i, q, done_q))
        d.daemon = True
        d.start()

    for i in xrange(1, 6):
        worker_queues[i][0].put("Start")
        if worker_queues[i][1].get():
            # move to next iteration
            pass

if __name__ == "__main__":
    main()

rawDescription: Python - Locking a shared resource (Multiprocessing)


code:
lis = ['Text', 'in', 'the', 'middle!', 'END']
for item in lis:
    print item.center(13, '.')

rawDescription: Python Logic - Centering text using periods


code: print sum(listOfCRs)

rawDescription: Identify and extract number from text files in Python


rawDescription: Add a column to the end of csv using pandas or in general python


code:
def validate(narg, conv, logMessage = None):
    def decorate(func):
        def funcDecorated(*args):
            newArgs = list(args)
            try:
                newArgs[narg] = conv(newArgs[narg])
            except Exception, e:
                # wrong argument! do some logging here and re-raise
                raise Exception("Invalid argument #{}: {}".format(narg, e))
            else:
                return func(*newArgs)

        return funcDecorated
    return decorate

rawDescription: Python input validity and using asserts

description: Python input validity and using asserts


code:
checkio = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41]

def checkzi(array):
    if len(array) != 0:
        sum_array = 0
        for idx, x in enumerate(array):
            print "testing element", idx, " which has value ", x
            if (idx % 2 == 0):
                sum_array += x
                print "sum is now ", sum_array
            else:
                print "odd element - not summing"
        print (sum_array)
        answer = (sum_array) * (array[len(array)-1])
        return (answer)
    else:
        return 0

checkzi(checkio)

rawDescription: Bad value returned by calculation

description: Bad value returned by calculation


code:
def get_item(response, path):
    for item in path:
        response = response[item]
    return response

# Assume we need the string 'qwerty'
spam = {'spam': ['eggs', {'13': 'qwerty'}], 'eggs': 123}
# Then that will be our path:
path = ['spam', 1, '13']

print get_item(spam, path)

rawDescription: How to Create a Dynamic Path for Grabbing Variables from JSON in Python


code:
Arr= [("Forename","Paul"),("Surname","Dinh")]
for Key,Value in Arr:
    print Key,"=",Value

Forename = Paul
Surname = Dinh

rawDescription: How to implement associative array (not dictionary) in Python?

description: Implement associative array (not dictionary) in Python


code:
sample = [
    {u'': u'TERRY.HERREGON@EXAMPLE.COM', u'USD38020': u''},
    {u'': u'REMKO.VON.DER.WURF@ABV.COM', u'GELREM01': u''},
    {u'': u'MARTIN.YGOR@ANTEL.COM'},
    {u'': u'DON.VASSILLI@TEST.COM', u'AUVASSILIDE': u''},
    {u'': u'JAMES.WOJOCICK@SUN.COM', u'JAMES.WOJCIK': u''},
    {u'': u'DANIEL.MUCHANA@JAVA.COM', u'DEHDAMU': u''},
]
for d in sample:
    print {next((k for k in d.iterkeys() if k), u''): d['']}

rawDescription: Python nested dictionaries key,value swap


code:
match = re.finditer('A', 'ABBACDA')
for m in match:
    print m.start(), m.end(), m.group(0)

rawDescription: Finding character position from a RegEx'd string


code:
from itertools import combinations, islice

def cons(nums):
    if len(nums)%2 or len(nums)<2:
        raise ValueError
    if len(nums) == 2:
        yield (nums,)
        return
    for c in islice(combinations(nums, 2), len(nums)-1):
        for sub in cons(tuple(set(nums) - set(c))):
            yield ((c,) + sub)

def constellations(n):
    return cons(range(1, n+1))

for c in constellations(6):
    print c

rawDescription: Enumeration of all possible two-member group constellations


code:
import base64
from datetime import datetime
import httplib
import io
import os
import time

from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

wdir = "workdir"
stream_url = ''
username = ''
password = ''

def main():
    time_count = 2
    looper_stream(time_count)

def looper_stream(time_count):
    h = httplib.HTTP(stream_url)
    h.putrequest('GET', '/videostream.cgi')
    h.putheader('Authorization', 'Basic %s' % base64.encodestring('%s:%s' % (username, password))[:-1])
    h.endheaders()
    errcode, errmsg, headers = h.getreply()
    stream_file = h.getfile()
    start = time.time()
    end = start + time_count
    while time.time() <= end:
        now = datetime.now()
        dte = str(now.day) + "-" + str(now.month) + "-" + str(now.year)
        dte1 = str(now.hour) + "-" + str(now.minute) + "-" + str(now.second) + "." + str(now.microsecond)
        cname = "Cam1-"
        dnow = """Date: %s """ % dte
        dnow1 = """Time: %s""" % dte1
        # your camera may have a different streaming format
        # but I think you can figure it out from the debug style below
        source_name = stream_file.readline()    # '--ipcamera'
        content_type = stream_file.readline()    # 'Content-Type: image/jpeg'
        content_length = stream_file.readline()   # 'Content-Length: 19565'
        print 'confirm/adjust content (source?): ' + source_name
        print 'confirm/adjust content (type?): ' + content_type
        print 'confirm/adjust content (length?): ' + content_length
        # find the beginning of the jpeg data BEFORE pulling the jpeg framesize
        # there must be a more efficient way, but hopefully this is not too bad
        b1 = b2 = b''
        while True:
            b1 = stream_file.read(1)
            while b1 != chr(0xff):
                b1 = stream_file.read(1)
            b2 = stream_file.read(1)
            if b2 == chr(0xd8):
                break
        # pull the jpeg data
        framesize = int(content_length[16:])
        jpeg_stripped = b''.join((b1, b2, stream_file.read(framesize - 2)))
        # throw away the remaining stream data. Sorry I have no idea what it is
        junk_for_now = stream_file.readline()
        # convert directly to an Image instead of saving / reopening
        # thanks to SO: http://stackoverflow.com/a/12020860/377366
        image_as_file = io.BytesIO(jpeg_stripped)
        image_as_pil = Image.open(image_as_file)
        draw = ImageDraw.Draw(image_as_pil)
        draw.text((0, 0), cname, fill="white")
        draw.text((0, 10), dnow, fill="white")
        draw.text((0, 20), dnow1, fill="white")
        img_name = "Cam1-" + dte + dte1 + ".jpg"
        img_path = os.path.join(wdir, img_name)
        image_as_pil.save(img_path)

if __name__ == '__main__':
    main()

rawDescription: How to efficiently read and save video from an IP camera?

description: Efficiently read and save video from an IP camera


code:
def recursive_search(tags, results_queryset):
    if len(tags) > 0:
        result_qs = result_queryset.filter(tags_name=tags[0])
        if result_queryset.exists():
            return filter_recursion(tags[1:],result_queryset)
        else:
            return None
    return result_queryset

tags = ["comedy", "80s", "action", "thriller"]  # This can be variable
result_queryset = Item.objects.filter(brands__in=brands) # Could be Item.objects.all()
print recursive_search(tags, result_queryset)

rawDescription: Django Combine a Variable Number of QuerySets


code:
for i in links:
    if 'posters' in i.get_attribute('src'):
        print i.get_attribute('src')

rawDescription: Beautifulsoup parse not working

description: Beautifulsoup parse not working


code:
import numpy as np

a = np.random.rand(10)
print a

rawDescription: Fast 1D convolution with finite filter and sum of dirac deltas in python


code:
from cProfile import Profile
from pstats import Stats
prof = Profile()
prof.disable()

file = open('cart_output.txt', 'wt')

def cart(n, seq):
    import itertools
    b = 8
    while b < n:
        n = n - 1
        for p in itertools.product(seq, repeat=n):
            file.write(''.join(p))
            file.write('\n')

prof.enable()
cart(10, 'abc')
prof.disable()

prof.dump_stats('cart.stats')
with open('cart_stats.txt', 'wt') as output:
    stats = Stats('cart.stats', stream=output)
    stats.sort_stats('cumulative', 'time')
    stats.print_stats()

file.close()
print 'done'

rawDescription: python itertools product slow is the write speed to a output file a bottleneck

description: Python itertools product slow is the write speed to a output file a bottleneck


code:
from HTMLParser import HTMLParser

# create a subclass and override the handler methods
class MyHTMLParser(HTMLParser):

    srces = []
    datas = []
    onclicks = []

    def handle_starttag(self, tag, attrs):
        for attr in attrs:
            if attr[0] == 'src':
                 self.srces.append(attr[1])
            if  attr[0] == 'onclick':
                  self.onclicks.append(attr[1].split("getit('")[1].split("',")[0])

    def handle_data(self, data):
        if data != " " and not data.startswith('\n'):
           self.datas.append(data)

parser = MyHTMLParser()
parser.feed("""
mango season

cherry season

"""
)

#To fit your output
for i in range(0,len(parser.srces)):
    print "1: " + parser.onclicks[i] + "\n" + "2: " + parser.datas[i] + "\n" + "3: " + parser.srces[i] + "\n"

#  Output
# 1: call.php?category=fruits&fruitid=123456&
# 2: mango season
# 3: http://imagehosting.com/images/fru_123456.png

# 1: call.php?category=fruits&fruitid=123457&
# 2: cherry season
# 3: http://imagehosting.com/images/fru_123457.png

rawDescription:
How to scrape onclick value (
<a id="FR123456" onclick=) and its name and image using python?

description:
Scrape onclick value (
<a id="FR123456" onclick=) and its name and image using python


code:
import sharedmem as shm
import numpy as np
import multiprocessing as mp

def worker(q,arr):
    done = False
    while not done:
        cmd = q.get()
        if cmd == 'done':
            done = True
        elif cmd == 'data':
            ##Fake data. In real life, get data from hardware.
            rnd=np.random.randint(100)
            print('rnd={0}'.format(rnd))
            arr[:]=rnd
        q.task_done()

if __name__=='__main__':
    N=10
    arr=shm.zeros(N,dtype=np.uint8)
    q=mp.JoinableQueue()
    proc = mp.Process(target=worker, args=[q,arr])
    proc.daemon=True
    proc.start()

    for i in range(3):
        q.put('data')
        # Wait for the computation to finish
        q.join()
        print arr.shape
        print(arr)
    q.put('done')
    proc.join()

rawDescription: How do I pass large numpy arrays between python subprocesses without saving to disk?

description: Pass large numpy arrays between python subprocesses without saving to disk


code:
a = ['1','2','3','4']
b = ['2','2','3','6','5']

from difflib import unified_diff

def merge(a,b):
    output = []
    for line in list(unified_diff(a,b))[3:]:
        if '+' in line:
            output.append(line.strip('+'))
        elif not '-' in line:
            output.append(line.strip())
    return output

print merge(a,b)

rawDescription: restoring list from unified_diff output

description: Restoring list from unified_diff output


rawDescription: Get Maximum Value based upon Multiple Dictionary Objects using Python


code: print args.lambda_

rawDescription: Python: How to escape 'lambda'

description: Python: How to escape 'lambda'


code:
import requests

url = 'http://genome-euro.ucsc.edu/cgi-bin/hgTables?hgsid=201790284_dkwVYFu7V6ISmTzFGlXzo23aUhXk'
session = requests.Session()

params = {
    'hgsid': '201790284_dkwVYFu7V6ISmTzFGlXzo23aUhXk',
    'jsh_pageVertPos': '0',
    'clade': 'mammal',
    'org': 'Human',
    'db': 'hg19',
    'hgta_group': 'genes',
    'hgta_track': 'refGene',
    'hgta_table': 'refFlat',
    'hgta_regionType': 'range',
    'position': 'chr9:21802635-21865969',
    'hgta_outputType': 'gff',
    'boolshad.sendToGalaxy': '0',
    'boolshad.sendToGreat': '0',
    'boolshad.sendToGenomeSpace': '0',
    'hgta_outFileName': '',
    'hgta_compressType': 'none',
    'hgta_doTopSubmit': 'get output'
}

response = session.post(url, data=params)
print response.content

rawDescription: How to extract table browser results from ucsc genome browser by scraping

description: Extract table browser results from ucsc genome browser by scraping


code:
def food(input,boolean):
    time = int(input)
    food_type = ""
    if time >= 0 and time < 6 or time >= 22:
        food_type = "no food"
    if time >= 6 and time <= 10:
        food_type = "breakfast"
    if time >= 11 and time <= 15:
        food_type = "lunch"
    if time >= 16 and time < 22:
        food_type = "dinner"
    dessert = ""
    if boolean == True and food_type == "breakfast":
        dessert = "marmalade"
    if boolean == False and food_type == "breakfast":
        dessert = "coffee"
    if boolean == True and food_type == "lunch":
        dessert = "dessert"
    if boolean == True and food_type == "dinner":
        dessert = "dessert"
    return (dessert, food_type)
     # return food_type
print food(7,True)

rawDescription: using multiple parameters in python function

description: Using multiple parameters in python function


code:
from itertools import permutations
A = {'a', 'b', 'c'}
B = {'1', '2'}
C = {'d', 'e', 'f'}
for b in B:
    for cperm in permutations(C):
        res = {''.join([a, b, c]) for a, c in zip(A, cperm)}
        print res

rawDescription: Permutation between three sets in python


code:
import shelve
s = shelve.open("pickles2.dat")
s['1plus1']='2'
s['2plus2']='4'
s.close()

s = shelve.open("pickles2.dat")
print s['1plus1']
s['3plus3']='zero'
s.close()

s = shelve.open("pickles2.dat")
print s

rawDescription: I have a couple of questions on shelves in the module pickle?


code:
try:
   path = 'http://user:password@blah.com/plone/sites/ng/path/to/folder'
   xmlrpclib.ServerProxy(path).getPhysicalPath()
   return True
except xmlrpclib.Fault, e:
   return False

rawDescription: How to check if a folder exists by its complete path in Plone?

description: Check if a folder exists by its complete path in Plone


code:
class Page(object):
    cache = []
    """ Return cached object """
    @classmethod
    def __getCache(cls, title):
        for o in Page.cache:
            if o.__searchTerm == title or o.title == title:
                return o
        return None
    """ Initilize the class and start processing """
    def __new__(cls, title, api=None):
        o = cls.__getCache(title)
        if o:
            return o
        page = super(Page, cls).__new__(cls)
        cls.cache.append(page)
        page.title = title
        page.api = api
        page.__searchTerm = title
        # ...etc
        return page

a = Page('test')
b = Page('test')

print a.title # works
print b.title

assert a is b

rawDescription: Python object cache

description: Python object cache


code:
from example import Foo, more_vals
def print_list(l): print ', '.join(str(v) for v in l)

f = Foo()
f.vals[0] = 10
print f.vals[0]
f.vals[0] = '10'
f.vals[100] = 10
f.vals[:] = xrange(100,103)
print_list(f.vals)
f.strs[:] = ("a", "b", "c", "d", "e")
print_list(f.strs)
f.vals[-1] = 30
print_list(f.vals)
v = f.vals
del v[:-1]
print_list(f.vals)
print_list(v)
x = v[-1:]
f = None
v = None
print_list(x)
more_vals()[:] = xrange(50, 100)
print_list(more_vals())

rawDescription: Wrapping arrays in Boost Python

description: Wrapping arrays in Boost Python


code:
for node in species.xpath("child::node()"):
    print type(node), node

rawDescription: Iterate over both text and elements in lxml etree


rawDescription: oneliner scramble program

description: Oneliner scramble program


code:
from time import sleep
import example

def spam1(x):
  print "spam1: ", x

def spam2(x):
  print "spam2: ", x

c = example.MyClass()
c.connect_slot(spam1)
c.connect_slot(spam2)
c.event(123)
print "Sleeping"
c.event_in(3, 321)
sleep(5)
print "Done sleeping"

rawDescription: Boost.Python and Boost.Signals2: Segmentation faults


code:
import Queue

class Client(threading.Thread):

    def __init__(self, msg_queue, host=SERVER_IP, port=PORT):
        threading.Thread.__init__(self)
        self.msg_queue = msg_queue
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock = socket.create_connection((host, port), 1)
        self.sock.setblocking(0)

    def run(self):
        while 1:
            try:
                ready = select.select([self.sock], [], [], TIMEOUT*1000)
                if ready[0]:
                    buf = self.sock.recv(BUF_SIZE)
                    print buf
                    #TODO:do stuff with buf

                    print 'messages left:'+ str(self.msg_queue.qsize())
                    try:
                        msg = self.msg_queue.get_nowait()
                        self.sock.send(msg)
                    except Queue.Empty:
                        pass
            except KeyboardInterrupt:
                self.sock.close()
                sys.exit(1)
            except Exception, e:
                print '\n[ERR] %s' % e
                self.sock.close()
                sys.exit(1)

def main():
    # create a queue and pass it to the client
    msg_queue = Queue.Queue()
    client = Client(msg_queue)
    client.start()
    msg_queue.put("test1")
    msg_queue.put("test2")
    msg_queue.put("test3")

rawDescription: Sending data from outside socket object in python


code:
lists_output = []

for main_key, values in results.items():
    for subkey, count in values.items():
       lists_output.append([main_key,count,subkey])

print lists_output

rawDescription: how to merge multiple lists in python

description: Merge multiple lists in python


code:
import time
millis = int(round(time.time() * 1000))
print millis

rawDescription: Get current time in milliseconds in Python?


code:
def replace_last(source_string, replace_what, replace_with):
    head, sep, tail = source_string.rpartition(replace_what)
    return head + replace_with + tail

s = "123123"
r = replace_last(s, '2', 'x')
print r

rawDescription: How to replace the some characters from the end of a string?

description: Replace the some characters from the end of a string


code:
import re

line = "2015/04/13.16:26:07 156.0 GB of instance data copied, dev_iosecs 1887, dev_iorate 88.8 MB/s"

if re.search("of instance data copied", line):
    m = re.search("(?P<datasize>\d*[.][\d]) (?P<units>TB|GB|MB|KB) of instance data copied", line)
    print m.group('datasize'), m.group('units')
    if m.group('units') == "GB":
        print "MATCH!!!!!"

rawDescription: Having trouble with re and matching groups


code:
from HTMLParser import HTMLParser

class extract_text(HTMLParser):
    def handle_data(self,data):
        if data.strip():
            print data

parser = extract_text()
parser.feed(doc)

rawDescription: Remove all XML tags, only keep the text between the tags


code:
from __main__ import args

print args

rawDescription: Passing python script arguments to test modules


code:
# Usage : Translate string of userid to its Integer primary key representation

#userid_mapper.py
#!/usr/bin/env python

import sys
for line in sys.stdin:
   line = line.strip()
   try :
       user_string, dat, appcat = line.split("\x7C")
       print "%s\t%s\t%s" % (user_string, dat, appcat)
   except Exception as e :
       user_string, dat, appcat = Unspecified, Unspecified, Unspecified
       print "%s\t%s\t%s" % (user_string, dat, appcat)

rawDescription: Converting a string column values to “primary integer indexes” in a very large pandas dataframe or before feeding it to csv from hive


code:
for k in os.environ.keys():
    print "<b>%20s</b>: %s<\br>" % (k, os.environ[k])

rawDescription: Translate simple Perl script into Python that sends a response to client?


code:
import json
print json.dumps(dict(table_data))

rawDescription: Convert a HTML Table to JSON

description: Convert a HTML Table to JSON


code:
for key in sorted(x, key=x.get, reverse=True)[:10]:
    print key, x[key]

rawDescription: Iterating through a dictionary for X number of times


code:
import numpy as np
import scipy.cluster.vq as vq
import matplotlib.pyplot as plt

target_points = np.array([1.,2.,3.,4.,5.,6.,7.])
points = (np.random.rand(50)*2*np.pi)
values = np.sin(points)
code, dist = vq.vq(target_points, points)
interp = values[code]

plt.plot(points,values,'o')
plt.plot(target_points,interp,'ro')
print interp
plt.show()

rawDescription: why 1D scipy.interpolate.griddata using method=nearest produces nans?

description: Why 1D scipy.interpolate.griddata using method=nearest produces nans


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

rawDescription: tuple to string formatting

description: Tuple to string formatting


code:
import numpy

x_count = 10000
y_count = 20000

x_indices = numpy.arange(x_count)
y_indices = numpy.arange(y_count)

large_table = numpy.arange(y_count * x_count).reshape(y_count, x_count)
print large_table

def get_random_item(sample_size):
    from random import sample
    for i in sample(xrange(y_count * x_count), sample_size):
        y,x = divmod(i, y_count)
        yield (x,y)

for x,y in get_random_item(10):
    print '%12i   x: %5i y: %5i' % (large_table[x][y],  x,y)

rawDescription: memory efficient random number iterator without replacement

description: Memory efficient random number iterator without replacement


code:
#!/usr/bin/env python2.4
import os
import signal
import sys
import tempfile
import time
from subprocess import Popen

class TimeoutExpired(Exception):
    pass

def wait(process, timeout, _sleep_time=.1):
    for _ in xrange(int(timeout * 1. / _sleep_time + .5)):
        time.sleep(_sleep_time)  # NOTE: assume it doesn't wake up earlier
        if process.poll() is not None:
            return process.wait()
    raise TimeoutExpired  # NOTE: timeout precision is not very good

f = tempfile.TemporaryFile()
p = Popen(["/bin/bash", "-c", sys.argv[1]], stdout=f, preexec_fn=os.setsid,
          close_fds=True)
try:
    wait(p, timeout=5)
except TimeoutExpired:
    print >>sys.stderr, "timeout"
    os.killpg(os.getpgid(p.pid), signal.SIGKILL)
    p.wait()
else:
    f.seek(0)
    for line in f:
        print line,
f.close()  # delete it
print >>sys.stderr, "done"

rawDescription: python subprocess poll() is not returning None even if Popen is still running

description: Python subprocess poll() is not returning None even if Popen is still running


code:
user_input = raw_input("Please provide your title and name: ").split()
if len(user_input) > 1:   # at least two entries provided
    title = user_input[0] # the first is the title
    name = user_input[1]  # the second is the name
elif len(user_input) > 0: # one input provided
    title = ""        # Or another default
    name = user_input[0]  # assign to name
print title,name

rawDescription: How to determine how strings will be assigned to specific variables with input() and .split()

description: Determine how strings will be assigned to specific variables with input() and .split()


rawDescription: Python code for finding unique path from tuple list


code:
import re
w = "TEMPLATES = ( ('index.html', 'home'), ('base.html', 'base'))"

# find outer parens
outer = re.compile("\((.+)\)")
m = outer.search(w)
inner_str = m.group(1)

# find inner pairs
innerre = re.compile("\('([^']+)', '([^']+)'\)")

results = innerre.findall(inner_str)
for x,y in results:
  print "%s <-> %s" % (x,y)

rawDescription: Python regex: matching a parenthesis within parenthesis


code:
import re

with open('inputfilename', 'rU') as f:
    for line in f:
        mat = re.match("(\d+):(\d+):(\d+)([AP]M).*Long\s+([^EW]+[EW]).*Lat\s+([^NS]+[NS])", line)
        if mat is not None:
            print mat.groups()

rawDescription: Finding text in a string matching patterns


code:
intervals = [[100,200],[150,250],[300,400],[250,500]]

overlapping = [ [x,y] for x in intervals for y in intervals if x is not y and x[1]>y[0] and x[0]<y[0] ]
for x in overlapping:
    print '{0} overlaps with {1}'.format(x[0],x[1])

# results:
# [100, 200] overlaps with [150, 250]
# [250, 500] overlaps with [300, 400]

rawDescription: Checking for overlap between time spans

description: Checking for overlap between time spans


code:
from __future__ import print_function

import ast
import operator
import sys

OPERATORS = {
    '<': operator.lt,
    '<=': operator.le,
    '>': operator.gt,
    '>=': operator.ge,
    '==': operator.eq,
    '!=': operator.ne,
    # 'in' is using a lambda because of the opposite operator order
    # 'in': (lambda item, container: operator.contains(container, item),
    'in': (lambda item, container: item in container),
    'contains': operator.contains,
    }

def process_conditionals(conditional_strings, variables):
    for conditional_string in conditional_strings:
        # Everything after first and op is part of second
        first, op, second = conditional_string.split(None, 2)

        resolved_operands = []
        for raw_operand in (first, second):
            try:
                resolved_operand = ast.literal_eval(raw_operand)
            except ValueError:  # If the operand is not a valid literal
                ve = sys.exc_info()
                try:
                    # Check if the operand is a known value
                    resolved_operand = variables[raw_operand]
                except KeyError:  # If the operand is not a known value
                    # Re-raise the ValueError
                    raise ve[1], None, ve[2]

            resolved_operands.append(resolved_operand)

        yield (op, tuple(resolved_operands))

def main(lines, *conditional_strings):
    for line in lines:
        key, category, details = line

        variables = {
            'key': key,
            'category': category,
            'elapsed': details['elapsed'],
            'jobname': details['jobname'],
            }

        conditionals = process_conditionals(conditional_strings, variables)

        try:
            # You could check each conditional separately to determine
            # which ones have errors.
            condition = all(OPERATORS[op](*operands)
                            for op, operands in conditionals)
        except TypeError:
            print("A literal in one of your conditionals is the wrong type. "
                  "If you can't see it, try running each one separately.",
                  file=sys.stderr)
            break
        except ValueError:
            print("An operand in one of your conditionals is neither a known "
                  "variable nor a valid literal. If you can't see it, try "
                  "running each one separately.", file=sys.stderr)
            break
        else:
            if condition:
                print(line)

if __name__ == '__main__':
    lines = [
        [1, 'runtime', {'elapsed': 12.3, 'jobname': 'high38853'}],
        [2, 'runtime', {'elapsed': 45.6, 'jobname': 'high38854'}],
        [3, 'runtime', {'elapsed': 78.9, 'jobname': 'high38855'}],
        [4, 'runtime', {'elapsed': 14.7, 'jobname': 'high38856'}],
        [5, 'runtime', {'elapsed': 25.8, 'jobname': 'high38857'}],
        [6, 'runtime', {'elapsed': 36.9, 'jobname': 'high38858'}],
        [7, 'runtime', {'elapsed': 75.3, 'jobname': 'high38859'}],
        ]

    conditional_strings = sys.argv[1:]

    main(lines, *conditional_strings)

rawDescription: Interpreting a Python string as a conditional statement?


code: print [hex(ord(i)) for i in section[0:2]]

rawDescription: unprintable string to number in python

description: Unprintable string to number in python


code: print "\n".join(tarfiles)

rawDescription: Assigning elements from a for loop to another list


code:
import hashlib

hash = hashlib.md5()
hash.update('555555555')
print hash.hexdigest()

rawDescription: converting character or integer to md5 hash using python script

description: Converting character or integer to md5 hash using python script


code:
class Node(object):
    def __init__(self):
        self.children = []
        self.result = None
    def add(self, node):
        self.children.append(node)
    def check(self):
        self.result = True
        print "Node: result:%s" % self.result
        return self.result
    def accept(self, visitor):
        visitor.visit(self)

class Node_A(Node):
    def __init__(self):
        super(Node_A, self).__init__()
    def check(self):
        self.result = True
        print "Node_A: result:%s" % self.result
        return self.result

class Node_A_A(Node_A):
    def __init__(self):
        super(Node_A_A, self).__init__()
    def check(self):
        self.result = True
        print "Node_A_A: result:%s" % self.result
        return self.result

class Node_A_B(Node_A):
    def __init__(self):
        super(Node_A_B, self).__init__()
    def check(self):
        self.result = True
        print "Node_A_B: result:%s" % self.result
        return self.result

class Node_A_A_A(Node_A_A):
    def __init__(self):
        super(Node_A_A_A, self).__init__()
    def check(self):
        self.result = True
        print "Node_A_A_A: result:%s" % self.result
        return self.result
class Node_A_A_B(Node_A_A):
    def __init__(self):
        super(Node_A_A_B, self).__init__()
    def check(self):
        self.result = False
        print "Node_A_A_B: result:%s" % self.result
        return self.result

class Node_A_B_A(Node_A_B):
    def __init__(self):
        super(Node_A_B_A, self).__init__()
    def check(self):
        self.result = True
        print "Node_A_B_A: result:%s" % self.result
        return self.result

class NodeRunner:
    def visit(self, node):
        if len(node.children) > 0:
            for child in node.children:
                child.accept(self)
        node.check()

class NodeChecker:
    def visit(self, node):
        results = []
        if len(node.children) > 0:
            for child in node.children:
                child.accept(self)
                results.append(child.result)
            node.result = all(results)

if __name__ == "__main__":
    node = Node()
    node_a = Node_A()
    node_a_a = Node_A_A()
    node_a_b = Node_A_B()

    node_a_a_a = Node_A_A_A()
    node_a_a_b = Node_A_A_B()

    node_a_b_a = Node_A_B_A()

    node.add(node_a)

    node_a.add(node_a_a)
    node_a_a.add(node_a_a_a)
    node_a_a.add(node_a_a_b)

    node_a.add(node_a_b)
    node_a_b.add(node_a_b_a)

    print("-------------------")
    nVisitor = NodeRunner()
    nVisitor.visit(node)
    print("-------------------")
    nVisitor = NodeChecker()
    nVisitor.visit(node)
    print("-------------------")
    print "node_a_a_a: result: %s" % node_a_a_a.result
    print "node_a_a_b: result: %s" % node_a_a_b.result
    print "node_a_a: result: %s" % node_a_a.result
    print "node_a_b_a: result: %s" % node_a_b_a.result
    print "node_a_b: result: %s" % node_a_b.result
    print "node_a: result: %s" % node_a.result
    print "node: result: %s" % node.result

rawDescription: Visitor pattern (from bottom to top)

description: Visitor pattern (from bottom to top)


code:
import email
m = server.fetch([456], ['RFC822'])
msg = email.message_from_string(m[456]['RFC822'])
print msg['from']
# Facebook <register+mr4k25sa@facebookmail.com>
from email.utils import parseaddr
print parseaddr(msg['from'])
# ('Facebook', 'register+mr4k25sa@facebookmail.com')
print msg.get_payload()
# content of email...

rawDescription: Using IMAPClient how do you view the Senders address?


code:
import sympy.physics.units as u
subs = {}
for k, v in u.__dict__.items():
    if (isinstance(v, Expr) and v.has(u.Unit)) or isinstance(v, Integer):
        subs[Symbol(k)] = v
print sympify('mega*m').subs(subs)

rawDescription: How to parse and simplify a string like '3cm/µs² + 4e-4 sqmiles/km/h**2' treating physical units correctly?

description: Parse and simplify a string like '3cm/µs² + 4e-4 sqmiles/km/h**2' treating physical units correctly


code:
from itertools import izip
import xml.etree.ElementTree as ET

def main():
    prefix = "{http://jbpm.org/4.4/jpdl}"

    xml = ET.parse("testWF2.xml")
    root = xml.getroot()
    i = root.findall('*')

    # convert list to dictionary indexed by Element.name
    temp = []
    for it in i:
        name = it.get("name")
        if name:
            temp.append(name)
        else:
            tag = it.tag
            temp.append(tag.replace(prefix, '')) # if no name exists use tag (ex. start and end)
    b = dict(izip(temp, i)) # create the dictionary with key = name

    nodes = []
    # add root to the list
    start_pair = (None, b["start"])  # # # # # using pairs
    nodes.append(start_pair)

    while(nodes):
        parent, n = nodes.pop()  # # # # # using pairs
        transitions = n.findall(prefix+"transition")
        children = []
        # get all of n's children
        for t in transitions:
            child = b[t.get("to")]
            children.append(child)
            nodes.append((n, child))  # add parent/child pair

        # only output the parent (thus outputing k times)
        try:
            print parent.get("name", "start")
        except AttributeError:
            pass  # ignore the start position
        # also output the node if it has no children (terminal node)
        if len(children) < 1:
            print n.get("name", "start")

    # end while loop

main()

rawDescription: Iterative preorder k-ary tree traversal

description: Iterative preorder k-ary tree traversal


code:
from BeautifulSoup import BeautifulSoup

xml = '<enclosure name="link1" url="http://google.co.uk/vid.mp4" type="video/mp4" />'
parsed_xml= BeautifulSoup(xml)
print parsed_xml.enclosure['url']

rawDescription: Parse XML in python

description: Parse XML in python


code:
from collections import Counter
s=r'So I have an array of words, stored as key value pairs. Now I am trying to count the frequency of words in an array of strings, tokens. I have tried the following but this doesnt find the index of x as it is only a string. I do not have the corresponding value, if any, of x in tokens array. Is there any way to directly access it rather than adding one more loop to find it first?'
tokens=s.split()
c=Counter(tokens)
print c
c['of']

rawDescription: Updating a dict which is stored in an array


code:
from pyparsing import *
latex_h  = QuotedString("$")('latex')
reg_text = Word(alphas)('text')
grammar  = Group(OneOrMore( latex_h | reg_text ))('line')

def parse_reg_text(s, loc, toks):
    if toks.text == 'dog':
        return "atomic " + toks.text
    else:
        return "ninja " + toks.text

reg_text.setParseAction(parse_reg_text)

sol = grammar.parseString('''dog $x^2$ cat $y^3$''')
print sol.dump()

rawDescription: Why is pyparsing removing a named result?


code:
start_date = datetime(year=2015, month=1, day=1)
end_date = datetime.today()
for single_date in daterange(start_date, end_date):
    value = single_date.strftime("%Y-%m-%d")
    print value

rawDescription: Writing loop to get the data during some specific periods in Python


code:
from functools import wraps
def my_decorator(f):
    @wraps(f)
    def wrapper(*args, **kwds):
        print 'Calling decorated function'
        return f(*args, **kwds)
    return wrapper
@my_decorator
def example():
    """Docstring"""
    print 'Called example function'
example()
example.__name__
example.__doc__

rawDescription: python wraps.who can give a example

description: Python wraps.who can give a example


code:
@rename('new name')
def f():
    pass
print f.__name__

rawDescription: Possible to change function name in definition?


code: print doc.body.text_content()

rawDescription: Problems faced while scraping webpages in Python


code:
def do_stuff(data):
    print 'alice does stuff with %s' % data

rawDescription: Spawning a thread in python

description: Spawning a thread in python


code:
# Never do this -- insecure!
symbol = 'RHAT'
c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)

# Do this instead
t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)
print c.fetchone()

rawDescription: How can you use python variables in an sql search

description: Use python variables in an sql search


code:
import sys
sys.stdin.encoding
import locale
locale.getpreferredencoding()
'kožušèek'
u'kožušèek'
print u'kožušèek'

rawDescription: Python's handling of shell strings

description: Python's handling of shell strings


code:
import numpy as np
from random import randint

class Thing:

    def __init__(self, words, images, audios, videos):
        self.words  = words
        self.images = images
        self.audios = audios
        self.videos = videos

    def compare(self, other):
        score = 0
        # Assuming the attribute lists have the same length for both objects
        # and that they are sorted in the same manner:
        for i in range(len(self.words)):
            if self.words[i] == other.words[i]:
                score += 1
        for i in range(len(self.images)):
            if self.images[i] == other.images[i]:
                score += 1
        # And so one for audio and video. You have to make sure you know
        # what method to use for determining when an image/audio/video are
        # equal.
        return score

N = 1000
things = []
words  = np.random.randint(5, size=(N,5))
images = np.random.randint(5, size=(N,5))
audios = np.random.randint(5, size=(N,5))
videos = np.random.randint(5, size=(N,5))
# For testing purposes I assign each attribute to a list (array) containing
# five random integers. I don't know how you actually intend to do it.
for i in xrange(N):
    things.append(Thing(words[i], images[i], audios[i], videos[i]))

# I will assume that object number 999 (i=999) is the Ox:
ox = 999
scores = np.zeros(N - 1)
for i in xrange(N - 1):
    scores[i] = (things[ox].compare(things[i]))

best = np.argmax(scores)
print "The most similar thing is thing number %d." % best
print
print "Ox attributes:"
print things[ox].words
print things[ox].images
print things[ox].audios
print things[ox].videos
print
print "Best match attributes:"
print things[ox].words
print things[ox].images
print things[ox].audios
print things[ox].videos

rawDescription: Algorithm for matching objects

description: Algorithm for matching objects


code:
print xover(ts_array, 0.400), '\n'

0

print xover(ts_array, 0.460), '\n'

2

print xover(ts_array, 0.465), '\n'

3

print xover(ts_array, 1.000), '\n'

-1

rawDescription: First element of series to cross threshold in numpy, with handling of series that never cross


code:
import re

#this is just a small example of your configuration file string
data = """application.baseUrl='http://192.168.1.100:9000'"""

##compile a re pattern that creates a "group" of the data we we want to replace
##that is comprised of only the numbers 0-9, a period and a colon.
pattern = re.compile("application.baseUrl='http://([0-9\.\:]*)'")
##see if we have a match
match = re.match(pattern, data)
if match:
   ##if we have a match then grab the ip and port number from the re "group"
   ip_and_port = match.group(1)
   print ip_and_port
   ##now replace the old data with the new data
   new_data = data.replace(ip_and_port, "111.222.333.444:65535")
   print new_data

rawDescription: Python search and replace in configuration file


code:
L = list(item for item in range(10))
A = list(item for item in range(10,20))
data_frame = pd.DataFrame(L,columns=['L'])
data_frame['A'] = pd.Series(A, index=data_frame.index)
print data_frame

rawDescription: Append a list of arrays as column to pandas Data Frame with same column indices


code:
EXTENDABLE_KEYS = ('templates', )

def extend_dict(extend_me, extend_by):
    if isinstance(extend_me, dict):
        for k, v in extend_by.iteritems():
            if k in extend_me:
                extend_dict(extend_me[k], v)
            else:
                extend_me[k] = v
    else:
        if isinstance(extend_me, list):
            extend_list(extend_me, extend_by)
        else:
            extend_me += extend_by

def extend_list(extend_me, extend_by):
    missing = []
    for item1 in extend_me:
        if not isinstance(item1, dict):
            continue

        for item2 in extend_by:
            if not isinstance(item2, dict) or item2 in missing:
                continue

            # Check if any key is an extendable key
            if filter(lambda x: x in EXTENDABLE_KEYS, item1.keys()):
                extend_dict(item1, item2)
            else:
                missing += [item2, ]
    extend_me += missing

extend_dict(file1, file2)

print yaml.dump(file1, default_flow_style=False)

rawDescription: python merge complex YAML

description: Python merge complex YAML


code:
import os

def list_dir(dir_name, traversed = [], results = []):
    dirs = os.listdir(dir_name)
    if dirs:
        for f in dirs:
            new_dir = dir_name + f + '/'
            if os.path.isdir(new_dir) and new_dir not in traversed:
                traversed.append(new_dir)
                list_dir(new_dir, traversed, results)
            else:
                results.append([new_dir[:-1], os.stat(new_dir[:-1])])
    return results

dir_name = '../c_the_hard_way/Valgrind/' # sample dir
for file_name, stat in list_dir(dir_name):
    print file_name, stat.st_size # sample with file size

rawDescription: Python mapping all files inside a folder


code:
class dump_into_namespace:
    def __init__(self, env, *vs):
        vars(self).update(dict([(x, env[x]) for v in vs for x in env if v is env[x]]))

def f():
    x = 10
    y = 20
    return dump_into_namespace(locals(), x, y)

o = f()
print o.x, o.y
globals().update(vars(o))
print x

rawDescription: Easily dumping variables from/to namespaces/dictionaries in Python


code:
import numpy as np

matrix = np.array(
    [
        [0, 1 ,0 ,0],
        [0, 0, 1, 0],
        [0, 1, 1, 0],
        [1, 0, 0, 1]
    ])

print np.linalg.det(matrix)

for i in range(matrix.shape[0]):
    for j in range(matrix.shape[0]):
        if i != j:
            inner_product = np.inner(
                matrix[:,i],
                matrix[:,j]
            )
            norm_i = np.linalg.norm(matrix[:,i])
            norm_j = np.linalg.norm(matrix[:,j])

            print 'I: ', matrix[:,i]
            print 'J: ', matrix[:,j]
            print 'Prod: ', inner_product
            print 'Norm i: ', norm_i
            print 'Norm j: ', norm_j
            if np.abs(inner_product - norm_j * norm_i) < 1E-5:
                print 'Dependent'
            else:
                print 'Independent'

rawDescription: How to find linearly independent rows from a matrix

description: Find linearly independent rows from a matrix


code:
list_of_pressed = [ i for i in range(len(pressed)) if pressed[i] ]

if K_a in list_of_pressed:
    print "key 'a' was pressed"

rawDescription: How to process pygame.get_pressed() to receive input

description: Process pygame.get_pressed() to receive input


code:
s = u'that\u2019s \U0001f63b'
print s

rawDescription: Python - Reading Emoji Unicode Characters


code:
import win32gui
def enumWindowsProc(hwnd, lParam):
    print win32gui.GetWindowText(hwnd)
win32gui.EnumWindows(enumWindowsProc, 0)

rawDescription: Get the title of a window of another program using the process name


code:
def flatten(l):
    return [item for sublist in l for item in sublist]

def generate_strings(rule):
    if not rule:
        return [""]
    begin,end=rule[0],rule[1:]
    if begin=='[':
        i=end.find(']')
        if i==-1:
            raise Exception("Unmatched '['")
        alt=flatten([generate_strings(a) for a in [end[0:i],""]])
        end=end[i+1:]
        return [a+e for e in generate_strings(end) for a in alt]
    if begin=='(':
        i=end.find(')')
        if i==-1:
            raise Exception("Unmatched '('")
        alt=flatten([generate_strings(a) for a in end[0:i].split('|')])
        end=end[i+1:]
        return [a+e for e in generate_strings(end) for a in alt]
    if begin in [']',')','|']:
        raise Exception("Unexpected " + begin)
    return [begin + e for e in generate_strings(end)]

print generate_strings(rule)

rawDescription: How can I generate all possible strings given a grammar rule?

description: Generate all possible strings given a grammar rule


code:
import wx
import sys
import os
import SocketServer
import socket
import threading
class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(320, 350))

        getfile = os.path.abspath(sys.argv[1])
        print getfile
        fopen = open (getfile, 'r')
        fread = fopen.read()

        panel = wx.Panel(self, -1)
        self.static = wx.StaticText(panel, -1, fread, (45, 25), style=wx.ALIGN_CENTRE)
        self.Centre()
class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        data = self.request.recv(1024)
        cur_thread = threading.currentThread()

        data = data.split()
        gfile = os.path.abspath(data[-1])
        fopen = open(gfile, 'r')
        fread = fopen.read()
        self.server.app.static.SetLabel(fread)
        #Note to the self.server.app
        response = 'string length: %d' % len(data)

        print 'responding to',data,'with',response
        self.request.send(response)
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    stopped = False
    allow_reuse_address = True

    def serve_forever(self):
        while not self.stopped:
            self.handle_request()

    def force_stop(self):
        self.server_close()
        self.stopped = True

def client(ip, port, message):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))
    sock.send(message)
    response = sock.recv(1024)
    print "Received: %s" % response
    sock.close()

def start_server(host, port):

    server = ThreadedTCPServer((host, port), ThreadedTCPRequestHandler)
    ip, port = server.server_address

    # Start a thread with the server -- that thread will then start one
    # more thread for each request
    server_thread = threading.Thread(target=server.serve_forever)
    # Exit the server thread when the main thread terminates
    server_thread.setDaemon(True)
    server_thread.start()

    return server
class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, 'test')
        frame.Show(True)
        server.app = frame
        #Note the server.app
        self.SetTopWindow(frame)
        return True

def main():
    app = MyApp(0)
    app.MainLoop()

if __name__ == '__main__':
    HOST, PORT = socket.gethostname(), 61955

    server = None
    try:
        client(HOST, PORT, ' '.join(sys.argv))
        sys.exit()
    except socket.error:
        server = start_server(HOST, PORT)
        main()

rawDescription: Single Instance Application - Passing commands


code:
CURSOR_UP_ONE = '\x1b[1A'
ERASE_LINE = '\x1b[2K'

print "these"
print "are"
print "four"
print "lines"
time.sleep(2)
# go back two lines and overwrite with two new lines
sys.stdout.write(CURSOR_UP_ONE)
sys.stdout.write(CURSOR_UP_ONE)
print "five"
print "lines"
print "now"

rawDescription: how can avoid the output to look like a log(Python)

description: How can avoid the output to look like a log(Python)


code: print df['diff_modulo'].sum()

rawDescription: Sum of precipitation per Timeframe in pandas DataFrame


code:
import urllib2, urllib, json

def get_json(url):
    f = urllib2.urlopen(url)
    resp = f.read()
    f.close()
    return json.loads(resp)

def post(url, data):
    headers = {'Content-Type': 'application/json'}
    request = urllib2.Request(url, data, headers)
    f = urllib2.urlopen(request)
    response = f.read()
    f.close()
    return response

geturl = 'http://localhost:24573/Service1.svc/json/GetMessage'
sendurl = 'http://localhost:24573/Service1.svc/json/SendMessage'

msg = get_json(geturl)
print 'Got', msg
print 'Sending...'
retval = post(sendurl, json.dumps(msg))
print 'POST RESPONSE', retval

rawDescription: Passing inherited object to a WCF service using JSON


code:
from twisted.protocols import amp
from twisted.internet import reactor
from twisted.internet.protocol import Factory
import time

class AMPDBAccessProtocolFactory(Factory):
    def getDBConnection(self):
        if 'dbConnection' in dir(self):
            return self.dbConnection
        else:
            self.dbConnection = SQLLiteTestConnection(self.dbURL)
            return self.dbConnection

class SQLLiteTestConnection(object):
    """
    Provides abstraction for database access and some business functions.
    """
    def __init__(self,dbURL):
        self.dbPool =  adbapi.ConnectionPool("sqlite3" , dbURL,  check_same_thread=False)

    def insertBTData4(self,data):
        query = "INSERT INTO btdata4(co2_data, patient_Id, sensor_Id) VALUES (%s,%s,%s)"
        tm=time.time()
        print "insert time:",tm
        chx=data
        PID=2
        device_ID=5
        dF = self.dbPool.runQuery(query,(chx, PID, device_ID))
        dF.addCallback(self.onQuerySuccess,insert_data=data)
        return dF
    def onQuerySuccess(self,insert_data,*r):
        """
        Here you can inspect query results or add any other valuable information to be parsed at client.
        For the test sake we will just return True to a customer if query was a success.
        original data available at kw argument insert_data
        """
        return True

class Insert(amp.Command):
    arguments = [('data', amp.Integer())]
    response = [('insert_result', amp.Integer())]

class MyAMPProtocol(amp.AMP):

    @Insert.responder
    def dbInsert(self, data):
        db = self.factory.getDBConnection()
        dF = db.insertBTData4(data)
        dF.addErrback(self.onInsertError,data)
        return dF

    def onInsertError(self, error, data):
        """
        Here you could do some additional error checking or inspect data
        which was handed for insert here. For now we will just throw the same exception again
        so that the client gets notified
        """
        raise error

if __name__=='__main__':
    pf = AMPDBAccessProtocolFactory()
    pf.protocol = MyAMPProtocol
    pf.dbURL='biomed1.db'
    reactor.listenTCP(1234, pf)
    reactor.run()

rawDescription: Using Twisted AMP with Database insertion


code:
string = 'The quick brown fox jusmps over the lazy dog'
# Define your variables
result = ''
for i in string:
        if i == 'o':
                i = '0'
        result += i
print result

rawDescription: Python: replacing characters in string without replace() function


code:
from numpy import vstack,array
import numpy as np
from numpy.random import rand
from scipy.cluster.vq import kmeans,vq
# data generation
data = vstack((rand(150,2) + array([.5,.5]),rand(150,2)))
# computing K-Means with K = 2 (2 clusters)
centroids,_ = kmeans(data,2)
# assign each sample to a cluster
idx,_ = vq(data,centroids)

#Print number of elements per cluster
print np.bincount(idx)

rawDescription: Determining the size of cluster after Kmeans in Python


code:
text = "Hey, this is paragraph 1 \n and this is paragraph 2 \n and this will be paragraph 3"
html = ''
for line in text.split('\n'):
   html += '<p>' + line + '</p>'

print html

rawDescription: Transform textarea input to paragraphed HTML


code:
#!/usr/bin/env python2

def check(s, i):
  mid = s[i]
  j = 1
  try:
    while s[i-j] == s[i+j]:
      j += 1
  except:
    pass
  return s[i-j+1:i+j]

def do_all(a):
  pals = []
  mlen = 0
  for i in range(len(a)/2):
    #print "check for", i
    left = check(a, len(a)/2 + i)
    mlen = max(mlen, len(left))
    pals.append(left)

    right = check(a, len(a)/2 - i)
    mlen = max(mlen, len(right))
    pals.append(right)

    if mlen > max(2, i*2-1):
      return left if len(left) > len(right) else right

string = "palindnilddafa"

print do_all(string)

rawDescription: Python: Clever ways at string manipulation


code:
class A(object):
    def foo(self):
        print 'hello A'

class B(object):
    def foo(self):
        print 'hello B'

a = A()
a.foo()
a.__class__
a.__class__ = B
a.foo()

rawDescription: Universally create Derived class from Base in python


code:
def nest_me(x, y):
    """
    Take two arguments and return a one element dict with first
    argument as a value and second as a key
    """
    return {y: x}

a = ['item1', 'item2', 'item3','item4']
rev_a = reversed(a) # ['item4', 'item3', 'item2','item1']
print reduce(
    nest_me, # Function applied until the list is reduced to one element list
    rev_a # Iterable to be reduced
)
# {'item1': {'item2': {'item3': 'item4'}}}

rawDescription: List to nested dictionary in python

description: List to nested dictionary in python


code:
# create sample data
from datetime import datetime, timedelta
d = datetime.now()
data = [d + timedelta(minutes=i) for i in xrange(100)]

# prepare and group the data
from itertools import groupby

def get_key(d):
    # group by 30 minutes
    k = d + timedelta(minutes=-(d.minute % 30))
    return datetime(k.year, k.month, k.day, k.hour, k.minute, 0)

g = groupby(sorted(data), key=get_key)

# print data
for key, items in g:
    print key
    for item in items:
        print '-', item

rawDescription: Python: Grouping into timeslots (minutes) for days of data


code:
def test1():
   print "In function."

a = test1()
print a

print test1()
test1()

rawDescription: Why python is printing None in the output?


code:
from timeit import timeit

f = lambda x: x + 2
g = lambda x: x ** 2

def fg(x):
    yield f(x)
    yield g(x)

print timeit(stmt='list(chain.from_iterable((f(x), g(x)) for x in range(3)))',
             setup='gc.enable(); from itertools import chain; f = lambda x: x + 2; g = lambda x: x ** 2')

print timeit(stmt='list(chain.from_iterable(fg(x) for x in range(3)))',
             setup='gc.enable(); from itertools import chain; from __main__ import fg; f = lambda x: x + 2; g = lambda x: x ** 2')

print timeit(stmt='[func(x) for x in range(3) for func in (f, g)]',
             setup='gc.enable(); f = lambda x: x + 2; g = lambda x: x ** 2')

print timeit(stmt='list(chain.from_iterable((f(x), g(x)) for x in xrange(10**6)))',
             setup='gc.enable(); from itertools import chain; f = lambda x: x + 2; g = lambda x: x ** 2',
             number=20)

print timeit(stmt='list(chain.from_iterable(fg(x) for x in xrange(10**6)))',
             setup='gc.enable(); from itertools import chain; from __main__ import fg; f = lambda x: x + 2; g = lambda x: x ** 2',
             number=20)

print timeit(stmt='[func(x) for x in xrange(10**6) for func in (f, g)]',
             setup='gc.enable(); f = lambda x: x + 2; g = lambda x: x ** 2',
             number=20)

rawDescription: Python (List Comprehension): Returning two (or more) items for each item


code:
#!/usr/bin/env python

words_list = ['hello', 'lo', 'he']
words_list2 = ['narf', 'poit', 'troz']
words_list3 = ['narf', 'narfpoit', 'troznarf', 'zort']

def check_words(words_list):
    print "#####"

    for outer_num, outer_word in enumerate(words_list):
        print "Outer word #{0}: {1}".format(outer_num, outer_word)

        for inner_num, inner_word in enumerate(words_list):
            print "Inner word #{0}: {1}".format(inner_num, inner_word)

            if outer_num != inner_num:
                if outer_word.endswith(inner_word):
                    print "True"
                    return True
                else:
                    print "False"

    return False

result = check_words(words_list)
print "result: {0}".format(result)

result = check_words(words_list2)
print "result: {0}".format(result)

result = check_words(words_list3)
print "result: {0}".format(result)

rawDescription: Stuck on iterating through a list and returning True when a string ends with another string from the list


rawDescription: Extracting two numbers in each loop iteration