Instruction:
Write a Python 3 function that can
induce a set of correlations on a column - wise dataset.

Response:
def induce_correlations(data, corrmat):
    """
    Induce a set of correlations on a column-wise dataset

    Parameters
    ----------
    data : 2d-array
        An m-by-n array where m is the number of samples and n is the
        number of independent variables, each column of the array corresponding
        to each variable
    corrmat : 2d-array
        An n-by-n array that defines the desired correlation coefficients
        (between -1 and 1). Note: the matrix must be symmetric and
        positive-definite in order to induce.

    Returns
    -------
    new_data : 2d-array
        An m-by-n array that has the desired correlations.

    """
    # Create an rank-matrix
    data_rank = np.vstack([rankdata(datai) for datai in data.T]).T

    # Generate van der Waerden scores
    data_rank_score = data_rank / (data_rank.shape[0] + 1.0)
    data_rank_score = norm(0, 1).ppf(data_rank_score)

    # Calculate the lower triangular matrix of the Cholesky decomposition
    # of the desired correlation matrix
    p = chol(corrmat)

    # Calculate the current correlations
    t = np.corrcoef(data_rank_score, rowvar=0)

    # Calculate the lower triangular matrix of the Cholesky decomposition
    # of the current correlation matrix
    q = chol(t)

    # Calculate the re-correlation matrix
    s = np.dot(p, np.linalg.inv(q))

    # Calculate the re-sampled matrix
    new_data = np.dot(data_rank_score, s.T)

    # Create the new rank matrix
    new_data_rank = np.vstack([rankdata(datai) for datai in new_data.T]).T

    # Sort the original data according to new_data_rank
    for i in range(data.shape[1]):
        vals, order = np.unique(
            np.hstack((data_rank[:, i], new_data_rank[:, i])), return_inverse=True
        )
        old_order = order[: new_data_rank.shape[0]]
        new_order = order[-new_data_rank.shape[0] :]
        tmp = data[np.argsort(old_order), i][new_order]
        data[:, i] = tmp[:]

    return data


Instruction:
Can you generate a brief explanation for the following Python 3 code
def plotcorr(X, plotargs=None, full=True, labels=None):
    import matplotlib.pyplot as plt

    X = [Xi._mcpts if isinstance(Xi, UncertainFunction) else Xi for Xi in X]
    X = np.atleast_2d(X)
    numvars, numdata = X.shape
    fig, axes = plt.subplots(nrows=numvars, ncols=numvars, figsize=(8, 8))
    fig.subplots_adjust(hspace=0.0, wspace=0.0)

    for ax in axes.flat:
        # Hide all ticks and labels
        ax.xaxis.set_visible(False)
        ax.yaxis.set_visible(False)

        # Set up ticks only on one side for the "edge" subplots...
        if full:
            if ax.is_first_col():
                ax.yaxis.set_ticks_position("left")
            if ax.is_last_col():
                ax.yaxis.set_ticks_position("right")
            if ax.is_first_row():
                ax.xaxis.set_ticks_position("top")
            if ax.is_last_row():
                ax.xaxis.set_ticks_position("bottom")
        else:
            if ax.is_first_row():
                ax.xaxis.set_ticks_position("top")
            if ax.is_last_col():
                ax.yaxis.set_ticks_position("right")

    # Label the diagonal subplots...
    if not labels:
        labels = ["x" + str(i) for i in range(numvars)]

    for i, label in enumerate(labels):
        axes[i, i].annotate(
            label, (0.5, 0.5), xycoords="axes fraction", ha="center", va="center"
        )

    # Plot the data
    for i, j in zip(*np.triu_indices_from(axes, k=1)):
        if full:
            idx = [(i, j), (j, i)]
        else:
            idx = [(i, j)]
        for x, y in idx:
            # FIX #1: this needed to be changed from ...(data[x], data[y],...)
            if plotargs is None:
                if len(X[x]) > 100:
                    plotargs = ",b"  # pixel marker
                else:
                    plotargs = ".b"  # point marker
            axes[x, y].plot(X[y], X[x], plotargs)
            ylim = min(X[y]), max(X[y])
            xlim = min(X[x]), max(X[x])
            axes[x, y].set_ylim(
                xlim[0] - (xlim[1] - xlim[0]) * 0.1, xlim[1] + (xlim[1] - xlim[0]) * 0.1
            )
            axes[x, y].set_xlim(
                ylim[0] - (ylim[1] - ylim[0]) * 0.1, ylim[1] + (ylim[1] - ylim[0]) * 0.1
            )

    # Turn on the proper x or y axes ticks.
    if full:
        for i, j in zip(list(range(numvars)), itertools.cycle((-1, 0))):
            axes[j, i].xaxis.set_visible(True)
            axes[i, j].yaxis.set_visible(True)
    else:
        for i in range(numvars - 1):
            axes[0, i + 1].xaxis.set_visible(True)
            axes[i, -1].yaxis.set_visible(True)
        for i in range(1, numvars):
            for j in range(0, i):
                fig.delaxes(axes[i, j])

    # FIX #2: if numvars is odd, the bottom right corner plot doesn't have the
    # correct axes limits, so we pull them from other axes
    if numvars % 2:
        xlimits = axes[0, -1].get_xlim()
        ylimits = axes[-1, 0].get_ylim()
        axes[-1, -1].set_xlim(xlimits)
        axes[-1, -1].set_ylim(ylimits)

    return fig

Response: Returns a scatterplot matrix of the same size as the input array X.


Instruction:
Can you tell what is the following Python 3 function doing
def chol(A):
    A = np.array(A)
    assert A.shape[0] == A.shape[1], "Input matrix must be square"

    L = [[0.0] * len(A) for _ in range(len(A))]
    for i in range(len(A)):
        for j in range(i + 1):
            s = sum(L[i][k] * L[j][k] for k in range(j))
            L[i][j] = (
                (A[i][i] - s) ** 0.5 if (i == j) else (1.0 / L[j][j] * (A[i][j] - s))
            )

    return np.array(L)

Response: Calculates the lower triangular matrix of the Cholesky decomposition of a symmetric positive - definite matrix.


Instruction:
Make a summary of the following Python 3 code
def get(self, uri, params={}):
        '''A generic method to make GET requests to the OpenDNS Investigate API
        on the given URI.
        '''
        return self._session.get(urljoin(Investigate.BASE_URL, uri),
            params=params, headers=self._auth_header, proxies=self.proxies
        )

Response:
A generic method to make GET requests to the OpenDNS Investigate API
        on the given URI.


Instruction:
How would you explain what the following Python 3 function does
def post(self, uri, params={}, data={}):
        '''A generic method to make POST requests to the OpenDNS Investigate API
        on the given URI.
        '''
        return self._session.post(
            urljoin(Investigate.BASE_URL, uri),
            params=params, data=data, headers=self._auth_header,
            proxies=self.proxies
        )

Response: A generic method to make POST requests to the OpenDNS Investigate API on the given URI.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_parse(self, uri, params={}):
        '''Convenience method to call get() on an arbitrary URI and parse the response
        into a JSON object. Raises an error on non-200 response status.
        '''
        return self._request_parse(self.get, uri, params)

Response:
Convenience method to call get on an arbitrary URI and parse the response
        into a JSON object. Raises an error on non - 200 response status.


Instruction:
Can you create a Python 3 function that
gets the domain status and categorization of a list of domains.

Response:
def categorization(self, domains, labels=False):
        '''Get the domain status and categorization of a domain or list of domains.
        'domains' can be either a single domain, or a list of domains.
        Setting 'labels' to True will give back categorizations in human-readable
        form.

        For more detail, see https://investigate.umbrella.com/docs/api#categorization
        '''
        if type(domains) is str:
            return self._get_categorization(domains, labels)
        elif type(domains) is list:
            return self._post_categorization(domains, labels)
        else:
            raise Investigate.DOMAIN_ERR


Instruction:
Can you generate the documentation for the following Python 3 function
def cooccurrences(self, domain):
        '''Get the cooccurrences of the given domain.

        For details, see https://investigate.umbrella.com/docs/api#co-occurrences
        '''
        uri = self._uris["cooccurrences"].format(domain)
        return self.get_parse(uri)

Response: Get the cooccurrences of the given domain.


Instruction:
Create a Python 3 function to
get the related domains of the given domain.

Response:
def related(self, domain):
        '''Get the related domains of the given domain.

        For details, see https://investigate.umbrella.com/docs/api#relatedDomains
        '''
        uri = self._uris["related"].format(domain)
        return self.get_parse(uri)


Instruction:
Implement a Python 3 function for
getting the Security Information for the given domain.

Response:
def security(self, domain):
        '''Get the Security Information for the given domain.

        For details, see https://investigate.umbrella.com/docs/api#securityInfo
        '''
        uri = self._uris["security"].format(domain)
        return self.get_parse(uri)


Instruction:
How would you code a function in Python 3 to
get the RR ( Resource Record ) History of the given domain or IP.

Response:
def rr_history(self, query, query_type="A"):
        '''Get the RR (Resource Record) History of the given domain or IP.
        The default query type is for 'A' records, but the following query types
        are supported:

        A, NS, MX, TXT, CNAME

        For details, see https://investigate.umbrella.com/docs/api#dnsrr_domain
        '''
        if query_type not in Investigate.SUPPORTED_DNS_TYPES:
            raise Investigate.UNSUPPORTED_DNS_QUERY

        # if this is an IP address, query the IP
        if Investigate.IP_PATTERN.match(query):
            return self._ip_rr_history(query, query_type)

        # otherwise, query the domain
        return self._domain_rr_history(query, query_type)


Instruction:
Make a summary of the following Python 3 code
def domain_whois(self, domain):
        '''Gets whois information for a domain'''
        uri = self._uris["whois_domain"].format(domain)
        resp_json = self.get_parse(uri)
        return resp_json

Response: Gets whois information for a domain


Instruction:
How would you code a function in Python 3 to
get whois history for a domain

Response:
def domain_whois_history(self, domain, limit=None):
        '''Gets whois history for a domain'''

        params = dict()
        if limit is not None:
            params['limit'] = limit

        uri = self._uris["whois_domain_history"].format(domain)
        resp_json = self.get_parse(uri, params)
        return resp_json


Instruction:
Explain what the following Python 3 code does
def ns_whois(self, nameservers, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, sort_field=DEFAULT_SORT):
        '''Gets the domains that have been registered with a nameserver or
        nameservers'''
        if not isinstance(nameservers, list):
            uri = self._uris["whois_ns"].format(nameservers)
            params = {'limit': limit, 'offset': offset, 'sortField': sort_field}
        else:
            uri = self._uris["whois_ns"].format('')
            params = {'emailList' : ','.join(nameservers), 'limit': limit, 'offset': offset, 'sortField': sort_field}

        resp_json = self.get_parse(uri, params=params)
        return resp_json

