code:
import os

def mp3gen():
    for root, dirs, files in os.walk('.'):
        for filename in files:
            if os.path.splitext(filename)[1] == ".mp3":
                yield os.path.join(root, filename)

for mp3file in mp3gen():
    print mp3file

rawDescription: Is this an efficient way of listing all .mp3 files inside in a directory (including eventual subdirectories) in Python?


code:
import re
a = '1D10M1I10M1D'
start = 10
for num1, i_or_d, num2, m in re.findall('(\d+)([ID])(\d+)?([A-Za-z])?', a):
    print num1, i_or_d, start
    if num1:
        start += int(num1)
    if num2:
        start += int(num2)

rawDescription: regular expression, report position by integer count from string

description: Regular expression, report position by integer count from string


rawDescription: How can I create functions that handle polynomials?

description: Create functions that handle polynomials


code:
def foo():
   i = 1
   class bar( object ):
      def show( self ):
         print i
   return bar

c = foo()()
c
c.show
c.show.__func__
c.show.__func__.__closure__
c.show.__func__.__closure__[0].cell_contents

rawDescription: Python instance of class defined inside a method is still able to use local variables inside the method when called outside the method


code:
from lxml import html

HTML = """

    Last Day
    Last Week

    Race
    Killed Players
    Killed by Players
    Killed Players
    Killed by Players

    A
    0
    3
    0
    13

    B
    0
    0
    0
    7

    C
    0
    0
    0
    1

    D
    0
    0
    0
    7

    Total
    210
    1060458
    1132
    5585115

"""

tree = html.fromstring(HTML)
results = defaultdict

for item in tree.xpath('//table/tr[not(contains(@bgcolor, "505050"))]'):
    print item.xpath('.//td/text()')

rawDescription: Extracting rows from a table

description: Extracting rows from a table


code:
import sys
T = raw_input()
lines = sys.stdin.readlines() #fetch all lines from the STDIN
lines.sort(key=int)           #sort the list in-place(faster than sorted)
print "\n".join(lines)        #use `str.join` instead of a for-loop

rawDescription: Turbo sort - Time Limit Exceeded

description: Turbo sort - Time Limit Exceeded


code:
from datetime import datetime, timedelta

now = datetime.now()
print now.strftime("%Y-%m")
month_ago = now.replace(day=1) - timedelta(days = 1)
print month_ago.strftime("%Y-%m")
months_ago = month_ago.replace(day=1) - timedelta(days = 1)
print months_ago.strftime("%Y-%m")

rawDescription: date_range not accepting the variable I want to use?

description: Date_range not accepting the variable I want to use


code:
for i in xrange(len(a)):
   if ''.join(a[i:i+len(substring)]) == substring:
      print 'Found @ %d!' % i

rawDescription: Open file into array, search for string and return value


code:
r = requests.post('https://stream.twitter.com/1/statuses/filter.json',
    data={'track': 'requests'}, auth=('username', 'password'),
    prefetch=False)

for line in r.iter_lines():
    if line: # filter out keep-alive new lines
        print json.loads(line)

rawDescription: The Requests streaming example does not work in my environment


code: print [int(re.sub(r'\\', r'0', raw(i)), 16) for i in s if len(raw(i))>1]

rawDescription: Convert String of Hex and Ints to List in Python


code:
data = [{u'num': u'081143282', u'location': u'My Town'},
        {u'num': u'081143282', u'location': u'My Town'}]
print [(int(currentDict["num"]), currentDict["location"]) for currentDict in data]

rawDescription: How can I simulate .fetchall() in Python?

description: Simulate .fetchall() in Python


code:
list  = ["A111", "T112", "I113", "A114", "T112", "S115", "I116", "D117", "I116", "K119", "R120", "E121", "T122", "C123", "V124", "V125", "V126"]

subsequence = []
for item in list:
    if item in subsequence:
        print subsequence
        index = subsequence.index(item)+1
        subsequence = subsequence[index:]
    subsequence.append(item)
print subsequence

rawDescription: Create all possible subsequence with unique elements


code:
def nest_in_pairs(original):
    return [original[i:i+2] for i in range(0,len(original),2)]

print nest_in_pairs(nest_in_pairs(original))

rawDescription: create sublists within sublists in python

description: Create sublists within sublists in python


code:
mydf = pandas.DataFrame.from_csv('myfile.csv', header=1)
print mydf.head(20) # Prints first 20 lines

rawDescription: equivalent of R's View for Python's pandas

description: Equivalent of R's View for Python's pandas


code: print "Center Colour (orig)", cimg[i[0], i[1]]

rawDescription: OpenCV pixel colours all the same

description: OpenCV pixel colours all the same


code: print s.decode('string_escape')

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

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


code:
for before, after in zip(my_list, my_clean_list):
    if before != after:
        print before, ' --> ', after

rawDescription: eliminate unwanted data from list using python

description: Eliminate unwanted data from list using python


code:
class yo:
    @staticmethod
    def say_hi():
        print "Hi there!"

rawDescription: Where is the best place to put support functions in a class?


code:
for b in range(56):
    for a in range(b+1, 57):
        print "a:", a, "b:", b

rawDescription: How to do a loop inside of a loop in python

description: Do a loop inside of a loop in python


code:
import copy
a = {}

def func():
    b = {'1': 'd'}
    return copy.deepcopy(b)

a = func()
print a

rawDescription: Save a dictionary value in python functions


code:
import numpy
import ctypes
mkl_rt = ctypes.CDLL('libmkl_rt.so')
mkl_get_max_threads = mkl_rt.mkl_get_max_threads
def mkl_set_num_threads(cores):
    mkl_rt.mkl_set_num_threads(ctypes.byref(ctypes.c_int(cores)))

mkl_set_num_threads(4)
print mkl_get_max_threads() # says 4

rawDescription: Using mkl_set_num_threads with numpy

description: Using mkl_set_num_threads with numpy


code:
import re

with open("input.txt", "rb") as f_input:
    start_token = raw_input("Please enter the start keyword: ")
    end_token = raw_input("Please enter the end keyword: ")
    reText = re.search("%s(.*?)%s" % (re.escape(start_token), re.escape(end_token)), f_input.read(), re.S)

    if reText:
        for word in re.findall(r"\b\w+\b", reText.group(1)):
            print word
    else:
        print "not found"

rawDescription: Trying to read certain text from a file that has strange characters. (Python)


code:
for parent in tree.findall('./Parent'):
    children = [child for child in parent.findall('./Child')]
    print '{} has {}'.format(parent.get('name'), ' '.join(c.get('name') for c in children))

rawDescription: Parse XML with ElementTree

description: Parse XML with ElementTree


code: print zip(x,y)

rawDescription: concise and fast zip operation on lists of lists in Python?

description: Concise and fast zip operation on lists of lists in Python


code:
print "/".join(dic['0'])   #sunday
print "/".join(dic['2'])   #tuesday

rawDescription: Python Function returns wrong value

description: Python Function returns wrong value


code:
matches = re.compile('<div>(.+?)(?: / ).*?</div>').findall(resp)
print matches # ['עבר']

rawDescription: Regex - select string until ' / '

description: Regex - select string until ' / '


code:
astring = "I cant figure this out"

if "can\'t" in astring:
    print "found it"
else:
    print "did not find it"

rawDescription: How do I search for a substring with a quotation mark inside a string in Python?

description: Search for a substring with a quotation mark inside a string in Python


code:
def outer():
    variab = [""]
    #some code including, presumably, a call to inner()

    def inner():
        # some code
        variab[0] = "new_value"

    print variab[0]

rawDescription: Change value of variable in outer-function


code:
import numpy as np
from nltk import ngrams

def everygrams(sequence):
    """
    This function returns all possible ngrams for n
    ranging from 1 to len(sequence).
    >>> list(everygrams('a b c'.split()))
    [('a',), ('b',), ('c',), ('a', 'b'), ('b', 'c'), ('a', 'b', 'c')]
    """
    for n in range(1, len(sequence)+1):
        for ng in ngrams(sequence, n):
            yield ng

doc1 = "Singularity is still a confusing phenomenon in physics".split()
doc2 = "Quantum theory still wins over String theory".split()
_vec1 = list(everygrams(doc1))
_vec2 = list(everygrams(doc2))
# Create a full dictionary of all possible ngrams.
vec_dict = list(set(_vec1).union(_vec2))
print 'Vector Dict:', vec_dict, '\n'
# Now vectorize the documents
vec1 = [_vec1.count(ng)/float(len(_vec1)) if ng in _vec1 else 0 for ng in vec_dict]
vec2 = [_vec2.count(ng)/float(len(_vec2)) if ng in _vec2 else 0 for ng in vec_dict]
print 'Vectorzied:', vec1, vec2, '\n'
print 'Similarity:', np.dot(vec1, vec2), '\n'

rawDescription: Normalize ranking score with weights

description: Normalize ranking score with weights


code:
class Foo(object):
    @deprecated("method bar is deprecated", "use baz instead")
    def bar(self, baz):
        print 'Goodbye', baz
f = Foo()
f.bar('World')

rawDescription: Get specific info about calling code in Python decorators


code:
the_items = ('OR','BA','AP','PR','CH')
new_items = ('Orange','Banana','Apple','Pear','Cherry')
corresp = dict(zip(the_items,new_items))

r = re.compile('(%s) *(\d+)' % '|'.join(the_items),
               re.IGNORECASE)

def fruiting(ma,longname = corresp):
    fresh = '%-12s %s' % (longname[ma.group(1).upper()],
                          ma.group(2) )
    tu = ('OLD: %r\n'
          'NEW: %r\n'
          %
          ( ma.group(),fresh) )
    print tu
    return fresh

print '%s%s' % (text, r.sub(fruiting,text))

rawDescription: Capturing the replaced text using re.sub in python


rawDescription: How to print a binary number

description: Print a binary number


code:
data= """
eth0      Link encap:Ethernet  HWaddr 08:ee:27:ff:b3:d7
          inet addr:10.0.2.45  Bcast:10.3.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe00:b3d7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:16178 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8559 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:14045795 (14.0 MB)  TX bytes:1355632 (1.3 MB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:666 errors:0 dropped:0 overruns:0 frame:0
          TX packets:666 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:72748 (72.7 KB)  TX bytes:72748 (72.7 KB)"""

import re

def findSeq(block,data):
    isInRightBlock= False
    for line in data.splitlines():
        if block in line:
            isInRightBlock= True
        m = re.match("\W*RX packets[^:]*:(\d+)", line)
        if m and isInRightBlock:
            isInRightBlock= False
            return m.group(1)

res= findSeq("eth0",data)
print res #Your Value

rawDescription: Extract string and assign to the variable using regex in Python


code:
medications = driver.find_elements_by_css_selector("input.medMedications")

# count
print len(medications)

# values
for medication in medications:
    print medication.get_attribute("value")

rawDescription: Getting value of a class in selenium and python


code:
if np.abs(determinant<0.000001):
    determinant=0
print determinant

rawDescription: Python Numpy - Treat really small numbers as zero


code:
df = df.groupby('category').apply(lambda x: list(x.product_id)[:2])
print df

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


code:
import math, random, time, itertools

def main (num=1000000) :
    # This small optimization speeds things up by a couple percent.
    sqrt = math.sqrt

    # Generate random points and numbers

    random.seed (0x7126434a2ea2a259e9f4196cbb343b1e6d4c2fc8)

    def random_point () :
        return [random.random(), random.random(), random.random()]

    def random_index () :
       return random.randint(0, num-1)

    # Big optimization:
    # Don't generate the lists of points.
    # Instead use list comprehensions that create iterators.
    # It's best to avoid creating lists of millions of entities when you don't
    # need those lists. You don't need the lists; you just need the iterators.
    pt_list = [random_point() for i in xrange(num)]
    rand_pts = [pt_list[random_index()] for i in xrange(num)]

    # Compute

    beg_time = time.clock()
    dist = 0

    # Don't loop over a range. That's too C-like.
    # Instead loop over some iterable, preferably one that doesn't create the
    # collection over which the iteration is to occur.
    # This is particularly important when the collection is large.
    for (pt0, pt1) in itertools.izip (pt_list, rand_pts) :

        # Small optimization: inner loop inlined,
        # intermediate variable 'val' eliminated.
        d0 = pt0[0]-pt1[0]
        d1 = pt0[1]-pt1[1]
        d2 = pt0[2]-pt1[2]

        dist += sqrt(d0*d0 + d1*d1 + d2*d2)

    end_time = time.clock()
    elap_time = (end_time - beg_time)

    print elap_time
    print dist

