code:
from threading import Thread
import functools

def timeout(timeout):
    def deco(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            res = [Exception('function [%s] timeout [%s seconds] exceeded!' % (func.__name__, timeout))]
            def newFunc():
                try:
                    res[0] = func(*args, **kwargs)
                except Exception, e:
                    res[0] = e
            t = Thread(target=newFunc)
            t.daemon = True
            try:
                t.start()
                t.join(timeout)
            except Exception, je:
                print 'error starting thread'
                raise je
            ret = res[0]
            if isinstance(ret, BaseException):
                raise ret
            return ret
        return wrapper
    return deco

rawDescription: Timeout a python function in windows

description: Timeout a python function in windows


code:
import timeit
def nonlocal(): pass

def callnonlocal(): nonlocal()

def calllocal():
    def localf(): pass
    localf()

timeit.timeit('callnonlocal()', 'from __main__ import callnonlocal')
timeit.timeit('calllocal()', 'from __main__ import calllocal')

rawDescription: Does creating functions inside functions have a recurring cost?


code:
import re
apache_log = {'@source': 'file://xxxxxxxxxxxxxxx//var/log/apache2/access.log', '@source_host': 'xxxxxxxxxxxxxxxxxxx', '@message': 'xxxxxxxxxxxxxxx xxxxxxxxxx - - [02/Aug/2013:12:38:37 +0000] "POST /user/12345/product/2 HTTP/1.1" 404 513 "-" "PycURL/7.26.0"', '@tags': [], '@fields': {}, '@timestamp': '2013-08-02T12:38:38.181000Z', '@source_path': '//var/log/apache2/access.log', '@type': 'Apache-access'}
post_path = re.compile(r'"POST (/\S+) HTTP')
match = post_path.search(apache_log['@message'])
if match:
    print match.group(1)

rawDescription: Is there any way to make this function look nicer?


code:
from bs4 import BeautifulSoup

data = """

25% Cashback on Recharges :

Get 25% Cashback upto Rs.25 per transaction.

Coupon Code : AXISCB50

Maximum 2 transaction per Debit/Credit card.

"""

soup = BeautifulSoup(data)

print soup.find(text=lambda x: x.startswith('Coupon Code')).next_element.text

rawDescription: Scrap Data from Website using css style Using Beautifull soup


code:
import functools

def decorator(func):
    @functools.wraps(func)
    def wrapper(self, *args, **kwargs):
        save = getattr(self, '_FUNC_', None)
        self._FUNC_ = func
        retval = func(self, *args, **kwargs)
        self._FUNC_ = save
        if save:  self._FUNC_ = save
        else: delattr(self, '_FUNC_')
        return retval
    return wrapper

class C(object):
    @decorator
    def M(self, i):
        if i > 0:
            print i,
            self._FUNC_(self, i-1)  # explicit 'self' argument required
        else:
            print '- Blast Off!'

C().M(3)  # -> 3 2 1 - Blast Off!

rawDescription: Python:method call itself without method name


code:
class Ratings(UserDict.DictMixin, dict):
    '''The implementation carefully mixes inheritance and delegation
    to achieve reasonable performance while minimizing boilerplate,
    and, of course, to ensure semantic correctness as above. All
    mappings' methods not implemented below get inherited, mostly
    from DictMixin, but, crucially!, __getitem__ from dict. '''

    def __init__(self, *args, **kwds):
        ''' This class gets instantiated just like 'dict' '''
        dict.__init__(self, *args, **kwds)
        # self._rating is the crucial auxiliary data structure: a list
        # of all (value, key) pairs, kept in "natural"ly-sorted order
        self._rating = [ (v, k) for k, v in dict.iteritems(self) ]
        self._rating.sort()

    def copy(self):
        ''' Provide an identical but independent copy '''
        return Ratings(self)

    def __setitem__(self, k, v):
        ''' besides delegating to dict, we maintain self._rating '''
        if k in self:
            del self._rating[self.rating(k)]  ##
        dict.__setitem__(self, k, v)
        insort_left(self._rating, (v, k))

    def __delitem__(self, k):
        ''' besides delegating to dict, we maintain self._rating '''
        del self._rating[self.rating(k)]
        dict.__delitem__(self, k)
        ''' delegate some methods to dict explicitly to avoid getting
        DictMixin's slower (though correct) implementations instead '''
        __len__ = dict.__len__
        __contains__ = dict.__contains__
        has_key = __contains__
        ''' the key semantic connection between self._rating and the order
        of self.keys( ) -- DictMixin gives us all other methods 'for
        free', although we could implement them directly for slightly
        better performance. '''

    def __iter__(self):
        for v, k in self._rating:
            yield k
        iterkeys = __iter__

    def keys(self):
        return list(self)

    #the three ratings-related methods
    def rating(self, key):
        item = self[key], key
        i = bisect_left(self._rating, item)
        if item == self._rating[i]:
            return i
        raise LookUpError, "item not found in rating"

    def getValueByRating(self, rating):
        return self._rating[rating][0]

    def getKeyByRating(self, rating):
        return self._rating[rating][1]

    def _test( ):
        ''' we use doctest to test this module, which must be named
        rating.py, by validating all the examples in docstrings. '''
        import doctest, rating
        doctest.testmod(rating)
        print "doc test?"

if __name__ == "__main__":

    r = Ratings({"bob":30, "john":30})

    print "r is"
    print r
    print "\n"
    print "len(r) is"
    print len(r)
    print "\n"
    print "updating with {'paul': 20, 'tom': 10} "
    r.update({"paul": 20, "tom": 10})
    print "\n"
    print "now r is"
    print r
    print "\n"
    print "r.has_key('paul') is"
    print r.has_key("paul")
    print "\n"
    print " 'paul' in r is"
    print ("paul" in r)
    print "\n"
    print "r.has_key('alex') is"
    print r.has_key("alex")
    print "\n"
    print " 'alex' in r is"
    print ("alex" in r)
    print '\n'
    print 'r is'
    print r
    print "changing john to '20' with 'r['john']= 20' doesn't work. "
    r["john"]=20

rawDescription: Recipe from Python Cookbook

description: Recipe from Python Cookbook


code:
joint_names=  get_unique_scene_names('joint')

existing = cmds.ls( type='joint', l = True)
existing .sort()
existing .reverse()
# do this to make sure it works from leaves backwards!

for item in existing :
    apply_convention(item,  'jnt_', joint_names)

# check the uniqueness constraint by looking for how many items share a short name in the dict:
for d in joint_names:
    print d, len (joint_names[d])

rawDescription: Iterating over a large unicode list taking a long time?


code: if 'workflow' in children: print 'workflow'

rawDescription: How to compare strings in python in List?

description: Compare strings in python in List


code:
@profile
def do_stuff(numbers):
    print numbers

numbers = 2
do_stuff(numbers)

rawDescription: How do I use line_profiler (from Robert Kern)?

description: Use line_profiler (from Robert Kern)


code:
newlst = [[key, unused, maxtable[key]] for key, unused, val in lst]
print newlst

rawDescription: How to change the items in a list of sublists based on certain rules and conditions of those sublists?

description: Change the items in a list of sublists based on certain rules and conditions of those sublists


code:
class MyList(list):
    def __getitem__(self, *args):
            return [list.__getitem__(self, i) for i in args[0]]

mine = MyList(['a','b','c','d','e','f'])
print mine[0, 3, 5]

rawDescription: Grabbing specific indices of a list in Python


code:
from bs4 import BeautifulSoup

html = '''

Demand No
<input type="text" name="T1" size="12" onFocus="this.blur()" value="876716001">

'''

soup = BeautifulSoup(html)
print soup.find('td', attrs={'width': '65%'}).findNext('input')['value']

rawDescription: Can't get a regex pattern to work in Python


code:
import json
from urllib2 import Request, urlopen, URLError

request = Request('http://192.168.1.***:8081/api/*****/?cmd=future&sort=date&type=missed')

try:
    response = urlopen(request)
    tv_shows = json.loads(response.read())
    for show_data in tv_shows['data']['missed']:
        if 'show_name' in show_data:
            print show_data['show_name']

except URLError, e:
    print ('Error')

rawDescription: searching sickbeard api output and print only keyword line

description: Searching sickbeard api output and print only keyword line


code:
for i,g in zip(ids, gos):
   for ge in g:
      print i,ge

rawDescription: print multiple values for each key in a dictionary on a new line, preceded by the key

description: Print multiple values for each key in a dictionary on a new line, preceded by the key


code:
while True:
    dir = raw_input('Copy and Paste Course Directory Here: ')
    files = os.listdir(dir)
    if all(file.endswith('.html') for file in files):
        # do the search and replace stuff
        break
    else:
        print 'Sorry, there are non-HTML files here. Try again.'

rawDescription: How to confirm only html files exist in a given folder and if not then how to prompt the user to specify a folder with only html files within

description: Confirm only html files exist in a given folder and if not then how to prompt the user to specify a folder with only html files within


code:
sample = [1, 2, [3, 4, [5, 6]], [7, 8], 9]
for _, res in yield_and_flatten(sample):
    print res

mlist = [1, 2, 3, [[4, [5, 6]], 7], 8, 9]
for _, res in yield_and_flatten(mlist):
    print res

rawDescription: Python: how to generate a list of "flattened" lists out of a big nested list


code:
import csv

original = file('southpark.csv', 'rU')
reader = csv.reader(original)

stan = reader.next()
kyle = reader.next()
eric = reader.next()
kenny = reader.next()
butters = reader.next()

print eric

rawDescription: How to Store Rows from CSV File into Python and Print Data with HTML


code: print a

rawDescription: numpy - issue with creating a 2D matrix

description: Numpy - issue with creating a 2D matrix


code:
def f(df):
    return df[df.C>df.C.mean()]
df3 = df.groupby("A", group_keys=False).apply(f)
print df3

rawDescription: Drop some Pandas dataframe rows using group based condition


code:
for child in soup.findAll(attrs={'class': "f14 blue001"}):
    print child.getText()

rawDescription: to use Python (Beautifulsoup) to pickup texts on webpage

description: To use Python (Beautifulsoup) to pickup texts on webpage


code:
cursor = conn.cursor(name='cursor_x')
query = "select * from t"
cursor.execute(query)
for row in cursor:
    print row

rawDescription: How to use postgres cursor within Python code

description: Use postgres cursor within Python code


code:
def apple():
    print "I AM APPLES!"

# this is just a variable
tangerine = "Living reflection of a dream"

rawDescription: Python module object without a file

description: Python module object without a file


code:
# Dictionary of edges, indexed in both directions by node number.
edge = {}

# Ingest the data and build teh dictionary
with open("edges.txt") as efile:
    for line in efile:
        eid, src, dst = line.strip().split(';')
        src = int(src)
        dst = int(dst)

        for key, val in [(src, dst), (dst, src)] :
            if key in edge:
                edge[key].append(val)
            else:
                edge[key] = ([val])
print "edge dictionary has entries:", len(edge)

# Identify endpoint nodes: order other than 2
end_ct = 0
print "Endpoint Nodes"
endpoint = []
for src, dst in edge.iteritems():
    if len(dst) != 2:
        print len(dst), src, dst
        endpoint.append(src)
        end_ct += len(dst)
print end_ct, "road ends"

atlas = []    # List of roads, each a list of nodes

# Build roads between the identified endpoints
# Pick the first endpoint in the remaining list.
# Move to the first-listed adjacent node.
# Keep going until we hit another node on the endpoint list.
while len(endpoint) > 0:
    here = endpoint[0]
#   print "Road starting at", here, edge[here]

    # Pick a first step and consume the edge
    next = edge[here].pop(0)
    edge[next].remove(here)
    road = [here, next]

    # If that's the last connection to the node, remove that node from the endpoints list.
    if len(edge[here]) == 0:
        del endpoint[0]
        del edge[here]
    # Special case for a one-segment road; endpoint entry of "next" is removed after the loop
    if len(edge[next]) == 0:
        del edge[next]

    # Consume edges until we reach another endpoint.
    debug = False
    while next not in endpoint:
        here = next
        next = edge[here].pop(0)
        edge[next].remove(here)
        road.append(next)
        if len(edge[next]) == 0:
            del edge[next]
#           print "removing node", next

    if next not in edge:
        endpoint.remove(next)
#       print "removing endpoint", next

    print "\nRoad from", road[0], "to", road[-1], ':\n\t', road
    atlas.append(road)

print "\n", len(atlas), "roads built"
# print "edge dictionary still has entries:", len(edge)

rawDescription: Finding connectedness from a list of nodes and a list of edges


code:
import pandas as pd
def funcx(x, test_str):
    return test_str in x['bla']

tst = pd.DataFrame({'id': [0, 11, 222, 3333, 44444],
                'bla': ['ab', 'ba', 'ca', 'bc', 'db']})
test = pd.Series(['a', 'b', 'c', 'd'])
result = {}
for xstring in test:
    result[xstring] = tst.apply(funcx, args=( xstring), axis=1)

print result

rawDescription: pandas groupby of DataFrame using Series of substrings

description: Pandas groupby of DataFrame using Series of substrings


code:
for i, n in enumerate(shoestring):
    if not n.isalpha():
        print n, i

rawDescription: Python - Why is this function not returning the index for white space?


code:
class Wrapped (object):
    __metaclass__ = Wrapper

    def crawl_1(self):
        print 'this is crawl 1'

    def crawl_2(self):
        print 'this is crawl 2'

    def this_is_not_wrapped(self):
        print 'this is not wrapped'

    def save_to_db(self):
        print 'saving to database'

rawDescription: How to get a method called (decorator?) after every object method

description: Get a method called (decorator?) after every object method


code:
import re

args = {}
args['pythonFile'] =  sys.argv[0]

for arg in sys.argv[1:]:
  variable = re.search('\-\-(.*)\=',arg)
  variable = variable.group(1)
  value = re.search('\=(.*)',arg)
  value = value.group(1)
  args[variable] = value

print args

rawDescription: Passing Python variables via command line?


code: map(lambda (i, el): i * el, enumerate(a))

rawDescription: Can I use index information inside the map function?

description: Use index information inside the map function


code:
import itertools as it
import collections

L = ['a', 'ab', 'bba']
M = ['baa', 'aa', 'bb']

def cmbs(L=L, M=M):
  Ucans = collections.defaultdict(list)
  Vcans = collections.defaultdict(list)
  sides = (L, Vcans, Ucans), (M, Ucans, Vcans)
  for i in it.count(1):
    for k, (G, Ocans, Tcans) in enumerate(sides):
      for u in it.product(G, repeat=i):
        j = ''.join(u)
        if j in Ocans:
          for samp in Ocans[j]:
            result = samp, u
            yield result[1-k], result[k]
        Tcans[j].append(u)

if __name__ == '__main__':
  for x, y in it.islice(cmbs(), 10):
    print x, y, ''.join(x), ''.join(y)

rawDescription: Python - Simple algorithmic task on lists (standard question for a job-interview)


code:
from django.test import TestCase
from django.db.models import Count
from foo.models import Foo

class MyTestCase(TestCase):
    def test_group_query(self):
        options = ('created', 'deleted', 'updated')
        for i in range(32):
            Foo.objects.create(
                action_type=options[i%3]
                )
        results = Foo.objects.values('action_type').annotate(Count('action_type'))
        print results

rawDescription: What is an efficient way to get a count of objects grouped by a field in Django?


code:
from sklearn import linear_model
import csv
import numpy as np
import matplotlib.pyplot as plt

def process_chunk(chuk):

    training_set_feature_list = []
    training_set_label_list = []
    test_set_feature_list = []
    test_set_label_list = []
    count = 1
    # to divide into training & test
    chuk = map(lambda x: x[2:], chuk) # Removing first 2 columns
    chunk = np.array(chuk,dtype = np.float) # Make floats array from strings
    ########## Testing dataset: Data after 30th row =########################################
    test_set_feature_list = chunk[30:,3:5]  #4rd and 5th column of chunk
    test_set_label_list = chunk[30:,2] #3rd column of chunk

    ########## Training dataset: All data before 30th row########################################
    training_set_feature_list = chunk[:30,3:5]
    training_set_label_list = chunk[:30, 2]

    # Create linear regression object
    regr = linear_model.LinearRegression()
    # Train the model using the training sets
    regr.fit(training_set_feature_list, training_set_label_list)

    predictedTestSet = regr.predict(test_set_feature_list)

     # The coefficients
    print 'Coefficients: {}'.format(regr.coef_)
    # The mean square error
    print 'Residual sum of squares: %.2f' % np.mean(predictedTestSet - test_set_label_list) ** 2
    # Explained variance score: 1 is perfect prediction
    print 'Variance score: %.2f' % regr.score( test_set_feature_list, test_set_label_list)
    X = [x for (y,x) in sorted(zip(test_set_label_list, predictedTestSet))]
    Y = [y for (y,x) in sorted(zip(test_set_label_list, predictedTestSet))]
    plt.plot(range(len(X)),X , 'r.', label='predicted')
    plt.plot(range(len(Y)),Y , 'g-',label='test_set')
    plt.legend()
    plt.show()
    return predictedTestSet

# Load and parse the data
file_read = open('file1.csv', 'r')

reader = csv.reader(file_read)

chunk, chunksize = [], 12

for i, line in enumerate(reader):
    if ( i > 0):
        chunk.append(line)

predictedSet = process_chunk(chunk)
print predictedSet

rawDescription: error in using scikit-learn linear regression model in python

description: Error in using scikit-learn linear regression model in python


code:
category= Category.objects.get(id='id')
tag= Tag.objects.get_or_create(tag='some_tag')
test= Test.objects.get_or_create(testname= 'some test', category= category )
tagorder= TagOrder.objects.create(test= test, tag= tag, added= datetime.now())

tags= TagOrder.objects.filter(test=test).order_by('added').select_related('tag')

for t in tags:
   print t.tag.tag

rawDescription: Django show the tags in the order in which they are added to particular test


code:
from itertools import product
from collections import OrderedDict
a = OrderedDict({'id': ['1221AB12'], 's': ['Party!'], 'flag': ['urgent', 'english'], 't': ['0523456789', '0301234567'], 'f':              ['0412345678']})
res = product(*a.values())
for line in res:
    print " ".join(["%s=%s" % (m, n) for m,n in zip(a.keys(), line) ])

rawDescription: Python: Convert multiple instances of the same key into multiple rows


code:
for key in data:
    if key != 'Time':
        pattern = re.compile(val)
        for name, _ in specie_name_and_initial_values:
            print re.findall(pattern, name)

rawDescription: Regular expressions matching words which contain the pattern but also the pattern plus something else


code:
list1 = list2 = []
print id(list1)  # Same as id(list2)
print id(list2)  # Same as id(list1)

list1, list2 = [], []
print id(list1)  # Different than id(list2)
print id(list2)  # Different than id(list1)

rawDescription: Declaring an empty list

description: Declaring an empty list


code:
import rpy2.robjects as robjects
import numpy as np

# load your file
robjects.r['load']('fname.RData')

# retrieve the matrix that was loaded from the file
matrix = robjects.r['fname']

# turn the R matrix into a numpy array
a = np.array(matrix)

print a

rawDescription: How to access a matrix in an .Rdata file in Python using rpy2

description: Access a matrix in an .Rdata file in Python using rpy2


code:
myPets = ['dog', 'cat', 'rabbit']    # create a list of pets
for pet in myPets:
    print pet                  # print each pet

rawDescription: Learning python... stumped. Where is the loop in this function?


code:
def to_bytes(n, width):
    b = bytearray(width)
    for i in range(width-1, -1, -1):
        b[i] = n & 0xff
        n >>= 8
        if n == 0:
            break
    return bytes(b)

n = 0x8182838485868788
print repr(to_bytes(n, 8))

rawDescription: How to do bit manipulation of 64 bit data and output to a C function in a dll

description: Do bit manipulation of 64 bit data and output to a C function in a dll


code:
aList = [["Monday", "2.03","4.03"],
         ["Tuesday","4.03","5.03"],
         ["Wednesday","3.2","4.3"]]

def helper(s):
    try: return float(s)
    except ValueError: return s

aList[:] = [[helper(item) for item in subl] for subl in aList]
print aList

rawDescription: Iterating over a Nested List and Changing Values


code:
import numpy as np
from statsmodels.formula.api import logit

FNAME2 = "C:/Users/lenovo/Desktop/Nouveau dossier (2)/table.csv"
FinalTableau = np.savetxt(FNAME2, my_data[index_to_use], delimiter=",")
my_data2 = np.genfromtxt(FNAME2, delimiter=',')

# converts my binary data series from (1, 2) to (0,1)
my_data2[:, 1] -= 1
# print my_data2

N = len(my_data2)//2
learning_sample, test_sample = my_data2[:N], my_data2[N:]
x = learning_sample[:, 1]
a = learning_sample[:, 3]
# x with values 1 and 2
print x

form = 'x ~ a'
affair_model = logit(form, learning_sample)
affair_result = affair_model.fit()

print affair_result.summary()
print affair_result.predict()

rawDescription: How can I divide my data table called (my_data2) in two samples

description: Divide my data table called (my_data2) in two samples


code:
import numpy as np
import pandas as pd
from scipy import stats

df = pd.DataFrame({'sample': np.random.choice([1, 2], 100, replace=True),
                   'x': np.random.uniform(size=100),
                   'y': np.random.normal(size=100),
                   'z': np.random.choice([1,5,7,3,9],100, replace=True)})

def ttest(x):
    y = stats.ttest_ind(df.ix[df['sample']==1, x],
        df.ix[df['sample']==2, x],
        equal_var= False)
    return y

for col in df.columns:
    print col, ttest(col)

rawDescription: Calling a function on every column of a pandas DataFrame


code:
s = 'Hi,\r\n\r\nThis is my email.\r\n.\r\n'
print s.rsplit('\r\n', 2)[0]

rawDescription: Extracting email body in python

description: Extracting email body in python


code:
import requests
from lxml import html

def searchRecordInSpecificPosition(url, position):
    ## Making request to the specified URL
    response = requests.get(url)

    ## Parsing the DOM to a tree
    tree = html.fromstring(response.content)

    ## Creating a dict of links.
    links_dict = dict()

    ## Format of the dictionary:
    ##
    ##  {
    ##      1: {
    ##          'href': "http://pr4e.dr-chuck.com/tsugi/mod/python-data/data/known_by_Medina.html",
    ##          'text': "Medina"
    ##      },
    ##
    ##      2: {
    ##          'href': "http://pr4e.dr-chuck.com/tsugi/mod/python-data/data/known_by_Chiara.html",
    ##          'text': "Chiara"
    ##      },
    ##
    ##      ... and so on...
    ## }

    counter = 1

    ## For each <a> tag found, extract its text and link (href) and insert it into links_dict
    for link in tree.xpath('//ul/li/a'):
        href = link.xpath('.//@href')[0]
        text = link.xpath('.//text()')[0]
        links_dict[counter] = dict(href=href, text=text)
        counter += 1

    return links_dict[position]['text'], links_dict[position]['href']

times_to_search = int(raw_input("Enter the amount of times to search: "))
position = int(raw_input('Enter position: '))

count = 0

print ""

while count < times_to_search:
    if count == 0:
        name, url = searchRecordInSpecificPosition("http://pr4e.dr-chuck.com/tsugi/mod/python-data/data/known_by_Brenae.html", position)
    else:
        name, url = searchRecordInSpecificPosition(url, position)
    print "[*] Name: {}".format(name)
    print "[*] URL: {}".format(url)
    print ""
    count += 1

rawDescription: Automatizing a Python Web Crawler - How to prevent raw_input all the time?


code:
previous = None
for percentage in percentages_minus_index:
    other_segment_angles = percentage * 360
    print previous
    previous = other_segment_angles

rawDescription: How to store last value of iteration?

description: Store last value of iteration


code:
csv='''\
A\tB\tC
D    E    F
'''

data=[]
for line in csv.splitlines():
    data.append(line.split())

print data
# [['A', 'B', 'C'], ['D', 'E', 'F']]

rawDescription: How to read in a file with a mixture of different delimiters using Python csv module?

description: Read in a file with a mixture of different delimiters using Python csv module


code:
processes = []
for _i, _v in enumerate(info):
    print 'start....', _i
    _p = multiprocessing.Process(target=select, args=(_v, ))
    _p.start()
    processes.append(_p)
for _p in processes:
    _p.join()

rawDescription: Python, how to implement parallel process


code:
from bs4 import BeautifulSoup
sample = '''\

'''
soup = BeautifulSoup('
{}
'.format(sample))
table = soup.find(class_='apple-font', id=True).find_parent('table')
groups = {}
group = None
for tr in table.find_all('tr'):
    id_span = tr.find(class_='apple-font', id=True)
    if id_span is not None:
        # new group
        group = []
        groups[id_span['id']] = group
    else:
        vid_link = tr.find('a', vid=True)
        if vid_link is not None:
            group.append(vid_link['vid'])

print groups

rawDescription: Using BeautifulSoup with Python to parse page for attribute values


code:
# Simple , crude solution

f = open('file1', 'r')
manipulated_lines = []
readFirstLine = False
config = ''
configComma = ''
uncommentLine = 0
for line in f:
    tokens = line.split()

    if uncommentLine == 1:
        # this is comment line
        if tokens[0] == '#export':
            manipulated_lines.append(line[1:])
            uncommentLine = uncommentLine + 1
            continue
    elif uncommentLine > 1:
        manipulated_lines.append(line)
        continue

    if not readFirstLine:
        config = line.rstrip('\n')
        configComma = config + ','
        readFirstLine = True

    # Process additional lines
    manipulated_lines.append(line)

    if len(tokens) > 0 and tokens[0] == '#':
        if tokens[1] == 'for':
            if config in tokens or configComma in tokens:
                uncommentLine = uncommentLine + 1
                continue

print manipulated_lines
f.close()
fw = open('file2', 'w')
fw.writelines(manipulated_lines)
fw.close()

rawDescription: python to uncomment the right line

description: Python to uncomment the right line


code:
mul = lambda lst, val: [i * val for i in lst]
add = lambda lst, val1, val2: [i + val1 + val2 for i in lst]

def functions(lst, *fns):
    for fn, args in fns:
        lst = fn(lst, *args)
    return lst

lst = [1, 2, 3, 4, 5, 6]
result = functions(lst, [mul, (10,)], [add, (10, 100)])
print result

rawDescription: Applying a series of functions to a list


code:
from json import JSONDecoder

def parse_object_pairs(pairs):
    return pairs

data = """
{"foo": {"baz": 42}, "foo": 7}
"""

decoder = JSONDecoder(object_pairs_hook=parse_object_pairs)
obj = decoder.decode(data)
print obj

rawDescription: Python json parser allow duplicate keys

description: Python json parser allow duplicate keys


code:
import collections

# namedtuple is a regular tuple that can be accessed by name rather than just by position
NameRecord = collections.namedtuple("NameRecord", "id name age score1 score2 score3")

# Return the first record in file where the first element of the line
# matches the given id
def f(id):
  with open('file.txt') as r:
    for line in r:
        # Split the line and pass as arguments to a new NameRecord
        record = NameRecord(*line.split())
        if record.id == id:
            return record
  return None

def f2():
    id = raw_input("Your ID")
    record = f(id)
    if record:
        #print '{} {} {} {} {}'.format(*record[1:])
        print '{} {} {} {} {}'.format(record.name,
                                      record.age,
                                      record.score1,
                                      record.score2,
                                      record.score3)
    else:
        print 'Id not found:', id

f2()

rawDescription: How can i get a specific value from a key that has multiple values in it?

description: Get a specific value from a key that has multiple values in it


code:
import pygame
import pygame.locals

pygame.init()

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

d_pressed = False

running = True

while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False
            elif event.key == pygame.K_d:
                d_pressed = True

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_d:
                d_pressed = False

        keys = pygame.key.get_pressed()

        if d_pressed:
            print '> d_pressed <'

        if keys[pygame.K_RETURN]:
            print '> K_RETURN <'

        if keys[pygame.K_d]:
            print '> K_d <'

rawDescription: Not sure why key press is not doing anything


code: print "a %s %d" % ()

rawDescription: Escaping % in MySQLdb

description: Escaping % in MySQLdb


rawDescription: Splitting lists by short numbers

description: Splitting lists by short numbers


rawDescription: python: split string after comma and dots

description: Python: split string after comma and dots


code:
from datetime import datetime, timedelta

print datetime.today() - timedelta(30)

rawDescription: Is there an inverse of python's date.timetuple().tm_yday?


code:
import timeit
from operator import itemgetter
from itertools import imap

x = range(100)
y = reversed(range(100))
d = [dict((('x',xx), ('y', yy))) for (xx, yy) in zip(x,y)]
# d is [{'y': 99, 'x': 0}, {'y': 98, 'x': 1}, ... ]
D={x:y for x,y in zip(range(10),reversed(range(10)))}

def test_list_comp(d):
    return [dd['x'] for dd in d]

def test_list_comp_v2(d):
    return [(x["x"], x["y"]) for x in d]

def testD_keys_values(d):
    return d.keys()

def test_map(d):
    return map(itemgetter('x'), d)

def test_positional(d):
    return [dd.values()[1] for dd in d]

def test_lambda(d):
    return list(imap(lambda x: x['x'], d))

def test_imap_iter(d):
    return list(imap(itemgetter('x'), d))

for test in sorted(globals()):
    if test.startswith("test_"):
        print "%30s : %s" % (test, timeit.Timer("f(d)",
              "from __main__ import %s as f, d" % test).timeit())
for test in sorted(globals()):
    if test.startswith("testD_"):
        print "%30s : %s" % (test, timeit.Timer("f(D)",
              "from __main__ import %s as f, D" % test).timeit())

rawDescription: transpose dictionary (extract all the values for one key from a list of dictionaries)

description: Transpose dictionary (extract all the values for one key from a list of dictionaries)


code:
if p.isupper and p.islower:
    print "Your Password is of medium strength, try adding some numbers"

rawDescription: Python password strength

description: Python password strength


rawDescription: how to create a sub list for a specific string in a nested list

description: Create a sub list for a specific string in a nested list


code:
engines = hxs.select("//a/@href").extract()
print engines

rawDescription: XPath Only Selects Part of Href

description: XPath Only Selects Part of Href


code:
import pandas as pd
# BDay is business day, not birthday...
from pandas.tseries.offsets import BDay

# pd.datetime is an alias for datetime.datetime
today = pd.datetime.today()
print today - BDay(4)

rawDescription: Business days in Python

description: Business days in Python


code:
data = np.arange(27).reshape(3,3,3)
print 'Original:\n', data
print 'Zoomed by 2x gives an array of shape:', ndimage.zoom(data, 2).shape

rawDescription: Resampling a numpy array representing an image


code:
import inspect

def myargs(val1, val2, val3=None, val4=5):
        print inspect.currentframe().f_locals

rawDescription: Is it possible get a dictionary of passed in parameters similar to kwargs(python)?


code:
class recipe:
    def __init__(self,name,ingredients):
        self.name = name
        self.ingredients = {}
        for i in ingredients:
            self.addIngredient(i)
    def addIngredient(self, ingredient):
        count = self.ingredients.get(ingredient,0)
        self.ingredients[ingredient] = count + 1
    def __str__(self):
        out =  "{name}: \n".format(name=self.name)
        for ingredient in self.ingredients.keys():
            count = self.ingredients[ingredient]
            out += "\t{c} x {i}\n".format(c=count,i=ingredient)
        return out

sandwich = recipe("sandwich",("bread","butter","ham","cheese","butter","bread"))
print str(sandwich)

rawDescription: is it possible to create a dictionary "template" in python?

description: Is it possible to create a dictionary "template" in python


code:
from PyQt4.QtGui import QComboBox

class Bar(QComboBox):

    def connect_activated(self):
        # The PyQt4 documentation will define what the default overload is.
        # In this case it is the overload with the single integer argument.
        self.activated.connect(self.handle_int)

        # For non-default overloads we have to specify which we want to
        # connect.  In this case the one with the single string argument.
        # (Note that we could also explicitly specify the default if we
        # wanted to.)
        self.activated[str].connect(self.handle_string)

    def handle_int(self, index):
        print "activated signal passed integer", index

    def handle_string(self, text):
        print "activated signal passed QString", text

rawDescription: What is the meaning of the brackets in the code line 'combo.activated[str].connect(self.onActivated)' for pyside?


rawDescription: Python re finding string between underscore and ext


code:
def fprint(output):
   print output
   with open("somefile.txt", "a") as f:
       f.write("{}\n".format(output))

rawDescription: Python output on file and terminal

description: Python output on file and terminal


code:
decoder = MyJSONDecoder()
print decoder.decode('{"field1":null, "field2": "data!"}')

rawDescription: Python JSONDecoder custom translation of null type


code:
from time import sleep
import subprocess
import datetime
import os

class Worker:

    def __init__(self, cmd):

        print datetime.datetime.now(), ":: starting subprocess :: %s"%cmd
        self.cmd = cmd
        self.log = "[running :: %s]\n"%cmd
        self.subp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        self.start_time = datetime.datetime.now()

    def wait_to_finish(self, timeout_seconds = None):

        while True:
            retcode = self.subp.poll()
            if retcode is not None:
                self.get_process_output()
                self.log += "\n[subprocess finished, return code: %d]\n"%retcode
                print datetime.datetime.now(), ":: subprocess %s exited, retcode=%d"%(self.cmd, retcode)
                return
            else:
                # process hasn't finished yet
                sleep(1)
                if timeout_seconds is not None:
                    cur_time = datetime.datetime.now()
                    if (cur_time - self.start_time).seconds > timeout_seconds:
                        print datetime.datetime.now(), ":: subprocess %s :: killing after %d seconds"%(self.cmd, timeout_seconds)
                        self.kill()
                        return

    def still_running(self):
        return (self.subp.poll() is None)

    def kill(self):
        self.subp.terminate()
        self.get_process_output()
        self.log += "\n[subprocess killed by explicit request]\n"
        return

    def get_process_output(self):
        out, err = self.subp.communicate()
        self.log += out
        self.log += err

rawDescription: Custom Scheduler to have sequential + semi-sequential scripts with timeouts/kill switches?


code:
def contains(s, sub):
    if sub in s:
        print sub, "is a substring of s."
        return True

if __name__ == '__main__':
    s = ["apples", "bananas", "oranges", "watermelon", "apricots"]
    sub = raw_input("Please enter a substring of s: ")
    contains(s, sub)

rawDescription: Error: name 's' is not defined

description: Error: name 's' is not defined


code:
import re
s = "The quick, brown fox jumps over the lazy dog."

print re.search(r'(?:\w+\W*){0}((?:\w+\W*){3})', s).group(1)
# The quick, brown
print re.search(r'(?:\w+\W*){1}((?:\w+\W*){3})', s).group(1)
# quick, brown fox
print re.search(r'(?:\w+\W*){2}((?:\w+\W*){3})', s).group(1)
# brown fox jumps

rawDescription: string mask and offset with regex

description: String mask and offset with regex


code:
import re
from urlparse import unquote

from lxml import html
import requests

url = "http://rads.stackoverflow.com/amzn/click/1597805483"
page = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36'})
tree = html.fromstring(page.content)

script = tree.xpath('//script[contains(., "bookDescEncodedData")]')[0]
match = re.search(r'bookDescEncodedData = "(.*?)",', script.text)
if match:
    description_html = html.fromstring(unquote(match.group(1)))
    print description_html.text_content()

rawDescription: how to scrape imbeded script on webpage in python

description: Scrape imbeded script on webpage in python


rawDescription: Sorting a nesting list by the first item -- itemgetter not doing the trick


code:
strA = '0.8243'
strB = '12.3'
C = float(strA) + float(strB)
print '{:.{}f}'.format(C, len(strB.partition('.')[2]))

rawDescription: How do I copy the formatting of a number to another number?

description: Copy the formatting of a number to another number


code:
for waypt in data:
    print waypt.ID, waypt.EquipClassStr

rawDescription: Class Iterator for Nested (2-D) Dictionary?


code:
import urllib

url = ('http://www.epicurious.com/articlesguides/bestof/'
       'toprecipes/bestchickenrecipes/recipes/food/views/'
       'My-Favorite-Simple-Roast-Chicken-231348')

sock = urllib.urlopen(url)
ch = sock.read()
sock.close()

gen = (str(i)+' '+repr(line) for i,line in enumerate(ch.splitlines(1)))

print '\n'.join(gen)

rawDescription: Python HTML parsing specific information within tags


code:
import numpy as np
scores, ratings = np.array([(t.score, t.rating) for t in text_collection]).T

print 'average score: ', np.mean(scores)
print 'average rating: ', np.mean(ratings)
print 'average positive score: ', np.mean(scores[scores > 0])
print 'average negative score: ', np.mean(scores[scores < 0])

rawDescription: Finding average of variable in objects python


code:
def get_measurement(df, conditions):
    mask = pd.Series(True, index=df.index)
    for k, v in conditions.iteritems():
        mask &= (df[k] == v)
    return df[mask]

df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'measurement': [100,200,300]})
print df
print get_measurement(df, {'a': 1, 'b': 4})

