code:
import re

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

my_file_m= '''TTCCATTCTCTACCCAGCCCCCACTCTGACCCCTTTACTCTGACCCCTTTATTGTCTACTCCTCAGAGCCCCCAGTCTGTATCCTTCTAACTTAGAAAGGGGATTATGGCTCAGGGTCCAACTCTGTGCTCAGAGCTTTCAACAACTACTCAGAAACACAAGATGCTGGGACAGTGACCTGGACTGTGGGCCTCTCATGCACCACCATCAAGGACTCAAATGGGCTTTCCGAATTCACTGGAGCCTCGAATGTCCATTCCTGAGTTCTGCAAAGGGAGAGTGGTCAGGTTGCCTCTGTCTCAGAATGAGGCTGGATAAGAT'''

pat = re.compile(r'(?=(TAA|AAT|TGA|TAG))') # Very important, if you do not need overlaps then remove '?='
matches = re.finditer(pat,my_file_m)
result1 = [int(match.start(1)) for match in matches] # find all the starting positions of the string
result2 = [range(x,x+3) for x in result1 ] # find all the positions of the characters (given that we search for patterns of length 3, can be modified for other lengths too )
result3 = set().union(*result2) # generate a union

for chari in range(len(my_file_m)): # colorize based on if it is in a sequence or not
    if(chari in result3):
        print bcolors.OKGREEN + my_file_m[chari]  + bcolors.ENDC,
    else:
        print my_file_m[chari],

rawDescription: how to find a three letter in a sequence?

description: Find a three letter in a sequence


code:
bills = [] # initialize a list
while True: # loop
    bill = raw_input('Enter the value for the seat [\'q\' to quit]: ' ) # get input
    try: # try to...
      bills.append(float(bill)) # convert this input to a number and add it to the list
    except ValueError: # if it can't be converted because it's "q" or empty,
        break # stop asking for input

result = 0 # initialize a zero
for bill in bills: # go through each item in bills
  result += bill # add it to the result

print result

rawDescription: Addition of raw_input numbers in python

description: Addition of raw_input numbers in python


code:
import re

def add_photos(s,n):
    def helper(m):
        num = int(m.group(1)) + n
        plural = '' if num == 1 else 's'
        return 'added %d new photo%s' % (num,plural)
    return re.sub(r'added (\d+) new photo(s?)',helper,s)

s = "martin added 0 new photos to the martins photos album."
s = add_photos(s,1)
print s
s = add_photos(s,5)
print s
s = add_photos(s,7)
print s

rawDescription: How to replace digits in string?

description: Replace digits in string


code:
import pandas as pd
df = pd.DataFrame({'a': [1.5, 2.5], 'b': [0.25, 2.75], 'c': [1.25, 0.75]})

print "The data frame"
print df
print "The mean value"
print df.mean()
print "The value after subraction of mean"
print df -df.mean()

rawDescription: Subtract every column in dataframe with the mean of the that column python


code:
handle=open('file','r+')
var=handle.read()
print var

rawDescription: How to read and write a file using python?

description: Read and write a file using python


code:
print out.Sentence.Name
print out.Sentence.Prefix

rawDescription: Getting a data structure equivalent to asXML() in pyparsing?


code:
def updateplot(self):
    print "Update"
    data1 = self.amplitude*np.sin(np.linspace(0,30,121)+self.t)
    self.plotcurve.setData(data1)

rawDescription: How to update a realtime plot and use buttons to interact in pyqtgraph?

description: Update a realtime plot and use buttons to interact in pyqtgraph


code:
for k, g in groupby(lis, key=lambda x:x[0].partition('.')[0]):
    print '\n'.join(x[0] for x in g) + '\n'

rawDescription: Selecting some elements in python lists

description: Selecting some elements in python lists


code:
with open('myfile') as f:
    for line in f:
        print line,

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

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


code:
output = []
output_filter = set()
for y in input:
    if (y, type(y)) not in output_filter:
        output_filter.add((y, type(y)))
        output.append(y)
print output

rawDescription: How to distinguish same int and float elements?

description: Distinguish same int and float elements


code:
#!/usr/bin/python

import alsaaudio as aa
import audioop
import Tkinter as tk
import tkFont

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

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

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

        # using a Tkinter variable to display different messages, on the same widget
        self.messageVar = tk.StringVar()
        self.message = tk.Message(self.parent, textvar=self.messageVar, bg="#000000", font=self.parent.displayFont, fg="#777777", justify="c")
        self.message.place(relx=.5, rely=.5, anchor="c")

    def printMessage(self, messageString):
        self.messageVar.set(messageString)

def update_ui_from_audio(data_in, window):
    l, data = data_in.read()
    if l:
        # catch frame error
        try:
            max_vol=audioop.rms(data,2)
            scaled_vol = max_vol//4680
            print scaled_vol
            window.printMessage(scaled_vol)
        except audioop.error, e:
            if e.message !="not a whole number of frames":
                raise e

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

    # Set up audio
    data_in = aa.PCM(aa.PCM_CAPTURE, aa.PCM_NONBLOCK, 'hw:1')
    data_in.setchannels(2)
    data_in.setrate(44100)
    data_in.setformat(aa.PCM_FORMAT_S16_LE)
    data_in.setperiodsize(256)

    def audio_read_loop():
        update_ui_from_audio(data_in, window)
        root.after(1, audio_read_loop)

    root.after(1, audio_read_loop)
    root.mainloop()

if __name__ == '__main__':
    main()

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


code:
from collections import Counter
myDict = {'a':'ATGTTCN','b':'ATTTCCG'}
result = [(i,Counter(site)) for i,site in enumerate(zip(*myDict.values()))]
result = [x for x in result if x[1].most_common()[0][1] < 2]
for site,count in result:
    print 'Site {}: {}'.format(site,str(count)[9:-2])

rawDescription: Changing the output of counter in python


code:
from Crypto.Cipher import AES

cipher = 'A14E71A6F4E8D8E465C3C302C2A96BD2'
aes = AES.new(key, AES.MODE_CBC, iv)
print aes.decrypt(cipher.decode('hex'))

rawDescription: Decrypt hex string with python

description: Decrypt hex string with python


code:
#!/usr/bin/env python
from suds.xsd.doctor import Import, ImportDoctor
from suds.client import Client

# enable logging to see transmitted XML
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)

# fix broken wsdl
# add <s:import namespace="http://www.w3.org/2001/XMLSchema"/> to the wsdl
imp = Import('http://www.w3.org/2001/XMLSchema',
             location='http://www.w3.org/2001/XMLSchema.xsd')
imp.filter.add('http://tempuri.org/')
wsdl_url = 'http://204.9.76.243/nuCast.DataFeedService/Service1.asmx?WSDL'
client = Client(wsdl_url, doctor=ImportDoctor(imp))

# make request
arrayofstring = client.factory.create('ArrayOfString')
arrayofstring.string = [1,2]
print client.service.CalculateWeb1D(1073757, arrayofstring, 99).string

rawDescription: Soap call in Python

description: Soap call in Python


code:
from bs4 import BeautifulSoup
import requests

for page in range(1, 100):
   r  = requests.get("http://sourceforge.net/directory/?page=" + str(page))
   data = r.text
   soup = BeautifulSoup(data, "html.parser")

   print soup.find_all('span',{'itemprop': 'name'})

rawDescription: Python Scraping URL issue

description: Python Scraping URL issue


code:
def print_nonrec_breathfirst(node):
    queue = [node]
    while queue:
        node, queue = queue[0], queue[1:]
        print node
        for c in node.children:
            queue.append(c)

rawDescription: Print tree without recursion

description: Print tree without recursion


rawDescription: Iterating over dictionary returns keys in sorted order


code:
print AantalCores
print CPUSSpecNumber
print Snelheid

rawDescription: Python Multiple variables has one Value

description: Python Multiple variables has one Value


code:
import threading
import random
import time
import Queue

def work(worker_queue, id, stop_event):
    while not stop_event.is_set():
        print "This is worker", id

        # do stuff
        time.sleep(random.random() * 5)

        # put worker ID in queue
        if not stop_event.is_set():
            worker_queue.put(id)

        break

# queue for workers
worker_queue = Queue.Queue()

# indicator for other threads to stop
stop_event = threading.Event()

# run workers
threads = []
threads.append(threading.Thread(target=work, args=(worker_queue, 0, stop_event)))
threads.append(threading.Thread(target=work, args=(worker_queue, 1, stop_event)))
threads.append(threading.Thread(target=work, args=(worker_queue, 2, stop_event)))

for thread in threads:
    thread.start()

# this will block until the first element is in the queue
first_finished = worker_queue.get()

print first_finished, 'was first!'

# signal the rest to stop working
stop_event.set()

rawDescription: python will different threads end at the same time as the first one to finish

description: Python will different threads end at the same time as the first one to finish


code:
from scipy.stats import t

def StudentTCI(loc, scale, df, alpha=0.95):
    return t.interval(alpha, df, loc, scale)

print StudentTCI(1, 2, 10)
print StudentTCI(1, 2, 10, 0.99)

rawDescription: student t confidence interval in python

description: Student t confidence interval in python


code:
for product in products:
    print product.xpath('//tr[contains(td[1], "Merk")]/td[2]/a/text()').extract()

rawDescription: Scrapy scraping content of td with multiple td's of same class


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

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


code:
text = '''To get to the destination follow this direction
1. First turn left and then
text text text
2. Secondly you have to some text here
some text here

For the second instruction follow the text below:
«1. some text some text some text
text text text.
2. some text some text text text text.
3. some text some text text text text.»'''

print re.findall(ur"\d+",re.findall(ur"«([\s\S]*?)»",text)[0])

rawDescription: find all digits between a character in python

description: Find all digits between a character in python


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

rawDescription: Python - Tweepy, retrieving the created_at


code:
class Foo(object):

    def __new__(*args):
        #ignore the args
        return '__call__'

    @staticmethod
    def bar():
        return 'bar'

print Foo()
#__call__

rawDescription: decorate __call__ with @staticmethod

description: Decorate __call__ with @staticmethod


rawDescription: My Maxent Classifier works fine with gis algorithm but does not work with iis algorithm. It is not throwing any error, just some warnings


code:
import grequests as grq
from bs4 import BeautifulSoup as bsoup

def get_title(response):

    soup = bsoup(response.content)
    title = soup.find_all("title")[0].get_text()
    print title.split(" - ")[0]

def main():

    urls = [
    "http://www.wikiart.org/en/henri-rousseau/tiger-in-a-tropical-storm-surprised-1891",
    "http://www.wikiart.org/en/edgar-degas/the-green-dancer-1879",
    "http://www.wikiart.org/en/claude-monet/dandelions",
    "http://www.wikiart.org/en/albrecht-durer/the-little-owl-1506",
    "http://www.wikiart.org/en/gustav-klimt/farmhouse-with-birch-trees-1903",
    "http://www.wikiart.org/en/jean-michel-basquiat/boxer",
    "http://www.wikiart.org/en/fernand-leger/three-women-1921",
    "http://www.wikiart.org/en/alphonse-mucha/flower-1897",
    "http://www.wikiart.org/en/alphonse-mucha/ruby",
    "http://www.wikiart.org/en/georges-braque/musical-instruments-1908",
    "http://www.wikiart.org/en/rene-magritte/the-evening-gown-1954",
    "http://www.wikiart.org/en/m-c-escher/lizard-1",
    "http://www.wikiart.org/en/johannes-vermeer/the-girl-with-a-pearl-earring"
    ]

    rs = (grq.get(u) for u in urls)
    for i in grq.map(rs):
        get_title(i)

if __name__ == "__main__":
    main()

rawDescription: Why am I getting an httplib2.RedirectLimit error?


code:
# ...skipped...

t = Table(
    't', metadata,
    Column('id', Integer, primary_key=True),
    Column('state1', String(10)),
    Column('state2', String(10)),
)

# ...skipped...

obj1 = Model()
obj1.state1 = 'value1-1'
obj1.state2 = 'value2-1'
session.add(obj1)
session.commit()
obj_id = obj1.id
session.expunge_all()

