code:
import os, fnmatch

def glob_walk(top, pattern, **kwargs):
    """ Wrapper for os.walk() that filters the files returned
        with a pattern composed of Unix shell-style wildcards
        as documented in the fnmatch module.
    """
    for root, dirs, files in os.walk(top, **kwargs):
        yield root, dirs, fnmatch.filter(files, pattern)

# sample usage
def get_all_files(path, pattern):
    for root, dirs, files in glob_walk(path, pattern):
        for file in files:
            pathname = os.path.join(root, file)
            filesize = os.stat(pathname).st_size
            yield file, pathname, filesize

print list(get_all_files('.', 'something.*')) # all files named 'something'

rawDescription: Getting files with same name irrespective of their extension


code:
from PyQt4 import QtCore, QtGui
import sys

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    class button(QtGui.QPushButton):
        def __init__(self, parent=None):
            super(button, self).__init__(parent)
            self.clicked.connect(self.click)
            self.show()

        def click(self):
            self.emit(QtCore.SIGNAL('customSignal'), 'String Argument')

    btn = button()

    class label(QtGui.QLabel):
        def __init__(self, parent=None):
            super(label, self).__init__(parent)
            self.connect(btn, QtCore.SIGNAL('customSignal'), self.method)
            self.show()

        def method(self, arg=None):
            print 'method arg: %r'%arg

    lbl = label()

    sys.exit(app.exec_())

rawDescription: When Signal Does Not Find its Slot

description: When Signal Does Not Find its Slot


code:
p1 = Point(1,1)
p2 = Point(3,4)
print p1 + p2

rawDescription: How can I manipulate cartesian coordinates in Python?

description: Manipulate cartesian coordinates in Python


code: print 'I have %s apples and %s pears.'%(input(),input())

rawDescription: Python: "Print" and "Input" in one line

description: Python: "Print" and "Input" in one line


code:
def hours(day):
    while True:
        try:
            prompt = "Enter the number of hours the employee worked on {0}: ".format(day)
            hours = int(raw_input(prompt))
            if 0 <= hours <= 24:
                return hours
        except ValueError:
            pass # Silently catch failure.
        print "Please enter a whole number that is more than zero & less than 24"

WEEKDAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
for day in WEEKDAYS:
    print hours(day)

rawDescription: How can I reference a list element in this function?

description: Reference a list element in this function


code:
from bs4 import BeautifulSoup
import urllib2

url = "https://maps.google.com.au/maps?saddr=A6&daddr=A6&hl=en&ll=-33.877613,151.039867&spn=0.081236,0.083599&sll=-33.869204,151.034546&sspn=0.081244,0.083599&geocode=FYSu-v0d2KMACQ%3BFbp0-_0dJKoACQ&mra=ls&t=m&z=14&layer=t"

content = urllib2.urlopen(url).read()
soup = BeautifulSoup(content)

div = soup.find('div', {'class':'altroute-rcol altroute-aux'}) #get the div where it's located
span = div.find('span')
pos = span.text.find(': ')
print 'Current Listeners:', span.text[pos+2:]

rawDescription: Obtain span title using beautifulsoup

description: Obtain span title using beautifulsoup


code:
for tag in soup.find_all('input'):
    print tag['title']

rawDescription: How to make a RegEx fit in Python

description: Make a RegEx fit in Python


code:
def pair(list1, list2, x):
    for a in list1:
        for b in list2:
            if a + b == x:
                print a, b

rawDescription: How to create a function that uses 2 integers from 2 different lists and adding the pair to a certain integer?

description: Create a function that uses 2 integers from 2 different lists and adding the pair to a certain integer


code:
try:
    x = next(dict_elem for dict_elem in list_above if dict_elem['name'] == reded)
except StopIteration:
    print "No match found"

rawDescription: Concise Way of Get Matching Element in List


code:
import nltk
feature_vectors =[
({'drama': True, 'mystery': True, 'horror': True, 'psychological': True}, 'relevant'),
({'drama': True, 'fantasy': True, 'romance': True, 'adventure': True, 'science fiction': True}, 'unrelevant')]
classifier = nltk.classify.naivebayes.NaiveBayesClassifier.train(feature_vectors)
prob_dist = classifier.prob_classify({'Fantasy': True, 'Comedy': True, 'Supernatural': True})
print "relevant " + str(prob_dist.prob("relevant"))
print "unrelevant " + str(prob_dist.prob("unrelevant"))

rawDescription: trouble with nltk python NaiveBayesClassifier, I keep getting same probabilities inputs correct?

description: Trouble with nltk python NaiveBayesClassifier, I keep getting same probabilities inputs correct


code:
import json

resp = urllib2.urlopen('http://api.ipinfodb.com/v3/ip-city/?format=json&key=mykey&ip=someip')
data = json.load(resp)

print data['countryName']

rawDescription: Python splitting values from urllib in string


rawDescription: Python Get row in a list by one of its attributes


code:
from flask import Flask, request

app = Flask(__name__)

def exclude_from_analytics(func):
    func._exclude_from_analytics = True
    return func

@app.route('/a')
@exclude_from_analytics
def a():
    return 'a'

@app.route('/b')
def b():
    return 'b'

@app.before_request
def analytics_view(*args, **kwargs):
    # Default this to whatever you'd like.
    run_analytics = True

    # You can handle 404s differently here if you'd like.
    if request.endpoint in app.view_functions:
        view_func = app.view_functions[request.endpoint]
        run_analytics = not hasattr(view_func, '_exclude_from_analytics')

    print 'Should run analytics on {0}: {1}'.format(request.path, run_analytics)

app.run(debug=True)

rawDescription: Flask hit decorator before before_request signal fires


code:
import configproxy
import sys

if not configproxy.override('blogsample.config'):
    print "could not load configuration file"
    sys.exit(1)

import clr
clr.AddReference('System.Configuration')
from System.Configuration import *
connstr = ConfigurationManager.ConnectionStrings['TestConnStr']
print "The configuration string is {0}".format(connstr)

rawDescription: How to use IronPython with App.Config?

description: Use IronPython with App.Config


code:
import re
matcher = re.compile(r"""
^[^\{]*          # Starting from the beginning of the string, match anything that isn't an opening bracket
       (         # Open a group to record what's next
        \{.+\}   # The JSON substring
       )         # close the group
 [^}]*$          # at the end of the string, anything that isn't a closing bracket
""", re.VERBOSE)

# Your example
print matcher.match('100{"1":2, "3":4}312').group(1)

# Example with embedded hashmap
print matcher.match('100{"1":{"a":"b", "c":"d"}, "3":4}312').group(1)

rawDescription: Python remove JSON substring

description: Python remove JSON substring


code:
for s in itertools.islice(iter_all_strings(), 54):
    print s

rawDescription: How to make a continuous alphabetic list python (from a-z then from aa, ab, ac etc)

description: Make a continuous alphabetic list python (from a-z then from aa, ab, ac etc)


code:
from itertools import permutations
from random import shuffle
A=range(26)
shuffle(A)
for i,perm in enumerate(permutations(A)):
    print perm
    if i>=5:
        break

rawDescription: Itertools.permutations create n random solutions to the TSP


code:
print np.dstack((a,b)).shape
# (3, 2, 2)

rawDescription: understanding numpy's dstack function

description: Understanding numpy's dstack function


rawDescription: how to make a pandas dataframe column into a datetime object showing just the date to correctly sort

description: Make a pandas dataframe column into a datetime object showing just the date to correctly sort


code:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("192.168.0.1", 22))
s.sendall("SSH-2.0-MyPythonSSHProbingClient")
s.settimeout(5*60)  # hard-limit
print s.recv(500)   # remote banner
ts_start = time.time()
if not s.recv(100):
    # exits when remote site closes connection, or raises socket.timeout when hard-limit is hit.
    print time.time()-ts_start
else:
    raise Exception("whoop, something's gone wrong")

rawDescription: Paramiko check login timeout of SSH Server


code:
xls_sig = b'\x09\x08\x10\x00\x00\x06\x05\x00'
offset = 512
size = 8

with open('spreadsheet.xls', 'rb') as f:
    f.seek(offset)       # Seek to the offset.
    bytes = f.read(size) # Capture the specified number of bytes.

    if bytes == xls_sig:
        print 'Uploaded file is an xls.'
    else:
        print 'File is not an xls.'

rawDescription: How to check the uploaded file is csv or xls in python?

description: Check the uploaded file is csv or xls in python


code:
import codecs, re, time
from itertools import chain

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

trainfile = 'train.txt'
testfile = 'test.txt'

# Vectorizing data.
train = []
word_vectorizer = CountVectorizer(analyzer='word')
trainset = word_vectorizer.fit_transform(codecs.open(trainfile,'r','utf8'))
tags = ['bs','pt','es','sr']

# Training NB
mnb = MultinomialNB()
mnb.fit(trainset, tags)

# Tagging the documents
codecs.open(testfile,'r','utf8')
testset = word_vectorizer.transform(codecs.open(testfile,'r','utf8'))
results = mnb.predict(testset)

print results

rawDescription: How to vectorize bigrams with the hashing-trick in scikit-learn?

description: Vectorize bigrams with the hashing-trick in scikit-learn


code:
from struct import *

recognised_tags = {
    0x0100 : 'imageWidth',
    0x0101 : 'imageLength',
    0x0102 : 'bitsPerSample',
    0x0103 : 'compression',
    0x010f : 'make',
    0x0110 : 'model',
    0x0111 : 'stripOffset',
    0x0112 : 'orientation',
    0x0117 : 'stripByteCounts',
    0x011a : 'xResolution',
    0x011b : 'yResolution',
    0x0128 : 'resolutionUnit',
    0x0132 : 'dateTime',
    0x8769 : 'EXIF',
    0x8825 : 'GPS data'};

def GetHeaderFromCR2( buffer ):
    # Unpack the header into a tuple
    header = unpack_from('HHLHBBL', buffer)

    print "\nbyte_order = 0x%04X"%header[0]
    print "tiff_magic_word = %d"%header[1]
    print "tiff_offset = 0x%08X"%header[2]
    print "cr2_magic_word = %d"%header[3]
    print "cr2_major_version = %d"%header[4]
    print "cr2_minor_version = %d"%header[5]
    print "raw_ifd_offset = 0x%08X\n"%header[6]

    return header

def FindDateTimeOffsetFromCR2( buffer, ifd_offset, endian_flag ):
    # Read the number of entries in IFD #0
    (num_of_entries,) = unpack_from(endian_flag+'H', buffer, ifd_offset)
    print "Image File Directory #0 contains %d entries\n"%num_of_entries

    # Work out where the date time is stored
    datetime_offset = -1

    # Go through all the entries looking for the datetime field
    print " id  | type |  number  |  value   "
    for entry_num in range(0,num_of_entries):

        # Grab this IFD entry
        (tag_id, tag_type, num_of_value, value) = unpack_from(endian_flag+'HHLL', buffer, ifd_offset+2+entry_num*12)

        # Print out the entry for information
        print "%04X | %04X | %08X | %08X "%(tag_id, tag_type, num_of_value, value),
        if tag_id in recognised_tags:
            print recognised_tags[tag_id]

        # If this is the datetime one we're looking for, make a note of the offset
        if tag_id == 0x0132:
            assert tag_type == 2
            assert num_of_value == 20
            datetime_offset = value

    return datetime_offset

if __name__ == '__main__':
    with open("IMG_6113.CR2", "rb") as f:
        # read the first 1kb of the file should be enough to find the date/time
        buffer = f.read(1024)

        # Grab the various parts of the header
        (byte_order, tiff_magic_word, tiff_offset, cr2_magic_word, cr2_major_version, cr2_minor_version, raw_ifd_offset) = GetHeaderFromCR2(buffer)

        # Set the endian flag
        endian_flag = '@'
        if byte_order == 0x4D4D:
            # motorola format
            endian_flag = '>'
        elif byte_order == 0x4949:
            # intel format
            endian_flag = '<'

        # Search for the datetime entry offset
        datetime_offset = FindDateTimeOffsetFromCR2(buffer, 0x10, endian_flag)

        datetime_string = unpack_from(20*'s', buffer, datetime_offset)
        print "\nDatetime: "+"".join(datetime_string)+"\n"