Response: Gets the domains that have been registered with a nameserver or a nameserver list


Instruction:
Here you have a function in Python 3, explain what it does
def search(self, pattern, start=None, limit=None, include_category=None):
        '''Searches for domains that match a given pattern'''

        params = dict()

        if start is None:
            start = datetime.timedelta(days=30)

        if isinstance(start, datetime.timedelta):
            params['start'] = int(time.mktime((datetime.datetime.utcnow() - start).timetuple()) * 1000)
        elif isinstance(start, datetime.datetime):
            params['start'] = int(time.mktime(start.timetuple()) * 1000)
        else:
            raise Investigate.SEARCH_ERR

        if limit is not None and isinstance(limit, int):
            params['limit'] = limit
        if include_category is not None and isinstance(include_category, bool):
            params['includeCategory'] = str(include_category).lower()

        uri = self._uris['search'].format(quote_plus(pattern))

        return self.get_parse(uri, params)

Response: Searches for domains that match a given pattern


Instruction:
Can you implement a function in Python 3 that
returns an object representing the samples identified by the input domain IP or URL

Response:
def samples(self, anystring, limit=None, offset=None, sortby=None):
        '''Return an object representing the samples identified by the input domain, IP, or URL'''

        uri = self._uris['samples'].format(anystring)
        params = {'limit': limit, 'offset': offset, 'sortby': sortby}

        return self.get_parse(uri, params)


Instruction:
Can you tell what is the following Python 3 function doing
def sample(self, hash, limit=None, offset=None):
        '''Return an object representing the sample identified by the input hash, or an empty object if that sample is not found'''

        uri = self._uris['sample'].format(hash)
        params = {'limit': limit, 'offset': offset}

        return self.get_parse(uri, params)

Response: Return an object representing the sample identified by the input hash or an empty object if that sample is not found


Instruction:
Explain what the following Python 3 code does
def as_for_ip(self, ip):
        '''Gets the AS information for a given IP address.'''
        if not Investigate.IP_PATTERN.match(ip):
            raise Investigate.IP_ERR

        uri = self._uris["as_for_ip"].format(ip)
        resp_json = self.get_parse(uri)

        return resp_json

Response: Gets the AS information for a given IP address.


Instruction:
Can you write a function in Python 3 where it
gets the AS information for a given ASN. Return the CIDR and geolocation associated with the AS.

Response:
def prefixes_for_asn(self, asn):
        '''Gets the AS information for a given ASN. Return the CIDR and geolocation associated with the AS.'''

        uri = self._uris["prefixes_for_asn"].format(asn)
        resp_json = self.get_parse(uri)

        return resp_json


Instruction:
Can you write a function in Python 3 where it
gets the domain tagging timeline for a given uri.

Response:
def timeline(self, uri):
        '''Get the domain tagging timeline for a given uri.
        Could be a domain, ip, or url.
        For details, see https://docs.umbrella.com/investigate-api/docs/timeline
        '''
        uri = self._uris["timeline"].format(uri)
        resp_json = self.get_parse(uri)

        return resp_json


Instruction:
Can you generate a brief explanation for the following Python 3 code
def abs(x):
    if isinstance(x, UncertainFunction):
        mcpts = np.abs(x._mcpts)
        return UncertainFunction(mcpts)
    else:
        return np.abs(x)

Response: Absolute value of a single object


Instruction:
Create a Python 3 function for
inversing cosine of a sequence of numbers.

Response:
def acos(x):
    """
    Inverse cosine
    """
    if isinstance(x, UncertainFunction):
        mcpts = np.arccos(x._mcpts)
        return UncertainFunction(mcpts)
    else:
        return np.arccos(x)


Instruction:
Given the following Python 3 function, write the documentation
def asin(x):
    if isinstance(x, UncertainFunction):
        mcpts = np.arcsin(x._mcpts)
        return UncertainFunction(mcpts)
    else:
        return np.arcsin(x)

Response: Inverse sine of numpy. asin


Instruction:
Write a Python 3 script to
inverse tangent of a single object

Response:
def atan(x):
    """
    Inverse tangent
    """
    if isinstance(x, UncertainFunction):
        mcpts = np.arctan(x._mcpts)
        return UncertainFunction(mcpts)
    else:
        return np.arctan(x)


Instruction:
Can you tell what is the following Python 3 function doing
def ceil(x):
    if isinstance(x, UncertainFunction):
        mcpts = np.ceil(x._mcpts)
        return UncertainFunction(mcpts)
    else:
        return np.ceil(x)

Response: ceil - Returns the ceil of x


Instruction:
How would you explain what the following Python 3 function does
def cos(x):
    if isinstance(x, UncertainFunction):
        mcpts = np.cos(x._mcpts)
        return UncertainFunction(mcpts)
    else:
        return np.cos(x)

Response: cos - like function


Instruction:
Explain what the following Python 3 code does
def degrees(x):
    if isinstance(x, UncertainFunction):
        mcpts = np.degrees(x._mcpts)
        return UncertainFunction(mcpts)
    else:
        return np.degrees(x)

Response: Convert radians to degrees


Instruction:
Can you create a Python 3 function that
calculates exp - 1 of x

Response:
def expm1(x):
    """
    Calculate exp(x) - 1
    """
    if isinstance(x, UncertainFunction):
        mcpts = np.expm1(x._mcpts)
        return UncertainFunction(mcpts)
    else:
        return np.expm1(x)


Instruction:
Can you tell what is the following Python 3 function doing
def floor(x):
    if isinstance(x, UncertainFunction):
        mcpts = np.floor(x._mcpts)
        return UncertainFunction(mcpts)
    else:
        return np.floor(x)

Response: Floor function (round towards negative infinity)


Instruction:
Make a summary of the following Python 3 code
def hypot(x, y):
    if isinstance(x, UncertainFunction) or isinstance(x, UncertainFunction):
        ufx = to_uncertain_func(x)
        ufy = to_uncertain_func(y)
        mcpts = np.hypot(ufx._mcpts, ufy._mcpts)
        return UncertainFunction(mcpts)
    else:
        return np.hypot(x, y)

Response: Calculate the hypotenuse given two legs of a right triangle


Instruction:
Can you tell what is the following Python 3 function doing
def log(x):
    if isinstance(x, UncertainFunction):
        mcpts = np.log(x._mcpts)
        return UncertainFunction(mcpts)
    else:
        return np.log(x)

Response: Natural logarithm of a single object


Instruction:
Implement a function in Python 3 to
base - 10 logarithm of x

Response:
def log10(x):
    """
    Base-10 logarithm
    """
    if isinstance(x, UncertainFunction):
        mcpts = np.log10(x._mcpts)
        return UncertainFunction(mcpts)
    else:
        return np.log10(x)


Instruction:
Write a Python 3 function that can
convert degrees to radians

Response:
def radians(x):
    """
    Convert degrees to radians
    """
    if isinstance(x, UncertainFunction):
        mcpts = np.radians(x._mcpts)
        return UncertainFunction(mcpts)
    else:
        return np.radians(x)


Instruction:
How would you explain what the following Python 3 function does
def sqrt(x):
    if isinstance(x, UncertainFunction):
        mcpts = np.sqrt(x._mcpts)
        return UncertainFunction(mcpts)
    else:
        return np.sqrt(x)

Response: sqrt - returns the square root of x


Instruction:
Can you generate the documentation for the following Python 3 function
def trunc(x):
    if isinstance(x, UncertainFunction):
        mcpts = np.trunc(x._mcpts)
        return UncertainFunction(mcpts)
    else:
        return np.trunc(x)

Response: Truncates the values to the integer value without rounding


