code:
import os

def close_files(fd_min=3, fd_max=-1):
    if os.path.exists('/proc/self/fd'):
        close_files_with_procfs(fd_min, fd_max)
    else:
        close_files_exhaustively(fd_min, fd_max)

def close_files_exhaustively(fd_min=3, fd_max=-1):
    import resource
    fd_top = resource.getrlimit(resource.RLIMIT_NOFILE)[1] - 1
    if fd_max == -1 or fd_max > fd_top:
        fd_max = fd_top
    for fd in range(fd_min, fd_max+1):
        try:
            os.close(fd)
        except OSError:
            pass

def close_files_with_procfs(fd_min=3, fd_max=-1):
    for nm in os.listdir("/proc/self/fd"):
        if nm.startswith('.'):
            continue
        fd = int(nm)
        if fd >= fd_min and (fd_max == -1 or fd < fd_max):
            try:
                os.close(fd)
            except OSError:
                pass

def timereps(reps, func):
    from time import time
    start = time()
    for i in range(0, reps):
        func()
    end = time()
    return (end - start) / reps

print "close_files: %f" % timereps(100, lambda: close_files())
print "close_files_exhaustively: %f" % timereps(100, lambda: close_files_exhaustively())
print "close_files_with_procfs: %f" % timereps(1000, lambda: close_files_with_procfs())

rawDescription: fork: close all open sockets

description: Fork: close all open sockets


code:
x = int(raw_input("Please enter an integer: "))
if x < 0:
    x = 0
    print 'Negative changed to zero'
elif x == 0:
    print 'Zero'
elif x == 1:
    print 'Single'
else:
    print 'More'

rawDescription: Loop commands equivalent in IDL to python


code:
nums = [5] * 3
print map(id, nums)

rawDescription: Star operator (*) applied to lists and integers


code:
for ind,line in enumerate(l):
    print ind,line

rawDescription: How to loop through the index of a list in python?

description: Loop through the index of a list in python


code:
class MyClass(object):
    @myclassmethod
    def method(cls):
        print cls

m = MyClass()
m.method()

rawDescription: Location of @classmethod

description: Location of @classmethod


code: print sorted.__doc__

rawDescription: Can anyone explain why this sorting won't work?


code:
import time
import thread

class ledController():
    #static variables
    def __init__(self, GPIO, state=False): # x = " " - Default variable if user leaves it empty
        self.GPIO = GPIO
        self.state = state #Default LED state is off
    def ledSwitch(self):
        self.state = not self.state
    def ledON(self):
        self.state = True
    def ledOFF(self):
        self.state = False

    def ledBlink(self, duration):
        self.ledON()
        print(self.ledDetails())
        time.sleep(duration)
        self.ledOFF()
        print(self.ledDetails())
        time.sleep(duration)

    def ledDetails(self):
        return "LED: " + str(self.GPIO) + " State: " + str(self.state) + '\n'

redLED = ledController(15)
blueLED = ledController(16, True)

try:
   thread.start_new_thread( redLED.ledBlink, (5, ) )
   thread.start_new_thread( blueLED.ledBlink, (2, ) )
except:
   print "Error: unable to start thread"

rawDescription: Multiplethreading pauses the second thread


code:
import re
inp="({A,B,C},{(A,B),(B,C),(C,A)})"

m = re.search("{(.*?)},{\((.*?)\)}",inp)

nodes = m.group(1).split(',')
edges = map(lambda x:x.split(','), m.group(2).split('),('))
graph = {}

for i in nodes:
 graph[i]=set([edge[edge.index(i)-1] for edge in edges if i in edge])

print graph

rawDescription: Converting string into dictionary containing sets


code:
import re

ss = '123/abc'

match = re.search('/', ss)

print ss[:match.start()]
print match.group()
print ss[match.end():]

rawDescription: Equivalent perl Regex in python

description: Equivalent perl Regex in python


code:
import struct
import ctypes

data=ctypes.create_string_buffer(10)
struct.pack_into(">i", data, 5, 0x12345678)
print list(data)

rawDescription: Python, how to put 32-bit integer into byte array


code:
import xml.dom.minidom

def parseList(el, lvl=0):
    txt = ""
    indent = "\t" * (lvl)
    for item in el.childNodes:
        # These are the <list-item>s: They can have text and nested <list> tag
        for subitem in item.childNodes:
            if subitem.nodeType is xml.dom.minidom.Element.TEXT_NODE:
                # This is the text before the next <list> tag
                txt += "\n" + indent + "* " + subitem.nodeValue
            else:
                # This is the next list tag, its indent level is incremented
                txt += parseList(subitem, lvl=lvl+1)
    return txt

def parseXML(s):
    doc = xml.dom.minidom.parseString(s)
    return parseList(doc.firstChild)

a = "<list><list-item>First level<list><list-item>Second level</list-item><list-item>Second level 2<list><list-item>Third level</list-item></list></list-item></list></list-item></list>"
print parseXML(a)

rawDescription: Converting HTML list (<li>) to tabs (i.e. indentation)


code:
for i in l2:
  print ','.join(i)

Bob,two,5,4,2
Gerald,five,9,6,3
Abby,one,9,4,1

rawDescription: How do I sort a numerical list with names attached?

description: Sort a numerical list with names attached


code:
try:
    urllib.urlopen(
        "http://example.com",
        proxies={'http':'http://example.com:8080'}
    )
except IOError:
    print "Connection error! (Check proxy)"
else:
    print "All was fine"

rawDescription: Proxy Check in python

description: Proxy Check in python


code:
class turn(object):
    def __init__(self):
        cls = type(self)
        self.currentTurn = cls.TURNS[cls.curr]

    def getTurn(self):
        cls = type(self)
        return cls.TURNS[cls.curr]

    def switch(self):
        cls = type(self)
        cls.curr = 1 - cls.curr

        if cls.curr == 0:
            print "It's your move! \n"

        self.currentTurn = cls.TURNS[cls.curr]

rawDescription: Variable naming in Python classes

description: Variable naming in Python classes


code:
words_in_order = []
for item in g_data:
    words_from_span = item.text.split()
    # You might want to break this out into its own function for modularity.
    for word in words_from_span:
        if word not in STOP_WORDS:
            words_in_order.append(word)
print words_in_order

rawDescription: Removing words from python lists?

description: Removing words from python lists


code: print json.dumps({'success': True, 'data': [dict(d, Week='1238') for d in celldict.values()]}, indent=4)

rawDescription: format python output to json

description: Format python output to json


code:
import os

def string_similarity(string, _cp=os.path.commonprefix):
    return sum(len(_cp([string, string[i:]])) for i in xrange(len(string)))

for _ in xrange(int(raw_input())):
    print string_similarity(raw_input())

rawDescription: How to optimize this python script further?

description: Optimize this python script further


code:
import random

#Convert string to list
msg = list(original_str)

random.seed(str_key)
random.shuffle(msg)
print "".join(msg)

rawDescription: Influence of choosing string as seed of random on the output


code:
unformatted_returns = [('2015-6-10', u'88.48'), ('2015-6-9', u'86.73'), ('2015-6-8', u'86.15'), ('2015-6-5', u'86.05')]
date_new  = '2015-6-8'
for n in unformatted_returns:
    if date_new == n[0]:
        print "found date"
        break
else:
    print "date {0} not found".format(date_new)

date_new = '2015-6-7'  # not in the list
for n in unformatted_returns:
    if date_new == n[0]:
        print "found date"
        break
else:
    print "date {0} not found".format(date_new)

rawDescription: Python: for each tuple in list, check if string in tuple


code:
import random, time
from subprocess import Popen

def work_diligently():
    cmd = ["/bin/sleep", str(random.randrange(2,4))]
    proc = Popen(cmd)
    print '\t{}\t{}'.format(proc.pid, cmd) # pylint: disable=E1101
    return proc

def spawn(num):
    return [ work_diligently() for _ in xrange(num) ]

NUM_PROCS = 3
procs = spawn(NUM_PROCS)
while True:
    print time.ctime(), 'scan'
    procs = [
        proc for proc in procs
        if proc.poll() is None
    ]
    num_exited = NUM_PROCS - len(procs)
    if num_exited:
        print 'Uhoh! Restarting {} procs'.format(num_exited)
        procs.extend( spawn(num_exited) )
    time.sleep(1)

rawDescription: wait() on a group of Popen objects

description: Wait() on a group of Popen objects


code:
def createButton(text, pt1button, pt2button, win):
    button = Text(Point(pt1button, pt2button), text)
    button.draw(win)
    return button

def _callback(pt):
   print "I love unicorns!"
   print
   print "Mouse clicked at x=%d, y=%d"%(pt.x,pt.y)
   print

def test():
   win = GraphWin()
   win.setCoords(0,0,100,100)
   quitButton = createButton("Quit",70,73,win)
   Rectangle(Point(45, 50), Point(95,97)).draw(win)
   win.setMouseHandler(_callback)
   while True:
      win.getMouse()
   win.close()

rawDescription: Creating two button methods Python

description: Creating two button methods Python


code:
a, b = "ab"
a
b
print a, b
print (a, b)

rawDescription: pyparsing: getting results from parsed data

description: Pyparsing: getting results from parsed data


code: print "x equals {}".format(x)

rawDescription: turning variables that use 'str()' into numbers

description: Turning variables that use 'str()' into numbers


code:
if __name__=='__main__':
    class Foo(object):
        @ReadOnlyCachedAttribute
        # @read_only_lazyprop
        def bar(self):
            print 'Calculating self.bar'
            return 42
    foo=Foo()
    print(foo.bar)
    # Calculating self.bar
    # 42
    print(foo.bar)
    # 42
    try:
        foo.bar=1
    except AttributeError as err:
        print(err)
        # This property is read-only
    del(foo.bar)
    print(foo.bar)
    # Calculating self.bar
    # 42

rawDescription: How to create decorator for lazy initialization of a property

description: Create decorator for lazy initialization of a property


code:
import datetime
user_time = str(input("Enter datetime from when you want to delete, like this format 2015-3-4 14:00:00:"))
real_user_time = datetime.datetime.strptime(user_time, '%Y-%m-%d %H:%M:%S')
print real_user_time  # as you can see here, user time has been changed from a string to a datetime object

rawDescription: Python script for EC2 snapshots, use datetime to delete old snapshots


code:
import re, itertools

with open("input.txt", "r") as f_input:
    for line in f_input:
        reLine = re.match(r"<(\w+)>\[(.*?)\].*?{(.*?)\}", line)
        lNumbers = [re.findall(".*?(\d+).*?", entry) for entry in  reLine.groups()[1:]]
        lNumbers = list(itertools.chain.from_iterable(lNumbers))
        print reLine.group(1), " | ".join(lNumbers)

rawDescription: process a text file using various delimiters

description: Process a text file using various delimiters


code:
import glob
res = [f for f in glob.glob("*.txt") if "abc" in f or "123" in f or "a1b" in f]
for f in res:
    print f

rawDescription: regular expression using in glob.glob of python

description: Regular expression using in glob.glob of python


rawDescription: Having some trouble with regex and double brackets


code: exec exp

rawDescription: Boost python, calling function objects with a namespace


code:
for i, item in enumerate(ndimage.find_objects(x), start=1):
    print i, ':'
    print x[item], '\n'

rawDescription: How to select all locations of unique elements in numpy 2d array with bounding box around them?

description: Select all locations of unique elements in numpy 2d array with bounding box around them


code:
from itertools import product

def Leet2Combos(word):
    possibles = []
    for l in word.lower():
        ll = REPLACE.get(l, l)
        possibles.append( (l,) if ll == l else (l, ll) )
    return [ ''.join(t) for t in product(*possibles) ]

print Leet2Combos('zit')
print Leet2Combos('red')

rawDescription: 'leet' program - getting all permutations


code:
alist = ["a", "b", "c"]
print "b" in alist      # will print True
print "d" in alist      # will print False

rawDescription: List & Loops in python3

description: List & Loops in python3


code:
s = "mouse"
animal_letter = s.find('s')
print animal_letter

