code:
# Concatenate all elements into a 2D array
all_arr = np.concatenate(li).reshape(-1,li[0].size)

# Reduce each row with IDs such that each they represent indexing tuple
ids = np.ravel_multi_index(all_arr.T,all_arr.max(0)+1)

# Tag each such IDs based on uniqueness against other IDs
_,unqids,C = np.unique(ids,return_inverse=True,return_counts=True)

# Sort the unique IDs and split into groups for final output
sidx = unqids.argsort()

# Mask corresponding to unqids that has ID counts > 1
mask = np.in1d(unqids,np.where(C>1)[0])

# Split masked sorted indices at places corresponding to cumsum-ed counts
out = np.split(sidx[mask[sidx]],C[C>1].cumsum())[:-1]

rawDescription: Find indexes of equal numpy 2D rows

description: Find indexes of equal numpy 2D rows


code: div.results > div.item

rawDescription: Can't get any data with Scrapy

description: Can't get any data with Scrapy


code:
obj = []
for i in range(100):
    name=input('Your name:')
    obj.append(Person(name))

rawDescription: Python - how to connect word with iteration


code:
data = []

while True:
    name = raw_input("Pigment name [DONE to finish]: ")
    # ....

with open('spectral_data.csv', 'ab') as f:
    writer = csv.DictWriter(f, ('name', 'data'))
    writer.writerows(data)

rawDescription: Python 2.7 exporting data in a .csv

description: Python 2.7 exporting data in a .csv


code:
import sys
sys.path.append('C:/full/path')
from foo import util,bar

rawDescription: Import python module NOT on path

description: Import python module NOT on path


code:
self.label = None  # Initialize `self.label` as None somewhere.

...

if not self.label:
    self.label = Label(self, text="Label")

rawDescription: How to control if a component exists in a Tk object/window?

description: Control if a component exists in a Tk object/window


code:
class MyModel(EndpointsModel):
  _parent = None

  attr1 = ndb.StringProperty()
  attr2 = ndb.StringProperty()
  created = ndb.DateTimeProperty(auto_now_add=True)

rawDescription: Setting parent key but not child key while using endpoint-proto-datastore


code:
get_data_from_table.sort() # only if not already sorted
tables = [[key] + [g[1] for g in groups]
          for (key, groups) in itertools.groupby(get_data_from_table,
                                                 key=operator.itemgetter(0))]

rawDescription: Getting all columns in a list with the same tablename


code: subprocess.Popen(['R', 'CMD', 'BATCH', '/path/to/script.R'])

rawDescription: How do I start a subprocess in python and not wait for it to return

description: Start a subprocess in python and not wait for it to return


rawDescription: Select rows from a DataFrame based on values in a column in pandas


code:
with open('input') as fin, open('output','w') as fout:
    for line in fin:
        fout.write(line)
        if line == 'xxxxx\n':
           next_line = next(fin)
           if next_line == 'yyyyy\n':
              fout.write('my_line\n')
           fout.write(next_line)

rawDescription: How to write to a specific line in file in Python?

description: Write to a specific line in file in Python


code:
import os.path
import shutil
import sys
import tempfile
from zipfile import ZipFile

PREVIEW_PATH = 'QuickLooks/Preview.pdf'  # archive member path
pages_file = input('Enter the path to the .pages file in question: ')
#pages_file = r'C:\Stack Overflow\extract_test.pages'  # hardcode for testing
pages_file = os.path.abspath(pages_file)
filename, file_extension = os.path.splitext(pages_file)
if file_extension == ".pages":
    tempdir = tempfile.gettempdir()
    temp_filename = os.path.join(tempdir, PREVIEW_PATH)
    with ZipFile(pages_file, 'r') as zipfile:
        zipfile.extract(PREVIEW_PATH, tempdir)
    if not os.path.isfile(temp_filename):  # extract failure?
        sys.exit('unable to extract {} from {}'.format(PREVIEW_PATH, pages_file))
    final_PDF = filename + '.pdf'
    shutil.copy2(temp_filename, final_PDF)  # copy and rename extracted file
    # delete the temporary subdirectory created (along with pdf file in it)
    shutil.rmtree(os.path.join(tempdir, os.path.split(PREVIEW_PATH)[0]))
    print('Check out the PDF! It\'s located at "{}".'.format(final_PDF))
    #view_file(final_PDF)  # see Bonus below
else:
    sys.exit('Sorry, that isn\'t a .pages file.')

rawDescription: How to extract a file within a folder within a zip in python?

description: Extract a file within a folder within a zip in python


code:
#import Beautiful Soup
import bs4
#get text in document
text = editor.getText()
#soupify it to fix XML
soup = bs4.BeautifulSoup(text)
#convert soup object to string again
text = str(soup)
#clear editor and replace bad xml with fixed xml
editor.clearAll()
editor.addText(text)
#change language to xml
notepad.menuCommand( MENUCOMMAND.LANG_XML )
#soup has its own prettify, but I like the XML tools version better
notepad.runMenuCommand('XML Tools', 'Pretty print (XML only - with line breaks)', 1)

rawDescription: How to auto-close xml tags in truncated file?

description: Auto-close xml tags in truncated file


code:
from itertools import permutations, product, ifilter

no_no_data = lambda v: v != 'No Data'
dict_corresp_media = {}

for a, b in permutations(dict_STU.iterkeys(), r=2):
    # retrieve the a and b lists of possible keys, for which we need to loop over their products
    # we filter each for the `No Data` keys
    aval, bval = ifilter(no_no_data, dict_STU[a]), ifilter(no_no_data, dict_STU[b])

    matches = [allVSall[c1, c2] for c1, c2 in product(aval, bval) if (c1, c2) in allVSall]
    dict_corresp_media['{}_{}'.format(a, b)] = sum(matches) / len(matches) if matches else 0

rawDescription: Computation time insanely long in python


rawDescription: Splitting a string with multiple delimiters and conditions


rawDescription: Python: How do I find the complement of a list?


code: addRectFilled(pict, x+100, (h-(maxLength/10))-volumes[n], 30, volumes[n], myColourList[n])

rawDescription: How to colour maixum and minimun of each bar in bar graph in python?

description: Colour maixum and minimun of each bar in bar graph in python


rawDescription: Python Auto Fill with Mechanize

description: Python Auto Fill with Mechanize


code:
import re

import pytest

def __get_pod(hostname):  # dummy impl.
    hostname = hostname.split('.', 1)[0]
    if '-' not in hostname:
        return 'Unknown'
    hostname = re.sub(r'\d+', '', hostname)
    return hostname.rsplit('-', 1)[0]

@pytest.mark.parametrize(['hostname', 'expected'], [
    ["web01-east.domain.com", 'web'],
    ["redis01-master-east.domain.com", 'redis-master'],
    ["web01.domain.com", 'Unknown'],
])
def test__get_pod(hostname, expected):
    assert __get_pod(hostname) == expected

rawDescription: py.test multiple tests for different results

description: Py.test multiple tests for different results


code: image = r.content

rawDescription: python keep-alive connection and download image

description: Python keep-alive connection and download image


code:
import numpy as np
quarters = np.array([200712, 200803, 200806, 200809, 200812, 200903])
months = np.arange(200801, 200813)
loc = np.searchsorted(months, quarters)

rawDescription: Equivalent of Matlab 'ismember' in numpy (Python)?


code:
i=0
offset = 1
while i <len(str_numbers):
    if offset>1:
        int_number = int(str_numbers[i:i+offset])
    else:
        int_number = int(str_numbers[i])
    if i==5:
        offset=2
    i+=offset

rawDescription: Python - Looping through a string pulling 1 character at a time and then 2


code:
def create(self, validated_data):
    user_data = validated_data.pop('user')
    user = User(
        email=user_data['email'],
        username=user_data['username']
    )
    user.set_password(user_data['password'])
    user.save()
    caretaker = Caretaker.objects.create(**validated_data)
    return caretaker

rawDescription: How do I create objects with nested items using serializers in Django REST

description: Create objects with nested items using serializers in Django REST


code: map(tz.compute_local_time, lat, lon, time)

rawDescription: Passing class into iPython Parallel

description: Passing class into iPython Parallel


code: math.sqrt(sum((a[k] - b[k])**2 for k in set(a.keys()).intersection(set(b.keys()))))

rawDescription: Calculate euclidean distance between two vector (bag of words) in python


rawDescription: separate a list inside a list

description: Separate a list inside a list


code:
Counter(u'不')
repr(u'不')

rawDescription: Word-counter won't print foreign characters


code:
def call_ws_1(group_id):
    return list(range(20))

def call_ws_2(host):
    sum(range(33000000))  # CPU-bound
    #time.sleep(1)  # I/O-bound
    return "{} property".format(host)

rawDescription: Python : efficiency concerns in parallel async calls to fetch data from web services


code: (v for k,v in a.items() if k[2] == 'l1p')

rawDescription: Get elements of a tuple-indexed dictionary specifying only one field of the tuple


code:
class Interface(tk.Frame):
    def __init__(self, master, app):
        ...
        self._app = app
        ...
    def clearall(self):
        self._app.clearall()

class App(object):
    def __init__(self, master):
        ...
        self._canvas = tk.Canvas(master, bg='white')
        self._interface = Interface(master, self)
        ...
    def clearall(self):
        self._calvas.delete(tk.ALL)

rawDescription: Conveying a tkinter button's execution across different classes?


rawDescription: How to compare dates from Excel sheets for value filling purposes on python

description: Compare dates from Excel sheets for value filling purposes on python


code:
y_and_data = np.genfromtxt('131014-data-xy-conv-1.txt', names=True, delimiter=',')
x = np.array(y_and_data.dtype.names[1:], int)
y = y_and_data['YX_mm']
data = y_and_data.view(np.float).reshape(-1, len(y_and_data.dtype))[:,1:]

rawDescription: How to manipulate the data after numpy.loadtxt?

description: Manipulate the data after numpy.loadtxt


code:
from pyspark.mllib.linalg import Vectors, SparseVector, DenseVector
import numpy as np

def add(v1, v2):
    """Add two sparse vectors
    >>> v1 = Vectors.sparse(3, {0: 1.0, 2: 1.0})
    >>> v2 = Vectors.sparse(3, {1: 1.0})
    >>> add(v1, v2)
    SparseVector(3, {0: 1.0, 1: 1.0, 2: 1.0})
    """
    assert isinstance(v1, SparseVector) and isinstance(v2, SparseVector)
    assert v1.size == v2.size
    # Compute union of indices
    indices = set(v1.indices).union(set(v2.indices))
    # Not particularly efficient but we are limited by SPARK-10973
    # Create index: value dicts
    v1d = dict(zip(v1.indices, v1.values))
    v2d = dict(zip(v2.indices, v2.values))
    zero = np.float64(0)
    # Create dictionary index: (v1[index] + v2[index])
    values =  {i: v1d.get(i, zero) + v2d.get(i, zero)
       for i in indices
       if v1d.get(i, zero) + v2d.get(i, zero) != zero}

    return Vectors.sparse(v1.size, values)

rawDescription: How to add two Sparse Vectors in Spark using Python

description: Add two Sparse Vectors in Spark using Python


code:
class MyYql(yql.Public):

    def __init__(self, api_key=None, shared_secret=None, httplib2_inst=None, env=None):
        super(MyYql, self).__init__(api_key, shared_secret, httplib2_inst)
        self.env = env if env else None

    def execute(self, query, params=None, **kwargs):
        kwargs["env"] = kwargs.get("env", self.env)
        return super(MyYql, self).execute(query, params, **kwargs);