rawDescription: Filter Pandas DataFrame depending on whether the filter value is specified


code:
import sys as sys
import struct
import decimal

#Small value for if statements
eps=1e-2

#Create empty sets to append with column values
freq = []
J_i = []
w_i = []
n_i = []
J_f = []
w_f = []
n_f = []
Tline = []

#Import data, split into columns you want, then assign column values to arrays
#3s = next three spaces are a string that I want to look at
#19x = skip the next 19 spaces in document
#Decimal used to keep last digit of frequency even if it is zero
#Used float to make numbers out of string. Can now do operations on numbers.
with open("path/to/file","r") as f:
    for line in f:
         (col0,col1,col2,col3,col4,col5,col6) = struct.unpack("3s19x4s2x2s8x3s9x4s5s65x10s14x",line.strip())
         freq.append(decimal.Decimal(col6))
         J_i.append(float(col0))
         w_i.append(float(col1))
         n_i.append(float(col2))
         J_f.append(float(col3))
         w_f.append(float(col4))
         n_f.append(float(col5))

#Define each line with the transition
for j in xrange(len(freq)):
    if abs(J_f[j] - J_i[j]) < eps:
        if abs(w_i[j] - 0.5) < eps and abs(w_f[j] - 0.5) < eps:
            Tline.append("Q_{+}^{+}(")
        elif abs(w_i[j] + 0.5) < eps and abs(w_f[j] - 0.5) < eps:
            Tline.append("Q_{-}^{+}(")
        elif abs(w_i[j] - 0.5) < eps and abs(w_f[j] + 0.5) < eps:
            Tline.append("Q_{+}^{-}(")
        elif abs(w_i[j] + 0.5) < eps and abs(w_f[j] + 0.5) < eps:
            Tline.append("Q_{-}^{-}(")
    elif abs(J_f[j] - J_i[j] - 1) < eps:
        if abs(w_i[j] - 0.5) < eps and abs(w_f[j] - 0.5) < eps:
            Tline.append("R_{+}^{+}(")
        elif abs(w_i[j] + 0.5) < eps and abs(w_f[j] - 0.5) < eps:
            Tline.append("R_{-}^{+}(")
        elif abs(w_i[j] - 0.5) < eps and abs(w_f[j] + 0.5) < eps:
            Tline.append("R_{+}^{-}(")
        elif abs(w_i[j] + 0.5) < eps and abs(w_f[j] + 0.5) < eps:
            Tline.append("R_{-}^{-}(")
    elif abs(J_f[j] - J_i[j] + 1) < eps:
        if abs(w_i[j] - 0.5) < eps and abs(w_f[j] - 0.5) < eps:
            Tline.append("P_{+}^{+}(")
        elif abs(w_i[j] + 0.5) < eps and abs(w_f[j] - 0.5) < eps:
            Tline.append("P_{-}^{+}(")
        elif abs(w_i[j] - 0.5) < eps and abs(w_f[j] + 0.5) < eps:
            Tline.append("P_{+}^{-}(")
        elif abs(w_i[j] + 0.5) < eps and abs(w_f[j] + 0.5) < eps:
            Tline.append("P_{-}^{-}(")

