code:
class MyPath(str):
    def __div__(self, other):
        assert isinstance(other, str)
        return os.path.join(self, other)
    def __rdiv__(self, other):
        assert isinstance(other, str)
        return os.path.join(other, self)

# Using __div__
print MyPath("/home/bob") / MyPath("bin")
print MyPath("/home/bob") / "bin"

# Using __rdiv__
print "/home/bob" / MyPath("bin")

rawDescription: Is there a function corresponding to "\" in python?


code:
def fibsum():
    a, b = 0, 1
    while True:
        a, b = b, a + b
        if b > 4000000:
            break
        yield b

result = []
for val in fibsum():
    if val < 4000000 and val % 2 == 0:
            result.append(val)

print sum(result)

rawDescription: Python: yield vs print in a loop

description: Python: yield vs print in a loop


code:
data = {"rich": 15, "tom": 19, "bob": 7}
print list(enumerate(sorted(data, key = lambda x: int(data[x]), reverse=True),1))

rawDescription: Getting the index of a players rank by points


code:
def recursive_sum(l1, l2, idx = 0):
    if idx < min(len(l1), len(l2)):
        return [l1[idx] + l2[idx]] + recursive_sum(l1, l2, idx + 1)
    else:
        return []

print recursive_sum([1, 2, 3], [4, 5, 6])
# [5, 7, 9]

rawDescription: sum two lists element-by-element in python recursively

description: Sum two lists element-by-element in python recursively


code:
from ConfigParser import ConfigParser

class MyConfigParser(ConfigParser):
    def get(self, section, option):
      return ConfigParser.get(self, section, option).replace('\n', '\r\n')

config = MyConfigParser()
config.read('test_config.ini')

orig_str = """line1\r\nline2"""
config_str = config.get('General', 'orig_str')
print orig_str == config_str    # Prints True

rawDescription: How python ConfigParser module translate the string "\r\n" to CRLF?


code:
print soup.find_all('span', class_='jewelCount')
print soup.find_all('a', class_='_awj')

rawDescription: Parse FacebookPage Using BeautifullSoup

description: Parse FacebookPage Using BeautifullSoup


code:
def inc(k):
    globals()[k] += 1

a = 7
inc('a')
print a

rawDescription: Python procedure for changing an object

description: Python procedure for changing an object


code:
from itertools import izip_longest

my_list = [[1, 2, 3], "abcd", (4, 5, 6)]
print list(izip_longest(*my_list))

rawDescription: izip_longest on a list of lists

description: Izip_longest on a list of lists


code:
t1=timeit.Timer("foo1()","from __main__ import foo1")
t2=timeit.Timer("foo2()","from __main__ import foo2")
t3=timeit.Timer("foo3()","from __main__ import foo3")
t4=timeit.Timer("foo4()","from __main__ import foo4")
print "%.2f usec/pass" % (1000000 * t1.timeit(number=10000)/10000)
print "%.2f usec/pass" % (1000000 * t2.timeit(number=10000)/10000)
print "%.2f usec/pass" % (1000000 * t3.timeit(number=10000)/10000)
print "%.2f usec/pass" % (1000000 * t4.timeit(number=10000)/10000)

rawDescription: How fast are nested python generators?

description: How fast are nested python generators


code: print '%s:' % 'test'

rawDescription: Python Base64 encoding Vs Java Base64 encoding


code:
x = lambda d:d*d
import types
print type(x) is types.LambdaType
# True
print isinstance(x, types.LambdaType)
# True

rawDescription: How to check that variable is a lambda function

description: Check that variable is a lambda function


code:
def print_info():
    print myListWidget.currentRow()
    print myListWidget.currentItem().text()

rawDescription: How to get a current Item's info from QtGui.QListWidget?

description: Get a current Item's info from QtGui.QListWidget


code:
for item in dir(ax):
    print item
#or in case i guess there should be a setter for sth. i search:
for item in dir(ax):
    if item.startswith("set_"):
        print item
#or if i search sth. about labels:
for item in dir(ax):
    if "label" in item:
        print item

rawDescription: How to learn Matplotlib without searching Google or Stackoverflow?

description: Learn Matplotlib without searching Google or Stackoverflow


code:
import os
import sys
import timeit

setup_str = \
'''
from __main__ import setup, handler

setup()
'''
def setup():
    global grid
    grid = [[9 for col in range(25)] for row in range(25)]

    global start
    start = [12, 12]
    grid[start[0]][start[1]] = 0
    global numRips
    numRips = 8

def handler():
    global grid
    global start
    global numRips
    allExpanded = [start]
    expanded = [start]
    num = 1
    for r in range(numRips):
        toExpand = []
        for n in expanded:
            toExpand = toExpand + (getUnvisitedNeighbors(n, allExpanded))
        expanded = []
        for u in toExpand:
            grid[u[0]][u[1]] = num
            expanded.append(u)
            allExpanded.append(u)
        num += 1

def getUnvisitedNeighbors(loc, visitedCells):
    global grid
    x, y = loc[0], loc[1]

    neighbors = [[x - 1, y], [x + 1, y], [x, y - 1], [x, y + 1], \
                 [x - 1, y - 1], [x - 1, y + 1], [x + 1, y - 1], [x + 1, y + 1]]

    f = lambda p: p[0] >=0 and p[0] < len(grid) and \
                    p[1] >= 0 and p[1] < len(grid[0]) and \
                    not p in visitedCells

    unvisitedNeighbors = filter(f, neighbors)

    return unvisitedNeighbors

print timeit.repeat(stmt="handler()", setup=setup_str, repeat=3, number=1)
for i in range(len(grid)):
    print grid[i]

rawDescription: Inefficient Expansion Algorithm

description: Inefficient Expansion Algorithm


