code:
from heapq import heapify, heappop, heappush, heappushpop
import warnings
import sys

import numpy as np
from scipy import sparse

from sklearn.base import BaseEstimator, ClusterMixin
from sklearn.externals.joblib import Memory
from sklearn.externals import six
from sklearn.utils.validation import check_arrays
from sklearn.utils.sparsetools import connected_components
from sklearn.cluster import _hierarchical
from sklearn.cluster.hierarchical import ward_tree
from sklearn.cluster._feature_agglomeration import AgglomerationTransform
from sklearn.utils.fast_dict import IntFloatDict

def _fix_connectivity(X, connectivity, n_components=None,
                      affinity="euclidean"):
    """
    Fixes the connectivity matrix
        - copies it
        - makes it symmetric
        - converts it to LIL if necessary
        - completes it if necessary
    """
    n_samples = X.shape[0]
    if (connectivity.shape[0] != n_samples or
        connectivity.shape[1] != n_samples):
        raise ValueError('Wrong shape for connectivity matrix: %s '
                         'when X is %s' % (connectivity.shape, X.shape))

    # Make the connectivity matrix symmetric:
    connectivity = connectivity + connectivity.T

    # Convert connectivity matrix to LIL
    if not sparse.isspmatrix_lil(connectivity):
        if not sparse.isspmatrix(connectivity):
            connectivity = sparse.lil_matrix(connectivity)
        else:
            connectivity = connectivity.tolil()

    # Compute the number of nodes
    n_components, labels = connected_components(connectivity)

    if n_components > 1:
        warnings.warn("the number of connected components of the "
                      "connectivity matrix is %d > 1. Completing it to avoid "
                      "stopping the tree early." % n_components,
                      stacklevel=2)
        # XXX: Can we do without completing the matrix?
        for i in xrange(n_components):
            idx_i = np.where(labels == i)[0]
            Xi = X[idx_i]
            for j in xrange(i):
                idx_j = np.where(labels == j)[0]
                Xj = X[idx_j]
                D = pairwise_distances(Xi, Xj, metric=affinity)
                ii, jj = np.where(D == np.min(D))
                ii = ii[0]
                jj = jj[0]
                connectivity[idx_i[ii], idx_j[jj]] = True
                connectivity[idx_j[jj], idx_i[ii]] = True

    return connectivity, n_components

# average and complete linkage
def linkage_tree(X, connectivity=None, n_components=None,
                 n_clusters=None, linkage='complete', affinity="euclidean",
                 return_distance=False):
    """Linkage agglomerative clustering based on a Feature matrix.
    The inertia matrix uses a Heapq-based representation.
    This is the structured version, that takes into account some topological
    structure between samples.
    Parameters
    ----------
    X : array, shape (n_samples, n_features)
        feature matrix representing n_samples samples to be clustered
    connectivity : sparse matrix (optional).
        connectivity matrix. Defines for each sample the neighboring samples
        following a given structure of the data. The matrix is assumed to
        be symmetric and only the upper triangular half is used.
        Default is None, i.e, the Ward algorithm is unstructured.
    n_components : int (optional)
        Number of connected components. If None the number of connected
        components is estimated from the connectivity matrix.
        NOTE: This parameter is now directly determined directly
        from the connectivity matrix and will be removed in 0.18
    n_clusters : int (optional)
        Stop early the construction of the tree at n_clusters. This is
        useful to decrease computation time if the number of clusters is
        not small compared to the number of samples. In this case, the
        complete tree is not computed, thus the 'children' output is of
        limited use, and the 'parents' output should rather be used.
        This option is valid only when specifying a connectivity matrix.
    linkage : {"average", "complete"}, optional, default: "complete"
        Which linkage critera to use. The linkage criterion determines which
        distance to use between sets of observation.
            - average uses the average of the distances of each observation of
              the two sets
            - complete or maximum linkage uses the maximum distances between
              all observations of the two sets.
    affinity : string or callable, optional, default: "euclidean".
        which metric to use. Can be "euclidean", "manhattan", or any
        distance know to paired distance (see metric.pairwise)
    return_distance : bool, default False
        whether or not to return the distances between the clusters.
    Returns
    -------
    children : 2D array, shape (n_nodes-1, 2)
        The children of each non-leaf node. Values less than `n_samples`
        correspond to leaves of the tree which are the original samples.
        A node `i` greater than or equal to `n_samples` is a non-leaf
        node and has children `children_[i - n_samples]`. Alternatively
        at the i-th iteration, children[i][0] and children[i][1]
        are merged to form node `n_samples + i`
    n_components : int
        The number of connected components in the graph.
    n_leaves : int
        The number of leaves in the tree.
    parents : 1D array, shape (n_nodes, ) or None
        The parent of each node. Only returned when a connectivity matrix
        is specified, elsewhere 'None' is returned.
    distances : ndarray, shape (n_nodes-1,)
        Returned when return_distance is set to True.
        distances[i] refers to the distance between children[i][0] and
        children[i][1] when they are merged.
    See also
    --------
    ward_tree : hierarchical clustering with ward linkage
    """
    X = np.asarray(X)
    if X.ndim == 1:
        X = np.reshape(X, (-1, 1))
    n_samples, n_features = X.shape

    linkage_choices = {'complete': _hierarchical.max_merge,
                       'average': _hierarchical.average_merge,
                      }
    try:
        join_func = linkage_choices[linkage]
    except KeyError:
        raise ValueError(
            'Unknown linkage option, linkage should be one '
            'of %s, but %s was given' % (linkage_choices.keys(), linkage))

    if connectivity is None:
        from scipy.cluster import hierarchy  # imports PIL

        if n_clusters is not None:
            warnings.warn('Partial build of the tree is implemented '
                          'only for structured clustering (i.e. with '
                          'explicit connectivity). The algorithm '
                          'will build the full tree and only '
                          'retain the lower branches required '
                          'for the specified number of clusters',
                          stacklevel=2)

        if affinity == 'precomputed':
            # for the linkage function of hierarchy to work on precomputed
            # data, provide as first argument an ndarray of the shape returned
            # by pdist: it is a flat array containing the upper triangular of
            # the distance matrix.
            i, j = np.triu_indices(X.shape[0], k=1)
            X = X[i, j]
        elif affinity == 'l2':
            # Translate to something understood by scipy
            affinity = 'euclidean'
        elif affinity in ('l1', 'manhattan'):
            affinity = 'cityblock'
        elif callable(affinity):
            X = affinity(X)
            i, j = np.triu_indices(X.shape[0], k=1)
            X = X[i, j]
        out = hierarchy.linkage(X, method=linkage, metric=affinity)
        children_ = out[:, :2].astype(np.int)

        if return_distance:
            distances = out[:, 2]
            return children_, 1, n_samples, None, distances
        return children_, 1, n_samples, None

    if n_components is not None:
        warnings.warn(
            "n_components is now directly calculated from the connectivity "
            "matrix and will be removed in 0.18",
            DeprecationWarning)
    connectivity, n_components = _fix_connectivity(X, connectivity)

    connectivity = connectivity.tocoo()
    # Put the diagonal to zero
    diag_mask = (connectivity.row != connectivity.col)
    connectivity.row = connectivity.row[diag_mask]
    connectivity.col = connectivity.col[diag_mask]
    connectivity.data = connectivity.data[diag_mask]
    del diag_mask

    if affinity == 'precomputed':
        distances = X[connectivity.row, connectivity.col]
    else:
        # FIXME We compute all the distances, while we could have only computed
        # the "interesting" distances
        distances = paired_distances(X[connectivity.row],
                                     X[connectivity.col],
                                     metric=affinity)
    connectivity.data = distances

    if n_clusters is None:
        n_nodes = 2 * n_samples - 1
    else:
        assert n_clusters <= n_samples
        n_nodes = 2 * n_samples - n_clusters

    if return_distance:
        distances = np.empty(n_nodes - n_samples)
    # create inertia heap and connection matrix
    A = np.empty(n_nodes, dtype=object)
    inertia = list()

    # LIL seems to the best format to access the rows quickly,
    # without the numpy overhead of slicing CSR indices and data.
    connectivity = connectivity.tolil()
    # We are storing the graph in a list of IntFloatDict
    for ind, (data, row) in enumerate(zip(connectivity.data,
                                          connectivity.rows)):
        A[ind] = IntFloatDict(np.asarray(row, dtype=np.intp),
                              np.asarray(data, dtype=np.float64))
        # We keep only the upper triangular for the heap
        # Generator expressions are faster than arrays on the following
        inertia.extend(_hierarchical.WeightedEdge(d, ind, r)
                       for r, d in zip(row, data) if r < ind)
    del connectivity

    heapify(inertia)

    # prepare the main fields
    parent = np.arange(n_nodes, dtype=np.intp)
    used_node = np.ones(n_nodes, dtype=np.intp)
    children = []

    # recursive merge loop
    for k in xrange(n_samples, n_nodes):
        # identify the merge
        while True:
            edge = heappop(inertia)
            if used_node[edge.a] and used_node[edge.b]:
                break
        i = edge.a
        j = edge.b

        if return_distance:
            # store distances
            distances[k - n_samples] = edge.weight

        parent[i] = parent[j] = k
        children.append((i, j))
        # Keep track of the number of elements per cluster
        n_i = used_node[i]
        n_j = used_node[j]
        used_node[k] = n_i + n_j
        used_node[i] = used_node[j] = False

        # update the structure matrix A and the inertia matrix
        # a clever 'min', or 'max' operation between A[i] and A[j]
        coord_col = join_func(A[i], A[j], used_node, n_i, n_j)
        for l, d in coord_col:
            A[l].append(k, d)
            # Here we use the information from coord_col (containing the
            # distances) to update the heap
            heappush(inertia, _hierarchical.WeightedEdge(d, k, l))
        A[k] = coord_col
        # Clear A[i] and A[j] to save memory
        A[i] = A[j] = 0

    # Separate leaves in children (empty lists up to now)
    n_leaves = n_samples

    # # return numpy array for efficient caching
    children = np.array(children)[:, ::-1]

    if return_distance:
        return children, n_components, n_leaves, parent, distances
    return children, n_components, n_leaves, parent

# Matching names to tree-building strategies
def _complete_linkage(*args, **kwargs):
    kwargs['linkage'] = 'complete'
    return linkage_tree(*args, **kwargs)

def _average_linkage(*args, **kwargs):
    kwargs['linkage'] = 'average'
    return linkage_tree(*args, **kwargs)

_TREE_BUILDERS = dict(
    ward=ward_tree,
    complete=_complete_linkage,
    average=_average_linkage,
    )

def _hc_cut(n_clusters, children, n_leaves):
    """Function cutting the ward tree for a given number of clusters.
    Parameters
    ----------
    n_clusters : int or ndarray
        The number of clusters to form.
    children : list of pairs. Length of n_nodes
        The children of each non-leaf node. Values less than `n_samples` refer
        to leaves of the tree. A greater value `i` indicates a node with
        children `children[i - n_samples]`.
    n_leaves : int
        Number of leaves of the tree.
    Returns
    -------
    labels : array [n_samples]
        cluster labels for each point
    """
    if n_clusters > n_leaves:
        raise ValueError('Cannot extract more clusters than samples: '
                         '%s clusters where given for a tree with %s leaves.'
                         % (n_clusters, n_leaves))
    # In this function, we store nodes as a heap to avoid recomputing
    # the max of the nodes: the first element is always the smallest
    # We use negated indices as heaps work on smallest elements, and we
    # are interested in largest elements
    # children[-1] is the root of the tree
    nodes = [-(max(children[-1]) + 1)]
    for i in xrange(n_clusters - 1):
        # As we have a heap, nodes[0] is the smallest element
        these_children = children[-nodes[0] - n_leaves]
        # Insert the 2 children and remove the largest node
        heappush(nodes, -these_children[0])
        heappushpop(nodes, -these_children[1])
    label = np.zeros(n_leaves, dtype=np.intp)
    for i, node in enumerate(nodes):
        label[_hierarchical._hc_get_descendent(-node, children, n_leaves)] = i
    return label

