code:
import os
#hard code the path to the external file
external_file = 'names.txt'
username = 'testuser'#Our hardcoded name

#if file doesn' exists, create a new file
if not os.path.exists(external_file):
    #Ask the user's name
    name = raw_input("What's your name?")
    with open(external_file, "a") as f: #using "a" will append to the file
        f.write(name)# Write the name to names.txt
        f.write("\n")
        f.close()
else:
    #if file exists, use it to load name, else ask user
    with open(external_file, "r") as f:#r+ opens a file for reading & writing
        lines = f.read().split('\n') # split the names
        f.close()
    if username in lines: #Check if the file has any username as 'testuser'
        print "Hi {}".format(username)
        response = raw_input("Is this your name?")
        if 'no' in response.lower():
            name = raw_input("What's your name?")# Asks for new name
            with open(external_file, "w") as f:
                f.write(name)# Write the name to names.txt
                f.write("\n")
                f.close()
    else: # If there is no username as 'testuser' then ask for a name
        name = raw_input("What's your name?")
        with open(external_file, "w") as f:# Write name to the file
            #f.seek(0,2) # Resolves an issue in Windows
            f.write(name)# Write the name to names.txt
            f.write("\n")
            f.close()

rawDescription: Taking info from a File

description: Taking info from a File


code:
import sys
def a():
   b = 1
   f = sys._getframe()
   print f.f_locals
   f.f_locals["b"] = 2
   print b

a()

rawDescription: Prepend function call in other function in Python


code:
def binary(num):
    # Struct can provide us with the float packed into bytes. The '!' ensures that
    # it's in network byte order (big-endian) and the 'f' says that it should be
    # packed as a float. Alternatively, for double-precision, you could use 'd'.
    packed = struct.pack('!f', num)
    print 'Packed: %s' % repr(packed)

    # For each character in the returned string, we'll turn it into its corresponding
    # integer code point
    #
    # [62, 163, 215, 10] = [ord(c) for c in '>\xa3\xd7\n']
    integers = [ord(c) for c in packed]
    print 'Integers: %s' % integers

    # For each integer, we'll convert it to its binary representation.
    binaries = [bin(i) for i in integers]
    print 'Binaries: %s' % binaries

    # Now strip off the '0b' from each of these
    stripped_binaries = [s.replace('0b', '') for s in binaries]
    print 'Stripped: %s' % stripped_binaries

    # Pad each byte's binary representation's with 0's to make sure it has all 8 bits:
    #
    # ['00111110', '10100011', '11010111', '00001010']
    padded = [s.rjust(8, '0') for s in stripped_binaries]
    print 'Padded: %s' % padded

    # At this point, we have each of the bytes for the network byte ordered float
    # in an array as binary strings. Now we just concatenate them to get the total
    # representation of the float:
    return ''.join(padded)

rawDescription: Binary representation of float in Python (bits not hex)


code:
if 'comment' in request.POST.keys():
    comment = post.forumreply_set.filter(pk=request.POST['comment'])[0]
    if not comment in request.user.userprofile.liked_replies.all():
        print 'liked'
        comment.points += 1
        comment.save()
        request.user.userprofile.liked_replies.add(comment)
        request.user.userprofile.save()

rawDescription: Django voting system: Prevent duplicate votes


code:
blacklist = ["olive leaf plucked", "during mating searson"]
blacklist = [tuple(b.split()) for b in blacklist]
finder3.apply_ngram_filter(lambda w1, w2, w3: (w1,w2,w3) in blacklist)

print finder3.nbest(trigram_measures.pmi, 10)

rawDescription: finder.apply_ngram_filter in nltk.collocations to classify some ngrams

description: Finder.apply_ngram_filter in nltk.collocations to classify some ngrams


code:
import collections

ordered_d = collections.OrderedDict([('banana', 3),('apple',4),('pear', 1),('orange', 2)])
for k in ordered_d.keys():
    print k

rawDescription: Why iteration on dict will change keys order in output?


code:
js = json.loads(response.content)
ourResult = js['transactions'][0]['meta']

for majorkey, subdict in ourResult.iteritems():
     print majorkey
     if type(subdict) == dict:
         for subkey, value in subdict.iteritems():
             print subkey, value

rawDescription: Write a loop that parses json dictionaries using python


code:
user = User.objects.get(id=1)

# set the fk reference
user.bgpic = BackgroundImage.objects.get(id=2)
user.save()

# access the reference
print user.bgpic.url
print user.bgpic.name

rawDescription: Django: Reference Database Entry

description: Django: Reference Database Entry


rawDescription: making index with python

description: Making index with python


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

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


code:
from lxml import etree

data = """<?xml version="1.0" encoding="utf-8"?>
<tree>
    <item>
        <element1>somedata</element1>
        <element2>moredata</element2>
        <element3>data?</element3>
        <optional_element>data!</optional_element>
    </item>
    <item>
        <element1>somedata</element1>
        <element2>moredata</element2>
        <element3>data?</element3>
    </item>
    <item>
        <element1>somedata</element1>
        <element2>moredata</element2>
        <element3>data?</element3>
        <optional_element>data!</optional_element>
    </item>
    <item>
        <element1>somedata</element1>
        <element2>moredata</element2>
        <element3>data?</element3>
    </item>
</tree>
"""

tree = etree.fromstring(data)
print [item.findtext("optional_element", default="") for item in tree.findall("item")]

rawDescription: Python XPath include missing elements

description: Python XPath include missing elements


code:
import mechanize
browser = mechanize.Browser()
browser.open('http://www.animeplus.tv/anime-show-list/')
print browser.response().read()

rawDescription: Python error trying to parse webpage

description: Python error trying to parse webpage


code:
def sin(x):
    ans = 0
    for n in range(4):
        ans += (math.pow(-1, n)/math.factorial(2*n + 1)) * math.pow(x, 2*n + 1)
    print ans

rawDescription: Not Getting Correct Answer for sine & cosine in Python


code:
print a
a._position.bounds
a._position.bounds[3]

rawDescription: Extracting subplot coordinates in Python


code:
column_widths = [max(len(x) for x in l) for l in zip(*ll)]
for row in ll:
  print '  '.join(x.ljust(w) for x,w in zip(row, column_widths))

rawDescription: writing column entry just one below another in python

description: Writing column entry just one below another in python


code:
def fct_in_extern_module(where):
    exec 'A = 42' in where

rawDescription: exec a global variable from an extern module

description: Exec a global variable from an extern module


code:
for centre, points in itertools.groupby(all_neighbours, centroid):
    print tuple([centre] + map(point, points))

neighbourhoods = [tuple([centre] + map(point, points)) for centre, points
                  in itertools.groupby(all_neighbours, centroid)]

rawDescription: Group list by values and keep both

description: Group list by values and keep both


code:
''' long comment '''
q=7
y=0
foo=10
for x in range(y,y+foo):
    print "Hello", 'world', '''lookout for the # tricky nest''', q # comment

rawDescription: Using python to remove all strings and comments from python code


code:
class Object(object):
    def __init__(self):
        self.name = self.__class__.__name__

class SimplePhysicObject(Object):
    pass

a = SimplePhysicObject()
print a.name

rawDescription: Macros in python

description: Macros in python


code:
def random_letter(letters):
    print random.choice(letters)

while True:
    x = raw_input('Press enter for a random letter...')
    if x == 'done':
        break
    random_letter('abcdef')

rawDescription: Running a function in Python every time any key is pressed?


code:
#!/usr/bin/python
import subprocess, os, select, time

path = "/usr/bin/python"
proc = subprocess.Popen([path, "foo.py"], shell=False,
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE)
for i in xrange(0,5):
    time.sleep(1)
    inputready, outputready, exceptready = select.select(
        [proc.stdout, proc.stderr], [proc.stdin,],
        [proc.stdout, proc.stderr, proc.stdin], 0)
    for s in inputready:
        line = s.readline()
        if line:
            print s.fileno(), line

proc.terminate()
print "After Terminating"

for i in xrange(0,5):
    time.sleep(1)
    inputready, outputready, exceptready = select.select(
        [proc.stdout, proc.stderr], [proc.stdin,],
        [proc.stdout, proc.stderr, proc.stdin], 0)
    for s in inputready:
        line = s.readline()
        if line:
            print s.fileno(), line

rawDescription: Asyncronous Bidirectional IO Redirection for a child process


code:
import re

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def extract_data(browser):
    links = browser.find_elements_by_xpath('//i[@class="RecordStats"]/a')
    return [link.get_attribute('href') for link in links]

browser = webdriver.Firefox()
browser.get("http://www.scba.gov.ar/jurisprudencia/Navbar.asp?Busca=Fallos+Completos&SearchString=Inconstitucionalidad")

# get max pages
element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "//p[@class='c'][last()]")))
max_pages = int(re.search(r'\d+ de (\d+)', element.text).group(1), re.UNICODE)

# extract from the current (1) page
print "Page 1"
print extract_data(browser)

# loop over the rest of the pages
for page in xrange(2, max_pages + 1):
    print "Page %d" % page

    next_page = browser.find_element_by_xpath("//table[last()]//td[last()]/a").click()

    print extract_data(browser)
    print "-----"

rawDescription: How to scrape urls on a paginated site with Selenium / Python

description: Scrape urls on a paginated site with Selenium / Python


code:
N = 3
handy = df.groupby('factor1')['factor1'].count()
handy.sort('factor1',ascending=False)
topNFactors = handy.head(N)
print topNFactors

dataOfTopNFactors = df[df['factor1'].map(lambda x: x in topNFactors)]
print dataOfTopNFactors

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


code:
#!/usr/bin/python

import alsaaudio as aa
import audioop
import Tkinter as tk
import tkFont
from threading import Thread, Event

class Display():
    def __init__(self, parent):
        self.parent = parent

        self._geom = '200x200+0+0'
        parent.geometry("{0}x{1}+0+0".format(parent.winfo_screenwidth(), parent.winfo_screenheight()))
        parent.overrideredirect(1)

        parent.title('Listen')
        parent.configure(background='#000000')
        parent.displayFont = tkFont.Font(family="Unit-Bold", size=150)

    def printMessage(self, messageString):
        self.message = tk.Message(self.parent, text=messageString, bg="#000000", font=self.parent.displayFont, fg="#777777", justify="c")
        self.message.place(relx=.5, rely=.5, anchor="c")

def setup_audio(window, stop_event):
    data_in = aa.PCM(aa.PCM_CAPTURE, aa.PCM_NONBLOCK, 'hw:1')
    data_in.setchannels(2)
    data_in.setrate(44100)
    data_in.setformat(aa.PCM_FORMAT_S16_LE)

    data_in.setperiodsize(256)

    while not stop_event.is_set():
       # Read data from device
       l,data = data_in.read()
       if l:
          # catch frame error
          try:
             max_vol=audioop.rms(data,2)
             scaled_vol = max_vol//4680
             print scaled_vol

             window.printMessage(scaled_vol)

          except audioop.error, e:
             if e.message !="not a whole number of frames":
                raise e

def main():
    root = tk.Tk()
    window = Display(root)

    stop_event = Event()
    audio_thread = Thread(target=setup_audio, args=[window, stop_event])
    audio_thread.start()
    try:
        root.mainloop()
    finally:
        stop_event.set()
        audio_thread.join()

if __name__ == '__main__':
    main()

rawDescription: Calling Tkinter class from another class (Python on Raspberry Pi)


code:
from collections import Counter
import fileinput

c = Counter()

for line in  fileinput.input('file1.txt', inplace=True):
    if '(*) Begin' in line:
        text = line.split(None, 2)[1]
        print '{}[{}]'.format(line.strip(), c[text])
    elif '(*) End' in line:
        text = line.split(None, 2)[1]
        print '{}[{}]'.format(line.strip(), c[text])
        # End found, so now increment the counter
        c[text] += 1
    else:
        print line,