code:
p = subprocess.Popen("pdb2mdb.exe modsrcdll", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = p.communicate()
print out
print err

rawDescription: Windows subprocess with arguments

description: Windows subprocess with arguments


rawDescription: Resampling with start and end date columns


code:
import random

my_list = []

for i in range(1, 100):
    my_list.append(i)
    if i > 50:
        my_list.append(i)

print my_list
print 'Random (sort of) choice: %s' % random.choice(my_list)

rawDescription: generating random numbers in python with percentage function on what selected value in python

description: Generating random numbers in python with percentage function on what selected value in python


code:
for k in ['The maximum in line %d is in column %d' %(linenum, line.split().index(max(line.split()))) for linenum, line in enumerate(open('mytsv.tsv'))]:
    print k

rawDescription: Python: Identify column that has greatest value


code:
import re
import urllib2

from bs4 import BeautifulSoup

url = "http://thedataweb.rm.census.gov/ftp/cps_ftp.html"
soup = BeautifulSoup(urllib2.urlopen(url))

links = soup.find_all(href=re.compile(r'pub/cps/basic/\d+\-/\w+\.dat\.gz$'))

for link in links:
    print link.text, link['href']

rawDescription: Python - BeautifulSoup Webscrape

description: Python - BeautifulSoup Webscrape


code:
l = [{"coefficient": -1.0, "compartment": "c", "molecule": "A", "evidence": []},
     {"coefficient": -1.0, "compartment": "c", "molecule": "B", "evidence": []},
     {"coefficient": -1.0, "compartment": "c", "molecule": "C", "evidence": []},
     {"coefficient": 1.0, "compartment": "c", "molecule": "D", "evidence": []},
     {"coefficient": 1.0, "compartment": "c", "molecule": "E", "evidence": []},
     {"coefficient": 1.0, "compartment": "c", "molecule": "F", "evidence": []}]

def format_mol_list(molecules):
    # Create lists that will hold the molecules of each type, in order
    reactants = []
    products = []

    # Sort the items into reactants and products
    for item in molecules:
       if item["coefficient"] > 0:
          products.append(item["molecule"])
       else:
          reactants.append(item["molecule"])

    return " + ".join(reactants) + " --> " + " + ".join(products)

print format_mol_list(l)

rawDescription: Text manipulation in Python

description: Text manipulation in Python


code:
if my_var != "a" and my_var != "b":
    print  "You didn't type \"a\" or \"b\".  Try again."
    print " "
    Dat_Function()

rawDescription: Why does my python function return None?

description: Why does my python function return None


code:
from bluetooth import *

print "performing inquiry..."

nearby_devices = discover_devices(lookup_names = True)

print "found %d devices" % len(nearby_devices)

for name, addr in nearby_devices:
     print " %s - %s" % (addr, name)

rawDescription: How to find visible bluetooth devices in Python?

description: Find visible bluetooth devices in Python


code:
from timeit import default_timer
start = default_timer()
# Martijn reads another post somewhere

print default_timer() - start

rawDescription: How to create a timer on python

description: Create a timer on python


code:
trial = test()
trial.a = 4
trial.b = 5
print trial.prod()

rawDescription: How to create a simple function within a class in Python?

description: Create a simple function within a class in Python


code:
def join(*paths):
    import os
    return os.path.join(*[part for path in paths for part in path.split(os.sep)])

path1 = '/Volumes/disk1/'
path2 = '/dir2/dir3/'

print join(path1, path2)

rawDescription: Is it expected behavior with os.path.join()


code:
class A(object):
    def __eq__(self, other):
        print "A __eq__ called: %r == %r ?" % (self, other)
        return self.value == other
class B(object):
    def __eq__(self, other):
        print "B __eq__ called: %r == %r ?" % (self, other)
        return self.value == other

a = A()
a.value = 3
b = B()
b.value = 4
a == b

rawDescription: How is __eq__ handled in Python and in what order?


code:
for dealt_cards in combinations(deck, 6):
    for hand1 in combinations(dealt_cards, 3):
        hand2 = tuple(card for card in dealt_cards if card not in hand1)
        print hand1, hand2

rawDescription: generate two sets of combination with one list in python

description: Generate two sets of combination with one list in python


code:
import Image
import numpy as np

im = Image.open('temp.png')
data = np.array(im)
flattened = data.flatten()

print data.shape
print flattened.shape

rawDescription: Flatten OpenCV/Numpy Array

description: Flatten OpenCV/Numpy Array


rawDescription: Get US ASCII Character Set - Python

description: Get US ASCII Character Set - Python


code:
import os
import re

# configuration information
root = 'C:\Users'
keywords = [ 'one', 'two']
extensions = [ 'jpg', 'txt' ]
use_wildcard = True # Enables you to catch the keyword anywhere in the filename
# end of configuration

keyword_pattern = ''
first = True
for k in keywords:
    if use_wildcard:
        k = '.*' + k + '.*'
    if first:
        keyword_pattern += '(' + k + ')'
        first = False
    else:
        keyword_pattern += '|(' + k + ')'

extension_pattern = ''
first = True
for ext in extensions:
    if first:
        extension_pattern += '(' + ext + ')'
        first = False
    else:
        extension_pattern += '|(' + ext + ')'

pattern_regex = r"({0})\.({1})".format(keyword_pattern, extension_pattern)
print "Searching for: " + pattern_regex
pattern = re.compile(pattern_regex)

for path, subdirs, files in os.walk(root):
    for name in files:
        if re.match(pattern, name):
            print os.path.join(path, name)

rawDescription: Python file searching trouble

description: Python file searching trouble


code:
from Bio import Phylo
# assuming you want to read from a string rather than a file
from StringIO import StringIO

tree = Phylo.read(StringIO(s), "newick")

for c in tree.get_nonterminals():
    if c.confidence != None:
        c.confidence = c.confidence / 100

print tree.format("newick")

rawDescription: Perform simple math on regular expression output? (Python)


code:
import re

results = []
for element in dir(re):
    if element.startswith('find'):
        results.append(element)

print sorted(results)

rawDescription: learnpython.org modules exercise

description: Learnpython.org modules exercise


code:
for letter in word:
    if letter == 'e': break
else:
    print word

rawDescription: has_no_e() exercise from Think Python

description: Has_no_e() exercise from Think Python


code:
import urlparse
str = "payload={'key':'value'}"
print urlparse.parse_qs(str)

rawDescription: Parsing response from Github

description: Parsing response from Github


code:
import csv
import time
import datetime

import requests
import pytz

from yahoo_finance import Share

hist_url = 'http://real-chart.finance.yahoo.com/table.csv?s=XLV&a=4&b=23&c=2016&g=d&ignore.csv'
realtime_url = 'http://download.finance.yahoo.com/d/quotes.csv?s=XLV&f=p&e=.csv'

eastern = pytz.timezone('US/Eastern')

while True:
    stock = Share('XLV')
    a = stock.get_historical('2016-05-23','2016-05-23')[0]['Close']
    b = stock.get_prev_close()
    r = requests.get(hist_url)
    reader = csv.DictReader(r.iter_lines())
    c = float(next(reader)['Close'])
    d = float(requests.get(realtime_url).text)
    print datetime.datetime.now(eastern).time(), a, b, c, d
    time.sleep(120)

rawDescription: Incorrect close data from yahoo_finance in python


code:
for x in l:
    print l

for a,b in zip(l,k):
    print a+b

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

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


code:
import re

log_file = open('test.txt', 'r')
data = log_file.read()
log_file.close()
block = re.compile(
    ur"(PAGE\\\(leave\\\) 'Data1'\n)"
    "(.*?)"
    "(PAGE\\\(enter\\\) 'Data1')",
    re.IGNORECASE | re.DOTALL | re.MULTILINE
)
data_in_home_block = [result[1] for result in re.findall(block, data)]
for data_block in data_in_home_block:
    print "Found data_block: %s" % (data_block,)

rawDescription: Finding data in-between two strings in python


code:
from lxml import html
import requests

page = requests.get('http://twitter.com/aberdeencc')
tree = html.fromstring(page.text)

followers = tree.xpath('//a[@data-nav="followers"]/@title')
print 'Followers: ', followers

rawDescription: Grabbing a specific value from a webpage using LXML


code:
match = re.match(r"noi(\w+)k|fnoi(\w+)v\w+|noi(\w+)v\w+",j)
if match:
    results = next(g for g in match.groups() if g)
    print "Results :", results

rawDescription: Regular expression match a or b pattern

description: Regular expression match a or b pattern


code:
for i, j in zip(bounding_time, v):
        print i[0].ljust(6) + j[0]

rawDescription: Python list in list formatting the output


code:
DATA = [
    { 'name' : 'A', 'value' : '10.0' },
    { 'name' : 'B', 'value' : '2.0' },
]

def get_attr(name):
    def inner_func(o):
        try:
            rv = float(o[name])
        except ValueError:
            rv = o[name]
        return rv
    return inner_func

for attrname in ('name', 'value'):
    DATA.sort(key=get_attr(attrname))
    print "%r-sorted: %s" % (attrname, DATA)

rawDescription: Sorting a Python list by key... while checking for string OR float?


code:
a, seen, result = [1, 2, 3, 2, 1, 5, 6, 5, 5, 5], set(), []
for idx, item in enumerate(a):
    if item not in seen:
        seen.add(item)          # First time seeing the element
    else:
        result.append(idx)      # Already seen, add the index to the result
print result
# [3, 4, 7, 8, 9]

rawDescription: List indexes of duplicate values in a list with Python


code:
b = [{u'TOT_PTS_Misc': u'Utley, Alex', u'Total_Points': 96.0},
     {u'TOT_PTS_Misc': u'Russo, Brandon', u'Total_Points': 96.0},
     {u'TOT_PTS_Misc': u'Chappell, Justin', u'Total_Points': 96.0},
     {u'TOT_PTS_Misc': u'Foster, Toney', u'Total_Points': 80.0},
     {u'TOT_PTS_Misc': u'Lawson, Roman', u'Total_Points': 80.0},
     {u'TOT_PTS_Misc': u'Lempke, Sam', u'Total_Points': 80.0},
     {u'TOT_PTS_Misc': u'Gnezda, Alex', u'Total_Points': 78.0},
     {u'TOT_PTS_Misc': u'Kirks, Damien', u'Total_Points': 78.0},
     {u'TOT_PTS_Misc': u'Worden, Tom', u'Total_Points': 78.0},
     {u'TOT_PTS_Misc': u'Korecz, Mike', u'Total_Points': 78.0},
     {u'TOT_PTS_Misc': u'Swartz, Brian', u'Total_Points': 66.0},
     {u'TOT_PTS_Misc': u'Burgess, Randy', u'Total_Points': 66.0},
     {u'TOT_PTS_Misc': u'Smugala, Ryan', u'Total_Points': 66.0},
     {u'TOT_PTS_Misc': u'Harmon, Gary', u'Total_Points': 66.0},
     {u'TOT_PTS_Misc': u'Blasinsky, Scott', u'Total_Points': 60.0},
     {u'TOT_PTS_Misc': u'Carter III, Laymon', u'Total_Points': 60.0},
     {u'TOT_PTS_Misc': u'Coleman, Johnathan', u'Total_Points': 60.0},
     {u'TOT_PTS_Misc': u'Venditti, Nick', u'Total_Points': 60.0},
     {u'TOT_PTS_Misc': u'Blackwell, Devon', u'Total_Points': 60.0},
     {u'TOT_PTS_Misc': u'Kovach, Alex', u'Total_Points': 60.0},
     {u'TOT_PTS_Misc': u'Bolden, Antonio', u'Total_Points': 60.0},
     {u'TOT_PTS_Misc': u'Smith, Ryan', u'Total_Points': 60.0}]

a = multikeysort(b, ['-Total_Points', 'TOT_PTS_Misc'])
for item in a:
    print item

rawDescription: Python sorting list of dictionaries by multiple keys


code:
import meminfo, gc, commands

page_size = int(commands.getoutput("getconf PAGESIZE"))

def stat(message):
    s = meminfo.proc_stat()
    print "My memory usage %s: RSS: %dkb, VSIZE: %dkb" % (
        message, s['rss']*page_size/1024, s['vsize']/1024)
mylist = []

stat("before allocating a big list")
for i in range(0,3000000):
    mylist.append('something here to take memory')

stat("after allocating big list")
### uncomment one of these:
mylist = []
# del mylist[:]
stat("after dropping a big list")
gc.collect()
stat("after gc.collect()")
gc.collect()
stat("after second gc.collect()")
gc.collect()
stat("after third gc.collect()")

rawDescription: python memory del list[:] vs list = []

description: Python memory del list[:] vs list = []


code:
def compress(signal):
    prev_t, prev_val = None, None
    for t, val in zip(*signal):
        if val != prev_val:
            if prev_t is not None:
                yield prev_t, prev_val
            yield t, val
            prev_t, prev_val = None, val
        else:
            prev_t, prev_val = t, val
    if prev_t is not None:
        yield prev_t, prev_val

signal = [
    [0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1], #time values
    [1,1,1,2,3,4,4,4,4,2,1,1] #function values
    ]
print zip(*compress(signal))

rawDescription: Signal Compressing

description: Signal Compressing


code:
print 10, '->', getSignedNumber(10, 4), type(getSignedNumber(10, 4)),getSignedNumber(10, 4)*500000, bin(getSignedNumber(10, 4))
print 15, '->', getSignedNumber(15, 4), type(getSignedNumber(15, 4)),getSignedNumber(15, 4)*500000, bin(getSignedNumber(15, 4))
print 9, '->', getSignedNumber(9, 4), type(getSignedNumber(9, 4)),getSignedNumber(9, 4)*500000, bin(getSignedNumber(9, 4))
print 8, '->', getSignedNumber(8, 4), type(getSignedNumber(8, 4)),getSignedNumber(8, 4)*500000, bin(getSignedNumber(8, 4))
print 7, '->', getSignedNumber(7, 4), type(getSignedNumber(7, 4)),getSignedNumber(7, 4)*500000, bin(getSignedNumber(7, 4))

rawDescription: How to create or interpret custom data type (4bit signed)

description: Create or interpret custom data type (4bit signed)


code:
import urllib2
f = open("url_file.txt","a+")
urlhere = "http://www.google.com"
print "for url: " + urlhere + ":"

try:
    fileHandle = urllib2.urlopen(urlhere)
    data = fileHandle.read()
    fileHandle.close()
    f.write(urlhere + "\n")
    f.close()
    print "It exists"

except urllib2.URLError, e:
    print 'PAGE 404: It Doesnt Exist', e

rawDescription: Writing text to txt file in python on new lines?


rawDescription: python: match and group sets of similar strings in a loop

description: Python: match and group sets of similar strings in a loop


code:
from threading import Thread

def runA():
    while True:
        print 'A\n'

def runB():
    while True:
        print 'B\n'

if __name__ == "__main__":
    t1 = Thread(target = runA)
    t2 = Thread(target = runB)
    t1.setDaemon(True)
    t2.setDaemon(True)
    t1.start()
    t2.start()
    while True:
        pass

rawDescription: Running infinite loops using threads in python


code:
def _print():
    print "no short circuit"
True or _print()
False or _print()

rawDescription: Why is 'True or False' faster than 'False or True'?


code:
import unittest
import mock
import calc

SEVEN = 7

class MockCalc:
    def __init__(self):
        print self.__class__
    def add(self,a,b):
        return SEVEN

class TestIt(unittest.TestCase):

    def setUp(self):
        self.mock_calc = mock.patch('calc.Calc',create=True, new=MockCalc)
        self.mock_calc.start()

    def tearDown(self):
        self.mock_calc.stop()

    def test_patch_class(self):
        '''Mocking the Calc and replace it with MockCalc.'''
        print " \npatch class "
        # client should be created with 'MockCalc'
        client = calc.Client()
        # result should be 7
        result = client.add(4,8)
        print "1)" + str(result)
        self.assertEqual(result,SEVEN)

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

rawDescription: mock.patch does not work properly

description: Mock.patch does not work properly


code:
for i in range(1, 6):
    print 'sum({}) => {}'.format(i, sum(i))

rawDescription: I need help on a summing pattern

description: I need help on a summing pattern


code:
_items = []
def append_and_return_static_list(item):
    _items.append(item)
    return _items

append_and_return_static_list(0)
append_and_return_static_list(1)
print append_and_return_static_list(2) #=> [0,1,2]

rawDescription: static effect on python

description: Static effect on python


code:
# determine bounding box
mean_variability = variability.mean()
non_empty_columns = np.where(variability.min(axis=0)<mean_variability)[0]
non_empty_rows = np.where(variability.min(axis=1)<mean_variability)[0]
boundingBox = (min(non_empty_rows), max(non_empty_rows), min(non_empty_columns), max(non_empty_columns))

# plot and print boundingBox
bb = boundingBox
plt.plot([bb[2], bb[3], bb[3], bb[2], bb[2]],
         [bb[0], bb[0],bb[1], bb[1], bb[0]],
         "r-")
plt.xlim(0,variability.shape[1])
plt.ylim(variability.shape[0],0)

print boundingBox
plt.show()

rawDescription: How to detect object on images?

description: Detect object on images


code:
def solve(lis, num):
    for i, (x, y) in enumerate(zip(lis, lis[1:]), 1):
        if y - x >= num:
            yield i

print list(solve([0,1,-1,3,7,4,5,3,2,4,8], 2))
#[3, 4, 9, 10]

rawDescription: How to print out the indexes in a list with repetitive elements

description: Print out the indexes in a list with repetitive elements


code:
with open("mybigfile.txt") as file:
    lines = file.readlines()

for idx, line in enumerate(lines):
    if line.startswith("<seg>"):
        line_to_detect = lines[idx-3]
        #use idx-2 if you want the _second_ line before this one,
        #ex `id =>3</prop>`
        print "This line was detected:"
        print line_to_detect

rawDescription: how to detect the third line before certain line

description: Detect the third line before certain line


code:
l = [(1,2),(2,3),(3,4),(4,5)]  # replace with whatever your full list is
import itertools
for perm in itertools.permutations(l,len(l) / 2):
    pair_list = []
    for pair in perm:
        if pair[0] in itertools.chain(*(i for i in pair_list)) \
        or pair[1] in itertools.chain(*(i for i in pair_list)):
            break
        pair_list.append(pair)
    if len(pair_list) == len(l) / 2:
        unused = []
        for n in itertools.chain(*(i for i in l)):
            if n not in itertools.chain(*(i for i in pair_list)) and n not in unused:
                unused.append(n)
        print pair_list,'unused:',unused

rawDescription: Generate adjacent pair permutations where each side of the pair can only be used once


code:
import urllib
import multiprocessing
import time

def download_images():
    image = urllib.URLopener()
    file_ = 1
    name = 1
    for i in range(1,1000):
        try:
            image.retrieve("http://mangawriter.com/pics/pic"+str(file_)+".jpeg","pic"+str(name)+".jpeg")
            print "save file %s" %file_
            name += 1
            file_ += 1
        except IOError:
            file_ += 1

rawDescription: How to stop a Python program after some arbitrary timeout

description: Stop a Python program after some arbitrary timeout


rawDescription: Embed list into python file

description: Embed list into python file


code:
class A(object):
    def __init__(self, i, **kwargs):
        super(A, self).__init__(**kwargs)
        self.i = i
    def run(self, value):
        return self.i * value

class B(A):
    def __init__(self, j, **kwargs):
        super(B, self).__init__(**kwargs)
        self.j = j
    def run(self, value):
        return super(B, self).run(value) + self.j

class Logger(object):
    def __init__(self, name, **kwargs):
        super(Logger,self).__init__(**kwargs)
        self.name = name
    def run_logged(self, value):
        print "Running", self.name, "with info", self.info()
        return self.run(value)

class BLogged(B, Logger):
    def __init__(self, **kwargs):
        super(BLogged, self).__init__(name="B", **kwargs)
    def info(self):
        return 42

b = BLogged(i=3, j=4)

rawDescription: Why is super() broken in Python-2.x?

description: Why is super() broken in Python-2.x


code:
class Interval(object):
     def __init__(self, s=0, e=0):
         self.start = s
         self.end = e

class Solution(object):
    def canAttendMeetings(self, intervals):
        """
        :type intervals: List[Interval]
        :rtype: bool
        """
        intervals.sort()

        for i in range(1, len(intervals)):
            if intervals[i].start < intervals[i-1].end:
                return False

        return True

B = Solution()
print B.canAttendMeetings([Interval(20,30),Interval(25,30)])

rawDescription: Something about python class

description: Something about python class


code: print str.split.__doc__

rawDescription: python inspect module positional_only param

description: Python inspect module positional_only param


code:
for item in mylist:
    print item

for key,item in enumerate(mylist):
    print key,":",item

for i in range(0,50):
    print i

rawDescription: i don't know why iter(not __iter__) function use in this place,what is the mean of iter in this code

description: I don't know why iter(not __iter__) function use in this place,what is the mean of iter in this code


code:
with open("input.bin","rb") as f:
    f.seek(250) #offset
    print struct.unpack("<l",f.read(4)) #grabs one little endian 32 bit long

rawDescription: Start at offset from beginning of file and unpack 4 byte word


code:
from numpy import array, linspace

area = 2.0

stresses = array([ linspace(0., 10., inc)  for inc in range(1, 4)])
print 'stresses =\n', stresses

forces = area * stresses
print
print 'forces =\n', forces

rawDescription: Python (NumPy) Arrays with Alternative Lengths which can not be predefined


code:
list1 = ['a', 'b']
list2 = ['cat', 'dog', 'bird']

print [x for x in list2 if not any(y for y in list1 if y in x)]

rawDescription: How to remove items from a list if they match any items in another list in Python

description: Remove items from a list if they match any items in another list in Python


code:
import csv

username = input()

with open('Users.csv', 'rt') as f:
     reader = csv.reader(f, delimiter=',') # good point by @paco
     for row in reader:
          for field in row:
              if field == username:
                  print "is in file"

rawDescription: Check whether string is in CSV

description: Check whether string is in CSV


code:
r=[]
for k in sorted(dc):
    print dc[k]
    if '3' in dc[k]:
        r.append("'3' number {} with key: {}".format(len(r)+1,k))

rawDescription: Dictionary Iteration Python

description: Dictionary Iteration Python


code: print 'Your name is {}!'.format(name)

rawDescription: Python - Removing space in a string

description: Python - Removing space in a string


code:
from decimal import Decimal, getcontext
getcontext().prec=100
print sum(1/Decimal(16)**k *
          (Decimal(4)/(8*k+1) -
           Decimal(2)/(8*k+4) -
           Decimal(1)/(8*k+5) -
           Decimal(1)/(8*k+6)) for k in range(100))

rawDescription: Python pi calculation?

description: Python pi calculation


code:
import lxml.etree

class InstanceNamePrinter(object):
    def start(self, tag, attrib):
        if tag == 'Instance':
            self.name = attrib['name']
        elif tag == 'UserValue':
            if attrib['title'] == 'THIRD' and attrib['value'] != '':
                print self.name
    def close(self):
        pass

with open('example.xml') as xml:
    parser = lxml.etree.XMLParser(target=InstanceNamePrinter())
    lxml.etree.parse(xml, parser)

rawDescription: Can findall or finditer be used in this python ElementTree example?


code:
conx = sqlite3.connect( 'nameofdatabase.db' )
curs = conx.cursor()
curs.execute( 'SELECT COMPONENT_ID, LEVEL, COMPONENT_NAME, PARENT ' +
              'FROM DOMAIN_HIERARCHY' )
rows = curs.fetchall()
cmap = {}
hrcy = None
for row in rows:
    entry = (row[2], {})
    cmap[row[0]] = entry
    if row[1] == 1:
        hrcy = {entry[0]: entry[1]}

# raise if hrcy is None

for row in rows:
    item = cmap[row[0]]
    parent = cmap.get(row[3], None)
    if parent is not None:
        parent[1][row[2]] = item[1]

print hrcy

rawDescription: Insert tree kind of data taken from a database into a python dictionary


code:
for k, v in dictionary.token2id.items():
    print k, v

rawDescription: Access key value pairs in gensim dictionary


code:
import numpy
a = [1, 1, 1 ,1, 1]
ar = numpy.array(a)
print ar + 2

rawDescription: Sum one number to every element in a list (or array) in Python


code:
def seo(url):
    result = requests.get(url)
    soup = BeautifulSoup(result.text)
    lowercased = result.text.lower()
    found=False
    keywords = ['creative']
    print keywords[0] in lowercased
    for keyword in keywords:
        if keyword.lower() in lowercased:
            found =True
    links = soup.find_all('a')[1:]
    for link in links:
        if not found:
            seo(link['href'])
        else:
            print(keyword)
            return

rawDescription: How to get my code to stop printing a keyword in my web crawler

description: Get my code to stop printing a keyword in my web crawler


code:
from StringIO import StringIO
from pandas import *
import numpy as np

data = """column1  column2  column3  column4  column5
1         none     2       'gona'    5.3
2         34       2       'gina'    5.5
3         none     2       'gana'    5.1
4         43       2       'gena'    5.0
5         none     2       'guna'    5.7"""

data = StringIO(data)
print read_csv(data, delim_whitespace=True).drop('column3',axis =1)

rawDescription: how to read from an array without a particular column in python

description: Read from an array without a particular column in python


code:
print re.sub("Mary B\. Miller", "Jane M. Goldstein", str)
## notice I have escaped the . with \.
## in regex . means any non white space characters.

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

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


code:
from PyQt4 import QtCore, QtGui
from qt_gui import Ui_Form
import sys

class ExampleApp(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(ExampleApp, self).__init__(parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)

        # Set up your signals at this point:
        self.ui.pushButton.clicked.connect(self.printit)

    def printit(self):
        print "qt print"

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    ex = ExampleApp()
    ex.show()
    sys.exit(app.exec_())

rawDescription: Qt vs. Wx graphical interface for python


code:
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from mixer import fakers as f
from mixer.backend.flask import mixer

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'

db = SQLAlchemy(app)
mixer.init_app(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(120), unique=True)

    def __repr__(self):
        return '<User %r>' % self.username

if __name__ == '__main__':
    with app.app_context():
        db.create_all()
        user = mixer.blend(User, username=f.get_username(), email=f.get_email())
        print user.id, user.username, user.email

        # Prints: 1 lover_boots robinson1963@google.vn

rawDescription: Receiving null values from python mixer

description: Receiving null values from python mixer


code:
#!/usr/bin/env python

reservations = [
    # [DHCP SERVER, IP ADDRESS, MAC ADDRESS, HOSTNAME, DESCRIPTION]
    ['server1','172.16.0.120','31872fcefa33','wks120.domain.net','Description of client'],
    ['server1','172.16.0.125','4791ca3d7279','wks125.domain.net','Description of client'],
    ['server1','172.16.0.132','6035a71c930c','wks132.domain.net','Description of client'],
]

def addr_to_int(address):
    """Convert an IP address to a 32-bit int"""
    a, b, c, d = map(int, address.split('.'))
    return a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d

def int_to_addr(value):
    """Convert a 32-bit int into a tuple of its IPv4 byte values"""
    return value >> 24, value >> 16 & 255, value >> 8 & 255, value & 255

def reservationlist(reservations, serverpattern, addresspattern, hostpattern,
        start, end):

    reservationdict = dict((addr_to_int(item[1]), item)
            for item in reservations)
    startint = addr_to_int(start)
    endint = addr_to_int(end)
    for i in range(startint, endint + 1):
        try:
            item = reservationdict[i]
        except KeyError:
            addressbytes = int_to_addr(i)
            item = [
                serverpattern.format(*addressbytes),
                addresspattern.format(*addressbytes),
                '[no mac]',
                hostpattern.format(*addressbytes),
                'Unregistered']
        yield item

for entry in reservationlist(
    reservations,
    'server{3}',
    '172.16.{2}.{3}',
    'wks{3}.domain.net',
    '172.16.0.120',
    '172.16.1.132'):
    print entry

rawDescription: How do I fill in unused ip addresses in a python sublist?

description: Fill in unused ip addresses in a python sublist


code:
from itertools import product
somedict = {'foo': [1, 2], 'bar': ['a', 'b', 'c'], 'baz': [42, 81]}
for combo in product(*somedict.values()):
    print combo

for combo in product(*somedict.values()):
    print dict(zip(somedict.keys(), combo))

rawDescription: Iterating through all combinations across many iterables


rawDescription: Avoiding Excel's Scientific Notation Rounding when Parsing with Pandas


code:
from bs4 import BeautifulSoup

text = "
 hello

 world
"

soup = BeautifulSoup(text)
print soup.get_text()  # prints " hello  world "

rawDescription: Python, get text value of an html document


code:
fp_while = lambda pred, fun, acc: (lambda val: fp_while(pred, fun, val) if pred(val) else val)(fun(acc))

print fp_while(lambda x: x < 5, lambda x: x + 1, 1)

rawDescription: Functional programming - for and while loops


code:
import numpy as np

N = 100

# x coordinates of the data
xmin = -10
xmax = 10
x = np.linspace(xmin, xmax, N, endpoint=False)

# Generate data in y.
n = 35
y = np.zeros(N)
y[:n] = 1 - np.cos(np.linspace(0, 2*np.pi, n))
y[:n] /= 0.7 + 0.3*np.random.rand(n)
m = 10
y = np.r_[y[m:], y[:m]]

# Compute coefficent 1 of the discrete Fourier transform.
w = np.exp(-2j*np.pi*np.arange(N) / N)
F1 = y.dot(w)
print "F1 =", F1

# Get the angle of F1 (in the interval [0,2*pi]).
angle = np.angle(F1.conj())
if angle < 0:
    angle += 2*np.pi

center_x = xmin + (xmax - xmin) * angle / (2*np.pi)
print "center_x = ", center_x

# Create the first sinusoidal mode for the plot.
mode1 = (F1.real * np.cos(2*np.pi*np.arange(N)/N) -
         F1.imag*np.sin(2*np.pi*np.arange(N)/N))/np.abs(F1)

import matplotlib.pyplot as plt

plt.clf()
plt.plot(x, y)
plt.plot(x, mode1)
plt.axvline(center_x, color='r', linewidth=1)
plt.show()

rawDescription: using fft to find the center of mass under periodic boundary conditions

description: Using fft to find the center of mass under periodic boundary conditions


code:
import itertools
import collections

q1 = 'q1'
q2 = 'q2'
q3 = 'q3'

dic_list = {
     q1:[1,2,3,4,5],
     q2:[2,3,5],
     q3:[2,5]
     }

dic_set = {k:set(v) for k,v in dic_list.items()}

def combo_solution():
     new_dic = collections.defaultdict(dict)
     for k1,k2 in itertools.combinations(dic_set,2):
          value = len(dic_set[k1] & dic_set[k2])
          new_dic[k1][k2] = value
          new_dic[k1][k2] = value
     return new_dic

def perm_solution():
     new_dic = collections.defaultdict(dict)
     for k1, k2 in itertools.permutations(dic_set,2):
          new_dic[k1][k2] = len(dic_set[k1] & dic_set[k2])
     return new_dic

import timeit
print timeit.timeit('combo_solution()','from __main__ import combo_solution',number=100000)
print timeit.timeit('perm_solution()','from __main__ import perm_solution',number=100000)

rawDescription: how to get the common items count of lists

description: Get the common items count of lists


code:
import HTMLParser
parser = HTMLParser.HTMLParser()
parser.unescape('í')
print parser.unescape('í')

rawDescription: Decoding html entities in python2

description: Decoding html entities in python2


code:
import bs4
my_string = "
this is some \n fun
And this is \n some more fun!"
soup = bs4.BeautifulSoup(my_string)
p = soup.p.contents[0].replace('\n ','')
print p

rawDescription: How do I remove linebreaks ONLY if they occur inside html tags?

description: Remove linebreaks ONLY if they occur inside html tags


code:
import re

from lxml import etree

pattern = re.compile(r"^(\d+) day\(s\)")

data = """your XML here"""
tree = etree.fromstring(data)

content = tree.findtext(".//String[@name='content']")
if content:
    match = pattern.search(content)
    if match:
        days = int(match.group(1))

        # TODO: check the days if needed

        node = tree.findtext(".//String[@name='node']")

        print node, days

rawDescription: Python ElementTree - Search children/grandchildren in poorly written XML


code:
def is_reverse(word1, word2):
    if len(word1) == len(word2):
        if word1 == word2[::-1]:
            return True
    return False

file = open('List.txt')
words = list(file)
for word1 in words:
    word1 = word1.strip()
    word1 = word1.lower()
    for word2 in words:
        word2 = word2.strip()
        word2 = word2.lower()
        print word1 + word2
        if is_reverse(word1, word2):
            print word1 + ' is the opposite of ' + word2

rawDescription: Finding words that are the reverse of each other in a file


code:
class wrapped(object):
    def __init__(self, func):
        self.func = func
    def __enter__(self):
        def wrapper(*args, **kwargs):
            return self.func(self.func, *args, **kwargs)
        return wrapper
    def __exit__(self, *args):
        pass

def test(callee, value=None):
    if value is None:
        print 'function attribute "x" is {}'.format(callee.x)
    else:
        print 'setting function attribute "x" to {}'.format(value)
        callee.x = value

with wrapped(test) as test:
    test(42)  # -> setting function attribute "x" to 42
    test()    # -> function attribute "x" is 42

rawDescription: Python: Reference to the function from inside itself (like arguments.callee in JavaScript)


code:
opener = urllib2.build_handler(MyRedirectHandler)
try:
    fp = opener.open(url)
except urllib2.HTTPError:
    content = None
    print 'This URL is not allowed or something'
else:
    content = fp.read()

rawDescription: How do I intercept a request after all redirects are resolved but before page download with urllib2?

description: Intercept a request after all redirects are resolved but before page download with urllib2


code:
def comp_door(self):
    choice = self.door_decision.user_text()  # actually use the returned result

    if choice == "left":
        self.left_door()
    elif choice == "right":
        self.right_door()
    else:
        print "incorrect input"

rawDescription: How do I make an imported function call a new function?

description: Make an imported function call a new function


code:
from gensim import corpora, models, similarities
from itertools import chain

""" DEMO """
documents = ["Human machine interface for lab abc computer applications",
             "A survey of user opinion of computer system response time",
             "The EPS user interface management system",
             "System and human system engineering testing of EPS",
             "Relation of user perceived response time to error measurement",
             "The generation of random binary unordered trees",
             "The intersection graph of paths in trees",
             "Graph minors IV Widths of trees and well quasi ordering",
             "Graph minors A survey"]

# remove common words and tokenize
stoplist = set('for a of the and to in'.split())
texts = [[word for word in document.lower().split() if word not in stoplist]
         for document in documents]

# remove words that appear only once
all_tokens = sum(texts, [])
tokens_once = set(word for word in set(all_tokens) if all_tokens.count(word) == 1)
texts = [[word for word in text if word not in tokens_once] for text in texts]

# Create Dictionary.
id2word = corpora.Dictionary(texts)
# Creates the Bag of Word corpus.
mm = [id2word.doc2bow(text) for text in texts]

# Trains the LDA models.
lda = models.ldamodel.LdaModel(corpus=mm, id2word=id2word, num_topics=3, \
                               update_every=1, chunksize=10000, passes=1)

# Prints the topics.
for top in lda.print_topics():
  print top
print

# Assigns the topics to the documents in corpus
lda_corpus = lda[mm]

# Find the threshold, let's set the threshold to be 1/#clusters,
# To prove that the threshold is sane, we average the sum of all probabilities:
scores = list(chain(*[[score for topic_id,score in topic] \
                      for topic in [doc for doc in lda_corpus]]))
threshold = sum(scores)/len(scores)
print threshold
print

cluster1 = [j for i,j in zip(lda_corpus,documents) if i[0][1] > threshold]
cluster2 = [j for i,j in zip(lda_corpus,documents) if i[1][1] > threshold]
cluster3 = [j for i,j in zip(lda_corpus,documents) if i[2][1] > threshold]

print cluster1
print cluster2
print cluster3

rawDescription: Topic distribution: How do we see which document belong to which topic after doing LDA in python


code:
for string in itertools.imap(''.join, itertools.product('ABC', repeat=3)):
    print string

rawDescription: How to generate all possible strings in python?

description: Generate all possible strings in python


code:
import struct

class TypeConv(object):
    BSA = '='  # native Byte order, standard Size, with no Alignment
    def __init__(self, conv_func, fmt_chr):
        self.__dict__.update(conv_func=conv_func, fmt_chr=fmt_chr)

    def pack(self, data):
        py_value = self.conv_func(data)
        count = str(len(py_value)) if self.conv_func is str else ''
        return struct.pack(self.BSA+count+self.fmt_chr, py_value)

type_conv = {'string': TypeConv(str, 's'),
             'int32': TypeConv(int, 'i'),
             'int64': TypeConv(long, 'q'),
             'float32': TypeConv(float, 'f'),
            }

array = [['string', 'int32', 'string', 'int64', 'float32', 'string', 'string'],
         ['any string', '19', 'any string', '198732132451654654',
          '0.6', 'any string', 'any string']]

binary_values = [type_conv[type_id].pack(data)
                     for type_id, data in zip(array[0], array[1])
                         if type_id in type_conv]  # to skip any unknown type_ids
print binary_values

rawDescription: How to convert a list with multiple types to binary in Python?

description: Convert a list with multiple types to binary in Python


code:
from PySide import QtCore
QtCore.QSettings.setDefaultFormat(QtCore.QSettings.IniFormat)

settings = QtCore.QSettings("Moose Soft", "Facturo-Pro")
print settings.format()

QtCore.QCoreApplication.setOrganizationName("MooseSoft")
QtCore.QCoreApplication.setApplicationName("Facturo-Pro")
settings = QtCore.QSettings()
print settings.format()

rawDescription: QSettings setpath ini format still using registry on windows


code:
import os.path, time
print "last modified: %s" % time.ctime(os.path.getmtime(file))
print "created: %s" % time.ctime(os.path.getctime(file))

rawDescription: getting files in a directory modified within a date range in linux using python

description: Getting files in a directory modified within a date range in linux using python


code:
import wx

def onDocClick(evt):
   message = wx.MessageBox("did it work?","Question",wx.YES_NO)

def onAboutClick(evt):
   print "clicked"

app = wx.App()
frame = wx.Frame(parent=None,title="RVR GUI",size=(315,490))
frame.Show()
panel = wx.Panel(frame,-1,size=(315,490))

menubar = wx.MenuBar()
fileMenu = wx.Menu()
documentation = fileMenu.Append(wx.ID_NEW,"Documentation")
about = fileMenu.Append(wx.ID_ABOUT,"About")
menubar.Append(fileMenu,'File')

frame.SetMenuBar(menubar)
frame.Bind(wx.EVT_MENU,onDocClick,documentation)
frame.Bind(wx.EVT_MENU,onAboutClick,about)
app.MainLoop()

rawDescription: wxPython: MenuBar Bound Events Execute on Init

description: WxPython: MenuBar Bound Events Execute on Init


code:
fdr= os.open("./mypipe", os.O_RDONLY | os.O_NONBLOCK)
while (1):
  try:
    s= os.read(fdr, 10)
    print s
  except OSError, e:
    if e.errno==errno.EBUSY:
        print "nothing to read yet\n"
    else:
        print os.strerror(e.errno)
  time.sleep(1)

rawDescription: Is there a way to send a signal if something happens, and receive the signal and do something else?


code:
def tester(key,value):
    if ('e' in str(value)) & (key not in ('x')):
        return 0
    elif key in ('x'):     # This was what you wanted last time.
        return value       # I have commented it out to match output
    else:
        return (10**6) * value

newdic = {key:[tester(key,i) for i in value] for key,value in dic_1.items()}
print newdic

rawDescription: Exponential not working in Python

description: Exponential not working in Python


code:
board = ['0']*5
print '\n'.join([' '.join(board)]*5)

rawDescription: Trying to get a nice format output, easy Python


code:
import ruamel.yaml as yaml

from ruamel.yaml.loader import Reader, Scanner, Composer, SafeConstructor, \
     Resolver, Parser

import datetime

class TimeConstructor(SafeConstructor):
    def __init__(self):
        SafeConstructor.__init__(self)

    def construct_yaml_time(self, node):
        value = self.construct_scalar(node)
        if ':' not in value:
            return SafeConstructor.construct_yaml_int(self, node)
        return datetime.time(*[int(x) for x in value.split(':')])

TimeConstructor.add_constructor(
    u'tag:yaml.org,2002:int',
    TimeConstructor.construct_yaml_time)

class TimeLoader(Reader, Scanner, Parser, Composer, TimeConstructor, Resolver):
    def __init__(self, stream):
        Reader.__init__(self, stream)
        Scanner.__init__(self)
        Parser.__init__(self)
        Composer.__init__(self)
        TimeConstructor.__init__(self)
        Resolver.__init__(self)

yaml_str = """\
MyDateTime: 10:08:37
x: 12345
"""

data = yaml.load(yaml_str, Loader=TimeLoader)
print data

rawDescription: Create datetime.time() object in yaml?

description: Create datetime.time() object in yaml


code: print In[59]

rawDescription: get current prompt number in ipython

description: Get current prompt number in ipython


code:
from simpleparse.parser import Parser

grammar = r"""
<space>      := [ \t]
<newline>    := '\n'
<identifier> := [A-Za-z_],[A-Za-z0-9z_]*
match        := newline,identifier,space+,'WORD'
<junk>       := newline*,identifier,space+,-'WORD',(identifier/space)*
data         := (match/junk)*
"""

parser = Parser(grammar, 'data')

data = 'some junk\nvariable1 WORD\nvariable2 some ' +\
       'junk\nvariable3 WORD\nvariable4 some other ' +\
       'junk\nvariable5 WORD'

(start, matches, stop) = parser.parse(data)

print [ start for name, start, stop, other in matches ]

rawDescription: Regex matching newline before word in python


code:
import Tkinter, Tkconstants, tkFileDialog
class TkFileDialogExample(Tkinter.Frame):

  def __init__(self, root):
    Tkinter.Frame.__init__(self, root)

    # define buttons
    Tkinter.Button(self, text='askopenfilename',
                   command=self.askopenfilename).pack()
    Tkinter.Button(self, text='askdirectory',
                   command=self.askdirectory).pack()

    self.file = '' # initialize attributes as empty strings
    self.path = '' #

  def askopenfilename(self):
    self.file = tkFileDialog.askopenfilename() # set attribute
    print self.file

  def askdirectory(self):
    self.path = tkFileDialog.askdirectory()    # set attribute
    print self.path

if __name__=='__main__':
    root = Tkinter.Tk()
    TkFileDialogExample(root).pack()         # these lines must be *before*
    myFileDialog = TkFileDialogExample(root) # root.mainloop()
    root.mainloop()

rawDescription: Use Tkinter in OOP approach

description: Use Tkinter in OOP approach


code:
from itertools import izip_longest

rows = [line.split() for line in myfile]            # Read
rows = zip(*izip_longest(*rows, fillvalue="0"))     # Add zeroes
print "\n".join(" ".join(row) for row in rows)      # Write

rawDescription: zero fill empty spaces at specific locations

description: Zero fill empty spaces at specific locations


code:
import struct

def toU32(bits):
        return ord(bits[0]) << 24 | ord(bits[1]) << 16 | ord(bits[2]) << 8  | ord(bits[3])

def toS32(bits):
    candidate = toU32(bits);
    if (candidate >> 31): # is the sign bit set?
        return (-0x80000000 + (candidate & 0x7fffffff)) # "cast" it to signed
    return candidate

for x in range(-5,5):
    bits = struct.pack(">i", x)
    print toU32(bits)
    print toS32(bits)

rawDescription: Read 32-bit signed value from an "unsigned" bytestream


code:
for x in elements:
    print str(x[0]), ':', str(x[1]), str(x[2])

rawDescription: Using for loops with nested loops in python


rawDescription: Python: sort dictionary with tuples as key by the value


code:
import multiprocessing as mp
import multiprocessing.util as util
pool=mp.Pool(1)
print list(pool.imap(abs, range(3)))

rawDescription: multiprocessing Pool.imap broken?

description: Multiprocessing Pool.imap broken


code:
seq = ['znc-devel-0:1.4-1.el6.i686', 'znc-devel2-0:1.4-1.el6.x86_64']
print [item.partition("-0")[0] for item in seq]

rawDescription: Removing suffix from list Python

description: Removing suffix from list Python


rawDescription: Python numpy: Convert string in to numpy array


code:
import csv
def skip_comments(iterable):
    for line in iterable:
        if not line.startswith('#'):
            yield line

with open('data.csv','rb') as f:
    firstline = next(f).split()
    delimiter = firstline[1][-1]
    fields = firstline[2:]
    for line in csv.DictReader(skip_comments(f),
                               delimiter = delimiter, fieldnames = fields):
        print line

rawDescription: Reading formatted text using python

description: Reading formatted text using python


code:
print len(exactMatch.findall("my blue cat"))
print len(exactMatch.findall("my red car"))
print len(exactMatch.findall("my red and blue monkey"))
print len(exactMatch.findall("my yellow dog"))

rawDescription: Python RegEx, match words in string and get count


code:
import parsedatetime.parsedatetime as pdt

def datetimeFromString( s ):

    c = pdt.Calendar()
    result, what = c.parse( s )

    dt = None

    # what was returned (see http://code-bear.com/code/parsedatetime/docs/)
    # 0 = failed to parse
    # 1 = date (with current time, as a struct_time)
    # 2 = time (with current date, as a struct_time)
    # 3 = datetime
    if what in (1,2):
        # result is struct_time
        dt = datetime.datetime( *result[:6] )
    elif what == 3:
        # result is a datetime
        dt = result

    if dt is None:
        # Failed to parse
        raise ValueError, ("Don't understand date '"+s+"'")

    return dt

rawDescription: Handling the different results from parsedatetime


code:
def get_data():
    header_list = ['Gross profit', 'Research and development',
                   'Total costs and expenses', 'Total operating expenses',
                   'Operating income', 'Income before income taxes']
    raw_financial_data = [['Fiscal year ends in December. USD in millions except per share data.', 'TTM', '2012-12', '2011-12', '2010-12', '2009-12', '2008-12'],
                          ['Gross profit', '125390', '146216', '179627', '120923', '98817', '188549'],
                          ['Costs and expenses'],
                          ['Total costs and expenses', '64695', '67490', '106370', '67964', '64040', '106799'],
                          ['Income before income taxes', '60695', '78726', '73257', '52959', '34777', '81750']]
    financial_data = []
    rfd_header = [h[0] for h in raw_financial_data]
    ttm_count = 0
    for d in header_list:
        print ''
        print d
        for i in raw_financial_data:
            try:
                if i[1] == 'TTM' and ttm_count == 0:
                    print '1st append', i
                    financial_data.append(i)
                    ttm_count = 1
                    continue
            except IndexError:
                print 'IndexError'
                continue
            if i[0] == d:
                print '2nd append', i
                financial_data.append(i)
            elif d not in rfd_header:
                rfd_header.append(d)
                print '3nd append', 'No Data'
                financial_data.append(['No Data', 'N/A', 'N/A',
                                                'N/A', 'N/A', 'N/A','N/A'])
            else:
                print 'no append'
    return financial_data

if __name__ == "__main__":
    for row in get_data():
        print row

rawDescription: Continue in try: except: seems not to skip code block


code:
#Winner checker.

def check_done(playerSym,board):
    return ((board[6] in playerSym and board[7] in playerSym and board[8] in playerSym) or # across the top
    (board[3] in playerSym and board[4] in playerSym and board[5] in playerSym) or # across the middle
    (board[0] in playerSym and board[1] in playerSym and board[2] in playerSym) or # across the bottom
    (board[6] in playerSym and board[3] in playerSym and board[0] in playerSym) or # down the left side
    (board[7] in playerSym and board[4] in playerSym and board[3] in playerSym) or # down the middle
    (board[8] in playerSym and board[5] in playerSym and board[2] in playerSym) or # down the right side
    (board[6] in playerSym and board[4] in playerSym and board[2] in playerSym) or # diagonal
    (board[8] in playerSym and board[4] in playerSym and board[0] in playerSym)) # diagonal

# Sets up the Board

def drawBoard(board):
    print('   |   |')
    print(' ' + board[6] + ' | ' + board[7] + ' | ' + board[8])
    print('   |   |')
    print('-----------')
    print('   |   |')
    print(' ' + board[5] + ' | ' + board[4] + ' | ' + board[3])
    print('   |   |')
    print('-----------')
    print('   |   |')
    print(' ' + board[0] + ' | ' + board[1] + ' | ' + board[2])
    print('   |   |')

#Verifies if board is not full with all largest signs and no mpre moves are possible.
def movesPossible(board):
    #TODO: implement this
    return True

#Prints the current board

def print_board(board):
    # Make a duplicate of the board list and return it the duplicate.
    dupeBoard = []
    for i in board:
        dupeBoard.append(i)
    return dupeBoard

#The Board

board = ["0", "1", "2", "3", "4", "5", "6", "7", "8"]

done = False

#The Move list for X's and O's

xmovesList = ["*","x","X"]
omovesList = [".","o","O"]

#Greeting message and choosing the initial X and O assignment.
print "Welcome to Tic-Tac-Toe-Cover-Up!"
playerSym = raw_input("Player 1, please select either X or O for your pieces: ")
print

#Player and opponent move assignment
if (playerSym == "X"):
    player = xmovesList
    opponent = omovesList
else:
    player = omovesList
    opponent = xmovesList

drawBoard(board)

#Main game loop.
while movesPossible(board):
    print player[2],"'s turn"
    print "Select the place you want to play (0-8)"
    print_board(board)
    moved = False
    pos = input("Select: ")
    if pos <=8 and pos >=0:
        Y = pos/3
        X = pos%3
        if X != -1:
            X -=1
        else:
            X = 2
            Y -=1

        if board[pos] == str(pos):
            board[pos] = player[0]
            moved = True
            done = check_done(player,board)
        else :
            if board[pos] == opponent[0]:
                board[pos] = player[1]
                moved = True
                done = check_done(player,board)
            if board[pos] == opponent[1]:
                board[pos] = player[2]
                moved = True
                done = check_done(player,board)
    drawBoard(board)
    if done:
        break;
    if moved :
        player,opponent = opponent,player

if(done):
    print player[2], "wins "
else:
    print "No more moves possible. It's a draw."

rawDescription: Tic Tac Cover up!! Can't get the program to work correctly


code:
import struct
s = struct.pack('S5if',"Hello",42,3.1415)
print s
print struct.unpack('5sif')

rawDescription: In Python, how can I convert both numbers and strings into byte arrays?


code:
import sys
from PyQt4 import QtGui, QtCore

def text_change():
    print "change ok"
def text_click(event): # <--- NOTE: event object is passed.
    print "clicked ok "

app = QtGui.QApplication(sys.argv)
textEdit = QtGui.QTextEdit()
textEdit.setGeometry(QtCore.QRect(30, 20, 351, 51))
textEdit.setObjectName("textEdit")
textEdit.mousePressEvent = text_click
QtCore.QObject.connect(textEdit, QtCore.SIGNAL("textChanged()"), text_change)
textEdit.show()
sys.exit(app.exec_())

rawDescription: clicked event not working in textedit of pyside

description: Clicked event not working in textedit of pyside


code:
import sys
def main():
    pass
    sys.exit(2) #comment this out, or change the exit status to 0 to test.

try:
    main()
except SystemExit as e:
    if e.args[0]:  #non-zero exit status
        print "Failed"
    else:
        print "Success"
    raise
else:
    print "Success"

rawDescription: Make atexit registered function act according to exit status


code:
def getValuesGen(s):
    while s:
        v1, v2, v3, _, _, _, s = s.split(',', 6)
        yield "%s %s %s" % (v1, v2, v3)
for v in getValuesGen(s):
    print v

rawDescription: Python Speeding Up Retrieving data from extremely large string


code:
conn = sqlite3.connect('/path/to/database.db')
cursor=conn.cursor()
t=(value,)
cursor.execute("select field from table where other_field = ?", t)
returnObject = cursor.fetchone()
if returnObject:
    print returnObject[0]
else:
    print "Nothing found!"

rawDescription: SQLite no return type in Python

description: SQLite no return type in Python


code:
for header in soup.select('tr td a h6'):
    name = header.get_text(strip=True)
    practice = header.parent.find_next_sibling(text=True).strip()
    address = header.find_parent('td').find_next_sibling('td').get_text(' ', strip=True)
    print name, practice, address

rawDescription: beautifulsoup selecting certain values only

description: Beautifulsoup selecting certain values only


code:
bad_list=[]
good_list=[]

bad_list.append(['blue_widget', 'cracked', '776'])
bad_list.append(['red_widget', 'not_smooth', '545'])
bad_list.append(['yellow_widget', 'spots', '35'])
bad_list.append(['green_widget', 'smells_bad', '10'])
bad_list.append(['purple_widget', 'not_really_purple', '10'])

good_list.append(['blue_widget', 'ok', '776'])
good_list.append(['red_widget', 'ok', '545'])
good_list.append(['green_widget', 'ok', '10'])

# ['yellow_widget', 'spots', '35']
# ['purple_widget', 'not_really_purple', '10']

labels = zip(*good_list)[0]

new_bad_list=[]

for item in bad_list:
    if item[0] not in labels:
        new_bad_list.append(item)

print new_bad_list

rawDescription: List comprehension on lists within lists


code:
myints, mystrings = separate(r'myfile.txt')
print myints
print mystrings

rawDescription: How to read a text file into separate lists python

description: Read a text file into separate lists python


code:
def printItems(dictObj, parent, indent):
    if len(dictObj):
        print '{}
'.format('  ' * indent)
        for k,v in dictObj.iteritems():
            print '{}
<input type="checkbox" id="{}-{}">{}
'.format(
                            '  ' * (indent+1), k, parent, k)
            printItems(v, k, indent+1)
        print '{}
'.format('  ' * indent)

rawDescription: From python dictionary to html list

description: From python dictionary to html list


code:
#!/usr/bin/python

import random

def create_list(sampleSize, upperLimit):
    numbList = []
    while sampleSize > 0:
        sampleSize -= 1
        randomNum = random.randrange(0,upperLimit+1)
        numbList.append(randomNum)
    numbList.sort(key=int)
    return numbList

def median(numList):
    list_len = len(numList)
    if list_len % 2:
        return numList[list_len / 2]
    else:
        return (numList[list_len / 2] + numList[list_len / 2 - 1]) / 2.0

def main():
    number_lists = 4
    sample_size = 5
    upper_limit = 50
    lists = []
    median_list = []

    for i in range(number_lists):
        lists.append(create_list(sample_size, upper_limit))

    for current_list in lists:
        current_median = median(current_list)
        print current_list, " : median (", current_median, ")"
        median_list.append(current_median)

    print "Median list is ", median_list

if __name__ == "__main__":
    main()

rawDescription: How to make a loop repeat itself n number of times - Python 3

description: Make a loop repeat itself n number of times - Python 3


code:
sentence = raw_input("Please enter a sentence : ").lower()
shift = input("Please enter your shift : ")

validLetters = map(chr, range(97, 123))
validLetters.append(' ')

tmp = [ord(ch)+shift for ch in sentence if ch in validLetters]
resulta = [chr(x if 97 <= x <= 122 else 96+x%122) if x != ord(' ')+shift else ' ' for x in tmp]

print sentence
print
print("Your encryption is :")
print
print ''.join(resulta)

rawDescription: Python Caesar Cipher keeping spaces

description: Python Caesar Cipher keeping spaces


code:
class Square():
    def __init__(self, length,name=""):
        self.length = length
        self.name = name
    def area(self):
        return self.length ** 2
    def __str__(self): #this is the part I don't know how to write
        return "%s is a square with side:%s and area:%s." % (self.name, self.length, self.area())
a = Square(5,'MySquare')
print a # MySquare is a square with side:5 and area:25.

rawDescription: How do I fix the part in __str__ so it would display the name of the instance within the class? Python 2X

description: Fix the part in __str__ so it would display the name of the instance within the class? Python 2X


code:
import random
def func1():
    lis=list(xrange(100))
    for i in xrange(100):
        index=random.randrange(0,100)
        remainder = len(lis[index:])-1
def func2():
    lis=list(xrange(100))
    for i in xrange(100):
        index=random.randrange(0,100)
        remainder = (len(lis) - index) -1

if __name__ == '__main__':
    from timeit import Timer
    t = Timer("func2()", "from __main__ import func2")
    print t.timeit()
    t = Timer("func1()", "from __main__ import func1")
    print t.timeit()

rawDescription: Calculating the number of remaining items in a list from a given index


code:
i = random.randint(0, len(s) - 4)
w = s[i:i+4]
print w

rawDescription: create and insert randomly duplicates

description: Create and insert randomly duplicates


code:
import mock

from sqlalchemy.sql import ClauseElement

class MockSession(mock.MagicMock):
    def __init__(self, *arg, **kw):
        kw.setdefault('side_effect', self._side_effect)
        super(MockSession, self).__init__(*arg, **kw)
        self._lookup = {}

    def _side_effect(self, *arg, **kw):
        if self._mock_return_value is not mock.sentinel.DEFAULT:
            return self._mock_return_value
        else:
            return self._generate(*arg, **kw)

    def _get_key(self, arg, kw):
        return tuple(self._hash(a) for a in arg) + \
                tuple((k, self._hash(kw[k])) for k in sorted(kw))

    def _hash(self, arg):
        if isinstance(arg, ClauseElement):
            expr = str(arg.compile(compile_kwargs={"literal_binds": True}))
            return expr
        else:
            assert hash(arg)
            return arg

    def _generate(self, *arg, **kw):
        key = self._get_key(arg, kw)
        if key in self._lookup:
            return self._lookup[key]
        else:
            self._lookup[key] = ret = MockSession()
            return ret

if __name__ == '__main__':

    from sqlalchemy import Column, Integer
    from sqlalchemy.ext.declarative import declarative_base

    Base = declarative_base()

    class Foo(Base):
        __tablename__ = 'foo'

        id = Column(Integer, primary_key=True)
        x = Column(Integer)
        y = Column(Integer)

    sess = MockSession()

    # write out queries as they would in the code, assign return_value
    sess.query(Foo.x).filter(Foo.x == 5).first.return_value = 5
    sess.query(Foo.x).filter(Foo.x == 2).first.return_value = 2
    sess.query(Foo).filter(Foo.x == 2).filter_by(y=5).all.return_value = [Foo(x=2, y=5)]
    sess.query(Foo).filter(Foo.x == 9).all.return_value = [Foo(x=9, y=1), Foo(x=9, y=2)]

    # those queries are now replayable and will return what was assigned.
    print sess.query(Foo.x).filter(Foo.x == 5).first()
    print sess.query(Foo.x).filter(Foo.x == 2).first()
    print sess.query(Foo).filter(Foo.x == 2).filter_by(y=5).all()
    print sess.query(Foo).filter(Foo.x == 9).all()

rawDescription: What's the correct way to unit test Pyramid views with a SQLAlchemy DBSession?


code:
from nltk.corpus import wordnet as wn

for i in wn.all_synsets():
    if i.pos() in ['a', 's']: # If synset is adj or satelite-adj.
        for j in i.lemmas(): # Iterating through lemmas for each synset.
            if j.antonyms(): # If adj has antonym.
                # Prints the adj-antonym pair.
                print j.name(), j.antonyms()[0].name()

rawDescription: How to generate a list of antonyms for adjectives in WordNet using Python

description: Generate a list of antonyms for adjectives in WordNet using Python


code:
from abc import ABCMeta, abstractmethod
from _weakrefset import WeakSet

class EditedABCMeta(ABCMeta):

    def __new__(mcls, name, bases, namespace):
        cls = type.__new__(mcls, name, bases, namespace)
        # Compute set of abstract method names
        abstracts = set(name
                     for name, value in namespace.items()
                     if getattr(value, "__isabstractmethod__", False))

        for base in cls.__mro__:
            for name, value in base.__dict__.items():
                if getattr(value, "__isabstractmethod__", False) and name not in cls.__dict__:
                    abstracts.add(name)

        cls.__abstractmethods__ = frozenset(abstracts)
        # Set up inheritance registry
        cls._abc_registry = WeakSet()
        cls._abc_cache = WeakSet()
        cls._abc_negative_cache = WeakSet()
        cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
        return cls

class A(object):
    __metaclass__ = EditedABCMeta

    @abstractmethod
    def veryspecificmethod(self):
        pass

class B(A):
    def veryspecificmethod(self):
        print 'doing something in B'

    @abstractmethod
    def foo(self):
        print 'foo from B'

class C(B):
    def foo(self):
        pass

class D(C, B):
    pass

if __name__ == '__main__':
    for cls in (C, D):
        try:
            cls().veryspecificmethod
        except TypeError as e:
            print e.message
    print '-'*20
    for cls in (C, D):
        try:
            cls().foo
        except TypeError as e:
            print e.message

rawDescription: Force implementation of a method in all inheriting classes


code:
#!/usr/bin/python
print "Hello, world!"

rawDescription: Asyncronous Bidirectional IO Redirection for a child process


code:
iceweasel.py
"""
Usage:
    iceweasel.py <pythonfile>...
    iceweasel.py -h

Prints internal details for arbirtary set of <pythonfile> files.
"""
import os

def srcdetails(fname):
    with open(fname) as f:
        content = f.read()
    shortname = os.path.split(fname)[-1]
    size = len(content)
    words = len(content.split())
    templ = """
    ---- {fname} -----
    short name: {shortname}
    size: {size}
    words: {words}
    """
    print templ.format(**locals())

def main(pythonfiles):
    for fname in pythonfiles:
        srcdetails(fname)

if __name__ == "__main__":
    from docopt import docopt
    args = docopt(__doc__)
    pythonfiles = args["<pythonfile>"]
    main(pythonfiles)

rawDescription: Execute the script with special command iceweasel ( which is a firefox webbrowser for debian )


rawDescription: Take last value of the prior grouping in a rolling sum function? Pandas Python


code:
import re
separator = "INFO1: BLAH\nINFO2: BLAH\n"
map(lambda(p): "%s%s" % (separator, p), re.split(r'%s.*' % separator, all_text)[1:])

rawDescription: How to get the text from a match till the next match of the same pattern

description: Get the text from a match till the next match of the same pattern


code:
M = 2
handy = df.groupby('factor1')['factor1'].count()
minimumValueMFactors = handy[handy>=M]
dataOfMinimumValueMFactors = df[df['factor1'].isin(minimumValueMFactors.index)]
print dataOfMinimumValueMFactors

rawDescription: Using pandas to filter data frame for the most popular factors


code:
from bs4 import BeautifulSoup
data = """

test3

        test4

"""
soup = BeautifulSoup(data)

for img in soup.select('div.separator > a > img'):
    print img.get('src')

rawDescription: Beautiful Soup: searching for a nested pattern?


code:
def list_example():
    output_list = []
    output_list.append(1)
    print "x"
    output_list.append(2)
    return output_list

rawDescription: Questions about Python 'yield' keyword that I have not found answers elsewhere, and its specific use in a code I am working on


code:
main = ['123', '147', '159', '258', '369', '357', '456', '789']

match1 = str(1374)
match2 = str(1892)

def has_any_match(main, match):
    return any(all(c in match for c in x) for x in main)

print has_any_match(main, match1)
print has_any_match(main, match2)

rawDescription: best way to match these string or list?

description: Match these string or list


code:
def f(g):
    last = len(g.values)-1
    g.iloc[last,:] = g.iloc[last-1,:]
    return g
print df.groupby(level=0).apply(f)

rawDescription: How to fillna the last row of each group in Pandas?

description: Fillna the last row of each group in Pandas


code:
import os
import sys
import timeit

setup_str = \
'''
from __main__ import setup, handler

setup()
'''

dirs = \
(
    ( - 1,   0),
    ( + 1,   0),
    (   0, - 1),
    (   0, + 1),
    ( - 1, - 1),
    ( - 1, + 1),
    ( + 1, - 1),
    ( + 1, + 1)
)

def setup():
    global grid_max_x
    grid_max_x = 25
    global grid_max_y
    grid_max_y = 25
    global grid
    grid = [[9 for col in range(grid_max_y)] for row in range(grid_max_x)]

    global start
    start = (12, 12)
    grid[start[0]][start[1]] = 0
    global numRips
    numRips = 8

def handler():
    global grid
    global start
    global numRips
    border_expanded = set([start])
    allExpanded = set([start])
    num = 1
    for r in range(numRips):
        toExpand = set([])
        map(lambda x: toExpand.update(x), [(getUnvisitedNeighbors(n, allExpanded)) for n in border_expanded])
        border_expanded = toExpand
        allExpanded.update(toExpand)
        for u in toExpand:
            grid[u[0]][u[1]] = num
        num += 1

def getUnvisitedNeighbors(loc, visitedCells):
    global grid_max_x
    global grid_max_y
    global dirs

    x, y = loc

    neighbors = set([((x + dx) % grid_max_x, (y + dy) % grid_max_y) for (dx, dy) in dirs])

    unvisitedNeighbors = neighbors - visitedCells

    return unvisitedNeighbors

print timeit.repeat(stmt="handler()", setup=setup_str, repeat=3, number=1)
for i in range(len(grid)):
    print grid[i]

rawDescription: Inefficient Expansion Algorithm

description: Inefficient Expansion Algorithm


code:
book = open_workbook(path_to_file)
sheet = book.sheet_by_index(0)
for i in range(1, sheet.nrows):
    row = sheet.row_values(i)
    for cnt in range(len(row)):
       print row[cnt]

rawDescription: How to grab the value in a certain column in a certain row using XLRD

description: Grab the value in a certain column in a certain row using XLRD


code:
import os
import shutil
folder_name = "c:\\users\\andrew\\pdf"
for root_folder, _, file_names in os.walk(folder_name):
    for file_n in file_names:
        new_name = os.path.join(root_folder, "ORG" + file_n)
        old_name = os.path.join(root_folder, file_n)
        print "We will copy at ", new_name, old_name
        shutil.copyfile(old_name, new_name)

rawDescription: Renaming/copying in windows python

description: Renaming/copying in windows python


code:
import numpy as np
x = np.arange(10, dtype=np.uint16)
y = x.view(np.uint8)

print 'The original array:', x
print '...Viewed as uint8:', y
print '...Which we can do some tricks with', y[::2]

# Now let's interpret the uint16's as two streams of uint8's...
a, b = y[::2], y[1::2]
b[:] = np.arange(10, dtype=np.uint8)
print a
print b
print x
print y
# Notice that the original is modified... We're not duplicating memory anywhere!

rawDescription: Numpy: Permanent changes to using numpy.ndarray.view?


code:
class MyClass(object):
    pass

def class_factory(name):
    new_cls = type(name + MyClass.__name__, (MyClass,), {})
    return new_cls() # Or pass whatever you want in here...

print type(class_factory('name1'))
print type(class_factory('name2'))

rawDescription: Python: make every instance have a different superclass


code:
from bs4 import BeautifulSoup,SoupStrainer
f = open('dataset/reut2-001.sgm', 'r')
data= f.read()
soup = BeautifulSoup(data)
contents = soup.findAll('content')
for content in contents:
    print content.text

rawDescription: extracting body tags from smg file Beautiful Soup and Python

description: Extracting body tags from smg file Beautiful Soup and Python


rawDescription: Sorting bytes in words, tuples in python


code:
def call_clsinit(cls):
    cls._clsinit()
    return cls

@call_clsinit
class MyClass:
    @classmethod
    def _clsinit(cls):
        print "MyClass._clsinit()"

rawDescription: Calling a class method upon creation of Python classes


code: print msg[end:end+10]

rawDescription: getting a grammar to read more than one keyword in the text

description: Getting a grammar to read more than one keyword in the text


rawDescription: Returning Output of Python CGI MySQL Script


code:
print "Start Command!"
os.system("adb pull ...")
print "Command Finished!!"

rawDescription: ADB pull completion

description: ADB pull completion


code:
import __builtin__
realimp = __builtin__.__import__
def my_import(name, globals={}, locals={}, fromlist=[]):
  print 'importing', name, fromlist
  return realimp(name, globals, locals, fromlist)
__builtin__.__import__ = my_import

from os import path

rawDescription: Python: intercept a class loading action


code:
data = [(1, 'item1'), (2, "item2"), (2, "item3"), (3, "item4"), (2, "item5")]

import json

class Entity(object):
    def __init__(self, name=None, parent=None):
        self.name = name
        self.parent = parent
        self.level = parent.level + 1 if parent else 0
        self.children = []

    def to_json_obj(self):
        if len(self.children):
            return {'name': self.name, 'children': [child.to_json_obj() for child in self.children]}
        else:
            return {'name': self.name}

def data_to_json(data):
    tree = Entity()
    current = tree
    for item in data:
        if item[0] > current.level:
            pass
        else:
            while True:
                current = current.parent
                if current.level == item[0] - 1:
                    break
        new = Entity(item[1], current)
        current.children.append(new)
        current = new
    return json.dumps(tree.to_json_obj()["children"][0], indent=2)

print data_to_json(data)

rawDescription: How do i turn `[(1, 'item1'),(2, "item2"),(2, "item3"),(3, "item4"),(2, "item5")]` into a JSON tree and HTML tree in python?

description: Turn `[(1, 'item1'),(2, "item2"),(2, "item3"),(3, "item4"),(2, "item5")]` into a JSON tree and HTML tree in python


code:
from __future__ import division
print 8/7  # prints 1.1428571428571428
print 8//7 # prints 1

rawDescription: What is __future__ in Python used for and how/when to use it, and how it works


code:
class MyCallable(object):
    def __init__(self):
        self.message = "Look ma, I got called!"

    def __call__(self, *args, **kwargs):
        self.print_message()

    def print_message(self):
        print(self.message)

class CallableChild(object):
    def __call__(self, *args, **kwargs):
        super(CallableChild, self).__call__(*args, **kwargs)
        print "...as a child, too!"

rawDescription: Can methods have methods in Python?

description: Can methods have methods in Python


code:
url = 'http://varenhor.st/wp-content/uploads/emails.txt'
document = urllib2.urlopen(url)
for line in document:
    cleaned = line.rpartition('@')[0]
    print cleaned

rawDescription: Python - how to delete all characters in all lines after some sign?


code:
import requests
r = requests.get('https://github.com/timeline.json', timeout=5, stream=True)

r.raw.decode_content = True
content = r.raw.read(100000+1)
if len(content) > 100000:
    raise ValueError('Too large a response')
print content
print json.loads(content)

rawDescription: http request with timeout, maximum size and connection pooling

description: Http request with timeout, maximum size and connection pooling


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

# Configure test database for SA
engine = create_engine("sqlite:///:memory:", echo=False)
session = scoped_session(sessionmaker(bind=engine, autoflush=False))

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

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

t_character_tag = Table(
    "t_character_tag", Base.metadata,
    Column("character_id", Integer, ForeignKey("t_character.id")),
    Column("tag_id", Integer, ForeignKey("t_tag.id"))
    )

class Character(Base):
    __tablename__ = u"t_character"
    id = Column(Integer, primary_key=True)
    name = Column(String)
    tags = relationship("Tag", secondary=t_character_tag, backref="characters")

class Tag(Base):
    __tablename__ = u"t_tag"
    id = Column(Integer, primary_key=True)
    name = Column(String)

# create db schema
Base.metadata.create_all(engine)

# 0. create test data
ch1 = Character(id=1, name="ch-1")
ch2 = Character(id=2, name="ch-2")
ch3 = Character(id=3, name="ch-3")
ch4 = Character(id=4, name="ch-4")
ta1 = Tag(id=1, name="tag-1")
ta2 = Tag(id=2, name="tag-2")
ta3 = Tag(id=3, name="tag-3")
ch1.tags.append(ta1)
ch2.tags.append(ta1); ch2.tags.append(ta2);
ch3.tags.append(ta1); ch3.tags.append(ta2); ch3.tags.append(ta3);
ch4.tags.append(ta3)
session.add_all((ch1, ch2, ch3, ch4,))
session.commit()

# 1. some data checks
session.expunge_all()
assert len(session.query(Character).all()) == 4
assert session.query(Tag).get(2).name == "tag-2"
assert len(session.query(Character).get(3).tags) == 3

# 2. create a final query (THE ANSWER TO THE QUESTION):
session.expunge_all()
t_c1 = aliased(Character)
t_c2 = aliased(Character)
t_t1 = aliased(Tag)
t_t2 = aliased(Tag)
q =(session.query(t_c1, t_c2).
    join((t_c2, t_c1.id < t_c2.id)).
    join((t_t1, t_c1.tags)).
    join((t_t2, t_c2.tags)).
    filter(t_t1.id == t_t2.id).
    filter(t_c1.name != t_c2.name). # if tag name is unique, this can be dropped
    order_by(t_c1.id).
    order_by(t_c2.id)
    )
q = q.distinct() # filter out duplicates
res = [_r for _r in q.all()]
assert len(res) == 4
for _r in res:
    print _r

rawDescription: Many-to-many intersection in sqlalchemy

description: Many-to-many intersection in sqlalchemy


code:
import re
def repl(matchobj):
    global counter
    counter = counter+1
    if counter == 2:
        return '{replace-string}'
    else:
        return matchobj.group(0)

counter = 0
line = "$lang['MY_KEY'] = '{search-string} wird in Analyse {search-string} bercksichtigt'";
print re.sub(r'{.*?}',repl,line)

rawDescription: Search & replace a substring in python

description: Search & replace a substring in python


code:
for i in ServerList:
  print i['IP_']

rawDescription: Python - Is it possible to have a dictionary in a list?


code:
import re
p = re.compile(ur'<span [^>]*>(\d+)\D(\d+)\D[^<]*')
test_str = u"..."

re.findall(p, test_str)

rawDescription: How to extract the price from html using regex in python

description: Extract the price from html using regex in python


code:
classifier = '!\0!'
print classifier
classifier == '!!'

rawDescription: Python split() not working as expected for first line in file


code:
import time

for i in range(10):
  print i
  time.sleep(1)

rawDescription: Cross compiling a python script on windows into linux executable


code:
def same_structure(a,b):
    if not is_list(a) and not is_list(b):
        print '#1'
        return True
    else:
        if is_list(a) and is_list(b):
            print '#2'
            if len(a) != len(b):
                print '#3'
                return False
            if len(a) == len(b):
                print '#4'
                for e in range(len(a)):
                    print 'e = ', e, 'a[e]= ', a[e], 'b[e]=', b[e]
                    if not same_structure(a[e], b[e]):
                        return False
                return True
        else:
            return False

rawDescription: How to properly use recursion in this (simple) python procedure?

description: Properly use recursion in this (simple) python procedure


code:
#! /usr/bin/env python

import re

log_lines = '''

2014-11-22 10:51:31 (96.9 KB/s) - `C:/r1/www.itb.ie/AboutITB/index.html' saved [13302]

--2014-11-22 10:51:31--  http://www.itb.ie/CurrentStudents/index.html
Connecting to www.itb.ie|193.1.36.24|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: ignored [text/html]
Saving to: `C:/r1/www.itb.ie/CurrentStudents/index.html'

     0K .......... .......                                      109K=0.2s

2014-11-22 10:51:31 (109 KB/s) - `C:/r1/www.itb.ie/CurrentStudents/index.html' saved [17429]

--2014-11-22 10:51:32--  http://www.itb.ie/Vacancies/index.html
Connecting to www.itb.ie|193.1.36.25|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: ignored [text/html]
Saving to: `C:/r1/www.itb.ie/Vacancies/index.html'

     0K .......... .......... ..                                118K=0.2s

2014-11-22 10:51:32 (118 KB/s) - `C:/r1/www.itb.ie/Vacancies/index.html' saved [23010]

--2014-11-22 10:51:32--  http://www.itb.ie/Location/howtogetthere.html
Connecting to www.itb.ie|193.1.36.26|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: ignored [text/html]
Saving to: `C:/r1/www.itb.ie/Location/howtogetthere.html'

     0K .......... .......                                      111K=0.2s
'''

time_and_url_pat = re.compile(r'--(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})--\s+(.*)')
ip_pat = re.compile(r'Connecting to.*\|(.*?)\|')

time_and_url_list = time_and_url_pat.findall(log_lines)
print '\ntime and url\n', time_and_url_list

ip_list = ip_pat.findall(log_lines)
print '\nip\n', ip_list

all_data = [(t, u, i) for (t, u), i in zip(time_and_url_list, ip_list)]
print '\nall\n', all_data, '\n'

for t in all_data:
    print t

rawDescription: Parse wget log file in python

description: Parse wget log file in python


rawDescription: How to split a csv file on date using python

description: Split a csv file on date using python


code:
import os
def Test1(rootDir):
    list_dirs = os.walk(rootDir)
    for root, dirs, files in list_dirs:
        for d in dirs:
            print os.path.join(root, d)
        for f in files:
            print os.path.join(root, f)

rawDescription: Print out the whole directory tree

description: Print out the whole directory tree


code:
import multiprocessing as mp
import time

def build():
    print 'I build things'
    for i in range(10):
        with open('testfile{}.txt'.format(i), 'w+') as f:
            f.write('')
            time.sleep(5)

def main():
    build_p = mp.Process(name='build process', target=build)
    build_p.start()
    return 18

if __name__ == '__main__':

    v = main()
    print v
    print 'done'

rawDescription: How do I place a process in the background so it does not interrupt my return?

description: Place a process in the background so it does not interrupt my return


code:
import urllib2
import json
import re

def test(*args):
     f = urllib2.urlopen("http://api.urbandictionary.com/v0/define?term=%s" % '+'.join(args).lower())  # note urllib2.urlopen rather than urllib.request.urlopen
     data = json.loads(f.read().decode("utf-8"))  # note f.read() instead of f.readall()
     if len(data['list']) > 0:
        definition = data['list'][0][u'definition']
        example = data['list'][0][u'example']
        permalink = data['list'][0][u'permalink']
        return "Urban Dictionary search for %s: %s Example: %s Link: %s" % (str(args), definition, example, permalink)  # returns a string

print test('mouth', 'hugging').replace('\n\n', '\n')  # prints the string after replacing '\n\n' with '\n'

rawDescription: Removing \r and \n from list

description: Removing \r and \n from list


code: print val_2

rawDescription: Count the duplicates in a list of tuples


code:
text = """JOHN ...""" # text without the header

# These can be inferred if necessary
cols = [0, 24, 29, 39, 43, 52, 71, 84, 95, 109, 117]

db = []
row = []
for line in text.strip().split("\n"):
    data = [line[cols[i]:cols[i+1]] for i in xrange((len(cols)-1))]
    if data[0][0] != " ":
        if row:
            db.append(row)
        row = map(lambda x: [x], data)
    else:
        for i, c in enumerate(data):
            row[i].append(c)
print db

rawDescription: How can I scrape data from a text table using Python?

description: Scrape data from a text table using Python


code:
positions = {
    "P": {"name": "Pitcher","avg": 0.200},
    "C": {"name": "Catcher","avg": 0.404},
    "1B": {"name": "1st Base","avg": 0.224},
    "2B": {"name": "2nd Base","avg": 0.245},
    "3B": {"name": "3rd Base","avg": 0.333},
    "SS": {"name": "Short Stop","avg": 0.234},
    "LF": {"name": "Left Field","avg": 0.240},
    "CF": {"name": "Center Field","avg": 0.200},
    "RF": {"name": "Right Field","avg": 0.441}
}
def print_player(hits, at_bats, pos):
    position = positions[pos]
    ba = round(float(hits) / at_bats, 3)
    pa = round(position["avg"], 3)
    diff = abs(round((ba - pa) / pa * 100, 1))
    comp = "worse than" if ba < pa else "better than" if ba > pa else "equal to"
    print """
          This player's batting average is {}
          and it is {} percent {} the average {}
          """.format(ba, diff, comp, position["name"])

rawDescription: Creating functions in Python with "if else" statements and named constants


code:
my_str = 'ATTTTACGTA'

# This loop will check that
#  - the casting to int is ok
#  - there are only two numbers inputted
#  - stop >= start
#  - start > 0
#  - stop < len(my_str)
while True:
    try:
        start, stop = map(int, raw_input(
            'Please enter start and stop index separated by whitespace\n').split())
        if stop < start or start < 0 or stop > len(my_str):
            raise ValueError
        break
    except ValueError:
        print 'Bad input, try again'

# Loop over all chars, check if the current index is inside range(start, stop).
# If it is, add (char, '?') to the array, if not, add ('?', char) to the array.
#
# This would give you an array of something like this:
# [('?', 'A'), ('?', 'T'), ('T', '?'), ('T', '?'), ('?', 'T'), ('?', 'A'),
#  ('?', 'C'), ('?', 'G'), ('?', 'T'), ('?', 'A')]
#
# By using zip(*array), we unpack each element, and saves the first indexes as
# one list, and the second indexes as another, giving you a list like this:
#
# [('?', '?', 'T', 'T', '?', '?', '?', '?', '?', '?'),
#  ('A', 'T', '?', '?', 'T', 'A', 'C', 'G', 'T', 'A')]

chars = zip(*((c, '?') if i in range(start, stop) else ('?', c)
              for i, c in enumerate(my_str)))

# ''.join is used to concencate all chars into two strings
my_lst = [''.join(s) for s in chars]
print my_lst

rawDescription: Split a list item in python at user defined index


code: print repr(getStockData('MSFT'))

rawDescription: Download prices with python

description: Download prices with python


code:
class DummyItem:
    def __add__(self, other):
        return other

s = DummyItem()
s += 23
print s

rawDescription: python create empty object of arbitrary type?

description: Python create empty object of arbitrary type


code: print "".join(re.findall(ur'[\u0600-\u06FF]', unicode(my_string, "utf-8"), re.UNICODE))

rawDescription: How to retrieve only arabic texts from a string using regular expression?

description: Retrieve only arabic texts from a string using regular expression


code: print type(frames)

rawDescription: How to append to a custom python Sequence?

description: Append to a custom python Sequence


code:
li_dict =[{'invoice':1,'debit':10,'credit':10},{'name':'b','invoice':2,'debit':10,'credit':18},
{'invoice':1,'debit':10,'credit':10}, {'name':'b','invoice':2,'debit':10,'credit':18}]

final_result = []
for dic in li_dict :
    temp_dict = {}
    flag = False

    for dic_f in final_result:
        if 'invoice' in dic and 'invoice' in dic_f:
            if dic['invoice'] == dic_f['invoice']:
                dic_f['debit'] = dic['debit'] + dic_f['debit']
                dic_f['credit'] = dic['credit'] + dic_f['credit']
                flag = True
                break

    if not flag:
        temp_dict['invoice'] = dic.get('invoice', 0)
        temp_dict['debit'] = dic.get('debit', 0)
        temp_dict['credit'] = dic.get('credit', 0)
        final_result.append(temp_dict)

print final_result

rawDescription: How to add (sum) the two values in a python dictionary based on another value?

description: Add (sum) the two values in a python dictionary based on another value


code: print "a", "b" # prints something like "a b\n"

rawDescription: f.write vs print >> f

description: F.write vs print >> f


code:
def permute(inputData, outputSoFar):
    for elem in inputData:
        if elem not in outputSoFar:
            outputSoFar.append(elem)
            if len(outputSoFar) == len(inputData):
                yield outputSoFar
            else:
                for permutation in permute(inputData, outputSoFar):
                    yield permutation # --- Recursion
            outputSoFar.pop()

for i in permute([1,2,3], []):
    print i

rawDescription: Python permutation generator puzzle

description: Python permutation generator puzzle


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

import os
import sys
from SimpleHTTPServer import SimpleHTTPRequestHandler
import BaseHTTPServer

class MyHTTPRequestHandler(SimpleHTTPRequestHandler):
    def translate_path(self,path):
        path = SimpleHTTPRequestHandler.translate_path(self,path)
        if os.path.isdir(path):
            for base in "index", "default":
                for ext in ".html", ".htm", ".txt":
                    index = path + "/" + base + ext
                    if os.path.exists(index):
                        return index
        return path

def test(HandlerClass = MyHTTPRequestHandler,
         ServerClass = BaseHTTPServer.HTTPServer):

    import argparse
    parser = argparse.ArgumentParser(description='Dump ANT files')
    parser.add_argument('-p','--port',
                        type=int,
                        default=8080,
                        help='port number')
    parser.add_argument('-i','--ip',
                        default='',
                        help='IP address to listen on: "" means all')
    parser.add_argument('-d','--docroot',
                        default='.',
                        help='Directory to serve files from')
    parser.add_argument('-s','--https',
                        action='store_true',
                        help='Use HTTPS instead of HTTP')
    parser.add_argument('-c', '--certfile', help='server certificate file')
    parser.add_argument('-k', '--keyfile', help='private key file')
    args = parser.parse_args()
    if os.path.isdir(args.docroot):
        os.chdir(args.docroot)
    else:
        parser.error('Docroot must be a directory')

    proto = 'HTTP'
    server_address = (args.ip, args.port)
    httpd = ServerClass(server_address, HandlerClass)

    if args.https:
        import ssl
        if not args.certfile:
            parser.error('Certificate file must be specified')
        if not os.path.isfile(args.certfile):
            parser.error('Certificate file must exist')
        if not args.keyfile:
            parser.error('Private key file must be specified')
        if not os.path.isfile(args.keyfile):
            parser.error('Private key file must exist')
        httpd.socket = ssl.wrap_socket(
            httpd.socket,
            server_side=True,
            certfile=args.certfile,
            keyfile=args.keyfile)
        proto = 'HTTPS'

    sa = httpd.socket.getsockname()
    print "Serving %s on %s port %s ..."%(proto, sa[0], sa[1])
    httpd.serve_forever()

if __name__ == '__main__':
    test()

rawDescription: SimpleHTTPServer add default.htm default.html to index files


code:
a = bytearray(b"Hello World") # put your records into byte arrays like this
b = bytearray(b"Stack Overflow")
complexStructure = bytearray(a[0:6]+b[0:]) # Slice and join arrays to form
                                           # new array with just data from your
                                           # high level entity
print complexStructure

rawDescription: How to create a memoryview for a non-contiguous memory location?

description: Create a memoryview for a non-contiguous memory location


code:
#!/usr/bin/env python
logfile = 'logfile'

start_line = 'Starting time loop'
started = False

with open(logfile) as f:
  for l in f.readlines():
    if l.startswith(start_line):
      started = True
    if started:
      print l.strip()

rawDescription: Skipping lines from a text file using Python


code:
# Construct list of keywords
# ** EDIT** this might be better as a set
# A list will use less memory, but a set is much faster lookup
keywords = [kw.rstrip() for kw in open('file1')]

# Construct dictionary of keywords
kvdict = {}

with open('file2') as f2:
    for line in f2:
        kw = line.split()[0]
        if kw in keywords:
            kvdict[kw] = line.rstrip()

with open('file3') as f3:
    for line in f3:
        kw = line.split()[1]
        if kw in kvdict:
            newline = line.replace(kw, kvdict[kw])
            print newline,       # or maybe write it to a file?

rawDescription: Python : text replacemnt in two files using a keyword?


code: print mydict["no.3"]

rawDescription: from elements in a line to key and values

description: From elements in a line to key and values


code:
#my_script.py
with open('t1.txt', 'r') as fl1:
    f1 = [i.split(',') for i in fl1.read().split('\n')]

with open('t2.txt', 'r') as fl2:
    f2 = [i.split(',') for i in fl2.read().split('\n')]

f3 = [[a if b is None or b==len(b)*b[0] else b for a,b in map(None,x,y)] for x,y in zip(f1,f2)]

for i in f3:
    for j in i:
        print j,
    print

rawDescription: Joining two csv files in Python

description: Joining two csv files in Python


code:
import feedparser
import string

d = feedparser.parse(
    'http://api.wxbug.net/getLiveCompactWeatherRSS.aspx?ACode=A6787859817&zipcode=18657&unittype=0')

print str(d.feed['aws_weather']).translate(None, string.ascii_letters)

rawDescription: How to obtain value from WeatherBug XML File in Python?

description: Obtain value from WeatherBug XML File in Python


code:
s = db.session
comments = models.Comment.query.all()
for c in comments:
    print c.user_id
s.close()

rawDescription: How to specify more than 1 backref

description: Specify more than 1 backref


code:
for _ in range(_):
    print name1
    print name2
    print value1
    print value2

rawDescription: Trying to create a (list or array, not sure) with data I have outputted from a nested for loop


code:
class MyClass(QtCore.QAbstractListModel):
    def __init__(self):
        super(MyClass, self).__init__()
        self.elements=['Bison','Panther','Elephant','Duck','Hawk','Pigeon','Shark','Salmon','Piranha']
    def rowCount(self, index=QtCore.QModelIndex()):
        return len(self.elements)
    def data(self, index, role=QtCore.Qt.DisplayRole):
        print'MyClass.data():',index,role
        if index.isValid() and role == QtCore.Qt.DisplayRole:
            return QtCore.QVariant(self.elements[index.row()])
        else:
            return QtCore.QVariant()

rawDescription: How to assing model() to QListView

description: Assing model() to QListView