rawDescription: YQL - No definition found for Table

description: YQL - No definition found for Table


rawDescription: SQLAlchemy Match Against clause

description: SQLAlchemy Match Against clause


rawDescription: How do I force recv() in Socket to NOT convert my hex values into ASCII if it can (python)

description: Force recv() in Socket to NOT convert my hex values into ASCII if it can (python)


code:
def setencoding():
    """Set the string encoding used by the Unicode implementation.  The
    default is 'ascii', but if you're willing to experiment, you can
    change this."""

    encoding = "ascii" # Default value set by _PyUnicode_Init()
    if 0:
        # Enable to support locale aware default string encodings.
        import locale
        loc = locale.getdefaultlocale()
        if loc[1]:
            encoding = loc[1]
    if 0:
        # Enable to switch off string to Unicode coercion and implicit
        # Unicode to string conversion.
        encoding = "undefined"
    if encoding != "ascii":
        # On Non-Unicode builds this will raise an AttributeError...
        sys.setdefaultencoding(encoding) # Needs Python Unicode build !

rawDescription: Persist UTF-8 as default encoding

description: Persist UTF-8 as default encoding


code: messages = [(msg.id, msg.string) for msg in cat if msg.id]

rawDescription: Storing a text file into SQLite3 database using python


code: df.pivot_table(columns=['goods', 'category'], index='month', values='stock')

rawDescription: Pandas Multi-indexing from a Flatten DataFrame


code:
@app.route('/simple/<contract_id>/page', methods=['GET', 'POST'])
def contracts(contract_id):
    result = dbs.Contracts.select().where(dbs.Contracts.id == contract_id).first()
    return render_template('contracts.html', result=result)

rawDescription: How to use python flask routes with html

description: Use python flask routes with html


rawDescription: matplotlib: Don't compress plot in the horizontal direction

description: Matplotlib: Don't compress plot in the horizontal direction


code: sorted(((k1, k2) for k1 in d for k2 in d[k1]), key=lambda t: d[t[0]][t[1]], reverse=True)

rawDescription: Sort nested dictionary python 2.7, by deepest value, return key tuples


code: np.unravel_index(idxs, arr.shape)[0]

rawDescription: How do I sort the rows of a 2d numpy array based on indices given by another 2d numpy array

description: Sort the rows of a 2d numpy array based on indices given by another 2d numpy array


code:
def parse_page(self, response):
    # do some processing
    return scrapy.Request("http://www.example.com/otherpage",
        meta={'cookiejar': response.meta['cookiejar']},
        callback=self.parse_other_page)

rawDescription: Scrapy - how to manage cookies/sessions

description: Scrapy - how to manage cookies/sessions


code:
res=obj.lookup_rws()
pprint(res)

rawDescription: How to get WhoIs info by IP in Python 3?

description: Get WhoIs info by IP in Python 3


code:
#!/usr/bin/python
import os
virtenv = os.environ['OPENSHIFT_PYTHON_DIR'] + '/virtenv/'
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
try:
    execfile(virtualenv, dict(__file__=virtualenv))
except IOError:
    pass

from yourdjproject.wsgi import application

rawDescription: Deploying a local django app using openshift


code:
class A(object):

    def __getattribute__(self, attrname):
        try:
            attr = object.__getattribute__(self, attrname)
        except AttributeError as e:
            raise e

        if attrname == 'users':
            attr = attr.get()

        return attr

rawDescription: How to emulate .users.get in Python class?

description: Emulate .users.get in Python class


code: b = [c[1::2] for c in a]

rawDescription: transfer every second item in list (python)

description: Transfer every second item in list (python)


code: fig, ax = plt.subplots(figsize=[3.375, 3.375])

rawDescription: How to specify size (dimension) of .eps when exported from matplotlib using savefig()

description: Specify size (dimension) of .eps when exported from matplotlib using savefig()


rawDescription: Protection against downloading too big files


code:
while i<100:
    i+=1
    set1 = random.sample(numbers1,5)
    while len(set1) != len(set(set1)):
        print('duplicate(s) found, random selection repeated')
        set1 = random.sample(numbers1,5)

rawDescription: Python failure to find all duplicates

description: Python failure to find all duplicates


code:
tu = (('aa',), ('bb',), ('cc',))
import itertools
list(itertools.chain(*tu))

rawDescription: How do I convert tuple of tuples to list in one line (pythonic)?

description: Convert tuple of tuples to list in one line (pythonic)


code:
import inspect
inspect.getmembers(MyClass, predicate=inspect.ismethod)

rawDescription: How to convert a Python file docstring's to HTML

description: Convert a Python file docstring's to HTML


code:
def create_profile(user, is_new=False, *args, **kwargs):
    if is_new:
        # create a profile instance for the given user
        create_user_profile(user)

rawDescription: django social-auth multiple account association

description: Django social-auth multiple account association


code:
import numpy as np
import pandas as pd
import string
import itertools as IT

columns = iter(string.letters)
dfs = []
for i in range(3):
    d1 = pd.DataFrame( np.random.randn(15, 4),
                           columns = list(IT.islice(columns, 4)),
                           index = pd.DatetimeIndex(start = '01/01/2001',
                                                    freq = 'b',
                                                    periods = 15))
    dfs.append(d1)

ind = range(0, 10, 2)
my_index = d1.index[ind].copy()
print(select_join(dfs, my_index))

rawDescription: pandas concat DataFrame on different Index

description: Pandas concat DataFrame on different Index


code:
import collections, csv

with open('myfile.csv', 'rb') as f:
    rows = [x for x in csv.reader(f)]
    count = collections.Counter([x[0] for i, x in enumerate(rows) if i > 0])
    for row in rows:
        if count.get(row[0], 0) > 3:
            print(row)

rawDescription: Identify values in column that occur more than once


rawDescription: Pandas Ordered Histogram Matrix

description: Pandas Ordered Histogram Matrix


code: d = {name:foo(name,100, True) for name in desired_names}

rawDescription: Generating objects in python 3.4.3

description: Generating objects in python 3.4.3


code:
import random
randomPlayer = random.choice(players)

rawDescription: How can I create a list of players in python

description: Create a list of players in python


code: if a & b: myChain = myChain(a,b)

rawDescription: Python using function with if

description: Python using function with if


code: term = urllib.quote("'%s'" % (term,))

rawDescription: Enclose a variable in single quotes in Python


code:
instance = getattr(rsyslog, 'instance{}'.format(this_instance_number))
setattr(instance, attribute, variable)

rawDescription: How to use string formatting to dynamically assign variables

description: Use string formatting to dynamically assign variables


rawDescription: keys with multiple values in a dictionary (Python)

description: Keys with multiple values in a dictionary (Python)


code:
import copy
new_list_copy = copy.deepcopy(old_list)

rawDescription: Python make list copy, not reference

description: Python make list copy, not reference


code:
def cut_insignificant_digits(number, places):
    '''cut the least significant decimal digits of a number,
    leave only [places] decimal digits'''
    if  type(number) != float: return number
    number_as_str = str(number)
    end_of_number = number_as_str.find('.')+places+1
    if end_of_number > len(number_as_str): return number
    return float(number_as_str[:end_of_number])

def cut_insignificant_digits_lazy(iterable, places):
    for obj in iterable:
        yield cut_insignificant_digits_recursively(obj, places)

def cut_insignificant_digits_recursively(obj, places):
    '''return a copy of obj except that every float loses its least significant
    decimal digits remaining only [places] decimal digits'''
    t = type(obj)
    if t == float: return cut_insignificant_digits(obj, places)
    if t in (list, tuple, set):
        return t(cut_insignificant_digits_lazy(obj, places))
    if t == dict:
        return {cut_insignificant_digits_recursively(key, places):
                cut_insignificant_digits_recursively(val, places)
                for key,val in obj.items()}
    return obj

rawDescription: assertAlmostEqual in Python unit-test for collections of floats

description: AssertAlmostEqual in Python unit-test for collections of floats


code:
import gc

class Object(object):
    def __init__(self):
        self.speed = 0

    def set_all_speed(self, value):
        for instance in (obj for obj in gc.get_referrers(self.__class__) if isinstance(obj, self.__class__)):
            instance.speed = value

rawDescription: Changing variables in multiple Python instances


code:
import numpy as np
values = np.array([10.0, 30.1, 50, 40, 20])
order=[0, 3, 1, 4, 2]
sorted_array=values[order]
sorted_array

rawDescription: How to sort a numpy array into a specific order, specified by a separate list, based on the relative sizes of the values in the array

description: Sort a numpy array into a specific order, specified by a separate list, based on the relative sizes of the values in the array


rawDescription: Displaying 6.5235375356299998e-07 without exponential notation


rawDescription: python unique list based on item

description: Python unique list based on item


code:
random.seed(1)
uuid += random.choice(hexdigits)

rawDescription: Jython random module produces different results to cpython


code: from GP import class1, class2, class3

rawDescription: how to import only the class methods in python

description: Import only the class methods in python


code:
int('42')
int('fortytwo')

rawDescription: Python simple number comparison

description: Python simple number comparison


code:
"Student Student","quiz1: grade","quiz2: grade"
"Student Student","quiz1: grade","quiz2: grade"

rawDescription: I'm creating a quiz which is for primary school students


code:
from collections import defaultdict
def set_with_path(d, path, val):
    end = path.pop()
    for k in path:
        d = d.setdefault(k, {})
    d[end] = val

rawDescription: Python array from set of keys

description: Python array from set of keys


code: MySet(['baz', 'foo', 'bar'])

rawDescription: Python, subclassing immutable types

description: Python, subclassing immutable types


code:
ax.set_xlim(0, .05)
ax.set_ylim(-2, 2)
plt.draw()

rawDescription: Is it possible to speed up interactive IPython Notebook plots by not generating new figures every time?


rawDescription: Creating a local variable to represent a range of numbers within a xpath tr[ ]?


