code:
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt

# Generate log-normal distributed set of samples
np.random.seed(1234)
samples = np.random.lognormal(mean=1., sigma=.4, size=10000)

# Make a fit to the samples.
shape, loc, scale = scipy.stats.lognorm.fit(samples, floc=0)

# Create the histogram plot using matplotlib.  The first two values in
# the tuple returned by hist are the number of samples in each bin and
# the values of the histogram's bin edges.  counts has length num_bins,
# and edges has length num_bins + 1.
num_bins = 50
clr = '#FFE090'
counts, edges, patches = plt.hist(samples, bins=num_bins, color=clr, label='Sample histogram')

# Create an array of length num_bins containing the center of each bin.
centers = 0.5*(edges[:-1] + edges[1:])

# Compute the CDF at the edges. Then prob, the array of differences,
# is the probability of a sample being in the corresponding bin.
cdf = scipy.stats.lognorm.cdf(edges, shape, loc=loc, scale=scale)
prob = np.diff(cdf)

plt.plot(centers, samples.size*prob, 'k-', linewidth=2, label='Expected histogram')

# prob can also be approximated using the PDF at the centers multiplied
# by the width of the bin:
# p = scipy.stats.lognorm.pdf(centers, shape, loc=loc, scale=scale)
# prob = p*(edges[1] - edges[0])
# plt.plot(centers, samples.size*prob, 'r')

plt.legend()

plt.show()

rawDescription: Scaling the fitted PDF of a log-normal distribution to the histrogram in python


rawDescription: Basic Slicing Review

description: Basic Slicing Review


code: car_count = len(indexes)

rawDescription: How to print the index values of two identical items in a list?

description: Print the index values of two identical items in a list


rawDescription: How to remove an item in a list that has a specific ordering of words

description: Remove an item in a list that has a specific ordering of words


code:
class Foo(object):
    locals().update(a=1, b=2, c=3)

rawDescription: python dynamically set non-instance class attributes

description: Python dynamically set non-instance class attributes


code:
class MyForm(Form):
    some_field = fields.TextField(u'Some Field', [validators.required()])
    some_option = fields.BooleanField(u'Some Option')
    people = ListField()

    def validate_people(self, field):
        if len(field.data) == 0:
            raise ValidationError('Must select at least one person')

rawDescription: Validating an unknown set of items with WTForms


code:
def target(loop, timeout=None):
    future = asyncio.run_coroutine_threadsafe(add(1, b=2), loop)
    return future.result(timeout)

async def add(a, b):
    await asyncio.sleep(1)
    return a + b

loop = asyncio.get_event_loop()
future = loop.run_in_executor(None, target, loop)
assert loop.run_until_complete(future) == 3

rawDescription: Submit a job to an asyncio event loop

description: Submit a job to an asyncio event loop


code:
demo = set([GlobeLearningTable('a', 10, 'b')])
GlobeLearningTable('a', 10, 'b') in demo

rawDescription: Creating Set of objects of user defined class in python


code:
@property
def numerator(self):
    """Integers are their own numerators."""
    return +self

@property
def denominator(self):
    """Integers have a denominator of 1."""
    return 1

rawDescription: Python: Why do int.numerator and int.denominator exist?


code: SimplePhysicObject

rawDescription: Macros in python

description: Macros in python


rawDescription: Find matching rows in 2 dimensional numpy array


code: print([k for k,_  in sorted(d.items(), key=itemgetter(1, 0))])

rawDescription: How to sort a dictionary by value and by key?

description: Sort a dictionary by value and by key


code: results = [x for x in itertools.product(range(3), repeat=12) if myfilter(x)]

rawDescription: Recursive generation + filtering. Better non-recursive?


code:
notification_config = {'SNSTopic': 'my_notification_topic',
                       'Events': ['ArchiveRetrievalCompleted',
                                  'InventoryRetrievalCompleted']}
vault.set_vault_notifications(vault, notification_config)

rawDescription: Using Boto to tell when a file has succesfully been uploaded to Glacier


code: set_key('ProxyEnable', 0)

rawDescription: Change browser proxy settings from Python?


rawDescription: Join two timelines / list of tuples

description: Join two timelines / list of tuples


code:
url(r'^register/$',
    RegistrationView.as_view(form_class=EmailDomainFilterRegistrationForm),
    name='registration_register'),

rawDescription: django-registration email domain whitelist

description: Django-registration email domain whitelist


rawDescription: Python fastest way to remove single spaces from spaced out letters in string


rawDescription: is it possible to convert this loop into a list comprehension in python

description: Is it possible to convert this loop into a list comprehension in python


rawDescription: Django: How to get related objects of a queryset?


code:
import os
import sys

walk_dir = sys.argv[1]

print('walk_dir = ' + walk_dir)

# If your current working directory may change during script execution, it's recommended to
# immediately convert program arguments to an absolute path. Then the variable root below will
# be an absolute path as well. Example:
# walk_dir = os.path.abspath(walk_dir)
print('walk_dir (absolute) = ' + os.path.abspath(walk_dir))

for root, subdirs, files in os.walk(walk_dir):
    print('--\nroot = ' + root)
    list_file_path = os.path.join(root, 'my-directory-list.txt')
    print('list_file_path = ' + list_file_path)

    with open(list_file_path, 'wb') as list_file:
        for subdir in subdirs:
            print('\t- subdirectory ' + subdir)

        for filename in files:
            file_path = os.path.join(root, filename)

            print('\t- file %s (full path: %s)' % (filename, file_path))

            with open(file_path, 'rb') as f:
                f_content = f.read()
                list_file.write(('The file %s contains:\n' % filename).encode('utf-8'))
                list_file.write(f_content)
                list_file.write(b'\n')

rawDescription: Python recursive folder read

description: Python recursive folder read


rawDescription: py3k: mapping dictionary (string->number) into list (of strings)

description: Py3k: mapping dictionary (string->number) into list (of strings)


code:
import sqlite3

@app.route('/approve/<int:post_id>')
def approve(post_id):
    con = sqlite3.connect('ugly_grills.db')
    con.row_factory = sqlite3.Row
    cur = con.cursor()
    cur.execute('UPDATE grills SET approved = "True" WHERE ID=?', (post_id,))
    # ------------------------------------------- DON'T FORGET THIS COMMA ^
    con.commit()
    cur.execute('SELECT * FROM grills')
    posts = cur.fetchall()
    return render_template("approval.html", posts=posts)

rawDescription: Python flask for loop

description: Python flask for loop


code:
frame = Frame1(None)
frame.readInformation()
frame.Show()

rawDescription: How do I load values into a wxPython app when it opens?

description: Load values into a wxPython app when it opens


code:
from ctypes import *
user= windll.LoadLibrary("c:\\winnt\\system32\\user32.dll") #I am in windows 2000, may be yours will be windows
h = user.GetDC(0)
gdi= windll.LoadLibrary("c:\\winnt\\system32\\gdi32.dll")
gdi.GetPixel(h,1023,767)
gdi.GetPixel(h,1024,767)

rawDescription: Faster method of reading screen pixel in Python than PIL?


code: { valueA: resultA, valueB: resultB, [valueC1, valueC2]: resultC }

rawDescription: Checking input values to methods to reduce the number of computations


code:
name =  "11111A"
ser = name[:3]     # first 3 characters
code = name[3:]    # rest of chars after the third
ser
code

rawDescription: Splitting letters and numbers to figure out and assign value in table


rawDescription: How to make celery retry using the same worker?

description: Make celery retry using the same worker


code:
word_tokenizer = RegexpTokenizer('[\S]+')
word_tokenizer.tokenize(sentence)

rawDescription: How to keep certain entities as one word using nltk tokenize in python?

description: Keep certain entities as one word using nltk tokenize in python


code:
def process_record(record):
    now = datetime.datetime.utcnow()
    try:
        delta = now - process_record.now
    except AttributeError:
        delta = 0
    process_record.now = now
    return {'time_since_last': delta}

logaugment.add(logger, process_record)
logger.warn("My message")

rawDescription: Python logging module - time since last log


code:
data2 = using_default_dtype(filename)
print(data2)
# [ (76920, '1995-12-31-00:00', nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 108.0, nan, nan, nan, nan, nan, nan, 14.3, nan, 17.4, nan, nan, nan, nan, 14.3, nan, nan, nan, nan, 1005.8, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 2.6, nan, nan, nan, nan, nan, nan, nan, nan, nan, 7.4, nan, nan, 54.0, 3.2, 5.1, nan, nan, nan, nan, nan, nan, nan, 7.4, nan, nan, 5.7, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 3.7, nan, nan, nan, nan, nan)
#  (76920, '2005-01-01-00:00', 52.0, nan, nan, nan, nan, nan, nan, nan, nan, 0.06, nan, nan, nan, nan, nan, nan, nan, nan, 230.0, nan, nan, nan, nan, nan, nan, 10.1, nan, 12.3, nan, nan, 8.5, nan, nan, nan, nan, nan, nan, 1016.7, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 7.0, nan, 4.7, nan, nan, nan, nan, nan, nan, nan, 7.8, nan, nan, 85.0, 1.5, 2.5, nan, nan, nan, nan, nan, nan, nan, 6.1, 12.8, 8.5, 4.9, nan, nan, nan, nan, nan, nan, 229.0, nan, 10.3, 12.3, nan, 8.6, nan, nan, 1016.8, nan, 7.1, 4.7, nan, nan, nan, 84.0, nan, 2.8, 19.88, 6.8, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 1.5, 2.4, 17.4, nan, 12.6, 5.8, 4.9)]

print(data2.dtype.descr[:5])
# [('Stno', '<i8'), ('DateHourNMT', '|S16'), ('CD', '<f8'), ('CD147', '<f8'), ('CD15', '<f8')]

print(type(data2))
# <type 'numpy.ndarray'>

rawDescription: Set one specific date type for reading in genfromtxt


code:
list_var = ['sales_qty' , 'returns_qty' , 'net_sales_qty' , 'sales_amt' , 'returns_amt' ,'product_discount_amt' , 'product_net_amt' ,'product_cost_amt' , 'gross_sales_amt' , 'supplier_disc_amt' , 'category_disc_amt' , 'topup_disc_amt' , 'value_at_cost']

list_dic = {k: [] for k in list_var}

rawDescription: Python - Initializing List variable from List elements


code:
import re
def multiple_replacer(*key_values):
    replace_dict = dict(key_values)
    replacement_function = lambda match: replace_dict[match.group(0)]
    pattern = re.compile("|".join([re.escape(k) for k, v in key_values]))
    return lambda string: pattern.sub(replacement_function, string)

rawDescription: Optimizing excel data collection/reduction using xlrd/xlwt and loop iteration


code:
f = makeGraph(int(self.gameType.get()))
try:
    self.vsb.destroy()
except:
    pass
try:
    self.tree.destroy()
except:
    pass
try:
    self.toolbar.destroy()
except:
    pass
try:
   self.canvas.get_tk_widget().destroy()
except:
   pass

self.canvas = FigureCanvasTkAgg(f,master = self.display_frame)
self.canvas.show()
self.canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
self.toolbar = NavigationToolbar2TkAgg( self.canvas, self.display_frame )
self.toolbar.update()
self.canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=1)

rawDescription: TkInter Python: Destroyed widget still shows and covers new widget


code:
import os, heapq
def oldest_files_in_tree(rootfolder, count=1, extension=".avi"):
    return heapq.nsmallest(count,
        (os.path.join(dirname, filename)
        for dirname, dirnames, filenames in os.walk(rootfolder)
        for filename in filenames
        if filename.endswith(extension)),
        key=lambda fn: os.stat(fn).st_mtime)

rawDescription: Find the oldest file (recursively) in a directory


rawDescription: Python - csv.DictWriter() or writerow() rounding my floats?


