code:
import sys

def add_to_data(*items):
    def decorator(func):
        name = func.__name__
        data = sys._getframe(1).f_locals.get('data')
        if data is not None:
            data.setdefault(name, []).extend(items)
        return func
    return decorator

rawDescription: Modifying a class attribute via method decorator


code:
for a in soup.find_all('a'):
    id = a.get('id')
    value = a.string # This is a NavigableString
    unicode_value = unicode(value)

rawDescription: Using Beautiful Soup4 can I get the <a> embedded text?


code:
file = open("newfile.py", "w")
file.write("""
ModelName = 'Model-1'
mdb.models[ModelName].Material(name='Mat-hom'))
mdb.models[ModelName].materials['Mat-hom'].Density(table=((rho_hom, ), )))
mdb.models[ModelName].materials['Mat-hom'].Elastic(type=ENGINEERING_CONSTANTS, table=((E1, E2, E3, v12, v13, v23, G12, G13, G23), ))

""")

file.close()

rawDescription: Creating a python file with python

description: Creating a python file with python


code:
class MyForm(forms.ModelForm):
       ...
       def __init__(self, *args, **kwargs):
           super(MyForm, self).__init__(*args, **kwargs)
           self.fields['cost'].localize = True
           self.fields['cost'].widget.is_localized = True

rawDescription: Django Model Form validation failing for FloatField


rawDescription: Python Serialization (to JSON) issue

description: Python Serialization (to JSON) issue


code:
now = datetime.datetime.now()
if now.weekday() < 5 and now.hour == 18 and 0 <= now.minute <= 15:
    do_something()

rawDescription: Check current time is between two times

description: Check current time is between two times


code:
import datetime as dt
mydatetimes = [dt.datetime(x[2], x[1], x[0]) + dt.timedelta(hours=x[3]) for x in myarray]

rawDescription: time values in numpy array to array of datetimes

description: Time values in numpy array to array of datetimes


code: return dictionary

rawDescription: Function not returning the Value of its Key


code: numpy.nan is numpy.NaN is numpy.NAN

rawDescription: What's the difference between nan, NaN and NAN


code: '<br/>'.join(['%s:: %s' % (key, value) for (key, value) in d.items()])

rawDescription: python: dictionary to string, custom format?

description: Python: dictionary to string, custom format


rawDescription: Classifying Data in a New Column

description: Classifying Data in a New Column


code:
from xml.etree.ElementTree import ElementTree, dump

tree = ElementTree()
tree.parse('file.xml')

items = tree.findall('item')
for item in items:
    link, description = list(item)
    link.text = description.text

dump(tree)

rawDescription: Python Minidom : Change Value of Node

description: Python Minidom : Change Value of Node


code:
import bisect

intervals = [(999, 2333, 'int1'), (0, 128, 'int2'), (235, 865, 'int3')]
intervals.sort()

def find_int(intervals, val):
   pos = bisect.bisect_left([interval[1] for interval in intervals], val)
   if pos < len(intervals) and val >= intervals[pos][0]:
      return intervals[pos]
   else:
      return None

print(find_int(intervals, 0))
print(find_int(intervals, 1))
print(find_int(intervals, 200))
print(find_int(intervals, 998))
print(find_int(intervals, 999))
print(find_int(intervals, 1000))
print(find_int(intervals, 2333))
print(find_int(intervals, 2334))

rawDescription: Is there a way to avoid the linear search on this?


code:
def run(*posargs, **argdict):
   """
   This does the work and can be called with:
   import scriptname
   scriptname.run(someargs)
   """
   # Code goes here and uses posargs[n] where it would use sys.argv[n+1]

if __name__ == "__main__":
   import sys
   run(sys.argv[1:])

rawDescription: How to pass args to embedded python sub-interpreter?

description: Pass args to embedded python sub-interpreter


code:
myfile = open("test.out", "w")

for tstep in arange (1,500,1):
    Intensity=np.sum(Itarray*detarray)
    myfile.write(str(tstep*t*10**9) + '\t' + str(Intensity.real) + '\n')

myfile.close()

rawDescription: saving a set of arrays from python output to csv file

description: Saving a set of arrays from python output to csv file


code:
s = "2 3 4 5"
s.split()  # split string using spaces
n, m, x, y = [int(i) for i in s.split()] # used `int()` for str --> int
n            # iterate over list and convert each number into int

rawDescription: Convert string into integer separated by space


code:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

plt.close('all')
fig = plt.figure()
ax = fig.gca(projection='3d')

X = np.linspace(-5, 5, 43)
Y = np.linspace(-5, 5, 28)
X, Y = np.meshgrid(X, Y)

varone=np.random.rand(75,28,43) * 5.0 - 10.0
Z=varone[0,:,:]

cset = [[],[],[]]

# this is the example that worked for you:
cset[0] = ax.contourf(X, Y, Z, zdir='z', offset=5,
                      levels=np.linspace(np.min(Z),np.max(Z),30),cmap='jet')

# now, for the x-constant face, assign the contour to the x-plot-variable:
cset[1] = ax.contourf(Z, Y, X, zdir='x', offset=5,
                      levels=np.linspace(np.min(Z),np.max(Z),30),cmap='jet')

# likewise, for the y-constant face, assign the contour to the y-plot-variable:
cset[2] = ax.contourf(X, Z, Y, zdir='y', offset=-5,
                      levels=np.linspace(np.min(Z),np.max(Z),30),cmap='jet')

# setting 3D-axis-limits:
ax.set_xlim3d(-5,5)
ax.set_ylim3d(-5,5)
ax.set_zlim3d(-5,5)

plt.show()

rawDescription: Contourf on the faces of a Matplotlib cube


code:
#!/usr/bin/env python3
from urllib.request import urlopen

try:
     with urlopen("http://apache.domain.com/get.php?id=1001") as response:
          print("Has content" if response.read(1) else "Content Empty")
except OSError as e:
    print("error happened: {}".format(e))

rawDescription: Using Python fetch remote URL

description: Using Python fetch remote URL


rawDescription: Django prefetch_related from foreignkey with manytomanyfield not working


rawDescription: Grid appearing inside table in matplotlib/pyplot graph


rawDescription: Python selenium click on button link

description: Python selenium click on button link


code:
import re
def tokenize(document):
    return re.findall("[A-Za-z'-]+", document)
tokenize("Hi there. What's going on? first-class")

rawDescription: python split a text file function

description: Python split a text file function


code:
import sqlite3

# Get connections to the databases
db_a = sqlite3.connect('database_a.db')
db_b = sqlite3.connect('database_b.db')

# Get the contents of a table
b_cursor = db_b.cursor()
b_cursor.execute('SELECT * FROM mytable')
output = b_cursor.fetchall()   # Returns the results as a list.
#b_cursor.close()

# Insert those contents into another table.
a_cursor = db_a.cursor()
for row in output:
    a_cursor.execute('INSERT INTO myothertable VALUES (?, ?, ...etc..., ?, ?)', row)
db_a.commit()
a_cursor.close()
b_cursor.close()

rawDescription: Merge tables from two different databases - sqlite3/Python


code:
@patch("my_view.call_external_api", autospec=True)
class MyTest(unittest.TestCase):
    def setUp(self):
        self.client = Client()

    def test_get_data(self, mock_call_external_api):
        self.client.get('/example/url/')
        self.assertTrue(mock_call_external_api.called)

rawDescription: Django UnitTest with Mock

description: Django UnitTest with Mock


code:
# indices between 0 and 255, corresponding to the rows of cr
idx = np.arange(cr.shape[0])

im = np.empty_like(ra)
im[..., 0] = np.interp(ra[..., 0], cr[:, 2], idx) # red
im[..., 1] = np.interp(ra[..., 1], cr[:, 1], idx) # green
im[..., 2] = np.interp(ra[..., 2], cr[:, 0], idx) # blue

rawDescription: Speed up numpy interpolation using for and if-else by vectorization?


code:
from django.db.models import Q

q = Q()
for filter in filters:
    q |= Q(*f.args)
data = Data.objects.filter(q)

# Do things with `data`

rawDescription: Best Django practice, filter ORM or python?


code: ' '.join([y[i] for i in x])

rawDescription: Python - how to print values of a list given many indexes?


rawDescription: How to get the tag of a shape when cliked

description: Get the tag of a shape when cliked


code:
#imports here

class YourTestCase(TestCase):
    fixtures = ['user-data.json']

    def setUp(self):
        self.client = Client()

    def test_empty_datas(self):
        self.client.login(username='something', password='something')
        response = self.client.get('/path/to/view/')  # or reverse by name
        self.assertEqual(response.status_code, 302,
            'View did not redirect on empty queryset.')

    def test_populated_datas(self):
        self.client.login(username='something', password='something')
        Data.objects.create(some_field=some_value)
        response = self.client.get('/path/to/view/')  # or reverse by name
        self.assertEqual(response.status_code, 200,
            'View did not return a 200.')

rawDescription: Django unit test. Simple example

description: Django unit test. Simple example


code:
# This will run on the 'default' database.
Author.objects.all()
# So will this.
Author.objects.using('default').all()
# This will run on the 'other' database.
Author.objects.using('other').all()
my_object.save(using='legacy_users')

rawDescription: Route single model to alternate database in django


rawDescription: Python invert numbers in a list of strings


code: 'hello world'.count('l')

rawDescription: how to search for all occurrences of a Tamil character in a line?

description: Search for all occurrences of a Tamil character in a line


code:
def fix_chromosome_id(chromosome):
        replacements = [('lcl\|', ''),
                        ('gi\|', ''),
                        ('chromosome', ''),
                        ('^chr', ''),
                        ('^_+', ''),
                        ('\s+', ''),
                        ('^\s', ''),
                        ('\s$', ''),
                        ('\/', '_'),
                        ('\|$', ''),
                        ('\|', '_'),
                        ('\(', '_'),
                        ('\)', '_'),
                        ('_+', '')]  # Regex and substitutions from list of tuples

        # Compile as regex objects, substitute regex as specified in the ordered dictionary
        for rep_tuple in replacements:
            regex_pattern = re.compile(rep_tuple[0], re.IGNORECASE)
            rep = rep_tuple[1]
            chromosome = regex_pattern.sub(rep, chromosome)

rawDescription: Whitespace and ^ Not Working - Multiple RegEx Substitution in Python 3


rawDescription: Calculating Autocorrelation of Pandas DataFrame along each Column


code:
def encode(key, string):
    encoded_chars = []
    for i in xrange(string):
        key_c = key[i % len(key)]
        encoded_c = chr(ord(string[i]) + ord(key_c) % 256)
        encoded_chars.append(encoded_c)
    encoded_string = "".join(encoded_chars)
    return base64.urlsafe_b64encode(encoded_string)

rawDescription: Simple way to encode a string according to a password?


code:
def PrintGame():
    htmlquestions = []
    for question in QASKEDN:
        htmlquestions.append("""

                              {} :
                              <input type="text" name="A1"/>

""".format(question))
    html = ("""

Quiz

                <form method="post" action="quiz.cgi">

"""
    + "".join(htmlquestions) +
    """

                </form>

    """)

rawDescription: Linking python variables to a HTML script


code: items = [dicttolatex(dic) for dic in items_to_clean]

rawDescription: Is there a more pythonic way to populate a this list?


rawDescription: Python: Pass 1 argument to a 2d array of functions and get results in 2d array of same size


code:
import unittest

class TestMyObj(unittest.TestCase):

    def test_myobj_with_statement__should_create_delete_temp_directory(self):
        with MyObj() as obj:
            # Directory is created
            self.assertTrue(os.path.isdir(obj.tmpdir))
        # Directory is gone
        self.assertFalse(os.path.isdir(obj.tmpdir))

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

rawDescription: How to unit test class with __enter__ / __exit__ methods?

description: Unit test class with __enter__ / __exit__ methods


code:
x = np.linspace(0.2,10,100)
fig, ax = plt.subplots()
ax.plot(x, 1/x)
ax.plot(x, np.log(x))
ax.set_aspect('equal')
ax.grid(True, which='both')