rawDescription: Index breaks in a set

description: Index breaks in a set


code:
import os
import shutil
files_to_rename = ["c:\\users\\andrew\\pdf\\p.pdf", "c:\\users\\andrew\\pdf2\\p2.pdf"]

for file_full_path in files_to_rename:
    folder_n, file_n = os.path.split(file_full_path)
    new_name = os.path.join(folder_n, "ORG" + file_n)
    print "We will copy at ", new_name, file_full_path
    shutil.copyfile(file_full_path, new_name)

rawDescription: Renaming/copying in windows python

description: Renaming/copying in windows python


code:
#!/usr/bin/env python
import os
command = ['/usr/bin/ssh', 'ssh', 'localhost', '/home/gd/test/python/back.sh']
print os.spawnlp(os.P_NOWAIT, *command)
print "Python done"

rawDescription: python: os.spawn . cannot start bash process on background

description: Python: os.spawn . cannot start bash process on background


code:
def foo_by_statement(x):
    return x + 3

foo_by_expression = lambda x: x + 3

print foo_by_statement(2)
print foo_by_expression(2)
print (lambda x: x + 3)(2)

rawDescription: if annonymous, what is a call to python lambda?

description: If annonymous, what is a call to python lambda


code:
import ctypes
from ctypes import wintypes
import uuid

class _SP_DEVINFO_DATA(ctypes.Structure):
    _fields_ = [("cbSize", wintypes.DWORD),
                ("ClassGuid", ctypes.c_char * 16),
                ("DevInst", wintypes.DWORD),
                ("Reserved", wintypes.LPVOID)]

    def __init__(self, guid, inst):
        self.cbSize = ctypes.sizeof(_SP_DEVINFO_DATA)
        self.ClassGuid = uuid.UUID(guid).get_bytes()
        self.DevInst = (ctypes.c_ulong)(inst)
        self.Reserved = None

    def __repr__(self):
        return "_SP_DEV_INFO_DATA(cbsize={},ClassGuid={},DevInst={})".format(
            self.cbSize,uuid.UUID(bytes=self.ClassGuid),hex(self.DevInst))

sp = _SP_DEVINFO_DATA('08751880-13bb-11e2-96f0-402cf4ca5e51',0x12345678)
print sp

rawDescription: How do I pass a class object to a ctypes.windll.LoadLibrary(DLL) function?

description: Pass a class object to a ctypes.windll.LoadLibrary(DLL) function


code:
def sort_key(item):
    code = item['sizeCode']

    # Get the index for S, M and L where S is -1 and L is 1
    a = 'SML'.index(code[-1]) - 1

    # Get the X, 2X, 3X
    if code[0].isdigit():
        b = int(code[0])
    elif code[0] == 'X':
        b = 1
    else:
        b = 0

    return a, a * b

print sorted(sizes, key=sort_key)

rawDescription: python insert or update order of list items

description: Python insert or update order of list items


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

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

rawDescription: Python: make every instance have a different superclass


code:
import numpy as np
from scipy import sparse
M = sparse.coo_matrix((data,(rows,cols)))
print M
print
Md = M.todok()
print Md

rawDescription: group by on scipy sparse matrix

description: Group by on scipy sparse matrix


code:
from bs4 import BeautifulSoup

html = "
\
 some texts1\

\
 \

\
 some texts2\

"

soup = BeautifulSoup(html)
text = "+".join(soup.stripped_strings).split("+")

print text[0]
print soup.find("img")['src']
print text[1]

rawDescription: how to scrap text and image together?

description: Scrap text and image together


code:
total = 0
for x, y in D1.keys():
    try:
        total = total + D1[(x, y)] * D2[(y, x)]
    except KeyError:
        pass
print total

rawDescription: Parse dictionary with Tuple as key

description: Parse dictionary with Tuple as key


code:
with open('/tmp/ba.data') as dataF:
    oldk, oldsub = None, None
    for key, subi in groupby(map(str.split,dataF), lambda x: (x[1],x[2])):
        if oldk == None:
            oldk, oldsub = key, list(subi)
        else:
            newsub = list(subi)
            print ' '.join(oldk), '->', ' '.join(key), float(oldsub[-1][3])-float(newsub[0][3])
            oldk, oldsub = None, None

rawDescription: Vectorize High Frequency Data, Calculate Spread,


code:
print is_third_friday('Jan 18, 2013')  # True
print is_third_friday('Feb 22, 2013')  # False
print is_third_friday('Jun 21, 2013')  # True
print is_third_friday('Sep 20, 2013')  # True

rawDescription: Python: third Friday of a month

description: Python: third Friday of a month


code:
file_name = "/data/app/com.rovio.angrybirdsstarwars.ads.iap-1.apk"
cmd = "adb shell ls -l %s"%file_name #stat doesnt work i dont think
file_size = subprocess.Popen(cmd,stdout=subprocess.PIPE).communicate()[0].split()[3]
usb2_xfer = 4603904.0 #4496 KB/s  (http://forum.xda-developers.com/showthread.php?t=882608)
print "EST TIME:%0.2f secs"%(file_size/usb2_xfer)

rawDescription: ADB pull completion

description: ADB pull completion


code:
print product_list.category_name.iat[int(prod)-1]
print product_list.brand_name.iat[int(prod)-1]

rawDescription: How I can speed up row column access to pandas dataframe?


code:
output = subprocess.Popen(['chkconfig', '--list'], stdout=subprocess.PIPE).stdout

for line in output:
    if line.startswith('httpd_'):
        print line.split()[0]

rawDescription: python parse file and put in for loop

description: Python parse file and put in for loop


code:
for line in Table:
    print ' '.join(map(str, line))

rawDescription: how to print array as a table in python

description: Print array as a table in python


code:
def func(x, y):
    return x ** y

data = pd.read_csv('data.dat', sep=';', index_col=0, parse_dates=True,
                    header=None, names='abc')
para = pd.read_csv('parameter.dat', sep=';', index_col=0, parse_dates=True,
                    header=None, names=['para'])

for col in data:
    data['%s_result' % col] = func(data[col], para.para)

print data

rawDescription: How to optimally apply a function on all items of a dataframe using inputs from another dataframe?

description: Optimally apply a function on all items of a dataframe using inputs from another dataframe


rawDescription: Recursive generator in C++

description: Recursive generator in C++


code:
from serial import Serial
import sys,time
ser = Serial('/dev/ttyACM0', 9600, 7, 'E', 1)

t1 = time.time()
while time.time() - t1 <= 3:
    # Read a line and convert it from b'xxx\r\n' to xxx
    line = ser.readline().decode('utf-8')[:-2]
    print line
sys.exit()     #exit script

rawDescription: Python script auto close

description: Python script auto close


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

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


code:
#my_server.py
from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
from subprocess import *

proc_srvr = xmlrpclib.ServerProxy("http://localhost:12345")

def echo_fn():
    p = Popen(("echo", "hello"), shell=False)
    ret = p.wait()
    print "Process ended with status %s" % ret
    return True  # RPC Server doesn't like to return None

def main():
    server = SimpleXMLRPCServer(("localhost", 12345), ErrorHandler)
    server.register_function(echo_fn, "echo_fn")
    while True:
        server.handle_request()

if __name__ == "__main__":
    main()

rawDescription: Unable to open a Python subprocess in Web2py (SIGABRT)


rawDescription: Search & replace a substring in python

description: Search & replace a substring in python


code:
for k,v in ServerDict.items():
 print k, v['IP_']
for k in ServerDict.keys():
  print ServerDict[k]['IP_']
for v in ServerDict.values():
  print v['IP_']

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


code:
class _test(object): pass

import sys
print sys.modules['__main__']
# <module '__main__' from 'test.py'>  <<< the test.py is the name of this module
sys.modules[__name__] = _test()
# Which is the same as doing sys.modules['__main__'] = _test() but wait a
# minute isn't sys.modules['__main__'] was referencing to this module so
# Oops i just overwrite this module entry so this module will be deleted
# it's like if i did:
#
#   import test
#   __main__ = test
#   del test
#   __main__ = _test()
#   test will be deleted because the only reference for it was __main__ in
#   that point.

print sys, __name__
# None, None

import sys   # i should re import sys again.
print sys.modules['__main__']
# <__main__._test instance at 0x7f031fcb5488>  <<< my new module reference.

rawDescription: Why is the value of __name__ changing after assignment to sys.modules[__name__]?


code:
e  = input("Enter a 7 digit number - ")

# note: an integer number can not be accessed like a string or list,
# make it a string first
e=str(e)
sum= int(e[0])*3+int(e[1])+\
     int(e[2])*3+int(e[3])+\
     int(e[4])*3+int(e[5])+\
     int(e[6])*3

checkdigit = 9 - (sum-1) % 10
print sum,checkdigit

rawDescription: Check a variable is a multiple of 10

description: Check a variable is a multiple of 10


code:
animals=[]
preys=[]

with open('AquaticFoodWeb.txt','r') as f:
    for line in f:
        animals.append(line.split()[0])
        preys.append(line.split()[-1])

height = {}

length = len(preys)
rank = 0
while preys != [None]*length:
    for index,(animal,prey) in enumerate(zip(animals,preys)):
        if prey not in animals:
            try:
                if height[prey] < rank:
                    height[prey] = rank
            except KeyError:
                height[prey] = 0

            height[animal] = height[prey] + 1

            preys[index] = None
            animals[index] = None
    rank += 1

print sorted(height.items(),key = lambda x:x[1],reverse=True)

rawDescription: Trying to determine the "height" of organisms Using Python Code


code:
#!/usr/bin/env python
from __future__ import with_statement
import decimal

def pi_gauss_legendre():
    D = decimal.Decimal
    with decimal.localcontext() as ctx:
        ctx.prec += 2
        a, b, t, p = 1, 1/D(2).sqrt(), 1/D(4), 1
        pi = None
        while 1:
            an    = (a + b) / 2
            b     = (a * b).sqrt()
            t    -= p * (a - an) * (a - an)
            a, p  = an, 2*p
            piold = pi
            pi    = (a + b) * (a + b) / (4 * t)
            if pi == piold:  # equal within given precision
                break
    return +pi

decimal.getcontext().prec = 100
print pi_gauss_legendre()

rawDescription: Gauss-Legendre Algorithm in python

description: Gauss-Legendre Algorithm in python


code:
src_path = "/home/vivek/Desktop/Work/stack/"
main_file = "/home/vivek/Desktop/Work/stack/main.csv"
import csv
import collections
import pprint

with open(main_file, "rb") as fp:
    root = csv.reader(fp, delimiter=',')
    result = collections.defaultdict(list)
    for row in root:
        year = row[0].split("-")[0]
        result[year].append(row)

print "Result:-"
pprint.pprint(result)

for i,j in result.items():
    file_path = "%s%s.csv"%(src_path, i)
    with open(file_path, 'wb') as fp:
        writer = csv.writer(fp, delimiter=',')
        writer.writerows(j)

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

description: Split a csv file on date using python


code:
import os
def Test2(rootDir):
    for lists in os.listdir(rootDir):
        path = os.path.join(rootDir, lists)
        print path
        if os.path.isdir(path):
            Test2(path)

rawDescription: Print out the whole directory tree

description: Print out the whole directory tree


code:
#other imports
from django.views.decorators.csrf import csrf_exempt
from django.core.context_processors import csrf
from django.views.decorators.csrf import csrf_protect

def jqueryserver(request):
    print "in jqueryserver"
    response_string="hello"
    if request.method == 'GET':
        if request.is_ajax()== True:
            return HttpResponse(response_string,mimetype='text/plain')

rawDescription: how call a python function from Jquery script in django template