#Rearrange the Columns, then
#Write out new arrangement with specified widths to align columns
sys.stdout = open('path/to/file', 'w')
for i in xrange(len(freq)):
    print '{:>9}  {:>3}  {:>4}  {:>4}  {:>3}  {:>4}  {:>4}  {:>10}{:>3}{:>1}'.format(freq[i], J_i[i], w_i[i], n_i[i], J_f[i], w_f[i], n_f[i], Tline[i], J_i[i], ")")
sys.stdout.close()

rawDescription: Columns into array and then indexing multiple arrays in a loop


code:
import paho.mqtt.client as mqtt

topic_names = []

def on_message(mqttc, obj, msg,):
    # print(msg.topic + " " + str(msg.payload))
    payload = str(msg.payload)
    print(msg.topic + " Payload -> " + payload)

    topic_names.append(msg.topic)

try:
    mqttc = mqtt.Client()
    mqttc.on_message = on_message

    mqttc.connect("localhost", 1883, 60)
    mqttc.subscribe("weather/current/#", 0)

    mqttc.loop_forever()

except KeyboardInterrupt:
    print "Received topics:"
    for topic in topic_names:
        print topic

rawDescription: Place MQTT topics and message in array

description: Place MQTT topics and message in array


code:
def f(return_something=True):
    if return_something:
        return 'something'
    print "I won't return something"