rawDescription: Finding the Index of a character within a string


code:
class Foo:
    bar = 1

print Foo.bar # prints '1'

rawDescription: Static class members python

description: Static class members python


code:
class Foo:
    def __str__(self):
            return '__str__'
    def __repr__(self):
            return '__repr__'
bar = Foo()
bar
print bar
repr(bar)
str(bar)

rawDescription: How to print a list, dict or collection of objects, in Python

description: Print a list, dict or collection of objects, in Python


code:
from collections import Counter
counter = Counter()
import ast
keywords = []
with open("abc") as f:
    for line in f:
        #strip {} and split the line at ", "
        line = line.strip("{}\n").split(", ")
        counter += Counter(x.strip('"') for x in line)

le = len(counter)
for key,val in sorted(counter.items()):
    print "%s: %.1f%s" %(key, (val*1.0 / le)*100, '%')

print

for key,val in sorted(counter.items(), key = lambda x :(x[1],x[0]) ):
    print "%s: %.1f%s" %(key, (val*1.0 / le)*100, '%')

rawDescription: How arrange data in python

description: How arrange data in python


code:
d = {2: 1, 27: 28, 56: 28, 57: 29, 58: 29, 59: 29, 28: 29, 29: 1, 30: 1, 31: 1}

def path_to_root(d, start):
    yield start
    while start in d:
        start = d[start]
        yield start

print list(path_to_root(d, 27))

rawDescription: How to sort python dictionary based on similar values and keys?

description: Sort python dictionary based on similar values and keys


code:
import re
p = re.compile(ur'(?<!\*)\*{2}\w+?\*{2}(?!\*)')
test_str = u"**Company** [Click1](call:58200) ******provides*** **housekeeping** **clean***"

re.findall(p, test_str)

rawDescription: How to design regex pattern to match string

description: Design regex pattern to match string


code:
import re

matcher = re.compile(r"""\\plottwo   # The literal \plottwo
                         {           # Opening brace for the first image group
                          (          # Start the first capture group
                           [^}]+     # Match anything OTHER than a closing brace
                          )          # End the first capture group
                         }           # Closing brace
                         (?:         # Non-saving group that we can make optional
                            {        # Opening brace for the second image group
                             (       # Start the second capture group
                              [^}]+  # Match anything OTHER than a closing brace
                             )       # End the second capture group
                            }        # Closing brace
                         )?          # End the non-capturing group
                      """, re.VERBOSE)

print matcher.findall('\\plottwo{image1}{image2}')
print matcher.findall('\\plottwo{image2}')

rawDescription: Find two strings next to a keyword with re in python


rawDescription: python - xPath syntax for second occurence

description: Python - xPath syntax for second occurence


code:
start = 10
for num1, i_or_d, num2, m in re.findall('(\d+)([IDS])(\d+)?([A-Za-z])?', a):
    if i_or_d not in 'ID':
        start += int(num1) + int(num2)
        continue
    print num1, i_or_d, start
    if num1:
        start += int(num1)
    if num2:
        start += int(num2)

rawDescription: regular expression, report position by integer count from string

description: Regular expression, report position by integer count from string


code:
infoFromJson = json.loads(jsonfile)
print json2html.convert(json = infoFromJson)

rawDescription: Converting json to html table in python

description: Converting json to html table in python


code:
related = Group._meta.get_all_related_objects()
print related

rawDescription: How to filter objects in Django that do not have any other objects pointing to them?

description: Filter objects in Django that do not have any other objects pointing to them


code:
import numpy as np
import scipy.linalg

num_lines = 5
end_alpha_value = 0.97  ## end_alpha_value must be in the interval (0, 1)

pascal_triangle = scipy.linalg.pascal(num_lines + 1, kind='lower')

print 'num_reps: 1, minimum real root: %.3f' % end_alpha_value
for i in range(2, num_lines + 1):
    coeff_list = []
    for j, coeff in enumerate(pascal_triangle[i][:i]):
        coeff_list.append(coeff * ((-1)**(i+j+1)))
    coeff_list.append(-end_alpha_value)

    all_roots = np.roots(coeff_list)
    real_roots = all_roots[np.isreal(all_roots)]
    min_real_root = min(real_roots)
    real_valued = min_real_root.real[abs(min_real_root.imag) < 1e-5]

    print 'num_reps: %i, minimum real root: %.3f' % (i, real_valued[0])

rawDescription: Is it possible for matplotlib's alpha transparency values to "sum" to 1?


code:
def printValue(value):
    print 'This is the printValue() method. The value is %s'%(value)

def callPrintValue(methodName, *args):
    methodName(*args)
    print 'This is the callPrintValue() method'

rawDescription: How to pass a method that receives parameters as a parameter of another function in Python

description: Pass a method that receives parameters as a parameter of another function in Python


code:
s = r"Escaped\nNewline"
print s
s.decode('string_escape')
print s.decode('string_escape')
"a\\nb".decode('string_escape')

rawDescription: Python Replace \\ with \

description: Python Replace \\ with \


code:
def some_func():
    print "This is my original function!"

some_func = measured(some_func)

rawDescription: Is @measured a standard decorator? What library is it in?


code:
import re

regx = re.compile('(at\\b)|([^ ]r(?! ))')

def repl(mat, dic = {1:'ATU',2:'XIXI'}):
    return dic[mat.lastindex]

ch = 'the fat cat was impressed by all the rats gathering at one corner of the great room'

print ch
print
print regx.sub(repl,ch)

rawDescription: string suffix replacement in python

description: String suffix replacement in python


code:
r = requests.get('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=stackoverflow&count=100', auth=auth)
for tweet in r.json():
  print tweet['text']

rawDescription: How to perform oauth when doing twitter scrapping with python requests

description: Perform oauth when doing twitter scrapping with python requests


code:
def debug_nice(locals_dict, keys=[]):
    globals()['types'] = `__import__`('types')
    exclude_keys = ['copyright', 'credits', 'False',
                    'True', 'None', 'Ellipsis', 'quit']
    exclude_valuetypes = [types.BuiltinFunctionType,
                          types.BuiltinMethodType,
                          types.ModuleType,
                          types.TypeType,
                          types.FunctionType]
    return {k: v for k,v in locals_dict.iteritems() if not
               (k in keys or
                k in exclude_keys or
                type(v) in exclude_valuetypes) and
               k[0] != '_'}

rawDescription: How to print all variables values when debugging Python with pdb, without specifying each variable?

description: Print all variables values when debugging Python with pdb, without specifying each variable


code: print textwrap.fill('This is a cool sentence... How about eating it??? Whats more?? pepper is available all for free!!!', 10, break_long_words=False)

rawDescription: Include full words in string slicing

description: Include full words in string slicing


rawDescription: Python how to force one string to match format of another


code:
seq = ['To', 'sleep', 'perchance', 'to', 'dream']
print zip(seq, seq[1:])

rawDescription: Pythonic way of getting all consecutive 2-tuples from list


code:
from datetime import datetime

params = {
    'opened': '05/07/2015',
    'vendor_code': 'VC',
    'country_code': 'US',
}

class Inventory(object):
    def __init__(self, vendor_code=None, country_code=None, date_opened=None):
        self.vendor_code = vendor_code
        self.country_code = country_code
        self.date_opened = date_opened

    def __repr__(self):
        return repr(self.__dict__)

def create_inventory(params):
    # Deal with a raw value that needs to be processed
    date_opened = None
    if len(params['opened']) >= 1:
        date_opened = datetime.strptime(params['opened'], '%m/%d/%Y')

    # Remove the raw value from the params dict
    params.pop('opened')

    # Add the processed value to the `params` dict with the proper key
    params['date_opened'] = date_opened

    # All the other parameters get passed through unmodified
    inv = Inventory(**params)
    return inv

print create_inventory(params)

rawDescription: Is there a way in flask to get each object in the request.json and save each attribute on the model?


code: print elem.xpath('local-name()')

rawDescription: lxml parse xsd file without Schema URL

description: Lxml parse xsd file without Schema URL


code:
import glob
import subprocess

files_to_remove=['*.jpg', '*.txt', '*.gif']

for files_glob in files_to_remove:
    matches = glob.glob(files_glob)
    if matches:
        subprocess.check_call(['rm'] + matches)
    else:
        print 'no %s files in directory' % files_glob

rawDescription: Calling list items in subprocess

description: Calling list items in subprocess


code:
import urllib
from bs4 import BeautifulSoup

f = open('log1.txt', 'w')

url ='http://www.brothersoft.com/tamil-font-513607.html'
pageUrl = urllib.urlopen(url)
soup = BeautifulSoup(pageUrl)

for a in soup.select("div.class1.coLeft a[href]"):
    try:
        suburl = ('http://www.brothersoft.com'+a['href']).encode('utf-8','replace')
        f.write ('http://www.brothersoft.com'+a['href']+'\n')
    except:
        print 'cannot read'
        f.write('cannot read:'+'http://www.brothersoft.com'+a['href']+'\n')

        pass

    content = urllib.urlopen(suburl)
    soup = BeautifulSoup(content)
    for a in soup.select("div.Sever1.coLeft a[href]"):
        try:
            suburl2 = ('http://www.brothersoft.com'+a['href']).encode('utf-8','replace')
            f.write ('http://www.brothersoft.com'+a['href']+'\n')
        except:
            print 'cannot read'
            f.write('cannot read:'+'http://www.brothersoft.com'+a['href']+'\n')

            pass

        content = urllib.urlopen(suburl2)
        soup = BeautifulSoup(content)
        try:
            suburl3 = soup.find('body')['onload'][10:-2]
            print suburl3
            print suburl3.replace("&" + suburl3.split('&')[-1],"")
            #f.write (soup.find('body')['onload'][10:-2]+'\n')
        except:
            print 'cannot read'
            f.write(soup.find('body')['onload'][10:-2]+'\n')

            pass
f.close()

rawDescription: How to split and remove unwanted string in url?

description: Split and remove unwanted string in url


code:
from collections import defaultdict
from operator import iadd

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

def build_dd(lst):
    d = defaultdict(int)
    for item in lst:
        d[item] += 1
    return d

def subtract_dd(left, right):
    return {k: left[k] - v for k, v in right.iteritems()}

db = build_dd(b)
da = build_dd(a)
result = reduce(iadd,
                [[k] * v for k, v in subtract_dd(db, da).iteritems() if v > 0],
                [])

print result

rawDescription: Removing Elements in A from B

description: Removing Elements in A from B


code:
import collections

data = [('a', 3), ('b', 5), ('a', 7), ('c', 15), ('d', 12)]
result = collections.defaultdict(list)

for key, value in data:
    result[key].append(value)

print result

rawDescription: How do I turn a list of tuples into a dictionary while keeping redundant values?

description: Turn a list of tuples into a dictionary while keeping redundant values


rawDescription: Breaking down large dataset into organized index


code:
from itertools import islice
with open("Input.txt") as in_file:
    print [[int(line)] + map(int, islice(in_file, 3)) for line in in_file]

rawDescription: Python CSV transpose data in one column to rows


code:
g= Graphics() # create an object
# access the object's instance variables
print "screen", g.screen
g.screen= pygame.display.set_mode(SCREEN_SIZE, FULLSCREEN, 32)

rawDescription: How do you make a class attribute that isn't a standard data type?

description: Make a class attribute that isn't a standard data type


code:
import time, os
from  multiprocessing import Process

def worker():
    print "I'm process %s, my father is %s" % (os.getpid(), os.getppid())

print "I'm the main process %s" % os.getpid()
for i in range(0,3):
    Process(target=worker).start()

rawDescription: Starting a process as a subprocess in Python


code:
import httplib
import time

lst = """
science
scifi
"""

hdr= { 'User-Agent' : 'super happy flair bot by /u/spladug' }
conn = httplib.HTTPConnection('www.reddit.com')
for name in lst.split():
    conn.request('GET', '/r/'+name, headers=hdr)
    print conn.getresponse().read()
    time.sleep(2)
conn.close()