rawDescription: Why is computing point distances so slow in Python?


code:
raw= '''

                                    Gemara
                                    Kiddushin
                                    Morning

                                    12-04-2104

'''

from lxml import html
source2 = html.fromstring(raw)

Mp3filename = source2.xpath('//tr[1]//td[@colspan=2]//a[1]')
print html.tostring(Mp3filename[0])
# output :
#
#             ^notice that the attribute name changed to lower-case

rawDescription: xpath selectors not returning a match

description: Xpath selectors not returning a match


code:
from google.appengine.ext.webapp.util import run_wsgi_app
import webapp2
from urllib import urlencode, urlopen
from xml.dom import minidom
import hashlib
import os

HOST = 'http://api.flickr.com'
API = '/services/rest'
API_KEY = 'my_key'

debug = False

class MainPage(webapp2.RequestHandler):

    def get(self):
        self.response.headers['Content-Type'] = 'text/html'

        data = _doget('flickr.photos.search', auth=False, text='boston', per_page='2')

        if data:
            photos = data.getElementsByTagName("photo")
            for photo in photos:
                farm_id = 1 # ???
                server_id = photo.attributes['server'].value
                photo_id = photo.attributes['id'].value
                secret = photo.attributes['secret'].value
                photo_url = 'http://farm{farm_id}.staticflickr.com/{server_id}/{photo_id}_{secret}.jpg'.format(farm_id=farm_id,                                                                                server_id=server_id,                                                                                photo_id=photo_id,                                                                                secret=secret)
                self.response.write('<img src="{0}">'.format(photo_url))
        else:
            self.response.write('Error')

def _doget(method, auth=False, **params):
    #print "***** do get %s" % method

    params = params
    url = '%s%s/?api_key=%s&method=%s&%s'% \
      (HOST, API, API_KEY, method, urlencode(params))

    #another useful debug print statement
    if debug:
        print "_doget", url

    res = urlopen(url)
    res = res.read()

    return minidom.parseString(res)