rawDescription: In python, return value only when the function is used in an assignment


code:
for rev, rip, rnmap in revLookup(ips):
    print rev, rip, rnmap

rawDescription: Python 2.7 returning values each time through the loop


code:
for word in PhoneWords("34"):
    print word

rawDescription: custom permutation python?

description: Custom permutation python


code:
root = Tree(2)
root.left = Tree(1)
root.right = Tree(4)
root.right.left = Tree(3)
root.right.right = Tree(5)

print list(root) # [1, 2, 3, 4, 5]
print list(root.bfs()) # [2, 1, 4, 3, 5]

rawDescription: Can you define multiple different iterators for a Python class?

description: Define multiple different iterators for a Python class


code:
for dirname, dirnames, filenames in os.walk('.'):
     for i in glob.glob(dirname+'/*'+filename+'*'):
           print i

rawDescription: how do i search for specific files by user input

description: Search for specific files by user input


rawDescription: SQLAlchemy mapping joined tables' columns to one object


code:
import socket, hashlib, base64, threading

class PyWSock:
    MAGIC = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
    HSHAKE_RESP = "HTTP/1.1 101 Switching Protocols\r\n" + \
                "Upgrade: websocket\r\n" + \
                "Connection: Upgrade\r\n" + \
                "Sec-WebSocket-Accept: %s\r\n" + \
                "\r\n"
    LOCK = threading.Lock()

    clients = []

    def recv_data (self, client):
        # as a simple server, we expect to receive:
        #    - all data at one go and one frame
        #    - one frame at a time
        #    - text protocol
        #    - no ping pong messages
        data = bytearray(client.recv(512))
        if(len(data) < 6):
            raise Exception("Error reading data")
        # FIN bit must be set to indicate end of frame
        assert(0x1 == (0xFF & data[0]) >> 7)
        # data must be a text frame
        # 0x8 (close connection) is handled with assertion failure
        assert(0x1 == (0xF & data[0]))

        # assert that data is masked
        assert(0x1 == (0xFF & data[1]) >> 7)
        datalen = (0x7F & data[1])

        #print("received data len %d" %(datalen,))

        str_data = ''
        if(datalen > 0):
            mask_key = data[2:6]
            masked_data = data[6:(6+datalen)]
            unmasked_data = [masked_data[i] ^ mask_key[i%4] for i in range(len(masked_data))]
            str_data = str(bytearray(unmasked_data))
        return str_data

    def broadcast_resp(self, data):
        # 1st byte: fin bit set. text frame bits set.
        # 2nd byte: no mask. length set in 1 byte.
        resp = bytearray([0b10000001, len(data)])
        # append the data bytes
        for d in bytearray(data):
            resp.append(d)

        self.LOCK.acquire()
        for client in self.clients:
            try:
                client.send(resp)
            except:
                print("error sending to a client")
        self.LOCK.release()

    def parse_headers (self, data):
        headers = {}
        lines = data.splitlines()
        for l in lines:
            parts = l.split(": ", 1)
            if len(parts) == 2:
                headers[parts[0]] = parts[1]
        headers['code'] = lines[len(lines) - 1]
        return headers

    def handshake (self, client):
        print('Handshaking...')
        data = client.recv(2048)
        headers = self.parse_headers(data)
        print('Got headers:')
        for k, v in headers.iteritems():
            print k, ':', v

        key = headers['Sec-WebSocket-Key']
        resp_data = self.HSHAKE_RESP % ((base64.b64encode(hashlib.sha1(key+self.MAGIC).digest()),))
        print('Response: [%s]' % (resp_data,))
        return client.send(resp_data)

    def handle_client (self, client, addr):
        self.handshake(client)
        try:
            while 1:
                data = self.recv_data(client)
                print("received [%s]" % (data,))
                self.broadcast_resp(data)
        except Exception as e:
            print("Exception %s" % (str(e)))
        print('Client closed: ' + str(addr))
        self.LOCK.acquire()
        self.clients.remove(client)
        self.LOCK.release()
        client.close()

    def start_server (self, port):
        s = socket.socket()
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.bind(('', port))
        s.listen(5)
        while(1):
            print ('Waiting for connection...')
            conn, addr = s.accept()
            print ('Connection from: ' + str(addr))
            threading.Thread(target = self.handle_client, args = (conn, addr)).start()
            self.LOCK.acquire()
            self.clients.append(conn)
            self.LOCK.release()

ws = PyWSock()
ws.start_server(9876)

rawDescription: html5 websocket connecting to python

description: Html5 websocket connecting to python


code:
import random
random.seed(42)
for i in range(10):
  s.sort(key=lambda i:random.random())
  print ''.join(s)

rawDescription: Influence of choosing string as seed of random on the output