description: How call a python function from Jquery script in django template


code:
def make_printer(msg):
    def printer(msg=msg):
        print msg
    return printer

printer = make_printer("Foo!")
printer()  #Output: Foo!

rawDescription: Why aren't python nested functions called closures?


code:
html = '''
blah

blah

<blah>
'''

tree = BeautifulSoup(html)
for node in tree.findAll(attrs={'class': re.compile(r".*\bwikitable\b.*")}):
    print node

rawDescription: BeautifulSoup and Searching By Class

description: BeautifulSoup and Searching By Class


code:
for key in numb:
    if key in combo:
        print combo[key]

rawDescription: Printing the values of a dictionary when its key is equal to the value of a list element


code:
import timeit, itertools

lst = range(1000) * 5
value = 0
occurrence = 5

print timeit.timeit(lambda: [i for i,elem in enumerate(lst) if elem==value][occurrence-1], number=1000)
print timeit.timeit(lambda: next(itertools.islice((i for i,elem in enumerate(lst) if elem==value), occurrence-1, occurrence)), number=1000)
print timeit.timeit(lambda: nth_index(lst, occurrence, value), number=1000)

rawDescription: How do I return the index number for nth occurrence of a given value?

description: Return the index number for nth occurrence of a given value


code:
seats = ''.join(row * 10 for row in 'ABC') # add DEFG etc.. for additional rows
try:
    print seats[15] # note Python indices are 0 based
except IndexError:
    pass # no row found - do something

rawDescription: How to find exact place for given value in python dictionary?

description: Find exact place for given value in python dictionary


code:
from SNMP import v2Manager
import time

def poll_eth0(manager=None):
    # NOTE: 2nd arg to get_index should be a valid ifName value
    in_bytes = manager.get_index('ifHCInOctets', 'eth0')
    out_bytes = manager.get_index('ifHCOutOctets', 'eth0')
    return (time.time(), int(in_bytes), int(out_bytes))

# Prep an SNMP manager object...
mgr = v2Manager('localhost')
mgr.index('ifName')
stats = list()
# Insert condition below, instead of True...
while True:
    stats.append(poll_eth0(mgr))
    print poll_eth0(mgr)
    time.sleep(5)

rawDescription: How can I check the data transfer on a network interface in python?

description: Check the data transfer on a network interface in python


code:
L = [('n0', 1), ('n1', 4), ('n1', 2), ('n2', 5)]

from itertools import groupby, product
groups = [list(group) for key, group in groupby(L, lambda x: x[0])]
R = map(list, product(*groups))
print R

rawDescription: specific handling of list of list in python... itertools?

description: Specific handling of list of list in python... itertools


code:
sample = """IO_SET(BLOCK, key1, function(1+2,3, val11), key2, val2, key3, (3U)+cVAL3);"""

from pyparsing import *

SEMI,LPAREN,RPAREN = map(Suppress,";()")
identifier = Combine(Optional(Word(nums+'_')) + Word(alphas, alphanums+'_'))
integer= Combine(Optional('-') + Word(nums))
realnum = Combine(integer.copy() + '.' + Optional(Word(nums)))

fn_call = Forward()
# this order is *critical*
value = realnum | fn_call | identifier | integer

expr = infixNotation(value,
            [
            (oneOf('* /'), 2, opAssoc.LEFT),
            (oneOf('+ -'), 2, opAssoc.LEFT),
            ])
fn_call <<= Group(identifier + LPAREN + Group(Optional(delimitedList(expr))) + RPAREN)

print value.parseString(sample).asList()

rawDescription: Not so simple recursive descent in PyParsing


code:
distance = Distance(meters=Meters(12))
print distance.meters.value
print distance.meters.type
# Here do whatever you want with them

rawDescription: Python class Inheritance Initialization

description: Python class Inheritance Initialization


code:
import md5
import mechanize
br = mechanize.Browser()
r=br.open("http://www.helifreak.com")
for f in br.forms():
    if (f.action == 'http://www.helifreak.com/login.php'):
            ff=f

m=md5.new()
m.update('password123')
d=m.hexdigest()
ff.set_all_readonly(False)
ff.set_value(d,'vb_login_md5password')
ff.set_value(d,'vb_login_md5password_utf')
ff.set_value('testsubject','vb_login_username')
br.form=ff
r=br.submit()
print r.read()

rawDescription: Login on a website with python, I tried many examples, but what am I doing wrong?


code: print 1 # prints something like "1\n"

rawDescription: f.write vs print >> f

description: F.write vs print >> f


code:
for color in ['red', 'orange', 'yellow', 'green', 'blue', 'indago', 'violet']:
    colors.put(color)
numbers = list(range(20)) # We won't be using it like a queue, so just make it a list.

for i in range(0, num_threads):
  threading.Thread(target=handle)

def handle():
  while not colors.empty():
    color = colors.get()
    for i in numbers:
      print color, i # Edit this to get it to print what you want

rawDescription: Python Queue Combination

description: Python Queue Combination


code:
for i in range(0,30):
    while i is not False: # False == 0 but False is not 0
        print i
        break

rawDescription: Range(0,30) does not start with 0 if using while . Python


code:
for line in found:
    print line

rawDescription: read snippet of file with regular expressions from text file in python

description: Read snippet of file with regular expressions from text file in python


code:
with open('file') as f:
    lines = f.readlines()
    lines = lines[-1:] + lines[2:-1]
    for line in lines:
        print line[0:11] + line[35:-1]

rawDescription: Remove all characters between two words and replace with a space


code:
with open('filename1', 'r') as fl1:
    f1 = [i.split(',') for i in fl1.read().split('\n')]

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

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

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

[OUTPUT]
feb,55,1.23,12,KL,0
mar,65,2.33,10,MN,1

rawDescription: Joining two csv files in Python

description: Joining two csv files in Python


code:
from BeautifulSoup import BeautifulSoup as Soup

file = 'data.xml'
handler = open(file).read()

soup = Soup(handler)

data = soup.find('aws:temp')
print data.text

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

description: Obtain value from WeatherBug XML File in Python


code:
#!/usr/bin/env python
"""
Use a pcolor or imshow with a custom colormap to make a contour plot.

Since this example was initially written, a proper contour routine was
added to matplotlib - see contour_demo.py and
http://matplotlib.sf.net/matplotlib.pylab.html#-contour.
"""

from pylab import *

delta = 0.01
x = arange(-3.0, 3.0, delta)
y = arange(-3.0, 3.0, delta)
X,Y = meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = Z2 - Z1 # difference of Gaussians

cmap = cm.get_cmap('PiYG', 11)    # 11 discrete colors

im = imshow(Z, cmap=cmap, interpolation='bilinear',
            vmax=abs(Z).max(), vmin=-abs(Z).max())
#axis('off')
cb = colorbar()

for path in cb.solids.get_paths():
    print path

show()

rawDescription: colorbar find the width of color segment

description: Colorbar find the width of color segment


code:
import pygame

# --- constants ---

BLACK = (  0,  0,  0)
RED   = (255,  0,  0)

# --- classes ---

class Label():

    def __init__(self, text, x, y, width):

        font = pygame.font.SysFont(None, 35)

        # generate full image
        self.image = font.render(text, True, RED)
        self.rect = self.image.get_rect(x=x, y=y)

        self.width = width
        self.speed = 1

        # offset of visible part
        self.offset_x = 0
        self.max_offset_x = self.rect.width - self.width

        # visible part of image
        self.sub_image = self.image.subsurface((self.offset_x,0), (self.width, self.rect.height))
        self.sub_image_rect = self.sub_image.get_rect(x=x, y=y)

    def move(self):

        # change offset
        self.offset_x += self.speed

        # change move direction
        if self.offset_x < 0:
            self.offset_x = 0
            self.speed = -self.speed
        elif self.offset_x > self.max_offset_x:
            self.offset_x = self.max_offset_x
            self.speed = -self.speed

        print self.offset_x, self.max_offset_x
        # visible part of image
        self.sub_image = self.image.subsurface((self.offset_x, 0),(self.width, self.rect.height))

    def draw(self, surface):
        surface.blit(self.sub_image, self.sub_image_rect)

# --- main ---

pygame.init()
screen = pygame.display.set_mode((800,600))

label = Label("Hello World of Python and Pygame!", 100, 100, 200)

# --- mainloop ---

fps = pygame.time.Clock()
running = True

while running:

    # --- events ---

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key ==  pygame.K_ESCAPE:
                running = False

    # --- updates ---

    label.move()

    # --- draws ---

    screen.fill(BLACK)

    label.draw(screen)

    fps.tick(25)
    pygame.display.flip()

# --- the end ---

pygame.quit()

rawDescription: Assign static Mask on moving Surface with PyGame


code:
from bs4 import BeautifulSoup

html = '''

        NAME

        PROVINCE - CITY ADDRESS

'''

soup = BeautifulSoup(html)
name_tag = soup.find('h4', itemprop='name')
addr_div = name_tag.find_next_sibling('div')
province_tag, city_tag = addr_div.find_all('a')

name, province, city = [t.text.strip() for t in name_tag, province_tag, city_tag]
address = city_tag.next_sibling.strip()

rawDescription: How can I get data from a specific class of a html tag using beautifulsoup?

description: Get data from a specific class of a html tag using beautifulsoup


code:
while True: # loop
  bills = raw_input('Enter the value for the seat [\'q\' to quit]: ' ) # get input
  if bills == 'q': # if the input is 'q'
    break # stop asking for input

for bill in bills: # for each character in 'q'
  new_number = bill+bill # double 'q' and save it

print '{}'.format(new_number) # print the number with no special formatting of any kind

rawDescription: Addition of raw_input numbers in python

description: Addition of raw_input numbers in python


code: print np.delete(a, [7,2,8])

rawDescription: Numpy Indexing: Return the rest

description: Numpy Indexing: Return the rest


code:
@app.route('/page')
def get_page():
    return send_file('templates/progress.html')

@app.route('/progress')
def progress():
    def generate():
        x = 0
        while x < 100:
            print x
            x = x + 10
            time.sleep(0.2)
            yield "data:" + str(x) + "\n\n"
    return Response(generate(), mimetype= 'text/event-stream')

rawDescription: text/event-stream recognised as a download

description: Text/event-stream recognised as a download


code:
new_cards = [Card(**card) for card in card_list]  # make new card objects
cards = {card.name: card for card in new_cards}  # dict to ref cards by name
print cards['Knight'].name

rawDescription: Creating multiple class instances from a list of names in Python?


code: print section_heading

rawDescription: Removing part of a string that has been obtained by a regular expression using strip functions


code:
import openpyxl as px

W = px.load_workbook('Workbook1.xlsx', use_iterators = True)
p = W.get_sheet_by_name(name = 'Sheet1')

print p['A1'].value
print [ p['A%s'%i].value for i in range(1,10) ]

rawDescription: Python Read selected Excel columns into arrays


code:
class parser(threading.Thread):
    output_lock = threading.Lock()

    def __init__ (self, data_input):
        threading.Thread.__init__(self)
        self.data_input = data_input

    def run(self):
        for elem in self.data_input:
            time.sleep(3)
            with self.output_lock:
                print elem + 'Finished'

rawDescription: Python Threading stdin/stdout

description: Python Threading stdin/stdout


code:
print "last modified: %s, created: %s" %(format_time(os.path.getmtime(single_file)),
                                         format_time(os.path.getctime(single_file)))

rawDescription: How to list latest modification time of contents of some directories?

description: List latest modification time of contents of some directories


code:
from lxml import html

def super_func(keyword, htmldoc):
    query = '//*[contains(text(),"{0}")]'
    result = htmldoc.xpath(query.format(keyword))
    if len(result) > 0:
        return html.tostring(result[0])
    else:
        return ''