code:
p = subprocess.Popen(['cat'], bufsize=1, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write('hello world\n')
p.stdout.readline()
p.stdout.readline()        # THIS CALL WILL BLOCK

rawDescription: Persistent python subprocess

description: Persistent python subprocess


code:
a = np.arange(27)
max(a)
np.max(a)

rawDescription: What does numpy.max function do?

description: What does numpy.max function do


code:
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler

# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

# Create server
server = SimpleXMLRPCServer(("localhost", 8000),
                            requestHandler=RequestHandler)
server.register_introspection_functions()

# Register pow() function; this will use the value of
# pow.__name__ as the name, which is just 'pow'.
server.register_function(pow)

# Register a function under a different name
def adder_function(x,y):
    return x + y
server.register_function(adder_function, 'add')

# Register an instance; all the methods of the instance are
# published as XML-RPC methods (in this case, just 'div').
class MyFuncs:
    def div(self, x, y):
        return x // y

server.register_instance(MyFuncs())

# Run the server's main loop
server.serve_forever()

rawDescription: PHP scripts that combined with Python modules


rawDescription: Split many lists into 2 sections which contain strings and numbers


rawDescription: List sorts correctly one time, then the subsequent times it sorts incorrectly


code:
f = open("file.txt", "w")
for i in email_addresses:
    f.write(i + "\n")

rawDescription: Write query rows in csv python

description: Write query rows in csv python


code:
A = numpy.array([0,1,2],'uint32')
B = numpy.array([1,2,3],'uint32')
A-B
A = A.astype(int)
B = B.astype(int)
A-B

rawDescription: Subtracting 3D numpy arrays in Python Vs Matlab


code:
new_items = []
for item in pos_list:
    if not any(x[0] == item[0] for x in expos_list):
        print("Different chromosome")
        new_items.append(item)
    elif any(x[1] < item[1] < x[2] or x[1] < item[2] < x[2]
             for x in expos_list):
        print("Discard")
    else:
        print("New")
        new_items.append(item)
expos_list.extend(new_items)
print(expos_list)

rawDescription: Python - comparing sublists within two different lists


code: ex_method(arg1, arg2, **kw)

rawDescription: Pythonic way to pass around many arguments


rawDescription: Finding multiple occurrences of dictionary?


code:
for o in list_of_four_o_mocks:
    o.mocked_method()
self.assertEqual(o.mocked_method.call_count, 4)

rawDescription: How to MagicMock a list of object with method and get assert count


code:
names = []
for friend in friends:
    if friend['type'] == 0:
        names.append(friend[u'name'])

rawDescription: Counting lines in python without using a textfile


rawDescription: How many elements may python dictionary hold?


code:
def fib(n, depth=1):
    if n == 0 or n == 1:
        return (1, depth)
    else:
        result1, depth1 = fib(n-1, depth+1)
        result2, depth2 = fib(n-2, depth+1)
        return (result1 + result2, max(depth1, depth2))

rawDescription: how to I get the total number of stack frames used in recursion in Python?


code:
import re
links = re.findall('(https?://\S+)', text)

rawDescription: Translating PHP’s preg_match_all to Python


code:
d = dict(m_out[:,::-1])
[(k, d[k]) for k in eqn_out if k in d]

rawDescription: Using Python List Comprehension to match values that exist in a 2d array and simple list


rawDescription: Python Selenium: Can't find the login element of costar.com


code:
import operator

class MyNestedClass(object):
    def __init__(self):
        self.att1 = 5.

class MyClass(object):
    def __init__(self):
        self.my_nested_inst = MyNestedClass()

def get_nested_attr(vars_dict, attrs):
    inst = vars_dict[attrs[0]]
    return operator.attrgetter('.'.join(attrs[1:]))(inst)

def set_nested_attr(vars_dict, attrs, value):
    setattr(get_nested_attr(vars_dict, attrs[0:-1]), attrs[-1], value)

my_inst = MyClass()
my_list = ['my_inst','my_nested_inst','att1']

assert(my_inst.my_nested_inst.att1 == 5.)
set_nested_attr(vars(), my_list, 10.)
assert(my_inst.my_nested_inst.att1 == 10.)

rawDescription: Use a dict to access nested instances of classes in Python


code:
def recursive_iterator(iterable):
    for item in iterable:

        # directly iterable types:
        if type(item) in (list, tuple, set, frozenset):
            for child_item in recursive_iterator(item):
                yield child_item

        # other iterable types where we do not want to iterate over the item directly:
        elif type(item) in (dict,):
            for child_item in recursive_iterator(item.values()):
                yield child_item

        # not iterable types which we want to return as they are:
        else:
            yield item

rawDescription: how to Iterate in exhaustively in python 2?

description: Iterate in exhaustively in python 2


code:
uniqueWords = []
for i in allWords:
      if not i in uniqueWords:
          uniqueWords.append(i);

return uniqueWords

rawDescription: finding all unique words from a list using loops

description: Finding all unique words from a list using loops


code:
with open('tobefixed.txt') as f:
    data = f.read()

for search_txt, replacement_txt in Label.iteritems():
    data = data.replace(search_txt, replacement_txt)

with open('fixed.txt', 'w') as f:
    f.write(data)

rawDescription: Search and replace between two files with search phrase in first file: Python


code:
for eachitem in dicta:
    salary = eachitem['SALARY']
    eachitem.clear()
    eachitem['SALARY'] = salary

rawDescription: deleting element from python dictionary

description: Deleting element from python dictionary


rawDescription: Strip special characters from string, retain, alpabets, numbers and punctuation marks


code:
df
import itertools as it
[list(it.combinations(t[1:][0], 2)) for t in df.groupby('A').B if len(t[1:][0])>1]

rawDescription: pandas: how to check for repeated values in one column and create a pairwise list of values from another column

description: Pandas: how to check for repeated values in one column and create a pairwise list of values from another column


code:
import pandas as pd
import seaborn as sns
tips = sns.load_dataset("tips")

def plot_good_tip(day, total_bill, **kws):

    expected_tip = (total_bill.groupby(day)
                              .mean()
                              .apply(lambda x: x * .2)
                              .reset_index(name="tip"))
    sns.pointplot(expected_tip.day, expected_tip.tip,
                  linestyles=["--"], markers=["D"])

g = sns.FacetGrid(tips, col="sex", size=5)
g.map(sns.pointplot, "day", "tip")
g.map(plot_good_tip, "day", "total_bill")
g.set_axis_labels("day", "tip")

rawDescription: Showing data and model predictions in one plot using Seaborn and Statsmodels


rawDescription: Different results when running in python interpreter vs. script file


code: ', '.join([' : '.join((k, str(keywordsList[k]))) for k in sorted(keywordsList, key=keywordsList. get, reverse=True)])

rawDescription: Python dict to string

description: Python dict to string


code:
parser=argparse.ArgumentParser()
arglist = []
arg1 = parser.add_argument('-f','--foo')
arglist.append(arg1)
arg2=parser.add_argument('bar')
arglist.append(arg2)
arg3=parser.add_argument('phi')
arglist.append(arg3)
arglist

rawDescription: generic command line generator in python

description: Generic command line generator in python


code:
def startencoder(fname, crf, preset):
    """
    Use ffmpeg to convert a video file to H.264/AAC streams in an MP4
    container.

    Arguments:
        fname: Name of the file to convert.
        crf: Constant rate factor. See ffmpeg docs.
        preset: Encoding preset. See ffmpeg docs.

    Returns:
        A 3-tuple of a Process, input path and output path.
    """
    basename, ext = os.path.splitext(fname)
    known = ['.mp4', '.avi', '.wmv', '.flv', '.mpg', '.mpeg', '.mov', '.ogv',
            '.mkv', '.webm']
    if ext.lower() not in known:
        ls = "File {} has unknown extension, ignoring it.".format(fname)
        logging.warning(ls)
        return (None, fname, None)
    ofn = basename + '.mp4'
    args = ['ffmpeg', '-i', fname, '-c:v', 'libx264', '-crf', str(crf),
            '-preset', preset, '-flags',  '+aic+mv4', '-c:a', 'libfaac',
            '-sn', '-y', ofn]
    try:
        p = subprocess.Popen(args, stdout=subprocess.DEVNULL,
                            stderr=subprocess.DEVNULL)
        logging.info("Conversion of {} to {} started.".format(fname, ofn))
    except:
        logging.error("Starting conversion of {} failed.".format(fname))
    return (p, fname, ofn)

rawDescription: How to process sub-batches of asynchronous jobs?

description: Process sub-batches of asynchronous jobs


code:
df = pd.DataFrame([[1, 2], [3, 4], [1,6]], columns=['A', 'B'])
df
g = df.groupby('A')
size = g.size()
size[size > 1]

rawDescription: Pandas groupby: get size of a group knowing its id (from .grouper.group_info[0])


code:
TICK += 1
to_sleep = (start_time + TICK / 10.0) - time.time()
if to_sleep > 0:
    time.sleep(to_sleep)

rawDescription: I'm trying to get the correct minutes, and seconds when using integers


rawDescription: Python: How to sum up the word count for each person in a dialogue?


code:
for line in labStorageShort.splitlines():
    spl = line.rstrip().split()
    letter, flt = spl[-1][-1], float(spl[-1][:-1])  # if there are possibly nums without a letter use rstrip
     # do calculation here if preferable
     # flt = flt / 1024 if letter == "G" else flt
    labStorageArray.append(spl[0],flt)

rawDescription: How do I modify a python array of strings to strip any alphanumerics, convert to float, and then store the float in the same array?

description: Modify a python array of strings to strip any alphanumerics, convert to float, and then store the float in the same array


code:
import re
m = re.search(r"[a-z]*\(([a-z]*), ([a-z]*), ([a-z]*)\)", string)
m.group(0)
m.group(1)
m.group(2)
m.group(3)

rawDescription: Python re: Storing multiple matches in variables


code:
import example
example.get_list('abcd')

rawDescription: OUT argument internally allocated to return an array of structs


rawDescription: Construct a list of integers from bigger to smaller in python


code:
api.users.get()
# is equivalent to
api.users.get.__call__() # no arguments, because we passed none to `get()`
# will return
api.users.get._api_session(api.users.get._method_name)
# which is
api('users.get')

rawDescription: Why can I call all these methods that aren't explicitly defined in a class?


code:
import os

try:
    important_info = os.environ['IMPORTANT_INFO']
except KeyError:
    raise Exception('Set IMPORTANT_INFO environment variable, please!')

rawDescription: How to have python program listen on an environment variable?

description: Have python program listen on an environment variable


code: import mfxLib01 as mfxLib

rawDescription: How to maintain different version of a python module?

description: Maintain different version of a python module


code:
worker = {'admin':{'unknown': 1}, 'worker1':{'Paul': 1},  'worker2':{'Francis': 1}}
if 'admin' in worker:
    print("Here is admin")

rawDescription: check if item is part of dict keys

description: Check if item is part of dict keys


code:
from threading import currentThread

caches = {}

class RequestCache(object):
    def set(self, key, value):
        cache_id = hash(currentThread())
        if caches.get(cache_id):
            caches[cache_id][key] = value
        else:
            caches[cache_id] = {key: value}

    def get(self, key):
        cache_id = hash(currentThread())
        cache = caches.get(cache_id)
        if cache:
            return cache.get(key)
        return None

class RequestCacheMiddleware(object):
    def process_response(self, request, response):
        cache_id = hash(currentThread())
        if caches.get(cache_id):
            del(caches[cache_id])
        return response

rawDescription: django: how to memoize model manager methods?

description: Django: how to memoize model manager methods


code:
def fill_profile(self, **kwargs):
    for attr, value in kwargs.iteritems():
        setattr(self, attr, value)

rawDescription: How to initialize variable amount of Django model fields at once?

description: Initialize variable amount of Django model fields at once


code:
def roundrobin(*iterables):
    "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
    # Recipe credited to George Sakkis
    pending = len(iterables)
    nexts = cycle(iter(it).next for it in iterables)
    while pending:
        try:
            for next in nexts:
                yield next()
        except StopIteration:
            pending -= 1
            nexts = cycle(islice(nexts, pending))

rawDescription: Exhaust a list of generators in a cyclic fashion


code: lines = [ [data[x][abst.index(item)] for item in dic if item in abst] for x in range(0, 2) ]

rawDescription: how to print lists separately instead of merging them?

description: Print lists separately instead of merging them


code:
df = DataFrame({'A' : ['foo', 'foo', 'foo', 'foo',
                           'bar', 'bar', 'bar', 'bar'],
                    'C' : [1,2,3,4,4,3,2,1]})

grouped = df.groupby('A')
df['C1'] = grouped['C'].apply(lambda x: x - x.iloc[0])

rawDescription: Looping through a groupby and adding a new column


code: df1.sort(['a', 'b'], ascending=[True, False], inplace=True)

rawDescription: How to sort a dataFrame in python pandas by two or more columns?

description: Sort a dataFrame in python pandas by two or more columns


rawDescription: Mapping data from excel with Python

description: Mapping data from excel with Python


code:
for triplet in sod_hng_hhl_lst:
    name,type,numberString = triplet.split(",")
    # because this is a string and we want a number:
    num_as_integer = int(numberString)
    # do something with num_as_integer
    new_number = num_as_integer * 2
    newtriplet = ','.join([name, type, new_number])

rawDescription: spliting list and adding values

description: Spliting list and adding values


rawDescription: generating cross product for list of lists

description: Generating cross product for list of lists


Restoration:
AS Build
Capacity Management

Implementation:
Hardware Stack
SA and SF

Testing and Validation: NFRU-SS NRFU-UC

rawDescription: list of tuples to dictionary of dictionaries

description: List of tuples to dictionary of dictionaries


code:
x = ''.encode('utf8')
type(x)

rawDescription: python 3.4 condition involving byte literal returns false instead of true

description: Python 3.4 condition involving byte literal returns false instead of true


rawDescription: "Guess my number" game with a while loop


code:
class Child2(Parent):
    def function(self):
        do_something_else_2()

rawDescription: How to enforce (with a warning) that parent functions are called by the children that override them?

description: Enforce (with a warning) that parent functions are called by the children that override them


code: ''.join(c for c in x if c in 'aeiou')

rawDescription: Deleting consonants from a string in Python


code:
import collections
data = collections.defaultdict(set)
for row in reader:
    asset_id = row[ID_COLUMN]
    for prop, column in PROP_COLUMNS:
        prop_value = row[column]
        data[(asset_id, prop)].add(prop_value)

rawDescription: Process CSV in Python

description: Process CSV in Python


code:
'Spams spam and spam'[:15]
'viking inc'[:15]

rawDescription: A simple way to pad and slice to get a fixed length string


code:
aaaa
bcsg
aacd
gdee
aadw
hwer

rawDescription: Read lines in one file and find all strings starting with 4-letter strings listed in another txt file


code:
import numpy as np
import cv2

im0 = cv2.imread('objects/pix0.png',0)
im1 = cv2.imread('objects/pix1.png',0)
im2 = cv2.imread('objects/pix2.png',0)
im3 = cv2.imread('objects/pix3.png',0)
im4 = cv2.imread('objects/pix4.png',0)
im5 = cv2.imread('objects/pix5.png',0)

h0, w0 = im0.shape[:2]
h1, w1 = im1.shape[:2]
h2, w2 = im2.shape[:2]
h3, w3 = im3.shape[:2]
h4, w4 = im4.shape[:2]
h5, w5 = im5.shape[:2]
maxh=max(h0,h1,h2,h3,h4,h5)

#add 50 for space between the objects

new = np.zeros((maxh, w0+w1+w2+w3+w4+w5+50),np.uint8)
new=(255-new)
new[maxh-h0:, :w0] = im0
new[maxh-h1:, w0+10:w0+w1+10] = im1
new[maxh-h2:, w0+w1+20:w0+w1+w2+20] = im2
new[maxh-h3:, w0+w1+w2+30:w0+w1+w2+w3+30] = im3
new[maxh-h4:, w0+w1+w2+w3+40:w0+w1+w2+w3+w4+40] = im4
new[maxh-h5:, w0+w1+w2+w3+w4+50:] = im5
gray = cv2.cvtColor(new, cv2.COLOR_GRAY2BGR)

cv2.imshow('norm',gray)
cv2.imwrite('objects/new_image.jpg',gray)
key = cv2.waitKey(0)

rawDescription: How can i access the ordering of contours in `opencv`

description: Access the ordering of contours in `opencv`


code: filtered = [item for index, item in enumerate(list2) if index not in ignore_indices]

rawDescription: Remove elements from a list according to indexes list efficiently


code:
import requests

respText = requests.get("http://www.imdb.com/chart/top").text

for title in re.findall(r'<td class="titleColumn">.+?>(.+?)<', respText, re.DOTALL):
    print(title)

rawDescription: Need to extract data from a website and store in list using regex


rawDescription: flask return image created from database

description: Flask return image created from database


code:
with open(myFile.json) as f:
    for line in f:
        data = json.loads(line)

rawDescription: Export dicts to a json file

description: Export dicts to a json file


code:
def read_csv(file, columns, type_name="Row"):
  try:
    row_type = namedtuple(type_name, columns)
  except ValueError:
    row_type = tuple
  rows = iter(csv.reader(file))
  header = rows.next()
  mapping = [header.index(x) for x in columns]
  for row in rows:
    row = row_type(*[row[i] for i in mapping])
    yield row

rawDescription: Read specific columns in csv using python


code:
['123', 'Subject of Ticket 123', ['Line 1 in Description field of Ticket 123.', 'Line 2 in Description field of Ticket 123.', 'Line 3 in Description field of Ticket 123.']]
['124', 'Subject of Ticket 124', ['Line 1 in Description field of Ticket 124.', 'Line 2 in Description field of Ticket 124.', 'Line 3 in Description field of Ticket 124.']]

rawDescription: Python - How to nest file read loops?

description: Python - How to nest file read loops


code:
res = kegg_con.find('genes', 'b3640')
orgnanism = res.split("\n")[2].split()[0].split(":")[0]

rawDescription: How to access a KEGG entry without specifying the organism using bioservices?

description: Access a KEGG entry without specifying the organism using bioservices


code:
import subprocess
env = os.environ.copy()
env['LD_LIBRARY_PATH'] = os.getcwd()
proc = subprocess.Popen("./mydemo", shell=True, env=env)
proc.wait()

rawDescription: I use os.system to run executable file, but it need a .so file, it can't find library


rawDescription: Can someone explain mdict in python (scipy.io), for example in scipy.io.savemat()?


code:
def validated_input(prompt, valid_values):
    valid_input = False
    while not valid_input:
        value = input(prompt)
        valid_input = value.lower() in valid_values
    return value

rawDescription: How to redo an input if user enters invalid answer

description: Redo an input if user enters invalid answer


code:
rememberer = request.environ['repoze.who.plugins']['cookie']
identity = {'repoze.who.userid': user.username}
response.headerlist = response.headerlist + \
        rememberer.remember(request.environ, identity)

rawDescription: How do I log a user in using repoze.who?

description: Log a user in using repoze.who


code:
import subprocess
import os
import time

# Set TERM=xterm in case it isn't already
os.environ['TERM'] = 'xterm'

# Spawn aptitude
p = subprocess.Popen('aptitude', stdin=subprocess.PIPE)

# Wait for a bit to let it load from cache
time.sleep(5)

# Control it using xterm control codes
p.stdin.write('\033OB') # arrow down
time.sleep(1)
p.stdin.write('\033OB') # arrow down
time.sleep(1)
p.stdin.write('\033OA') # arrow up
time.sleep(1)
p.stdin.write('\033OA') # arrow up
time.sleep(1)
p.stdin.write('q')      # quit
time.sleep(1)
p.stdin.write('y')      # confirm

rawDescription: How do I interact with a child process pretending to be a terminal?

description: Interact with a child process pretending to be a terminal


rawDescription: How to change value for item in a list of dict in python in a simple way?

description: Change value for item in a list of dict in python in a simple way


code:
text = "Some nouns like eggs egg bacon what a lovely donkey"
import re
text = "Some nouns like eggs egg bacon what a lovely donkey"
noun_list = ['eggs', 'bacon', 'donkey', 'dog']
noun_patt = r'\s(?:{})\s'.format('|'.join(noun_list))
re.findall(noun_patt, text)

rawDescription: Python - Don't Understand The Returned Results of This Concatenated Regex Pattern


code:
xs = np.array(["NY", "LA", "GA"])
''.join('1' if f else '0' for f in np.in1d(xs, 'NY GA'.split(' ')))

rawDescription: How to encode categorical values in Python

description: Encode categorical values in Python


code:
df
df2

rawDescription: pandas reindex a dataframe with duplicate keys

description: Pandas reindex a dataframe with duplicate keys


code:
class Foo():
    var = "Foo"

    @classmethod
    def print_var(cls):
        print(cls.var)

class Bar(Foo):
    var = "Bar"

rawDescription: Access overridden class variable in parent of subclass


rawDescription: Converting an array to hash in python

description: Converting an array to hash in python


code:
# Save JSON file
f = open('names.json', 'w')
f.write( json.dumps(json_ready, sort_keys=True, indent=4) )
f.close()

rawDescription: Casper.js unable to open JSON file when activated through python script


code:
import re
d = {'account_id':'a_value','group_id':'g_value','other_id':'o_value' }

tests = (r'^settings/usergroups/(?P<group_id>\d+)/cargroups/$',
         r'^settings/usergroups/(?P<group_id>\d+)/other/(?P<other_id>\d+)/$',
         r'^settings/account/(?P<account_id>\d+)/other/(?P<other_id>\d+)/$',
         r'^settings/usergroups/(?P<group_id>(\d+|\w))/other/(?P<other_id>\d+)/$'
         )
metapat = r'\(\?P<(.*?)>.*?\)'
for t in tests:
      result = re.sub(metapat, r'{\1}', t)
      if result:
            print(result.format(**d))

rawDescription: Python regular expressions replacing

description: Python regular expressions replacing


code:
class MyButton(QtGui.QPushButton):

    custom_click_signal = QtCore.Signal()

    def enterEvent(self, event):
        super().enterEvent(event)
        # Change icon hove image here

    def leaveEvent(self, event):
        super().leaveEvent(event)
        # Change icon back to original image here.

    def mousePressEvent(self, event):
        super().mousePressEvent(event)
        self.custom_click_signal.emit()
        # connect to signal btn.custom_click_signal.connect(method)

rawDescription: How to add a second pixmap to an abstract button when pushed in PySide/PyQt

description: Add a second pixmap to an abstract button when pushed in PySide/PyQt


rawDescription: Python3 how is mro defined in object?

description: Python3 how is mro defined in object


rawDescription: Pandas, unexpected behavior of .resample('B')


code:
Serializers
class ProductCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = ProductCategory

class ProductSerializer(serializers.ModelSerializer):
    # The related_name="categories" on the ProductCategory.product
    #    will allow us to retrieve categories, and convert it to
    #    the __unicode__ equivalent.
    # http://www.django-rest-framework.org/api-guide/relations/#stringrelatedfield
    categories = serializers.StringRelatedField(many=True)

    class Meta:
        model = Product

rawDescription: DjangoRestFramework - How to get result from different serializers into one


code:
def create_environment(home_dir, site_packages=False, clear=False,
                       unzip_setuptools=False, use_distribute=False,
                       prompt=None, search_dirs=None, never_download=False):
    """
    Creates a new environment in ``home_dir``.

    If ``site_packages`` is true, then the global ``site-packages/``
    directory will be on the path.

    If ``clear`` is true (default False) then the environment will
    first be cleared.
    """
    home_dir, lib_dir, inc_dir, bin_dir = path_locations(home_dir)

    py_executable = os.path.abspath(install_python(
        home_dir, lib_dir, inc_dir, bin_dir,
        site_packages=site_packages, clear=clear))

    install_distutils(home_dir)

    if use_distribute:
        install_distribute(py_executable, unzip=unzip_setuptools,
                           search_dirs=search_dirs, never_download=never_download)
    else:
        install_setuptools(py_executable, unzip=unzip_setuptools,
                           search_dirs=search_dirs, never_download=never_download)

    install_pip(py_executable, search_dirs=search_dirs, never_download=never_download)

    install_activate(home_dir, bin_dir, prompt)

rawDescription: virtualenv not accessing internal pypi server

description: Virtualenv not accessing internal pypi server


code:
df = pd.DataFrame(np.arange(15).reshape(5,3))
df.index.name = 'index'
df
df.index.name = None
df

rawDescription: How to remove ugly row in pandas.dataframe

description: Remove ugly row in pandas.dataframe


code:
# Let's assume that 'a' contains your values
a = [(531266, (255, 123, 0))]
times, (r, g, b) = a[0]

rawDescription: Python split a tuple into individual parts...getpixel(rgb)


code: zip(a,x)

rawDescription: Find the indices of elements greater than x


code:
import re

found = re.findall(r'\n*(A.*?\n\$\$)\n*', open('input.txt', 'r').read(), re.M | re.S)

[open(str(found.index(f)+1)+'.txt', 'w').write(f) for f in found[:3]]

rawDescription: How can I split a text file into multiple text files using python?

description: Split a text file into multiple text files using python


rawDescription: How to print non-ascii characters to file in Python 2.7

description: Print non-ascii characters to file in Python 2.7


code: x & y

rawDescription: Numpy Modular arithmetic

description: Numpy Modular arithmetic


rawDescription: append multiple values for one key in Python dictionary

description: Append multiple values for one key in Python dictionary


code:
from twisted.internet.protocol import Protocol
class Echo(Protocol):
    def dataReceived(self, data):
        self.transport.write(data)

rawDescription: General question regarding wether or not use Twisted in TCP proxy project


code:
from sqlalchemy.orm import class_mapper
mapper = class_mapper(MySubClass)
mapper.polymorphic_identity = "some_identity"
mapper.polymorphic_map["some_identity"] = mapper
# in 0.8, might try to fix this for 0.9
mapper._identity_class = mapper.inherits._identity_class

rawDescription: How can I set Polymorphic Identity after a class is defined?

description: Set Polymorphic Identity after a class is defined


rawDescription: How to distinguish between multiple shapes in a figure or array?

description: Distinguish between multiple shapes in a figure or array


code:
olist = myImage_mask.outline()
pygame.draw.polygon(blankSurf,(200,150,150),olist,0)

rawDescription: Display PyGame Mask on screen

description: Display PyGame Mask on screen


rawDescription: How to remove first item and last item using python

description: Remove first item and last item using python


code:
i = 456
bitstring.BitString(uint=i, length=(i.bit_length()+7)/8*8).bytes

rawDescription: How to convert an integer to variable length byte string?

description: Convert an integer to variable length byte string


code:
#we separate the diameters from the rest
diameter_column= arr[:,0]
#we create a (1D) bool array that match diameter between 0 and 20
diameter_filter=(diameter_column<=20) * (diameter_column>=0)
#we use the boolean array as an index to select the right asteroids
filtered_arr=arr[diameter_filter,:]

rawDescription: Split an array according to it's information


code:
tt= tuple(((a,b), c) for a,b,c in t)
tt

rawDescription: Python Pandas Dataframe to Nested Tuples


code:
class vec2:
    x = 0.0
    y = 0.0

    def __init__(self, x=0.0, y=0.0):
        self.x, self.y = x, y

    def __add__(self, other):
        if not isinstance(other, self.__class__):
            return NotImplemented
        result = self.__class__(self.x, self.y)
        result.x += other.x
        result.y += other.y
        return result

    def __iadd__(self, other):
        if not isinstance(other, self.__class__):
            return NotImplemented
        self.x += other.x
        self.y += other.y
        return self

    def __mul__(self, other):
        if not isinstance(other, self.__class__):
            return NotImplemented
        result = self.__class__(self.x, self.y)
        result.x *= other.x
        result.y *= other.y
        return result

    def __imul__(self, other):
        if not isinstance(other, self.__class__):
            return NotImplemented
        self.x *= other.x
        self.y *= other.y
        return self

rawDescription: Method argument conversion: 'tuple' has no attribute 'attribute'


code:
class ConcordanceIndex(object):
    def __init__(self, tokens, key=lambda x:x):
        ...
    def print_concordance(self, word, width=75, lines=25):
        ...
        offsets = self.offsets(word)
        ...
        right = ' '.join(self._tokens[i+1:i+context])

rawDescription: Calling NLTK's concordance - how to get text before/after a word that was used?


code:
def pack_keypoint(keypoints, descriptors):
    kpts = np.array([[kp.pt[0], kp.pt[1], kp.size,
                  kp.angle, kp.response, kp.octave,
                  kp.class_id]
                 for kp in keypoints])
    desc = np.array(descriptors)
    return kpts, desc

def unpack_keypoint(array):
    try:
        kpts = array[:,:7]
        desc = array[:,7:]
        keypoints = [cv2.KeyPoint(x, y, _size, _angle, _response, int(_octave), int(_class_id))
                 for x, y, _size, _angle, _response, _octave, _class_id in list(kpts)]
        return keypoints, np.array(desc)
    except(IndexError):
        return np.array([]), np.array([])

def process_image(imagename, resultname):
    img = cv2.imread(imagename, 0)
    k = surf.detect(img, None)
    if len(k) > 0:
        k, des = surf.compute(img, k)
    else:
        des = []
    k, des = pack_keypoint(k, des) #
    write_features_to_file(resultname, k, des)

rawDescription: Store the extracted SURF descriptors and keypoints in *.npy file


rawDescription: Merging one-by-many CSV files in Python

description: Merging one-by-many CSV files in Python


rawDescription: Django get all records of related models


code:
import csv

import requests
from bs4 import BeautifulSoup

r = requests.get('http://www.hoopsstats.com/basketball/fantasy/nba/opponentstats/16/12/eff/1-1')
soup = BeautifulSoup(r.text, "html.parser")

with open("output.csv", "w") as f:
    writer = csv.writer(f)

    for table in soup.find_all('table', class_='statscontent'):
        team = table.find('a').text
        stats = [team] + [stat.text for stat in table.find_all('center')]

        writer.writerow(stats)

rawDescription: Adding new line to data for csv in python


code:
{'title_1': {'audio_tracks': {'1': 'English (AC3) (5.1 ch) (iso639-2: eng), 48000Hz, 384000bps',
   '2': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps',
   '3': 'Deutsch (AC3) (5.1 ch) (iso639-2: deu), 48000Hz, 384000bps',
   '4': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'},
  'duration': '02:06:44',
  'subtitle_tracks': {'1': 'English (iso639-2: eng) (Bitmap)(VOBSUB)',
   '10': 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)',
   '11': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '2': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '3': 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)',
   '4': 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)',
   '5': 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)',
   '6': 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)',
   '7': 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)',
   '8': 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)',
   '9': 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'}},
 'title_2': {'audio_tracks': {'1': 'English (AC3) (5.1 ch) (iso639-2: eng), 48000Hz, 384000bps',
   '2': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps',
   '3': 'Deutsch (AC3) (5.1 ch) (iso639-2: deu), 48000Hz, 384000bps',
   '4': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'},
  'duration': '00:00:12',
  'subtitle_tracks': {'1': 'English (iso639-2: eng) (Bitmap)(VOBSUB)',
   '10': 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)',
   '11': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '2': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '3': 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)',
   '4': 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)',
   '5': 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)',
   '6': 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)',
   '7': 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)',
   '8': 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)',
   '9': 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'}},
 'title_3': {'audio_tracks': {'1': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'},
  'duration': '00:01:51',
  'subtitle_tracks': {'1': 'English (iso639-2: eng) (Bitmap)(VOBSUB)',
   '10': 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)',
   '11': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '2': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '3': 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)',
   '4': 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)',
   '5': 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)',
   '6': 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)',
   '7': 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)',
   '8': 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)',
   '9': 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'}},
 'title_4': {'audio_tracks': {'1': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'},
  'duration': '00:00:19',
  'subtitle_tracks': {'1': 'English (iso639-2: eng) (Bitmap)(VOBSUB)',
   '10': 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)',
   '11': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '2': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '3': 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)',
   '4': 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)',
   '5': 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)',
   '6': 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)',
   '7': 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)',
   '8': 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)',
   '9': 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'}},
 'title_5': {'audio_tracks': {'1': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'},
  'duration': '00:00:34',
  'subtitle_tracks': {'1': 'English (iso639-2: eng) (Bitmap)(VOBSUB)',
   '10': 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)',
   '11': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '2': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '3': 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)',
   '4': 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)',
   '5': 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)',
   '6': 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)',
   '7': 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)',
   '8': 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)',
   '9': 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'}},
 'title_6': {'audio_tracks': {'1': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'},
  'duration': '00:00:35',
  'subtitle_tracks': {'1': 'English (iso639-2: eng) (Bitmap)(VOBSUB)',
   '10': 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)',
   '11': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '2': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '3': 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)',
   '4': 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)',
   '5': 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)',
   '6': 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)',
   '7': 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)',
   '8': 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)',
   '9': 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'}},
 'title_7': {'audio_tracks': {'1': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'},
  'duration': '00:00:34',
  'subtitle_tracks': {'1': 'English (iso639-2: eng) (Bitmap)(VOBSUB)',
   '10': 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)',
   '11': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '2': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '3': 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)',
   '4': 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)',
   '5': 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)',
   '6': 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)',
   '7': 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)',
   '8': 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)',
   '9': 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'}},
 'title_8': {'audio_tracks': {'1': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'},
  'duration': '00:01:37',
  'subtitle_tracks': {'1': 'English (iso639-2: eng) (Bitmap)(VOBSUB)',
   '10': 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)',
   '11': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '2': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '3': 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)',
   '4': 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)',
   '5': 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)',
   '6': 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)',
   '7': 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)',
   '8': 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)',
   '9': 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'}}}

