code:
from collections import defaultdict

ri = [('123', 'abc', 'def'),('123', 'efg', 'hij'),('456', 'klm','nop')]
response_items = defaultdict(list)
for r in ri:
    response_items[r[0]].append(r[1:])
print response_items

rawDescription: List of tuples, first tuple as key, grouping by key into into a dictionary


code:
def nearest_date_for_weekday(weekday):
    today = datetime.date.today()
    return today + datetime.timedelta(days=weekday - today.weekday())

parsed_date = datetime.datetime.combine(
    nearest_date_for_weekday(int(tow_str[0])),
    datetime.datetime.strptime(tow_str[1:], '%H%M').time())

print parsed_date.strftime('%w%H%M')

rawDescription: Python custom datetime(?) format handling


code:
from collections import namedtuple
from json import JSONEncoder

class MyEncoder(JSONEncoder):

    def _iterencode(self, obj, markers=None):
        if isinstance(obj, tuple) and hasattr(obj, '_asdict'):
            gen = self._iterencode_dict(obj._asdict(), markers)
        else:
            gen = JSONEncoder._iterencode(self, obj, markers)
        for chunk in gen:
            yield chunk

class foobar(namedtuple('f', 'foo, bar')):
    pass

enc = MyEncoder()
for obj in (foobar('a', 1), ('a', 1), {'outer': foobar('x', 'y')}):
    print enc.encode(obj)

{"foo": "a", "bar": 1}
["a", 1]
{"outer": {"foo": "x", "bar": "y"}}

rawDescription: Serializing a Python namedtuple to json

description: Serializing a Python namedtuple to json


code:
from importing_modules import *
#Now you can call the methods of that module directly
print sqrt(25) #Now we can call sqrt() directly in place of math.sqrt() or importing_modules.math.sqrt().

rawDescription: Python import modules in another file

description: Python import modules in another file


code:
from threading import *
import time

class MyThread(Thread):
    def __init__(self):
        self.running = True
        Thread.__init__(self)

    def stop(self):
        self.running = False

    def run(self):
        while self.running:
            print 'Doing something'
            time.sleep(1.0/60)

thread = MyThread()
thread.start()

rawDescription: Exit an imported module without exiting the main program - Python


code:
import collections

with open("data") as f:
        lines=[line.strip().split() for line in f]

combinations=collections.Counter(
        ", ".join(lines[0][i]
                for i in xrange(len(combination))
                        if combination[i] != '0')
                for combination in lines[1:])

for key, value in combinations.iteritems():
        print value, '\t', key

rawDescription: Computing the combinations for presence or absence of a factor in a table


code:
import re
with open("input") as f:
    for line in f:
        mo=re.match(r'.*?(?<![$\d])(\d+).*?(tons|feet|lbs)', line)
        if mo: print mo.group(1), mo.group(2)

rawDescription: Matching Regex in Python

description: Matching Regex in Python


code:
for price in soup.select("div.product-price > div:nth-of-type(1)"):
    print price.get_text(strip=True)

rawDescription: BeautifulSoup: how to get children of div tab


code:
#!/usr/bin/env python
import re

for n in xrange(0, 51):
  ones = '1' * n
  matches = re.match(r'^1?$|^(11+?)\1+$', ones)
  if matches:
    div1 = n if matches.group(1) is None else len(matches.group(1))
    div2 = 0 if div1 is 0 else len(ones)/div1
    print "[{0:2}]:{1:2} * {2:2} = '{3}'".format(n, div1, div2, ones)

rawDescription: Regex matching

description: Regex matching


code:
a = NoProps(3)
b = NoProps(4)
c = Props(5)

print a.spam, b.spam, c.spam
# Prints 3, 4, (something between 5 and 25)

rawDescription: Turning an attribute into a property on an object-by-object basis?


code:
var_names = "abcdefghijklmnopqrstuvwxyz"
named_lookup = dict(zip(var_names, list_of_input))

print named_lookup["c"]

rawDescription: Taking dynamic number of inputs from the same line


code:
print '<?xml version="1.0"?>'
print "<dcenters>"
for line in s.splitlines():
    type, fields = line.split(":")
    params = fields.split(",")
    code = params[0].split("=")[1].strip()
    print '<%s code="%s">' % (type, code)
    for p in params[1:]:
        ptype, pvalue = p.strip().split("=")
        print '<%s> %s </%s>' % (ptype, pvalue, ptype)
    print '</%s>' % type
print "</dcenters>"

rawDescription: Please help me with this program to parse a file into an XML file


code:
import numpy

class NN:

    def __init__(self, sl):

        #sl = number of units (not counting bias unit) in layer l
        self.sl = sl
        self.layers = len(sl)

        #Create weights
        self.weights = []
        for idx in range(1, self.layers):
            self.weights.append(numpy.matrix(numpy.random.rand(self.sl[idx-1]+1, self.sl[idx]))/5)

        self.cost = []

    def update(self, input):

        if input.shape[1] != self.sl[0]:
            raise ValueError, 'The first layer must have a node for every feature'

        self.z = []
        self.a = []

        #Input activations.  Expected inputs as numpy matrix (Examples x Featrues)
        self.a.append(numpy.hstack((numpy.ones((input.shape[0], 1)), input)))#Set inputs ai + bias unit

        #Hidden activations
        for weight in self.weights:
            self.z.append(self.a[-1]*weight)
            self.a.append(numpy.hstack((numpy.ones((self.z[-1].shape[0], 1)), 1/(1+numpy.exp(-self.z[-1]))))) #sigmoid

        #Output activation
        self.a[-1] = self.z[-1] #Not logistic regression thus no sigmoid function
        del self.z[-1]

    def backPropagate(self, targets, lamda):

        m = float(targets.shape[0]) #m is number of examples

        #Calculate cost
        Cost = -1/m*sum(numpy.power(self.a[-1] - targets, 2))
        for weight in self.weights:
            Cost = Cost + lamda/(2*m)*numpy.power(weight[1:, :], 2).sum()
        self.cost.append(abs(float(Cost)))

        #Calculate error for each layer
        delta = []
        delta.append(self.a[-1] - targets)
        for idx in range(1, self.layers-1): #No delta for the input layer because it is the input
            weight = self.weights[-idx][1:, :] #Ignore bias unit
            dsigmoid = numpy.multiply(self.a[-(idx+1)][:,1:], 1-self.a[-(idx+1)][:,1:]) #dsigmoid is a(l).*(1-a(l))
            delta.append(numpy.multiply(delta[-1]*weight.T, dsigmoid)) #Ignore Regularization

        Delta = []
        for idx in range(self.layers-1):
            Delta.append(self.a[idx].T*delta[-(idx+1)])

        self.weight_gradient = []
        for idx in range(len(Delta)):
            self.weight_gradient.append(numpy.nan_to_num(1/m*Delta[idx] + numpy.vstack((numpy.zeros((1, self.weights[idx].shape[1])), lamda/m*self.weights[idx][1:, :]))))

    def train(self, input, targets, alpha, lamda, iterations = 1000):

        #alpha: learning rate
        #lamda: regularization term

        for i in range(iterations):
            self.update(input)
            self.backPropagate(targets, lamda)
            self.weights = [self.weights[idx] - alpha*self.weight_gradient[idx] for idx in range(len(self.weights))]

    def autoparam(self, data, alpha = [0.001, 0.003, 0.01, 0.03, 0.1, 0.3], lamda = [0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1, 3, 10]):

        #data: numpy matrix with targets in last column
        #alpha: learning rate
        #lamda: regularization term

        #Create training, cross validation, and test sets
        while 1:
            try:
                numpy.seterr(invalid = 'raise')
                numpy.random.shuffle(data) #Shuffle data
                training_set = data[0:data.shape[0]/10*6, 0:-1]
                self.ntraining_set = (training_set-training_set.mean(axis=0))/training_set.std(axis=0)
                self.training_tgt = numpy.matrix(data[0:data.shape[0]/10*6, -1]).T

                cv_set = data[data.shape[0]/10*6:data.shape[0]/10*8, 0:-1]
                self.ncv_set = (cv_set-cv_set.mean(axis=0))/cv_set.std(axis=0)
                self.cv_tgt = numpy.matrix(data[data.shape[0]/10*6:data.shape[0]/10*8, -1]).T

                test_set = data[data.shape[0]/10*8:, 0:-1]
                self.ntest_set = (test_set-test_set.mean(axis=0))/test_set.std(axis=0)
                self.test_tgt = numpy.matrix(data[data.shape[0]/10*8:, -1]).T

                break

            except FloatingPointError:
                pass

        numpy.seterr(invalid = 'warn')
        cost = 999999
        for i in alpha:
            for j in lamda:
                self.__init__(self.sl)
                self.train(self.ntraining_set, self.training_tgt, i, j, 2000)
                current_cost = 1/float(cv_set.shape[0])*sum(numpy.square(self.predict(self.ncv_set) - self.cv_tgt)).tolist()[0][0]
                print current_cost
                if current_cost < cost:
                    cost = current_cost
                    self.learning_rate = i
                    self.regularization = j
        self.__init__(self.sl)

    def predict(self, input):

        self.update(input)
        return self.a[-1]

rawDescription: Trying to write my own Neural Network in Python


code:
# I named your list 'sample' here
for result in sequences(CodD, sample):
    print result

rawDescription: Python list slicing sequence genetic code


code:
import datetime
import time

feed = client.YouTubeQuery(query)
for entry in feed.entry:
    _tmp = time.strptime(entry.published.text, '%Y-%m-%dT%H:%M:%S.000Z')
    ptime = datetime.datetime(*_tmp[:6])
    print ptime
    print ptime.time()

rawDescription: Fetch the exact time a video was uploaded


code:
from Tkinter import *
import tkMessageBox
import threading
import time
root = Tk()
root.geometry("450x250+300+300")
root.title("Raspberry PI Test")
print dir(root)
def myloop():
    def run():
        count = 0
        while (count < 500) and root.wm_state():
           print 'The count is:', count
           count = count + 1
           time.sleep(1)

        root.after(1,count_complete)
    thread = threading.Thread(target=run)
    thread.start()
def count_complete():
    print "DONE COUNTING!! ... I am now back in the main thread"
def mymessage():
    tkMessageBox.showinfo(title="Alert", message="Hello World!")

buttonLoop = Button(root, text="Start Loop", command=myloop)
buttonLoop.place(x=5, y=15)

buttonMessage = Button(root, text="Start Loop", command=mymessage)
buttonMessage.place(x=85, y=15)

root.mainloop()

rawDescription: Python - While Loop Locks Application

description: Python - While Loop Locks Application


code:
wanted = "fish chips steak"
cnt = Counter()
words = re.findall('\w+', open('1976.03.txt').read().lower())
for word in words:
    if word in wanted:
        cnt[word] += 1
print cnt

rawDescription: How can I find frequency of a specific word from document in python?

description: Find frequency of a specific word from document in python


code:
val = "I was with my friend"

print val.replace("was ", "")

rawDescription: How to print a text without substring in Python

description: Print a text without substring in Python


code:
import sys
sys.stdout = open('output.txt', 'w')
print 'Hello World'

rawDescription: Get current console output in python

description: Get current console output in python


code:
for filename in set(newlist) - set(oldlist):
    print 'New file: ', filename

rawDescription: Python programming with FTP and lists

description: Python programming with FTP and lists


code:
def EntryToFieldMatch(Entry, Fields):
    valid = []
    st = set(Fields)
    for word in Entry:
        spl = word.split(None,1)
        if spl[0] in st:
            valid.append(word)
            st.remove(spl[0])
    print valid
    print "-"*50
    print st

rawDescription: How to compare 2 list where string matches element in alternate list

description: Compare 2 list where string matches element in alternate list