a = '
Test moree test
 London is ...
mooo

'
doc = html.fromstring(a)
text = 'London'
print super_func(text, doc)
text = 'mooo'
print super_func(text, doc)

rawDescription: Python get tag with certain text

description: Python get tag with certain text


code:
from lxml.html import fromstring

html_data = """<input name="utf8" type="hidden" value="✓" />
<input name="ohboy" type="hidden"  value="I_WANT_THIS" />
<label for="user_email">Email</label>
<input class="form-control" id="user_email" name="user[email]" size="30" type="email" value=""      />"""

tree = fromstring(html_data)
print tree.find('.//input[2]').attrib['value']

rawDescription: python - xPath syntax for second occurence

description: Python - xPath syntax for second occurence


code:
import random

fruit = ['apple', 'pear', 'banana', 'orange']
labels = ['A', 'B', 'B', 'D']

# shuffle fruits
result = list(fruit)
random.shuffle(result)

# insert labels at random positions
# between the beginning of the list and the corresponding fruit
for l,f in zip(labels,fruit):
    result.insert(random.randint(0,result.index(f)),l)

print result

rawDescription: How do I shuffle a list with constraints in Python where some elements cannot appear after another element

description: Shuffle a list with constraints in Python where some elements cannot appear after another element


code:
from pyparsing import Word,alphanums,Forward,Optional,nestedExpr,delimitedList

topicString = Word(alphanums+'-')
expr = Forward()
expr << topicString + Optional(nestedExpr(content=delimitedList(expr)))

test = 'topic(subtopic(sub-subtopic), subtopic2), topic2'

print delimitedList(expr).parseString(test).asList()

rawDescription: Difficulty of this particular job using pyparsing? (beginner)


code:
list1 = ["a","b","c"]
list2=[]
for item in list1:
    number = 1       # You need to reset number within the loop, not outside!
    while number < 5:
        list2.append(str(item)+str(number))
        number = number + 1
print list2

rawDescription: Trying to Write For-While Loop, not working


code:
myset = IntSet()
print 'My set contains ', len(myset)
myset.insert(9)
myset.insert(10)
print 'My set contains ', len(myset)
myset.insert(9)
print 'My set contains ', len(myset)

rawDescription: Python - Can't get overload to work

description: Python - Can't get overload to work


code:
filenames = ['file1', 'file2', 'file3']
for filename in filenames:
    with open(filename) as f:
        for line in f:
            print line,

rawDescription: python equivalent of GNU 'cat' that shows unique lines

description: Python equivalent of GNU 'cat' that shows unique lines


code:
import sys
filename = __file__.decode(sys.getfilesystemencoding())
print type(filename), filename

rawDescription: Getting unicode path of current running script


code:
matches=re.findall(r'(demeanou?r)', input)
print matches, len(matches)

rawDescription: regex is counting only one pattern, when two same patterns are kept consecutive.why?

description: Regex is counting only one pattern, when two same patterns are kept consecutive.why


code:
selected_features = fs.transform(feature_names_example)

print selected_features[0] # Select the one example
# ['sepal length (cm)' 'petal length (cm)' 'petal width (cm)']

rawDescription: Extracting selected feature names from scikit pipeline


code:
import threading
class MyThreadClass(threading.Thread):
  def __init__(self, fruits):
    threading.Thread.__init__(self)
    self.fruits = fruits

  def run(self):
    self.fruits.append('banana')

list_fruit = ['apple', 'orange']
print 'BEFORE:', list_fruit
thread = MyThreadClass(list_fruit)
thread.start()
thread.join() #Wait for the thread to finish
print 'AFTER:', list_fruit

rawDescription: how to avoid global in python threads when sharing resources?

description: Avoid global in python threads when sharing resources


code:
from stem.control import Controller

from stem.util.str_tools import get_size_label

with Controller.from_port() as controller:
  controller.authenticate()

  bw_rate = get_size_label(int(controller.get_conf('BandwidthRate', '0')))
  bw_burst = get_size_label(int(controller.get_conf('BandwidthBurst', '0')))
  print "you're configured to relay at most %s/s, with bursts up to %s/s" % (bw_rate, bw_burst)

rawDescription: How read the bandwidth limit parameter from Tor with Stem


code:
item = [0,1,2,3,4,5,6,7,8,9]

for element in item:
    if element not in z:
        print element

rawDescription: Finding elements not in a list

description: Finding elements not in a list


code:
def print_nonrec_depthfirst(node):
    stack = [node]
    while stack:
        node = stack.pop()
        print node
        for c in node.children:
            stack.append(c)

rawDescription: Print tree without recursion

description: Print tree without recursion


rawDescription: Finding Incrementing value from list

description: Finding Incrementing value from list


code: print 'Var1: {}'.format(value.Var1)

rawDescription: Get the object name of a specific object from namedtuple


code:
for status in tweepy.Cursor(api.user_timeline,id=screen_name).items(count):
        print status.text+'\n'

rawDescription: Python - Tweepy, retrieving the created_at


code:
for album,song in song_database.iteritems():
    for s in song:
        for sdir in 'Songs_1', 'Songs_2', 'Songs_3':
            try:
                src_dir_1 = src_dir + "/" + sdir + "/" + album + "/" + s + ".mp3"
                shutil.copy2(src_dir_1,dest_dir)
                print src_dir_1
            except IOError:
                pass

rawDescription: Need to implement nested try statements with one exception


code:
word = 'holiday'
word = change(word)
print word

rawDescription: concatenating strings?

description: Concatenating strings


code:
import subprocess

text = "This is the text"