rawDescription: Reading a CR2 (Raw Canon Image) header using Python


code:
lst = list(itertools.product(a, b))
for a, b in lst:
    print a, b

rawDescription: Combining lists in python

description: Combining lists in python


code:
number = input('Enter a number! ') # Let's say I put 5
print number

rawDescription: How do I repeat my initial input code?

description: Repeat my initial input code


code:
import SocketServer
import threading

class MyUDPHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        data = self.request[0].strip()
        socket = self.request[1]
        print "%s wrote:" % self.client_address[0]
        print data
        socket.sendto(data.upper(), self.client_address)

def serve_thread(host, port):
    server = SocketServer.UDPServer((host, port), MyUDPHandler)
    server.serve_forever()

threading.Thread(target=serve_thread,args=('localhost', 9999)).start()
threading.Thread(target=serve_thread,args=('localhost', 12345)).start()

rawDescription: python listen 2 port same file

description: Python listen 2 port same file


code:
def execute():
    load_data()
    for i in host:
        print host

rawDescription: [Python]Array function called in other function


code:
def functionl(lst):
    return list(set(lst))

print functionl([1])              # [1]
print functionl([1, 2, 3, 1])     # [1, 2, 3]

rawDescription: making a function that takes a list of any size as an argument Python 3

description: Making a function that takes a list of any size as an argument Python 3


code:
import pytest

@pytest.fixture
def cleaner(request):
  def finalizer():
    print '\n"cleaner" finalized'
  print '\n"cleaner" fixture'
  request.addfinalizer(finalizer)

@pytest.fixture
def other(cleaner):
    print '\n"other" fixture'

def test_foo(other):
    pass

rawDescription: Can pytest fixtures be combined?

description: Can pytest fixtures be combined


code:
def doclistings(request):
    d = getVariables(request)
    doctors = Doctor.objects.all().order_by('-likes')
    paginator = Paginator(doctors, 20) #Show 20 doctors per page
    page =  page = request.GET.get('page')
    print request.session["selection"]
    try:
        doctors = paginator.page(page)
    except PageNotAnInteger:
        doctors = paginator.page(1)
    except EmptyPage:
        doctors = paginator.page(paginator.num_pages)
    d['doctors'] = doctors
    d['paginator'] = paginator

    return render_to_response('meddy1/doclistings.html',d)

rawDescription: Django 1.6: Pass dropdown value from one template another


code: print ra.content.decode('utf16')

rawDescription: Requests Session with local HTTP adapter output-ing junk


code:
word_list = [['obytay'], ['ikeslay'], ['ishay'], ['artway']]
print ' '.join(word[0] for word in word_list)

rawDescription: How do i turn a list of words into a sentence string?

description: Turn a list of words into a sentence string


code:
a.speed = 10
Object.speed = 20
print b.speed
print a.speed

rawDescription: Changing variables in multiple Python instances


code:
with open(r'path to input file') as f:
    inputfileContents = f.readlines()

def printLines(lines):
    nest_level = 0
    for i, line in enumerate(lines):
        print line.split('{')[-1].split('}')[0]
        if line.startswith('Jump '):
            nest_level -= 1
            if nest_level < 0:
                return
        elif line.startswith('Loop '):
            nest_level += 1
            loopCount = int(line.split()[1])-1
            for cnt in range(loopCount):
                printLines(lines[i+1:])

printLines(inputfileContents)

rawDescription: How to decompress a nested loop in a file : Python

description: Decompress a nested loop in a file : Python


code:
class Factory (list):
    def append(self, cls):
        super(Factory, self).append(cls)
        return cls

factory = Factory()

@factory.append
class Base (object):
    pass

@factory.append
class A (Base):
    pass

@factory.append
class B (Base):
    pass
print factory

rawDescription: Get objects in scope in the order they're defined


rawDescription: Calling Upon An Enviromental Variable in a Path


code:
data="""AB 500
Bus AB 500
News CA
News CA BLAH"""

def getCombinations(tokens):
    count = len(tokens)
    for L in range(1,count+1):
        for i in range(count-L+1):
            yield tuple(tokens[i:i+L])

groupDict = {}
for s in data.split("\n"):
    tokens = s.split()
    for groupKey in getCombinations(tokens):
        if groupKey not in groupDict:
            groupDict[groupKey] = [s]
        else:
            groupDict[groupKey].append(s)

for group, values in groupDict.iteritems():
    if len(values) > 1:
        print group, "->", values

rawDescription: Classify array of strings based on commonalities


code:
from lxml import etree

html='''

Instructions

Sharpen your pencil (or, alternatively, use your pen)

In a large paper sheet, write your name. When the ink thickens slightly, gently open the envelop and insert the letter inside folded into 3. Set aside.

Use the pen again to write your name and address into the evelope. Include the destination address.

Seal the envelop and stamp it

Instruction courtesy of John Doe

  '''

tree=etree.HTML(html)
result=[]
for a in tree.xpath('//*[@itemprop="instructions"]'):
    for i in a.xpath('./p'):
        temptext = ""
        temptext += i.text
        for j in i.getchildren():
            temptext += j.text
            temptext += j.tail
        result.append(temptext)

print result

rawDescription: XPath through python

description: XPath through python


code:
myIndexArray = [1, 2, 3]
first, second, third = myIndexArray
print "First: %d, Second: %d" % (first, second)

rawDescription: PHP list() equivalent in Python

description: PHP list() equivalent in Python


code:
def progressCB(typ, value):
    """Example prototype for progress monitoring.

    This aims to provide info about what is going on
    during a big job.  It should enable, for example, a reasonably
    smooth progress bar to be drawn.  We design the argument
    signature to be predictable and conducive to programming in
    other (type safe) languages.  If set, this will be called
    repeatedly with pairs of values.  The first is a string
    indicating the type of call; the second is a numeric value.

    typ 'STARTING', value = 0
    typ 'SIZE_EST', value = numeric estimate of job size
    typ 'PASS', value = number of this rendering pass
    typ 'PROGRESS', value = number between 0 and SIZE_EST
    typ 'PAGE', value = page number of page
    type 'FINISHED', value = 0

    The sequence is
        STARTING - always called once
        SIZE_EST - always called once
        PROGRESS - called often
        PAGE - called often when page is emitted
        FINISHED - called when really, really finished

    some juggling is needed to accurately estimate numbers of
    pages in pageDrawing mode.

    NOTE: the SIZE_EST is a guess.  It is possible that the
    PROGRESS value may slightly exceed it, or may even step
    back a little on rare occasions.  The only way to be
    really accurate would be to do two passes, and I don't
    want to take that performance hit.
    """
    print 'PROGRESS MONITOR:  %-10s   %d' % (typ, value)

rawDescription: Django PDF: How to display progress in browser?


code:
def recurse_keys(document,parent):
    for key in document.keys():
        if parent!="":
            print(parent+'.'+str(key))
        else:
            print str(key)
        if isinstance(document[key], dict):
            if parent!="":
                recurse_keys(document[key],parent+'.'+str(key))
            else:
                recurse_keys(document[key],str(key))

rawDescription: How to get all the keys of a Json document using pymongo?

description: Get all the keys of a Json document using pymongo


code:
def do_something(foo):
    print 'from do_something', foo

rawDescription: How to access argparse parse_args from another function

description: Access argparse parse_args from another function


code:
from sqlalchemy import and_
from sqlalchemy.sql.expression import case
#...
q = (session.query(#User.username,
                   Channel.id, Channel.title,
                   case([(Subscription.channelId == None, False)], else_=True)
                  ).outerjoin((Subscription,
                                and_(Subscription.userId==User.id,
                                     Subscription.channelId==Channel.id))
                             )
    )
# optionally filter by user
q = q.filter(User.id == uid()) # assuming uid() is the function that provides user.id
q = q.filter(User.sessionId == id()) # assuming uid() is the function that provides user.sessionId
res = q.all()
for r in res:
    print r

rawDescription: How do I join three tables with SQLalchemy and keeping all of the columns in one of the tables?

description: Join three tables with SQLalchemy and keeping all of the columns in one of the tables


rawDescription: Comparing two files and printing similar lines


code:
from ConfigParser import RawConfigParser

dict = {"Fruits":{"Apple":"Green", "Strawberry":"Red"},"Vegies":{"Carrot":"Orange"}  }
# Get a config object
config = RawConfigParser()
# Read the file 'desktop.ini'
config.read(r'C:\Path\To\desktop.ini')

for section in dict.keys():
    for option in dict[section]:
        try:
            # Read the value from section 'Fruit', option 'Apple'
            currentVal = config.get( section, option )
            print "Current value of " + section + " - " + option + ": " + currentVal
            # If the value is not the right one
            if currentVal != dict[section][option]:
                print "Replacing value of " + section + " - " + option + ": " + dict[section][option]
                # Then we set the value to 'Llama'
                config.set( section, option, dict[section][option])
        except:
            print "Could not find " + section + " - " + option

# Rewrite the configuration to the .ini file
with open(r'C:\Path\To\desktop.ini', 'w') as myconfig:
    config.write(myconfig)

rawDescription: How to customize a folder's icon via Python?

description: Customize a folder's icon via Python


code:
import codecs, re, time
from itertools import chain

import numpy as np

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

trainfile = 'train.txt'

# Vectorizing data.
train = []
word_vectorizer = CountVectorizer(analyzer='word')
trainset = word_vectorizer.fit_transform(codecs.open(trainfile,'r','utf8'))
tags = ['bs','pt','bs','pt']

# Training NB
mnb = MultinomialNB()
mnb.fit(trainset, tags)

def most_informative_feature_for_binary_classification(vectorizer, classifier, n=10):
    class_labels = classifier.classes_
    feature_names = vectorizer.get_feature_names()
    topn_class1 = sorted(zip(classifier.coef_[0], feature_names))[:n]
    topn_class2 = sorted(zip(classifier.coef_[0], feature_names))[-n:]

    for coef, feat in topn_class1:
        print class_labels[0], coef, feat

    print

    for coef, feat in reversed(topn_class2):
        print class_labels[1], coef, feat

most_informative_feature_for_binary_classification(word_vectorizer, mnb)

rawDescription: How to get most informative features for scikit-learn classifier for different class?

description: Get most informative features for scikit-learn classifier for different class


code:
decorated = my_decorator(foo)
print foo.view_func

rawDescription: writing custom python decorators, and the magic of how they init

description: Writing custom python decorators, and the magic of how they init


code:
class Test:
    @injectArguments
    def __init__(self, name, surname):
        pass

if __name__=='__main__':
    t = Test('mickey', surname='mouse')
    print t.name, t.surname

rawDescription: python: __init__ template

description: Python: __init__ template


code:
# server.py
from taskit.backend import BackEnd

def my_function(a, b):
    return a * b

backend = BackEnd(dict(my_function=my_function))
backend.main()

# client.py
from taskit.frontend import FrontEnd

backend_addr = '127.0.0.1'
frontend = FrontEnd([backend_addr])
print frontend.work('my_function', 4, 5)

rawDescription: How to pass object as commandline argument in python?

description: Pass object as commandline argument in python


code:
print list(yieldContext(ex_paragraph, t1))
print list(yieldContext(ex_paragraph, t2))

rawDescription: Joining the previous and next sentence using python


code:
for i, txt in enumerate(n):
    print a.iloc[i]
1.6
6.7

rawDescription: Error Annotating Scatterplot

description: Error Annotating Scatterplot


code:
from UserDict import UserDict

class Barn(UserDict):
    '''Barn automatically sets correct values for chickens.
    TBD: What to do if chickens get changed?'''
    def __init__(self, *args, **kw):
        UserDict.__init__(self, *args, **kw)
        pigs = self.data.get('pigs', None)
        if pigs is not None:
            self.data['chicken'] = pigs * 2

    def __setitem__(self, name, value):
        if name == 'pigs':
            self.data['chicken'] = value * 2
        self.data[name] = value