Instruction:
Explain what the following Python 3 code does
def lhd(
    dist=None,
    size=None,
    dims=1,
    form="randomized",
    iterations=100,
    showcorrelations=False,
):
    """
    Create a Latin-Hypercube sample design based on distributions defined in the
    `scipy.stats` module

    Parameters
    ----------
    dist: array_like
        frozen scipy.stats.rv_continuous or rv_discrete distribution objects
        that are defined previous to calling LHD

    size: int
        integer value for the number of samples to generate for each
        distribution object

    dims: int, optional
        if dist is a single distribution object, and dims > 1, the one
        distribution will be used to generate a size-by-dims sampled design

    form: str, optional (non-functional at the moment)
        determines how the sampling is to occur, with the following optional
        values:
            - 'randomized' - completely randomized sampling
            - 'spacefilling' - space-filling sampling (generally gives a more
              accurate sampling of the design when the number of sample points
              is small)
            - 'orthogonal' - balanced space-filling sampling (experimental)

        The 'spacefilling' and 'orthogonal' forms require some iterations to
        determine the optimal sampling pattern.

    iterations: int, optional (non-functional at the moment)
        used to control the number of allowable search iterations for generating
        'spacefilling' and 'orthogonal' designs

    Returns
    -------
    out: 2d-array,
        A 2d-array where each column corresponds to each input distribution and
        each row is a sample in the design

    Examples
    --------

    Single distribution:
        - uniform distribution, low = -1, width = 2

    >>> import scipy.stats as ss
    >>> d0 = ss.uniform(loc=-1,scale=2)
    >>> print lhd(dist=d0,size=5)
    [[ 0.51031081]
     [-0.28961427]
     [-0.68342107]
     [ 0.69784371]
     [ 0.12248842]]

    Single distribution for multiple variables:
        - normal distribution, mean = 0, stdev = 1

    >>> d1 = ss.norm(loc=0,scale=1)
    >>> print lhd(dist=d1,size=7,dims=5)
    [[-0.8612785   0.23034412  0.21808001]
     [ 0.0455778   0.07001606  0.31586419]
     [-0.978553    0.30394663  0.78483995]
     [-0.26415983  0.15235896  0.51462024]
     [ 0.80805686  0.38891031  0.02076505]
     [ 1.63028931  0.52104917  1.48016008]]

    Multiple distributions:
        - beta distribution, alpha = 2, beta = 5
        - exponential distribution, lambda = 1.5

    >>> d2 = ss.beta(2,5)
    >>> d3 = ss.expon(scale=1/1.5)
    >>> print lhd(dist=(d1,d2,d3),size=6)
    [[-0.8612785   0.23034412  0.21808001]
     [ 0.0455778   0.07001606  0.31586419]
     [-0.978553    0.30394663  0.78483995]
     [-0.26415983  0.15235896  0.51462024]
     [ 0.80805686  0.38891031  0.02076505]
     [ 1.63028931  0.52104917  1.48016008]]
    """
    assert dims > 0, 'kwarg "dims" must be at least 1'
    if not size or not dist:
        return None

    def _lhs(x, samples=20):
        """
        _lhs(x) returns a latin-hypercube matrix (each row is a different
        set of sample inputs) using a default sample size of 20 for each column
        of X. X must be a 2xN matrix that contains the lower and upper bounds of
        each column. The lower bound(s) should be in the first row and the upper
        bound(s) should be in the second row.

        _lhs(x,samples=N) uses the sample size of N instead of the default (20).

        Example:
            >>> x = np.array([[0,-1,3],[1,2,6]])
            >>> print 'x:'; print x
            x:
            [[ 0 -1  3]
             [ 1  2  6]]

            >>> print 'lhs(x):'; print _lhs(x)
            lhs(x):
            [[ 0.02989122 -0.93918734  3.14432618]
             [ 0.08869833 -0.82140706  3.19875152]
             [ 0.10627442 -0.66999234  3.33814979]
             [ 0.15202861 -0.44157763  3.57036894]
             [ 0.2067089  -0.34845384  3.66930908]
             [ 0.26542056 -0.23706445  3.76361414]
             [ 0.34201421 -0.00779306  3.90818257]
             [ 0.37891646  0.15458423  4.15031708]
             [ 0.43501575  0.23561118  4.20320064]
             [ 0.4865449   0.36350601  4.45792314]
             [ 0.54804367  0.56069855  4.60911539]
             [ 0.59400712  0.7468415   4.69923486]
             [ 0.63708876  0.9159176   4.83611204]
             [ 0.68819855  0.98596354  4.97659182]
             [ 0.7368695   1.18923511  5.11135111]
             [ 0.78885724  1.28369441  5.2900157 ]
             [ 0.80966513  1.47415703  5.4081971 ]
             [ 0.86196731  1.57844205  5.61067689]
             [ 0.94784517  1.71823504  5.78021164]
             [ 0.96739728  1.94169017  5.88604772]]

            >>> print 'lhs(x,samples=5):'; print _lhs(x,samples=5)
            lhs(x,samples=5):
            [[ 0.1949127  -0.54124725  3.49238369]
             [ 0.21128576 -0.13439798  3.65652016]
             [ 0.47516308  0.39957406  4.5797308 ]
             [ 0.64400392  0.90890999  4.92379431]
             [ 0.96279472  1.79415307  5.52028238]]
      """

        # determine the segment size
        segmentSize = 1.0 / samples

        # get the number of dimensions to sample (number of columns)
        numVars = x.shape[1]

        # populate each dimension
        out = np.zeros((samples, numVars))
        pointValue = np.zeros(samples)

        for n in range(numVars):
            for i in range(samples):
                segmentMin = i * segmentSize
                point = segmentMin + (np.random.random() * segmentSize)
                pointValue[i] = (point * (x[1, n] - x[0, n])) + x[0, n]
            out[:, n] = pointValue

        # now randomly arrange the different segments
        return _mix(out)

    def _mix(data, dim="cols"):
        """
        Takes a data matrix and mixes up the values along dim (either "rows" or
        "cols"). In other words, if dim='rows', then each row's data is mixed
        ONLY WITHIN ITSELF. Likewise, if dim='cols', then each column's data is
        mixed ONLY WITHIN ITSELF.
        """
        data = np.atleast_2d(data)
        n = data.shape[0]

        if dim == "rows":
            data = data.T

        data_rank = list(range(n))
        for i in range(data.shape[1]):
            new_data_rank = np.random.permutation(data_rank)
            vals, order = np.unique(
                np.hstack((data_rank, new_data_rank)), return_inverse=True
            )
            old_order = order[:n]
            new_order = order[-n:]
            tmp = data[np.argsort(old_order), i][new_order]
            data[:, i] = tmp[:]

        if dim == "rows":
            data = data.T

        return data

    if form is "randomized":
        if hasattr(dist, "__getitem__"):  # if multiple distributions were input
            nvars = len(dist)
            x = np.vstack((np.zeros(nvars), np.ones(nvars)))
            unif_data = _lhs(x, samples=size)
            dist_data = np.empty_like(unif_data)
            for i, d in enumerate(dist):
                dist_data[:, i] = d.ppf(unif_data[:, i])

        else:  # if a single distribution was input
            nvars = dims
            x = np.vstack((np.zeros(nvars), np.ones(nvars)))
            unif_data = _lhs(x, samples=size)
            dist_data = np.empty_like(unif_data)
            for i in range(nvars):
                dist_data[:, i] = dist.ppf(unif_data[:, i])

    elif form is "spacefilling":

        def euclid_distance(arr):
            n = arr.shape[0]
            ans = 0.0
            for i in range(n - 1):
                for j in range(i + 1, n):
                    d = np.sqrt(
                        np.sum(
                            [(arr[i, k] - arr[j, k]) ** 2 for k in range(arr.shape[1])]
                        )
                    )
                    ans += 1.0 / d ** 2
            return ans

        def fill_space(data):
            best = 1e8
            for it in range(iterations):
                d = euclid_distance(data)
                if d < best:
                    d_opt = d
                    data_opt = data.copy()

                data = _mix(data)

            print("Optimized Distance:", d_opt)
            return data_opt

        if hasattr(dist, "__getitem__"):  # if multiple distributions were input
            nvars = len(dist)
            x = np.vstack((np.zeros(nvars), np.ones(nvars)))
            unif_data = fill_space(_lhs(x, samples=size))
            dist_data = np.empty_like(unif_data)
            for i, d in enumerate(dist):
                dist_data[:, i] = d.ppf(unif_data[:, i])

        else:  # if a single distribution was input
            nvars = dims
            x = np.vstack((np.zeros(nvars), np.ones(nvars)))
            unif_data = fill_space(_lhs(x, samples=size))
            dist_data = np.empty_like(unif_data)
            for i in range(nvars):
                dist_data[:, i] = dist.ppf(unif_data[:, i])

    elif form is "orthogonal":
        raise NotImplementedError(
            "Sorry. The orthogonal space-filling algorithm hasn't been implemented yet."
        )
    else:
        raise ValueError('Invalid "form" value: %s' % (form))

    if dist_data.shape[1] > 1:
        cor_matrix = np.zeros((nvars, nvars))
        for i in range(nvars):
            for j in range(nvars):
                x_data = dist_data[:, i].copy()
                y_data = dist_data[:, j].copy()
                x_mean = x_data.mean()
                y_mean = y_data.mean()
                num = np.sum((x_data - x_mean) * (y_data - y_mean))
                den = np.sqrt(
                    np.sum((x_data - x_mean) ** 2) * np.sum((y_data - y_mean) ** 2)
                )
                cor_matrix[i, j] = num / den
                cor_matrix[j, i] = num / den
        inv_cor_matrix = np.linalg.pinv(cor_matrix)
        VIF = np.max(np.diag(inv_cor_matrix))

        if showcorrelations:
            print("Correlation Matrix:\n", cor_matrix)
            print("Inverted Correlation Matrix:\n", inv_cor_matrix)
            print("Variance Inflation Factor (VIF):", VIF)

    return dist_data

Response: Returns a new Latin - Hypercube sample design for the objects in the specified object.


Instruction:
How would you explain what the following Python 3 function does
def to_uncertain_func(x):
    if isinstance(x, UncertainFunction):
        return x

    # ! In Python 2.6+, numbers.Number could be used instead, here:
    elif isinstance(x, CONSTANT_TYPES):
        # No variable => no derivative to define:
        return UncertainFunction([x] * npts)

    raise NotUpcast("%s cannot be converted to a number with" " uncertainty" % type(x))

Response: Converts x into an UncertainFunction - compatible object.


Instruction:
Implement a Python 3 function for
returning a random variate of the beta distribution.

Response:
def Beta(alpha, beta, low=0, high=1, tag=None):
    """
    A Beta random variate

    Parameters
    ----------
    alpha : scalar
        The first shape parameter
    beta : scalar
        The second shape parameter

    Optional
    --------
    low : scalar
        Lower bound of the distribution support (default=0)
    high : scalar
        Upper bound of the distribution support (default=1)
    """
    assert (
        alpha > 0 and beta > 0
    ), 'Beta "alpha" and "beta" parameters must be greater than zero'
    assert low < high, 'Beta "low" must be less than "high"'
    return uv(ss.beta(alpha, beta, loc=low, scale=high - low), tag=tag)


Instruction:
Can you implement a function in Python 3 that
returns a random variate of the given size with the Bradford distribution.

Response:
def Bradford(q, low=0, high=1, tag=None):
    """
    A Bradford random variate

    Parameters
    ----------
    q : scalar
        The shape parameter
    low : scalar
        The lower bound of the distribution (default=0)
    high : scalar
        The upper bound of the distribution (default=1)
    """
    assert q > 0, 'Bradford "q" parameter must be greater than zero'
    assert low < high, 'Bradford "low" parameter must be less than "high"'
    return uv(ss.bradford(q, loc=low, scale=high - low), tag=tag)


Instruction:
Create a Python 3 function to
return a random variate of the Chi - Squared distribution.

Response:
def ChiSquared(k, tag=None):
    """
    A Chi-Squared random variate

    Parameters
    ----------
    k : int
        The degrees of freedom of the distribution (must be greater than one)
    """
    assert int(k) == k and k >= 1, 'Chi-Squared "k" must be an integer greater than 0'
    return uv(ss.chi2(k), tag=tag)


Instruction:
Can you write a function in Python 3 where it
returns a new random variate with the specified size and scale.

Response:
def Erlang(k, lamda, tag=None):
    """
    An Erlang random variate.

    This distribution is the same as a Gamma(k, theta) distribution, but
    with the restriction that k must be a positive integer. This
    is provided for greater compatibility with other simulation tools, but
    provides no advantage over the Gamma distribution in its applications.

    Parameters
    ----------
    k : int
        The shape parameter (must be a positive integer)
    lamda : scalar
        The scale parameter (must be greater than zero)
    """
    assert int(k) == k and k > 0, 'Erlang "k" must be a positive integer'
    assert lamda > 0, 'Erlang "lamda" must be greater than zero'
    return Gamma(k, lamda, tag)


Instruction:
Given the following Python 3 function, write the documentation
def Exponential(lamda, tag=None):
    assert lamda > 0, 'Exponential "lamda" must be greater than zero'
    return uv(ss.expon(scale=1.0 / lamda), tag=tag)

Response: Returns a random variate with exponential distribution.


Instruction:
How would you code a function in Python 3 to
return the Extreme Value maximum random variate.

Response:
def ExtValueMax(mu, sigma, tag=None):
    """
    An Extreme Value Maximum random variate.

    Parameters
    ----------
    mu : scalar
        The location parameter
    sigma : scalar
        The scale parameter (must be greater than zero)
    """
    assert sigma > 0, 'ExtremeValueMax "sigma" must be greater than zero'
    p = U(0, 1)._mcpts[:]
    return UncertainFunction(mu - sigma * np.log(-np.log(p)), tag=tag)


Instruction:
Can you implement a function in Python 3 that
returns a random variate of the Fisher factor.