y = 1.25
xmin = 2
xmax = ax.get_xlim()[1]
ax.plot([xmin, xmax], [y, y], color='k')

rawDescription: Matplotlib conversion from data units to axis units


code:
import numpy as np
import pylab as pl
from matplotlib.colors import hsv_to_rgb

V, H = np.mgrid[0:1:100j, 0:1:300j]
S = np.ones_like(V)
HSV = np.dstack((H,S,V))
RGB = hsv_to_rgb(HSV)
pl.imshow(RGB, origin="lower", extent=[0, 360, 0, 1], aspect=150)
pl.xlabel("H")
pl.ylabel("V")
pl.title("$S_{HSV}=1$")
pl.show()

rawDescription: 2d hsv color space in matplotlib

description: 2d hsv color space in matplotlib


code:
def f():
    return 1, 2, 3

a, b, c = f()  # if you want to use all
a, _, _ = f()  # use only first element in the returned tuple, 'a'
_, b, _ = f()  # use only 'b'

rawDescription: Do Python functions know how many outputs are requested?


rawDescription: Regex find all integers that don't start with '$'


code: sorted(A, key=A.get)

rawDescription: sort values and return list of keys from dict python

description: Sort values and return list of keys from dict python


code:
import itertools
list(itertools.product('ab', repeat=3))

rawDescription: Finding permuations and combinations using Python


rawDescription: Print a list with the original style

description: Print a list with the original style


code:
import psycopg2
conn = psycopg2.connect("dbname=test user=postgres")
cur = conn.cursor()
cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)",
     (100, "abc'def"))
cur.execute("SELECT * FROM test;")
cur.fetchone()
conn.commit()
cur.close()
conn.close()

rawDescription: Python and PostgreSQL - GeomFromText

description: Python and PostgreSQL - GeomFromText


code:
words = set(message.split(" "))
result = [int(word in words) for word in terms]

rawDescription: Create Array of found words in Python

description: Create Array of found words in Python


code:
def rotate(*args):
    print 'rotate button press...'
    theta = 90
    rotated = ndimage.rotate(image, theta)
    im.set_data(rotated)
    canvas.draw()

rawDescription: updating matplotlib imshow from within a Tkinter gui

description: Updating matplotlib imshow from within a Tkinter gui


code:
class MyClass:
    def display_info(self, caller):
        print caller

def my_function(my_obj):
    my_obj.display_info(caller='my_function')

rawDescription: How to get a function's name from a object passed as that function's parameter in Python?

description: Get a function's name from a object passed as that function's parameter in Python


rawDescription: Multiple assignment using map function

description: Multiple assignment using map function


code:
from subprocess import Popen, PIPE
from sys import argv

input = ' '.join(argv[1:])
if not input: input = "no arguments given"
cproc = Popen("./c_prog", stdin=PIPE, stdout=PIPE)
out, err = cproc.communicate(input)
print "output:", out
print "errors:", err

rawDescription: Pass many pieces of data from Python to C program


code:
from lxml import etree

# create XML
root = etree.Element('root')
root.append(etree.Element('child'))
# another child with text
child = etree.Element('child')
child.text = 'some text'
root.append(child)

# pretty string
s = etree.tostring(root, pretty_print=True)
print s

rawDescription: Best way to generate xml in python?

description: Generate xml in python


code:
for r in range(1,10):
    for c in range(1, r + 1):
        print c,
    print ""

rawDescription: range() function is giving me trouble

description: Range() function is giving me trouble


code:
for name in ['A', 'B', 'C']:
    class_ = type(name, (object, ), {})
    exec "%s = class_" % name

print A, B, C

rawDescription: Populating values in module namespace

description: Populating values in module namespace


code:
words = 'blue yellow'

if 'blue' in words:
    print 'yes'
else:
    print 'no'

rawDescription: Determining if a string contains a word

description: Determining if a string contains a word


code:
def send_notification_task(self, user, content):
    a = datetime.now()

    latch = CountDownLatch(1)
    print 'Sending message to pubnub'

    status = {'result' : 0}

    print "Here is the content:"
    print content

    if 'image_url' in content:
        image_url = content['image_url']
    else:
        image_url = None

    if 'category' in content:
        category = content['category']
    else:
        category = 'pnr'

    msg = prepare_message(user, content['title'], content['message'], image_url, category)

    if msg==None:
        print 'Not sending None message (probably iOS device)'
        return

    def _callback(message):
        print 'success callback'
        print(message)
        status['result'] = 1
        latch.count_down()

    def _error(message):
        print 'error callback'
        countdown = exponential_backoff(self.request.retries)
        status['result'] = 2
        print str(status) + ' error'
        latch.count_down()

    print msg
    print user['user_channel_id']

    publish(user['user_channel_id'], msg, _callback, _error)

    latch.await()

    # while status['result'] == 0:
    #     print 'loop'
    #     print status
    #     time.sleep(.001)

    if status['result'] == 2:
        print 'retry'
        countdown = exponential_backoff(self.request.retries)
        raise self.retry(countdown = countdown)
    elif status['result'] == 1:
        print 'success'
        b = datetime.now()

        c = b - a

        print 'send_notification_task {0}'.format(c)
        return 'success'

rawDescription: How to prevent task marking as success on return [celery]

description: Prevent task marking as success on return [celery]


code:
L = """hello
- x1
- x2
- x3
- x4
- x6
morning
- x7
world""".splitlines()
print getblocks(L)

rawDescription: multi-line pattern matching in python

description: Multi-line pattern matching in python


code:
import grequests

urls = [
    'http://www.heroku.com',
    'http://python-tablib.org',
    'http://httpbin.org',
    'http://python-requests.org',
    'http://kennethreitz.com'
]

rs = (grequests.get(u) for u in urls)

for i in grequests.imap(rs):
    print i, i.url

rawDescription: For-loop calling urllib.urlopen().getcode() is slow


code:
nested = [L[i : i+size] for i in xrange(0, len(L), size)]
for sub in nested: print sub

rawDescription: divide the list into rows (create a nested list from a list)

description: Divide the list into rows (create a nested list from a list)


code:
var = "1,2,3,4,5,6,7,8,9"

spl = var.split(",") # split into list of individual items

print "\n".join(["".join(spl[i:i+3]) for i in range(0,len(spl),3)]) # join into chunks of 3 and add a newline to separate the output
123
456
789

rawDescription: Replace every third comma with line break


code:
from yapsy.IPlugin import IPlugin

class PluginOne(IPlugin):
    def print_name(self):
        print "This is plugin 1"

rawDescription: Yapsy minimal example

description: Yapsy minimal example


code:
A = [[1, -2], [-2, 1]]
for lst in A:
    for i, val in enumerate(lst):
        lst[i] = max(val, 0)
print A

rawDescription: Replacing Negatives in an Array, Python

description: Replacing Negatives in an Array, Python


code:
import numpy as np
import pandas as pd

tr_df = pd.read_csv('test.csv', usecols=['X', 'Y'])

for i in range(0,7):
    df = np.round(tr_df, decimals=i)
    df = df.drop_duplicates()
    print df
    print(df.shape)

rawDescription: Looping through pandas rows

description: Looping through pandas rows


code: print reduce(lambda x,y: x+y*y,l) # as pointed by @espang reduce(lambda x,y: x+y*y,l) is only ok, when the first value is 1 (because 1*1 == 1). The first value is not squared

rawDescription: sum of squares in a list in one line?

description: Sum of squares in a list in one line


code:
import json
import requests

URL = "http://d3biamo577v4eu.cloudfront.net/api/private/v1.0/m/list/find?page=2&limit=30&type=dvd-top-rentals&services=amazon%3Bamazon_prime%3Bflixster%3Bhbo_go%3Bitunes%3Bnetflix_iw%3Bvudu&sortBy=popularity"

response = requests.get(URL)

for movie in response.json()['results']:
    movie_id = movie.get('id', None)
    title = movie.get('title', None)
    synopsis = movie.get('synopsis', None).encode('utf-8')
    actors = ', '.join(movie.get('actors', None)).encode('utf-8')
    tomato_score = movie.get('tomatoScore', None)
    popcorn_score = movie.get('popcornScore', None)
    mpaaRating = movie.get('mpaaRating', None)
    runtime = movie.get('runtime', None)

    print "Id: {}".format(movie_id)
    print "Title: {}".format(title)
    print "Synopsis: {}".format(synopsis)
    print "Actors: {}".format(actors)
    print "Rating: {}".format(mpaaRating)
    print "Runtime: {}".format(runtime)
    print "Scoring:"
    print "Tomato score: {}".format(tomato_score)
    print "PopCorn score: {}".format(popcorn_score)
    print ""

rawDescription: How to scrape this webpage with Python and lxml? empty list returned

description: Scrape this webpage with Python and lxml? empty list returned


code:
lo_values = (1, 10, 20, 30, 40, 50) # this a tuple
hi_values = (9, 19, 29, 39, 49, 60) # this too

rows = [ ('T', 'L', 'L', 'L', 'T', 'B'),
         ('B', 'B', 'B', 'T', 'T', 'T')] # rows is a list containing 2 tuples, one with each row

for row in rows:
    result = []
    previous_value = row[0]
    run_start = 0
    for i in range(1, len(row)):
        value = row[i]
        if value != previous_value:
            lower_bound = lo_values[run_start]
            upper_bound = hi_values[i-1]
            result.append((lower_bound, upper_bound, previous_value))
            run_start = i
        previous_value = value
    lower_bound = lo_values[run_start]
    upper_bound = hi_values[-1]
    result.append((lower_bound, upper_bound, row[-1]))
    print result

rawDescription: assigning range values to a data table

description: Assigning range values to a data table


code:
from itertools import product
N=2
elements = [1,2,3]
B=product(elements,repeat=N)
for c in product(B,repeat=N):
    print c

rawDescription: All possible matrices (list of list) containing only elements from raw list elements python


code:
from difflib import SequenceMatcher

sq = SequenceMatcher(None)
for x in list_of_words:
    for w in sp:
        sq.set_seqs(x,w)
        print '%-7s %-10s %f' % (x,w,sq.ratio())

rawDescription: find variations of a word in a string on python

description: Find variations of a word in a string on python


code:
import pandas as pd
import datetime
timeseries = {datetime.datetime(2013, 3, 17, 18, 19): {'t2': 400, 't1': 1000},
                 datetime.datetime(2013, 3, 17, 18, 20): {'t2': 300, 't1': 3000}
                }
print pd.DataFrame.from_dict(timeseries, orient="index")

rawDescription: How to get python dictionaries into a pandas time series dataframe where key is date object

description: Get python dictionaries into a pandas time series dataframe where key is date object


code:
soup.p.append(text[end:])
print soup

rawDescription: Python BeautifulSoup - Add Tags around found keyword


code:
refDict = {
         'user': 'USR',
         'call': 'CALL',
         'detail': 'DTL',
         'record': 'REC',
         'call detail record': 'CDR',
         'count': 'CNT'}

sorted_ref = sorted( refDict.items(), key=lambda x:len(x[0]), reverse = True )

def do_work(input_string):
    noMatchFound = {}
    rval = input_string[:]
    for key, value in sorted_ref:
        rval = rval.replace(key, value)
    not_founds = [x for x in rval.split() if x.islower()]
    for not_found in not_founds:
        noMatchFound[not_found] = input_string
        rval = rval.replace(not_found, not_found.upper())
    rval = '_'.join( rval.split() )
    return rval, noMatchFound

inputs = ["user call detail record", "user test call detail record",
          "user call count detail record","user call detail record count"]

for inp in inputs:
    print inp
    output, noMatchFound = do_work(inp)
    print output
    print noMatchFound
    print '---'

rawDescription: Data dictionary logical to physical abbreviated name conversion


rawDescription: Clarification on socket.socket()

description: Clarification on socket.socket()


code:
import itertools
lol = [["A1", "A2"], ["A2", "A3", "A4"], ["A3", "A5"]]
print [x for x in itertools.product(*lol) if len(set(x)) == len(lol)]

rawDescription: Creating resources combination

description: Creating resources combination


