code:
from bs4 import BeautifulSoup

soup = BeautifulSoup()
div = soup.new_tag('div')

a_tags = ["1", "2", "3", "4", "5"]
a_str = ",".join(a_tags)

div.append(BeautifulSoup(a_str, 'html.parser'))

soup.append(div)
print soup

rawDescription: Append markup string to a tag in BeautifulSoup


rawDescription: Python, Regular expression for taking a file name in a string


rawDescription: Check for pattern recursively

description: Check for pattern recursively


code:
class A(object):
    pass

def silly(arg=None):
    print 'This is silly', arg

b = A()
b.my = silly
b.my()

rawDescription: Wrap an object into a derived class

description: Wrap an object into a derived class


code:
import numpy

def single_insert_or_delete(s1,s2):

    s1 = s1.lower()
    s2 = s2.lower()

    l1 = [i for i in s1] # convert the string into an array
    l2 = [j for j in s2] # convert the second string in an array

    if s1 == s2:
        flag = 0

    elif abs(len(s1)-len(s2)) == 1:
        if len(l2) > len(l1):
            l1,l2 = l2,l1
        l3 = list(numpy.in1d(l1,l2))
    # This will compare the elements of 2 arrays and return bool values based on the match

        if l3.count(False) > 1: # count the number of false values
            flag = 2
        else:
            flag = 1
    else:
        flag = 2

    return flag

print "Output-1 ", single_insert_or_delete("book","boot")
print "Output-2 ", single_insert_or_delete("Java","Python")
print "Output-3 ", single_insert_or_delete("dog","Dog")
print "Output-4 ", single_insert_or_delete("sin","sink")
print "Output-5 ", single_insert_or_delete("programming","programing")

rawDescription: A single letter insert or delete function


code:
a = 0
b = 0
value = 0
for value in range(100):
    if a == value and b == value:
        print "foo bar"
    a = a + 1
    b = b + 1

rawDescription: Execute multiple if loops

description: Execute multiple if loops


code:
def carriage_return():
    if sys.platform.lower().startswith('win'):
        print '\r'
    else:
        print chr(27) + '[A'

rawDescription: Problems refreshing stdout line using print with python


code:
l = []
for key, value in locals().items():
    if callable(value) and value.__module__ == __name__:
        l.append(key)
print l

rawDescription: Getting a list of locally-defined functions in python


code:
def myfunc(*args):
    for argument in args:
        print "myfunc was given", argument

rawDescription: Passing multiple values to a function call (Python)


code:
for element in tree.iter():
    if element.tag in ('item', 'test'):
        print element.text

rawDescription: ElementTree findall 'or' operator

description: ElementTree findall 'or' operator


code:
import Tkinter
import time

class App:
    def __init__(self, root):
        self.root = root
        self.left_mouse_pressed = False
        self.right_mouse_pressed = False

        f = Tkinter.Frame(width=100, height=100, background="cyan")
        f.pack()

        f.bind("<Button-1>", self.onAnyofTwoPressed)
        f.bind("<Button-3>", self.onAnyofTwoPressed)

        f.bind("<ButtonRelease-1>", self.resetPressedState)
        f.bind("<ButtonRelease-3>", self.resetPressedState)

    def onAnyofTwoPressed(self, event):
        if self.left_mouse_pressed and self.left_mouse_pressed <= time.time():
            self.left_mouse_pressed = False

        if self.right_mouse_pressed and self.right_mouse_pressed <= time.time():
            self.right_mouse_pressed = False

        if event.num==1:
            self.left_mouse_pressed = time.time() + 500
        if event.num==3:
            self.right_mouse_pressed = time.time() + 500

    def resetPressedState(self, event):
        if self.left_mouse_pressed and self.right_mouse_pressed:
            print 'both pressed'
        elif self.left_mouse_pressed:
            print 'left pressed'
        elif self.right_mouse_pressed:
            print 'rigth pressed'

        self.left_mouse_pressed = False
        self.right_mouse_pressed = False

root=Tkinter.Tk()
app = App(root)
root.mainloop()

rawDescription: Detect simultaneous left and right clicks in python gui


code: print session.query(User).join(SystemAdmin).filter(User.name == 'john doe').exists()

rawDescription: SQLAlchemy mapping joined tables' columns to one object


code:
gobject.threads_init()

class T(threading.Thread):
    loop = threading.Event()
    stop = False

    def start(self, *args):
        super(T, self).start()

    def run(self):
        while not self.stop:
            #Waits for button to be clicked.#
            self.loop.wait()

            #Bot Startup sequence.#
            deploy_button.set_label('Disconnect')
            irc = botOpenSocket()
            joinRoom(irc, self)
            readbuffer = ""
            irc.send("CAP REQ :twitch.tv/membership\r\n")
            irc.send("CAP REQ :twitch.tv/commands\r\n")
            irc.send("CAP REQ :twitch.tv/tags\r\n")

            try:
                with file("commands.json","r") as commandsDatabase:
                    commands = json.load(commandsDatabase)
            except IOError:
                commands = {}
                with file("commands.json","w") as commandsDatabase:
                    json.dump(commands, commandsDatabase)

            globalcommands = {"spank": True}
            moderatorcommands = {"addcom": True, "delcom": True}
            stringspace = " "
            nothing = ""
            now = time.time()
            cooldown = lambda: time.time() > now + 1

            #Keeps reading chat and awsering.#
            while self.loop.is_set():
                try:
                    readbuffer = readbuffer + irc.recv(1024)
                    temp = string.split(readbuffer, "\n")
                    readbuffer = temp.pop()
                except:
                    pass
                else:
                    for line in temp:
                ###Essenciais###--------------------------------------------------------------------------------------------------------------------------------------------
                #Mostra a linha que e dada pelo servidor de IRC (So pelo sim pelo nao).-----------------------------------------------------------------------
                        print (line)
                #---------------------------------------------------------------------------------------------------------------------------------------------
                #Impede que seja desconectado pelo servidor de IRC.-------------------------------------------------------------------------------------------
                        if line.startswith('PING'):
                            irc.send('PONG ' + line.split( ) [ 1 ] + '\r\n')
                            print "PONGED BACK"
                            break
                #---------------------------------------------------------------------------------------------------------------------------------------------
                #Le a linha que e dada pelo servidor de IRC e devevole o utilizador, a menssagem e o canal. Volta se algum for nulo.--------------------------
                        user = getUser(line)
                        message = getMessage(line)
                        channel = getChannel(line)
                        moderator = getModerator(line)
                        if channel == None or user == None or message == None:
                            break
                #---------------------------------------------------------------------------------------------------------------------------------------------
                #Formata o texto e mostra mostra na consola.--------------------------------------------------------------------------------------------------
                        print channel + ": " + user + " > " + message
                #---------------------------------------------------------------------------------------------------------------------------------------------
                ###Essenciais END###----------------------------------------------------------------------------------------------------------------------------------------

                        if message == "!commands\r":
                            globalcommandskeys = str(globalcommands.keys()).replace("[", "").replace("]", "")
                            moderatorcommandskeys = str(moderatorcommands.keys()).replace("[", "").replace("]", "")
                            channelcommandskeys = str(commands.keys()).replace("[", "").replace("]", "")
                            sendMessage(irc, "Global commands: " + globalcommandskeys)
                            if channelcommandskeys != "":
                                sendMessage(irc, "Channel specific commands: " + channelcommandskeys )
                            if moderator == "1":
                                sendMessage(irc, "Moderator commands: " + moderatorcommandskeys)
                            break

                        if message.startswith("!addcom ") and (moderator == "1" or user == channel):
                            if message.count(" ") >= 2:
                                try:
                                    commandadd = command_add(message)
                                    answer = command_answer(message)
                                except IndexError:
                                    sendMessage(irc, user + " the command is used this way !addcom !<command_name> <command_answer>")
                                    break
                                if globalcommands.has_key(commandadd) or moderatorcommands.has_key(commandadd):
                                    sendMessage(irc, user + " you can't add the command " + '"!' + commandadd + '" !!!')
                                    break
                                try:
                                    commands[commandadd]
                                except KeyError:
                                    commands[commandadd] = answer
                                    sendMessage(irc, user + " the command !" + commandadd + " has been added!!!")
                                    with file("commands.json","w") as commandsDatabase:
                                        json.dump(commands, commandsDatabase)
                                    break
                                sendMessage(irc, user + " the command you tried to add alredy exists!!!")
                                break
                            sendMessage(irc, user + " the command is used this way !addcom !<command_name> <command_answer>")
                            break

                        if message.startswith("!delcom ") and (moderator == "1" or user == channel):
                            if message.count(" ") == 1:
                                try:
                                    commanddel = command_del(message)
                                except IndexError:
                                    sendMessage(irc, user + "the command is used this way !delcom !<command_name>")
                                    break
                                if globalcommands.has_key(commanddel) or moderatorcommands.has_key(commanddel):
                                    sendMessage(irc, user + " you can't delete the command " + '"!' + commanddel + '" !!!')
                                    break
                                try:
                                    commands[commanddel]
                                except KeyError:
                                    sendMessage(irc, user + " the command you tried to delete doens't exist!!!")
                                    break
                                del commands[commanddel]
                                sendMessage(irc, user + " the command !" + commanddel + " has been deleted!!!")
                                with file("commands.json","w") as commandsDatabase:
                                    json.dump(commands, commandsDatabase)
                                break
                            sendMessage(irc, user + " the command is used this way !delcom !<command_name>")
                            break

                        if message.startswith("!"):
                            if cooldown() == True:
                                if message.count(" ") == 0:
                                    try:
                                        command = getCommand(message)
                                    except IndexError:
                                        break
                                    try:
                                        sendMessage(irc, commands[command])
                                        now = time.time()
                                        cooldown = lambda: time.time() > now + 10
                                    except KeyError:
                                        break
                                if message.count(" ") == 1:
                                    try:
                                        command = getCommandSpaced(message)
                                        target = getString(message)
                                    except IndexError:
                                        break
                                    try:
                                        replacing = commands[command]
                                        sendMessage(irc, replacing.replace("$target", target))
                                        now = time.time()
                                        cooldown = lambda: time.time() > now + 10
                                    except KeyError:
                                        break
                                break

            #When button is clicked again do the shutdown sequence.#
            print "coming here"
            irc.close()
            deploy_button.set_label('Connect')

            #Waits for 0.1 seconds before going to the top again, just to be sure.#
            time.sleep(0.1)

rawDescription: Making a irc bot with a gui. Dificulties with desconecting on toggle button toggle


code:
khmer_number = [u'\u17e0', u'\u17e1', u'\u17e2', u'\u17e3', u'\u17e4', u'\u17e5', u'\u17e6', u'\u17e7', u'\u17e8', u'\u17e9']
international_number = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
number_map = dict(zip(khmer_number, international_number))

ls = [khmer_number[n] for n in [4, 2, 5, 3, 7, 0]]
print list(naive_sort(ls, key=lambda x: number_map[x]))

rawDescription: What Can I do for sorting algorithm with my number language?


code:
tmp = x.findNext('img')
print tmp.get_text().strip()

rawDescription: How to extract exact data from html page?

description: Extract exact data from html page


code:
import argparse

def main():
    parser = argparse.ArgumentParser()

    parser.add_argument('-2', dest='two', action='store_true')
    parser.add_argument('-3', dest='three')
    parser.add_argument('-4', dest='four')
    parser.add_argument('-5', dest='five')

    args = parser.parse_args()

    if not args.two:
        if args.three is None or args.four is None:
            parser.error('without -2, *both* -3 <a> *and* -4 <b> are required')

    print args
    return 0