if __name__ == '__main__':
    b = Barn(cows=1, pigs=2)
    print b
    b['pigs'] = 3
    print b

rawDescription: Dynamic Self Referencing Dictionary in Python


code:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--s',
                  help='''first line
second line''')
args = parser.parse_args()
print args.s

rawDescription: How to string format OptionParser() help message?

description: String format OptionParser() help message


code:
import re
p = re.compile(ur'(<(?:abc|ghj)>.*?<\*>)', re.IGNORECASE | re.MULTILINE)
test_str = u"<abc>somechars<*><def>somechars<*><ghj>somechars<*><ijk>somechars<*>"

re.findall(p, test_str)

rawDescription: Regular expression in python re.findall()


code:
def f(*args):
    print args
foo = ['foo', 'bar', 'baz']
gen = (f for f in foo)
f(*gen)

rawDescription: Do python's variable length arguments (*args) expand a generator at function call time?


code:
@callonce
def func():
    print "Creating Table"

rawDescription: How to make a few lines of function execute only once in python? preferably using lambda?

description: Make a few lines of function execute only once in python? preferably using lambda


code:
for name, score in sorted_names:
    print name, score

rawDescription: How to get the top 10 students in the class using Python dict

description: Get the top 10 students in the class using Python dict


code:
from itertools import combinations
print list(combinations(["hel", "lo", "bye"], 2))

rawDescription: Generate all possible strings from a list of token


code:
@Given('^I have (\d+) cukes in my belly')
def i_have_cukes_in_my_belly(self, cukes):
   print "Cukes: " + cukes

rawDescription: How to write cucumber Step definitions in python

description: Write cucumber Step definitions in python


code: print "auth called"

rawDescription: Bind function to Kivy button

description: Bind function to Kivy button


code:
import timeit

setup = 'import numpy as np; a = np.arange(1000); offset_length = %s'

for title, ol in [('few short', '[(3,10),(50,3),(60,10),(95,1)]'),
                  ('few long', '[(3,100),(200,200),(600,300)]'),
                  ('many short', '[(2*x,1) for x in range(400)]')]:
  print '**',title,'**'
  print 'dbaupp 1st:', timeit.timeit('np.concatenate([a[offset:offset+length] for offset,length in offset_length])', setup % ol, number=10000)
  print 'dbaupp 2nd:', timeit.timeit('a[[offset + i for offset,length in offset_length for i in xrange(length)]]', setup % ol, number=10000)
  print '    fraxel:', timeit.timeit('a[np.concatenate([np.arange(offset,offset+length) for offset,length in offset_length])]', setup % ol, number=10000)

rawDescription: Numpy Array Slicing

description: Numpy Array Slicing


code:
def countdown():
    seconds = 60
    print ''
    while seconds >= 0:
        sys.stdout.write('\r  \r')
        sys.stdout.write(str(seconds))
        sys.stdout.flush()
        time.sleep(1)
        seconds -= 1

rawDescription: How to replace string from previous line using \r (Python)

description: Replace string from previous line using \r (Python)


code:
#!/usr/bin/python
#weather.scraper

from bs4 import BeautifulSoup
import urllib

def main():
    """weather scraper"""
    r = urllib.urlopen("https://www.wunderground.com/history/airport/KPHL/2016/1/1/MonthlyHistory.html?&reqdb.zip=&reqdb.magic=&reqdb.wmo=&MR=1").read()
    soup = BeautifulSoup(r, "html.parser")
    tables = soup.find_all("table")

    for table in tables:
        print '>>>>>>> NEW TABLE <<<<<<<<<'

        trs = table.find_all("tr")

        for tr in trs:
            # for each row of current table, write it using | between cells
            print '|'.join([x.get_text().replace('\n','') for x in tr.find_all('td')])

if __name__ == "__main__":
    main()

rawDescription: Python Data Scrapper

description: Python Data Scrapper


code:
# Top Of script file.
def main():
        print [i for i in range(0,100)]

start_time = time.clock()
#YOUR CODE HERE - 1

main()

#YOUR CODE HERE - N
print time.clock() - start_time, "seconds"

rawDescription: Counting Time of code running Python

description: Counting Time of code running Python


code:
import requests

url = 'http://www.whoscored.com/stageplayerstatfeed'
params = {
    'field': '1',
    'isAscending': 'false',
    'orderBy': 'Rating',
    'playerId': '-1',
    'stageId': '9155',
    'teamId': '32'
}
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36',
           'X-Requested-With': 'XMLHttpRequest',
           'Host': 'www.whoscored.com',
           'Referer': 'http://www.whoscored.com/Teams/32/'}

response = requests.get(url, params=params, headers=headers)

fixtures = response.json()
print fixtures

rawDescription: XHR request URL says does not exist when attempting to parse it's content


code:
import json

json_data = open('data.json')
jdata = json.load(json_data)

def printKeyVals(data, indent=0):
    if isinstance(data, list):
        print
        for item in data:
            printKeyVals(item, indent+1)
    elif isinstance(data, dict):
        print
        for k, v in data.iteritems():
            print "    " * indent, k + ":",
            printKeyVals(v, indent + 1)
    else:
        print data

rawDescription: Decoding JSON with Python and storing nested object


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

parser = argparse.ArgumentParser()
parser.add_argument('--file_list', action='append')

args = parser.parse_args()
if args.file_list:
    for f in args.file_list:
        print f

rawDescription: Perl's Getop::Long equivalent in Python: options with multiple values


code:
import imdb

access = imdb.IMDb()
movie = access.get_movie(1132626)

print "title: %s year: %s" % (movie['title'], movie['year'])
print "Cover url: %s" % movie['cover url']

rawDescription: How do you get a thumbnail of a movie using IMDbPy?

description: Get a thumbnail of a movie using IMDbPy


code:
def poly(x,y,n):
    counter = 0
    for nc in range(n+1):
        for i in range(nc+1):
            print "c[", counter, "]",
            print " * ", x, "**", i,
            print " * ", y, "**", nc-i,
            print " + ",
            counter += 1

rawDescription: generate polynomial in python

description: Generate polynomial in python


code:
import time
import sys

def slowshow(text, delay=0.2):
    for c in text:
        time.sleep(delay)
        sys.stdout.write(c)
    print ""

slowshow("Hello, I am Reut Sharabani!")

rawDescription: How to code writing type python script

description: Code writing type python script


code:
from Tkinter import Tk
root = Tk()
print Tk.clipboard_get(root).split(" ")

rawDescription: Is there a way to convert the clipboard content into list in the run in Python?


code:
from Tkinter import *

main = Tk()

def leftKey(event):
    print "Left key pressed"

def rightKey(event):
    print "Right key pressed"

frame = Frame(main, width=100, height=100)
main.bind('<Left>', leftKey)
main.bind('<Right>', rightKey)
frame.pack()
main.mainloop()

rawDescription: python get arrow keys from command line

description: Python get arrow keys from command line


code:
import requests
r = requests.get('http://www.tulane.edu/~howard/SPAN-NLP/mp3/')
print r.text

rawDescription: How to read a url directory in Python

description: Read a url directory in Python


code:
from other_module import ptree
from my_module import ptree__contains

# The __contains__ method is a special api function
# that enables "foo in bar" boolean test statements
ptree.__contains__ = ptree__contains

test_ptree = ptree()
test_ptree.put("follow.the.yellow.brick.road", "OZ!")

print "follow.the.yellow.brick.road" in test_ptree
# > true

rawDescription: Amending a Boost Python wrapper class?

description: Amending a Boost Python wrapper class


code:
from webob.acceptparse import Accept

language_accept = Accept(request.META['HTTP_ACCEPT_LANGUAGE'])
print language_accept.best_match(('en', 'de', 'fr'))
print 'en' in language_accept

rawDescription: Python - Extract Main Language Code from HTTP / Browser


code:
di={r'^\w\w?a': '"a" is second or third letter' , r'^[^aeiou][aeiou]': "vowel after non-vowel"}
for s in ('aaa', 'bag', 'box', 'drag'):
    print s, [di[k] for k in di if re.search(k, s)]

rawDescription: Dictionary key in python are RegExp

description: Dictionary key in python are RegExp


rawDescription: Getting exclusive (only one) latest related object from relationship


code:
import re

full_pattern = r'\w{3,4}\W\s\d{1,2}\s\w{3}\s\d{4}\s\d{2}:\d{2}:\d{2}'
time_pattern = r'\d{1,2}:\d{2}(\s(A|P)M)?'
combo_regex = '(%s)' % ('|'.join([full_pattern, time_pattern]),)

with open(r'C:\Users\spikem\file_with_two_different_types_of_dates.txt','r') as f:
    for line in f:
        p = re.search(combo_regex, line, re.IGNORECASE)
        if p:
            print p.group() # Assumes a max of one match per line

rawDescription: Matching multiple kinds of dates/timestamps using regex


rawDescription: converting scrapy to lxml

description: Converting scrapy to lxml


code:
l = ["foo", "boo", "foobar"]
common = set.intersection(*map(set,l))
print common
set(['o'])

rawDescription: How to approach this loop?

description: Approach this loop


code:
from collections import defaultdict

results = defaultdict(list)

with open('flattext', 'r') as f:
    for row in f.read().strip().split('\n'):
        if row.strip() != "":
            tenant_id, ip = row.split()
            results[tenant_id].append(ip)

print results.get('1235b3a73ad24b9c86cf301525310b24', None)
print results.items()

rawDescription: Fetching a IP from text file using python


code:
'''
Created on Apr 20, 2015

@author: chris
'''
import web
import time
import threading
import json
from Queue import Queue, Empty
import os
from jobqueue import JobManagement

urls = (
        '/request', 'ProcessRequest',
        '/status', 'CheckStatus',
    )

app = web.application(urls, globals())

next_int = 0

def getNextInt():
    global next_int
    the_int = next_int
    next_int += 1
    return the_int

class ProcessRequest:
    def GET(self):
        if JobManagement.incoming_queue.full():
            print "Queue is full"
            return web.InternalError("Queue is full, please try submitting later.")
        else:
            job_id = getNextInt()
            req_folder = "req" + str(job_id)
            print JobManagement.incoming_queue
            JobManagement.incoming_queue.put(job_id)
            JobManagement.incoming_jobs.append(job_id)
            print "Received request, assigning Drop Folder {0}".format(req_folder)
            web.header('Drop-Folder', req_folder)
            return req_folder

class CheckStatus:
    def GET(self):
        if str(web.input().jobid) == 'all':
            # Construct JSON to return
            web.header('Content-Type', 'application/json')
            return {'In Queue': JobManagement.incoming_jobs,
                            'Currently Processing': JobManagement.current_job,
                            'Finished': JobManagement.finished_jobs
                    }
        try:
            jobid = int(web.input().jobid)
        except ValueError:
            jobid = -1
        print jobid
        if jobid in JobManagement.finished_jobs:
            file_string = "results{0}.json".format(jobid)
            try:
                json_file = open(file_string)
                JobManagement.finished_jobs.remove(jobid)
                os.remove(file_string)
                web.header('Process-Status', 'Complete')
                web.header('Content-Type', 'application/json')
                return json.load(json_file)
            except IOError:
                web.header('Process-Status', 'Complete, but failed to retrieve file, saving')
                return ""

        elif jobid is JobManagement.current_job:
            web.header('Process-Status', 'Processing')
        elif jobid in JobManagement.incoming_jobs:
            web.header('Process-Status', 'In Queue')
        else:
            web.header('Process-Status', 'Unknown')
        return ""

def processJobs():
    while True:
        print JobManagement.incoming_queue
        print "Job processor thread active"
        JobManagement.current_job = JobManagement.incoming_queue.get(block=True)
        JobManagement.incoming_jobs.remove(JobManagement.current_job)
        print "Processing job {0}".format(JobManagement.current_job)
        # Do magical Spark stuff here
        time.sleep(10)  # Simulate a Spark Job
        JobManagement.finished_jobs.append(JobManagement.current_job)
        JobManagement.current_job = None
        print "Job processor thread ready for next job"
    print "Job processor thread finished"