obj2 = Model()
obj2.id = obj_id
obj2.state1 = 'value1-2'
obj3 = session.merge(obj2)
session.commit()
print obj3 is obj1, obj3 is obj2
print obj3.state1, obj3.state2

rawDescription: Can SQLAlchemy's session.merge() update its result with newer data from the database?


code:
for line in log_lines:
    if(line.__contains__(alert)):
        partsOfLine = str(line).split()
        dateParts = str(partsOfLine[0]).split("-")
        timeParts = str(partsOfLine[1]).split(":")
        alerttime = datetime.datetime(int(dateParts[0]),int(dateParts[1]),int(dateParts[2]),int(timeParts[0]),int(timeParts[1]),int(timeParts[2]))
        timediff_in_min = (alerttime - datetime.now()).total_seconds()/60
        if(timediff_in_min >= 120):
            print "ignoring older than 2 hours"
        else:
            print "new alert in the last 2 hours"

rawDescription: Python Logging: Evaluate Time before adding a new entry


code:
import csv
from itertools import izip_longest
from os import remove,rename

# Creating a CSV file
with open(r'Data.csv','wb') as f:
    fw = csv.writer(f)
    fw.writerows( (('Heading 1', 'Heading 2'),
                   ('1'),
                   ('1'),
                   ('0'),
                   ('2'),
                   ('1'),
                   ('0'))   )

print "The CSV file at start, read by a csv.reader :\n"
with open(r'Data.csv','rb') as f:
    print '\n'.join(map(repr,csv.reader(f)))

#------------------------------------------
newdata = [10,0,10,20,10,20,10,10]

with open(r'Data.csv','rb') as f, open(r'newData.csv','wb') as g:
    fr = csv.reader(f)
    gw = csv.writer(g)
    gw.writerow(fr.next())
    gw.writerows( a+[b] for a,b in izip_longest(fr, newdata,
                                                fillvalue=[0]) )
remove (r'Data.csv')
rename (r'newData.csv',r'Data.csv')

#------------------------------------------

print "The new CSV file created, read by a csv.reader :\n"
with open(r'Data.csv','rb') as f:
    print '\n'.join(map(repr,csv.reader(f)))

rawDescription: Python inserting list of integers into CSV column after other data


code:
import sys
f = open("test.out", 'w')
sys.stdout = f

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

f.close()

rawDescription: How to write output of terminal to file

description: Write output of terminal to file


code: print numpy.random.randn(3,4)

rawDescription: Probability based randomzation of 2d Numpy array


code:
import pickle

d = {'WILLIAMS GABRIELLE': [10, 6, 2], 'JONES CHLOE': [7, 5, 5],\
     'MILLER NOEL': [1, 8, 6], 'CLARK BRANDON': [8, 6, 1],\
     'NATHAN WILSON': [7, 9, 6], 'SMITH ISABELLE': [2, 9, 6],\
     'WHITE SAM': [1, 9, 5], 'BROWN ADAM': [8, 10, 6], 'PATEL SAPNA': [7, 8, 7],\
     'DAVIS RYAN': [10, 6, 9]}

with open("Class6A.txt", "wb") as fp:
    pickle.dump(d, fp)

import pickle
import collections

def getAverage(data):
    """ Create Average of student data """
    records = dict()
    for i, j in data.iteritems():
        records[i] = (j, float(sum(j))/len(j))

    return records

def startover():
    #. Tells the user what number corresponds with what class.
    print('1 = classA, 2 = classB, 3 = classC')
    #. accepts an input for what class the user would like to view.
    while 1:
        try:
            class_no = int(raw_input('Choose the class you would like to view.'))
            if class_no not in [1,2,3]:
                print "Enter vlaid Class Number."
                continue
            break
        except ValueError:
            print "Enter valid Class Numner i.e.digit"
            continue

    #. If class a is selected, open class a.
    if class_no==1:
        file_name = 'Class6A.txt'
        #If class b is selected, open class b.
    elif class_no==2:
        file_name = 'Class6B.txt'
        #If class c is selected, open class c.
    elif class_no==3:
        file_name = 'Class6C.txt'

    while 1:
        try:
            sorttype = int(raw_input('How do you want to sort this data? 1=alphabetical\
             2=High to low by highest number 3=High to low by average number.'))
            if sorttype not in [1,2,3]:
                print "Enter vlaid option from the list.."
                continue
            break
        except ValueError:
            print "Enter valid option Number i.e. digit"
            continue

#    fp = open(file_name, 'rb')
#    data = pickle.load(fp)
#    fp.close()

    with open(file_name, "rb") as fp:
        data = pickle.load(fp)

    #- Add Average value in data.
    records = getAverage(data)

    if sorttype==1:
        student_names = records.keys()
        student_names = sorted(student_names)
    elif sorttype==3:
        student_names = []
        tmp = collections.defaultdict(list)
        for i, j in records.iteritems():
            tmp[j[1]].append(i)
        tmp1 = tmp.keys()
        tmp1 = sorted(tmp1)
        tmp1.reverse()
        for i in tmp1:
            student_names.extend(tmp[i])
    elif sorttype==2:
        student_names = []
        print "Try your self."

    print "Result:"
    print "Name\t Marks\t Average"
    for i in student_names:
        print "%s\t%s\t%s"%(i, records[i][0], records[i][1])

startover()

rawDescription: How do I sort scores and names saved in a .txt file into alphabetical order with python?

description: Sort scores and names saved in a .txt file into alphabetical order with python


code:
import urllib2
from bs4 import BeautifulSoup

soup = BeautifulSoup(urllib2.urlopen('http://www.asanet.org/funding/funding_and_grants.cfm'))

for heading in soup.select('div#memberscontent p.heading2'):
    deadline = heading.find_next_sibling('p')
    article = deadline.find_next_sibling('p')

    print heading.get_text(strip=True)
    print deadline.get_text(strip=True)
    print article.get_text(strip=True)
    print "----"

rawDescription: Web-scraping: I only get 1/10 of the text I want (using BeautifulSoup)


code:
list = ['LnNlbHBtYXhlIGVzdSBvdCBlZXJmIGxlZUYgLnNldGlzYmV3IGNpZmZhcnQgaGdpaCBubyBub2l0YXNpbGFtcm9uZWQgZm8gdHBlY25vYyBlaHQgZWJpcmNzZUQgLjQ=', 'ZWxpZiBlbm8gbmkgZWIgdHN1TSApaXYgICAg', 'c25vaXRhY2lmaWNlcHMgOC1QRVAgdGVlbSB0c3VNICl2ICAgIA==', 'Ni4yIG5vaHR5UCBodGl3IGtyb3cgdHN1TSApdmkgICAg', 'c2VsdWRvbSByZWh0byB5YiBlbGJhdHJvcG1pIGViIGRsdW9ocyBzc2FsQyApaWlpICAgIA==']
print "\n".join(s.decode('base64')[::-1] for s in list)

rawDescription: Decoding reversed mulitline string encoded in base64 format automatically


code:
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer(min_df=2)
corpus = [
    'This is the first document.',
    'This is the second second document.',
    'And the third one.',
    'Is this the first document?',
]
vectorizer = vectorizer.fit(corpus)
print vectorizer.vocabulary_
#prints {u'this': 4, u'is': 2, u'the': 3, u'document': 0, u'first': 1}
X = vectorizer.transform(corpus)

rawDescription: Equivalent of R's removeSparseTerms in Python


code:
import operator

def kenobi(l):
    ops = {'+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.div}

    for i in xrange(1,len(l)):
        if isinstance(l[i],list):
            l[i] = kenobi(l[i])

    return ops[l[0]](l[1],l[2])

print kenobi(['+',['-',['*',['*',5,5],['+',3,5]],8],2])

194

rawDescription: how to get the modified input back in a prefix notation

description: Get the modified input back in a prefix notation


code:
import os

root, dirs, files = os.walk("/path/to/your/dir").next()
print dirs

rawDescription: Python folder names in the directory

description: Python folder names in the directory


code: rdd.map(lambda (k, v): (tuple(k), v)).groupByKey()

rawDescription: A list as a key for PySpark's reduceByKey


code:
a = [x for x in "abyffgh fg99"]
b = [x for x in "999aby99ff9h9"]
c = a[:]

s = SequenceMatcher(None, a, b)

i = 0
for tag, i1, i2, j1, j2 in s.get_opcodes():
    print ("%7s a[%d:%d] (%s) b[%d:%d] (%s) c[%d:%d] (%s)" %
    (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2], i1, i2, c[i1 + i:i2 + i]))
    if tag == "delete":
        del c[i1 + i:i2 + i]
        i -= i2 - i1
    elif tag == "replace":
        c[i1 + i:i2 + i] = b[j1:j2]
        i -= i2 - i1 - j2 + j1
    elif tag == "insert":
        c[i1 + i:i2 + i] = b[j1:j2]
        i += j2 - j1
    print c
    print i
print c
print b
print c == b
a == b

rawDescription: python diff SequenceMatcher - patching a list

description: Python diff SequenceMatcher - patching a list


code:
import markdown as m

print "Markdown version: ", m.version
file = "file.md"
md = m.Markdown(extensions=['meta']) # doesn't complain

print "Registered extensions: ", md.registeredExtensions
print "Preprocessors:", md.preprocessors.keys()
text = open(file).read()
try:
    print md.convert(text)
except AttributeError as a:
    print "Error: ", a
print "Meta: ", md.Meta

rawDescription: 'meta' extension for Markdown not loading in Python2.6?


code:
a = np.arange(16).reshape((8,2))
b = [2, 6]
print np.split(a, b, axis=0) # plus some extra formatting

rawDescription: How do I split an ndarray based on array of indexes?

description: Split an ndarray based on array of indexes


code:
from difflib import SequenceMatcher

def get_best_match(query, corpus, step=4, flex=3, case_sensitive=False, verbose=False):
    """Return best matching substring of corpus.

    Parameters
    ----------
    query : str
    corpus : str
    step : int
        Step size of first match-value scan through corpus. Can be thought of
        as a sort of "scan resolution". Should not exceed length of query.
    flex : int
        Max. left/right substring position adjustment value. Should not
        exceed length of query / 2.

    Outputs
    -------
    output0 : str
        Best matching substring.
    output1 : float
        Match ratio of best matching substring. 1 is perfect match.
    """

    def _match(a, b):
        """Compact alias for SequenceMatcher."""
        return SequenceMatcher(None, a, b).ratio()

    def scan_corpus(step):
        """Return list of match values from corpus-wide scan."""
        match_values = []

        m = 0
        while m + qlen - step <= len(corpus):
            match_values.append(_match(query, corpus[m : m-1+qlen]))
            if verbose:
                print query, "-", corpus[m: m + qlen], _match(query, corpus[m: m + qlen])
            m += step

        return match_values

    def index_max(v):
        """Return index of max value."""
        return max(xrange(len(v)), key=v.__getitem__)

    def adjust_left_right_positions():
        """Return left/right positions for best string match."""
        # bp_* is synonym for 'Best Position Left/Right' and are adjusted
        # to optimize bmv_*
        p_l, bp_l = [pos] * 2
        p_r, bp_r = [pos + qlen] * 2

        # bmv_* are declared here in case they are untouched in optimization
        bmv_l = match_values[p_l / step]
        bmv_r = match_values[p_l / step]

        for f in range(flex):
            ll = _match(query, corpus[p_l - f: p_r])
            if ll > bmv_l:
                bmv_l = ll
                bp_l = p_l - f

            lr = _match(query, corpus[p_l + f: p_r])
            if lr > bmv_l:
                bmv_l = lr
                bp_l = p_l + f

            rl = _match(query, corpus[p_l: p_r - f])
            if rl > bmv_r:
                bmv_r = rl
                bp_r = p_r - f

            rr = _match(query, corpus[p_l: p_r + f])
            if rr > bmv_r:
                bmv_r = rr
                bp_r = p_r + f

            if verbose:
                print "\n" + str(f)
                print "ll: -- value: %f -- snippet: %s" % (ll, corpus[p_l - f: p_r])
                print "lr: -- value: %f -- snippet: %s" % (lr, corpus[p_l + f: p_r])
                print "rl: -- value: %f -- snippet: %s" % (rl, corpus[p_l: p_r - f])
                print "rr: -- value: %f -- snippet: %s" % (rl, corpus[p_l: p_r + f])

        return bp_l, bp_r, _match(query, corpus[bp_l : bp_r])

    if not case_sensitive:
        query = query.lower()
        corpus = corpus.lower()

    qlen = len(query)

    if flex >= qlen/2:
        print "Warning: flex exceeds length of query / 2. Setting to default."
        flex = 3

    match_values = scan_corpus(step)
    pos = index_max(match_values) * step

    pos_left, pos_right, match_value = adjust_left_right_positions()

    return corpus[pos_left: pos_right].strip(), match_value