code:
import json

traffic = json.load(open('xxx.json'))

columns = ['local', 'coord', 'sentido', 'veiculos', 'modalidade', 'pistas']
for timestamp, data in traffic.iteritems():
    keys = (timestamp,) + tuple(data[c] for c in columns)
    print str(keys)

rawDescription: Convert JSON to SQLite in Python - How to map json keys to database columns properly?


code:
import re
p = re.compile(ur']*>[^<]*\s*([^<]*)<div\b')
test_str = u"Wont open I have the GS5 and the game wont open. I got this game when i got the first droid. The fact that people havent been able to play since almost 2013 is bull. Please fix this or there isnt a point in even having the game on the server. <div class=\"review-link\" "
print [x.group(1) for x in re.finditer(p, test_str)]

rawDescription: Advanced string parsing in python

description: Advanced string parsing in python


code:
import collections
import itertools
import string

def allocate_goals(prop_low, prop_high):
    prop_high3 = prop_high * 3
    while True:
        for g in prop_low:
            yield g
        for g in prop_high3:
            yield g

def allocate(goals, players):
    letters = string.ascii_uppercase[:goals]
    high_count = goals // 2
    prop_high, prop_low = letters[:high_count], letters[high_count:]
    g = allocate_goals(prop_low, prop_high)
    return collections.Counter(itertools.islice(g, players))

for goals in xrange(2, 9):
    print goals, sorted(allocate(goals, 8).items())

rawDescription: Allocate items according to an approximate ratio in Python


code:
y = 'albumjcker' # all characters inside "lumberjack"
s = 'lumberjack'

for x in s:
    if any(x in alpha for alpha in y):
           print x

rawDescription: For loop with If not working as expected


code:
from itertools import islice
for line in islice(airtemp, 12, None):  # Skip lines numbered 0 through 11
    print line

rawDescription: How can I skip the header in Python?

description: Skip the header in Python


code:
with open('/Users/.../USER_INPUT.txt', 'w+') as UserInputFile:
    if UserInput.strip() == 'List contents': # or if s in f:
        UserInputFile.write("ls")
    else:
        print "Didn't work"

rawDescription: Comparing a variable with a string python not working when redirecting from bash script


code:
import os
print os.path.splitext("path_to_file")[0]

rawDescription: How to get the filename without the extension from a path in Python?

description: Get the filename without the extension from a path in Python


code:
import numpy as np
import scipy
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import LinearSVC

# We have lists called tags_train, descs_train, tags_test, descs_test with the test and train data
tags_train = [('label', ), ('international' ,'solved'), ('international','open')]
descs_train = ['description of ticket one', 'some other ticket two', 'label']

X_train = np.array(descs_train)
y_train = tags_train
X_test = np.array(descs_train)

classifier = Pipeline([
    ('vectorizer', CountVectorizer()),
    ('tfidf', TfidfTransformer()),
    ('clf', OneVsRestClassifier(LinearSVC(class_weight='auto')))])

classifier = classifier.fit(X_train, y_train)
predicted = classifier.predict(X_test)

print predicted

rawDescription: Python: using scikit-learn to predict, gives blank predictions


code:
for n in range(12):
    answer = list(distant_pairs(n))
    prev_answer = sorted(combinations(range(n), 2), key=lambda a: -abs(a[0]-a[1]))

    print "SIZE", n
    print answer
    print prev_answer
    assert answer == prev_answer
    print "---"
print "done"

rawDescription: Fastest way to get pairs from a list ordered by their distance in the list


code:
with open(file_name) as f:
      pre_data,post_data =[s.strip() for s in (f.read()).split("[data]")]
post_data_lines = post_data.splitlines()
headers = post_data_lines[0].split()
print headers
for line in post_data_lines[1:]:
      print line.split()
      print dict(zip(headers,line.split()))

rawDescription: reading files python

description: Reading files python


code:
with open("russ.txt", "r") as f:
    for line in f:
        line = line.split()
        line = line[-1]
        if line == "Ticker": continue
        print line
        with open("output.txt", "a") as fh:
            fh.write(line + "\n")

rawDescription: Reading/writing files in Python

description: Reading/writing files in Python


code:
import random
import string
import hashlib
import time

def getRandText(N):
    return  "".join([random.choice(string.printable) for i in xrange(N)])

N=1000000
randText1 = getRandText(N)
randText2 = getRandText(N)

def cmpHash(text1, text2):
    hash1 = hashlib.md5()
    hash1.update(text1)
    hash1 = hash1.hexdigest()

    hash2 = hashlib.md5()
    hash2.update(text2)
    hash2 = hash2.hexdigest()

    return  hash1 == hash2

def cmpByteByByte(text1, text2):
    return text1 == text2

for cmpFunc in (cmpHash, cmpByteByByte):
    st = time.time()
    for i in range(10):
        cmpFunc(randText1, randText2)
    print cmpFunc.func_name,time.time()-st

rawDescription: see if two files have the same content in python

description: See if two files have the same content in python


code:
f = open("parrot_sketch.txt")
for line in f:
    p = process_line(
        line,
        O.r, ["He's resting...", "This is an ex-parrot!"],
        O.c, [],
        O.s, []
    )
    print p

rawDescription: Defining dynamic functions to a string

description: Defining dynamic functions to a string


code:
import numpy as np
from scipy.cluster.hierarchy import linkage

d = np.array(
        [
            [1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]
        ]
)
print linkage(d, 'complete')

rawDescription: sklearn agglomerative clustering linkage matrix

description: Sklearn agglomerative clustering linkage matrix


code:
# import our required modules
import json
import urllib2

# declare some initial variables
input_file = "users.txt"
output_file = "available_tags.txt"
available_tags = [] # an empty list to hold our available gamertags

# open the file
with open(input_file) as input_f:
    # loop through each line
    for line in input_f:
        # strip any new line character from the line
        tag = line.strip()
        # set up our URL and open a connection to the API
        url = "http://360api.chary.us/?gamertag=%s" % tag
        u = urllib2.urlopen(url)
        # load the returned data as a JSON object
        data = json.loads(u.read())
        # check if the gamertag is available
        if not data['GamertagExists']:
            # print it and add it to our list of available tags if so
            print "Tag %s is available." % tag
            available_tags.append(tag)
        else:
            print "Tag %s is not available." % tag #otherwise

# check that we have at least one valid tag to store
if len(available_tags) > 0:
    # open our output file
    with open(output_file, "w") as output_f:
            # loop through our available tags
            for tag in available_tags:
                # write each one to the file
                output_f.write("%s\n" % tag)
else:
    print "No valid tags to be written to output file."

rawDescription: Python increment pulling from an array in a loop


code:
print z
print x
print newx

rawDescription: Line smoothing with Numpy/SciPy

description: Line smoothing with Numpy/SciPy


code:
import sys
import zmq

port = "5556"

context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.setsockopt(zmq.SUBSCRIBE, "")

print "Collecting updates from weather server..."
socket.connect ("tcp://127.0.0.1:%s" % port)

while True:
    print socket.recv()

rawDescription: ZeroMQ Pub/Sub simplest example is not working - What am I doing wrong?


code:
string='''@r=v4 "v4"
 A -> C : B
 Cell * kcat * B * A / (km + A)
@r=v4 "v4"
 C -> C+D
 Cell * v2_k * C
@r=v4 "v4"
 C -> : D
 Cell * kcat2 * D * C / (km2 + C)
@r=v4 "v4"
 C -> C+D
 Cell * v2_k * C'''

i = 0
for strg in string.splitlines():
    if strg == '@r=v4 "v4"':
        i += 1
        print '@r=v{} "v{}"'.format(i,i)
    else:
        print strg

rawDescription: Using regex's to relabel and remove redundent items in a string


code:
import timeit

repeat = 1

setup = '''
num = 1000000
import random
import string
from itertools import izip
cpos = [random.randint(1, 100) for x in range(num)]
cal = [random.choice(string.letters) for x in range(num)]
mpos = [random.randint(1, 100) for x in range(num)]
mal = [random.choice(string.letters) for x in range(num)]
'''

# izip: 0.38 seconds (Python 2.7.2)
t = timeit.Timer(
     setup = setup,
     stmt = '''set(izip(cpos, cal)).intersection(izip(mpos, mal))'''
)

print "%.2f second" % (t.timeit(number=repeat))

# zip: 0.53 seconds (Python 2.7.2)
t = timeit.Timer(
     setup = setup,
     stmt = '''set(zip(cpos, cal)) & set(zip(mpos, mal))'''
)

print "%.2f second" % (t.timeit(number=repeat))

# Nested loop: 616 seconds (Python 2.7.2)
t = timeit.Timer(
     setup = setup,
     stmt = '''

mat = []
for i in xrange(len(cpos)):
     for j in xrange(len(mpos)):
          if mpos[j] == cpos[i] and mal[j] == cal[i]:
               mat.append(mpos[j]) # or mat.append((mpos[j], mal[j])) ?
               break
'''
)

print "%.2f seconds" % (t.timeit(number=repeat))

rawDescription: Finding a match in two sets of synchronized lists


code:
import subprocess
import json
import sys
import os

def php(script_path, argument):
    p = subprocess.Popen(['php', script_path, argument], stdout=subprocess.PIPE)
    result = p.communicate()[0]
    return result

image_dimensions_json =str(php("get_image_size.php", image_location_relative[1:]))
dic = json.loads(image_dimensions_json)
print str(dic["0"]) + "|" + str(dic["1"])

rawDescription: Running a PHP script from within Python - Pass value from Python to PHP


code:
import re

f = """

            <button>text1</button>

text2

"""
x = re.sub('<[^>]*>', '', f)  # you can also use re.sub('<[A-Za-z\/][^>]*>', '', f)

print '\n'.join(x.split())

rawDescription: Python: Regular expression to extract text between any two tags in a html


rawDescription: I cannot seem to find a string in a subarray in an array of arrays


code:
import random       # To generate your random numbers

LOW = 0             # Lowest random number
HIGH = 100          # Highest random number
NUM_RANDS = 5       # Number of random numbers to generate for each case

l = ['x', 'y', 'z'] # Your pre-existing list

d = {}              # An empty dictionary

for i in l:         # For each item in the list
    # Make a dictionary entry with a list of random numbers
    d[i] = [random.randint(LOW, HIGH) for j in range(NUM_RANDS)]

print d             # Here is your dictionary

rawDescription: Dictionary within a list through a random data stream


code:
from bs4 import BeautifulSoup

soup=BeautifulSoup(htmltext)
for script in soup(["script", "style"]):
    script.extract()
text = soup.get_text()
print text

rawDescription: Read a URL in Raspberry Pi

description: Read a URL in Raspberry Pi


code: print map(list, set(map(frozenset, l)))

rawDescription: Removing repeated sub-lists from a list

description: Removing repeated sub-lists from a list


code:
with open('jokes.txt') as myfile:
    for line in myfile.readlines():
        if line.strip() != 'Start' and line.strip() != 'End' and line.strip():
            print line[:-1]

rawDescription: Extracting repeated patterns from a text file in python


code:
import numpy as np

def rowwise(func):
    def new_func(array2d, **kwargs):
        # Run the function once to determine the size of the output
        val = func(array2d[0], **kwargs)
        output_array = np.zeros((array2d.shape[0], val.size), dtype=val.dtype)
        output_array[0] = val
        for i,row in enumerate(array2d[1:], start=1):
            output_array[i] = func(row, **kwargs)
        return output_array
    return new_func

@rowwise
def test(data):
    return np.cumsum(data)

x = np.arange(20).reshape((4,5))
print test(x)

rawDescription: numpy ndarrays: row-wise and column-wise operations