code:
class A:
    def __init__(self):
        setattr(self, 'prepare_orig', getattr(self,'prepare'))
        setattr(self, 'prepare', getattr(self,'prepare_wrapper'))

    def prepare_wrapper(self,*args,**kwargs):
        def prepare_thread(*args,**kwargs):
            try:
                self.prepare_orig(*args,**kwargs)
            except:
                print "Unexpected error:", sys.exc_info()[0]
        t = threading.Thread(target=prepare_thread, args=args, kwargs=kwargs)
        t.start()

    def prepare(self):
        pass

rawDescription: Python: inject new functions into an instance of class?


code:
a = np.arange(size_a)[::-1]
b = np.random.randint(0, size_a, size_b)

ja = np.take(a, np.searchsorted(a, b, side='right', sorter=a)-1)

#Compare to digitize
if ~np.allclose(ja,np.digitize(b,a)):
    print 'Comparison failed'

timing_digitize[num_a,num_b] = timeit.timeit('np.digitize(b,a)',
                      'import numpy as np; from __main__ import a, b',
                      number=3)
timing_searchsorted[num_a,num_b] = timeit.timeit('np.take(a, np.searchsorted(a, b, side="right", sorter=a)-1)',
                      'import numpy as np; from __main__ import a, b',
                      number=3)

rawDescription: Comparing two numpy arrays of different length


code:
ttaken = time.time()
for i in range(100000):
    d = Decimal(random.uniform(-3, +3))
    to_int_exp1(d)
ttaken = time.time() - ttaken
print ttaken

rawDescription: How to Convert pythons Decimal() type into an INT and exponent


code:
def findSum(nbr_list):
    total = 0
    for nbr in nbr_list:
        total += nbr  # Same as 'total = total + nbr'
    print 'Total: ', total
nbr_list = [1,2,3,4,100]
findSum(nbr_list)

rawDescription: I can't seem to get this function to add the list together


code:
X1 = np.zeros((n,m,4),dtype='u1')
np.lib.stride_tricks.as_strided(X1, shape=X.shape, strides=X1.strides)[:] = X
print X1

rawDescription: Change stride of numpy array (altering data)


code:
import lxml.etree
from StringIO import StringIO

def ns_join(element, tag, namespace=None):
    '''Joins the namespace and tag together, and
    returns the fully qualified name.
    @param element - The lxml.etree._Element you're searching
    @param tag - The tag you're joining
    @param namespace - (optional) The Namespace shortname default is None'''

    return '{%s}%s' % (element.nsmap[namespace], tag)

def parse_car(element):
    '''Parse a car element, This will return a dictionary containing
    brand_name, maxspeed_value, and maxspeed_unit'''

    maxspeed = element.find(ns_join(element,'maxspeed'))
    return {
        'brand_name' : element.findtext(ns_join(element,'brandName')),
        'maxspeed_value' : maxspeed.findtext(ns_join(maxspeed,'value')),
        'maxspeed_unit' : maxspeed.find(ns_join(maxspeed, 'unit')).attrib['abbrev']
        }

#Create the StringIO object to feed to the parser.
XML = StringIO('''
<Reports>
    <Car xmlns="http://example.com/vocab/xml/cars#">
        <dateStarted>2011-02-05</dateStarted>
        <dateSold>2011-02-13</dateSold>
        <name type="http://example.com/codes/bmw#" abbrev="X6" value="BMW X6" >BMW X6</name>
        <brandName type="http://example.com/codes/bmw#" abbrev="BMW" value="BMW" >BMW</brandName>
        <maxspeed>
            <value>250</value>
            <unit type="http://example.com/codes/units#" value="miles per hour" abbrev="mph" />
        </maxspeed>
        <route type="http://example.com/codes/routes#" abbrev="HW" value="Highway" >Highway</route>
        <power>
            <value>180</value>
            <unit type="http://example.com/codes/units#" value="powerhorse" abbrev="ph" />
        </power>
        <frequency type="http://example.com/codes/frequency#" value="daily" >Daily</frequency>
    </Car>
</Reports>
''')

#Get the root element object of the xml
car_root_element = lxml.etree.parse(XML).getroot()

# For each 'Car' tag in the root element,
# we want to parse it and save the list as cars
cars = [ parse_car(element)
    for element in car_root_element.iter() if element.tag.endswith('Car')]

print cars

rawDescription: Get attribute of complex element using lxml


code:
import os, multiprocessing
p = os.popen("ps aux|awk 'NR > 0 { s +=$3 }; END {print s}'").read().rstrip("\n")
print "Cores:", multiprocessing.cpu_count(), '\nCPU Load:', p, "%"

rawDescription: Python: need to use bash one-liner

description: Python: need to use bash one-liner


code:
for word in doc.Words:
    print word.Style

rawDescription: Extract headings from a MS Word document in Python


code: print lxml.html.tostring(hxs)

rawDescription: crawling imdb database using python and lxml

description: Crawling imdb database using python and lxml


code:
import re

s = '* TODO [#A] Do something important :Tag1:Tag2:Tag3:'
m = re.search(r'(.*?)\s*:(.*):$', s)

print "\n".join(m.groups())

rawDescription: Extracting the tags from an org mode line using Python regex


code:
import pprint

print pprint.pprint(group_similar([
    'the', 'be', 'to', 'of', 'and', 'a', 'in', 'that', 'have',
    'I', 'it', 'for', 'not', 'on', 'with', 'he', 'as', 'you',
    'do', 'at', 'this', 'but', 'his', 'by', 'from', 'they', 'we',
    'say', 'her', 'she', 'or', 'an', 'will', 'my', 'one', 'all',
    'would', 'there', 'their', 'what', 'so', 'up', 'out', 'if',
    'about', 'who', 'get', 'which', 'go', 'me', 'when', 'make',
    'can', 'like', 'time', 'no', 'just', 'him', 'know', 'take',
    'people', 'into', 'year', 'your', 'good', 'some', 'could',
    'them', 'see', 'other', 'than', 'then', 'now', 'look',
    'only', 'come', 'its', 'over', 'think', 'also', 'back',
    'after', 'use', 'two', 'how', 'our', 'work', 'first', 'well',
    'way', 'even', 'new', 'want', 'because', 'any', 'these',
    'give', 'day', 'most', 'us'
]), width=120)

rawDescription: Fuzzy Group By, Grouping Similar Words

description: Fuzzy Group By, Grouping Similar Words


code:
from django.core.validators import URLValidator
from django.core.exceptions import ValidationError

def save_url(request, url):
    url_validator = URLValidator(verify_exists=False)
    try:
        url_validator(url)
    except ValidationError:
        print 'Not a valid URL'

rawDescription: allowing urls as parameters

description: Allowing urls as parameters


code:
with open( file.txt, 'r') as f:
     for i, line in enumerate(f.readlines(), 0):
          if i >= 6:
              print line

rawDescription: How to start reading from certain line?

description: Start reading from certain line


code:
s = False
i = 0
while (s == False) and (i < 100):
    i = i + 10
    s = False
    print 'Hello' , i

rawDescription: or operator not working in python while loop

description: Or operator not working in python while loop


code:
from PySide import QtCore, QtGui

class Communicate(QtCore.QObject):
    signal = QtCore.Signal(str)

connector=Communicate()

class label(QtGui.QLabel):
    def __init__(self, parent=None):
        super(label, self).__init__(parent)
        connector.signal.connect(self.labelFunction)
        self.show()

    def labelFunction(self, arg=None):
        print '...labelFunction: arg =', arg

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

    def click(self):
        connector.signal.emit("Hello World")

lbl = label()
btn = button()

rawDescription: Signals And Slots: When simple example is not working


code:
class Settings(dict):
    """Singleton settings class"""

    settings = {
                   'SectionA': {'a': 1, 'b': 2, 'c': 3},
                   'SectionB': {'d': 4, 'e': 5, 'f': 6}
               }

    def __getitem__(self, key): return self.settings[key]
    def __setitem__(self, key, item): self.settings[key] = item
    def keys(self): return self.settings.keys()

s = Settings()

for key in s.keys():
    print key
    print s[key]

rawDescription: Python dictionary class, 'KeyError'

description: Python dictionary class, 'KeyError'


code:
from xml.dom import minidom
from xml.dom.minidom import parse

xmldoc = minidom.parse('sample.xml')

# find the name element, if found return a list, get the first element
name_element = xmldoc.getElementsByTagName("name")[0]

# this will be a text node that contains the actual text
text_node = name_element.childNodes[0]

# get text
print text_node.data

rawDescription: Parsing xml using python minidom

description: Parsing xml using python minidom


code:
# prints: today, runner_up, blabla, oooo
for tag in get_tags(html):
    print tag

rawDescription: Split tags in python

description: Split tags in python


code:
[submission] = submissions.get_new(limit=1)
print submission.url

rawDescription: get URL of any submission for subreddits

description: Get URL of any submission for subreddits


code:
if all((3, True, "abc")):
    print "Yes!"

rawDescription: How to do "if-for" statement in python?

description: Do "if-for" statement in python


code:
def my_sort(array_to_sort):
    sort = False
    number_of_items = len(array_to_sort)
    print "sorted array: ", array_to_sort

    for i in range(0, number_of_items-1):
        if array_to_sort[i] > array_to_sort[i+1]:
            tmp = array_to_sort[i]
            array_to_sort[i] = array_to_sort[i+1]
            array_to_sort[i+1] = tmp
            sort = True
    if sort:
        return my_sort(array_to_sort)
    else:
        return array_to_sort

if __name__ == '__main__':
    # main()

    arr = [4,5,7,3,2,1]
    test = my_sort(arr)
    print (test)

rawDescription: Lite empty on return from function in python


code: print "hello World!"

rawDescription: Conquering Complexity, Eckel on Java and Python and Chunk Theory


code:
def run(self):
    while 1:
        self.parent.anApp.execute()  # Do you really want this inside the infinite loop?

        if self.parent.anApp.appX.listen.is_alive():
             print 'App x is running'
        else:
             print 'App x is not running'

rawDescription: Get the thread state stated from another class


code:
tree = ET.parse('command_details.xml')
root = tree.getroot()

for child in root:

    if child.attrib['major'] == str(hex(int(major_bits[::-1], 2))) and child.attrib['minor'] == str(hex(int(minor_bits[::-1], 2))):
        command_name = str(child.attrib['name'])
        for grandchild in child.getchildren():
            print str(grandchild.attrib['bytes'])
            print str(grandchild.attrib['descrip'])

rawDescription: Iterate through XML child of a child tags in Python