code:
# construct the list of arrays
data = []
for i in xrange(8):
    data.append(np.random.randint(0,2, (20, 20)))
indices = data[0] == data[1]
for i in xrange(2, 8):
    indices *= (data[0] == data[i])
result = np.empty_like(data[1], dtype=float)
result[indices] = data1[indices]
result[~indices] = np.nan
result

rawDescription: Extraction of common element in given arrays to make a new array


code:
def pyglatin2(word):
    output = ""
    wordlenedit = 0
    wordlen = len(word)
    for l in word:
        if l == 'a' or l == 'e' or l == 'o' or l == 'i' or l == 'u':
            break
        else:
            wordlenedit += 1

    output = word[wordlenedit:wordlen:1] + '-' + word[0:wordlenedit:1] + 'ay'
    return output

rawDescription: Python skipping my if statement

description: Python skipping my if statement


code: result = obj

rawDescription: Accessing elements in arbitarily nested structure of lists / dicts


code:
rows = [[255 for element in xrange(4) for number_of_pixles in xrange(256)] for number_of_rows in xrange(256)]
import numpy # Using numpy is much faster
rows = numpy.zeros((256, 256 * 4), dtype = 'int')
rows[:] = 255
png_writer = png.Writer(width = 256, height = 256, alpha = 'RGBA')
png_writer.write(open('white_panel.png', 'wb'), rows)

rawDescription: PyPNG to draw a simple filled box

description: PyPNG to draw a simple filled box


code:
# group by the most outer index
groups = df.groupby(level='messageId')

rawDescription: Read hierarchical (tree-like) XML into a pandas dataframe, preserving hierarchy


code: ArgSpec(args=['arg1', 'arg2', 'arg3'], varargs=None, keywords=None, defaults=(1,))

rawDescription: preserve argspec when decorating?

description: Preserve argspec when decorating


rawDescription: Build Back Adjusted Continous Futures contract from Historical Data


rawDescription: gnuplot.py - plotting time vs distance graph - multiple lines

description: Gnuplot.py - plotting time vs distance graph - multiple lines


code:
events = login.join(logout, how='outer')
events.fillna(value=0, inplace=True)

rawDescription: How do I get a Pandas TimeSeries for user sessions (using Pandas or Numpy)

description: Get a Pandas TimeSeries for user sessions (using Pandas or Numpy)


code:
class MyClass(object):
    def __init__(self, data):
        self.data = data

    def __getitem__(self, index):
        return type(self)(self.data[index])

    def __repr__(self):
        return repr(self.data)

x = MyClass(range(10))
x2 = x[0:2]

rawDescription: Ensuring __init__ is only called once when class instance is created by constructor or __new__


code:
def marco_filter(a):
    return a[a[:,0] > 1][:2]

def rico_filter(a):
    mask = a[:, 0] > 1
    where = np.where(mask)[0][:2]
    return a[where]

rawDescription: is it possible to combine a logical and a limit condition in a numpy array slice operator

description: Is it possible to combine a logical and a limit condition in a numpy array slice operator


rawDescription: python element wise add in list of list with other list

description: Python element wise add in list of list with other list


code:
def filterNegs(L):
    answer = []
    for i in L:
        if -1*i in L:
            answer.append(i)
    return answer

rawDescription: How to "negate"(?) a list - Python

description: "negate"(?) a list - Python


code:
import numpy as np
test1 = np.arange(6).reshape(2,3)
test1
0 in test1
test1[0][0] = 42
test1
0 in test1

rawDescription: Determine if there is at least one zero in a multidimensional numpy array


code: com = Commonest[Join[mc[[1]], mc[[-1]], Transpose[mc][[1]], Transpose[mc][[-1]]]]

rawDescription: Square detection with aberrations in edges and 1 missing corner


code: d = numpy.array([a, b, c]).T.flatten()

rawDescription: Numpy union arrays in order

description: Numpy union arrays in order


code:
qry = (session.query(Person)
        .join(Skill, Person.skills)
        .filter(Skill.id.in_(list_of_skill_ids))
        .group_by(Person)
        .having(len(list_of_skill_ids) <= func.count(Skill.id))
    )

print('\n'.join("{}".format(_) for _ in qry.all()))

rawDescription: SQLAlchemy - Query parent by its childer, filter a join to match every children in a list


code:
class Mixin:
    def set_value(self):
        more_set_value()
        super().set_value()

class AA(Mixin, A):
    pass

rawDescription: Python, how to flexibly create subclass?

description: Python, how to flexibly create subclass


code:
In : a = numpy.random.randn(5,2,3)

In : b = numpy.random.randn(2,3,8)

In : c = a[..., numpy.newaxis]*b[numpy.newaxis, ...]

In : c.shape
Out: (5, 2, 3, 8)

rawDescription: Elementwise multiplication of arrays of different shapes in python


code:
myfile.write('Hello, this is not a joke!')
myfile.close()

rawDescription: Opening and reading txt files

description: Opening and reading txt files


code:
def chaffify2(val, chaff_val = 87953):
    """ Add chaff to the given positive integer. """
    return val * chaff_val

def dechaffify2(chaffy_val, chaff_val = 87953):
    """ Dechaffs the given chaffed value. chaff_val must be the same as given to chaffify2(). If the value does not seem to be correctly chaffed, raises a ValueError. """
    val, chaff = divmod(chaffy_val, chaff_val)
    if chaff != 0:
        raise ValueError("Invalid chaff in value")
    return val

rawDescription: What is the best way to get a semi long unique id (non sequential) key for Database objects


code:
import pickle
some_dict = {'this':1,'is':2,'an':3,'example':4}

with open('saved_dict.pkl','w') as pickle_out:
    pickle.dump(some_dict,pickle_out)

rawDescription: Python: Append dictionary in another file


code:
@property
def max_health(self):
    return 10 + self.level

rawDescription: Having a Function change variable in Python 3.4?


code:
someSet = set(someList)
someDict.keys() & someSet

rawDescription: Optimize the performance of dictionary membership for a list of Keys


code:
df['dilation'] = ndimage.binary_dilation(mask, structure=[1]*(consecutive+1))
#      RF   mask dilation
# 0   0.3   True     True
# 1   1.1   True     True
# 2   0.2   True     True
# 3   0.0  False     True   <--,
# 4   0.0  False     True      |
# 5   0.0  False     True      |  dilation filled over 5 rainy days
# 6   0.0  False     True      |
# 7   0.0  False     True   <--'
# 8   1.1   True     True
# 9   0.6   True     True
# 10  0.0  False     True   <-- But the `True`s extend a bit too far
# 11  0.0  False     True   <--
# 12  0.0  False    False
# 13  0.0  False     True
# 14  0.0  False     True
# 15  0.0  False     True
# 16  0.8   True     True
# 17  1.1   True     True
# 18  2.3   True     True
# 19  1.4   True     True
# 20  0.4   True     True
# 21  0.2   True     True
# 22  0.0  False     True
# 23  0.0  False     True
# 24  0.0  False    False
# 25  0.0  False    False
# 26  0.0  False    False
# 27  0.0  False    False

rawDescription: How to identify approximately (threshold defined) continuous non null data using pandas?

description: Identify approximately (threshold defined) continuous non null data using pandas


code: ''.join(map(chr, smith.name))

rawDescription: How to print c_ubyte_Array object in Python

description: Print c_ubyte_Array object in Python


code:
def getCombs(pattern):
    pattern = pattern + [pattern[0]]
    return [pattern[ix:ix+2] for ix in xrange(len(pattern)-1)]

rawDescription: Itertools: selecting in pandas based on previous three rows, or previous elements in a list


rawDescription: Loading a config file from operation system independent place in python


code:
dict_st={}
for line in f:
    for item in line.split():
        k, v = map(int, item.split('-'))
        dict_st.setdefault(k, set()).add(v)

rawDescription: Creating dictionaries in a faster way - Python


rawDescription: Cleaning HTML tables with Pandas

description: Cleaning HTML tables with Pandas


code:
recent = my_table.objects.filter(date__lt=datetime.date.today()) \
                         .order_by('-date').first()

rawDescription: Django-Getting most recent entry before today


code:
index = string.find('banana', 'nana', 0) + 1 # index = 3
index = string.find('banana', 'nana', 3) + 1 # index = 0

rawDescription: Is there a difference between -1 and False in Python?


rawDescription: General Random Walk generator questions

description: General Random Walk generator questions


code:
import scraperwiki
superdictionary = {}
superlist       = []
superdictionary['1'] = {"a":1, "b":"Foo"}
superdictionary['1'].update({"c":"Bar"})
superdictionary['2'] = {"a":2, "b":"Grue", "c":"Gnu"}

for subdictionary in superdictionary:
    superlist.append(superdictionary[subdictionary])
scraperwiki.sqlite.save(["a"], superlist)

rawDescription: Appending data to ScraperWiki datastore

description: Appending data to ScraperWiki datastore


code: print(metadata)

rawDescription: Save additional attributes in Pandas Dataframe


code: list(map(lambda a, b: [a] + b, x, y))

rawDescription: How to use zip and map to write csv file in Python?

description: Use zip and map to write csv file in Python


rawDescription: Create multiindex based on single index

description: Create multiindex based on single index


code: (lambda x : x + 3)(5)

rawDescription: if annonymous, what is a call to python lambda?

description: If annonymous, what is a call to python lambda


code: apns_dict = {'aps':{'alert':'inner message','sound':'mySound.caf'}}

rawDescription: Format APNS-style JSON message in Python for use with Amazon SNS


code:
for lst in lists: random.shuffle(lst)
lists

rawDescription: Python: Random from multiple lists, no repeats


rawDescription: Python : How to vectorize my split function with Numpy


rawDescription: Optional Argument for Function in Python


code: series

rawDescription: Pandas: Select from date in MultiIndex

description: Pandas: Select from date in MultiIndex


code: words = set(line.strip() for line in open("words"))

rawDescription: what's a simple way to search a very long text file for a word in python?

description: What's a simple way to search a very long text file for a word in python


rawDescription: Upload files in Google App Engine

description: Upload files in Google App Engine


code:
def viewEvent(request, event_pk):
    event = Event.objects.get(pk=event_pk)
    event.num_views += 1
    event.save()

    return render_to_response("eventView.html", { 'event' : event })

rawDescription: How to get the count of views of a particular event?

description: Get the count of views of a particular event


code:
my_string = "The boy who cried wolf made many many silly mistakes."
def f(string, n):
    ...

rawDescription: Finding the number of letters that occur more than a specific number of times on python?


code:
s.align(s2, fill_value=0)
s, s2 = s.align(s2, fill_value=0)
s + s2

rawDescription: Join pandas series while keeping new indices


code: print ('%.20f' % myarray[0])

rawDescription: are numpy array elements rounded automatically?

description: Are numpy array elements rounded automatically