code:
class A:
    def meth(self):
        foo=1
        bar=2
        def func():
            foo=2     # if you place this statement below the print statement then you'll get
                      # UnboundLocalError: local variable 'foo' referenced before assignment
            print foo,bar
        func()
        print (foo) #meth's foo is unchanged
a=A()
a.meth()

rawDescription: Can a function within a class's method access all the local variables within the method?


code:
a = set(slices("aab", 2)) # {"aa", "ab"}
b = set(slices("abb", 2)) # {"ab", "bb"}
c = set(slices("abc", 2)) # {"ab", "bc"}
all = [a, b, c]
import operator
a_unique = reduce(operator.sub, (x for x in all if x is not a), a)
print a_unique # {"aa"}

rawDescription: Python, Huge Iteration Performance Problem


code:
agg = df.groupby('A').agg(lambda x: x.loc[x.B.idxmax(),:])
print df.join(agg, on='A', rsuffix = '_max')

rawDescription: pandas df getting the value of a column associated with the max of a groupby

description: Pandas df getting the value of a column associated with the max of a groupby


code:
a = [0,13,26,39]
b = [12,25,38,51]
strs = 'If you are done with the file, move to the command area across from the file name in the RL screen and type'
spl = strs.split()
for x,y in zip(a,b):
    print spl[x:y]

rawDescription: Sequence Generation with Number applied to string


code:
# Wait a maximum of 10 seconds for foo
# Usage: join([timeout in seconds])
p.join(10)

# If thread is active
if p.is_alive():
    print "foo is running... let's kill it..."

    # Terminate foo
    p.terminate()
    p.join()

rawDescription: Stop code after time period

description: Stop code after time period


code:
import xml.etree.ElementTree as etree

data = """<?xml version="1.0" encoding="UTF-8"?>
<VersionedFile EntityPath="c:\\a.zip" Name="VfOMP_CRM.zip">
   <WorkfileDescription>something</WorkfileDescription>
   <Revision EntityPath="c:\\a.zip" Name="1.1" Author="me">
      <ChangeDescription>Some comentary</ChangeDescription>
      <PGROUP Name="A" />
      <PGROUP Name="B" />
      <PGROUP Name="C" />
      <Label Name="SOFTWARE" />
      <Label Name="READY" />
   </Revision>
   <Revision EntityPath="c:\\a.zip" Name="1.0" Author="me">
      <ChangeDescription>Some comentary</ChangeDescription>
      <PGROUP Name="A" />
      <Label Name="GAME" />
      <Label Name="READY" />
   </Revision>
</VersionedFile>
"""

tree = etree.fromstring(data)
for revision in tree.findall('Revision'):
    for key, value in revision.attrib.iteritems():
        print "%s: %s" % (key, value)

    for child in revision:
        print "%s: %s" % (child.tag, child.attrib.get('Name', ''))

    print

rawDescription: Parsing XML using xml.etree ( only )

description: Parsing XML using xml.etree ( only )


code:
import numpy as np
import scipy.sparse
import timeit

def generate_data(num):
    x = np.random.random(num)
    y = np.random.random(num)
    return x, y

def crazy_histogram2d(x, y, bins=10):
    try:
        nx, ny = bins
    except TypeError:
        nx = ny = bins
    xmin, xmax = x.min(), x.max()
    ymin, ymax = y.min(), y.max()
    dx = (xmax - xmin) / (nx - 1.0)
    dy = (ymax - ymin) / (ny - 1.0)

    weights = np.ones(x.size)

    # Basically, this is just doing what np.digitize does with one less copy
    xyi = np.vstack((x,y)).T
    xyi -= [xmin, ymin]
    xyi /= [dx, dy]
    xyi = np.floor(xyi, xyi).T

    # Now, we'll exploit a sparse coo_matrix to build the 2D histogram...
    grid = scipy.sparse.coo_matrix((weights, xyi), shape=(nx, ny)).toarray()

    return grid, np.linspace(xmin, xmax, nx), np.linspace(ymin, ymax, ny)

if __name__ == '__main__':
    num=1e6
    numruns = 1
    x, y = generate_data(num)
    t1 = timeit.timeit('crazy_histogram2d(x, y, bins=500)',
            setup='from __main__ import crazy_histogram2d, x, y',
            number=numruns)
    t2 = timeit.timeit('np.histogram2d(x, y, bins=500)',
            setup='from __main__ import np, x, y',
            number=numruns)
    print 'Average of %i runs, using %.1e points' % (numruns, num)
    print 'Crazy histogram', t1 / numruns, 'sec'
    print 'numpy.histogram2d', t2 / numruns, 'sec'

rawDescription: Efficiently create 2d histograms from large datasets


code: print [m.cons for m in ELEM['sets']['material']]

rawDescription: Fast and Clean way to find to find Numpy arrays index


code:
import sys

for arg in sys.argv:
    print arg
print "Hello World!"

rawDescription: making exe file from python that uses command line arguments

description: Making exe file from python that uses command line arguments


code:
for r in [regex.match(s) for s in str]:
    if r:
        print r.groups()

rawDescription: pythonic way to rewrite an assignment in an if statement

description: Pythonic way to rewrite an assignment in an if statement


code:
queueLock = threading.Lock()

def do_stuff(q):
    print threading.current_thread()
    while True:
        queueLock.acquire()
        x=str(q.get())
        print [x[i:]for i in range(len(x))]
        queueLock.release()
        q.task_done()

rawDescription: Threading in Python and Queuing

description: Threading in Python and Queuing


code:
class Counter:
    i = 0
    foo = []
    def count(self):
        self.__class__.i += 1
        self.__class__.foo.append(['lala' + str(self.__class__.i)])

a = Counter()
a.count()
print Counter().foo
b = Counter()
b.count()
print Counter().foo

rawDescription: Scapy set class attribute question

description: Scapy set class attribute question


code:
values = sorted(set(List))
colwidth = len(str(pair_counts.most_common(1)[0][1]))
row_template = '{} ' + ' '.join(['{:>{colwidth}}'] * len(values))
print row_template.format(' ', colwidth=colwidth, *values)
for a in values:
    print row_template.format(a, colwidth=colwidth, *(
        pair_counts.get((a, b), '') for b in values))

rawDescription: Find the letters in a python list that follow each other most frequently


code:
from nose_parameterized import parameterized

def chk(number):
    print number

@parameterized.expand([[x, x] for x in range(100)])
def test(_, number):
    chk(number)

rawDescription: Filtering nosetests by numeric range

description: Filtering nosetests by numeric range


code:
class Struct(object):
    """ generic container object """
    def __init__(self, **kwds): # keyword args define attribute names and values
        self.__dict__.update(**kwds)

info2 = {}
with open('scoring_info.txt') as input:
    for line in input:
        player, stats, outcome, date = (item.strip() for item in line.split('-', 3))
        stats = dict(zip(('kills', 'deaths', 'assists'), map(int, stats.split('/'))))
        victory = (outcome.lower() == 'win') # change to T/F
        date = dict(zip(('year','month','day'), map(int, date.split('-'))))
        info2[player] = Struct(champ_name=player, stats=Struct(**stats),
                               victory=victory, date=Struct(**date))
print
print 'info2:'
for rec in info2.values():
    print '  player %r:' % rec.champ_name
    print '    stats: kills=%s, deaths=%s, assists=%s' % (
          rec.stats.kills, rec.stats.deaths, rec.stats.assists)
    print '    victorious: %s' % rec.victory
    print '    date: %d-%02d-%02d' % (rec.date.year, rec.date.month, rec.date.day)

# sample usage
print
player = 'Fizz'
print '%s had %s kills in the game' % (player, info2[player].stats.kills)

rawDescription: Extract data from lines of text file in Python


code:
for member in theList:
    print "%.2f" % member

rawDescription: Format all elements of a list

description: Format all elements of a list


code:
import lxml.etree as ET

data = """<ProceduresDB>
  <Airport ICAOcode="EHRD">
    <Sid Name="ANDI1A" Runways="06">
      <Sid_Waypoint ID="1">
        <Latitude>0.000000</Latitude>
        <Longitude>0.000000</Longitude>
      </Sid_Waypoint>
      <Sid_Waypoint ID="2">
        <Latitude>51.973697</Latitude>
        <Longitude>4.480970</Longitude>
      </Sid_Waypoint>
    </Sid>
   </Airport>
</ProceduresDB>"""

tree = ET.fromstring(data)
for waypoint in tree.findall('.//Sid[@Name="ANDI1A"]/Sid_Waypoint'):
    print "Waypoint %s" % waypoint.attrib["ID"]
    for item in waypoint:
        print item.tag, item.text

rawDescription: Python and lxml: get sub-sub-element from given element


code:
from __future__ import with_statement

class cat(object):

    def __init__(self, fname):
        self.fname = fname

    def __enter__(self):
        print "[Opening file %s]" % (self.fname,)
        self.file_obj = open(self.fname, "rt")
        return self

    def __exit__(self, *exc_info):
        print "[Closing file %s]" % (self.fname,)
        self.file_obj.close()

    def __iter__(self):
        return self

    def next(self):
        line = self.file_obj.next().strip()
        print "[Read: %s]" % (line,)
        return line

def main():
    with cat("/etc/passwd") as lines:
        for line in lines:
            if "mail" in line:
                print line.strip()
                break

if __name__ == "__main__":
    import sys
    sys.exit(main())

rawDescription: How to break from a Python generator with open file handles

description: Break from a Python generator with open file handles


code:
for col in arr.T:
    if len(set(col)) != len(col):
        print 'Duplicates in column: ', col

rawDescription: Finding duplicates in each row and column


code:
import re
subject = "ab-cd"
subject = re.sub(r"([a-z])\-([a-z])", r"\1 \2", subject , 0, re.IGNORECASE)
print subject
#ab cd

rawDescription: How to replace dash between characters with space using regex

description: Replace dash between characters with space using regex


code:
def printdiags(diags):
    for i in sorted(diags):
        print diags[i]

rawDescription: Diagonal Grid Traverse in Python

description: Diagonal Grid Traverse in Python


code:
from collections import defaultdict

class foo(object):
    allowedattrs = ['eric', 'john', 'michael']

    def __init__(self):
        self.values = defaultdict(int)

    def update(self, whattoupdate, value):
        self.values[whattoupdate] += value

f = foo()
f.update('john', 5)
f.update('john', 4)
print f.values['john']

rawDescription: Use a dict as a switch to update class attributes - how to insert them as references?


code:
if index <= 5:
    print item.title

rawDescription: How "break" in iterative Python loop works


code:
for word in SideMember:
    if word.startswith('Base'):
        print word

rawDescription: Check if a string starts with the a specific word, if True then print word - PYTHON


code:
from email.utils import formatdate
from datetime import datetime
from time import mktime

now = datetime.now()
stamp = mktime(now.timetuple())
print formatdate(
    timeval     = stamp,
    localtime   = False,
    usegmt      = True
) #--> Wed, 22 Oct 2008 10:55:46 GMT

rawDescription: RFC 1123 Date Representation in Python?

description: RFC 1123 Date Representation in Python


code:
from subprocess import *
p = Popen('ssh servername who', shell=True, stdout=PIPE)
p.wait()
print p.stdout.readlines()

rawDescription: Interface with remote computers using Python


rawDescription: Scipy.optimize.root does not converge in Python while Matlab fsolve works, why?


code:
def test(i1, i2):
    try:
        i1 = int(i1)
        i2 = int(i2)
    except ValueError:
        print "you failed in typing a number"

def input():
    i1 = raw_input('please type a number \n >')
    i2 = raw_input('please type a number \n >')
    test(i1, i2)   # here we call directly test() with entered "numbers"

rawDescription: how to get around UnboundLocalError?

description: Get around UnboundLocalError


code:
from itertools import groupby, izip_longest

matrix_string = """
afaaf
rbaca
rlaff
"""

def longest_repetition(row):
    return max((sum(1 for item in group), letter)
               for letter, group in groupby(row)
               if letter is not None)