code:
class ImageFile:
    def __init__(self, filename, identifier, total_chunks, chunk_size, total_size):
        self.bucket_name = os.environ.get('BUCKET_NAME', app_identity.get_default_gcs_bucket_name())
        self.original_filename = filename
        self.filename = '/' + self.bucket_name + '/' + self.original_filename
        self.identifier = identifier
        self.total_chunks = total_chunks
        self.chunk_size = chunk_size
        self.total_size = total_size
        self.stat = None
        self.chunks = []
        self.load_stat()
        self.load_chunks(identifier, total_chunks)

    def load_stat(self):
        try:
            self.stat = gcs.stat(self.filename)
        except gcs.NotFoundError:
            self.stat = None

    def load_chunks(self, identifier, number_of_chunks):
        for n in range(1, number_of_chunks + 1):
            self.chunks.append(Chunk(self.bucket_name, identifier, n))

    def exists(self):
        return not not self.stat

    def content_type(self):
        if self.filename.lower().endswith('.jpg'):
            return 'image/jpeg'
        elif self.filename.lower().endswith('.jpeg'):
            return 'image/jpeg'
        elif self.filename.lower().endswith('.png'):
            return 'image/png'
        elif self.filename.lower().endswith('.git'):
            return 'image/gif'
        else:
            return 'binary/octet-stream'

    def ready(self):
        return self.exists() and self.stat.st_size == self.total_size

    def ready_chunks(self):
        for c in self.chunks:
            if not c.exists():
                return False
        return True

    def delete_chunks(self):
        for c in self.chunks:
            c.delete()

    def ready_to_build(self):
        return not self.ready() and self.ready_chunks()

    def write_chunk(self, chunk_number, current_chunk_size, data):
        chunk = self.chunks[int(chunk_number) - 1]
        chunk.write(current_chunk_size, data)

    def build(self):
        try:
            log.info('File \'' + self.filename + '\': assembling chunks.')
            write_retry_params = gcs.RetryParams(backoff_factor=1.1)
            gcs_file = gcs.open(self.filename,
                                'w',
                                content_type=self.content_type(),
                                options={'x-goog-meta-identifier': self.identifier},
                                retry_params=write_retry_params)
            for c in self.chunks:
                log.info('Writing chunk ' + str(c.chunk_number) + ' of ' + str(self.total_chunks))
                c.write_on(gcs_file)
            gcs_file.close()
        except Exception, e:
            log.error('File \'' + self.filename + '\': Error during assembly - ' + e.message)
        else:
            self.delete_chunks()
            key = blobstore.create_gs_key('/gs' + self.filename)
            url = images.get_serving_url(key)
            info = ImageInfo(name=self.original_filename, url=url)
            info.put()
            return info

rawDescription: Files uploaded using ng-flow to upload to gae blobstore always named 'blob'


code:
def pulsing2(callback=None):

    progressbar.set_inverted(True)
    fraction = 0.0
    while True:
        fraction += 0.2
        gobject.idle_add(progressbar.set_fraction, fraction)
        print fraction # THIS WORKS CORRECTLY EVERY TIME, 5 TIMES IN TOTAL (1/0.2)
        time.sleep(1)
        if fraction == 1:
            break
    gobject.idle_add(builder.get_object('Azioni_Window').show)

rawDescription: Thread.join() doesn't let the thread itself to update the GUI


code:
import re

line_in = "gi|393925858|gb|AGTA02071966.1|    739  .   G   A   121.2   PASS    NS=74;AN=2;DP=8448  GT:DP:GQ:EC:SG  0/1:262:99:116:R    1:32:93:32:A    0:87:42:72:R    .:0:0:0:.   .:0:0:0:.   0/1:222:99:113:R"

thing_pattern = r'(:\d+:\d+:\d+:[.|\w])'
line_parts = line_in.split()

output_parts = []

for part in line_parts:

    output_part = part

    if re.match('0'+thing_pattern, part):
        output_part = part.replace('0', '0/0')

    elif re.match('1'+thing_pattern, part):
        output_part = part.replace('1', '1/1')

    output_parts.append(output_part)

line_out = ' '.join(output_parts)
print line_out

rawDescription: split the elements in the line, how to rejoin them with the updated value

description: Split the elements in the line, how to rejoin them with the updated value


code:
prices = {'banana': 4, 'apple': 2, 'orange': 1.5, 'pear': 3}
stock = {'banana': 6, 'apple': 0, 'orange': 32, 'pear': 15}

for key in prices:
    print key
    print "price: %s" % prices[key]
    try:
        print "stock: %s" % stock[key]
    except KeyError:
        print "stock: KeyError"

rawDescription: how to print keys and values of a dictionary in python

description: Print keys and values of a dictionary in python


code:
#!/usr/bin/env python
import os, sys

pid = os.fork()
if pid:
    # we are the parent
    background_process.start()
    os.waitpid(pid, 0) # make sure the child process gets cleaned up
else:
    # we are the child
    gui_app.start()
    sys.exit(0)

print "parent: got it; text =", txt

rawDescription: Executable path to Mac App

description: Executable path to Mac App


code:
import re

regexp = re.compile(expression)
with open('words.txt', 'r') as f:

    for word in f.read().split(' \t\n'):
        if regexp.match(word):
            print word

rawDescription: Code review - Simple Mnemonic generator in python


code:
word_count = {'The' : 2, 'quick' : 8, 'brown' : 4, 'fox' : 1 }
from operator import itemgetter
for word, count in reversed(sorted(word_count.iteritems(), key=itemgetter(1))):
    print word, count

rawDescription: Python: sort dict by values, print key and value


code:
text = 'a, b, c'
text = text.replace(',', '\n')
print text

rawDescription: Python and Line Breaks

description: Python and Line Breaks


code:
from functools import partial

def callback(msg):
    print msg

funcList=[partial(callback, m) for m in ('do', 're', 'mi')]
for f in funcList:
    f()

rawDescription: Scope of python lambda functions and their parameters


code:
a = re.finditer('<a.+?question-hyperlink">(.+?)</a>', html)
for m in a:
    print m.group(1)

rawDescription: Python: Find a Sentence between some website-tags using regex


code:
s='1.25,3.455,3.7'
print sum(float(num) for num in s.split(","))

rawDescription: Sum of all numbers inside a string

description: Sum of all numbers inside a string


code:
numbers = [int(x) for x in s.strip('[]').split(':')]
print numbers[0] - numbers[1]

rawDescription: clear and comprehensible way to calculate the string [12:3]

description: Clear and comprehensible way to calculate the string [12:3]


code:
num_headers = 5
with open("input.csv", 'r') as file_in, open("output.csv", 'w') as file_out:
    for i, line in enumerate(file_in):
        if len(line.split(",")) == num_headers:
            file_out.write(line)
        else:
            print "line %d is malformed" % i

rawDescription: eliminate malformed records from a large .csv file

description: Eliminate malformed records from a large .csv file


code:
def get_all_substrings(input_string):
  length = len(input_string)
  return [input_string[i:j+1] for i in xrange(length) for j in xrange(i,length)]

print get_all_substrings('abcde')

rawDescription: How To Get All The Contiguous Substrings Of A String In Python?


code:
x = 'hello'
print x
print repr(x)
print '{!r}'.format(x)

rawDescription: How do I format a string value as an actual string

description: Format a string value as an actual string


code:
class Base(object):
    cc = ''
    def __init__(self):
        print set(dir(self)) - set(dir(Base))
        # print SubClass' new attribues' names ('aa' and 'bb' for example)

class SubClass(Base):
    def __init__(self):
        self.dd = ''
        super(SubClass, self).__init__()
    aa = ''
    bb = ''

foo = SubClass()
# set(['aa', 'dd', 'bb'])

rawDescription: Python: how to get subclass's new attributes name in base class's method?


code:
required_part = SoupStrainer('table', class_="results")
for page in xrange(1, num_pages + 1):
    url = base_url.format(page=page, zipcode=zipcode)
    soup = BeautifulSoup(urllib2.urlopen(url), parse_only=required_part)

    print "Page Number: %s" % page
    for h2 in soup.find_all('h2'):
        print h2.text

rawDescription: Trouble printing all items from a list in python


code:
from bs4 import Tag

d = {1: [1,2,3,4,5], 2: [2,3,4]}

root = Tag(name='html')
for key, values in d.iteritems():
    page = Tag(name='page')
    src = Tag(name='src')
    src.string = str(key)
    page.append(src)

    links = Tag(name='links')
    for value in values:
        link = Tag(name='link')
        link.string = str(value)
        links.append(link)

    page.append(links)
    root.append(page)

print root.prettify()

rawDescription: simplest way to generate xml in python

description: Simplest way to generate xml in python


code:
mylist1 = [1, 2, 3, 4, 5]
mylist2=["hi","how","are","you","doing"]
for index,value in enumerate(mylist1):
    mylist1[index]="{} {}".format(str(value),mylist2[index])
print mylist1

rawDescription: Python: Add strings to a list full of integers


code:
def generate_random_data():
   hex1 = '%012X' % random.randint(0,10000000000)
   hex2 = '%012X' % random.randint(0,10000000000)
   flt = float(random.randint(0,100000)) # change this value as required
   int1 = random.randint(0,1000000)
   int2 = random.randint(0,1000000)
   int3 = random.randint(0,1000000)
   int4 = random.randint(0,1000000)

   s = '%s %s %.1f %d %d %d %d' % (hex1.lower(), \
                                   hex2.lower(), \
                                   flt, \
                                   int1, \
                                   int2, \
                                   int3, \
                                   int4)
   return s

def generate_random_data_strings(n):
    for i in xrange(n):
        print generate_random_data()

rawDescription: Increase the data size

description: Increase the data size


code:
from optparse import OptionParser

p = OptionParser()
p.add_option('-f', '--filename', dest='input', action='store', type='string')
options, args = p.parse_args()

print options
print args

rawDescription: Capturing filesystem globs using optparse


code:
import Tkinter
root = Tkinter.Tk()
def callback(event):
    print "clicked at", event.x, event.y

root.bind("<Button-1>", callback)
root.mainloop()

rawDescription: Hide function not called

description: Hide function not called


code:
latest = datetime(2011, 4, 22, 15, 27, 28)
first = datetime(2010, 3, 17, 21, 0, 0)
for d in spanning_months(first, latest):
    print d

rawDescription: Get the difference between dates in the form of list years and months


code:
from itertools import groupby, islice, cycle

# The following is from the itertools recipes
# but it has had its splot removed for simplicity
def roundrobin(iterables):
    "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
    # Recipe credited to George Sakkis
    pending = len(iterables)
    nexts = cycle(iter(it).next for it in iterables)
    while pending:
        try:
            for next in nexts:
                yield next()
        except StopIteration:
            pending -= 1
            nexts = cycle(islice(nexts, pending))

test_list = ['categorya.1.file','categorya.2.file','categoryb.1.file','categoryc.1.file']
new_list = list(roundrobin(list(list(l) for (c, l) in groupby(test_list, lambda v: v.split('.')[0]))))
print new_list

rawDescription: Python Rearrange order of a list according to element contents


code:
#!/usr/bin/env python

#
# Does the same thing
#
mylist = []
mylist.append(1)
mylist.append(2)

print mylist    # prints [1, 2]

rawDescription: adding to a list defined in a function

description: Adding to a list defined in a function


code:
import string

def xsort(L,xlat):
    def xform(s):
        return int(str(s).translate(string.maketrans(xlat,'9876543210')))
    return sorted(L,key=xform)

print xsort([20,50,11,121],'9231476058')
print xsort([319,311,198,420],'1948327605')

rawDescription: Sort List of Numbers according to Custom Number Sequence


code:
from itertools import chain
nested_list = [['a', 'b'], ['c', 'd']]
for elem in chain.from_iterable(nested_list):
    print elem,

rawDescription: python iterator over multiple lists

description: Python iterator over multiple lists


code:
node = lxml.html.fromstring(r.text)
self.fingerprint['Title'] = node.cssselect('.document-title div')[0].text
print '@@####', self.fingerprint['Title']

rawDescription: lxml not parsing unicode properly for HTML

description: Lxml not parsing unicode properly for HTML


code:
df = zz.pivot_table(index='date', columns='feccandid', fill_value='0', aggfunc=np.mean)
df.columns = ['_'.join(col) for col in df.columns.values]
print df

rawDescription: Pandas: reshape data frame

description: Pandas: reshape data frame


code:
def dostuff(command):
    print "Write the numbers to %s" (command)
    a = int(raw_input("First number: "))
    b = int(raw_input("Second number: "))
    if command == 'add':
        return a + b
    elif command == 'subtract':
        return a - b
    elif command == 'multiply':
        return a * b
    elif command == 'divide':
        return a / b
    else:
        return 'not a valid command'

# Choose what to do
command = raw_input("Write what you want do do: add, subtract, multiply or divide")
print dostuff(command)

