Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_finished_results(self):
        task_and_results = []
        for pending_result in self.pending_results:
            if pending_result.ready():
                ret = pending_result.get()
                task_id, result = ret
                task = self.task_id_to_task[task_id]
                # process any pending messages for this task (will also process other tasks messages)
                self.process_all_messages_in_queue()
                task.after_run(result)
                task_and_results.append((task, result))
                self.pending_results.remove(pending_result)
        return task_and_results

Response: Get the results for all tasks that have finished.


Instruction:
Explain what the following Python 3 code does
def to_xml(body_element):
    doctype = b"""<?xml version="1.0" encoding="UTF-8"?>\n"""
    if body_element is None:
        return succeed(b"")
    d = flattenString(None, body_element)
    d.addCallback(lambda flattened: doctype + flattened)
    return d

Response:
Serialize a L{twisted.web.template.Tag} to a UTF-8 encoded XML document
    with an XML doctype header.


Instruction:
Write a Python 3 script for
getting a Route53 client.

Response:
def get_route53_client(agent, region, cooperator=None):
    """
    Get a non-registration Route53 client.
    """
    if cooperator is None:
        cooperator = task
    return region.get_client(
        _Route53Client,
        agent=agent,
        creds=region.creds,
        region=REGION_US_EAST_1,
        endpoint=AWSServiceEndpoint(_OTHER_ENDPOINT),
        cooperator=cooperator,
    )