data = subprocess.Popen(['python', 'other_script.py'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=text)
print data[0]

rawDescription: Pipe into raw_input() in Python

description: Pipe into raw_input() in Python


rawDescription: Possible to go over nested lists with for loops in a similar way to dicts?


code:
import SocketServer
import SimpleHTTPServer
import BaseHTTPServer
from webbrowser import open
import hanging_threads

PORT = 8005
RUNNING = True

class CustomHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        print "GET"
        if self.path=='/atom.json':
            # return data
            return
        elif self.path == '/shutdown':
            httpd.shutdown()
            print 'shutdown'
            # quit server, this line is never reached
        else:
            # serve other files
            SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

class ThreadingHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
    allow_reuse_address = True

httpd = ThreadingHTTPServer(('0.0.0.0', PORT), CustomHandler)

print "serving at port", PORT
open('http://localhost:{}/three.html'.format(PORT))
httpd.serve_forever()

print 'closed'

rawDescription: Shutting down python TCPServer by custom handler


code:
import sys

with open("test.out", 'w') as f:
  sys.stdout = f

  i=0
  while i<10:
    a=raw_input('Write a number')
    print 'Result%s'%str(2*a)
    i+=1

rawDescription: How to write output of terminal to file

description: Write output of terminal to file


code:
import datetime
from pyspark.sql import Row
from pyspark.sql.functions import col

row = Row("vacationdate")

df = sc.parallelize([
    row(datetime.date(2015, 10, 07)),
    row(datetime.date(1971, 01, 01))
]).toDF()

rawDescription: Pyspark changing type of column from date to string


code:
for start in nodes():
    for path in simple_paths(start, 4):
        print path

rawDescription: Sub graph isomorphism

description: Sub graph isomorphism


code:
def diff(a, b):
    y = []
    for x in a:
        if x not in b:
            y.append(x)
        else:
            b.remove(x)
    return y

with open('output_ref.txt', 'r') as file1:
    with open('output_ref1.txt', 'r') as file2:
        same = diff(list(file1), list(file2))
        print same
        print "\n"

if '\n' in same:
    same.remove('\n')

with open('some_output_file.txt', 'w') as FO:
    for line in same:
        FO.write(line)

rawDescription: Python : Compare two files

description: Python : Compare two files


code:
def hex(s):
    _hexer = "0123456789ABCDEF"

    if not all([var in _hexer for var in s.upper()]):
        print "Invalid string"
        return None

    return sum([_hexer.find(var) * 16 ** i for i, var in enumerate(reversed(s.upper()))])

rawDescription: Writing a function to convert hex to decimal


code:
# Python 2.7.6
class A(object):
    a_static_var = 'A static var'

class B(A):
    b_static_var = 'B static var'

print dir(B)

rawDescription: Get list (key-value dictionary) of static variables, while respecting inheritance


code:
output_dict = {}
current_alphabet = ""
current_vendor = ""
for td in data:
    for p in td.find_all('p'):
        print p.text.strip()
        if p.get('class')[0] == 'style6':
            current_alphabet = p.text.strip()
            vendors = {}
            output_dict[current_alphabet] = vendors
            continue
        if p.get('class')[0] == 'style4':
            print "Here"
            current_vendor = p.text.strip()
            category = []
            output_dict[current_alphabet][current_vendor] = category
            continue
        output_dict[current_alphabet][current_vendor].append(p.text.strip())

rawDescription: Using BeautifulSoup to find a tag and evaluate whether it fits some criteria


code:
import base64
import urllib2
from xml.dom.minidom import Node, Document, parseString
from xml.etree import ElementTree as ET
from xml.etree.ElementTree import XML, fromstring, tostring

print outputxml ##Printing xml correctly,  outputxml contains xml above
content = ET.fromstring(outputxml)
boxes = content.find('boxes')
print boxes

boxes
content #note that the content is the root level node - boxes
content.find('boxtype')

rawDescription: XML Not Parsing in Python 2.7 with ElementTree


code:
import os
print os.environ["test1"]

rawDescription: Read Bash Variables into a Python Script


code:
word = '"Some word"'
print word

rawDescription: printing double quotes around a variable

description: Printing double quotes around a variable


code:
import urllib

f = { 'eventName' : 'myEvent',
      'eventDescription' : 'cool event',
      'url' : 'http://www.google.com'}

print 'POST&%s' % urllib.urlencode(f)

rawDescription: How do I URL encode this

description: URL encode this


code:
import urllib2
f = urllib2.urlopen('http://slackbuilds.org/slackbuilds/14.1/games/berusky/berusky.info')
r = f.read()
for line in r.splitlines():
    if "REQUIRES" in line:
        print line,

rawDescription: How get line from web site with Python

description: How get line from web site with Python


code:
from collections import Counter
tree = [Counter()] * 3
tree[0] is tree[1]
tree[0] is tree[2]
another_counter = Counter()
tree[0] is another_counter
for counter in tree: print id(counter)

rawDescription: Python list simultaneous update

description: Python list simultaneous update


code:
replacementDict = {}
replacementDict["firstRep"]  = "1st, "
replacementDict["secondRep"] = "2nd, "
replacementDict["thirdRep"]  = "3rd, "

print "{firstRep}{secondRep}{thirdRep}".format(**replacementDict)
# 1st, 2nd, 3rd,

rawDescription: using .format() with a lot of replacement fields

description: Using .format() with a lot of replacement fields


code:
import sys
from itertools import imap
T = int(raw_input())
lines = sys.stdin.readlines()
lis = imap(str, sorted(imap(int, lines)))
print "\n".join(lis)

rawDescription: Turbo sort - Time Limit Exceeded

description: Turbo sort - Time Limit Exceeded


code:
from itertools import *

a='aaaabbbccccddddefgggghhhhhiiiiiijjjkkklllmnooooooppppppppqqqqqqsssstuuvv'

def grp_isl():
    [k for k,g in groupby(a) if len(list(islice(g,0,2)))==2]

def grpby():
    [k for k,g in groupby(a) if len(list(g))>=2]

def chn():
    set(x[1] for x in chain(izip(*([iter(a)] * 2)), izip(*([iter(a[1:])] * 2))) if x[0] == x[1])

def dread():
    set(a[i] for i in range(1, len(a)) if a[i] == a[i-1])

def xdread():
    set(a[i] for i in xrange(1, len(a)) if a[i] == a[i-1])

def inrow():
    inRow = []
    last = None
    for x in a:
        if last == x and (len(inRow) == 0 or inRow[-1] != x):
            inRow.append(last)
        last = x

def zipp():
    set(x[0] for x in zip(a,a[1:]) if x[0]==x[1])

def izipp():
    set(x[0] for x in izip(a,a[1:]) if x[0]==x[1])

if __name__=="__main__":
    import timeit
    print "islice",timeit.timeit("grp_isl()", setup="from __main__ import grp_isl")
    print "grpby",timeit.timeit("grpby()", setup="from __main__ import grpby")
    print "dread",timeit.timeit("dread()", setup="from __main__ import dread")
    print "xdread",timeit.timeit("xdread()", setup="from __main__ import xdread")
    print "chain",timeit.timeit("chn()", setup="from __main__ import chn")
    print "inrow",timeit.timeit("inrow()", setup="from __main__ import inrow")
    print "zip",timeit.timeit("zipp()", setup="from __main__ import zipp")
    print "izip",timeit.timeit("izipp()", setup="from __main__ import izipp")

rawDescription: Repeating elements in list Python

description: Repeating elements in list Python


code:
#!/usr/bin/env python

import itertools

items = {
    'fruits' : ('Orange', 'Apple'),
    'toys' : ('GI-Joe', ),
    'electronics' : ('VCR', ),
    'sporting_goods' : ('Racquet', )
}

def combinate(items, size=3):
    if size > len(items):
        raise Exception("Lower the `size` or add more products, dude!")

    for cats in itertools.combinations(items.keys(), size):
        cat_items = [[products for products in items[cat]] for cat in cats]
        for x in itertools.product(*cat_items):
            yield zip(cats, x)

if __name__ == '__main__':
    for x in combinate(items):
        print x

rawDescription: Creating restricted permutations of a list of items by category


code:
from lxml import html
doc = html.fromstring(body)
print doc.text_content()

rawDescription: Use html5lib to convert an HTML fragment to plain text


code:
import sys
import datetime
import time
import subprocess

# call like this: python startTime.py $PID

pid = sys.argv[1]
proc = subprocess.Popen(['ps','-eo','pid,etime'], stdout=subprocess.PIPE)
# get data from stdout
proc.wait()
results = proc.stdout.readlines()
# parse data (should only be one)
for result in results:
    try:
        result.strip()
        if result.split()[0] == pid:
            pidInfo = result.split()[1]
            # stop after the first one we find
            break
    except IndexError:
        pass # ignore it
else:
    # didn't find one
    print "Process PID", pid, "doesn't seem to exist!"
    sys.exit(0)
pidInfo = [result.split()[1] for result in results
           if result.split()[0] == pid][0]
pidInfo = pidInfo.partition("-")
if pidInfo[1] == '-':
    # there is a day
    days = int(pidInfo[0])
    rest = pidInfo[2].split(":")
    hours = int(rest[0])
    minutes = int(rest[1])
    seconds = int(rest[2])
else:
    days = 0
    rest = pidInfo[0].split(":")
    if len(rest) == 3:
        hours = int(rest[0])
        minutes = int(rest[1])
        seconds = int(rest[2])
    elif len(rest) == 2:
        hours = 0
        minutes = int(rest[0])
        seconds = int(rest[1])
    else:
        hours = 0
        minutes = 0
        seconds = int(rest[0])

# get the start time
secondsSinceStart = days*24*3600 + hours*3600 + minutes*60 + seconds
# unix time (in seconds) of start
startTime = time.time() - secondsSinceStart
# final result
print "Process started on",
print datetime.datetime.fromtimestamp(startTime).strftime("%a %b %d at %I:%M:%S %p")

rawDescription: How to retrieve the process start time (or uptime) in python

description: Retrieve the process start time (or uptime) in python


code:
from sqlalchemy import create_engine
from sqlalchemy.engine.reflection import Inspector

# engine = create_engine(...)

insp = Inspector.from_engine(engine)

print "PK: %r" % insp.get_pk_constraint("user")
print "UNIQUE: %r" % insp.get_unique_constraints("user")

rawDescription: SQLAlchemy Determine If Unique Constraint Exists


code:
dicts = {1: {'age': 1, 'cost': 14, 'score': 0.3, 'sum_cost': 0},
         2: {'age': 1, 'cost': 9, 'score': 0.5, 'sum_cost': 0},
         3: {'age': 13, 'cost': 7, 'score': 0.4, 'sum_cost': 0}}

sum_addition = 0

for key, values in sorted(dicts.items(), key=lambda x: x[1].get('score', None)):
    if dicts[key].get('score'): #By default gives None when key is not available
        dicts[key]['sum_cost'] = sum_addition
        sum_addition += dicts[key]['cost']
        print key, dicts[key]

rawDescription: Python dictionary: Add to the key sum of values fulfilling given condition


code:
def print_board(board):
    for counter, row in enumerate(board, 1):
        print '{}  {}'.format(counter, "   ".join(row))

rawDescription: print edited list without actually editing the list

description: Print edited list without actually editing the list


code:
l = [1,2,3,3,4]

print sorted(set(itertools.permutations(l,3)))

rawDescription: Code for generating all unique permutations recursively?


code:
from lxml import etree

attributes = ("name", "address")
context = etree.iterparse("test.xml", tag="document")

for event, element in context:
    for child in element:
        if child.attrib.get("name") in attributes:
            print next(c for c in child).text
    element.clear() # clear the subtree from the memory

rawDescription: lxml event parsing big file to extract only some data

description: Lxml event parsing big file to extract only some data


code:
import copy

class my_op_class(object):
    def __init__(self):
        self.data = 1

lst = list()
my_op = my_op_class()

my_op.param = 2
lst.append(copy.deepcopy(my_op))

my_op.param = 3
lst.append(copy.deepcopy(my_op))

for l in lst:
    print l.data, l.param

rawDescription: Add different object states to a list

description: Add different object states to a list


code:
import argparse

def funcExtended(line):
   return " ".join(line)

def funcBasic(line):
    return line.upper()

p = argparse.ArgumentParser(description="program.py")
p.add_argument("-e", "--extended", dest="func", action="store_const", const=funcExtended, default=funcBasic)
p.add_argument("-s", "--string")
p.add_argument("-f", "--infile")

args = p.parse_args()

def readlines(args):
    if args.infile:
        with open(args.infile,'r') as f:
            for line in f:
                yield line.rstrip("\n")
    elif args.string:
        yield args.string
    else:  #my future default option to grab strings from a database
        print 'This will soon work: extended'

for line in readlines(args):
    print 'Input: ', line
    output = args.func(line)
    print "Output: ", output

rawDescription: Using argparse to choose a function AND supply options


code:
from dateutil.relativedelta import  relativedelta
from dateutil import parser
d = parser.parse(StartPostingDateFilter)
print((d - relativedelta(years=1)).date())
2005-01-01

rawDescription: Separate one year from a data value; taking care of leap years


code:
#!/usr/bin/env python

from itertools import product

lista = [
    'a=aVal1,aVal2',
    'b=bVal1,bVal2,bVal3',
    'c=cVal1,cVal2',
]

newlists = []
for s in lista:
    head, _, tail = s.partition('=')
    newlists.append(['%s=%s' % (head, u) for u in tail.split(',')])

listb = ['&'.join(t) for t in product(*newlists)]

for row in listb:
    print row

rawDescription: How to get different combinations in python?

description: Get different combinations in python


code:
splitted_data = data.split(",")
splitted_data
print splitted_data[1]

rawDescription: How to use split with utf8 coding?

description: Use split with utf8 coding


code:
mylist = ['abc123', 'def456', 'ghi789', 'ABC987', 'aBc654']
sub = 'abc'

print "\n".join(s for s in mylist if sub.lower() in s.lower())

rawDescription: python: finding substring within a list

description: Python: finding substring within a list


code:
sum=0
for i in range(val):
    num=(random.randint(1,99))
    sum=sum+num
    print"random number:", num
print"The total sum is:", sum

rawDescription: Store the numbers of a `for` loop's output, and sum them all?


code:
from __future__ import division
1 / 3
print "{0:.0f}%".format(1/3 * 100)
print "{:.0%}".format(1/3)

rawDescription: how to show Percentage in python

description: Show Percentage in python


code:
data = string.ascii_letters[:36]
print nCr(data,10,1200)

rawDescription: Calculating and putting all possibilities of 36 nCr 10 in a list in Python


code:
#! /usr/bin/env python

import random

class myclass():
    def __init__(self, data):
        self.data = data

def main():
    random.seed(42)

    #Create some instances of myclass
    for i in range(10):
        s = "a%02d = myclass(%d)" % (i, random.randint(0, 99))
        print s
        exec(s) in globals(), locals()

    #Extract all instances of myclass from the locals() dict
    instances = dict((k, v) for k, v in locals().items() if isinstance(v, myclass))

    for name in sorted(instances.keys()):
        print "%s : %2d" % (name, instances[name].data)

if __name__ == "__main__":
    main()

rawDescription: How To Get Values of Variable For All Instances of A Class


rawDescription: Space character in python that won't be interpreted by bash


code:
class Test(object):
    def __init__(self):
        print "WORKS!"

room = globals()['Test']

rawDescription: Using globals() to create class object

description: Using globals() to create class object


code:
total_links_clicks = 0

for url in short_urls:
    query_params = {
         'access_token': ACCESS_TOKEN,
         'link': url.values()}

    endpoint = "https://api-ssl.bitly.com/v3/link/clicks"
    response = requests.get(endpoint, params = query_params)

    data = json.loads(response.content)

    print "link_clicks:" + " " + str(data['data']['link_clicks'])
    total_links_clicks += data['data']['link_clicks']

print 'total link_clicks:', total_links_clicks

rawDescription: Adding all the integers returned from a for loop?


code:
from mythread import MyThread
import Queue
import threading

threadList = ["Thread-1", "Thread-2", "Thread-3"]
nameList = ["One", "Two", "Three", "Four", "Five"]
workQueue = Queue.Queue(10)
threads = []
threadID = 1

# Create new threads
for tName in threadList:
    thread = MyThread(threadID, tName, workQueue)
    thread.start()
    threads.append(thread)
    threadID += 1

# Fill the queue
for word in nameList:
    workQueue.put(word)

# Wait for queue to empty
while not workQueue.empty():
    pass

# Notify threads it's time to exit
for t in threads:
    t.stop()

# Wait for all threads to complete
for t in threads:
    t.join()
print "Exiting Main Thread"

rawDescription: python - global variables

description: Python - global variables


code:
from itertools import chain, islice

with open('abc1') as f:
    n, m = map(int, next(f).split())
    data = chain.from_iterable(map(int, line.split()) for line in f)
    matrix = [list(islice(data, m)) for i in xrange(n)]
    print matrix
    #[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]

rawDescription: reading a file and storing values in a matrix using python

description: Reading a file and storing values in a matrix using python


code:
wordlist = ['construct','destructer','constructs','deconstructs']

from pyparsing import StringEnd, oneOf, FollowedBy, Optional, ZeroOrMore, SkipTo

endOfString = StringEnd()
prefix = oneOf("de con")
suffix = oneOf("er s") + FollowedBy(endOfString)

word = (ZeroOrMore(prefix)("prefixes") +
        SkipTo(suffix | endOfString)("root") +
        Optional(suffix)("suffix"))

for wd in wordlist:
    print wd
    res = word.parseString(wd)
    print res.dump()
    print res.prefixes
    print res.root
    print res.suffix
    print

rawDescription: Parsing words into (prefix, root, suffix) in Python


code:
import fileinput
import re

for line in fileinput.input():
    iter = re.finditer(r'(\'[^\']+\'|"[^"]+")',line)
    for m in iter:
       span = m.span()
       paren = m.group()[0]
       line = line[:span[0]]+paren+'#'*(span[1]-span[0]-2)+paren+line[span[1]:]
       print line.rstrip()

rawDescription: how to replace the characters in strings with '#'s by using regex in Python

description: Replace the characters in strings with '#'s by using regex in Python


code:
from random import randint

def random_with_N_digits(n):
    range_start = 10**(n-1)
    range_end = (10**n)-1
    return randint(range_start, range_end)

print random_with_N_digits(2)
print random_with_N_digits(3)
print random_with_N_digits(4)

rawDescription: How to generate random number with the specific length in python

description: Generate random number with the specific length in python


code:
def parse(data):

    root = {}
    currentDict = root
    prevLevel = -1
    parents = []
    for line in data:
        if line.strip() == '': continue
        level = len(line) - len(line.lstrip(" "))
        key, value = [val.strip() for val in line.split(':', 1)]

        if level > prevLevel and not len(value):
            currentDict[key] = {}
            # if currentDict is not root:
            #     root[key] = currentDict[key]
            parents.append((currentDict, level))
            currentDict = currentDict[key]
            prevLevel = level
        elif level < prevLevel and not len(value):
            parentDict, parentLevel = parents.pop()
            while parentLevel != level:
                if not parents: return root
                parentDict, parentLevel = parents.pop()
            parentDict[key] = {}
            parents.append((parentDict, level))
            # if parentDict is not root:
            #     root[key] = parentDict[key]
            currentDict = parentDict[key]
            prevLevel = level
        else:
            currentDict[key] = value
    return root

with open('data.txt', 'r') as f:
    data = parse(f)
    #for pretty print of nested dict
    import json
    print json.dumps(data,sort_keys=True, indent=4)

rawDescription: Nesting dictionaries while looping through data


code:
def prediction_up_dn_intervals( aPredictorMODEL,                        # >>> http://blog.datadive.net/prediction-intervals-for-random-forests/
                                X_,                                     # aStateVECTOR: X_sampled
                                aPredictorOutputIDX =  0,               # (4,2,2) -> singleQUAD ( LONG.TP/SL, SHORT.TP/SL ) <-- idxMAP( 'LONG', 'TP', 1 )
                                aRequiredPercentile = 95
                                ):
    err_dn      = []
    err_up      = []
    #-----------------------------------------------------------------------------------------------
    if len( X_.shape ) == 1:                                            # for a single X_example run
        preds   = []
        for pred in aPredictorMODEL.estimators_:
            preds.append( pred.predict( X_ )[0,aPredictorOutputIDX] )   # de-array-ification

        err_dn.append( np.percentile( preds,       ( 100 - aRequiredPercentile ) / 2. ) )
        err_up.append( np.percentile( preds, 100 - ( 100 - aRequiredPercentile ) / 2. ) )
    else:
        #------------------------------------------------------------------------------------------
        for x in xrange( len( X_ ) ):                                   # for a multi X_example run
            preds   = []
            for pred in aPredictorMODEL.estimators_:
                preds.append( pred.predict( X_[x] )[0,aPredictorOutputIDX] ) # de-array-ification

            err_dn.append( np.percentile( preds,       ( 100 - aRequiredPercentile ) / 2. ) )
            err_up.append( np.percentile( preds, 100 - ( 100 - aRequiredPercentile ) / 2. ) )
    #-----------------------------------------------------------------------------------------------
    return err_up, err_dn

#numba.jit( 'f8(<<OBJECT>>,f8[:,:],f8[:,:],i8,f8)' )                    # <<OBJECT>> prevents JIT
def getPredictionsOnINTERVAL(   aPredictorENGINE,                       # a MULTI-OBJECTIVE PREDICTOR -> a singleQUAD or a full 4-QUAD (16,0) <-(4,2,2)
                                X_,
                                y_GndTRUTH,                             # (4,2,2) -> (16,0) a MULTI-OBJECTIVE PREDICTOR
                                aPredictionIDX  =  0,                   # (4,2,2) -> singleQUAD ( LONG.TP/SL, SHORT.TP/SL ) <-- idxMAP( 'LONG', 'TP', 1 )
                                percentile      = 75
                                ):
    """
    |>>> getPredictionsOnINTERVAL( loc_PREDICTOR, X_sampled, y_sampled, idxMAP( "LONG", "TP", 1 ), 75 )     1.0                         +0:01:29.375000
    |>>> getPredictionsOnINTERVAL( loc_PREDICTOR, X_sampled, y_sampled, idxMAP( "LONG", "TP", 1 ), 55 )     0.9992532724237898          +0:03:59.922000
    |>>> getPredictionsOnINTERVAL( loc_PREDICTOR, X_sampled, y_sampled, idxMAP( "LONG", "TP", 1 ), 50 )     0.997100939998243           +0:09:16.328000
    |>>> getPredictionsOnINTERVAL( loc_PREDICTOR, X_sampled, y_sampled, idxMAP( "LONG", "TP", 1 ),  5 )     0.31375735746288325         +0:01:16.422000
    """
    correct_on_interval = 0                                                 # correct        = 0. ____________________- faster to keep asINTEGER ... +=1 and only finally make DIV on FLOAT(s) in RET
    #ruth               = y_                                                # Y[idx[trainsize:]]
    err_up, err_dn      = prediction_up_dn_intervals(   aPredictorENGINE,   # ( rf,
                                                        X_,                 #   X[idx[trainsize:]],
                                                        aPredictionIDX,     #   idxMAP( "LONG", "TP", 1 ),
                                                        percentile          #   percentile = 90
                                                        )                   #   )

    #-------------------------------------------------------------------# for a single X_ run
    if ( len( X_.shape ) == 1 ):
        if ( err_dn[0] <= y_GndTRUTH[aPredictionIDX] <= err_up[0] ):
            return 1.
        else:
            return 0.
    #-------------------------------------------------------------------# for a multi X_ run
    for i, val in enumerate( y_GndTRUTH[:,aPredictionIDX] ):            # enumerate( truth )
        if err_dn[i] <= val <= err_up[i]:
            correct_on_interval += 1
    #-------------------------------------------------------------------
    return correct_on_interval / float( y_GndTRUTH.shape[0] )           # print correct / len( truth )

def mapPredictionsOnINTERVAL(   aPredictorENGINE,                       #
                                X_,
                                y_GndTRUTH,
                                aPredictionIDX      =  0,
                                aPercentilleSTEP    =  5
                                ):
    for aPercentille in xrange( aPercentilleSTEP, 100, aPercentilleSTEP ):
        Quotient = getPredictionsOnINTERVAL( aPredictorENGINE, X_, y_GndTRUTH, aPredictionIDX, aPercentille )
        print "{0: > 3d}-percentil   {1: > 6.3f} %".format( aPercentille, 100 * Quotient )
        """
          5%  0.313757
         10%  0.420847
         15%  0.510191
         20%  0.628481
         25%  0.719758
         30%  0.839058
         35%  0.909646
         40%  0.963454
         45%  0.986603
         50%  0.997101
         55%  0.999253
         60%  0.999912
         65%  1.000000 >>> RET/JIT
         70%  1.000000 xxxxxxxxxxxxxx
         75%  1.000000 xxxxxxxxxxxxxx       ???? .fit( X_, y_[:,8:12] ) # .fit() on HORIZON-T0+3???? ... y_GndTRUTH.shape[1] v/s .predict().shape[1]
        """
        if ( Quotient == 1 ):
             return

rawDescription: Why doesn't my Python RandomForestRegressor accurately predict training set data?


code:
import os
print os.path.abspath(__file__)  #this is your current working directory

rawDescription: python reading a video

description: Python reading a video


code:
import timeit

def fib(x):
    if x==0 or x==1: return 1
    else: return fib(x-1)+fib(x-2)

print timeit.Timer('fib(5)', 'from __main__ import fib').timeit()

rawDescription: finding the greatest Fibonacci number within limited time in python

description: Finding the greatest Fibonacci number within limited time in python


code:
import itertools
calclists = [[1,2,3], [4,5,6], [1, 2, 3]]
calcset = set()
for calclist in calclists:
    for x in calclist:
        calcset.add(x)

comb_itr = itertools.combinations(calcset, 2)
for comb in comb_itr:
    print comb

rawDescription: Editing script to account for every combination of two lists


code:
def worker(t):
    d, i = t
    d[i % 10] += 1

if __name__ == "__main__":
    import multiprocessing as mp
    pool = mp.Pool()
    d = mp.Manager().dict()
    for i in range(10):
        d[i] = 0
    pool.map(worker, ((d, i) for i in xrange(1000)))
    pool.close()
    pool.join()
    print d, sum(d.values())

rawDescription: share dict between process

description: Share dict between process


code: print "The company's info is:", str(comp_info)

rawDescription: using a class instead of a list python

description: Using a class instead of a list python


code:
def sem1Sort(semester1, selectionSEM1):
    for period in semester1:
        if period == '1':
            for cls in semester1[period]:
                if cls in selectionSEM1:
                     print cls

rawDescription: Indexing in Python

description: Indexing in Python


code:
from scrapely import Scraper

s = Scraper()

url1 = 'http://www.sabormediterraneo.com/recetas/postres/leche_frita.htm'
data = {'ingreds': ['medio litro de leche',   # first and last items
  u'canela y az\xfacar para espolvorear']}
s.train(url1, data)

url2 = 'http://www.sabormediterraneo.com/recetas/cordero_horno.htm'
print s.scrape(url2)

rawDescription: How to extract a list of items using scrapely?

description: Extract a list of items using scrapely


code:
import numpy as np

length = 1000
monte_carlo_array=np.random.uniform(0.0, 1.0, length)
# print monte_carlo_array
threshold = 0.5
above = 0

for i in range (0,length):
    if monte_carlo_array[i] > threshold:
        above+=1
print above

rawDescription: Difference between C++ random number generation and Python


code:
from win32com.client import Dispatch, WithEvents
from new import classobj

class MyEventHandler(object):
    def OnVisible(self, visible):
        print "got event from object %d" % self.obj_index

listOfObjects = []
for i in range(3):
    handler = classobj('Handler_%s' % i,(MyEventHandler,),{})
    handler.obj_index = i
    ie = Dispatch("InternetExplorer.Application")
    listOfObjects.append(ie)
    WithEvents(ie, handler)

listOfObjects[0].Visible = 1
listOfObjects[2].Visible = 1

rawDescription: How to mimic vb's control array in python for win32com?

description: Mimic vb's control array in python for win32com


code:
ax.legend(loc = 2)
plt.colorbar(p)

print p.get_label()

rawDescription: Legend using PathCollections in matplotlib


code:
from collections import defaultdict
c = defaultdict(int)
for item in l:
    c[item[0]] += 1
print sorted(l, key = lambda x: -c[x[0]])

rawDescription: Python: How to sort a list of lists by the most common first element?


code:
#!/usr/bin/python
"""
memcached objects for use with SQLAlchemy
"""
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import memcache
import sqlalchemy
import sqlalchemy.orm

SQLA_SESSION = sqlalchemy.orm.sessionmaker()

MEMCACHED_CLIENT = memcache.Client(['127.0.0.1:11211'])

class DetachedORMObject(object):
    """
    Session-detached object for use with ORM Mapping.  As the SQLAlchemy
    documentation indicates, creating and closing a session is not analogous
    to creating and closing a database connection.  Connections are pooled by
    the database engine by default.  Creating new sessions is of a minimal
    cost.  Also, objects using this wrapper will not likely interact in with
    the database through the full power of SQLAlchemy queries.

    """
    @classmethod
    def fetch_by_field(cls, field, value):
        """Fetch a mapped orm object with the give field and value"""
        session = SQLA_SESSION()
        try:
            class_object = session.query(cls).filter(field == value).one()
        except sqlalchemy.orm.exc.NoResultFound:
            class_object = None
        finally:
            session.close()
        return class_object

    def update(self):
        """Update the database with the values of the object"""
        session = SQLA_SESSION()
        session.add(self)
        session.commit()
        session.refresh(self)
        session.close()

    def refresh(self):
        """Refresh the object with the values of the database"""
        session = SQLA_SESSION()
        session.add(self)
        session.refresh(self)
        session.close()

    def delete(self):
        """Delete the object from the database"""
        session = SQLA_SESSION()
        session.add(self)
        session.delete(self)
        session.commit()
        session.close()

class MemcachedObject(object):
    """
    Object Wrapper for serializing objects in memcached. Utilizes an abstract
    method, get_isntance_key, to understand how to get and set objects that
    impliment this class.
    """
    @classmethod
    def get_cached_instance(cls, instance_key):
        """Retrieve and return the object matching the instance_key"""
        key = str(cls.__module__ + '.' + cls.__name__ + ':' \
           + str(instance_key))
        print "Memcached Getting:", key
        return MEMCACHED_CLIENT.get(key)

    def set_cached_instance(self, time=0, min_compress_len=0):
        """Set the cached instance of an object"""
        print "Memcached Setting:", self.get_cache_key()
        return MEMCACHED_CLIENT.set(self.get_cache_key(), self, time, \
            min_compress_len)

    def delete_cached_instance(self, time=0):
        """Wrapper for the memcached delete method"""
        print "Memcached Deleting:", self.get_cache_key()
        return MEMCACHED_CLIENT.delete(self.get_cache_key(), time)

    def get_cache_key(self):
        """Prepends the full class path of the object to the instance key"""
        return self.__class__.__module__ + '.' + \
            self.__class__.__name__ + ':' + self.get_instance_key()

    def get_instance_key(self):
        """Get the instance key, must be implemented by child objects"""
        raise NotImplementedError \
            ("'GetInstanceKey' method has not been defined.")

class MemcachedORMObject(DetachedORMObject, MemcachedObject):
    """
    Putting it all together now.  Implements both of the above classes. Worth
    noting is the method for checking to see if the fetch_by_field method is
    invoked using a primary key of the class.  The same technique is used to
    generate an instance key for an instance of the class.
    """
    @classmethod
    def fetch_by_field(cls, field, value):
        """Fetch the requested object from the cache and database"""
        orm_object = None
        matched_primary_key = True
        for key in cls._sa_class_manager.mapper.primary_key:
            if field.key != key.key:
                matched_primary_key = False
        if matched_primary_key:
            orm_object = cls.get_cached_instance('(' + str(value) + ')')
        if orm_object is None:
            orm_object = super(MemcachedORMObject, cls). \
                fetch_by_field(field, value)
            if orm_object is not None:
                orm_object.set_cached_instance()
        return orm_object

    def update(self):
        """Update the object in the database and memcached"""
        DetachedORMObject.update(self)
        self.set_cached_instance()

    def refresh(self):
        """Refresh the object from the database and memcached"""
        DetachedORMObject.refresh(self)
        self.set_cached_instance()

    def delete(self):
        """Delete the object from the database and memcached"""
        DetachedORMObject.delete(self)
        self.delete_cached_instance()

    def get_instance_key(self):
        """Get the instance key, implimenting abstract method in base"""
        key = []
        for column in self._sa_instance_state.manager.mapper.primary_key:
            key.append('(' + str(getattr(self, column.key)) + ')')
        return ''.join(key)

rawDescription: Pylons and Memcached

description: Pylons and Memcached


code:
for i in reference:
    if i not in done:
        print i,

rawDescription: Comparing file contents in Python

description: Comparing file contents in Python


code:
import time

def chap4():
    print "You feel around the room.\n"
    time.sleep(3)
    print "You find a small chest...\n"
    time.sleep(3)
    print "You open the chest...\n"
    time.sleep(2)
    print "pickaxe", "shovel", "lighter", "9mm(0)", "knife"
    while (True):
        chest = raw_input("What will you take?: ")
        if chest == "pickaxe":
            print "You take the pickaxe"
            weapon = "pickaxe"
            break
        elif chest == "shovel":
            print "You take the shovel"
            weapon = shovel
            break
        elif chest == "lighter":
            print "You take the lighter"
            weapon = "lighter"
            break
        elif chest == "9mm":
            print "You take the empty 9mm pistol"
            weapon = "9mm"
            break
        elif chest == "knife":
            print "You take the knife"
            weapon = "knife"
            break
        elif chest == "axe":
            print "You take the axe"
            weapon = "axe"
            break
        else:
            print "Invalid choice. Try again..."
    return (chest)

class Zombie:
    def __init__(self):
        self.hp = 100

def attack(target):
    target.hp -= DAMAGE
    print "Zombie health %s/100"%target.hp
    if target.hp <= 0:
        print "Zombie dies"

def attack1(target1):
    target1.hp -= DAMAGE1
    print "Zombie health %s/100"%target1.hp
    if target1.hp <= 0:
        print "Zombie dies"

def attack2(target2):
    target2.hp -= DAMAGE2
    print "Zombie health %s/100"%target2.hp
    if target2.hp <= 0:
        print "Zombie dies"

def attack3(target3):
    target3.hp -= DAMAGE3
    print "Zombie health %s/100"%target3.hp
    if target3.hp <= 0:
        print "Zombie dies"

def attack4(target4):
    target4.hp -= DAMAGE4
    print "Zombie health %s/100"%target4.hp
    if target4.hp <= 0:
        print "Zombie dies"

def attack5(target5):
    target5.hp -= DAMAGE5
    print "Zombie health %s/100"%target5.hp
    if target5.hp <= 0:
        print "Zombie dies"

DAMAGE = 20
DAMAGE1 = 50
DAMAGE2 = 10
DAMAGE3 = 30
DAMAGE4 = 30
DAMAGE = 25
zombie1 = Zombie()
weapon = chap4()

print "You see a zombie in the distance"
while zombie1.hp > 0:
    user_input = raw_input("What will you do?: ")
    if weapon == "knife":
        if user_input == 'stab':
            attack(zombie1)
    if weapon == "9mm":
        if user_input == 'shoot':
            attack1(zombie1)
    if weapon == "shovel":
        if user_input == "smack":
            attack2(zombie2)
    if weapon == "axe":
        if user_input == "chop":
            attack3(zombie3)
    if weapon == "pickaxe":
        if user_input == "mine":
            attack4(zombie4)
    if weapon == "lighter":
        if user_input == "burn":
            attack5(zombie5)

rawDescription: How do you call something in Python?

description: Call something in Python


code:
from django.contrib.auth.models import User

def compare(user1, user2):
    for field in user1._meta.fields:
        val1 = getattr(user1, field.name)
        val2 = getattr(user2, field.name)
        if val1 != val2:
            print "%s differ '%s' != '%s'"%(field.name, val1, val2)
            break

compare(User(username="anurag"), User(username="uniyal"))

rawDescription: Comparing 2 model entities

description: Comparing 2 model entities


code:
labels = ['id', 'Version', 'chip_name', 'xversion', 'device', 'opt_param',
    'place_effort']
values = [1, 1.0, u'virtex2', u'xilinx11.5', u'xc5vlx50', u'Speed', u'High']

paddedLabels = []
paddedValues = []

for label, value in zip(labels, values):
    value = str(value)
    columnWidth = max(len(label), len(value))
    paddedLabels.append(label.center(columnWidth))
    paddedValues.append(value.center(columnWidth))

print ' '.join(paddedLabels)
print ' '.join(paddedValues)

rawDescription: pretty printer with Python?

description: Pretty printer with Python


code:
# use `numbers = 100` as an example
numbers = 100

prime = []
not_prime = []

for i in range(2,numbers+1):

    if i not in not_prime:
        prime.append(i)

        for x in range(i**2,numbers+1,i):
            not_prime.append(x)

# `enumerate` gives us a tuple of (index, element) in an iterable
for idx, p in enumerate(prime):

    # "{:3d}" is a format string that is similar to the C-style
    # of %X.YA where `X` denotes the character width, `.Y` denotes
    # how many places to display in a floating point number,
    # and `A` denotes the type of what's being printed. Also note,
    # in Python, you don't need to use the `d` in `:3d`, you can just
    # do `{:3}`, but I've included it for your knowledge.
    #
    # the comma says 'don't add a newline after you print this'
    print "{:3d}".format(p),

    # we'll use `idx + 1` to avoid having to deal with the
    # 0-indexing case (i.e., 0 modulo anything is 0)
    if (idx + 1) % 10 == 0:

        # just print a newline
        print

rawDescription: How to print a list of integers in 10 columns?

description: Print a list of integers in 10 columns


code:
a = [1, 2, 3, 4]
print [item if item > 3 else 3 for item in a]
# [3, 3, 3, 4]

rawDescription: Check if all values of a list are less than a certain number, if not set it to that number


code: print re.sub(u'番地$', u'', add.replace(u'丁目', u'－'))

rawDescription: Python - regex with Japanese letters matches only one character


code:
dt = datetime.datetime(2015,8,01,9,30,15,150000)
print dt
pd.Timestamp(dt)
pd.Timestamp(dt) + pd.Timedelta(888)

rawDescription: How to create a pandas.Timestamp from datetime.datetime object and nanoseconds value

description: Create a pandas.Timestamp from datetime.datetime object and nanoseconds value


code:
class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel
    def clean_date_created(self):
        if self.cleaned_fields['date_created'] != self.instance.date_created:
            raise ValidationError, 'date_created has been tampered'
        self.cleaned_fields['date_created']

rawDescription: Django Form with extra information

description: Django Form with extra information


code:
import queue
try:
    res = result.get(block = True, timeout = 10)
except Queue.Empty:
    print error

rawDescription: Calling os.fsync on the stdout/stderr file descriptors kills a subprocess


rawDescription: Using regular expressions to manipulate strings


code:
v1 = [10]

listA = [v1, 11, 12]
listB = [v1, 13, 14]

for i in listA:
    if isinstance(i, list): i[0] += 10
    else: i += 10
for i in listB:
    if isinstance(i, list): i[0] += 10
    else: i += 10

print listA[0]
print listB[0]

rawDescription: Multiple references in separate lists; Python


code:
import re

r = re.compile("[ab]")
text = "abcdedfe falijbijie bbbb laifsjelifjl"

matches = []
replaced = []
pos = 0
for m in r.finditer(text):
    matches.append(m.group(0))
    replaced.append(text[pos:m.start()])
    pos = m.end()
replaced.append(text[pos:])

print matches
print ''.join(replaced)

rawDescription: Capture the contents of a regex and delete them, efficiently


code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame(
{
    'A' : ['x', 'y', 'x', 'y', 'x', 'y', 'x', 'x'],
    'B' : ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],
    'C' : np.random.randn(8),
    'D' : np.random.randn(8)
}
)