description: Numpy ndarrays: row-wise and column-wise operations


code:
AvailibilityLine1 = 1.9
AvailibilityLine2 = 45.9
PerformanceLine1 = 76.5
PerformanceLine2 = 99.9
QualityLine1 = 100.0
QualityLine2 = 0.0

#these come in as floats with a single decimal space, ex:99.9
val = [AvailibilityLine1, AvailibilityLine2, PerformanceLine1,
       PerformanceLine2, QualityLine1, QualityLine2]

j = 0
for i in val:
    if i >= float(100):
        val[j] = "100 "
    elif i >= 10 and i < 100:
        val[j] = str(val[j])
    elif i > 0 and  i < 10:
        val[j] = " " + str(val[j])
    elif i <= 0:
        val[j] = "    "    #insert 4 spaces
    else:
        val[j] = "    "    #insert 4 spaces if all else fails
    j = j + 1

print val

rawDescription: Modifying a list while iterating when programming with python


code:
a, b = 1,2
print a
print b

rawDescription: Returning multiple values from function in python 3.5


code:
from byteplay import *

def foo():
    x = 10
    print 'inside ',x
    return 42

c = Code.from_code(foo.func_code)
print c.code

ret = foo()
print 'outside: ',ret

c.code[10] = (LOAD_CONST,1000)

foo.func_code = c.to_code()
print '*'*20
print c.code

ret = foo()

print 'outside: ',ret

rawDescription: Assembling Bytecode - PyCode New

description: Assembling Bytecode - PyCode New


code:
import optparse
import sys

if __name__ == '__main__':
    parser = optparse.OptionParser()

    (options, args) = parser.parse_args()

    if len(args) != 3:
        print 'please specify all required arguments - qtype qname output_file'
        sys.exit(-1)

    qtype, qname, output = args

    if qtype == 'web':
        pass
    elif qtype == 'local':
        pass
    else:
        print 'no qtype specified! exiting'
        sys.exit(-1)

rawDescription: Optparse Option Actions

description: Optparse Option Actions


code:
print [''.join((first, second, third, fourth, fifth))
       for first in firsts
       for second in seconds
       for third in thirds
       for fourth in fourths
       for fifth in fifths]

rawDescription: Python script to generate words from letters and two letter combination


code:
import numpy
tmp=numpy.array([-24160,18597,-24160,18597,-24160, 0], numpy.int16)
tmp.dtype = numpy.float32
print tmp

rawDescription: Converting 16-bit integer to 32-bit floating-point


code:
def is_prime(number):
    candidate_factor = 0
    amount_of_factors = 0
    while candidate_factor<number:
        #A += B is equivalent to A = A + B
        candidate_factor += 1
        #A little easier way of testing whether one number divides another evenly
        if number % candidate_factor == 0:
            amount_of_factors += 1
    if amount_of_factors == 2:
        return True
    else:
        return False

number=1
prime_total=0
#generates a list of numbers.
while number<100:
    number += 1
    if is_prime(number):
        prime_total += number
print prime_total

rawDescription: python sum of primes

description: Python sum of primes


code:
pattern = re.compile("(red|green) light")
print pattern.match("red light")
print pattern.match("red lights everywhere")
print pattern.match("green lights forever")
print pattern.match("blue lights begone")

rawDescription: is this possible in re in python?

description: Is this possible in re in python


code:
import pygame, sys, glob
from pygame import *

class Player:
    def __init__(self):
        self.x = 200
        self.y = 300
        self.speed_init = 6
        self.images = [pygame.image.load(img) for img in glob.glob("walk\Pinks_w*.png")]
        self.index = 0
        self.max_index = len(self.images)-1
        self.speed = 0
        self.img = self.images[self.index]
        print self.max_index

    def update(self, pos):
        if pos != 0:
            self.speed -= 1
            self.x += pos
            print self.index, self.max_index
            if self.index < self.max_index:
                self.index += 1
            else:
                self.index = 0
            self.img = self.images[self.index]
        # change True to False if needed, or change the operator.
        if pos < 0:
            self.img = pygame.transform.flip(self.img,True,False)
        screen.blit(self.img,(self.x,self.y))

h = 400
w = 800
screen = pygame.display.set_mode((w,h))
clock = pygame.time.Clock()
player1 = Player()
pos = 0

while True:
    screen.fill((0,0,0))
    clock.tick(20)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == KEYDOWN and event.key == K_RIGHT:
            pos = 2
        elif event.type == KEYUP and event.key == K_RIGHT:
            pos = 0
        elif event.type == KEYDOWN and event.key == K_LEFT:
            pos = -2
        elif event.type == KEYUP and event.key == K_LEFT:
            pos = 0

    player1.update(pos)

    pygame.display.update()

rawDescription: How to flip an img in Pygame, having trouble in my code?

description: Flip an img in Pygame, having trouble in my code


code:
unit_pattern = re.compile(r"[+-]? *((?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?)\s*(square meters|square metres|square meter|square metre|square m|square mt|sqmt|sq mt|sq.mt|sq. mt|sqm|sq m|sq.m|sq. m|meters2|metres2|meter2|metre2|mt2|m2)", re.IGNORECASE)
for i, line in enumerate(open('/Users/USERNAME/text.txt')):
    for match in re.finditer(unit_pattern, line):
        print 'Found on line %s: %s' % (i+1, match.groups())

rawDescription: Text parsing looking for data with different strings expressing the base units (ex. square meters, sq.m, m^2, m2, etc.)


code:
class Your_Streamer(TwythonStreamer, Process):
    def __init__(self, ):
        Process.__init__(self)
        consumer_key, consumer_secret, oauth_token,oauth_secret = '','','','' #your twitter keys
        super(SocialVane_Streamer, self).__init__(consumer_key, consumer_secret, oauth_token,oauth_secret)
    def run (self):
        keyword_list =[] #the keywords you want to monitorize
        if keyword_list:
            logger.info("start_monitoring, tracking keywords = %s" % ','.join(keyword_list))
            self.statuses.filter(track=','.join(keyword_list))
    def on_error(self, status_code, data):
        print status_code
        # Want to stop trying to get data because of the error?
        # Uncomment the next line!
        # self.disconnect()
    def on_success(self, data):
        if 'text' in data:
            pass #do what you need

rawDescription: How to disconnect the streaming on the exact date & time

description: Disconnect the streaming on the exact date & time


code:
import argparse

parser = argparse.ArgumentParser(description = "Compare files")
parser.add_argument('first', help="the first file")
parser.add_argument('other', nargs='+', help="the other files")

args = parser.parse_args()
print args

rawDescription: Can argparse in python 2.7 be told to require a minimum of TWO arguments?


code:
from win32com.client import Dispatch

SHELL = Dispatch("Shell.Application")

def get_ie(shell):
    for win in shell.Windows():
        if win.Name == "Windows Internet Explorer":
            return win
    return None

def main():
    ie = get_ie(SHELL)
    if ie:
        print ie.LocationURL
    else:
        print "no ie window"

if __name__ == '__main__':
    main()

rawDescription: how to get internet explorer address bar for python

description: Get internet explorer address bar for python


code:
s = "Hello I am a very long string <status>I've got a lovely bunch of coconuts</status> here they are standing in a row"
excerpt = s.partition("<status>")[2].rpartition("</status>")[0]
print excerpt

rawDescription: Get a substring within two strings

description: Get a substring within two strings


code:
datafile = open('C:\\middlelist3.txt', 'r')

smallerdataset = open('C:\\nocommas.txt', 'w')

for counter, line in enumerate(datafile):
    print counter
    smallerdataset.write(line.split(',', 1)[0])

smallerdataset.close()

rawDescription: Need to delete part of text lines in python


code:
from pyparsing import *

# define some basic operand expressions
number = Regex(r'\d+(\.\d*)?([Ee][+-]?\d+)?')
ident = Word(alphas+'_', alphanums+'_')

# forward declare our overall expression, since a slice could
# contain an arithmetic expression
expr = Forward()
slice_ref = '[' + expr + ']'

# define our arithmetic operand
operand = number | Combine(ident + Optional(slice_ref))

# parse actions to convert parsed items
def convert_to_pow(tokens):
    tmp = tokens[0][:]
    ret = tmp.pop(-1)
    tmp.pop(-1)
    while tmp:
        base = tmp.pop(-1)
        # hack to handle '**' precedence ahead of '-'
        if base.startswith('-'):
            ret = '-pow(%s,%s)' % (base[1:], ret)
        else:
            ret = 'pow(%s,%s)' % (base, ret)
        if tmp:
            tmp.pop(-1)
    return ret

def unary_as_is(tokens):
    return '(%s)' % ''.join(tokens[0])

def as_is(tokens):
    return '%s' % ''.join(tokens[0])

# simplest infixNotation - may need to add a few more operators, but start with this for now
arith_expr = infixNotation( operand,
    [
    ('-', 1, opAssoc.RIGHT, as_is),
    ('**', 2, opAssoc.LEFT, convert_to_pow),
    ('-', 1, opAssoc.RIGHT, unary_as_is),
    (oneOf("* /"), 2, opAssoc.LEFT, as_is),
    (oneOf("+ -"), 2, opAssoc.LEFT, as_is),
    ])

# now assign into forward-declared expr
expr <<= arith_expr.setParseAction(lambda t: '(%s)' % ''.join(t))

assert "2**3" == expr
assert "2**-3" == expr

# test it out
tests = [
    "2**3",
    "2**-3",
    "2**3**x5",
    "2**-3**x6[-1]",
    "2**-3**x5+1",
    "(a+1)**2",
    "((a+b)*c)**2",
    "B**2",
    "-B**2",
    "(-B)**2",
    "B**-2",
    "B**(-2)",
    "B**2&&T**2*X",
    ]

x5 = 2
a,b,c = 1,2,3
B = 4
x6 = [3,2]
for test in tests:
    print test
    xform = expr.transformString(test)[1:-1]
    print xform
    print '**' not in xform and eval(xform) == eval(test)
    print

rawDescription: changing ** operator to power function using parsing?

description: Changing ** operator to power function using parsing


code:
from copy import copy

class MetaStruct(type):
    def __init__(cls, name, bases, cls_dict):
        try:
            fields = cls_dict['fields']
        except KeyError:
            raise TypeError("No 'fields' attribute defined in class " + `name`)

        # field names may be separated by whitespace and/or commas
        fields = fields.replace(',', ' ').split()
        del cls_dict['fields']  # keep out of class instances

        if 'default_field_value' not in cls_dict:
            default_field_value = None  # default default field value
        else:
            default_field_value = cls_dict['default_field_value']
            del cls_dict['default_field_value']   # keep out of class instances

        super(MetaStruct, cls).__init__(name, bases, cls_dict)

        def __init__(self, **kwds):
            """ __init__() for class %s """ % name
            self.__dict__.update(zip(fields, [copy(default_field_value)
                                                  for _ in xrange(len(fields))]))
            self.__dict__.update(**kwds)

        def __setattr__(self, field, value):
            """ Prevents new field names from being added after creation. """
            if field in self.__dict__:
                self.__dict__[field] = value  # avoid recursion!
            else:
                raise AttributeError('Can not add field %r to instance of %r' %
                                     (field, name))
        # add defined methods to class instance
        setattr(cls, '__init__', __init__)
        setattr(cls, '__setattr__', __setattr__)

rawDescription: Does this Python class look stupid?

description: Does this Python class look stupid


code:
s = "test"
chk = reduce(lambda a,b : a^b, [ord(c) for c in s])

print "Checksum : ", chk, " \n"

rawDescription: Need help for checksum on python

description: Need help for checksum on python


code:
searchstringsProblem = 'BIG Problem Line'
searchstringsGPSLoc = 'GPS INFO'
matches = []