rawDescription: Find best substring match

description: Find best substring match


code:
from collections import OrderedDict
d = OrderedDict()

for num in range(5):
    d[num] = num
print d #OrderedDict([(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)])

rawDescription: How does the dictionary add keys in python?


code:
from xml.dom import minidom
xml = '''\
<root>
   <tag_type>
      Foo
      Bar
   </tag_type>
</root>
'''
dom = minidom.parseString(xml)
for tag_type in dom.getElementsByTagName('tag_type'):
    while tag_type.hasChildNodes():
        tag_type.removeChild(tag_type.firstChild)
    tag_type.appendChild(dom.createTextNode("Replacement Word"))

print dom.toxml()

rawDescription: Find replace Element using Python

description: Find replace Element using Python


code:
import random
l = []
for i in range (200000):
    l.append (random.random())
print len(set(l))

rawDescription: Ensuring Unique Fields when Randomly Generating MySQL rows


code: print '{}{}'.format(counter, "   ".join(row))

rawDescription: print edited list without actually editing the list

description: Print edited list without actually editing the list


code:
df['E']= df['B'].apply(lambda x: 1 if x in genValues(0,20) else 2 if x in genValues(21,40) else 3 if x in genValues(41,100) else x)

print df

rawDescription: Pandas replace issue

description: Pandas replace issue


code: print '{}{}'.format(item.strip(), i)

rawDescription: String concatenation acting crazy

description: String concatenation acting crazy


code:
time = 1
print time/time  #Output is 1

rawDescription: Python Variable Division

description: Python Variable Division


code:
if child.attrib.get("name") in attributes:
    for subchild in child:
        if subchild.tag == 'value':
            print subchild.text

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

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


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

lst = list()

my_op = my_op_class()
my_op.param = 2
lst.append(my_op)

my_op = my_op_class()
my_op.param = 3
lst.append(my_op)

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

rawDescription: Add different object states to a list

description: Add different object states to a list


code:
import codecs
f = codecs.open('unicode.rst', encoding='utf-8')
for line in f:
    print repr(line)

rawDescription: Character reading from file in Python

description: Character reading from file in Python


rawDescription: Python pandas give comma separated values new column


code:
profile = gmail_service.users().getProfile(userId='me').execute()

print 'Email address of user is: %s' % profile['emailAddress']

rawDescription: What is the best way to use the Google API to get just a user's email?


code:
import sys
print '\n'.join(sys.path)

rawDescription: python and bpython using different PYTHONPATHs in Virtualenv

description: Python and bpython using different PYTHONPATHs in Virtualenv


code:
for row_index, row in enumerate(depth):
   for col_index, v in enumerate(row):
      print 'depth[%d][%d]=%s' % (row_index, col_index, v)

rawDescription: Iterating within bounds of a 2D list (matrix) in python


code:
for price in soup.select("div.product-price > div:nth-of-type(1)"):
    print price.find(text=True, recursive=False).strip()

rawDescription: BeautifulSoup: how to get children of div tab


code:
index = {}
for word, s_word in words:
    index[s_word] = index.get(s_word, []) + [word]

for similar_words in index.values():
    print ' '.join(similar_words)

rawDescription: How to think in Python after working in C++?

description: Think in Python after working in C++


rawDescription: How to use regex with optional characters in python?

description: Use regex with optional characters in python


code:
import subprocess
import os

def main():
    pid = subprocess.Popen(['nohup', '/path/to/main_p.py'],
                            stdout = open(os.devnull, 'w+', 0),
                            stderr = subprocess.STDOUT
                            ).pid
    print pid

if __name__ == "__main__":

    main()

rawDescription: Python wrapper script to let a program be executed remotely with no blocking


code:
old_import = __import__
def my_import(module,*args,**kwargs):
  print module, 'loaded'
  return old_import(module,*args,**kwargs)
__builtins__.__import__ = my_import
import datetime
import datetime
import django

rawDescription: Do something every time a module is imported


code:
class base:
    context = None # if you want to be able to create without context.
    # just omit the previous line if you want an error
    # when you haven't set a context and you try to instantiate a subclass

class a(base):
    def __init__(self, aa):
        self.aa = aa
        self.context = self.context # sets an instance variable
        # so the class variable can be changed

class b(base):
    def __init__(self, bb):
        self.bb = bb
        self.context = self.context

base.context = 'context'

A = a(1)
B = b(2)

base.context = 'newcontext'

print A.context # context

rawDescription: How to share common context between classes?

description: Share common context between classes


code:
if "\n" in foo:
    print "There's a newline in variable foo"

rawDescription: How can I check for a new line in string in Python 3.x?

description: Check for a new line in string in Python 3.x


code:
arr = [10, 7, 15, 12, 3, 21]

main_stack = []

def get_min():
    if len(main_stack) == 0:
        return None
    return main_stack[-1] >> 32

def push(key):
    current_min = get_min()
    if current_min:
        if key < current_min:
            current_min = key
    else:
        current_min = key
    main_stack.append(key + (current_min << 32))

def pop():
    key = main_stack.pop()
    return key & 0xFFFFFFFF

for key in arr:
    push(key)

def print_state():
    print(", ".join(str(x & 0xFFFFFFFF) for x in main_stack))
    print("min: %d" %(get_min(),))

for _ in arr:
    print_state()
    print "popped:", pop()

rawDescription: Finding minumum element in a stack in constant time


rawDescription: numpy: get rid of loop

description: Numpy: get rid of loop


code:
def f(*args, **kw): print args, kw

f(**{1: 2})

rawDescription: Function accepts keyword arguments that are not identifiers


code:
import re
splitterForIndexing = re.compile(r"(?:[a-zA-Z]+)|(?:[a-zA-Z0-9][a-zA-Z0-9\-(),]+[a-zA-Z0-9\-()])|(?:[,.-])")
source = "Hello. 1-methyl-4-phenylpyridinium is ultra-bad. However, 1-methyl-4-phenyl-1,(2,3),6-tetrahydropyridine is worse."
print "\n".join( splitterForIndexing.findall(source))

rawDescription: Python: question about parsing human-readable text


code:
with open(someFile, 'r') as data:
    xmlData = '<rows>' + data.read() + '</rows>'
rows = ET.fromstring(xmlData)
for row in rows:
    print row.get('PostTypeId')

rawDescription: How to parse/extract specific values from an input huge file in python?

description: Parse/extract specific values from an input huge file in python


code:
tickset = set(s.firstChild.data for s in ticklist)
print "There are %d unique tick types" % len(tickset)
for s in tickset:
    print s

rawDescription: Using minidom to break down an xml file into managable files. Python


code:
def counter(number):
    number = number + 1
    return number

def iterator(iteration, function, arg):
    for i in range(iteration):
        arg = function(arg)
    return arg

number = 0
number = iterator(5, counter, number)
print number # => 5

rawDescription: Python: Outputting data from a function called in a function


code:
d = {
   "1":"sdv","2":"wefc","3":"sdsdv","4":"dvsdv",
   "5":"igsdhc","6":"gvvxAS","7":"sdvsfdv","8":"sdagf"
}

for key in sorted(d, key=int)[:6]:
    del d[key]

print d

rawDescription: python truncating a dictionary to get last elements

description: Python truncating a dictionary to get last elements


code:
from collections import Counter
with open("Input1.txt") as in_file1, open("Input2.txt") as in_file2:
    line = next(in_file1).rstrip().split(",")
    for row in map(str.split, in_file2):
        print map(Counter(row).__getitem__, line)

rawDescription: How to check word in file with list element by using index?

description: Check word in file with list element by using index


code:
def matches(s, pattern):
    d = {}
    return all(cp == "." or d.setdefault(cp, cs) == cs
               for cs, cp in zip(s, pattern))

a = ["resign", "resins", "redyed", "resist", "reeded"]
print [s for s in a if matches(s, ".01.01")]
print [s for s in a if matches(s, ".00.0.")]

rawDescription: Python - finding words based on repeated characters specified in a string


code:
group = groupby([1, 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 1, 3, 3, 3, 3, 3])
result = []
index = 0
for k, g in group:
   length = len(list(g))
   result.append((k, length, index))
   index += length

print max(result, key=lambda a:a[1])

rawDescription: How do you calculate the greatest number of repetitions in a list?

description: Calculate the greatest number of repetitions in a list


code:
author = {'date': ['06-08-2012 09:01:52 PM'], 'path': [u'/branches/Patch_4_2_0_Branch'], 'author': u'glv', 'msg': ['none', u'blah blah blah \n']}
s = format_author(author)
print s

rawDescription: Python,Sorting a dictionary

description: Python,Sorting a dictionary


code:
class PlotPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        self.figure = mplFigure(figsize=(9, 6))
        self.ax = self.figure.add_subplot(111)
        self.ax.plot([1, 2, 3, 4], [2, 3, 5, 8], marker="o", markersize=20, picker=10, linestyle="None")
        self.canvas = mplCanvas(self, -1, self.figure)

        self.figure.canvas.mpl_connect('pick_event', self.onClick)
        self.canvas.Bind(wx.EVT_KEY_DOWN, self._on_key_down)
        self.canvas.Bind(wx.EVT_KEY_UP, self._on_key_up)

        self.states = {"cmd":False, "ctrl":False, "shift":False}

    def onClick(self, event):
        print event
        #print "You pressed key '%s' while clicking!" % pressedKey
        print "Pressed keys:", [k for k in self.states if self.states[k]]
        dlg = TestDialog(self)
        dlg.ShowModal()

    def _on_key_down(self, evt):
        self._set_state(evt)
        evt.Skip()

    def _on_key_up(self, evt):
        self._set_state(evt)
        evt.Skip()

    def _set_state(self, evt):
        self.states["cmd"] = evt.CmdDown()
        self.states["ctrl"] = evt.ControlDown()
        self.states["shift"] = evt.ShiftDown()

class MainFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, -1, "matplotlib pick_event problem")
        self.plotarea = PlotPanel(self)
        self.mainSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.mainSizer.Add(self.plotarea, 1, wx.EXPAND)
        self.SetSizer(self.mainSizer)
        self.mainSizer.Fit(self)

class TestDialog(wx.Dialog):

    def __init__(self, parent):

        pre = wx.PreDialog()
        pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP)
        pre.Create(parent, -1, "sample dialog", size=(200, 100), style=wx.CAPTION|wx.RESIZE_BORDER)
        self.PostCreate(pre)

        self.parent = parent
        self.Bind(wx.EVT_KEY_DOWN, self.parent._on_key_down)
        self.Bind(wx.EVT_KEY_UP, self.parent._on_key_up)

        btn = wx.Button(self, -1, "OK")
        btn.Bind(wx.EVT_BUTTON, self._OnClick)

    def _OnClick(self, evt):
        self.EndModal(wx.ID_OK)

rawDescription: matplotlib pick_event returns incorrect pressed keys

description: Matplotlib pick_event returns incorrect pressed keys


rawDescription: Exercises 3.3 from Think Python

description: Exercises 3.3 from Think Python


code:
try:
    if not all( (0 <= int(i) <= 10000) for i in mystring.split() ):
       raise ValueError("arg!")