class AgglomerativeClustering(BaseEstimator, ClusterMixin):
    """
    Agglomerative Clustering
    Recursively merges the pair of clusters that minimally increases
    a given linkage distance.
    Parameters
    ----------
    n_clusters : int, default=2
        The number of clusters to find.
    connectivity : array-like or callable, optional
        Connectivity matrix. Defines for each sample the neighboring
        samples following a given structure of the data.
        This can be a connectivity matrix itself or a callable that transforms
        the data into a connectivity matrix, such as derived from
        kneighbors_graph. Default is None, i.e, the
        hierarchical clustering algorithm is unstructured.
    affinity : string or callable, default: "euclidean"
        Metric used to compute the linkage. Can be "euclidean", "l1", "l2",
        "manhattan", "cosine", or 'precomputed'.
        If linkage is "ward", only "euclidean" is accepted.
    memory : Instance of joblib.Memory or string (optional)
        Used to cache the output of the computation of the tree.
        By default, no caching is done. If a string is given, it is the
        path to the caching directory.
    n_components : int (optional)
        Number of connected components. If None the number of connected
        components is estimated from the connectivity matrix.
        NOTE: This parameter is now directly determined from the connectivity
        matrix and will be removed in 0.18
    compute_full_tree : bool or 'auto' (optional)
        Stop early the construction of the tree at n_clusters. This is
        useful to decrease computation time if the number of clusters is
        not small compared to the number of samples. This option is
        useful only when specifying a connectivity matrix. Note also that
        when varying the number of clusters and using caching, it may
        be advantageous to compute the full tree.
    linkage : {"ward", "complete", "average"}, optional, default: "ward"
        Which linkage criterion to use. The linkage criterion determines which
        distance to use between sets of observation. The algorithm will merge
        the pairs of cluster that minimize this criterion.
        - ward minimizes the variance of the clusters being merged.
        - average uses the average of the distances of each observation of
          the two sets.
        - complete or maximum linkage uses the maximum distances between
          all observations of the two sets.
    pooling_func : callable, default=np.mean
        This combines the values of agglomerated features into a single
        value, and should accept an array of shape [M, N] and the keyword
        argument ``axis=1``, and reduce it to an array of size [M].
    Attributes
    ----------
    labels_ : array [n_samples]
        cluster labels for each point
    n_leaves_ : int
        Number of leaves in the hierarchical tree.
    n_components_ : int
        The estimated number of connected components in the graph.
    children_ : array-like, shape (n_nodes-1, 2)
        The children of each non-leaf node. Values less than `n_samples`
        correspond to leaves of the tree which are the original samples.
        A node `i` greater than or equal to `n_samples` is a non-leaf
        node and has children `children_[i - n_samples]`. Alternatively
        at the i-th iteration, children[i][0] and children[i][1]
        are merged to form node `n_samples + i`
    """

    def __init__(self, n_clusters=2, affinity="euclidean",
                 memory=Memory(cachedir=None, verbose=0),
                 connectivity=None, n_components=None,
                 compute_full_tree='auto', linkage='ward',
                 pooling_func=np.mean):
        self.n_clusters = n_clusters
        self.memory = memory
        self.n_components = n_components
        self.connectivity = connectivity
        self.compute_full_tree = compute_full_tree
        self.linkage = linkage
        self.affinity = affinity
        self.pooling_func = pooling_func

    def fit(self, X, y=None):
        """Fit the hierarchical clustering on the data
        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]
            The samples a.k.a. observations.
        Returns
        -------
        self
        """
        X = check_arrays(X)[0]
        memory = self.memory
        if isinstance(memory, six.string_types):
            memory = Memory(cachedir=memory, verbose=0)

        if self.linkage == "ward" and self.affinity != "euclidean":
            raise ValueError("%s was provided as affinity. Ward can only "
                             "work with euclidean distances." %
                             (self.affinity, ))

        if self.linkage not in _TREE_BUILDERS:
            raise ValueError("Unknown linkage type %s."
                             "Valid options are %s" % (self.linkage,
                                                       _TREE_BUILDERS.keys()))
        tree_builder = _TREE_BUILDERS[self.linkage]

        connectivity = self.connectivity
        if self.connectivity is not None:
            if callable(self.connectivity):
                connectivity = self.connectivity(X)
            connectivity = check_arrays(
                connectivity, accept_sparse=['csr', 'coo', 'lil'])

        n_samples = len(X)
        compute_full_tree = self.compute_full_tree
        if self.connectivity is None:
            compute_full_tree = True
        if compute_full_tree == 'auto':
            # Early stopping is likely to give a speed up only for
            # a large number of clusters. The actual threshold
            # implemented here is heuristic
            compute_full_tree = self.n_clusters < max(100, .02 * n_samples)
        n_clusters = self.n_clusters
        if compute_full_tree:
            n_clusters = None

        # Construct the tree
        kwargs = {}
        kwargs['return_distance'] = True
        if self.linkage != 'ward':
            kwargs['linkage'] = self.linkage
            kwargs['affinity'] = self.affinity
        self.children_, self.n_components_, self.n_leaves_, parents, \
            self.distance = memory.cache(tree_builder)(X, connectivity,
                                       n_components=self.n_components,
                                       n_clusters=n_clusters,
                                       **kwargs)
        # Cut the tree
        if compute_full_tree:
            self.labels_ = _hc_cut(self.n_clusters, self.children_,
                                   self.n_leaves_)
        else:
            labels = _hierarchical.hc_get_heads(parents, copy=False)
            # copy to avoid holding a reference on the original array
            labels = np.copy(labels[:n_samples])
            # Reasign cluster numbers
            self.labels_ = np.searchsorted(np.unique(labels), labels)
        return self

rawDescription: sklearn agglomerative clustering linkage matrix

description: Sklearn agglomerative clustering linkage matrix


rawDescription: Python: How to find sequences of readings with same id and calculating the interval between first and the last element for every such sequence


rawDescription: pandas rolling computation to find set of small numbers

description: Pandas rolling computation to find set of small numbers


code:
char_function_dict = {
    ':': colon_function,
    'eq': eq_function,
    'lt': lt_function
    # ...and so on...
}

rawDescription: Expressions in a dictionary mapping

description: Expressions in a dictionary mapping


code: d = app('Numbers').documents.first()  # reference to current top document

rawDescription: Getting the Current Table in Numbers (Python/Appscript)


code: matching = [p for p in people if p.name == 'Bob']

rawDescription: Python - testing if an object attribute is equal to a given value


code:
def extract_links(value):
    return ["https://{}.com".format(part) for part in value.split(";")]

rawDescription: Scrapy - get multiple urls from one link


code:
with open('input.txt', 'r+') as f:
   lines = f.readlines()
   for i, line in enumerate(lines):
       if 'string' in line:
          lines.insert(i,"somedata")  # inserts "somedata" above the current line
   f.truncate(0)         # truncates the file
   f.seek(0)             # moves the pointer to the start of the file
   f.writelines(lines)   # write the new data to the file

rawDescription: How do I write a string to a specific line number?

description: Write a string to a specific line number


code: fw.write("Number of files processed within 512±1 1 samples:  "+str(count))

rawDescription: Python 3.4 unicode character displayed correctly on console but no in text file


code:
'hello "howdy"'  # Correct
"hello 'howdy'"  # Correct

rawDescription: Finding data in-between two strings in python


code:
coautorshipDictionary = {
    int(k):[int(i) for i in v] for k,v in json.load(json_data)}

rawDescription: Convert a string key to int in a Dictionary


code:
a = raw_input("Type something: ")
if sys.stdin is not sys.__stdin__:
    print(a)
print("You typed in: "+a)

rawDescription: How to display the redirected stdin in Python?

description: Display the redirected stdin in Python


code:
now = datetime.now(tzoffset('EDT', -4*60*60))
print(now.isoformat())

rawDescription: How to convert date string to ISO8601 standard?

description: Convert date string to ISO8601 standard


code:
legal = set(submission) & set(permissible)
illegal = set(submission) - set(permissible)

rawDescription: check that a list contains only elements from a given "permissible" list

description: Check that a list contains only elements from a given "permissible" list


code:
l = ['abc','defg','hijkl']
map(lambda x:(x,len(x)),l)

rawDescription: Transforming a string in a list

description: Transforming a string in a list


code:
from operator import itemgetter

with open('inFile2.csv', 'rb') as f2, file('outFile.csv', 'wb') as outf:
    reader = csv.reader(f2)
    writer = csv.writer(outf)

    headers = next(reader, [])
    # produce indices for what headers are present in the ids set
    matching_indices = [i for i, header in enumerate(headers[2:], 2) if header in ids]
    selector = itemgetter(0, 1, *matching_indices)
    # write selected columns to output file
    writer.writerow(selector(headers))
    writer.writerows(selector(row) for row in reader)

rawDescription: print columns matching a header value

description: Print columns matching a header value


code:
result = re.findall('([a-zA-Z]+)-\s+(\w+)', a)
result
[''.join(item) for item in result]

rawDescription: removing relevant hyphens in text

description: Removing relevant hyphens in text


rawDescription: Keeping multiple figures open with matplotlib after script is executed


Unexpected tornado.ioloop.PeriodicCallback behavior


rawDescription: Single line statement to print range of numbers on a single line


code: saStr = " ".join(service_args)

rawDescription: I dont know how to add Proxy to my Phantomjs script


code:
df = df.drop_duplicates()
df.SampleDate = [pd.Timestamp(ts) for ts in df.SampleDate]

rawDescription: Use Python/Pandas to Match Sample Pairs Yearly Data


rawDescription: GAE datastore cache keys vs filter

description: GAE datastore cache keys vs filter


code:
from collections import Counter
x = Counter([-1,-1,-1, 1,1,1,1,0,0,0,0,0,0,0,0])
x
x.most_common(1)
x.most_common(1)[0][0]

rawDescription: Finding majority votes on -1s, 1s and 0s in list - python


rawDescription: Python list in list formatting the output


code:
a = np.arange(4)
b = np.ones(5)
mo = np.multiply.outer

rawDescription: Creating 3rd order tensors with python and numpy


code:
Namespace(cmd=['ls'], host='server', login='a')
Namespace(cmd=['ls', '-la'], host='server', login=None)

rawDescription: argparse: require optional arguments to precede positional arguments

description: Argparse: require optional arguments to precede positional arguments


code:
class file_like(ABCFileLike):
    def __init__(self):
        pass

    def write(self, line):
        print("Written:", line)

    def close(self):
        pass

    def flush(self):
        pass

print(isinstance(file_like(), ABCFileLike))  # True

rawDescription: How to make a file-like class work with `isinstance(cls, io.IOBase)`?

description: Make a file-like class work with `isinstance(cls, io.IOBase)`


code:
import itertools
import sys

def unique_substrings(txt_lst:list, k:int) -> set:
    return set([' '.join(combo) for combo in itertools.combinations(txt_lst, 3) \
                if len(set(combo))==3])

if __name__ == "__main__":
    infile, k, outfile = sys.argv[1:]
    with open(infile) as inf:
        txt_lst = infile.read().split()
    with open(outfile) as outf:
        for line in unique_substrings(txt_lst, k):
            outf.write(line + "\n")

rawDescription: Python: Finding Unique Subsequences of Unique Strings


code:
import pandas as pd

df = pd.read_csv('data.csv', delim_whitespace=True)

rawDescription: Create unique ID from the existing two columns, python