rawDescription: urllib2 HTTP error 429

description: Urllib2 HTTP error 429


code:
import nltk
from nltk.tokenize import sent_tokenize, word_tokenize
import csv

para = "What can I say about this place. The staff of the restaurant is nice and the eggplant is not bad. Apart from that, very uninspired food, lack of atmosphere and too expensive. I am a staunch vegetarian and was sorely dissapointed with the veggie options on the menu. Will be the last time I visit, I recommend others to avoid"

sentense = word_tokenize(para)
word_features = []

for i,j in nltk.pos_tag(sentense):
    if j in ['JJ', 'JJR', 'JJS', 'RB', 'RBR', 'RBS']:
        word_features.append(i)

rating = 0

for i in word_features:
    with open('words.txt', 'rt') as f:
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            if i == row[0]:
                print i, row[1]
                if row[1] == 'pos':
                    rating = rating + 1
                elif row[1] == 'neg':
                    rating = rating - 1
print  rating

rawDescription: to find the opinion of a sentence as positive or negative

description: To find the opinion of a sentence as positive or negative


code:
print np.arange(100).itemsize
print np.arange(100).nbytes

rawDescription: Numpy size of data type

description: Numpy size of data type


code:
for b in range(56):
    for a in range(57):
        print "a:", a, "b:", b

rawDescription: How to do a loop inside of a loop in python

description: Do a loop inside of a loop in python


code:
from itertools import groupby, chain
from datetime import datetime

filenames = """20121118011335_team1-pathway_Truck_Report_Data_10342532.zip
20121119011335_team1-pathway_Truck_Report_Data_102345234.zip
20121120011335_team1-pathway_Truck_Report_Data_10642224.zip
20121121011335_team1-pathway_Truck_Report_Data_133464.zip
20121122011335_team1-pathway_Truck_Report_Data_126434344.zip
20121123011335_team1-pathway_Truck_Report_Data_12444656.zip
20121124011335_team1-pathway_Truck_Report_Data_1624444.zip
20121125011335_team1-pathway_Truck_Report_Data_3464433.zip
randomefilewithnodate.zip
20121119011335_team2-Paper_Size_Report_336655.zip
20121120011335_team2-Paper_Size_Report_336677.zip
20121121011335_team2-Paper_Size_Report_338877.zip
20121122011335_team2-Paper_Size_Report_226688.zip
20121123011335_team2-Paper_Size_Report_776688.zip
20121124011335_team2-Paper_Size_Report_223355.zip
20121125011335_team2-Paper_Size_Report_111111.zip""".split("\n")

def extract_date(s):
    return datetime.strptime(s.split("_")[0], "%Y%m%d%H%M%S")

def starts_with_date(s):
    try:
        extract_date(s)
        return True
    except Exception:
        return False

def get_name_root(s):
    return "".join(s.split(".")[0].split("_")[1:-1])

def find_files_to_delete_for_group(group):
    sorted_group = sorted(group, key=extract_date)
    return sorted_group[:-5]

fn_groups = groupby(
                filter(
                    starts_with_date,
                    filenames),
                get_name_root
            )

fns_to_delete = chain(*[find_files_to_delete_for_group(g) for k, g in fn_groups])

print list(fns_to_delete)

rawDescription: python deleting older files that match an unknown pattern (tricky)

description: Python deleting older files that match an unknown pattern (tricky)


code: print zip(*[x, y])

rawDescription: concise and fast zip operation on lists of lists in Python?

description: Concise and fast zip operation on lists of lists in Python


code:
for dict_elem in map(operator.itemgetter('a'), dict_lst):
    print dict_elem

rawDescription: Refer to dictionary element in 'for' loop


code:
from collections import OrderedDict
dic = OrderedDict()

for k,v in dic.iteritems():
    print "/".join(v)

rawDescription: Python Function returns wrong value

description: Python Function returns wrong value


code:
if astring.find("can\'t")>-1:
    print "found it"
else:
    print "did not find it"

rawDescription: How do I search for a substring with a quotation mark inside a string in Python?

description: Search for a substring with a quotation mark inside a string in Python


code:
from xml.etree import ElementTree as et

def indent(elem, level=0):
    i = "\n" + level*"  "
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

m = et.Element(u'main')
s1 = et.SubElement(m,u'sub1')
s2 = et.SubElement(s1,u'sub2')
s2.text = u'马克'

print et.tostring(m,'utf-8')
indent(m)
print et.tostring(m,'utf-8')

rawDescription: Is there a way to remove xml header added by ElementTree.tostring method?


code:
final = [] # add values you want saved to final
uniq_ident = 1
for name in glob.glob('/Users/jorjis/Desktop/test/*'):
     jfile = open(name, 'r')
     values = json.load(jfile)
     jfile.close()
     body1 = values['article']['description']
     tokens = nltk.wordpunct_tokenize(body1)
     tokens = [w.lower() for w in tokens]
     vocab = [word for word in tokens if word not in stop]
     final.append([uniq_ident,vocab]) # append vocab or whatever values you want to keep
     uniq_ident += 1
     print body1

rawDescription: Get specific data from a .json file and save them to a 2D matrix/dictionary in python


code:
mycar = Car("white","Honda","Civic")
print mycar

rawDescription: Outputting object in Python using print

description: Outputting object in Python using print


code:
from urllib2 import urlopen
import re

contents = urlopen("http://www.radioreference.com/apps/audio/?ctid=5586").read()

playlist_pattern = r'link = "([^"]+)" \+ a.id \+ "([^"]+\.asx)'
feed_pattern = r'href="/apps/audio/\?feedId=(\d+)"><span class="px13">([^<]+)'
pattern = playlist_pattern + ".*" + feed_pattern

playlist, asx, feed, title = re.search(pattern, contents, re.DOTALL).groups()

streamURL = "http://www.radioreference.com" + playlist + feed + asx

print title
print streamURL

rawDescription: Manually extracting portions of strings contained in a list (parsing)


code:
import pandas as pd
import glob
import datetime as dt

# number of lines to skip before the first date
offset = 32

# number of lines from one date to the next
recordlength = 9

pattern = '1511??.mnd'

dates = []

for filename in glob.iglob(pattern):

    with open(filename) as datafile:

        count = -offset
        for line in datafile:
            if count == 0:
                fmt = '%Y-%m-%d %H:%M:%S %f'
                date_object = dt.datetime.strptime(line[:-6], fmt)
                dates.append(date_object)

            count += 1

            if count == recordlength:
                count = 0

data_nov15_hereford = pd.DataFrame(dates, columns=['Dates'])

print dates

rawDescription: Reading in specific date lines from a file with pandas python


code:
na = np.setdiff1d(np.arange(len(df_a)), ia)
nb = -1 * np.ones_like(na)
oa = np.concatenate((ia, na))
ob = np.concatenate((ib, nb))
print pd.concat([df_a.take(oa).reset_index(drop=True),
                 df_b.take(ob).reset_index(drop=True)], axis=1)

rawDescription: how to perform an inner or outer join of DataFrames with Pandas on non-simplistic criterion

description: Perform an inner or outer join of DataFrames with Pandas on non-simplistic criterion


code:
from bs4 import BeautifulSoup

html = """

    Text title here

        text link here

"""

soup = BeautifulSoup(html)

p = soup.find('h1', attrs={'class': 'titleClass'})
p.a.extract()
print p.text.strip()

rawDescription: How to find the text of a h1 tag html format with no link?

description: Find the text of a h1 tag html format with no link


code:
import xml.etree.ElementTree as ET
tree = ET.parse('rec.xml')
root = tree.getroot()
data = {}
child_list = []
def recursive_print(string,x):
    if x in data.keys():
     for x_child in data[x]:
        if x_child in data.keys():
          recursive_print(string+'-------->'+x_child,x_child)
        else:
         print string+'-------->'+x_child
    else:
       print string

for nodes in root.findall('node'):
    parent = nodes.attrib.get('name')
    child = nodes.attrib.get('child')
    child_list.append(child)
    if parent not in data.keys():
        data[parent] = []
    data[parent].append(child)
for key in data.keys():
    if key not in child_list:
      for x in data[key]:
        string = key+'------->'+x
        recursive_print(string,x)

rawDescription: Recursively search for parent child combinations and build tree in python and XML


code:
def get_choice(options):
    while True:
        print options
        choice = raw_input('> ').lower()
        for i, opt in enumerate(options):
            if opt.lower() in choice:
                 return i
        print "You need to select one of the given options."

rawDescription: While keeps asking for input. Not skipping to second function


code:
myA = A(1, 2)
print myA
print B(3,4,5)   # regular B
myB = B(myA, 10) # B created from an A
print myB

rawDescription: Python construct child class from parent


code:
from collections import OrderedDict

b = OrderedDict([('04-25', 5), ('04-23', 53), ('04-20', 3), ('04-2', 33)])
c = OrderedDict([('04-20', 5), ('03-18', 53), ('03-13', 3), ('03-12', 133)])
f_data = OrderedDict()
for key in b.keys() + c.keys():
    if not f_data.has_key(key):
        try:
            val_b = b[key]
        except KeyError:
            val_b = 0
        try:
            val_c = c[key]
        except KeyError:
            val_c = 0
        f_data[key] = (val_b, val_c)
print f_data

rawDescription: OrderedDict Merge and Append 0 if not

description: OrderedDict Merge and Append 0 if not


code:
import argparse
import ConfigParser
import sys

def main(argv=None):
    # Do argv default this way, as doing it in the functional
    # declaration sets it at compile time.
    if argv is None:
        argv = sys.argv

    # Parse any conf_file specification
    # We make this parser with add_help=False so that
    # it doesn't parse -h and print help.
    conf_parser = argparse.ArgumentParser(
        description=__doc__, # printed with -h/--help
        # Don't mess with format of description
        formatter_class=argparse.RawDescriptionHelpFormatter,
        # Turn off help, so we print all options in response to -h
        add_help=False
        )
    conf_parser.add_argument("-c", "--conf_file",
                        help="Specify config file", metavar="FILE")
    args, remaining_argv = conf_parser.parse_known_args()

    defaults = { "option":"default" }

    if args.conf_file:
        config = ConfigParser.SafeConfigParser()
        config.read([args.conf_file])
        defaults.update(dict(config.items("Defaults")))

    # Parse rest of arguments
    # Don't suppress add_help here so it will handle -h
    parser = argparse.ArgumentParser(
        # Inherit options from config_parser
        parents=[conf_parser]
        )
    parser.set_defaults(**defaults)
    parser.add_argument("--option")
    args = parser.parse_args(remaining_argv)
    print "Option is \"{}\"".format(args.option)
    return(0)

if __name__ == "__main__":
    sys.exit(main())

rawDescription: Which is the best way to allow configuration options be overridden at the command line in Python?


code:
import multiprocessing

def f(x):
    print multiprocessing.current_process()
    return x * x

p = multiprocessing.Pool()
print p.map(f, range(6))

rawDescription: Get a unique ID for worker in python multiprocessing pool


code:
for line in difflib.unified_diff(lines1, lines2, fromfile='file1', tofile='file2', lineterm='', n=0):
    for prefix in ('---', '+++', '@@'):
        if line.startswith(prefix):
            break
    else:
        print line

rawDescription: Compare two files report difference in python


code: print find_all(master, namecount)[3]

rawDescription: How to replace different blocks of text in all combinations using awk?

description: Replace different blocks of text in all combinations using awk


code:
import numpy, random, time

def main (num=1000000) :

    # Generate random points and numbers

    random.seed (0x7126434a2ea2a259e9f4196cbb343b1e6d4c2fc8)

    def random_point () :
        return [random.random(), random.random(), random.random()]

    def random_index () :
       return random.randint(0, num-1)

    pt_list = numpy.array([random_point() for i in xrange(num)])
    rand_pts = pt_list[[random_index() for i in xrange(num)],:]

    # Compute

    beg_time = time.clock()

    # Mondo slow.
    # dist = numpy.sum (
    #            numpy.apply_along_axis (
    #                numpy.linalg.norm, 1, pt_list - rand_pts))

    # Mondo fast.
    dist = numpy.sum ((numpy.sum ((pt_list-rand_pts)**2, axis=1))**0.5)

    end_time = time.clock()
    elap_time = (end_time - beg_time)

    print elap_time
    print dist