rawDescription: How to parse complex string

description: Parse complex string


code:
columns = [col for col in df.columns if col[0] not in set(list('XYZ'))]
df.columns = pd.MultiIndex.from_product([columns, list('WXYZ')])

rawDescription: How can I use melt() to reshape a pandas DataFrame to a list, creating an index from a crosstab column and creating a new variable in its place?

description: Use melt() to reshape a pandas DataFrame to a list, creating an index from a crosstab column and creating a new variable in its place


code:
def mysplit(line):
    split1 = line.split(':')
    last = len(split1) - 1
    for i, t1 in enumerate(split1):
        if i == 0 or i == last:
            yield t1.strip()
        else:
            for t2 in t1.split(None, 1):
                yield t2.strip()

testline = "t 1 : n1   t t 2 : n2   t t t 3 : n3"
testsplit = list(mysplit(testline))
print(testsplit)
# prints ['t 1', 'n1', 't t 2', 'n2', 't t t 3', 'n3']

rawDescription: How to find index of a character after space in python

description: Find index of a character after space in python


code:
class SMSWait(self):
 def run(self):
     self.open()
     while 1:
         SMSRead = 'AT+CMGL="ALL"\r\n'
         self.SendCommand(SMSRead, getline=True)
         data = self.ser.readall()
         time.sleep(2)
         datalist.append(data)
         if any('+CMGL:' in d for d in datalist):
             if 'AT' in datalist: datalist.remove['AT']
             if any('\r\n' in d for d in datalist): datalist = [d.replace('\r\n', '') for d in datalist]
             if any('\r' in d for d in datalist): datalist   = [d.replace('\r', '') for d in datalist]
             if any('\n' in d for d in datalist): datalist   = [d.replace('\n', '') for d in datalist]
             if any('OK' in d for d in datalist): datalist   = [d.replace('OK', '') for d in datalist]
             datalist = filter(None, datalist)
             split_data = datalist[0].split('+CMGL: ')
             split_data = filter(None, split_data)