if __name__ == '__main__':
    print JobManagement.incoming_queue
    job_processor_thread = threading.Thread(target=processJobs)
    job_processor_thread.start()
    app.run()

rawDescription: Python: Blocking issues with a queue and threads


code:
from termcolor import colored

print colored('hello', 'red'), colored('world', 'green')

rawDescription: Change The Colour Of Python Text

description: Change The Colour Of Python Text


code:
from collections import Counter

c = Counter()
for s in (listOfLists):
    c.update(s)

for item, nbItems in c.iteritems():
    if nbItems == 3:
        print '%s belongs to three lists.' % item

rawDescription: How to get the smallest list of duplicate items from list of lists?

description: Get the smallest list of duplicate items from list of lists


code:
val = 0123
print val

rawDescription: how to retain the leading "00" when incrementing a value

description: Retain the leading "00" when incrementing a value


code:
import cv2

img = cv2.imread("sample_frame.jpg") #Loading an image in RGB mode.

def iterate_line(img, r_thresh, g_thresh, b_thresh, y):
    """
        This function returns true only when a given row at a height "y"
        from the origin(top - left) if fully black and false othrwise
    """
    for i in img[y]:
        if not((i[0]<b_thresh) and (i[1]<g_thresh) and i[2]<b_thresh):
            return False
    return True

height, width, channels = img.shape

print width, height

border_threshold_R = 4
border_threshold_G = 4
border_threshold_B = 4

top_border_height = 0
bottom_border_height = 0

for i in xrange(height/2):
    mid_pixel_top_half = img[i][width/2]
    R, G, B = mid_pixel_top_half[2], mid_pixel_top_half[1], mid_pixel_top_half[0]
    if (R<border_threshold_R) and (G<border_threshold_G) and (B<border_threshold_B):
        top_border_height+=1
    else:
        break

for i in xrange(height-1, (height/2)-1, -1):
    mid_pixel_bottom_half = img[i][width/2]
    R, G, B = mid_pixel_bottom_half[2], mid_pixel_bottom_half[1], mid_pixel_bottom_half[0]
    if (R<border_threshold_R) and (G<border_threshold_G) and (B<border_threshold_B):
        bottom_border_height+=1
    else:
        break

if (top_border_height>1) and (bottom_border_height>1):

    corrected_top_border_height = 0
    corrected_bottom_border_height =0

    for i in xrange(top_border_height):
        if iterate_line(img, border_threshold_R, border_threshold_G, border_threshold_B, i):
            corrected_top_border_height+=1
        else:
            break

    for i in xrange(height-1, height-1-bottom_border_height, -1):
        if iterate_line(img, border_threshold_R, border_threshold_G, border_threshold_B, i):
            corrected_bottom_border_height+=1
        else:
            break

    if corrected_bottom_border_height>1 and corrected_top_border_height>1:
        print "The frame has borders."
    else:
        print "The frame has no borders."

else:
    print "The frame has no borders."

print top_border_height, bottom_border_height
print corrected_top_border_height, corrected_bottom_border_height

rawDescription: Detect if image has border using PIL

description: Detect if image has border using PIL


code:
text = """show the (name) of the (person)
calc the sqrt of (+ (* (2 4) 3))"""
import pyparsing
for match in pyparsing.nestedExpr('(',')').searchString(text):
  print match[0]

rawDescription: Regex: Match brackets both greedy and non greedy


code:
rows = soup.find('tbody').findAll('tr')

for row in rows:
    cells = row.findAll('td')

    output = []

    for i, cell in enumerate(cells):
        if i == 0:
            output.append(cell.text.strip())
        elif cell.find('img'):
            output.append(cell.find('img')['title'])
        elif cell.find('input'):
            output.append(cell.find('input')['value'])
    print output

rawDescription: Beautiful soup, html table parsing

description: Beautiful soup, html table parsing


code:
# assume that your data rows are dicts
template = "{CLASSID:8}|{DEPT:10}|{C_NUM:15}|{AREA:7}|{TITLE:10}" # same, but named
print template.format( # header
  CLASSID="CLASSID", DEPT="DEPT", C_NUM="COURSE NUMBER",
  AREA="AREA", TITLE="TITLE"
)
for rec in your_data_source:
  print template.format(**rec)

rawDescription: How to Print "Pretty" String Output in Python

description: Print "Pretty" String Output in Python


code:
temp = {(cd["dept"], cd["sku"]):cd["qty"] for cd in returned}

for cd in sold:
    cd["sold"] = cd["qty"]
    del cd["qty"]
    cd["return"] = temp.get((cd["dept"], cd["sku"]), 0)

print sold

rawDescription: Merge multiple lists of dictionaries and rename the key


rawDescription: Extracting data from a list

description: Extracting data from a list


code:
while abs(newbalance) > 0.001: # check if we are withing +- .001 of clearing balance
    newbalance = balance
    month = 0
    for month in range(12): # loop over 12 month range
        print('low = ' + str(Low) + ' high = ' + str(High) + ' ans = ' + str(ans))
        newbalance -= ans # deduct ans not the the original value set outside the while
        interest = monthlyInterestRate * newbalance
        newbalance += interest
        print month
    if newbalance > 0:
        Low = ans
    else:
        High = ans
    ans = (Low + High) / 2.0
print "Lowest Payment: {:.2f}".format(ans) # format to two decimal places

description: Pset2 Python Problem3-UsingBisectionSearch.py


code:
my_dictionary = {}

f =  open('PSWD_output.txt','r')
line = f.readline()
while(line):
    for letter in line:
        if letter in my_dictionary:
            my_dictionary[letter] +=1
        else:
            my_dictionary[letter] = 1
    line = f.readline()

print my_dictionary

rawDescription: Python - Ready text file and report count of individual characters


code: print vars.__doc__

rawDescription: Print / view all variables whilst a script is running in python


code:
#!/usr/bin/env python

TEST1="""
#include "Foo.h"
#define bar foo\\
    x
#include "Bar.h"
"""

TEST2="""
#define bar foo
#define x 1 \\
    12 \\
    2 \\\\ 3
Foobar
"""

TEST3="""
#define foo(x) if(x) \\
doSomething(x)
"""

TEST4="""
#define foo(x) if(x) \\
doSomething(x)
normalCode();
"""

import re
matcher = re.compile(r"^[ \t]*#define(.*\\\n)+.*$",re.MULTILINE)

def extractDefines(s):
    mo = matcher.search(s)
    if not mo:
    	print mo
    	return
    print mo.group(0)

extractDefines(TEST1)
extractDefines(TEST2)
extractDefines(TEST3)
extractDefines(TEST4)

rawDescription: python regex to match multi-line preprocessor macro

description: Python regex to match multi-line preprocessor macro


code:
#!/usr/bin/python3.2
print "Hello"

rawDescription: Setting alternative version of python in script


code:
from bs4 import BeautifulSoup
htmlsource = '
Appstore for Android
'
soup = BeautifulSoup(htmlsource)
for link in soup.select('li > a[href^=http://www.amazon.ca/Best-Sellers]'):
    print link['href'], link.get_text()

rawDescription: Python RE does not return anything after /ref=


code:
from lxml import etree
import pandas as pd

tree = etree.parse("http://charts.realclearpolitics.com/charts/1044.xml")

date = [s.text for s in tree.xpath("series/value")]
approve = [float(s.text) if s.text else 0.0
           for s in tree.xpath("graphs/graph[@title='Approve']/value")]
disapprove = [float(s.text) if s.text else 0.0
              for s in tree.xpath("graphs/graph[@title='Disapprove']/value")]

assert len(date) == len(approve) == len(disapprove)

finalresult = pd.DataFrame({'Date': date, 'Approve': approve, 'Disapprove': disapprove})
print finalresult

rawDescription: Parse through an xml in python

description: Parse through an xml in python


rawDescription: Python: Regular expression to change image filename


code:
class whatever:
      def __init__(self,fn):
         self.fn = fn
      def do_it(self,*args,**kwargs):
         return self.fn(*args,**kwargs)
         #if you wanted the fn to have self as the first argument
         #return self.fn(self,*args,**kwargs) #just pass self as first argument

x = whatever(add)
print x.do_it(5,8)

rawDescription: How to pass a function as a parameter to a class in python

description: Pass a function as a parameter to a class in python


code:
import time

start_time = 0
aList = [1,2,3,4,5]
k = len(aList)//2

def quickSelect(aList, k):
    start_time = time.time()