rawDescription: Python argparse conditional requirements


code:
def explore():
    expr_string = raw_input("Enter an expression: ")
    expr = sympify(expr_string)
    variables = sorted(expr.free_symbols)
    for truth_values in cartes([False, True], repeat=len(variables)):
        values = dict(zip(variables, truth_values))
        print sorted(values.items()), expr.subs(values)

rawDescription: Truth tables in python using sympy

description: Truth tables in python using sympy


code:
def msg_received(msg):
  print "received:", msg

c = Channel()
c.register("public_address_string_returned_in_process_A", msg_received)

rawDescription: How to create channel with socket in Python

description: Create channel with socket in Python


code:
def n_times_print(c, n):
    print c*n

n_times_print("=", 16)

rawDescription: An easy way of printing a n-times-character string in Python


code:
tests= [
    "A/B/C/D",
    "A//C//",
    "A//C//E",
    "A///D",
    "///D",
    ]
for test in tests:
    print "\nTest:", test
    row = test.split("/")
    ncols = len(row)
    # modelling the OP's code
    # (using xlrd-style 0-relative column indexes)
    d = {}
    for cnum in xrange(ncols):
        if row[cnum]:
            k = row[cnum]
            v = (cnum, ncols) #### BUG; should be ncols - 1 ("inclusive")
            print "outer", cnum, k, '=>', v
            d[k] = v
            for cend in xrange(cnum + 1, ncols):
                if row[cend]:
                    k = row[cnum]
                    v = (cnum, cend - 1)
                    print "inner", cnum, cend, k, '=>', v
                    d[k] = v
                    break
    print d
    # modelling a slightly better algorithm
    d = {}
    prev = None
    for cnum in xrange(ncols):
        key = row[cnum]
        if key:
            d[key] = [cnum, cnum]
            prev = key
        elif prev:
            d[prev][1] = cnum
    print d
    # if tuples are really needed (can't imagine why)
    for k in d:
        d[k] = tuple(d[k])
    print d

rawDescription: Why is win32com so much slower than xlrd?


code:
for i, line in enumerate(airtemp):
    if i < 12:  # Skip lines numbered 0 through 11
        continue
    print line

rawDescription: How can I skip the header in Python?

description: Skip the header in Python


code:
import xml.etree.cElementTree as etree

with open(xml_file_path) as xml_file:
    tree = etree.iterparse(xml_file)
    for items in tree:
        for item in items:
            print item.text

rawDescription: Python XML to dictionary to iterate over items


code:
import pandas as pd

df = pd.read_csv('test/EURUSD-2015-11.csv', header=None, parse_dates=[1],
                  names =['eurusd','Date','a','b'], sep=",")

#sort values if not sorted
df = df.sort_values('Date')
print df.head()

rawDescription: Create a DataFrame with DatetimeIndex with unique values by adding a timedelta


code:
#!/usr/bin/env python

class human(object):
    def __init__(self):
        pass

    def nameBaby(self, name):
        self.name = name

    def getName(self):
        return self.name

kid = human()
print kid.getName()
kid.nameBaby('Bruce')
print kid.getName()

rawDescription: Instance Variable Assignment in def other than __init__?


code:
with open('file.txt','r') as f_open:
    data = f_open.read()

print data

rawDescription: importing a text file in python

description: Importing a text file in python


code:
from lxml import etree
from lxml.html.soupparser import fromstring
html = '<iframe width="560" height="315" src="" frameborder="0" allowfullscreen></iframe>'
root = fromstring(html)
print etree.tostring(root.find('iframe'), encoding='utf-8', method="html")

rawDescription: lxml modify tags prevent

description: Lxml modify tags prevent


code:
# python ≥ 2.5
import itertools, heapq

def make_sparse(sequence):
    grouped= sorted(sequence)
    item_counts= []
    for item, item_seq in itertools.groupby(grouped):
        count= max(enumerate(item_seq))[0] + 1
        item_counts.append( (-count, item) ) # negative count for heapq purposes
    heapq.heapify(item_counts)

    count1, item1= heapq.heappop(item_counts)
    yield item1; count1+= 1
    while True:
        try:
            count2, item2= heapq.heappop(item_counts)
        except IndexError: # no other item remains
            break
        yield item2; count2+= 1
        if count1 < 0:
            heapq.heappush(item_counts, (count1, item1))
        item1, count1= item2, count2

    # loop is done, produce remaining item1 items
    while count1 < 0:
        yield item1; count1+= 1

if __name__ == "__main__":
    # initial example
    print list(make_sparse(
        "duck duck duck duck goose goose goose dog".split()))
    # updated example
    print list(make_sparse([
        'duck', 'duck', 'duck', 'duck', 'duck', 'duck',
        'goose', 'goose', 'goose', 'goose', 'dog', 'dog']))
    # now a hard case: item 'a' appears more than:
    # > total_len//2 times if total_len is even
    # > total_len//2+1 times if total_len is odd
    print list(make_sparse("aaaaaabbcc"))

rawDescription: Sorting a string in array, making it sparsely populated


code:
from collections import Counter, defaultdict
import numpy as np
import itertools

def pmf(L):
    C = Counter(L)
    total = float(sum(C.values()))
    for key in C:
        C[key]/=total
    return C

N = 10

h = 2
n = 2**h
X = np.array(list(itertools.product([-1,1],repeat = n))).T

result = defaultdict(list)

for _ in xrange(N):
    A = (np.random.randint(2, size=(h,n)))*2-1
    B = np.dot(A,X)
    probs = pmf([tuple(x) for x in B.T.tolist()])
    print probs
    result[ frozenset(probs.items()) ].append( A ) #append the one you need

rawDescription: How to group identical collections together

description: Group identical collections together


code:
for block in gen_range(100000000,600000000,100):
    print block

rawDescription: Generate list of range tuples with given boundaries in python


code:
if 5 in (data.n for data in myList):
    print "Found it"

rawDescription: Difference between two "contains" operations for python lists


code:
t = "s" if True else "b"
print t
t = ("s" if True else "b")
print t
t = ("s" if True else "b"),

rawDescription: Facebook login with Django

description: Facebook login with Django


code:
from collections import defaultdict
import csv

species = defaultdict(list)

with open('speciesandbss.csv', 'rb') as inputfile:
    for row in csv.reader(inputfile):
        species[row[0]].append(row[1])

for name in sorted(species, key=str.lower):
    print '{} = {}'.format(name, species[name])

rawDescription: Obtain and store adjacent values from a .csv file (Python)


code:
from collections import Counter
c = Counter()
def fib3(n):
    c[n] += 1
    if n < 9980:
        return 1
    else:
        return fib3(n-1) + fib3(n-2) + fib3(n-3)

fib3(10000)
print c

rawDescription: Analysis of running time on recursive function


code:
#!/usr/bin/python

class A(object):

    def __init__(self):
        self.configured = []
        self.set_up = []

    def __configure(self):
        self.configured.append("A")

    def setup(self):
        self.__configure()
        self.set_up.append("A")

class B(A):

    def __configure(self):
        self.configured.append("B")

    def setup(self):
        super(B, self).setup()
        self.__configure()
        self.set_up.append("B")

class Z(B):
    pass

if __name__ == "__main__":
    z = Z()
    z.setup()
    print "configured: %s" % z.configured
    print "set up: %s" % z.set_up

rawDescription: Child method is run instead of parent's?

description: Child method is run instead of parent's


code:
from pyparsing import QuotedString, Group, delimitedList, OneOrMore

# a row of headings or data is a list of quoted strings, delimited by ';'s
qs = QuotedString('"')
datarow = Group(delimitedList(qs, ';'))

# an entire data set is a single data row containing the headings, followed by
# one or more data rows containing the data
dataset_parser = datarow("headings") + OneOrMore(datarow)("rows")

# parse the returned response
data = dataset_parser.parseString(response)

# create dict by zipping headings with each row's data values
datadict = [dict(zip(data.headings, row)) for row in data.rows]

print datadict

rawDescription: I am getting this sort of CSV data while making Http request to the CSV file. Very malformed string


code:
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument

fp = open('diveintopython.pdf', 'rb')
parser = PDFParser(fp)
doc = PDFDocument(parser)

print doc.info  # The "Info" metadata

rawDescription: Reading the pdf properties/metadata in python


code:
b=1
d = 0
#generates a list of numbers.
while b<100:
    b=b+1
    x = 0.0
    a = 0
    #generates a list of numbers less than b.
    while x<b:
        x=x+1
        #this will check for divisors.
        if (b/x)-int(b/x) == 0.0:
            a=a+1
    if a==2:
        #if it finds a prime it will add it.
        d=d+b
print d

rawDescription: python sum of primes

description: Python sum of primes


code:
import re
pattern = re.compile("ab|bc")
print pattern.match("abbbb")
print pattern.match("fooo")
print pattern.match("bcdd")
print pattern.match("abc")

rawDescription: is this possible in re in python?

description: Is this possible in re in python


code:
import csv
for d in csv.DictReader(open('file.txt')): print d

rawDescription: Excel parsing in python and forming dictionary


rawDescription: How can i move more than one item from one list to another in Python

description: Move more than one item from one list to another in Python


code:
import socket
import struct
import binascii

rawSocket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0003))

while True:

    packet = rawSocket.recvfrom(2048)

    ethernet_header = packet[0][0:14]
    ethernet_detailed = struct.unpack("!6s6s2s", ethernet_header)

    arp_header = packet[0][14:42]
    arp_detailed = struct.unpack("2s2s1s1s2s6s4s6s4s", arp_header)

    # skip non-ARP packets
    ethertype = ethernet_detailed[2]
    if ethertype != '\x08\x06':
        continue

    print "****************_ETHERNET_FRAME_****************"
    print "Dest MAC:        ", binascii.hexlify(ethernet_detailed[0])
    print "Source MAC:      ", binascii.hexlify(ethernet_detailed[1])
    print "Type:            ", binascii.hexlify(ethertype)
    print "************************************************"
    print "******************_ARP_HEADER_******************"
    print "Hardware type:   ", binascii.hexlify(arp_detailed[0])
    print "Protocol type:   ", binascii.hexlify(arp_detailed[1])
    print "Hardware size:   ", binascii.hexlify(arp_detailed[2])
    print "Protocol size:   ", binascii.hexlify(arp_detailed[3])
    print "Opcode:          ", binascii.hexlify(arp_detailed[4])
    print "Source MAC:      ", binascii.hexlify(arp_detailed[5])
    print "Source IP:       ", socket.inet_ntoa(arp_detailed[6])
    print "Dest MAC:        ", binascii.hexlify(arp_detailed[7])
    print "Dest IP:         ", socket.inet_ntoa(arp_detailed[8])
    print "*************************************************\n"

rawDescription: Python arp sniffing raw socket no reply packets


code:
from bs4 import BeautifulSoup

page = """

Alpha blurb

* content here one
* content here two

Bravo blurb

* content here one
* content here two
* content here tree
* content here four
* content here fyve
* content here seeks

Charlie blurb

* content here four
* content here fyve
* content here seeks

Delta blurb

* blah

"""
soup = BeautifulSoup(page)

for h2 in soup.find_all("h2"):

    print h2.text

    # loop over siblings until h2 is met (or no more siblings left)
    for item in h2.next_siblings:
        if item.name == "h2":
            break

        print item.strip()

    print "----"

rawDescription: Bsoup4 extracting child elements that are not wrapped by a parent element


code:
import numpy as np
import ast

# Example input for a 3x2 matrix of numbers
n,m = 3,2
s = raw_input("Enter space sep. matrix elements: ")