print df

rawDescription: Using print to access individual cells of pandas dataframe after groupby


code:
from google.appengine.ext import ndb

class Fingerprint(ndb.Model):
    fingerprint = ndb.StringProperty(required=True)

fingerprint = Fingerprint(fingerprint='abc')
new_key = fingerprint.put()
print new_key.id(), type(new_key.id())

fingerprint = Fingerprint(id='abc', fingerprint='abc')
new_key = fingerprint.put()
print new_key.id(), type(new_key.id())

fingerprint = Fingerprint(id=100, fingerprint='abc')
new_key = fingerprint.put()
print new_key.id(), type(new_key.id())

rawDescription: In Google App Engine datastore (for Python), are ndb key ids by default always integers?


code:
import csv

DELIMITER = chr(255)
data = ["itemA", "itemB", "itemC",
        "Sentence that might contain commas, colons: or even \"quotes\"."]

with open('data.csv', 'wb') as outfile:
    writer = csv.writer(outfile, delimiter=DELIMITER)
    writer.writerow(data)

with open('data.csv', 'rb') as infile:
    reader = csv.reader(infile, delimiter=DELIMITER)
    for row in reader:
        print row

rawDescription: CSV writing strings of text that need a unique delimiter


code: `system("date +%Y-%m-%d")`