Response:
def Fisher(d1, d2, tag=None):
    """
    An F (fisher) random variate

    Parameters
    ----------
    d1 : int
        Numerator degrees of freedom
    d2 : int
        Denominator degrees of freedom
    """
    assert (
        int(d1) == d1 and d1 >= 1
    ), 'Fisher (F) "d1" must be an integer greater than 0'
    assert (
        int(d2) == d2 and d2 >= 1
    ), 'Fisher (F) "d2" must be an integer greater than 0'
    return uv(ss.f(d1, d2), tag=tag)


Instruction:
Create a Python 3 function to
return a random variate of the Gamma distribution.

Response:
def Gamma(k, theta, tag=None):
    """
    A Gamma random variate

    Parameters
    ----------
    k : scalar
        The shape parameter (must be positive and non-zero)
    theta : scalar
        The scale parameter (must be positive and non-zero)
    """
    assert (
        k > 0 and theta > 0
    ), 'Gamma "k" and "theta" parameters must be greater than zero'
    return uv(ss.gamma(k, scale=theta), tag=tag)


Instruction:
Given the following Python 3 function, write the documentation
def LogNormal(mu, sigma, tag=None):
    assert sigma > 0, 'Log-Normal "sigma" must be positive'
    return uv(ss.lognorm(sigma, loc=mu), tag=tag)

Response: Returns a random variate with lognormal distribution.


Instruction:
How would you code a function in Python 3 to
return a random variate with a normal distribution.

Response:
def Normal(mu, sigma, tag=None):
    """
    A Normal (or Gaussian) random variate

    Parameters
    ----------
    mu : scalar
        The mean value of the distribution
    sigma : scalar
        The standard deviation (must be positive and non-zero)
    """
    assert sigma > 0, 'Normal "sigma" must be greater than zero'
    return uv(ss.norm(loc=mu, scale=sigma), tag=tag)


Instruction:
Can you tell what is the following Python 3 function doing
def Pareto(q, a, tag=None):
    assert q > 0 and a > 0, 'Pareto "q" and "a" must be positive scalars'
    p = Uniform(0, 1, tag)
    return a * (1 - p) ** (-1.0 / q)

Response: Returns a random variate of the given size q and a.


Instruction:
Write a Python 3 function that can
return a Pareto random variate.

Response:
def Pareto2(q, b, tag=None):
    """
    A Pareto random variate (second kind). This form always starts at the
    origin.

    Parameters
    ----------
    q : scalar
        The scale parameter
    b : scalar
        The shape parameter
    """
    assert q > 0 and b > 0, 'Pareto2 "q" and "b" must be positive scalars'
    return Pareto(q, b, tag) - b


Instruction:
Can you implement a function in Python 3 that
returns a random variate of the given degree of freedom.

Response:
def StudentT(v, tag=None):
    """
    A Student-T random variate

    Parameters
    ----------
    v : int
        The degrees of freedom of the distribution (must be greater than one)
    """
    assert int(v) == v and v >= 1, 'Student-T "v" must be an integer greater than 0'
    return uv(ss.t(v), tag=tag)


Instruction:
Given the following Python 3 function, write the documentation
def Triangular(low, peak, high, tag=None):
    assert low <= peak <= high, 'Triangular "peak" must lie between "low" and "high"'
    low, peak, high = [float(x) for x in [low, peak, high]]
    return uv(
        ss.triang((1.0 * peak - low) / (high - low), loc=low, scale=(high - low)),
        tag=tag,
    )

Response: Returns a random variate of the given lower and upper bound of the triangle.


Instruction:
How would you implement a function in Python 3 that
returns a random variate with uniform values.

Response:
def Uniform(low, high, tag=None):
    """
    A Uniform random variate

    Parameters
    ----------
    low : scalar
        Lower bound of the distribution support.
    high : scalar
        Upper bound of the distribution support.
    """
    assert low < high, 'Uniform "low" must be less than "high"'
    return uv(ss.uniform(loc=low, scale=high - low), tag=tag)


Instruction:
Write a Python 3 script for
returning a random weibull random variate.

Response:
def Weibull(lamda, k, tag=None):
    """
    A Weibull random variate

    Parameters
    ----------
    lamda : scalar
        The scale parameter
    k : scalar
        The shape parameter
    """
    assert (
        lamda > 0 and k > 0
    ), 'Weibull "lamda" and "k" parameters must be greater than zero'
    return uv(ss.exponweib(lamda, k), tag=tag)


Instruction:
Explain what the following Python 3 code does
def Bernoulli(p, tag=None):
    assert (
        0 < p < 1
    ), 'Bernoulli probability "p" must be between zero and one, non-inclusive'
    return uv(ss.bernoulli(p), tag=tag)

Response: A Bernoulli random variate from a random variate.


Instruction:
Given the following Python 3 function, write the documentation
def Binomial(n, p, tag=None):
    assert (
        int(n) == n and n > 0
    ), 'Binomial number of trials "n" must be an integer greater than zero'
    assert (
        0 < p < 1
    ), 'Binomial probability "p" must be between zero and one, non-inclusive'
    return uv(ss.binom(n, p), tag=tag)

Response: Returns a random variate of the given number of trials and probability p.


Instruction:
Can you implement a function in Python 3 that
returns the geometric random variate of the given probability.

Response:
def Geometric(p, tag=None):
    """
    A Geometric random variate

    Parameters
    ----------
    p : scalar
        The probability of success
    """
    assert (
        0 < p < 1
    ), 'Geometric probability "p" must be between zero and one, non-inclusive'
    return uv(ss.geom(p), tag=tag)


Instruction:
Can you generate the documentation for the following Python 3 function
def Hypergeometric(N, n, K, tag=None):
    assert (
        int(N) == N and N > 0
    ), 'Hypergeometric total population size "N" must be an integer greater than zero.'
    assert (
        int(n) == n and 0 < n <= N
    ), 'Hypergeometric interest population size "n" must be an integer greater than zero and no more than the total population size.'
    assert (
        int(K) == K and 0 < K <= N
    ), 'Hypergeometric chosen population size "K" must be an integer greater than zero and no more than the total population size.'
    return uv(ss.hypergeom(N, n, K), tag=tag)

Response: Returns a Hypergeometric random variate from the given population size n and K.


Instruction:
Create a Python 3 function for
returning a random variate with a Poisson distribution.

Response:
def Poisson(lamda, tag=None):
    """
    A Poisson random variate

    Parameters
    ----------
    lamda : scalar
        The rate of an occurance within a specified interval of time or space.
    """
    assert lamda > 0, 'Poisson "lamda" must be greater than zero.'
    return uv(ss.poisson(lamda), tag=tag)


Instruction:
How would you code a function in Python 3 to
calculate the covariance matrix of uncertain variables.

Response:
def covariance_matrix(nums_with_uncert):
    """
    Calculate the covariance matrix of uncertain variables, oriented by the
    order of the inputs

    Parameters
    ----------
    nums_with_uncert : array-like
        A list of variables that have an associated uncertainty

    Returns
    -------
    cov_matrix : 2d-array-like
        A nested list containing covariance values

    Example
    -------

        >>> x = N(1, 0.1)
        >>> y = N(10, 0.1)
        >>> z = x + 2*y
        >>> covariance_matrix([x,y,z])
        [[  9.99694861e-03   2.54000840e-05   1.00477488e-02]
         [  2.54000840e-05   9.99823207e-03   2.00218642e-02]
         [  1.00477488e-02   2.00218642e-02   5.00914772e-02]]

    """
    ufuncs = list(map(to_uncertain_func, nums_with_uncert))
    cov_matrix = []
    for (i1, expr1) in enumerate(ufuncs):
        coefs_expr1 = []
        mean1 = expr1.mean
        for (i2, expr2) in enumerate(ufuncs[: i1 + 1]):
            mean2 = expr2.mean
            coef = np.mean((expr1._mcpts - mean1) * (expr2._mcpts - mean2))
            coefs_expr1.append(coef)
        cov_matrix.append(coefs_expr1)

    # We symmetrize the matrix:
    for (i, covariance_coefs) in enumerate(cov_matrix):
        covariance_coefs.extend(cov_matrix[j][i] for j in range(i + 1, len(cov_matrix)))

    return cov_matrix


Instruction:
Create a Python 3 function for
calculating the correlation matrix of uncertain variables oriented by the order of the inputs.

Response:
def correlation_matrix(nums_with_uncert):
    """
    Calculate the correlation matrix of uncertain variables, oriented by the
    order of the inputs

    Parameters
    ----------
    nums_with_uncert : array-like
        A list of variables that have an associated uncertainty

    Returns
    -------
    corr_matrix : 2d-array-like
        A nested list containing covariance values

    Example
    -------

        >>> x = N(1, 0.1)
        >>> y = N(10, 0.1)
        >>> z = x + 2*y
        >>> correlation_matrix([x,y,z])
        [[ 0.99969486  0.00254001  0.4489385 ]
         [ 0.00254001  0.99982321  0.89458702]
         [ 0.4489385   0.89458702  1.        ]]

    """
    ufuncs = list(map(to_uncertain_func, nums_with_uncert))
    data = np.vstack([ufunc._mcpts for ufunc in ufuncs])
    return np.corrcoef(data.T, rowvar=0)


Instruction:
Can you tell what is the following Python 3 function doing
def var(self):
        mn = self.mean
        vr = np.mean((self._mcpts - mn) ** 2)
        return vr

Response: Returns the variance value of the current object


Instruction:
Can you generate a brief explanation for the following Python 3 code
def kurt(self):
        mn = self.mean
        sd = self.std
        kt = 0.0 if abs(sd) <= 1e-8 else np.mean((self._mcpts - mn) ** 4) / sd ** 4
        return kt

Response: Returns the Kurtosis coefficient value as a result of an uncertainty calculation


Instruction:
Write a Python 3 function that can
return a list of the first four standard moments of a distribution.

Response:
def stats(self):
        """
        The first four standard moments of a distribution: mean, variance, and
        standardized skewness and kurtosis coefficients.
        """
        mn = self.mean
        vr = self.var
        sk = self.skew
        kt = self.kurt
        return [mn, vr, sk, kt]


Instruction:
Write a Python 3 function that can
get the actual distribution value at a given percentile or set of percentiles.

Response:
def percentile(self, val):
        """
        Get the distribution value at a given percentile or set of percentiles.
        This follows the NIST method for calculating percentiles.

        Parameters
        ----------
        val : scalar or array
            Either a single value or an array of values between 0 and 1.

        Returns
        -------
        out : scalar or array
            The actual distribution value that appears at the requested
            percentile value or values

        """
        try:
            # test to see if an input is given as an array
            out = [self.percentile(vi) for vi in val]
        except (ValueError, TypeError):
            if val <= 0:
                out = float(min(self._mcpts))
            elif val >= 1:
                out = float(max(self._mcpts))
            else:
                tmp = np.sort(self._mcpts)
                n = val * (len(tmp) + 1)
                k, d = int(n), n - int(n)
                out = float(tmp[k] + d * (tmp[k + 1] - tmp[k]))
        if isinstance(val, np.ndarray):
            out = np.array(out)
        return out