rawDescription: Making selections linked to definitions in python


code:
def profile_main():
    # This is the main function for profiling
    # We've renamed our original main() above to real_main()
    import cProfile, pstats
    prof = cProfile.Profile()
    prof = prof.runctx("real_main()", globals(), locals())
    print "<pre>"
    stats = pstats.Stats(prof)
    stats.sort_stats("time")  # Or cumulative
    stats.print_stats(80)  # 80 = how many to print
    # The rest is optional.
    # stats.print_callees()
    # stats.print_callers()
    print "</pre>"

rawDescription: Best way to profile/optimize a website on google's appengine

description: Profile/optimize a website on google's appengine


code:
#!/usr/bin/env python

# keyword occurrences -> [('key_1', (11, 59)), ('key_2', (301, 333)), ('key_3', ())]
kw_all_occ = {"key_1" : [(11, 59)],
"key_2" : [(24, 56), (301, 333), (1208, 1240), (1673, 1705)],
"key_3" : [(24, 46), (1208, 1230)]}

def non_overlapping_occ(occ):
    # dictionary with all keyword occurrences
    all_occ = dict({})
    all_occ.update(occ)

    # list with the first non overlapping occurrences of every keyword ->
    # [('key_1', (start_1, end_1)),('key_2', (start_2, end_2)),...]
    result = []

    # Sort keywords by length -> [(22, 'key_3'), (32, 'key_2'), (48, 'key_1')]
    kw_lengths = []
    for k, v in all_occ.iteritems():
        kw_lengths.append((v[0][1] - v[0][0], k))
    kw_lengths.sort()

    while len(kw_lengths):
        # Current longest keyword
        longest_keyword = kw_lengths.pop(-1)[1]
        try:
            result.append((longest_keyword, all_occ[longest_keyword][0]))
            # Remove occurrences overlapping any occurrence of the current
            # longest_keyword value
            for item in all_occ[longest_keyword]:
                start = item[0]
                end = item[1]
                for l, k in kw_lengths:
                    v = all_occ[k]
                    all_occ[k] = filter(lambda x: (x[0] > end) | (x[1] < start), v)

        except IndexError:
            result.append((longest_keyword, ()))

    return result

print non_overlapping_occ(kw_all_occ)

rawDescription: Exact hitting set for keywords in text

description: Exact hitting set for keywords in text


code:
from itertools import islice
with open('data.txt') as f:
    for line in islice(f, 3, None, 3):
        print line, # Python3: print(line, end='')

rawDescription: How to print every third (or n-th) line from file in Python?

description: Print every third (or n-th) line from file in Python


code:
name = "foo"

def f():
    name = "bar"
    print "In f:", name

def g():
    global name
    name = "baz"
    print "In g:", name

def h():
    global mame
    mame = "foobar"
    print "In h:", mame

print "Globally:", name
f()
print "Globally:", name
g()
print "Globally:", name
h()
print "Globally", name
print "Globally", mame

rawDescription: what is difference between global and intizilation outside the function in python

description: What is difference between global and intizilation outside the function in python


code:
step = 1,1,1,1,2,2,2,2
time = 1,2,5,6,1,3,5,6

from itertools import groupby, izip
from operator import itemgetter

for key, group in groupby(izip(step, time), itemgetter(0)):
    group = [item[1] for item in group]
    print 'Step:', key, 'Min:', min(group), 'Max:', max(group)

rawDescription: Best way in Python to combine 2 lists and return a min / max of a set


code: datetime.datetime.combine(datetime.date(2011, 01, 01), datetime.time(10, 23))

rawDescription: Pythonic way to add datetime.date and datetime.time objects


code:
from contextlib import contextmanager

def razz_the_jazz():
    print gval

@contextmanager
def quietude(level):
    global gval
    saved_gval = gval
    gval = level

    try:
        yield
    finally:
        gval = saved_gval

gval = 1

with quietude(3):
     razz_the_jazz()

razz_the_jazz()

rawDescription: what is the Python equivalent of Ruby's yield?

description: What is the Python equivalent of Ruby's yield


code:
if __name__ == "__main__":
    x = count_line_occurrences(["a","b","c"],["c","c","b","a"])
    print x

rawDescription: python - find occurrences of strings from a reference file within an input file

description: Python - find occurrences of strings from a reference file within an input file


code:
print inspect.getmembers(mymodule,
                         predicate=lambda o: inspect.isclass(o) and \
                                             issubclass(o, A))

rawDescription: How to obtain all subclasses (of a specific class) defined by a module?

description: Obtain all subclasses (of a specific class) defined by a module


code:
str="suleman, h (2001) Some Random Piece Of Junk, Journal of Junk, ACM, pp1-100, 2000"

parts=str.split(',')
newparts=[]

for chunk in parts[:-3]:
    newparts.append(chunk.title())

newparts.append(parts[3].upper())

for chunk in parts[4:]:
    newparts.append(chunk.lower())

newstr=",".join(newparts)

print newstr

rawDescription: Reformatting a user-input

description: Reformatting a user-input


code:
def test():
    import time
    def m_int(s, memo={}):
        if s in memo:
            return memo[s]
        else:
            retval = memo[s] = int(s)
            return retval
    data = get_data()
    all_point_sets = []
    time_start = time.time()
    for xs, ys in data:
        point_set = []
        # Convert points from strings to integers
        y_iter = iter(ys.split(","))
        curr_points = [Point(m_int(i), m_int(next(y_iter))) for i in xs.split(",")]
        all_point_sets.append(curr_points)
    time_end = time.time()
    print "total time: ", (time_end - time_start)

rawDescription: Speeding up pairing of strings into objects in Python


code:
import re
s = 'this is a test 4242 did you get it'
pat = re.compile('[a-zA-Z]+ ([0-9]+)')
print pat.match(s, 10).group(0)

rawDescription: Python re match at specific point in string


code: print "C2: ", z1.mean(axis=2).mean(axis=1)

rawDescription: Conditioning and operation on multidimensional (3D) array in Python-NumPy


code:
elements = driver.find_elements_by_class_name('bar')
for element in elements:
    print element.text
    print element.get_attribute('data-value')

rawDescription: Parsing HTML5 data-* attribute values with Selenium in Python


code:
def tryit(fn):
    def wraps(*args, **kwargs):
        try:
            fn(*args, **kwargs)
        except Exception, e:
            print e
    return wraps

def doStuff(x, y):
    return x / y

@tryit
def main(arg1, arg2):
    print doStuff(arg1, arg2)

if __name__ == "__main__":
    main(1, 10.0)
    main(100, 0)

rawDescription: python program wraparound

description: Python program wraparound


code:
def f(x):
 class C(A):
  def hello(self):
   print "Hello C"
 c = C()
 c.hello()
 print x
 for cls in A.__subclasses__():
  print cls.__name__
f(4)

rawDescription: How to know all the derived classes of a parent?

description: Know all the derived classes of a parent


code:
def before_run(logging=True):
    def decorator(func):
        print "hello from before run"
        def handle_arg(a,b):
            if(a>0):
                if logging:
                    print "Altering argument a to 100"
                a = 100
            return func(a,b)

        return handle_arg

    return decorator

rawDescription: when to use decorator and decorator factory?

description: When to use decorator and decorator factory


code:
for i, j in zip(val, reversed(val)):
    if i != j:
        print 'no'
        break
else:
    print 'yes'

rawDescription: Testing palindrome with nested loops

description: Testing palindrome with nested loops


code:
import string

vals = string.letters
def spy_code(txt,n):
    result = ''
    for i in txt:
        if i in vals:
            c = ord(i)+n
            if ord(i) != c:
                b = chr(c)
                result += b
        else:
            result += i
    print result

spy_code('abord mission, dont atk teacher!',5)

rawDescription: How to rotate chars in string to encrypt message

description: Rotate chars in string to encrypt message


code:
def foo(arg1, arg2, src, dst=None):
    dst = dst if dst is not None else src
    print dst

foo(0, 0, "test")

rawDescription: Python - how to set default parameter equal to another parameter


code:
import numpy as np

print np.where(header=='ID')
(array([0], dtype=int64),)

columns = np.delete(header, np.where(header=='ID'))
print columns
['Age' 'Income' 'Deductions' 'Hours' 'Adjustment' 'Adjusted'
 'Employment_Consultant' 'Employment_PSFederal' 'Employment_PSLocal'
 'Employment_PSState' 'Employment_Private' 'Employment_SelfEmp'
 'Employment_Unemployed' 'Employment_Volunteer' 'Education_Associate'
 'Education_Bachelor' 'Education_College' 'Education_Doctorate'
 'Education_HSgrad' 'Education_Master' 'Education_Preschool'
 'Education_Professional' 'Education_Vocational' 'Education_Yr10'
 'Education_Yr11' 'Education_Yr12' 'Education_Yr5t6' 'Education_Yr7t8'
 'Education_Yr9' 'Marital_Absent' 'Marital_Divorced' 'Marital_Married'
 'Marital_Married-spouse-absent' 'Marital_Unmarried' 'Marital_Widowed'
 'Occupation_Cleaner' 'Occupation_Clerical' 'Occupation_Executive'
 'Occupation_Farming' 'Occupation_Machinist' 'Occupation_Professional'
 'Occupation_Repair' 'Occupation_Sales' 'Occupation_Service'
 'Occupation_Support' 'Occupation_Transport' 'Sex_Female' 'Sex_Male'
 'Accounts_Cuba' 'Accounts_England' 'Accounts_Germany' 'Accounts_India'
 'Accounts_Indonesia' 'Accounts_Iran' 'Accounts_Ireland' 'Accounts_Jamaica'
 'Accounts_Malaysia' 'Accounts_Mexico' 'Accounts_Philippines'
 'Accounts_Portugal' 'Accounts_UnitedStates' 'Accounts_Vietnam']

rawDescription: Python pandas dataframe- remove columns from header


code:
mult_list = input("Enter numbers seperated by a comma: ")
mult = reduce(lambda x, y: x*y, mult_list)
print mult

rawDescription: Using lambda with user input list

description: Using lambda with user input list


code:
instrument = visa.instrument('GPIB::01')  # Assuming a GPIB device on channel 1
print instrument.ask('*STB?')  # Print status register

rawDescription: Python VISA serial polling function

description: Python VISA serial polling function


code:
a = 1

def fn():
    print a
    a = 2  # <<< We're adding this

fn()

# Throws: UnboundLocalError: local variable 'a' referenced before assignment

rawDescription: Python : Why is it said that variables that are only referenced are implicitly global?


code:
marker = [x for x in range(100) if x % 10 == 0]
marker.pop(0)

# make it 10:0, 20:0, 30:0, 40:0 and so forth...
mark = dict(zip(marker, [0 for i in range(len(marker))]))
print "BEFORE:"
for i in marker:
    print "%s percent index: %s" % (i, mark[i])

l = len(comb)
for i,v in enumerate(comb):
    p = perc(i, l)
    ip = math.ceil(p)
    if ip in marker:
        mark[ip] = i

rawDescription: Getting indices for certain percent of a length of a list


code:
for form in br.forms():
    print form

rawDescription: Screenscaping aspx with Python Mechanize - Javascript form submission


code:
import re

def correct(string):
    fmatch = re.sub(r'\s+', ' ', string)
    return fmatch

print correct("This   is  very funny  and    cool.Indeed!")

rawDescription: Python match whitespaces

description: Python match whitespaces


code:
from lxml import html

sample = '<?xml version="1.0" encoding="UTF-8"?><root>I am the correct one.I am the wrong one.</root>'
tree = html.fromstring(sample)
print tree.xpath("//a[@class='asig']/text()")[0]
print tree.cssselect("a[class='asig']")[0].text