#     print "%10.6f"%start_time
#     pivot = aList[0]
    if len(aList) != 0:
        pivot = aList[(len(aList) // 2)]
        smallerList = []
        for i in aList:
            if i < pivot:
                smallerList.append(i)
                largerList = []
        for i in aList:
            if i > pivot:
                largerList.append(i)
        m = len(smallerList)
        count = len(aList) - len(smallerList) - len(largerList)
        if k >= m and k < m + count:
            print "Pivot", pivot
#             print "%10.6f"%time.time()
            print "time: ", time.time() -start_time
            return pivot
        elif m > k:
            return quickSelect(smallerList, k)
        else:
            return quickSelect(largerList, k - m - count)

quickSelect(aList, k)

rawDescription: How would quickselectg act differently if pivot wasn't the middle term


code:
from collections import defaultdict

with open('1.txt') as fin:
    frame_rects = defaultdict(list)
    for row in (map(int, line.split()) for line in fin):
        id, frame, rect = row[0], row[2], row[3:7]
        frame_rects[frame].append((id, rect))
        # print '{} {} {}'.format(frame, id, rect) # (if you want to sample)

for key, value in frame_rects.items():
    print key, ' '.join([' '.join([str(i) for i in v]) for v in value])

rawDescription: Parsing a text file and grouping certain values


code:
# Just like we want a class to represent teams, since those are "a thing" in our
# program, we want one for each player as well.

class player(object):
  __slots__ = ['name', 'stats', 'pos']
  def __init__(self, name, stats, pos):
    self.name = name
    self.stats = stats
    self.pos = pos

# Asking the user for information to create an object is not the responsibility of
# that class. We should use external functions for this.
def create_player(name):
  print 'add stats' # fill out the appropriate stats through raw_input
  stat1 = raw_input('stat1: ')
  stat2 = raw_input('stat2: ')
  pos = raw_input('POS: ')
  # Now we create and return the 'player' object.
  return player(name, {'stat1': stat1, 'stat2': stat2}, pos)

class team(object):
  __slots__ = ['name_to_player', 'position_to_player']
  def __init__(self):
    # We don't make any lists, just dicts, because we want to use them primarily
    # for lookup. Notice how I've named the attributes. In particular, I **don't**
    # talk about type names. That's just an implementation detail. What we care about
    # is how they work: you put a name in, get a player out.
    self.name_to_player = {}
    self.position_to_player = {}

  # Again, we don't ask the questions here; this just actually adds the player.
  def add_player(self, player):
    self.name_to_player[player.name] = player
    self.position_to_player[player.pos] = player

  # Again, we don't ask the questions here; this just does the actual edit.
  def edit_player(self, name, stat, new_value):
    self.name_to_player[name].stats[stat] = new_value

def main(): # the main function
  teams = {} # dict from team name to team object.
  while True:
    print teams.keys()
    # Your human interface was needlessly awkward here; you know from the supplied name
    # whether it's a new team or an old one, because it will or won't be in your
    # existing set of teams. Similarly for players.
    team_name = raw_input('team name? ')
    if team_name not in teams.keys():
      teams[team_name] = team() # create a new team
    else: # edit an existing one
      team = teams[team_name]
      player_name = raw_input('player name? ')
      if player_name in team.name_to_player.keys(): # edit an existing player
        stat = raw_input("stat? ")
        value = raw_input("value? ")
        team.edit_player(player_name, stat, value)
      else: # add a new player
        team.add_player(create_player(player_name))

if __name__ == '__main__':
  main()

rawDescription: In python. How do I have a user change a dictionary value, when that dictionary is in a class?


code:
for line in f:
    print line

rawDescription: Python Read URLs from File and print to file


code:
data = [0x1, 0x4, 0x5, 0xFF, 0x12]

new_data = ['%02X' %i for i in data]
print ''.join(new_data)

rawDescription: Convert Integer to Hex with result always having 2 bytes


code:
class Engine:
    def __init__(self):
        self.scenes = [Scene1(),Scene2(),Scene3()]
    def run(self):
        for s in self.scenes:
            print s.enter()

Engine().run()

rawDescription: OOP how to get the script (a game) to run


code:
lat_start = 53.22010374071
lat_stop = 53.46673202514641

lon_start = 1.22201037407
lon_stop = 1.33312034607

for lat in numpy.linspace( lat_start, lat_stop, 100 ):
  for lon in numpy.linspace( lon_start, lon_stop, 100 ):
     print "Doing grid at %f %f " % (lat,lon)

rawDescription: How do I set the boundaries for a 'for loop'?

description: Set the boundaries for a 'for loop'


code:
if set(pla_back) >= set(acceptItem):
    print 'Player has the required items'
else:
    print "Player doesn't have the required items"

rawDescription: Comparing two lists for true and false

description: Comparing two lists for true and false


code:
t = "first%s\n"
print repr(t),len(t)
t = t %("second")
print repr(t),len(t)

print '-------------------'

t = "first%s\\n" # the same as r"first%s\n"
print repr(t),len(t)
t = t %("second")
print repr(t),len(t)

rawDescription: Retain the "\n"

description: Retain the "\n"


code:
import difflib

def diff_indices(a, b):
    sm = difflib.SequenceMatcher(a=a, b=b)
    res = []
    for tag, i1, i2, j1, j2 in sm.get_opcodes():
        if tag == 'replace':
            res.append([range(i1,i2), range(j1,j2)])
    return res

print diff_indices(
    a="node1 ip up ether 40  46 server low".split(),
    b="node1 ip <down> ether <number(s)> server <status>".split())
#[[[2], [2]], [[4, 5], [4]], [[7], [6]]]
print diff_indices(
    a=' label 0 12000 '.split(),
    b=' label  <number>  <number>'.split())
#[[[1, 2], [1, 2]]]

rawDescription: find indices where 2 list differ

description: Find indices where 2 list differ


code:
while True:
    for line in f:
        print line
    time.sleep(0.5)

rawDescription: Read a file continuously and sending the output to another file


code:
import csv

with open('all16.csv', 'rb') as inf:
    has_header = csv.Sniffer().has_header(inf.read(1024))
    inf.seek(0)  # rewind
    incsv = csv.reader(inf)
    if has_header:
        next(incsv)  # skip header row
    column = 1
    datatype = float
    data = (datatype(row[column]) for row in incsv)
    least_value = min(data)

print least_value

rawDescription: When processing CSV data, how do I ignore the first line of data?


code:
try:
  int(userInputVariable)
except ValueError:
  print "Not a number"

rawDescription: Python logical NOT operator for regular expressions


rawDescription: Regex filter numbers divisible by 3

description: Regex filter numbers divisible by 3


rawDescription: filter list of values in a dictionary

description: Filter list of values in a dictionary


code:
import zipfile, sys, os, glob, shutil

def zipShapefile(myFolder):
    # Check if folder exists
    if not (os.path.exists(myFolder)):
        print myFolder + ' Does Not Exist!'
        return False

    # Get a list of shapefiles
    ListOfShapeFiles = glob.glob(myFolder + '*.shp')

    # Main shapefile loop
    for sf in ListOfShapeFiles:
        print 'Zipping ' + sf

        # Create an output zip file name from shapefile
        newZipFN = sf[:-3] + 'zip'

        # Check if output zipfile exists, delete it
        if (os.path.exists(newZipFN)):
            print 'Deleting '+newZipFN
            os.remove(newZipFN)
            if (os.path.exists(newZipFN)):
                print 'Unable to Delete' + newZipFN
                return False

        # Create zip file object
        zipobj = zipfile.ZipFile(newZipFN,'w')

        # Cycle through all associated files for shapefile adding them to zip file
        for infile in glob.glob( sf.lower().replace(".shp",".*")):
            print 'zipping ' + infile + ' into ' + newZipFN
            if infile.lower() != newZipFN.lower() :
                # Avoid zipping the zip file!
                zipobj.write(infile,os.path.basename(infile),zipfile.ZIP_DEFLATED)

        # Close zipfile
        print 'ShapeFile zipped!'
        zipobj.close()

    # Got here so everything is OK
    return True

def main():
    # Set the folder that contains the ShapeFiles
    myFolder = "C:/data/shp/recycling/"

    # Call function to zip files
    b = zipShapefile(myFolder)

    if b:
        print "Zipping done!"
    else:
        print "An error occurred during zipping."

if __name__ == '__main__':
    main()

rawDescription: loop through path list held in txt

description: Loop through path list held in txt


code:
def track(tracker):
    def wrapper(func):
        tracker.append(func)
        return func
    return wrapper

class Example:
    tracker = []

    @track(tracker)
    def method_a(self):
        return [('key_a1', 'val_a1'), ('key_a2', 'val_a2')]

    @track(tracker)
    def method_b(self):
        return [('key_b1', 'val_b1'), ('key_b2', 'val_b2')]

    def collect_data(self):
        return dict(tup for method in self.tracker for tup in method(self))

print Example().collect_data()
# {'key_b1': 'val_b1', 'key_b2': 'val_b2', 'key_a1': 'val_a1', 'key_a2': 'val_a2'}

rawDescription: Calling methods inside class to modify list


code:
FEED_TYPES = [
    ('fan_mail',     'Fan Mail'),
    ('review',       'Review'),
    ('tip',          'Tip'),
    ('fan_user',     'Fan User'),
    ('fan_song',     'Fan Song'),
    ('fan_album',    'Fan Album'),
    ('played_song',  'Played Song'),
    ('played_album', 'Played Album'),
    ('played_radio', 'Played Radio'),
    ('new_event',    'New Event'),
]

def add_feed_types(cls):
    for type_tuple in FEED_TYPES:
        kind, name = type_tuple
        def make_notify(kind):
            def notify(self, **kwargs):
                print "notifying %s" % kind
                self.create(kind, **kwargs)
            return notify
        notify = make_notify(kind)
        notify.__name__ = "notify_%s" % kind
        setattr(cls, notify.__name__, classmethod(notify))
    return cls

@add_feed_types
class Feed:
    @classmethod
    def do_create(cls, **kwargs):
        print kwargs

    @classmethod
    def create(cls, kind, **kwargs):
        kwargs['feed_type'] = kind
        cls.do_create(**kwargs)

Feed.create("FanMail", to_profile="Gerson", from_profile="Felipe")
Feed.notify_fan_mail(to_profile="Gerson2", from_profile="Felipe2")

rawDescription: Dynamically adding class methods to a class


code:
import unittest
from selenium import webdriver
import datetime
import os
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from random import randint
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from sshtunnel import SSHTunnelForwarder
import MySQLdb
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import autoit

class SprintTests(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.get("http://colombo.in.ua/colombo-design/ruchka-colombo-gira-jm11.html")
        self.driver.implicitly_wait(30)
        self.driver.maximize_window()

    def test_input(self):
        dropdown = self.driver.find_element_by_id("jshop_attr_id13")
        options = dropdown.find_elements_by_tag_name('option')
        for option in options:
                a = self.driver.find_element_by_xpath('//*[@id="block_price"]')
                b=a.text
                a = self.driver.find_element_by_xpath('//*[@id="jshop_attr_id13"]').click()
                print b
                autoit.send("{DOWN}{ENTER}")
                time.sleep(2)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main(verbosity=2)

rawDescription: Scrapy, python+selenium. Errors in the information gathered from the page


code:
#!/usr/bin/env python2
import os
from glob import glob

dirpath = os.path.expanduser(ur"~\Desktop\1\save")
for mojibake_path in glob(os.path.join(dirpath, '*.txt')):
    path = mojibake_path.encode('cp1251').decode('utf-8')
    os.rename(mojibake_path, path)

rawDescription: Convert windows-1251 directory names to unicode (using Python)


code:
for line in t:
    print line

rawDescription: Extracting data from generator object

description: Extracting data from generator object


code:
import pandas as pd
import numpy as np
import random
import string

k = 5
N = 10

#http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randint.html
#http://stackoverflow.com/a/2257449/2901002

df = pd.DataFrame({ 'A' : range(1, N + 1 ,1),
    'B' : np.random.randint(k, k + 100 , size=N),
    'C' : pd.Series(random.choice(string.ascii_uppercase) for _ in range(N)) })

print df
#    A   B  C
#0   1  60  O
#1   2  94  L
#2   3  10  W
#3   4  94  X
#4   5  60  O
#5   6  20  K
#6   7  58  Y
#7   8  40  I
#8   9  49  X
#9  10  65  S

rawDescription: pandas create a series with n elements (sequential or randbetween)

description: Pandas create a series with n elements (sequential or randbetween)


code:
import socket,struct

def makeMask(n):
    "return a mask of n bits as a long integer"
    return (2L<<n-1) - 1

def dottedQuadToNum(ip):
    "convert decimal dotted quad string to long integer"
    return struct.unpack('L',socket.inet_aton(ip))[0]

def networkMask(ip,bits):
    "Convert a network address to a long integer"
    return dottedQuadToNum(ip) & makeMask(bits)

def addressInNetwork(ip,net):
   "Is an address in a network"
   return ip & net == net

address = dottedQuadToNum("192.168.1.1")
networka = networkMask("10.0.0.0",24)
networkb = networkMask("192.168.0.0",24)
print (address,networka,networkb)
print addressInNetwork(address,networka)
print addressInNetwork(address,networkb)

rawDescription: How can I check if an ip is in a network in python

description: Check if an ip is in a network in python


code:
import re
pattern = '()'
url1 = '
\r\n
'
url2 = '
\r\n
'
print 'result of url1: \n', re.sub(pattern, r'\1',url1), '\n'
print 'result of url2: \n', re.sub(pattern, r'\1',url2)

rawDescription: removing a string using Regex and lookahead

description: Removing a string using Regex and lookahead


code:
class QTableWidgetClickable(QtGui.QTabWidget):
    tabClicked = pyqtSignal(int)

    def __init__(self, parent=None):
        super(QTableWidgetClickable, self).__init__(parent)

        # add qpushbutton
        self.addButton = QtGui.QPushButton()

        # connect a signal
        self.addButton.clicked.connect(self.tabClicked)

    def tabClicked(self):
        print "tab clicked!"

rawDescription: PyQt4: How/when to emit custom signal from subclass of QTabWidget?


rawDescription: How can I pairwise sum two equal-length tuples

description: Pairwise sum two equal-length tuples


code:
from itertools import product
A = [[1, 2, 3], ['foo', 'bar'], ['spam', 'eggs']]
for vector in product(*A):
    print vector

rawDescription: How to return all possible combination

description: Return all possible combination


code:
from itertools import groupby
from operator import itemgetter

l = (('A', '1', 'UTC\xb100:00'), ('B', '1', 'UTC+01:00'), ('C', '1', 'UTC+02:00'), ('D', '1', 'UTC+01:00'), ('E', '1', 'UTC\xb100:00'), ('F', '1', 'UTC+03:00'))

result = []
key_items = itemgetter(1, 2)
for key, group in groupby(sorted(l, key=key_items), key=key_items):
    item = []
    item.extend([k[0] for k in group])
    item.extend(key)
    result.append(tuple(item))

print tuple(result)

rawDescription: python group items in tuple with no duplicates

description: Python group items in tuple with no duplicates


code:
import inspect

func = lambda num1,num2: num1 + num2
funcString = str(inspect.getsourcelines(func)[0])
funcString = funcString.strip("['\\n']").split(" = ")[1]
print funcString

rawDescription: How do you read a lambda function as a string?

description: Read a lambda function as a string


code:
for letters in zip('hello', 'apple', 'green', 'paint', 'sting'):
     print letters

rawDescription: In Python, is it possible to unpack a list of strings and put a single character from each string into a generator?


rawDescription: Finding the version of an application from Python?


code:
from nltk.corpus import brown
for sent in brown.tagged_sents():
    if any(pos for word, pos in sent if pos == 'NPS'):
            print sent
            break

rawDescription: Provoke the NLTK part-of-speech tagger to report a plural proper noun


code:
from itertools import islice

class counter:
    count = 0
    def __init__(self):
        print ('  |  counter.count   first == %d  at  %d\n'
               '  |     self.count   first == %d  at  %d')\
               % (counter.count,id(counter.count),
                  self.count,id(self.count))

        self.__class__.count += 1 # <<=====

        print ('  |  counter.count  second == %d  at  %d\n'
               '  |     self.count  second == %d  at  %d\n'
               '  |  id(counter) == %d   id(self) == %d')\
               % (counter.count,id(counter.count),
                  self.count,id(self.count),
                  id(counter),id(self))

def display(*li):
    it = iter(li)
    for ch in it:
        nn = (len(ch)-len(ch.lstrip('\n')))*'\n'
        x = it.next()
        print '%s ==  %s %s' % (ch,x,'' if '__dict__' in ch else 'at '+str(id(x)))

display('counter.count AT START',counter.count)

print ('\n\n----- C1 = counter() ------------------------')
C1 = counter()
display('C1.__dict__',C1.__dict__,
        'C1.count ',C1.count,
        '\ncounter.count ',counter.count)

print ('\n\n----- C2 = counter() ------------------------')
C2 = counter()
print ('  -------------------------------------------')
display('C1.__dict__',C1.__dict__,
        'C2.__dict__',C2.__dict__,
        'C1.count ',C1.count,
        'C2.count ',C2.count,
        'C1.__class__.count',C1.__class__.count,
        'C2.__class__.count',C2.__class__.count,
        '\ncounter.count ',counter.count)

print '\n\n------- C2.__class__.count = 3 ------------------------\n'
C2.__class__.count = 3
display('C1.__dict__',C1.__dict__,
        'C2.__dict__',C2.__dict__,
        'C1.count ',C1.count,
        'C2.count ',C2.count,
        'C1.__class__.count',C1.__class__.count,
        'C2.__class__.count',C2.__class__.count,
        '\ncounter.count ',counter.count)

print '\n\n------- C2.count = 5 ------------------------\n'
C2.count = 5
display('C1.__dict__',C1.__dict__,
        'C2.__dict__',C2.__dict__,
        'C1.count ',C1.count,
        'C2.count ',C2.count,
        'C1.__class__.count',C1.__class__.count,
        'C2.__class__.count',C2.__class__.count,
        '\ncounter.count ',counter.count)

rawDescription: What in the world is the attribute "__class__" in python


code: print poly.powers_

rawDescription: Generate the full polynomial regression formula, not just coefficients using sklearn?


code:
import time

input_format = "%m/%d/%Y %I:%M:%S %p INFO" # or %d/%m...
output_format = "%Y-%m-%d %H:%M:%S"

def convert_time(logging_time):
    return time.strftime(output_format, time.strptime(logging_time, input_format))

print convert_time("08/07/2013 11:40:08 PM INFO")
# prints 2013-08-07 23:40:08

rawDescription: Python logging time stamp convert to mysql timestamp


code:
import sys
import inspect

class A(object):
    def __init__(self):
        self.vanilla_b = B()
        self.chocolate_b = B()

class B(object):
    def __init__(self):
        line_num = sys._getframe().f_back.f_lineno
        lines = inspect.getsourcelines( sys.modules[__name__] )[0]
        line = lines[line_num - 1]
        attr = line.split("=")[0].split(".")[1].strip()
        print "B() is being assigned to", attr

A()

rawDescription: Getting the attribute name that the created object will be given


code:
d = {}

def f(n):
    if n == 1:
        return 1, -1
    if d.get(n) is not None:
        return d[n]
    ans = (f(n - 1)[0] + 1, n - 1)

    if n % 2 == 0:
        ret = f(n // 2)
        if ans[0] > ret[0]:
            ans = (ret[0] + 1, n // 2)

    if n % 3 == 0:
        ret = f(n // 3)
        if ans[0] > ret[0]:
            ans = (ret[0] + 1, n // 3)

    d[n] = ans
    return ans

def print_solution(n):
    if f(n)[1] != -1:
        print_solution(f(n)[1])
    print n,

def solve(n):
    print f(n)[0]
    print_solution(n)
    print ''

solve(10)

rawDescription: Dynamic programming for primitive calculator


code:
class Person():
    name = "temp"
    def __init__(self, aname):
        global name
        print("A Person is created!")
        name=aname
        self.test()

    def test(self):
        print(name)
        print(self.name)

p1 = Person("Tim")
print name

rawDescription: Multiple values for same variable when designing Objects (use of self in Python)


code:
print quotedData.getvalue()
"1","2","3"

rawDescription: Read a CSV file and put double quote on each item in python


code:
print '=' * 30 + " Out " + "=" * 30

# Remove first and last rows.
del sub[0]
sub.pop()

# reverse values in lists
temp_sub = [l[::-1] for l in sub]
# reverse lists in matrix.
temp_sub.reverse()

# Add them and Print.
pprint([head] + sub + temp_sub + [tail])

rawDescription: Numbers in a Matrix

description: Numbers in a Matrix


code:
def distance_from_zero(n):
    try:
        n = int(n)
    except ValueError:
        try:
            n = float(n)
        except ValueError:
            print "Not a number!"
            n = float("NaN")
    return abs(n)

rawDescription: Review: Built-In Functions

description: Review: Built-In Functions


code:
processors_info = "\n\t\t\t".join(machine_info['processors'])
print "Processor(s):\t{}".format(processors_info)

rawDescription: Python printing Formatted text with lists nested in dict


code:
import re
mylist = ['"5" is a magic number.',
'"6" is a magic number.',
'"7" is a magic number.',
'This line is extra...',
'This line is extra...',
'"8" is a magic number.']
print re.findall(r'"\d+" is a magic number', ' '.join(mylist))

rawDescription: What‘s the best way to pick out string of a specific pattern in python?


code:
def Walk(obj):
  recurse = (yield obj)
  if not recurse:
    #print 'stop recurse:', recurse
    return

  if isinstance(obj, list):
    #print 'is list:', obj
    for item in obj:
      #print 'item loop:', item
      walker = Walk(item)

      recurse = None #first send must be None
      while True:
        try:
          x = walker.send(recurse)
        except StopIteration:
          break
        #print 'item walk:', x
        recurse = (yield x)
        #print 'item walk recurse:', recurse

root = ['a', ['b.0', ['b.0.0']]]

walker = Walk(root)
i = 0
x = walker.next()
while True:
  print i, x
  try:
    x = walker.send(True)
  except StopIteration:
    break
  i += 1

rawDescription: Recursive generators and send()

description: Recursive generators and send()


code:
with open("input.txt", "r") as f_input:
    for line in f_input:
        print tuple([x for x in re.split("(?<=@)([-a-z0-9]+)|([0-9=+-])|( )", line.rstrip("\n")) if x])

rawDescription: Readline into Tuples

description: Readline into Tuples


code:
import re
p = re.compile(ur'&\w+=')
test_str = u"asdfasdf&asdf=asdfffff&abc123=asdf\n\n\n"

re.findall(p, test_str)

rawDescription: Regex for ampersand, alphabetic characters and equal sign in python


code:
import mapnik
stylesheet = 'world_style.xml'
image = 'world_style.png'
m = mapnik.Map(2000, 1500)
mapnik.load_map(m, stylesheet)
m.zoom_all()
mapnik.render_to_file(m, image)
print "rendered image to '%s'" % image

rawDescription: Python: What is the best way of creating a png of a map with a route for a written report?


code:
import itertools

for pair in itertools.combinations(range(len(A)),2):
    if np.array_equal(A[pair[0]],A[pair[1]]) and np.array_equal(A[:,pair[0]],A[:,pair[1]]): #compare columns
        print pair

rawDescription: finding identical rows and columns in a numpy array

description: Finding identical rows and columns in a numpy array


code:
from itertools import islice, izip

s = "to_be_or_not_to_be"
pos = [15, 2, 8]

length = len(s)

for start1, start2 in izip(pos, islice(pos, 1, None)):
    pref = 0
    for pos1, pos2 in izip(xrange(start1, length), xrange(start2, length)):
        if s[pos1] == s[pos2]:
            pref += 1
        else:
            break
    print pref
# prints 3 1

rawDescription: Longest common prefix using buffer?

description: Longest common prefix using buffer


code:
list_1 = ['a', 'big', 'list']
list_2 = ['another', 'big', 'list']

target_set = set(list_2)

for a in list_1:
    if a in target_set:
         print a

rawDescription: Avoiding nesting two for loops

description: Avoiding nesting two for loops


rawDescription: BYTE to python C-Type structure Conversion Issue


code:
for info in data['features']:
    print info['properties']['location']['road']

rawDescription: return value in json file 5 levels deep in python using simplejson

description: Return value in json file 5 levels deep in python using simplejson


code:
import random
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure

try:
    import Tkinter as Tk
except ImportError:
    import tkinter as Tk
import tkMessageBox

class PlotClassifier(Tk.Tk):
    def __init__(self, plot_generator, arguments, classes, classification_callback, *args, **kwargs):
        Tk.Tk.__init__(self, *args, **kwargs)
        self.title("Plot classifier, working on %i plots" % len(arguments))
        #self.label = Tk.Label(text="Plot classifier, working on %i plots" % len(arguments))
        #self.label.pack(padx=10, pady=10)
        self._plot_generator = plot_generator
        self._arguments = arguments
        self._classes = [str(x) for x in classes]
        self._classification_callback = classification_callback
        self._setup_gui()

    def _setup_gui(self):
        #self.columnconfigure(0, minsize=100, weight=2)
        #self.columnconfigure(1, minsize=500, weight=8)
        f = Figure()
        self._ax = f.add_subplot(111)
        buttons_frame = Tk.Frame(self)
        buttons_frame.pack(side=Tk.TOP, fill=Tk.BOTH, expand=True)
        buttons_class = []
        for i, cls in enumerate(self._classes):
            buttons_class.append(Tk.Button(master=buttons_frame, text=cls,
                                           command=lambda x=i: self.button_classification_callback(self._current_args, x)))
            buttons_class[-1].pack(side=Tk.LEFT)
        button_quit = Tk.Button(master=buttons_frame, text='Quit', command=self.destroy)
        button_quit.pack(side=Tk.RIGHT) #.grid(row=0,column=0)

        self._canvas = FigureCanvasTkAgg(f, master=self)
        self._canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) #.grid(row=0, column=1, rowspan=3) #
        self._canvas.show()

        toolbar = NavigationToolbar2TkAgg( self._canvas, self )
        toolbar.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) #.grid(row=3, column=1) #
        toolbar.update()

    def button_classification_callback(self, args, class_idx):
        self._classification_callback(args, self._classes[class_idx])
        self.classify_next_plot()

    def classify_next_plot(self):
        try:
            self._current_args = self._arguments.pop(0)
            self._ax.cla()
            self._plot_generator(self._ax, *self._current_args)
            self._canvas.draw()
        except IndexError:
            tkMessageBox.showinfo("Complete!", "All plots were classified")
            self.destroy()

def create_plot(ax, factor):
    ax.plot([(i*factor) % 11 for i in range(100)])

def announce_classification(arguments, class_):
    print arguments, class_

if __name__ == "__main__":
    classes = ["Class %i"%i for i in range(1, 6)]
    arguments_for_plot = [[random.randint(1,10)] for x in range(10)]
    root = PlotClassifier(create_plot, arguments_for_plot, classes, classification_callback=announce_classification)
    root.after(50, root.classify_next_plot)
    root.mainloop()

rawDescription: Matplotlib event and replotting

description: Matplotlib event and replotting


code:
with open("qwer.txt",'r') as v1:
  for line in v1:
    if 'good' in line: # note 'line' from here on in
       print(line)
    if '=' in line:
       print line.split('=')[1]
    if '==' in line:
        print line.split('==')[1]

rawDescription: python parsing from dynamic input line

description: Python parsing from dynamic input line


code:
for key, values in dic.iteritems():
    print key, values[0] - values[1]

rawDescription: How to calculate the difference of the values in a dictionary in python?

description: Calculate the difference of the values in a dictionary in python


code:
import urllib2
from xml.etree import ElementTree as ET

Main_Page = urllib2.urlopen("http://www.sec.gov/Archives/edgar/data/75829/000144530512003029/pll-20120731.xml")
for event, (name, value) in ET.iterparse(Main_Page, ['start-ns']):
    print name, ':', value

rawDescription: XBRL Format XML Root Node Attributes Python


code:
from bs4 import BeautifulSoup

html = '\

\

Hindi and \
      English

	'

soup = BeautifulSoup(html)
lang = soup.findAll('a', href = True)
for i in lang:
    print i.string

rawDescription: accessing values in subgroups in html code

description: Accessing values in subgroups in html code


code:
from string import maketrans
instr = "abcdenghi"
outstr = "222555777"
trans = maketrans(instr, outstr)
text = "again"
print text.translate(trans)

rawDescription: Replacing multiple letters in a word with number in python?


code:
data = json.loads(data+'}')
for key, value in data.iteritems():
    print key, value['category'], value['xyz'], value['ABC']

rawDescription: How to process the irregular json file in python to get the serialized values in CSV?

description: Process the irregular json file in python to get the serialized values in CSV


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup('''\

1

2

3

4

5

6

7

8

9

''')
soup.select("div.parent > div.column")[1]
for parent in soup.select('div.parent'):
    column = parent.select('div.column')[1]
    print column

rawDescription: soup.select css nth of type?

description: Soup.select css nth of type


code:
for k in range(1, len(shapes), 2):
    nowcx = shapes[k][0][0] #x coordinate of the center
    nowcy = shapes[k][0][1] #y coordinate of the center
    for z in range(k+1, len(shapes), 2):
        if cv2.pointPolygonTest(shapes[z],(nowcx, nowcy),False)==1:
            print "inside"
        else:
            print "outside"

rawDescription: Data structure in python containing shapes corners and centroids


code:
#Make every codon by zipping the sequence offset by one each time
codon_sequence = [''.join(z) for z in zip(seq,seq[1:],seq[2:])]
#Print every 3rd codon - starting at zero...
print 'aaseq1 = ',''.join([codons[c] for c in codon_sequence[::3]])
#...then starting at 1...
print 'aaseq2 = ',''.join([codons[c] for c in codon_sequence[1::3]])
#...you get the picture...
print 'aaseq3 = ',''.join([codons[c] for c in codon_sequence[2::3]])

rawDescription: Using a for loop in python with more than 1 variable


code:
import glob
import pandas as pd

all_data = pd.DataFrame() #initializes DF which will hold aggregated csv files

for f in glob.glob("*.csv"): #for all csv files in pwd
    df = pd.read_csv(f, header=None) #create dataframe for reading current csv
    all_data = all_data.append(df, ignore_index=True) #appends current csv to final DF
print all_data
#       0      1      2
#0  test1  test1  test1
#1  test1  test1  test1
#2  test1  test1  test1
#3  test2  test2  test2
#4  test2  test2  test2
#5  test2  test2  test2

all_data.to_csv("test/final.csv", index=None, header=None)

rawDescription: using pandas in python to append csv files into one

description: Using pandas in python to append csv files into one


code:
s = '\x89PNG'
print s

rawDescription: How to convert hex string "\x89PNG" to plain text in python

description: Convert hex string "\x89PNG" to plain text in python


code:
class AssignableSetattr(type):
    def __new__(mcls, name, bases, attrs):
        def __setattr__(self, attr, value):
            object.__setattr__(self, attr, value)

        init_attrs = dict(attrs)
        init_attrs['__setattr__'] = __setattr__

        init_cls = super(AssignableSetattr, mcls).__new__(mcls, name, bases, init_attrs)

        real_cls = super(AssignableSetattr, mcls).__new__(mcls, name, (init_cls,), attrs)
        init_cls.__real_cls = real_cls

        return init_cls

    def __call__(cls, *args, **kwargs):
        self = super(AssignableSetattr, cls).__call__(*args, **kwargs)
        print "Created", self
        real_cls = cls.__real_cls
        self.__class__ = real_cls
        return self

class Foo(object):
    __metaclass__ = AssignableSetattr

    def __init__(self, a, b):
        self.a = a
        self.b = b
        for key, value in process(a).items():
            setattr(self, key, value)

    def __setattr__(self, attr, value):
        frob(self.b)
        super(Foo, self).__setattr__(attr, value)

def process(a):
    print "processing"
    return {'c': 3 * a}

def frob(x):
    print "frobbing", x

myfoo = Foo(1, 2)
myfoo.d = myfoo.c + 1

rawDescription: Overriding __setattr__ at runtime

description: Overriding __setattr__ at runtime


code:
import pandas as pd
from datetime import datetime

df = pd.DataFrame({
    'name': ['John', 'Jane', 'Max', 'David'],
    'birthdate': [datetime(1990,1,1), datetime(1957,4,3), datetime(1987,2,3), datetime(1964,2,12)],
})

def get_birthdays(df_x):
    d = {'name': [], 'birthday': []}
    name = df_x.iloc[0]['name']
    original = df_x.iloc[0]['birthdate']
    for year in range(2016, 2019):
        d['name'].append(name)
        d['birthday'].append(original.replace(year=year))
    return pd.DataFrame(d)

print df.groupby('name', group_keys=False).apply(get_birthdays).reset_index(drop=True)

rawDescription: Multiple results for each individual row (one-to-many) with Pandas


code:
def query_separate_sql(cursor):
    print 'query_separate_sql'
    sql = []
    sql.append("""CREATE TEMPORARY TABLE tmp3 (id int)""")
    sql.append("""INSERT INTO tmp3 VALUES (1)""")
    sql.append("""SELECT * from tmp3""")
    for query in sql:
        cursor.execute(query)
        print list(cursor.fetchall())

rawDescription: Python MySQLdb doesn't wait for the result


code:
try:
    domain = a.split('@')[1]
except IndexError:
    print "Oops! No @ symbols exist!"

rawDescription: Python: Using split on a string and returning a tuple?


code:
txt='''\
1\tDemo
\t\tExample
\t\t\tThis is the description text body that I am
\t\t\ttrying to capture with regex.
2\tSecond Demo
\t\tAnother Section
\t\t\tAnd Another 3rd level Section
\t\t\tOn two lines
3\tNo section below
4\tOnly one level below
\t\tThis is that one level
'''

import re

result=[]
for ms in re.finditer(r'^(\d+.*?)(?=^\d|\Z)',txt,re.S | re.M):
    section=ms.group(1)
    tm=map(len,re.findall(r'(^\t+)', section, re.S | re.M))
    subsections=max(tm) if tm else 0
    sec=[re.search(r'(^\d+.*)', section).group(1)]
    if subsections:
        for i in range(2,subsections+1):
            lt=r'^{}([^\t]+)$'.format(r'\t'*i)
            level=re.findall(lt, section, re.M)
            sec.append(' '.join(s.strip() for s in level))

    print '->'.join(sec)

rawDescription: RegEx to capture multi-line text body

description: RegEx to capture multi-line text body


code:
enable_privilege('SeSecurityPrivilege')
print get_file_security('C:\\Program Files')

rawDescription: Python - Get windows folder ACL permissions


code:
from bs4 import Tag

result = []
for child in html.find('div', class_='textbox').children:
    if isinstance(child, Tag):
        result.append(child.get('alt', ''))
    else:
        result.append(child.strip())

print ' '.join(result)

rawDescription: Python, BeautifulSoup - <div> text and <img> attributes in correct order


code:
class Board(tuple):
    def __init__(self, tup):
        if not isinstance(tup, tuple):
            raise TypeError("Constrcutor argument to Board should be a tuple")
        super().__init__()

print Board([1, 2])

rawDescription: Inheriting a tuple as a list

description: Inheriting a tuple as a list


code:
def hasHigherPrecedence(a, b):
    if (a=='*' or a=='/') and (b!='*' and b!='/'):
        return True
    return False

s = '++*+/+'
l = [3, 8, 9, 110, 26, 38, 1]

## computing postfix from infix
stk = []
postfix = []
for i in range(len(l)):
    postfix.append(l[i])
    if i < len(s):
        if len(stk) == 0 :
            stk.append(s[i])
            continue
        while len(stk):
            if hasHigherPrecedence(s[i],stk[-1]):
                stk.append(s[i])
                break
            else:
                postfix.append(stk[-1])
                stk.pop(-1)
        else:
            stk.append(s[i])
while len(stk):
    postfix.append(stk[-1])
    stk.pop(-1)

## evaluate the postfix expression
stk=[]
for x in postfix:
    r = None
    if x=='+':
        r = stk[-2]+stk[-1]
    elif x=='*':
        r = stk[-2]*stk[-1]
    elif x=='/':
        r = 1.0*stk[-2]/stk[-1]
    elif x=='-':
        r = stk[-2]-stk[-1]
    else:
        stk.append(x) ## x is a operand
    if r != None:
        stk.pop(-1)
        stk.pop(-1)
        stk.append(r)
print stk[0]

rawDescription: implementing strange calculator

description: Implementing strange calculator


code:
col_sum = np.where(np.sum(a, axis = 0)>0)
row_sum = np.where(np.sum(a, axis = 1)>0)
y1, y2 = row_sum[0][0], row_sum[0][-1]
x1, x2 = col_sum[0][0], col_sum[0][-1]
print x1, y1
print x2, y2

rawDescription: Python: Center array image

description: Python: Center array image


code:
def search(values, searchFor):
    for element, details in values.items():
        if element == searchFor:
           print element, details #As @PM 2Ring says, for Python 3x you should print (element, details)

rawDescription: search python dictionary not showing

description: Search python dictionary not showing


code:
from IPy import IP
if '192.168.0.1' in IP('192.168.0.0/30'):
    print "My IP is in the whitelist! Yay!"

rawDescription: How to determine if IP address in whitelist with IPy?

description: Determine if IP address in whitelist with IPy


code:
for b in membuf:
  print ord(b)

rawDescription: Pointer arithmetic in LLDB Python scripts


rawDescription: extract latitude and longitude from given input

description: Extract latitude and longitude from given input


code:
import numpy as np
import math
from matplotlib import pyplot as plt
from matplotlib import animation

#----- Numerical integration of ODE via fixed-step classical Runge-Kutta -----

def RK4Stream(odefunc,TimeSpan,uhat0,nt):
    h = float(TimeSpan[1]-TimeSpan[0])/nt
    print h
    w = uhat0
    t = TimeSpan[0]
    while True:
        w = RK4Step(odefunc, t, w, h)
        t = t+h
        yield t,w

def RK4Step(odefunc, t,w,h):
    k1 = odefunc(t,w)
    k2 = odefunc(t+0.5*h, w+0.5*k1*h)
    k3 = odefunc(t+0.5*h, w+0.5*k2*h)
    k4 = odefunc(t+h,     w+k3*h)
    return w + (k1+2*k2+2*k3+k4)*(h/6.)

#----- Constructing the grid and kernel functions -----
L   = 40
nx  = 512
x   = np.linspace(-L/2,L/2, nx+1)
x   = x[:nx]

kx1 = np.linspace(0,nx/2-1,nx/2)
kx2 = np.linspace(1,nx/2,  nx/2)
kx2 = -1*kx2[::-1]
kx  = (2.* np.pi/L)*np.concatenate((kx1,kx2))

def uhat2vhat(t,uhat):
    return np.exp( 0.5j * (kx**2) *t) * uhat

def vhat2uhat(t,vhat):
    return np.exp(- 0.5j * (kx**2) *t) * vhat

#----- Define RHS -----
def uhatprime(t, uhat):
    u = np.fft.ifft(uhat)
    return - 0.5j * (kx**2) * uhat + 1j * np.fft.fft((abs(u)**2) * u)

def vhatprime(t, vhat):
    u = np.fft.ifft(vhat2uhat(t,vhat))
    return  1j * uhat2vhat(t, np.fft.fft((abs(u)**2) * u) )

#------ Initial Conditions -----
u0      = 1./np.cosh(x) #+ 1./np.cosh(x+0.4*L)+1./np.cosh(x-0.4*L) #symmetric or remove jump at wrap-around
uhat0   = np.fft.fft(u0)

#------ Solving for ODE -----
t0 = 0; tf = 10.0;
TimeSpan = [t0, tf]
# nt       = 500 # limit case, barely stable, visible spurious bumps in phase
nt       = 1000 # boring  but stable. smaller step sizes give same picture
vhat0 = uhat2vhat(t0,uhat0)

fig = plt.figure()
ax1 = plt.subplot(211,ylim=(-0.1,2))
ax2 = plt.subplot(212,ylim=(-3.2,3.2))
line1, = ax1.plot(x,u0)
line2, = ax2.plot(x,u0*0)

vhatstream = RK4Stream(vhatprime,[t0,tf],vhat0,nt)

def animate(i):
    t,vhat = vhatstream.next()
    print t
    u = np.fft.ifft(vhat2uhat(t,vhat))
    line1.set_ydata(np.real(np.abs(u)))
    line2.set_ydata(np.real(np.angle(u)))
    return line1,line2

anim = animation.FuncAnimation(fig, animate, interval=15000/nt+10, blit=False)

plt.show()

rawDescription: Error in RK4 algorithm in Python

description: Error in RK4 algorithm in Python


code:
Users = win32net.NetGroupGetUsers(IP,'none',0)
print ', '.join(d['name'] for d in Users[0])

rawDescription: Removing characters from a tuple

description: Removing characters from a tuple


code:
from lxml import html
import requests

page = requests.get('http://web.international.ucla.edu/institute/events')
tree = html.fromstring(page.text)

event_titles = tree.xpath('//a[starts-with(@href,"/institute/event/")]/text()')

for event_title in event_titles:
    print "Event Title: ", event_title

rawDescription: Xpath Attribute REGEX

description: Xpath Attribute REGEX


code:
import pandas as pd

data = {'product_id': [23, 65, 66, 98, 998, 798],
        'category': ['cat1', 'cat2', 'cat1', 'cat1', 'cat1', 'cat2'],
        'number_of_purchase': [18,19,4,9,1,8]}

df = pd.DataFrame(data)
print df

rawDescription: Python, use "order by" inside a "group concat" with pandas DataFrame


code:
z = a,b
type(z)
z
len(z[0])
for ele in z:
   print ele

zip(a,b)  # using zip gives you what you want.

rawDescription: append list of values to sublists

description: Append list of values to sublists


code:
import re

def unescape(filename):
    return re.sub(r'(?<!\\)\\', '', filename)

print unescape(r'foo\ bar')
print unescape(r'foo\=bar')
print unescape(r'foo\\bar')

rawDescription: Unescaping filenames generated by ls -R

description: Unescaping filenames generated by ls -R


code:
wordlist = ['mississippi','miss','lake','que']

letters = set('aqk')

for word in wordlist:
    if letters & set(word):
        print word

rawDescription: Search strings in list containing specific letters in random order


code:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--range', default=[4,3,2], nargs=3, metavar=('start', 'end', 'step'),
             type=int, help='specify a range')
print parser.parse_args([])

# prints Namespace(range=[4, 3, 2])

rawDescription: argparse set default to multiple args

description: Argparse set default to multiple args


code:
import re
rex = re.compile('([^:]+\S)\s*:\s*(\d+)\s*\(\s*(\d+)/Sec\)')
for line in temp:
    match = rex.match(line)
    if match:
        print match.groups()

rawDescription: how to correctly split the following string ? - Python

description: Correctly split the following string ? - Python


code:
class RPC(object):
    DEPENDS = []
    REPLY_CLS = RPCReply
    def __init__(self, session, async=False, timeout=30, raise_mode=RaiseMode.NONE):
        self._session = session
        try:
            for cap in self.DEPENDS:
                self._assert(cap)
        except AttributeError:
            pass
        self._async = async
        self._timeout = timeout
        self._raise_mode = raise_mode
        self._id = uuid1().urn # Keeps things simple instead of having a class attr with running ID that has to be locked

rawDescription: How to override stuff in a package at runtime?

description: Override stuff in a package at runtime


code:
from HTMLParser import HTMLParser

data = """

Sam
John

"""

class Parser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.recording = False

    def handle_starttag(self, tag, attrs):
        if tag == 'p':
            self.recording = True

    def handle_endtag(self, tag):
        if tag == 'p':
            self.recording = False

    def handle_data(self, data):
        if self.recording:
            print data

parser = Parser()
parser.feed(data)

rawDescription: Extract data using HTMLParser

description: Extract data using HTMLParser


code:
def repeat_value(grp):
    grp['DAY2'] = grp['DAY'].min() + pd.Timedelta('7 days')
    return grp
df = df.groupby(['GROUP']).apply(repeat_value)
print df

rawDescription: Pandas apply to multiple rows with missing dates


code:
import pandas as pd
f=open('myData.txt','r')
df = pd.DataFrame(pd.read_csv(f, sep='\t', header=None, names=['val1', 'val2', 'val3']))
print df
df.loc[:,"val1":"val3"] = df.loc[:,"val1":"val3"].div(df.sum(axis=0), axis=1)
print df
df.to_csv('output.txt', header=None,sep='\t', encoding='utf-8')

rawDescription: Parsing a tab seperated file using python


code:
soup = BeautifulSoup.BeautifulSoup(xml)
for section in soup.findAll("section"):
    for post in section.findAll('a', attrs={'class':['package-link']}):
        print post

rawDescription: Beautiful soup findAll didn't find all of them


code:
import matplotlib.pyplot as plt
import random
from shapely.geometry import Polygon, MultiPolygon
from shapely.ops import cascaded_union, polygonize
from descartes import PolygonPatch

def make_square(x, y, size1, size2):
    dx = [size1, -size1, -size1, size1, size1]
    dy = [size2, size2, -size2, -size2, size2]
    return [(x+sx, y+sy) for sx, sy in zip(dx, dy)]

fig, ax = plt.subplots()

bigsquare = make_square(0.5, 0.5, 1.0, 1.0)
a, b = zip(*bigsquare)
ax.plot(a, b, 'r-')
polygons = []

for i in xrange(10):
    x = random.random()
    y = random.random()
    s1 = random.random()
    s2 = random.random()

    square = make_square(x, y, s1, s2)
    polygons.append(Polygon(square))
    a, b = zip(*square)
    ax.plot(a, b, 'k-')

union = cascaded_union(polygons)
patch2b = PolygonPatch(union, fc='#00ff00', ec='#00ff00', alpha=0.5, zorder=2)
ax.add_patch(patch2b)

#-- Get the region not covered by individual squares.
uncovered_region = Polygon(bigsquare).difference(union)

# In some cases, the result will be a single polygon...
if not isinstance(uncovered_region, MultiPolygon):
    uncovered_region = [uncovered_region]

for poly in polygonize(uncovered_region):
    print poly
    patch = PolygonPatch(poly, fc='purple', alpha=0.5, zorder=2)
    ax.add_patch(patch)

plt.margins(0.05)
plt.show()

rawDescription: Find holes in a union of rectangles?

description: Find holes in a union of rectangles


code:
# test.py
import re, sys

s = re.sub(r'\\', r'/', sys.argv[1])
print s

rawDescription: To replace all backslashes with forward slashes using sed or python


code:
class MyList(object):
    def __init__(self):
        self.list = [42, 3.1415, "Hello World!"]
    def __iter__(self):
        return iter(self.list)

m = MyList()
for x in m:
    print x

rawDescription: What makes something iterable in python

description: What makes something iterable in python


code: print Model.objects.get(pk=1).ranking

rawDescription: How do I get the position of a result in the list after an order_by?

description: Get the position of a result in the list after an order_by


code:
from bar import bar
from foo import foo

print bar()

print foo(3)

rawDescription: Delete modules to clean namespace?

description: Delete modules to clean namespace


code:
import itertools

for digs in itertools.product(range(8), repeat=8):
    counts = [0] * 8
    for d in digs:
        counts[d] += 1
    if counts == list(digs):
        print digs

rawDescription: Optimized way for finding the number which satisfies specific conditions


code:
import nmap

nm = nmap.PortScannerAsync()
def callback_result(host, scan_result):
    print '------------------'
    print host, scan_result

nm.scan('192.168.1.0/24', arguments="-O -v", callback=callback_result)
while nm.still_scanning():
    print("Waiting >>>")
    nm.wait(2)

rawDescription: Python - nmap scan without looking for specific ports


code:
from collections import deque
def survivor(names, step):
    circle = deque(names)
    while len(circle) > 1:
        circle.rotate(1-step)
        print circle.popleft()
    return circle[0]

rawDescription: deleting every nth element from a list in python 2.7

description: Deleting every nth element from a list in python 2.7


code:
print 'artistName:Axwell /\\ Ingrosso'
print 'artistName:Axwell /\\\\ Ingrosso'
print r'artistName:Axwell /\\ Ingrosso'

rawDescription: Escaping backslash in Python requests

description: Escaping backslash in Python requests


code:
import sys, subprocess, multiprocessing.pool

nb_threads = int(sys.argv[1])
pool = multiprocessing.pool.ThreadPool(nb_threads)

processes = []
for i in range(nb_threads):
    processes.append(lambda i=i: subprocess.check_output((["python", "mic.py", "mic" + str(i)])))

outputs = pool.map(lambda x: x(), processes)

for o in outputs:
    print o

rawDescription: Recover output of 2 python scripts with subprocess


code:
my_list = [1]  # shortened for the sake of example
for e in my_list:
    print 'BEFORE id(e)', id(e)
    e = e*2
    print 'AFTER id(e)', id(e)

rawDescription: python behavior of list modification in for loop

description: Python behavior of list modification in for loop


code:
from collections import Counter

results = Counter()
with open('input.txt', 'r') as f:
    for line in f:
        if line.startswith('-'):
            continue

        results.update(line)

print results

rawDescription: Disregarding a line while calculating frequency in python


code:
# Flip the dict
old_reverse = {v: k for k, v in old.items()}

# Map new VS old
data = []
for k, v in new.iteritems():
    try:
        old_k = old_reverse[v]
        data.append((old_k, v, k, v,))
    except KeyError:
        data.append((None, None, k, v,))

# Print them sorted
for old_k, old_v, k, v in sorted(data, key=lambda d: d[0]):
    print "%s\t%s\t%s\t%s" % (
        old_k if old_k is not None else "[]",
        repr(old_v) if old_k is not None else "[]",
        k,
        repr(v),
    )

rawDescription: Python: find differences in two dicts

description: Python: find differences in two dicts


code: print [list(item) for item in totals.items()]

rawDescription: Algorithm in Python To Solve This Problem


code:
if weapon == True:  # weapon isn't equal to True
    print "True"

if weapon:
    print "True"

rawDescription: Why is a list containing objects False when value tested?


code:
class base1(object):
    def name(self, value):
        print "base1", value
        super(base1, self).name(value)

    def to_python(self, value):
        value = value + " base 1 "
        if(hasattr(super(base1, self), "to_python")):
            value = super(base1, self).to_python(value)
        return value

class base2(object):
    def name(self, value):
        print "base2", value

    def to_python(self, value):
        value = value + " base 2 "
        if(hasattr(super(base2, self), "to_python")):
            value = super(base2, self).to_python(value)
        return value

class superClass(base1, base2):
    def name(self, value):
        super(superClass, self).name(value)
        print "super Class", value

rawDescription: Django custom field with multiple inheritance


code:
a = np.genfromtxt('meanvals2.txt').T[1]
b = np.std(a)
c = np.mean(a)

#step 1, values lower than 1 std from mean
ok = a > (c - b)
a[~ok] = c - b

#step 2, values higher than 1 std from mean
ok = a < (c + b)
a[~ok] = c + b

print a

rawDescription: changing the values of a list if they are more than a certain value

description: Changing the values of a list if they are more than a certain value


code: print repr(value)

rawDescription: Python "is not None" returning None

description: Python "is not None" returning None