Instruction:
How would you implement a function in Python 3 that
prints the description of the object.

Response:
def describe(self, name=None):
        """
        Cleanly show what the four displayed distribution moments are:
            - Mean
            - Variance
            - Standardized Skewness Coefficient
            - Standardized Kurtosis Coefficient

        For a standard Normal distribution, these are [0, 1, 0, 3].

        If the object has an associated tag, this is presented. If the optional
        ``name`` kwarg is utilized, this is presented as with the moments.
        Otherwise, no unique name is presented.

        Example
        =======
        ::

            >>> x = N(0, 1, 'x')
            >>> x.describe()  # print tag since assigned
            MCERP Uncertain Value (x):
            ...

            >>> x.describe('foobar')  # 'name' kwarg takes precedence
            MCERP Uncertain Value (foobar):
            ...

            >>> y = x**2
            >>> y.describe('y')  # print name since assigned
            MCERP Uncertain Value (y):
            ...

            >>> y.describe()  # print nothing since no tag
            MCERP Uncertain Value:
            ...

         """
        mn, vr, sk, kt = self.stats
        if name is not None:
            s = "MCERP Uncertain Value (" + name + "):\n"
        elif self.tag is not None:
            s = "MCERP Uncertain Value (" + self.tag + "):\n"
        else:
            s = "MCERP Uncertain Value:\n"
        s += " > Mean................... {: }\n".format(mn)
        s += " > Variance............... {: }\n".format(vr)
        s += " > Skewness Coefficient... {: }\n".format(sk)
        s += " > Kurtosis Coefficient... {: }\n".format(kt)
        print(s)


Instruction:
Write a Python 3 function for
plotting the distribution of the UncertainFunction. By default the plot is displayed with a density estimate estimate.

Response:
def plot(self, hist=False, show=False, **kwargs):
        """
        Plot the distribution of the UncertainFunction. By default, the
        distribution is shown with a kernel density estimate (kde).

        Optional
        --------
        hist : bool
            If true, a density histogram is displayed (histtype='stepfilled')
        show : bool
            If ``True``, the figure will be displayed after plotting the
            distribution. If ``False``, an explicit call to ``plt.show()`` is
            required to display the figure.
        kwargs : any valid matplotlib.pyplot.plot or .hist kwarg

        """
        import matplotlib.pyplot as plt

        vals = self._mcpts
        low = min(vals)
        high = max(vals)

        p = ss.kde.gaussian_kde(vals)
        xp = np.linspace(low, high, 100)

        if hist:
            h = plt.hist(
                vals,
                bins=int(np.sqrt(len(vals)) + 0.5),
                histtype="stepfilled",
                normed=True,
                **kwargs
            )
            plt.ylim(0, 1.1 * h[0].max())
        else:
            plt.plot(xp, p.evaluate(xp), **kwargs)

        plt.xlim(low - (high - low) * 0.1, high + (high - low) * 0.1)

        if show:
            self.show()


Instruction:
Make a summary of the following Python 3 code
def plot(self, hist=False, show=False, **kwargs):
        import matplotlib.pyplot as plt

        if hist:
            vals = self._mcpts
            low = vals.min()
            high = vals.max()
            h = plt.hist(
                vals,
                bins=int(np.sqrt(len(vals)) + 0.5),
                histtype="stepfilled",
                normed=True,
                **kwargs
            )
            plt.ylim(0, 1.1 * h[0].max())
        else:
            bound = 0.0001
            low = self.rv.ppf(bound)
            high = self.rv.ppf(1 - bound)
            if hasattr(self.rv.dist, "pmf"):
                low = int(low)
                high = int(high)
                vals = list(range(low, high + 1))
                plt.plot(vals, self.rv.pmf(vals), "o", **kwargs)
            else:
                vals = np.linspace(low, high, 500)
                plt.plot(vals, self.rv.pdf(vals), **kwargs)
        plt.xlim(low - (high - low) * 0.1, high + (high - low) * 0.1)

        if show:
            self.show()

Response: Plots the distribution of the UncertainVariable.


Instruction:
How would you implement a function in Python 3 that
loads the hat from a picture at path.

Response:
def load_hat(self, path):  # pylint: disable=no-self-use
        """Loads the hat from a picture at path.

        Args:
            path: The path to load from

        Returns:
            The hat data.
        """
        hat = cv2.imread(path, cv2.IMREAD_UNCHANGED)
        if hat is None:
            raise ValueError('No hat image found at `{}`'.format(path))
        b, g, r, a = cv2.split(hat)
        return cv2.merge((r, g, b, a))


Instruction:
Can you create a Python 3 function that
uses a haarcascade to detect faces inside an image.

Response:
def find_faces(self, image, draw_box=False):
        """Uses a haarcascade to detect faces inside an image.

        Args:
            image: The image.
            draw_box: If True, the image will be marked with a rectangle.

        Return:
            The faces as returned by OpenCV's detectMultiScale method for
            cascades.
        """
        frame_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
        faces = self.cascade.detectMultiScale(
            frame_gray,
            scaleFactor=1.3,
            minNeighbors=5,
            minSize=(50, 50),
            flags=0)

        if draw_box:
            for x, y, w, h in faces:
                cv2.rectangle(image, (x, y),
                              (x + w, y + h), (0, 255, 0), 2)
        return faces


Instruction:
Write a Python 3 function that can
find instances of rsrc_type that match the filter in kwargs

Response:
def find_resources(self, rsrc_type, sort=None, yield_pages=False, **kwargs):
        """Find instances of `rsrc_type` that match the filter in `**kwargs`"""
        return rsrc_type.find(self, sort=sort, yield_pages=yield_pages, **kwargs)


Instruction:
Make a summary of the following Python 3 code
def changed(self, message=None, *args):
        if message is not None:
            self.logger.debug('%s: %s', self._repr(), message % args)
        self.logger.debug('%s: changed', self._repr())
        if self.parent is not None:
            self.parent.changed()
        elif isinstance(self, Mutable):
            super(TrackedObject, self).changed()

Response: Marks the object as changed.


Instruction:
Explain what the following Python 3 code does
def register(cls, origin_type):
        def decorator(tracked_type):
            """Adds the decorated class to the `_type_mapping` dictionary."""
            cls._type_mapping[origin_type] = tracked_type
            return tracked_type
        return decorator

Response: Decorator for mutation tracker registration.


Instruction:
How would you explain what the following Python 3 function does
def convert(cls, obj, parent):
        replacement_type = cls._type_mapping.get(type(obj))
        if replacement_type is not None:
            new = replacement_type(obj)
            new.parent = parent
            return new
        return obj

Response: Converts objects to registered tracked types.


Instruction:
Here you have a function in Python 3, explain what it does
def convert_items(self, items):
        return ((key, self.convert(value, self)) for key, value in items)

Response: Generator like convert_iterable but for 2 - tuple iterators.


Instruction:
Can you generate the documentation for the following Python 3 function
def convert_mapping(self, mapping):
        if isinstance(mapping, dict):
            return self.convert_items(iteritems(mapping))
        return self.convert_items(mapping)

Response: Convenience method to track either a dict or a 2 - tuple iterator.


Instruction:
How would you code a function in Python 3 to
redirect to the changelist view if we only have one preference object redirect to it otherwise display listing.

Response:
def changelist_view(self, request, extra_context=None):
        """
        If we only have a single preference object redirect to it,
        otherwise display listing.
        """
        model = self.model
        if model.objects.all().count() > 1:
            return super(PreferencesAdmin, self).changelist_view(request)
        else:
            obj = model.singleton.get()
            return redirect(
                reverse(
                    'admin:%s_%s_change' % (
                        model._meta.app_label, model._meta.model_name
                    ),
                    args=(obj.id,)
                )
            )


Instruction:
Given the following Python 3 function, write the documentation
def aslist(generator):
    'Function decorator to transform a generator into a list'
    def wrapper(*args, **kwargs):
        return list(generator(*args, **kwargs))
    return wrapper

Response: Function decorator to transform a generator into a list


Instruction:
Implement a Python 3 function for
getting a package release from a Python package.

Response:
def get_package_release_from_pypi(pkg_name, version, pypi_json_api_url, allowed_classifiers):
    """
    No classifier-based selection of Python packages is currently implemented: for now we don't fetch any .whl or .egg
    Eventually, we should select the best release available, based on the classifier & PEP 425: https://www.python.org/dev/peps/pep-0425/
    E.g. a wheel when available but NOT for tornado 4.3 for example, where available wheels are only for Windows.
    Note also that some packages don't have .whl distributed, e.g. https://bugs.launchpad.net/lxml/+bug/1176147
    """
    matching_releases = get_package_releases_matching_version(pkg_name, version, pypi_json_api_url)
    src_releases = [release for release in matching_releases if release['python_version'] == 'source']
    if src_releases:
        return select_src_release(src_releases, pkg_name, target_classifiers=('py2.py3-none-any',), select_arbitrary_version_if_none_match=True)
    if allowed_classifiers:
        return select_src_release(matching_releases, pkg_name, target_classifiers=allowed_classifiers)
    raise PypiQueryError('No source supported found for package {} version {}'.format(pkg_name, version))


Instruction:
Implement a function in Python 3 to
extract a PEP425 - compliant classifier and extension from a file.

Response:
def extract_classifier_and_extension(pkg_name, filename):
    """
    Returns a PEP425-compliant classifier (or 'py2.py3-none-any' if it cannot be extracted),
    and the file extension
    TODO: return a classifier 3-members namedtuple instead of a single string
    """
    basename, _, extension = filename.rpartition('.')
    if extension == 'gz' and filename.endswith('.tar.gz'):
        extension = 'tar.gz'
        basename = filename[:-7]
    if basename == pkg_name or basename[len(pkg_name)] != '-':
        return 'py2.py3-none-any', extension
    basename = basename[len(pkg_name)+1:]
    classifier_parts = basename.split('-')
    if len(classifier_parts) < 3:
        return 'py2.py3-none-any', extension
    if len(classifier_parts) == 3:
        _, _, classifier_parts[0] = classifier_parts[0].rpartition('.')
    return '-'.join(classifier_parts[-3:]), extension


Instruction:
Can you generate the documentation for the following Python 3 function
def coerce(cls, key, value):
        if value is None:
            return value
        if isinstance(value, cls):
            return value
        if isinstance(value, dict):
            return NestedMutableDict.coerce(key, value)
        if isinstance(value, list):
            return NestedMutableList.coerce(key, value)
        return super(cls).coerce(key, value)