code:
def kanagrams(word,k):
    return [x[0:k] for x in set(anagram(word))]

rawDescription: Generate all k-size anagrams of a string


code:
self.numpad_1 = tk.Button(num_frame, text='1',
    command=functools.partial(self.concat, '1'))

rawDescription: How can I execute more a command based on the Button that called it? (Classes and tkinter)

description: Execute more a command based on the Button that called it? (Classes and tkinter)


code:
from binascii import unhexlify
unhexlify('000000000005FF70000000')

rawDescription: Issue with using the chr() method to add \x to characters in a list


code:
def search(text,n):
    '''Searches for text, and retrieves n words either side of the text, which are retuned seperatly'''
    word = r"\W*([\w]+)"
    groups = re.search(r'{}\W*{}{}'.format(word*n,'place',word*n), text).groups()
    return groups[:n],groups[n:]

rawDescription: Extract words surrounding a search word

description: Extract words surrounding a search word


code:
class MyDict(dict):
    def __setitem__(self, key, value):
         dict.__setitem__(self, key, 2 * value)

rawDescription: How to change the behavior of a python dictionary's __setattr__?

description: Change the behavior of a python dictionary's __setattr__


code:
import example
spam = example.Spam()
spam.action()
spam.times_two(5)
example.action()
example.times_two(21)

rawDescription: Boost.Python: Wrap functions to release the GIL


code:
title = ax.set_title('test')
title.set_position([-0.1, 1])

rawDescription: Graphs: contours, arrow and naming

description: Graphs: contours, arrow and naming


code: mkdir /mnt/pip-cache

rawDescription: PyPI is slow. How do I run my own server?


code: fit(X)

rawDescription: How to assign sample_weights in sklearn.cluster DBSCAN?

description: Assign sample_weights in sklearn.cluster DBSCAN


code:
seen = set()
[x for x in lst if (x[1] not in seen, seen.add(x[1]))[0]]

rawDescription: How to erase duplicated element in row of a list in python

description: Erase duplicated element in row of a list in python


code: sorted(pareto, key=lambda x: x[0][0], reverse=True)

rawDescription: Sort list containing both lists and scalars


code:
df.columns = [re.sub(r'[^A-Za-z0-9\\]+', '', c).strip() for c in df.columns]
for i, col in df.items():
    df.loc[:, i] = col.str.replace(r'[^A-Za-z0-9\\ ]+', '').str.strip()

rawDescription: Adding new headers and splitting column

description: Adding new headers and splitting column


rawDescription: python pandas groupby for first date

description: Python pandas groupby for first date


code:
from collections import Counter

filetypecounter = Counter()
filetypecounter.update([filetype])

rawDescription: Set value in dictionary if value not there?


rawDescription: Epoch time is one hour out from UTC, should be UTC+1


code:
conn.run_instances(
    '<ami-image-id>',
    key_name='myKey',
    instance_type='c1.xlarge',
    security_groups=['your-security-group-here'])

rawDescription: Spin up new EC2 instances programatically with Python Boto


rawDescription: How to pass more than one parameter to map function in panda

description: Pass more than one parameter to map function in panda


code:
for thisList in [listA, listB, listC]:
    thisList.sort()

rawDescription: python - change element while in for loop

description: Python - change element while in for loop


code:
class classmethod3:
    def __init__(self, thefunc, klass):
        self.f = thefunc
        self.klass = klass
    def __call__(self, *a, **k):
        return self.f(self.klass, *a, **k)

def decorate(klass, klassmethodnames):
  for n in klassmethodnames:
    thefunc = klass.__dict__[n]
    setattr(klass, n, classmethod3(thefunc, klass))

class sic2:
  def f(cls): ...
  def g(self): ...
  def h(cls): ...
decorate(sic2, ['f', 'h'])

rawDescription: backport function modifiers to python2.1

description: Backport function modifiers to python2.1


rawDescription: Numpy - summing up a list of vectors

description: Numpy - summing up a list of vectors


code: char_idle = pygame.image.load(os.path.join("Data", "char_idle.png"))

rawDescription: How to create files to and open files from a sub directory in Python?

description: Create files to and open files from a sub directory in Python


code: #ScriptInterpreterSource Registry

rawDescription: How to use same python cgi file on windows and linux (apache)?

description: Use same python cgi file on windows and linux (apache)


code:
for p in soup.find_all('p'):
    if p.string:
        p.string.replace_with(p.string.strip())

rawDescription: Remove empty spaces inside <p> tags using BeautifulSoup


code:
company = "one"
moviedata = [("one","two"), ("two","one"), ("two","three")]
print([s for s in moviedata if (company) in s])

rawDescription: Errors with obtaining and reading data from a file


code: z = [item.strip() for item in z.items()]

rawDescription: Printing a dictionary without newlines?

description: Printing a dictionary without newlines


code: documents = [([w for w in mr.words(i) if w.lower() not in stop and w not in string.punctuation], i.split('/')[0]) for i in mr.fileids()]

rawDescription: Testing the NLTK classifier on specific file


code:
def log_add(logA,logB):
    return numpy.logaddexp(0,-numpy.fabs(logB-logA)) + numpy.maximum(logA,logB)

rawDescription: Numeric function for log of sum in Python


code: any(x.lower() in new_names for x in t.split())

rawDescription: What's a more efficient way of looping with regex?


code:
from django.conf import settings
settings.ADMINS

rawDescription: E-Mail Site Admins in Django without using mail_admins()


code:
s = SomeLibraryClass(33)
s.foo(5)

rawDescription: How do I modify class method without touching source code in python?

description: Modify class method without touching source code in python


rawDescription: Python 3 - A Specific Operation on a List


code:
class One(...):
    ...
    def set_target(self, other_class):
        self.other_class = other_class
class Two(...):
    ...
one = One(...)
two = Two(...)
one.set_target(two)

rawDescription: python / tk - affecting widget in other class

description: Python / tk - affecting widget in other class


code:
from datetime import datetime

line = "1,2011,54,0,.375,-.183,2.325,1.221,0,.016,0,0,431.4,.345,1.563,25.13,13.23"

# split string and ignore 4th and every thing from the 6th element on
id, year, julian, _, value, *_ = line.split(",")

# format date, convert julian day-of-year to 'day-month'
date = "%s-%s 00:00:00" % (int(year), datetime.strptime(julian, "%j").strftime("%d-%m"))

print(",".join((date, id, value)))
#>>> 2011-23-02 00:00:00,1,.375

# could cast to numeric types if needed
#id = int(id)
#value = float(value)

rawDescription: Python parsing for MySQL import

description: Python parsing for MySQL import


code: pickle.dumps(data, 0)

rawDescription: How to pickle unicodes and save them in utf-8 databases

description: Pickle unicodes and save them in utf-8 databases


code:
import numpy as np
from pyspark.sql.functions import array, udf
from pyspark.sql.types import FloatType

combined = array(*(col(x) for x in df.columns[1:]))
median_udf = udf(lambda xs: float(np.median(xs)), FloatType())

df.select(median_udf(combined).alias("median"))

rawDescription: Spark DataFrame: Computing row-wise mean (or any aggregate operation)


code: data = data.groupby(['A','B','C','D','E']).agg(({'G' : np.mean})).reset_index()

rawDescription: python pandas beginner: multi-dimensional data-analysis workflow (groupby+agg+plot)

description: Python pandas beginner: multi-dimensional data-analysis workflow (groupby+agg+plot)


code: {'key': key, 'value': [{'name': k, 'data': v} for k, v in value.items()]}

rawDescription: Get data from dict by using for-loop in Python


rawDescription: Dynamically assigning variable with locals() isn't working in recursive function


code:
import scipy.optimize
scipy.optimize

rawDescription: Solve this equation with fixed point iteration


rawDescription: OOP conditionals for greater specificity - Python


code: [x.get_text() for x in cbar_ax.get_yticklabels()]

rawDescription: Python - Seaborn: Modifying the heatmap legend


code:
user_name = 'mauricio'
sum = 42
line_to_print = 'Hello user %s, your sum is %i' % (user_name, sum)
print(line_to_print)

rawDescription: Python-Need to turn this into a loop so it look like my picture?


code:
import multiprocessing as mp
import numpy as np
import logging
import os

logger = mp.log_to_stderr(logging.WARNING)

def free_memory():
    total = 0
    with open('/proc/meminfo', 'r') as f:
        for line in f:
            line = line.strip()
            if any(line.startswith(field) for field in ('MemFree', 'Buffers', 'Cached')):
                field, amount, unit = line.split()
                amount = int(amount)
                if unit != 'kB':
                    raise ValueError(
                        'Unknown unit {u!r} in /proc/meminfo'.format(u = unit))
                total += amount
    return total

def worker(i):
    x = data[i,:].sum()    # Exercise access to data
    logger.warn('Free memory: {m}'.format(m = free_memory()))

def main():
    procs = [mp.Process(target = worker, args = (i, )) for i in range(4)]
    for proc in procs:
        proc.start()
    for proc in procs:
        proc.join()

logger.warn('Initial free: {m}'.format(m = free_memory()))
N = 15000
data = np.ones((N,N))
logger.warn('After allocating data: {m}'.format(m = free_memory()))

if __name__ == '__main__':
    main()

rawDescription: Using multiprocessing.Manager.list instead of a real list makes the calculation take ages


code:
from BeautifulSoup import BeautifulSoup
s='[<span class="street-address">\n            510 E Airline Way\n           </span>]'
b = BeautifulSoup(s)
b.find('span').getText()

rawDescription: Python beautifulsoup trying to remove html tags 'span'


code:
class Foo(object): some_static = staticmethod(lambda x: x+1)
Foo.some_static(1)
Foo().some_static(1)
class Bar(Foo): some_static = staticmethod(lambda x: x*2)
Bar.some_static(1)
Bar().some_static(1)

rawDescription: When should I use @classmethod and when def method(self)?


code:
params = []
params.append("Task_{0}".format(i))
# ...
params.append("task{0}_types".format(i))
params.append(None)
t = Task(*params)

rawDescription: Passing formatted strings to functions as arguments


code: self.isDisarmed = True

rawDescription: Canceling events after the .after() method has been called on them?


code: avg_V = sum(sum(V))/n/m

rawDescription: non-negative matrix factorization failing to converge

description: Non-negative matrix factorization failing to converge


rawDescription: angularJS & web2py: calling python from ng/javascript/html

description: AngularJS & web2py: calling python from ng/javascript/html


code:
def prod(terms, expansions):
    if not terms: # base case
        yield ()
        return

    t = terms[0] # take the first term

    for v in expansions[t]: # expand the term, to get values
        if v not in expansions: # can the value can be expanded?
            gen = prod(terms[1:], expansions) # if not, we do a basic recursion
        else:
            gen = prod(terms[1:] + [v], expansions) # if so, we add it to terms

        for p in gen: # now we get iterate over the results of the recursive call
            yield (v,) + p # and add our value to the start

rawDescription: generating conditional product of lists in python (combinatorics)

description: Generating conditional product of lists in python (combinatorics)


code:
Mixin
class Foo(object):
    id = Column(Integer, primary_key=True)
    title = Column('title', String(200))
    link = Column('link', String(200))

class tableA(Foo, DeclarativeBase):
    __tablename__ = "A"

rawDescription: In sqlalchemy, how to define same table model(calss) quickly with only table name is different?


rawDescription: How to implement an array-like property wrapper in python?

description: Implement an array-like property wrapper in python


rawDescription: How to convert a array of dimension 3 * 200 * 120 into a 1*600 *120 in python?

description: Convert a array of dimension 3 * 200 * 120 into a 1*600 *120 in python


rawDescription: Simple python communication class error?

description: Simple python communication class error


code:
from lmfit import minimize, Parameters, Parameter, report_fit
import numpy as np