with open("test.txt") as f:
    problem = False
    problem_line = ""

    for line in f:
        if not problem and searchstringsProblem in line:
            problem_line = line.strip()
            problem = True
        elif problem and searchstringsGPSLoc in line:
            matches.append((problem_line, line.strip(), f.next().strip(), f.next().strip()))
            problem = False

for item in matches:
    print item

rawDescription: Using Python to run nested string searches


code:
import sys
from collections import defaultdict
import csv

# see http://stackoverflow.com/questions/6180609/group-and-check-mark-using-python
def main():
    # files = ["group.txt"]
    files = sys.argv[1:]
    if len(files) < 1:
        print "usage: ./python_checkmark.py file1 [file2 ... filen]"

    name_map = defaultdict(set)

    for f in files:
        file_handle = open(f, "r")
        process_file(file_handle, name_map)
        file_handle.close()

    print_csv(sys.stdout, name_map)

def process_file(input_file, name_map):
    cur_name = ""
    for line in input_file:
        if ":" in line:
            cur_name, item = [x.strip() for x in line.split(":")]
        else:
            item = line.strip()
        name_map[cur_name].add(item)

def print_csv(output_file, name_map):
    names = name_map.keys()
    items = set([])
    for item_set in name_map.values():
        items = items.union(item_set)

    writer = csv.writer(output_file, quoting=csv.QUOTE_MINIMAL)
    writer.writerow( [""] + names )
    for item in sorted(items):
        row_contents = map(lambda name:"X" if item in name_map[name] else "", names)
        row = [item] + row_contents
        writer.writerow( row )

if __name__ == '__main__':
    main()

rawDescription: Group and Check-mark using Python

description: Group and Check-mark using Python


code:
def function():
  return blahblah

def function2():
  print function()

rawDescription: TypeError: password() takes exactly 1 argument (0 given)


code:
while True:
    print "Sending App Request"
    s.sendall("{'app':'windows_process_monitor', 'platform':'windows'}")
    print "Receiving Files"

    first_flag = True
    with open("windows_process_monitor.dll", "wb") as dd:
        app_len = 0
        remaining_part_len = 0

        while(True):
            data = s.recv(1024)
            print data
            if not data:
                break
            if first_flag:
                app_name, app_len_str, first_part_content = data.split("#", 2)
                app_len += int(app_len_str)
                remaining_part_len += app_len
                first_received_len = len(first_part_content)
                dd.write(first_part_content)
                first_flag = False
                if app_len <= first_received_len:
                    print "all received"
                    break
                remaining_part_len -= first_received_len
            else:
                dd.write(data)
                remaining_part_len -= 1024
            if remaining_part_len <= 0:
                break
            print "inside while"

    print "Received ", repr(data)
    break

rawDescription: Python TCP File Transmission over Client Server


rawDescription: Python Pandas If value in column B = equals [X, Y, Z] replace column A with "T"


code:
def IP_Range():
    ipLIST = []
    while True:
        IP = raw_input('Please enter IP or Network, enter "end" to break: ')
        if IP == 'end':
            break
        else:
            ipLIST.append(IP)
            print ipLIST
    print ipLIST

IP_Range()

rawDescription: Python Function does not return as expected


code:
mystuff = [1, 2, "Foo", 34, 42, "Ni", 12, 14, "Blue"]
for x in zip(*[iter(mystuff)]*3):
    print "%d %d %s"%x

rawDescription: Iterate a format string over a list

description: Iterate a format string over a list


code:
def printTTriangle(height):
    row = 1
    while row < height+1:
        col = 1
        while col < row+1:
            print 'T',
            col += 1
        print
        row += 1

rawDescription: Converting for loops to while loops in python


code:
from itertools import combinations

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

print result

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

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


code:
# get result when you need ordinal number of count in decreasing order
print [ (index, item[0], item[1]) for index, item in enumerate(c.most_common(),1)]
# [(1, 'y', 3), (2, 'z', 2), (3, 'b', 2), (4, 'a', 1)]

# get result when you need ordinal number of count in increasing order
print [ (index, item[0], item[1]) for index, item in enumerate(sorted(c.items(), key=lambda x: x[1]),1)]
# [(1, 'a', 1), (2, 'z', 2), (3, 'b', 2), (4, 'y', 3)]

rawDescription: Make tuples of a list include alphabet ordered number, alphabet, counts


code:
# Here's my list
mylist = [1, 2, 3, 4]

# Save a reference to the list using a function to close over it
def myref(x): mylist[1] = x

# Update the referenced value to 7
myref(7)

# mylist is now [1, 7, 3, 4]
print mylist

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


code:
def foo(bar, baz, spam):
    print bar, baz, spam
arguments = [1, 2, 3]
foo(*arguments)

rawDescription: unpacking function argument

description: Unpacking function argument


code:
class class_reify(object):
    def __init__(self, wrapped):
        self.wrapped = wrapped
        try:
            self.__doc__ = wrapped.__doc__
        except: # pragma: no cover
            pass

    # original sets the attributes on the instance
    # def __get__(self, inst, objtype=None):
    #    if inst is None:
    #        return self
    #    val = self.wrapped(inst)
    #    setattr(inst, self.wrapped.__name__, val)
    #    return val

    # ignore the instance, and just set them on the class
    # if called on a class, inst is None and objtype is the class
    # if called on an instance, inst is the instance, and objtype
    # the class
    def __get__(self, inst, objtype=None):
        # ask the value from the wrapped object, giving it
        # our class
        val = self.wrapped(objtype)

        # and set the attribute directly to the class, thereby
        # avoiding the descriptor to be called multiple times
        setattr(objtype, self.wrapped.__name__, val)

        # and return the calculated value
        return val

class Test(object):
    @class_reify
    def foo(cls):
        print "foo called for class", cls
        return 42

print Test.foo
print Test.foo

rawDescription: Lazy class property decorator

description: Lazy class property decorator


code: for i in sorted(d.keys(), key=lambda x: x.lower()): print i

rawDescription: How to iterate over case insensitive sorted dictionary items?

description: Iterate over case insensitive sorted dictionary items


code:
import clang.cindex
import sys

path = '/your/path/to/libclang.so'
clang.cindex.Config.set_library_file(path)

def get_ts(source_path):
    index = clang.cindex.Index.create()
    return index.parse(source_path)

def print_node(node):
    if node.kind == clang.cindex.CursorKind.INTEGER_LITERAL:
        print node.type.kind, node.get_tokens().next().spelling
    map(print_node, node.get_children())

ts = get_ts('test.cpp')
map(print_node, ts.cursor.get_children())

rawDescription: How to retrieve function call argument values using libclang

description: Retrieve function call argument values using libclang


code:
def bar(a, b):
    a = a + 1
    b = b + 2
    print a

x = 0
y = 0
bar(x, y)
print x
print y

rawDescription: Do you change variables AFTER you run a function in python?


code:
from xml.dom import minidom

xmldoc = minidom.parse('file.xml')
nodes = xmldoc.getElementsByTagName('node')

for node in nodes:
    node_id = node.attributes['id'].value
    for ngh in node.getElementsByTagName('ngh'):
        ngh_id = ngh.attributes['id'].value
        ngh_text = ngh.firstChild.nodeValue

        print node_id, ngh_id, ngh_text

rawDescription: XML parsing with XMLtree or MINIDOM

description: XML parsing with XMLtree or MINIDOM


code:
#!/usr/bin/env python

import os, sys
from pyinotify import WatchManager, Notifier, ProcessEvent, EventsCodes

def Monitor(path):
    class PClose(ProcessEvent):
        def process_IN_CLOSE(self, event):
            f = event.name and os.path.join(event.path, event.name) or event.path
            print 'close event: ' + f

    wm = WatchManager()
    notifier = Notifier(wm, PClose())
    wm.add_watch(path, EventsCodes.IN_CLOSE_WRITE|EventsCodes.IN_CLOSE_NOWRITE)

    try:
        while 1:
            notifier.process_events()
            if notifier.check_events():
                notifier.read_events()
    except KeyboardInterrupt:
        notifier.stop()
        return

if __name__ == '__main__':
    try:
        path = sys.argv[1]
    except IndexError:
        print 'use: %s dir' % sys.argv[0]
    else:
        Monitor(path)

rawDescription: Python: Waiting for a file to reach a size limit in a CPU friendly manner


code:
from itertools import takewhile
print list(takewhile(str.strip,column_generator))

rawDescription: Using xlrd to get list of excel values in python


code:
mylist = [L]*3
mylist[0][0] = 6
print mylist
print L

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


code:
from datetime import datetime, date
past = date(2013, 1, 1)
today = date.today()
print today - past
now = datetime.now()
print now.date() - past

rawDescription: Trouble with datetime in python2.7/django1.5.1


code:
from collections import Counter
alist=[1,1,1,2,2,3,4,2,2,3,2,2,1]
print Counter(alist)

rawDescription: Count occurence in a list with time complexity of O(nlogn)


code:
from re import match
from collections import Counter
with open('alice.txt','r') as f:
    words = [(len(word) >= 10 and 'Extralong') or (len(word) >= 7 and 'Long') or \
        (len(word) >= 5 and 'Medium') or (len(word) >= 2 and 'Small') or \
    (word in 'aeiou' and 'Vowel') for word in f.read().split() if match(r'^[a-z][a-z0-9]*$', word) ]
    print dict (Counter(words))

rawDescription: Python basic count the different type in txt file


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

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

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

description: Parse a string using a CSV parser in Python


code:
class Counter(object):
    counts = {}

    @staticmethod
    def count(func):
        def wrapped(*args,**kwargs):
            if func.__name__ in Counter.counts.keys():
                Counter.counts[func.__name__] += 1
            else:
                Counter.counts[func.__name__] = 1
            return func(*args,**kwargs)
        return wrapped

@Counter.count
def test():
    pass

@Counter.count
def test2():
    test()

@Counter.count
def test3():
    test()
    test2()

for _ in range(4):
    test3()
    test2()
    test()

print Counter.counts

rawDescription: How to test how many times a function has been called

description: Test how many times a function has been called


code:
import numpy as np
a = np.array([-2, 1, 5, 3, 8, 5, 6])
b = [1, 2, 5]
print list(a[b])

rawDescription: Access multiple elements of list knowing their index


code:
#!/usr/bin/python

import logging
import subprocess
import sys

def forever():
    cmd = [sys.executable, 'infinit.py']
    while 1:
        try:
            print 'running new instance of:'
            print ' '.join(cmd)
            popen = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                stderr=subprocess.PIPE, universal_newlines=True)

            for line in iter(popen.stderr.readline, ""):
                print line,

        except Exception as e:
            print 'Something bad happend'
            logging.error(e)

if __name__ == '__main__':
    forever()

rawDescription: How to get subprocess output during its running?

description: Get subprocess output during its running


code:
import os

root_folder = r'C:\Users\Steinar\Google Drive\Kode\Ymse\test\test'
content = {}

for root, dirs, files in os.walk(root_folder):
    for subdir in dirs:
        content[os.path.join(root, subdir)] = []
    content[root] = files

# Print out the content dict
for folder, filenames in content.items():
    print 'Folder: {}'.format(folder)
    print 'Filenames:'
    for filename in filenames:
        print '-> {}'.format(filename)

rawDescription: Python Dictionary, structure of folder and files but in two dictionaries


code:
max_depth = int(raw_input("Enter max depth of pyramid (2 - 9): "))
for i in range(max_depth, 0, -1):
    print str(i)*i + " "*((max_depth-i)*2) + str(i)*i

rawDescription: How can I create a decremented numberPyramid(num) in Python?

description: Create a decremented numberPyramid(num) in Python