rawDescription: Why is computing point distances so slow in Python?


code:
import re
s = 'a{asdfgh{asf}xvc}defg}des}'
p = re.compile('(\{[\w|\W]+?\}.+?\})')
print p.findall(s)

rawDescription: Python nongreedy second match

description: Python nongreedy second match


code:
b_array = bytearray('yoyo')
for elem in b_array:
    print elem

rawDescription: Python string to bytearray and back

description: Python string to bytearray and back


code:
for key in rectangles:
    print rectangles[key]

rawDescription: Load rectangle data from txt file with Python?


code:
import numpy as np
import cv2

img = cv2.imread('cross.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
thresh2 = cv2.bitwise_not(thresh)

contours,hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, 1)

max_area = -1

# find contours with maximum area
for cnt in contours:
    approx = cv2.approxPolyDP(cnt, 0.02*cv2.arcLength(cnt,True), True)
    if len(approx) == 4:
        if cv2.contourArea(cnt) > max_area:
            max_area = cv2.contourArea(cnt)
            max_cnt = cnt
            max_approx = approx

# cut the crossword region, and resize it to a standard size of 130x130
x,y,w,h = cv2.boundingRect(max_cnt)
cross_rect = thresh2[y:y+h, x:x+w]
cross_rect = cv2.resize(cross_rect,(130,130))

# you need to uncomment these lines if your image is rotated
#new_pts = np.float32([[0,0], [0,129],[129,129],[129,0]])
#old_pts = max_approx.reshape(4,2).astype('float32')
#M = cv2.getPerspectiveTransform(old_pts,new_pts)
#cross_rect = cv2.warpPerspective(thresh2,M,(130,130))

cross = np.zeros((13,13))

# select each box, if number of white pixels is more than 50, it is white box
for i in xrange(13):
    for j in xrange(13):
        box = cross_rect[i*10:(i+1)*10, j*10:(j+1)*10]
        if cv2.countNonZero(box) > 50:
            cross.itemset((i,j),1)

print cross

rawDescription: Crossword digitization using image processing


code:
import json
sample = '''\
{
    "id": "694543830607034",
    "name": "Ankit Dhawan",
    "accounts": {
        "data": [
            {
                "access_token": "a",
                "category_list": [
                    {
                        "id": "192119584190796",
                        "name": "Event"
                    }
                ],
                "name": "Ignitron 2014",
                "id": "731808386867764",
                "perms": [
                    "ADMINISTER",
                    "EDIT_PROFILE",
                    "CREATE_CONTENT",
                    "MODERATE_CONTENT",
                    "CREATE_ADS",
                    "BASIC_ADMIN"
                ]
            }
        ]
    }
}
'''
dat = json.loads(sample)
for entry in dat['accounts']['data']:
    print entry['name']

rawDescription: parsing json data using request function in python.... i am unable to access the objects

description: Parsing json data using request function in python.... i am unable to access the objects


code:
import re

ts = ['w.lejournal.fr/actualite/politique/sarkozy-terminator_1557749.html',
    'w.lejournal.fr/palmares/palmares-immobilier/',
    'w.lejournal.fr/actualite/societe/adeline-hazan-devient-la-nouvelle-controleuse-des-lieux-de-privation-de-liberte_1558176.html']

rgx = r'(?<=w.lejournal.fr/)([aA-zZ]*)/([aA-zZ_-]*)(?=/)'

for url_address in ts:
    found_group = re.findall(rgx, url_address)
    for item in found_group:
        print item

rawDescription: pandas/python: splitting url and adding columns to a database

description: Pandas/python: splitting url and adding columns to a database


code:
from collections import Counter
state_count = ['Alabama','Alabama','Alabama','Alaska','Alaska']
m = Counter(state_count)
for i, j in m.items():
    print {'state':i, 'other_value':j}

rawDescription: How do I make a dictionary from a Python counter with the right keys?

description: Make a dictionary from a Python counter with the right keys


code:
names = collections.Counter()
rows = {}

with open(infile) as input_file:
    for row in csv.reader(input_file, delimiter='\t'):
        rows[row[0]] = row
        names[row[0]] += 1

for name, count in names.iteritems():
    print rows[name], count

rawDescription: count occurencies of a word

description: Count occurencies of a word


code:
for elem in allnext(comment):
    print elem

rawDescription: Split html document in pieces parsing html comments with BeautifulSoup


code:
import sys
import itertools

def getFirstDup(c, toTest):

    # Original idea using list slicing => 5.014 s
    if toTest == '1':
        for i in xrange(0, len(c)):
            if c[i] in c[:i]:
                return c[i]

    # Using two sets => 4.305 s
    elif toTest == '2':
        s = set()
        for i in c:
            s2 = s.copy()
            s.add(i)
            if len(s) == len(s2):
                return i

    # Using dictionary LUT => 0.763 s
    elif toTest == '3':
        d = {}
        for i in c:
            if i in d:
                return i
            else:
                d[i] = 1

    # Using set operations => 0.772 s
    elif toTest == '4':
        s = set()
        for i in c:
            if i in s:
                return i
            else:
                s.add(i)

    # Sorting then walking => 5.130 s
    elif toTest == '5':
        c = sorted(c)
        for i in xrange(1, len(c)):
            if c[i] == c[i - 1]:
                return c[i]

    # Sorting then groupby-ing => 5.086 s
    else:
        c = sorted(c)
        for k, g in itertools.groupby(c):
            if len(list(g)) > 1:
                return k

    return None

c = list(xrange(0, 10000000))
c[5000] = 0

for i in xrange(0, 10):
    print getFirstDup(c, sys.argv[1])

rawDescription: Python: find a duplicate in a container efficiently


code:
from collections import namedtuple
UrlTuple = namedtuple("UrlTuple", "url msg")
a = [UrlTuple(url="http://wwww.ff.com", msg="msg 1..."),
     UrlTuple(url="http://wwww.xx.com", msg="msg 2..."),
     UrlTuple(url="http://wwww.yy.com", msg="msg 3...")]
print a[0].url
# http://wwww.ff.com

rawDescription: How to do a multidimensional array in python?

description: Do a multidimensional array in python


code:
from functools import wraps

def print_on_entry(fn):
    @wraps(fn)
    def wrapper(*args):
        print "{}({})".format(fn.func_name, ", ".join(str(a) for a in args))
        fn(*args)
    return wrapper

rawDescription: How to print graph call as tree?

description: Print graph call as tree


code:
f = open('file.txt').readlines()
my_file = [ x for x in f if x!='\n' ]
for x in range(0,len(my_file)-1,2):
    start = my_file[x].split()[0][:-1]
    end = my_file[x+1].split()[0][:-1]
    print str(datetime.datetime.strptime(end,"%H:%M:%S")- datetime.datetime.strptime(start,"%H:%M:%S")).split(':')[-1]

rawDescription: Subtracting an integer value from a text file and displaying the result in Python2.7


code:
class LoggerMiddleware(object):
    '''WSGI middleware'''

    def __init__(self, application):

        self.app = application

    def __call__(self, environ, start_response):

        # write logs

        try:
            return self.app(environ, start_response)
        except Exception, e:
            # write logs
            pass
        finally:
            # write logs
            pass

rawDescription: How do I write a logging middleware for pyramid/pylons 2?

description: Write a logging middleware for pyramid/pylons 2


code:
import requests
response = requests.get('http://www.haodf.com')
response.request.headers
print response.text

rawDescription: Want to fetch information from a website but it shows "250 forbidden" with Python


code:
import sqlite3
database = sqlite3.connect("my.db")
db = database.cursor()
Player = "iScrE4m"
Played = 10
db.execute("SELECT * FROM players WHERE Name='%s' AND Played='%i'" % (Player, Played))
print db.fetchone()
Played = 8
db.execute("SELECT * FROM players WHERE Name='%s' AND Played='%i'" % (Player, Played))
print db.fetchone()

rawDescription: Login script using Python and SQLite

description: Login script using Python and SQLite


code:
from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
import math

def lognormDrive(mu,variance):
    size = 1000
    sigma = math.sqrt(variance)
    np.random.seed(1)
    gaussianData = stats.norm.rvs(loc=mu, scale=sigma, size=size)
    logData = np.exp(gaussianData)
    shape, loc, scale = stats.lognorm.fit(logData, floc=0)
    print "Estimated mu:", np.log(scale)
    print "Estimated var: ", shape**2
    logData.sort()
    return logData, stats.lognorm.pdf(logData, shape, loc, scale)

x, y = lognormDrive(37, 0.8)
plt.plot(x, y)
plt.grid()
plt.show()

rawDescription: Fitting and Plotting Lognormal

description: Fitting and Plotting Lognormal


code:
#!/usr/bin/env python

from lxml import etree, html
from urlparse import urljoin
import time

t = time.time()
base = 'http://scoutmob.com/'
main = html.parse(base)
cities = [x.split('?')[0] for x in main.xpath("//a[starts-with(@class, 'cities-')]/@href")]
urls = [urljoin(base, x + '/today') for x in cities]
docs = [html.parse(url) for url in urls]
feeds = [doc.xpath("//link[@rel='alternate']/@href")[0] for doc in docs]
# filter out the "coming soon" feeds
feeds = [x for x in feeds if x != 'http://feeds.feedburner.com/scoutmob']
print time.time() - t
print len(cities), cities
print len(feeds), feeds

t = time.time()
items = [etree.parse(x).xpath("//item") for x in feeds]
print time.time() - t
count = sum(map(len, items))
print count

rawDescription: Python creating worker queues

description: Python creating worker queues


code: print picture.getpixel((1, 1))

rawDescription: How to change pixel color in Python

description: Change pixel color in Python


code: print [list(i) for i in s.split('\n')]

rawDescription: Opening a file- How to make a .txt file return a list of lists


code:
my_table = {
    "x_time": 90*[0],
    "t_msg": 90*[0],
    "g_up": 90*[0],
}

with open("p.csv") as f:
    for line in f:
        r, d, p = line.split(',')
        rank = int(r)
        day = int(d)
        param = p.strip()
        my_table[param][day-1] = rank

print my_table

rawDescription: Reshaping a table in python

description: Reshaping a table in python


code:
from collections import Counter, defaultdict

hello = ["hello","hi","hello","hello"]
counter= Counter(hello)
bye = defaultdict(int)
print"
"
for word in counter.keys():
    print "
"
    print "" + str(word) + ":" + str(counter[word]) + "	"
    print "
"
    bye[word] = counter[word]
print"
"

rawDescription: How to change list into HTML table ? (Python)

description: Change list into HTML table ? (Python)


code:
import random

items = range(10)

results = []

while len(results) < 5:
    number = random.sample(items, 3)
    if number not in results:
        results.append(number)

print results

[[2, 6, 3], [8, 0, 4], [0, 3, 4], [7, 8, 6], [2, 6, 9]]

rawDescription: First Python program, "lotto prediction"


code:
class Item(object):
    def __init__(self, unq_id, name, price, qty):
        self.unq_id = unq_id
        self.product_name = name
        self.price = price
        self.qty = qty

class Cart(object):
    def __init__(self):
        self.content = dict()

    def update(self, item):
        if item.unq_id not in self.content:
            self.content.update({item.unq_id: item})
            return
        for k, v in self.content.get(item.unq_id).iteritems():
            if k == 'unq_id':
                continue
            elif k == 'qty':
                total_qty = v.qty + item.qty
                if total_qty:
                    v.qty = total_qty
                    continue
                self.remove_item(k)
            else:
                v[k] = item[k]

    def get_total(self):
        return sum([v.price * v.qty for _, v in self.content.iteritems()])

    def get_num_items(self):
        return sum([v.qty for _, v in self.content.iteritems()])

    def remove_item(self, key):
        self.content.pop(key)