rawDescription: Read lines (two or more lines) from the output of AT command and put it to database


code:
data = np.array([range(10),range(10,20)])
fig = plt.figure(figsize=(3,5))
ax = fig.add_subplot(111)
ax.imshow(data,aspect='auto',extent=[10000,10010,0,1])

rawDescription: Custom ticks autoscaled when using imshow?


code:
max((combination
    for combination in all_combinations
    if combination.stats.turbo >= 50
    and combination.stats.handling >= 40),
    key=lambda c:c.stats.speed)

rawDescription: What is an elegant way to solve this max and min problem in Ruby or Python?


rawDescription: How to strip the string which contain forward slash?

description: Strip the string which contain forward slash


rawDescription: Trigger ImportError to test Coverage of Fallback Code


code:
from timeit import timeit
timeit(lambda:list(assignments(12, 5)), number=1)
sum(1 for _ in assignments(12, 5))

rawDescription: Generating a list of repetitions regardless of the order


code: return_code = tcProcess.wait ()

rawDescription: Running test cases for C program in run time via python


code: email = models.EmailField(blank=True)

rawDescription: python-social-auth require email field

description: Python-social-auth require email field


rawDescription: Raspberry Pi - Interfacing with an i2c enabled IR temp sensor (MLX90614)


code:
from optparse import make_option