rawDescription: How do I select an element with the exact class using cssselect in lxml?

description: Select an element with the exact class using cssselect in lxml


code:
a = input("Letter: ")
b = input("Input: ")
a=a.lower()
b=b.lower()
c=b.count(a)
print(c)
words = b.split()
print ' '.join([word for word in words if a in word])

rawDescription: How to join strings with a space without adding an extra space at the end?

description: Join strings with a space without adding an extra space at the end


code:
def Clicked(self, event):
    checkedItems = [i for i in range(self.panel.citList.GetCount()) if self.panel.citList.IsChecked(i)]
    print checkedItems
    event.Skip()

rawDescription: Cannot understand how to get data from checklistbox in wxpython


code:
#! /usr/bin/env python

from PIL import Image

def extract_text(iname):
    last = 0
    chars = []
    for i, v in enumerate(bytearray(Image.open(iname).tobytes())):
        if v:
            chars.append(chr(i - last))
            last = i
    return ''.join(chars)

print extract_text('coded.png')

rawDescription: Analysing a png (1bit color) for pixel

description: Analysing a png (1bit color) for pixel


code:
#!/usr/bin/python
from random import randint
from datetime import date, timedelta
from itertools import groupby

""" finding best two months the functional way """

def find_best_two_months(orders):
  """
  Expect a list of tuples of form (date_of_order, amount):

  [ (date1, amount1), (date2, amount2), ...]
  """

  " helper functions for extracting first or second from tuple "
  first  = lambda x: x[0]
  second = lambda x: x[1]

  " converts [(date, amount)] -> [(YYYY-MM, amount)] "
  orders_by_year_and_month = [ ("%04d-%02d" % (date.year, date.month), amount) for date, amount in orders]

  " Sorts by YYYY-MM. This step can be omitted if orders were already sorted by date"
  sorted_orders = sorted(orders_by_year_and_month, key=first)

  " Compresses orders from the same month, so ve get [(YYYY-MM), total_amount_of_orders]"
  totals_by_month = [ (ym, sum(map(lambda x:x[1], groupped_orders)))
    for ym, groupped_orders in groupby(sorted_orders, key=first)]

  " Zips orders to two month periods"
  totals_two_months = [ ("%s - %s" % (m1[0], m2[0]), m1[1]+m2[1]) for m1, m2 in zip(totals_by_month, totals_by_month[1:]) ]

  " Returns two-month period with maximum total amount. If there were many periods with max amount, only the first is returned "
  return max(totals_two_months, key=second)

"""
this code is for generating random list of orders
and is not a part of the solution
"""
MIN_AMOUNT=70
MAX_AMOUNT=500
MAX_DAY_SPREAD=5

def gen_order(last_date):
  """ returns (order_date, amount) """
  days = timedelta()
  return (
      last_date+timedelta(days=randint(0, MAX_DAY_SPREAD)),  # new date
      randint(MIN_AMOUNT, MAX_AMOUNT)) # amount

def gen_orders(total, start_date):
  orders = []
  last_date = start_date
  for i in range(total):
    order = gen_order(last_date)
    orders.append(order)
    last_date = order[0]
  return orders

if __name__ == "__main__":
  orders = gen_orders(300, date(2010,1,1))
  print find_best_two_months(orders)

rawDescription: How to create a list of totals for durations?

description: Create a list of totals for durations


code:
@static_var("counter", 0)
def foo():
    foo.counter += 1
    print "Counter is %d" % foo.counter

rawDescription: What is the Python equivalent of static variables inside a function?


rawDescription: loop trough a python list of tuples and change a value

description: Loop trough a python list of tuples and change a value


code:
from itertools import groupby

filelist = [
    'C:\\West-California-North-10.xlsx',
    'C:\\West-California-North-5.xlsx',
    'C:\\West-California-East-1.xlsx',
    'C:\\West-California-South-1.xlsx',
    'C:\\South-California-North-5.xlsx',
    'C:\\West-California-South-3.xlsx']

key_fn = lambda s: s.rsplit('-',1)[0]

# before grouping, list has to be sorted
filelist = sorted(filelist, key=key_fn)

# usually use the same key_fn for grouping as was used for sorting
for key, grouped_file_names in groupby(filelist, key=key_fn):
    # groupby returns an iterator of tuples
    # the first element of the tuple is the grouped key value
    # the second element is a generator of the items that matched that key
    # (YOU MUST CONSUME THIS GENERATOR BEFORE MOVING ON TO THE NEXT KEY)
    print '\n'.join(list(grouped_file_names))
    print

rawDescription: Filter through Python list to find common elements


code:
from lxml.html import etree

html = """

"""
root = etree.fromstring(html)
result = root.xpath("//*[@*[contains(.,'{%')]]")
for r in result:
    print etree.tostring(r)

rawDescription: CSS selectors to query by attribute alone, with LXML


code:
import sys, cStringIO
zen = cStringIO.StringIO()
old_stdout = sys.stdout
sys.stdout = zen
import this
sys.stdout = old_stdout
print zen.getvalue()

rawDescription: How can I return python's "import this" as a string?

description: Return python's "import this" as a string


code:
data = {
"Technology": {"20809": 98},
"Movie":{"1193435": 1},
"Entertainment":{"13398": 76},
"Computer": {"28765": 54},
"Food": {"708": 4},
"Gasoline": {"358": 18},
"Material": {"98": 6},
"Water": {"3": 6}
}

def searchAtHashTable(input):
    return {input : data.get(input, {})}

print searchAtHashTable("Entertainment")

rawDescription: Python: Return hash values from hash table regarding to a searching keyword


code:
import os
import subprocess
from subprocess import Popen
os.chdir("C:\Users\OFFICE2\Desktop")
Popen("myconvertedbatchfile.exe")

rawDescription: Can't run 32-bit VB script from Python

description: Can't run 32-bit VB script from Python


code:
field = Choice._meta.get_field_by_name('poll')[0]
print field.rel.to.__name__  # prints 'Poll'

rawDescription: Get an attribute's class from its name in Python?


code:
import re

def ind_sbt(text,indent='\t'):
    return text.replace('\n','\n%s' % (indent,))

def adjust_indents(fmtstr,*args):
    """Hacky, crude detection of identation level"""
    indented_substitutions = re.compile(r'\n(\s+)%s')
    return fmtstr % tuple([ ind_sbt(arg,indent=i) for (arg,i) in zip(args,indented_substitutions.findall(fmtstr))])

p1 = p2 = p3 = p4 = "foo\nbar"

print adjust_indents('''
<
    %s
    <
        %s
        %s
        <
            %s
        >
    >
>
''',p1,p2,p3,p4)

rawDescription: Python Block Formatting

description: Python Block Formatting


code:
def get_key(data, key):
    keys = {k: v for k, v in [i.split('=') for i in data.split('&')]}
    for k, v in keys.items():
        if k == key: return int(v)
    return None

results = []

for line in [line.strip() for line in open('filename', 'r')]:
    value = get_key(line, 'kv1')
    if value:
        results.append({'kv1': value})

print results # could be [{'kv1': 2}, {'kv1': 1}, {'kv1': 4}]

rawDescription: Aggregating key value pair in python

description: Aggregating key value pair in python


code:
output = {'name':'StackOverflow',
       'competitors':[{   'competitor':'bing',
                          'link':'bing.com'},
                      {   'competitor':'google',
                          'link':'google.com'}],
       'acquisition': {'acquired_day': 16,
                       'acquired_month': 12,
                       'acquired_year': 2013,
                       'acquiring_company': {'name': 'Viggle',
                                             'permalink': 'viggle'}}}

print flatten_dict(output)
# {'acquisition_acquired_year': 2013, 'acquisition_acquiring_company_name': 'Viggle', 'name': 'StackOverflow', 'acquisition_acquiring_company_permalink': 'viggle', 'competitors_0_competitor': 'bing', 'acquisition_acquired_month': 12, 'competitors_1_link': 'google.com', 'acquisition_acquired_day': 16, 'competitors_1_competitor': 'google', 'competitors_0_link': 'bing.com'}

rawDescription: Iterate through Nested, mixed dictionary


code:
for k, v in dct.iteritems():
    print repr(k)+ ":(" + ", ".join("Country{}:{}".format(i,c) for i,c in enumerate(v, start=1)) + ")"

rawDescription: Naming each item in a list which is a value of a dictionary


code:
from bs4 import BeautifulSoup
s = """

                Love Heals

                Friday, March 20, 2015

"""
soup = BeautifulSoup(s)
for span in soup.find_all('span'):
    print span.text.strip()

rawDescription: Extra newline character for children of Beautiful Soup


code:
import timeit

print timeit.timeit("int('{0:b}'.format(n)[::-1], 2)", 'n = 123456')

print timeit.timeit("int(bin(n)[:1:-1], 2)", 'n = 123456')

print timeit.timeit("int(''.join(reversed(bin(n)[2:])),2)", 'n = 123456')

rawDescription: Slick way to reverse the (binary) digits of a number in Python?


code:
from bs4 import BeautifulSoup

data = """
 November General Election Day.Scheduled Elections:
    My Test String

Federal, Statewide, Legislative and Judicial Offices

County Offices

Initiatives and Constitutional Amendments, if applicable

New Paragraph

"""

soup = BeautifulSoup(data, 'html.parser')
for element in soup.td.find_all('li'):
    element.insert(0, '\n')

print soup.td.text

rawDescription: Can I modify the text within a beautiful soup tag without converting it into a string?

description: Modify the text within a beautiful soup tag without converting it into a string


code:
import re
tmplt = '  7352.103      26.0      2.61    -8.397                         11.2'
def fmt_from_template(tmplt):
    pat = r'( *-?\d+\.(\d+))' # one number with its decimal
    fmt = []
    while tmplt:
        match = re.search(pat,tmplt)
        if match:
            x = len(match.group(1)) # length of the whole number
            d = len(match.group(2)) # length of decimals
            fmt += ['%%%d.%df'%(x,d)]
            tmplt = tmplt[x:]
    fmt = ''.join(fmt)
    return fmt
print fmt_from_template(tmplt)
# %10.3f%10.1f%10.2f%10.3f%29.1f

rawDescription: Use python to handle and create input files for external software


code:
def myFunc( a, b ):
    def innerFunc( c ):
        print c
    innerFunc( 2 )
    myFunc.innerFunc = innerFunc
    print a, b
myFunc(1,2)
myFunc.innerFunc(3)

rawDescription: Accessing inner function of a function

description: Accessing inner function of a function


code:
import re
data = ["Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko) Ubuntu/11.04 Chromium/14.0.825.0 Chrome/14.0.825.0 Safari/535.1",
"Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10gin_lib.cc",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; pt-PT; rv:1.9.2.7) Gecko/20100713 Firefox/3.6.7 (.NET CLR 3.5.30729)"]
output = []
for str in data:
   output.append(re.sub(r'\d[0-9a-zA-Z._:-]+', '*', str))
print output

rawDescription: Regex for identifying version numbers - Python


code:
import networkx as nx

g = nx.Graph()
g.add_edge(1,2);g.add_edge(1,3)
g.add_edge(1,7);g.add_edge(2,4)
g.add_edge(3,4);g.add_edge(3,5)
g.add_edge(3,6);g.add_edge(4,5)
g.add_edge(5,6);g.add_edge(6,7)

import itertools

target = nx.Graph()
target.add_edge(1,2)
target.add_edge(2,3)

for sub_nodes in itertools.combinations(g.nodes(),len(target.nodes())):
    subg = g.subgraph(sub_nodes)
    if nx.is_connected(subg) and nx.is_isomorphic(subg, target):
        print subg.edges()

rawDescription: How can I extract all possible induced subgraphs from a given graph with networkx