if __name__ == '__main__':
    item1 = Item(1, "Banana", 1., 1)
    item2 = Item(2, "Eggs", 1., 2)
    item3 = Item(3, "Donut", 1., 5)
    cart = Cart()
    cart.update(item1)
    cart.update(item2)
    cart.update(item3)
    print "You have %i items in your cart for a total of $%.02f" % (cart.get_num_items(), cart.get_total())
    cart.remove_item(1)
    print "You have %i items in your cart for a total of $%.02f" % (cart.get_num_items(), cart.get_total())

rawDescription: Python shopping cart add to cart, get total get num items


code:
import xml.etree.ElementTree as ET

doc = ET.parse(path_to_xml_file)
values = doc.findall('.//*[@Value]')
print [value.get('Value') for value in values]

rawDescription: Parsing xml using import minidom with if conditions


code:
class MyTime(object):
    def __init__(self, year, month, mday, hour, minute):
        self.data =  year, month, mday, hour, minute
    def __getitem__(self, idx):
        return self.data[idx]
    def __str__(self):
        return "MyTime(tm_year=%s, tm_mon=%s, tm_mday=%s, tm_hour=%s, tm_min=%s)"%self.data

x=MyTime(2010,2,7,14,46)
x[0]
x[1]
print x

rawDescription: time.localtime() - how does it work? Brief questions on how to use it too - super easy stuff!

description: Time.localtime() - how does it work? Brief questions on how to use it too - super easy stuff!


code:
def f(**kwargs): print kwargs

d1 = { u'foo': u'bar' }
d2 = { 'foo': u'bar' }
f(**d1)
f(**d2)

rawDescription: Why is Django reverse() failing with unicode?


code:
import ctypes
import os
import sys
import threading

print 'Start'

liba = ctypes.cdll.LoadLibrary('libtest.dylib')

# Create pipe and dup2() the write end of it on top of stdout, saving a copy
# of the old stdout
stdout_fileno = sys.stdout.fileno()
stdout_save = os.dup(stdout_fileno)
stdout_pipe = os.pipe()
os.dup2(stdout_pipe[1], stdout_fileno)
os.close(stdout_pipe[1])

captured_stdout = ''
def drain_pipe():
    global captured_stdout
    while True:
        data = os.read(stdout_pipe[0], 1024)
        if not data:
            break
        captured_stdout += data

t = threading.Thread(target=drain_pipe)
t.start()

liba.hello()  # Call into the shared library

# Close the write end of the pipe to unblock the reader thread and trigger it
# to exit
os.close(stdout_fileno)
t.join()

# Clean up the pipe and restore the original stdout
os.close(stdout_pipe[0])
os.dup2(stdout_save, stdout_fileno)
os.close(stdout_save)

print 'Captured stdout:\n%s' % captured_stdout

rawDescription: In python, how to capture the stdout from a c++ shared library to a variable


code:
with open('templ.txt') as templ, open('in.txt') as f:
    items = [i.strip().split('=')[0] for i in templ if '=?' in i]
    d = dict(i.strip().split('=') for i in f)
    print [(i,d[i]) for i in items if i in d]

rawDescription: Python how to match files to templates

description: Python how to match files to templates


code:
from ast import literal_eval

file_info = ['{"file":"file1", "directory":"dir1"}', '{"file":"file2", "directory":"directory2"}']

for item in file_info:
    item = literal_eval(item)
    print item['file'], item['directory']

rawDescription: Extract values from within strings in a list - python


code:
result = {u'kind': u'bigquery#queryResponse', u'rows': [{u'f': [{u'v': u'jobs'}, {u'v': u'46'}]},      {u'f': [{u'v': u'employment'}, {u'v': u'24'}]}, {u'f': [{u'v': u'Employment'}, {u'v': u'20'}]}, {u'f': [{u'v': u'donate furniture'}, {u'v': u'16'}]}, {u'f': [{u'v': u'feeding the homeless'}, {u'v': u'16'}]}, {u'f': [{u'v': u'Perth stores'}, {u'v': u'14'}]}, {u'f': [{u'v': u'bed'}, {u'v': u'14'}]}, {u'f': [{u'v': u'Jobs'}, {u'v': u'14'}]}, {u'f': [{u'v': u'food parcels'}, {u'v': u'14'}]}, {u'f': [{u'v': u'history'}, {u'v': u'14'}]}, {u'f': [{u'v': u'Adherent'}, {u'v': u'12'}]}, {u'f': [{u'v': u'volunteer'}, {u'v': u'12'}]}, {u'f': [{u'v': u'Historical society'}, {u'v': u'12'}]}, {u'f': [{u'v': u'Donate furniture'}, {u'v': u'12'}]}, {u'f': [{u'v': u'pick up'}, {u'v': u'12'}]}, {u'f': [{u'v': u'Careers'}, {u'v': u'12'}]}, {u'f': [{u'v': u'contact'}, {u'v': u'12'}]}, {u'f': [{u'v': u'helping with a funeral'}, {u'v': u'10'}]}, {u'f': [{u'v': u'Basketball'}, {u'v': u'10'}]}, {u'f': [{u'v': u'email'}, {u'v': u'10'}]}], u'jobReference': {u'projectId': u'2323', u'jobId': u'2323'}, u'jobComplete': True, u'totalRows': u'20', u'totalBytesProcessed': u'0', u'cacheHit': True, u'schema': {u'fields': [{u'type': u'STRING', u'name': u'Query', u'mode': u'NULLABLE'}, {u'type': u'INTEGER', u'name': u'Count', u'mode': u'NULLABLE'}]}}
for row in result['rows']:
    job, count = (col['v'] for col in row['f'])
    print job, count

rawDescription: parse through JSON

description: Parse through JSON


code:
import re

from bs4 import BeautifulSoup

data = """

    something new
    $#something new,.
    thisSomething news
    Somethingnew

"""

soup = BeautifulSoup(data)
for item in soup.find_all(text=re.compile("something new")):
    print item

rawDescription: get specific text such as "Something new" using BeautifulSoup Python

description: Get specific text such as "Something new" using BeautifulSoup Python


code:
print re.findall(
    r"""(?ix)             # case-insensitive, verbose regex
    \b                    # match a word boundary
    (?:                   # match the following three times:
     (?:                  # either
      \d+                 # a number,
      (?:\.|st|nd|rd|th)* # followed by a dot, st, nd, rd, or th (optional)
      |                   # or a month name
      (?:(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*)
     )
     [\s./-]*             # followed by a date separator or whitespace (optional)
    ){3}                  # do this three times
    \b """,
    text)

rawDescription: Python NLTK interpret a fixed pattern of sentence and tokenize it


code:
# open the file
with open('c:\\file.txt') as f:
    # read all lines as list
    lines = f.readlines()
# iterate over the list
for line in lines:
    # if any of the the strings in the list is in the line print it (using list comprehensions)
    if any(s in line for s in ['fbchat', 'fb_timeline', 'Facebook Wall Post']):
        # print but first remove new line character
        print line.strip("\n")

rawDescription: python/perl script for text extraction

description: Python/perl script for text extraction


code:
def test():
    value=input("Value here!") #eval is bad AND dangerous practice. Avoid using it.
    with open ("word-text.txt","r") as f:
        #At this point you can do one of two things:
        #1) read the entire file line by line
        #2) read the entire file into a list, and loop/iterate over that list
        #In this example, I'll read the file line by line:
        nice_strings = []
        for line in f:
            line = line.strip() #remove any extraneous whitespace from the line (e.g. spaces and newlines)
            nice_str_chars = []
            #At this point, you want to read EACH character in the line
            #N.B.: ord is the name of a built-in function. Use another name like `char` (below):
            for char in line:
                #Since you're in this loop, you are dealing with EACH individual character of each line
                #And, as you want to "shift" the character by some number of places
                #Python doesn't allow you to natively shift strings by arbitrary amounts,
                #so, the only way we can do so is to convert EACH character into its numeric representation.
                #In order to do so, you have to use the `ord` function which converts characters into
                #their respective integer representation.
                int_of_char = ord(char)
                #At this point, the character has been converted to it's integer equivalent
                #NOW, you can "shift" it. In other words, add the value to the `int_of_char`:
                int_of_char += value #update the value of the variable
                #At this point, the value has been modified, and the INTEGER value of the character has been
                #shifted. However, we don't want the shifted INTEGER value: we want the shifted CHARACTER value.
                #So, how do we convert integers to characters? Python has a builtin function called `chr` which
                #does exactly that.
                #So, let's convert the shifted INTEGER to a shifted CHARACTER using `chr`:
                nice_char = chr(int_of_char)
                #print it out the original and modified characters so you can see what's in the string (this step is optional)
                print char, '=>', nice_char
                #At this point we've successfully shifted the character. However, since this is just A character of the original
                #string, we need a way to save the updated characters as a string. There are a number of ways to do this.
                #For simplicity, we can use a list `nice_str_chars`:
                nice_str_chars.append(nice_char)
            #At this point we've looped over all the characters in ONE LINE, and we've calculated and stored the shifted
            #characters for that same line. However, since we've stored the characters in a list: `nice_str_chars`, we
            #have to convert it to a string. We can do that using the `join` method on an empty string, with
            #`nice_str_chars` as the list (as we want to join the characters of the list into a string):
            nice_str = ''.join(nice_str_chars)
            #And, since you want to shift ALL the lines, you ought to append the corrected line to a list that can later be
            #returned:
            nice_strings.append(nice_str)
            #you can print it out if necessary:
            print line, '=>', nice_str, '\n'
        #At this point, you've successfully looped over the entire file, shifted all lines by a user entered integer amount,
        #now, all you have to do is return it, or do something else with them (i.e. write the updated strings to another file).
        #That part, I'll leave up to you.
        return nice_strings

rawDescription: shifting letters using ord and chr

description: Shifting letters using ord and chr


code:
for group in grouper(paths):
    print group

rawDescription: Group Python list of lists into groups based on overlapping items


rawDescription: python if a list is over 20 characters shorten it to 20 if it is less than 20 characters, add 0s to make it 20

description: Python if a list is over 20 characters shorten it to 20 if it is less than 20 characters, add 0s to make it 20


code:
from google.appengine.ext import db

model_class = db.class_for_kind("my_model")     # this method is not published
query = model_class.all()
print query.count()

rawDescription: How to query the datastore by kind name as string using metadata

description: Query the datastore by kind name as string using metadata


code:
for i in xrange(totalCells):
    print "AAAA"
    cellValue = "Long string in here....."
    if sh.cell_value(rowx=i, colx=i) == cellValue:
        print "BBBB"
        break

rawDescription: Increment function argument variable [Python]


code:
from dateutil.rrule import *
import datetime
r = rrule(WEEKLY, byweekday=SA, count=10, dtstart=datetime.datetime(2008,10,01))
print list(r)
r._dtstart = r[-1]
print list(r)

rawDescription: using existing rrule to generate a further set of occurrences

description: Using existing rrule to generate a further set of occurrences


code:
@curry
def join_three(a,b,c):
    return '%s-%s-%s' % (a,b,c)

mols = [11,22,33]
print map(join_three('aa', 'bb'), mols)
# prints ['aa-bb-11', 'aa-bb-22', 'aa-bb-33']

rawDescription: Applying function to iterable in python?

description: Applying function to iterable in python


code:
from bs4 import BeautifulSoup

html = """

        WordA

Something

Something

        WordB

Something

Something

"""
soup = BeautifulSoup(html)
for a in soup.select('p > a'):
    print a.text

    for li in a.parent.find_next_sibling('ul').find_all('li'):
        print li.text
    print "-----"

rawDescription: How to find lists after a specific word

description: Find lists after a specific word


code:
from collections import OrderedDict
dict = {}
fp = open('test.txt')
for line in fp.readlines():
    line = line.split(" ")
    dict[line[0]] = line[1].replace('\n','')
fp.close()
dict = OrderedDict(sorted(dict.items(), key=lambda t: t[0]))
for key in dict:
    if dict.has_key(dict[key]):
        print key,dict[key],dict[dict[key]]
    else:
        print key,dict[key]