code: print pickle.dumps(MyClass()) # the new instance doesn't even get a long-lived name at all

rawDescription: How to write a class method in Python that captures the name of the instance and apply some action on it

description: Write a class method in Python that captures the name of the instance and apply some action on it


code:
def fineSearch(wd):
    for word in wd.strip().split():
            if word.endswith(('ts.','ld.')): # test for ld. at end
                print word

rawDescription: Read a file line by line and pass to a method


code:
proc=subprocess.Popen(
    "ls /etc -alR 2>/dev/null | grep -s \"^[-l]\" ", shell = True,
    stdout = subprocess.PIPE)         #Expr1
out, err = proc.communicate()
expr1 = map(line_to_filename, out.splitlines())

expr2 = list(itertools.chain.from_iterable(
    files for root,dirs,files in os.walk('/etc') if files))  #Expr2

for expr in ('expr1', 'expr2'):
    print '{e} is of length {l}'.format(e = expr, l = len(vars()[expr]))

rawDescription: Different results achieved using Python's os.walk function and ls command


code:
from Tkinter import *
import ttk
from time import sleep

root = Tk()

def foo():
    print 'starting...'
    pb = ttk.Progressbar(root, mode='indeterminate')
    pb.pack()
    root.update()
    sleep(5)
    print 'stopping...'
    pb.destroy()

ttk.Button(root, text="Run", command=foo).pack()

root.mainloop()

rawDescription: Tkinter command ignores some lines

description: Tkinter command ignores some lines


code:
s = 'azcbobobegghakl'
print s[3:6]

rawDescription: Search consecutive indices in string

description: Search consecutive indices in string


code:
ret = {}

def build(ret, upv):
    if not t:
        return (None, None)
    k, v = t.pop(0)
    while k and v:
        if upv and v[:len(upv)] != upv:
            return (k, v)
        r = {}
        ret[k] = r
        k, v = build(r, v)
    return None, None

build(ret, None)
print ret

rawDescription: Create a hierarchy from a dictionary of lists


code: print  [''.join(x) for x in product('ABC', repeat=3)]

rawDescription: enumerating all possible strings of length K from an alphabet in Python

description: Enumerating all possible strings of length K from an alphabet in Python


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

rawDescription: BeautifulSoup: how to get children of div tab


code:
import re
regx = re.compile('(?!\Z)'
                  '[. \n\r]*'
                  '('
                    '(?:[^."]*"[^"]*")+'
                    '[^."]*'
                    '(?:\.|\Z)'
                  ')')

s = ('''\nThe "some.rutu"  and "oula oulah, poto."  are '''
     '''all good. A "bi'didi."  is not.  I '''
     """don't know why... 5 "million" people """
     """died . \nAnd here's a sentence without """
     """a quote. "Halt!" he shouted. 'Sunny """
     """days and "nights"'  is a strange phrase""")
print s
print
for el in regx.findall(s):
    print '- %s' % el

rawDescription: Regex to match all sentences with quotes in them


code:
from itertools import groupby
import re
myAuthlog=open('dict.txt', 'r')
myAuthlog = (line for line in myAuthlog if "Failed password for" in line)
for key, group in groupby(myAuthlog, key = lambda x: x[:9] + re.search('from(.+?) port', x).group(1)):
    month, day, hour, ip = key[0:3], key[4:6], key[7:9] , key[10:]
    print "On%s-%s at %s:00 There was %d attacks FROM IP %s"%(day, month, hour, len(list(group)), ip)

rawDescription: Python Log file count per Hour per IP

description: Python Log file count per Hour per IP


code:
import numpy as np
import pyopencl as cl

# Context
ctx = cl.create_some_context()
# Create queue
queue = cl.CommandQueue(ctx)

someArray = np.array([
    [1, 2, 3, 4],
    [5, 6, 7, 8]
]).astype(np.float32)

print ""
print("Input:")
print(someArray)
print("------------------------------------")

# Get mem flags
mf = cl.mem_flags

# Create a read-only buffer on device and copy 'someArray' from host to device
someArrayAsOpenCLType = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=someArray)

# Create a write-only buffer to get the result from device
resultAsOpenCLType = cl.Buffer(ctx, mf.WRITE_ONLY, someArray.nbytes)

# Creates a kernel in context
program = cl.Program(ctx, """
__kernel void multiplyByTwo(__global const float4 *someArrayAsOpenCLType, __global float4 *resultAsOpenCLType) {
        int gid = get_global_id(0);

        float4 vector = someArrayAsOpenCLType[gid];
        resultAsOpenCLType[gid] =  vector * (float) 2.0;
}
""").build()

# Execute
program.multiplyByTwo(queue, someArray.shape, None, someArrayAsOpenCLType, resultAsOpenCLType)

# Creates a buffer for the result (host memory)
result = np.empty_like(someArray)

# Copy the results from device to host
cl.enqueue_copy(queue, result, resultAsOpenCLType)

print("------------------------------------")
print("Output")
# Show the result
print (result)

rawDescription: NumPy array of arrays to PyOpenCL array of vecs


code:
from random import randrange, choice

# this part is to create an exemple file with newline being :;:
alphabet = 'abcdefghijklmnopqrstuvwxyz '
ch = ':;:'.join(''.join(choice(alphabet) for nc in xrange(randrange(0,40)))
                for i in xrange(50))
with open('fofo.txt','wb') as g:
    g.write(ch)

# this generator function is an iterator for a file
# if nl receives an argument whose bool is True,
# the newlines :;: are returned in the lines

def liner(filename,eol,lenchunk,nl=0):
    L = len(eol)
    NL = len(eol) if nl else 0
    with open(filename,'rb') as f:
        ch = f.read()
        the_end = '\n\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'+\
                  '\nend of the file=='+ch[-50:]+\
                  '\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n'
        f.seek(0,0)
        chunk = f.read(lenchunk)
        tail = ''
        while chunk:
            if (chunk[-1]==':' and chunk[-3:]!=':;:') or chunk[-2:]==':;':
                wr = [' ##########---------- cut newline cut ----------##########'+\
                     '\nchunk== '+chunk+\
                     '\n---------------------------------------------------']
            else:
                wr = ['chunk== '+chunk+\
                     '\n---------------------------------------------------']
            last = chunk.rfind(eol)
            if last==-1:
                kept = chunk
                newtail = ''
            else:
                kept = chunk[0:last+L]   # here: L
                newtail = chunk[last+L:] # here: L
            wr.append('\nkept== '+kept+\
                      '\n---------------------------------------------------'+\
                      '\nnewtail== '+newtail)
            chunk = tail + kept
            tail = newtail
            wr.append('\n---------------------------------------------------'+\
                      '\ntail + kept== '+chunk+\
                      '\n---------------------------------------------------')
            print ''.join(wr)
            x = y = 0
            while y+1:
                y = chunk.find(eol,x)
                if y+1: yield chunk[x:y+NL] # here: NL
                else: break
                x = y+L # here: L
            print '\n\n==================================================='
            chunk = f.read(lenchunk)
        yield tail
        print the_end

for line in liner('fofo.txt',':;:',1):
    print 'line== '+line

rawDescription: Special End-line characters/string from lines read from text file, using Python


code:
import wx

class TestDialog(wx.Dialog):
    def __init__(self, parent, msg, title):
        wx.Dialog.__init__(self, parent, id=-1, title=title)
        # outer sizer, this is to allow a spot at the bottom for the buttons
        outerSizer = wx.BoxSizer(wx.VERTICAL)

        # Main sizer for the message dialog
        mainSizer = wx.BoxSizer(wx.HORIZONTAL)

        staticIcon = wx.StaticBitmap(self, bitmap=wx.ArtProvider.GetBitmap(wx.ART_INFORMATION), size=(32,32))
        mainSizer.Add(staticIcon, flag=wx.ALL, border=10)

        # Sizer to hold the message and buttons
        controlsSizer = wx.BoxSizer(wx.VERTICAL)

        # Static text field to show the (error/warning) message on the message dialog
        errorText = wx.StaticText(self, -1, msg, wx.DefaultPosition, wx.DefaultSize, 0)
        errorText.Wrap(600)

        # This is a sizer so we can nest it inside the controls sizer. This allows us to use multiple border flags
        errorTextSizer = wx.BoxSizer(wx.HORIZONTAL)
        errorTextSizer.Add(errorText, flag=wx.TOP, border=15)

        # Add the error text to the controls sizer
        controlsSizer.Add(errorTextSizer, flag=wx.RIGHT, border=10)

        # Outer button panel, to get that slighty greyed look
        outerButtonPanel = wx.Panel(self)
        outerButtonPanelSizer = wx.BoxSizer(wx.VERTICAL)
        outerButtonPanel.SetSizer(outerButtonPanelSizer)

        # inner panel for the buttons (this allows us to right align the buttons
        innerButtonPanel = wx.Panel(outerButtonPanel)
        innerButtonPanelSizer = wx.BoxSizer(wx.HORIZONTAL)

        # Button for the "yes" option
        btnYes = wx.Button(innerButtonPanel, label='Yes')
        btnYes.Bind(wx.EVT_BUTTON, self.__yes)
        innerButtonPanelSizer.Add(btnYes, flag=wx.ALL, border=15)

        # Button for the "no" option
        btnNo = wx.Button(innerButtonPanel, label='No')
        btnNo.Bind(wx.EVT_BUTTON, self.__no)
        innerButtonPanelSizer.Add(btnNo, flag=wx.RIGHT | wx.TOP | wx.BOTTOM, border=15)

        # Add the inner button panel to the outer button panel and align it right
        innerButtonPanel.SetSizer(innerButtonPanelSizer)
        outerButtonPanelSizer.Add(innerButtonPanel, flag=wx.ALIGN_RIGHT)

        outerButtonPanel.SetBackgroundColour(wx.Colour(100, 100, 100)) # find decent colour

        # Add all the sizers to each other, finish up
        mainSizer.Add(controlsSizer)
        outerSizer.Add(mainSizer)

        # ====================================================================
        outerSizer.Add(outerButtonPanel, flag=wx.EXPAND)

        # Done layout
        self.SetSizerAndFit(outerSizer)
        self.CenterOnScreen()

    def __yes(self, evt):
        self.EndModal(wx.ID_YES)

    def __no(self, evt):
        self.EndModal(wx.ID_NO)

if __name__ == '__main__':
    app = wx.App()
    dlg = TestDialog(None, "test test test test test test test test test test test test test test test test", "Test Title")
    val = dlg.ShowModal()
    print "Dialog result: " + str(val == wx.ID_YES)
    app.Exit()

rawDescription: wxPython - Align buttons in an expanding sizer

description: WxPython - Align buttons in an expanding sizer


code:
for this_row in range(3):
    for this_column in range(2):
         print this_row, this_column

rawDescription: Iteration to place widgets in Tkinter and Pmw


code:
import subprocess

proc = subprocess.Popen('ls', shell=True, stdout=subprocess.PIPE)
output = proc.stdout.read()

print output

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


code:
from tornado import httpclient, ioloop

config = {
    'proxy_host': 'YOUR_PROXY_HOSTNAME_OR_IP_ADDRESS',
    'proxy_port': 3128
}

httpclient.AsyncHTTPClient.configure(
    "tornado.curl_httpclient.CurlAsyncHTTPClient")

def handle_request(response):
    if response.error:
        print "Error:", response.error
    else:
        print response.body
    ioloop.IOLoop.instance().stop()

http_client = httpclient.AsyncHTTPClient()
http_client.fetch("http://twitter.com/",
    handle_request, **config)
ioloop.IOLoop.instance().start()

rawDescription: Accessing twitter using tornado.httpclient using proxy