except ValueError:
    print "Oops, didn't pass"

rawDescription: Validating integers in Python strings

description: Validating integers in Python strings


code:
tries = [
    ('S0001', '.tif'),
    ('S0001', '.tiff'),
    ('_0_', '.tif'),
]

for sep, subst in tries:
    num = imBN.split(sep)[-1].replace(subst, '')
    try:
        imNo = int(num)
        break
    except ValueError:
        pass
else:
    raise ValueError, "String doesn't match any of the possible patterns"

rawDescription: What is the pythonic way to implement sequential try-excepts?


code:
def is_relative(url):
    return not bool(urlparse.urlparse(url).netloc)

print soup.find_all('a', href=is_relative)

rawDescription: Get relative links from html page

description: Get relative links from html page


code:
c = {}
for item in l:
    c[item[0]] = c.get(item[0], 0) + 1
print sorted(l, key = lambda x: -c[x[0]])

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


code:
class Animal(object):

    def __init__(self, name):
        self.name = name
        print "I am an Animal named %s" % self.name

class Dog(Animal):

    def __init__(self, name):
        super(Dog, self).__init__(name) # That's the important line
        print "I am actually a Dog named %s, I bark too!" % self.name

rover = Dog("Rover")
# Output:
# I am an Animal named Rover
# I am actually a Dog named Rover, I bark too!

rawDescription: Understanding Class Inheritance in Python from learningPythonthehardway


code:
from nltk.collocations import BigramCollocationFinder, BigramAssocMeasures
from nltk.corpus import genesis

# Initialize an association measure for bigrams.
bigram_measures = BigramAssocMeasures()

# Puts the corpus into a BigramCollocationFinder class.
# Now you can search for bigrams in the corpus
finder2 = BigramCollocationFinder.from_words(genesis.words('english-web.txt'))
finder3 = BigramCollocationFinder.from_words(genesis.words('english-web.txt'))

# Let's rank the bigrams by their PMI score and output the top 10.
print finder2.nbest(bigram_measures.pmi, 10)
print
print finder3.nbest(bigram_measures.pmi, 10)

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

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


code:
class Parser():
    def __init__(self,primarydivider,secondarydivider):
        self.prime = primarydivider
        self.second = secondarydivider

    def parse(self,string):
        res = self.initialsplit(string)
        new = []
        for entry in res:
            if self.second not in entry:
                new[-1] += ' ' + entry
            else:
                new.append(entry)
        return dict((entry[0],eval(entry[1])) for entry in [entry.split(self.second) for entry in new])

    def initialsplit(self,string):
        return string.split(self.prime)

mytest = "mode='clip' clipzeros=True field='AEF D' clipminmax=[0,1.2]"
myParser = Parser(' ', '=')
parsed = myParser.parse(mytest)
print parsed

rawDescription: how to preserve types when using pyparsing and returning the result as a dictionary

description: Preserve types when using pyparsing and returning the result as a dictionary


code:
from bs4 import BeautifulSoup as bsoup, Tag
import re

ofile = open("unstructured.html", "rb")
soup = bsoup(ofile)
soup.prettify()

categories = ["Type:","Main Type:","Wheel:","Movement type:","Level:","Sport:","Force:"]
for category in categories:
    wheel_list = []
    f = soup.find(text=re.compile(category)).next_sibling
    if category != "Wheel:":
        string = f.get_text()
        ref = f.get("href")
        print "%s %s (%s)" % (category, string, ref)
    else:
        while f.name == "a":
            content = f.contents[0]
            res = f.get("href")
            wheel_list.append("%s (%s)" % (content, res))
            f = f.find_next()
        ref = ", ".join(wheel_list)
        print "%s %s" % (category, ref)

rawDescription: Parsing links and strings from unstructured HTML data


code:
items = [i.strip() for i in reference if i not in done]
print ' '.join(items)

rawDescription: Comparing file contents in Python

description: Comparing file contents in Python


code: print '\n'.join('Val = {}'.format(val) for val in new_float_array[8])

rawDescription: Convert a float array into little/big endian


code:
x = 0
while x < 9:
    print "this works"
    x = x + 1

print "this doesn't work"

rawDescription: Simple "if" statements in Python

description: Simple "if" statements in Python


code:
my_list = [
    (1, "BankA"),
    (2, "BankB"),
    (3, "BankA"),
    (4, "BankC"),
]

ids, result = {}, {}
for num, bank in my_list: ids.setdefault(bank, []).append (num)
for num, bank in my_list: result[num] = ids[bank]
print result

rawDescription: Grouping elements according to a single value


code:
from bs4 import BeautifulSoup
import urllib2
import datetime

url1 = 'http://www.salatomatic.com/d/Revesby+17154+Ahlus-Sunnah-Wal-Jamaah-Revesby'

content1 = urllib2.urlopen(url1).read()
soup1 = BeautifulSoup(content1)
div1 = soup1.find('div', {'class':'titleBM'}) #get the div where it's located
pos1 = div1.next_sibling

print datetime.datetime.now(), 'street address:  ' , pos1

rawDescription: Scrape address using BeautifulSoup for Python


code:
# Add this code to your script
t = Timer("for i in xrange(1, 100): gen_pent(i)", "from __main__ import gen_pent")
print t.timeit()

rawDescription: Optimizing a Partition Function

description: Optimizing a Partition Function


code:
def by_three(n):
    orig_num = n
    if (isinstance(orig_num, int) and orig_num%3 == 0 ):
        cube(orig_num)
    else:
        print "False"

def cube(orig_num):
    cube = orig_num**3
    print cube
    return

by_three(11)
by_three(12)
by_three(13)

rawDescription: Strange multiple return values in Python


code:
with open("myfile") as fo:
    reader = csv.reader(
        fo,
        quotechar='"',
        delimiter=',',
        quoting=csv.QUOTE_ALL,
        skipinitialspace=True,
        escapechar='\\'
    )

    for row in reader:
        print row

rawDescription: python csv reader not handling quotes

description: Python csv reader not handling quotes


code:
for a, b in zip(range(10), range(10)[1:]):
    print a, b

rawDescription: When walking through files and folders, how do I compare the current filename with the next, in python?


code: print '{name} is {age} old'.format(**person.__dict__)

rawDescription: Can I express named Python string arguments more succintly?

description: Express named Python string arguments more succintly


code:
from prettytable import PrettyTable

table = PrettyTable(["Column 1", "Column 2", "Column 3"])
table.add_row(["A", "B", "C"])
table.add_row(["F", "O", "O"])
table.add_row(["B", "A", "R"])

for row in table:
    print row.get_string(fields=["Column 1"]) # Column 1

rawDescription: How to get the value of a cell when using prettytable

description: Get the value of a cell when using prettytable


code:
class SharedInt:
    val = None
    def __init__(self, v): self.val = v

    def __add__(self, a):
        self.val += a
        return self.val

    def __int__(self): return self.val

v1 = SharedInt(10)

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

for i in listA: i += 10
for i in listB: i += 10

print int(listA[0])
print int(listB[0])

rawDescription: Multiple references in separate lists; Python


code:
from django.core.management.base import BaseCommand

class Command(BaseCommand):
    help = 'Description of your command.'

    def handle(self, *args, **options):
        # Add your initialization here.
        print 'What should I initialize?'

rawDescription: Django app initialization process

description: Django app initialization process


code:
import xml.etree.ElementTree as ET
txt = """
<component>

    <id root="8e9e0719-efa5-451c-bea3-d547298ad0a1"/>
    <code code="34070-3" codeSystem="2.16.840.1.113883.6.1" displayName="CONTRAINDICATIONS SECTION"/
    <title>CONTRAINDICATIONS</title>
    <text>
    <paragraph>Atenolol and chlorthalidone tablets are contraindicated in patients with: sinus brady
        <linkHtml href="#LINK_0df2629f-13c7-4b14-8664-475c32377c68">WARNINGS</linkHtml>
            </content>); anuria; hypersensitivity to this product or to sulfonamide-derived drugs.</
    </text>
    <effectiveTime value="20101001"/>

</component>"""

root = ET.fromstring(txt)

for e in root.iter('text'):
    print ">>"
    print ET.tostring(e, method="text")
    print "<<"

rawDescription: Python Element Tree Handling Tags within Tags XML


code:
pool.map_async( myFunct, myArgsList)
pool.close()
pool.join()
print 'completed'

rawDescription: Why function called by multiprocessing is not printing the messages?


code:
for i,word in enumerate(word_generator()):
    if i > 100:
      break
    print word
print "Last Word:",word

rawDescription: Python: How to retrieve second word from the text


code:
data = """35\tIMAGE:1679942\tSAMD4\tSterile alpha motif domain\tHs.98259\tATI146610
36\tIMAGE:1700154\t\t\tAI049531"""

for line in data.split("\n"):
    print line.split("\t")

rawDescription: keep tabs after .split()

description: Keep tabs after .split()


code:
class MyHTMLParser(HTMLParser):

    def handle_starttag(self, tag, attrs):
        # Only parse the 'anchor' tag.
        if tag == "a":
           # Check the list of defined attributes.
           for name, value in attrs:
               # If href is defined, print it.
               if name == "href":
                   print name, "=", value

rawDescription: Extracting Info From Webpage using urllib2


code:
from bs4 import BeautifulSoup
s = '''

    Ugug
    dfgd454

'''
soup = BeautifulSoup(s)
print soup.select('div > span.value')[0].text

rawDescription: How to crawl a specific value out of a HTML page with python HTMLParser

description: Crawl a specific value out of a HTML page with python HTMLParser


code:
import re
print re.sub(r'(\d.*?)$', '55', mystr)

rawDescription: Replace string element with split method


code:
from pyspark.sql import Row

document_row = Row("document_id", "document_text")
keyword_row = Row("keyword")

documents_df = sc.parallelize([
    document_row(1L, "apache spark is the best"),
    document_row(2L, "erlang rocks"),
    document_row(3L, "but haskell is better")
]).toDF()

keywords_df = sc.parallelize([
    keyword_row("erlang"),
    keyword_row("haskell"),
    keyword_row("spark")
]).toDF()

rawDescription: How can we JOIN two Spark SQL dataframes using a SQL-esque "LIKE" criterion?


code:
df['Numbers'] = df['Numbers'] + 10
print df

rawDescription: How do you apply a function to one of several columns in a DataFrame?

description: Apply a function to one of several columns in a DataFrame


code:
Lists = [[1,2,4,5],[1,2,3,4,5,7],[1,4,5,6]]

for List in Lists:
    i = 1
    while i <= List[-1] + 1:
        if i in List:
            i += 1
        else:
            break
    print i

rawDescription: How to find the smallest missing numbers from a list without using inbuilt functions or modules

description: Find the smallest missing numbers from a list without using inbuilt functions or modules


rawDescription: Split a string into N equal parts?

description: Split a string into N equal parts


code:
print len(a) if isinstance(a, collections.Iterable) else 1

print len(a) if isinstance(a, collections.Sized) else 1

rawDescription: What is the most pythonic way to use len on a scalar?


code:
results = []
for value in range(0, 4):
    print 'For f(',(value),')', 'the number is:' , math_formula(value)
    results.append(math_formula(value)) # add the value to the list

print ''
print 'The maximum of these four numbers is:', max(results)

rawDescription: How can i return maximum number in range in Python

description: Return maximum number in range in Python


code:
from BeautifulSoup import BeautifulSoup
def removeTag(soup, tagname):
    for tag in soup.findAll(tagname):
        contents = tag.contents
        parent = tag.parent
        tag.extract()

s = BeautifulSoup("abcd <b> btag </b> hello <d>dtag</d>")

removeTag(s,"b")
print s
removeTag(s, "d")
print s

rawDescription: strip tags python

description: Strip tags python


code: print i

rawDescription: Dice generator, Picking number of sides in PYTHON 3.3.4


code: print [i for i in re.split(r"d\b",x) if i] #if you dont want null strings.

rawDescription: Split by suffix with Python regular expression