rawDescription: How to have automated headers for python files

description: Have automated headers for python files


code:
class Hello:
    __gui = None

    def __init__(self, gui):
        self.__gui = gui

    def run(self):
        print 'Hello world!'

rawDescription: Can we call a python method from java?

description: Can we call a python method from java


code:
item = objectify.Element("item")
item.title = "Best of python"
item.price = 17.98
item.price.set("currency", "EUR")
order = objectify.Element("order")
order.append(item)
order.item.quantity = 3
order.price = sum(item.price * item.quantity
for item in order.item)
import lxml.etree
print lxml.etree.tostring(order, pretty_print=True)

rawDescription: How to convert XML to objects?

description: Convert XML to objects


code:
#!/usr/bin/env python

import itertools
import re

# This algorithm only works if DATA is sorted.
DATA = ["image_0001", "image_0002", "image_0003",
        "image_0010", "image_0011",
        "image_0011-1", "image_0011-2", "image_0011-3",
        "image_0100", "image_9999"]

def extract_number(name):
    # Match the last number in the name and return it as a string,
    # including leading zeroes (that's important for formatting below).
    return re.findall(r"\d+$", name)[0]

def collapse_group(group):
    if len(group) == 1:
        return group[0][1]  # Unique names collapse to themselves.
    first = extract_number(group[0][1])  # Fetch range
    last = extract_number(group[-1][1])  # of this group.
    # Cheap way to compute the string length of the upper bound,
    # discarding leading zeroes.
    length = len(str(int(last)))
    # Now we have the length of the variable part of the names,
    # the rest is only formatting.
    return "%s[%s-%s]" % (group[0][1][:-length],
        first[-length:], last[-length:])