rawDescription: Recursive key value in a file

description: Recursive key value in a file


code:
z_counts = []
lines = []

with open('C://foo.csv', 'r') as f:
    lines = f.readlines() #This is your list of all the rows/lines (now populated with data)
    for index, line in enumerate(lines):
        #store the z counts and the index of the line as pairs using a tuple: `( ... )`
        z_counts.append((line.strip().split('z').count('z'), index))

    #Since you want the top 5 matches
    #Reverse is necessary since Python automatically sorts in ascending order
    print sorted(z_counts, reverse=True)[:5]

rawDescription: python: How to compare values in a list by row?

description: Python: How to compare values in a list by row


code:
def _tree_edges(n,r):
    # helper function for trees
    # yields edges in rooted tree at 0 with n nodes and branching ratio r
    nodes=iter(range(n))
    parents=[next(nodes)] # stack of max length r
    while parents:
        source=parents.pop(0)
        for i in range(r):
            try:
                target=next(nodes)
                parents.append(target)
                yield source,target
            except StopIteration:
                break

print list(_tree_edges(13,3))
#[(0, 1), (0, 2), (0, 3), (1, 4), (1, 5), (1, 6), (2, 7), (2, 8), (2, 9), (3, 10), (3, 11), (3, 12)]
import networkx as nx
G = nx.Graph(_tree_edges(13,3))

rawDescription: Faster way to build a tree graph using Networkx Python?


code:
def digitClicked(self):
    sender = self.sender()
    self.sigTextButtonClicked.emit(sender.text())
    print sender.text()

rawDescription: How to show the number pressed on a virtual keyboard in a QTextEdit box PYQT

description: Show the number pressed on a virtual keyboard in a QTextEdit box PYQT


code:
myset = set(["first", "second", "third", "fourth", "fifth"])
for i in myset:
    print i

rawDescription: How do I loop a set of strings in reverse in Python?

description: Loop a set of strings in reverse in Python


code:
import numpy as np

def f(k):
    return(np.log(k**2))

N=8
fsum = 0
for k in range(1, N+1):
    fsum += f(k)

print fsum

rawDescription: Filling a numpy array using an arbitrary function


code:
hasmoustache={'jon':[False,'Male'],'sam':[False,'Male'],'sarah':[False,'Female']}
barber={'jon':1,'sam':8,'sarah':10}

if barber.get("sam")  > 4:
   hasmoustache.get("sam")[0]=  True
print hasmoustache
{'sarah': [False, 'Female'], 'sam': [True, 'Male'], 'jon': [False, 'Male']}

rawDescription: Dynamic list of booleans python

description: Dynamic list of booleans python


code:
from multiprocessing import Process

def func1():
  print 'func1: starting'
  for i in xrange(10000000): pass
  print 'func1: finishing'

def func2():
  print 'func2: starting'
  for i in xrange(10000000): pass
  print 'func2: finishing'

if __name__ == '__main__':
  p1 = Process(target=func1)
  p1.start()
  p2 = Process(target=func2)
  p2.start()
  p1.join()
  p2.join()

rawDescription: Python: How can I run python functions in parallel?


rawDescription: Order data coming to my Python console from another computer using sockets


code:
from passlib.hash import md5_crypt
hash = md5_crypt.encrypt("test",salt="VFvON1xK")
print hash

rawDescription: python crypt in OSX

description: Python crypt in OSX


code: print s[s==True].index[-1]

rawDescription: Find index of last true value in pandas Series or DataFrame


code:
print m32.compile
['gcc', '-O', '-Wall']

rawDescription: Get the commands distutils passes to the compiler


code:
from sqlalchemy.schema import CreateTable

print CreateTable(table)

rawDescription: SQLAlchemy printing raw SQL from create()


code:
dna='abcde'
sequence=' 0..2, 4..5, 4..5'
import re
b=re.finditer(r'([0-9]+)\.\.([0-9]+)', sequence)
for j in b:
   print dna[int(j.group(1)) : int(j.group(2))]

rawDescription: Extract letters from a string using the position


code: print 'My name is {}\nMy job is {}\nI am {}'.format(*info)

rawDescription: How do you extract words in a list in python

description: Extract words in a list in python


code:
def spanningsets(items):
    if not items: return
    if len(items) == 1:
        yield [[items[-1]]]
    else:
        for cc in spanningsets(items[:-1]):
            yield cc + [[items[-1]]]
            for i in range(len(cc)):
                yield cc[:i] + [cc[i] + [items[-1]]] + cc[i+1:]

for sset in spanningsets([1, 2, 3, 4]):
    print ' '.join(map(str, sset))

rawDescription: Algorithm to generate spanning set

description: Algorithm to generate spanning set


code:
import re
print re.findall(r'(?<=\n\n\*)[^*]*(?=\*)','\n\n*text here, can be any spaces, etc. etc.*')

rawDescription: Match string between special characters

description: Match string between special characters


code:
size = 5

grid = [[1 for i in range(size)] for j in range(size)]
for i in range(1, size):
    for j in range(1, size):
        grid[i][j] = grid[i-1][j] + grid[i][j-1]

print grid

rawDescription: python: list comp fill data conditionally

description: Python: list comp fill data conditionally


code:
table = soup.find('table', class_='result-data')
tr = table.find('tr', class_='result-item')
print tr.find('td', class_="result-value-bold").get_text(strip=True)

rawDescription: Navigating to second string text using BeautifulSoup


code:
with open("f1") as f1:
    with open("f2") as f2:
       if f1.read().strip() in f2.read():
             print 'found'

rawDescription: Comparing some data of a file with another file in python


code:
for my_content in tree.find('maintag/content').itertext():
    print my_content
' '.join(tree.find('maintag/content').itertext())
' '.join(t.strip() for t in tree.find('maintag/content').itertext())

rawDescription: lxml Xml Parsing

description: Lxml Xml Parsing


rawDescription: Looping over multiple lists in Django Template


code:
mn, mx, span = -50., 50., 200.
ix = ((np.array([x0, y0, z0])-mn)*span/(mx-mn)).astype(np.int)

print ix
#  [191  85 105]    # similar to above.. but a bit different due to rounding/indexing issues which I don't want to think through right now..

rawDescription: How to set the origin of a volumetric spot knowing its coordinates origin?

description: Set the origin of a volumetric spot knowing its coordinates origin


code:
class Fred:
    def __eq__(self, other):
        return True
    def __ne__(self, other):
        return True
x = Fred()
print x == 23
print x != 23

rawDescription: No implied relationship among the comparison operators


code:
from itertools import groupby

with open('auth.log') as myAuthlog:
    myAuthlog = (line for line in myAuthlog if "Failed password for" in line)
    for key, group in groupby(myAuthlog, key = lambda x: x[:9]):
        month, day, hour = key[0:3], key[4:6], key[7:9]
        print "%s:00 %s-%s: %d"%(hour, day, month, len(list(group)))

rawDescription: Python Count per hour log file

description: Python Count per hour log file


code:
import random
import time

def run(n):
    r = random.SystemRandom()
    for i in xrange(n):
        if i%30000 == 0: print i
        bin(r.getrandbits(2000))[2:].zfill(2000)

s = time.time()
run(300000)
e = time.time()
print "Took %.2fs" % (e-s,)

rawDescription: Multiplying a huge number times random() (Python)


code: print name[len(name) - 1]

rawDescription: How do I slice a sequence to get the last item?

description: Slice a sequence to get the last item


code:
while True:
    if len(numbers) == 501:
        print number
        print count
        break
    numbers = triangle_numbers(count)
    count += 1
    # print every 500 loops
    if count % 500 == 0:
         print count

rawDescription: Make sure python script is still running


code:
class Foo(models.Model):
    name = models.CharField(max_length=128)

Foo.objects.create(name='a10')
Foo.objects.create(name='a1')
Foo.objects.create(name='a2')

ordered = sorted(Foo.objects.all(), key=lambda n: (n[0], int(n[1:])))
print ordered # yields a1, a2, 10

rawDescription: Custom ordering in Django

description: Custom ordering in Django


code:
for i in range(2):
    print fmt % ((i, ) + tuple(A[i]))

rawDescription: Elegant way to print formatted list together with index values in python?


code:
import os
import glob
import datetime,time #as required
os.chdir("/mypath")
for files in glob.glob("*.doc"):
    newfilename = files[-12:]
    # here to check date if desired
    try:
       os.rename(files,newfilename)
    except OSError,e:
       print e
    else: print "ok"

rawDescription: rename files with python - regex

description: Rename files with python - regex


code:
a = list()
print dir(a.__class__)

rawDescription: See class methods in Python console

description: See class methods in Python console


code:
class StrangeList(list):
    def __getitem__(self, idx):
        self[idx] = 23
        return 23

def a():
    x = StrangeList([1,2,3])
    print x

def b():
    x = StrangeList([1,2,3])
    obj = x[0]
    print x

print "Calling a"
a()
print "Calling b"
b()

rawDescription: Confused over variable necessity

description: Confused over variable necessity


code:
fo = open("output.txt", "r+")
str = fo.readline()
str = str[7:11]
print "Read String is : ", str
fo.close()

rawDescription: How to read specific word of a text file in Python

description: Read specific word of a text file in Python


code:
if not latest:
    print "I could not find data about your version [%s]" %module_name
    print "Are you ok?"
elif not current:
    print "I could not determine your version [%s]" %module_name
    print "Are you ok?"
    print "Latest stable ETE version is", latest
elif latest > current:
    print "You look a bit old."
    print "A newer version is available: rev%s" %latest
    print "Use 'easy_install -U %s' to upgrade" %module_name
else:
    print "I see you are in shape."
    print "No updates are available."

rawDescription: ETE2 .phonehome() Is it an Easter Egg?

description: ETE2 .phonehome() Is it an Easter Egg


code:
import colorsys

def pseudocolor(val, minval, maxval):
    # convert val in range minval..maxval to the range 0..120 degrees which
    # correspond to the colors red..green in the HSV colorspace
    h = (float(val-minval) / (maxval-minval)) * 120
    # convert hsv color (h,1,1) to its rgb equivalent
    # note: the hsv_to_rgb() function expects h to be in the range 0..1 not 0..360
    r, g, b = colorsys.hsv_to_rgb(h/360, 1., 1.)
    return r, g, b

if __name__ == '__main__':
    steps = 10
    print 'val       R      G      B'
    for val in xrange(0, 100+steps, steps):
        print '%3d -> (%.3f, %.3f, %.3f)' % ((val,) + pseudocolor(val, 0, 100))

rawDescription: Range values to pseudocolor

description: Range values to pseudocolor


code:
s = Seq(range(0, 100))
print s.head(10).odd().even().reverse()

rawDescription: chain the function as shell pipe command in python

description: Chain the function as shell pipe command in python


code:
with open('textfile.txt') as f:
    list = []
    for line in f:
        line = line.split()
        if line:
            list += [int(i) for i in line]

    print " ".join([str(i) for i in list])

rawDescription: Change the display of a list took from text file


code:
import binascii
mytext='☺00AB12☻0200D60000040001♥t'
print binascii.hexlify(mytext)

rawDescription: Convert string to HEX characters

description: Convert string to HEX characters


rawDescription: Parsing date from OCR response in Python


code:
import collections
import subprocess
import time
import threading

def read_output(process, append):
    for line in iter(process.stdout.readline, ""):
        append(line)

def main():
    # start process, redirect stdout
    process = subprocess.Popen(["top"], stdout=subprocess.PIPE, close_fds=True)
    try:
        # save last `number_of_lines` lines of the process output
        number_of_lines = 200
        q = collections.deque(maxlen=number_of_lines) # atomic .append()
        t = threading.Thread(target=read_output, args=(process, q.append))
        t.daemon = True
        t.start()

        #
        time.sleep(2)
    finally:
        process.terminate() #NOTE: it doesn't ensure the process termination

    # print saved lines
    print ''.join(q)