from django.core.management.commands.loaddata import Command as LoadDataCommand

class Command(LoadDataCommand):
    option_list = LoadDataCommand.option_list + (
        make_option('-e', '--exclude', action='append',
                    help='Exclude given fixture/s from being loaded'),
    )

    def handle(self, *fixture_labels, **options):
        self.exclude = options.get('exclude')
        return super(Command, self).handle(*fixture_labels, **options)

    def find_fixtures(self, *args, **kwargs):
        updated_fixtures = []
        fixture_files = super(Command, self).find_fixtures(*args, **kwargs)
        for fixture_file in fixture_files:
            file, directory, name = fixture_file

            # exclude a matched file path, directory or name (filename without extension)
            if file in self.exclude or directory in self.exclude or name in self.exclude:
                if self.verbosity >= 1:
                    self.stdout.write('Fixture skipped (excluded: %s, matches %s)' %
                                      (self.exclude, [file, directory, name]))
            else:
                updated_fixtures.append(fixture_file)
        return updated_fixtures

rawDescription: Django 1.6: How to ignore a fixture in python manage.py loaddata?


code: img = imread("%d.EXT" % i)

rawDescription: read files according to their serial numbers and not names

description: Read files according to their serial numbers and not names


code:
import numpy as np
x = np.array([1,2,3,4,5])
np.linalg.norm(x)

rawDescription: How do you get the magnitude of a vector in Numpy?

description: Get the magnitude of a vector in Numpy


rawDescription: Documentation for understanfing

description: Documentation for understanfing


code:
from itertools import groupby
from itertools import groupby
def parse_sec(bk):
    with open(bk) as f:
        grps = groupby(f, key=lambda x: bool(x.isupper()))
        for k, sec in grps:
            if k:
                print("First paragraph from section titled :{}".format(next(sec).rstrip()))
                v = next(grps)[1]
                next(v, ""),next(v,"")
                for line in v:
                    if not line.strip():
                        break
                    print(line)

rawDescription: print first paragraph in python

description: Print first paragraph in python


code:
word = 'letters'
scrambled = ['s', 'e', 'l', 'r', 'e', 't', 't']
def make_sort_key(word):
    indices = {}
    for i, c in enumerate(word):
        indices.setdefault(c, []).append(i)
    def key(c):
        return indices[c].pop()
    return key

sorted(scrambled, key=make_sort_key(word))

rawDescription: Sorting string into identical words

description: Sorting string into identical words


code: [shv[i:i+2] for i in range(0, len(shv)-1, 2)]

rawDescription: Return a list of string letters grouped by two


code:
#!/usr/bin/env python3
import re
from datetime import datetime, timedelta, timezone

time_string = u"/Date(1409522400000+0200)/"
epoch = datetime(1970, 1, 1, tzinfo=timezone.utc)
ticks, offset = re.match(r'/Date\((\d+)([+-]\d{4})?\)/$', time_string).groups()
utc_dt = epoch + timedelta(milliseconds=int(ticks))
print(utc_dt)
if offset:
   offset = int(offset)
   hours, minutes = divmod(abs(offset), 100)
   if offset < 0:
      hours, minutes = -hours, -minutes
   dt = utc_dt.astimezone(timezone(timedelta(hours=hours, minutes=minutes)))
   print(dt)

rawDescription: Convert weird Python date format to readable date


rawDescription: Python tree storing expressions from json input file


code:
import re
description = "11:26:16 ENTRY 'Insert Imaginative Description of a person' 11:29:17 EXIT 'Insert The Description of the Same Person'"
re.search(r'\b(\d{2}:\d{2}:\d{2}(?:\.\d{3})?) ENTRY', description).group(1)

rawDescription: Python Regex - Capturing a sentence based on the beginning and ending


code:
x = [1,2,3]
id(x)
y = x
id(y)
z = list(x)
id(z)
x.append(10)
x
y
z

rawDescription: Python 3.4 What is this list doing?

description: Python 3.4 What is this list doing


code:
import json
from datetime import datetime
from time import mktime

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return {
                '__type__': '__datetime__',
                'epoch': int(mktime(obj.timetuple()))
            }
        else:
            return json.JSONEncoder.default(self, obj)

def my_decoder(obj):
    if '__type__' in obj:
        if obj['__type__'] == '__datetime__':
            return datetime.fromtimestamp(obj['epoch'])
    return obj

# Encoder function
def my_dumps(obj):
    return json.dumps(obj, cls=MyEncoder)

# Decoder function
def my_loads(obj):
    return json.loads(obj, object_hook=my_decoder)

rawDescription: Celery: is there a way to write custom JSON Encoder/Decoder?


code:
def foo(thelist, n):
     thelist.append(n)
     return thelist

rawDescription: Reassigning a value returned from a function in Python


code:
class PressureReadingUpdate(forms.Form):
    """
    Form that asks for the pressure of a line given some attributes of that line.
    """
    psi = forms.IntegerField(widget=forms.NumberInput)

    def __init__(self, *args, **kwargs):
        self.line = kwargs.pop('line')
        kwargs['auto_id'] = "%s".format(self.line.number)
        super(PressureReadingUpdate, self).__init__(*args, **kwargs)

class BasePsiReadingFormset(BaseFormSet):
    """
    Formset that constructs a group of PressureReadingUpdate forms by taking a queryset
    of Line objects and passing each one in turn to a PressureReadingUpdate form as it
    gets constructed
    """
    def __init__(self, *args, **kwargs):
        self.lines = kwargs.pop('lines')
        super(BasePsiReadingFormset, self).__init__(*args, **kwargs)
        self.extra = len(self.lines)
        self.max_num = len(self.lines)

    def _construct_form(self, i, **kwargs):
        kwargs['line'] = self.lines[i]
        form = super(BasePsiReadingFormset, self)._construct_form(i, **kwargs)
        return form

PsiReadingFormset = formset_factory(form=PressureReadingUpdate, formset=BasePsiReadingFormset)

rawDescription: Django -- Generate form based on queryset


code:
from itertools import combinations, product

words = ["hello", "goodbye", "foo"]
lists = [["111", "450", "nice"], ["can", "be", "of", "different", "sizes"]]

# how many elements of `lists` to pick from?
for n in range(1, len(lists) + 1):
    # This returns in-order combinations, ie you will get
    # '111', 'can'  and not 'can', '111'.
    # If you want all orderings as well as all combinations,
    # use itertools.permutations instead,
    for sublist in combinations(lists, n):
        # now we generate all combinations of
        # one element from each basis list,
        basis = [words] + list(sublist)
        for combo in product(*basis):
            # and display the result
            print("".join(combo))

rawDescription: want to combine every element of a list with every element of n lists

description: Want to combine every element of a list with every element of n lists


rawDescription: converting IP string to hex for dpkt.IP

description: Converting IP string to hex for dpkt.IP


code: call(['bash', 'run.sh', 'var1', 'var2'])

rawDescription: Pass a variable from python to shell script


code:
from subprocess import Popen, PIPE

first = Popen(["firstCommand", "-"], stdin=PIPE, stdout=PIPE, bufsize=1)
second = Popen(["secondCommand", "-"], stdin=PIPE, stdout=PIPE, bufsize=1)
bind(first.stdout, second.stdin, somePythonRoutine)
with open("finalOutput", "wb") as file:
    third = Popen(["thirdCommand", "-"], stdin=PIPE, stdout=file, bufsize=1)
bind(second.stdout, third.stdin, anotherPythonRoutine)

# provide input for the pipeline
first.stdin.write(b"foo")
first.stdin.close()

# wait for it to complete
pipestatus = [p.wait() for p in [first, second, third]]

rawDescription: How to thread multiple subprocess instances in Python 2.7?

description: Thread multiple subprocess instances in Python 2.7


code:
def zero_out_column(s, index):
    nums = [substring.split(',') for substring in s.split()]
    for row in nums:
        row[index] = '0'
    return ' '.join(','.join(row) for row in nums)

rawDescription: Is there an elegant and concise syntax to do this in Python?


code:
import math

array1 = [1, 2, 3]
array2 = ['math.pow(x, 3)', 'math.pow(x, 2)+x', '3*x']

def cal(values, expressions):
    return [eval(expression, {'math': math, '__builtins__': None}, {'x': value})
                for value, expression in zip(values, expressions)]

print(cal(array1, array2))

rawDescription: Pass an array of math.functions as parameters with multiple variables


rawDescription: Django Form values without HTML escape

description: Django Form values without HTML escape


code:
import marshal, types

code = marshal.loads(code_string)
func = types.FunctionType(code, globals(), "some_func_name")

func(10)  # gives 100

rawDescription: Pass client-side function to celery task


code:
a = zip(*a)
b = "ABCDEFGHI"
zip(b,a)

rawDescription: How to iterate for specific elements in a list?

description: Iterate for specific elements in a list


code:
positions = np.array([1, 10, 100, 1000])
ax.set_xticks(positions)
ax.set_xticklabels(np.log10(positions))

rawDescription: Matplotlib changing axis ticks

description: Matplotlib changing axis ticks


code:
def merge_no_repeat(iter1, iter2, key=None):
    if key is None:
        key = lambda x: x
    ref = next(iter1, None)
    for elem in iter2:
        key_elem = key(elem) # caching value so we won't compute it for each value in iter1 that is before this one
        while ref is not None and key_elem > key(ref):
            # Catch up with low values from iter1
            yield ref
            ref = next(iter1, None)
        if ref is None or key_elem < key(ref):
            # Catch up with low values from iter2, eliminate duplicates
            yield elem
    # Update: I forgot to consume iter1 in the first version of this code
    for elem in iter1:
        # Use remaining items of iter1 if needed
        yield elem

rawDescription: Merge two sorted iterators without replace


code:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1977)

def main():
    numpoints = 30
    theta = np.linspace(0, 2*np.pi, numpoints)
    r1 = np.random.random(numpoints)
    r2 = 5 * np.random.random(numpoints)

    params = dict(projection='polar', theta_direction=-1, theta_offset=np.pi/2)
    fig, ax = plt.subplots(subplot_kw=params)
    ax2 = polar_twin(ax)

    ax.fill_between(theta, r2, color='blue', alpha=0.5)
    ax2.fill_between(theta, r1, color='green', alpha=0.5)

    plt.setp(ax2.get_yticklabels(), color='darkgreen')
    plt.setp(ax.get_yticklabels(), color='darkblue')
    ax.set_ylim([0, 5])
    ax2.set_ylim([0, 1])

    plt.show()