def main():
    matrix = [[letter for letter in row.strip()]
              for row in matrix_string.strip().split('\n')]

    count, letter = max(
        max(longest_repetition(row) for row in matrix),
        max(longest_repetition(col) for col in izip_longest(*matrix))
    )

    print letter, count

if __name__ == '__main__':
    main()

rawDescription: Trying to figure out longest path algorithm python


code:
text = u'Try \u201cquotated text should be here\u201d try'
'\\' in text
print text

rawDescription: Python: Replacing certain Unicode entities with entities from dictionary


code: print i

rawDescription: Does ipython notebook 'run all cells' execute simultaneously or in sequence?


code:
def moo():
    os.rename(DocIn, DocOut) #the last DocIn and DocOut from your csv file
    file(DocIn, "wb").write(urllib2.urlopen(WebAddy).read())
    hosts0 = open(DocIn,"r")
    hosts1 = open(DocOut,"r")
    lines1 = hosts0.readlines()
    for i,lines2 in enumerate(hosts1):
        if lines2 != lines1[i]:
            print "line ", i, " in hosts1 is different \n"
            print lines2

rawDescription: Python, need to input csv file into a loop


code:
def read_csv_file():
    import csv, os
    this_dir = os.path.abspath(os.path.dirname(__file__))
    csv_file = this_dir+"/sample_data.csv"
    data1_list=[]
    data2_list=[]
    with open(csv_file, 'rb') as csvfile:
        csv_reader = csv.DictReader(csvfile, dialect= 'excel')
        print "1st data row:"
        for csv_row in csv_reader:
            data1_list.append(csv_row["data1"])
            print csv_row["data1"]
            data2_list.append(csv_row["data2"])
    return data1_list, data2_list

def calculate_min(data1):
    from operator import itemgetter
    minimum_data1=min(enumerate(data1), key=itemgetter(1))[1]
    max_data1=max(enumerate(data1), key=itemgetter(1))[1]
    print "the min of the set was: %s"%minimum_data1
    print "the max of the set was: %s"%max_data1

rawDescription: Creating table and calculation of min from txt file


code:
import fileinput
f=0
for line in fileinput.input("file",inplace=0):
    if "hostname" in line and "ser1-xyz" in line:
       line=line.replace("ser1-xyz","ser1")
       f=1
    if f and "group" in line:
       a=line.rstrip().split(" ")
       a[-1]=str(int(a[-1])+1)
       line=' '.join(a)
       f=0
    print line.rstrip()

rawDescription: file manipulation and find a word and tricky replace

description: File manipulation and find a word and tricky replace


code:
from functools import wrap
import time

def timer(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start  = time.time()
        ret = func(*args, **kwargs)
        end = time.time()
        print 'Function took {} seconds'.format(end-start)
        return ret
    return wrapper

class TestUploadHandler(tornado.web.RequestHandler):
   @timer
   def post(self):
        logging.debug("Testing upload")
        self.write({})

rawDescription: How can I benchmark a tornado handler within the handler?

description: Benchmark a tornado handler within the handler


code:
for dVals in yourData:
    print max(dVals['items'], key=lambda x:x['age'])

rawDescription: Find maximum value in a list of dictionaries


code:
import SimpleHTTPServer
import SocketServer

PORT = 8000
handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), handler)

print "serving at port", PORT
httpd.serve_forever()

rawDescription: Creating a WebSocket Client in Python

description: Creating a WebSocket Client in Python


code:
import subprocess

stream = subprocess.Popen(stdout=subprocess.PIPE).stdout

for line in stream:
    #parse lines as you recieve them.
    print line

rawDescription: Parsing the output of a subprocess while executing and clearing the memory (Python 2.7)


code:
import re
def syllables(word):
    word = word.lower()
    if word.endswith('e'):
        word = word[:-1]
    count = len(re.findall('[aeiou]+', word))
    return count

for word in ('honour', 'decode', 'decodes', 'oiseau', 'pie'):
    print word, syllables(word)

rawDescription: Sequence of vowels count

description: Sequence of vowels count


code:
from lxml import html
import requests

page = requests.get('http://www.amazon.in/gp/product/B00EYCBFDQ/ref=s9_pop_gw_g147_i3?pf_rd_m=A1VBAL9TL5WCBF&pf_rd_s=center-3&pf_rd_r=1YP3T548XBFHJ1RA3EH8&pf_rd_t=101&pf_rd_p=402518447&pf_rd_i=1320006031')
tree = html.fromstring(page.text)
price = tree.xpath("//*[@id='actualPriceValue']/b/span/text()")[0]

print price

rawDescription: Xpath to get prices on amazon

description: Xpath to get prices on amazon


code:
def wiggle(start, end, moves):
    if start == end and moves == 0:
        return 1
    elif start != end and moves == 0:
        return 0
    else:
        return wiggle(start-1, end, moves-1) + wiggle(start+1, end, moves-1)

print wiggle(1,3,4)

rawDescription: recursion in python for delivering something by only going back and forth

description: Recursion in python for delivering something by only going back and forth


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup('

')
print soup.find('div').name

rawDescription: Check element type in BeautifulSoup 3

description: Check element type in BeautifulSoup 3


code:
code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))
cheat = 'cheat'
guess = raw_input("[keypad]> ")
guesses = 0

while guess not in [cheat, code] and guesses < 9:
    print "WRONG!"
    guesses += 1
    guess = raw_input("Guess 3 numbers> ")

if guess in [cheat, code]:
    print "Correct!"
    return my_func()
else:
    print "Game over"
    return exit()

rawDescription: How can I add a "cheat code" to this guess a number code I have?

description: Add a "cheat code" to this guess a number code I have


code:
def decompose(number):
    return number // m, number % m

m = 2
n1, r = decompose(5)
print "5 = %s * %s + %s" % (m, n1, r)
m = 4
n1, r = decompose(5)
print "5 = %s * %s + %s" % (m, n1, r)

rawDescription: Decomposition function

description: Decomposition function


code:
import win32api, win32con, win32gui, win32ui

def main():
    hInstance = win32api.GetModuleHandle()
    className = 'MyWindowClassName'

    # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633576(v=vs.85).aspx
    # win32gui does not support WNDCLASSEX.
    wndClass                = win32gui.WNDCLASS()
    # http://msdn.microsoft.com/en-us/library/windows/desktop/ff729176(v=vs.85).aspx
    wndClass.style          = win32con.CS_HREDRAW | win32con.CS_VREDRAW
    wndClass.lpfnWndProc    = wndProc
    wndClass.hInstance      = hInstance
    wndClass.hCursor        = win32gui.LoadCursor(None, win32con.IDC_ARROW)
    wndClass.hbrBackground  = win32gui.GetStockObject(win32con.WHITE_BRUSH)
    wndClass.lpszClassName  = className
    # win32gui does not support RegisterClassEx
    wndClassAtom = win32gui.RegisterClass(wndClass)

    # http://msdn.microsoft.com/en-us/library/windows/desktop/ff700543(v=vs.85).aspx
    # Consider using: WS_EX_COMPOSITED, WS_EX_LAYERED, WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW, WS_EX_TOPMOST, WS_EX_TRANSPARENT
    # The WS_EX_TRANSPARENT flag makes events (like mouse clicks) fall through the window.
    exStyle = win32con.WS_EX_COMPOSITED | win32con.WS_EX_LAYERED | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOPMOST | win32con.WS_EX_TRANSPARENT

    # http://msdn.microsoft.com/en-us/library/windows/desktop/ms632600(v=vs.85).aspx
    # Consider using: WS_DISABLED, WS_POPUP, WS_VISIBLE
    style = win32con.WS_DISABLED | win32con.WS_POPUP | win32con.WS_VISIBLE

    # http://msdn.microsoft.com/en-us/library/windows/desktop/ms632680(v=vs.85).aspx
    hWindow = win32gui.CreateWindowEx(
        exStyle,
        wndClassAtom,
        None, # WindowName
        style,
        0, # x
        0, # y
        win32api.GetSystemMetrics(win32con.SM_CXSCREEN), # width
        win32api.GetSystemMetrics(win32con.SM_CYSCREEN), # height
        None, # hWndParent
        None, # hMenu
        hInstance,
        None # lpParam
    )

    # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633540(v=vs.85).aspx
    win32gui.SetLayeredWindowAttributes(hWindow, 0x00ffffff, 255, win32con.LWA_COLORKEY | win32con.LWA_ALPHA)

    # http://msdn.microsoft.com/en-us/library/windows/desktop/dd145167(v=vs.85).aspx
    #win32gui.UpdateWindow(hWindow)

    # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633545(v=vs.85).aspx
    win32gui.SetWindowPos(hWindow, win32con.HWND_TOPMOST, 0, 0, 0, 0,
        win32con.SWP_NOACTIVATE | win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW)

    # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx
    #win32gui.ShowWindow(hWindow, win32con.SW_SHOW)

    win32gui.PumpMessages()

def wndProc(hWnd, message, wParam, lParam):
    if message == win32con.WM_PAINT:
        hdc, paintStruct = win32gui.BeginPaint(hWnd)

        dpiScale = win32ui.GetDeviceCaps(hdc, win32con.LOGPIXELSX) / 60.0
        fontSize = 80

        # http://msdn.microsoft.com/en-us/library/windows/desktop/dd145037(v=vs.85).aspx
        lf = win32gui.LOGFONT()
        lf.lfFaceName = "Times New Roman"
        lf.lfHeight = int(round(dpiScale * fontSize))
        #lf.lfWeight = 150
        # Use nonantialiased to remove the white edges around the text.
        # lf.lfQuality = win32con.NONANTIALIASED_QUALITY
        hf = win32gui.CreateFontIndirect(lf)
        win32gui.SelectObject(hdc, hf)

        rect = win32gui.GetClientRect(hWnd)
        # http://msdn.microsoft.com/en-us/library/windows/desktop/dd162498(v=vs.85).aspx
        win32gui.DrawText(
            hdc,
            'Text on the screen',
            -1,
            rect,
            win32con.DT_CENTER | win32con.DT_NOCLIP | win32con.DT_SINGLELINE | win32con.DT_VCENTER
        )
        win32gui.EndPaint(hWnd, paintStruct)
        return 0

    elif message == win32con.WM_DESTROY:
        print 'Closing the window.'
        win32gui.PostQuitMessage(0)
        return 0

    else:
        return win32gui.DefWindowProc(hWnd, message, wParam, lParam)

if __name__ == '__main__':
    main()

rawDescription: How to display text on the screen without a window using Python

description: Display text on the screen without a window using Python


code:
import re
if re.match("^\d+?\.\d+?$", element) is None:
    print "Not float"

rawDescription: Checking if a string can be converted to float in Python


code:
data = JSONBackedMapping('data.json')
data
data['foo'] = 'bar'
data['spam'] = ['eggs', 'ham']
print open('data.json').read()
del data
data = JSONBackedMapping('data.json')
data

rawDescription: Save a dictionary when it changes

description: Save a dictionary when it changes


code: for linenum, line in enumerate(source,1):  print linenum, line

rawDescription: How can I limit iterations of a loop in Python?

description: Limit iterations of a loop in Python


code:
from nose.plugins.base import Plugin

env = None

class EnvironmentSelector(Plugin):
    """Selects if test will be run against production or sandbox environments.
    """

    def __init__(self):
        Plugin.__init__(self)
        self.environment = "spam"  ## runs against sandbox by default

    def options(self, parser, env):
        """Register command line options"""
        parser.add_option("--set-env",
                          action="store",
                          dest="setEnv",
                          metavar="ENVIRON",
                          help="Run tests against production or sandbox"
                          "If no --set-env specified, runs against sandbox by default")

    def configure(self, options, config):
        """Configure the system, based on selected options."""

        #set variable to that which was passed in cmd
        self.environment = options.setEnv
        self.enabled = self.environment

        if self.enabled:
            global env
            print "This is env before: " + str(env)
            env = self.passEnv()
            print "This is env after: " + str(env)
            return env

    def passEnv(self):
        run_production = False

        if self.environment.lower() == "sandbox":
            print ("Environment set to sandbox")
            return run_production

        elif self.environment.lower() == "prod":
            print ("Environmnet set to prod")
            run_production = True
            return run_production

        else:
            print ("NO environment was set, running sandbox by default")
            return run_production