if __name__=="__main__":
    main()

rawDescription: Stop reading process output in Python without hang?


code:
import timeit

# setup stat_parser
from stat_parser import Parser
parser = Parser()

# setup bllip
from bllipparser import RerankingParser
from bllipparser.ModelFetcher import download_and_install_model
# download model (only needs to be done once)
model_dir = download_and_install_model('WSJ', '/tmp/models')
# Loading the model is slow, but only needs to be done once
rrp = RerankingParser.from_unified_model_dir(model_dir)

sentence = "In linguistics, grammar is the set of structural rules governing the composition of clauses, phrases, and words in any given natural language."

if __name__=='__main__':
    from timeit import Timer
    t_bllip = Timer(lambda: rrp.parse(sentence))
    t_stat = Timer(lambda: parser.parse(sentence))
    print "bllip", t_bllip.timeit(number=5)
    print "stat", t_stat.timeit(number=5)

rawDescription: Quick NLTK parse into syntax tree

description: Quick NLTK parse into syntax tree


code:
import argparse_temp as argparse

lst = [1,2,3]
parser = argparse.ArgumentParser()
parser.add_argument('-l',default=lst,action='append')
print parser.parse_args()
print lst

rawDescription: Please explain reasoning behind code snippet from argparse module


code:
import chardet

text = '<2011315123.04C6DACE618A7C2763810@\x82\xb1\x82\xea\x82\xa9\x82\xe7' + \
    '\x8c\xa9\x82\xa6\x82\xe9\x82\xbe\x82\xeb\x82\xa4>'
cset = chardet.detect(text)
print cset
encoding = cset['encoding']
print encoding, text.decode(encoding)

rawDescription: In python how do you deal with other encodings in domain names


code:
import csv
from itertools import izip_longest

with open('/tmp/line.csv','r') as fin:
    cr=csv.reader(fin)
    n=10
    data=izip_longest(*[iter(list(cr)[0])]*n,fillvalue='')
    print '\n'.join(', '.join(t) for t in data)

rawDescription: How to create a new list or new line after a certain number of iterations

description: Create a new list or new line after a certain number of iterations


code:
AddActivity("Jumping Jacks")
time.sleep(10)
AddActivity("Push Ups")

import datetime
today = datetime.datetime.now()
activities = GetAllActivitiesOnDate(today.month,today.day,today.year)
print "Found %d Entries"%len(activities)
for activity in activities:
   print "Activity %s @ %s"%(activity[1],time.strftime("%x %X",
                                                 time.gmtime(int(activity[0]))))

rawDescription: A good way to store date-based data using python?


code:
for key in data:
    print key

rawDescription: Python remove or replace key

description: Python remove or replace key


code: print pubdate('pubDate')

rawDescription: Generating a British date/time pubDate for an RSS in Python


code:
import numpy as np

# make fake data:
np.random.seed(5)

qi = np.random.rand(551)
N = np.random.randint(0,18,18)#np.arange(18)
M = np.random.randint(0,18,18)#np.arange(18)
I = np.arange(551)
Iq = np.random.rand(1, 551, 3)
U = np.random.rand(1, 551, 3)

B = np.zeros((18, 551))
C = np.zeros((18, 18))
Y = np.zeros((18))
Bv = np.zeros((18, 551))
Cv = np.zeros((18, 18))
Yv = np.zeros((18))

dmaxi = 1.

def B1(n, i):
    return np.pi * n * dmaxi * (-1)**(n+1) * np.sin(qi[i]*dmaxi) * ((np.pi*n)**2 - (qi[i]*dmaxi)**2)**(-1)

for n in N:
    B[n, :] = B1(n, I)

for m in M:
    for n in N:
        C[m, n] = np.dot((1/np.square(qi*Iq[0, :, 2]))*B[m, :], B[n, :])
    Y[m] = np.dot((1/np.square(qi*Iq[0, :, 2]))*U[0, :, 1], B[m, :])

Bv[N] = B1(N[:, None], I)
print "B correct?", np.allclose(Bv, B)

# np.einsum test case:
n, m = 2, 3
a = np.arange(n*m).reshape(n,m)*8 + 2
b = np.arange(n*m)[::-1].reshape(n,m)
c = np.empty((n,n))
for i in range(n):
    for j in range(n):
        c[i,j] = np.dot(a[i],b[j])
cv = np.einsum('ij,kj->ik', a, b)
print "einsum test successful?", np.allclose(c,cv)

Cv[M[:, None], N] = np.einsum('ij,kj->ik',
        B[M]/np.square(qi*Iq[0, :, 2]),
        B[N])
print "C correct?", np.allclose(Cv, C)

Yv[M] = np.einsum('i, ki->k',
        U[0, :, 1]/np.square(qi*Iq[0, :, 2]),
        B[M])
print "Y correct?", np.allclose(Yv, Y)

rawDescription: How to vectorize multiple levels of recursion?

description: Vectorize multiple levels of recursion


code:
class PluginMount(type):
    def __init__(cls, name, bases, attrs):
        if not hasattr(cls, 'plugins'):
            cls.plugins = []
        else:
            cls.plugins.append(cls)

class PluginBase(object):
    __metaclass__ = PluginMount
    i = 0
    def __init__(self):
        self.i = self.__class__.i
        print "Hi, I'm instance #{} of {}!".format(self.i, self.__class__.__name__)
        self.__class__.i += 1
    def see_class(self, name):
        print "Can {} be seen by {}:{}?  {}!".format(name, self.__class__.__name__, self.i, 'Yes' if name in (cls.__name__ for cls in self.__class__.plugins) else 'No')
    def create_inst(self, name):
        for cls in self.__class__.plugins:
            if name == cls.__name__:
                return cls()
        return None

rawDescription: least burdensome technique for package submodule to see all classes of importer

description: Least burdensome technique for package submodule to see all classes of importer


rawDescription: Python regex format

description: Python regex format


code:
width = 20
args = ("Python", "Very Good")
padded_args = zip([width] * len(args), args)
# Flatten the padded argument list.
print "%*s : %*s" % tuple([item for list in padded_args for item in list])

rawDescription: Format string dynamically

description: Format string dynamically


code:
print "Max speed:", max(all_combinations, key=lambda c:c.stats.speed)
print "Max weight:", max(all_combinations, key=lambda c:c.stats.weight)
print "Max acceleration:", max(all_combinations, key=lambda c:c.stats.acceleration)
print "Max handling:", max(all_combinations, key=lambda c:c.stats.handling)
print "Max drift:", max(all_combinations, key=lambda c:c.stats.drift)
print "Max offroad:", max(all_combinations, key=lambda c:c.stats.offroad)
print "Max turbo:", max(all_combinations, key=lambda c:c.stats.turbo)
print
print "Min speed:", min(all_combinations, key=lambda c:c.stats.speed)
print "Min weight:", min(all_combinations, key=lambda c:c.stats.weight)
print "Min acceleration:", min(all_combinations, key=lambda c:c.stats.acceleration)
print "Min handling:", min(all_combinations, key=lambda c:c.stats.handling)
print "Min drift:", min(all_combinations, key=lambda c:c.stats.drift)
print "Min offroad:", min(all_combinations, key=lambda c:c.stats.offroad)
print "Min turbo:", min(all_combinations, key=lambda c:c.stats.turbo)

rawDescription: What is an elegant way to solve this max and min problem in Ruby or Python?


code:
di = {
    '2015-03-03': [{'traffic': 80.0, 'Server': 'Server1'},
                   {'traffic': 20.0, 'Server': 'Server2'}],
    '2015-03-04': [{'traffic': 100.0, 'Server': 'Server1'}]
}

dates = sorted(di.keys())                                       # Get all dates
servers = set(e['Server'] for v in di.values() for e in v)      # Get all servers

# Refactor the dictionary
df = {}
for (k,v) in di.iteritems():
    df[k] = {}
    for elem in v:
        df[k][elem['Server']] = elem['traffic']

# Compute series (if the [date][server] key-chain doesn't exist in df, use 0)
series = []
for server in servers:
    traffic = []
    for date in dates:
        try:    traffic.append(df[date][server])
        except: traffic.append(0)
    series.append({'traffic':traffic, 'Server':server})

# Create output dict
do = {
    'Series': series,
    'dates': dates,
}

print do

rawDescription: How to convert this data structure in python

description: Convert this data structure in python


code:
with open('titles_500subset.txt') as searchfile:
    for line in searchfile:
        words = line.lower().split()
        if 'dog' in words[1:]:
            print words[words.index('dog')-1]

rawDescription: In python, how do I search for only one word before my selected substring


code:
matrix = [
   ["Ah!",  "We do have some Camembert", "sir"],
   ["It's a bit", "runny", "sir"],
   ["Well,",  "as a matter of fact it's", "very runny, sir"],
   ["I think it's runnier",  "than you",  "like it, sir"]
]

s = [[str(e) for e in row] for row in matrix]
lens = [max(map(len, col)) for col in zip(*s)]
fmt = '\t'.join('{{:{}}}'.format(x) for x in lens)
table = [fmt.format(*row) for row in s]
print '\n'.join(table)

rawDescription: Pretty print 2D Python list

description: Pretty print 2D Python list


code:
import sys

print "How old are you?"
sys.stdout.flush()
age = raw_input()
print "How tall are you?"
sys.stdout.flush()
height = raw_input()
print "How much do you weigh?"
sys.stdout.flush()
weight = raw_input()

print "So, you're %r old, %r tall and %r heavy." % (age, height, weight)

rawDescription: Cmd and Git bash have a different result when run a Python code


code:
from selenium import webdriver

def print_header(element):
    table = element.find_element_by_id('fs-table')
    for row in table.find_elements_by_tag_name('th'):
        print row.text

driver = webdriver.Firefox()
driver.get('http://www.google.com/finance?q=GOOG&fstype=ii')

print_header(driver.find_element_by_id('incinterimdiv'))
print "----"

# activate Balance Sheet
element = driver.find_element_by_xpath('//*[@id=":1"]/a/b/b')
element.click()

print_header(driver.find_element_by_id('balinterimdiv'))
print "----"

# activate Cash Flow
element = driver.find_element_by_xpath('//*[@id=":2"]/a/b/b')
element.click()

print_header(driver.find_element_by_id('casinterimdiv'))

driver.quit()

rawDescription: How can I web-scrape with Python when the HTML doesn't change?

description: Web-scrape with Python when the HTML doesn't change


code:
f = open('data.txt', 'r')
raw = f.readlines()

#generate set of all possible groupings
groups = set()
for lines in raw:
    data = lines.strip().split()
    for items in data:
        groups.add(items)

#parse input into groups
for group in groups:
    print "Group \'%s\':" % group
    for line in raw:
        if line.find(group) is not -1:
            print line.strip()
    print

#consider storing into a dictionary instead of just printing

rawDescription: Grouping related search keywords

description: Grouping related search keywords


code:
for line in f:
    print line

rawDescription: Opening File (Tkinter)

description: Opening File (Tkinter)


code:
def mazeDetector(row,col, solution):
    c= m[row][col]
    solution.append((row, col))
    if c =="W":
        print "Wall here: "+ str(row)+ ","+ str(col)
        return False
    elif c =="V":
        print "Visited: " + str(row)+ ","+ str(col)
        return False
    elif c=="F":
        print "Found: "+ str(row)+ ","+ str(col)
        print solution
        return True

    print "visiting:"+ str(row)+ ","+ str(col)
    m[row][col]=="V"
    if (col>0 and mazeDetector(row,col-1, list(solution))):
        return True
    elif (row< len(m)-1 and mazeDetector(row+1,col, list(solution))):
        return True
    elif (row>0 and mazeDetector(row-1, col, list(solution))):
        return True
    elif (col<=len(m)-1 and mazeDetector(row, col+1, list(solution))):
        return True
    return False
mazeDetector(1,5, [])

rawDescription: how to make my maze function print out the solution

description: Make my maze function print out the solution


code:
if choice_day == "monday" and choice_period == "NO":
    for v in monday.values():
        print v