code:
n, nf = 2, 4
x = [str(i) for i in range(n * nf)]
y = [str(i*2) for i in range(n * nf)]
ID = [str(1001*i) for i in range(nf)]
OutputFile = open('output3.txt', 'w')
for i in range(0, len(x), nf):
    line = '\t'.join([x[i+j] + '\t' + y[i+j] for j in range(nf)])
    OutputFile.write(ID[i//nf] + '\t' + line + '\n')
OutputFile.close()

rawDescription: writing lines to a txt file python

description: Writing lines to a txt file python


code:
def f(x):
   x = numpy.asarray(x)
   if len(x) != 2:
      raise Exception("invalid argument")

f([1,2])
f([1,2,3])

rawDescription: Python function to accept numpy ndarray or sequence as arguments


code:
import multiprocess, time
def foo():
    time.sleep(60)
if __name__ == "__main__":
    process = [
        multiprocessing.Process(name="a",target=foo),
        multiprocessing.Process(name="b",target=foo),
        multiprocessing.Process(name="c",target=foo),
    ]
    for p in process:
        p.start()
    for p in process:
        print(p.name, p.pid)
    for p in process:
        p.join()

rawDescription: Easy way to tell apart python multiprocessing's OS processes


rawDescription: Python: Frequency of occurrences

description: Python: Frequency of occurrences


code:
import subprocess
seconds = "1" # has to be a string
subprocess.call(['ffmpeg', '-i', inputfilename, '-ss', seconds, outputfilename])

rawDescription: Trim (remove frames from) a video using Python


code:
Bone = df.groupby('B').filter(lambda x: len(x) == 1)
Bone

rawDescription: Only allow one to one mapping between two columns in pandas dataframe


code:
def f(x):
    """does some math"""
    return x + x * x
f = logged(f)

rawDescription: What does functools.wraps do?

description: What does functools.wraps do


code:
class TermsManager(models.Manager):
    def get_query_set(self):
        return super(TermsManager, self).get_query_set().filter(condition_here)

rawDescription: Django, override many-to-many field ModelManager


code:
import ansible.utils as utils
import ansible.errors as errors
import socket

class LookupModule(object):

    def __init__(self, basedir=None, **kwargs):
        self.basedir = basedir

    def run(self, terms, inject=None, **kwargs):

        if not isinstance(terms, basestring):
            raise errors.AnsibleError("ip lookup expects a string (hostname)")

        return [socket.gethostbyname(terms)]

rawDescription: Arbitrary host name resolution in Ansible


code:
a='testemail@email.com'
replace=['@','.']
for i in replace:
  a=a.replace(i,'_')

rawDescription: Remove special chracters from a string in django


code:
from collections import OrderedDict
D1 = {(10,12): 23, (8,14): 45, (12, 9): 29}
D1_sorted = OrderedDict(sorted(D1.items()))

rawDescription: Parse dictionary with Tuple as key

description: Parse dictionary with Tuple as key


code:
for ele in arr:
    ele = stripEscape(ele)
    if ele == '':
        arr.remove(ele)

rawDescription: Remove escape sequences from parsed HTML


code:
from datarray.datarray import DataArray as DA
D1 = DA(D, [rows, cols])
D1.axes
# now you can use R-like syntax to reference a NumPy data array by column:
D1[:,'col1']

rawDescription: How do I index n sets of 4 columns to plot multiple plots using matplotlib?

description: Index n sets of 4 columns to plot multiple plots using matplotlib


code:
getter = itemgetter(1,3,4)
desired = getter(items)

rawDescription: python find items in a list given indices

description: Python find items in a list given indices


code:
class CombinedForm(BaseForm):
  __metaclass__ = DeclarativeFieldsMetaclassFromMultipleClasses

rawDescription: Django multiple form factory

description: Django multiple form factory


code:
import sys
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt

class MainFrame(QtWidgets.QWidget):

    def __init__(self, parent=None):
        super(MainFrame, self).__init__(parent)

        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setFixedSize(860, 560)

        # Set the opacity
        #self.setWindowOpacity(1 - 50 / 100)

        layout = QtWidgets.QHBoxLayout(self)

        layout.addWidget(QtWidgets.QPushButton("TEST"))

        self.setAttribute(Qt.WA_TranslucentBackground)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    Frame = MainFrame(None)
    Frame.show()
    app.exec_()

rawDescription: transparent widgets inside transparent frame pyqt5

description: Transparent widgets inside transparent frame pyqt5


code:
count = Counter((x, y) for (x, y, *z) in a)
count['309', '308']

rawDescription: Searching for a word pattern in a Python dataset


code:
import nltk.corpus as corpus

def mark_sentence(row):
    if row[-1] == '.':
        row[-1] = '</s>'
    else:
        row.append('</s>')
    return ['<s>'] + row

news = corpus.brown.sents(categories = 'editorial')
for row in news[:5]:
    print(mark_sentence(row))

rawDescription: NLTK tokenize questions

description: NLTK tokenize questions


code:
s_tuples = zip(finger_lengths, finger_names)
s_tuples.sort()
finger_lengths, finger_names = zip(*temp)

rawDescription: How to sort object attributes

description: Sort object attributes


rawDescription: How can make callbacks in two sequentially Requests with scrapy


code:
[[{'eventCode': 'A',
   'eventCoding_FTNT_0': 'F1',
   'eventCoding_FTNT_1': 'F2',
   'eventCycled': '',
   'eventDate': '2003-12-31',
   'eventTitle': 'Some Event',
   'eventType': '47',
   'eventVoltage': '40000'}]]

rawDescription: How to associate values of tags with label of the tag the using ElementTree in a Pythonic way

description: Associate values of tags with label of the tag the using ElementTree in a Pythonic way


code:
class ContractAdmin(admin.ModelAdmin):
    list_display('clientNumber', 'supplier')

admin.register(Contract, ContractAdmin)

rawDescription: Get value of field from referenced model displayed in Django Admin site


code:
from d import D, evaluateExpr
from sympy import sin, cos, Matrix
sin.is_commutative = False
cos.is_commutative = False
g1 = []
g2 = []
g3 = []
var('x,t,r,A', commutative=False)
m=5
n=5
for j in xrange(1,n+1):
    for i in xrange(1,m+1):
        g1 += [sin(i*x)*sin(j*t),                 0,                 0]
        g2 += [                0, cos(i*x)*sin(j*t),                 0]
        g3 += [                0,                 0, sin(i*x)*cos(j*t)]
g = Matrix( [g1, g2, g3] )

B = Matrix(\
    [[     D(x),        0,        0],
     [    1/r*A,        0,        0],
     [ 1/r*D(t),        0,        0],
     [        0,     D(x),        0],
     [        0,    1/r*A, 1/r*D(t)],
     [        0, 1/r*D(t), D(x)-1/x],
     [        0,        0,        1],
     [        0,        1,        0]])

ans = evaluateExpr(B*g)

rawDescription: Differential Operator usable in Matrix form, in Python module Sympy


code:
import re
p = re.compile(r'(TAG|CORE)START(?: B)?(.+?)(\1END(?: B)?)', re.DOTALL)
test_str = "intro intro intro intro intro intro\nBEGIN A This is where some text starts\nThat is not interesting or wanted\nCORESTART save text A save text A save text A save text A \nsave text A save text A save text A save text A save text A \nsave text A COREEND\nThis is an addendum that is not needed but is just in the way\nEND A outro outro outro outro outro outro \noutro outro outro outro outro outro outro \n.\n\nintro intro intro intro intro intro\nINIT B This is where some text starts\nThat is not interesting or wanted\nTAGSTART B save text B save text B save text B save text B \nsave text B save text B save text B save text B save text B \nsave text B TAGEND B\nThis is an addendum that is not needed but is just in the way\nTERM B outro outro outro outro outro outro \noutro outro outro outro outro outro outro "
print([x.group(2) for x in p.finditer(test_str)])

rawDescription: python compound regex to extract text between different tags in different documents

description: Python compound regex to extract text between different tags in different documents


rawDescription: CSV programming read write

description: CSV programming read write


rawDescription: Python multiprocessing passing refrence

description: Python multiprocessing passing refrence


code: df['z-1'] = df.groupby('a')['z'].transform(lambda x:x.shift(-1))

rawDescription: Pandas Pivot table nearest neighbor

description: Pandas Pivot table nearest neighbor


code:
...
(r'^pages/(?P<page_name>\w+)', 'pages.views.display_content'),
...

rawDescription: Django - using regex in urls.py to pass values


code:
pages = DBSession.query(Pages)
for page in pages:
    config.add_route(str(page.name), page.url)
    if page.template.decode('utf-8') == "home_view":
        config.add_view(home_view, route_name=str(page.name), renderer='templates/home.pt')
    elif page.template.decode('utf-8') == "detail_view":
        config.add_view(home_view, route_name=str(page.name), renderer='templates/detail.pt')

rawDescription: Python Pyramid: Load view based on database variable


rawDescription: Python Pandas - how to apply boolean series to extract rows from dataframe


code: "http://{0}:{1}".format(*(d[key] for key in keys))

rawDescription: Having list of keys, get list/tuple of values from dict


rawDescription: How can I get from 'pyspark.sql.types.Row' all the columns/attributes name?

description: Get from 'pyspark.sql.types.Row' all the columns/attributes name


code:
caption = "This will be the description of the image in the album and also the message that appears on the wall"  # caption = '' is acceptable if you do not wish to have a message
albumid = ''  # this can be the album id of a facebook album or it can be left blank
with open("test.jpg","rb") as image:
        posted_image_id = GraphAPI.put_photo(image, caption, albumid)

rawDescription: Upload images to Facebook wall via python and Graphapi


code:
def get_size(source):
    total_size = 0
    total_size = os.path.getsize(source)
    for item in os.listdir(source):
        itempath = os.path.join(source, item)
        if os.path.isfile(itempath):
            total_size += os.path.getsize(itempath)
        elif os.path.isdir(itempath):
            total_size += get_size(itempath)
    return float(total_size) / 1048576

rawDescription: getting directory size of subdirectory in python

description: Getting directory size of subdirectory in python


code:
ax = plt.gca()
ax.xaxis.set_ticks(np.arange(0,10))  # turn on all tick locations
ax.xaxis.set_ticklabels(neworder)    # use neworder for labels

rawDescription: 're-sort' / adapt ticks of matshow matrix plot


code:
@substitute_args({}) # this function is called and return value is the decorator
def f(x):
  return x
# that (anonymous) decorator is applied to f

rawDescription: Python Decorator 3.0 and arguments to the decorator


rawDescription: How can I set the x-axis tick locations for a bar plot created from a pandas DataFrame?

description: Set the x-axis tick locations for a bar plot created from a pandas DataFrame


code:
import multiprocessing as mp

def f(mlist):
    b = [3, 5, 7]
    mlist[:]=b

if __name__ == '__main__':
    manager = mp.Manager()
    mlist = manager.list()
    p = mp.Process(target=f, args=[mlist])
    p.start()
    p.join()

    print(mlist[:])

rawDescription: How to change multiprocessing shared array size?

description: Change multiprocessing shared array size


code:
def pumpQt():
    global app
    def processor():
        app.processEvents()
    while 1:
        time.sleep(0.01)
        utils.executeDeferred( processor )

rawDescription: Using external GUI libraries to make user interfaces in Autodesk Maya


code:
def test():
    print("Hello, world!")
test = deco(test) # note that test is overwritten by the wrapper that deco returns

rawDescription: How decorator works?

description: How decorator works


code: image = Image.new('RGB', (xsize, ysize))

rawDescription: How to output unicode characters and lines into an image file? -python

description: Output unicode characters and lines into an image file? -python


code:
from operator import itemgetter
distances = ((k, abs(v - value)) for k, v in bucketed_items_dict.items())
return min(distances, key=itemgetter(1))[0]

rawDescription: Python: Elegant way of determining the nearest match for numbers in a dictionary


code:
from itertools import groupby
from collections import defaultdict
d = defaultdict(list)
with open("file.txt") as f:
    for k, val in groupby(f, lambda x: x.strip() != ""):
        # if k is True we have a section
       if k:
            # get key  "k" which is the first line
           # from each section, val will be the remaining lines
           k,*v = val
           # add or add to the existing key/value pairing
           d[k].extend(map(str.rstrip,v))
from pprint import pprint as pp
pp(d)

rawDescription: Append items to dictionary Python

description: Append items to dictionary Python


code:
def func(self):
    return self.__class__.__name__

setattr(a,'method',types.MethodType(func,a))

rawDescription: modify method only having the method's name and instance object

description: Modify method only having the method's name and instance object


code:
from itertools import chain
pprint([[item] for item in chain.from_iterable(lists)])

rawDescription: Split lists within list

description: Split lists within list


rawDescription: How to get euclidean distance on a 3x3x3 array in numpy

description: Get euclidean distance on a 3x3x3 array in numpy


code:
def time2sec(hr, min, sec):
    totalSeconds = sec + min*60 + hr*3600
    return totalSeconds

rawDescription: Define a python function time2sec with three optional keyword arguments hour minute and second


code:
l = range(10)
for i in l:
    l.remove(i)

l

rawDescription: python remove element from list while traversing it

description: Python remove element from list while traversing it


code:
def main():
    # define these here since you aren't modifying them outside of main
    x = 475
    y = 325
    drive = 0  # 0 = not moving, 1 = moving
    turn = 0   # 1 = clockwise, -1 = counter-clockwise
    # set to -90 since 0 points east.
    d = -90

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    drive = 1
                elif event.key == pygame.K_RIGHT:
                    turn = 1
                elif event.key == pygame.K_LEFT:
                    turn = -1
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP:
                    drive = 0
                elif event.key in (pygame.K_RIGHT, pygame.K_LEFT):
                    turn = 0

        if drive == 1:
            d += turn * 4

            x += 6 * math.cos(math.radians(d))
            y += 6 * math.sin(math.radians(d))

        screen.fill((40,40,40))
        pygame.draw.rect(screen, (0,0,255), (round(x, 0), round(y, 0), 50, 50))

        pygame.display.update()
        clock.tick(20)

rawDescription: Problems With a Simple Driving Game

description: Problems With a Simple Driving Game


code: z = linkage(a)

rawDescription: Tutorial for scipy.cluster.hierarchy

description: Tutorial for scipy.cluster.hierarchy


code:
class A(Base):
    __tablename__ = 'table_A'
    id = Column(Integer, primary_key=True)
    a = Column(String(32))
    b = Column(String(32))
    __table_args__ = (Index('my_index', "a", "b"), )

rawDescription: Multiple columns index when using the declarative ORM extension of sqlalchemy


code:
def __init__(self, frameType='All'):
    myClass.__init__(self, frameType)
    def addingCol(cObject):
        ...
    addingCol(self.cObject)

rawDescription: creating subclass form class returning pandas dataFrame

description: Creating subclass form class returning pandas dataFrame


code:
#!/usr/bin/env python
from subprocess import check_call

cmd = "sox input.wav - -t wav pad 0.0 3.0 | sox - -t wav out.wav trim 0.0 3.0"
check_call(cmd, shell=True)

rawDescription: Wait for external program to finish writing to file


rawDescription: How to use a list in other function?

description: Use a list in other function


code:
for i in range(10):
    print(('' if i else 'number\n')+str(i))

rawDescription: How to tell when a method is called for first time of many

description: Tell when a method is called for first time of many


code:
for item in col.find({"$and": [{"item1": 'a', "item1": 'b', "item1": 'c'}]}):
    print(item)

rawDescription: mongo - find items who at least match an array of values

description: Mongo - find items who at least match an array of values


code:
import string, random, timeit
def regex_filter(words, blacklist):
    [word for word in A if not blacklist.search(word)]

def any_filter(words, blacklist):
    [word for word in A if not any(bad in word for bad in B)]

words = [''.join([random.choice(string.letters) for _ in range(random.randint(3, 20))])
         for _ in range(1000)]
blacklist = [''.join([random.choice(string.letters) for _ in range(random.randint(2, 5))])
             for _ in range(10)]
timeit.timeit('any_filter(words, blacklist)', 'from __main__ import any_filter, words, blacklist', number=100000)
timeit.timeit('regex_filter(words, blacklist)', "from __main__ import re, regex_filter, words, blacklist; blacklist = re.compile('|'.join([re.escape(word) for word in blacklist]))", number=100000)

rawDescription: Delete item from list if it contains a substring from a "blacklist"


code:
from django.conf import settings

class DatabaseAppsRouter(object):
    """
    A router to control all database operations on models for different
    databases.

    In case an app is not set in settings.DATABASE_APPS_MAPPING, the router
    will fallback to the `default` database.

    Settings example:

    DATABASE_APPS_MAPPING = {'model_name1': 'db1', 'model_name2': 'db2'}

    """

    def db_for_read(self, model, **hints):
        """Point all read operations to the specific database."""
        return settings.DATABASE_APPS_MAPPING.get(model._meta.model_name, None)

    def db_for_write(self, model, **hints):
        """Point all write operations to the specific database."""
        return settings.DATABASE_APPS_MAPPING.get(model._meta.model_name, None)

    def allow_relation(self, obj1, obj2, **hints):
        """Have no opinion on whether the relation should be allowed."""
        return None

    def allow_syncdb(self, db, model): # if using Django version <= 1.6
        """Have no opinion on whether the model should be synchronized with the db. """
        return None

    def allow_migrate(db, model): # if using Django version 1.7
        """Have no opinion on whether migration operation is allowed to run. """
        return None

    def allow_migrate(db, app_label, model_name=None, **hints): # if using Django version 1.8
        """Have no opinion on whether migration operation is allowed to run. """
        return None

rawDescription: Single django app to use multiple sqlite3 files for database


code:
class myarray(np.ndarray):
    def __new__(cls, *args, **kwargs):
        return np.array(*args, **kwargs).view(myarray)
    def index(self, value):
        return np.where(self==value)

rawDescription: Index of element in Numpy array

description: Index of element in Numpy array


code:
import urlparse
path = urlparse.urlparse('http://www.mydomain.com/hithere/something/else').path
path

rawDescription: Python: Get URL path sections

description: Python: Get URL path sections


code:
M = np.matrix([[x, y], [w, z]])

# or
A = np.array([[x, y], [w, z]])

rawDescription: Values of Variables Matrix NumPy

description: Values of Variables Matrix NumPy


rawDescription: Permute 5 (or 6) lists

description: Permute 5 (or 6) lists


code:
def new_player(n, name="none"):
    return {"player" : n, "name": name, "fired": 0, "hits": 0, "misses": 0, "streak": 0, "longest streak": 0}

y = [new_player(n) for n in range(2)]
y

rawDescription: For Loop Over Dictionaries in List (Python 2.7.6)


code:
from collections import defaultdict
myNewDict = defaultdict(set)
for staff_id, names in myDict.items():
    for name in names:
        myNewDict[name].add(staff_id)

rawDescription: Extend dict from key/values pair to key/value pair in Python


code: elements2 = elements0.append(elements1)

rawDescription: Py ReportLab append platypus

description: Py ReportLab append platypus


code:
from __future__ import division
import numpy as np
import scipy.fftpack as fftpack

# Define signal.
Fs = 128  # Sampling rate.
Ts = 1 / Fs  # Sampling interval.
Time = np.arange(0, 10, Ts)  # Time vector.
signal = np.cos(4*np.pi*Time) + np.cos(6*np.pi*Time) + np.cos(8*np.pi*Time)

def spectrum(sig, t):
    """
    Represent given signal in frequency domain.
    :param sig: signal.
    :param t: time scale.
    :return:
    """
    f = fftpack.rfftfreq(sig.size, d=t[1]-t[0])
    y = fftpack.rfft(sig)
    return f, np.abs(y)

def bandpass(f, sig, min_freq, max_freq):
    """
    Bandpass signal in a specified by min_freq and max_freq frequency range.
    :param f: frequency.
    :param sig: signal.
    :param min_freq: minimum frequency.
    :param max_freq: maximum frequency.
    :return:
    """
    return np.where(np.logical_or(f < min_freq, f > max_freq), sig, 0)

freq, spec = spectrum(signal, Time)
signal_filtered = fftpack.irfft(bandpass(freq, spec, 5, 7))

print(signal_filtered)

rawDescription: How to convert complex numbers back into "normal" numbers after performing FFT

description: Convert complex numbers back into "normal" numbers after performing FFT


code:
s = sorted(x)
s
original = [None] * len(s)
for i, c in zip(r, s): original[i] = c

original

rawDescription: undo or reverse argsort(), python

description: Undo or reverse argsort(), python


rawDescription: How to I select individual characters out of a sting and replace them with another [Python 3]?


code: eastern.localize(datetime(2002, 10, 27, 1, 30, 00), is_dst=None)

rawDescription: How to convert EST/EDT to GMT?

description: Convert EST/EDT to GMT


rawDescription: NumPy sort matrix rows by number of non-zero entries


code:
strip_result = Play.strip()
split_result = strip_result.split()
argu = split_result[0]
Work.find(argu)

rawDescription: How does multiple function calls works in python 2.7


code: my_utf_8_bytearray =  mystring.encode()

rawDescription: Changing encoding python 3

description: Changing encoding python 3


code:
reordered_corpus = open('reordered_corpus.txt', 'w')
with open('corpus.txt', 'r') as corpus:
    for phrase in corpus:
        phrase = phrase.split()                 # split on whitespace
        vb_index = rp_index = -1                # variables for the indices
        for i, word_pos in enumerate(phrase):
            pos = word_pos.split('_')[1]        # POS at index 1 splitting on _
            if pos == 'VB' or pos == 'VBZ':     # can add more verb POS tags
                vb_index = i
            elif vb_index >= 0 and pos == 'RP': # or more particle POS tags
                rp_index = i
                break                           # found both so can stop
        if vb_index >= 0 and rp_index >= 0:     # do any rearranging
            phrase = phrase[:vb_index+1] + [phrase[rp_index]] + \
                     phrase[vb_index+1:rp_index] + phrase[rp_index+1:]
        reordered_corpus.write(' '.join(word_pos for word_pos in phrase)+'\n')
reordered_corpus.close()

rawDescription: How to change the word order of phrasal verbs in a POS-tagged corpus file

description: Change the word order of phrasal verbs in a POS-tagged corpus file


code:
depths = [[] for i in range(4)]
depths

rawDescription: dynamically declare/create lists in python

description: Dynamically declare/create lists in python


code:
masked = (df['emld'] == 'aol.com') & (df['bounce_type'] == 'hard')
# then the result will be
df[masked]

rawDescription: How to split pandas data frame by many criteria

description: Split pandas data frame by many criteria


code:
# df is the dataframe
from collections import defaultdict
sample_dict = defaultdict(list)
for line in df.iterrows():
    k = line[1][0]  # key
    d_tuple = (line[1][1], line[1][2]) # (data_1, data_2)
    sample_dict[k].append(d_tuple)

rawDescription: pandas how to convert a dataframe into a dictionary of tuples tuple using 1 col as key and the rest as a tuple of form (col2:col3)

description: Pandas how to convert a dataframe into a dictionary of tuples tuple using 1 col as key and the rest as a tuple of form (col2:col3)


code:
def find_all_paths(graph, start, path=[]):
    path = path + [start]
    if not graph.has_key(start):
        return [path]
    paths = [path]
    for node in graph[start]:
        if node not in path:
            newpaths = find_all_paths(graph, node, path)
            for newpath in newpaths:
                paths.append(newpath)
    return paths

rawDescription: Find all paths without specifing end node?


code:
def interact(something, player):
   # note both Sprites must have a rect attributes
   return pygame.sprite.collide_rect(something,player)

rawDescription: Python v2.7. PyGame. Difference in pixels size between image.load and image.fill


rawDescription: Recursively find the last edit on a file in a folder


code:
authentication_url = '/accounts/login'
def feed_safe_login_required ( view ):
    def _ ( request, *args, **kwargs ):
        if not request.user.is_authenticated:
            return HttpResponseNotAuthorized, authentication_url
    return _

feed = feed_safe_login_required(django.contrib.syndication.views.feed)

rawDescription: Django RSS Feed Authentication

description: Django RSS Feed Authentication


code:
import csv
import datetime

with open('raindata.csv') as file:
    csv_reader = csv.DictReader(file)
    field_names = list(csv_reader.fieldnames)
    rows = list(csv_reader)

# insert new column header
if 'WEEKDAY' not in field_names:
    field_names.insert(field_names.index('OB_DATE') + 1, 'WEEKDAY')

with open('raindata.csv', 'w') as file:
    csv_writer = csv.DictWriter(file, field_names)
    csv_writer.writeheader()
    for row in rows:
        row['WEEKDAY'] = datetime.datetime.strptime(row['OB_DATE'], ' %Y-%m-%d %H:%M').strftime('%A')
        csv_writer.writerow(row)

rawDescription: Python: how to identify existing field value and avoid updating while writing


code: sub_m = m[i]

rawDescription: How should I go about subsampling from a scipy.sparse.csr.csr_matrix and a list


code: df.apply(lambda x: np.sort(x.values)[::-1], axis=1)

rawDescription: `pandas.DataFrame.apply` in a row by row operation


rawDescription: Gauss-Legendre Algorithm in python

description: Gauss-Legendre Algorithm in python


code:
import sys

today = str(sys.argv[1]) if len(sys.argv) > 1 else datetime.datetime.now().strftime('%Y%m%d')

rawDescription: Python how to pass in an optional filename parameter when running a script to process a file


code:
ax.add_collection(patch_collection)
plt.colorbar(patch_collection)

plt.show()

rawDescription: Log Scale Matplotlib PatchCollection Colors


code: R = (self.VVm[:,None,None]-VVs[None,:,:])**2 + (self.HHm[:,None,None]-HHs[None,:,:])**2

rawDescription: push for-loop to numpy

description: Push for-loop to numpy


code:
import numpy as np
a = np.array([[1,2,3],[4,5,6],[0,0,1]])
np.sort(a.view('i8,i8,i8'), order=['f1'], axis=0).view(np.int)

rawDescription: sorting arrays in numpy by column

description: Sorting arrays in numpy by column


code:
arg_map = {1 : 'JOHN', 4 : 'SMITH', 8 : 'BEN'}
translated_result = [arg_map.get(val, '%s NOT MAPPED' % str(val)) for val in result_list]

rawDescription: Python: Best way to translate constants?

description: Python: Best way to translate constants


code:
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://httpbin.org/get", params=payload)

rawDescription: Pass JSON function arguements using requests


code:
class Iterable:
    ...

    @classmethod
    def __subclasshook__(cls, C):
        if cls is Iterable:  # <<<<
            if _hasattr(C, "__iter__"):
                return True
        return NotImplemented

rawDescription: issubclass of abstract base class Sequence

description: Issubclass of abstract base class Sequence


code:
plt.figure()
CS = plt.pcolormesh(X, Y, Z)
plt.contour(X, Y, Z, 4, colors='k')
plt.colorbar(CS)
plt.show()

rawDescription: Plotting contours over pcolormesh data

description: Plotting contours over pcolormesh data


rawDescription: How to save and load an array of complex numbers using numpy.savetxt?

description: Save and load an array of complex numbers using numpy.savetxt


code:
img = Image.new('RGB', (200, 100))
d = ImageDraw.Draw(img)
d.text((20, 20), 'Hello', fill=(255, 0, 0))

rawDescription: Convert string to image in python

description: Convert string to image in python


code:
def parse(self, response):
    item = DmozItem()
    images = response.xpath('//div[@class="overhid"]//img')
    item['image_urls'] = [image.xpath('@src | @lazysrc').extract()[0]
                          for image in images]
    return item

rawDescription: Downloading mulitple images through scrapy in a single page


code:
import numpy as np
import scipy.ndimage.interpolation

def ndim_interp(A, ranges, p):
    # A: array with n dimensions
    # ranges: list of n lists or numpy arrays of values along each dimension
    # p: vector of values to find (n elements)

    # calculate the coordinates into array positions in each direction
    p_arr = []
    # iterate through all dimensions
    for i in range(A.ndim):
        # check if we are overrange; if we are, use the edgemost values
        if p[i] <= ranges[i][0]:
            p_arr.append(0)
            continue
        if p[i] >= ranges[i][-1]:
            p_arr.append(A.shape[i] - 1)
            continue

        # find the nearest values to the left
        right = np.searchsorted(ranges[i], p[i])
        left = right - 1

        # find the relative distance
        d = (p[i] - ranges[i][left]) / (ranges[i][right] - ranges[i][left])

        # append the position
        p_arr.append(left + d)

    coords = np.array(p_arr).reshape(A.ndim, -1)
    return scipy.ndimage.interpolation.map_coordinates(A, coords, order=1, mode='nearest')[0]

rawDescription: How to interpolate using nearest neighbours for high dimension numpy python arrays

description: Interpolate using nearest neighbours for high dimension numpy python arrays


code:
from scipy.spatial.distance import cdist
out = cdist(a,b,'sqeuclidean')

rawDescription: NumPy Broadcasting: Calculating sum of squared differences between two arrays


code:
a = [('X', '63.658'), ('Y', '21.066'), ...]

processed = {}

for char, value in data:
    if char not in processed:
        processed[char] = []
    processed[char].append(value)

rawDescription: How to Search data from a list of Key-Value pair that it is in list or not


code: #! /usr/bin/env python2

rawDescription: Using subprocess with script using another version


code:
#!/usr/bin/env python

''' Parse object data from a .obo file

    From http://stackoverflow.com/q/32989776/4014959

    Written by PM 2Ring 2015.10.07
'''

from __future__ import print_function, division

import json
from collections import defaultdict

fname = "go-basic.obo"
term_head = "[Term]"

#Keep the desired object data here
all_objects = {}

def add_object(d):
    #print(json.dumps(d, indent = 4) + '\n')
    #Ignore obsolete objects
    if "is_obsolete" in d:
        return

    #Gather desired data into a single list,
    # and store it in the main all_objects dict
    key = d["id"][0]
    is_a = d["is_a"]
    #Remove the next line if you want to keep the is_a description info
    is_a = [s.partition(' ! ')[0] for s in is_a]
    all_objects[key] = d["name"] + is_a

#A temporary dict to hold object data
current = defaultdict(list)

with open(fname) as f:
    #Skip header data
    for line in f:
        if line.rstrip() == term_head:
            break

    for line in f:
        line = line.rstrip()
        if not line:
            #ignore blank lines
            continue
        if line == term_head:
            #end of term
            add_object(current)
            current = defaultdict(list)
        else:
            #accumulate object data into current
            key, _, val = line.partition(": ")
            current[key].append(val)

if current:
    add_object(current)

print("\nall_objects =")
print(json.dumps(all_objects, indent = 4, sort_keys=True))

rawDescription: Parsing file into Parent/ Child format for a JSON file


code:
if request.is_ajax():

    rendered = render_to_string('lootr/deal-item.html', params, context)

    if not request.user.is_authenticated():
        cache.set(request.get_full_path(), rendered, 60 * 30)

    return HttpResponse(json.dumps(rendered), content_type="application/json")

rawDescription: Django cache 2 different versions of a url (JSON & HTML)


code:
for line in file:
    print(''.join(c * n for c, n in line))

rawDescription: Python Challenge - Level 5 , almost there


code:
import numpy as np

n = 100000
a1 = np.random.randint(0, 100, size=(n, 3))
a2 = a1[np.random.permutation(np.arange(n))]
idx1 = np.lexsort(a1.T)
idx2 = np.lexsort(a2.T)
idx = idx2[np.argsort(idx1)]
np.all(a1 == a2[idx])

rawDescription: Order of elements in a numpy array

description: Order of elements in a numpy array


code:
def for_loop(A):
    N1 = B.shape[0]
    for i in xrange(N1):
       A[y[i]] -= B[i,:]
    return A

rawDescription: Vectorize addition into array indexed by another array


code:
import glob
from shutil import copy2
import shutil
files = glob.glob('cvs_DIR/*csv')

for file in files:

    try:
        # need to have full path of cvs_DIR
        oldName = os.path.join(cvs_DIR, file)
        newName = os.path.join(cvs_DIR, file[:4] + '_offset.csv')
        copy2(oldName,newName)

    except shutil.Error as e:
        print('Error: {}'.format(e))

rawDescription: Python: Looping through multiple csv files and making multiple new csv files


code:
num = ['1', '2', '', '6']
L = [float(n) for n in num if n]
avg = sum(L)/len(L) if L else '-'

rawDescription: Average of a list of numbers, stored as strings in a Python list


code: [f for f in arange(1,1.1,.02)]

rawDescription: Dealing with floating points: With numpy python in particular


rawDescription: Custom format to JSON

description: Custom format to JSON


code:
B=filter(lambda x:x<0,A)
C=filter(lambda x:x>0,A)
B
C

rawDescription: Python - Split a list of integers into positive and negative


rawDescription: Change set of indices

description: Change set of indices


code:
a = [range(2), range(3)]
b = copy.copy(a)          # shallow copy of a, equivalent to a[:]
b[0] = range(4)
a, b                      # setting an element of b leaves a unchanged
b[1].append(3)
a, b                      # modifying an element of b modifies the element in a
a = [range(2), range(3)]
b = copy.deepcopy(a)      # deep recursive copy of a
b[1].append(3)
a, b                      # modifying anything in b leaves a unchanged

rawDescription: Python objects confusion: a=b, modify b and a changes!


code:
import dill
dill.loads(dill.dumps(your_bad_object))
...
# if you get a pickling error, use dill's tools to figure out a workaround
dill.detect.badobjects(your_bad_object, depth=0)
dill.detect.badobjects(your_bad_object, depth=1)
...

rawDescription: How to get a python function's dependencies for pickling?

description: Get a python function's dependencies for pickling


code:
def get_dollar_format_text(dol_and_cent):
    '''Returns the dollar and cents part of a money amount as
    a two element list of integers, where the first element is
    dollars and the second is cents.
    '''
    return [int(x) for x in '{0:.2f}'.format(dol_and_cent).split('.')]

rawDescription: Beginner Python: Extract List to integer


code: without_last = msg.decode('utf8')[:-1]

rawDescription: How to remove last utf8 char of a python string

description: Remove last utf8 char of a python string


code: my_list = [elem[0] for elem in your_dict.values()]

rawDescription: How to get the first value in a python dictionary

description: Get the first value in a python dictionary


code:
import cv2

cap = cv2.VideoCapture(r'C:\Users\e01069\Downloads\drop.avi')
print ("open  = ",cap.isOpened())

rawDescription: python reading a video

description: Python reading a video


rawDescription: PyQt or PySide: QTextEdit deselect all

description: PyQt or PySide: QTextEdit deselect all


code:
from itertools import imap, izip

print(all(x == y for x,y in izip(imap(str, tup2),imap(str, tup1))))

rawDescription: Python compare tuples with different data types but same values


rawDescription: Extract information from Gmail with Python


code:
mydictionary
mydictionary[1]

rawDescription: Zip key value pairs in python

description: Zip key value pairs in python


code:
from random import *
summ = 0
all_rolls = [1,2,3]
roll = randint(1,6)
all_rolls.append(roll)

for die_roll in all_rolls:
    summ += die_roll

print ("Roll:",roll," Total sum",summ)

rawDescription: Python Tk: Can't Retrieve Dice Roll Integer for Calculations


code:
url("^permalink/(?P<key>[a-f0-9]{8})$",
    "view.that.redirects.to.permalink.refersTo"),

rawDescription: How to generate shareable links in django? (similar to pastebin links)

description: Generate shareable links in django? (similar to pastebin links)


code:
def sum_of_squares_of_digits(value):
    return sum(int(c) ** 2 for c in str(value))

sum_of_squares_of_digits(987)

rawDescription: Python Sum of Squares Function

description: Python Sum of Squares Function


rawDescription: Django - Following ForeignKey relationships "backward" for entire QuerySet


code: df['Date'] = pd.to_datetime(df['Date'])

rawDescription: calculate total events in month using pandas

description: Calculate total events in month using pandas


code: [combo[key] for key in numb if key in combo]

rawDescription: Printing the values of a dictionary when its key is equal to the value of a list element


code:
import SocketServer
import json

class Server(SocketServer.ThreadingTCPServer):
    allow_reuse_address = True

class Handler(SocketServer.BaseRequestHandler):
    def handle(self):
        self.request.sendall(json.dumps({'id':'3000'}))  # your JSON

server = Server(('127.0.0.1', 50009), Handler)
server.serve_forever()

rawDescription: Stream Json with python localy

description: Stream Json with python localy


code:
import requests
url = "http://www.numbertheory.org/php/classnopos.php"
payload = {'M1value':'7'}
r = requests.post(url, params=payload)
print(r.content)

rawDescription: Submitting web form from python and saving results


code:
results = []
result = []
for num in inputlist:
    if sum(result) + num < 100:
        result.append(num)
    else:
        results.append([tuple(result), sum(result)])
        result = [num]
if result:
    results.append([tuple(result), sum(result)])

rawDescription: take sum of ints preserving specific information

description: Take sum of ints preserving specific information


code:
from django.shortcuts import render
from .models import Fight, Fighter
from .forms import FighterSelectForm
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.views.generic.edit import FormView

def random_fight(request):
    fighters = Fighter.objects.order_by("?")[:2]
    fight = Fight.objects.create(member1=fighters[0], member2=fighters[1])
    return HttpResponseRedirect(reverse('fight_view', kwargs={"fight_id": str(fight.id)}))

class FightView(FormView):
    template_name = 'fight.html'
    success_url = '/thanks/'    # Not defined here

    def get_form(self, form_class):
        fight = Fight.objects.get(id=self.kwargs.get("fight_id"))
        choices = [(fight.member1.id, fight.member1.name),
                   (fight.member2.id, fight.member2.name)]
        kwargs = super(FightView, self).get_form_kwargs()
        kwargs.update({"choices": choices})
        form = FighterSelectForm(**kwargs)
        return form

    def form_valid(self, form):
        selected = Fighter.objects.get(id=form.cleaned_data['fighter_choices'])
        selected.rating += 100  # Use any logic to change rating here
        selected.save()
        print("Fighter selected: {}".format(selected.name))
        return super(FightView, self).form_valid(form)

rawDescription: What Form should I create to display these models?


rawDescription: Distinguish between a file or a directory in paramiko


code:
def curry(_curried_func,  *args,  **kwargs):
    def _curried(*moreargs, **morekwargs):
        return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
    return _curried

rawDescription: How to debug dynamically defined functions in Python?

description: Debug dynamically defined functions in Python


rawDescription: sorting of dataframe based on condition and return all the group by assigning to random vector dynamically

description: Sorting of dataframe based on condition and return all the group by assigning to random vector dynamically


rawDescription: Extract a digit from a unicode string

description: Extract a digit from a unicode string


rawDescription: How do I return the index number for nth occurrence of a given value?

description: Return the index number for nth occurrence of a given value


code:
def __format__(self, spec):
    if spec.endswith('pretty'):
        prettified = self.pretty_version()
        return prettified.__format__(spec[:-6])
    return str(self).__format__(spec)

rawDescription: what is the Pythonic way to implement a __str__ method with different format options?

description: What is the Pythonic way to implement a __str__ method with different format options


code:
lists = [
    [True, True, True, False, False, True, False, False],
    [True, True, False, False, True, False, False, True],
    [True, False, False, True, False, False, True, True],
    [False, False, True, False, False, True, True, True],
    [False, True, False, False, True, True, True, False],
    [True, False, False, True, True, True, False, False],
    [False, False, True, True, True, False, False, True],
    [False, True, True, True, False, False, True, False],
]

for l in lists:
    zero_one = map(int, l)  # convert True to 1, False to 0  using `int`
    n = int(''.join(map(str, zero_one)), 2)  # numbers to strings, join them
                                             # convert to number (base 2)
    print('{:02x}'.format(n))  # format them as hex string using `str.format`

rawDescription: Changing an array of True and False answers to a hex value Python


code: event_form = CreateEventForm(request.POST or None, prefix='event', is_authenticated=request.user.is_authenticated())

rawDescription: not show field in form if user is authenticated

description: Not show field in form if user is authenticated


rawDescription: Comparing the elements in two JSON dicts and getting the difference as a ratio or percentage


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

import os
import urllib

from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

class MainHandler(webapp.RequestHandler):
    def get(self):
        upload_url = blobstore.create_upload_url('/upload')
        self.response.out.write('')
        self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
        self.response.out.write("""Upload File: <input type="file" name="file">
 <input type="submit"
            name="submit" value="Submit"> </form>""")

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get_uploads('file')
        blob_info = upload_files[0]
        self.redirect('/serve/%s' % blob_info.key())

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self, resource):
        resource = str(urllib.unquote(resource))
        blob_info = blobstore.BlobInfo.get(resource)
        self.send_blob(blob_info)

def main():
    application = webapp.WSGIApplication(
          [('/', MainHandler),
           ('/upload', UploadHandler),
           ('/serve/([^/]+)?', ServeHandler),
          ], debug=True)
    run_wsgi_app(application)

if __name__ == '__main__':
  main()

rawDescription: Python app engine: how to save a image?

description: Python app engine: how to save a image


rawDescription: Many tutorials on writing csv files have the mode set to 'wb', why?


code:
data = [1, 2, 3]

for item in data:
    try:
        do_stuff(item)
        break # we just need to do stuff with the first successful item
    except Exception:
        handle_errors(item) # in case of no success, handle and skip to next item
else:
    raise Exception("All items weren't successful")

rawDescription: Iterate through list and handle StopIteration in Python beautifully


code:
cnxn=ceODBC.connect('DRIVER={SOMEODBCDRIVER};DBCName=XXXXXXXXXXX;UID=XXXXXXX;PWD=XXXXXXX;QUIETMODE=YES;', autocommit=False)
cursor = cnxn.cursor()

rawDescription: how to transform pandas dataframe for insertion via executemany() statement?

description: Transform pandas dataframe for insertion via executemany() statement


code:
def init(self, *args, **kwargs):
    kwargs.update(zip(self._fields, args))
    args = [kwargs[k] for k in self._fields]
    list.__init__(self, args)

rawDescription: Improving my `namedlist` class inspired by `namedtuple`


code:
def reduce_matrix(M, i, j):
    newM=[k[:] for k in M]
    #rest of code

rawDescription: Python copy matrix

description: Python copy matrix


code: np.unravel_index(4, (2,3))

rawDescription: How to find bin edges of given bin number returned by scipy.stats.binned_statistic_dd()?

description: Find bin edges of given bin number returned by scipy.stats.binned_statistic_dd()


code:
names = pd.unique(df.values.ravel())
names = pd.Series(np.arange(len(names)), names)
names

rawDescription: Assign unique id to columns pandas data frame


code:
indices = df.loc[(df == 0).any(axis=1), :].index.tolist() ## multi-index values that contain 0
for ind in indices:
    df = df.drop(ind[0])

rawDescription: Filter out rows/columns with zero values in MultiIndex dataframe


code:
ARG = list(sys.argv[1])
ARG.sort()

rawDescription: Find out the words from a python list which contains permutation of another word


code:
source
import threading
import signal

class Class1(object):
    _stop_event = threading.Event()

    @staticmethod
    def interrupt():
        Class1._stop_event.set()

    def wait(self):
        print("Waiting for _stop_event")
        if Class1._stop_event.wait(5):
            print("_stop_event set.")
        else:
            print("Timed out.")

def stop(signum, frame):
    print("Received SIG")
    Class1.interrupt()

signal.signal(signal.SIGINT, stop)

c = Class1()
c.wait()

rawDescription: multiprocessing.Event.wait hangs when interrupted by a signal

description: Multiprocessing.Event.wait hangs when interrupted by a signal


rawDescription: python list to dict conversion confusion

description: Python list to dict conversion confusion


code:
SEG
S
SG
VON
Vontobel

rawDescription: Key to Value to Key in Python Dictionary


rawDescription: Handling windows authentication while accessing url using requests


code:
def IterAllSums(lst) :
    combinations = [i for i in range(1 << len(lst)) if HasAtLeastTwoBitsSet(i)]
    for comb in combinations :
        sublist = GetSublistByCombination(lst, comb)
        sum_str = '+'.join(map(str, sublist))
        sum_val = sum(sublist)
        yield (sum_str, sum_val)

rawDescription: Permutations in python 2.5.2

description: Permutations in python 2.5.2


code:
from bson.son import SON
pipeline = [
    {"$match": {"user.statuses_count": {"$gt": 99}, "user.time_zone": "Brasilia"}},
    {"$group": {"_id": "$user.id", "followers": { "$max": "$user.followers_count" }, "doc": {"$addToSet": "$$ROOT"}}},
    {"$sort": {"followers": -1 }},
    {"$unwind": "$doc"}, {"$project": {"_id": 0, "userId": "$_id", "screen_name": "$doc.user.screen_name", "retweet_count": "$doc.retweet_count", "followers": 1}},
    {"$limit": 1}
]
list(db.tweet.aggregate(pipeline))

rawDescription: Nothing happens when trying to use $project


rawDescription: Naming inline images when sending HTML mails with Python's email.mime.multipart


code:
import argparse
parser = argparse.ArgumentParser()
_ = parser.add_argument("-p", type=int, choices=set((0, 1)))
parser.parse_args(["-p", "0"])

rawDescription: Restricting values of commadline options


rawDescription: From Matlab to Python - Solve function

description: From Matlab to Python - Solve function


code:
from itertools import product
serial_result = map(lambda x:(x[0]**2,x[1]**3), product(range(20), range(10)))

rawDescription: Is it possible to include mutiple equations in a lambda function


rawDescription: Not so simple recursive descent in PyParsing


code:
tabela_dict = dict(tabela)
for i in pixels:
    if i in tabela_dict :
        arrayBits.append(tabela_dict[i])

rawDescription: How to make comparisons between two large lists fast python

description: Make comparisons between two large lists fast python


code:
class Distance(object):
    def __init__(self, meters=None, feet=None):
        self.feet = feet
        self.meters = meters

rawDescription: Python class Inheritance Initialization

description: Python class Inheritance Initialization


rawDescription: Conversion of Matlab sparse to Python scipy csr_matrix


code:
s = 'hello world'
[s[i:i+3] for i in range(len(s)) if not i % 3]

rawDescription: Split string into array with many char pro items


code: [{'greeting': 'HELLO WORLD!'}, {'greeting': 'Hello Mars'}, {}, {}]

rawDescription: How to uniquefy a list of dicts based on percentage similarity of a value in the dicts

description: Uniquefy a list of dicts based on percentage similarity of a value in the dicts


code:
isdark = True
print('bt' if isdark else 'dasd')

rawDescription: Python3 defining room conditions

description: Python3 defining room conditions


code:
prefix, sprint = version.rsplit('.', 1)  # split just once
newversion = '{}.{}'.format(prefix, int(sprint) + 1)

rawDescription: increment a variable like */2.4.7 via python

description: Increment a variable like */2.4.7 via python


code:
import pycountry
lang = pycountry.languages.get(alpha2='aa')
lang.terminology

rawDescription: How to convert IETF BCP 47 language identifier to ISO-639-2?

description: Convert IETF BCP 47 language identifier to ISO-639-2


code:
s, t = 'acegikmoqsuwy\nbdfhjlnprtvxz', 'bdfhjlnprtvxzacegikmoqsuwy'
print(''.join((s, t)[i % 2][i // 2] for i in range(len(s + t))))

rawDescription: How you can unite-merge parts of a sliced string?


code:
replacement = "test"
original_string = "(cats && dogs) || (cows && chickens)"
new_string = re.sub(r'[^()|& ]+', replacement, original_string)

rawDescription: Replace Section of String Rather than Character Python


code:
for word in wordlist1:
    if word in filterlist:
        wordlist1.append("")

rawDescription: Exclude list of words when reading a file


code:
def add_to_dict(d, t):
    if t:
        first, rest = t[0], t[1:]
        nested = d.setdefault(first, {})
        add_to_dict(nested, rest)

rawDescription: list of lists to either autovivify or recursive data structure

description: List of lists to either autovivify or recursive data structure


code:
if __name__ == '__main__':
    from gearbox.main import GearBox
    gearbox = GearBox()
    gearbox.run(["serve", "--config=development.ini"])

rawDescription: TurboGears 2.3: debug through Eclipse

description: TurboGears 2.3: debug through Eclipse


code:
p1 = subprocess.Popen(["exit"], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["theprogram", arg1], stdin=p1.stdout)
p1.stdout.close()
p2.communicate()

rawDescription: Using pipes with the python "subprocess" module


code: LookupElement = namedtuple('LookupElement', ReadElement._fields + ('lookups',))

rawDescription: Python: Extending a predefined named tuple


code:
from subprocess import Popen, PIPE, check_output, STDOUT

def runAppleScript(script):
    "Returns the result of running string in osascript (Applescript)"

    if hasattr(script, "encode"): #Assumes Python 3
        script = script.encode("utf-8")

    osa = Popen(["osascript", "-"], stdout=PIPE, stdin=PIPE, stderr=PIPE)
    results, err = osa.communicate(script)
    if err:
        raise Exception(err)

    return results.decode("utf-8")

def runBash(command):
    output = check_output(command, stderr=STDOUT, shell=True)
    return output

def getCurrentTerminalTTYS():
    script = '''
    tell application "Terminal"
        return (tty of selected tab of front window)
    end tell
    '''
    result = runAppleScript(script)
    return result.strip()

def getPathForTTYS(ttys):
    lsof = runBash('lsof').split('\n')
    process = None
    for line in lsof:
        if ttys in line:
            process = line.split()[1]
            break
    path = None
    for line in lsof:
        if 'cwd' in line and process in line:
            path = ' '.join(line.split()[8:])
            break
    return path

def getCurrentTerminalPath():
    ttys = getCurrentTerminalTTYS()
    return getPathForTTYS(ttys)

rawDescription: Find the Current Directory of the Current Terminal Tab in Python


code:
import dis
def func(x):
    return cos(x)

dis.dis(func)

rawDescription: What is the difference between locals and globals when using Python's eval()?


code:
df = pd.DataFrame([['@a @b'], ['@a'], ['@c']], columns=['tweets'])
df
at_mentions = df['tweets'].str.findall('@[a-zA-Z0-9_]+')

rawDescription: Split a Pandas 'findall' result list into multiple items, to group by uniques


code: call(["java", "-cp", "<yourclasspath>", "-jar", "foo.jar"])

rawDescription: How to run jar files in python

description: Run jar files in python


code:
cluster_id = conn.run_jobflow(
    "Name for my cluster",
    instance_groups=instance_groups,
    action_on_failure='TERMINATE_JOB_FLOW',
    keep_alive=True,
    enable_debugging=True,
    log_uri="s3://mybucket/logs/",
    hadoop_version=None,
    ami_version="2.4.9",
    steps=[],
    bootstrap_actions=[],
    ec2_keyname="my-ec2-key",
    visible_to_all_users=True,
    job_flow_role="EMR_EC2_DefaultRole",
    service_role="EMR_DefaultRole")

rawDescription: How to launch and configure an EMR cluster using boto

description: Launch and configure an EMR cluster using boto


code:
ActionPrevious(
    size_hint_x = None,
    width = 0,
    app_icon_width = 0.1,
    with_previous = False)

rawDescription: Facing issue using Action Bar in Python kivy Application


code:
def iso2screen(self, x, y):
    xx = (x - y) * (self.SETTING['TILE_WIDTH'] / 2)
    yy = (x + y) * (self.SETTING['TILE_HEIGHT'] / 2)
    return xx, yy

rawDescription: Python pygame Detect if mouse is over non transparent part of surface


code:
from collections import defaultdict

p_2_dict = defaultdict(set)
for a, b in p_2:
    p_2_dict[a].add(b)
    p_2_dict[b].add(a)

def find_neigbours(start_vertex, levels):
    found = []
    from_vertices = [start_vertex]
    for i in range(1, levels+1):
        new_from_vertices = []
        for vertex in from_vertices:
            for neighbour in p_2_dict[vertex]:
                new_from_vertices.append(neighbour)
                found.append( (neighbour, i) )
        from_vertices = new_from_vertices
    return found

rawDescription: Most Efficient Method of Searching Neighbour Data for Nth Neighbour


code: print

rawDescription: How to print on the same line in a loop?

description: Print on the same line in a loop


rawDescription: List comprehension to return the sum of n/2...?


code:
sterm = "some word".split()
result = " ".join("<strong>%s</strong>" % w for w in sterm)

rawDescription: replace multiple words - python

description: Replace multiple words - python


code:
class MyMethod(models.Model):
    def method(self, user):
        # do something with user
        return result    # true or false

def my_method(request):
    obj = MyModel.all().filter(...).get()
    result = obj.method(request.user)
    # pass "result" to template

rawDescription: Access to user in django models

description: Access to user in django models


code:
def parse(line):
    values = line.split('/', 3)
    if len(values) < 4:
        return None
    else:
        types = [int, int, int, str]
        try:
            return tuple(type(value) for type, value in zip(types, values))
        except ValueError:
            return None

rawDescription: How to convert string input into tuple with integers and a string

description: Convert string input into tuple with integers and a string


code: df.groupby(['ID','Category'], as_index=False).agg({'Date_Field' : [min, max]})

rawDescription: establish begin/end dates via groupby

description: Establish begin/end dates via groupby


code:
#!python
from ctypes import *
from ctypes.util import find_library

# this struct will be passed as a ponter,
# so we don't have to worry about the right layout
class dl_phdr_info(Structure):
  _fields_ = [
    ('padding0', c_void_p), # ignore it
    ('dlpi_name', c_char_p),
                            # ignore the reset
  ]

# call back function, I changed c_void_p to c_char_p
callback_t = CFUNCTYPE(c_int,
                       POINTER(dl_phdr_info),
                       POINTER(c_size_t), c_char_p)

dl_iterate_phdr = CDLL('libc.so.6').dl_iterate_phdr
# I changed c_void_p to c_char_p
dl_iterate_phdr.argtypes = [callback_t, c_char_p]
dl_iterate_phdr.restype = c_int

def callback(info, size, data):
  # simple search
  if data in info.contents.dlpi_name:
    print(info.contents.dlpi_name)
  return 0

if __name__ == '__main__':
  # the target lib we want to find
  target_lib = find_library('xml2')
  print(target_lib)
  # load it
  lib = CDLL(target_lib)
  # iterate over the loaded libs
  dl_iterate_phdr(callback_t(callback), target_lib)

rawDescription: Getting the fullpath of a library using ctypes.util.find_library in Python


rawDescription: Transpose a text file to csv using Python


code:
def post(self):
    if self.request.get('approve'):
        updated_remark = self.request.get('remark' + self.request.get('approve'))
        record.remark = db.Text(updated_remark)
        record.put()

rawDescription: Dynamically generate client-side HTML form control using JavaScript and server-side Python code in Google App Engine


code:
def interval_screen():
    ...
    i_screen.after(3000, maker_screen)

def masker_screen():
    ...
    m_screen.after(3000, lambda: scale_screen(event = None))

rawDescription: Pausing from within a tkinter function

description: Pausing from within a tkinter function


code:
class LoginError(Exception):
    pass

rawDescription: How to close a login dialog and show the main window (PyQt4)

description: Close a login dialog and show the main window (PyQt4)


code:
for file in onlyfiles:
  with open(file, 'r') as f:
    d = f.read().replace('\n  world: Trolling\n','\n  world: world\n')
  with open(file, 'w') as f:
    f.write(d)

rawDescription: a program to replace a line in a .yml file(PYTHON)

description: A program to replace a line in a .yml file(PYTHON)


code:
with fetcherLock:
    fetcherProcess.terminate()

rawDescription: How to work around the Queue corruption when using Process.Terminate()

description: Work around the Queue corruption when using Process.Terminate()


code:
from collections import Counter
strings = ["bla1okay", "okay1243bla", "blaokay", "bla12okay", "okaybla"]

def substrings(s, minlength=3):
    """Finds all possible unique substrings of s, given a minimum length.

    >>> substrings("12345")
    {'1234', '234', '345', '12345', '123', '2345'}
    >>> substrings("123123")
    {'2312', '123123', '12312', '123', '23123', '1231', '231', '3123', '312'}
    >>> substrings("aaaaa")
    {'aaaaa', 'aaaa', 'aaa'}
    """
    maxsize = current = len(s)
    result = []
    while current >= minlength:
        result.extend([s[start:start+current]
                       for start in range(maxsize-current+1)])
                                  # range(5) is [0,1,2,3,4]
        current -= 1
    return set(result) # set() removes duplicates

def all_substrings(strings, minlength=3):
    """Returns the union of all the sets of substrings of a list of strings.

    >>> all_substrings(["abcd", "1234"])
    {'123', 'abc', 'abcd', '1234', 'bcd', '234'}
    >>> all_substrings(["abcd", "bcde"])
    {'abc', 'bcd', 'cde', 'abcd', 'bcde'}
    """
    result = set()
    for s in strings:
        result |= substrings(s, minlength)
        # "|=" is the set union operator
    return result

def count(strings, minlength=3):
    """Counts the occurrence of each substring within the provided list of strings,
    given a minimum length for each substring.

    >>> count(["abcd", "bcde"])
    Counter({'bcd': 2, 'bcde': 1, 'abc': 1, 'abcd': 1, 'cde': 1})
    """
    substrings = all_substrings(strings, minlength)
    counts = Counter()
    for substring in substrings:       # Check each substring
         for string in strings:        # against each of the original strings
             if substring in string:   # to see whether it is contained there
                 counts[substring] += 1
    return counts

def prune(counts, mincount=4):
    """Returns only the longest substrings whose count is >= mincount.
    First, all the substrings with a count < mincount are eliminated.
    Then, only those that aren't substrings of a longer string are kept.
    >>> prune(Counter({'bla': 5, 'kay': 5, 'oka': 5, 'okay': 5, 'la1': 2, 'bla1': 2}))
    [('okay', 5), ('bla', 5)]
    """
    # Throw out all counts < mincount. Sort result by length of the substrings.
    candidates = sorted(((s,c) for s,c in counts.items() if c >= mincount),
                        key=lambda l: len(l[0]), reverse=True) # descending sort
    result = []
    seenstrings = set()      # Set of strings already in our result
    # (we could also look directly in the result, but set lookup is faster)
    for item in candidates:
        s = item[0]          # item[0] contains the substring
        # Make sure that s is not already in our result list
        if not any(s in seen for seen in seenstrings):
            result.append(item)
            seenstrings.add(s)
    return result

counts = count(strings)
print(prune(counts))

rawDescription: Get often occuring string patterns from column in R or Python


rawDescription: Splitting a comma-separated string

description: Splitting a comma-separated string


rawDescription: Slice substrings from long strong to a list in python


rawDescription: Translating Array Manipulation in C++ to Python


code:
long_string = ('this is a really long string I want '
               'to wrap over multiple lines')

rawDescription: Python - Long string on multiple line

description: Python - Long string on multiple line


code:
class DogImageForm(ModelForm):
    dogs = forms.ModelChoiceField(queryset=Dog.objects.none())
    class Meta:
        model = ResultsUpload

    def __init__(self, user, *args, **kwargs):
        super(DogImageForm, self).__init__(*args, **kwargs)
        self.fields['dogs'].queryset = Dog.objects.filter(user=user)

rawDescription: Access request.user in modelForm

description: Access request.user in modelForm


code:
def foo():
    print("before importing module")
    import module
    module.foo()
    print("after importing module")

if __name__ == '__main__':
    foo()
    foo()

rawDescription: python import multiple times

description: Python import multiple times


code:
import multiprocessing
p = multiprocessing.Pool(2)
def f(a):
 print(a)

p.apply(f, "hi")

rawDescription: Why would it throws "'module' object has no attribute XXX" error when I call on apply_async from multiprocessing.Pool?


code: next(obj for obj in objs if obj.val==5)

rawDescription: find first list item that matches criteria

description: Find first list item that matches criteria


code:
all_points = range(100)
slice_size = 3
sliced_points = zip(* [iter(all_points)]*slice_size)

rawDescription: Python: apply slice values within a callable function


code:
import MyLib.Module as mm

mm._a()
mm._b()

rawDescription: Import symbols starting with underscore

description: Import symbols starting with underscore


code: "WHERE has = '401k:T' and has = 'health_in:F'"

rawDescription: How to Make Efficient Google-App-Engine Schema with Lots of BooleanPropertys


code: print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

rawDescription: How to print from a loop onto a single line

description: Print from a loop onto a single line


rawDescription: Python - How to sort a dictionary based on its value as well it key


code:
import Tkinter as tk
import random

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.canvas = tk.Canvas(self, background="bisque")
        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
        self.hsb = tk.Scrollbar(self, orient="horizontal", command=self.canvas.xview)
        self.canvas.configure(xscrollcommand=self.hsb.set, yscrollcommand=self.vsb.set)

        self.canvas.grid(row=0, column=0, sticky="nsew")
        self.vsb.grid(row=0, column=1, sticky="ns")
        self.hsb.grid(row=1, column=0, sticky="ew")

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

        for i in range(100):
            x = random.randint(0, 1000)
            y = random.randint(0, 1000)
            width = random.randint(10, 50)
            height = random.randint(10, 50)
            fill = random.choice(("red", "orange", "yellow", "green", "blue", "violet"))
            self.canvas.create_rectangle(x, y, x+width, y+height, fill=fill)

        self.canvas.configure(scrollregion = self.canvas.bbox("all"))

        self.canvas.bind("<1>",     lambda event: self.canvas.focus_set())
        self.canvas.bind("<Left>",  lambda event: self.canvas.xview_scroll(-1, "units"))
        self.canvas.bind("<Right>", lambda event: self.canvas.xview_scroll( 1, "units"))
        self.canvas.bind("<Up>",    lambda event: self.canvas.yview_scroll(-1, "units"))
        self.canvas.bind("<Down>",  lambda event: self.canvas.yview_scroll( 1, "units"))

        self.canvas.focus_set()

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

rawDescription: Tkinter: How to scroll an entire canvas using arrow keys?


code:
def close_directory_handle(handle):
    rqIndexLck.acquire()
    rqset4handle = rqIndex.get(handle, None)
    if rqset4handle is not None:
        for rq in rqset4handle:
            rq.CancelIo()
        del rqIndex[handle]
    if handle in handle2file:
        del handle2file[handle]
    rqIndexLck.release()

rawDescription: Watchdog compatibility: A workaround for "CancelIoEx"


code:
foo= u'\u65E5\u672C\u8A9E' # 日本語
url= 'http://www.example.jp/something?foo='+urllib.quote(foo.encode('cp932'))

rawDescription: how to open a URL with non utf-8 arguments

description: Open a URL with non utf-8 arguments


rawDescription: Count occurrences of items in Series in each row of a DataFrame


code:
# Use lexsort to bring duplicate coo XY's in succession
sortidx = np.lexsort(coo.T)
sorted_coo =  coo[sortidx]

# Get mask of start of each unique coo XY
unqID_mask = np.append(True,np.any(np.diff(sorted_coo,axis=0),axis=1))

# Tag/ID each coo XY based on their uniqueness among others
ID = unqID_mask.cumsum()-1

# Get unique coo XY's
unq_coo = sorted_coo[unqID_mask]

# Finally use bincount to get the summation of all coo within same IDs
# and their counts and thus the average values
average_values = np.bincount(ID,values[sortidx])/np.bincount(ID)

rawDescription: Numpy: Average of values corresponding to unique coordinate positions


rawDescription: Pandas. Selection by label. One-row output


code:
class Employee(object):
    def __init__(self, initial_data):
        for key in initial_data:
            setattr(self, key, initial_data[key])

rawDescription: Set attributes from dictionary in python


rawDescription: Selecting only some values from dictionaries in list


code:
workbook.save(response)
response = HttpResponse(content_type='application/ms-excel')
response['Content-Disposition'] = "attachment; filename=%s" % xls_name

rawDescription: Downloading a file with django

description: Downloading a file with django


code:
import threading

t = threading.Timer(30,func,args=[])
t.start()

rawDescription: How to wait for an input without blocking timer in python?

description: Wait for an input without blocking timer in python


code:
with open('file.txt', 'w') as f:
    for indexes, value in np.ndenumerate(a):
        x,y,z = indexes
        f.write('{} {} {} {}\n'.format(x,y,z,value))

rawDescription: Writing a 3D numpy array to files including its indices


code:
import numpy as np
x = np.logspace(-3, 3, 11)

rawDescription: logarithmic spaced values below 1

description: Logarithmic spaced values below 1


code:
ids = next(entry['id'] for entry in stop_list if entry['name'] == name)
next(ids)
next(ids)

rawDescription: How to get a value from a list of dictionary Python?

description: Get a value from a list of dictionary Python


code: print(*mister, sep='\n')

rawDescription: How to use one print() in python to print list items line by line - not in one streak?

description: Use one print() in python to print list items line by line - not in one streak


code: first_elem = output[0]

rawDescription: Python Json loads() returning string instead of dictionary?


code:
def ed_xed(self,arg):
    ip = self.api
    return ip.magic.im_class.magic_edit(ip.IP," -x %s "%arg)

ip.expose_magic('xed',ed_xed)

rawDescription: In IPython how do I create aliases for %magics?


code:
with open('mirnaduplicates.csv', "rb") as ifile, \
     open('mirnaduplicatesremoved.csv', 'wb') as fp:
    reader = csv.reader(ifile)
    writer = csv.writer(fp, delimiter=' ')

    seen = set()
    seen_add = seen.add
    writer.writerows(row for row in reader if row[0] not in seen and not seen_add(row[0]))

rawDescription: csv writer is adding delimiters in each words..

description: Csv writer is adding delimiters in each words..


code:
import pygame

# --- constants ---

BLACK = (  0,  0,  0)
RED   = (255,  0,  0)

# --- classes ---

class Label():

    def __init__(self, text, x, y, width):

        font = pygame.font.SysFont(None, 35)

        # generate full image
        self.image = font.render(text, True, RED)
        self.rect = self.image.get_rect(x=x, y=y)

        self.width = width
        self.speed = 1

        # offset of visible part
        self.offset_x = 0
        self.max_offset_x = self.rect.width - self.width

        # visible part of image as 'rect' - 'width' is always the same
        self.sub_image_rect = self.image.get_rect(x=0, width=self.width) # changed

    def move(self):

        # change offset
        self.offset_x += self.speed

        # change move direction
        if self.offset_x < 0:
            self.offset_x = 0
            self.speed = -self.speed
        elif self.offset_x > self.max_offset_x:
            self.offset_x = self.max_offset_x
            self.speed = -self.speed

        # visible part of image - change only `x`
        self.sub_image_rect.x = self.offset_x  # changed

    def draw(self, surface):
        surface.blit(self.image, self.rect, area=self.sub_image_rect)  # changed

# --- main ---

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

label = Label("Hello World of Python and Pygame!", 100, 100, 200)

# --- mainloop ---

fps = pygame.time.Clock()
running = True

while running:

    # --- events ---

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key ==  pygame.K_ESCAPE:
                running = False

    # --- updates ---

    label.move()

    # --- draws ---

    screen.fill(BLACK)

    label.draw(screen)

    fps.tick(25)
    pygame.display.flip()

# --- the end ---

pygame.quit()

rawDescription: Assign static Mask on moving Surface with PyGame


code:
import dis
dis.dis(foo)

rawDescription: Why is assigning to multiple targets (identifier/attribute) producing strange results?


code:
from celery.loaders import current_loader
current_loader().init_worker()

rawDescription: How to get all tasks and periodic tasks in Celery

description: Get all tasks and periodic tasks in Celery


code:
'||'.join(['eggs','and','ham'])
# returns 'eggs||and||ham'

rawDescription: Python word length function example needed


code:
autocorrect(['Abc',  ('Def',  'Ghi'),  'Jkl'])
autocorrect(['ABC', ("DEF", "GGGG"), "JKL"])
autocorrect(['abC', 'gggg', 'jkL'])

rawDescription: Auto-correct case spelling of a word database


code:
a = [1, 2, 3, 4]
for i in a:
    i = str(i)
a
b = [str(i) for i in a]
b

rawDescription: Change type of group of objects

description: Change type of group of objects


code:
# only take .txt files from the directory tree
files = [(path,f) for path,_,file_list in os.walk(root) for f in file_list if f.endswith('.txt')]

rawDescription: Python text files merge from subdirectory


code:
p = request.user.profile
p.about = 'abc'
p.save()

rawDescription: Getting logged in User's Profile in Django without repeating request.user a lot?


code:
import pandas as pd

data = pd.Series(range(1, 9))

data_mean = pd.rolling_mean(data, window=5).shift(-2)
print(data_mean)

rawDescription: Rolling mean with customized window with Pandas


rawDescription: Sort words by their usage

description: Sort words by their usage


code:
@decorate_first_arg
def apply_first(x):
    def decorate(f):
        def g(*args):
            return f(*((x,) + args))
        return g
    return decorate

rawDescription: How to write a decorator to set the first argument?

description: Write a decorator to set the first argument


code: Apple.objects.order_by('a', 'b', '-date').distinct('a', 'b')

rawDescription: Django 1.9: Create complex custom filter method for QuerySet


code: result[1].insert(1, 'O')

rawDescription: Adding (inserting) array to dict

description: Adding (inserting) array to dict


code:
import functools
d = defaultdict(functools.partial(myobj, param1))

rawDescription: Passing a parameter to objects created by defaultdict


rawDescription: Python Xpath: lxml.etree.XPathEvalError: Invalid predicate


rawDescription: Python - get variables from file using regular expressions


code:
from Tkinter import *

def key_press_wrapper():
    your_function1()
    your_function2()
root=Tk()
root.bind('<Return>',key_press_wrapper)

rawDescription: Python Tkinter: Binding Same Key Twice?

description: Python Tkinter: Binding Same Key Twice


rawDescription: python-taking part of the text stored within a combo box

description: Python-taking part of the text stored within a combo box


rawDescription: Boolean evaluation in a lambda

description: Boolean evaluation in a lambda


code:
class AutoContextMixin:

    def get_context_data(self, **kwargs):
        context = super(AutoContextMixin, self).get_context_data(**kwargs)
        for key in dir(self):
            value = getattr(self, key)
            if isinstance(value, str) and not key.startswith('_'):
                context[key] = value
        return context

class FooUpdate(AutoContextMixin, UpdateView):
    model = Foo
    page_title = 'foo-objects name should go here'

rawDescription: Get object attribute in class based view


code:
def remaining(self):
  return self.quantity - len( self.buyers.all() )

rawDescription: Django: How do I count the number of objects related to another in the database?


rawDescription: How to arrange plots of secondary axis to be below plots of primary axis in matplotlib?

description: Arrange plots of secondary axis to be below plots of primary axis in matplotlib


rawDescription: Please help me understand a bit shift operation


code:
def permute2(it):
    return itertools.chain.from_iterable(
        permute(p)
        for p in itertools.permutations(it)
    )

rawDescription: Python permutations of both sequence and subsequences