def polar_twin(ax):
    ax2 = ax.figure.add_axes(ax.get_position(), projection='polar',
                             label='twin', frameon=False,
                             theta_direction=ax.get_theta_direction(),
                             theta_offset=ax.get_theta_offset())
    ax2.xaxis.set_visible(False)

    # There should be a method for this, but there isn't... Pull request?
    ax2._r_label_position._t = (22.5 + 180, 0.0)
    ax2._r_label_position.invalidate()

    # Bit of a hack to ensure that the original axes tick labels are on top of
    # whatever is plotted in the twinned axes. Tick labels will be drawn twice.
    for label in ax.get_yticklabels():
        ax.figure.texts.append(label)

    return ax2

if __name__ == '__main__':
    main()

rawDescription: Add second axis to polar plot

description: Add second axis to polar plot


code: square_list = [n for n in start_list]

rawDescription: Python, issue with for loop and List can't figure it out


code: return pandas.Series({"IV": iv, "Vega": vega})

rawDescription: Create multiple columns in Pandas Dataframe from one function


code:
from pyramid.events import subscriber
from pyramid.events import BeforeRender
from pyramid.view import render_view_to_response

@subscriber(BeforeRender)
def add_render_view_global(event):
    event['render_view'] = lambda name: render_view_to_response(context, request, name, secure).ubody

rawDescription: ajax widgets in pyramid and chameleon

description: Ajax widgets in pyramid and chameleon


code:
termfile = open('terms.pkl', 'rb')
documentfile = open('documents.pkl', 'rb')
term_frequency = pickle.load(termfile)
document_frequency = pickle.load(documentfile)
termfile.close()
documentfile.close()

rawDescription: Python 2.7: Persisting search and indexing


code:
def match_display_hook(self, substitution, matches, longest_match_length):
    print()
    for match in matches:
        print(match)
    print(self.prompt.rstrip(), readline.get_line_buffer(), sep='', end='')
    sys.stdout.flush()

rawDescription: Python : correct use of set_completion_display_matches_hook


code:
def _render(resource, request):
    result = resource.render(request)
    if isinstance(result, str):
        request.write(result)
        request.finish()
        return succeed(None)
    elif result is server.NOT_DONE_YET:
        if request.finished:
            return succeed(None)
        else:
            return request.notifyFinish()
    else:
        raise ValueError("Unexpected return value: %r" % (result,))

rawDescription: How to test twisted web resource with trial?

description: Test twisted web resource with trial


code:
d = collections.defaultdict(list)
for t, c, n in get_data_from_table:
    d[t].append(c)
tables = [[key] + values for key, values in d.items()]

rawDescription: Getting all columns in a list with the same tablename


caption: 2 cols (full - 3)

rawDescription: Sublime layout split into 3 panel, to create custom layout


rawDescription: How to use regex to reference specific parts?

description: Use regex to reference specific parts


code: headers["Range"] = "bytes = 0-4096"

rawDescription: Limit return size of GAE url_fetch get method?


code:
type(open('file.txt'))
from io import TextIOWrapper
isinstance(open('file.txt'), TextIOWrapper)

rawDescription: Python What's type for open(file)

description: Python What's type for open(file)


code:
from b import some_func
some_func()

rawDescription: How do I import other scripts?

description: Import other scripts


code:
def checking():
    """
    >>> checking()  #doctest: +ELLIPSIS
    header
    ...
    test is passed
    ...
    footer
    """
    print("header\nrandom\nlines\ntest is passed\nother\nrandom lines\nfooter")
doctest.testmod(verbose=True)

rawDescription: python doctest check for a specific line

description: Python doctest check for a specific line


code:
from itertools import product
a = [[4], [8], [15]]
[sum(p) for p in product(*a)]
a = [[3], [4, 6], [10]]
[sum(p) for p in product(*a)]

rawDescription: Sum of elements in a list of lists with varying lengths in Python


code:
from scapy.all import *

def packet_handler(pkt) :
    # if packet has 802.11 layer
    if pkt.haslayer(Dot11):
        # do your stuff here
        print(pkt.show())

sniff(iface="mon0", prn=packet_handler, filter="type Data")

rawDescription: Wireless Data Packet Capturing with help of scapy


code: sorted(pairs, lambda x, y: cmp(x[0], y[0]) or cmp(y[1], x[1]))

rawDescription: Double sort with reverse

description: Double sort with reverse


code: hello

rawDescription: I do not understand Python raw string

description: I do not understand Python raw string


code:
from mapreduce import base_handler
from mapreduce import mapreduce_pipeline
from custom_input_reader import GoogleStorageLineInputReader

def testMapperFunc(row):
    # do process with csv row
    return

class TestGCSReaderPipeline(base_handler.PipelineBase):
    def run(self):
        yield mapreduce_pipeline.MapPipeline(
            "gcs_csv_reader_job",
            "main_app.testMapperFunc",
            "custom_input_reader.GoogleStorageLineInputReader",
            params={
                "input_reader": {
                        "file_paths": ['/' + bucketname + '/' + filename]
                    }
            })

rawDescription: Upload content from a BIG CSV to CloudSQL using App Engine Python


code:
from pprint import pprint
pprint(puzzle)

rawDescription: 2d array structure python

description: 2d array structure python


code:
import csv
with open(filename, 'w') as outfile:
    writer = csv.writer(outfile)
    writer.writerow(['Word', 'Length'])
    for word in mylist:
        writer.writerow([word, str(len(word))])

rawDescription: Python Extract Word/Token counts from items in a list?


code: import mock, inspect

rawDescription: Mocking function argument names to work with inspect.getargspec


rawDescription: Convert triangle matrix to square (Python | NumPy)?


rawDescription: Assorting elements in a list into a list of lists of specific lengths


code: from package.mainwindow import MainWindow

rawDescription: Generally speaking, how are (Python) projects structured?


rawDescription: How to calculate mean of values in the list omitting a special value?

description: Calculate mean of values in the list omitting a special value


rawDescription: How to find strings in pandas

description: Find strings in pandas


code:
[[[factors[r].append(i) for r in xrange(q, n+1, q)] for q in range(i,n,i)] for i in (y for y in xrange(2,n+1) if not factors[y])]
#                                                   ^^^^^^^^^^^^^^^^^^^^^

rawDescription: Speed of different prime factor functions


code: result = DBC.fetch_db("SELECT * FROM ileti WHERE sohbet_id='" + str(sohbet_id) + "' ORDER BY created ASC")

rawDescription: Retrieving New Rows of MySQL Database without Restarting App in Python


code:
import foo # or, from foo import *
foo.a
foo.b
foo.a + foo.b

rawDescription: How do I make variable values to be saved after running a Python script in PyCharm?

description: Make variable values to be saved after running a Python script in PyCharm


code:
def uses_transparency(filename):
    img = Image.open(filename)
    trans = img.info.get("transparency", None)
    if trans is not None:
        trans *= 3  # convert color number to palette table index
        palette = img.getpalette()
        imgs = []
        for bg in [0, 255]:   # map transparent color first to black, then white
            palette[trans:trans+3] = [bg] * 3
            img.putpalette(palette)
            imgs.append(img.convert("L"))
        return bool(ImageChops.difference(*imgs).getbbox())

rawDescription: Determining if a GIF is transparent in Python


code: sorted(filter(lambda x: x > 13, l))

rawDescription: Splitting a list in python

description: Splitting a list in python


code:
class myCallable:
    def __init__(self, doc):
        self.__doc__ = doc
        self.__help__ = doc

rawDescription: Where does Python's pydoc help function get its content?


rawDescription: Splitting longer patterns using regex without losing characters Python 3+


code: a = collections.defaultdict(list)

rawDescription: Most efficient way to create a dictionary of empty lists in Python?


rawDescription: Can I use CountVectorizer in scikit-learn to count frequency of documents that were not used to extract the tokens?

description: Use CountVectorizer in scikit-learn to count frequency of documents that were not used to extract the tokens


code: result = _possibly_downcast_to_dtype(result, dtype)

rawDescription: GroupBy - How to extract seconds from DateTime with diff()


code:
df = members.groupby("groupID").agg({"brightness": np.max})
df = df.reset_index()
df = df.merge(members[["groupID", "brightness", "color"]], on=("groupID", "brightness"))

rawDescription: using pandas to create new columns based on intra-group rank-order

description: Using pandas to create new columns based on intra-group rank-order


code: {len(s): s for s in jones}.values()

rawDescription: pythonic way to filter list for elements with unique length

description: Pythonic way to filter list for elements with unique length


code:
import numpy as np
arrs = [np.random.rand(2, 3) for x in range(5)]
np.dstack(arrs).shape

rawDescription: How to create an array of dimension n+1 given a function returning an array of dimension n

description: Create an array of dimension n+1 given a function returning an array of dimension n


code:
from pyspark.ml import Pipeline

pipeline = Pipeline(stages=[assembler, kmeans_estimator])
model = pipeline.fit(df)

rawDescription: Create feature vector programmatically in Spark ML / pyspark


code:
def readfiles(pattern, filenames):
    for textfile in filenames:
        with open(textfile, 'rb') as f:
            for line in f:
                try:
                    line = ujson.loads(line.rstrip('\n').rstrip(','))
                    if pattern in line:
                        yield line['value'], line['value']['value'], line['value'], line['value']['value'], line['value']['value']
                except ValueError as err:
                    pass

def convertodf(lines):
    df = pd.DataFrame.from_records(lines)
    df.to_csv('testdf2.csv', index=False, header=None)

def main(pattern, filenames):
    lines = readfiles(pattern, filenames)
    convertodf(lines)

rawDescription: Combine Python Pandas Dataframe more Efficiently than List Append Method


code:
counter = Counter()
def button_plus(name):
    counter.value += 1 if counter.value < 10 else 0
def button_minus(name):
    counter.value -= 1 if counter.value > 0 else 0

rawDescription: Linking ipython widget button and slider values


code: result = [1,3]

rawDescription: Detect index of lower case letters

description: Detect index of lower case letters


code:
from eve import Eve

def manipulate_inbound_documents(resource, docs):
    if resource == 'activity':
        for doc in docs:
            doc['id_field'] = '001'
            doc['qr'] = 'mycqcode'

app = Eve()
app.on_insert += manipulate_inbound_documents

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

rawDescription: How to add some self process when using Python eve?

description: Add some self process when using Python eve


code: shape_temp = np.zeros_like(shape)

rawDescription: python: making array index generation more efficient/elegant

description: Python: making array index generation more efficient/elegant


code:
for r in p.xpath('.//w:t[./ancestor::w:r[count(.//w:b | .//w:bCs)=1]]',namespaces={'w': w}):
    text2 += r.text

rawDescription: Conditioning xpath on presence of sibling , lxml


code:
def test_authenticate_authid_not_found(self):
    def _raiseException(db, user):
        return defer.fail(Exception("User {} not found!".format(user)))

    self.mocked_auth_user.side_effect = _raiseException
    return self.failUnlessFailure(self.api.authenticate("test", "raito", {}), AuthenticationError)

rawDescription: How to do unit test on Autobahn applications using Twisted Trial?

description: Do unit test on Autobahn applications using Twisted Trial


code:
#in UserSelectionForm definition:
...
#__init__
for f in userlist:
    self.fields[str(f.id)] = forms.BooleanField(initial=False, required=False)
#get_selected
return filter(lambda u: self.cleaned_data[str(u.id)],
    self.custom_fields)

rawDescription: How to render django form field in template

description: Render django form field in template