Instruction:
How would you implement a function in Python 3 that
constructs an L { _Operation representing a Route53 service API call.

Response:
def _route53_op(body=None, **kw):
    """
    Construct an L{_Operation} representing a I{Route53} service API call.
    """
    op = _Operation(service=b"route53", **kw)
    if body is None:
        return succeed(op)
    d = to_xml(body)
    d.addCallback(lambda body: attr.assoc(op, body=body))
    return d


Instruction:
Explain what the following Python 3 code does
def hostedzone_from_element(zone):
    return HostedZone(
        name=maybe_bytes_to_unicode(zone.find("Name").text).encode("ascii").decode("idna"),
        identifier=maybe_bytes_to_unicode(zone.find("Id").text).replace(u"/hostedzone/", u""),
        rrset_count=int(zone.find("ResourceRecordSetCount").text),
        reference=maybe_bytes_to_unicode(zone.find("CallerReference").text),
    )

Response: Construct a HostedZone instance from an I { HostedZone XML element.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def to_element(change):
    return tags.Change(
        tags.Action(
            change.action,
        ),
        tags.ResourceRecordSet(
            tags.Name(
                unicode(change.rrset.label),
            ),
            tags.Type(
                change.rrset.type,
            ),
            tags.TTL(
                u"{}".format(change.rrset.ttl),
            ),
            tags.ResourceRecords(list(
                tags.ResourceRecord(tags.Value(rr.to_text()))
                for rr
                in sorted(change.rrset.records)
            ))
        ),
    )

Response:
Converts an IRRSetChange to an Element which describes this
           .


Instruction:
Create a Python 3 function for
adding a method class to the regitry.

Response:
def add(self, method_class, action, version=None):
        """Add a method class to the regitry.

        @param method_class: The method class to add
        @param action: The action that the method class can handle
        @param version: The version that the method class can handle
        """
        by_version = self._by_action.setdefault(action, {})
        if version in by_version:
            raise RuntimeError("A method was already registered for action"
                               " %s in version %s" % (action, version))
        by_version[version] = method_class


Instruction:
Explain what the following Python 3 code does
def check(self, action, version=None):
        if action not in self._by_action:
            raise APIError(400, "InvalidAction", "The action %s is not valid "
                           "for this web service." % action)
        by_version = self._by_action[action]
        if None not in by_version:
            # There's no catch-all method, let's try the version-specific one
            if version not in by_version:
                raise APIError(400, "InvalidVersion", "Invalid API version.")

Response: Check if the given action is supported in the given version.


Instruction:
Can you create a Python 3 function that
gets the method class handing the given action and version.

Response:
def get(self, action, version=None):
        """Get the method class handing the given action and version."""
        by_version = self._by_action[action]
        if version in by_version:
            return by_version[version]
        else:
            return by_version[None]


Instruction:
Make a summary of the following Python 3 code
def scan(self, module, onerror=None, ignore=None):
        from venusian import Scanner
        scanner = Scanner(registry=self)
        kwargs = {"onerror": onerror, "categories": ["method"]}
        if ignore is not None:
            # Only pass it if specified, for backward compatibility
            kwargs["ignore"] = ignore
        scanner.scan(module, **kwargs)

Response: Scan the given module object for L { Method s and register them.


Instruction:
How would you explain what the following Python 3 function does
def calculate_parameters(self, independentTs, dependentTs):
        listX, listY = self.match_time_series(independentTs, dependentTs)

        if len(listX) == 0 or len(listY) == 0:
            raise ValueError("Lists need to have some equal dates or cannot be empty")
        if len(listX) != len(listY):
            raise ValueError("Each Timeseries need to have distinct dates")

        xValues = map(lambda item: item[1], listX)
        yValues = map(lambda item: item[1], listY)

        xMean = FusionMethods["mean"](xValues)
        yMean = FusionMethods["mean"](yValues)

        xDeviation = map(lambda item: (item - xMean), xValues)
        yDeviation = map(lambda item: (item - yMean), yValues)

        try:
            parameter1 = sum(x * y for x, y in zip(xDeviation, yDeviation)) / sum(x * x for x in xDeviation)
        except ZeroDivisionError:
            # error occures if xDeviation is always 0, which means that all x values are the same
            raise ValueError("Not enough distinct x values")
        parameter0 = yMean - (parameter1 * xMean)

        return (parameter0, parameter1)

Response: Calculate and return the parameters for the regression line.


Instruction:
Make a summary of the following Python 3 code
def predict(self, timeseriesX, n, m):
        new_entries = []
        for entry in timeseriesX:
            predicted_value = m * entry[1] + n
            new_entries.append([entry[0], predicted_value])
        return TimeSeries.from_twodim_list(new_entries)

Response: Calculates the dependent timeseries Y for the given parameters and independent timeseries X.


Instruction:
Can you generate the documentation for the following Python 3 function
def match_time_series(self, timeseries1, timeseries2):
        time1 = map(lambda item: item[0], timeseries1.to_twodim_list())
        time2 = map(lambda item: item[0], timeseries2.to_twodim_list())

        matches = filter(lambda x: (x in time1), time2)
        listX  = filter(lambda x: (x[0] in matches), timeseries1.to_twodim_list())
        listY  = filter(lambda x: (x[0] in matches), timeseries2.to_twodim_list())

        return listX, listY

Response: Return two lists containing the matched values and the dates of the two input time series.


Instruction:
Implement a function in Python 3 to
return the least - squares solution to a linear matrix equation.

Response:
def lstsq(cls, a, b):
        """Return the least-squares solution to a linear matrix equation.

        :param Matrix a:    Design matrix with the values of the independent variables.
        :param Matrix b:    Matrix with the "dependent variable" values.
                            b can only have one column.

        :raise:             Raises an :py:exc:`ValueError`, if
                            - the number of rows of a and b does not match.
                            - b has more than one column.
        :note:              The algorithm solves the following equations.
                            beta = a^+ b.
        """
        # Check if the size of the input matrices matches
        if a.get_height() != b.get_height():
            raise ValueError("Size of input matrices does not match")
        if b.get_width() != 1:
            raise ValueError("Matrix with dependent variable has more than 1 column")
        aPseudo = a.pseudoinverse()
        # The following code could be used if c is regular.
        # aTrans  = a.transform()
        # c       = aTrans * a
        # invers() raises an ValueError, if c is not invertible
        # cInvers = c.invers()
        # beta = cInvers * aTrans * b
        beta = aPseudo * b
        return beta


Instruction:
How would you explain what the following Python 3 function does
def _calculate(self, startingPercentage, endPercentage, startDate, endDate):
        # get the defined subset of error values
        errorValues = self._get_error_values(startingPercentage, endPercentage, startDate, endDate)
        errorValues = filter(lambda item: item is not None, errorValues)

        return float(sum(errorValues)) / float(len(errorValues))

Response: This method calculates the error value for the current language.


Instruction:
Create a Python 3 function to
calculate the error between two values.

Response:
def local_error(self, originalValue, calculatedValue):
        """Calculates the error between the two given values.

        :param list originalValue:    List containing the values of the original data.
        :param list calculatedValue:    List containing the values of the calculated TimeSeries that
            corresponds to originalValue.

        :return:    Returns the error measure of the two given values.
        :rtype:     numeric
        """
        originalValue = originalValue[0]
        calculatedValue = calculatedValue[0]

        if 0 == originalValue:
            return None

        return (math.fabs((calculatedValue - originalValue)/float(originalValue))) * 100.0


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_historic_means(self, timeSeries):
        # calculate the history values
        historyLength = self._historyLength
        historicMeans = []
        append        = historicMeans.append

        # not most optimized loop in case of calculation operations
        for startIdx in xrange(len(timeSeries) - historyLength - 1):
            value = 0
            for idx in xrange(startIdx, startIdx + historyLength):
                value += abs(timeSeries[idx+1][1] - timeSeries[idx][1])

            append(value / float(historyLength))

        return historicMeans

Response: Calculates the mean value for the history of the MeanAbsoluteScaledError.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def initialize(self, originalTimeSeries, calculatedTimeSeries):
        # ErrorMeasure was already initialized.
        if 0 < len(self._errorValues):
            raise StandardError("An ErrorMeasure can only be initialized once.")

        # calculating the number of datapoints used within the history
        if isinstance(self._historyLength, float):
            self._historyLength = int((self._historyLength * len(originalTimeSeries)) / 100.0)

        # sort the TimeSeries to reduce the required comparison operations
        originalTimeSeries.sort_timeseries()
        calculatedTimeSeries.sort_timeseries()

        self._historicMeans = self._get_historic_means(originalTimeSeries)

        # Performance optimization
        append      = self._errorValues.append
        appendDates = self._errorDates.append
        local_error = self.local_error
        minCalcIdx  = self._historyLength + 1

        # calculate all valid local errors
        for orgPair in originalTimeSeries[minCalcIdx:]:
            for calcIdx in xrange(minCalcIdx, len(calculatedTimeSeries)):
                calcPair = calculatedTimeSeries[calcIdx]

                # Skip values that can not be compared
                if calcPair[0] != orgPair[0]:
                    continue

                append(local_error(orgPair[1:], calcPair[1:]))
                appendDates(orgPair[0])

        # return False, if the error cannot be calculated
        if len(filter(lambda item: item is not None, self._errorValues)) < self._minimalErrorCalculationPercentage * len(originalTimeSeries):
            self._errorValues = []
            self._errorDates = []
            self._historicMeans = []
            return False

        return True

Response:
Initializes the ErrorMeasure.

        During initialization, all :py:meth:`BaseErrorMeasure.local_error()` are calculated.

        :param TimeSeries originalTimeSeries:    TimeSeries containing the original data.
        :param TimeSeries calculatedTimeSeries:    TimeSeries containing calculated data.
            Calculated data is smoothed or forecasted data.

        :return:    Return :py:const:`True` if the error could be calculated, :py:const:`False`
            otherwise based on the minimalErrorCalculationPercentage.
        :rtype: boolean

        :raise:    Raises a :py:exc:`StandardError` if the error measure is initialized multiple times.


Instruction:
Can you tell what is the following Python 3 function doing
def _calculate(self, startingPercentage, endPercentage, startDate, endDate):
        # get the defined subset of error values
        errorValues = self._get_error_values(startingPercentage, endPercentage, startDate, endDate)

        # get the historic mean
        if startDate is not None:
            possibleDates = filter(lambda date: date >= startDate, self._errorDates)

            # This piece of code is not required, because _get_error_values already ensured that the startDate
            # was correct. Otherwise it would have thrown an exception.
            #if 0 == len(possibleDates):
            #    raise ValueError("%s does not represent a valid startDate." % startDate)

            meanIdx = self._errorDates.index(min(possibleDates))
        else:
            meanIdx = int((startingPercentage * len(self._errorValues)) / 100.0)

        mad = sum(errorValues) / float(len(errorValues))
        historicMean = self._historicMeans[meanIdx]

        return mad / historicMean

Response: This method calculates the error value for a given date range.


Instruction:
Can you generate the documentation for the following Python 3 function
def __train(self, n_clusters=4):

        try:
            for obs in self.observations:
                features, ids = self.__get_features_for_observation(observation=obs, last_column_is_id=True)
                # the last column is the observation id
                normalised_data = whiten(features)

                # skip any rows that contain just zero values... they create nans
                first_safe_row = pdkit.utils.non_zero_index(normalised_data)
                observation_ids = features.tolist()
                sd = features[first_safe_row] / normalised_data[first_safe_row]

                # Calculate centroids and sort result
                centroids_array, _ = kmeans(normalised_data, n_clusters)
                sorted_centroids = pdkit.utils.centroid_sort(centroids_array)

                if not self.clusters:
                    self.clusters = [[obs, sd.tolist(), sorted_centroids.tolist()]]
                else:
                    self.clusters.append([obs, sd.tolist(),sorted_centroids.tolist()])
        except IOError as e:
            ierr = "({}): {}".format(e.errno, e.strerror)
            logging.error("Error training UPDRS, file not found, I/O error %s", ierr)

        except ValueError as verr:
            logging.error("Error training UPDRS ValueError ->%s", verr.message)

        except:
            logging.error("Unexpected error on training UPDRS init: %s", sys.exc_info()[0])

Response: This method is used to train the UPDRS cluster.


Instruction:
Explain what the following Python 3 code does
def get_single_score(self, point, centroids=None, sd=None):
        normalised_point = array(point) / array(sd)

        observation_score = {
            'original': point,
            'normalised': normalised_point.tolist(),
        }

        distances = [
            euclidean(normalised_point, centroid)
            for centroid in centroids
        ]

        return int(distances.index(min(distances)))

Response: This method returns a score for a given point and a list of current centroids.


Instruction:
Given the following Python 3 function, write the documentation
def score(self, measurement, output_format='array'):
        scores = np.array([])
        for obs in self.observations:
            c, sd = self.__get_centroids_sd(obs)
            p, ids = self.__get_features_for_observation(data_frame = measurement, observation=obs,
                                                         last_column_is_id=True)

            scores = np.append(scores, [self.get_single_score(p, centroids=c, sd=sd)], axis=0)

        if output_format == 'array':
            return scores.astype(int)
        else:
            return np.array_str(scores.astype(int))

Response: Method to score a given measurement against the trained KNN clusters.


Instruction:
Create a Python 3 function for
functioning run by CreateProjectCommand to create the project.

Response:
def download_file_part_run(download_context):
    """
    Function run by CreateProjectCommand to create the project.
    Runs in a background process.
    :param download_context: UploadContext: contains data service setup and project name to create.
    """
    destination_dir, file_url_data_dict, seek_amt, bytes_to_read = download_context.params
    project_file = ProjectFile(file_url_data_dict)
    local_path = project_file.get_local_path(destination_dir)
    retry_chunk_downloader = RetryChunkDownloader(project_file, local_path,
                                                  seek_amt, bytes_to_read,
                                                  download_context)
    retry_chunk_downloader.run()
    return 'ok'


Instruction:
Implement a Python 3 function for
downloading the contents of the specified project name or id to dest_directory.

Response:
def run(self):
        """
        Download the contents of the specified project name or id to dest_directory.
        """
        files_to_download = self.get_files_to_download()
        total_files_size = self.get_total_files_size(files_to_download)
        if self.file_download_pre_processor:
            self.run_preprocessor(files_to_download)

        self.try_create_dir(self.dest_directory)
        watcher = ProgressPrinter(total_files_size, msg_verb='downloading')
        self.download_files(files_to_download, watcher)
        watcher.finished()

        warnings = self.check_warnings()
        if warnings:
            watcher.show_warning(warnings)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def run_preprocessor(self, files_to_download):
        for project_file in files_to_download:
            self.file_download_pre_processor.run(self.remote_store.data_service, project_file)

Response: Run file_download_pre_processor for each file we are about to download.


Instruction:
Explain what the following Python 3 code does
def try_create_dir(self, path):
        if not os.path.exists(path):
            os.mkdir(path)
        elif not os.path.isdir(path):
            ValueError("Unable to create directory:" + path + " because a file already exists with the same name.")

Response: Try to create a directory if it doesn t exist and raise error if there is a non - directory with the same name.


Instruction:
Create a Python 3 function for
getting list of remote folders based on the list of all file urls

Response:
def _get_parent_remote_paths(self):
        """
        Get list of remote folders based on the list of all file urls
        :return: set([str]): set of remote folders (that contain files)
        """
        parent_paths = set([item.get_remote_parent_path() for item in self.file_urls])
        if '' in parent_paths:
            parent_paths.remove('')
        return parent_paths


Instruction:
Can you generate the documentation for the following Python 3 function
def make_local_directories(self):
        for remote_path in self._get_parent_remote_paths():
            local_path = os.path.join(self.dest_directory, remote_path)
            self._assure_dir_exists(local_path)

Response: Create directories necessary to download the files into dest_directory


Instruction:
Explain what the following Python 3 code does
def make_big_empty_files(self):
        for file_url in self.file_urls:
            local_path = file_url.get_local_path(self.dest_directory)
            with open(local_path, "wb") as outfile:
                if file_url.size > 0:
                    outfile.seek(int(file_url.size) - 1)
                    outfile.write(b'\0')

Response: Write out a empty file so the workers can seek to where they should write their data.


Instruction:
Implement a function in Python 3 to
divide file_url size into an array of start and end tuples.

Response:
def make_ranges(self, file_url):
        """
        Divides file_url size into an array of ranges to be downloaded by workers.
        :param: file_url: ProjectFileUrl: file url to download
        :return: [(int,int)]: array of (start, end) tuples
        """
        size = file_url.size
        bytes_per_chunk = self.determine_bytes_per_chunk(size)
        start = 0
        ranges = []
        while size > 0:
            amount = bytes_per_chunk
            if amount > size:
                amount = size
            ranges.append((start, start + amount - 1))
            start += amount
            size -= amount
        return ranges


Instruction:
Can you generate the documentation for the following Python 3 function
def determine_bytes_per_chunk(self, size):
        workers = self.settings.config.download_workers
        if not workers or workers == 'None':
            workers = 1
        bytes_per_chunk = int(math.ceil(size / float(workers)))
        if bytes_per_chunk < self.bytes_per_chunk:
            bytes_per_chunk = self.bytes_per_chunk
        return bytes_per_chunk

Response: Determines the size of a worker s memory chunk.


Instruction:
Write a Python 3 script to
return tuple that contains a list large files and a list of small files based on size parameter :param size: int: size (in bytes) that determines if a file is large or small :return: ([ProjectFileUrl],[ProjectFileUrl]): (large file urls, small file urls)

Response:
def split_file_urls_by_size(self, size):
        """
        Return tuple that contains a list large files and a list of small files based on size parameter
        :param size: int: size (in bytes) that determines if a file is large or small
        :return: ([ProjectFileUrl],[ProjectFileUrl]): (large file urls, small file urls)
        """
        large_items = []
        small_items = []
        for file_url in self.file_urls:
            if file_url.size >= size:
                large_items.append(file_url)
            else:
                small_items.append(file_url)
        return large_items, small_items


Instruction:
Can you generate a brief explanation for the following Python 3 code
def check_downloaded_files_sizes(self):
        for file_url in self.file_urls:
            local_path = file_url.get_local_path(self.dest_directory)
            self.check_file_size(file_url.size, local_path)

Response: Check that the files sizes are correct.


Instruction:
Here you have a function in Python 3, explain what it does
def check_file_size(file_size, path):
        stat_info = os.stat(path)
        if stat_info.st_size != file_size:
            format_str = "Error occurred downloading {}. Got a file size {}. Expected file size:{}"
            msg = format_str.format(path, stat_info.st_size, file_size)
            raise ValueError(msg)

Response: Raise an error if we didn t get all of the file.


Instruction:
How would you implement a function in Python 3 that
creates data needed by upload_project_run

Response:
def create_context(self, message_queue, task_id):
        """
        Create data needed by upload_project_run(DukeDS connection info).
        :param message_queue: Queue: queue background process can send messages to us on
        :param task_id: int: id of this command's task so message will be routed correctly
        """
        params = (self.settings.dest_directory, self.file_url.json_data, self.seek_amt, self.bytes_to_read)
        return DownloadContext(self.settings, params, message_queue, task_id)


Instruction:
Write a Python 3 function that can
return the url and headers to use for downloading part of a file.

Response:
def get_url_and_headers_for_range(self, file_download):
        """
        Return url and headers to use for downloading part of a file, adding range headers.
        :param file_download: FileDownload: contains data about file we will download
        :return: str, dict: url to download and headers to use
        """
        headers = self.get_range_headers()
        if file_download.http_headers:
            headers.update(file_download.http_headers)
        separator = ""
        if not file_download.url.startswith("/"):
            separator = "/"
        url = '{}{}{}'.format(file_download.host, separator, file_download.url)
        return url, headers


Instruction:
How would you explain what the following Python 3 function does
def download_chunk(self, url, headers):
        response = requests.get(url, headers=headers, stream=True)
        if response.status_code == SWIFT_EXPIRED_STATUS_CODE \
                or response.status_code == S3_EXPIRED_STATUS_CODE:
            raise DownloadInconsistentError(response.text)
        response.raise_for_status()
        self.actual_bytes_read = 0
        self._write_response_to_file(response)
        self._verify_download_complete()

Response: Download a chunk of a file and write it to our file.


Instruction:
Make a summary of the following Python 3 code
def _write_response_to_file(self, response):
        with open(self.local_path, 'r+b') as outfile:  # open file for read/write (no truncate)
            outfile.seek(self.seek_amt)
            for chunk in response.iter_content(chunk_size=self.bytes_per_chunk):
                if chunk:  # filter out keep-alive chunks
                    outfile.write(chunk)
                    self._on_bytes_read(len(chunk))

Response: Write the response to the appropriate section of the file at self. local_path.


Instruction:
Can you tell what is the following Python 3 function doing
def _on_bytes_read(self, num_bytes_read):
        self.actual_bytes_read += num_bytes_read
        if self.actual_bytes_read > self.bytes_to_read:
            raise TooLargeChunkDownloadError(self.actual_bytes_read, self.bytes_to_read, self.local_path)
        self.download_context.send_processed_message(num_bytes_read)

Response: Called when the bytes read of the data is received.


Instruction:
Can you generate the documentation for the following Python 3 function
def _verify_download_complete(self):
        if self.actual_bytes_read > self.bytes_to_read:
            raise TooLargeChunkDownloadError(self.actual_bytes_read, self.bytes_to_read, self.local_path)
        elif self.actual_bytes_read < self.bytes_to_read:
            raise PartialChunkDownloadError(self.actual_bytes_read, self.bytes_to_read, self.local_path)

Response: Verify that the download is complete.


Instruction:
Given the following Python 3 function, write the documentation
def optimize(self, timeSeries, forecastingMethods=None, startingPercentage=0.0, endPercentage=100.0):

        if forecastingMethods is None or len(forecastingMethods) == 0:
            raise ValueError("forecastingMethods cannot be empty.")

        self._startingPercentage = startingPercentage
        self._endPercentage      = endPercentage

        results = []
        for forecastingMethod in forecastingMethods:
            results.append([forecastingMethod] + self.optimize_forecasting_method(timeSeries, forecastingMethod))

        # get the forecasting method with the smallest error
        bestForecastingMethod = min(results, key=lambda item: item[1].get_error(self._startingPercentage, self._endPercentage))

        for parameter in bestForecastingMethod[2]:
            bestForecastingMethod[0].set_parameter(parameter, bestForecastingMethod[2][parameter])

        return bestForecastingMethod

Response: Runs the optimization of the given TimeSeries.


Instruction:
Implement a Python 3 function for
optimizing the parameters for the given timeSeries and forecastingMethod. :param TimeSeries timeSeries: TimeSeries instance, containing hte original data. :param BaseForecastingMethod forecastingMethod: ForecastingMethod that is used to optimize the parameters. :return: Returns a tuple containing only the smallest BaseErrorMeasure instance as defined in :py:meth:`BaseOptimizationMethod.__init__` and the forecastingMethods parameter. :rtype: tuple

Response:
def optimize_forecasting_method(self, timeSeries, forecastingMethod):
        """Optimizes the parameters for the given timeSeries and forecastingMethod.

        :param TimeSeries timeSeries:    TimeSeries instance, containing hte original data.
        :param BaseForecastingMethod forecastingMethod:    ForecastingMethod that is used to optimize the parameters.

        :return: Returns a tuple containing only the smallest BaseErrorMeasure instance as defined in
            :py:meth:`BaseOptimizationMethod.__init__` and the forecastingMethods parameter.
        :rtype: tuple
        """
        tuneableParameters = forecastingMethod.get_optimizable_parameters()

        remainingParameters = []
        for tuneableParameter in tuneableParameters:
            remainingParameters.append([tuneableParameter, [item for item in self._generate_next_parameter_value(tuneableParameter, forecastingMethod)]])

        # Collect the forecasting results
        forecastingResults = self.optimization_loop(timeSeries, forecastingMethod, remainingParameters)

        # Debugging GridSearchTest.inner_optimization_result_test
        #print ""
        #print "GridSearch"
        #print "Instance    /    SMAPE / Alpha"
        #for item in forecastingResults:
        #    print "%s / %s / %s" % (
        #    str(item[0])[-12:-1],
        #    str(item[0].get_error(self._startingPercentage, self._endPercentage))[:8],
        #    item[1]["smoothingFactor"]
        #)
        #print ""

        # Collect the parameters that resulted in the smallest error
        bestForecastingResult = min(forecastingResults, key=lambda item: item[0].get_error(self._startingPercentage, self._endPercentage))

        # return the determined parameters
        return bestForecastingResult


Instruction:
Here you have a function in Python 3, explain what it does
def energy_data():
    cur = db.cursor().execute("""SELECT timestamp, current FROM Readings""")
    original = TimeSeries()
    original.initialize_from_sql_cursor(cur)
    original.normalize("day", fusionMethod = "sum")
    return itty.Response(json.dumps(original, cls=PycastEncoder), content_type='application/json')

Response: Get the energy data for the current readings for the device 8.


Instruction:
Write a Python 3 script for
performing Holt Winters Parameter Optimization on the given data.

Response:
def optimize(request):
    """
    Performs Holt Winters Parameter Optimization on the given post data.
    Expects the following values set in the post of the request:
        seasonLength - integer
        valuesToForecast - integer
        data - two dimensional array of [timestamp, value]
    """
    #Parse arguments
    seasonLength = int(request.POST.get('seasonLength', 6))
    valuesToForecast = int(request.POST.get('valuesToForecast', 0))
    data = json.loads(request.POST.get('data', []))

    original = TimeSeries.from_twodim_list(data)
    original.normalize("day") #due to bug in TimeSeries.apply
    original.set_timeformat("%d.%m")

    #optimize smoothing
    hwm = HoltWintersMethod(seasonLength = seasonLength, valuesToForecast = valuesToForecast)
    gridSearch = GridSearch(SMAPE)
    optimal_forecasting, error, optimal_params = gridSearch.optimize(original, [hwm])

    #perform smoothing
    smoothed = optimal_forecasting.execute(original)
    smoothed.set_timeformat("%d.%m")
    result = {  'params': optimal_params,
                'original': original,
                'smoothed': smoothed,
                'error': round(error.get_error(), 3)
                }
    return itty.Response(json.dumps(result, cls=PycastEncoder), content_type='application/json')


Instruction:
Write a Python 3 function for
performing Holt Winters Smoothing on the given post data. Expects the following values set in the post of the request: smoothingFactor - float trendSmoothingFactor - float seasonSmoothingFactor - float seasonLength - integer valuesToForecast - integer data - two dimensional array of [timestamp, value]

Response:
def holtWinters(request):
    """
    Performs Holt Winters Smoothing on the given post data.
    Expects the following values set in the post of the request:
        smoothingFactor - float
        trendSmoothingFactor - float
        seasonSmoothingFactor - float
        seasonLength - integer
        valuesToForecast - integer
        data - two dimensional array of [timestamp, value]
    """
    #Parse arguments
    smoothingFactor = float(request.POST.get('smoothingFactor', 0.2))
    trendSmoothingFactor = float(request.POST.get('trendSmoothingFactor', 0.3))
    seasonSmoothingFactor = float(request.POST.get('seasonSmoothingFactor', 0.4))
    seasonLength = int(request.POST.get('seasonLength', 6))
    valuesToForecast = int(request.POST.get('valuesToForecast', 0))
    data = json.loads(request.POST.get('data', []))

    #perform smoothing
    hwm = HoltWintersMethod(smoothingFactor = smoothingFactor,
                            trendSmoothingFactor = trendSmoothingFactor,
                            seasonSmoothingFactor =  seasonSmoothingFactor,
                            seasonLength = seasonLength,
                            valuesToForecast = valuesToForecast)
    original = TimeSeries.from_twodim_list(data)
    original.set_timeformat("%d.%m")
    smoothed = hwm.execute(original)
    smoothed.set_timeformat("%d.%m")

    error = SMAPE()
    error.initialize(original, smoothed)

    #process the result
    result = {  'original': original,
                'smoothed': smoothed,
                'error': round(error.get_error(), 3)
            }
    return itty.Response(json.dumps(result, cls=PycastEncoder), content_type='application/json')


Instruction:
How would you explain what the following Python 3 function does
def parse(self, schema, strict=True):
        self.args, self.rest = schema.extract(self._raw_params)
        if strict and self.rest:
            raise APIError(400, "UnknownParameter",
                           "The parameter %s is not "
                           "recognized" % self.rest.keys()[0])

Response: Update the internal state of the object with the parsed parameters.


Instruction:
How would you explain what the following Python 3 function does
def _name_to_child_map(children):
    name_to_child = {}
    for child in children:
        name_to_child[child.name] = child
    return name_to_child

Response: Create a map of name to child based on a list of children.


Instruction:
Write a Python 3 script for
updating remote_ids based on the names of children.

Response:
def _update_remote_children(remote_parent, children):
    """
    Update remote_ids based on on parent matching up the names of children.
    :param remote_parent: RemoteProject/RemoteFolder who has children
    :param children: [LocalFolder,LocalFile] children to set remote_ids based on remote children
    """
    name_to_child = _name_to_child_map(children)
    for remote_child in remote_parent.children:
        local_child = name_to_child.get(remote_child.name)
        if local_child:
            local_child.update_remote_ids(remote_child)


Instruction:
How would you code a function in Python 3 to
build a tree of LocalFile or LocalFolder based on a path.

Response:
def _build_project_tree(path, followsymlinks, file_filter):
    """
    Build a tree of LocalFolder with children or just a LocalFile based on a path.
    :param path: str path to a directory to walk
    :param followsymlinks: bool should we follow symlinks when walking
    :param file_filter: FileFilter: include method returns True if we should include a file/folder
    :return: the top node of the tree LocalFile or LocalFolder
    """
    result = None
    if os.path.isfile(path):
        result = LocalFile(path)
    else:
        result = _build_folder_tree(os.path.abspath(path), followsymlinks, file_filter)
    return result


Instruction:
Can you implement a function in Python 3 that
builds a tree of LocalFolder objects based on a path.

Response:
def _build_folder_tree(top_abspath, followsymlinks, file_filter):
    """
    Build a tree of LocalFolder with children based on a path.
    :param top_abspath: str path to a directory to walk
    :param followsymlinks: bool should we follow symlinks when walking
    :param file_filter: FileFilter: include method returns True if we should include a file/folder
    :return: the top node of the tree LocalFolder
    """
    path_to_content = {}
    child_to_parent = {}
    ignore_file_patterns = IgnoreFilePatterns(file_filter)
    ignore_file_patterns.load_directory(top_abspath, followsymlinks)
    for dir_name, child_dirs, child_files in os.walk(top_abspath, followlinks=followsymlinks):
        abspath = os.path.abspath(dir_name)
        folder = LocalFolder(abspath)
        path_to_content[abspath] = folder
        # If we have a parent add us to it.
        parent_path = child_to_parent.get(abspath)
        if parent_path:
            path_to_content[parent_path].add_child(folder)
        remove_child_dirs = []
        for child_dir in child_dirs:
            # Record dir_name as the parent of child_dir so we can call add_child when get to it.
            abs_child_path = os.path.abspath(os.path.join(dir_name, child_dir))
            if ignore_file_patterns.include(abs_child_path, is_file=False):
                child_to_parent[abs_child_path] = abspath
            else:
                remove_child_dirs.append(child_dir)
        for remove_child_dir in remove_child_dirs:
            child_dirs.remove(remove_child_dir)
        for child_filename in child_files:
            abs_child_filename = os.path.join(dir_name, child_filename)
            if ignore_file_patterns.include(abs_child_filename, is_file=True):
                folder.add_child(LocalFile(abs_child_filename))
    return path_to_content.get(top_abspath)


Instruction:
How would you implement a function in Python 3 that
adds the path and any children files and folders to the list of content.

Response:
def add_path(self, path):
        """
        Add the path and any children files/folders to the list of content.
        :param path: str path to add
        """
        abspath = os.path.abspath(path)
        self.children.append(_build_project_tree(abspath, self.followsymlinks, self.file_filter))


Instruction:
Explain what the following Python 3 code does
def update_remote_ids(self, remote_project):
        if remote_project:
            self.remote_id = remote_project.id
            _update_remote_children(remote_project, self.children)

Response: Compare against remote_project saving off the matching uuids of matching content.


Instruction:
Write a Python 3 function for
updating the remote id based on remote_folder and check children against this folder s children.

Response:
def update_remote_ids(self, remote_folder):
        """
        Set remote id based on remote_folder and check children against this folder's children.
        :param remote_folder: RemoteFolder to compare against
        """
        self.remote_id = remote_folder.id
        _update_remote_children(remote_folder, self.children)


Instruction:
Write a Python 3 function for
updating the remote_id and need_to_send flag based on the hash info.

Response:
def update_remote_ids(self, remote_file):
        """
        Based on a remote file try to assign a remote_id and compare hash info.
        :param remote_file: RemoteFile remote data pull remote_id from
        """
        self.remote_id = remote_file.id
        hash_data = self.path_data.get_hash()
        if hash_data.matches(remote_file.hash_alg, remote_file.file_hash):
            self.need_to_send = False


Instruction:
Can you generate the documentation for the following Python 3 function
def count_chunks(self, bytes_per_chunk):
        chunks = math.ceil(float(self.size) / float(bytes_per_chunk))
        return max(chunks, 1)

Response: Count how many chunks we need to upload into a specific file.


Instruction:
Explain what the following Python 3 code does
def matches(self, hash_alg, hash_value):
        return self.alg == hash_alg and self.value == hash_value

Response: Returns True if the specified hash algorithm and hash value match the specified arguments.


Instruction:
Write a Python 3 script to
guess the mimetype of a file or application / octet - stream

Response:
def mime_type(self):
        """
        Guess the mimetype of a file or 'application/octet-stream' if unable to guess.
        :return: str: mimetype
        """
        mime_type, encoding = mimetypes.guess_type(self.path)
        if not mime_type:
            mime_type = 'application/octet-stream'
        return mime_type


Instruction:
Can you create a Python 3 function that
reads the whole file into memory.

Response:
def read_whole_file(self):
        """
        Slurp the whole file into memory.
        Should only be used with relatively small files.
        :return: str: file contents
        """
        chunk = None
        with open(self.path, 'rb') as infile:
            chunk = infile.read()
        return chunk


Instruction:
Implement a Python 3 function for
adding a file to the hash.

Response:
def add_file(self, filename, block_size=4096):
        """
        Add an entire file to this hash.
        :param filename: str filename of the file to hash
        :param block_size: int size of chunks when reading the file
        """
        with open(filename, "rb") as f:
            for chunk in iter(lambda: f.read(block_size), b""):
                self.hash.update(chunk)


Instruction:
Write a Python 3 function that can
inherit info from parent and return new object

Response:
def get(cls, parent=None, id=None, data=None):
        """Inherit info from parent and return new object"""
        # TODO - allow fetching of parent based on child?

        if parent is not None:
            route = copy(parent.route)
        else:
            route = {}

        if id is not None and cls.ID_NAME is not None:
            route[cls.ID_NAME] = id

        obj = cls(key=parent.key, route=route, config=parent.config)

        if data:
            # This is used in "get all" queries
            obj.data = data
        else:
            obj.fetch()

        return obj


Instruction:
Here you have a function in Python 3, explain what it does
def get_all(cls, parent=None, **params):

        if parent is not None:
            route = copy(parent.route)
        else:
            route = {}
        if cls.ID_NAME is not None:
            # Empty string triggers "get all resources"
            route[cls.ID_NAME] = ""

        base_obj = cls(key=parent.key, route=route, config=parent.config)
        """Perform a read request against the resource"""

        start = datetime.now()
        r = requests.get(
            base_obj._url(), auth=(base_obj.key, ""), params=params)
        cls._delay_for_ratelimits(start)

        if r.status_code not in cls.TRUTHY_CODES:
            return base_obj._handle_request_exception(r)

        response = r.json()
        objects_data = response.get(base_obj.ENVELOPE or base_obj, [])

        return_objects = []
        for data in objects_data:
            # Note that this approach does not get meta data
            return_objects.append(
                cls.get(
                    parent=parent,
                    id=data.get(cls.ID_NAME, data.get("id")),
                    data=data))

        return return_objects

Response: Get all the objects in the cache.


Instruction:
Create a Python 3 function for
getting the URL for the resource

Response:
def _url(self):
        """Get the URL for the resource"""
        if self.ID_NAME not in self.route.keys() and "id" in self.data.keys():
            self.route[self.ID_NAME] = self.data["id"]
        return self.config.BASE + self.PATH.format(**self.route)


Instruction:
Here you have a function in Python 3, explain what it does
def _handle_request_exception(request):
        try:
            data = request.json()
        except:
            data = {}

        code = request.status_code
        if code == requests.codes.bad:
            raise BadRequestException(response=data)

        if code == requests.codes.unauthorized:
            raise UnauthorizedException(response=data)

        if code == requests.codes.not_found:
            raise NotFoundException(response=data)

        # Generic error fallback
        request.raise_for_status()

Response: Raise the proper exception based on the response


Instruction:
Here you have a function in Python 3, explain what it does
def fetch(self):
        start = datetime.now()
        r = requests.get(self._url(), auth=(self.key, ""))
        self._delay_for_ratelimits(start)

        if r.status_code not in self.TRUTHY_CODES:
            return self._handle_request_exception(r)

        response = r.json()
        if self.ENVELOPE:
            self.data = response.get(self.ENVELOPE, {})
        else:
            self.data = response

        # Move to separate function so it can be overrridden
        self._process_meta(response)

Response: Perform a read request against the resource


Instruction:
Explain what the following Python 3 code does
def _process_meta(self, response):
        for key in self.META_ENVELOPES:
            self.meta[key] = response.get(key)

Response: Process additional data sent in response


Instruction:
Given the following Python 3 function, write the documentation
def delete(self):

        start = datetime.now()
        r = requests.delete(self._url(), auth=(self.key, ""))
        self._delay_for_ratelimits(start)

        if r.status_code not in self.TRUTHY_CODES:
            return self._handle_request_exception(r)

Response: Delete the object from the server


Instruction:
How would you code a function in Python 3 to
change attributes of the item

Response:
def patch(self, **kwargs):
        """Change attributes of the item"""
        start = datetime.now()
        r = requests.patch(self._url(), auth=(self.key, ""), data=kwargs)
        self._delay_for_ratelimits(start)

        if r.status_code not in self.TRUTHY_CODES:
            return self._handle_request_exception(r)

        # Refetch for safety. We could modify based on response,
        # but I'm afraid of some edge cases and marshal functions.
        self.fetch()


Instruction:
Here you have a function in Python 3, explain what it does
def create(cls, parent=None, **kwargs):

        if parent is None:
            raise Exception("Parent class is required")

        route = copy(parent.route)
        if cls.ID_NAME is not None:
            route[cls.ID_NAME] = ""

        obj = cls(key=parent.key, route=route, config=parent.config)

        start = datetime.now()
        response = requests.post(obj._url(), auth=(obj.key, ""), data=kwargs)
        cls._delay_for_ratelimits(start)

        if response.status_code not in cls.TRUTHY_CODES:
            return cls._handle_request_exception(response)

        # No envelope on post requests
        data = response.json()
        obj.route[obj.ID_NAME] = data.get("id", data.get(obj.ID_NAME))
        obj.data = data

        return obj

Response: Create an object and return it


Instruction:
Create a Python 3 function for
delaying for ratelimits if request was shorter than max request time.

Response:
def _delay_for_ratelimits(cls, start):
        """If request was shorter than max request time, delay"""
        stop = datetime.now()
        duration_microseconds = (stop - start).microseconds
        if duration_microseconds < cls.REQUEST_TIME_MICROSECONDS:
            time.sleep((cls.REQUEST_TIME_MICROSECONDS - duration_microseconds)
                       / MICROSECONDS_PER_SECOND)


Instruction:
Explain what the following Python 3 code does
def load_data(filename, format_file='cloudupdrs'):

        logging.debug("{} data --> Loaded".format(format_file))

        data_frame = load_data(filename, format_file)
        data_frame.sampling_rate = get_sampling_rate_from_timestamp(data_frame)

        return data_frame

Response: This method loads the data from a file and returns a DataFrame with the data.


Instruction:
Can you create a Python 3 function that
runs the configured method and writes the HTTP response status and text to the output stream.

Response:
def run(self):
        """
        Run the configured method and write the HTTP response status and text
        to the output stream.
        """
        region = AWSServiceRegion(access_key=self.key, secret_key=self.secret,
                                  uri=self.endpoint)
        query = self.query_factory(action=self.action, creds=region.creds,
                                   endpoint=region.ec2_endpoint,
                                   other_params=self.parameters)

        def write_response(response):
            print >> self.output, "URL: %s" % query.client.url
            print >> self.output
            print >> self.output, "HTTP status code: %s" % query.client.status
            print >> self.output
            print >> self.output, response

        def write_error(failure):
            if failure.check(AWSError):
                message = failure.value.original
            else:
                message = failure.getErrorMessage()
                if message.startswith("Error Message: "):
                    message = message[len("Error Message: "):]

            print >> self.output, "URL: %s" % query.client.url
            print >> self.output
            if getattr(query.client, "status", None) is not None:
                print >> self.output, "HTTP status code: %s" % (
                    query.client.status,)
                print >> self.output
            print >> self.output, message

            if getattr(failure.value, "response", None) is not None:
                print >> self.output
                print >> self.output, failure.value.response

        deferred = query.submit()
        deferred.addCallback(write_response)
        deferred.addErrback(write_error)
        return deferred


Instruction:
Can you write a function in Python 3 where it
returns the canonical host as for the Host HTTP header specification.

Response:
def get_canonical_host(self):
        """
        Return the canonical host as for the Host HTTP header specification.
        """
        host = self.host.lower()
        if self.port is not None:
            host = "%s:%s" % (host, self.port)
        return host


Instruction:
Can you create a Python 3 function that
sets host and port from a canonical host string as for the Host HTTP header specification.

Response:
def set_canonical_host(self, canonical_host):
        """
        Set host and port from a canonical host string as for the Host HTTP
        header specification.
        """
        parts = canonical_host.lower().split(":")
        self.host = parts[0]
        if len(parts) > 1 and parts[1]:
            self.port = int(parts[1])
        else:
            self.port = None


Instruction:
Create a Python 3 function for
getting a URL representation of the service.

Response:
def get_uri(self):
        """Get a URL representation of the service."""
        uri = "%s://%s%s" % (self.scheme, self.get_canonical_host(), self.path)
        return uri


Instruction:
Write a Python 3 script to
run the optimization on the given TimeSeries.

Response:
def optimize(self, timeSeries, forecastingMethods=None, startingPercentage=0.0, endPercentage=100.0):
        """Runs the optimization on the given TimeSeries.

        :param TimeSeries timeSeries:    TimeSeries instance that requires an optimized forecast.
        :param list forecastingMethods:    List of forecastingMethods that will be used for optimization.
        :param float startingPercentage: Defines the start of the interval. This has to be a value in [0.0, 100.0].
            It represents the value, where the error calculation should be started.
            25.0 for example means that the first 25% of all calculated errors will be ignored.
        :param float endPercentage:    Defines the end of the interval. This has to be a value in [0.0, 100.0].
            It represents the value, after which all error values will be ignored. 90.0 for example means that
            the last 10% of all local errors will be ignored.

        :return:    Returns the optimized forecasting method with the smallest error.
        :rtype:     (BaseForecastingMethod, Dictionary)

        :raise:    Raises a :py:exc:`ValueError` ValueError if no forecastingMethods is empty.
        """
        # no forecasting methods provided
        if forecastingMethods is None or len(forecastingMethods) == 0:
            raise ValueError("forecastingMethods cannot be empty.")


Instruction:
Explain what the following Python 3 code does
def include_path(self, path):
        self.seen_paths.add(path)
        return self.filter.include(path)

Response: Returns True if we should include the path


Instruction:
How would you code a function in Python 3 to
return which include_paths or exclude_paths that were not used via include_path method.

Response:
def get_unused_paths(self):
        """
        Returns which include_paths or exclude_paths that were not used via include_path method.
        :return: [str] list of filtering paths that were not used.
        """
        return [path for path in self.filter.paths if path not in self.seen_paths]


Instruction:
How would you code a function in Python 3 to
am child_path a parent directory of parent_path?

Response:
def is_child(child_path, parent_path):
        """
        Is parent_path a parent(or grandparent) directory of child_path.
        :param child_path: str: remote file path
        :param parent_path: str: remote file path
        :return: bool: True when parent_path is child_path's parent
        """
        parent_dir = os.path.join(parent_path, '')
        child_dir = os.path.join(child_path, '')
        return child_dir.startswith(parent_dir)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def parent_child_paths(path, some_path):
        return PathFilterUtil.is_child(path, some_path) or PathFilterUtil.is_child(some_path, path)

Response: Is path a parent of some_path?


Instruction:
Given the following Python 3 function, write the documentation
def method(method_class):

    def callback(scanner, name, method_class):
        if method_class.actions is not None:
            actions = method_class.actions
        else:
            actions = [name]

        if method_class.versions is not None:
            versions = method_class.versions
        else:
            versions = [None]

        for action in actions:
            for version in versions:
                scanner.registry.add(method_class,
                                     action=action,
                                     version=version)

    from venusian import attach
    attach(method_class, callback, category="method")
    return method_class

Response: Decorator to use to mark an API method.


Instruction:
How would you code a function in Python 3 to
take the byte content of a Python script and returns a hex encoded version of it.

Response:
def hexlify(script, minify=False):
    """
    Takes the byte content of a Python script and returns a hex encoded
    version of it.

    Based on the hexlify script in the microbit-micropython repository.
    """
    if not script:
        return ''
    # Convert line endings in case the file was created on Windows.
    script = script.replace(b'\r\n', b'\n')
    script = script.replace(b'\r', b'\n')
    if minify:
        if not can_minify:
            raise ValueError("No minifier is available")
        script = nudatus.mangle(script.decode('utf-8')).encode('utf-8')
    # Add header, pad to multiple of 16 bytes.
    data = b'MP' + struct.pack('<H', len(script)) + script
    # Padding with null bytes in a 2/3 compatible way
    data = data + (b'\x00' * (16 - len(data) % 16))
    if len(data) > _MAX_SIZE:
        # 'MP' = 2 bytes, script length is another 2 bytes.
        raise ValueError("Python script must be less than 8188 bytes.")
    # Convert to .hex format.
    output = [':020000040003F7']  # extended linear address, 0x0003.
    addr = _SCRIPT_ADDR
    for i in range(0, len(data), 16):
        chunk = data[i:min(i + 16, len(data))]
        chunk = struct.pack('>BHB', len(chunk), addr & 0xffff, 0) + chunk
        checksum = (-(sum(bytearray(chunk)))) & 0xff
        hexline = ':%s%02X' % (strfunc(binascii.hexlify(chunk)).upper(),
                               checksum)
        output.append(hexline)
        addr += 16
    return '\n'.join(output)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def unhexlify(blob):
    lines = blob.split('\n')[1:]
    output = []
    for line in lines:
        # Discard the address, length etc. and reverse the hexlification
        output.append(binascii.unhexlify(line[9:-2]))
    # Check the header is correct ("MP<size>")
    if (output[0][0:2].decode('utf-8') != u'MP'):
        return ''
    # Strip off header
    output[0] = output[0][4:]
    # and strip any null bytes from the end
    output[-1] = output[-1].strip(b'\x00')
    script = b''.join(output)
    try:
        result = script.decode('utf-8')
        return result
    except UnicodeDecodeError:
        # Return an empty string because in certain rare circumstances (where
        # the source hex doesn't include any embedded Python code) this
        # function may be passed in "raw" bytes from MicroPython.
        return ''

Response: Takes a hexlified script and turns it back into a string of Python code.


Instruction:
Write a Python 3 function that can
give a string representing the MicroPython runtime hex embeds a Python script into the resulting string.

Response:
def embed_hex(runtime_hex, python_hex=None):
    """
    Given a string representing the MicroPython runtime hex, will embed a
    string representing a hex encoded Python script into it.

    Returns a string representation of the resulting combination.

    Will raise a ValueError if the runtime_hex is missing.

    If the python_hex is missing, it will return the unmodified runtime_hex.
    """
    if not runtime_hex:
        raise ValueError('MicroPython runtime hex required.')
    if not python_hex:
        return runtime_hex
    py_list = python_hex.split()
    runtime_list = runtime_hex.split()
    embedded_list = []
    # The embedded list should be the original runtime with the Python based
    # hex embedded two lines from the end.
    embedded_list.extend(runtime_list[:-5])
    embedded_list.extend(py_list)
    embedded_list.extend(runtime_list[-5:])
    return '\n'.join(embedded_list) + '\n'


Instruction:
How would you implement a function in Python 3 that
gives a hex file containing the MicroPython runtime and an embedded Python script extract the original Python script.

Response:
def extract_script(embedded_hex):
    """
    Given a hex file containing the MicroPython runtime and an embedded Python
    script, will extract the original Python script.

    Returns a string containing the original embedded script.
    """
    hex_lines = embedded_hex.split('\n')
    script_addr_high = hex((_SCRIPT_ADDR >> 16) & 0xffff)[2:].upper().zfill(4)
    script_addr_low = hex(_SCRIPT_ADDR & 0xffff)[2:].upper().zfill(4)
    start_script = None
    within_range = False
    # Look for the script start address
    for loc, val in enumerate(hex_lines):
        if val[0:9] == ':02000004':
            # Reached an extended address record, check if within script range
            within_range = val[9:13].upper() == script_addr_high
        elif within_range and val[0:3] == ':10' and \
                val[3:7].upper() == script_addr_low:
            start_script = loc
            break
    if start_script:
        # Find the end of the script
        end_script = None
        for loc, val in enumerate(hex_lines[start_script:]):
            if val[9:41] == 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF':
                end_script = loc + start_script
                break
        # Pass the extracted hex through unhexlify
        return unhexlify('\n'.join(
            hex_lines[start_script - 1:end_script if end_script else -6]))
    return ''


Instruction:
Can you create a Python 3 function that
finds the microbit file that is to be flashed.

Response:
def find_microbit():
    """
    Returns a path on the filesystem that represents the plugged in BBC
    micro:bit that is to be flashed. If no micro:bit is found, it returns
    None.

    Works on Linux, OSX and Windows. Will raise a NotImplementedError
    exception if run on any other operating system.
    """
    # Check what sort of operating system we're on.
    if os.name == 'posix':
        # 'posix' means we're on Linux or OSX (Mac).
        # Call the unix "mount" command to list the mounted volumes.
        mount_output = check_output('mount').splitlines()
        mounted_volumes = [x.split()[2] for x in mount_output]
        for volume in mounted_volumes:
            if volume.endswith(b'MICROBIT'):
                return volume.decode('utf-8')  # Return a string not bytes.
    elif os.name == 'nt':
        # 'nt' means we're on Windows.

        def get_volume_name(disk_name):
            """
            Each disk or external device connected to windows has an attribute
            called "volume name". This function returns the volume name for
            the given disk/device.

            Code from http://stackoverflow.com/a/12056414
            """
            vol_name_buf = ctypes.create_unicode_buffer(1024)
            ctypes.windll.kernel32.GetVolumeInformationW(
                ctypes.c_wchar_p(disk_name), vol_name_buf,
                ctypes.sizeof(vol_name_buf), None, None, None, None, 0)
            return vol_name_buf.value

        #
        # In certain circumstances, volumes are allocated to USB
        # storage devices which cause a Windows popup to raise if their
        # volume contains no media. Wrapping the check in SetErrorMode
        # with SEM_FAILCRITICALERRORS (1) prevents this popup.
        #
        old_mode = ctypes.windll.kernel32.SetErrorMode(1)
        try:
            for disk in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
                path = '{}:\\'.format(disk)
                #
                # Don't bother looking if the drive isn't removable
                #
                if ctypes.windll.kernel32.GetDriveTypeW(path) != 2:
                    continue
                if os.path.exists(path) and \
                        get_volume_name(path) == 'MICROBIT':
                    return path
        finally:
            ctypes.windll.kernel32.SetErrorMode(old_mode)
    else:
        # No support for unknown operating systems.
        raise NotImplementedError('OS "{}" not supported.'.format(os.name))


Instruction:
Can you write a function in Python 3 where it
gives a string representation of a hex file, this function copies it to the specified path thus causing the device mounted at that point to be flashed. If the hex_file is empty it will raise a ValueError. If the filename at the end of the path does not end in '.hex' it will raise a ValueError.

Response:
def save_hex(hex_file, path):
    """
    Given a string representation of a hex file, this function copies it to
    the specified path thus causing the device mounted at that point to be
    flashed.

    If the hex_file is empty it will raise a ValueError.

    If the filename at the end of the path does not end in '.hex' it will raise
    a ValueError.
    """
    if not hex_file:
        raise ValueError('Cannot flash an empty .hex file.')
    if not path.endswith('.hex'):
        raise ValueError('The path to flash must be for a .hex file.')
    with open(path, 'wb') as output:
        output.write(hex_file.encode('ascii'))


Instruction:
Can you tell what is the following Python 3 function doing
def flash(path_to_python=None, paths_to_microbits=None,
          path_to_runtime=None, python_script=None, minify=False):
    """
    Given a path to or source of a Python file will attempt to create a hex
    file and then flash it onto the referenced BBC micro:bit.

    If the path_to_python & python_script are unspecified it will simply flash
    the unmodified MicroPython runtime onto the device.

    If used, the python_script argument should be a bytes object representing
    a UTF-8 encoded string. For example::

        script = "from microbit import *\\ndisplay.scroll('Hello, World!')"
        uflash.flash(python_script=script.encode('utf-8'))

    If paths_to_microbits is unspecified it will attempt to find the device's
    path on the filesystem automatically.

    If the path_to_runtime is unspecified it will use the built in version of
    the MicroPython runtime. This feature is useful if a custom build of
    MicroPython is available.

    If the automatic discovery fails, then it will raise an IOError.
    """
    # Check for the correct version of Python.
    if not ((sys.version_info[0] == 3 and sys.version_info[1] >= 3) or
            (sys.version_info[0] == 2 and sys.version_info[1] >= 7)):
        raise RuntimeError('Will only run on Python 2.7, or 3.3 and later.')
    # Grab the Python script (if needed).
    python_hex = ''
    if path_to_python:
        if not path_to_python.endswith('.py'):
            raise ValueError('Python files must end in ".py".')
        with open(path_to_python, 'rb') as python_script:
            python_hex = hexlify(python_script.read(), minify)
    elif python_script:
        python_hex = hexlify(python_script, minify)

    runtime = _RUNTIME
    # Load the hex for the runtime.
    if path_to_runtime:
        with open(path_to_runtime) as runtime_file:
            runtime = runtime_file.read()
    # Generate the resulting hex file.
    micropython_hex = embed_hex(runtime, python_hex)
    # Find the micro:bit.
    if not paths_to_microbits:
        found_microbit = find_microbit()
        if found_microbit:
            paths_to_microbits = [found_microbit]
    # Attempt to write the hex file to the micro:bit.
    if paths_to_microbits:
        for path in paths_to_microbits:
            hex_path = os.path.join(path, 'micropython.hex')
            print('Flashing Python to: {}'.format(hex_path))
            save_hex(micropython_hex, hex_path)
    else:
        raise IOError('Unable to find micro:bit. Is it plugged in?')

Response: This function will create a hex file and then flash it onto the device.


Instruction:
How would you code a function in Python 3 to
give a path_to_hex file this function will attempt to extract the embedded script from it and save it to output_path or stdout

Response:
def extract(path_to_hex, output_path=None):
    """
    Given a path_to_hex file this function will attempt to extract the
    embedded script from it and save it either to output_path or stdout
    """
    with open(path_to_hex, 'r') as hex_file:
        python_script = extract_script(hex_file.read())
        if output_path:
            with open(output_path, 'w') as output_file:
                output_file.write(python_script)
        else:
            print(python_script)


Instruction:
How would you code a function in Python 3 to
watch a file for changes by polling its last modification time.

Response:
def watch_file(path, func, *args, **kwargs):
    """
    Watch a file for changes by polling its last modification time. Call the
    provided function with *args and **kwargs upon modification.
    """
    if not path:
        raise ValueError('Please specify a file to watch')
    print('Watching "{}" for changes'.format(path))
    last_modification_time = os.path.getmtime(path)
    try:
        while True:
            time.sleep(1)
            new_modification_time = os.path.getmtime(path)
            if new_modification_time == last_modification_time:
                continue
            func(*args, **kwargs)
            last_modification_time = new_modification_time
    except KeyboardInterrupt:
        pass


Instruction:
Create a Python 3 function to
measure the execution time of a function call and print the result.

Response:
def timing(func):
    """Measure the execution time of a function call and print the result."""
    @functools.wraps(func)
    def wrap(*args, **kw):
        t0 = time()
        result = func(*args, **kw)
        t1 = time()
        print('func:%r args:[%r, %r] took: %2.4f sec' %
              (func.__name__, args, kw, t1 - t0))
        return result
    return wrap


Instruction:
Can you generate a brief explanation for the following Python 3 code
def deprecated(func):
    @functools.wraps(func)
    def new_func(*args, **kwargs):
        if sys.version_info < (3, 0):
            warnings.warn_explicit(
                "Call to deprecated function {}.".format(func.__name__),
                category=DeprecationWarning,
                filename=func.func_code.co_filename,
                lineno=func.func_code.co_firstlineno + 1
            )
        else:
            warnings.warn_explicit(
                "Call to deprecated function {}.".format(func.__name__),
                category=DeprecationWarning,
                filename=func.__code__.co_filename,
                lineno=func.__code__.co_firstlineno + 1
            )
        return func(*args, **kwargs)
    return new_func

Response: Mark functions as deprecated.


Instruction:
How would you explain what the following Python 3 function does
def get_currency(currency_str):
    path = 'units/currencies.csv'  # always use slash in Python packages
    filepath = pkg_resources.resource_filename('mpu', path)
    with open(filepath, 'r') as fp:
        reader = csv.reader(fp, delimiter=',', quotechar='"')
        next(reader, None)  # skip the headers
        for row in reader:
            is_currency = currency_str in [row[0], row[1], row[2]]
            if is_currency:
                entity = row[0]
                name = row[1]
                code = row[2]
                numeric_code = row[3]
                symbol = row[4]
                if len(row[5]) == 0:
                    exponent = None
                else:
                    exponent = int(row[5])
                if len(row[6]) > 0:
                    withdrawal_date = row[6]
                else:
                    withdrawal_date = None
                subunits = row[7]
                return Currency(name=name,
                                code=code,
                                numeric_code=numeric_code,
                                symbol=symbol,
                                exponent=exponent,
                                entities=[entity],
                                withdrawal_date=withdrawal_date,
                                subunits=subunits)
    raise ValueError('Could not find currency \'{}\''.format(currency_str))

Response: Convert an identifier for a currency into a Currency object.


Instruction:
Can you create a Python 3 function that
creates a Currency object from a JSON dump.

Response:
def from_json(cls, json):
        """Create a Currency object from a JSON dump."""
        obj = cls(name=json['name'],
                  code=json['code'],
                  numeric_code=json['numeric_code'],
                  symbol=json['symbol'],
                  exponent=json['exponent'],
                  entities=json['entities'],
                  withdrawal_date=json['withdrawal_date'],
                  subunits=json['subunits'])
        return obj


Instruction:
How would you explain what the following Python 3 function does
def print_table(table):
    table = [[str(cell) for cell in row] for row in table]
    column_widths = [len(cell) for cell in table[0]]
    for row in table:
        for x, cell in enumerate(row):
            column_widths[x] = max(column_widths[x], len(cell))

    formatters = []
    for width in column_widths:
        formatters.append('{:>' + str(width) + '}')
    formatter = '  '.join(formatters)
    for row in table:
        print(formatter.format(*row))

Response: Print a list of tables as a single table.


Instruction:
How would you implement a function in Python 3 that
checks if the input is a valid e - mail address.

Response:
def is_email(potential_email_address):
    """
    Check if potential_email_address is a valid e-mail address.

    Please note that this function has no false-negatives but many
    false-positives. So if it returns that the input is not a valid
    e-mail adress, it certainly isn't. If it returns True, it might still be
    invalid. For example, the domain could not be registered.

    Parameters
    ----------
    potential_email_address : str

    Returns
    -------
    is_email : bool

    Examples
    --------
    >>> is_email('')
    False
    >>> is_email('info@martin-thoma.de')
    True
    >>> is_email('info@math.martin-thoma.de')
    True
    >>> is_email('Martin Thoma <info@martin-thoma.de>')
    False
    >>> is_email('info@martin-thoma')
    False
    """
    context, mail = parseaddr(potential_email_address)
    first_condition = len(context) == 0 and len(mail) != 0
    dot_after_at = ('@' in potential_email_address and
                    '.' in potential_email_address.split('@')[1])
    return first_condition and dot_after_at


Instruction:
Create a Python 3 function for
converting a string to a boolean.

Response:
def str2bool(string_, default='raise'):
    """
    Convert a string to a bool.

    Parameters
    ----------
    string_ : str
    default : {'raise', False}
        Default behaviour if none of the "true" strings is detected.

    Returns
    -------
    boolean : bool

    Examples
    --------
    >>> str2bool('True')
    True
    >>> str2bool('1')
    True
    >>> str2bool('0')
    False
    """
    true = ['true', 't', '1', 'y', 'yes', 'enabled', 'enable', 'on']
    false = ['false', 'f', '0', 'n', 'no', 'disabled', 'disable', 'off']
    if string_.lower() in true:
        return True
    elif string_.lower() in false or (not default):
        return False
    else:
        raise ValueError('The value \'{}\' cannot be mapped to boolean.'
                         .format(string_))


Instruction:
Can you write a function in Python 3 where it
converts a string to a bool or to None.

Response:
def str2bool_or_none(string_, default='raise'):
    """
    Convert a string to a bool or to None.

    Parameters
    ----------
    string_ : str
    default : {'raise', False}
        Default behaviour if none of the "true" or "none" strings is detected.

    Returns
    -------
    bool_or_none : bool or None

    Examples
    --------
    >>> str2bool_or_none('True')
    True
    >>> str2bool_or_none('1')
    True
    >>> str2bool_or_none('0')
    False
    >>> str2bool_or_none('undefined')
    """
    if is_none(string_, default=False):
        return None
    else:
        return str2bool(string_, default)


Instruction:
Write a Python 3 script to
check if a string is equivalent to None.

Response:
def is_none(string_, default='raise'):
    """
    Check if a string is equivalent to None.

    Parameters
    ----------
    string_ : str
    default : {'raise', False}
        Default behaviour if none of the "None" strings is detected.

    Returns
    -------
    is_none : bool

    Examples
    --------
    >>> is_none('2', default=False)
    False
    >>> is_none('undefined', default=False)
    True
    """
    none = ['none', 'undefined', 'unknown', 'null', '']
    if string_.lower() in none:
        return True
    elif not default:
        return False
    else:
        raise ValueError('The value \'{}\' cannot be mapped to none.'
                         .format(string_))


Instruction:
How would you explain what the following Python 3 function does
def is_iban(potential_iban):
    path = 'data/iban.csv'  # always use slash in Python packages
    filepath = pkg_resources.resource_filename('mpu', path)
    data = mpu.io.read(filepath, delimiter=';', format='dicts')
    potential_iban = potential_iban.replace(' ', '')  # Remove spaces
    if len(potential_iban) < min([int(el['length']) for el in data]):
        return False
    country = None
    for element in data:
        if element['iban_fields'][:2] == potential_iban[:2]:
            country = element
            break
    if country is None:
        return False
    if len(potential_iban) != int(country['length']):
        return False
    if country['country_en'] == 'Germany':
        checksum_val = [value
                        for field_type, value in
                        zip(country['iban_fields'], potential_iban)
                        if field_type == 'k']
        checksum_val = ''.join(checksum_val)
        checksum_exp = _calculate_german_iban_checksum(potential_iban,
                                                       country['iban_fields'])
        return checksum_val == checksum_exp
    return True

Response: Check if a string is a valid IBAN number.


Instruction:
Implement a Python 3 function for
calculating the checksam of the German IBAN format.

Response:
def _calculate_german_iban_checksum(iban,
                                    iban_fields='DEkkbbbbbbbbcccccccccc'):
    """
    Calculate the checksam of the German IBAN format.

    Examples
    --------
    >>> iban =        'DE41500105170123456789'
    >>> _calculate_german_iban_checksum(iban)
    '41'
    """
    number = [value
              for field_type, value in zip(iban_fields, iban)
              if field_type in ['b', 'c']]
    translate = {'0': '0', '1': '1', '2': '2', '3': '3', '4': '4', '5': '5',
                 '6': '6', '7': '7', '8': '8', '9': '9'}
    for i in range(ord('A'), ord('Z') + 1):
        translate[chr(i)] = str(i - ord('A') + 10)
    for val in 'DE00':
        translated = translate[val]
        for char in translated:
            number.append(char)
    number = sum(int(value) * 10**i for i, value in enumerate(number[::-1]))
    checksum = 98 - (number % 97)
    return str(checksum)


Instruction:
Given the following Python 3 function, write the documentation
def human_readable_bytes(nb_bytes, suffix='B'):
    for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
        if abs(nb_bytes) < 1024.0:
            return '%3.1f %s%s' % (nb_bytes, unit, suffix)
        nb_bytes /= 1024.0
    return '%.1f %s%s' % (nb_bytes, 'Yi', suffix)

Response: Convert a byte number into a human readable format.


Instruction:
Explain what the following Python 3 code does
def list_files(bucket, profile_name=None):
    session = boto3.Session(profile_name=profile_name)
    conn = session.client('s3')
    keys = []
    ret = conn.list_objects(Bucket=bucket)
    print(ret)
    if 'Contents' not in ret:
        return []
    # Make this a generator in future and use the marker:
    # https://boto3.readthedocs.io/en/latest/reference/services/
    #     s3.html#S3.Client.list_objects
    for key in conn.list_objects(Bucket=bucket)['Contents']:
        keys.append('s3://' + bucket + '/' + key['Key'])
    return keys

Response: List up to 1000 files in a bucket.


Instruction:
Write a Python 3 script to
read a file from an S3 source.

Response:
def s3_read(source, profile_name=None):
    """
    Read a file from an S3 source.

    Parameters
    ----------
    source : str
        Path starting with s3://, e.g. 's3://bucket-name/key/foo.bar'
    profile_name : str, optional
        AWS profile

    Returns
    -------
    content : bytes

    Raises
    ------
    botocore.exceptions.NoCredentialsError
        Botocore is not able to find your credentials. Either specify
        profile_name or add the environment variables AWS_ACCESS_KEY_ID,
        AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN.
        See https://boto3.readthedocs.io/en/latest/guide/configuration.html
    """
    session = boto3.Session(profile_name=profile_name)
    s3 = session.client('s3')
    bucket_name, key = _s3_path_split(source)
    s3_object = s3.get_object(Bucket=bucket_name, Key=key)
    body = s3_object['Body']
    return body.read()


Instruction:
How would you implement a function in Python 3 that
copies a file from an S3 source to a local destination.

Response:
def s3_download(source, destination,
                exists_strategy=ExistsStrategy.RAISE,
                profile_name=None):
    """
    Copy a file from an S3 source to a local destination.

    Parameters
    ----------
    source : str
        Path starting with s3://, e.g. 's3://bucket-name/key/foo.bar'
    destination : str
    exists_strategy : {'raise', 'replace', 'abort'}
        What is done when the destination already exists?
        * `ExistsStrategy.RAISE` means a RuntimeError is raised,
        * `ExistsStrategy.REPLACE` means the local file is replaced,
        * `ExistsStrategy.ABORT` means the download is not done.
    profile_name : str, optional
        AWS profile

    Raises
    ------
    botocore.exceptions.NoCredentialsError
        Botocore is not able to find your credentials. Either specify
        profile_name or add the environment variables AWS_ACCESS_KEY_ID,
        AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN.
        See https://boto3.readthedocs.io/en/latest/guide/configuration.html
    """
    if not isinstance(exists_strategy, ExistsStrategy):
        raise ValueError('exists_strategy \'{}\' is not in {}'
                         .format(exists_strategy, ExistsStrategy))
    session = boto3.Session(profile_name=profile_name)
    s3 = session.resource('s3')
    bucket_name, key = _s3_path_split(source)
    if os.path.isfile(destination):
        if exists_strategy is ExistsStrategy.RAISE:
            raise RuntimeError('File \'{}\' already exists.'
                               .format(destination))
        elif exists_strategy is ExistsStrategy.ABORT:
            return
    s3.Bucket(bucket_name).download_file(key, destination)


Instruction:
Make a summary of the following Python 3 code
def s3_upload(source, destination, profile_name=None):
    session = boto3.Session(profile_name=profile_name)
    s3 = session.resource('s3')
    bucket_name, key = _s3_path_split(destination)
    with open(source, 'rb') as data:
        s3.Bucket(bucket_name).put_object(Key=key, Body=data)

Response: Copy a file from a local source to an S3 destination.


Instruction:
Can you generate the documentation for the following Python 3 function
def _s3_path_split(s3_path):
    if not s3_path.startswith('s3://'):
        raise ValueError('s3_path is expected to start with \'s3://\', '
                         'but was {}'.format(s3_path))
    bucket_key = s3_path[len('s3://'):]
    bucket_name, key = bucket_key.split('/', 1)
    return S3Path(bucket_name, key)

Response: Splits an S3 path into bucket and key.


Instruction:
Write a Python 3 function that can
get meta - information of an image.

Response:
def get_meta(filepath):
    """
    Get meta-information of an image.

    Parameters
    ----------
    filepath : str

    Returns
    -------
    meta : dict
    """
    meta = {}
    try:
        from PIL import Image
        with Image.open(filepath) as img:
            width, height = img.size
        meta['width'] = width
        meta['height'] = height
        meta['channels'] = len(img.mode)  # RGB, RGBA - does this always work?
    except ImportError:
        pass

    # Get times - creation, last edit, last open
    meta['file'] = mpu.io.get_file_meta(filepath)
    return meta


Instruction:
Can you create a Python 3 function that
flattens an iterable of iterables into one list.

Response:
def flatten(iterable, string_flattening=False):
    """
    Flatten an given iterable of iterables into one list.

    Parameters
    ----------
    iterable : iterable
    string_flattening : bool
        If this is False, then strings are NOT flattened

    Returns
    -------
    flat_list : List

    Examples
    --------
    >>> flatten([1, [2, [3]]])
    [1, 2, 3]

    >>> flatten(((1, 2), (3, 4), (5, 6)))
    [1, 2, 3, 4, 5, 6]

    >>> flatten(EList([EList([1, 2]), (3, [4, [[5]]])]))
    [1, 2, 3, 4, 5]
    """
    flat_list = []
    for item in iterable:
        is_iterable = (isinstance(item, collections.Iterable) and
                       (string_flattening or
                        (not string_flattening and not isinstance(item, str))
                        ))
        if is_iterable:
            flat_list.extend(flatten(item))
        else:
            flat_list.append(item)
    return flat_list


Instruction:
Given the following Python 3 function, write the documentation
def dict_merge(dict_left, dict_right, merge_method='take_left_shallow'):
    new_dict = {}
    if merge_method in ['take_right_shallow', 'take_right_deep']:
        return _dict_merge_right(dict_left, dict_right, merge_method)
    elif merge_method == 'take_left_shallow':
        return dict_merge(dict_right, dict_left, 'take_right_shallow')
    elif merge_method == 'take_left_deep':
        return dict_merge(dict_right, dict_left, 'take_right_deep')
    elif merge_method == 'sum':
        new_dict = deepcopy(dict_left)
        for key, value in dict_right.items():
            if key not in new_dict:
                new_dict[key] = value
            else:
                recurse = isinstance(value, dict)
                if recurse:
                    new_dict[key] = dict_merge(dict_left[key],
                                               dict_right[key],
                                               merge_method='sum')
                else:
                    new_dict[key] = dict_left[key] + dict_right[key]
        return new_dict
    else:
        raise NotImplementedError('merge_method=\'{}\' is not known.'
                                  .format(merge_method))

Response: This method merges two dictionaries.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _dict_merge_right(dict_left, dict_right, merge_method):
    new_dict = deepcopy(dict_left)
    for key, value in dict_right.items():
        if key not in new_dict:
            new_dict[key] = value
        else:
            recurse = (merge_method == 'take_right_deep' and
                       isinstance(dict_left[key], dict) and
                       isinstance(dict_right[key], dict))
            if recurse:
                new_dict[key] = dict_merge(dict_left[key],
                                           dict_right[key],
                                           merge_method='take_right_deep')
            else:
                new_dict[key] = value
    return new_dict

Response: See documentation of mpu.datastructures.dict_merge.


Instruction:
Can you write a function in Python 3 where it
sets a value in a nested dictionary by defining a list of keys.

Response:
def set_dict_value(dictionary, keys, value):
    """
    Set a value in a (nested) dictionary by defining a list of keys.

    .. note:: Side-effects
              This function does not make a copy of dictionary, but directly
              edits it.

    Parameters
    ----------
    dictionary : dict
    keys : List[Any]
    value : object

    Returns
    -------
    dictionary : dict

    Examples
    --------
    >>> d = {'a': {'b': 'c', 'd': 'e'}}
    >>> expected = {'a': {'b': 'foobar', 'd': 'e'}}
    >>> set_dict_value(d, ['a', 'b'], 'foobar') == expected
    True
    """
    orig = dictionary
    for key in keys[:-1]:
        dictionary = dictionary.setdefault(key, {})
    dictionary[keys[-1]] = value
    return orig


Instruction:
Implement a Python 3 function for
checking if a sequence of keys exist in a nested dictionary.

Response:
def does_keychain_exist(dict_, list_):
    """
    Check if a sequence of keys exist in a nested dictionary.

    Parameters
    ----------
    dict_ : Dict[str/int/tuple, Any]
    list_ : List[str/int/tuple]

    Returns
    -------
    keychain_exists : bool

    Examples
    --------
    >>> d = {'a': {'b': {'c': 'd'}}}
    >>> l_exists = ['a', 'b']
    >>> does_keychain_exist(d, l_exists)
    True

    >>> l_no_existant = ['a', 'c']
    >>> does_keychain_exist(d, l_no_existant)
    False
    """
    for key in list_:
        if key not in dict_:
            return False
        dict_ = dict_[key]
    return True


Instruction:
How would you implement a function in Python 3 that
returns a new list of items from the list that do not have the given indices.

Response:
def remove_indices(self, indices):
        """
        Remove rows by which have the given indices.

        Parameters
        ----------
        indices : list

        Returns
        -------
        filtered_list : EList
        """
        new_list = []
        for index, element in enumerate(self):
            if index not in indices:
                new_list.append(element)
        return EList(new_list)


Instruction:
Can you tell what is the following Python 3 function doing
def get_all_files(root, followlinks=False):
    filepaths = []
    for path, _, files in os.walk(root, followlinks=followlinks):
        for name in files:
            filepaths.append(os.path.abspath(os.path.join(path, name)))
    return filepaths

Response: Get all files within a given root directory.


Instruction:
Explain what the following Python 3 code does
def get_from_package(package_name, path):
    filepath = pkg_resources.resource_filename(package_name, path)
    return os.path.abspath(filepath)

Response: Get the absolute path to a file in a package.


Instruction:
Create a Python 3 function to
create an example dataframe.

Response:
def example_df():
    """Create an example dataframe."""
    country_names = ['Germany',
                     'France',
                     'Indonesia',
                     'Ireland',
                     'Spain',
                     'Vatican']
    population = [82521653, 66991000, 255461700, 4761865, 46549045, None]
    population_time = [dt.datetime(2016, 12, 1),
                       dt.datetime(2017, 1, 1),
                       dt.datetime(2017, 1, 1),
                       None,  # Ireland
                       dt.datetime(2017, 6, 1),  # Spain
                       None,
                       ]
    euro = [True, True, False, True, True, True]
    df = pd.DataFrame({'country': country_names,
                       'population': population,
                       'population_time': population_time,
                       'EUR': euro})
    df = df[['country', 'population', 'population_time', 'EUR']]
    return df


Instruction:
Implement a function in Python 3 to
print a description of a Pandas dataframe.

Response:
def describe(df, dtype=None):
    """
    Print a description of a Pandas dataframe.

    Parameters
    ----------
    df : Pandas.DataFrame
    dtype : dict
        Maps column names to types
    """
    if dtype is None:
        dtype = {}
    print('Number of datapoints: {datapoints}'.format(datapoints=len(df)))
    column_info, column_info_meta = _get_column_info(df, dtype)

    if len(column_info['int']) > 0:
        _describe_int(df, column_info)

    if len(column_info['float']) > 0:
        _describe_float(df, column_info)

    if len(column_info['category']) > 0:
        _describe_category(df, column_info, column_info_meta)

    if len(column_info['time']) > 0:
        _describe_time(df, column_info, column_info_meta)

    if len(column_info['other']) > 0:
        _describe_other(df, column_info, column_info_meta)

    column_types = {}
    for column_type, columns in column_info.items():
        for column_name in columns:
            if column_type == 'other':
                column_type = 'str'
            column_types[column_name] = column_type
    return column_types


Instruction:
Given the following Python 3 function, write the documentation
def indices2one_hot(indices, nb_classes):
    if nb_classes < 1:
        raise ValueError('nb_classes={}, but positive number expected'
                         .format(nb_classes))

    one_hot = []
    for index in indices:
        one_hot.append([0] * nb_classes)
        one_hot[-1][index] = 1
    return one_hot

Response: Convert an iterable of indices to one - hot encoded list.


Instruction:
How would you explain what the following Python 3 function does
def one_hot2indices(one_hots):
    indices = []
    for one_hot in one_hots:
        indices.append(argmax(one_hot))
    return indices

Response: Convert an iterable of one - hot encoded targets to a list of indices.


Instruction:
Write a Python 3 script for
generating an infinite sequence of prime numbers.

Response:
def generate_primes():
    """
    Generate an infinite sequence of prime numbers.

    The algorithm was originally written by David Eppstein, UC Irvine. See:
    http://code.activestate.com/recipes/117119/

    Examples
    --------
    >>> g = generate_primes()
    >>> next(g)
    2
    >>> next(g)
    3
    >>> next(g)
    5
    """
    divisors = {}  # map number to at least one divisor

    candidate = 2  # next potential prime

    while True:
        if candidate in divisors:
            # candidate is composite. divisors[candidate] is the list of primes
            # that divide it. Since we've reached candidate, we no longer need
            # it in the map, but we'll mark the next multiples of its witnesses
            # to prepare for larger numbers
            for p in divisors[candidate]:
                divisors.setdefault(p + candidate, []).append(p)
            del divisors[candidate]
        else:
            # candidate is a new prime
            yield candidate

            # mark its first multiple that isn't
            # already marked in previous iterations
            divisors[candidate * candidate] = [candidate]

        candidate += 1


Instruction:
Write a Python 3 function that can
return the prime factors of an integer except for 1.

Response:
def factorize(number):
    """
    Get the prime factors of an integer except for 1.

    Parameters
    ----------
    number : int

    Returns
    -------
    primes : iterable

    Examples
    --------
    >>> factorize(-17)
    [-1, 17]
    >>> factorize(8)
    [2, 2, 2]
    >>> factorize(3**25)
    [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
    >>> factorize(1)
    [1]
    """
    if not isinstance(number, int):
        raise ValueError('integer expected, but type(number)={}'
                         .format(type(number)))
    if number < 0:
        return [-1] + factorize(number * (-1))
    elif number == 0:
        raise ValueError('All primes are prime factors of 0.')
    else:
        for i in range(2, int(math_stl.ceil(number**0.5)) + 1):
            if number % i == 0:
                if i == number:
                    return [i]
                else:
                    return [i] + factorize(int(number / i))
        return [number]


Instruction:
Write a Python 3 function for
finding the first index of the biggest value in the iterable.

Response:
def argmax(iterable):
    """
    Find the first index of the biggest value in the iterable.

    Parameters
    ----------
    iterable : iterable

    Returns
    -------
    argmax : int

    Examples
    --------
    >>> argmax([0, 0, 0])
    0
    >>> argmax([1, 0, 0])
    0
    >>> argmax([0, 1, 0])
    1
    >>> argmax([])
    """
    max_value = None
    max_index = None
    for index, value in enumerate(iterable):
        if (max_value is None) or max_value < value:
            max_value = value
            max_index = index
    return max_index


Instruction:
Can you write a function in Python 3 where it
rounds a float down to decimal_places.

Response:
def round_down(x, decimal_places):
    """
    Round a float down to decimal_places.

    Parameters
    ----------
    x : float
    decimal_places : int

    Returns
    -------
    rounded_float : float

    Examples
    --------
    >>> round_down(1.23456, 3)
    1.234
    >>> round_down(1.23456, 2)
    1.23
    """
    from math import floor
    d = int('1' + ('0' * decimal_places))
    return floor(x * d) / d


Instruction:
Write a Python 3 function for
adding time to a timezone - aware datetime object.

Response:
def add_time(datetime_obj, days=0, hours=0, minutes=0, seconds=0):
    """
    Add time to a timezone-aware datetime object.

    This keeps the timezone correct, even if it changes due to daylight
    saving time (DST).

    Parameters
    ----------
    datetime_obj : datetime.datetime
    days : int
    hours : int
    minutes : int
    seconds : int

    Returns
    -------
    datetime : datetime.datetime
    """
    seconds += minutes * 60
    seconds += hours * 60**2
    seconds += days * 24 * 60**2
    t14 = datetime_obj + dt.timedelta(seconds=seconds)  # Invalid timezone!
    t14 = t14.astimezone(pytz.utc).astimezone(t14.tzinfo)  # Fix the timezone
    return t14


Instruction:
Can you tell what is the following Python 3 function doing
def generate(minimum, maximum, local_random=random.Random()):
    if not (minimum < maximum):
        raise ValueError('{} is not smaller than {}'.format(minimum, maximum))

    # Python 3 allows direct multiplication of timedelta with a float, but
    # Python 2.7 does not. Hence this work-around.
    time_d = maximum - minimum
    time_d_float = time_d.total_seconds()
    time_d_rand = dt.timedelta(seconds=time_d_float * local_random.random())
    generated = minimum + time_d_rand
    return generated

Response: Generate a random date.