groups = [collapse_group(tuple(group)) \
    for key, group in itertools.groupby(enumerate(DATA),
        lambda(index, name): index - int(extract_number(name)))]

print groups

rawDescription: How to list an image sequence in an efficient way? Numercial sequence comparison in Python

description: List an image sequence in an efficient way? Numercial sequence comparison in Python


code:
from inspect import getmembers, ismethod

for name, attr in getmembers(foo, ismethod):
    print '{}: {}'.format(name, type(attr))

rawDescription: Finding out type within dir()

description: Finding out type within dir()


code:
source
#!/bin/env python

import multiprocessing, threading, time
import logging, os, Queue, sys

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)-4s %(levelname)s %(threadName)s %(message)s",
    datefmt="%H:%M:%S",
    stream=sys.stderr,
)

def scan_dir(topdir):
    try:
        for name in os.listdir(topdir):
            path = os.path.join(topdir, name)
            yield (path, os.path.isdir(path))
    except OSError:
        logging.error('uhoh: %s', topdir)

def scan_dir_queue(inqueue, outqueue):
    logging.info('start')
    while True:
        try:
            dir_item = inqueue.get_nowait()
        except Queue.Empty:
            break

        res = list( scan_dir(dir_item) )
        logging.debug('- %d paths', len(res))
        for path,isdir in res:
            outqueue.put( (path,isdir) )
            if isdir:
                inqueue.put(path)
    logging.info('done')

def thread_master(root):
    dir_queue = Queue.Queue() # pylint: disable=E1101
    dir_queue.put(root)
    result_queue = Queue.Queue()

    threads = [
        threading.Thread(
            target=scan_dir_queue, args=[dir_queue, result_queue]
        )
        for _ in range(multiprocessing.cpu_count())
    ]

    for th in threads:
        th.start()
    for th in threads:
        th.join()
    return result_queue.queue

if __name__ == "__main__":
    topdir = os.path.expanduser('~')

    start = time.time()
    res = thread_master(topdir)
    print 'threaded:', time.time() - start
    print len(res), 'paths'

    def mywalk(topdir):
        for (dirpath, _dirnames, filenames) in os.walk(topdir):
            for name in filenames:
                yield os.path.join(dirpath, name)
    start = time.time()
    res = list(mywalk(topdir))
    print 'os.walk:', time.time() - start
    print len(res), 'paths'

rawDescription: Python multiprocessing in Nuke causing Nuke to hang


code:
import file1
print file1.x

rawDescription: Include / extend modules in python

description: Include / extend modules in python


code:
a_lines_raw=a_file.readlines()
b_lines_raw=b_file.readlines()

a_lines=[ line.split() for line in a_lines_raw ]
b_lines=[ line.split() for line in b_lines_raw ]

for a_line in a_lines:
    for b_line in b_lines:
        if compare(a_line,b_line):
            print "Match:", str(a_line), str(b_line)

rawDescription: Comparing 2 txt files in Python

description: Comparing 2 txt files in Python


code:
from datetime import date, timedelta

cur_date = date.today()
for i in xrange(365):
    start_date = cur_date
    cur_date = cur_date-timedelta(days=1)
    print "the date is between {} and {}".format(start_date.strftime("%Y-%m-%d"), cur_date.strftime("%Y-%m-%d"))

rawDescription: How to subtract a day from datetime format in python?

description: Subtract a day from datetime format in python


code:
element = driver.find_element_by_xpath('//div[@class="d3-tip n"][count(*)=0]')
print element.text

rawDescription: Unable to read text from a div tag in python


code:
html = '
 anyContent
'
print re.search('(?is)
]*>(.+?)
', html).group(1)

rawDescription: Python regex match to parse html

description: Python regex match to parse html


code:
#uses an iterable `seq` to populate a dictionary.
#the function `keyFunc` will be called on each element of seq to generate keys.
#if two elements `a` and `b` have the same key,
#`compFunc(a,b)` will return which element should belong in the dict.
def make_dict(seq, keyFunc, compFunc):
    d = {}
    for element in seq:
        key = keyFunc(element)
        if key not in d:
            d[key] = element
        else:
            d[key] = compFunc(d[key], element)
    return d

#I've put all your elements in quotes so that it's valid python.
#You can use whatever types you prefer,
#as long as the date and name can be used as a key,
#and the time supports comparison.
NestedList = [
['100', '08/08/2012', '08:00', 'John Smith'],
['100', '08/09/2012', '09:20', 'John Smith'],
['100', '08/08/2012', '10:00', 'John Smith'],
['131', '08/10/2012', '08:00', 'Jane Williams'],
['131', '08/12/2012', '22:00', 'Jane Williams']
]

#the key is generated from the element's date and name
keyFunc = lambda x: (x[1], x[3])

#prefer the element with the smaller time
compFunc = lambda a,b: a if a[2] < b[2] else b

NewList = make_dict(NestedList, keyFunc, compFunc).values()
NewList.sort() #optional

print NewList

rawDescription: Reorganizing and picking out elements of nested lists


code:
import itertools

for name, fruit, hobby in itertools.product(names, fruits, hobbies):
    print '%(name)s likes to eat %(fruit)s while %(hobby)s' % {'name':name, 'fruit':fruit, 'hobby':hobby}

rawDescription: Python string formating using values from multiple lists


code:
x='dord word'
import re
print re.split(r"d\b",x)

rawDescription: Split by suffix with Python regular expression


rawDescription: Shortest way to replace parts of strings in NumPy array


code:
from lxml import etree
xml_str = "<parent><child>text</child><child>other text</child></parent>"
root = etree.fromstring(xml_str)
print etree.tostring(root, pretty_print=True)

rawDescription: Python pretty XML printer for XML string


code:
import re

pat = re.compile('^(#+)',re.MULTILINE)

ch = '''# Section
## Subsection
## Subsection
# Section
## Subsection
### Subsubsection
### Subsubsection
## Subsection
### Subsubsection
### Subsubsection
#### Sub4section
#### Sub4section
#### Sub4section
#### Sub4section
##### Sub5section
#### Sub4section
##### Sub5section
##### Sub5section
### Subsubsection
### Subsubsection
#### Sub4section
#### Sub4section
## Subsection
### Subsubsection
### Subsubsection
### Subsubsection
#### Sub4section
##### Sub5section
##### Sub5section
### Subsubsection
#### Sub4section
## Subsection
### Subsubsection
### Subsubsection
# Section
## Subsection
## Subsection
# Section
## Subsection
### Subsubsection
#### Sub4section
#### Sub4section
#### Sub4section
##### Sub5section
#### Sub4section
### Subsubsection
## Subsection
### Subsubsection
# Section
## Subsection
'''

def cbk(match, nb = [0] ):
    if len(match.group())==len(nb):
        nb[-1] += 1
    elif  len(match.group())>len(nb):
        nb.append(1)
    else:
        nb[:] = nb[0:len(match.group())]
        nb[-1] += 1
    return match.group()+' '+('.'.join(map(str,nb)))

ch = pat.sub(cbk,ch)
print ch

rawDescription: "Counting" with multiple regular expressions matches in Python


code:
from Queue import Queue
def bfs(numRows, numCols, pacmanPos, grid):
    q = Queue()
    q.put((pacmanPos, [pacmanPos]))
    visited = set([pacmanPos])
    while not q.empty():
        currentPos, path = q.get()
        row, column = currentPos
        if grid[row][column] == '.':
            return len(path), path
        for newPos in [(row, column + 1), (row + 1, column), (row, column - 1), (row - 1, column)]:
            if newPos not in visited and newPos[0] < numRows and newPos[1] < numCols and grid[newPos[0]][newPos[1]] != '%':
                q.put((newPos, path + [newPos]))
                visited.add(newPos)

    return 0, []

r = 7
c = 20

pacmanPos = (3, 9)

grid = ['%%%%%%%%%%%%%%%%%%%%',
        '%--------------%---%',
        '%-%%-%%-%%-%%-%%-%-%',
        '%--------P-------%-%',
        '%%%%%%%%%%%%%%%%%%-%',
        '%.-----------------%',
        '%%%%%%%%%%%%%%%%%%%%']

pathLength, path = bfs(r, c, pacmanPos, grid)
print 'Path Length: {} \nPath: {}'.format(pathLength, path)

rawDescription: Find the path for Pacman from expanded/explored nodes


code:
s = list((i, d.get(i)) for i in L)
print s

rawDescription: Sorting dictionary keys by values in a list?


rawDescription: stripping away code in python using "re.sub"

description: Stripping away code in python using "re.sub"


code:
for doc in cursor:
    print doc["place"]["bounding_box"]["coordinates"]

rawDescription: Extracting data from a mongo collection

description: Extracting data from a mongo collection


code:
for function, genes in FunctionsWithGenes.iteritems():
    print function, genes

df

rawDescription: Pandas: Splitting and editing file based on dictionary


code:
def fib(a, b, n):
    if n == 1:
        return a
    else:
        return fib(a+b, a, n-1)

print fib(1, 0, 10) # prints 55

rawDescription: Non Brute Force Solution to Project Euler 25


code:
import networkx as nx

G = nx.MultiGraph()
G.add_edge(1,2,weight=.5)
G.add_edge(1,2,weight=.6)
G.add_edge(1,2,weight=.4)
G.add_edge(1,3,weight=.5)

cutoff = .45

top = [edge for edge in G.edges_iter(data=True)
       if edge[2]['weight'] > cutoff]

print top

rawDescription: Getting top n edges in a graph networkx

description: Getting top n edges in a graph networkx


code:
x = 0
def f():
    global x
    x = 1
f()
print x #=>1

rawDescription: Defining lists as global variables in Python