description: Extract all possible induced subgraphs from a given graph with networkx


code:
from collections import Counter
a = [1,2,3,1,2,1,1,1,3,2,2,1]
b = Counter(a)
print b.most_common(1)

rawDescription: find the most frequent number in a numpy vector

description: Find the most frequent number in a numpy vector


code:
def myFunc(myString, myList1, myList2):
    print myString, myList1, myList2

myFunc(*eval("('item 1', [1,2,3,4] , [4,3,2,1])"))

rawDescription: Read in tuple of lists from text file as tuple, not string - Python


code:
from docx import *
document = opendocx(r'test.docx')
words = document.xpath('//w:r', namespaces=document.nsmap)

WPML_URI = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}"
tag_rPr = WPML_URI + 'rPr'
tag_highlight = WPML_URI + 'highlight'
tag_val = WPML_URI + 'val'

for word in words:
    for rPr in word.findall(tag_rPr):
        if rPr.find(tag_highlight).attrib[tag_val] == 'yellow':
            print word.find(tag_t).text

rawDescription: Extracting Highlighted Words from Word Document (.docx) in Python


code:
indices = list(enumerate_nested(l, tuple()))
print l
for i in indices:
    print tuple_index(l, i),

rawDescription: Navigate manually with a cursor through nested lists by only providing "left()" and "right()" as commands?


code:
import re

# joblist list will store each line of parsed output
joblist=[]
prog = re.compile('^(\d+)\..*\s+.*\s+\w+\s+(.*)\s+(\w)\s+(.*)$')
with open('job.txt','r') as jobfile:
        for line in jobfile.readlines():
                result = prog.match(line)
        # Handle header line and skip lines that don't match regex
                if result is None:
                    continue
                else:
                    joblist.append(' '.join([result.group(1), result.group(2), result.group(3), result.group(4)]))

# displaying the list
for job in joblist:
    print job

rawDescription: reading lines from file into lists for a specific field

description: Reading lines from file into lists for a specific field


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

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


code:
import sys
buffer = []
while True:
    line = sys.stdin.readline().rstrip('\n')
    if line == 'quit':
        break
    else:
        buffer.append(line)
print buffer

rawDescription: taking multiline input with sys.stdin

description: Taking multiline input with sys.stdin


rawDescription: Python2: List splitting syntax - tail of a list


code:
import os
from collections import Counter

path = '/the/path/to/the/I want/to/count'
counts = Counter(fname[:2] for _, _, files in os.walk(path) for fname in files)
for initials, count in counts.most_common():
    print '{}: {:>20}'.format(initials, count)

rawDescription: Count Files based on the first two letters


code:
from xml.etree import ElementTree as ET

def get_items(elements):
    D = {elem.get("id"): dict((child.tag, child.text) for child in elem)
         for elem in elements}
    return D

tree = ET.parse("item.xml")
options = tree.findall(".//option")
sections = tree.findall(".//section")

print "options:"
print get_items(options)
print "sections:"
print get_items(sections)

rawDescription: Python XML parsing - multisections

description: Python XML parsing - multisections


code:
a1 = A( [], 0 )
a2 = A( [], 0 )
a3 = A( [ a1, a2 ], 10 )
a3.passToConnections( 0 )
a3.passToConnections( 1 )
a3.passToConnections( 1 )
print a1.sum
print a2.sum

rawDescription: How to implement pointers in Python? (or any similar solution else)

description: Implement pointers in Python? (or any similar solution else)


code:
from subprocess import call

def make_1024 ( src, tgt ):
    print "/usr/bin/convert %s -background none -gravity center -extent 1024x500! %s" % ( src, tgt )
    rc = call ( ["/usr/bin/convert", src, "-background",  "none",  "-gravity", "center", "-extent", "1024x500!", tgt] )
    print "rc is %d" % rc

if __name__ == "__main__":
  make_1024(src="usage.jpg", tgt="test.jpg")

rawDescription: Having trouble passing arguments to subprocess.call


code:
import wx
import wx.propgrid as wxpg
from random import random
import gc

from collections import defaultdict

def output_memory():
    d = defaultdict(int)
    for o in gc.get_objects():
        name = type(o).__name__
        d[name] += 1

    items = d.items()
    items.sort(key=lambda x:x[1])
    for key, value in items:
        print key, value

class CoordProperty(wxpg.PyProperty):
    def __init__(self, label, name):
        wxpg.PyProperty.__init__(self, label, name)
        self.SetValue(([0],[0],[0]))

    def GetClassName(self):
        return "CoordProperty"

    def GetEditor(self):
        return "TextCtrl"

    def GetValueAsString(self, flags):
        x,y,z = self.GetValue()
        return "%f,%f,%f"%(x[0],y[0],z[0])

app = wx.App(False)
frame = wx.Frame(None, -1, "Test")
pg = wxpg.PropertyGridManager(frame)
props = {}

for i in range(1000):
    prop_name = "prop_%d"%i
    prop = CoordProperty("Coord", prop_name)
    pg.Append(prop)
    props[prop_name] = prop

def OnTimer(event):
    for name, prop in props.iteritems():
        value = prop.GetValue()
        value[0][0] = random()
        value[1][0] = random()
        value[2][0] = random()
    pg.Refresh()
    #output_memory()

timer = wx.Timer(frame, 1)
frame.Bind(wx.EVT_TIMER, OnTimer)
timer.Start(10)

frame.Show()

app.MainLoop()
timer.Stop()

rawDescription: Dramatic memory leak in wxpython application


code:
words = phrase.split()

for n, w in enumerate(words):
    if w == 'in':
        print '(%s) in (%s) ' % (
            ' '.join(words[:n]),
            ' '.join(words[n+1:]))

rawDescription: How to get all possible interpretations in regex match?

description: Get all possible interpretations in regex match


code:
import numpy as np
from timeit import timeit
from itertools import izip_longest

def f1(x, enumerate=enumerate, max=max, len=len):
    n_rows = len(x)
    n_cols = max(len(ele) for ele in x)

    new_array = np.ones((n_rows, n_cols)) * -1
    for i, row in enumerate(x):
        for j, ele in enumerate(row):
            new_array[i, j] = ele
    return new_array

def f2(x, enumerate=enumerate, max=max, len=len, map=map):
    n_rows = len(x)
    n_cols = max(map(len, x))

    new_array = np.empty((n_rows, n_cols))
    new_array.fill(-1)
    for i, row in enumerate(x):
        for j, ele in enumerate(row):
            new_array[i, j] = ele

    return new_array

setup = '''x = [[0,2,3],
    [],
    [4],
    [5,6]]
from __main__ import f1, f2'''

print timeit(stmt='f1(x)', setup=setup, number=100000)
print timeit(stmt='f2(x)', setup=setup, number=100000)

rawDescription: 2D list to numpy array, and fill remaining values of shorter sublists with -1


code:
import pandas

dfnew= pandas.read_excel("sample_data_hj.xls")

dfpivotnew = pandas.pivot_table(dfnew, index=["mmc","chrom","start","end","experiment","is_bidirectional"],\
                   values=["segment_quality_reads","segment_reads",])

print dfpivotnew.groupby(level=['mmc','chrom']).filter(lambda y : len(y) > 3)

rawDescription: Pandas how to get a list of rows that have multiple values for an index level in a Multiindex DataFrame


code:
class Foo(object):
    def __init__(self):

        raise Exception

    def __del__(self):
        print "deling", self

if __name__ == '__main__':
    f = Foo()

rawDescription: How to force deletion of a python object?

description: Force deletion of a python object


code:
s = """\
mytuple = [('Andrew','Andrew@gmail.com','20'),('Jim',"Jim@gmail.com",'12'),("Sarah","Sarah@gmail.com",'43'),("Jim","Jim@gmail.com",'15'),("Andrew","Andrew@gmail.com",'56')]
seen = set()
[x[1] for x in mytuple if x[1] not in seen and not seen.add(x[1])]
"""
print timeit.timeit(stmt=s, number=100000)
0.0938332080841

rawDescription: Dropping values from a list of tuples

description: Dropping values from a list of tuples


rawDescription: Unstack or Pivot Only Some Columns

description: Unstack or Pivot Only Some Columns


code: print sorted(times, key=lambda k: k.values()[0])

rawDescription: Sort list of dictionaries by value, regardless of keyname in python


code:
d = {'happy':['sun', 'moon', 'chocolate'], 'sad':['fail', 'test', 'bills'], 'random': ['baloon', 'france', 'sun'] }
specific_word = 'bear' #uncomment to search for specific word

for key_a in d: #loop through the keys of d
   for key_b in d: #loop a second time through the keys of d
       if key_a == key_b: #if the keys are the same, skip it
           break
       for item in d[key_a]: #loop through items in d[key_a]
           if (item in d[key_b]): #check if the item is in d[key_b]
           #if you want to search ONLY for specific_word then this above if statement changes to this:
           #if (item in d[key_b]) and item == specific_word:
               print key_a,key_b #if u made it this far, print the keys
               break # stop printing other stuff, in case of multiple matches

rawDescription: How to find Duplicate values in Dict and print keys with those values

description: Find Duplicate values in Dict and print keys with those values


code:
sizeWritten = 0
totalSize = someMethodToGetTotalBytesInFile()
def handle(block):
    sizeWritten += 1024
    percentComplete = sizeWritten / totalSize
    print "%s percent complete" %str(sizeWritten / totalSize)

rawDescription: Python ftplib and storbinary

description: Python ftplib and storbinary


code:
r = unicodecsv.reader(open("test.csv", "rb"))
for row in r:
    print row[0], row[1]

rawDescription: write chinese words in csv file using python2.7

description: Write chinese words in csv file using python2.7


code:
df = DataFrame(np.random.randn(4, 4))
df['1','2']=3   #not what you want
print df
df.iloc[1,2]=3  #what I think you mean
print df

rawDescription: How to restructure data using a loop (without crashing computer!) and write to csv in python

description: Restructure data using a loop (without crashing computer!) and write to csv in python


code:
if __name__ == "__main__":
    # test
    install_auth_opener()
    page = "%s/me/api/python" % jenkins_url
    try:
        result = urllib2.urlopen(page)
        assert result.code == 200
        print "ok"
    except urllib2.HTTPError, err:
        assert err.code != 401, 'BAD CREDENTIALS!'
        raise err

rawDescription: Fetching a URL from a basic-auth protected Jenkins server with urllib2


code:
iterex = IteratorEx(xrange(10))
for i in iterex:
    print i, iterex.hasNext

rawDescription: Last element in a python iterator

description: Last element in a python iterator


code:
lookup = [None]*256
for cat, nums in categories.iteritems():
    for n in nums: lookup[n] = cat

# ... later
categorized = defaultdict(list)
for n in numbers:
    categorized[lookup[n]].append(n)
    print "put %d in category %s" % (n, lookup[n])

rawDescription: Categorize a number

description: Categorize a number


code:
from docx import Document
document = Document('TestDocument.docx')
for p in document.paragraphs:
    for run in p.runs:
            print run.text, run.italic, run.bold

[[run.text for run in p.runs if run.italic] for p in document.paragraphs]

rawDescription: Reading docx files, recognizing and storing italicized text


code:
try:
    with open("/tmp/foo.txt") as foo:
        print foo.read()
except:
    try:
        with open("./foo.txt") as foo:
            print foo.read()
    except:
        print "No foo'ing files!"

rawDescription: Idiom in python for opening second file when the first file does not exist


code:
import os.path, time
print "last modified: %s" % time.ctime(os.path.getmtime(file))
print "created: %s" % time.ctime(os.path.getctime(file))

rawDescription: How to get file creation & modification date/times in Python?

description: Get file creation & modification date/times in Python