# Use literal_eval
items  = map(ast.literal_eval, s.split(' '))

# Check to see if it matches
assert(len(items) == n*m)

# Convert to numpy array and reshape to the right size
matrix = np.array(items).reshape((n,m))

print matrix

rawDescription: How to add element from user in numpy matrix from user for nxn matrix?

description: Add element from user in numpy matrix from user for nxn matrix


code:
import decimal
context = decimal.Context(prec=100)
frct = context.sqrt(decimal.Decimal(2))

print 'Original square root:', frct

for i in range(7):
    frct = context.divide(1, frct - int(frct))
    print 'Attempt %d: %s' % (i, frct)

rawDescription: Python code on continued fractions

description: Python code on continued fractions


code:
from collections import defaultdict
from random import random as rn

class Grid(object):
    def __init__(self, spacing):
        self.spacing = spacing
        self.griddict = defaultdict(int)

    def add_point(self, coords):
        """
        `vid`, a voxel id, is a tuple of indices, indicating one grid
        bin for each dimension, e.g. (1, 5, 2)
        rule: i_x = int(floor(x_coord / spacing))
        """
        vid = tuple([int(c//self.spacing) for c in coords])
        self.griddict[vid] += 1

    def get_point(self, coords):
        vid = tuple([int(c//self.spacing) for c in coords])
        return self.griddict[vid]

    def vid_centercoords(self, vid):
        """
        Return the real coordinates in space for a certain voxel,
        which is identified by its voxel id `vid` (a tuple of indices).
        """
        return tuple([(i-1)*self.spacing + self.spacing/2 for i in vid])

N = 20
fillpoints = [(rn(),rn(),rn()) for _ in xrange(N)]
testpoints = [(rn(),rn(),rn()) for _ in xrange(N)]

grid = Grid(spacing=0.3)

for p in fillpoints:
    grid.add_point(p)

print [grid.get_point(p) for p in testpoints]

rawDescription: Matching a real number key in a Python dictionary


code:
for location in results:
    print location["trends"][0]['name']

rawDescription: Twitter Trending List Printing First Character Instead of First Entry


code:
import __builtin__

_max = __builtin__.max
def max(*args):
    print "Inside non standard max function"
    __builtin__.max = _max
    return min(args)

__builtin__.max = max

rawDescription: Function that replaces itself with builtin after first execution


code:
f = open("data.txt", "r")
for line in f:
    if "</MetaDataAtStart>" in line:
        print "Wavelength data was not found"
        break;
    if "Wavelength" in line:
        print line.split("=")[1]

rawDescription: How to read in metadata (with tags) from a text file using Python

description: Read in metadata (with tags) from a text file using Python


code:
# This doesn't work
from win32com.client import Dispatch

# This line will always create a new window
ie = Dispatch("InternetExplorer.Application")

print ie.LocationURL

rawDescription: how to get internet explorer address bar for python

description: Get internet explorer address bar for python


code:
inputs = [{
 "author": ["TIM ROCK"],
 "book_name": ["Truk Lagoon, Pohnpei & Kosrae Dive Guide"],
 "category": "Travel",
},
{
 "author": ["JOY"],
 "book_name": ["PARSER"],
 "category": "Accomp",
}
]

from csv import DictWriter
from cStringIO import StringIO

buf=StringIO()
c=DictWriter(buf, fieldnames=['author', 'book_name', 'category'])
c.writeheader()
c.writerows(inputs)
print buf.getvalue()

rawDescription: Arranging one items per one column in a row of csv file in scrapy python


code: print list(set([x.split(' ')[1] for x in a.split('\r\n\r\n') if len(x) > 1]))

rawDescription: Python Split Operator

description: Python Split Operator


code:
import git
r = git.Repo('.')
c1 = r.head.commit
c2 = r.commit('HEAD~1')
print c1.diff(c2)[0].diff

rawDescription: GitPython: git.diff(commit_a, commit_b) always returns empty string


code:
import csv
from lxml import html

with open('input.csv', 'r') as csvfile:
    urls = [row[0] for row in csv.reader(csvfile)]

for url in urls:
    print url

    doc = html.parse(url)
    linkziel = 'http://dandydiary.de/de'
    if doc.xpath('//a[@href=$url]', url=linkziel):
        for anchor_node in doc.xpath('//a[@href=$url]', url=linkziel):
            if anchor_node.xpath('./ancestor::div[contains(@class, "sidebar")]'):
                print 'Sidebar'
            elif anchor_node.xpath('./parent::div[contains(@class, "widget")]'):
                print 'Sidebar'
            elif anchor_node.xpath('./ancestor::div[contains(@class, "comment")]'):
                print 'Kommentar'
            elif anchor_node.xpath('./ancestor::div[contains(@id, "comment")]'):
                print 'Kommentar'
            elif anchor_node.xpath('./ancestor::div[contains(@class, "foot")]'):
                print "Footer"
            elif anchor_node.xpath('./ancestor::div[contains(@id, "foot")]'):
                print "Footer"
            elif anchor_node.xpath('./ancestor::div[contains(@class, "post")]'):
                print "Contextual"
            else:
                print 'Unidentified Link'
    else:
        print 'Link is Dead'

rawDescription: Importing a .csv with urls and process them (PYTHON)


code:
for child in data['data']['children']:
    print child['data']['title']

rawDescription: Error parsing JSON in python

description: Error parsing JSON in python


code:
def get_locator(tree, page, element):
    return tree.findtext('.//page[@name="{page}"]/uielement[@name="{element}"]/locator'.format(page=page, element=element), 'Not Found')

tree = ET.fromstring(data)

print get_locator(tree, 'login', 'username')
print get_locator(tree, 'login', 'password')
print get_locator(tree, 'login', 'invalid element')

rawDescription: Python function for getting locator values


code:
for match in pyparsing.originalTextFor(pyparsing.nestedExpr('(',')')).searchString(text):
  print match[0]

rawDescription: Regex: Match brackets both greedy and non greedy


code:
x = "scatter"
y = "-n"
z = 1
cmd = "{} {} {}".format(x, y, z)
print cmd

rawDescription: outputting a string to terminal and otherwise in a correct format

description: Outputting a string to terminal and otherwise in a correct format


code:
input_c = [chr(int(c,base=2)) for c in input_l]
print ''.join(input_c)

rawDescription: Binary to ASCII in Python

description: Binary to ASCII in Python


code:
from itertools import combinations

result = []
count = 0
for w in (''.join(c) for c in combinations(words, 2)):
    count += 1
    if count % 2000 == 0:
        print 'Progress: {0} combinations scanned'.format(count)

    if sorted(w) == letters:
        result.append(w)

print result

rawDescription: How to occasionaly provide feedback for a long running Python list comprehension

description: Occasionaly provide feedback for a long running Python list comprehension


code:
import arcpy,fnmatch,os

rootPath = r"D:\desktop"
pattern = '*.pdf'
counter = 0
for root, dirs, files in os.walk(rootPath):
    for filename in fnmatch.filter(files, pattern):
        os.startfile("%s/%s" %(rootPath,filename))
        counter = counter + 1
print counter

rawDescription: Open pdf files format with python

description: Open pdf files format with python


rawDescription: pandas dataframe, uniquifying a column

description: Pandas dataframe, uniquifying a column


code:
from collections import Counter

d = {
    'KEY1': {
        'key2_1': 5,
        'key2_2': 1,
        'key2_3': 3
    },
    'KEY2': {
        'key2_1': 2,
        'key2_2': 3,
        'key2_3': 4
    }
}

c = Counter()
for k, v in d.iteritems():
    c.update({(k, k1): v1 for k1, v1 in v.iteritems()})

print c.most_common(3)

rawDescription: Ordering a nested dictionary by the frequency of the nested value


code:
from bs4 import BeautifulSoup as Soup

html = """

<title>Test Page</title>

test

"""
soup = Soup(html)

title = soup.find('title')
meta = soup.new_tag('meta')
meta['content'] = "text/html; charset=UTF-8"
meta['http-equiv'] = "Content-Type"
title.insert_after(meta)

print soup

rawDescription: Inserting into a html file using python

description: Inserting into a html file using python


code: print list(fns_to_delete)

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

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


rawDescription: Date list in text

description: Date list in text


code:
# My 3D list
list3D = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]

# Find
def findEntry(data, x):
    for i, page in enumerate(data):
        for j, row in enumerate(page):
            for k, col in enumerate(row):
                if col == x:
                    def myref(y): data[i][j][k] = y
                    return myref

# Get a reference to the first cell containing 4
updater = findEntry(list3D, 4)

# Update that cell to be 44 instead
updater(44)

# list3D is now [[[1, 2], [3, 44]], [[5, 6], [7, 8]]]
print list3D

rawDescription: Is it possible to get a pointer of a list member?


code:
input = tf.placeholder(tf.float32, [None])  # Defines an arbitrary-sized vector.
result = tf.expand_dims(input, 1)

print result.get_shape()  # ==> TensorShape([Dimension(None), Dimension(1)])

rawDescription: Theano Dimshuffle equivalent in Google's TensorFlow?


code:
for country in zip(countries, hdi_list):
    if country[1] == max_value:
        print country[0], country[1]

rawDescription: Find max number in .CSV file in Python

description: Find max number in .CSV file in Python


code:
result_list = [] # This would store the end result with int
# The below list is the input list with floats
l1 = [0.0218354767535,
0.0218265654136,
0.0218184623573,
0.021811165579,
0.0218046731276,
0.0217989831063,
0.0217940936718,
0.0217900030345,
0.0217867094577,
0.0217842112574,
0.021782506802]

#For each float in the list, find the decimal part, convert to string, extract last 3, convert to int and append to result list
for each_item in l1:
    result_list.append(int(str(each_item-int(each_item))[-3:]))

print result_list

rawDescription: picking last 3 digits from long decimal values

description: Picking last 3 digits from long decimal values


code:
f = db.search(Query().time != 0)[-1]
print f['glucose']

rawDescription: How to save information to json as array python

description: Save information to json as array python


code:
sample = {'city': 'San Francisco', 2: 'Neato', 'name': 'Zed', 1: 'Wow', 'age': 39, 'height': 74}

for i in sample.keys():
    print '{} : {}\n'.format(i,sample.get(i))

rawDescription: How to print entries from a dictionary without quotes and with a newline in between?

description: Print entries from a dictionary without quotes and with a newline in between


code:
from lxml import etree as xml
xml_str="""
<note>
<property name="country">India</property>
<property name="city">Bangalore</property>
</note>
"""
xm=xml.fromstring(xml_str)

for a in xm.iter():
    if a.tag == "property" and a.attrib.get("name") == "country":
        a.text = "Africa"
print xml.tostring(xm)

rawDescription: I Want to replace a value of XLM Using python


code:
L = [1,2,3,4,5]
mylist = [L[:] for _ in range(3)]
mylist[0][0] = 6
print L
print mylist

rawDescription: Repeat a List Within a List X Number of Times


code:
iss_answer = ['Professor Plum','Dinning Room','Rope']

def computer_1(array):

    all_cards = ['professor plum','colonel mustard',
                 'mrs. white','mr. green','miss scarlet',
                 'mrs. peacock', 'revolver','wrench',
                 'candle stick','lead pipe','knife','rope',
                 'kitchen','hall','dinning room','lounge',
                 'study','billiard room',
                 'conservatory','library','ballroom']

    s = [i for i in all_cards if i.strip().lower() not in map(str.strip,map(str.lower,iss_answer))]
    print s

    return s

computer_1(iss_answer)

rawDescription: Removing items in one array based on whats in another array - python


code:
from csv import reader
LIMITINTER= 10
with open("fichierin.txt") as f:
    read = reader(f,delimiter="\t")
    first = last =  None
    for line in read:
        if last is None:
            first = last = line
        else:
            if abs(int(line[1]) - int(last[2])) < LIMITINTER :
                last = line
                continue
            else:

                print last[0], first[1], last[2]
                first = last = line

    print last[0], first[1], last[2]

rawDescription: Create intervals from list of positions

description: Create intervals from list of positions


code: print "hello world"

rawDescription: Python open file in specific application using xdg-utils?


rawDescription: Python here document without newlines at top and bottom


code:
#!/usr/bin/env python2

# Let's take this string:
a = "palindnilddafa"
# I surround with a try/catch block, explanation following
try:
  # In this loop I go from length of a minus 1 to 0.
  # range can take 3 params: start, end, increment
  # This way I start from the thow longest subsring,
  # the one without the first char and without the last
  # and go on this way
  for i in range(len(a)-1, 0, -1):
    # In this loop I want to know how many
    # Palidnrome of i length I can do, that
    # is len(a) - i, and I take all
    # I start from the end to find the largest first
    for j in range(len(a) - i):
      # this is a little triky.
      # string[start:end] is the slice operator
      # as string are like arrays (but unmutable).
      # So I take from j to j+i, all the offsets
      # The result of "foo"[1:3] is "oo", to be clear.
      # with string[::-1] you take all elements but in the
      # reverse order
      # The check string1 in string2 checks if string1 is a
      # substring of string2
      if a[j:j+i][::-1] in a:
        # If it is I cannot break, 'couse I'll go on on the first
        # cycle, so I rise an exception passing as argument the substring
        # found
        raise Exception(a[j:j+i][::-1])

# And then I catch the exception, carrying the message
# Which is the palindrome, and I print some info
except Exception as e:
  # You can pass many things comma-separated to print (this is python2!)
  print e, "is the longest palindrome of", a
  # Or you can use printf formatting style
  print "It's %d long and start from %d" % (len(str(e)), a.index(str(e)))

rawDescription: Python: Clever ways at string manipulation


code:
import pygame

class Puff(pygame.sprite.Sprite):

    def __init__(self, screen):
        pygame.sprite.Sprite.__init__(self)
        self.screen = screen

        try:
            self.imagePuff = pygame.image.load("puff.jpg").convert()
        except:
            raise UserWarning, "Unable to find Puff image"
        self.image = self.imagePuff
        self.imagePuff.set_colorkey((0,0,255))
        self.rect = self.image.get_rect()
        self.x = self.screen.get_width()/3
        self.y = self.screen.get_height()/3+10
        self.rect.center = (self.x, self.y)

        self.lifespan = 60
        self.speed = 1
        self.count = 0
        self.angle = 0

    def update(self):

        oldCenter = self.rect.center
        self.image = pygame.transform.rotate(self.imagePuff, self.angle)
        self.rect = self.image.get_rect()
        self.rect.center = oldCenter

    def calcPos(self):
        self.x -= 5
        self.y = self.y

    def turnLeft(self):
        self.angle = (self.angle + 45) % 360

    def turnRight(self):
        self.angle = (self.angle - 45) % 360

if __name__  == "__main__":
    pygame.init()
    screen = pygame.display.set_mode((400,300))
    clock = pygame.time.Clock()
    background = pygame.Surface(screen.get_size())
    background.fill((0, 0, 0))
    pygame.display.set_caption("Spinning sprite!!!")
    ball = Puff(screen)
    sprites = pygame.sprite.Group()
    sprites.add(ball)
    keepGoing = True
    while keepGoing:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
        ball.turnRight()
        sprites.clear(screen, background)
        sprites.update()
        sprites.draw(screen)
        pygame.display.flip()
        clock.tick(30)

rawDescription: How to animate scale and rotate in pygame using rotozoom

description: Animate scale and rotate in pygame using rotozoom


code:
import re
from collections import Counter

words = []
pattern = re.compile("^[a-z][a-z0-9]*$")

with open('alice.txt','r') as f:
    for line in f:
        for word in line.split():
            lword = word.lower()
            if pattern.match(lword):
                if len(lword) >= 10:
                     words.append("Extralong")
                elif len(lword) in [7, 8, 9] :
                     words.append("Long")
                elif len(lword) in [5, 6] :
                     words.append("Medium")
                elif len(lword) in [2, 3, 4] :
                     words.append("Small")
                elif len(lword) == 1 and lword in "aeiou":
                     words.append("Vowel")
                else: # lword is 0
                     words.append("Nothing")
    print dict(Counter(words))

rawDescription: Python basic count the different type in txt file


code:
s = "abcdefghijklmnopqrstuvwxyz"
import itertools
for val in itertools.islice(s, 0, None, 8):
  print val
for val in itertools.islice(s, 1, None, 8):
  print val
for val in itertools.islice(s, 2, None, 8):
  print val

rawDescription: Use Python zip to save data in separate columns from a binary file


code:
line = '1;2;3;1,2,3;4'

csv_file = StringIO.StringIO(line)
csv_reader = csv.reader(csv_file, delimiter=';')
for data in csv_reader:
    # output: ['1', '2', '3', '1,2,3', '4']
    print data

rawDescription: How to parse a string using a CSV parser in Python?

description: Parse a string using a CSV parser in Python


code:
from operator import itemgetter
a = [-2, 1, 5, 3, 8, 5, 6]
b = [1, 2, 5]
print itemgetter(*b)(a)

rawDescription: Access multiple elements of list knowing their index


code:
a = ['item1', 'item2', 'item3','item4']
print reduce(lambda x, y: {y: x}, reversed(a))

rawDescription: List to nested dictionary in python

description: List to nested dictionary in python


rawDescription: losing encoding of string when pushing to json in python

description: Losing encoding of string when pushing to json in python


code:
seed = 3345
maximum = 1000000
generator = random.Random(seed)
while True:
    print generator.random(maximum)

rawDescription: Algorithm to generate pseudorandom numbers serie, reproducible knowing initial seed


code:
import re
colourFormat = '\033[{0}m'
colourStr = colourFormat.format(32)
resetStr = colourFormat.format(0)
s = "This is a sentence where I talk about interesting stuff like sencha tea."

lastMatch = 0
formattedText = ''
for match in re.finditer(r'sen\w+', s):
    start, end = match.span()
    formattedText += s[lastMatch: start]
    formattedText += colourStr
    formattedText += s[start: end]
    formattedText += resetStr
    lastMatch = end
formattedText += s[lastMatch:]

print formattedText

rawDescription: How can I highlight regex matches in Python?

description: Highlight regex matches in Python


code: print filters.__dict__

rawDescription: Adding a modules functions automatically to a dict


code:
s = 123456
output = []
while s > 0:
    s, r = divmod(s, 10)
    output.append(r)

fmt='{:<12d}'*len(output)
print fmt.format(*output[::-1])

rawDescription: Taking an input of any length of number and splitting them one character each


code:
from sklearn.metrics import classification_report
from sklearn.metrics import f1_score
from sklearn.metrics import roc_auc_score

print "Classification report: \n", (classification_report(y_test, predictedlabel))
print "F1 micro averaging:",(f1_score(y_test, predictedlabel, average='micro'))
print "ROC: ",(roc_auc_score(y_test, predictedlabel))

rawDescription: How to Calculate F1 measure in multi-label classification?


code: print pd.DataFrame([x for x in flagInfoSeries], index=flagInfoSeries.index)

rawDescription: Python/pandas: data frame from series of dict: optimization


code:
print '{hours}:{minutes:02d}{postfix}'.format(
    hours=hours,
    minutes=minutes,
    postfix='pm' if afternoon else 'am'
)

rawDescription: 24-Hour Time Conversion to 12-Hour Clock (ProblemSetQuestion) on Python


rawDescription: Python: How to create a 2D list that each cell is holding two different values


code:
from django.core.urlresolvers import reverse

class ExampleView(TemplateView):
    some_var = reverse('example2')
    print some_var

rawDescription: Django reference url in views.py

description: Django reference url in views.py


code:
import numpy as np

def bit_iter(n):
    for i in xrange(2**(n)):
        bits = []
        for j in xrange(n):
            if i & (2**j) != 0:
                bits.append(1)
            else:
                bits.append(0)
        yield bits

def maximal_sum(M):
    Morig = M.copy()
    n, m = M.shape
    best = None
    for bits in bit_iter(n + m):
        nvec = bits[:n]
        mvec = bits[n:]
        assert(len(nvec) + len(mvec) == len(bits))
        M = Morig.copy()
        for i, v in enumerate(nvec):
            if v == 0:
                M[i, :] *= -1
        for i, v in enumerate(mvec):
            if v == 0:
                M[:, i] *= -1
        if best == None or np.sum(M) > np.sum(best):
            best = M
    return best

M = np.matrix([
    [2,2,2,2],
    [2,2,-2,2],
    [2,2,2,2],
    [2,2,2,1],
])
print maximal_sum(M)
M = np.matrix([
        [1,2],[3,-4]
    ])
print maximal_sum(M)
M = np.matrix([
        [2,-2,2,2],
        [2,2,2,2],
        [2,2,-2,2],
        [2,2,2,2],
        [2,2,2,1],
    ])
print maximal_sum(M)

rawDescription: Make the sum of integers in a matrix maximal by multiplying rows and columns by -1


code:
import sqlite3
print sqlite3.paramstyle

rawDescription: Passing SQLite variables in Python

description: Passing SQLite variables in Python


code:
valid_doors = {"door 1" : 1.0, "door 2" : 2.0, "door 3" : 3.0, "door 4": 4.0}

x = raw_input("Enter first door: ")
y = raw_input("Enter second door: ")

try:
    print abs(valid_doors[x] - valid_doors[y]) * 2.5
except KeyError:
    print 'not a door'

rawDescription: Distance of different points on a list

description: Distance of different points on a list


code:
import random

def sampling_mean(population, k, times):
    # Part of this is lifted straight from random.py
    _int = int
    _random = random.random

    n = len(population)
    kf = float(k)
    result = []

    if not 0 <= k <= n:
        raise ValueError, "sample larger than population"

    for t in range(times):
        selected = set()
        sum_ = 0
        selected_add = selected.add

        for i in xrange(k):
            j = _int(_random() * n)
            while j in selected:
                j = _int(_random() * n)
            selected_add(j)
            sum_ += population[j]

        mean = sum_/kf
        result.append(mean)
    return result

sampling_mean(x, 1000000, 100)

rawDescription: Python 3.1 - Memory Error during sampling of a large list


code: print 'this works:', re.sub('START.*END', 'NEWTEXT', text, flags=re.DOTALL)

rawDescription: Regular expression multi-line replacement in Python


code:
import cgi, os, sys

print 'Content-Type: text/plain\n'

if 'PATH_INFO' in os.environ:
    print os.environ['PATH_INFO']
form = cgi.FieldStorage()
if 'param' in form:
    print form['param'].value

rawDescription: Python + LigHTTPd + CGI: How to configure for executing single script for entire subpath


code:
from itertools import groupby

with open('data.txt') as f_input:
    for k, g in groupby(f_input, lambda x: x.split()[0].split('.')[1:]):
        entries = list(g)
        with open(entries[0].split()[0], 'w') as f_output:
            for line in entries:
                print line
                f_output.write(line.split(' ', 1)[1].lstrip())

rawDescription: Split one txt to several txt files with particular name in Python


code:
import re
print re.findall("<person>(.*?)</person>", input)
print re.findall("<location>(.*?)</location>", input)

rawDescription: Regex to find words between two tags

description: Regex to find words between two tags


code:
from StringIO import StringIO
import gzip
from bs4 import BeautifulSoup
import re
import urllib2

request = urllib2.Request('http://nz.sports.search.yahoo.com/search?p=basketball&fr=sports-nz-ss&age=1w&focuslim=age')
request.add_header('Accept-encoding', 'gzip')
response = urllib2.urlopen(request)
if response.info().get('Content-Encoding') == 'gzip':
    buf = StringIO( response.read())
    f = gzip.GzipFile(fileobj=buf)
    data = f.read()

soup = BeautifulSoup(data)

web_p = soup.find_all('span',class_='url')

for web in web_p:
    print web

rawDescription: Python BeautifulSoup picking webpages, same codes working on and off


code:
import numpy as np
import pyopencl as cl

def bar():
   mf = cl.mem_flags
   ctx = cl.create_some_context()
   queue = cl.CommandQueue(self.ctx)
   prog_f = open('kernels.cl', 'r')
   #a = (1, 2, 3), b = (4, 5, 6)
   ary = np.array([(1, 2, 3), (4, 5, 6), (0, 0, 0)], dtype='uint32, uint32, uint32')
   cl_ary = cl.Buffer(ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=ary)
   #Here should compute the size, but hardcoded for the example
   size = 3
   #The important part follows using -D option
   prog = cl.Program(ctx, prog_f.read()).build(options="-D SIZE={0}".format(size))
   prog.foo(queue, (size,), None, cl_ary)
   result = np.zeros_like(ary)
   cl.enqueue_copy(queue, result, cl_ary).wait()
   print result

rawDescription: Passing struct with pointer members to OpenCL kernel using PyOpenCL


code:
print "What equation would you like"
a = raw_input()
print "What's", a
b = raw_input()
print eval(a) == eval(b)

rawDescription: raw_input() and math equations

description: Raw_input() and math equations


code:
from scrapy import log

print 'PRINT OUTPUT BEFORE'
log.start(loglevel='DEBUG', logstdout=False)
print 'PRINT OUTPUT AFTER'

rawDescription: How to see output of print statements when scrapy logger enabled

description: See output of print statements when scrapy logger enabled


code:
def add_path(root, path):
    if path:
        child = root.setdefault(path[0], {})
        add_path(child, path[1:])

root = {}
with open('tree.txt') as f:
    for p in f:
        add_path(root, p.strip().split('.'))

import json
print json.dumps(root,  indent=4)

rawDescription: How do I create a tree out of this?

description: Create a tree out of this


code:
csv = open("mat.csv")

columns = csv.readline().strip().split(',')[1:]
for line in csv:
    tokens = line.strip().split(',')
    row = tokens[0]
    for column, cell in zip(columns,tokens[1:]):
        print '{},{},{}'.format(row,column,cell)

rawDescription: Python - Convert a matrix to edge list/long form


code:
data = [("email", "<object>"),("email", "<object>"), ('zip', "<object>"), ('name', "<object>"), ('phone', "<object>"),('name', "<object>")]
data.sort(key=lambda x:order.index(x[0]))
print data
[('name', '<object>'), ('name', '<object>'), ('email', '<object>'), ('email', '<object>'), ('phone', '<object>'), ('zip', '<object>')]

rawDescription: Order list of tuples based on a list of strings


code:
with open('filelist.csv', 'wb') as csvfile:
    lister = csv.writer(csvfile, delimiter=',')
    for root, dirnames, filenames in os.walk(os.getcwd()):
        matches = []
        for filename in fnmatch.filter(filenames, '*.wav'):
            matches.append(os.path.abspath(os.path.join(root, filename)))
            if len(matches) > 0:
                print matches
                lister.writerow(matches)

rawDescription: Get full path for a file in python

description: Get full path for a file in python


code:
from sklearn.datasets import load_iris
from sklearn.linear_model import RandomizedLogisticRegression

iris = load_iris()
X, y = iris.data, iris.target

clf = RandomizedLogisticRegression()
clf.fit(X,y)
print clf.get_support()

#prints [False  True  True  True]

rawDescription: Find selected features by RandomizedLogisticRegression


code:
class MyClass(object):
    counter = 0

    def __init__(self):
        MyClass.counter += 1

    def printCount(self):
        # these both work:
        print self.counter
        print MyClass.counter

rawDescription: Are class variables in the scope of instance methods or class methods?


code:
from PyQt4 import QtGui
import sys

class TestView(QtGui.QWidget):

    def __init__(self):
        super(TestView, self).__init__()
        self.initUI()

    def initUI(self):
        self.btn = QtGui.QPushButton('Button', self)
        self.btn.resize(self.btn.sizeHint())
        self.btn.move(50, 50)
        self.btn.clicked.connect(self.buttonClicked)

    def buttonClicked(self):
        print 'clicked'

def main():
    app = QtGui.QApplication(sys.argv)
    view = TestView()
    view.show()
    app.exec_()

if __name__ == '__main__':
    main()

rawDescription: How to connect a signal from the controller in PyQt4? (iOS like MVC structure in PyQt4)

description: Connect a signal from the controller in PyQt4? (iOS like MVC structure in PyQt4)


code:
import urllib2
from StringIO import StringIO

from BeautifulSoup import BeautifulSoup
from tidylib import tidy_document

html = urllib2.urlopen("http://www.hitmeister.de").read()
tidy, errors = tidy_document(html)
soup = BeautifulSoup(tidy)
print type(soup)

rawDescription: Python BeautifulSoup Error

description: Python BeautifulSoup Error


code:
try:
    iter(obj)
except TypeError, te:
    obj = list(obj)

rawDescription: python, wrap and object into a list if not is an iterable

description: Python, wrap and object into a list if not is an iterable


code:
for match in listb_dict.viewkeys() & lista_set:
    print match, listb_dict[match]

rawDescription: Compare list w/ sublist

description: Compare list w/ sublist


code:
t = Tree.parse("(S (S (S (S (X (PRO pro))) (X (V v))) (X (ADJ adj))) (X (N n)))")
print list(get_pairs(t, "X"))

rawDescription: NLTK: how can I list all pairs of adjacent subtrees (rooted in specific nonterminal) of a parse tree


code:
import os
import sys

# Create a directory
directoryName = raw_input('Enter new directory name: ')
cmdToExecute = 'mkdir ' + directoryName
print cmdToExecute
os.popen(cmdToExecute)

# Change Directory
directoryPath = os.getenv('PWD')
directoryPath = os.path.join(directoryPath, directoryName)
os.chdir(directoryPath)
print os.getcwd()

rawDescription: os.chdir() is not working in mac

description: Os.chdir() is not working in mac


code:
for line in f:
    if 'DB_NAME' in line:
        print line.split("'")[3]

rawDescription: Python: Extract substring that matches regular expression in a string


code:
import json
python_object = {'username': u'Tester1', 'age': 0L}
print json.dumps(python_object)

rawDescription: Convert utf string ftom python to javascript dictionary


code:
import re
import glob

for filename in glob.glob('*.*'):
    with open(filename, 'r') as f_input:
        data = f_input.read()
        if re.search(r'mutant\s.*?\scauses.*?GS', data, re.S):
            print "'{}' matches".format(filename)

rawDescription: Searching for specific phrase pattern within lines. python


code:
s = "..."
regex = re.compile('(\d+,?)+')
it = re.finditer(regex, s)

for match in it:
  print match.group(0)

rawDescription: python - regex search and findall

description: Python - regex search and findall


code:
target = soup.find("dl", class_="investment_sought")
print target.text
figure = target.find("dd").text
print figure

rawDescription: 'NoneType' object has no attribute 'text'


code:
for i in range(10):
    print "This should be reprinted only ten times"

rawDescription: Limiting loop repetition in python

description: Limiting loop repetition in python


code:
guess = 'o'
word, disp = ['n', 'o', 'o', 'n'], ['-', '-', '-', '-']
print [guess if i == guess else j for i, j in zip(word, disp)]

rawDescription: How can I use the index of an element in list 1 to change an index in list 2?

description: Use the index of an element in list 1 to change an index in list 2


code:
import timeit
import re

def splitit(input):
    res0 = input.split("|")
    res = []
    for element in res0:
        t = element.split("<>")
        if t != [element]:
            res0.remove(element)
            res.append(t)
    return (res0, res)

def split2(input):
    res0 = input.split("|")
    res1, res2 = [], []
    for r in res0:
        if "<>" in r:
            res2.append(r.split("<>"))
        else:
            res1.append(r)
    return res1, res2

def regexit(input):
    return re.split( "\||<>", input )

print "split:", timeit.Timer("splitit('a|b|c|de|f<>ge<>ah')","from __main__ import splitit").timeit()
print "split2:", timeit.Timer("split2('a|b|c|de|f<>ge<>ah')","from __main__ import split2").timeit()
print "regex:", timeit.Timer("regexit('a|b|c|de|f<>ge<>ah')","from __main__ import regexit").timeit()
print "split:", timeit.Timer("splitit('a|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>ah')","from __main__ import splitit").timeit()
print "split2:", timeit.Timer("split2('a|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>ah')","from __main__ import split2").timeit()
print "regex:", timeit.Timer("regexit('a|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>aha|b|c|de|f<>ge<>ah')","from __main__ import regexit").timeit()

rawDescription: Most efficient way to split strings in Python


code:
def getValues(s):
    # split your long string into a list of chunked strings
    strList = s.split(",,,")
    for chunk in strList:
        if chunk:
        # ...then just parse apart each individual set of data values
            vals = chunk.split(',')
            yield "%s %s %s" % (vals[0], vals[1], vals[2])
for v in getValues(s10):
    print v

rawDescription: Python Speeding Up Retrieving data from extremely large string


code:
from selenium import webdriver

url = "http://scores.espn.go.com/nba/scoreboard?date=20150405"

driver = webdriver.PhantomJS()
driver.get(url)

elements = driver.find_elements_by_css_selector("span.time")
for element in elements:
    print element.text

rawDescription: Scraping new ESPN site using xpath [Python]


code:
from django.db.models import get_models

for model in get_models():
   # Do something with your model here
   print model.__name__, [x.name for x in model._meta.fields]

rawDescription: Get a list of all installed applications in Django and their attributes


code:
mysql_field_name = "promotion_type"
mysql_data_type = "enum"
mysql_field_values = "$,%"
print '\t`%s` %s(\'%s\') COLLATE utf8_unicode_ci NOT NULL,' % (mysql_field_name, mysql_data_type, '\',\''.join(mysql_field_values.split(',')))

rawDescription: print dollar sign in python

description: Print dollar sign in python


code:
twoormore = [n for n,freq in histogram.iteritems() if freq > 1]

print twoormore

rawDescription: Seeing if values in a list are of equal value ~Python


code:
from itertools import cycle

def foobar():
    A = [1, 2, 3, 4, 5]
    B = [6, 7, 8, 9, 10]
    tmp = 0
    iter_A = cycle(A)
    iter_B = cycle(B)
    use_A = True
    while True:
        if use_A:
            item = next(iter_A)
        else:
            item = next(iter_B)
        print item, tmp
        if tmp > 10:
            use_A = False
        if tmp > 30:
            return tmp

        tmp += item

rawDescription: Can I redefine the iterator variable in the body of a for-loop?

description: Redefine the iterator variable in the body of a for-loop


code:
def foo(bar, baz="something or other"):
    print bar, baz

rawDescription: Named parameters with Python C API?

description: Named parameters with Python C API


code:
import re

def avrg_count(x):
    total_chars = len(re.sub(r'[^a-zA-Z0-9]', '', x))
    num_words = len(re.sub(r'[^a-zA-Z0-9 ]', '', x).split())
    print "Characters:{0}\nWords:{1}\nAverage word length: {2}".format(total_chars, num_words, total_chars/float(num_words))

phrase = '***The ?! quick brown cat:  leaps over the sad boy.'

avrg_count(phrase)

rawDescription: Finding average word length in a string

description: Finding average word length in a string


code: print 'Words in paragraph', para_type, ':', len(paragraph)

rawDescription: How to count the number of words in a paragraph and exclude some words (from file)?

description: Count the number of words in a paragraph and exclude some words (from file)


code:
from tkinter import *

def print_input(entry):
    print entry.get()

root = Tk()
root.geometry("250x250")
e = Entry(root)
e.pack()

b = Button(root, text="Print my input", command=lambda e=e: print_input(e))
b.pack()

root.mainloop()

rawDescription: Entry widget Tkinter. I am getting confused


code:
for gene in list_a:
    site = gene['site']
    range = iter(ranges[gene['Ch']])
    # skip anything with start > site
    name = previous = next((name for start, stop, name in range if start <= site), None)

    # search on for a matching stop, looking ahead. If we find a stop < site
    # the previous entry matched. If we ran of the end of our options, the last
    # entry matched.
    for start, stop, name  in range:
        if site > stop:
            previous = name
            continue
        if start > site:
            name = previous
        break

    print name, site

rawDescription: Is there a way to get next value for the same key in the next dictionary in the list of dictionaries?


code:
l = [ ]
for item in d:
    for i in item:
        if type(i) is dict:
            for j in i.items():
                for p in j:
                    l.append(p) if not isinstance(p,tuple) else [l.extend(k for k in p)]
        else:
            l.append(i)

print l

rawDescription: Flattening a list with a nested dictionary


code:
for n in G.nodes():
    print 'Node: '  + str(n)
    print 'Atrributes' + str(G.node[n]['attr'])

rawDescription: Add Nodes with attributes from a .txt file with multiple delimiters networkx/pandas


code:
data = np.arange(700).reshape((100, 7))
training, testing = train_test_split(data, test_size=0.4)
print len(data)
print len(training)
print len(testing)

rawDescription: Equivalent of R's createDataPartition in Python


code:
[x, x1, y, y1] = [0, 5, 0, 2]
surfaceXY = [[0]*10 for i in range(10)]
for i in range(x, x1):
  for j in range(y, y1):
    surfaceXY[i][j] = 5

for k in range(10):
  for l in range(10):
    print surfaceXY[k][l],
  print ""

rawDescription: Iterating in a area contained in a multidimensional array in python, why it doesn't work?


code:
x = pd.merge(df.drop_duplicates('Column1'), cntr.replace(0, pd.np.nan).reset_index())
print x

rawDescription: How to count through several lists contained in a groupby object and add counts together for every value in the lists of that group

description: Count through several lists contained in a groupby object and add counts together for every value in the lists of that group


code:
for c in build_structure(jdata):
    if c['uri'].startswith('http:'):
        print 'Started with http'

rawDescription: Parsing json and searching through it

description: Parsing json and searching through it


code:
import os
import re
import glob

filesiwant = re.compile('^HM.+') #if your files follow a more specific pattern and you don't know regular expressions you can give me a sample name and i'll give you the regex4it
path = '/server2/filedir/'
for infile in glob.glob( os.path.join(path, '*') ):
    if filesiwant.match(infile):
         print "current file is: " + infile

rawDescription: Python script to get files from one server into another and store them in separate directories?


code:
seq=[{'skillid': 1, 'QueText': u'quetext', 'Feedback':u'feedback'} , {'skillid': 1, 'QueText': u'quetext', 'Feedback':u'feedback'},{'skillid': 1, 'QueText': u'quetext', 'Feedback': u'feedback'} ]
# print seq
count=1
for subseq in seq:
    subseq.setdefault('QueNo',count)
    count+=1
    print subseq

rawDescription: Python adding a number sequence in a list of dictionaries


code:
#!/usr/bin/env python
import sys
for line in sys.stdin:
    line = line.strip()
    cols = line.split("\t")
    key = cols[-1]
    print key + "\t" + line

rawDescription: Input split for Map function in Hadoop

description: Input split for Map function in Hadoop


code:
a = ['a', 'b', 'a', 'c', 'b']
b = [0, 1, 2, 3, 4]
from collections import defaultdict
result = defaultdict(list)
for idx, item in zip(b, a):
    result[item].append(idx)
print result

rawDescription: Covert this list in python in the following format


code:
class MyDict(dict):
    def __getitem__(self, key):
        if key == 'density':
           return self['mass'] / self['volume']
        else:
            return dict.__getitem__(self,key)
    def keys(self):
        return ['density'] + dict.keys(self)

x = MyDict()
x['mass'] = 1.0
x['volume'] = 0.5

print x

print x.keys()

print x['density']

x['mass'] = 2.0

print x['density']

rawDescription: Python: linking/binding variables together in a dictionary


code:
from datetime import datetime
datetime(2015, 05, 26)
import datetime
datetime(2015, 05, 26)

rawDescription: Why am I getting TypeError: 'module' object is not callable in python?


code:
def swap_even_odd_entries(seq):
    tmp = list(seq)+[seq[-1]]  # convert sequence to mutable list and dup last
    for i in xrange(0, len(seq), 2):
        tmp[i],tmp[i+1] = tmp[i+1],tmp[i]  # swap each entry with following one
    return tuple(tmp[:len(seq)])  # remove any excess

a = (1, 0, 3, 2, 5, 4, 7, 6, 9, 8)
a = swap_even_odd_entries(a)
b = (91, 70, 23, 42, 75, 14, 87, 36, 19, 80)
b = swap_even_odd_entries(b)
c = (1, 0, 3, 2, 5)
c = swap_even_odd_entries(c)
print a
print b
print c
# output
#  (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
#  (70, 91, 42, 23, 14, 75, 36, 87, 80, 19)
#  (0, 1, 2, 3, 5)

rawDescription: Sorting bytes in words, tuples in python


code: print id(x) == id(y)  # True

rawDescription: Data-Binding in Python?

description: Data-Binding in Python


code:
from functools import wraps
def logged(func):
    @wraps(func)
    def with_logging(*args, **kwargs):
        print func.__name__ + " was called"
        return func(*args, **kwargs)
    return with_logging

@logged
def f(x):
   """does some math"""
   return x + x * x

print f.__name__  # prints 'f'
print f.__doc__   # prints 'does some math'

rawDescription: What does functools.wraps do?

description: What does functools.wraps do


code:
score = pandas.read_csv(filename, delimiter=' - ', header=None)
print score.groupby(0).agg([np.median, np.min, np.max])

rawDescription: Need to create a median function that draws from a dictionary


code:
def y():
  yield 1
  yield 2
  yield 3
def r():
  return [1, 2, 3]

vy = y()
for v in vy:
  print v
for v in vy:
  print v

print "---"

vr = r()
for v in vr:
  print v
for v in vr:
  print v

rawDescription: Different results from yield vs return

description: Different results from yield vs return


rawDescription: Replacing blank values (white space) with NaN in pandas


code:
import ast
import sys

dicts = {}
for line in sys.stdin:
    d = ast.literal_eval(line)
    for k, v in d.items():
        inner_dict = dicts.setdefault(k, {})
        inner_dict['longword'] = inner_dict.get('longword', 0) + v.get('longword', 0)
        inner_dict['charcount'] = inner_dict.get('charcount', 0) + v.get('charcount', 0)

print dicts

rawDescription: Multi dictionaries - For all identical keys, do some aggregation


code:
class MagicDict(dict):
    def __iter__(self):
        if num_expected_results() == 2:
            for k,v in self.iteritems():
                yield k,v
        else:
            for k in self.iterkeys():
                yield k

d=MagicDict(foo=1, bar=2)

print "Keys:"
for key in d:
    print "   ", key
print "Values"
for k,v in d:
    print "   ",k,v

rawDescription: how to program functions with alternative return value signatures in python? (next() for alternative iterators)

description: Program functions with alternative return value signatures in python? (next() for alternative iterators)


code:
with open("file.txt", "r") as f_input:
    for line in f_input:
        print line.replace("\t"," "),

rawDescription: parse list in python with \t

description: Parse list in python with \t


code:
def halved_loop(n):
    while n > 1:
        yield n
        n //= 2

for i in halved_loop(10):
    print i

rawDescription: For loop with custom steps in python

description: For loop with custom steps in python


code:
def sumdiv7(limit):
    for i in range(limit):
        result = sum(range(i*7)) / 7
        print "For", i, ", sumdiv = ", result

rawDescription: Sum and Division example (Python)

description: Sum and Division example (Python)


code:
import re

from bs4 import BeautifulSoup

data = """

        Last Login

        4/1/2011 at 07:01:11 AM

"""

soup = BeautifulSoup(data)
last_login = soup.find("td", text=re.compile(r"Last Login")).find_next_sibling("td").get_text(strip=True)
print last_login

rawDescription: Using beautifulsoup to extract data that's hard to identify


code:
import io
import cPickle as pickle

from nltk import pos_tag
from nltk.corpus import brown

print brown.sents()
print

# Let's tag the first 10 sentences.
tagged_corpus = [pos_tag(i) for i in brown.sents()[:10]]

with io.open('brown.pos', 'wb') as fout:
    pickle.dump(tagged_corpus, fout)

with io.open('brown.pos', 'rb') as fin:
    loaded_corpus = pickle.load(fin)

for sent in loaded_corpus:
    print sent
    break

rawDescription: Storing a POS tagged corpus

description: Storing a POS tagged corpus


code:
class TestClass(object):
    def __init__(self, value):
        self.variable = value

var_1, var_2 = TestClass(1), TestClass(2)
print var_1.variable is var_2.variable
# False
print TestClass.variable is var_1.variable
# AttributeError: type object 'TestClass' has no attribute 'variable'

rawDescription: python using __init__ vs just defining variables in class - any difference?

description: Python using __init__ vs just defining variables in class - any difference


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

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

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

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


code:
for row in c.execute('SELECT * FROM stocks ORDER BY price'):
        print row

rawDescription: Replace parts of string in sqlite3

description: Replace parts of string in sqlite3


code:
import matplotlib.pyplot as plt
n, bins, patches = plt.hist([1,2,3])
for p in patches:
    print p.get_facecolor()
    p.set_facecolor((1.0, 0.0, 0.0, 1.0))

rawDescription: Change color of histogram after it was plotted


code:
while True:
  print "In outer loop"
  i = 0
  while True:
    print "In inner loop"
    if i >= 5: break
    i += 1
  print "Got out of inner loop, still inside outer loop"
  break

rawDescription: Breaking from nested while loop

description: Breaking from nested while loop


code:
import re

tgt='''\
section aaa:
   some values

section bbb:
   more values
   value 6
section ccc:
   some values
   value 5
section ddd:
   more values
   value 6'''

pat1=re.compile(r'^section (\w+):(.*?)(?=section|\Z)',flags=re.MULTILINE|re.DOTALL)
pat2=re.compile(r'^\s*value (\d+)',flags=re.MULTILINE)

for m in pat1.finditer(tgt):
    m2=re.search(r'^\s*value (\d+)',m.group(2),flags=re.MULTILINE)
    if m2 and m2.group(1)=='6':
        print m.group(1)

rawDescription: Regex: Matching the closest value backwards


code:
def process_event(input):
    for events_list in input:  # It's more pythonic way to iterate over items, not by indices
        for event in events_list:
            orgunit = event['orgunit'] # Save current orgunit
            del event['orgunit'] # You will get "dataElement: orgunit" without this. Also you can make a copy and delete from a copy to do not corrupt input list
            for key, value in event.iteritems(): # You do not need copy here
                isolated = {'orgunit': orgunit} # The main point - each time create new isolated dict
                isolated['dataElement'] = key
                isolated['value'] = value
                # print
                print isolated
                yield isolated

rawDescription: Python generator creates duplicates of yield results when appended to a list


rawDescription: pandas timeseries identification values based on date index

description: Pandas timeseries identification values based on date index


code:
from collections import Counter
print Counter([0, 0, 1, 2, 3, 0]).most_common(1)
# [(0, 3)]

rawDescription: How to find the maximum number of times that the most common item in a list appears?

description: Find the maximum number of times that the most common item in a list appears


code:
from bs4 import BeautifulSoup
from itertools import izip

html = """

                    Target Point:
                    200.22

                    Target Point:
                    200.22

                    Target Point:
                    200.22

"""

bs = BeautifulSoup(html)

result = {}

ths = bs.findAll("th")
tds = bs.findAll("td")
elements = izip(ths, tds)

result = []

for x, y in elements:
    result.append((x.text, y.text))

print result

rawDescription: Scraping for text between nondescript tags


code:
for key, value in get_items({1: 2, 3: 4}):
    print key, value

rawDescription: Python 2 and 3 compatible way of iterating through dict with key and value


code:
#Define Key Words
##################################
#System Requirements Definitions
sysReq1 = set(['system', 'requirements'])
sysReq2 = [set(['can'])
          ,set(['handle', 'play', 'run'])
          ,set(['netbook', 'notebook', 'pc', 'mac', 'macbook', 'laptop', 'comp', 'computer'])
          ]
def test(testString):
    if not testString.endswith('?'):
        return False

    lowerTestString = set(testString.rstrip('?').lower().split())
    return  lowerTestString.issuperset(sysReq1) or all(not lowerTestString.isdisjoint(i) for i in sysReq2)
#System Requirements Response
sysReqResponse = 'PROGRESS'
##################################
testString1 = "i hate this"
#intended result for 1: nothing
testString2 = "is that real?"
#intended result for 2: nothing
testString3 = "What are the system requirements?"
#intended result for 3: 'PROGRESS'
testString4 = "Can my PC handle it?"
#intended result for 4: 'PROGRESS'
print testString1
if test(testString1):
    print sysReqResponse
print testString2
if test(testString2):
    print sysReqResponse
print testString3
if test(testString3):
    print sysReqResponse
print testString4
if test(testString4):
    print sysReqResponse

rawDescription: Python: Trying to create the equivalent of and(or(value,value,value))


code:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--quality", help="enter some quality limit")
args = parser.parse_args()
print "You gave quality = %s" % str(args.quality)

rawDescription: CommandLine Option in Python for filtering values from Column


code:
for k, v in groups: print k  # the v are subDataFrames for each item
groups.get_group(('foo' , 'one'))  # example v

rawDescription: Accessing groups in Pandas

description: Accessing groups in Pandas


code:
from nltk.corpus import indian

# NLTK reads the corpus as bytecodes.
hindi = " ".join(indian.words('hindi.pos'))
bangla = " ".join(indian.words('bangla.pos'))
marathi = " ".join(indian.words('marathi.pos'))
telugu = " ".join(indian.words('telugu.pos'))

# Prints out first 10 bytes (including spaces).
print 'hindi:', hindi[:10]
print 'bangla:', bangla[:10]
print 'marathi:', marathi[:10]
print 'telugu:', telugu[:10]
print

# Converts bytecodes to utf8.
hindi = hindi.decode('utf8')
bangla = bangla.decode('utf8')
marathi = marathi.decode('utf8')
telugu = telugu.decode('utf8')

# Prints out first 10 unicode char (including spaces).
print 'hindi:', hindi[:10]
print 'bangla:', bangla[:10]
print 'marathi:', marathi[:10]
print 'telugu:', telugu[:10]
print

rawDescription: python based naive base classifer for new language

description: Python based naive base classifer for new language


code: print [[parse_date(x) for x in i] for i in My_Dict[tbl]['Dates']]

rawDescription: Loop through all items in a list of list embedded in dictionary


code:
def permute(inputData, outputSoFar):
    for a in inputData:
        if a not in outputSoFar:
            if len(outputSoFar) == len(inputData) - 1:
                yield outputSoFar + [a]
            else:
                for b in permute(inputData, outputSoFar + [a]): # --- Recursion
                    yield b

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

rawDescription: Python permutation generator puzzle

description: Python permutation generator puzzle


code:
import json
import re
from bs4 import BeautifulSoup
import requests

pattern = re.compile(r"window\.BC\.product = (.*);", re.MULTILINE)

response = requests.get("http://www.steepandcheap.com/gear-cache/shop-smartwool-on-sale/SWL00II-GRA")
soup = BeautifulSoup(response.content)

script = soup.find("script", text=lambda x: x and "window.BC.product" in x).text
data = json.loads(re.search(pattern, script).group(1))
print data

rawDescription: Scrape 'dictionary' type object from top of HTML file (bunch of text, not in a class)


code:
import os
import re

print os.path.dirname(re.__file__)

rawDescription: get the path of a module in python

description: Get the path of a module in python


code:
from collections import defaultdict
wordmap = defaultdict(list)
for i in range(len(X)-kmer+1):
    wordmap[X[i:i+kmer]].append(i)

for i in range(len(Y)-kmer+1):
    word = Y[i:i+kmer]
    if word in wordmap:
        print word, wordmap[word], i

rawDescription: An efficient way to search similar words (with specified length) in two strings using python


rawDescription: Unknown characters fetching stock data from yahoo in csv format


code:
import re
id_and_link = re.compile(r'(\[[^]]+\]) Information Link: (https?://[\w\d/.]+)')

messages = (entry['_source']['message'] for entry in ThreeLine['hits']['hits'] if '_source' in entry and 'message' in entry['_source'])
for message in messages:
    match = id_and_link.search(message)
    if not match:
        continue
    id_, link = match.groups()
    print 'Message:', id_
    print 'Link:', link
    print

rawDescription: Remove Python dict from JSON file response


code:
import requests
r = requests.get('http://www.elegantthemes.com/gallery/')
print r.content

rawDescription: Strange Output from Python urllib2

description: Strange Output from Python urllib2


code:
#!/usr/bin/env python
import shutil
import sys
import time

print "In handler..."
with open(sys.argv[1], 'rb') as file:
    shutil.copyfileobj(file, sys.stdout)
sys.stdout.flush()
time.sleep(5)

rawDescription: Communication between two gnome-terminal sessions


rawDescription: How do I filter a python list containing dates according to the given start date and end date?

description: Filter a python list containing dates according to the given start date and end date


code:
from multiprocessing import Pool

def f(x):
    return (x[0], x[1]*x[1])

if __name__ == '__main__':
    p = Pool(5)
    d = {'a':1, 'b':2, 'c':3}
    print list(d.iteritems())
    print(p.map(f, d.iteritems()))

rawDescription: Python3 for loop with multiprocessing

description: Python3 for loop with multiprocessing


code:
print [p.text.strip()
       for h1 in soup.find_all('h1', text=re.compile('Title \d+'))
       for p in h1.find_next_siblings('p', limit=2)]

rawDescription: Parse HTML using BeautifulSoup depending on previous tag


code:
a = [1]
b = [3, 2]
c = [5, 1, 6]
d = [7, 4, 18]
max_len = len(max([a,b,c,d], key=len)) # max(map(len, [a,b,c,d]))
lists = {
    'a': a,
    'b': b,
    'c': c,
    'd': d,
}
names = [name for name in lists if len(lists[name]) == max_len]
print '{} contain the longest list(s).'.format(', '.join(names))

rawDescription: Comparing Sizes of Multiple Lists

description: Comparing Sizes of Multiple Lists


code:
import calendar
print calendar.monthrange(2012,1)[1]
calendar.monthrange(2012,2)[1]

rawDescription: How to find number of days in the current month

description: Find number of days in the current month


code:
import urllib
import lxml.html

data = urllib.urlopen('http://www.sec.gov/Archives/edgar/data/8177/000114036111018563/form10k.htm').read()
tree = lxml.html.fromstring(data)

tables = []
for tbl in tree.iterfind('.//table'):
    tele = []
    tables.append(tele)
    for tr in tbl.iterfind('.//tr'):
        text = [e.strip() for e in tr.xpath('.//text()') if len(e.strip()) > 0]
        tele.append(text)

print tables

rawDescription: Parsing an html table

description: Parsing an html table


code:
def menu(choice):
    def add():
        print 6+6
    def sub():
        print 6-3

    if choice == 'add':
        return add # returning the function, not calling it
    elif choice == 'sub':
        return sub # same here

func = menu('add')
func() # calls the add function defined in menu

rawDescription: How to call upon a function within a function?

description: Call upon a function within a function


code:
from itertools import ifilter

def f(d, func, l):
    if l == 0:
        return {k: d[k] for k in ifilter(func, d)}
    return {k: f(v, func, l - 1) for k, v in d.iteritems()}

d = {'fruit': {'sweet': {'green': 'apple', 'red': 'tomato'}, 'bitter': {'green': 'cucumber'}}}

print f(d, lambda k: k == 'green', 2)

rawDescription: How can I filter a nested dictionary at a certain nested level?

description: Filter a nested dictionary at a certain nested level


code:
words = ["don't", "hello'", "world'"]
quotes = ["'", '"']
output = list()

for word in words:
    if word[-1] in quotes:
        word = word[: (len(word) - 1)]
    output.append(word)

print output
#=> ["don't", "hello", "world"]

rawDescription: Remove quotations with conditions

description: Remove quotations with conditions


code:
print id(tester)
print id(hand)  #should be the same as `id(tester)`

rawDescription: Unsure why dictionary is mutating

description: Unsure why dictionary is mutating


code:
def group_splitter(it, key=None):
    if key is None:
        key = lambda x: x
    group = []
    for item in iter(it):
        if key(item) and group:
            yield group
            group = []
        group.append(item)
    if group:
        yield group

for g in group_splitter(foo, lambda x: x[0] == 0):
    print g

rawDescription: Split a list into sublists on a value and keep the value


code:
import socket, pickle

def socket_to_file(sock):
    try:
        client_sock.settimeout(15)
        return sock.makefile()
    finally:
        sock.close()

if __name__ == '__main__':
    listen_sock = socket.socket()
    try:
        listen_sock.bind(('', 3005))
        listen_sock.listen(10)
        client_sock, client_addr = listen_sock.accept()
    finally:
        listen_sock.close()

    try:
        client_sock = socket_to_file(client_sock)
        request = pickle.load(client_sock)
        print 'Request: %s' % (request,)
        response = 'My smart response.'
        print 'Response: %s' % (response,)
        pickle.dump(response, client_sock)
        client_sock.flush()
    finally:
        client_sock.close()

rawDescription: Python working with sockets the method recv


code:
import extmodule

#code is in extended module
print extmodule.makeBalls(1,2)
#returns Ball(1) Ball(2)

#code is in base module
print extmodule.makeBall(1)
#returns Ball(1)

#function from extended module overwrites base module
extmodule.override()
#returns OVERRIDE TWO

#function from extended module calls base module first
extmodule.dontoverride()
#returns THIS WILL BE PRESERVED\nTHIS WAS PRESERVED

rawDescription: How do I extend a python module? (python-twitter)

description: Extend a python module? (python-twitter)


code: for result in db.test.find({'EXPERIMENT':1}, {'DATA.TIMESTAMP':1, '_id':0}): print result

rawDescription: Pymongo iterate through list of docs

description: Pymongo iterate through list of docs


code:
x=['xxx.xxx.xxx.xxx xxx\n','yyy.yyy.yyy.yyy yyy\n']
l=[]

for i in x:
    l.append(i.split()[1])
print l

rawDescription: Dividing a text file into 2 lists using python


code:
import pandas as pd
data = [[5, 7, 10], [7, 20, 4,], [8, 1, 6,]]
cities = ['Boston', 'Phoenix', 'New York']
df = pd.DataFrame(data, columns=cities, index=cities)

print df.unstack().idxmax()

rawDescription: Dataframe Indexing Issue?

description: Dataframe Indexing Issue


code:
s = "Lorem ipsum foo bar foobar stuff etc"
print ' '.join(s.split(" ")[:4])

rawDescription: How to strip a string after a certain amount of words in python

description: Strip a string after a certain amount of words in python


code:
import pandas as pd
data = pd.read_csv("http://data.giss.nasa.gov/gistemp/tabledata_v3/GLB.Ts+dSST.csv",
na_values = ["**** ","***  "])
print data.head()

df = data.ix[:,0:13]

print df.columns
Index([u' Year', u'Jan', u'Feb', u'Mar', u'Apr', u'May', u'Jun', u'Jul',
       u'Aug', u'Sep', u'Oct', u'Nov', u'Dec'],
      dtype='object')

table = df.set_index(' Year').stack().reset_index()
table.columns = ['Year','Month','Value']

rawDescription: Display monthly columns by year using pandas


code:
import csv
from collections import defaultdict
from pprint import pprint

def tree(): return defaultdict(tree)

def tree_add(t, path):
  for node in path:
    t = t[node]

def pprint_tree(tree_instance):
    def dicts(t): return {k: dicts(t[k]) for k in t}
    pprint(dicts(tree_instance))

def csv_to_tree(input):
    t = tree()
    for row in csv.reader(input, quotechar='\''):
        tree_add(t, row)
    return t

def tree_to_newick(root):
    items = []
    for k in root.iterkeys():
        s = ''
        if len(root[k].keys()) > 0:
            sub_tree = tree_to_newick(root[k])
            if sub_tree != '':
                s += '(' + sub_tree + ')'
        s += k
        items.append(s)
    return ','.join(items)

def csv_to_weightless_newick(input):
    t = csv_to_tree(input)
    #pprint_tree(t)
    return tree_to_newick(t)

if __name__ == '__main__':
    # see https://docs.python.org/2/library/csv.html to read CSV file
    input = [
        "'Phylum','Class','Order','Family','Genus','Species','Subspecies','unique_gi'",
        "'Phylum','Class','Order','example'",
        "'Another','Test'",
    ]

    print csv_to_weightless_newick(input)

rawDescription: Convert csv to Newick tree

description: Convert csv to Newick tree


code:
@staticmethod
def impWords():

    with open('import.txt','r+') as tempFile:
        #re
        tempFile1 = re.findall(r'\w+', tempFile.read())
        # using enumerate here is cleaner
        Repo.words.extend(Word(i, w, 0) for i, w in enumerate(tempFile1))

    print Repo.words # print automatically calls __str__()
    UI.Controller.adminMenu()

rawDescription: Create from list of strings list of class instances py2.7


code:
for line in file:
    print pattern.findall(file.read())
    print re.findall(r'>(\w+ing<', line)

rawDescription: TypeError: findall() takes at least 2 arguments (1 given)


code:
import imp

import os

if 'MY_STARTUP_FILE' in os.environ:
    try:
        file_path = os.environ['MY_STARTUP_FILE']
        folder, file_name = os.path.split(file_path)
        module_name, _ = os.path.splitext(file_name)
        fp, pathname, description = imp.find_module(module_name, [folder])
    except Exception as e:
        # Broad exception handling since sitecustomize exceptions are ignored
        print "There was a problem finding startup file", file_path
        print repr(e)
        exit()

    try:
        imp.load_module(module_name, fp, pathname, description)
    except Exception as e:
        print "There was a problem loading startup file: ", file_path
        print repr(e)
        exit()
    finally:
        # "the caller is responsible for closing the file argument" from imp docs
        if fp:
            fp.close()

rawDescription: Making Python run a few lines before my script


code:
hdr = {
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
    "Accept-Encoding": "none",
    "Accept-Language": "en-US,en;q=0.8",
    "Connection": "keep-alive"}

def crawl(columns=None, *search):
    df = pd.DataFrame(columns= columns)
    for term in search:
        google = "http://www.google.com/search?q="
        url = google + term + "+" + "PDF"
        req = urllib2.Request(url, headers=hdr)
        try:
            page = urllib2.urlopen(req).read()
            soup = BeautifulSoup(page)
            pdfs = []
            links = soup.find_all("a",href=True)
            for link in links:
                lk = link["href"]
                if lk.endswith(".pdf"):
                     pdfs.append((term, lk))
            df2 = pd.DataFrame(pdfs, columns=columns)
            df = df.append(df2, ignore_index=True)
        except urllib2.HTTPError, e:
            print e.fp.read()
    return df

df = crawl(["Search", "PDF link"],"Shakespeare","Beowulf")
df.to_csv("out.csv",index=False)

rawDescription: Pandas: Write all re.search results to csv from BeautifulSoup


code:
import pyaudio
p = pyaudio.PyAudio()

for i in xrange(0,10):
    try:
        p.get_device_info_by_index(i)
    except Exception,e:print e

rawDescription: Detecting system sound on Windows using Python


rawDescription: Delete Data from a pandas array that is out of range


code:
def check_line(char='|'):
    correct_count = None

    while True:
        line = yield None
        line_count = line.count(char)
        if not correct_count:
           correct_count = line_count

        if line_count != correct_count:
           print """Line %d contains %d %s, should have %d""" % (
                 i, line_count, char, correct_count)

with open('file.txt', 'rb') as f:
     checker = check_line()
     for i, line in enumerate(f):
         checker.next(line)

rawDescription: Pythonic way to check generator values using its own elements


code:
import httplib, urllib2

class MyHTTPConnection(httplib.HTTPConnection):
    def send(self, s):
        print s  # or save them, or whatever!
        httplib.HTTPConnection.send(self, s)

class MyHTTPHandler(urllib2.HTTPHandler):
    def http_open(self, req):
        return self.do_open(MyHTTPConnection, req)

opener = urllib2.build_opener(MyHTTPHandler)
response = opener.open('http://www.google.com/')

rawDescription: How do you get default headers in a urllib2 Request?

description: Get default headers in a urllib2 Request


code:
import os

def get_abs_directory(path):
    try:
        if os.path.isdir(path):
            return path
        else:
            return os.path.split(os.path.abspath(path))[0]
    except TypeError:
        print "You inserted the wrong type!"

if __name__ == '__main__':
    get_abs_directory(1)  # Using an int instead of a string, which is caught by TypeError

rawDescription: Parameter validation, Best practices in Python


code:
for group in [data[:i+5] for i in range(0, len(data), 5)]:
   print 'Group from 0 to', len(group)
   print 'Sum:', sum(x[1] for x in group)

rawDescription: How to do a running sum in python?

description: Do a running sum in python


code:
list1,list2 = [['aaa','b','c'],['ddd','e','f']],[['aaa','1','2'],['ddd','3','4']]

from itertools import chain, groupby
from operator import itemgetter
get_first, result = itemgetter(0), []
for key, grp in groupby(sorted(chain(list1, list2), key = get_first), get_first):
    result.append([item for items in grp for item in items])
print result

rawDescription: How to extend one sublist by another sublist if they share a common id at the same index in both sublists?

description: Extend one sublist by another sublist if they share a common id at the same index in both sublists


code:
l = [['KenL', 'MA344', 80.0], ['Harry', 'PY101', 60.0]]

lookup=raw_input("Which course code do you want to lookup: ")
avg = 60
def find_above_avg(number):
    return [i for i in l if lookup in i and  i[2] > avg]

print find_above_avg(avg)

rawDescription: Python getting average from list

description: Python getting average from list


code:
import networkx as nx

# Undirected Network
D = nx.Graph()
D.add_weighted_edges_from([('A', 'B', 0.5),('A', 'C', 1)])

# Default max number of iterations failed to converge for me
print nx.pagerank(D, max_iter=200)

# Outputs:
{'A': 0.48648648872844047, 'C': 0.32567567418103965, 'B': 0.18783783709051982}

rawDescription: Textrank: complementing pagerank for sentence extraction using networkx


code:
import re

reg = re.compile('/'
                 '([^/.]*?)/'
                 '([^/.]*?)\.'
                 '([^/.]*?)\.'
                 '([^/.]*?)\.'
                 '([^/.]*?)\.'
                 '([^/.]*?)\.'
                 '[^/.]+\Z')

def main():
    #file = open('~/Desktop/client_docs.csv', "rb")
    #output = open('~/Desktop/client_docs_parsed.txt', "wb")
    li = ['/Household/LastName.FirstName.Account.Doctype.Date.extension',
          '- /Volumes/HD/Organized Files/Cosby, Bill/Cosby.Bill..Profile.2006.doc']
    for line in li:
        print "line == %r" % line
        household,lastn,firstn,acct,doctype,date = reg.search(line).groups('')
        print ('household == %r\n'
               'lastn     == %r\n'
               'firstn    == %r\n'
               'acct      == %r\n'
               'doctype   == %r\n'
               'date      == %r\n'
               % (household,lastn,firstn,acct,doctype,date))

if __name__ == "__main__": main()

rawDescription: Python parse filenames by custom field pattern


code:
for base in A.__bases__:
  print "A depends on module %s" % base.__module__

rawDescription: How can I capture dependancies used by a subset of code in a python source file?

description: Capture dependancies used by a subset of code in a python source file


code:
from xml.dom import minidom, getDOMImplementation

XML1 = """
<sub1>
 <foo>BAR1</foo>
</sub1>"""

XML2 = """
<sub2>
 <foo>BAR2</foo>
</sub2>"""

impl = getDOMImplementation()
doc = impl.createDocument(None, "root", None)

for s in [XML1, XML2]:
    elem = minidom.parseString(s).firstChild
    doc.firstChild.appendChild(elem)

print doc.toxml()

rawDescription: python and XML: how to place two documents into a single document

description: Python and XML: how to place two documents into a single document


code:
# create lookuptable
lut = [0]*256
lut[slice(ord('a'), ord('z'))] = [1] * 26
lut[slice(ord('A'), ord('Z'))] = [1] * 26

with open(fn) as f:
    # sum the matching characters
    print sum([lut[ord(c)] for c in f.read()])

rawDescription: Python: Number of letters in file, simple yet efficient way


code:
def func():pass
print func()     #print or print() prints the return Value
func()           #remove print and the returned value is not printed.

rawDescription: Python Script returns unintended "None" after execution of a function


code:
pGraph = {"id":root,"children":[]}

for tuple in queue_tuples:
    level = tuple[2]
    g = pGraph["children"]
    for i in range(level-1):
        for e in g:
            if tuple[1]==e["id"]:
                g = e["children"]
    flag = 0
    for e in g:
        if tuple[0]==e["id"]:
            flag = 1
            break
    if flag==0:
        g.append({"id":tuple[0],"children":[]})
print pGraph

rawDescription: Python: create nested dictionary from parent-child mappping