rawDescription: Using nose plugin to pass a boolean to my package


code:
from mpi4py import MPI
from time import sleep
import argparse

def parseOptions(comm):
    parser = argparse.ArgumentParser(
        description='Print some messages.')

    parser.add_argument('iteration_count', help='How many times', type=int)
    parser.add_argument('message',
                        help='What to say',
                        nargs=argparse.OPTIONAL,
                        default='Hello, World!')

    args = None
    try:
        if comm.Get_rank() == 0:
            args = parser.parse_args()
    finally:
        args = comm.bcast(args, root=0)

    if args is None:
        exit(0)
    return args

def main():
    comm = MPI.COMM_WORLD  # @UndefinedVariable
    rank = comm.Get_rank()
    size = comm.Get_size()

    args = parseOptions(comm)

    if rank == 0:
        print args.message

    for i in range(args.iteration_count):
        if i%size == rank:
            print '{} in rank {} started.'.format(i, rank)
            sleep(.5)
            print '...'
            sleep(.5)
            print '{} in rank {} ended.'.format(i, rank)

if __name__ == '__main__':
    main()

rawDescription: Parsing arguments using argparse and mpi4py


code:
a = A('a')
b = A('b')
c = A()
print a
print b
print c

rawDescription: Python - Exclusive attribute value for object?


code:
def printInFormat (list):
    value = list[0]
    length = len(list)
    for index in range(1, length):
        value = value * list[index]
    string = str(value) + "="
    for index in range(length-1):
        string = string + str(list[index]) + "*"
    string = string + str(list[length-1])
    return string

FactorList=[2,5,5]

print printInFormat(FactorList)

rawDescription: How can I convert a list into the following format?

description: Convert a list into the following format


code:
d = dict((list_of_categories.index(row['id']), row['name']) for row in rows)
print d

rawDescription: How do you create a list like PHP's in Python?

description: Create a list like PHP's in Python


code:
def foo_function(n):
    x = 1
    y = 2
    z = 'string'
    debugFunction(locals(), ['x', 'y', 'z'])
    return x + y

def _debugFunction(localdict, var_list):
    global debug_flag
    if debug_flag:
        print 'START DEBUGGING:'
        for name in var_list:
            print('{}: {}'.format(name, localdict[name]))
        print 'END DEBUGGING'

rawDescription: Printing 'var' = var

description: Printing 'var' = var


code:
import xml.etree.cElementTree as ET
tree = ET.ElementTree(file='Test.xml')
for elem in tree.findall('.//Pre_EQ/success'):
    print elem.tag, elem.attrib, elem.text

rawDescription: What is python 2.6 equivalent of iterfind()


code:
def account_map(request, slug):
   user_id = slug
   sam = Sam.objects.get(user_id=user_id)
   sam_check = SamAccount.objects.filter(sam=sam)
   message_account_map = "Please enter the Account Nagp you want to Map to your Profile"
   message_account_unmap = "Account is succesfully UnMapped"
   message_account_map_success = 1
   message_account_unmap_success = 1
   if 'save_changes' in request.POST:
       print "yes save_changes is there"
   if 'un_map' in request.POST:
       print "Yes Unmap is there"
   if request.method == 'POST':
       form = AccountMapForm(request.POST)
       if form.is_valid():
          user_id = slug
          account_from_form = form.cleaned_data['account_nagp']
          try:
            account = Account.objects.get(accnt_nagp=account_from_form)
            try:
                if 'save_changes' in request.POST:
                    account = SamAccount.objects.get(sam=sam, account=account_from_form)
                    message_account_map = "Account is already Mapped"
                    message_account_map_success = 0 # To get the color change for the Warning
                else:
                    SamAccount.objects.get(sam=sam, account=account_from_form).delete()
                    message_account_unmap_success = 0
            except ObjectDoesNotExist:
                if 'save_changes' in request.POST:
                    SamAccount.objects.create(sam=sam, account=account)
                    message_account_map = "Account is succesfully Mapped to your profile"
                else:
                    message_account_unmap = "The requested Account was not mapped to your profile"
                    message_account_unmap_success = 1
          except ObjectDoesNotExist:
            account = Account.objects.create(accnt_nagp=account_from_form)
            SamAccount.objects.create(sam=sam, account=account)
            message_account_map = "Created the Account and succesfully Mapped to your profile"
   else:
      form = AccountMapForm()
      template = loader.get_template('book/account_map.html')
      context = RequestContext(request, {
            'form' : form,
            'message_account_map' : message_account_map,
            'message_account_unmap' : message_account_unmap,
            'message_account_map_success' : message_account_map_success,
            'message_account_unmap_success' : message_account_unmap_success,
    })
   return HttpResponse(template.render(context))

rawDescription: How to edit/update a model via Django form with manytomany field?

description: Edit/update a model via Django form with manytomany field


code:
import multiprocessing
import time

def calculate1(result_queue):
    print "calculate1 started"
    time.sleep(10)
    result = 3.14
    result_queue.put(result)
    print "calculate1 found the result!"

def calculate2(result_queue):
    print "calculate2 started"
    time.sleep(2)
    result = 3.14
    result_queue.put(result)
    print "calculate2 found the result!"

result_queue = multiprocessing.Queue()

process1 = multiprocessing.Process(target=calculate1, args=[result_queue])
process2 = multiprocessing.Process(target=calculate2, args=[result_queue])

process1.start()
process2.start()

print "Calculating the result with 2 threads."

result = result_queue.get() # waits until any of the proccess have `.put()` a result

for process in [process1, process2]: # then kill them all off
    process.terminate()

print "Got result:", result

rawDescription: Run multiple threads until one exits in python


code:
import cookielib
import os
import urllib
import urllib2
import re
import string
from BeautifulSoup import BeautifulSoup

username = "user@email.com"
password = "password"

cookie_filename = "parser.cookies.txt"

class LinkedInParser(object):

    def __init__(self, login, password):
        """ Start up... """
        self.login = login
        self.password = password

        # Simulate browser with cookies enabled
        self.cj = cookielib.MozillaCookieJar(cookie_filename)
        if os.access(cookie_filename, os.F_OK):
            self.cj.load()
        self.opener = urllib2.build_opener(
            urllib2.HTTPRedirectHandler(),
            urllib2.HTTPHandler(debuglevel=0),
            urllib2.HTTPSHandler(debuglevel=0),
            urllib2.HTTPCookieProcessor(self.cj)
        )
        self.opener.addheaders = [
            ('User-agent', ('Mozilla/4.0 (compatible; MSIE 6.0; '
                           'Windows NT 5.2; .NET CLR 1.1.4322)'))
        ]

        # Login
        self.loginPage()

        title = self.loadTitle()
        print title

        self.cj.save()

    def loadPage(self, url, data=None):
        """
        Utility function to load HTML from URLs for us with hack to continue despite 404
        """
        # We'll print the url in case of infinite loop
        # print "Loading URL: %s" % url
        try:
            if data is not None:
                response = self.opener.open(url, data)
            else:
                response = self.opener.open(url)
            return ''.join(response.readlines())
        except:
            # If URL doesn't load for ANY reason, try again...
            # Quick and dirty solution for 404 returns because of network problems
            # However, this could infinite loop if there's an actual problem
            return self.loadPage(url, data)

    def loginPage(self):
        """
        Handle login. This should populate our cookie jar.
        """
        html = self.loadPage("https://www.linkedin.com/")
        soup = BeautifulSoup(html)
        csrf = soup.find(id="loginCsrfParam-login")['value']

        login_data = urllib.urlencode({
            'session_key': self.login,
            'session_password': self.password,
            'loginCsrfParam': csrf,
        })

        html = self.loadPage("https://www.linkedin.com/uas/login-submit", login_data)
        return

    def loadTitle(self):
        html = self.loadPage("http://www.linkedin.com/nhome")
        soup = BeautifulSoup(html)
        return soup.find("title")

parser = LinkedInParser(username, password)

rawDescription: Logging in to LinkedIn with python requests sessions


code:
ID - NAME - LASTNAME - USER - PASSWORD
0001 - Oliver - Jackson - newlog01 - newpass01
0002 - Samuel - Depp - samuel - pass321

rawDescription: Creating a .txt file from a dictionary

description: Creating a .txt file from a dictionary


code:
#! /usr/bin/python2.7

class Sensor:

    def __init__(self, loc, size=1, enabled=True):
        self.enabled = enabled
        self.distOfBorderFrmCentre = size / 2.0
        self.location = loc

    def position(self):
        return self.location

sensors = [ Sensor(loc) for loc in ('Loc 1', 'Loc 2', 'Loc 3') ]
for i in range(len(sensors)):
    print sensors[i].position()

#Previous two lines are a bit senseless, you can use:
for sensor in sensors:
    print sensor.position()

rawDescription: Accessing methods from objects within a list


code: print "This is on the first line\nThis is on the second line"

rawDescription: Need help fixing/refining a while loop sum calculator in python


code:
list = [20,4,67,22,445,1,34]
k = 3
newList=[]

for i in range(0,k):
    newList.append(list[i])

for i in list:
    mini = min(newList)
    if i <= mini:
        continue
    else:
        print newList
        print "Replacing {0} with {1}".format(mini, i)
        newList.remove(mini)
        newList.append(i)
        print newList
        print '-' * 20

rawDescription: finding top k elements in array python without heapq or sorting

description: Finding top k elements in array python without heapq or sorting


code:
from random import random
bigList = [int(1000 * random()) for i in xrange(10**6)]
bigList2 = bigList[:]
bigList2.append(bigList2.pop(0))

# then test how much time will it take to come up with an answer
from datetime import datetime
startTime = datetime.now()
print isCircular(bigList, bigList2)
print datetime.now() - startTime    # please fill free to use timeit, but it will give similar results

rawDescription: How to check whether two lists are circularly identical in Python

description: Check whether two lists are circularly identical in Python


code:
import re
regex = re.compile(r'\sable#1\s*$')
count = 0
with open("pro.txt") as file:
    for line in file:
        if regex.search(line):
            count += 1
print count

rawDescription: Regular expression using python is not working


code:
inventory = {'rope': 1, 'gold coin': 42,}
loot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']

for k in loot:
    inventory[k] = inventory.get(k, 0) + 1

print inventory

rawDescription: Is there a reasonable way to add to dictionary values without importing libraries?


code:
import requests
from bs4 import BeautifulSoup

url = "http://en.wikipedia.org/wiki/List_of_Telugu_films_of_2014"
soup = BeautifulSoup(requests.get(url).content)

# iterate over tables
for table in soup.select('table.wikitable.sortable'):
    # get the table header/description, continue if not found
    h3 = table.find_previous_sibling('h3')
    if h3 is None:
        continue
    print h3.text

    # get the links
    for link in table.select('tr > td > i > a'):
        print link.text, "|", link.get('href', '')

    print "------"

rawDescription: select a specific set of cell under a set of tables using python and beautifulsoup

description: Select a specific set of cell under a set of tables using python and beautifulsoup


rawDescription: How to iterate through a numpy array and select neighboring cells

description: Iterate through a numpy array and select neighboring cells


code:
print pcre_escape(u"\uC3A9\xe9")
print pcre_escape("[foo]{bar}")

rawDescription: unicode string to PCRE regular expression in python

description: Unicode string to PCRE regular expression in python


code:
def button_pressed(which):
    print 'I am button {}'.format(b1)

rawDescription: Python: Assign commnds to buttons using a for loop when using Tkinter


code:
import math

# calculate and return the distance unit2
# needs to move to reach unit1
def get_distance(unit1, unit2):
    phi = (unit2-unit1) % 360
    sign = -1
    # used to calculate sign
    if not ((phi >= 0 and phi <= 180) or (
            phi <= -180 and phi >= -360)):
        sign = 1
    if phi > 180:
        result = 360-phi
    else:
        result = phi
    return (result*sign), sign