code:
def open_file_handler():
    filePath= askopenfilename() # don't override the built-in file class
    print filePath
    # do whatever with the file here

rawDescription: python file dialog accessing file name

description: Python file dialog accessing file name


code:
from BeautifulSoup import BeautifulSoup as BS

html = """

hey!

hey!

hey!

"""

soup = BS(html)
for table in soup.findAll("table" ,text=None,recursive=True):
    if table.text:
        print table["id"]

rawDescription: BeautifulSoup: How to remove empty tables, while preserving tables that are partially empty or not empty


code:
print repr(X_train)
print repr(X_test)

rawDescription: scikit-learn: Classification timing correct?

description: Scikit-learn: Classification timing correct


code:
import string

from collections import Counter

some_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque fringilla augue ac metus laoreet quis imperdiet velit congue. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Quisque tincidunt lorem ut justo fermentum adipiscing. Nullam ullamcorper eros in arcu tincidunt non scelerisque ligula molestie. Vestibulum imperdiet facilisis nisi, et sodales leo sodales at. In hac habitasse platea dictumst."

letters = []
for i in some_text:
   # Each "i" is a letter or space
   if i in string.letters:
      # only collect letters, not punctuation marks or spaces
      letters.append(i)

# count how many of each
letter_count = Counter(letters)

# For each letter, print the count:

for letter,count in letter_count.iteritems():
    print "{}: {}".format(letter,count)

rawDescription: Generating a list of unique characters in a MySQL table


code:
from itertools import izip, tee
it1, it2 = tee(lis)   #creates two iterators from the list(or any iterable)
next(it2)             #drop the first item
print [x+y for x, y in izip(it1, it2)]
#[1, 3, 5, 7, 9, 11, 13, 15, 17]

rawDescription: Python for loop get index and index+1

description: Python for loop get index and index+1


code:
for files in glob.glob("*.csv"):
    for lines in files:
         if line == header:
              print member,line
         else:
              print line

rawDescription: Merge CSV Files in Python with Different file names


code:
while True:
    if all(x.isalpha() for x in n_strings ):
        print("All guessed correct {}".format(n_strings)) # if all are alpha print final n_string and break out of loop
        break
    print n_strings
    letter = input("Please enter a letter: ")
    if letter in L:
    # Find the position of the letter in the list
        position = (L.index(letter));
        number = N[position];
        n_strings = [item.replace(number, letter) for item in n_strings];
        print (n_strings, "\n");
    # repeat until the List only contains letters
    else:
        print ("That is not correct");
        print ("Please guess again. \n");

rawDescription: Replace an item in a list based on user input


code:
import re

text = "This is an example.A particular case.Made up of more "\
       "than once sentence?Yes, it is.But no blank !!!That's"\
       " a problem ????Yes.I think so! :)"

for x in re.split("(?<=[\.\?!]) ", text):
    print repr(x)

print '\n'

for x in re.findall("[^.?!]*[.?!]|[^.?!]+(?=\Z)",text):
    print repr(x)

rawDescription: Non-consuming regular expression split in Python


code:
with open("abc") as f:
    #read the first line and set the number from it as the value of `prev`
    num, col4 = next(f).rsplit(None,2)[-2:] #use `str.rsplit` for minimum splits
    prev  = int(num)
    col4_prev = col4
    count = 1                               #initialize `count` to 1
    for lin in f:
        num, col4 = lin.rsplit(None,2)[-2:]
        num  = int(num)
        if num - prev == 1:             #if current `num` - `prev` == 1
            count+=1                        # increment `count`
            prev = num                      # set `prev` = `num`
        else:
            print count,col4_prev       #else print `count` or write it to a file
            count = 1                       #reset `count` to 1
            prev = num                      #set `prev` = `num`
            col4_prev = col4

    if num - prev != 1:
        print count,col4

rawDescription: Challenging way of counting entries of a file dynamically


code: print sum([zip(*(a[i:] for i in range(n))) for n in range(len(a)+1)], [])

rawDescription: Generate partial subsets in Python

description: Generate partial subsets in Python


code:
import re

text = "The  Aquaporin protein, sometimes  'may' exhibit a big LOSS of gene."
text = ' '.join(re.findall(r'\b(\w+)\b', text.lower()))

indexes = sorted((text.find(x), len(x)) for x in ['loss of gene', 'aquaporin protein'])

if all(i[0] != -1 for i in indexes) and len(text[indexes[0][0] + indexes[0][1] : indexes[-1][0]].split()) <= 5:
    print "matched"

rawDescription: 'difficult' Determine proximity between 2 strings in python


code:
import subprocess
the_files = ['to_merge_1.txt', 'to_merge_2.txt']
cmdlnargs = ['python','argparse_example.py','--input_files']
cmdlnargs.extend(the_files)

subp = subprocess.Popen(cmdlnargs,
                        stdout=subprocess.PIPE,
                        stdin=subprocess.PIPE,
                        stderr=subprocess.STDOUT)
stdout, stderr = subp.communicate()
print stdout

rawDescription: Infinite input using argparse

description: Infinite input using argparse


code:
import re

def get_numbers(value):
    value = re.sub(r'^(\d+)$', r'\1-\1', value) # '88' -> '88-88'
    start, stop = map(int, re.findall(r'\d+', value))
    return range(start, stop+1)

print get_numbers('88-94')
print get_numbers('88 to 94')
print get_numbers('88')

rawDescription: re to identify range from string

description: Re to identify range from string


code:
calc = pp.CaselessLiteral('calc')
for entry in courselist:
    if calc.searchString(entry, 1):
        print entry

rawDescription: pyparsing: extracting strings containing specific text

description: Pyparsing: extracting strings containing specific text


code:
def printDiffs(x,y):
    diff = False
    for x_key in x:
        if x_key not in y:
            diff = True
            print "key %s in x, but not in y" %x_key
        elif x[x_key] != y[x_key]:
            diff = True
            print "key %s in x and in y, but values differ (%s in x and %s in y)" %(x_key, x[x_key], y[x_key])
    if not diff:
        print "both files are identical"

rawDescription: Comparing python dictionaries and find diffrence of the two


code:
from datetime import datetime
from dateutil import rrule
import pytz
now = datetime.utcnow()
pl = pytz.timezone("Europe/Warsaw")
r = rrule.rrule(rrule.DAILY, dtstart=now, interval=180, count=2)
now
for dt in r:
    local_dt = dt.replace(tzinfo=pytz.UTC).astimezone(pl)
    print local_dt - local_dt.dst()

# so what is the actual local time we store in the DB ?
now.replace(tzinfo=pytz.UTC).astimezone(pl)

rawDescription: How to handle DST and TZ in recurring events?

description: Handle DST and TZ in recurring events


code: print string,    # This way adds a space at the end.

rawDescription: Methods for printing without adding control characters in python


code:
lower = 0
upper = 0
for letter in full_name:
    if letter.islower():
        lower = lower + 1
    elif letter.isupper():
        upper = upper + 1
print "number of lower letters = %s" % lower
print "number of upper letters = %s" % upper

rawDescription: Python & Strings/Loops help please

description: Python & Strings/Loops help please


code:
from bs4 import BeautifulSoup

html_doc = '''

    Journal of     Infection (2015)

    xx
    , 1
    e
    4

'''

soup = BeautifulSoup(html_doc,'html.parser')
my_att = [i.attrs['style'] for  i in soup.find_all("div")]
dd = ''.join(my_att).split(";")
dd_cln= filter(None, dd)
dd_cln= [i.strip() for i in dd_cln ]
my_dict = dict(i.split(':') for i  in dd_cln)
print my_dict['width']

rawDescription: compute the average height and the average width of div tag

description: Compute the average height and the average width of div tag


code:
#foo module:

class Foo(object):
    sanitize = "value 1"
    @classmethod
    def parse(cls):
        print cls.sanitize

#foo2
from foo import Foo
class Foo2(Foo):
    sanitize = "value 2"

# code:
from foo import Foo
from foo2 import Foo2

Foo.parse()
Foo2.parse()

rawDescription: Alter module variables in one module while it doesn't change in the original


code:
def tohex(val, nbits):
  return hex((val + (1 << nbits)) % (1 << nbits))

print tohex(-199703103, 64)
print tohex(199703103, 64)

rawDescription: how to convert negative integer value to hex in python

description: Convert negative integer value to hex in python


code:
for key in sorted(m.groupdict().keys(), key=m.groupdict().get):
    print key, m.span(key)

rawDescription: Extract the Name and Span of Regex Matched Groups


rawDescription: unable to correctly identify Pareto observations in 4-dimensional solution space

description: Unable to correctly identify Pareto observations in 4-dimensional solution space


code:
data = [('color','blue'),('number',3),('place','usa'),('color','yellow'),('place','canada'), ('color', 'orange'), ('sport', 'hockey')]
dd = tups_to_dicts(data)
for d in dd:
    print d

rawDescription: function generates list of dictionaires

description: Function generates list of dictionaires


code:
# this computes the minimum priority of a ticket
def ticketMin (list):
    min = list[0]
    for ticket in list:
        if (ticket.priority < min.priority):
            min = ticket
    return min

# changed set to list
result = list()
for ticket in tickets:
# to get rid of the "None" priorities
    if ticket.priority != '<pirority range>':
        print ""
    else:
        #notice the change below
        result.append(ticket)

# changed 'min' to 'ticketMin'
minTicket = ticketMin(result)

print minTicket.priority
print minTicket.code

rawDescription: Keeping Values connected when sorting results of a for loop


code:
def edges(over_count, under_count, push_count):
  over = 1/(over_count / float(over_count + under_count))
  under = 1/ (under_count / float(over_count + under_count))
  over_odds_given = 1.77
  under_odds_given = 2.05
  overedge = 1/over * over_odds_given - 1
  underedge = 1/under * under_odds_given - 1
  print overedge, underedge

rawDescription: improving a function in python

description: Improving a function in python


code:
sam2_identifiers = set()
for line in reader2:
    sam1_identifiers.discard(line[0])

print sam1_identifiers

rawDescription: Python: Fastest way of parsing first column of large table in array


code:
import BeautifulSoup as bp

soup = bp.BeautifulSoup('<child>something</child>')

for t in soup.findAll('font'):

    t['style'] = 'font-family: %s; font-size: %s' % (t['face'], t['size'])

    del t['face']
    del t['size']
    t.name = 'span'

    print t

rawDescription: How to replace a obsolete font tag with span using Beautiful Soup

description: Replace a obsolete font tag with span using Beautiful Soup


code:
class Person(object):
    def pay_bill():
        raise NotImplementedError

class Millionare(Person):
    def pay_bill():
        print "Here you go! Keep the change!"

class GradStudent(Person):
    def pay_bill():
        print "Can I owe you ten bucks or do the dishes?"

rawDescription: Practical example of Polymorphism

description: Practical example of Polymorphism


code:
for i in range(c.shape[-1]):
    print c[...,i]

rawDescription: Iterating over arbitrary dimension of numpy.array


code:
from flask import Flask, render_template, request, url_for, redirect
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.security import Security, SQLAlchemyUserDatastore, \
    UserMixin, RoleMixin, login_required, current_user, logout_user
from flask.ext.restless import APIManager
from flask.ext.restless import ProcessingException
from flask.ext.login import user_logged_in
# JWT imports
from datetime import timedelta
from flask_jwt import JWT, jwt_required

# Create app
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'super-secret'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
# expiration delay for tokens (here is one minute)
app.config['JWT_EXPIRATION_DELTA'] = timedelta(seconds=60)

# Create database connection object
db = SQLAlchemy(app)

# creates the JWT Token authentication  ======================================
jwt = JWT(app)
@jwt.authentication_handler
def authenticate(username, password):
    user = user_datastore.find_user(email=username)
    print '%s vs. %s' % (username, user.email)
    if username == user.email and password == user.password:
        return user
    return None