xdata = np.array([177.,180.,183.,187.,189.,190.,196.,197.,201.,202.,203.,204.,206.,218.,225.,231.,234.,
      252.,262.,266.,267.,268.,277.,286.,303.])

ydata = np.array([0.81,0.74,0.78,0.75,0.77,0.81,0.73,0.76,0.71,0.74,0.81,0.71,0.74,0.71,
      0.72,0.69,0.75,0.59,0.61,0.63,0.64,0.63,0.35,0.27,0.26])

def fit_fc(params, x, data):
    n1 = params['n1'].value
    n2 = params['n2'].value
    n3 = params['n3'].value
    n4 = params['n4'].value
    n5 = params['n5'].value

    model = n1 + (n2 * x + n3) * 1./ (1. + np.exp(n4 * (n5 - x)))

    return model - data #that's what you want to minimize

# create a set of Parameters
# 'value' is the initial condition
# 'min' and 'max' define your boundaries
params = Parameters()
params.add('n1', value= 0.2, min=0.2, max=0.8)
params.add('n2', value= -0.005, min=-0.3, max=10**(-10))
params.add('n3', value= 1.0766, min=-1000., max=1000.)
params.add('n4', value= -0.36379, min=-1000., max=1000.)
params.add('n5', value= 274.0, min=0., max=1000.)

# do fit, here with leastsq model
result = minimize(fit_fc, params, args=(xdata, ydata))

# write error report
report_fit(params)

xplot = np.linspace(min(xdata), max(xdata), 1000)
yplot = result.values['n1'] + (result.values['n2'] * xplot + result.values['n3']) * \
                              1./ (1. + np.exp(result.values['n4'] * (result.values['n5'] - xplot)))
#plot results
try:
    import pylab
    pylab.plot(xdata, ydata, 'k+')
    pylab.plot(xplot, yplot, 'r')
    pylab.show()
except:
    pass

rawDescription: Curve fit data issues

description: Curve fit data issues


code:
#create the plugins module (pseudo-package)

import sys, os

sys.modules['plugins'] = plugins = type(sys)('plugins')

plugins.__path__ = []
for plugin_dir in ['plugins1', 'plugins2']:
    path = os.path.join(sys.path[0], 'addons', plugin_dir)
    plugins.__path__.append(path)

rawDescription: Telling __import__ where to look - am I stuck with sys.path?


rawDescription: Numbers in a Matrix

description: Numbers in a Matrix


code:
from bs4 import BeautifulSoup
from multiprocessing import Pool
p = Pool(1)
j = p.apply_async(BeautifulSoup, ["<html></html>"])
j.get(timeout=5)

rawDescription: how to introduce fault tolerance into beautiful soup

description: Introduce fault tolerance into beautiful soup


code:
def switch_every(self, n):
    for i in range(0,len(self.switches), n):
        self.switch(i)   # <-- Change here

rawDescription: How to operate a SwitchBoard that turns on the following switches within a sequence n? Python 3x

description: Operate a SwitchBoard that turns on the following switches within a sequence n? Python 3x


rawDescription: Creating a shifted matrix

description: Creating a shifted matrix


code:
class Participant(object):
    def __init__(self, name, level):
        self.name = name
        self.level = level

class Team(object):
    def __init__(self, name):
        self.name = name
        self.participants = []

    def add_participant(p):
        self.participants.append(p)

rawDescription: Add an object of one class to an object of another class in python


code: df.Value1.diff() / df.Value1.shift()

rawDescription: divide a calculated value/difference by the prior value -> 'slope'

description: Divide a calculated value/difference by the prior value -> 'slope'


code:
def assemble_fast2(x, n):
    res = list(x)
    while len(res) <= n:
        res.extend(['1' if c == '0' else '0' for c in res])
    return ''.join(res)

rawDescription: How do i speed up this piece of code?

description: Speed up this piece of code


code: DOWNLOADER_MIDDLEWARES = { 'test.ntlmauth.NTLM_Middleware': 400, }

rawDescription: NTLM authentication with Scrapy for web scraping


code:
s = 'elem1 elem2 elem3 elem4 elem5'
v1, v2, v3, vrest = s.split(None, 3)
d = { 'key1': v1, 'key2': v2, 'key3': v3, 'key4': vrest.split() }

rawDescription: Transform string into dictionary (with additional constraints)


code:
data1 = data.copy()

def old(data):
    d1 = pd.melt(data, id_vars=['country','region'], value_vars=['max', 'min','default']).sort_values(['country','region']).reset_index(drop=True)

    for ridx,i in enumerate(d1['value']):
        if pd.isnull(i):
            d1['value'].loc[ridx] = d1['value'].loc[ridx-1]
        else:
            pass
    d2 = d1
    d1['key'] = 1
    d2['key'] = 1
    d3 = pd.merge(d1, d2, on='key')

    d3 = d3.drop(['key'],axis=1)

    for index, row in d3.iterrows():
        if d3['region_x'].loc[index] == d3['region_y'].loc[index] and d3['country_x'].loc[index] == d3['country_y'].loc[index]:
            pass
        else:
            d3 = d3.drop([index])
    d3 = d3.reset_index(drop=True)

    d3['rate'] = ""

    for index, row in d3.iterrows():
        if d3['country_x'].loc[index] == 'A':
            d3['rate'].loc[index] = d3['value_x'].loc[index]
        else:
            d3['rate'].loc[index] = d3['value_y'].loc[index]

    d3 = d3.drop(['value_x','country_y','region_y','value_y'],axis=1)

    d3.columns = ['country','region','Option1','Option2','measure']
    d3 = d3[['country','region','measure','Option1','Option2']]
    return d3

rawDescription: How to get columns as row combinations in python?

description: Get columns as row combinations in python


code:
import xml.etree.ElementTree as ET

chunks = iter([
    '<root>'
    '<el a="1" b=',
    '"2"><sub c="',
    '3">test</sub',
    '></el><el d=',
    '"4" e="5"></',
    'el>',
    '</root>'
    ])

class Source(object):
    def read(self, size):
        # Replace this with code that reads XML chunks from the server
        return next(chunks)

for event, elem in ET.iterparse(Source(), events=('end', )):
    if elem.tag == 'el':
        print(elem)
        # handle_element(elem)

rawDescription: Python: parsing incomplete XML chunks

description: Python: parsing incomplete XML chunks


code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
driver.close()

rawDescription: searching web-client for python that can handle frames

description: Searching web-client for python that can handle frames


code:
my_turtles = []
for i in range(num_turtles):
    x = ...
    y = ...
    h = ...
    my_turtles.append(Turtle(x, y, h))

rawDescription: How can I name my objects in Python?

description: Name my objects in Python


rawDescription: Argparse to match the first option entry only


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

x = np.arange(100)
y = x
t = x
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.scatter(x, y, c=t, cmap='viridis')
ax2.scatter(x, y, c=t, cmap='viridis_r')
plt.show()

rawDescription: Scatter plot and Color mapping in Python


code:
import csv
from collections import OrderedDict

def parse_csv(filename):

    file = open(filename, mode='r')

    csvReader = csv.reader(file)

    # get rid of header row
    header = next(csvReader)
    # print(header)

    odict = OrderedDict()
    for row in csvReader:
        odict[row[0]] = row[1:]
        # print(row)

    return odict

parse_csv('example.csv')

rawDescription: Creating an OrderedDict from a csv file

description: Creating an OrderedDict from a csv file


code:
class Shell(object):
    def m(self):
        '''Docstring of C#m.'''
        return 1
    def t(self, a):
        '''Docstring of C#t'''
        return 2

for name, obj in dict(vars(Shell)).items():
    if not name.startswith('__'): #filter builtins
        print(name, '::', obj.__doc__)

rawDescription: exposing the inner methods of a class, and using them

description: Exposing the inner methods of a class, and using them


code:
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        profile, created = Profile.objects.get_or_create(user=instance)

post_save.connect(create_user_profile, sender=User)

rawDescription: Unique referral code generate in django

description: Unique referral code generate in django


code: pd.concat([df.loc[:, ['V{}'.format(i), 'I{}'.format(i)]].rename(columns={'I{}'.format(i): 'I'}) for i in range(1, 4)])

rawDescription: Re-index data frame; fill index using elements from multiple columns


code:
for i in range(len(pattern1)):
    df.loc[df.IDnumber.str.contains(pattern1[i]) & (df.TAG == ''),'TAG'] = group[1]

rawDescription: Python pandas tagging entries in dataframe shortening code


code: new = [str(i)[-1] for i in range(length)]

rawDescription: return a single digit in strings (python 3)

description: Return a single digit in strings (python 3)


code:
from PyQt4 import QtGui, QtCore
from time import sleep

class Test(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Test, self).__init__(parent)
        button = QtGui.QPushButton("Button")
        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(button)
        self.setLayout(hbox)
        button.clicked.connect(self.slot)

    def slot(self):
        progress = QtGui.QProgressDialog(self)
        progress.setWindowModality(QtCore.Qt.WindowModal)
        progress.setLabel(QtGui.QLabel("Doing things..."))
        progress.setAutoClose(True)
        for i in range(101):
             progress.setValue(i);
             sleep(0.05)
             if progress.wasCanceled():
                 break

if __name__=="__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    myapp = Test()
    myapp.show()
    sys.exit(app.exec_())

rawDescription: Deactivate all buttons during execution - PyQt


code: [False, False, False, False]

rawDescription: kivy settings: pythonic way of dealing with list of values

description: Kivy settings: pythonic way of dealing with list of values


code: dfm

rawDescription: Re-shaping Dataframe so that Column Headers are made into Rows


code:
class Atc(models.Model): # in models.py
    drug = models.ForeignKey('Drug')
    description = models.CharField(max_length=10000)
    def __unicode__(self): return u"%s - %s" % (self.id, self.description)

class AtcInline(admin.StackedInline): # in admin.py
    # set option for not being able to add more than one.
    model = Atc
    fields = ['description']

rawDescription: django ForeignKey display

description: Django ForeignKey display


rawDescription: Python : match string inside double quotes and bracket


code:
if ModelMetadata is not None:
    try:
        model_md = ModelMetadata.objects.get(_content_type=content_type, _language=language)
    except ModelMetadata.DoesNotExist:
        model_md = ModelMetadata(_content_type=content_type, _language=language)

    model_md._content_object = obj
    instances.append(model_md)

rawDescription: Django SEO app, integrating external data like 'Best {{ product }} of the year'


code:
from itertools import chain,izip_longest
''.join(chain.from_iterable(izip_longest(word1,*nwords,fillvalue='')))

rawDescription: Python: Unpack individual strings from tuple using Unpacking asterisk argument


code:
idx = a.data.argsort()[::-1][:1] #k is 1
a.data[idx] += 1

rawDescription: Increasing value of top k elements in sparse matrix


code:
y = test.large_message()
print(sys.getrefcount(y))
print(y.header)
z = [y.header]
print(sys.getrefcount(y))
z += [y.header]
print(sys.getrefcount(y))

rawDescription: Reference counting for SWIG-ed C structs containing complex types doesn't seem to work as expected


code:
import time
def infiniteloop():
    while True:
        print('Loop 1')
        time.sleep(1)
        print('Loop 2')
        time.sleep(1)

infiniteloop()

rawDescription: Can two infinite loops be ran at once?

description: Can two infinite loops be ran at once


code:
import string
b'!' in string.printable.encode()

rawDescription: Python - How can I change bytes in a file


code:
class Site(models.Model):
    name = models.CharField(null=False, blank=False, max_length=256, unique=True)
    script = models.CharField(null=False, blank=False, max_length=256)
    def __unicode__(self):
       return u"{0}".format(self.name)

rawDescription: Django 1.9: Add a custom button to run a python script on clicking in admin site of a app/model