application = webapp2.WSGIApplication([('/', MainPage),], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

rawDescription: Error while importing Flickr api using Python on Google App Engine


code:
for elem in b_array.decode():
    print elem

rawDescription: Python string to bytearray and back

description: Python string to bytearray and back


code:
from redis import Redis

r = Redis(db=1)
r.flushdb()
def store(t):
    h, m = map(int, t.split(':'))
    m = m - m % 5
    r.zincrby('tc', '%d:%02d' % (h, m), 1)

store('1:02')
store('1:30')
store('1:32')
store('1:35')

print r.zrange('tc', 0, -1, withscores=True)

rawDescription: How to bucket a time stamp in 5 minute intervals in python

description: Bucket a time stamp in 5 minute intervals in python


code:
row_number = 8
for rect in rectangles[8]: #Accessing a specific row
    print rect, #Will print multiple rectangles if present.

rawDescription: Load rectangle data from txt file with Python?


code:
def main(argv):
    seen = set()
    with open("test.txt", 'r') as infile:
        _spl = str.split
        for row in infile:
            v = _spl(row,"|",2)[1]
            if v:
                filename = "bby_" + v + ".dat"
                existingFile = open(filename, 'a')
                existingFile.write(row)
                existingFile.close()
                seen.add(v)
            else:
                print "Empty"

rawDescription: Get data between 1st and 2nd pipe in python


code:
local_var = 1
def method():
    print local_var

rawDescription: Forcing imported method to use local variables


rawDescription: Sanitizing user input path names

description: Sanitizing user input path names


rawDescription: Python: How can I print out an object as a regular dictionary without reference?


rawDescription: How to format a dict of lists as a table

description: Format a dict of lists as a table


rawDescription: python time interval algorithm sum

description: Python time interval algorithm sum


code:
with open('abc') as f:     #Always use with statement for handling files
    lis = []
    for line in f:         # for loop on file object returns one line at a time
        spl = line.split() # split the line at whitespaces, str.split returns a list
        lis.append(spl[0]) # append the first item to the output list, use int() get an integer
    print lis

rawDescription: convert first column of data from text file into a list in python

description: Convert first column of data from text file into a list in python


code:
from urllib2 import urlopen
from lxml import etree

url = 'http://www.uscho.com/standings/division-i-men/2011-2012/'

tree = etree.HTML(urlopen(url).read())

for section in tree.xpath('//section[starts-with(@id, "section_")]'):
    print section.xpath('h3[1]/text()')[0]
    for row in section.xpath('table/tbody/tr'):
        cols = row.xpath('td//text()')
        print '  ', cols[0].ljust(25), ' '.join(cols[1:])
    print

rawDescription: Extracting information from a webpage with python


code:
def Solve(level, available_numbers):
  if level == 10:
    return spot
  for i in available_numbers:
    print "We are %d far into level %d with fct. ONE" %(i,level)
    clearList(level)
    # Note you are not removing i from available_numbers yet
    spot[level] = pieces[i]
    result = checkPiece(level)
    print result
    if result == True:
      available_number = available_number.remove(i)
      Solve(level + 1, available_number)  # tail recursion!

    # note no need for your else statement. We haven't removed i from our list yet,
    # and we can just overwrite spot[level] on the next pass.

rawDescription: Recursive Python Methods

description: Recursive Python Methods


code:
from subprocess import Popen, PIPE

proc = Popen(["python2","client.py"], stdout=PIPE, stdin=PIPE, stderr=PIPE )

for text in ('A', 'B', 'C', 'D', 'E'):
  print text
  proc.stdin.write(text+"\n")
  proc.stdin.flush()
  i = ''
  result_list=[]
  while i != '\n':
    i = proc.stdout.read(1)
    result_list.append(i)
  print ("result " + "".join(result_list))

proc.stdin.write("END\n")

rawDescription: pipe input to and read output from another executable

description: Pipe input to and read output from another executable


code:
from lxml import etree
import urllib, StringIO

# Rip file from URL
resultado=urllib.urlopen('http://www.azlyrics.com/lyrics/ironmaiden/noprayerforthedying.html')
html = resultado.read()
# Parse html to etree
parser= etree.HTMLParser()
tree=etree.parse(StringIO.StringIO(html),parser)
# Apply the xpath rule
e = tree.xpath("//div[@style='margin-left:10px;margin-right:10px;']/text()")
# print output
for i in e:
    print str(i).strip()

rawDescription: Find words down (and between) string in Python


code:
import urllib2
request = urllib2.Request("http://www.haodf.com", headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'})
print urllib2.urlopen(request).read()

rawDescription: Want to fetch information from a website but it shows "250 forbidden" with Python


code:
f = open('test.txt','r')     #  Opening the "wrong" way again
for line in f:
    print "XXX{}YYY".format(line)

rawDescription: Unable to read huge (20GB) file from CPython


code:
dictionary = {'key1': ['value1', 'value2', 'value3'], 'key2': ['value1', 'value2']}

for key, values in sorted(dictionary.items()):
   for value in values:
       print '{} {}'.format(key, value)

rawDescription: Ordered dictionary output in python

description: Ordered dictionary output in python


code:
from sklearn.naive_bayes import MultinomialNB
from sklearn import datasets
iris = datasets.load_iris()

mnb = MultinomialNB()
y_pred = mnb.fit(iris.data, iris.target).predict_proba(iris.data)
tags = list(set(iris.target.tolist()))
probs = y_pred.tolist()

print [zip(i, tags) for i in probs]

rawDescription: How to get nbest predictions from sklearn naive bayes classifier? -python

description: Get nbest predictions from sklearn naive bayes classifier? -python


code: print ['%5.3f' % val for val in l]

rawDescription: Control a print format when printing a list in Python


code:
import re
color=['black', 'white', 'Blue']
s = "this is a phrase and it contains Blue"
print re.sub(r'(?i)\b(?:'+'|'.join(color)+r')\b', 'C', s)

rawDescription: Replace some string in some phrases using a dictionary of strings in Python


code: print dict.setdefault.__doc__

rawDescription: How do I pythonically set a value in a dictionary if it is None?

description: Pythonically set a value in a dictionary if it is None


code:
import random
import collections

items = range(10)

results = collections.Counter()

for __ in range(1000):
    number = random.sample(items, 3)
    results.update([tuple(number)])

print results.most_common(3)

[((2, 0, 6), 6), ((5, 4, 9), 6), ((1, 0, 3), 5)]

rawDescription: First Python program, "lotto prediction"


code:
from collections import defaultdict, Counter
import re

# optional trick to make each dict subclass print just like a regular one
class defaultdict(defaultdict):
    def __repr__(self):
        return dict(self).__repr__()

class Counter(Counter):
    def __repr__(self):
        return dict(self).__repr__()

# borrowed from my answer @ http://stackoverflow.com/a/23711788/355230
Tree = lambda: defaultdict(Tree)  # recursive definition - a dict of dicts

# some functions based on answer @ http://stackoverflow.com/a/14692747/355230
def nested_dict_get(nested_dict, keys):
    return reduce(lambda d, k: d[k], keys, nested_dict)

def nested_dict_set(nested_dict, keys, value):
    nested_dict_get(nested_dict, keys[:-1])[keys[-1]] = value

def nested_dict_update_count(nested_dict, keys):
    if nested_dict_get(nested_dict, keys[:-1]):  # exists, so update
        nested_dict_get(nested_dict, keys[:-1]).update([keys[-1]])
    else:                                        # doesn't exist, so create
        nested_dict_set(nested_dict, keys[:-1], Counter([keys[-1]]))

d = Tree()
pat = re.compile(r'[a-zA-z]+')
with open('abc.txt') as file:
    for line in file:
        nested_dict_update_count(d, [w for w in pat.findall(line.rstrip())])

print d  # prints like a regular dict, not defaultdict

rawDescription: creating dynamic nested dictionary

description: Creating dynamic nested dictionary


code:
questions = [Question('What is 0 + 0?', [0, 1, 2, 3], 0),
             Question('What is 1 + 1?', [0, 1, 2, 3], 2)]

test = Test(questions)

test.questions[0].answer(3) # Answers with the fourth item in answers, not three.
test.questions[1].answer(2)

print test.percentage()
# Prints 50.0

rawDescription: Assign variables to random elements from list? Python


code:
import itertools as it

a = [[1, 8, 9, 10],[2, 15, 3, -1],[3,1,6,11],[13,15,5,-2]]
rowSize = len(a[0])

maxVal = a[0][0]
maxIndex = 0
minVal = a[0][0]
minIndex = 0

for k, v in enumerate(it.chain(*a)):  # Loop through a flattened list of the values in the array, and locate the indices of the max and min values.
    if v > maxVal:
        maxVal = v
        maxIndex = k
    if v < minVal:
        minVal = v
        minIndex = k

U = (minIndex % rowSize) - (maxIndex % rowSize)
V = (minIndex / rowSize) - (maxIndex / rowSize)

print U, ",", V

rawDescription: Quiver vector from high value toward low value


code:
G = defaultdict(list)
for (s,t) in edges:
    G[s].append(t)
    G[t].append(s)

print DFS(G, '1')

rawDescription: finding longest path in a graph

description: Finding longest path in a graph


code:
from celery import Celery, signature, chain
import datetime as dt

app = Celery('test_tasks')
app.config_from_object('celery_config')

@app.task(bind=True, rate_limit=5)
def test_1(self):
    print dt.datetime.now()

app.control.broadcast('rate_limit',
                       arguments={'task_name': 'test_tasks.test_1',
                                  'rate_limit': 0.5})

test_task = signature('test_tasks.test_1').set(immutable=True)

l = [test_task] * 100

chain = chain(*l)
res = chain()

rawDescription: Set delay between tasks in group in Celery


code:
def setattr_logging_class(name, bases, dict_):
    """I am a metaclass"""
    def __setattr__(self, k, v):
        print "{} set attribute {} to {}".format(self, k, v)
        super(self.__class__, self).__setattr__(k, v)
    dict_['__setattr__'] = __setattr__
    cls = type(name, bases, dict_)
    return cls

class MyClass(object):
    __metaclass__ = setattr_logging_class
    def __init__(self):
        self.a = 1

obj = MyClass()
obj.b = 2

print obj.__dict__

rawDescription: Why is my metaclass not behaving as expected


code:
import re
p = re.compile(ur'(?![^()]*\))(\d+-\d+)')
test_str = u"Word word word; 123-125\nWord word (1000-1000)\nWord word word (1000-1000); 99-999\nWord word word word"
re.findall(p, test_str)

rawDescription: How to extract numbers from string using regular expression in Python except when within brackets?

description: Extract numbers from string using regular expression in Python except when within brackets


code:
from nltk.tag.stanford import NERTagger
def stanfordNERExtractor(sentence):
    st =  NERTagger('/usr/share/stanford-ner/classifiers/all.3class.distsim.crf.ser.gz',
               '/usr/share/stanford-ner/stanford-ner.jar')
    return st.tag(sentence.split())

stanfordNERExtractedLines = stanfordNERExtractor("New York")
print stanfordNERExtractedLines #[('New-York', 'LOCATION')]

rawDescription: Python NLTK interpret a fixed pattern of sentence and tokenize it


code:
import re
p = re.compile('^TestVar\s+(\d{8})\s+(\S+)')
m = p.match('TestVar 00000000  WWWWWW 222.222 222.222 222.222')
if m:
    print 'Match found: ', m.group(2) + '_' + m.group(1)
else:
    print 'No match'

rawDescription: get characters from string in python

description: Get characters from string in python


code:
import numpy as np
from scipy.signal import lfilter, lfiltic

c1 = 0.125
c2 = 0.5
c3 = 0.25

np.random.seed(123)
s = np.random.rand(8)
k = 3.0

# Original version (edited lightly)

res = np.zeros_like(s)
res[1] = k  # k is a constant
for i in range(2, len(s)):
    res[i] = c1*(s[i] + s[i-1])/2 + c2*res[i-1] + c3*res[i-2]

# Using scipy.signal.lfilter

# Coefficients of the filter's transfer function.
b = c1 * np.array([0.5, 0.5])
a = np.array([1, -c2, -c3])

# Create the initial condition of the filter such that
#     y[:2] == [0, k]
zi = lfiltic(b, a, [k, 0], x=s[1::-1])

y = np.empty_like(s)
y[:2] = [0, k]
y[2:], zo = lfilter(b, a, s[2:], zi=zi)

np.set_printoptions(precision=5)
print "res:", res
print "y:  ", y

rawDescription: python recursive vectorization with timeseries

description: Python recursive vectorization with timeseries


code:
def my_callback(oldVal, newVal):
    print "Value changed from {0} to {1}.".format(oldVal, newVal)

detector = EdgeTrigger(my_callback)

rawDescription: Python One-Shot, level-trigger, edge-trigger


code:
from urllib import unquote, quote
import json, urllib2, pprint
decoded = unicode(unquote("%DD%F2%EE%20%F2%E5%F1%F2"), "windows-1251")
print decoded
recoded = decoded.encode("utf-8")

rawDescription: How to decode string to use with Google Language Detection API?

description: Decode string to use with Google Language Detection API


code:
if any(day in daysOfTheWeek for day in days):
    for i, day in enumerate(days):
        if day in daysOfTheWeek:
            j=daysOfTheWeek.index(day)
            repaired=[daysOfTheWeek[k % len(daysOfTheWeek)].capitalize()
                                         for k in range(j-i, j+len(days)-i)]
            print days
            print repaired
            break
else:
    print 'cannot be repaired'

rawDescription: Comparing string lists to find missing value of sequence


code:
a = np.random.randint(0, 2, (10, 8))
a = np.vstack((a, np.array([[1,1,0,0,0,0,0,1]])))
b = np.unpackbits(bitmap[np.packbits(a, axis=1)], axis=1)
print a
print
print b

rawDescription: How to rotate a binary vector to minimum in Python

description: Rotate a binary vector to minimum in Python


code:
import settings
print settings.List1

rawDescription: Importing lists from another file

description: Importing lists from another file


code:
text = '[@a eee, fff fff, ggg @b eee, fff, ggg @c eee eee, fff fff,ggg ggg@d]'

from pyparsing import *

LBRACK,RBRACK,AT = map(Suppress,"[]@")

key = AT + Word(alphas)

# use originalTextFor to preserve whitespace between words between commas
list_item = originalTextFor(OneOrMore(Word(alphas)))

# define a key_value pair using Group to preserve structure
key_value = Group(key + Optional(delimitedList(list_item)))

parser = LBRACK + OneOrMore(key_value) + RBRACK
print parser.parseString(text)

rawDescription: Pyparsing sequence of repeating pattern

description: Pyparsing sequence of repeating pattern


code:
print missing_from_db

[{'status_code': 1, 'desc': 'Description sample5'},
 {'status_code': 4, 'desc': 'Description sample6'}]

print missing_from_api

[1, 2, 4]

rawDescription: Comparing lists of dictionaries in Python


code:
import re, random

def rand_replace(m):
    return random.choice(m.group(1).split('|'))

data = '''Random digits: (1|2|3), (4|5|6), (7|8|9).'''
print re.sub(r'\((.+?)\)', rand_replace, data)

rawDescription: Make random choice from string

description: Make random choice from string


code:
import random

class ArithmeticOperatorNode(object):
    def __init__(self, operator, *args):
        self.operator = operator
        self.children = args
    def eval(self):
        if self.operator == '+':
            return sum(x.eval() for x in self.children)
        assert False, 'Unknown arithmetic operator ' + self.operator
    def __str__(self):
        return '(%s)' % (' ' + self.operator + ' ').join(str(x) for x in self.children)

class ConstantNode(object):
    def __init__(self, constant):
        self.constant = constant
    def eval(self):
        return self.constant
    def __str__(self):
        return str(self.constant)

def build_tree(n):
    if n == 0:
        return ConstantNode(random.randrange(100))
    else:
        left = build_tree(n - 1)
        right = build_tree(n - 1)
        return ArithmeticOperatorNode('+', left, right)

node = build_tree(5)
print node
print node.eval()

rawDescription: Python: Optimizing a tree evaluator

description: Python: Optimizing a tree evaluator


code: print get_numbers(input)

rawDescription: How to parse a list of list of floats in python?

description: Parse a list of list of floats in python


code:
from bs4 import BeautifulSoup
from operator import itemgetter
from selenium import webdriver

url = "http://www.oddsportal.com/soccer/england/premier-league/everton-arsenal-tnWxil2o#over-under;2"
browser = webdriver.Firefox()

browser.get(url)
soup = BeautifulSoup(browser.page_source)
data_table = soup.find('div', {'id': 'odds-data-table'})

for div in data_table.find_all_next('div', class_='table-container'):
    row = div.find_all(['span', 'strong'])

    if len(row):
        print ','.join(cell.get_text(strip=True) for cell in itemgetter(0, 4, 3, 2, 1)(row))

rawDescription: How to scrape html table only after data loads using Python Requests?

description: Scrape html table only after data loads using Python Requests


code: print match.findall(str1)

rawDescription: Regex - Python: Capture three (3) words after a specific word


code:
import hashlib
print hashlib.md5('Hello World').hexdigest()

rawDescription: How to wrap a python class into a C++ class?

description: Wrap a python class into a C++ class


code:
def _tree_edges(nodes,r):
    # helper function for trees
    # yields edges in rooted tree with given nodes and branching ratio r
    nodes=iter(nodes)
    parents=[next(nodes)] # stack of max length r
    while parents:
        source=parents.pop(0)
        for i in range(r):
            try:
                target=next(nodes)
                parents.append(target)
                yield source,target
            except StopIteration:
                break

nodes = list('abcdefghijklm')
print list(_tree_edges(nodes,3))

rawDescription: Faster way to build a tree graph using Networkx Python?


code: exec code_object in module.__dict__

rawDescription: python importing code from a string

description: Python importing code from a string


code:
import socket
import threading
host = ''
port = 50000
connectionSevered=0

class client(threading.Thread):
    def __init__(self, conn):
        super(client, self).__init__()
        self.conn = conn
        self.data = ""
    def run(self):
        while True:
            self.data = self.data + self.conn.recv(1024)
            if self.data.endswith(u"\r\n"):
                print self.data
                self.data = ""

    def send_msg(self,msg):
        self.conn.send(msg)

    def close(self):
        self.conn.close()

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((host,port))
    s.listen(5)
except socket.error:
    print 'Failed to create socket'
    sys.exit()

print '[+] Listening for connections on port: {0}'.format(port)

conn, address = s.accept()
c = client(conn)
c.start()
print '[+] Client connected: {0}'.format(address[0])
c.send_msg(u"\r\n")
print "connectionSevered:{0}".format(connectionSevered)
while (connectionSevered==0):
    try:
        response = raw_input()
        c.send_msg(response + u"\r\n")
    except:
        c.close()

rawDescription: Python Socket Listening

description: Python Socket Listening


rawDescription: Django QuerySet values_list returning unknown characters


code:
lst = ['herp' , 'derp', 'foo' , 'derp', 'foo', 'foo' , 'foo']
from collections import Counter
counts = Counter(lst)
for word, count in counts.most_common():
    print '{} occurs {} times'.format(word, count)

import operator
for word, count in sorted(counts.items(), key=operator.itemgetter(1)):
    print '{} occurs {} times'.format(word, count)

rawDescription: I have a list in python which contains many names, I want to print out how many times each name occurs


code:
import math

def take_in():
    a = raw_input("Enter the value of side a -> ")
    b = raw_input("Enter the value of side b -> ")

    # Trying to convert to a float
    try:
        a, b = float(a), float(b)
        # If successfully converted, then we return
        if a > 0 and b > 0:
            return a, b
    except ValueError:
        pass
    # If we cannot return, then we return false, with a nice comment

    print "Invalid input"
    return False

def main():
    # Calling the function at least once
    valid = take_in()

    # While we are not getting valid input, we keep calling the function
    while not valid:
        # Assigning the value to valid
        valid = take_in()

    # Breaking the return tuple into a and b
    a, b = valid
    print math.sqrt(a ** 2 + b ** 2)

if __name__ == '__main__':
    main()

rawDescription: Stop python from going to the very last statement


code:
import re
regex = ur"\[P\] (.+?) \[/P\]+?"
line = "President [P] Barack Obama [/P] met Microsoft founder [P] Bill Gates [/P], yesterday."
person = re.findall(regex, line)
print(person)

rawDescription: Python regex findall

description: Python regex findall


code: print "\033c"

rawDescription: Python - Clearing the terminal screen more elegantly


code:
class A: # defined by the library
    def __init__(self, field):
        self.field = field

class B(A): # your fancy new class
    def __init__(self, field, field2):
        self.field = field
        self.field2 = field2 # B has some fancy extra stuff

def b_from_a(a_instance, field2):
    """Given an instance of A, return a new instance of B."""
    return B(a_instance.field, field2)

a = A("spam") # this could be your A instance from the library
b = b_from_a(a, "ham") # make a new B which has the data from a

print b.field, b.field2 # prints "spam ham"

rawDescription: Converting an object into a subclass in Python?


code:
import datetime
import thread
import threading

def foo():
     print datetime.datetime.now()
     print threading.active_count()

for x in range(0,300):
     t = threading.Timer(x + 1, foo)
     t.start()

rawDescription: Running a timer for few minutes in python


code:
class Base(object):
    def f(self):
        return self._f()

    def _f(self):
        pass

    def g(self):
        return self._g()

    def _g(self):
        pass

    FXN = {
        1 : f,
        2 : g
    }

class Child1(Base):
    def _f(self):
        print 'hi'

class Child2(Base):
    def _g(self):
        print 'hello'

if __name__ == '__main__':
    c1 = Child1()
    c2 = Child2()
    c1 = Child1.FXN[1](c1)
    c2 = Child1.FXN[2](c2)

rawDescription: python: Function pointer breaks polymorphism

description: Python: Function pointer breaks polymorphism


code:
image = sd.datasets.select('3d_image_name')
print "%.23f" % (image[2][2][2],)

rawDescription: Dump HDF4 arrays to ascii with full precision of the source file


code:
from sklearn.feature_extraction.text import TfidfVectorizer
from collections import defaultdict

lectures = ["this is some food", "this is some drink"]
vectorizer = TfidfVectorizer(ngram_range=(1,2))
X = vectorizer.fit_transform(lectures)
features_by_gram = defaultdict(list)
for f, w in zip(vectorizer.get_feature_names(), vectorizer.idf_):
    features_by_gram[len(f.split(' '))].append((f, w))
top_n = 2
for gram, features in features_by_gram.iteritems():
    top_features = sorted(features, key=lambda x: x[1], reverse=True)[:top_n]
    top_features = [f[0] for f in top_features]
    print '{}-gram top:'.format(gram), top_features

rawDescription: How to see top n entries of term-document matrix after tfidf in scikit-learn

description: See top n entries of term-document matrix after tfidf in scikit-learn


code:
names = soup.select('ul#secondaryconsumers > li.secondaryconsumerlist > div.name')
numbers = soup.select('ul#secondaryconsumers > li.secondaryconsumerlist > div.number')

print [name.text for name in names]
print [number.text for number in numbers]

rawDescription: Navigating to second string text using BeautifulSoup


code:
lst = [1970, 1991, 2012]
random.shuffle(lst)
for x in lst:
  print x

rawDescription: random iteration in Python

description: Random iteration in Python


code:
data = """
Name         At time Last  Inter\ Max    Logfile Location               Status
                     time   val   log
                           (mins) files
------------ ------- ----- ------ ------ ------------------------------ -------
foo1           now   16:00   60   100    flash:/schedule/foo1/          Job
                                                                        under
                                                                        progress
foo2           now     -     60   100    -                              Waiting
tech-support   now   16:00   60   100    flash:/schedule/tech-support/  Job
                                                                        under
                                                                        progress
                                                                        """
import re
print len(re.findall("Job\s+under\s+progress", data))

rawDescription: Finding a pattern inside a string in Python


code:
try:     # try block
    print 'I am before the raise.'
    raise Exception('An error has occured.') # throw an exception
    print 'I am after the raise.'            # won't be executed
except:  # optionally: `except Exception, ex:`
    print 'I am rescued.'
finally: # will always get executed
    print 'Always gets executed.'

rawDescription: Ruby equivalent for python try

description: Ruby equivalent for python try


code:
from itertools import chain, ifilterfalse
from collections import OrderedDict

def unique_everseen(iterable, key=None):
    "List unique elements, preserving order. Remember all elements ever seen."
    # unique_everseen('AAAABBBCCDAABBB') --> A B C D
    # unique_everseen('ABBCcAD', str.lower) --> A B C D
    seen = set()
    seen_add = seen.add
    if key is None:
        for element in ifilterfalse(seen.__contains__, iterable):
            seen_add(element)
            yield element
    else:
        for element in iterable:
            k = key(element)
            if k not in seen:
                seen_add(k)
                yield element

d1 = OrderedDict([('a', '1'), ('b', '2'),('c', 4)])
d2 = OrderedDict([('c', 3), ('e', 5)])   #dict containing items to be added at the front
new_dic = OrderedDict((k, d2.get(k, d1.get(k))) for k in \
                                           unique_everseen(chain(d2, d1)))
print new_dic

rawDescription: How can I add an element at the top of an OrderedDict in python?

description: Add an element at the top of an OrderedDict in python


code:
data = {'source_addr_long': '\x00\x13\xa2\x00@\xf7\nP', 'rf_data': 'python', 'source_addr': '\xe5\xbd', 'id': 'rx', 'options': '\x01'}
print repr(data['source_addr_long'])

rawDescription: garbage printing value of a key-value pair in python

description: Garbage printing value of a key-value pair in python


code:
def debug(*args):
    pass
    # print '[dbg]', ' '.join(str(a) for a in args)

class Interpreter:
    acc = 0
    defined = {}
    commands = []  # the stack

    def compile(self, program):
        program = textwrap.dedent(program).strip()
        lines = program.split("\n")
        lines.reverse()
        self.commands += lines

        while self.commands:
            command = self.commands.pop()
            debug('==> running:', command, 'stack:', self.commands)
            self.run_command(command)

    def run_command(self, cmd):
        if cmd.startswith("def(") and cmd.endswith(")"):
            name, code = cmd[4:-1].split('|')
            self.defined[name] = code.split(';')
            debug('::found def', self.defined)

        elif cmd in self.defined:
            debug('::found defined name', cmd, '=>', `self.defined[cmd]`)
            # mark command stack before executing function
            self.commands += ['$marker']
            self.commands += list(reversed(self.defined[cmd]))

        elif cmd == '$marker':
            pass  # do nothing (we get here if a def doesn't have an x0 when the acc is zero)

        elif cmd == 'x0':
            # exit function call if acc is zero
            if self.acc == 0:
                while self.commands:  # pop the stack until we get to the $marker
                    tmp = self.commands.pop()
                    if tmp == '$marker':
                        break

        elif cmd == "i":
            self.acc = int(input(">> "))

        elif cmd == "p":
            print(self.acc)

        else:
            self.acc += int(cmd)

rawDescription: Interpreter written in Python not working properly


code:
def gen_trees(nwklist, newid):
  yield [newid] + [nwklist]
  if isinstance(nwklist, list):
    for i in xrange(len(nwklist)):
      for l in gen_trees(nwklist[i], newid):
        yield nwklist[:i] + [l] + nwklist[i+1:]
  yield [nwklist] + [newid]

for l in gen_trees([1,[2,[3,4]]] , 5):
  print l

rawDescription: Python - Iteration over nested lists

description: Python - Iteration over nested lists


code:
from pyparsing import *

QUOTED = quotedString.setParseAction(removeQuotes)
OAND = CaselessLiteral("and")
OOR = CaselessLiteral("or")
ONOT = Literal("-")
WWORD = ~OAND + ~OOR + ~ONOT + Word(printables.replace("(", "").replace(")", ""))
TERM = (QUOTED | WWORD)
EXPRESSION = operatorPrecedence(TERM,
    [
    (ONOT, 1, opAssoc.RIGHT),
    (Optional(OAND,default="and"), 2, opAssoc.LEFT),
    (OOR, 2, opAssoc.LEFT)
    ])

STRING = OneOrMore(EXPRESSION) + StringEnd()

tests = """\
word and ward or wird
word werd or wurd""".splitlines()

for t in tests:
    print STRING.parseString(t)

rawDescription: Pyparsing problem with operators

description: Pyparsing problem with operators


code:
class Fred:
    def __eq__(self, other):
        print "eq was called!"
        return False
x = Fred()
print x == 23
#if eq and ne had an implied relationship,
#we'd expect this next line to also print "eq was called!"
print x != 23
#... but it doesn't.

rawDescription: No implied relationship among the comparison operators


rawDescription: Appending to dictionary value, pythonesque solution


code:
new_matrix = original_matrix.tocsr()
print new_matrix

rawDescription: Sparse matrix manipulation

description: Sparse matrix manipulation


code:
import re
lines = [line.strip() for line in open('file.csv')]
for x in lines:
    match=re.search(r'^_.*_$',x)
    if match: print x

rawDescription: Regular expression in Python for matching strings in a CSV file


code:
# make the dict and do whatever to it
ddd = dd_dict(a=1, b=2, c=3)

# now iterate over it, deferring deletes
with ddd:
    for k, v in ddd.iteritems():
        if k is "a":
            del ddd[k]
            print ddd     # shows that "a" is still there

print ddd                 # shows that "a" has been deleted

rawDescription: custom dict that allows delete during iteration

description: Custom dict that allows delete during iteration


code:
import re
string = u'fdfd{{dsfdss:dssssasa}} fdsfdsfds'
pattern = re.compile(ur'({{(\w+):(\w+)}})')
print(re.findall(pattern, string))

rawDescription: Python regular expression search doesn't works on a string contains colon(:)


code:
from random import randrange
import datetime

def random_date(start,l):
   current = start
   while l >= 0:
    current = current + datetime.timedelta(minutes=randrange(10))
    yield current
    l-=1

startDate = datetime.datetime(2013, 9, 20,13,00)

for x in reversed(list(random_date(startDate,10))):
    print x.strftime("%d/%m/%y %H:%M")

rawDescription: Create random time stamp list in python

description: Create random time stamp list in python


code:
print dt[tablenum] # list or dictionary

print dt[tablenum][element_number]

# or

for element in dt[tablenum]:
    print element

# or

alias = dt[tablenum]

for element in alias:
    print element

rawDescription: How do I create a table in Python? [rewritten, was a bit unclear]

description: Create a table in Python? [rewritten, was a bit unclear]


code: print name[-1]

rawDescription: How do I slice a sequence to get the last item?

description: Slice a sequence to get the last item


code:
V = [1,2,3,4,5]

f = open('Qin.txt')     # values in Qin.txt: 1, 3, 2
fdata = map(float, f.readlines())
f.close()

g = open('Qout.txt')    # values in Qout.txt: 4, 5, 5
gdata = map(float, g.readlines())
g.close()

for i,y in enumerate(gdata):
    for j,v in enumerate(V):
        V[j] = v + fdata[i] - y
    print V

rawDescription: How can I loop through a list changing the list elements at each time step, adding or subtracting input values that are in the txt files?

description: Loop through a list changing the list elements at each time step, adding or subtracting input values that are in the txt files


code: print my_module.pigtranslator()

rawDescription: Why does my function not run once I run the module and input?


code: betas=[[pd.ols(y=df[ref_col],x=df[col]).beta.x for col in df if col<>ref_col] for ref_col in df]

rawDescription: Pairwise regressions in Pandas

description: Pairwise regressions in Pandas


code:
import pandas as pd
from StringIO import StringIO

data =  StringIO("""Type     time
A        0
A        1
A        2
B        10
B        11
B        12""")
df = pd.read_csv(data, delim_whitespace=True, dtype="O")

def set_first(x):
    x["ptime"] = x.time.values[0]
    x = x[1:]
    r = x.Type + "." + x.time + "-" + x.Type + "." + x.ptime
    return r

print df.groupby(df.Type, group_keys=False).apply(set_first)

rawDescription: Generate combinations of groups in pandas Series or DataFrame


code:
def powercheck(lst):
    count = 0
    position = 0
    for pos in KnuthMorrisPratt(double(lst), lst):
        count += 1
        position = pos
        if count == 2:
            break

    return lst[:position]

def double(lst):
    for i in range(1,3):
        for elem in lst:
            yield elem

def main():
    print powercheck([1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1])

if __name__ == "__main__":
    main()

rawDescription: Detect whether sequence is a multiple of a subsequence in Python


code:
a = list()
print dir(a)

rawDescription: See class methods in Python console

description: See class methods in Python console


code:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Thread(Base):
    __tablename__ = 'thread'

    id = Column(Integer, primary_key=True)

class Post(Base):
    __tablename__ = 'post'

    id = Column(Integer, primary_key=True)
    thread_id = Column(Integer, ForeignKey('thread.id'))
    posted_at = Column(String)

s = Session()

scalar = select([func.max(Post.posted_at)]).where(Post.thread_id == Thread.id).as_scalar()

q = s.query(Thread).order_by(scalar.desc())

print q

rawDescription: Ordering by subquery in SQLAlchemy

description: Ordering by subquery in SQLAlchemy


rawDescription: Change the display of a list took from text file


rawDescription: Parsing date from OCR response in Python


code:
from sys import argv
script, input_filename = argv

def foo (arg1,arg2,arg3):
    print arg1,arg2,arg3.readline().strip("\r\n")

def bar (arg1, arg2, arg3):
    print arg2,arg1,repr(arg3.readline().strip("\r\n"))

open_input_file=open(input_filename)

foo ("Text1",1,open_input_file)
foo ("Text2",2,open_input_file)
bar ("Text3",3,open_input_file)
bar ("Text4","4",open_input_file)

rawDescription: Python .readline()

description: Python .readline()


code:
a = [1,2,3,2,1,5,6,5,5,5]

import collections
print [item for item, count in collections.Counter(a).items() if count > 1]

## [1, 2, 5]

rawDescription: Find and list duplicates in Python list

description: Find and list duplicates in Python list


code:
import sys
from random import choice

WORDPAIRS = [('Humiliate', 'abasement'), ('Swelling', 'billowing')]

def ask():
    pair = choice(WORDPAIRS)
    while True:
        answer = raw_input("%s: " % pair[0]).lower()
        if answer == pair[1]:
            print "well done!"
            return

def main():
    try:
        while True:
            ask()
    except KeyboardInterrupt:
        sys.exit(0)

if __name__ == "__main__":
    main()

rawDescription: Python- how to init random function

description: Python- how to init random function


code:
#!/usr/bin/env python

''' Convert Unix Epoch seconds to (proleptic) Gregorian date

    Algorithm by E. G. Richards, https://en.wikipedia.org/wiki/Julian_day

    See http://stackoverflow.com/q/35796786/4014959

    Python implementation by PM 2Ring 2016.03.07
'''

from time import gmtime

def epoch_seconds_to_gregorian_date(eseconds):
    # Algorithm parameters for Gregorian calendar
    y = 4716; j = 1401; m = 2; n = 12; r = 4; p = 1461
    v = 3; u = 5; s = 153; w = 2; B = 274277; C = -38

    #Julian day, rounded
    J = int(0.5 + eseconds / 86400.0 + 2440587.5)

    f = J + j + (((4 * J + B) // 146097) * 3) // 4 + C
    e = r * f + v
    g = (e % p) // r
    h = u * g + w
    D = (h % s) // u + 1
    M = (h // s + m) % n + 1
    Y = (e // p) - y + (n + m - M) // n

    return Y, M, D

# Tests
def test(s):
    t = gmtime(s)
    gmdate = t.tm_year, t.tm_mon, t.tm_mday
    e2gdate = epoch_seconds_to_gregorian_date(s)
    assert gmdate == e2gdate, (t, gmdate, e2gdate)
    return '%d.%d.%d' % e2gdate

print 'hours'
for i in xrange(25):
    s = 3600 * i
    print i, test(s)

print '\ndays'
for i in xrange(32):
    s = 86400 * i
    print i, test(s)

print '\n2 days by seconds...'
for s in xrange(86400 * 2):
    test(s)

n = 50
print '\n%d years by days...' % n
for i in xrange(365 * n):
    s = 86400 * i
    test(s)

print 'Ok'

rawDescription: How to calculate the current month from python time.time()

description: Calculate the current month from python time.time()


code:
import json
json_data = json.loads('''{"data":{"numberOfPages":512,"domainSummaryDTOs":[{"id":"http://www.cdfdmy.com/","timestamp":"Tue May 14 12:59:28 GMT 2013","title":"【推荐】成都空压机|四川空压机|成都空气压缩机|四川空气压缩机|成都螺杆空压机|四川螺杆空压机|成都双螺杆空压 机|四川双螺杆空压机|成都福道贸易有限公司","exploitabilityLevel":4,"bsqli":2,"sqli":2,"url":"http://www.cdfdmy.com/","xss":0},{"id":"http://www.chushijob.com/","timestamp":"Tue May 14 12:59:28 GMT 2013","title":"餐饮世界人才网-中国厨师人才网-中国酒店人才网","exploitabilityLevel":5,"bsqli":3,"sqli":2,"url":"http://www.chushijob.com/","xss":2},{"id":"http://www.hbenshi.com/","timestamp":"Tue May 14 12:59:28 GMT 2013","title":"恩施旅游网--恩施大峡谷 腾龙洞 利川 清江闯滩 土司城 欢迎您！","exploitabilityLevel":5,"bsqli":3,"sqli":4,"url":"http://www.hbenshi.com/","xss":1},{"id":"http://bbs.laiyb.com/","timestamp":"Mon Apr 29 03:30:09 GMT 2013","title":"莱阳论坛_莱阳吧_莱阳人的网络社区 -","exploitabilityLevel":4,"bsqli":4,"sqli":1,"url":"http://bbs.laiyb.com/","xss":0},{"id":"http://photostudio-town.com/","timestamp":"Mon Apr 29 03:30:09 GMT 2013","title":"フォトスタジオ・タウン-就職証明写真・お受験写真・オーディション写真-","exploitabilityLevel":5,"bsqli":1,"sqli":1,"url":"http://photostudio-town.com/","xss":1},{"id":"http://sp.sosfang.com/","timestamp":"Mon Apr 29 03:30:09 GMT 2013","title":"上海商铺出租/转让,上海门面房出租信息/上海门面转让-上海商铺网","exploitabilityLevel":2,"bsqli":0,"sqli":1,"url":"http://sp.sosfang.com/","xss":0},{"id":"http://www.msdssafe.com/","timestamp":"Sat Apr 06 11:03:33 GMT 2013","title":"MSDS查 询网 英文MSDS查询网 MSDS MSDS报告 MSDS下载 msds是什么意思 MSDS安全网","exploitabilityLevel":4,"bsqli":15,"sqli":3,"url":"http://www.msdssafe.com/","xss":0},{"id":"http://www.tiananjidian.com/","timestamp":"Sat Apr 06 11:15:03 GMT 2013","title":"上海精工阀门厂总代理★上海精工阀门|上工牌阀门|精工阀门厂|上海阀门|精工阀门|广东阀门|广州阀门|惠州阀门|东莞阀门|佛山阀门|深圳阀门|中山阀门|潮州阀门|珠海阀门|河源阀门|汕头阀门|肇庆阀门|","exploitabilityLevel":3,"bsqli":0,"sqli":2,"url":"http://www.tiananjidian.com/","xss":1},{"id":"http://www.ywscocie.com/","timestamp":"Sat Apr 06 11:20:46 GMT 2013","title":"","exploitabilityLevel":2,"bsqli":0,"sqli":2,"url":"http://www.ywscocie.com/","xss":0},{"id":"http://bookingsbarbados.com/","timestamp":"Wed May 15 00:54:31 GMT 2013","title":"Bookings Caribbean | Barbados Bookings Center. Book barbados Hotels and Activities. Search, tourism ","exploitabilityLevel":5,"bsqli":4,"sqli":2,"url":"http://bookingsbarbados.com/","xss":18}],"rowsFound":5115,"qTime":1}}
''')
for entry in json_data['data']['domainSummaryDTOs']:
    print entry['id']

rawDescription: decoding json in python

description: Decoding json in python


code:
import logging
import os

os.makedirs("log")

formatter = logging.Formatter('%(asctime)s %(levelname)s: %(funcName)s:%(lineno)d %(message)s')

# create logger with 'spam_application'
logger = logging.getLogger('factory')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('log/factory.log')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)

# just so we can see things as they happen in stdout/stderr
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)

# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)

logger.debug("Debug log msg")
logger.info("Info log msg")
logger.warn("Warning log msg")

with open("log/factory.log", "r") as f:
    print f.read()

rawDescription: How to use python logging to log to files?

description: Use python logging to log to files


code:
class ClassA(object):
    def __init__(self):
        self.var1 = 1
        self.var2 = 2

    def methodA(self):
        self.var1 = self.var1 + self.var2
        return self.var1

class ClassB(ClassA):
    def __init__(self, class_a):
        self.var1 = class_a.var1
        self.var2 = class_a.var2

object1 = ClassA()
sum = object1.methodA()
object2 = ClassB(object1)
print sum

rawDescription: How would I access variables from one class to another?


code:
print df
#most_exhibitions  India  Netherlands  Pakistan China USA United Kingdom
#name
#Alex                  5                      8     3
#Bill                  4                               11
#Bob                                5                   1
#Joe                                                                   2

rawDescription: How do I make new columns in dataframe from a row of a different column?

description: Make new columns in dataframe from a row of a different column


code:
import sys, os
import pythoncom
import win32com
import winerror
# importers check was old py2exe current uses frozen
if hasattr(sys, 'frozen'):
    # we are running as py2exe-packed executable
    print "is an exe"
    pythoncom.frozen = 1
else:
   print "not an exe"

class CImtg:
    _reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
    #
    # COM declarations
    #
    _reg_clsid_ = "{24c0e3fe-58e7-4485-87dc-9f9e823b85e1}"
    _reg_desc_ = "IMTGroup Python test object"
    _reg_progid_ = "ImtGroup.Test"
    if hasattr(sys, 'frozen'):
        # In the py2exe-packed version, specify the module.class
        # to use. In the python script version, python is able
        # to figure it out itself.
        _reg_class_spec_ = "imtg_server.CImtg"
        print "set reg_class_spec"
        print _reg_class_spec_
    ###
    ### Link to typelib - uuid matches uuid for type library in idl file
    _typelib_guid_ = '{d665e9d0-71a9-4e23-a1b4-abe3376d5c58}'
    _typelib_version_ = 1, 0
    _com_interfaces_ = ['IImtGroupApp']

    def __init__(self):
    ### initialize something here if necessary
    ### The item below is not used in this example
        self.MyProp1 = 10

    def EchoString(self,in1):
        return "Echoing " + in1

    def AddNumbers(self, in1, in2):
        return in1 + in2

def BuildTypelib():
    from distutils.dep_util import newer
    this_dir = os.path.dirname(__file__)
    # when running as a exe this directory includes the exe name
    if this_dir.endswith('imtg_server.exe'):
       this_dir = this_dir[:-15]
    idl = os.path.abspath(os.path.join(this_dir, "imtg.idl"))
    tlb=os.path.splitext(idl)[0] + '.tlb'
    if os.path.isfile(idl):
      # test for idl - if no idl don't create tlb assume its there
      # Comment below for building exe as we will have type library
      if newer(idl, tlb):
         print "Compiling %s" % (idl,)
         rc = os.system ('midl "%s"' % (idl,))
         if rc:
             raise RuntimeError("Compiling MIDL failed!")
        # Can't work out how to prevent MIDL from generating the stubs.
        # just nuke them
         for fname in "dlldata.c imtg_i.c imtg_p.c imtg.h".split():
             os.remove(os.path.join(this_dir, fname))

    print "Registering %s" % (tlb,)
    tli=pythoncom.LoadTypeLib(tlb)
    pythoncom.RegisterTypeLib(tli,tlb)
def UnregisterTypelib():
    k = CImtg
    try:
        pythoncom.UnRegisterTypeLib(k._typelib_guid_,
                                    k._typelib_version_[0],
                                    k._typelib_version_[1],
                                    0,
                                    pythoncom.SYS_WIN32)
        print "Unregistered typelib"
    except pythoncom.error, details:
        if details[0]==winerror.TYPE_E_REGISTRYACCESS:
            pass
        else:
            raise
if __name__=='__main__' or __name__ =='imtg_server':
      print "checking frozen"

      if hasattr(sys, 'frozen'):
         # running as packed executable

         if '--unregister' in sys.argv or '--register' in sys.argv:
            if '--unregister' in sys.argv:
              # Unregister the type-libraries.
              UnregisterTypelib()
              import win32com.server.register
              win32com.server.register.UseCommandLine(CImtg)
            else:
              # Build and register the type-libraries.
              BuildTypelib()
              import win32com.server.register
              win32com.server.register.UseCommandLine(CImtg)

      else:
        if '--unregister' in sys.argv:
          # Unregister the type-libraries.
          UnregisterTypelib()
          import win32com.server.register
          win32com.server.register.UseCommandLine(CImtg)
        else:
          if '--register' in sys.argv:
            # Build and register the type-libraries.
            BuildTypelib()
            import win32com.server.register
            win32com.server.register.UseCommandLine(CImtg)

rawDescription: Python Com Server unable to create instance when wrapped with py2exe - error object has no attribute


rawDescription: Python regex format

description: Python regex format


code:
def isactive():
    for output in results:
        print output.get("Active")

rawDescription: Looping over multiple dictionaries and not just the last dictionary in python


code:
from banyan import SortedSet, OverlappingIntervalsUpdator # pip install banyan

def maximize_nonoverlapping_count(intervals):
    # build "interval" tree sorted by the end-point O(n log n)
    tree = SortedSet(intervals, key=lambda (start, end): (end, (end - start)),
                     updator=OverlappingIntervalsUpdator)
    result = []
    while tree: # until there are intervals left to consider
        # pop the interval with the smallest end-point, keep it in the result
        result.append(tree.pop()) # O(log n)

        # remove intervals that overlap with the popped interval
        overlapping_intervals = tree.overlap(result[-1]) # O(m log n)
        tree -= overlapping_intervals # O(m log n)
    return result

rawDescription: Python - Removing overlapping lists

description: Python - Removing overlapping lists


code:
def collatz(n):
    print n
    while n > 1:
        n = n // 2 if n % 2 == 0 else n * 3 + 1
        print n

rawDescription: Is there any case in python when using a while loop would be best practice?


code:
import re
import ast

data = """ { Countries = { USA = 1; "Connections" = { "1 Flights" = 0; "10 Flights" = 0; "11 Flights" = 0; "12 Flights" = 0; "13 Flights" = 0; "14 Flights" = 0; "15 Flights" = 0; "16 Flights" = 0; "17 Flights" = 0; "18 Flights" = 0; "More than 25 Flights" = 0; }; "Single Connections" = 0; "No Connections" = 0; "Delayed" = 0; "Technical Fault" = 0; "Others" = 0; }; }"""

def arrify(string):
    string = string.replace("=", " : ")
    string = string.replace(";", " , ")
    string = string.replace("\"", "")
    stringDict = string.split()
    # print stringDict
    newArr = []
    quoteCosed = True
    for i, splitStr in enumerate(stringDict):
        if i > 0:
            # print newArr
            if not isDelim(splitStr):
                if isDelim(newArr[i-1]) and quoteCosed:
                    splitStr = "\"" + splitStr
                    quoteCosed = False

                if isDelim(stringDict[i+1]) and not quoteCosed:
                    splitStr += "\""
                    quoteCosed = True

        newArr.append(splitStr)

    newString = " ".join(newArr)
    newDict = ast.literal_eval(newString)
    return normalizeDict(newDict)

def isDelim(string):
    return str(string) in "{:,}"

def normalizeDict(dic):
    for key, value in dic.items():
        if type(value) is dict:
            dic[key] = normalizeDict(value)
            continue
        dic[key] = normalize(value)
    return dic

def normalize(string):
    try:
        return int(string)
    except:
        return string

print arrify(data)

rawDescription: Python : nested key value data parsing

description: Python : nested key value data parsing


code:
data = "Hello~|||~World!~|||~Lorem~|||~Ipsum"
delimiter = "~|||~"
new_text = "ABC"
data = data.replace(delimiter, delimiter+new_text)
print data

rawDescription: How to append to a stored variable to all delimiters?

description: Append to a stored variable to all delimiters


code:
import xml.etree.ElementTree as ET

data = """
<parent>
    <child attr="test">1</child>
    <child attr="something else">2</child>
    <child other_attr="other">3</child>
    <child>4</child>
    <child attr="test">5</child>
</parent>
"""

parent = ET.fromstring(data)
print [child.text for child in parent.findall('.//child[@attr]')]
print [child.text for child in parent.findall('.//child[@attr="test"]')]

rawDescription: Find all nodes by attribute in xml using Python


code:
import random
import decimal
import requests
from bs4 import BeautifulSoup

base_url = 'https://j2c-com.com/Euronaval14/catalogueWeb/cataloguerecherche.php?listeFavoris=&typeRecherche=1&typeRechSociete=&typeSociete=&typeMarque=&typeDescriptif=&typeActivite=&choixSociete=&choixPays=&choixActivite=&choixAgent=&choixPavillon=&choixZoneExpo=&langue=gb&rnd=0.1410133063327521'
fiche_url = 'https://j2c-com.com/Euronaval14/catalogueWeb/fiche.php'
reload_url = 'https://j2c-com.com/Euronaval14/catalogueWeb/reload.php'
data_url = 'https://j2c-com.com/Euronaval14/catalogueWeb/ajaxSociete.php'

def generate_random_number(i,d):
    "Produce a random between 0 and 1, with 16 decimal digits"
    return str(decimal.Decimal('%d.%d' % (random.randint(0, i),random.randint(0, d))))

# start session
session = requests.Session()

r = session.get(base_url)
soup = BeautifulSoup(r.content)
for span in soup.select('table#tableResultat tr span'):
    cle = span.get('id')

    session.post(reload_url)
    session.post(fiche_url, data={'page': 'page:catalogue',
                                  'pasFavori': '1',
                                  'listeFavoris': '',
                                  'cle': cle,
                                  'stand': '',
                                  'rnd': generate_random_number(0, 9999999999999999)})
    session.post(reload_url)
    pop = session.post(data_url, data={'cle': cle,
                                       'rnd': generate_random_number(0, 9999999999999999)})

    print pop.text

rawDescription: Scraping content from AJAX onclick pop-up


code:
import copy

def treeify(lst):
  tree = [copy.deepcopy(cat) for cat in lst if cat['level'] == '1']
  for el in tree:
    el["children"] = []
  for i in xrange(len(lst)):
    for j in xrange(len(tree)):
      if lst[i]["parent_id"] == tree[j]["id"]:
        tree[j]["children"].append(copy.deepcopy(lst[i]))
  return tree

list_of_dicts = [
  {'name':"Category1", 'id': '7', 'parent_id': '7', 'level': '1'},
  {'name':"Category3", 'id': '33', 'parent_id': '7', 'level': '2'},
  {'name':"Category5", 'id': '334', 'parent_id': '33', 'level': '3'},
  {'name':"Category10", 'id': '23', 'parent_id': '7', 'level': '2'},
  {'name':"Category2", 'id': '8', 'parent_id': '8', 'level': '1'},
  {'name':"Category6", 'id': '24', 'parent_id': '8', 'level': '2'}
]

tree = treeify(list_of_dicts)
for d in tree:
  print d

rawDescription: Convert list-of-dicts into tree

description: Convert list-of-dicts into tree


code:
import BeautifulSoup
import urllib2
import itertools
import random

class Crawler(object):
    """docstring for Crawler"""

    def __init__(self):

        self.soup = None                                        # Beautiful Soup object
        self.current_page   = "http://www.python.org/"          # Current page's address
        self.links          = set()                             # Queue with every links fetched
        self.visited_links  = set()

        self.counter = 0 # Simple counter for debug purpose

    def open(self):

        # Open url
        print self.counter , ":", self.current_page
        res = urllib2.urlopen(self.current_page)
        html_code = res.read()
        self.visited_links.add(self.current_page)

        # Fetch every links
        self.soup = BeautifulSoup.BeautifulSoup(html_code)

        page_links = []
        try :
            page_links = itertools.ifilter(  # Only deal with absolute links
                                            lambda href: 'http://' in href,
                                                ( a.get('href') for a in self.soup.findAll('a') )  )
        except Exception: # Magnificent exception handling
            pass

        # Update links
        self.links = self.links.union( set(page_links) )

        # Choose a random url from non-visited set
        self.current_page = random.sample( self.links.difference(self.visited_links),1)[0]
        self.counter+=1

    def run(self):

        # Crawl 3 webpages (or stop if all url has been fetched)
        while len(self.visited_links) < 3 or (self.visited_links == self.links):
            self.open()

        for link in self.links:
            print link

if __name__ == '__main__':

    C = Crawler()
    C.run()

rawDescription: Scrapy recursive link crawler

description: Scrapy recursive link crawler


code:
string = (r'http://www.youtube.com/watch?v=e4lHTj9xFqE'.replace('watch?','')).replace('=','/')
print string
#http://www.youtube.com/v/e4lHTj9xFqE

rawDescription: django - using regex to strip and replace a urlfield

description: Django - using regex to strip and replace a urlfield


code:
import re
p = re.compile(r'^(?P<SERVER>\S+)\s+(?P<SERVERKEY>\S+)\s+(?P<COMMAND>\S+)\s+(?P<FOLDERPATH>/.*?)\s+(?P<RETENTION>\d+)\s+(?P<TRANSFERMODE>\S+)\s+(?P<OUTPUTPATH>[A-Z]:\\.*?\\)\s+(?P<LOGTO>NO|[A-Z]:\\.*?)(?:\s+(?P<MAXSIZE>\d+)(?:\s+(?P<OFFSET>\d+))?)?\s*$',re.M)
data = r"""loveserver love copy /muffin* 20 bin C:\Puppies\ NO 256 300
loveserver love copy /muffin* 20 bin C:\Puppies\ NO 256
loveserver love copy /hats* 300 ascii C:\Puppies\no\ C:\log\love.log 256"""
import pprint
for match in p.finditer(data):
  print pprint.pprint(match.groupdict())

rawDescription: How do I process a string such as this using regular expressions?

description: Process a string such as this using regular expressions


code:
def runner(f, callable):
  def inner(*args, **kwds):
    for i in xrange(1000):
      pos = list(callable())
      pos.extend(args)
      f(*pos, **kwds)
  return inner

def f(a, b, c, d = 3):
  print a, b, c, d

runner(f, lambda: (random.randint(1,1000),))(3, 5, d = 7)

rawDescription: Run python function multiple times with different arguments


code:
def one(regions):
    with open(regions,'r') as f:
        for line in f:
            a = line.split()
            b = f.next().split()
            assert(a[0]=='Start:' and b[0]=='Stop:')
            assert(a[1]==b[1])
            yield (a[1], (int(a[3]),int(b[3])) )

def two(positions,regions):
    d = dict(one(regions))
    with open(positions,'r') as g:
        for line in g:
            ls = tuple(line.split())
            yield(ls + (1 if d[ls[0]][0]<= int(ls[1]) <=d[ls[0]][1] else 0,))

print list(two('filetwo.txt','fileone.txt'))

print '================================'

print '\n'.join('%s\t%s\t%s' % x for x in two('filetwo.txt','fileone.txt'))

rawDescription: Python: How to read in file once but not exhaust iterator in nested for loops


code:
from datetime import datetime, timedelta
from collections import OrderedDict

dates = ["2014-10-10", "2016-01-07"]

def monthlist_short(dates):
    start, end = [datetime.strptime(_, "%Y-%m-%d") for _ in dates]
    return OrderedDict(((start + timedelta(_)).strftime(r"%b-%y"), None) for _ in xrange((end - start).days)).keys()

def monthlist_fast(dates):
    start, end = [datetime.strptime(_, "%Y-%m-%d") for _ in dates]
    total_months = lambda dt: dt.month + 12 * dt.year
    mlist = []
    for tot_m in xrange(total_months(start)-1, total_months(end)):
        y, m = divmod(tot_m, 12)
        mlist.append(datetime(y, m+1, 1).strftime("%b-%y"))
    return mlist

assert monthlist_fast(dates) == monthlist_short(dates)

if __name__ == "__main__":
    from timeit import Timer
    for func in "monthlist_short", "monthlist_fast":
        print func, Timer("%s(dates)" % func, "from __main__ import dates, %s" % func).timeit(1000)

rawDescription: Generate list of months between interval in python


code:
import argparse
from functools import wraps

parser = argparse.ArgumentParser()
parser.add_argument('-d', '--dont-run-test', dest='dont_run_test', action='store_true', default=False)
arguments = parser.parse_args()

def run(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        if not arguments.dont_run_test:
            return f(*args, **kwargs)
        else: # To demonstrate it works
            print 'Skipping test %s' % f.__name__
    return wrapper

@run
def my_test():
    print 'This should run only if the -d command line flag is not specified'

my_test()

rawDescription: Run a method only if a tag or keyword is given using Python


code:
for cell in set:
    children = cell.getchildren()
    if not children:
        print "stage2"
    else:
        print "stage1"
        for child in children:
            print child.xpath('node()')[0].strip()

rawDescription: how to express `there is a child node in current node` in lxml

description: Express `there is a child node in current node` in lxml


code:
import pymel.core.uitypes as pmui

model_panel = pmui.ModelEditor(pm.getPanel(withFocus=True))
print pmui.ModelEditor.getRendererName(model_panel)

rawDescription: How to query current viewport renderer in maya

description: Query current viewport renderer in maya


code:
a.py
avariable = None

class a():

    def method(self):
        global avariable
        avariable = 100
        print "variable is", avariable

rawDescription: How do I access inherited variables from another module when they are changed in parent?

description: Access inherited variables from another module when they are changed in parent


code:
import string, math

def dms_string_to_radians(dms_string):

    dms = [float(x) for x in string.split(dms_string, '-')]
    degrees = sum(
            [scale * val for scale, val in zip([1.0, 1/60.0, 1/3600.0], dms)])

    return math.radians(degrees)

print dms_string_to_radians('47-39-28')

rawDescription: How to convert DMS to Radian with a given format : DD-MM-SS in python

description: Convert DMS to Radian with a given format : DD-MM-SS in python


code:
def run(project_dir, main_class, specific_args):
    classpath = []

    for root, dirs, files in os.walk(project_dir):
        has_properties = False
        for f in files:
            if f.endswith('.jar'):
                classpath.append(os.path.join(root, f))
            if f.endswith('.properties'):
                has_properties = True
        if has_properties:
            classpath.append(root)

    classpath_augment = ':'.join(classpath)

    print java_command, classpath_augment, main_class, specific_args

rawDescription: walking directory in python to concatenate a classpath variable

description: Walking directory in python to concatenate a classpath variable


code:
try:
    callable(Fun.goodbye)
except AttributeError, e:
    return False

rawDescription: Is there a Python equivalent to Ruby's respond_to?


code:
from bs4 import BeautifulSoup

data = """
<title>The Dormouse's story</title>

The Dormouse's story

Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.

...

"""

soup = BeautifulSoup(data)
for tag in soup.find_all():
    print tag.name

rawDescription: Finding all tags and attributes in a HTML


code: print make_triangle(13,5,2)[::-1]

rawDescription: Triangle Siblings in python

description: Triangle Siblings in python


code:
lookupdict = dict(lookups(L))
print lookupdict['bb'] # but you have to do 'bb' before trying 'b' ...

rawDescription: Most optimal way to reverse search list of similar strings


code:
sorted(cityPopulation.iteritems(),key=lambda (k,v): v[0],reverse=True) #1990
sorted(cityPopulation.iteritems(),key=lambda (k,v): v[2],reverse=True) #2010

rawDescription: Sorting a defaultdict by value in python


code: print [(key, infos.get(key)) for key in order]

rawDescription: python dictionary sorting oddly (want non-alphabetical)

description: Python dictionary sorting oddly (want non-alphabetical)


code:
if 'n' in playAgain:
    print "too bad"
    playAgain = 'yes'

rawDescription: How do I use 2 loops, and if it is false, repeat the program indefinetely

description: Use 2 loops, and if it is false, repeat the program indefinetely


code:
def tick():
    print time.time()
    s.enter(5, 0, addCount, ())
    s.run()
    print time.time()
    print counter

rawDescription: What does action mean for scheduled events in python?


code:
import threading

def action(arg):
    print arg[0],

mutable = [0]
for i in range(100):
    mutable[0] = i
    threading.Thread(target=action, args=(mutable,)).start()

rawDescription: thread in python vs C# vs Ruby

description: Thread in python vs C# vs Ruby


code:
def get_commodity_data(commodity):
    for item in data['lastStarport']['commodities']:
        if item['name'] == commodity:
            return item
    return None

def galPrice(commodity):
    comdata = get_commodity_data(commodity)
    return {
        'meanPrice': comdata['meanPrice'],
        'buyPrice': comdata['buyPrice'],
        'sellPrice': comdata['sellPrice'],
    }

print galPrice('Explosives')

rawDescription: Retrieve multiple values from JSON

description: Retrieve multiple values from JSON


code:
def perm2(n):
    if n == 1:
        print a
    else:
        for i in range(n):  # << here
            swap(i, n-1)
            perm2(n-1)
            swap(i, n-1)

rawDescription: Learning Python - Permutation Error

description: Learning Python - Permutation Error


code:
import subprocess
retcode = subprocess.call(["/path/to/myCprogram", "/path/to/file.c"])
if retcode == 0:
   print "success!"

rawDescription: Simplest way to integrate python gui app with c console app


code:
import wx

########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Tutorial",size=(700, 400))

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)
        self.index = 0

        self.list_ctrl = wx.ListCtrl(panel, size=(-1,200),
                         style=wx.LC_REPORT
                         |wx.BORDER_SUNKEN
                         )
        self.list_ctrl.InsertColumn(0, 'Machine Name')
        self.list_ctrl.InsertColumn(1, 'Sim Port')
        self.list_ctrl.InsertColumn(2, 'Sim Script', width=125)
        self.list_ctrl.InsertColumn(3, 'Status', width=150)
        self.list_ctrl.InsertColumn(4, 'Status Detail', width=300)

        btn = wx.Button(panel, label="Add Line")
        btn.Bind(wx.EVT_BUTTON, self.add_line)

        btn2 = wx.Button(panel,label = "Add Script")
        btn2.Bind(wx.EVT_BUTTON, self.add_script)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
        sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(btn2,0,wx.ALL|wx.CENTER, 5)
        panel.SetSizer(sizer)

        menu = wx.Menu()
        #exit = menu.Append(-1, "Exit")
        #self.Bind(wx.EVT_MENU, self.OnExit, exit)
        menuBar = wx.MenuBar()
        self.SetMenuBar(menuBar)
        #wx.StaticText(self.m_panel1, -1,(25,25))
        self.popupmenu = wx.Menu()
        for text in "Start Stop Remove".split():
            item = self.popupmenu.Append(-1, text)
            self.Bind(wx.EVT_MENU, self.OnPopupItemSelected, item)
            self.list_ctrl.Bind(wx.EVT_CONTEXT_MENU, self.ShowPopUp)

        #self.list_ctrl.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.ShowPopUp)
    #----------------------------------------------------------------------

    def add_script(self, event):
        # mp is item 2
        self.new = NewWindow(self, id=-1)
        self.new.Show()

    def add_line(self, event):
        #line = "Line %s" % self.index
        line = 'LDR2'
        self.list_ctrl.InsertStringItem(self.index,line)
        self.list_ctrl.SetStringItem(self.index, 1, "22000")
        self.list_ctrl.SetStringItem(self.index, 2, "LF1-trk")
        self.list_ctrl.SetStringItem(self.index, 3, "running")
        self.list_ctrl.SetStringItem(self.index, 4, "last Status info received")
        self.index += 1

    def add_line_dynamic(self, machine_name, sim_port, sim_script, status):
        print machine_name, sim_port, sim_script, status
        self.list_ctrl.InsertStringItem(self.index, machine_name)
        self.list_ctrl.SetStringItem(self.index, 1, sim_port)
        self.list_ctrl.SetStringItem(self.index, 2, sim_script)
        self.list_ctrl.SetStringItem(self.index, 3, status)
        self.list_ctrl.SetStringItem(self.index, 4, "none")
        self.index += 1

    def OnPopupItemSelected(self, event):
        item = self.popupmenu.FindItemById(event.GetId())
        text = item.GetText()
        wx.MessageBox("You selected item '%s'" % text)

    def ShowPopUp( self, event ):
        #pos = self.list_ctrl.GetPosition()
        pos = event.GetPosition()
        pos = self.list_ctrl.ScreenToClient(pos)
        self.list_ctrl.PopupMenu(self.popupmenu,pos)
        event.Skip()

class NewWindow(wx.Frame):

    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'New Window', size=(400,300))
        #self.frame1 = MainWindow;
        self.parent = parent

        wx.Frame.CenterOnScreen(self)
        panel = wx.Panel(self)

        sizer = wx.GridBagSizer(5, 5)

        text1 = wx.StaticText(panel, label="Machine")
        sizer.Add(text1, pos=(2, 0), flag=wx.LEFT, border=10)

        comboBox1Choices = [ u"LDR2", u"AT02" ]
       # self.m_comboBox4 = wx.ComboBox( panel, wx.ID_ANY, u"Running", wx.DefaultPosition, wx.DefaultSize, m_comboBoxChoices, 0 )
        self.comboBox1 = wx.ComboBox(panel,wx.ID_ANY,u"", wx.DefaultPosition, wx.DefaultSize, comboBox1Choices, 0 )
        #comboBox1.SetSelection(1)
        sizer.Add( self.comboBox1, pos=(2, 1), span=(1, 3), flag=wx.TOP|wx.EXPAND )

        text2 = wx.StaticText(panel, label="Sim Port")
        sizer.Add(text2, pos=(3, 0), flag=wx.LEFT|wx.TOP, border=10)

        comboBox2Choices = [ u"22000", u"23000" ]
        self.comboBox2 = wx.ComboBox(panel,wx.ID_ANY,u"", wx.DefaultPosition, wx.DefaultSize, comboBox2Choices, 0 )
        sizer.Add( self.comboBox2, pos=(3, 1), span=(1, 3), flag=wx.TOP|wx.EXPAND )

        text3 = wx.StaticText(panel, label="Sim Scripts")
        sizer.Add(text3, pos=(4, 0), flag=wx.TOP|wx.LEFT, border=10)

        comboBox3Choices = [ u"LF12-Ldr", u"LF1-Trk",u"CN1-Trk" ]
        self.comboBox3 = wx.ComboBox(panel,wx.ID_ANY,u"", wx.DefaultPosition, wx.DefaultSize, comboBox3Choices, 0 )
        sizer.Add( self.comboBox3, pos=(4, 1), span=(1, 3), flag=wx.TOP|wx.EXPAND )

        text4 = wx.StaticText(panel, label="Status")
        sizer.Add(text4, pos=(5, 0), flag=wx.TOP|wx.LEFT, border=10)

        comboBox4Choices = [ u"Not Started", u"Running" ]
        self.comboBox4 = wx.ComboBox(panel,wx.ID_ANY,u"", wx.DefaultPosition, wx.DefaultSize, comboBox4Choices, 0 )
        sizer.Add( self.comboBox4, pos=(5, 1), span=(1, 3), flag=wx.TOP|wx.EXPAND )

        self.button4 = wx.Button(panel, label="Update and Close")
        sizer.Add(self.button4, pos=(7, 3))

        sizer.AddGrowableCol(2)

        panel.SetSizer(sizer)

        self.button4.Bind(wx.EVT_BUTTON,self.onupdateandClose)

    def onupdateandClose(self,event):

       # self.index = 0
        self.main = MyForm()
        sc = self.comboBox1.GetSelection()
        machine_name = self.comboBox1.GetString(sc)
        #sa = self.main.m_staticText5.SetLabel(self.comboBox1.GetString(sc))
        #sa = self.frame1.m_staticText4.SetLabel(self.comboBox1.GetString(sc))
        #print sa
        machine_name = self.comboBox1.GetValue()
        #print sc
        sim_port = self.comboBox2.GetValue()
        sim_script = self.comboBox3.GetValue()
        status = self.comboBox4.GetValue()
        #num_items = self.main.list_ctrl.GetItemCount()
        #print num_items
        num_items = 0

        #line = "Line %s" % self.index
        #print line
        # line = 'LDR2'

        # self.main.list_ctrl.InsertStringItem(num_items,line)
        # self.main.list_ctrl.SetStringItem(num_items, 1, 'gjg')
        # self.main.list_ctrl.SetStringItem(num_items, 2, str(cb2))
        # num_items += 1

        self.parent.add_line_dynamic(machine_name, sim_port, sim_script, status)
        self.Close()

    # def test(self, event):
    #     self.new = NewWindow(parent=None, id=-1)
    #     self.new.Show()
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

rawDescription: Wxpython Listctrl

description: Wxpython Listctrl


code:
for k,v in dic.items():
    a,b = k.split(':')
    print a.ljust(30), b.ljust(30), v

rawDescription: How to format a dictionary of lists as a table in Python?

description: Format a dictionary of lists as a table in Python


code:
figure(1, figsize=(6,6))
ax = axes([0.1, 0.1, 0.8, 0.8])
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15, 30, 45, 10]
total = sum(fracs)
explode=(0, 0.05, 0, 0)
pie(fracs, explode=explode, labels=labels,
    autopct=lambda(p): '{:.0f}'.format(p * total / 100),
    shadow=True, startangle=90)
show()

rawDescription: Matplotlib pie-chart: How to replace auto-labelled relative values by absolute values


code:
class Programs:

    def __init__(self):  # class constructur (called at creation time)
        self.name = ""   # the default name is the empty string

    def ask(self):
        while 1: # infinite loop
            name = raw_input("Name: ")
            if name == "":
                print "Ops! Retry"
            else:
                print "Hello", name
                break  # this will break the loop

        self.name = name  # assign to self.name the value name

rawDescription: How to get user input within a Method (python)

description: Get user input within a Method (python)


code:
def foo(arg, **kwargs):
    print kwargs

foo('a', b="2", c="3", bar="bar")

rawDescription: what does double star followed by variable name mean in python?

description: What does double star followed by variable name mean in python


code:
txt='''\
Cluster 1
one
one
two
two
two
Cluster 2
two
one
one
two
Cluster 3
one
Cluster 4
two
two
one
'''

import re

for t in re.findall(r'(Cluster \d)(.*?)(?=Cluster|$)', txt, re.S):
    print t[0],t[1].count('one'), t[1].count('two')

rawDescription: Count duplicate records between two pattern


code:
def download_file(file_id, mimeType, filename):
    if "google-apps" in mimeType:
        # skip google files
        return
    request = drive_service.files().get_media(fileId=file_id)
    fh = io.FileIO(filename, 'wb')
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print "Download %d%%." % int(status.progress() * 100)

if __name__ == '__main__':
    flow = client.flow_from_clientsecrets(
      'client_secrets.json',
      scope='https://www.googleapis.com/auth/drive.readonly',
      redirect_uri='urn:ietf:wg:oauth:2.0:oob')

    auth_uri = flow.step1_get_authorize_url()
    webbrowser.open(auth_uri)

    print auth_uri

    auth_code = raw_input('Enter the auth code: ')

    credentials = flow.step2_exchange(auth_code)
    http_auth = credentials.authorize(httplib2.Http())

    drive_service = discovery.build('drive', 'v3', http_auth)
    files = drive_service.files().list().execute()
    for f in files['files']:
        print f['name']
        download_file(f['id'], f['mimeType'], f['name'])

rawDescription: Google drive API download files - python - no files downloaded


code:
wget = "wget  http://{IPaddress}"
wget = wget.format(IPaddress=ipAddr)
print "The command wget is %s" %wget

os.system(wget)

rawDescription: String format function does not parse my slashes character?


code:
import scipy.stats
import scipy.optimize
import math
import collections

def fit1(counter):
    def neg_log_likelihood(scale):
        total = 0.0
        for x, count in counter.iteritems():
           total += (math.log(scale) + x / scale) * count
        return total

    optimize_result = scipy.optimize.minimize(neg_log_likelihood, [1.0])
    if not optimize_result.success:
        raise Exception(optimize_result.message)
    return optimize_result.x[0]

def fit2(counter):
    data = []
    # Create an array where each key is repeated as many times
    # as the value of the counter.
    for x, count in counter.iteritems():
        data += [x] * count
    fit_result = scipy.stats.expon.fit(data, floc = 0)
    return fit_result[-1]

def test():
    c = collections.Counter()
    c[1] = 193260
    c[2] = 51794
    c[3] = 19112
    c[4] = 9250
    c[5] = 6486

    print "fit1 'scale' is %f " % fit1(c)
    print "fit2 'scale' is %f " % fit2(c)

test()

rawDescription: Fit a distribution to a Counter in scipy


code:
mask = 2 ** 32 - 1
x = int(raw_input())
print format(x ^ mask, '032b')

rawDescription: Bit reversing in Python

description: Bit reversing in Python


code:
fd = open('data.txt')
lines = [line.strip() for line in fd]
adict = {}
for line in lines:
  splitted = [i for i in line.split(',')]
  key = splitted[0]
  adict[key] = splitted[1:]
print adict

rawDescription: Creating dictionaries from a text file

description: Creating dictionaries from a text file


code:
from spyne.util.appreg import get_application

app = get_application('example2', 'Application').null
print app.service.say_hello('world')

rawDescription: Timeout while trying to use one service to call another deployed in the same domain


code:
class Dog(object):
    def bark(self):
        print "WOOF"

rawDescription: Python: call parent instancemethod

description: Python: call parent instancemethod


code: print [ delistify(x) for x in my_list ]

rawDescription: How to convert some items in a tuple from list to float?

description: Convert some items in a tuple from list to float


rawDescription: Confused myself with matrix and array addition


code:
df = pd.DataFrame({"A":np.arange(9), "B":np.arange(10, 19)},
                  index=np.arange(100, 109))
for tmp in np.split(df, 3):
    print tmp

rawDescription: pandas dataframe groupby a number of rows

description: Pandas dataframe groupby a number of rows


code: kx*wx

rawDescription: Python matrix represented as (40000,)

description: Python matrix represented as (40000,)


code:
p = subprocess.Popen(['ruby', 'ruby_program.rb'], stdin=subprocess.PIPE,
                                          stdout=subprocess.PIPE)
ruby_question = p.stdout.readline()
answer = calculate_answer(ruby_question)
p.stdin.write(answer)
print p.communicate()[0]  # prints further info ruby may show.

rawDescription: How do I take the output of one program and use it as the input of another?

description: Take the output of one program and use it as the input of another


code:
s = 'LAST LAST2 First First2'
print [i for i in s.split() if not i.isupper()]

rawDescription: How to extract non-uppercase string elements for first and last names?

description: Extract non-uppercase string elements for first and last names


code:
import sys
from PySide import QtCore

class Foo(QtCore.QObject):
    sig = QtCore.Signal(int)

def bar():
    print 'running bar'

def single_shot_connect(signal, slot):
    def disconnect(*args, **kwargs):
        signal.disconnect(slot)
        signal.disconnect(disconnect)
    signal.connect(slot)
    signal.connect(disconnect)

if __name__ == '__main__':
    app = QtCore.QCoreApplication(sys.argv)

    foo = Foo()
    single_shot_connect(foo.sig, bar)

    foo.sig.emit(1)
    foo.sig.emit(2)

    QtCore.QTimer.singleShot(1000, app.exit)

    sys.exit(app.exec_())

rawDescription: How to make generic PySide single shot slot

description: Make generic PySide single shot slot


code:
class Principal(object):
    def __init__(self):
        self.attribute = "an attribute of %s" % self
        self.Secondary = generateSecondary(self)

    def proceed(self):
        print self.attribute

rawDescription: How can I acess a metod from an object inside a class in python 2.6?

description: Acess a metod from an object inside a class in python 2.6


code:
def pingIP(self, ip):
    proc = subprocess.Popen("ping %s" % ip, shell=True,
                            stdout=subprocess.PIPE)
    print
    while True:
        line = proc.stdout.readline()
        wx.Yield()
        if line.strip() == "":
            pass
        else:
            print line.strip()
        if not line: break
    proc.wait()

rawDescription: Redirect command prompt output to a python generated window


code: print "Weekday"

rawDescription: how to find current day is weekday or weekends in Python?

description: Find current day is weekday or weekends in Python


code:
def run(self):
    while True:
        time.sleep(10-time.time()%10) #sleep to the start of next min
        print time.time()

rawDescription: Python Thread that runs at the start of every minute


rawDescription: Find section of a string from keyword to keyword


code:
somedict = {'howdy': lambda: 'bar'}
print somedict['howdy']()

rawDescription: Python dictionary commands with dynamic functions?


code:
def start():
    return lambda: increment(0)

def fizz(n):
    print 'fizz'
    return lambda: increment(n)

def buzz(n):
    print 'buzz'
    return lambda: increment(n)

def fizzbuzz(n):
    print 'fizzbuzz'
    return lambda: increment(n)

def increment(n):
    n = n + 1
    if n > 2000:
        # strictly speaking, transitions that takes no arguments
        # like terminate don't need to be wrapped in lambda
        # this is added here only for symmetry with others
        return lambda: terminate()
    elif n % 3 == 0 and n % 5 == 0:
        return lambda: fizzbuzz(n)
    elif n % 3 == 0:
        return lambda: fizz(n)
    elif n % 5 == 0:
        return lambda: buzz(n)
    else:
        print n
        return lambda: increment(n)

def terminate():
    raise StopIteration

def trampoline(func):
    try:
        while True:
            func = func()
    except StopIteration:
        pass

trampoline(lambda: start())

rawDescription: Is it advisable to write recursive functions in Python


code:
s = "123123STRINGabcabc"

def find_between( s, first, last ):
    try:
        start = s.index( first ) + len( first )
        end = s.index( last, start )
        return s[start:end]
    except ValueError:
        return ""

def find_between_r( s, first, last ):
    try:
        start = s.rindex( first ) + len( first )
        end = s.rindex( last, start )
        return s[start:end]
    except ValueError:
        return ""

print find_between( s, "123", "abc" )
print find_between_r( s, "123", "abc" )

rawDescription: Find string between two substrings

description: Find string between two substrings


code:
import arcpy
image_names=["img" + str(s) for s in range(1,143)]
wd="Y:/" #have this as your directory where all rasters are located
arcpy.env.workspace = wd
for image_name in image_names:
    print image_name
    raster_list=arcpy.ListRasters(image_name+"-*", "tif")
    import os
    outws = wd+"Stacked_Images_Folder/"
    os.makedirs(outws)
    arcpy.CompositeBands_management(raster_list, outws + image_name+ "_stacked_img.tif")

rawDescription: Batch processing raster images with Arcpy


code:
my_file = get_next_line("Input.txt")
print next(my_file)
print next(my_file)

rawDescription: Reading from a file: find the new line

description: Reading from a file: find the new line


code:
import sys, inspect, functools, StringIO

def supress_print(func):

    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        #print inspect.getouterframes(inspect.currentframe())[3][3], func.__name__
        if inspect.getouterframes(inspect.currentframe())[3][3] != 'main':
            stdout = sys.stdout
            sys.stdout = StringIO.StringIO()
            val = func(*args, **kwargs)
            sys.stdout = stdout
            return val
        else:
            return func(*args, **kwargs)

    return wrapper

@supress_print
def f1():
    print 'foo'

@supress_print
def f2(boo=True):
    f1()
    if boo:
        print 'bar'
    else:
        print 'black sheep'

def f3():
    f2()
    print 'shh!!!'

rawDescription: How to silence a function's output to console?

description: Silence a function's output to console


code:
import inspect

class Super:
    def __init__(self, name):
        print 'instantiated'
        self.name = name

print inspect.getargspec(Super.__init__)

rawDescription: Introspecting arguments from the constructor function __init__ in Python


code:
import pydoc
print pydoc.getdoc(dict)
print pydoc.getdoc(dict.get)

rawDescription: Where do I find Python Classes?

description: Where do I find Python Classes


code:
def matrixmult (A, B):
    rows_A = len(A)
    cols_A = len(A[0])
    rows_B = len(B)
    cols_B = len(B[0])

    if cols_A != rows_B:
      print "Cannot multiply the two matrices. Incorrect dimensions."
      return

    # Create the result matrix
    # Dimensions would be rows_A x cols_B
    C = [[0 for row in range(cols_B)] for col in range(rows_A)]
    print C

    for i in range(rows_A):
        for j in range(cols_B):
            for k in range(cols_A):
                C[i][j] += A[i][k]*B[k][j]
    return C

rawDescription: matrix using python

description: Matrix using python


rawDescription: Python looping through specific element in list


rawDescription: extracting paragraph in python using lxml

description: Extracting paragraph in python using lxml


code:
from fractions import gcd

def generateTriples(limit):
    for n in xrange(1, limit):
        if (n**2 + (n+1)**2 > limit):
            break
        for m in xrange(n+1, limit, 2):
            if (n**2 + m**2 > limit):
                break
            if (gcd(n, m) > 1):
                continue
            yield (m**2 - n**2, 2*m*n, m**2 + n**2)

def generate_triples_limit_leg(leg):
    limit = leg**2 / 2 + 1

    for triple in generateTriples(limit):
        if min(triple) <= leg:
            yield triple

print list(generate_triples_limit_leg(i))

rawDescription: Generating Pythagorean Triples with either leg <= some limit