def get_sign(unit1, unit2):
    phi = (unit2-unit1) % 360
    sign = 1
    if ((phi >= 0 and phi <= 180) or (
            phi <= -180 and phi >= -360)):
        sign = -1
    return sign

def new_position(unit1, unit2, variance = 20):
    distance_to_move, sign = get_distance(unit1, unit2)
    variance*=sign
    # %360 to keep it within the circle
    return (unit2+distance_to_move-variance)%360

# distance unit2 needs to move to reach unit1
assert (get_distance(90,45) == (45, 1))
assert (get_distance(270, 350) == (-80, -1))
assert (get_distance(350, 30) == (-40, -1))
assert (get_distance(30, 350) == (40, 1))
assert (get_distance(50, 360*4) == (50, 1))
assert (get_distance(360*4, 50) == (-50, -1))
print "----------"
assert (new_position(90,45) == 70)
assert (new_position(270, 350) == 290)
assert (new_position(350, 30) == 10)
assert (new_position(30, 350) == 10)
assert (new_position(50, 360*4) == 30)
assert (new_position(360*4, 50) == 20)

rawDescription: Calculate difference between two angles

description: Calculate difference between two angles


code:
import os
os.environ["LD_PRELOAD"] = "5"
p = subprocess.Popen(subprocess_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=os.environ)
p.start()
for line in iter(p.stdout.readline, b''):
   print line
   time.sleep(0.6)

rawDescription: How can I execute a shell command in a child process in Python?

description: Execute a shell command in a child process in Python


code:
methods = ['__add__', '__sub__', '__mul__']

a = 3
b = 4

for m in methods:
    print getattr(a, m)(b)

rawDescription: Call each method in a list in a loop?

description: Call each method in a list in a loop


code:
coords = [(10, 10), (13, 10), (13, 13)]
print "\n".join(str(step) for step in steps(coords))

rawDescription: divide ways on a map in parts of length one

description: Divide ways on a map in parts of length one


code:
dictionary = {
    'Louie Scored': [3, 5, 2],
    'Thomas Scored': [8, 4]
}

for username, values in sorted(dictionary.items(),
                               key=lambda x: (sum(x[1]) / len(x[1])), reverse=True):
    print "username: %s, values %s" % (username, sum(values) / len(values))

rawDescription: How to find an average of a key with multiple values?

description: Find an average of a key with multiple values


code:
import re

text = """1,000 barrels
5 Megawatts hours (MWh)
80 Megawatt hours (MWh) (...)"""

r_unit = re.compile("\((\w+)\)")
r_value = re.compile("([\d,]+)")

for line in text.splitlines():
    unit = r_unit.search(line)
    if unit:
        unit = unit.groups()[0]
    else:
        unit = ""
    value = r_value.search(line)
    if value:
        value = value.groups()[0]
    else:
        value = ""
    print value, unit

rawDescription: Extracting numeric data Python

description: Extracting numeric data Python


code:
from Bio import trie
import string

def get_trie(dictfile='/usr/share/dict/american-english'):
    tr = trie.trie()
    with open(dictfile) as f:
        for line in f:
            word = line.rstrip()
            try:
                word = word.encode(encoding='ascii', errors='ignore')
                tr[word] = len(word)
                assert tr.has_key(word), "Missing %s" % word
            except UnicodeDecodeError:
                pass
    return tr

def get_trie_word(tr, s):
    for end in reversed(range(len(s))):
        word = s[:end + 1]
        if tr.has_key(word):
            return word, s[end + 1: ]
    return None, s

def main(s):
    tr = get_trie()
    while s:
        word, s = get_trie_word(tr, s)
        print word

if __name__ == '__main__':
    s = "Imageclassificationmethodscan beroughlydividedinto two broad families of approaches:"
    s = s.strip(string.punctuation)
    s = s.replace(" ", '')
    s = s.lower()
    main(s)

rawDescription: Is there an easy way generate a probable list of words from an unspaced sentence in python?


code:
class FooType(type):
    def _foo_func(cls):
        return 'foo!'

    def _bar_func(cls):
        return 'bar!'

    def __getattr__(cls, key):
        if key == 'Foo':
            return cls._foo_func()
        elif key == 'Bar':
            return cls._bar_func()
        raise AttributeError(key)

    def __str__(cls):
        return 'custom str for %s' % (cls.__name__,)

class MyClass:
    __metaclass__ = FooType

print MyClass.Foo
print MyClass.Bar
print str(MyClass)

rawDescription: __getattr__ for static/class variables in python


code:
def audit_action(action):
    def decorator_func(func):
        def wrapper_func(*args, **kwargs):
            # Invoke the wrapped function first
            retval = func(*args, **kwargs)
            # Now do something here with retval and/or action
            print 'In wrapper_func, handling action {!r} after wrapped function returned {!r}'.format(action, retval)
            return retval
        return wrapper_func
    return decorator_func

rawDescription: How can I get a Python decorator to run after the decorated function has completed?

description: Get a Python decorator to run after the decorated function has completed


code:
import re
reg4 = re.compile(r'^(?:PMID- (?P<pmid>[0-9]+)|TI  - (?P<title>.*?)^PG|AB  - (?P<abstract>.*?)^AD)', re.MULTILINE | re.DOTALL)
for i in reg4.finditer(data):
    print i.groupdict()

rawDescription: Match multiple patterns in a multiline string


code:
myString = "This is a link http://www.google.com"
print re.search("(?P<url>https?://[^\s]+)", myString).group("url")

rawDescription: How do you extract a url from a string using python?

description: Extract a url from a string using python


code:
def test_no_content_found_with_patch(self):
    print "this function will mock Contact model get_by_phone to return none"
    with mock.patch('user_directory.models.Contact.get_by_phone') as fake_func:
        fake_func.return_value = None
        resp = self.client.get(self.get_address_by_phone, {'phone_no' : 1234567891})
        self.assertTrue(resp.status_code == 204)

def test_success_with_patch(self):
    print  "this function will test the address by phone view after mocking model"
    with mock.patch('user_directory.models.Contact.get_by_phone') as fake_func:
        contact_obj = Contact(recent_address_id = 123, best_address_id = 457)
        fake_func.return_value = contact_obj
        resp = self.client.get(self.get_address_by_phone, {'phone_no' : 1234567891})
        resp_body = json.loads(resp.content)
        self.assertTrue(resp_body == {  'recent_address_id' : contact_obj.recent_address_id,
                                        'frequent_address_id' : 457
                                    }
                    )

rawDescription: Python mock patch not reseting the return value


code:
def parse_chunk(chunk):
    node_id = int(chunk[0])
    node_type = int(chunk[1])

    nb_up = int(chunk[2])
    if nb_up:
        ups = map(int, chunk[3].split())
        next_pos = 4
    else:
        ups = []
        next_pos = 3

    nb_down = int(chunk[next_pos])
    if nb_down:
        downs = map(int, chunk[next_pos+1].split())
    else:
        downs = []

    return node_id, dict(
        node_type=node_type,
        ups=ups,
        downs=downs
        )

def collect_chunks(lines):
    chunk = []
    for line in lines:
        line = line.strip()
        if line:
            chunk.append(line)
        else:
            yield chunk
            chunk = []
    if chunk:
        yield chunk

def parse(stream):
    nb_nodes = int(stream.next().strip())
    if not nb_nodes:
        return []
    stream.next()
    return dict(parse_chunk(chunk) for chunk in collect_chunks(stream))

def main(*args):
    with open(args[0], "r") as f:
        print parse(f)

if __name__ == "__main__":
    import sys
    main(*sys.argv[1:])

rawDescription: Parsing graph data file with Python

description: Parsing graph data file with Python


code:
my_objects = MyObject.objects.all()
obj_dict = {obj.name: obj for obj in my_objects}
print obj_dict['any_name']

rawDescription: Filter an evaluated QuerySet in Django

description: Filter an evaluated QuerySet in Django


code:
def my_protocol(switch_A, switch_B, gauge_1):
    print 'Pressure is %.3f mbar.' % gauge_1.value
    switch_A.close()
    switch_B.open()

rawDescription: Accessing global variables from inside a module


code:
SECRET_KEY = "abc"
print signing.dumps("value", key=SECRET_KEY)
SECRET_KEY = "123"
print signing.dumps("value", key=SECRET_KEY)

rawDescription: Change Django secret key within script

description: Change Django secret key within script


code:
soup = BeautifulSoup(html)
credits = soup.find('table')

section = ''
DATA_SET = {}

for i in credits.findAll('tr'):
    if i.get('class', '') == 'header':
        section = i.text
        DATA_SET[section] = {}
    else:
        DATA_SET[section]['data_point_1'] = i.find('td', {'class' : 'data_point_1'}).find('p').contents
        DATA_SET[section]['data_point_2'] = i.find('td', {'class' : 'data_point_2'}).find('p').contents
        DATA_SET[section]['data_point_3'] = i.find('td', {'class' : 'data_point_3'}).find('p').contents

print DATA_SET

rawDescription: Iterating through a table of rows with beautiful soup in python


code:
value = "addr = &myvariable;\naddr = &myvariable;\nfuncCall( &myvariable )"
oldVarName = "myvariable"
newVarName = "mynewvariable"
m = re.sub("(\\&){0}(;?)".format(oldVarName), r"\1"+newVarName+r"\2", value)
print m
# this will print:
# addr = &mynewvariable;
# addr = &mynewvariable;
# funcCall( &mynewvariable )

rawDescription: Regex for finding variable references in a C file using Python


code:
import re
set1 = ['word1','word2','word3']
set2 = ['new_word1','new_word2','new_word3']
translation = dict(zip(set1, set2))

text = '''
here I am word1 talking about word2.
ipsum ipsumipsum ipsum ipsum ipsum ipsum ipsum
ipsum ipsumipsum word1 ipsum ipsum ipsum ipsum
ipsum ipsumipsum ipsum ipsum word2 ipsum ipsum
ipsum ipsumipsum ipsum ipsum ipsum ipsum ipsum
'''

# compile a pattern matching any of the words
pattern = re.compile('(%s)' % '|'.join(set1))

def translate(match):
    return translation[match.group(0)]

# replace with: for line in book:
for line in text.splitlines():
    print pattern.sub(translate, line)

rawDescription: Using Two Sets of Words for a Find and Replace RE


code:
print 'a'
import sys
print sys.argv

rawDescription: Multiple -m command line arguments (Python)


code:
for key in df.groupby('B'):
    print key

rawDescription: Conditional probability density estimation in python


code:
counter = {}
for r in result:
    tup = tuple(sorted(r.iteritems()))
    counter[tup] = counter.get(tup, 0) + 1
for tup, cnt in counter.iteritems():
    print dict(tup), cnt

rawDescription: Count the duplicates in list of dict in django python


code:
l2 = [i for i in range(2,200) if i % 2 != 0]
print l2

rawDescription: How to store a list of numbers generated as output in Python?

description: Store a list of numbers generated as output in Python


code:
from dateutil.parser import _timelex, parser

a = "I like peas on 2011-04-23, and I also like them on easter and my birthday, the 29th of July, 1928"

p = parser()
info = p.info

def timetoken(token):
  try:
    float(token)
    return True
  except ValueError:
    pass
  return any(f(token) for f in (info.jump,info.weekday,info.month,info.hms,info.ampm,info.pertain,info.utczone,info.tzoffset))

def timesplit(input_string):
  batch = []
  for token in _timelex(input_string):
    if timetoken(token):
      if info.jump(token):
        continue
      batch.append(token)
    else:
      if batch:
        yield " ".join(batch)
        batch = []
  if batch:
    yield " ".join(batch)

for item in timesplit(a):
  print "Found:", item
  print "Parsed:", p.parse(item)

rawDescription: How to parse multiple dates from a block of text in Python (or another language)

description: Parse multiple dates from a block of text in Python (or another language)


code:
a = { 'name': 'Jack',
  'age': 24,
  'friends': ['Bob', 'Alice', 'Karen'],
}

b = { 'name': 'Jack',
  'favorite_color': 'blue',
  'friends': ['Daren'],
}