code: all_accepted_parts = acceptedFragment.objects.filter(fragment__in = fragment.objects.filter(categories = fragmentCategory.objects.get(id=1)))

rawDescription: Really long query

description: Really long query


code:
from datetime import datetime
from pytz import timezone, country_timezones, utc

_utc = utc
dte = "2002-08-22T10:00:00Z"
utc = datetime.strptime(dte, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=_utc)
coun_to_tz = {tz: coun for coun, v in country_timezones.items() for tz in v}

for _tz, coun in coun_to_tz.items():
    tz = timezone(_tz)
    dt = utc.astimezone(tz)
    if str(dt.time()) == "00:00:00":
        print(coun)
        print(dt)
        print(_tz)

rawDescription: Translating timestamp to Human Readable format


code:
import sys
import markdown2

with open(sys.argv[0]) as f:
    script = 'tell application "Mail"' \
       'make new outgoing message with properties {' \
       'visible:true,content:"%s" }' \
    'end tell' % markdown2.markdown(f.read())

    p = Popen('/usr/bin/osascript',stdin=PIPE,stdout=PIPE)
    p.communicate(script)

rawDescription: Sublime Text Markdown-to-HTML-email

description: Sublime Text Markdown-to-HTML-email


code:
import numpy as np
import pandas as pd
from bokeh.charts import HeatMap, output_file, show

cats = ['active', 'based', 'viewers', 'situational']
df = pd.DataFrame({'val': np.random.randint(1,100, 1000), 'category': np.random.choice(cats, 1000)})

hm = HeatMap(df)
output_file('d:/temp/heatmap.html')
show(hm)

rawDescription: Single variable category scatter plot pandas


code:
with open('file.txt') as f:
    lines = [l.replace(r'"','').rstrip('\n').split('\t',1) for l in f]

rawDescription: Python: What's a fast way to read and split a file?


code:
@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False,
                    submenu=False):
    res = super(res_partner, self).fields_view_get(
        view_id=view_id, view_type=view_type, toolbar=toolbar,
        submenu=submenu)

    checking_obj = self.env['my.own.checkings']
    doc = etree.XML(res['arch'])

    if checking_obj.belongs_to_accountant_group():
        doc.set('create', 'false')
        doc.set('delete', 'false')
        doc.set('edit', 'true')

    if view_type == 'form':
        accounting_pages = doc.xpath("//page[@name='accounting']")
        accounting_page = accounting_pages[0] if accounting_pages \
            else False

        if accounting_page is not False:
            if checking_obj.belongs_to_accountant_group():
                all_fields = doc.xpath("//field")
                for field in all_fields:
                    if accounting_page not in field.iterancestors():
                        checking_obj.set_modifiers(field,
                                                   {'readonly': True, })

    res['arch'] = etree.tostring(doc)
    return res

rawDescription: How to allow an user of a group to modify specific parts of a form in Odoo?

description: Allow an user of a group to modify specific parts of a form in Odoo


code:
candidates = []
results = []
for line in my_file:
    candidates.append(line.split('\t'))
for line in candidates:
    seen = false
    for possible_match in results:
        if matching_line(possible_match, line):
            seen = true
    if seen:
        continue
    else:
        results.append(line)

rawDescription: Unique duplicate rows with range

description: Unique duplicate rows with range


code:
(False, "The tricky thing failed!")
{ "status": False, "message": "The tricky thing failed!" }
ResultObject( status=False, message="The tricky thing failed!" )

rawDescription: Rubyist in Pythonland: what is a real world example of a tuple?


code:
df1_merge = df1_melt.merge(df2, left_on='value', right_on='nodeID')
df1_merge.groupby('eID')[['dx','dy','dz']].mean()

rawDescription: Averaging values between Pandas dataframes with keys existing in one and data in the other


code:
import bisect
genes[bisect.bisect_left(keys, (550, 580))-1]
genes[bisect.bisect_left(keys, (110, 180))-1]

rawDescription: Bisecting lists containing multi-value entries? Advice on approaching this task


code:
M = zeros((4,4))
M.T.flat[:4] = 1
print(M)

rawDescription: Numpy: Assignment and Indexing as Matlab


rawDescription: How to convert the Raw part to normal format from a pcap file?

description: Convert the Raw part to normal format from a pcap file


code:
from .apple import Apple
from .grape import Grape

__all__ = ['Apple', 'Grape']

rawDescription: Python modules vs classes

description: Python modules vs classes


code: summaryA = super_do("a", "/output/path/a.tsv")

rawDescription: "Pointer to member" in python

description: "Pointer to member" in python


code:
Output
{
  "origin": "x.x.x.x",
  "files": {},
  "form": {},
  "url": "http://httpbin.org/post",
  "args": {},
  "headers": {
    "Content-Length": "48",
    "Accept-Encoding": "identity, deflate, compress, gzip",
    "Connection": "keep-alive",
    "Accept": "*/*",
    "User-Agent": "python-requests/0.13.9 CPython/2.7.3 Linux/3.2.0-30-generic",
    "Host": "httpbin.org",
    "Content-Type": "application/xml"
  },
  "json": null,
  "data": "<?xml version='1.0' encoding='utf-8'?>\n<a>\u0431</a>"
}

rawDescription: How can I send an xml body using requests library?

description: Send an xml body using requests library


code:
if sys.byteorder != 'little':
    arr.byteswap()

rawDescription: Reading 4 byte integers from binary file in Python


code:
class Command(BaseCommand):

    def handle(self, *args, **options):
        f = open('/tmp/output', 'w+')
        out = StringIO()
        management.call_command('basequery', 'list', 'log', stdout=out)
        out.seek(0)
        f.write(out.read())
        f.close()

rawDescription: Redirect management.call_command() stdout to a file


code:
d = {'q1': {'d1': 1.2 , 'd5': 5.2},
     'q2': {'d4': 3.23, 'd7': 3.9 , 'd8': 4.1},
     'q3': {'d3': 0.5}}

# First, flatten to a list of one key-value pair dictionaries
inner_dicts = [
    dict([(k,v)])
    for inner_d in d.values()
        for (k, v) in inner_d.items()]

# Second, sort that list based on the value in each dict
dictlist_sorted = sorted(
    inner_dicts,
    key=lambda temp_d: list(temp_d.values())[0],
    reverse=True)

# Third, extract the key of each dict
keys_sorted = [list(temp_d.keys())[0] for temp_d in dictlist_sorted]

print(keys_sorted)

rawDescription: How to order a dictionary of dictionaries based on inner dictionary's value

description: Order a dictionary of dictionaries based on inner dictionary's value


code:
import wx

class StaticWrapText(wx.PyControl):
   def __init__(self, parent, id=wx.ID_ANY, label='', pos=wx.DefaultPosition,
                size=wx.DefaultSize, style=wx.NO_BORDER,
                validator=wx.DefaultValidator, name='StaticWrapText'):
      wx.PyControl.__init__(self, parent, id, pos, size, style, validator, name)
      self.statictext = wx.StaticText(self, wx.ID_ANY, label, style=style)
      self.wraplabel = label
      #self.wrap()
   def wrap(self):
      self.Freeze()
      self.statictext.SetLabel(self.wraplabel)
      self.statictext.Wrap(self.GetSize().width)
      self.Thaw()
   def DoGetBestSize(self):
      self.wrap()
      #print self.statictext.GetSize()
      self.SetSize(self.statictext.GetSize())
      return self.GetSize()

class MyForm(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")

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

        text = "I'm subclasses the statictext because I want it to act exactly like a static text, but correctly wordwrap as needed. I've found several examples of it on the web, but none that worked how I wanted. The wordwrap makes it look much nicer when the user may decide to re-size the window, so I would definitely like to have it be wordwrapped. I know about the wx.lib.wordwrap, but chose to use the built in Wrap function of the statictext control instead. It basically does the same thing from what I understand."
        txt = StaticWrapText(self.panel, label=text)
        wxbutton = wx.Button(self.panel, label='Button', size=wx.Size(120,50))
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(txt,      0, wx.EXPAND, 5)
        sizer.Add(wxbutton, 1, wx.EXPAND, 5)
        self.panel.SetSizer(sizer)

# Run the program
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm()
    frame.Show()
    #frame.panel.SendSizeEvent()
    frame.panel.GetSizer().Layout()
    app.MainLoop()

rawDescription: wxPython problems with Wrapping StaticText

description: WxPython problems with Wrapping StaticText


code:
with open('cover.tex','w') as f:
    f.write(content%args.__dict__)

rawDescription: Generating pdf-latex with python script

description: Generating pdf-latex with python script


code:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from oscar.app import application
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^i18n/', include('django.conf.urls.i18n')),
    url(r'', include(application.urls)),
)+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

rawDescription: Django-Oscar Images Error

description: Django-Oscar Images Error