code:
class MyModel(models.Model):
     video_id = ...
     embed_code = ...

     def save(self, *args, **kwargs):
         # If we have enetered a video id, but there is not saved embed code, generate it
         if not self.embed_code and self.video_id:
             self.embed_code = "http://youtube.com/%s" % self.video_id
         super(MyModel, self).save(*args, **kwargs)

rawDescription: How to differentiate capital and small letters in using Django admin "prepopulated_fields"?

description: Differentiate capital and small letters in using Django admin "prepopulated_fields"


code:
mystr = ''
# this keeps the whole operation within the for loop
for i in range(len(letters)):
    mystr += letters[i] + ' '
    if i == len(letters)-1: # this condition will be tested each iteration, but it seemed more in keeping with your question
        mystr = mystr[:-1]

rawDescription: Iterating over list of str to make a str with spaces


code:
players = list()
for items in playerPars:
    players.append(items[0])
print('The player(s) with the most pars is/are', ', '.join(players))

rawDescription: Using a for loop to select items and put them into a list


code:
def keyboard(key):
  values = {
    110: discoMode,
    27: exit
  }

rawDescription: Python: Using a dictionary as switch not working


code: {{ mydate|date:"Y-m-d" }}

rawDescription: Convert date format python

description: Convert date format python


code:
Eq(y.as_explicit(), MatAdd(MatMul(x.as_explicit(),b.as_explicit()), (e).as_explicit()))
print(latex(Eq(y.as_explicit(), MatAdd(MatMul(x.as_explicit(),b.as_explicit()), (e).as_explicit()))))

rawDescription: Sympy - Display Full Equation in Dense Matrix Form


code: vm_id = "Test_Cone_VM"

rawDescription: Xenserver API - Create VM's with local storage?


rawDescription: RegEx up to a certain group of characters


rawDescription: 'b' character added when using numpy loadtxt


rawDescription: removing "()" using python

description: Removing "()" using python


code:
forms.py
...
class ClienteForm(ModelForm):
    class Meta:
        model = Cliente
class ContaForm(ModelForm):
    class Meta:
        model = Conta
        exclude = ("cliente") #exclude the cliente field from showing up in the form rendering.

rawDescription: How integrate two ModelForms to render one form?


code:
import os
from django.conf import settings
from django.views.generic import View
from django_sse.views import BaseSseView
import time
from django.utils.timezone import now
from datetime import datetime
from django.utils.timezone import now
from dateutil import parser

class MySseEvents(BaseSseView):
    def iterator(self):
        while True:
            if not os.path.exists('/var/log/gateway'):
                file('/var/log/gateway', 'w+').close()
            with open('/var/log/gateway') as f:
                while True:
                    #get the current time
                    currenttime = now()
                    #read a line from gateway
                    line = f.readline()
                    if line:
                        #split the line read into 4 section
                        str = line.split(' ',3)
                        #compare if the time from the log file is less than the current time
                        logtime = parser.parse(str[0])
                        if logtime >= currenttime:
                            #print when the log is written after the current time
                            output = '%s: %s' % (datetime.strftime(logtime,'%d/%m %H:%M %p'), str[3])
                            self.sse.add_message("line", output)
                            time.sleep(1)
                            yield

rawDescription: Output log file through Ajax in Django

description: Output log file through Ajax in Django


code:
with open("yourFile") as fd:
    arr = [x.strip().split() for x in fd.read().split("done")][:-1]

rawDescription: Python name variable from string

description: Python name variable from string


code:
############################################################
#filename: cvplot.py
import gdb
import cv2.cv as cv
import sys

class PlotterCommand(gdb.Command):
    def __init__(self):
        super(PlotterCommand, self).__init__("plot",
                                             gdb.COMMAND_DATA,
                                             gdb.COMPLETE_SYMBOL)
    def invoke(self, arg, from_tty):
        args = gdb.string_to_argv(arg)

        # generally, we type "plot someimage" in the GDB commandline
        # where "someimage" is an instance of cv::Mat
        v = gdb.parse_and_eval(args[0])

        # the value v is a gdb.Value object of C++
        # code's cv::Mat, we need to translate to
        # a python object under cv2.cv
        image_size =  (v['cols'],v['rows'])
        # print v
        # these two below lines do not work. I don't know why
        # channel = gdb.execute("call "+ args[0] + ".channels()", False, True)
        # channel = v.channels();
        CV_8U =0
        CV_8S =1
        CV_16U=2
        CV_16S=3
        CV_32S=4
        CV_32F=5
        CV_64F=6
        CV_USRTYPE1=7
        CV_CN_MAX = 512
        CV_CN_SHIFT = 3
        CV_MAT_CN_MASK = (CV_CN_MAX - 1) << CV_CN_SHIFT
        flags = v['flags']
        channel = (((flags) & CV_MAT_CN_MASK) >> CV_CN_SHIFT) + 1
        CV_DEPTH_MAX = (1 << CV_CN_SHIFT)
        CV_MAT_DEPTH_MASK = CV_DEPTH_MAX - 1
        depth = (flags) & CV_MAT_DEPTH_MASK
        IPL_DEPTH_SIGN = 0x80000000
        cv_elem_size = (((4<<28)|0x8442211) >> depth*4) & 15
        if (depth == CV_8S or depth == CV_16S or depth == CV_32S):
                mask = IPL_DEPTH_SIGN
        else:
                mask = 0
        ipl_depth = cv_elem_size*8 | mask
        img = cv.CreateImageHeader(image_size, ipl_depth, channel)

        # conver the v['data'] type to "char*" type
        char_type = gdb.lookup_type("char")
        char_pointer_type =char_type.pointer()
        buffer = v['data'].cast(char_pointer_type)

        # read bytes from inferior's memory, because
        # we run the opencv-python module in GDB's own process
        # otherwise, we use memory corss processes
        buf = v['step']['buf']
        bytes = buf[0] * v['rows'] # buf[0] is the step? Not quite sure.
        inferior = gdb.selected_inferior()
        mem = inferior.read_memory(buffer, bytes)

        # set the img's raw data
        cv.SetData(img, mem)

        # create a window, and show the image
        cv.NamedWindow('debugger')
        cv.ShowImage('debugger', img)

        # the below statement is necessory, otherwise, the Window
        # will hang
        cv.WaitKey(0)

PlotterCommand()
############################################################

rawDescription: GDB cv::Mat python object issue when debugging a c++ program


code:
from mpl_toolkits import mplot3d

fig, ax = plt.subplots(1, 1, subplot_kw={'projection':'3d'})
xcentres = (xedges[1:] + xedges[:-1]) / 2
ycentres = (yedges[1:] + yedges[:-1]) / 2
xc, yc = np.ix_(xcentres, ycentres)
ax.plot_surface(xc, yc, H, cmap='Blues', alpha=0.5)

rawDescription: How to generate data for matplotlib filled contour plots given the real coordinates?

description: Generate data for matplotlib filled contour plots given the real coordinates


code:
urlpatterns = patterns("",
    (r"", include("myapp.urls")),
)

rawDescription: How to use one app to satisfy multiple URLs in Django

description: Use one app to satisfy multiple URLs in Django


code:
def is_there_a_path(_from, _to):
    visited = set() # remember what you visited
    while _from:
        from_node = _from.pop(0) # get a new unvisited node
        if from_node in _to:
            # went the path
            return True
        # you need to implement get_nodes_referenced_by(node)
        for neighbor_node in get_nodes_referenced_by(from_node):
            # iterate over all the nodes the from_node points to
            if neighbor_node not in visited:
                # expand only unvisited nodes to avoid circles
                visited.add(neighbor_node)
                _from.append(neighbor_node)
    return False

rawDescription: Networkx graph: finding if path exists between any node in a given set of nodes and another set of nodes


rawDescription: join function of a numpy array composed of string

description: Join function of a numpy array composed of string


rawDescription: Find location of pair of elements in two arrays in numpy


code:
from collections import defaultdict
l = ['X', 'X', 'Y', 'Z']

v = [3, 2, 3, 5]

n = defaultdict(list)

for a, b in zip(l, v):
    n[a].append(b)

print(n.values()) # [[3], [3, 2], [5]]

rawDescription: how to merge duplicate elements?

description: Merge duplicate elements


code: list(map(chr, range(97, 123))) #or list(map(chr, range(ord('a'), ord('z')+1)))

rawDescription: Alphabet range python

description: Alphabet range python


code:
local_coords = [(dot(p - loc0, locx),  # local X coordinate
                 dot(p - loc0, locy))  # local Y coordinate
                for p in points]

rawDescription: Getting local 2D coordinates of vertices of a planar polygon in 3D space


code:
def save(self, commit=True):
    instance = super(PlaylistEditForm, self).save(commit=False)
    instance.duration = instance.songs.aggregate(Sum('duration'))['duration__sum'] or 0
    instance.num_of_songs = instance.songs.count()
    if commit:
        instance.save()
    return instance

rawDescription: How to change data in Django ModelForm?

description: Change data in Django ModelForm


code: df['OMDB_GENRE_DRAMA'] = df.OMDB_GENRE.apply(lambda x: 1 if 'Drama' in x else 0)

rawDescription: Create Columns on DataFrame with parsed list values


code:
def lookup(s):
    """
    This is an extremely fast approach to datetime parsing.
    For large data, the same dates are often repeated. Rather than
    re-parse these, we store all unique dates, parse them, and
    use a lookup to convert all dates.
    """
    dates = {date:pd.to_datetime(date) for date in s.unique()}
    return s.apply(lambda v: dates[v])

rawDescription: Pandas: slow date conversion

description: Pandas: slow date conversion


rawDescription: Python Custom Zipf Number Generator Performing Poorly


code: [tag.decompose() for tag in soup("script")]

rawDescription: Remove all inline styles using BeautifulSoup


code:
from collections import defaultdict
l = [("timestamp1", 1),("timestamp1", 2),("timestamp1", 1),("timestamp2", 2)]
d = defaultdict(int)
for ts, val in l:
    d[ts] += val
print (d.items())
[('timestamp2', 2), ('timestamp1', 4)]

rawDescription: How do I merge tuples in a list based on the values?

description: Merge tuples in a list based on the values


code:
def printTriangle(n):
    if n == 0:
        # Base case
        return
    else:
        # Recursive case
        printStars(n)
        printTriangle(n-1)

rawDescription: Writing a recursive function in Python

description: Writing a recursive function in Python


code: data = data.iloc[:-2]

rawDescription: Using pandas Combining/merging 2 different Excel files/sheets


code:
tree = ElementTree.parse('your_file.xml')
root = tree.getroot()
xmldict = XmlDictConfig(root)

rawDescription: How to convert an xml string to a dictionary in Python?

description: Convert an xml string to a dictionary in Python


code:
class Personne(models.Model):
    sexe = models.CharField(max_length=1,
                            choices=[(a, b) for a, b in
                                     list(PersonneEnums.TAB_SEXE.items())],
                            default=PersonneEnums.SEXE_HOMME)

rawDescription: What is the good way to organize a long list in a model?


code:
District.objects.create(name="Arc de Triomphe")\
    .transports.add(tr_m1, tr_m2)

rawDescription: Django: how to chain 2 add() calls in 1 create()?


code:
xx = np.random.randn(nobs)
ecdf = sm.distributions.ECDF(xx)
plt.plot(ecdf.x, ecdf.y)
plt.show()

rawDescription: ECDF in python without step function?

description: ECDF in python without step function


code:
x_str='1'*8+'01'
x_int=gmpy2.xmpz(x_str, 2)
list(x_int.iter_set())
list(x_int.iter_clear())
list(x_int.iter_bits())

rawDescription: Efficiently convert gmpy2.mpz to numpy boolean array