for key in a.keys():
    if key in b:
        if hasattr(a[key], 'extend'):
            a[key].extend(b[key])
        else:
            a[key] = b[key]

print a

rawDescription: Updating python dicts

description: Updating python dicts


code:
try:
    in_file = raw_input("Enter a file name:")
    myfile = open(in_file)
except :
    print "Couldn't open the file"
    exit()

d = dict()
for line in myfile :
    line = line.strip()
    elements = line.split(",")
    d[str(elements[0])] = [(int(elements[1]),float(elements[2]), str(elements[3]))]

for key in d :
        print key, d[key]

rawDescription: Reading a file to a dictionary

description: Reading a file to a dictionary


code:
post = request.POST
lists = zip(post.getlist('amount_paid[]'),
        post.getlist('paid_date[]'),
        post.getlist('method[]'),
        post.getlist('comments[]'),
        )
for amount_paid, paid_date, method, comments in lists:
    print amount_paid
    print paid_date
    # et cetera...

rawDescription: Create a python dictionary of lists

description: Create a python dictionary of lists


code:
import csv
from collections import defaultdict
from datetime import timedelta
sample = '''\
Johnny,In,0:02:36
Kate,Out,0:02:15
Paul,In,0:03:57
Chris,In,0:01:26
Jonathan,In,0:00:37
Kyle,In,0:06:46
Armand,Out,0:00:22
Ryan,In,0:00:51
Jonathan,Out,0:12:19
'''.splitlines()
durations = defaultdict(lambda: {'In': timedelta(), 'Out': timedelta()})
reader = csv.reader(sample)
for name, direction, duration in reader:
    hours, minutes, seconds = map(int, duration.split(':'))
    duration = timedelta(hours=hours, minutes=minutes, seconds=seconds)
    durations[name][direction] += duration

for name, directions in durations.items():
    print '{:10} In    {}'.format(name, directions['In'])
    print '           Out   {}'.format(directions['Out'])
    print '           Total {}'.format(
        directions['In'] + directions['Out'])

rawDescription: Adding time/duration from CSV file

description: Adding time/duration from CSV file


code:
col = tclass.end_time + case([(tclass.end_time<tclass.start_time, 86400000)], else_=0 )
q = session.query(col).order_by(col)
print q

rawDescription: Sqlalchemy order by calculated column

description: Sqlalchemy order by calculated column


code:
import random
import sys
import thread
import threading
import time

def wait():
  time.sleep(random.random())
  return 'W'

def targ():
  for n in range(8):
    wait()
    print 'Thr', wait(), thread.get_ident(), wait(), 'at', wait(), n

tls = threading.local()

class ThreadSafeFile(object):
  def __init__(self, f):
    self.f = f
    self.lock = threading.RLock()
    self.nesting = 0

  def _getlock(self):
    self.lock.acquire()
    self.nesting += 1

  def _droplock(self):
    nesting = self.nesting
    self.nesting = 0
    for i in range(nesting):
      self.lock.release()

  def __getattr__(self, name):
    if name == 'softspace':
      return tls.softspace
    else:
      raise AttributeError(name)

  def __setattr__(self, name, value):
    if name == 'softspace':
      tls.softspace = value
    else:
      return object.__setattr__(self, name, value)

  def write(self, data):
    self._getlock()
    self.f.write(data)
    if data == '\n':
      self._droplock()

# comment the following statement out to get guaranteed chaos;-)
sys.stdout = ThreadSafeFile(sys.stdout)

thrs = []
for i in range(8):
  thrs.append(threading.Thread(target=targ))
print 'Starting'
for t in thrs:
  t.start()
for t in thrs:
  t.join()
print 'Done'

rawDescription: How do I get a thread safe print in Python 2.6?

description: Get a thread safe print in Python 2.6


code:
def prepend_1(element, _list):
    print 'prepend_1 creates a new list, assigns it to _list and forgets the original'
    print '_list refers to the same object as _try -->', id(_list), _list
    _list = [element] + _list
    print '_list now refers to a different object -->', id(_list), _list

def prepend_2(element, _list):
    print 'prepend_2 updates the existing list'
    print '_list refers to the same object as _try -->', id(_list), _list
    _list.insert(0, element)
    print '_list still refers to the same object as _try -->', id(_list), _list

_try = [1,2,3]
print '_try is assigned -->', id(_try), _try
prepend_1(0, _try)
print '_try is the same object and is not updated -->', id(_try), _try
prepend_2(0, _try)
print '_try is the same object and is updated -->', id(_try), _try
print _try

rawDescription: Arguments are passed by reference or not in Python


code:
css_table = soup.find('table', class_='reference')
for row in css_table.find_all('tr'):
    cells = row.find_all('td')
    if cells:
        print cells[0].a.text, cells[1].a.text

rawDescription: Beautifulsoup url loading error

description: Beautifulsoup url loading error


code:
class Foo(object):
    def __getattribute__(self, attr):
        if attr != 'write':
            return super(Foo, self).__getattribute__(attr)
        meth = super(Foo, self).__getattribute__(attr)
        if meth.im_func is Foo.write.im_func:
            # subclass does not override the method
            return meth

        def assure_calls_base_class(*args, **kwargs):
            meth(*args, **kwargs)
            Foo.write(self, *args, **kwargs)
        return assure_calls_base_class

    def write(self, s=""):
        print "-From Foo"

class Bar(Foo):
    def write(self, s=""):
        print s

rawDescription: Controlling child class method from parent class


code:
def create_sub_cat(parent):
    print "creating subcat\n {} > ".format(parent)
    while True:
        conf = raw_input("type new subcat or"
                                       " press \"ENTER\" to save changes\n")
        if not conf: # if user enters empty string break
            break
        parent +=" > " + conf # else keep concatenating
    return parent

rawDescription: Function that calls itself returns only first result of its call


code:
def myfunc(num):
    print("in here %s" % num)
    time.sleep(2)
    print("done with %s" % num)
    return num+2

if __name__ == "__main__":
    pool = multiprocessing.Pool(4)
    results = [pool.apply_async(myfunc, args=(i,)) for i in range(8)]
    results = [res.get() for res in results]
    print results

rawDescription: Clarification on the `processes` argument for Python's `multiprocessing.Pool`


code:
intersects = set(c1).intersection(set(c2))
for intersect in intersects:
    print intersect

rawDescription: Python For loop repeats second loop

description: Python For loop repeats second loop


code:
class MyHTMLParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.links_found = set()

    def handle_starttag(self,tag,attrs):
        if tag=="a" and attrs:
            for a in attrs:
                if a[0]=="href" and a[1] not in self.links_found:
                    self.links_found.add(a[1])
                    print a[1]
                    with open("index_link.txt","a+") as f:
                        f.write(a[1]+"\n")

rawDescription: How to remove duplicate lines from a file and writing them in file in Python

description: Remove duplicate lines from a file and writing them in file in Python


code:
word = "שלום"
word
print word
word = word.decode('UTF-8')
word
print word
word = word.encode('UTF-8')
word
print word

rawDescription: decoding and encoding Hebrew string in Python

description: Decoding and encoding Hebrew string in Python


code:
def foo1(*args):
    param_of_foo1 = args[0]  # any other arguments will be discraded
    x = param_of_foo1 + 1
    print "x is:", x

def foo3(*uselessArgs):
    print "I have a parameter but I don't really need it..."

rawDescription: Calling random functions from a list when functions have different number of parameters


code:
import os, ctypes

off_t = ctypes.c_long  # YMMV
__NR_getdents = 78  # YMMV

class linux_dirent(ctypes.Structure):
    _fields_ = [
        ('d_ino', ctypes.c_long),
        ('d_off', off_t),
        ('d_reclen', ctypes.c_ushort),
        ('d_name', ctypes.c_char)
    ]

_getdents = ctypes.CDLL(None).syscall
_getdents.restype = ctypes.c_int
_getdents.argtypes = ctypes.c_long, ctypes.c_uint, ctypes.POINTER(ctypes.c_char), ctypes.c_uint

fd = os.open('/tmp/', os.O_RDONLY | os.O_DIRECTORY)

buf = ctypes.ARRAY(ctypes.c_char, 1024)()
while True:
    nread = _getdents(__NR_getdents, fd, buf, len(buf))
    if nread == -1:
        raise OSError('getdents')
    elif nread == 0:
        break

    pos = 0
    while pos < nread:
        d = linux_dirent.from_buffer(buf, pos)

        name = buf[pos + linux_dirent.d_name.offset : pos + d.d_reclen]
        name = name[:name.index('\0')]
        print 'name:', name

        pos += d.d_reclen

rawDescription: Make syscall in Python

description: Make syscall in Python


code:
myList = [25,3,26,27,56]
idx = myList.index(27)
if idx == len(myList) - 1: # If 27 is the last element, swap with the previous
    myList[idx], myList[idx - 1] = myList[idx - 1], myList[idx]
else:
    myList[idx], myList[idx + 1] = myList[idx + 1], myList[idx]
print myList

rawDescription: How to swap one value with next value in Python?

description: Swap one value with next value in Python


code:
class Knights:
   def __init__(self, name='Bedevere'):
       self.name = name
   def knight(self):
       self.name = 'Sir ' + self.name
   def speak(self):
       print self.name + ":", "Run away!"

class FerociousRabbit:
   def __init__(self):
       self.death = "awaits you with sharp pointy teeth!"
   def speak(self):
       print "Squeeeeeeee!"

def cave(thing):
   thing.speak()
   if isinstance(thing, Knights):
       thing.knight()

def scene():
   k = Knights()
   k2 = Knights('Launcelot')
   b = FerociousRabbit()
   for i in (b, k, k2):
      cave(i)

rawDescription: Should I use a class in this: Reading a XML file using lxml


code:
def norecurse(f):
    def func(*args, **kwargs):
        if len([l[2] for l in traceback.extract_stack() if l[2] == f.func_name]) > 0:
            raise Exception, 'Recursed'
        return f(*args, **kwargs)
    return func
@norecurse
def foo(depth=0):
    print depth
    foo(depth + 1)
foo()

rawDescription: Can a Python method check if it has been called from within itself?


code:
fruits = fruitcount.keys()
fruits.sort()
print fruits
fruits.sort(key=lambda x:x.color)
print fruits

rawDescription: Python: Tuples/dictionaries as keys, select, sort


code: print "foo"

rawDescription: Adding a datetime stamp to Python print

description: Adding a datetime stamp to Python print


code:
for (engine,) in engines:
    print engine

rawDescription: Dealing with a list of tuples - SQLite3 & Python 2.7


code:
def method_factory(i):
    def method():
        print i
    return method

for i in ['hello', 'world', 'app']:
    setattr(self, i, method_factory(i))

rawDescription: Dynamic assignment of methods in python class


code:
print 'Current Hand:',
displayHand(hand)

rawDescription: Print formatting a string and function call in python


code:
from django.conf import settings #or from my_project import settings
print settings.MEDIA_ROOT

rawDescription: how to access media_root in py file

description: Access media_root in py file


code:
import statsmodels.api as sm
mod_wls = sm.WLS(y, X, weights=weights)
res = mod_wls.fit()
print res.params

rawDescription: Understanding scipy's least square function with IRLS


code:
before = pool._pool[:]  # Make a copy of the list of Process objects in our pool
result = pool.map_async(func, iterable)  # Use map_async so we don't get stuck.
while not result.ready():  # Wait for the call to complete
    if any(proc.exitcode for proc in before):  # Abort if one of our original processes is dead.
        print "One of our processes has exited. Something probably went horribly wrong."
        break
    result.wait(timeout=1)
else:  # We'll enter this block if we don't reach `break` above.
    print result.get() # Actually fetch the result list here.

rawDescription: Retrieve exit code of processes launched with multiprocessing.Pool.map


code:
s = requests.Session()
for i in range(5):
    r = s.get("http://localhost:11282/")
    print r.text
    r = s.post("http://localhost:11282/")
    print r.text

rawDescription: Persisting Webapp2 Sessions on GAE

description: Persisting Webapp2 Sessions on GAE


code:
import time
import cPickle
import random