rawDescription: Shortening down printing single values in a dictionary


code:
path = '\$'
path
print path

rawDescription: python open() escape backslash

description: Python open() escape backslash


code:
Session = sessionmaker(bind=engine)
s = Session()

i = Item('ball', 'A round full ball')
i.notes['color'] = 'orange'
i.notes['size'] = 'big'
i.notes['data'] = 'none'

s.add(i)
s.commit()
print i.notes

rawDescription: Dictionary of tags in declarative SQLAlchemy?


code:
for tr in soup.find_all('tr')[2:]:
    tds = tr.find_all('td')
    print "Nome: %s, Cognome: %s, Email: %s" % \
          (tds[0].text, tds[1].text, tds[2].text)

rawDescription: Python BeautifulSoup scrape tables

description: Python BeautifulSoup scrape tables


code:
import numpy

a = numpy.random.random(100)

# a_by_a[i,j] = a[i] > a[j]
a_by_a = a[numpy.newaxis,:] > a[:,numpy.newaxis]
# by taking the upper triangular, we ignore all cases where i < j
a_by_a = numpy.triu(a_by_a)
# argmax will give the first index with the highest value (1 in this case)
print numpy.argmax(a_by_a, axis = 1)

rawDescription: Numpy vectorization algorithms to find first future element greater than current element


code:
from collections import Counter
dic = Counter()
for line in x:
    m = re.search(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b",line).group().split()
    c = re.search(r"\[(.+)\]",line).group().split()
    for i in range(len(m)):
        dic[m[i]] += 1

for k,v in dic.iteritems(): #or items if Python 3.X
    print k, v

rawDescription: Counting number of unique items from a dictionary


code:
links[0].get('href')
print links[0].get('wharrgarbl')
print links[0]['wharrgarbl']

rawDescription: TypeError : 'NoneType' object not callable when using split in Python with BeautifulSoup


code:
from BeautifulSoup import BeautifulSoup
import urllib2

html_page = urllib2.urlopen("http://www.example.com")
if html_page.getcode() == 200:
    soup = BeautifulSoup(html_page)
    for link in soup.findAll('a'):
        print link.get('href')
else:
    print "Error loading page"

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

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


code:
import a

class b(a.a):
    def mymethod(self):
        self.method()
        print "avariable is ", a.avariable

if __name__ == '__main__':
    print a.avariable
    b().mymethod()
    b().mymethod()

rawDescription: How do I access inherited variables from another module when they are changed in parent?

description: Access inherited variables from another module when they are changed in parent


code:
if len(set(modulo)) == len(modulo): # if lengths are  equal we have no duplicate nums
     print "valid"
else:
      print "Invalid"

rawDescription: how to check if two numbers in a list are the same in Python

description: Check if two numbers in a list are the same in Python


code:
pos=nt.graphviz_layout(graph,prog='dot')
print pos

# EXAMPLE RESULT
{'A': (51.5, 15.3), 'B': (20.0, 60.4)}

rawDescription: Draw graph with time label on the nodes

description: Draw graph with time label on the nodes


code:
class Fun:
    def hello(self):
        print 'Hello'

hasattr(Fun, 'hello')   # -> True
callable(Fun.hello)     # -> True

rawDescription: Is there a Python equivalent to Ruby's respond_to?


code:
class Main:
    def __init__(self):
        parser = self.get_arg()
        self.set_arg(parser)

    def set_arg(self, parser):
        options, args = parser.parse_args()
        if len(sys.argv) == 1:
            print 'Error: Usage: python %s <options>' % sys.argv[0]
            sys.exit()
        input_file = options.input_file
        flag = options.flag

    def get_arg(self):
        parser = OptionParser(usage='usage: %prog [options]', version='%prog 1.1.0')
        parser.add_option('-i', '--input-file', action='store', type='string', dest='input_file', default=None,
                            help='Input file.')
        parser.add_option('-f', '--flag', action='store_true', dest='flag', default=False,
                            help='A flag in your app')
        return parser

if __name__ == '__main__':
    Main()

rawDescription: Checking command-lines parameters with usage.Options


code:
from lxml.html import fromstring

root = fromstring(data)
print int(root.xpath("count(//*)")) + int(root.xpath("count(//@*)"))

rawDescription: Finding all tags and attributes in a HTML


code:
import optparse

def optional_arg(arg_default):
    def func(option,opt_str,value,parser):
        if parser.rargs and not parser.rargs[0].startswith('-'):
            val=parser.rargs[0]
            parser.rargs.pop(0)
        else:
            val=arg_default
        setattr(parser.values,option.dest,val)
    return func

def main(args):
    parser=optparse.OptionParser()
    parser.add_option('--foo',action='callback',callback=optional_arg('empty'),dest='foo')
    parser.add_option('--file',action='store_true',default=False)
    return parser.parse_args(args)

if __name__=='__main__':
    import sys
    print main(sys.argv)

rawDescription: Parsing empty options in Python

description: Parsing empty options in Python


code:
def makeFunc(n):
    return lambda x: x+n
stuff = [makeFunc(n) for n in [1, 2, 3]]
for f in stuff:
    print f(1)

rawDescription: Python lambda closure scoping

description: Python lambda closure scoping


code:
import random

def gen_people_in_group(age, num_male, num_female):
    males = ['m%s' % age] * num_male
    females = ['f%s' % age] * num_female
    return males + females

def gen_random_sample(num_in_group=5):
    groups = [1, 4, 6], [2, 5, 5], [3, 7, 3], [4, 2, 8], [5, 4, 6]

    population = []
    for group in groups:
        population += gen_people_in_group(*group)

    random.shuffle(population)

    for idx in xrange(0, len(population), num_in_group):
        yield population[idx:(idx + num_in_group)]

def main():
    for rand_group in gen_random_sample():
        print rand_group

if __name__ == '__main__':
    main()

rawDescription: Selecting random values from list until they are gone in Python


code:
from Tkinter import *

root = Tk()

w = Label(root, text="Hello, world!")
w.pack()
print w.cget('text')
root.mainloop()

rawDescription: Is it possible to get widget settings in Tkinter?


code:
instances = {filename: read_instance(filename) for filename in 'abcd'}

print instances['c']  # Prints the instance corresponding to filename 'c'

rawDescription: How to programmatically add instances in Python?

description: Programmatically add instances in Python


code:
import urllib
import socket
from urllib import unwrap, toBytes, quote, splittype, splithost, splituser, unquote, addinfourl

class MyURLOpener(urllib.FancyURLopener):
    def open_http(self, url, data=None, method=None):
        """Use HTTP protocol."""
        import httplib
        user_passwd = None
        proxy_passwd= None
        if isinstance(url, str):
            host, selector = splithost(url)
            if host:
                user_passwd, host = splituser(host)
                host = unquote(host)
            realhost = host
        else:
            host, selector = url
            # check whether the proxy contains authorization information
            proxy_passwd, host = splituser(host)
            # now we proceed with the url we want to obtain
            urltype, rest = splittype(selector)
            url = rest
            user_passwd = None
            if urltype.lower() != 'http':
                realhost = None
            else:
                realhost, rest = splithost(rest)
                if realhost:
                    user_passwd, realhost = splituser(realhost)
                if user_passwd:
                    selector = "%s://%s%s" % (urltype, realhost, rest)
                if proxy_bypass(realhost):
                    host = realhost

            #print "proxy via http:", host, selector
        if not host: raise IOError, ('http error', 'no host given')

        if proxy_passwd:
            import base64
            proxy_auth = base64.b64encode(proxy_passwd).strip()
        else:
            proxy_auth = None

        if user_passwd:
            import base64
            auth = base64.b64encode(user_passwd).strip()
        else:
            auth = None
        h = httplib.HTTP(host)

        if method is not None:
            h.putrequest(method, selector)
        else:
            h.putrequest('GET', selector)

        if data is not None:
            #h.putrequest('POST', selector)
            h.putheader('Content-Type', 'application/x-www-form-urlencoded')
            h.putheader('Content-Length', '%d' % len(data))

        if proxy_auth: h.putheader('Proxy-Authorization', 'Basic %s' % proxy_auth)
        if auth: h.putheader('Authorization', 'Basic %s' % auth)
        if realhost: h.putheader('Host', realhost)
        for args in self.addheaders: h.putheader(*args)
        h.endheaders(data)
        errcode, errmsg, headers = h.getreply()
        fp = h.getfile()
        if errcode == -1:
            if fp: fp.close()
            # something went wrong with the HTTP status line
            raise IOError, ('http protocol error', 0,
                            'got a bad status line', None)
        # According to RFC 2616, "2xx" code indicates that the client's
        # request was successfully received, understood, and accepted.
        if (200 <= errcode < 300):
            return addinfourl(fp, headers, "http:" + url, errcode)
        else:
            if data is None:
                return self.http_error(url, fp, errcode, errmsg, headers)
            else:
                return self.http_error(url, fp, errcode, errmsg, headers, data)

    def open(self, fullurl, data=None, method=None):
        """Use URLopener().open(file) instead of open(file, 'r')."""
        fullurl = unwrap(toBytes(fullurl))
        # percent encode url, fixing lame server errors for e.g, like space
        # within url paths.
        fullurl = quote(fullurl, safe="%/:=&?~#+!$,;'@()*[]|")
        if self.tempcache and fullurl in self.tempcache:
            filename, headers = self.tempcache[fullurl]
            fp = open(filename, 'rb')
            return addinfourl(fp, headers, fullurl)
        urltype, url = splittype(fullurl)
        if not urltype:
            urltype = 'file'
        if urltype in self.proxies:
            proxy = self.proxies[urltype]
            urltype, proxyhost = splittype(proxy)
            host, selector = splithost(proxyhost)
            url = (host, fullurl) # Signal special case to open_*()
        else:
            proxy = None
        name = 'open_' + urltype
        self.type = urltype
        name = name.replace('-', '_')
        if not hasattr(self, name):
            if proxy:
                return self.open_unknown_proxy(proxy, fullurl, data)
            else:
                return self.open_unknown(fullurl, data)
        try:
            return getattr(self, name)(url, data, method)
        except socket.error, msg:
            raise IOError, ('socket error', msg), sys.exc_info()[2]

opener = MyURLOpener()

# NOTE: including any data no longer implicitly makes the method POST,
#       so you must now specify the method to POST if you include data
# NOTE: this overrides only open_http, and not open_https, but you can
#       use a similar technique, and override open_https as well

d = opener.open('http://www.google.com/', method='HEAD')

rawDescription: using python urllib how to avoid non HTML content

description: Using python urllib how to avoid non HTML content


code:
def bullsandcows(given, number):
    given, number = map(str, (given, number))
    for g, n in zip(given, number):
        if g == n:
            yield (g, None)
        elif g in number:
            yield (None, g)

given, number = 8241, 1234
list(bullsandcows(given, number))
reduce(lambda (bcount, ccount), (b, c): (bcount + bool(b), ccount + bool(c)),
       bullsandcows(given, number), (0, 0))

rawDescription: Reduce list of tuples

description: Reduce list of tuples


code:
y= array([1, 1, 1, NaN, NaN, 2, 2, NaN, 0])
nans, x= nan_helper(y)
y[nans]= np.interp(x(nans), x(~nans), y[~nans])
print y.round(2)

rawDescription: Interpolate NaN values in a numpy array

description: Interpolate NaN values in a numpy array


code:
import itertools

class fancy_it(object):
    stack = []

    def __init__(self, iterable=None):
        if not iterable:
            prev = fancy_it.stack[-1]
            prev.it, iterable = itertools.tee(prev.it)
        self.it = iter(iterable)

    def __iter__(self):
        fancy_it.stack.append(self)
        try:
            while True:
                yield self.it.next()
        except StopIteration:
            fancy_it.stack.pop()
            raise StopIteration

for x in fancy_it(range(10)):
    print x
    for y in fancy_it():
        print '**', y
        for z in fancy_it():
            print '****', z

rawDescription: How to start a loop with a specific iterator

description: Start a loop with a specific iterator