@jwt.user_handler
def load_user(payload):
    user = user_datastore.find_user(id=payload['user_id'])
    return user

# Define Flask-security models ===============================================
roles_users = db.Table('roles_users',
        db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
        db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))

class Role(db.Model, RoleMixin):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))
    active = db.Column(db.Boolean())
    confirmed_at = db.Column(db.DateTime())
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))
#Some additional stuff to query over...
class SomeStuff(db.Model):
    __tablename__ = 'somestuff'
    id = db.Column(db.Integer, primary_key=True)
    data1 = db.Column(db.Integer)
    data2 = db.Column(db.String(10))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True)
    user = db.relationship(User, lazy='joined', join_depth=1, viewonly=True)
# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)

# Flask-Restless API ==========================================================
@jwt_required()
def auth_func(**kw):
    return True

apimanager = APIManager(app, flask_sqlalchemy_db=db)

apimanager.create_api(SomeStuff,
    methods=['GET', 'POST', 'DELETE', 'PUT'],
    url_prefix='/api/v1',
    collection_name='free_stuff',
    include_columns=['data1', 'data2', 'user_id'])

apimanager.create_api(SomeStuff,
    methods=['GET', 'POST', 'DELETE', 'PUT'],
    url_prefix='/api/v1',
    preprocessors=dict(GET_SINGLE=[auth_func], GET_MANY=[auth_func]),
    collection_name='protected_stuff',
    include_columns=['data1', 'data2', 'user_id'])

# Create some users to test with
@app.before_first_request
def create_user():
    db.create_all()
    user_datastore.create_user(email='test', password='test')
    user_datastore.create_user(email='test2', password='test2')
    ###
    stuff = SomeStuff(data1=2, data2='toto', user_id=1)
    db.session.add(stuff)
    stuff = SomeStuff(data1=5, data2='titi', user_id=1)
    db.session.add(stuff)
    db.session.commit()

# Views
@app.route('/')
@login_required
def home():
    print(request.headers)
    return render_template('index.html')

@app.route('/logout/')
def log_out():
    logout_user()
    return redirect(request.args.get('next') or '/')

if __name__ == '__main__':
    app.run()

rawDescription: Combining Flask-restless, Flask-security and regular Python requests


code:
def main():
    print "Hello world"

rawDescription: install Python module both as module and as script

description: Install Python module both as module and as script


code: print r.url

rawDescription: Sending a URL in a GET request to a Python server on Google App Engine


code:
with open('../json/routes_test.json') as json_data:
    datas = json.load(json_data)
    area_keys = ['area1','area2','area3','area4','area5']
    type_keys = ['type1','type2','type3','type4']

    for data in datas:
        data['area'] = [data[k] for k in area_keys]
        data['type'] = [data[k] for k in type_keys]
        for k in area_keys + type_keys:
            del data[k]

out = json.dumps(datas, sort_keys=True, indent=4, separators=(',', ': '))
print out
with open('../json/toues_test_intoarrays.json', 'w') as f_out:
    f_out.write(out)

rawDescription: Python - Convert JSON key/values into key/value where value is an array


code:
def test():
    s = r"""

FOO=a \

  b

  """
    import re
    print repr(s)
    print '---------------------'
    regex = re.compile(r'^FOO=(.+)(?<!\\)$', re.M)
    print regex.search(s).group(1)
    print '---------------------'
    regex = re.compile(r'^FOO=(.+)(?<!\\)$', re.M|re.DOTALL)
    print regex.search(s).group(1)

test()

rawDescription: matching a multiline make-line variable assignment with a python regexp

description: Matching a multiline make-line variable assignment with a python regexp


code:
from html5lib import HTMLParser, treebuilders

parser = HTMLParser(tree=treebuilders.getTreeBuilder("beautifulsoup"))

text = "a<b>b<b>c"
soup = parser.parse(text)
print soup.prettify()

rawDescription: Web scraping with Python

description: Web scraping with Python


code:
df = df[['0_1', 4, 6]]
print df.to_string()

rawDescription: Calcuate Duration and Average of CSV file data using python


code:
l = [None, 0, 13, -123]

for i in l:
    if i is not None:
        print i

rawDescription: Python - Same variable in if statement

description: Python - Same variable in if statement


code:
def asterix_triangle(i, t=0):
    if i == 0:
        return 0
    else:
        print ' ' * ( i + 1 ) + '*' * ( t * 2 + 1 )
        return asterix_triangle( i - 1, t + 1 )

asterix_triangle(5)

rawDescription: Asterisk triangle in python

description: Asterisk triangle in python


code:
xs = list("jkl;fpfmklmcvuioqwerklmwqpmksdvjioh0-45mkofwk903rmiok0fmdfjsd")
print leaders(xs)

rawDescription: Python: how to get sorted count of items in a list?


code:
from pyparsing import *

LPAR,RPAR,COMMA,HASH,COLON = map(Suppress, '(),#:')
ident = Word(alphas+'_', alphanums+'_')
fnumber = Regex(r'[+-]?\d+\.\d*').setParseAction(lambda t:float(t[0]))
inumber = Regex(r'[+-]?\d+').setParseAction(lambda t:int(t[0]))
number = fnumber | inumber
ref_name = Combine("$" + delimitedList(ident, delim=oneOf(". ->"), combine=True))
named_ref = Group(ident("name") + COLON + ref_name("ref"))
unnamed_ref = Group(ref_name("ref"))

IF, ELSE = map(Keyword, "if else".split())

stmt = Forward()

decl = named_ref | unnamed_ref

def setType(typ):
    def parseAction(tokens):
        tokens['type'] = typ
    return parseAction
cond = operatorPrecedence(ref_name | number,
            [
            (oneOf("< == > <= >= !="), 2, opAssoc.LEFT),
            ])
if_else = Group(HASH + IF + LPAR + cond("condition") + RPAR +
                    LPAR + stmt("then") + RPAR +
                    Optional(HASH + ELSE + LPAR + stmt("else") + RPAR))

if_else.setParseAction(setType("IF_ELSE"))
decl.setParseAction(setType("DECL"))

stmt << Group(decl | if_else | (HASH + LPAR + delimitedList(stmt) + RPAR))

section = Group(ident("section_name") + LPAR + Group(ZeroOrMore(stmt))("section_body") + RPAR)

parser = OneOrMore(section)

source = """
preview
(
    #if ($e.d.stateFlags == 0) (
        $e.d
    ) #else (
        #( $e.d->scheme, $e.d->host, $e.d->path )
    )
)
children
(
    #(
        scheme: $c.d->scheme,
        host: $c.d->host,
        path: $c.d->path,
        username: $c.d->userName,
        password: $c.d->password,
        encodedOriginal: $c.d->encodedOriginal,
        query: $c.d->query,
        fragment: $c.d->fragment
    )
)"""

def dump_stmt(tokens, depth=0):
    if 'type' in tokens:
        print tokens.type
        print tokens[0].dump(depth=depth)
    else:
        for stmt in tokens:
            dump_stmt(stmt, depth=depth+1)

for sec in parser.parseString(source):
    print sec.dump()
    print sec.section_name
    for statement in sec.section_body:
        dump_stmt(statement)
    print

rawDescription: How can I use pyparsing to data from VC++ autoexp.dat?

description: Use pyparsing to data from VC++ autoexp.dat


code:
strf = str1 + str2
print "'{}'".format(strf)

rawDescription: How can I concatenate ' in a string using python

description: Concatenate ' in a string using python


code:
for start, end in Months:
    print 'Start {}\nEnd'.format(start, end)

rawDescription: Loop through list of tuples, call elements independently


code:
def some_function(a,b,c):
    print(a,b,c)

code = 'some_function(a+b,c,%s)'

def func_one(a,b, c):
    exec code % 1

def func_two(a,b,c):
    exec code % 2

func_one(1,2,3)
func_two(1,2,3)

rawDescription: A way to automatically pass parameters to a function?


code:
handle = Entrez.efetch(db="Protein", id= "75192198", rettype = "fasta")

for r in SeqIO.parse(handle, "fasta"):
    print r.id, r.seq

rawDescription: How do I navigate results of a Biopython Entrez efetch?

description: Navigate results of a Biopython Entrez efetch


code:
#! /usr/bin/env python

def make_square(seq, size):
    return zip(*[iter(seq)] * size)

def main():
    fname = 'mydata'
    size = 3
    with open(fname, 'r') as f:
        for line in f:
            nums = [int(s) for s in line.split()]
            matrix = make_square(nums, size)
            print matrix
            #Now call the function to test if the data in matrix
            #really is a magic square.
            #test_square(matrix)

if __name__ == '__main__':
    main()

rawDescription: How to get a split up a list of numbers and insert into another list

description: Get a split up a list of numbers and insert into another list


code:
for x in xrange(1, 32):
    today = date(year=2010, month=3, day=x)
    offset = (today.weekday() - 2) % 7
    last_wednesday = today - timedelta(days=offset)

    print last_wednesday

rawDescription: How to get the Python date object for last Wednesday

description: Get the Python date object for last Wednesday


code:
data = {'test':'12345'}
url = 'http://myurl.com'
r = requests.post(url,params=data)
print r.url
r = requests.post(url,data=data)
print r.url

rawDescription: Difference between linux curl and python requests, why GET request has different result?


code:
from collections import Counter

GROUP_SIZE = 5
counter = Counter()
mylist = []

with open("tags.txt", "r") as tagfile:
    tags = (line.strip() for line in tagfile)
    try:
        while len(mylist) < GROUP_SIZE-1:
            mylist.append(tags.next())
    except StopIteration:
        pass

    for tag in tags:   # main loop
        mylist.pop(0)
        mylist.append(tag)
        counter.update((tuple(mylist),))

if len(counter) < 1:
    print 'too few tags in file'
else:
    for tags, count in counter.most_common(10):  # top 10
        print '{}, count = {:,d}'.format(list(tags), count)

rawDescription: List the most common lists, from a list

description: List the most common lists, from a list


code:
import subprocess

proc = subprocess.Popen('ls', shell=True, stdout=subprocess.PIPE)
output = proc.stdout.read()
array = output.split("\n")[:-1]

for i in range(len(array)):
    print str(i) + " : " + array[i]

rawDescription: Read results from terminal command without saving to a file first in python


code:
def connect_client_tcp(self):
    global client_tcp
    task = asyncio.async(do_connect())
    def cb(result):
        client_tcp = result
    task.add_done_callback(cb)

def tcp_client_disconnected(self, data, info):
    print(data)
    self.client_tcp_info = info
    self.client_tcp_timeout = loop.call_later(self.CLIENT_TCP_TIMEOUT, self.connect_client_tcp)

rawDescription: How to reconnect a socket on asyncio?

description: Reconnect a socket on asyncio


code:
from subproccess import check_output
print check_output('dir')

rawDescription: subprocess.Popen in different console

description: Subprocess.Popen in different console


code:
for od in items:
    for key,value in od.items():
        print key,value

rawDescription: Scrapy merging to 1 list

description: Scrapy merging to 1 list


code:
imgdata = StringIO.StringIO()
fig.savefig(imgdata, format='png')
imgdata.seek(0)

#Something along these lines
print "Content-Type: img/png\n"
imgdata.seek(0)
print imgdata.read()

rawDescription: Return Image from python CGI using javascript


code:
from bs4 import BeautifulSoup

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

for x in xrange(1, 6):
    link = soup.new_tag('a')
    link.string = str(x)
    div.append(link)

    # do not append comma after the last element
    if x != 6:
        div.append(",")

soup.append(div)

print soup

rawDescription: Append markup string to a tag in BeautifulSoup