code:
import mmap

handle = open('/usr/bin/xxd', 'r')
memorymap = mmap.mmap(handle.fileno(), 0, prot=mmap.PROT_READ)

value_to_hex = dict(enumerate('0123456789ABCDEF'))
hex_to_value = {v: k for (k, v) in value_to_hex.items()}

def expand_byte(byte):
    """ Converts a single byte into 2 4 bit values """
    return [(byte >> s) & 0xF for s in [4, 0]]

def compact_bytes(values):
    """ Converts 2 4 bit values into a single byte """
    return (values[0] << 4) | values[1]

def bin_to_hex(data):
    """ Converts binary data to hex characters """
    return [value_to_hex[v] for b in data for v in expand_byte(b)]

def hex_to_bin(hexadecimal):
    """ Converts hex characters to binary data """
    return [
        compact_bytes([hex_to_value[v] for v in hexadecimal[i:i + 2]])
        for i in range(0, len(hexadecimal), 2)
    ]

test_data = [ord(c) for c in memorymap[0:8]]
hex_data = bin_to_hex(test_data)
final_data = hex_to_bin(hex_data)

print "From '{0}'\nto '{1}'\nto '{2}'".format([chr(c) for c in test_data], hex_data, [chr(c) for c in final_data])

rawDescription: Python-Hex editing specific locations in a file


code: print map(lambda s: s.replace('HD\,' , 'HD '), L)

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


code:
if "1" in self.users:
   print "True"

rawDescription: Python if Statement not finding value

description: Python if Statement not finding value


code:
import numpy
import multiprocessing

x = numpy.linspace(0,20,10000)
p = multiprocessing.Pool(processes=4)

print p.map(numpy.sqrt, x)

rawDescription: Need help to parallelize a loop in python


rawDescription: how handle events on every individual entry

description: How handle events on every individual entry


code:
def characters(c):
  for n in xrange(ord(c), ord('a'), -1):
    print chr(n),
  for n in xrange(ord('a'), ord(c)+1):
   print chr(n),
  print

rawDescription: take a char and print out from char to 'a' and revers it should be recursive

description: Take a char and print out from char to 'a' and revers it should be recursive


code:
from win32file import *
from winioctlcon import FSCTL_GET_REPARSE_POINT

__all__ = ['islink', 'readlink']

# Win32file doesn't seem to have this attribute.
FILE_ATTRIBUTE_REPARSE_POINT = 1024
# To make things easier.
REPARSE_FOLDER = (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)

# For the parse_reparse_buffer function
SYMBOLIC_LINK = 'symbolic'
MOUNTPOINT = 'mountpoint'
GENERIC = 'generic'

def islink(fpath):
    """ Windows islink implementation. """
    if GetFileAttributes(fpath) & REPARSE_FOLDER:
        return True
    return False

def parse_reparse_buffer(original, reparse_type=SYMBOLIC_LINK):
    """ Implementing the below in Python:

    typedef struct _REPARSE_DATA_BUFFER {
        ULONG  ReparseTag;
        USHORT ReparseDataLength;
        USHORT Reserved;
        union {
            struct {
                USHORT SubstituteNameOffset;
                USHORT SubstituteNameLength;
                USHORT PrintNameOffset;
                USHORT PrintNameLength;
                ULONG Flags;
                WCHAR PathBuffer[1];
            } SymbolicLinkReparseBuffer;
            struct {
                USHORT SubstituteNameOffset;
                USHORT SubstituteNameLength;
                USHORT PrintNameOffset;
                USHORT PrintNameLength;
                WCHAR PathBuffer[1];
            } MountPointReparseBuffer;
            struct {
                UCHAR  DataBuffer[1];
            } GenericReparseBuffer;
        } DUMMYUNIONNAME;
    } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;

    """
    # Size of our data types
    SZULONG = 4 # sizeof(ULONG)
    SZUSHORT = 2 # sizeof(USHORT)

    # Our structure.
    # Probably a better way to iterate a dictionary in a particular order,
    # but I was in a hurry, unfortunately, so I used pkeys.
    buffer = {
        'tag' : SZULONG,
        'data_length' : SZUSHORT,
        'reserved' : SZUSHORT,
        SYMBOLIC_LINK : {
            'substitute_name_offset' : SZUSHORT,
            'substitute_name_length' : SZUSHORT,
            'print_name_offset' : SZUSHORT,
            'print_name_length' : SZUSHORT,
            'flags' : SZULONG,
            'buffer' : u'',
            'pkeys' : [
                'substitute_name_offset',
                'substitute_name_length',
                'print_name_offset',
                'print_name_length',
                'flags',
            ]
        },
        MOUNTPOINT : {
            'substitute_name_offset' : SZUSHORT,
            'substitute_name_length' : SZUSHORT,
            'print_name_offset' : SZUSHORT,
            'print_name_length' : SZUSHORT,
            'buffer' : u'',
            'pkeys' : [
                'substitute_name_offset',
                'substitute_name_length',
                'print_name_offset',
                'print_name_length',
            ]
        },
        GENERIC : {
            'pkeys' : [],
            'buffer': ''
        }
    }

    # Header stuff
    buffer['tag'] = original[:SZULONG]
    buffer['data_length'] = original[SZULONG:SZUSHORT]
    buffer['reserved'] = original[SZULONG+SZUSHORT:SZUSHORT]
    original = original[8:]

    # Parsing
    k = reparse_type
    for c in buffer[k]['pkeys']:
        if type(buffer[k][c]) == int:
            sz = buffer[k][c]
            bytes = original[:sz]
            buffer[k][c] = 0
            for b in bytes:
                n = ord(b)
                if n:
                    buffer[k][c] += n
            original = original[sz:]

    # Using the offset and length's grabbed, we'll set the buffer.
    buffer[k]['buffer'] = original
    return buffer

def readlink(fpath):
    """ Windows readlink implementation. """
    # This wouldn't return true if the file didn't exist, as far as I know.
    if not islink(fpath):
        return None

    # Open the file correctly depending on the string type.
    handle = CreateFileW(fpath, GENERIC_READ, 0, None, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT, 0) \
                if type(fpath) == unicode else \
            CreateFile(fpath, GENERIC_READ, 0, None, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT, 0)

    # MAXIMUM_REPARSE_DATA_BUFFER_SIZE = 16384 = (16*1024)
    buffer = DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, None, 16*1024)
    # Above will return an ugly string (byte array), so we'll need to parse it.

    # But first, we'll close the handle to our file so we're not locking it anymore.
    CloseHandle(handle)

    # Minimum possible length (assuming that the length of the target is bigger than 0)
    if len(buffer) < 9:
        return None
    # Parse and return our result.
    result = parse_reparse_buffer(buffer)
    offset = result[SYMBOLIC_LINK]['substitute_name_offset']
    ending = offset + result[SYMBOLIC_LINK]['substitute_name_length']
    rpath = result[SYMBOLIC_LINK]['buffer'][offset:ending].replace('\x00','')
    if len(rpath) > 4 and rpath[0:4] == '\\??\\':
        rpath = rpath[4:]
    return rpath

def realpath(fpath):
    from os import path
    while islink(fpath):
        rpath = readlink(fpath)
        if not path.isabs(rpath):
            rpath = path.abspath(path.join(path.dirname(fpath), rpath))
        fpath = rpath
    return fpath

def example():
    from os import system, unlink
    system('cmd.exe /c echo Hello World > test.txt')
    system('mklink test-link.txt test.txt')
    print 'IsLink: %s' % islink('test-link.txt')
    print 'ReadLink: %s' % readlink('test-link.txt')
    print 'RealPath: %s' % realpath('test-link.txt')
    unlink('test-link.txt')
    unlink('test.txt')

if __name__=='__main__':
    example()

rawDescription: Create NTFS junction point in Python

description: Create NTFS junction point in Python


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

rawDescription: Sorting dictionary keys by values in a list?


code:
def fib(n):
    a = 1
    b = 0
    while n > 1:
        a, b = a+b, a
        n = n - 1
    return a

print fib(100000)

rawDescription: Non Brute Force Solution to Project Euler 25


code:
for i in a:
   print '%.6g' % (i,)

rawDescription: How to make Python format floats with certain amount of significant digits?

description: Make Python format floats with certain amount of significant digits


code:
ACCESS_TOKEN = '<REDACTED>'

from collections import defaultdict
import json

from dropbox.client import DropboxClient

def make_tree():
    return {
        'is_dir': True,
        'contents': defaultdict(make_tree)
    }
tree = defaultdict(make_tree)

client = DropboxClient(ACCESS_TOKEN)

has_more = True
cursor = None

while has_more:
    delta = client.delta(cursor)

    cursor = delta['cursor']
    has_more = delta['has_more']

    for path, metadata in delta['entries']:
        if metadata is not None:

            # find the right place in the tree
            segments = path.split('/')
            location = tree['/']
            for i in xrange(1, len(segments)-1):
                current_path = '/'.join(segments[:i+1])
                location = location['contents'][current_path]

            # insert the new entry
            location['contents'][path] = {
                'is_dir': metadata['is_dir'],
                'contents': {}
            }

print json.dumps(tree['/'], indent=4)

rawDescription: Build directory tree from dropbox API

description: Build directory tree from dropbox API


code:
import os, json
import pandas as pd

path_to_json = 'somedir/'
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]
print json_files  # for me this prints ['foo.json']

rawDescription: Python: Read several json files from a folder


code:
stuff = """
Other stuff
[quote user2]
[quote user1]Hello[/quote]
World
[/quote]
Other stuff after the stuff
"""

from pyparsing import (Word, printables, originalTextFor, Literal, OneOrMore,
    ZeroOrMore, Forward, Suppress)

# prototype username
username = Word(printables, excludeChars=']')

# BBCODE quote tags
openQuote = originalTextFor(Literal("[") + "quote" + username + "]")
closeQuote = Literal("[/quote]")

# use negative lookahead to not include BBCODE quote tags in tbe body of the quote
contentWord = ~(openQuote | closeQuote) + (Word(printables,excludeChars='[') | '[')
content = originalTextFor(OneOrMore(contentWord))

# define recursive definition of quote, suppressing any nested quotes
quotes = Forward()
quotes << ( openQuote + ZeroOrMore( Suppress(quotes) | content ) + closeQuote )

# put separate tokens back together
quotes.setParseAction(lambda t : '\n'.join(t))

# quote extractor
for q in quotes.searchString(stuff):
    print q[0]

# nested quote stripper
print quotes.transformString(stuff)

rawDescription: Remove nested bbcode quotes in Python?

description: Remove nested bbcode quotes in Python


rawDescription: python how to transfer between a list of integers and string of bits?

description: Python how to transfer between a list of integers and string of bits


code:
import inspect
import sys

lines = inspect.getsourcelines(sys.modules[__name__])[0]

for index, line in enumerate(lines):
    print "{:4d} {}".format(index + 1, line)

rawDescription: How to get the current script's code in Python?

description: Get the current script's code in Python


code:
s='abc/def/ghi'
print s[:s.find("/")]
print s[:s.rfind("/")]
print s[s.find("/")+1:]
print s[s.rfind("/")+1:]

rawDescription: ksh-style left and right string stripping up to matched expression?

description: Ksh-style left and right string stripping up to matched expression


code:
for n in range(3):
    print n
#output:
# 0
# 1
# 2

rawDescription: assign values executed to array in python

description: Assign values executed to array in python


code:
import os, csv, sys, Tkinter, tkFileDialog as fd, xlrd

# stop tinker shell from opening as only needed for file dialog
root = Tkinter.Tk()
root.withdraw()

def format_date(dt):
    yyyy, mm, dd = str(dt[0]), str(dt[1]), str(dt[2])
    hh, mi, ss = str(dt[3]), str(dt[4]), str(dt[5])

    if len(mm) == 1:
        mm = '0'+mm
    if len(dd) == 1:
        dd = '0'+dd

    if hh == '0' and mi == '0' and ss == '0':
        datetime_str = dd+'/'+mm+'/'+yyyy
    else:
        if len(hh) == 1:
            hh = '0'+hh
        if len(mi) == 1:
            mi = '0'+mi
        if len(ss) == 1:
            ss = '0'+ss
        datetime_str = dd+'/'+mm+'/'+yyyy+' '+hh+':'+mi+':'+ss

    return datetime_str