code:
def sets_2(n, k=3, s=1):
    for x in range(s, (n // k) + 1):
        if k == 2:
            yield x, n - x
        else:
            for s in sets_2(n - x, k - 1, x):
                yield (x, ) + s

rawDescription: Generating products of lists that sum to a parameter N


code:
import math

for i in range(1, N + 1):
    angle = (i - 1) * math.pi * 2 / N
    x_coord = radius * math.cos(angle)
    y_coord = radius * math.sin(angle)
    z_coord = 0
    cube = place_cube(x_coord, y_coord, z_coord)

rawDescription: I need help forming a circle with cubes, useing blender 2.69 with python engine


code:
logging.basicConfig(level=logging.NOTSET, format='[%(process)-5d:%(threadName)-10s] %(name)-25s: %(levelname)-8s %(message)s')
logger = logging.getLogger(__name__)

rawDescription: Python logger not picking up the configure time format.


code: screen = pygame.display.set_mode((screen_width, screen_height), 0, 32)

rawDescription: pygame error: TypeError: must be 2-item sequence

description: Pygame error: TypeError: must be 2-item sequence


rawDescription: Programmatically accessing arbitrarily deeply-nested values in a dictionary


code:
from flask import Flask, url_for
from werkzeug.routing import BaseConverter, ValidationError

class User:

    def __init__(self, id, name):
        self.id = id
        self.name = name

class UserConverter(BaseConverter):
    """Converts Users for flask URLs."""

    def to_python(self, value):
        """Called to convert a `value` to its python equivalent.

        Here, we convert `value` to a User.
        """
        # Anytime an invalid value is provided, raise ValidationError.  Flask
        # will catch this, and continue searching the other routes.  First,
        # check that value is an integer, if not there is no way it could be
        # a user.
        if not value.isdigit():
            raise ValidationError()
        user_id = int(value)

        # Look up the user in the database.
        if user_id not in _database:
            raise ValidationError()

        # Otherwise, return the user.
        return _database[user_id]

    def to_url(self, value):
        """Called to convert a `value` to its `url` equivalent.

        Here we convert `value`, the User object to an integer - the user id.
        """
        return str(value.id)

# Create a `database` of users.
_database = {
    1: User(1, 'Bob'),
    2: User(2, 'Jim'),
    3: User(3, 'Ben')
}

app = Flask(__name__)
app.url_map.converters['user'] = UserConverter

@app.route('/find/<user:user>')
def find_user(user):
    return "User: %s" % user.name

@app.route('/find/<user:user>/extrapath')
def find_userextra(user):
    return 'User extra: %s' % user.name

@app.route('/users')
def list_users():
    # Return some broken HTML showing url's to our users.
    s = ''
    for user in _database.values():
        s += url_for('find_user', user=user) + '<br/>'
    return s

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

rawDescription: Flask multiple URL handlers process URLs partially


rawDescription: How to use a bundling/packetizing generator in Python?

description: Use a bundling/packetizing generator in Python


code:
def foo(a, b, c):
    return a + b if c else a * b
reduce(functools.partial(foo, c=True), [1,2,3,4,5], 0)
reduce(functools.partial(foo, c=False), [1,2,3,4,5], 1)

rawDescription: Passing more than two arguments in reduce function


code:
password = db.Column(db.String)  # no length
# or
password = db.Column(db.String(255))  # some longer length

rawDescription: Why is check_password_hash always returning False in this login view?


rawDescription: how to speed up a vector cross product calculation

description: Speed up a vector cross product calculation


code: re.findall(r"EVENT\((X.*?)\)", s)

rawDescription: Getting strings in between two keywords from a file in python


code:
def find_instance(fn, objs):
    all_matches = filter(fn, objs)
    return len(all_matches) > 0 and all_matches[0] # uses the fact that 'and' returns its second argument if its first argument evaluates to True.

rawDescription: Get class instance where a certain property has a certain value


rawDescription: Getting last item this line using python


code:
memory = [1, 2, 3, 4, None, None, None, None]
index = memory.index(None)
memory[index:index + 4] = 'foo', 'bar', 'spam', 'eggs'
memory

rawDescription: unpacking class variables as a list

description: Unpacking class variables as a list


code:
import csv
from itertools import product
from collections import Counter

rdr = csv.reader(open(r"data.csv"), quotechar='"',delimiter='|')
c = Counter((x, y) for _, a, b in rdr for x, y in product(a.split(), b.split()))

rawDescription: Finding word co-occurrences

description: Finding word co-occurrences


code:
grp = df.groupby('B')
s = grp.apply(lambda x : set(x['A']))
s

rawDescription: Count size of rolling intersection in pandas


code:
def ppt(data):
    if isinstance(data, int):
        return "Int()"
    elif isinstance(data, str):
        return "Str()"
    elif isinstance(data, list):
        if len(data) == 0:
            return "List()"
        else:
            return "List()<" + ", ".join([ppt(item) for item in data]) + ">"
    elif isinstance(data, dict):
        if len(data) == 0:
            return "Dict()"
        else:
            return "Dict()<" + ", ".join([ppt(data[key]) for key in data]) + ">"
    else:
        return "Unknown()"

rawDescription: Print data-structure/ Data-type hierarchy


code:
SST = Sum(i=1..n) (y_i - y_bar)^2
SSReg = Sum(i=1..n) (y_ihat - y_bar)^2
Rsquared = SSReg/SST

rawDescription: How do I calculate r-squared using Python and Numpy?

description: Calculate r-squared using Python and Numpy


code: map('state', plot = TRUE, fill = FALSE, col = palette())

rawDescription: Detecting geographic clusters

description: Detecting geographic clusters


code: print(table.update().values(col1 = select_query.with_only_columns([table2.c.col1]), col2 = select_query.with_only_columns([table2.c.col2])))

rawDescription: Updating a table from another table with multiple columns in sqlalchemy


code:
DF = DataFrame()
for sample,data in D_sample_data.items():
    SR_row = pd.Series(data.D_key_value,name=sample)
    DF = DF.append(SR_row)
DF.head()

rawDescription: Is it possible to append Series to rows of DataFrame without making a list first?


rawDescription: Different slicing behaviors on left/right hand side of assignment operator


code:
import example
factory = example.SpamFactory()
spam = factory.create("test")
with spam:
    assert(bool(spam) == True)
    if spam:
        assert(spam.name == "test")
        factory.destroy()
        assert(bool(spam) == True)
        assert(spam.name == "test")

assert(bool(spam) == False)

rawDescription: Using weak pointers that evaluate to a None type


rawDescription: Grouping pandas data frame by time intervals


code:
class A(object):
    def __init__(self, i):
        self.i = i
    def run(self, value):
        return self.i * value

class B(A):
    def __init__(self, i, j):
        super(B, self).__init__(i)
        self.j = j
    def run(self, value):
        return super(B, self).run(value) + self.j

rawDescription: Why is super() broken in Python-2.x?

description: Why is super() broken in Python-2.x


code:
import psycopg2
conn = psycopg2.connect("dbname='mydb' user='username' host='localhost' password='foobar'")
cur = conn.cursor()
cur.execute("select * from information_schema.tables where table_name=%s", ('mytable',))
bool(cur.rowcount)

rawDescription: Checking if a postgresql table exists under python (and probably Psycopg2)


code:
from PyQt4 import QtGui

app = QtGui.QApplication([])

w = QtGui.QMainWindow()
menu = QtGui.QMenu("menu", w)
ag = QtGui.QActionGroup(w, exclusive=True)

a = ag.addAction(QtGui.QAction('50%', w, checkable=True))
menu.addAction(a)

a = ag.addAction(QtGui.QAction('100%', w, checkable=True))
menu.addAction(a)

a = ag.addAction(QtGui.QAction('200%', w, checkable=True))
menu.addAction(a)

a = ag.addAction(QtGui.QAction('300%', w, checkable=True))
menu.addAction(a)

a = ag.addAction(QtGui.QAction('400%', w, checkable=True))
menu.addAction(a)

w.menuBar().addMenu(menu)
w.show()
app.exec_()

rawDescription: how to make qmenu item checkable pyqt4 python

description: Make qmenu item checkable pyqt4 python


code:
usd_curr = [
    float(val)
    for val, val_type in (elem['currency'].split(',') for elem in curr)
    if val_type == 'USD' and float(val) > 0
]

rawDescription: python 3.4 list comprehension - calling a temp variable within list

description: Python 3.4 list comprehension - calling a temp variable within list


code:
datetime.datetime.today()
datetime.datetime.today() + datetime.timedelta(days=1)

rawDescription: Arithmetic on datetime objects

description: Arithmetic on datetime objects


rawDescription: Convert float64 numpy array to floats not in scientific notation


code:
def main():
    print('Select from  the list below which operation you want the calculator to do.')
    print("A.Add")
    print("S.Subtract")
    print("M.Multiply")
    print("D.Divide")
    while True:
        choice = input("Enter choice(a/s/m/d) or q to quit:")
        if choice not in {"a", "s", "m", "d","q"}:
            print (" the letter you entered is not in our lists!")
            continue # if invalid input, ask for input again
        elif choice == "q":
            print("Goodbye.")
            break
        num1 = int(input("Enter an integer as your first number: "))
        num2 = int(input("Enter an integer as second number: "))
        if choice == 'a':
            print("{} + {} = {}".format(num1, num2, add(num1, num2)))
        elif choice == 's':
            print("{} - {} = {}".format(num1, num2, subtract(num1, num2)))

rawDescription: how to add restart command in python?

description: Add restart command in python


code: df[day_names] = df.days.apply(lambda x: pd.Series(x))

rawDescription: Pandas - How do I subset a column composed of list objects?


code:
@app.route("/approve/<int:post_id>")
def approve(post_id):
    """approve this post!"""

rawDescription: python + flask passing data from html back to python script (specifically the value of a label)

description: Python + flask passing data from html back to python script (specifically the value of a label)


code:
fig = plt.figure()
ax = fig.add_subplot(111)

x = 1.5*10**(-2)
l = ("%.0e"%x).split("e")
x_str = r"$ %s \times 10^{%s}$" % (l[0], l[1] )

ax.set_title(x_str)

plt.show()

rawDescription: Formatting numbers consistently in Python


code:
# contents of C:\tmp\eggs\libs\foo-2.0.egg\foo.py
version=(2,0)

rawDescription: How can I call python module inside versioned package folder?

description: Call python module inside versioned package folder


code: Photo.query.filter(id == id).first()

rawDescription: Flask SQLalchemy - many to many - show photo with all tags (or blog with all posts etc)


code:
dict1 = {1: 'val1', 2: 'val2', 3: 'val3'}
literal = repr(dict1)

print(literal)
# Output: {1: 'val1', 2: 'val2', 3: 'val3'}

rawDescription: Outputting a dictionary to file exactly as defined in variable


rawDescription: Map a function's argument, which is a lambda, to a tuple?


code:
words = list(dict_gen(5))
len(words) == 26 + 26**2 + 26**3 + 26**4 + 26**5  # verify correct length
words[20:30]  # transition from one letter to two letters
words[-10:]   # last 10 elements

rawDescription: improve this very-simple dictionary generator in python

description: Improve this very-simple dictionary generator in python


code: return (self.year and (self.year == date.year)) or True

rawDescription: Logical operator "and" in python

description: Logical operator "and" in python


code:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
matplotlib.interactive(True)

fig, ax = plt.subplots()

map = Basemap(projection='merc', lat_0 = 57, lon_0 = -135,
    resolution = 'i', area_thresh = 0.1,
    llcrnrlon=-136.25, llcrnrlat=56.0,
    urcrnrlon=-134.25, urcrnrlat=57.75)

map.drawcoastlines(zorder = 0)
map.drawcountries(zorder = 0)
map.fillcontinents(color = 'lightgray', zorder = 0)
map.drawmapboundary(zorder = 0)

lons = [-135.3318, -134.8331, -134.6572]
lats = [57.0799, 57.0894, 56.2399]
x,y = map(lons, lats)
pts = map.scatter(x, y, c ='r', marker = 'o', s = 600)

rawDescription: Get working alpha value of scatter points in mpl_toolkits.basemap.Basemap


code: sorted(my_list, key=lambda x: x[1])

rawDescription: Python equivalent for PHP's usort()?

description: Python equivalent for PHP's usort()


code:
def lucas_kanade_np(im1, im2, win=2):
    assert im1.shape == im2.shape
    I_x = np.zeros(im1.shape)
    I_y = np.zeros(im1.shape)
    I_t = np.zeros(im1.shape)
    I_x[1:-1, 1:-1] = (im1[1:-1, 2:] - im1[1:-1, :-2]) / 2
    I_y[1:-1, 1:-1] = (im1[2:, 1:-1] - im1[:-2, 1:-1]) / 2
    I_t[1:-1, 1:-1] = im1[1:-1, 1:-1] - im2[1:-1, 1:-1]
    params = np.zeros(im1.shape + (5,)) #Ix2, Iy2, Ixy, Ixt, Iyt
    params[..., 0] = I_x * I_x # I_x2
    params[..., 1] = I_y * I_y # I_y2
    params[..., 2] = I_x * I_y # I_xy
    params[..., 3] = I_x * I_t # I_xt
    params[..., 4] = I_y * I_t # I_yt
    del I_x, I_y, I_t
    cum_params = np.cumsum(np.cumsum(params, axis=0), axis=1)
    del params
    win_params = (cum_params[2 * win + 1:, 2 * win + 1:] -
                  cum_params[2 * win + 1:, :-1 - 2 * win] -
                  cum_params[:-1 - 2 * win, 2 * win + 1:] +
                  cum_params[:-1 - 2 * win, :-1 - 2 * win])
    del cum_params
    op_flow = np.zeros(im1.shape + (2,))
    det = win_params[...,0] * win_params[..., 1] - win_params[..., 2] **2
    op_flow_x = np.where(det != 0,
                         (win_params[..., 1] * win_params[..., 3] -
                          win_params[..., 2] * win_params[..., 4]) / det,
                         0)
    op_flow_y = np.where(det != 0,
                         (win_params[..., 0] * win_params[..., 4] -
                          win_params[..., 2] * win_params[..., 3]) / det,
                         0)
    op_flow[win + 1: -1 - win, win + 1: -1 - win, 0] = op_flow_x[:-1, :-1]
    op_flow[win + 1: -1 - win, win + 1: -1 - win, 1] = op_flow_y[:-1, :-1]
    return op_flow

rawDescription: Lucas Kanade python numpy implementation uses enormous amount of memory


code:
def include( filename, classname, doReload=False ):
    # http://www.php2python.com/wiki/function.include/
    # http://docs.python.org/dev/3.0/whatsnew/3.0.html
    # http://stackoverflow.com/questions/4821104/python-dynamic-instantiation-from-string-name-of-a-class-in-dynamically-imported

    #_locals_ = {'Contemplate': Contemplate}
    #_globals_ = {'Contemplate': Contemplate}
    #if 'execfile' in globals():
    #    # Python 2.x
    #    execfile(filename, _globals_, _locals_)
    #    return _locals_[classname]
    #else:
    #    # Python 3.x
    #    exec(Contemplate.read(filename), _globals_, _locals_)
    #    return _locals_[classname]

    # http://docs.python.org/2/library/imp.html
    # http://docs.python.org/2/library/functions.html#__import__
    # http://docs.python.org/3/library/functions.html#__import__
    # http://stackoverflow.com/questions/301134/dynamic-module-import-in-python
    # http://stackoverflow.com/questions/11108628/python-dynamic-from-import
    # also: http://code.activestate.com/recipes/473888-lazy-module-imports/
    # using import instead of execfile, usually takes advantage of Python cached compiled code

    global _G
    getClass = None
    directory = _G.cacheDir
    # add the dynamic import path to sys
    os.sys.path.append(directory)
    currentcwd = os.getcwd()
    os.chdir(directory)   # change working directory so we know import will work

    if os.path.exists(filename):

        modname = filename[:-3]  # remove .py extension
        mod = __import__(modname)
        if doReload: reload(mod) # Might be out of date
        # a trick in-order to pass the Contemplate super-class in a cross-module way
        getClass = getattr( mod, '__getClass__' )

    # restore current dir
    os.chdir(currentcwd)
    # remove the dynamic import path from sys
    del os.sys.path[-1]

    # return the Class if found
    if getClass:  return getClass()
    return None

rawDescription: Python: how to create a class name on the fly


code: rand_smpl = [ mylist[i] for i in sorted(random.sample(xrange(len(mylist)), 4)) ]

rawDescription: Get random sample from list while maintaining ordering of items?


rawDescription: Django: How to write the reverse function for the following


code: print(nums)

rawDescription: Extracting digits in line and storing them to list in Python


code:
dct = {}
bool(dct)
not dct

rawDescription: Python: Checking if a 'Dictionary' is empty doesn't seem to work


rawDescription: Python/Numpy/Scipy - Converting string to mathematical function


code:
import os
import sys
import ctypes, ctypes.util

kernel32 = ctypes.WinDLL('kernel32')

STD_OUTPUT_HANDLE = -11

if sys.version_info < (3, 5):
    libc = ctypes.CDLL(ctypes.util.find_library('c'))
else:
    if hasattr(sys, 'gettotalrefcount'): # debug build
        libc = ctypes.CDLL('ucrtbased')
    else:
        libc = ctypes.CDLL('api-ms-win-crt-stdio-l1-1-0')

    # VC 14.0 doesn't implement printf dynamically, just
    # __stdio_common_vfprintf. This take a va_array arglist,
    # which I won't implement, so I escape format specificiers.

    class _FILE(ctypes.Structure):
        """opaque C FILE type"""

    libc.__acrt_iob_func.restype = ctypes.POINTER(_FILE)

    def _vprintf(format, arglist_ignored):
        options = ctypes.c_longlong(0) # no legacy behavior
        stdout = libc.__acrt_iob_func(1)
        format = format.replace(b'%%', b'\0')
        format = format.replace(b'%', b'%%')
        format = format.replace(b'\0', b'%%')
        arglist = locale = None
        return libc.__stdio_common_vfprintf(
            options, stdout, format, locale, arglist)

    def _printf(format, *args):
        return _vprintf(format, args)

    libc.vprintf = _vprintf
    libc.printf = _printf

rawDescription: stdout redirection with ctypes and Python

description: Stdout redirection with ctypes and Python


code:
from functools import reduce
from operator import mul
lst = [1,7,3,4]
prod = reduce(mul,lst)
newl = [prod//i for i in lst]
newl

rawDescription: Product of integer in the list except the integer at that index


code:
data=np.random.randint(10,size=(5,10))
[len(set(i)) for i in data.reshape(data.shape[0],-1)]
sdata=np.sort(data,axis=1)
(np.diff(sdata)>0).sum(axis=1)+1

rawDescription: vectorize numpy unique for subarrays

description: Vectorize numpy unique for subarrays


code:
class MyModel(models.Model):
    name = models.CharField(max_length=255)
    code = models.CharField(MAX_FILE_LENGTH)

rawDescription: Django FileField: how to set default value (auto-create empty file)?


rawDescription: Python select ith element in OrderedDict


code: ["{} {}".format(first, item) for item in last.split(",")]

rawDescription: Python 3.3: Split string and create all combinations


rawDescription: Python and appending items to text and excel file


code:
import pystache
import os
pystache.render("{{PATH}}",os.environ)

rawDescription: how do i parse environment variables with pystache?

description: Parse environment variables with pystache


rawDescription: python - reading file, modifiing it and saving, but it is saved with no modifications. why?

description: Python - reading file, modifiing it and saving, but it is saved with no modifications. why


code:
from django.db.models import DateTimeField, ExpressionWrapper, F

Post.objects.annotate(
        timestamp_minus_duration=ExpressionWrapper(
            F('timestamp') + F('duration'),
            output_field=DateTimeField()
        )
    )

rawDescription: How to filter using an expression in Django

description: Filter using an expression in Django


code:
def add(x, y):
    return x+y
map(functools.partial(add, y=10), [1, 2, 3])

rawDescription: How to specify arguments in map in Python?

description: Specify arguments in map in Python


rawDescription: Get name of QPushButton

description: Get name of QPushButton


code:
l = LazyClass()
l.test1
l.test2

rawDescription: Batch-adding properties in Python?

description: Batch-adding properties in Python


code: abs(z)

rawDescription: How to avoid getting imaginary/complex number python

description: Avoid getting imaginary/complex number python


code:
def conv_tz2(dt, dest_tz):
    if not dt: return None
    return datetime.fromtimestamp(dt.timestamp(), dest_tz)

rawDescription: pytz timezone conversion performance

description: Pytz timezone conversion performance


code:
import sys
import os
from stat import S_ISFIFO

if S_ISFIFO(os.fstat(0).st_mode):
    print("Reading from pipe")
    cmd = sys.stdin.read()
    print(cmd)
else:
    print("not reading from pipe")

rawDescription: Is there a way for a python script to "know" if it is being piped to?


code: exec("print(input());"*eval(input()))

rawDescription: exec in Python 3

description: Exec in Python 3


code:
import numpy as np

series = np.array([1.1, 2.1, 3.1, 7.1, 8.1, 9.1, 10.1, 14.1, 15.1, 16.1, 20.1])
data_a = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
data_b = [1.2, 1.2, 1.2, 2.2, 2.2, 2.2, 2.2, 3.2, 3.2, 3.2, 4.2]

d_max = 1.0    # Normal increment in series where no gaps shall be filled
shift = 0

# the following two statements use NumPy's broadcasting
# to implicit run some loop at the C level
diff = series[1:] - series[:-1]
num_fills = np.round(diff / d_max) - 1
for i in np.where(diff > d_max)[0]:
    nf = num_fills[i]
    nans = [np.nan] * nf
    data_a[i+1+shift:i+1+shift] = nans
    data_b[i+1+shift:i+1+shift] = nans
    shift = int(shift + nf)

rawDescription: How to improve my performance in filling gaps in time series and data lists with Python

description: Improve my performance in filling gaps in time series and data lists with Python


code:
import pandas
a = pandas.read_csv('temp.csv', names = ["c%d" % i for i in range(8)], skiprows = 1)
a

rawDescription: Read each entire Column of CSV file using python (preferably by help of pandas )


code:
def GetInterpolatedZ(tri, u, v):
    t0 = Vec3(tri[0].x/tri[0].z, tri[0].y/tri[0].z, 1.0/tri[0].z)
    t1 = Vec3(tri[1].x/tri[1].z, tri[1].y/tri[1].z, 1.0/tri[1].z)
    t2 = Vec3(tri[2].x/tri[2].z, tri[2].y/tri[2].z, 1.0/tri[2].z)

    inter = t0 + u * (t1-t0) + v * (t2-t0)
    inter.z = 1.0 / inter.z
    inter.x *= inter.z
    inter.y *= inter.z
    return inter

rawDescription: Retrieving the actual 3D coordinates of a point on a triangle that has been flattened to 2 dimensions


code:
filenames = []
lay = QtGui.QHboxLayout()
window.scrollArea.setLayout(lay)
def addfiles():
    fileName= QtGui.QFileDialog.getOpenFileName()
    fileDirectory = unicode(fileName)
    global filenames
    filenames.append(fileDirectory)
    button = QtGui.QPushButton(os.path.basename(fileDirectory))
    lay.addWidget(button)

rawDescription: How to create a button for each element in a list and put it in a scroll-area?

description: Create a button for each element in a list and put it in a scroll-area


code:
import types

class copyattributes(object):
    def __init__(self, source):
        self.source = source

    def __call__(self, target):
        for attr, value in self.source.__dict__.items():
            if attr.startswith('__'):
                continue
            if isinstance(value, (property, types.FunctionType)):
                continue
            setattr(target, attr, value)
        return target

rawDescription: Python decorator to add class-level variables


rawDescription: Django search multiple filters

description: Django search multiple filters


rawDescription: Use Python to edit XML header

description: Use Python to edit XML header


code:
class Foo(object):
    def __init__(self, helpers):
        self.helpers = helpers

# then, instead of import foo:
foo = Foo(mock_helpers)

rawDescription: Python: Mock a module without importing it or needing it to exist


code:
x, y = a.nonzero()
zip(x,y)
nonzero = zip(x,y)

rawDescription: python - saving numpy array to a file (smallest size possible)

description: Python - saving numpy array to a file (smallest size possible)


code:
import logging

# custom tree formatter
class TreeFormatter(logging.Formatter):
    formatPrefix = {} # map loggername, formatPrefix

    def format(self, record):
        s = ""
        # first time this name is encountered: create the prefix and print the name
        if not record.name in self.formatPrefix:
            f = self.getFormatPrefix(record)
            s += "%s \"%s\"\n" % (f, record.name)

        # print the actual message
        s += "%s %s: %s" % (self.formatPrefix[record.name], record.levelname, record.msg)
        return s

    # create the format prefix for the given package name
    # (stored in self.formatPrefix[record.name])
    # and return the first line to print
    def getFormatPrefix(self, record):
        depth = record.name.count(".")
        self.formatPrefix[record.name] = "   |" * (depth+1)

        if depth == 0:
            return "<--"

        return "%so<--" % ( ("   |" * depth)[:-1])

rawDescription: Format log messages as a tree

description: Format log messages as a tree


code:
from BeautifulSoup import BeautifulSoup as bs
text = """

            stuff

The content I want

"""
a = bs(text)

rawDescription: Python: how to extract the content of a column in a table


code:
def additem(uuid,work,the_queue):
    the_queue.put((uuid,word)) # Queue in a tuple containing the data

def proceeditems(the_queue):
    while True:
        item = the_queue.get() # This will block until something is inside the queue
        try:
            result = somework(item)
            itemdone.append(result)
        except:
            the_queue.put(item) # If failed, put back to queue for retry.
        # You don't need the last two lines

rawDescription: check a list constantly and do something if list has items

description: Check a list constantly and do something if list has items


code:
aa.iteritems()   # tuples of (key, value)
                 # ('ALA', 'A')
aa.iterkeys()    # keys only -- equivalent to just making an iterator directly from aa
                 # 'ALA'
aa.itervalues()  # items only
                 # 'A'

rawDescription: Python: Simple Dictionary referencing Problem


code: df = df.assign(new_column=df.apply(my_fxn, axis=1))

rawDescription: What is the `pandas` way to create a column in a dataframe by operating on each row?


code:
{'brand': u'Samsung', 'price': u'695 GEL', 'title': u'G7102 Grand dous 2'}
{'brand': u'Samsung', 'price': u'572 GEL', 'title': u'I9060 Galaxy grand...'}
...

rawDescription: struggling on xpath establishment

description: Struggling on xpath establishment


code:
import bisect
def bin_slice(L, min, max):
    i = bisect.bisect_left(L, min)
    j = bisect.bisect(L, max)
    return L[i:j]

bin_slice([1,2,3,4,5,6,7,8,9], 3, 6)
bin_slice([500,757,2412,10000,123123], 600, 5000)

rawDescription: Using ternary search to find all elements within a range?


code:
my_list = [2,3,4,"5cm", 6,"2.5km"]

def get_digits(s):
    return ''.join(ele for ele in s if not ele.isalpha())

def convert_to_nums(my_list):
    result = []
    for ele in my_list:
        if isinstance(ele, (int, float)):
            result.append(ele)
        else:
            ele = get_digits(ele)
            try:
                result.append(int(ele))
            except ValueError:
                result.append(float(ele))
    return result

rawDescription: how to convert string like "5cm" into an integer

description: Convert string like "5cm" into an integer


code: dict.fromkeys(['foo', 'bar'])

rawDescription: How to present a class as a function?

description: Present a class as a function


rawDescription: Editing all text in childNodes of XML file with Python


code:
import numpy as np
np.random.seed(11)
k = 10
A_list = np.random.rand(k,2,2)
B_list = np.random.rand(k,2)
solution = np.linalg.solve(A_list,B_list)
all(np.allclose(np.dot(A_list[i, :], solution[i,:]), B_list[i, :])
        for i in range(A_list.shape[0]))

rawDescription: How to map np.linalg.solve to an array of matrice and keep speed?

description: Map np.linalg.solve to an array of matrice and keep speed


code:
def sort(request):
    sortid = request.GET.get('sortid')
    ratings = Bewertung.objects.order_by(sortid)
    locations = Location.objects.filter(locations_bewertung__in=ratings)
    if request.is_ajax():
        template = 'partial-results.html'
    else:
        template = 'result-page.html'
    return render_to_response(template,   {'locs':locations},context_instance=RequestContext(request))

rawDescription: how to render only part of html with data using django

description: Render only part of html with data using django


code:
some_complex_type = client.factory.create('SomeComplexType')
some_complex_type.foo = 'Hello'  # borks is the definition of SomeComplexType is missing from the wsdl
some_complex_type.bar = 'World'
return_value = client.service.MethodThatUsesSomeComplexType(some_complex_type)

rawDescription: How do I make a Python client that can consume a .NET (soap) web service?

description: Make a Python client that can consume a .NET (soap) web service


code:
import random
nameLines = '''\
apple
cheese
cake
tree
pie
'''.splitlines()
random.sample(nameLines, random.randint(1, 2))
random.sample(nameLines, random.randint(1, 2))

rawDescription: Python - Print random number of lines from file


code:
out = []
for k in d.viewkeys() & d2.viewkeys():
    vals, v = d2[k], d[k]
    for v2 in vals:
        if v[0] <= int(v2) <= v[1]:
            out.append([k,v2])

rawDescription: Compare dictionary key, values with nested list elements - Python


rawDescription: Python classes with decorated methods and __del__ defined do not get garbage collected: how do I uncouple the decorated method?


code:
class MyMeta(type):
    @NonDataProperty
    def x(self):
        return "foo"

class Foo(metaclass=MyMeta):
    pass # does not override x

class Bar(metaclass=MyMeta):
    x = "bar" # does override x

class Baz:
    x = "baz"

class Baz_with_meta(Baz, metaclass=MyMeta):
    pass # inherited x value will take precedence over non-data descriptor

print(Foo.x) # prints "foo"
print(Bar.x) # prints "bar"
print(Baz_with_meta.x) # prints "baz"

rawDescription: How to let inheritance take precedence over class properties when using a metaclass?

description: Let inheritance take precedence over class properties when using a metaclass


code:
class MyInt(int):
    def __new__(cls, value):
        new_myint = super().__new__(cls, value)
        return new_myint

rawDescription: Inheriting from immutable types

description: Inheriting from immutable types


code:
timeit('myList = myList + ["a"]', 'myList = []', number = 50000)
timeit('myList.append("a")', 'myList = []', number = 50000)

rawDescription: Faster operation of appending an element to a list


code:
all_files = []
for i in range(len(files)):
    all_files.append(read_data(files[i]))

rawDescription: Variable dimension array

description: Variable dimension array


code:
cmdline = "awk '{print $0}' %s > another_file" % (shlex.quote(file),)
subprocess.check_call(cmdline, shell=True)

rawDescription: python variables in awk

description: Python variables in awk


code:
A
ccworder(A)

rawDescription: How to draw the largest polygon from a set of points

description: Draw the largest polygon from a set of points


code:
# substitute your format
# the one below is likely to be what's saved by str(datetime)
previousTime = datetime.datetime.strptime(line[x:x+26], "%Y-%m-%d %H:%M:%S.%f")

rawDescription: Converting a datetime into a string and back again


code:
from Queue import Queue
import threading

class myQueue(Queue):
    def putall(self , objects , *args):
        with self.mutex:
            for object in objects:
                self.put(object, *args) # This will hang.

    def getall(self , number , *args ):
        with self.mutex:
            return [self.get(*args) for _ in xrange(number)] # This will hang

q = myQueue()
q.putall(["1", "2", "3"])
print(q.getall(2))

rawDescription: How can I make putall and getall methods of a Queue subclass atomic?

description: Make putall and getall methods of a Queue subclass atomic


code:
limit_choices_to=reduce(
                     operator.xor,
                     map(query_group_lkup, getattr(settings, 'AUTHORIZED_AUTHORS', ''))
                 )

rawDescription: Perform a logical exclusive OR on a Django Q object


code:
if number >= 0:
    print("Positive")
if number <= 100:
    print("<= 100")

if number < 0 or number > 100:
    print("x < 0 or x > 100")

rawDescription: Python 3 else for all previous ifs

description: Python 3 else for all previous ifs


code:
from TestUnits import test_something

def suite():
    suite = unittest.TestSuite()
    suite.addTest(test_something.suite())
    return suite

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

rawDescription: Python unittesting: run tests in another module


code:
('potato',)
('potat',)
('pota',)

rawDescription: Regex? Match part of or whole word

description: Regex? Match part of or whole word


code:
y
x = 2
y = eval(expr)
y

rawDescription: Parsing code used in loop only once

description: Parsing code used in loop only once


code:
a = [1, 1, 2, 3]
all(map(lambda x: a.count(x) <= b.count(x), a))

rawDescription: Python not all in operation

description: Python not all in operation


code:
import jwt
from rest_framework_jwt.utils import jwt_payload_handler

def create_token(user):
    payload = jwt_payload_handler(user)
    token = jwt.encode(payload, settings.SECRET_KEY)
    return token.decode('unicode_escape')

rawDescription: How can i make django-rest-framework-jwt return token on registration?

description: Make django-rest-framework-jwt return token on registration


code: pd.DataFrame(np.multiply.outer(a, b), columns=b.index, index=a.index)

rawDescription: How to broadcast pandas series with different indices?

description: Broadcast pandas series with different indices


code:
rooms = list(db.rooms.find().limit(1000))
print(rooms)

rawDescription: Find() a specified amount of records in mongo/python


rawDescription: Using $each with $push on all fields in python


rawDescription: How do i check if input is a date

description: Check if input is a date


code:
ListA = [1,2,3]
ListB = [10,20,30]
list(map(sum, zip(ListA, ListB)))

rawDescription: Python - Merge two lists with a simultaneous concatenation


code: nearestMagnetValue = min(magnetValues, key=lambda x: abs(event.xdata - x))

rawDescription: Plot hlines on fixed x values when click happens between x values


code:
class RULES(object):
    TOTAL_REQUESTS = 'totalrequests'
    HTML_SIZE = 'htmlsize'
    JS_SIZE = 'jssize'
    JS_COUNT = 'jscount'
    DOMAIN_COUNT = 'domaincount'
    REQ_PER_DOMAIN = 'reqperdomain'
    DOMAIN_WITH_MAX_REQUESTS = 'domainwithmaxrequests'
    IMAGE_SIZE = 'imagesize'
import re
all_caps = re.compile('^[A-Z_]+$')
[attr for attr in dir(RULES) if all_caps.match(attr)]

rawDescription: How to get all the attributes of a class as a list in python

description: Get all the attributes of a class as a list in python


code:
class Categoria(models.Model):
    # fields..

    def __unicode__(self):
        return self.titulo

rawDescription: JSON from django rest Framework

description: JSON from django rest Framework


code:
import math

upto = 1000000

a = [True] * upto
p = []

for n in xrange(2,upto):
    if a[n]:
        p.append(n)
        for k in xrange(2,(upto+n-1)//n):
            a[k*n] = False

print('primes generated')

s = []
p = set(p)
for e in p:
    pr=True
    w=str(e)
    if all(c not in w for c in ['2','4','6','8','5','0']):
        for x in (w[i:]+w[:i] for i in range(len(w))):
            if int(x) not in p:
                pr=False
                break
        if pr:
            s.append(e)

print(s)

rawDescription: Python Code Optimisation

description: Python Code Optimisation


rawDescription: "normalize" dataframe of sentences into larger dataframe of words


code:
import numpy as np
a[~np.isnan(a).any(1)]

rawDescription: removing pairs of elements from numpy arrays that are NaN (or another value) in Python

description: Removing pairs of elements from numpy arrays that are NaN (or another value) in Python


code:
original_dict = {
(123, 456): 45,
(123, 457): 90,
(124, 234): 70,
(125, 87): 3,
(125, 103): 56,
(125, 897): 34
}
from itertools import groupby
from operator import itemgetter
sorted_keys = sorted(original_dict, key=lambda key: (key[0], -original_dict[key]))
for primary_key, key_group in groupby(sorted_keys, key=itemgetter(0)):
    print(primary_key, [key[1] for key in key_group])

rawDescription: How to sort and subset a python dictionary with compound key?

description: Sort and subset a python dictionary with compound key


code:
def my_view(request):
    ...
    return redirect('some-view-name', foo='bar')

rawDescription: Django - template not loading, perpetual 302 message


code:
from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def main():
    print(request.headers)
    print(request.headers['User-Agent'])

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

rawDescription: How to capture and read headers of incoming HTTP requests in Flask?

description: Capture and read headers of incoming HTTP requests in Flask


code:
def ohlc(df):
    return (
        df.Datetime.iat[-1], # last timestamp in group.
        df.Open.iat[0], # First Open.
        df.High.max(),
        df.Low.min(),
        df.Close.iat[-1], # Last Close.
        df.Up.sum(),
        df.Down.sum(),
        df.Close.count(), # Count number of closing bars.
        df.market_open.iat[0])   # Take first True/False indicator.

rawDescription: Select CONSECUTIVE rows from a DataFrame based on values in a column in Pandas with Groupby


code:
import base64
raw_data = base64.b64decode(data)

rawDescription: Load image from base64 encoded image in Gtk3+


code:
import sys
sys.path.append('.')

rawDescription: Python: changing IDLE's path in WIN7

description: Python: changing IDLE's path in WIN7


code:
from datetime import datetime
import numpy as np
dt = datetime.utcnow()
dt
dt64 = np.datetime64(dt)
ts = (dt64 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')
ts
datetime.utcfromtimestamp(ts)
np.__version__

rawDescription: Converting between datetime, Timestamp and datetime64


rawDescription: How to get current time in Python

description: Get current time in Python