Response: Convert plain dictionary to NestedMutable.


Instruction:
Make a summary of the following Python 3 code
def is_mod_function(mod, fun):
    return inspect.isfunction(fun) and inspect.getmodule(fun) == mod

Response: Checks if a function in a module was declared in that module.


Instruction:
Given the following Python 3 function, write the documentation
def is_mod_class(mod, cls):
    return inspect.isclass(cls) and inspect.getmodule(cls) == mod

Response: Checks if a class in a module was declared in that module.


Instruction:
Can you implement a function in Python 3 that
lists all functions declared in a module.

Response:
def list_functions(mod_name):
    """Lists all functions declared in a module.

    http://stackoverflow.com/a/1107150/3004221

    Args:
        mod_name: the module name
    Returns:
        A list of functions declared in that module.
    """
    mod = sys.modules[mod_name]
    return [func.__name__ for func in mod.__dict__.values()
            if is_mod_function(mod, func)]


Instruction:
Can you generate a brief explanation for the following Python 3 code
def list_classes(mod_name):
    mod = sys.modules[mod_name]
    return [cls.__name__ for cls in mod.__dict__.values()
            if is_mod_class(mod, cls)]

Response: Lists all classes declared in a module.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_linenumbers(functions, module, searchstr='def {}(image):\n'):
    lines = inspect.getsourcelines(module)[0]
    line_numbers = {}
    for function in functions:
        try:
            line_numbers[function] = lines.index(
                    searchstr.format(function)) + 1
        except ValueError:
            print(r'Can not find `{}`'.format(searchstr.format(function)))
            line_numbers[function] = 0
    return line_numbers

Response: Returns a dictionary which maps function names to line numbers.


Instruction:
Create a Python 3 function for
formatting the documentation in a nicer way and for notebook cells.

Response:
def format_doc(fun):
    """Formats the documentation in a nicer way and for notebook cells."""
    SEPARATOR = '============================='
    func = cvloop.functions.__dict__[fun]

    doc_lines = ['{}'.format(l).strip() for l in func.__doc__.split('\n')]
    if hasattr(func, '__init__'):
        doc_lines.append(SEPARATOR)
        doc_lines += ['{}'.format(l).strip() for l in
                      func.__init__.__doc__.split('\n')]

    mod_lines = []
    argblock = False
    returnblock = False
    for line in doc_lines:
        if line == SEPARATOR:
            mod_lines.append('\n#### `{}.__init__(...)`:\n\n'.format(fun))
        elif 'Args:' in line:
            argblock = True
            if GENERATE_ARGS:
                mod_lines.append('**{}**\n'.format(line))
        elif 'Returns:' in line:
            returnblock = True
            mod_lines.append('\n**{}**'.format(line))
        elif not argblock and not returnblock:
            mod_lines.append('{}\n'.format(line))
        elif argblock and not returnblock and ':' in line:
            if GENERATE_ARGS:
                mod_lines.append('- *{}:* {}\n'.format(
                    *line.split(':')))
        elif returnblock:
            mod_lines.append(line)
        else:
            mod_lines.append('{}\n'.format(line))
    return mod_lines


Instruction:
How would you explain what the following Python 3 function does
def prepare_axes(axes, title, size, cmap=None):
    if axes is None:
        return None

    # prepare axis itself
    axes.set_xlim([0, size[1]])
    axes.set_ylim([size[0], 0])
    axes.set_aspect('equal')

    axes.axis('off')
    if isinstance(cmap, str):
        title = '{} (cmap: {})'.format(title, cmap)
    axes.set_title(title)

    # prepare image data
    axes_image = image.AxesImage(axes, cmap=cmap,
                                 extent=(0, size[1], size[0], 0))
    axes_image.set_data(np.random.random((size[0], size[1], 3)))

    axes.add_image(axes_image)
    return axes_image

Response: Prepares an axes object for clean plotting.


Instruction:
Given the following Python 3 function, write the documentation
def connect_event_handlers(self):
        self.figure.canvas.mpl_connect('close_event', self.evt_release)
        self.figure.canvas.mpl_connect('pause_event', self.evt_toggle_pause)

Response: Connects event handlers to the figure.


Instruction:
Given the following Python 3 function, write the documentation
def evt_toggle_pause(self, *args):  # pylint: disable=unused-argument
        if self.event_source._timer is None:  # noqa: e501 pylint: disable=protected-access
            self.event_source.start()
        else:
            self.event_source.stop()

Response: Pauses and resumes the video source.


Instruction:
Write a Python 3 function that can
print information about the unprocessed image.

Response:
def print_info(self, capture):
        """Prints information about the unprocessed image.

        Reads one frame from the source to determine image colors, dimensions
        and data types.

        Args:
            capture: the source to read from.
        """
        self.frame_offset += 1
        ret, frame = capture.read()
        if ret:
            print('Capture Information')
            print('\tDimensions (HxW): {}x{}'.format(*frame.shape[0:2]))
            print('\tColor channels:   {}'.format(frame.shape[2] if
                                                  len(frame.shape) > 2 else 1))
            print('\tColor range:      {}-{}'.format(np.min(frame),
                                                     np.max(frame)))
            print('\tdtype:            {}'.format(frame.dtype))
        else:
            print('No source found.')


Instruction:
Create a Python 3 function to
determine the height and width of the image source.

Response:
def determine_size(self, capture):
        """Determines the height and width of the image source.

        If no dimensions are available, this method defaults to a resolution of
        640x480, thus returns (480, 640).
        If capture has a get method it is assumed to understand
        `cv2.CAP_PROP_FRAME_WIDTH` and `cv2.CAP_PROP_FRAME_HEIGHT` to get the
        information. Otherwise it reads one frame from the source to determine
        image dimensions.

        Args:
            capture: the source to read from.

        Returns:
            A tuple containing integers of height and width (simple casts).
        """
        width = 640
        height = 480
        if capture and hasattr(capture, 'get'):
            width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
            height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
        else:
            self.frame_offset += 1
            ret, frame = capture.read()
            if ret:
                width = frame.shape[1]
                height = frame.shape[0]
        return (int(height), int(width))


Instruction:
Write a Python 3 function for
initializing the drawing of the images by setting the images to random colors.

Response:
def _init_draw(self):
        """Initializes the drawing of the frames by setting the images to
        random colors.

        This function is called by TimedAnimation.
        """
        if self.original is not None:
            self.original.set_data(np.random.random((10, 10, 3)))
        self.processed.set_data(np.random.random((10, 10, 3)))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def read_frame(self):
        ret, frame = self.capture.read()
        if not ret:
            self.event_source.stop()
            try:
                self.capture.release()
            except AttributeError:
                # has no release method, thus just pass
                pass
            return None
        if self.convert_color != -1 and is_color_image(frame):
            return cv2.cvtColor(frame, self.convert_color)
        return frame

Response: Reads a frame and converts it to a color if needed.


Instruction:
How would you code a function in Python 3 to
annotate the processed axis with given annotations for the current frame number.