def xl_to_csv(in_path, out_path):
    # set up vars to read file
    wb = xlrd.open_workbook(in_path)
    sh1 = wb.sheet_by_index(0)
    row_cnt, col_cnt = sh1.nrows, sh1.ncols

    # set up vars to write file
    fileout = open(out_path, 'wb')
    writer = csv.writer(fileout)

    # iterate through rows and cols
    for r in range(row_cnt):

        # make list from row data
        row = []
        for c in range(col_cnt):
            #print "...debug - sh1.cell(",r,c,").value set to:", sh1.cell(r,c).value
            #print "...debug - sh1.cell(",r,c,").ctype set to:", sh1.cell(r,c).ctype

            # check data type and make conversions
            val = sh1.cell(r,c).value
            if sh1.cell(r,c).ctype == 2: # number data type
                if val == int(val):
                    val = int(val) # convert to int if only no decimal other than .0
                #print "...debug - res 1 (float to str), val set to:", val
            elif sh1.cell(r,c).ctype == 3: # date fields
                dt = xlrd.xldate_as_tuple(val, 0) # date no from excel to dat obj
                val = format_date(dt)
                #print "...debug - res 2 (date to str), val set to:", val
            elif sh1.cell(r,c).ctype == 4: # boolean data types
                val = str(bool(val)) # convert 1 or 0 to bool true / false, then string
                #print "...debug - res 3 (bool to str), val set to:", val
            else:
                val = str(val)
                #print "...debug - else, val set to:", val

            row.append(val)
            #print ""

        # write row to csv file
        try:
            writer.writerow(row)
        except:
            print '...row failed in write to file:', row
            exc_type, exc_value, exc_traceback = sys.exc_info()
            lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
            for line in lines:
                print '!!', line

    print 'Data written to:', out_path, '\n'

def main():
    in_path, out_path = None, None

    # set current working directory to user's my documents folder
    os.chdir(os.path.join(os.getenv('userprofile'),'documents'))

    # ask user for path to Excel file...
    while not in_path:
        print "Please select the excel file to read data from ..."
        try:
            in_path = fd.askopenfilename()
        except:
            print 'Error selecting file, please try again.\n'

    # get dir for output...
    same = raw_input("Do you want to write the output to the same directory? (Y/N): ")
    if same.upper() == 'Y':
        out_path = os.path.dirname(in_path)
    else:
        while not out_path:
            print "Please select a directory to write the csv file to ..."
            try:
                out_path = fd.askdirectory()
            except:
                print 'Error selecting file, please try again.\n'

    # get file name and join to dir
    f_name = os.path.basename(in_path)
    f_name = f_name[:f_name.find('.')]+'.csv'
    out_path = os.path.join(out_path,f_name)

    # get data from file and write to csv...
    print 'Attempting read data from', in_path
    print ' and write csv data to', out_path, '...\n'
    xl_to_csv(in_path, out_path)

    v_open = raw_input("Open file (Y/N):").upper()
    if v_open == 'Y':
        os.startfile(out_path)
    sys.exit()

if __name__ == '__main__':
    main()

rawDescription: Search through directories for specific Excel files and compare data from these files with inputvalues


code:
def intWithCommas(x):
    if type(x) not in [type(0), type(0L)]:
        raise TypeError("Parameter must be an integer.")
    if x < 0:
        return '-' + intWithCommas(-x)
    result = ''
    while x >= 1000:
        x, r = divmod(x, 1000)
        result = ",%03d%s" % (r, result)
    return "%d%s" % (x, result)

rawDescription: How to print number with commas as thousands separators?

description: Print number with commas as thousands separators


code:
n = 3
result = [bin(k)[2:].rjust(n, '0') for k in xrange(2**n)]
print result
n = 24

rawDescription: How to generate decimal in range for 24 bit?

description: Generate decimal in range for 24 bit


code:
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector

class MySpider(BaseSpider):
    name = "ashp"
    allowed_domains = ["ashp.org"]
    start_urls = ["http://ashp.org/menu/DrugShortages/CurrentShortages"]

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        links = hxs.select("//div[@id='Mid_3Col']/div/table/tr/td/a")
        for link in links:
            title = link.select("text()").extract()[0]
            link = link.select("@href").extract()[0]
            print title, link

rawDescription: XPath cutting off href attribute

description: XPath cutting off href attribute


code:
import re

def format_to_re(format):
    # Replace Python string formatting syntax with named group re syntax.
    return re.compile(re.sub(r'%\((\w+)\)s', r'(?P<\1>\w+)', format))

twitter_format = 'twitter.com/%(username)s/'
twitter_re = format_to_re(twitter_format)

m = twitter_re.search('twitter.com/dir01/')
print m.groupdict()
print twitter_format % m.groupdict()

rawDescription: How to make string from regex and value of group

description: Make string from regex and value of group


code: print subsetSum(tot, vals, mem)

rawDescription: Modify Recursive Subset Sum + Memoize to print values


code:
import csv
import re
from collections import OrderedDict

with open('input.txt','r') as f:
    reader = csv.reader(f)
    thelist = list(reader)
result = OrderedDict()
parsed = [(re.split('\s+', line[0]))[:2] for line in thelist][1:]
for x, y in parsed:
    if x:
        temp = x
        result[x] = y
    else:
        result[temp] = ','.join([result[temp], y])
print result.items()

rawDescription: Python - read table with empty fields from file


code:
from nltk.collocations import BigramCollocationFinder, BigramAssocMeasures

tokens = ['she', 'knocked', 'on', 'his', 'door', 'she', 'knocked', 'at',
'the', 'door','100', 'women', 'knocked', 'on', "Donaldson's", 'door', 'a',
'man', 'knocked', 'on', 'the', 'metal', 'front', 'door']

finder = BigramCollocationFinder.from_words(tokens)
bigram_measures = BigramAssocMeasures()
print finder.word_fd.N()

student_t = {k:v for k,v in finder.score_ngrams(bigram_measures.student_t)}
print student_t['she', 'knocked']

rawDescription: What is the total bigram count returned for NLTK BigramCollocationFinder?


code:
import random

things = ['fish', 'mammal', 'bird']
randomThing = thing(random.sample(things,  1))

print randomThing.type
print randomThing.flight
print randomThing.swim

rawDescription: Assign variables to random elements from list? Python


code:
import pandas as pd
df = pd.DataFrame({ 'DateVar' : ['9/1/2013', '10/1/2013', '2/1/2014'],
                'Field' : 'foo' })

print df.DateVar.str.extract(r"(?P<month>\d+)/(?P<day>\d+)/\d+").astype(int)

rawDescription: Python How to get 1st element of date token


code:
import clr
clr.AddReference("IronPythonDelegates")

import IronPythonDelegates

def camActionPy(camera, time):
  print "Camera: " + str(camera) + ", time: " + str(time)

IronPythonDelegates.CameraAnimation(camActionPy);

rawDescription: How to pass a lambda expression to a C# constructor from an IronPython script?

description: Pass a lambda expression to a C# constructor from an IronPython script


code:
for key in your_dict.keys():
  print key

rawDescription: Enumerate Keys in a dictionary?

description: Enumerate Keys in a dictionary


code:
c1=Cookie.SimpleCookie()
c1['name']='A:0'
print c1
c1['name']=r'"A:0"'
print c1
c1['name']=r'"\"A:0\""'
print c1

rawDescription: Backslashes being added into my cookie in Python


code:
x=5      # The name x is assigned to the integer object 5
y=x      # The name y is assigned to the same integer object referenced by x
x=7      # The name x is *reassigned* to the new integer object 7
print y  # This prints 5 because the value of y was never changed

rawDescription: List vs. int assignment - "Every variable is a pointer"


code:
import string

class Resume():
    def __init__(self,filename):
        self.filepath = filename
        self.load()
        self.parse()

    def load(self):
        with open(self.filepath,'rb') as f:
            self.content = f.read().splitlines()

    def checkLine(self,word,value, content, line):
        if word in content.lower():
            value = self.addValue(value,line)
        return value

    def addValue(self,value,line):
        value[line] = value.get(line,0) + 1
        return value

    def dict_List(self,dict_, content):
        new = [(key,value) for key,value in dict_.items() if dict_[key] == max(dict_.values())]
        return [(x[0],content[x[0]]) for x in sorted(new)]

    def get_name(self):
        names = []
        for each in self.name:
            if each[0] not in self.headings:
                each = each[1].replace('Name',"")
                if each[0] not in string.letters:
                    each = each[1:]
                names.append(each.strip())
            else:
                index = self.headings[self.headings.index(each[0])+1]
                names.append("\n".join(self.content[each[0]+1:index]))
        if len(names) == 1:
            return names[0]
        else:
            return names

    def get_work(self):
        experience = []
        for each in self.work:
            index = self.headings[self.headings.index(each[0])+1]
            experience.append("\n".join(self.content[each[0]+1:index]))
        if len(experience) == 1:
            return experience[0]
        else:
            return epxerience

    def parse(self):
        name = dict()
        work_experience = dict()
        isHeading = dict()
        for line_num in range(len(self.content)):
            for checkName in ["name",":"]:
                name.update(self.checkLine(checkName,name,self.content[line_num], line_num))
            for checkWork in ["work","experience"]:
                work_experience.update(self.checkLine(checkWork,work_experience, self.content[line_num],line_num))
            if line_num != len(self.content) - 1:
                if len(self.content[line_num + 1]) > len(self.content[line_num]):
                    isHeading.update(self.addValue(isHeading,line_num))
            if line_num > 0:
                if self.content[line_num - 1] == "":
                    isHeading.update(self.addValue(isHeading,line_num))
            if len(self.content[line_num]) == len(self.content[line_num].lstrip()):
                isHeading.update(self.addValue(isHeading,line_num))
            if self.content[line_num] == "":
                isHeading[line_num] = isHeading.get(line_num,0) - 1

        self.name = self.dict_List(name, self.content)
        self.work = self.dict_List(work_experience, self.content)
        self.headings = self.dict_List(isHeading, self.content)
        self.headings = [x[0] for x in self.headings]

if __name__ == "__main__":
    resume = Resume(filename = 'sampleresume.txt')
    print resume.get_name()
    print resume.get_work()

rawDescription: Extracting multiple fields from resume with Python


code:
soup = BeautifulSoup(data, 'html.parser')
for item in soup:
    if not isinstance(item, Tag):
        for part in item.split():
            print part
    else:
        print item

rawDescription: How to parse sentences into tokens with either regex or toolkits

description: Parse sentences into tokens with either regex or toolkits


code:
iterator = iter(proc.stdout.readline, '')

# Somewhere else in your program...
for line in iterator:
    print line

rawDescription: How do you execute a terminal command and then repeatedly check and store the response in Python?

description: Execute a terminal command and then repeatedly check and store the response in Python


code:
from io import open
import re
with open("dictionary", encoding="utf-8") as f:
    words = set(line.strip() for line in f)
with open("document", encoding="utf-8") as f:
    for w in re.findall(r"\w+", f.read()):
        if w not in words:
            print "Misspelled:", w.encode("utf-8")

rawDescription: Fastest possible dictionary-like matching


code:
import ipaddr
import sys

try:
   ip = ipaddr.IPAddress(sys.argv[1])
   print '%s is a correct IP%s address.' % (ip, ip.version)
except ValueError:
   print 'address/netmask is invalid: %s' % sys.argv[1]
except:
   print 'Usage : %s  ip' % sys.argv[0]

rawDescription: Checking for IP addresses

description: Checking for IP addresses


code:
import itertools

def gen():
    yield 'egg'
    yield 'spam'
    yield 'ham'

for i, j in itertools.combinations(gen(), 2):
    print i, j