random.seed()
l = [ random.random()*1000 for i in xrange(100000) ]

t0 = time.time()
s1 = ','.join(map(str,l))
t1 = time.time() - t0

t0 = time.time()
s2 = cPickle.dumps(l, -1)
t2 = time.time() - t0

print 's1: len1=%s t1=%s' % (len(s1), t1)
print 's2: len2=%s t2=%s' % (len(s2), t2)
print 's2/s1: len2/len1=%s t2/t1=%s' % (float(len(s2))/len(s1), t2/t1)

rawDescription: Python - specifying lengths of strings, mostly


code:
usage
print value_to_string('')
print value_to_string(1234)
print value_to_string([1,2,3])
print value_to_string({"id": 1})
print value_to_string(datetime.datetime.now())

rawDescription: How to convert the following scala code to python

description: Convert the following scala code to python


code:
listword = ['the', 'mit']
if any(re.search(r'\b{}\b'.format(re.escape(word)), sentence, re.I) for word in listword):
    print 'yes'

rawDescription: Finding whether a word exist in a string in python


code: print '"A word that needs quotation marks"'

rawDescription: Python - Using quotation marks inside quotation marks


rawDescription: Check if a matrix is in a dictionary list


code:
def run(self):
    print threading.currentThread().getName(), self.receive_messages
    while True:
        val = self.queue.get()
        if val is None:   # If you send `None`, the thread will exit.
            return
        self.do_thing_with_message(val)

rawDescription: How do I send data to a running python thread?

description: Send data to a running python thread


code: print self.e.get()

rawDescription: Tkinter, Python: How do I save text entered in the Entry widget? How do I move a label?


rawDescription: Map string formatting to a list of numbers


code: print ' '.join([str(ord(a)) for a in data])

rawDescription: Manipulating binary data in Python

description: Manipulating binary data in Python


code:
import numpy as np

x = np.ones((6, 12000))

a = np.reshape(x, (1, -1))

b = np.concatenate([x[i,:] for i in range(x.shape[0])])

print x.shape     # (6, 12000)
print a.shape     # (1, 72000)
print b.shape     # (72000,)

rawDescription: efficient algorithmic method to concatenate rows of an array into a 1-D array

description: Efficient algorithmic method to concatenate rows of an array into a 1-D array


code:
# rpyc client
import rpyc
conn = rpyc.connect("localhost", 12345)
c = conn.root

# do stuff over rpyc
import time
print 'update =', c.get_main_update()
time.sleep(2)
print 'update =', c.get_main_update()
print 'testing returned:', c.testthings(lambda x: x)  # calling a method of the remote service
print 'update =', c.get_main_update()

rawDescription: How to interact with Running Python Script

description: Interact with Running Python Script


code:
import pandas as pd
import itertools

df = pd.read_csv("avito_trend.csv",
                      parse_dates=[2])

def f(df):
    dfs = []
    for x in [list(x) for x in itertools.combinations(df['address'].unique(), 2)]:

        c1 = df.loc[df['address'].isin([x[0]]), 'ID']
        c2 = df.loc[df['address'].isin([x[1]]), 'ID']
        c = pd.Series(list(set(c1).intersection(set(c2))))
        dfs.append(pd.DataFrame({'a':len(c), 'b':' and '.join(x)}, index=[0]))
    return pd.concat(dfs)

print df.groupby([df['used_at'].dt.year]).apply(f).reset_index(drop=True, level=1).reset_index()

rawDescription: Print Common Values for columns pandas

description: Print Common Values for columns pandas


code:
def u2s(v, width=32):
    return v if v < (1L<<(width-1)) else v - (1L<<width)

def s2u(v, width=32):
    return v if v >= 0 else v + (1L<<width)

rawDescription: Sneaking unsigned values from Jython, through Java, to C and back again


code:
# foo_level.py
print "foo"

rawDescription: Import same python module more than once


code:
keys = d1.viewkeys() & d2
print zip(map(d1.get, keys), map(d2.get, keys))

rawDescription: Python: fastest way to match keys and values between two dicts


code:
class A(object):
    def func(self):
        self.f()

class B(A):
    @staticmethod
    def f():
        print "I'm B"

rawDescription: Can @staticmethod's be inherited?

description: Can @staticmethod's be inherited


code:
print "key:%(key)s value:%(value)s" % {"key":"key1","value":"value1"}
print "{{keep me}} key:{key} value:{value}".format(**{"key":"key1","value":"value1"})

rawDescription: Use placeholder to delete rows code in mysql with python mysqldb


code:
import re

messy_string = """/* <![CDATA[ */
...
var flashvars = {
video_id: '94577',
license_code: '38f7x%48',
video_url: 'http://www.domain.com/handy/url/f1d571a.mp4/?br=1114',
postfix: '.mp4',
video_url_text: '720p'
..."""

string_parser = re.compile('video_url: \'(.*)\'')
parsed_text = string_parser.search(messy_string)
print parsed_text.group(1)

rawDescription: How to match string between '', that's followed by "video_url:" text

description: Match string between '', that's followed by "video_url:" text


code:
total_displacement = np.sum(step_matrix, axis=1).reshape(num_walkers, 1)
print total_displacement

rawDescription: General Random Walk generator questions

description: General Random Walk generator questions


code:
class A(object):
    def a(self):
            print 'a'
    def b(self):
            print 'b'

A.b = A.a
x = A()
x.b()

rawDescription: can one function be overwritten by calling another?

description: Can one function be overwritten by calling another


code:
class FooMeta(type):

    def __new__(cls, name, bases, attrs):
        foos = []
        for base in bases:
            if hasattr(base, 'foos'):
                foos.extend(base.foos)
        attrs['foos'] = foos + attrs.get('foos', [])
        return type.__new__(cls, name, bases, attrs)

class Foo(object):
    __metaclass__ = FooMeta
    foos = ['one', 'two']

class SpecialFoo(Foo):
    foos = ['three']

print Foo.foos
# ['one', 'two']

print SpecialFoo.foos
# ['one', 'two', 'three']

rawDescription: Append to a member list when subclassing in Python


code:
r = requests.get('http://example.com')
print r.headers['content-length']

rawDescription: urllib keeps freezing while trying to pull HTML data from a website - is my code correct?

description: Urllib keeps freezing while trying to pull HTML data from a website - is my code correct


code:
import threading
import time
import random

def loop_fn(i):
    n = random.randint(1,5)
    print "process", i," started "
    time.sleep(n)
    print "process", i," finished"

threads = []
for i in range(1, 5):
    t = threading.Thread(target=loop_fn, args=(i,))
    t.start()
    threads.append(t)

for t in threads:
    t.join()
print "end"

rawDescription: Python threading, weird result

description: Python threading, weird result


code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
import time
# Make random number generation consistent between runs
np.random.seed(5)

def main():
    numlines, numpoints = 10000, 3
    lines = np.random.random((numlines, numpoints, 2))

    # Overly simplistic timing, but timeit is overkill for this exmaple
    tic = time.time()
    single_collection(lines).savefig('/tmp/test_single.png')
    toc = time.time()
    print 'Took {} sec with a single collection'.format(toc-tic)

    tic = time.time()
    multiple_collections(lines).savefig('/tmp/test_multiple.png')
    toc = time.time()
    print 'Took {} sec with multiple collections'.format(toc-tic)

def single_collection(lines):
    # Add "num" additional segments to each line
    segments, color_scalar = zip(*[interp(item, num=20) for item in lines])
    segments = np.vstack(segments)
    color_scalar = np.hstack(color_scalar)

    fig, ax = plt.subplots()
    coll = LineCollection(segments)
    coll.set_array(color_scalar)
    ax.add_collection(coll)
    return fig

def multiple_collections(lines):
    fig, ax = plt.subplots()
    for line in lines:
        # Add "num" additional segments to the line
        segments, color_scalar = interp(line, num=20)
        coll = LineCollection(segments)
        coll.set_array(color_scalar)
        ax.add_collection(coll)
    return fig

def interp(data, num=20):
    """Add "num" additional points to "data" at evenly spaced intervals and
    separate into individual segments."""
    x, y = data.T
    dist = np.hypot(np.diff(x - x.min()), np.diff(y - y.min())).cumsum()
    t = np.r_[0, dist] / dist.max()

    ti = np.linspace(0, 1, num, endpoint=True)
    xi = np.interp(ti, t, x)
    yi = np.interp(ti, t, y)

    # Insert the original vertices
    indices = np.searchsorted(ti, t)
    xi = np.insert(xi, indices, x)
    yi = np.insert(yi, indices, y)

    return reshuffle(xi, yi), ti

def reshuffle(x, y):
    """Reshape the line represented by "x" and "y" into an array of individual
    segments."""
    points = np.vstack([x, y]).T.reshape(-1,1,2)
    points = np.concatenate([points[:-1], points[1:]], axis=1)
    return points

if __name__ == '__main__':
    main()

rawDescription: Matplotlib: How to colorize a large number of line segments as independent gradients, efficiently


code:
import subprocess
import glob
import os.path

with open('output.txt', 'w') as f_output:
    # ap files sorted by modified order
    files = sorted(glob.glob(r'ap*.hdf5'), key=lambda x: os.path.getmtime(x))

    for ap in files:
        path, filename = os.path.split(ap)
        sta = os.path.join(path, 'sta{}'.format(filename[2:]))

        # Do we have an ap/sta pair?
        if os.path.exists(sta):
            # Launch the Python script with the required parameters
            p = subprocess.Popen(['python.exe', 'result.py', ap, sta], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            out, err = p.communicate()
            # Write stdout to a file and stderr to the screen
            f_output.write(out)
            print err
        else:
            print '{} is missing'.format(sta)

rawDescription: Repeatedly execute python script over files in folder arranged in order of date


code:
new_dict = dict((x.split(' ') for x in list_1))
for item in new_dict:
    if item in list_interest:
        print "yes"
    else:
        print "\t", item

rawDescription: Comparing Lists and Dictionaries correctly


code: print list(izip_longest(*my_list, fillvalue = -1))

rawDescription: izip_longest on a list of lists

description: Izip_longest on a list of lists


code:
import numpy as np
ans=[np.nonzero(l[i])[0] for i in range(1,len(l))]
print ans

rawDescription: Find out indices of an array elements

description: Find out indices of an array elements


code:
from math import pi

class Circle(object):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return pi * self.radius**2

class CirclePlus(Circle):
    def diameter(self):
        return self.radius*2

    def circumference(self):
        return self.radius*2*pi

c = Circle(10)
print c.radius
print c.area()
print repr(c)

c.__class__ = CirclePlus
print c.diameter()
print c.circumference()
print repr(c)

rawDescription: Cast base class to derived class python (or more pythonic way of extending classes)


code:
def resu(chars, depth = None, prefix=''):
    if depth is None:
            depth = len(chars)
    if depth == 0:
            print prefix
            return
    for ch in chars:
            resu(chars, depth - 1, ch + prefix)

rawDescription: simple recursive function

description: Simple recursive function


code:
array = ['hello', 'hello2', '?hello', '?hello2']
filtered = filter(str.isalpha, array)
print filtered

rawDescription: python regular expression

description: Python regular expression


code:
u'université'
len(u'université')
print u'université'

rawDescription: Python Latin Characters and Unicode

description: Python Latin Characters and Unicode


code:
import sys,imp

my_code = 'a = 5'
mymodule = imp.new_module('mymodule')
exec my_code in mymodule.__dict__

rawDescription: Load module from string in python

description: Load module from string in python


code:
print taskchain(('ps','aux'), ('grep','python'), ('cut','-c','1-50'))[0].trim()
print taskchain(('ls',), ('grep', 'ic'))[0]

rawDescription: Solving process chaining in Python

description: Solving process chaining in Python


code:
secret = os.urandom(16)
crypto = AES.new(os.urandom(32), AES.MODE_CTR, counter=lambda: secret)
encrypted = crypto.encrypt("aaaaaaaaaaaaaaaa")
print crypto.decrypt(encrypted)

rawDescription: PyCrypto problem using AES+CTR

description: PyCrypto problem using AES+CTR