Response:
def annotate(self, framedata):
        """Annotates the processed axis with given annotations for
        the provided framedata.

        Args:
            framedata: The current frame number.
        """
        for artist in self.annotation_artists:
            artist.remove()
        self.annotation_artists = []
        for annotation in self.annotations:
            if annotation[2] > framedata:
                return
            if annotation[2] == framedata:
                pos = annotation[0:2]
                shape = self.annotations_default['shape']
                color = self.annotations_default['color']
                size = self.annotations_default['size']
                line = self.annotations_default['line']
                if len(annotation) > 3:
                    shape = annotation[3].get('shape', shape)
                    color = annotation[3].get('color', color)
                    size = annotation[3].get('size', size)
                    line = annotation[3].get('line', line)
                if shape == 'CIRC' and hasattr(size, '__len__'):
                    size = 30

                if not hasattr(color, '__len__'):
                    color = (color,) * 3

                if shape == 'RECT':
                    patch = patches.Rectangle((pos[0] - size[0] // 2,
                                               pos[1] - size[1] // 2),
                                              size[0], size[1], fill=False,
                                              lw=line, fc='none', ec=color)
                elif shape == 'CIRC':
                    patch = patches.CirclePolygon(pos, radius=size, fc='none',
                                                  ec=color, lw=line)
                self.annotation_artists.append(patch)
                self.axes_processed.add_artist(self.annotation_artists[-1])


Instruction:
Write a Python 3 function for
reading processes and draws the frames.

Response:
def _draw_frame(self, framedata):
        """Reads, processes and draws the frames.

        If needed for color maps, conversions to gray scale are performed. In
        case the images are no color images and no custom color maps are
        defined, the colormap `gray` is applied.

        This function is called by TimedAnimation.

        Args:
            framedata: The frame data.
        """
        original = self.read_frame()
        if original is None:
            self.update_info(self.info_string(message='Finished.',
                                              frame=framedata))
            return

        if self.original is not None:
            processed = self.process_frame(original.copy())

            if self.cmap_original is not None:
                original = to_gray(original)
            elif not is_color_image(original):
                self.original.set_cmap('gray')
            self.original.set_data(original)
        else:
            processed = self.process_frame(original)

        if self.cmap_processed is not None:
            processed = to_gray(processed)
        elif not is_color_image(processed):
            self.processed.set_cmap('gray')

        if self.annotations:
            self.annotate(framedata)

        self.processed.set_data(processed)

        self.update_info(self.info_string(frame=framedata))


Instruction:
Given the following Python 3 function, write the documentation
def update_info(self, custom=None):
        self.figure.suptitle(self.info_string() if custom is None else custom)

Response: Updates the figure s suptitle.


Instruction:
Here you have a function in Python 3, explain what it does
def info_string(self, size=None, message='', frame=-1):
        info = []
        if size is not None:
            info.append('Size: {1}x{0}'.format(*size))
        elif self.size is not None:
            info.append('Size: {1}x{0}'.format(*self.size))
        if frame >= 0:
            info.append('Frame: {}'.format(frame))
        if message != '':
            info.append('{}'.format(message))
        return ' '.join(info)

Response: Returns a string containing size frame number and info messages.


Instruction:
Given the following Python 3 function, write the documentation
def main():
    with open(sys.argv[1], 'r') as nbfile:
        notebook = json.load(nbfile)

    # remove kernelspec (venvs)
    try:
        del notebook['metadata']['kernelspec']
    except KeyError:
        pass

    # remove outputs and metadata, set execution counts to None
    for cell in notebook['cells']:
        try:
            if cell['cell_type'] == 'code':
                cell['outputs'] = []
                cell['execution_count'] = None
            cell['metadata'] = {}
        except KeyError:
            pass

    with open(sys.argv[1], 'w') as nbfile:
        json.dump(notebook, nbfile, indent=1)

Response: Sanitizes the loaded *. ipynb.


Instruction:
Write a Python 3 function for
creating comment :param comment: :param mentions: list of pair of code and type("USER", "GROUP", and so on) :return:

Response:
def create(self, comment, mentions=()):
        """
        create comment
        :param comment:
        :param mentions: list of pair of code and type("USER", "GROUP", and so on)
        :return:
        """

        data = {
            "app": self.app_id,
            "record": self.record_id,
            "comment": {
                "text": comment,
            }
        }

        if len(mentions) > 0:
            _mentions = []
            for m in mentions:
                if isinstance(m, (list, tuple)):
                    if len(m) == 2:
                        _mentions.append({
                            "code": m[0],
                            "type": m[1]
                        })
                    else:
                        raise Exception("mention have to have code and target type. ex.[('user_1', 'USER')]")
                elif isinstance(m, Mention):
                    _mentions.append(m.serialize())

            data["comment"]["mentions"] = _mentions

        resp = self._request("POST", self._url, data)
        r = cr.CreateCommentResult(resp)
        return r


Instruction:
Can you generate the documentation for the following Python 3 function
def _consume(iterator, n=None):
    # Use functions that consume iterators at C speed.
    if n is None:
        # feed the entire iterator into a zero-length deque
        collections.deque(iterator, maxlen=0)
    else:
        # advance to the empty slice starting at position n
        next(itertools.islice(iterator, n, n), None)

Response: Advance the iterator n - steps ahead.


Instruction:
Can you write a function in Python 3 where it
calculates how many items must be in the collection to satisfy this slice

Response:
def _slice_required_len(slice_obj):
    """
    Calculate how many items must be in the collection to satisfy this slice

    returns `None` for slices may vary based on the length of the underlying collection
    such as `lst[-1]` or `lst[::]`
    """
    if slice_obj.step and slice_obj.step != 1:
        return None
    # (None, None, *) requires the entire list
    if slice_obj.start is None and slice_obj.stop is None:
        return None

    # Negative indexes are hard without knowing the collection length
    if slice_obj.start and slice_obj.start < 0:
        return None
    if slice_obj.stop and slice_obj.stop < 0:
        return None

    if slice_obj.stop:
        if slice_obj.start and slice_obj.start > slice_obj.stop:
            return 0
        return slice_obj.stop
    return slice_obj.start + 1


Instruction:
Implement a function in Python 3 to
stylize text with given styles.

Response:
def stylize_interactive(text, styles, reset=True):
    """stylize() variant that adds C0 control codes (SOH/STX) for readline
    safety."""
    # problem: readline includes bare ANSI codes in width calculations.
    # solution: wrap nonprinting codes in SOH/STX when necessary.
    # see: https://github.com/dslackw/colored/issues/5
    terminator = _c0wrap(attr("reset")) if reset else ""
    return "{}{}{}".format(_c0wrap(styles), text, terminator)


Instruction:
Explain what the following Python 3 code does
def attribute(self):

        paint = {
            "bold": self.ESC + "1" + self.END,
            1: self.ESC + "1" + self.END,
            "dim": self.ESC + "2" + self.END,
            2: self.ESC + "2" + self.END,
            "underlined": self.ESC + "4" + self.END,
            4: self.ESC + "4" + self.END,
            "blink": self.ESC + "5" + self.END,
            5: self.ESC + "5" + self.END,
            "reverse": self.ESC + "7" + self.END,
            7: self.ESC + "7" + self.END,
            "hidden": self.ESC + "8" + self.END,
            8: self.ESC + "8" + self.END,
            "reset": self.ESC + "0" + self.END,
            0: self.ESC + "0" + self.END,
            "res_bold": self.ESC + "21" + self.END,
            21: self.ESC + "21" + self.END,
            "res_dim": self.ESC + "22" + self.END,
            22: self.ESC + "22" + self.END,
            "res_underlined": self.ESC + "24" + self.END,
            24: self.ESC + "24" + self.END,
            "res_blink": self.ESC + "25" + self.END,
            25: self.ESC + "25" + self.END,
            "res_reverse": self.ESC + "27" + self.END,
            27: self.ESC + "27" + self.END,
            "res_hidden": self.ESC + "28" + self.END,
            28: self.ESC + "28" + self.END,
        }
        return paint[self.color]

Response: Set or reset attributes of the class.


Instruction:
Implement a function in Python 3 to
print 256 foreground colors

Response:
def foreground(self):
        """Print 256 foreground colors"""
        code = self.ESC + "38;5;"
        if str(self.color).isdigit():
            self.reverse_dict()
            color = self.reserve_paint[str(self.color)]
            return code + self.paint[color] + self.END
        elif self.color.startswith("#"):
            return code + str(self.HEX) + self.END
        else:
            return code + self.paint[self.color] + self.END


Instruction:
Implement a function in Python 3 to
perform a reset and check for presence pulse.

Response:
def reset(self, required=False):
        """
        Perform a reset and check for presence pulse.

        :param bool required: require presence pulse
        """
        reset = self._ow.reset()
        if required and reset:
            raise OneWireError("No presence pulse found. Check devices and wiring.")
        return not reset


Instruction:
Create a Python 3 function to
read from the device into a byte array.

Response:
def readinto(self, buf, *, start=0, end=None):
        """
        Read into ``buf`` from the device. The number of bytes read will be the
        length of ``buf``.

        If ``start`` or ``end`` is provided, then the buffer will be sliced
        as if ``buf[start:end]``. This will not cause an allocation like
        ``buf[start:end]`` will so it saves memory.

        :param bytearray buf: buffer to write into
        :param int start: Index to start writing at
        :param int end: Index to write up to but not include
        """
        if end is None:
            end = len(buf)
        for i in range(start, end):
            buf[i] = self._readbyte()


Instruction:
Given the following Python 3 function, write the documentation
def write(self, buf, *, start=0, end=None):
        if end is None:
            end = len(buf)
        for i in range(start, end):
            self._writebyte(buf[i])

Response: Write the bytes from buf to the device.


Instruction:
Can you tell what is the following Python 3 function doing
def scan(self):
        devices = []
        diff = 65
        rom = False
        count = 0
        for _ in range(0xff):
            rom, diff = self._search_rom(rom, diff)
            if rom:
                count += 1
                if count > self.maximum_devices:
                    raise RuntimeError(
                        "Maximum device count of {} exceeded."\
                        .format(self.maximum_devices))
                devices.append(OneWireAddress(rom))
            if diff == 0:
                break
        return devices

Response: Scan for devices on the bus and return a list of addresses.


Instruction:
Can you implement a function in Python 3 that
performs the 1 - Wire CRC check on the provided data.

Response:
def crc8(data):
        """
        Perform the 1-Wire CRC check on the provided data.

        :param bytearray data: 8 byte array representing 64 bit ROM code
        """
        crc = 0

        for byte in data:
            crc ^= byte
            for _ in range(8):
                if crc & 0x01:
                    crc = (crc >> 1) ^ 0x8C
                else:
                    crc >>= 1
                crc &= 0xFF
        return crc


Instruction:
How would you explain what the following Python 3 function does
def _deserialize(cls, json_body, get_value_and_type):

        instance = cls()
        is_set = False
        properties = cls._get_property_names(instance)

        def get_property_detail(name):
            p = [p for p in instance._property_details if p.name == name or p.field_name == name]
            return None if len(p) == 0 else p[0]

        for k in json_body:
            field = json_body[k]
            pd = get_property_detail(k)
            pn = k if not pd else pd.to_property_name(k)
            if pn in properties:
                v, t = get_value_and_type(field)
                initial_value = getattr(instance, pn)
                value = instance._field_to_property(v, t, pd, initial_value)
                setattr(instance, pn, value)
                is_set = True

        return instance if is_set else None

Response: Deserialize json to model


Instruction:
How would you explain what the following Python 3 function does
def _serialize(self, convert_to_key_and_value, ignore_missing=False):

        serialized = {}
        properties = self._get_property_names(self)

        def get_property_detail(name):
            p = [p for p in self._property_details if p.name == name]
            return None if len(p) == 0 else p[0]

        for p in properties:
            pd = get_property_detail(p)
            value = self._property_to_field(p, pd)
            field_name = p if not pd else pd.to_field_name()

            if value is None or (ignore_missing and not value) or (pd and pd.unsent):
                continue
            else:
                key, value = convert_to_key_and_value(field_name, value, pd)
                if key:
                    serialized[key] = value

        return serialized

Response: Serialize the current object to a dictionary.


Instruction:
How would you code a function in Python 3 to
read from the device into a buffer.

Response:
def readinto(self, buf, *, start=0, end=None):
        """
        Read into ``buf`` from the device. The number of bytes read will be the
        length of ``buf``.

        If ``start`` or ``end`` is provided, then the buffer will be sliced
        as if ``buf[start:end]``. This will not cause an allocation like
        ``buf[start:end]`` will so it saves memory.

        :param bytearray buf: buffer to write into
        :param int start: Index to start writing at
        :param int end: Index to write up to but not include
        """
        self._bus.readinto(buf, start=start, end=end)
        if start == 0 and end is None and len(buf) >= 8:
            if self._bus.crc8(buf):
                raise RuntimeError('CRC error.')


Instruction:
Write a Python 3 script for
writing the bytes from buf to the device.

Response:
def write(self, buf, *, start=0, end=None):
        """
        Write the bytes from ``buf`` to the device.

        If ``start`` or ``end`` is provided, then the buffer will be sliced
        as if ``buffer[start:end]``. This will not cause an allocation like
        ``buffer[start:end]`` will so it saves memory.

        :param bytearray buf: buffer containing the bytes to write
        :param int start: Index to start writing from
        :param int end: Index to read up to but not include
        """
        return self._bus.write(buf, start=start, end=end)


Instruction:
How would you explain what the following Python 3 function does
def preferences_class_prepared(sender, *args, **kwargs):
    cls = sender
    if issubclass(cls, Preferences):
        # Add singleton manager to subclasses.
        cls.add_to_class('singleton', SingletonManager())
        # Add property for preferences object to preferences.preferences.
        setattr(preferences.Preferences, cls._meta.object_name, property(lambda x: cls.singleton.get()))

Response: Add singleton manager to preferences. preferences and preferences. preferences.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def site_cleanup(sender, action, instance, **kwargs):
    if action == 'post_add':
        if isinstance(instance, Preferences) \
            and hasattr(instance.__class__, 'objects'):
            site_conflicts = instance.__class__.objects.filter(
                sites__in=instance.sites.all()
            ).only('id').distinct()

            for conflict in site_conflicts:
                if conflict.id != instance.id:
                    for site in instance.sites.all():
                        conflict.sites.remove(site)

Response: Remove sites from pre - existing preferences objects.


Instruction:
Implement a Python 3 function for
returning the first preferences object for the current site. If preferences do not exist create it.

Response:
def get_queryset(self):
        """
        Return the first preferences object for the current site.
        If preferences do not exist create it.
        """

        queryset = super(SingletonManager, self).get_queryset()

        # Get current site
        current_site = None
        if getattr(settings, 'SITE_ID', None) is not None:
            current_site = Site.objects.get_current()

        # If site found limit queryset to site.
        if current_site is not None:
            queryset = queryset.filter(sites=settings.SITE_ID)

        if not queryset.exists():
            # Create object (for current site) if it doesn't exist.
            obj = self.model.objects.create()
            if current_site is not None:
                obj.sites.add(current_site)

        return queryset


Instruction:
Explain what the following Python 3 code does
def load_iterable(self, iterable, session=None):
        '''Load an ``iterable``.

        By default it returns a generator of data loaded via the
        :meth:`loads` method.

        :param iterable: an iterable over data to load.
        :param session: Optional :class:`stdnet.odm.Session`.
        :return: an iterable over decoded data.
        '''
        data = []
        load = self.loads
        for v in iterable:
            data.append(load(v))
        return data

Response: Load an iterable over data.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def search_model(self, q, text, lookup=None):
        '''Implements :meth:`stdnet.odm.SearchEngine.search_model`.
It return a new :class:`stdnet.odm.QueryElem` instance from
the input :class:`Query` and the *text* to search.'''
        words = self.words_from_text(text, for_search=True)
        if not words:
            return q
        qs = self._search(words, include=(q.model,), lookup=lookup)
        qs = tuple((q.get_field('object_id') for q in qs))
        return odm.intersect((q,)+qs)

Response:
Implements : meth : stdnet. ODM. SearchEngine. search_model.
It returns a new : class : QueryElem instance from the input : class : Query and the text to search.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _search(self, words, include=None, exclude=None, lookup=None):
        '''Full text search. Return a list of queries to intersect.'''
        lookup = lookup or 'contains'
        query = self.router.worditem.query()
        if include:
            query = query.filter(model_type__in=include)
        if exclude:
            query = query.exclude(model_type__in=include)
        if not words:
            return [query]
        qs = []
        if lookup == 'in':
            # we are looking for items with at least one word in it
            qs.append(query.filter(word__in=words))
        elif lookup == 'contains':
            #we want to match every single words
            for word in words:
                qs.append(query.filter(word=word))
        else:
            raise ValueError('Unknown lookup "{0}"'.format(lookup))
        return qs

Response: Full text search. Return a list of queries to intersect.


Instruction:
How would you implement a function in Python 3 that
gets a new Redis client.

Response:
def redis_client(address=None, connection_pool=None, timeout=None,
                 parser=None, **kwargs):
    '''Get a new redis client.

    :param address: a ``host``, ``port`` tuple.
    :param connection_pool: optional connection pool.
    :param timeout: socket timeout.
    :param timeout: socket timeout.
    '''
    if not connection_pool:
        if timeout == 0:
            if not async:
                raise ImportError('Asynchronous connection requires async '
                                  'bindings installed.')
            return async.pool.redis(address, **kwargs)
        else:
            kwargs['socket_timeout'] = timeout
            return Redis(address[0], address[1], **kwargs)
    else:
        return Redis(connection_pool=connection_pool)


Instruction:
Create a Python 3 function to
return a bytestring version of s with the specified encoding as specified in encoding.

Response:
def to_bytes(s, encoding=None, errors='strict'):
    """Returns a bytestring version of 's',
encoded as specified in 'encoding'."""
    encoding = encoding or 'utf-8'
    if isinstance(s, bytes):
        if encoding != 'utf-8':
            return s.decode('utf-8', errors).encode(encoding, errors)
        else:
            return s
    if not is_string(s):
        s = string_type(s)
    return s.encode(encoding, errors)


Instruction:
Explain what the following Python 3 code does
def date_decimal_hook(dct):
    '''The default JSON decoder hook. It is the inverse of
:class:`stdnet.utils.jsontools.JSONDateDecimalEncoder`.'''
    if '__datetime__' in dct:
        return todatetime(dct['__datetime__'])
    elif '__date__' in dct:
        return todatetime(dct['__date__']).date()
    elif '__decimal__' in dct:
        return Decimal(dct['__decimal__'])
    else:
        return dct

Response:
The default JSON decoder hook. It is the inverse of
 : class : stdnet. utils. jsontools. JSONDateDecimalEncoder.


Instruction:
Here you have a function in Python 3, explain what it does
def flat_to_nested(data, instance=None, attname=None,
                   separator=None, loads=None):
    '''Convert a flat representation of a dictionary to
a nested representation. Fields in the flat representation are separated
by the *splitter* parameters.

:parameter data: a flat dictionary of key value pairs.
:parameter instance: optional instance of a model.
:parameter attribute: optional attribute of a model.
:parameter separator: optional separator. Default ``"__"``.
:parameter loads: optional data unserializer.
:rtype: a nested dictionary'''
    separator = separator or JSPLITTER
    val = {}
    flat_vals = {}
    for key, value in iteritems(data):
        if value is None:
            continue
        keys = key.split(separator)
        # first key equal to the attribute name
        if attname:
            if keys.pop(0) != attname:
                continue
        if loads:
            value = loads(value)
        # if an instance is available, inject the flat attribute
        if not keys:
            if value is None:
                val = flat_vals = {}
                break
            else:
                continue
        else:
            flat_vals[key] = value

        d = val
        lk = keys[-1]
        for k in keys[:-1]:
            if k not in d:
                nd = {}
                d[k] = nd
            else:
                nd = d[k]
                if not isinstance(nd, dict):
                    nd = {'': nd}
                    d[k] = nd
            d = nd
        if lk not in d:
            d[lk] = value
        else:
            d[lk][''] = value

    if instance and flat_vals:
        for attr, value in iteritems(flat_vals):
            setattr(instance, attr, value)

    return val

Response: Convert a flat representation of a dictionary to a nested representation.


Instruction:
Make a summary of the following Python 3 code
def dict_flat_generator(value, attname=None, splitter=JSPLITTER,
                        dumps=None, prefix=None, error=ValueError,
                        recursive=True):
    '''Convert a nested dictionary into a flat dictionary representation'''
    if not isinstance(value, dict) or not recursive:
        if not prefix:
            raise error('Cannot assign a non dictionary to a JSON field')
        else:
            name = '%s%s%s' % (attname, splitter,
                               prefix) if attname else prefix
            yield name, dumps(value) if dumps else value
    else:
        # loop over dictionary
        for field in value:
            val = value[field]
            key = prefix
            if field:
                key = '%s%s%s' % (prefix, splitter,
                                  field) if prefix else field
            for k, v2 in dict_flat_generator(val, attname, splitter, dumps,
                                             key, error, field):
                yield k, v2

Response: Convert a nested dictionary into a flat dictionary representation


Instruction:
Can you implement a function in Python 3 that
multiplies dictionaries by a numeric values and add them together.

Response:
def addmul_number_dicts(series):
    '''Multiply dictionaries by a numeric values and add them together.

:parameter series: a tuple of two elements tuples. Each serie is of the form::

        (weight,dictionary)

    where ``weight`` is a number and ``dictionary`` is a dictionary with
    numeric values.
:parameter skip: optional list of field names to skip.

Only common fields are aggregated. If a field has a non-numeric value it is
not included either.'''
    if not series:
        return
    vtype = value_type((s[1] for s in series))
    if vtype == 1:
        return sum((weight*float(d) for weight, d in series))
    elif vtype == 3:
        keys = set(series[0][1])
        for serie in series[1:]:
            keys.intersection_update(serie[1])
        results = {}
        for key in keys:
            key_series = tuple((weight, d[key]) for weight, d in series)
            result = addmul_number_dicts(key_series)
            if result is not None:
                results[key] = result
        return results


Instruction:
Can you implement a function in Python 3 that
downloads all TPFs for a given K2 campaign.

Response:
def Download(campaign=0, queue='build', email=None, walltime=8, **kwargs):
    '''
    Submits a cluster job to the build queue to download all TPFs for a given
    campaign.

    :param int campaign: The `K2` campaign to run
    :param str queue: The name of the queue to submit to. Default `build`
    :param str email: The email to send job status notifications to. \
           Default `None`
    :param int walltime: The number of hours to request. Default `8`

    '''

    # Figure out the subcampaign
    if type(campaign) is int:
        subcampaign = -1
    elif type(campaign) is float:
        x, y = divmod(campaign, 1)
        campaign = int(x)
        subcampaign = round(y * 10)
    # Submit the cluster job
    pbsfile = os.path.join(EVEREST_SRC, 'missions', 'k2', 'download.pbs')
    str_w = 'walltime=%d:00:00' % walltime
    str_v = 'EVEREST_DAT=%s,CAMPAIGN=%d,SUBCAMPAIGN=%d' % (
        EVEREST_DAT, campaign, subcampaign)
    if subcampaign == -1:
        str_name = 'download_c%02d' % campaign
    else:
        str_name = 'download_c%02d.%d' % (campaign, subcampaign)
    str_out = os.path.join(EVEREST_DAT, 'k2', str_name + '.log')
    qsub_args = ['qsub', pbsfile,
                 '-q', queue,
                 '-v', str_v,
                 '-o', str_out,
                 '-j', 'oe',
                 '-N', str_name,
                 '-l', str_w]
    if email is not None:
        qsub_args.append(['-M', email, '-m', 'ae'])
    # Now we submit the job
    print("Submitting the job...")
    subprocess.call(qsub_args)


Instruction:
Can you generate the documentation for the following Python 3 function
def _Download(campaign, subcampaign):
    '''
    Download all stars from a given campaign. This is
    called from ``missions/k2/download.pbs``

    '''

    # Are we doing a subcampaign?
    if subcampaign != -1:
        campaign = campaign + 0.1 * subcampaign
    # Get all star IDs for this campaign
    stars = [s[0] for s in GetK2Campaign(campaign)]
    nstars = len(stars)
    # Download the TPF data for each one
    for i, EPIC in enumerate(stars):
        print("Downloading data for EPIC %d (%d/%d)..." %
              (EPIC, i + 1, nstars))
        if not os.path.exists(os.path.join(EVEREST_DAT, 'k2',
                                           'c%02d' % int(campaign),
                                           ('%09d' % EPIC)[:4] + '00000',
                                           ('%09d' % EPIC)[4:],
                                           'data.npz')):
            try:
                GetData(EPIC, season=campaign, download_only=True)
            except KeyboardInterrupt:
                sys.exit()
            except:
                # Some targets could be corrupted...
                print("ERROR downloading EPIC %d." % EPIC)
                exctype, value, tb = sys.exc_info()
                for line in traceback.format_exception_only(exctype, value):
                    ln = line.replace('\n', '')
                    print(ln)
                continue

Response: Download all stars from a given campaign. This is the main function for the main function.