rawDescription: Managing a computation onlt with iterators Python


rawDescription: How to format numbers in Python 3 sortable into strings?

description: Format numbers in Python 3 sortable into strings


rawDescription: Implementing a bit-swap hack in Python

description: Implementing a bit-swap hack in Python


code:
width, height = 11, 11
a, b = 5, 5
r = 5
EPSILON = 2.2

map_ = [['.' for x in range(width)] for y in range(height)]

# draw the circle
for y in range(height):
    for x in range(width):
        # see if we're close to (x-a)**2 + (y-b)**2 == r**2
        if abs((x-a)**2 + (y-b)**2 - r**2) < EPSILON**2:
            map_[y][x] = '#'

# print the map
for line in map_:
    print ' '.join(line)

rawDescription: How can I draw a circle in a data array/map in python

description: Draw a circle in a data array/map in python


code:
import urllib
from bs4 import BeautifulSoup

html = urllib.urlopen('http://www.last.fm/user/Jehl/charts?rangetype=overall&subtype=artists').read()
soup = BeautifulSoup(html)
print soup('a')
# prints [Last.fm, Music....

rawDescription: How can I extract the text between ?

description: Extract the text between


code:
import sys
from proton import *

#This code is for initiating the AMQP messenger
amqpmng = Messenger()
amqpmng._set_timeout(2000L) #Set timeout for sending and receiving at 2000 ms
address = "amqp://owner:<longpassword>@<namespace>.servicebus.windows.net/<queuename>"

#This code is for creating messages
msg = Message()
msg.subject = "This is a testmessage"
msg.body = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit."

#This code is for sending messages
try:
    msg.address = address
    amqpmng.put(msg)
    amqpmng.send()
except:
    e = sys.exc_info()[0]
    print e, "Waited for 2s to send messages, nothing send, connection timed out"

amqpmng.stop();

#This code is for receiving messages
amqpmng.subscribe(address)
amqpmng.start()
try:
    amqpmng.recv(1) #receive exactly 1 message (you can enter any value)
    msg = Message()
    while amqpmng.incoming > 0:
        amqpmng.get(msg)
        print(msg.body)
except:
    e = sys.exc_info()[0]
    print e, "Waited for 2s to receive messages, nothing received, connection timed out"

amqpmng.stop()

rawDescription: Setting custom message properties in Qpid Proton using the Python binding


code: print datetime.datetime(1970, 1, 1) + datetime.timedelta(microseconds=time.time()*1000*1000)

rawDescription: PRTime to datetime in Python

description: PRTime to datetime in Python


code:
print 'Total Count =', totalCount
print 'Percentage of Positive numbers =', numberOfPositives * 100./totalCount, '%'

rawDescription: Python excel number of times a negative and positive number appears (count/frequency)


code:
import multiprocessing as mp
import time

def seq(count):
    print "runing seq"
    start_time = time.time()
    result = []
    for i in range(count):
        result.append(cube(i))
    print "seq --- time:{0:.4f}".format(time.time() - start_time)
    #print "seq --- time:{0:.4f}, result:{1}".format(time.time() - start_time, result)

def par(count):
    print "runing par"
    start_time = time.time()
    result = mp.Pool(processes=2).map(cube,range(count))
    print "par --- time:{0:.4f}".format(time.time() - start_time)
    #print "par --- time:{0:.4f}, result:{1}".format(time.time() - start_time, result)

def cube(x):
    time.sleep(.01)
    return x*x*x

if __name__ == "__main__":
    count = 400
    seq(count)
    par(count)

rawDescription: python multiprocessing not working?

description: Python multiprocessing not working


code:
from pytz import timezone
from datetime import datetime

eastern = timezone('US/Eastern')
utc = timezone('UTC')

created_at = datetime.strptime(tweet[0]['created_at'], '%a %b %d %H:%M:%S +0000 %Y')
utc_created_at = utc.localize(created_at)
print utc_created_at
est_created_at = utc_created_at.astimezone(eastern)
print est_created_at

rawDescription: python twitter api tweet timestamp convert from UTC to EST

description: Python twitter api tweet timestamp convert from UTC to EST


code:
ctsquare.py
import numpy
import ctypes

# pointer to float type, for convenience
c_float_p = ctypes.POINTER(ctypes.c_float)

# load the library
ctsquarelib = ctypes.cdll.LoadLibrary("ctsquare.so")

# define the return type and arguments of the function
ctsquarelib.mysumsquares.restype = ctypes.c_float
ctsquarelib.mysumsquares.argtypes = [c_float_p, ctypes.c_size_t]

# python front-end function, takes care of the ctypes interface
def myssq(arr):
    # make sure that the array is contiguous and the right data type
    arr = numpy.ascontiguousarray(arr, dtype='float32')

    # grab a pointer to the array's data
    dataptr = arr.ctypes.data_as(c_float_p)

    # this assumes that the array is 1-dimensional. 2d is more complex.
    datasize = arr.ctypes.shape[0]

    # call the C function
    ret = ctsquarelib.mysumsquares(dataptr, datasize)

    return ret

if __name__ == '__main__':
    a = numpy.array([1,2,3,4])
    print 'sum of squares of [1,2,3,4] =', myssq(a)

rawDescription: Beginner extending C with Python (specifically Numpy)


code:
def lazyprop(fn):
    attr_name = '_lazy_' + fn.__name__
    @property
    def _lazyprop(self):
        if not hasattr(self, attr_name):
            setattr(self, attr_name, fn(self))
        return getattr(self, attr_name)
    return _lazyprop

class Test(object):

    @lazyprop
    def a(self):
        print 'generating "a"'
        return range(5)

rawDescription: Python memoising/deferred lookup property decorator


code:
c = 9
qq = 4
b = 5
a = 0

for i in range(5):
    answer = (b*a)+c
    print a, answer
    a += qq

rawDescription: Ignore the initial value of the parameter in calculation


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

def two_floats(value):
    values = value.split()
    if len(values) != 2:
        raise argparse.ArgumentError
    values = map(float, values)
    return values

parser = argparse.ArgumentParser()
parser.add_argument('--xlim',
                  help = 'X axis limits',
                  action = 'store', type=two_floats,
                  default = [-1.e-3, 1.e-3])
print parser.parse_args()

rawDescription: Python Argparse: Issue with optional arguments which are negative numbers


code:
properties = soup.findAll('a', title=re.compile('Bedroom'))
for eachproperty in properties:
    print re.findall("'([a-zA-Z0-9,\s]*)'", eachproperty['onclick'])

rawDescription: Get values from onclick attribute using python bs4


code:
from django import forms
from django.shortcuts import render_to_response

class UploadForm(forms.Form):
    cpk = forms.CharField(max_length=256)
    f = forms.FileField()

def my_upload_view(request):
    if request.method == 'POST':
        form = UploadForm(request.POST, request.FILES)
        if form.is_valid():
            print "Got cpk",form.cleaned_data['cpk']
            print "Got file",request.FILES['f'].read()
    else:
        form = UploadForm()
    return render_to_response('upload.html', {'form':form})

rawDescription: Django 1.1.1 chokes on multipart/form-data


code:
for i in range(N_profiles_select):
    for t in time:
        if t < cldsat_timeafter and t > cldsat_timebefore:
            print t

rawDescription: Iterating over for loops

description: Iterating over for loops


code:
humidity = 99
temp = 100
print "[ {{ temperature: {0}, humidity: {1} }} ]".format(temp, humidity)

rawDescription: Python and variables

description: Python and variables


code:
parser.add_option("-e", help="e desc", dest="e_opt", action="store_true")
parser.add_option("-d", help="d desc", dest="d_opt", action="store_true")
(opts, args) = parser.parse_args()
if (parser.has_option("-e") and parser.has_option("-d")):
    print "Error!  Found both d and e options.  You can't do that!"
    sys.exit(1)

rawDescription: optparse(): Input validation

description: Optparse(): Input validation


code:
def flip(keyword,t,unflip=False):
    text = list(t)
    key = map(lambda x: ord(x)-ord('a'), keyword.lower())

    trange = reversed(range(len(text))) if unflip else range(len(text))

    for i in trange:
        keynum = i%len(keyword)
        actnum = key[keynum]%len(text)
        text[i],text[actnum] = text[actnum],text[i]
        print '%2d <-> %2d  '%(i,actnum),''.join(text)

    return ''.join(text)

x = flip("WONDERLAND","But I didn't think.")
print
flip("WONDERLAND",x,unflip=True)

rawDescription: Python Flip Cipher

description: Python Flip Cipher


code:
def add_details(request):
    response_data = {}
    if request.is_ajax():

        lists = request.POST.get('list')
        jd = json.dumps(lists)
        list = eval(json.loads(jd))
        print lists
        for list in lists:
            print list
        response_data['status'] = "success"

    else:
        response_data['status'] = "Error"

    return HttpResponse(json.dumps(response_data), content_type='application/json')

rawDescription: How to send the javascript list of dictionaries object to django ajax

description: Send the javascript list of dictionaries object to django ajax


code:
import csv
from itertools import groupby, ifilter
from operator import itemgetter

data = []

with open('in.csv') as csvfile:
    reader = csv.DictReader(csvfile)

    regions = []

    regions_dict = sorted(list(reader), key=itemgetter('Region'))
    for region_id, region_group in groupby(regions_dict, itemgetter('Region')):

        districts = []
        regions.append({'Region': region_id, 'Districts': districts})

        districts_dict = sorted(region_group, key=itemgetter('District'))
        for district_id, district_group in groupby(districts_dict, itemgetter('District')):
            districts.append({'District': district_id, 'Stores': list(district_group)})

print regions

rawDescription: Convert CSV to well-structured JSON in Python


code:
from config import settings
print settings

rawDescription: Python import similar to Django settings file


code:
from itertools import groupby
import datetime

SRC_DATA = [
    (1, datetime.datetime(2015, 06, 20, 1)),
    (2, datetime.datetime(2015, 06, 20, 4)),
    (3, datetime.datetime(2015, 06, 20, 5)),
    (4, datetime.datetime(2015, 06, 21, 1)),
    (5, datetime.datetime(2015, 06, 22, 1)),
    (6, datetime.datetime(2015, 06, 22, 4)),
]

for group_date, group in groupby(SRC_DATA, key=lambda X: X[1].date()):
    print "Group {}: {}".format(group_date, list(group))

rawDescription: Value difference comparison within a list in python


code:
context={}
for i,w in enumerate(tokens):
     if w.endswith("ing"):
        try:
           context[w]=tokens[i:i+10]  # this try...except is used to pass last 10 words whose context is less than 10 words.
        except: pass

fp=open('dataframes','w')   # save results in this file
fp.write('Word'+'\t\t'+'PreviousContext\n')
for word in context:
   fp.write(word+'\t\t'+' '.join(context[word])+'\n')

fp.close()
fp=open('dataframes','r')
for line in fp.readlines()[:10]: # first 10 lines of generated file
   print line

rawDescription: Extracting a word and its prior 10 word context to a dataframe in Python


code:
import matplotlib.pyplot as plt
from numpy.random import rand
from numpy import arange

val = 3-6*rand(5)    # the bar lengths
pos = arange(5)+.5    # the bar centers on the y axis
print pos

fig = plt.figure()
ax = fig.add_subplot(111)
ax.barh(pos,val, align='center',height=0.1)
ax.set_yticks(pos, ('Tom', 'Dick', 'Harry', 'Slim', 'Jim'))

ax.axvline(0,color='k',lw=3)   # poor man's zero level

ax.set_xlabel('Performance')
ax.set_title('horizontal bar chart using matplotlib')
ax.grid(True)
plt.show()

rawDescription: matplotlib chart - creating horizontal bar chart

description: Matplotlib chart - creating horizontal bar chart


code:
articles = Category.get(1).articles
print [x.title for x in articles]

rawDescription: flask-sqlalchemy three way association

description: Flask-sqlalchemy three way association


code:
import codecs

connectives=[u'കാരണം',u'അതുകൊണ്ട് ',u'പക്ഷേ',u'അതിനാല്',u'എങ്കിലും',u'എന്നാലും',u'എങ്കില്',u'എങ്കില്‍പോലും',
u'എന്നതുകൊണ്ട് ',u'എന്ന']

fr = codecs.open('i.txt', encoding='utf-8')
# fw = codecs.open('outputfile.txt', 'w')
for line in fr:
    line_data=line.split()
    for x, e in list(enumerate(line_data)):
        if e in connectives:
            line_data[x]='.\n'

    print " ".join(line_data).lstrip()

rawDescription: Find a word in the lines in file and split it into two lines


code:
class MetaA(type):
    def __new__(cls, name, bases, dct):
        print 'MetaA.__new__'
        return type(name, bases, dct)
    def __init__(cls, name, bases, dct):
        print 'MetaA.__init__'

class A(object):
    __metaclass__ = MetaA

rawDescription: What is the difference between type and type.__new__ in python?


code:
search_response = service.search().list(
  q="google",
  part="id",
  type="video",
  fields="items/id"
).execute()

videos = []

for search_result in search_response.get("items", []):
    videos.append("%s" % (search_result["id"]["videoId"]))

print "Videos:\n", "\n".join(videos), "\n"

rawDescription: Python - YouTube API v3 - How to get video ID only?


code:
import fileinput
import string

for line in fileinput.input("your_file.txt", inplace=True):
    print line.translate(None, string.digits),

rawDescription: Removing digits from a file

description: Removing digits from a file


code:
with open("new.txt") as file_2_read:
    text = file_2_read.read()

print text, len(text), type(text)    # తెలుగులిపి 30 <type 'str'>

utext = text.decode('utf-8')
print utext, len(utext), type(utext) # తెలుగులిపి 10 <type 'unicode>

rawDescription: How to understand unicode and utf-8

description: Understand unicode and utf-8


code:
def rewrite_write_plain(self, text, split=True):
    if self.state == self.expect_block_mapping_simple_value:
        text = '###' + text + '###'
        while self.column < 20:
            text = ' ' + text
            self.column += 1
    self._org_write_plain(text, split)

def rewrite_write_literal(self, text):
    if self.state == self.expect_block_mapping_simple_value:
        last_nl = False
        if text and text[-1] == '\n':
            last_nl = True
            text = text[:-1]
        text = '###' + text + '###'
        if False:
            extra_indent = ''
            while self.column < 15:
                text = ' ' + text
                extra_indent += ' '
                self.column += 1
            text = text.replace('\n', '\n' + extra_indent)
        if last_nl:
            text += '\n'
    self._org_write_literal(text)

def rewrite_write_single_quoted(self, text, split=True):
    if self.state == self.expect_block_mapping_simple_value:
        last_nl = False
        if text and text[-1] == u'\n':
            last_nl = True
            text = text[:-1]
        text = u'###' + text + u'###'
        if last_nl:
            text += u'\n'
    self.write_folded(text)

def rewrite_write_indicator(self, indicator, need_whitespace,
                    whitespace=False, indention=False):
    if indicator and indicator[0] in u"*&":
        indicator = u'###' + indicator + u'###'
        while self.column < 20:
            indicator = ' ' + indicator
            self.column += 1
    self._org_write_indicator(indicator, need_whitespace, whitespace,
                              indention)

dumper._org_write_plain = dumper.write_plain
dumper.write_plain = rewrite_write_plain
dumper._org_write_literal = dumper.write_literal
dumper.write_literal = rewrite_write_literal
dumper._org_write_single_quoted = dumper.write_single_quoted
dumper.write_single_quoted = rewrite_write_single_quoted
dumper._org_write_indicator = dumper.write_indicator
dumper.write_indicator = rewrite_write_indicator

print yaml.dump(data, Dumper=dumper, indent=4)

rawDescription: C# parse and change strings in yaml

description: C# parse and change strings in yaml


code:
from pprint import pprint
print pprint(result)
pprint(result)

rawDescription: Python accessing data in JSON object

description: Python accessing data in JSON object


code:
abominable_html = '''

services

Interesting stuff!

'''

from lxml.html import fromstring
doc = fromstring(abominable_html)

# These are equivalent, for demonstration purposes.
print doc.xpath('//table/tr/td/table/tr/text()')[0]
print doc.xpath('//table//table/tr')[0].text

rawDescription: Perl to Python , Match Pattern on a html output


code: print Ticket.objects.get_ticket_available_product('2011-12-29', 5, 1)

rawDescription: how to return the looped values in a method in python/django that no using array

description: Return the looped values in a method in python/django that no using array


code:
g = "ENGL 1301,preprofessional,MATH 2413,"
commas = g.count(",")  # I put this up here so it isn't called multiple times
if commas == 2:
    print "yay"
elif commas == 3:  # I used `elif` here since `commas` cannot equal 2 and 3
    print "nay"

rawDescription: File Inout Output multiple delimiters

description: File Inout Output multiple delimiters


code:
def foo(a=True):
    print a

foo()
foo.func_defaults
foo.func_defaults = (False,)
foo()

rawDescription: Command line argument as default to a function defined in another module


code:
def skip_if(condition):
    def decorator(task):
        print "decorating "  + str(task)
        task._skip_condition = condition
        return task
    return decorator

class MyFramework(object):
    def run(self):
        try:
            if self.task._skip_condition(self):
                print "Skipped task"
                return
        except AttributeError:
            print str(self.task) + " is not decorated"
            pass

        print "Starting task"
        try:
            self.task()
        except Exception as e:
            print "Failed task: " + str(e)
            raise
        else:
            print "Finished task"

rawDescription: Detect if method is decorated before invoking it


code:
import numpy as np

matrix = np.array(
         [[-2,  5,  3,  2],
          [ 9, -6,  5,  1],
          [ 3,  2,  7,  3],
          [-1,  8, -4,  8]])

diags = [matrix[::-1,:].diagonal(i) for i in range(-3,4)]
diags.extend(matrix.diagonal(i) for i in range(3,-4,-1))
print [n.tolist() for n in diags]

rawDescription: Get all the diagonals in a matrix/list of lists in Python


code:
for k in range(len(V)):
     for i, j in zip(x, y):
         if V[k]<i<V[k+1]:
             print j

rawDescription: Arrays in loops

description: Arrays in loops


code:
import random
import time
import collections
import cProfile

LIST_LEN = 14000

def timefunc(f):
    t = time.time()
    f()
    return time.time() - t

def random_string(length=3):
    """Return a random string of given length"""
    return "".join([chr(random.randint(65, 90)) for i in range(length)])

class Profiler:
    def __init__(self):
        self.original = [[random_string() for i in range(LIST_LEN)]
                            for j in range(4)]

    def old_method(self):
        self.ListVar = self.original[:]
        for b in range(len(self.ListVar)):
            self.list1 = []
            self.temp = []
            for n in range(len(self.ListVar[b])):
                if not self.ListVar[b][n] in self.temp:
                    self.list1.insert(n, self.ListVar[b][n] + '(' +    str(self.ListVar[b].count(self.ListVar[b][n])) + ')')
                    self.temp.insert(0, self.ListVar[b][n])

            self.ListVar[b] = list(self.list1)
        return self.ListVar

    def new_method(self):
        self.ListVar = self.original[:]
        for i, inner_lst in enumerate(self.ListVar):
            freq_dict = collections.defaultdict(int)
            # create frequency dictionary
            for e in inner_lst:
                freq_dict[e] += 1
            temp = set()
            ret = []
            for e in inner_lst:
                if e not in temp:
                    ret.append(e + '(' + str(freq_dict[e]) + ')')
                    temp.add(e)
            self.ListVar[i] = ret
        return self.ListVar

    def time_and_confirm(self):
        """
        Time the old and new methods, and confirm they return the same value
        """
        time_a = time.time()
        l1 = self.old_method()
        time_b = time.time()
        l2 = self.new_method()
        time_c = time.time()

        # confirm that the two are the same
        assert l1 == l2, "The old and new methods don't return the same value"

        return time_b - time_a, time_c - time_b

p = Profiler()
print p.time_and_confirm()

rawDescription: Python, using multiprocess is slower than not using it


code:
import re
findthe = re.compile(r"\b(the)\b")
print re.sub(findthe, replacement[1],re.sub(findthe, replacement[0],sentence, 1), 1)

rawDescription: Replacing each match with a different word


code:
class ResultsHandler(object):
    """Define base class to initialize location and tokens.
       Call subclass-specific post_init() if one is defined."""
    def __init__(self, s,locn,tokens):
        self.locn = locn
        self.tokens = tokens
        if hasattr(self, "post_init"):
            self.post_init()

class AHandler(ResultsHandler):
    """Handler for A expressions, which contain a numeric string."""
    def post_init(self):
        self.int_value = int(self.tokens[0])
        self.odd_even = ("EVEN","ODD")[self.int_value % 2]
    def doIt(self):
        print "An A-Type was found at %d with value %d, which is an %s number" % (
                self.locn, self.int_value, self.odd_even)

class BHandler(ResultsHandler):
    """Handler for B expressions, which contain an alphabetic string."""
    def post_init(self):
        self.string = self.tokens[0]
        self.vowels_count = sum(self.string.lower().count(c) for c in "aeiou")
    def doIt(self):
        print "A B-Type was found at %d with value %s, and contains %d vowels" % (
                self.locn, self.string, self.vowels_count)

# pass expression-specific handler classes as parse actions
A = Word(nums).setParseAction(AHandler)
B = Word(alphas).setParseAction(BHandler)
expr = OneOrMore(A | B)

# parse string and run handlers
result = expr.parseString("123 abc 456 7 d")
for handler in result:
    handler.doIt()

rawDescription: Distinguish matches in pyparsing

description: Distinguish matches in pyparsing


code:
for element in l:
    print id(element)

rawDescription: python how to change element in nested list

description: Python how to change element in nested list


code:
if data.get('items', {}).get('name', '').lower() == 'scarf':
    print 'There is Scarf'
else:
    print 'There is NO Scarf'

rawDescription: check if json parent value is empty

description: Check if json parent value is empty


code:
import time

adt = '2009-09-10T12:00:00'

def dttosecs(dt=adt):
    # string to seconds since the beginning
    date,tim = dt.split('T')
    y,m,d = date.split('-')
    h,mn,s = tim.split(':')
    y = int(y)
    m = int(m)
    d = int(d)
    h = int(h)
    mn = int(mn)
    s = min(59,int(float(s)+0.5)) # round to neatest second
    s = int(s)
    secs = time.mktime((y,m,d,h,mn,s,0,0,-1))
    return secs

def simpler(dt=adt):
  return time.mktime(time.strptime(dt, '%Y-%m-%dT%H:%M:%S'))

if __name__ == '__main__':
  print adt, dttosecs(), simpler()
assert dttosecs() == simpler()

rawDescription: Finding closest match in collection of strings representing numbers


code:
with open(inputfilea,"r") as infile:
    for line in infile:
        if line.startswith(" "):
            partial_in = line.strip().split()[1] #splits the line and gets 2nd word
            with open(inputfileb,"r") as infileb:
                for line2 in infileb:
                    if line2: #check if it's not empty line
                    #last strip added because of DFF lines has backslashes infront of them
                        if partial_in == line2.strip().split()[1].strip("\\"):
                            print partial_in, line2,
                            #since you wanted to write to a file, instead of print
                            #f_out.write(line2)

rawDescription: Sorting text files based on elements from another file in python


code:
from xml.etree import ElementTree as etree

xml = """
<TrdCaptRpt RptID="10000001" TransTyp="0">
    <RptSide Side="1" Txt1="XXXXX">
        <Pty ID="XXXXX" R="1"/>
    </RptSide>
</TrdCaptRpt>
"""

root = etree.fromstring(xml)
rpt_side = root.find('RptSide')
rpt_side.set('Txt1', 'Updated')
pty = rpt_side.find('Pty')
pty.set('ID', 'Updated')
print etree.tostring(root)

rawDescription: Updating XML Elements and Attribute values using Python etree