Instruction:
Can you implement a function in Python 3 that
computes the CQT with unwrapped phase

Response:
def transform_audio(self, y):
        '''Compute the CQT with unwrapped phase

        Parameters
        ----------
        y : np.ndarray
            The audio buffer

        Returns
        -------
        data : dict
            data['mag'] : np.ndarray, shape=(n_frames, n_bins)
                CQT magnitude

            data['dphase'] : np.ndarray, shape=(n_frames, n_bins)
                Unwrapped phase differential
        '''
        data = super(CQTPhaseDiff, self).transform_audio(y)
        data['dphase'] = self.phase_diff(data.pop('phase'))
        return data


Instruction:
Given the following Python 3 function, write the documentation
def transform_audio(self, y):
        '''Compute the HCQT

        Parameters
        ----------
        y : np.ndarray
            The audio buffer

        Returns
        -------
        data : dict
            data['mag'] : np.ndarray, shape = (n_frames, n_bins, n_harmonics)
                The CQT magnitude

            data['phase']: np.ndarray, shape = mag.shape
                The CQT phase
        '''
        cqtm, phase = [], []

        n_frames = self.n_frames(get_duration(y=y, sr=self.sr))

        for h in self.harmonics:
            C = cqt(y=y, sr=self.sr, hop_length=self.hop_length,
                    fmin=self.fmin * h,
                    n_bins=(self.n_octaves * self.over_sample * 12),
                    bins_per_octave=(self.over_sample * 12))

            C = fix_length(C, n_frames)

            C, P = magphase(C)
            if self.log:
                C = amplitude_to_db(C, ref=np.max)
            cqtm.append(C)
            phase.append(P)

        cqtm = np.asarray(cqtm).astype(np.float32)
        phase = np.angle(np.asarray(phase)).astype(np.float32)

        return {'mag': self._index(cqtm),
                'phase': self._index(phase)}

Response: Compute the HCQT magnitude and phase of the audio buffer.


Instruction:
Create a Python 3 function for
rearranging a tensor according to the convolution mode Input is assumed to be in ( channels bins time format.

Response:
def _index(self, value):
        '''Rearrange a tensor according to the convolution mode

        Input is assumed to be in (channels, bins, time) format.
        '''

        if self.conv in ('channels_last', 'tf'):
            return np.transpose(value, (2, 1, 0))

        else:  # self.conv in ('channels_first', 'th')
            return np.transpose(value, (0, 2, 1))


Instruction:
How would you explain what the following Python 3 function does
def transform_audio(self, y):
        '''Compute HCQT magnitude.

        Parameters
        ----------
        y : np.ndarray
            the audio buffer

        Returns
        -------
        data : dict
            data['mag'] : np.ndarray, shape=(n_frames, n_bins)
                The CQT magnitude
        '''
        data = super(HCQTMag, self).transform_audio(y)
        data.pop('phase')
        return data

Response:
Compute HCQT magnitude.

        Parameters
        ----------
        y : np.ndarray
            the audio buffer

        Returns
        -------
        data : dict
            data['mag'] : np.ndarray, shape=(n_frames, n_bins)
                The CQT magnitude


Instruction:
Can you generate a brief explanation for the following Python 3 code
def transform_audio(self, y):
        '''Compute the HCQT with unwrapped phase

        Parameters
        ----------
        y : np.ndarray
            The audio buffer

        Returns
        -------
        data : dict
            data['mag'] : np.ndarray, shape=(n_frames, n_bins)
                CQT magnitude

            data['dphase'] : np.ndarray, shape=(n_frames, n_bins)
                Unwrapped phase differential
        '''
        data = super(HCQTPhaseDiff, self).transform_audio(y)
        data['dphase'] = self.phase_diff(data.pop('phase'))
        return data

Response: Compute the HCQT with unwrapped phase


Instruction:
Implement a Python 3 function for
getting a fill - value for a given dtype

Response:
def fill_value(dtype):
    '''Get a fill-value for a given dtype

    Parameters
    ----------
    dtype : type

    Returns
    -------
    `np.nan` if `dtype` is real or complex

    0 otherwise
    '''
    if np.issubdtype(dtype, np.floating) or np.issubdtype(dtype, np.complexfloating):
        return dtype(np.nan)

    return dtype(0)


Instruction:
Implement a Python 3 function for
creating an empty jams. Annotation for this task.

Response:
def empty(self, duration):
        '''Create an empty jams.Annotation for this task.

        This method should be overridden by derived classes.

        Parameters
        ----------
        duration : int >= 0
            Duration of the annotation
        '''
        return jams.Annotation(namespace=self.namespace, time=0, duration=0)


Instruction:
How would you implement a function in Python 3 that
transforms the jam object to make data for this task.

Response:
def transform(self, jam, query=None):
        '''Transform jam object to make data for this task

        Parameters
        ----------
        jam : jams.JAMS
            The jams container object

        query : string, dict, or callable [optional]
            An optional query to narrow the elements of `jam.annotations`
            to be considered.

            If not provided, all annotations are considered.

        Returns
        -------
        data : dict
            A dictionary of transformed annotations.
            All annotations which can be converted to the target namespace
            will be converted.
        '''
        anns = []
        if query:
            results = jam.search(**query)
        else:
            results = jam.annotations

        # Find annotations that can be coerced to our target namespace
        for ann in results:
            try:
                anns.append(jams.nsconvert.convert(ann, self.namespace))
            except jams.NamespaceError:
                pass

        duration = jam.file_metadata.duration

        # If none, make a fake one
        if not anns:
            anns = [self.empty(duration)]

        # Apply transformations
        results = []
        for ann in anns:

            results.append(self.transform_annotation(ann, duration))
            # If the annotation range is None, it spans the entire track
            if ann.time is None or ann.duration is None:
                valid = [0, duration]
            else:
                valid = [ann.time, ann.time + ann.duration]

            results[-1]['_valid'] = time_to_frames(valid, sr=self.sr,
                                                   hop_length=self.hop_length)

        # Prefix and collect
        return self.merge(results)


Instruction:
How would you code a function in Python 3 to
encode labeled events as a time - series matrix.

Response:
def encode_events(self, duration, events, values, dtype=np.bool):
        '''Encode labeled events as a time-series matrix.

        Parameters
        ----------
        duration : number
            The duration of the track

        events : ndarray, shape=(n,)
            Time index of the events

        values : ndarray, shape=(n, m)
            Values array.  Must have the same first index as `events`.

        dtype : numpy data type

        Returns
        -------
        target : ndarray, shape=(n_frames, n_values)
        '''

        frames = time_to_frames(events, sr=self.sr,
                                hop_length=self.hop_length)

        n_total = int(time_to_frames(duration, sr=self.sr,
                                     hop_length=self.hop_length))

        n_alloc = n_total
        if np.any(frames):
            n_alloc = max(n_total, 1 + int(frames.max()))

        target = np.empty((n_alloc, values.shape[1]),
                          dtype=dtype)

        target.fill(fill_value(dtype))
        values = values.astype(dtype)
        for column, event in zip(values, frames):
            target[event] += column

        return target[:n_total]


Instruction:
How would you implement a function in Python 3 that
encodes labeled intervals as a time - series matrix.

Response:
def encode_intervals(self, duration, intervals, values, dtype=np.bool,
                         multi=True, fill=None):
        '''Encode labeled intervals as a time-series matrix.

        Parameters
        ----------
        duration : number
            The duration (in frames) of the track

        intervals : np.ndarray, shape=(n, 2)
            The list of intervals

        values : np.ndarray, shape=(n, m)
            The (encoded) values corresponding to each interval

        dtype : np.dtype
            The desired output type

        multi : bool
            If `True`, allow multiple labels per interval.

        fill : dtype (optional)
            Optional default fill value for missing data.

            If not provided, the default is inferred from `dtype`.

        Returns
        -------
        target : np.ndarray, shape=(duration * sr / hop_length, m)
            The labeled interval encoding, sampled at the desired frame rate
        '''
        if fill is None:
            fill = fill_value(dtype)

        frames = time_to_frames(intervals, sr=self.sr,
                                hop_length=self.hop_length)

        n_total = int(time_to_frames(duration, sr=self.sr,
                                     hop_length=self.hop_length))

        values = values.astype(dtype)

        n_alloc = n_total
        if np.any(frames):
            n_alloc = max(n_total, 1 + int(frames.max()))

        target = np.empty((n_alloc, values.shape[1]),

                          dtype=dtype)

        target.fill(fill)

        for column, interval in zip(values, frames):
            if multi:
                target[interval[0]:interval[1]] += column
            else:
                target[interval[0]:interval[1]] = column

        return target[:n_total]


Instruction:
Explain what the following Python 3 code does
def decode_events(self, encoded, transition=None, p_state=None, p_init=None):
        '''Decode labeled events into (time, value) pairs

        Real-valued inputs are thresholded at 0.5.

        Optionally, viterbi decoding can be applied to each event class.

        Parameters
        ----------
        encoded : np.ndarray, shape=(n_frames, m)
            Frame-level annotation encodings as produced by ``encode_events``.

        transition : None or np.ndarray [shape=(2, 2) or (m, 2, 2)]
            Optional transition matrix for each event, used for Viterbi

        p_state : None or np.ndarray [shape=(m,)]
            Optional marginal probability for each event

        p_init : None or np.ndarray [shape=(m,)]
            Optional marginal probability for each event

        Returns
        -------
        [(time, value)] : iterable of tuples
            where `time` is the event time and `value` is an
            np.ndarray, shape=(m,) of the encoded value at that time

        See Also
        --------
        librosa.sequence.viterbi_binary
        '''
        if np.isrealobj(encoded):
            if transition is None:
                encoded = (encoded >= 0.5)
            else:
                encoded = viterbi_binary(encoded.T, transition,
                                         p_state=p_state,
                                         p_init=p_init).T

        times = times_like(encoded,
                           sr=self.sr,
                           hop_length=self.hop_length,
                           axis=0)

        return zip(times, encoded)

Response: Decode labeled events into time value pairs.


Instruction:
Can you tell what is the following Python 3 function doing
def decode_intervals(self, encoded, duration=None, multi=True, sparse=False,
                         transition=None, p_state=None, p_init=None):
        '''Decode labeled intervals into (start, end, value) triples

        Parameters
        ----------
        encoded : np.ndarray, shape=(n_frames, m)
            Frame-level annotation encodings as produced by
            ``encode_intervals``

        duration : None or float > 0
            The max duration of the annotation (in seconds)
            Must be greater than the length of encoded array.

        multi : bool
            If true, allow multiple labels per input frame.
            If false, take the most likely label per input frame.

        sparse : bool
            If true, values are returned as indices, not one-hot.
            If false, values are returned as one-hot encodings.

            Only applies when `multi=False`.

        transition : None or np.ndarray [shape=(m, m) or (2, 2) or (m, 2, 2)]
            Optional transition matrix for each interval, used for Viterbi
            decoding.  If `multi=True`, then transition should be `(2, 2)` or
            `(m, 2, 2)`-shaped.  If `multi=False`, then transition should be
            `(m, m)`-shaped.

        p_state : None or np.ndarray [shape=(m,)]
            Optional marginal probability for each label.

        p_init : None or np.ndarray [shape=(m,)]
            Optional marginal probability for each label.

        Returns
        -------
        [(start, end, value)] : iterable of tuples
            where `start` and `end` are the interval boundaries (in seconds)
            and `value` is an np.ndarray, shape=(m,) of the encoded value
            for this interval.
        '''
        if np.isrealobj(encoded):
            if multi:
                if transition is None:
                    encoded = encoded >= 0.5
                else:
                    encoded = viterbi_binary(encoded.T, transition,
                                             p_init=p_init, p_state=p_state).T
            elif sparse and encoded.shape[1] > 1:
                # map to argmax if it's densely encoded (logits)
                if transition is None:
                    encoded = np.argmax(encoded, axis=1)[:, np.newaxis]
                else:
                    encoded = viterbi_discriminative(encoded.T, transition,
                                                     p_init=p_init,
                                                     p_state=p_state)[:, np.newaxis]
            elif not sparse:
                # if dense and multi, map to one-hot encoding
                if transition is None:
                    encoded = (encoded == np.max(encoded, axis=1, keepdims=True))
                else:
                    encoded_ = viterbi_discriminative(encoded.T, transition,
                                                      p_init=p_init,
                                                      p_state=p_state)
                    # Map to one-hot encoding
                    encoded = np.zeros(encoded.shape, dtype=bool)
                    encoded[np.arange(len(encoded_)), encoded_] = True

        if duration is None:
            # 1+ is fair here, because encode_intervals already pads
            duration = 1 + encoded.shape[0]
        else:
            duration = 1 + time_to_frames(duration,
                                          sr=self.sr,
                                          hop_length=self.hop_length)

        # [0, duration] inclusive
        times = times_like(duration + 1,
                           sr=self.sr, hop_length=self.hop_length)

        # Find the change-points of the rows
        if sparse:
            idx = np.where(encoded[1:] != encoded[:-1])[0]
        else:
            idx = np.where(np.max(encoded[1:] != encoded[:-1], axis=-1))[0]

        idx = np.unique(np.append(idx, encoded.shape[0]))
        delta = np.diff(np.append(-1, idx))

        # Starting positions can be integrated from changes
        position = np.cumsum(np.append(0, delta))

        return [(times[p], times[p + d], encoded[p])
                for (p, d) in zip(position, delta)]

Response: Decode labeled intervals into start end value pairs.


Instruction:
Implement a function in Python 3 to
transform an audio signal y into a dictionary containing features extracted from y.

Response:
def transform(self, y, sr):
        '''Transform an audio signal

        Parameters
        ----------
        y : np.ndarray
            The audio signal

        sr : number > 0
            The native sampling rate of y

        Returns
        -------
        dict
            Data dictionary containing features extracted from y

        See Also
        --------
        transform_audio
        '''
        if sr != self.sr:
            y = resample(y, sr, self.sr)

        return self.merge([self.transform_audio(y)])


Instruction:
Can you write a function in Python 3 where it
computes the phase differential along a given axis

Response:
def phase_diff(self, phase):
        '''Compute the phase differential along a given axis

        Parameters
        ----------
        phase : np.ndarray
            Input phase (in radians)

        Returns
        -------
        dphase : np.ndarray like `phase`
            The phase differential.
        '''

        if self.conv is None:
            axis = 0
        elif self.conv in ('channels_last', 'tf'):
            axis = 0
        elif self.conv in ('channels_first', 'th'):
            axis = 1

        # Compute the phase differential
        dphase = np.empty(phase.shape, dtype=phase.dtype)
        zero_idx = [slice(None)] * phase.ndim
        zero_idx[axis] = slice(1)
        else_idx = [slice(None)] * phase.ndim
        else_idx[axis] = slice(1, None)
        zero_idx = tuple(zero_idx)
        else_idx = tuple(else_idx)
        dphase[zero_idx] = phase[zero_idx]
        dphase[else_idx] = np.diff(np.unwrap(phase, axis=axis), axis=axis)
        return dphase


Instruction:
Can you generate the documentation for the following Python 3 function
def layers(self):
        '''Construct Keras input layers for the given transformer

        Returns
        -------
        layers : {field: keras.layers.Input}
            A dictionary of keras input layers, keyed by the corresponding
            field keys.
        '''
        from keras.layers import Input

        L = dict()
        for key in self.fields:
            L[key] = Input(name=key,
                           shape=self.fields[key].shape,
                           dtype=self.fields[key].dtype)

        return L

Response: Construct Keras input layers for the given transformer


Instruction:
Can you tell what is the following Python 3 function doing
def n_frames(self, duration):
        '''Get the number of frames for a given duration

        Parameters
        ----------
        duration : number >= 0
            The duration, in seconds

        Returns
        -------
        n_frames : int >= 0
            The number of frames at this extractor's sampling rate and
            hop length
        '''

        return int(time_to_frames(duration, sr=self.sr,
                                  hop_length=self.hop_length))

Response: Get the number of frames for a given duration


Instruction:
Can you generate a brief explanation for the following Python 3 code
def start(component, exact):
    # type: (str, str) -> None
    """ Create a new release branch.

    Args:
        component (str):
            Version component to bump when creating the release. Can be *major*,
            *minor* or *patch*.
        exact (str):
            The exact version to set for the release. Overrides the component
            argument. This allows to re-release a version if something went
            wrong with the release upload.
    """
    version_file = conf.get_path('version_file', 'VERSION')

    develop = conf.get('git.devel_branch', 'develop')
    common.assert_on_branch(develop)

    with conf.within_proj_dir():
        out = shell.run('git status --porcelain', capture=True).stdout
        lines = out.split(os.linesep)
        has_changes = any(
            not l.startswith('??') for l in lines if l.strip()
        )

    if has_changes:
        log.info("Cannot release: there are uncommitted changes")
        exit(1)

    old_ver, new_ver = versioning.bump(component, exact)

    log.info("Bumping package version")
    log.info("  old version: <35>{}".format(old_ver))
    log.info("  new version: <35>{}".format(new_ver))

    with conf.within_proj_dir():
        branch = 'release/' + new_ver

        common.git_checkout(branch, create=True)

        log.info("Creating commit for the release")
        shell.run('git add {ver_file} && git commit -m "{msg}"'.format(
            ver_file=version_file,
            msg="Releasing v{}".format(new_ver)
        ))

Response: Create a new release branch.


Instruction:
Implement a Python 3 function for
tagging the current commit with the current version.

Response:
def tag(message):
    # type: () -> None
    """ Tag the current commit with the current version. """
    release_ver = versioning.current()
    message = message or 'v{} release'.format(release_ver)

    with conf.within_proj_dir():
        log.info("Creating release tag")
        git.tag(
            author=git.latest_commit().author,
            name='v{}'.format(release_ver),
            message=message,
        )


Instruction:
Implement a function in Python 3 to
lint python files. Args: exclude (list[str]): A list of glob string patterns to test against. If the file/path matches any of those patters, it will be filtered out. skip_untracked (bool): If set to **True** it will skip all files not tracked by git. commit_only (bool): Only lint files that are staged for commit.

Response:
def lint(exclude, skip_untracked, commit_only):
    # type: (List[str], bool, bool) -> None
    """ Lint python files.

    Args:
        exclude (list[str]):
            A list of glob string patterns to test against. If the file/path
            matches any of those patters, it will be filtered out.
        skip_untracked (bool):
            If set to **True** it will skip all files not tracked by git.
        commit_only (bool):
            Only lint files that are staged for commit.
    """
    exclude = list(exclude) + conf.get('lint.exclude', [])
    runner = LintRunner(exclude, skip_untracked, commit_only)

    if not runner.run():
        exit(1)


Instruction:
Can you generate the documentation for the following Python 3 function
def pep8_check(files):
    # type: (List[str]) -> int
    """ Run code checks using pep8.

    Args:
        files (list[str]):
            A list of files to check

    Returns:
        bool: **True** if all files passed the checks, **False** otherwise.

    pep8 tool is **very** fast. Especially compared to pylint and the bigger the
    code base the bigger the difference. If you want to reduce check times you
    might disable all pep8 checks in pylint and use pep8 for that. This way you
    use pylint only for the more advanced checks (the number of checks enabled
    in pylint will make a visible difference in it's run times).
    """
    files = fs.wrap_paths(files)
    cfg_path = conf.get_path('lint.pep8_cfg', 'ops/tools/pep8.ini')
    pep8_cmd = 'pep8 --config {} {}'.format(cfg_path, files)

    return shell.run(pep8_cmd, exit_on_error=False).return_code

Response: Run code checks using pep8.


Instruction:
Write a Python 3 function for
running code checks using pylint.

Response:
def pylint_check(files):
    # type: (List[str]) -> int
    """ Run code checks using pylint.

    Args:
        files (list[str]):
            A list of files to check

    Returns:
        bool: **True** if all files passed the checks, **False** otherwise.
    """
    files = fs.wrap_paths(files)
    cfg_path = conf.get_path('lint.pylint_cfg', 'ops/tools/pylint.ini')
    pylint_cmd = 'pylint --rcfile {} {}'.format(cfg_path, files)

    return shell.run(pylint_cmd, exit_on_error=False).return_code


Instruction:
Can you write a function in Python 3 where it
runs all linters and report results.

Response:
def run(self):
        # type: () -> bool
        """ Run all linters and report results.

        Returns:
            bool: **True** if all checks were successful, **False** otherwise.
        """
        with util.timed_block() as t:
            files = self._collect_files()

        log.info("Collected <33>{} <32>files in <33>{}s".format(
            len(files), t.elapsed_s
        ))
        if self.verbose:
            for p in files:
                log.info("  <0>{}", p)

        # No files to lint - return success if empty runs are allowed.
        if not files:
            return self.allow_empty

        with util.timed_block() as t:
            results = self._run_checks(files)

        log.info("Code checked in <33>{}s", t.elapsed_s)

        success = True
        for name, retcodes in results.items():
            if any(x != 0 for x in retcodes):
                success = False
                log.err("<35>{} <31>failed with: <33>{}".format(
                    name, retcodes
                ))

        return success


Instruction:
Can you generate the documentation for the following Python 3 function
def send_to_azure(instance, data, replace=True, types=None, primary_key=(), sub_commit=True):

    # Time initialization
    start = datetime.datetime.now()

    # Extract info
    rows = data["rows"]
    if not rows:
        return 0
    table_name = data["table_name"]
    columns_name = data["columns_name"]
    total_len_data = len(rows)

    # Create table if needed
    if not existing_test(instance, table_name) or (types is not None) or (primary_key != ()):
        create.create_table(instance, data, primary_key, types)

    # Clean table if needed
    if replace:
        cleaning_function(instance, table_name)

    connection_kwargs = credential(instance)

    # Create an SSH tunnel
    ssh_host = os.environ.get("SSH_%s_HOST" % instance)
    ssh_user = os.environ.get("SSH_%s_USER" % instance)
    ssh_path_private_key = os.environ.get("SSH_%s_PATH_PRIVATE_KEY" % instance)
    if ssh_host:
        tunnel = SSHTunnelForwarder(
            (ssh_host, 22),
            ssh_username=ssh_user,
            ssh_private_key=ssh_path_private_key,
            remote_bind_address=(
                os.environ.get("AZURE_%s_HOST" % instance), int(os.environ.get("AZURE_%s_PORT" % instance))),
            local_bind_address=('localhost', 1433),  # could be any available port
        )
        # Start the tunnel
        try:
            tunnel.start()
            print("Tunnel opened!")
        except sshtunnel.HandlerSSHTunnelForwarderError:
            pass

        connection_kwargs["host"] = "localhost,1433"
        connection_kwargs["port"] = 1433

    cnxn = pyodbc.connect(**connection_kwargs)
    cursor = cnxn.cursor()

    small_batch_size = int(2099 / len(columns_name))

    print("Initiate send_to_azure...")

    # Initialize counters
    boolean = True
    question_mark_pattern = "(%s)" % ",".join(["?" for i in range(len(rows[0]))])
    counter = 0
    while boolean:
        temp_row = []
        question_mark_list = []
        for i in range(small_batch_size):
            if rows:
                temp_row.append(rows.pop())
                question_mark_list.append(question_mark_pattern)
            else:
                boolean = False
                continue
        counter = counter + len(temp_row)
        # percent = round(float(counter * 100) / total_len_data)
        if sub_commit:
            suffix = "%% rows sent"
            print_progress_bar(counter, total_len_data, suffix=suffix)
            # print("%s %% rows sent" % str(percent))
        else:
            suffix = "% rows prepared to be sent"
            print_progress_bar(counter, total_len_data, suffix=suffix)
            # print("%s %% rows prepared to be sent" % str(percent))
        data_values_str = ','.join(question_mark_list)
        columns_name_str = ", ".join(columns_name)
        inserting_request = '''INSERT INTO %s (%s) VALUES %s ;''' % (table_name, columns_name_str, data_values_str)

        final_data = [y for x in temp_row for y in x]
        if final_data:
            cursor.execute(inserting_request, final_data)

        if sub_commit:
            commit_function(cnxn)
    if not sub_commit:
        commit_function(cnxn)
    cursor.close()
    cnxn.close()

    if ssh_host:
        tunnel.close()
        print("Tunnel closed!")

    print("data sent to azure")
    print("Total rows: %s" % str(total_len_data))
    print(C.BOLD + "Total time in seconds : %s" % str((datetime.datetime.now() - start).seconds) + C.ENDC)
    return 0

Response: Send data to the Azure Cloud


Instruction:
Can you implement a function in Python 3 that
returns a standard python Dict with computed values from the DynDict

Response:
def getdict(source):
    """Returns a standard python Dict with computed values
    from the DynDict
    :param source: (DynDict) input
    :return: (dict) Containing computed values
    """
    std_dict = {}
    for var, val in source.iteritems():
        std_dict[var] = source[var]
    return std_dict


Instruction:
Explain what the following Python 3 code does
def enrich_app(self, name, value):
        '''
        Add a new property to the app (with setattr)

        Args:
            name (str): the name of the new property
            value (any): the value of the new property
        '''
        #Method shouldn't be added:  https://stackoverflow.com/a/28060251/3042398
        if type(value) == type(self.enrich_app):
            raise ValueError("enrich_app can't add method")

        setattr(self.app, name, value)

Response: Add a new property to the app with setattr


Instruction:
Implement a Python 3 function for
returning a new node with the least squares linear fit.

Response:
def linfit(x_true, y, sigmay=None, relsigma=True, cov=False, chisq=False, residuals=False):
    """
    Least squares linear fit.

    Fit a straight line `f(x_true) = a + bx` to points `(x_true, y)`.  Returns
    coefficients `a` and `b` that minimize the squared error.

    Parameters
    ----------
    x_true : array_like
        one dimensional array of `x_true` data with `n`>2 data points.
    y : array_like
        one dimensional array of `y` data with `n`>2 data points.
    sigmay : NoneType or float or array_like, optional
        one dimensional array of uncertainties (errors) in `y` data or a single
        positive number if all uncertainties are the same.  `sigmay` determines
        the weighting in the least squares minimization. Leaving `sigmay=None`
        uses no weighting and is equivalent to `sigmay=1`.
    relsigma : bool, optional
        If `relsigma` is True, the residuals are used to scale the covariance
        matrix.  Use this option if you do not know the absolute uncertainties
        (`sigmay`) in the data but still want a covariance matrix whose entries
        give meaningful estimates of the uncertainties in the fitting parameters
        `a` and `b` (from `f = a + bx`).  If `relsigma` is False, the covariance
        matrix is calculated (provided `cov` = True) using sigmay assuming
        sigmay represents absolute undertainties.
    cov : bool, optional
        If True, calculate and return the 2x2 covarience matrix of the fitting
        parameters.
    chisq : bool, optional
        If True, calculate and return redchisq.
    residuals : bool, optional
        If True, calculate and return residuals.

    Returns
    -------
    fit : array([a,b]) ndarray of floats
        The best fit model parameters `a` (the slope) and `b` (the
        `y`-intercept) for the input data arrays `x_true` and `y`

    cvm : array, shape (2,2) : returned only if cov=True
        Covarience matrix of the fitting parameters.  Diagonal elements are
        estimated variances of the fitting parameters a and b; square roots of
        the diagonal elements thus provide estimates of the uncertainties in the
        fitting parameters `a` and `b`. Off diagonal elements (equal to each
        other) are the covarience between the fitting parameters `a` and `b`.

    redchisq : float : returned only if chisq=True
        Reduced chi-squared goodness of fit parameter.

    residuals : ndarray of floats : returned only if residuals=True
        Length n array of the differences `y-(ax+b)` between `y`-data and the
        fitted data `ax + b`.

    Raises
    ------
    TypeError : if `x_true` and `y` have different lengths
    TypeError : If `x_true` and `y` have 2 or fewer elements
    TypeError : If `sigmay` length is not 1 or the same as `y`

    See Also
    --------
    polyfit : Least squares fit to polynomial.
    linalg.lstsq : Least-squares solution to a linear matrix equation.

    Notes
    -----
    By default, ``linfit`` returns optimal fitting parameters `a` and `b` without
    weighting of the data.  In that case, linfit minimizes the squared error

    .. math ::
        E = \\sum_{i=0}^n [y_i - (a x_i + b)]^2

    If `sigmay` is set equal to the uncertainties in the `y` data points, then
    linfit minimizes the `chi-squared` sum

    .. math ::
        \chi^2 = \\sum_{i=0}^n \\left[ \\frac{y_i-(a x_i + b)}{\\sigma_i} \\right]^2

    where :math:`\sigma_i` is given by `sigmay`, the "error" or standard
    deviation of :math:`y_i`.  `sigmay` can be either a single number that gives the
    uncertainty for all elements of `y`, or it can be an array of the same
    length as `y` that gives the "error" for each element of `y`.
    `redchisq` is :math:`\chi^2/(n-2)` where :math:`n` is the number of data
    points (the length of `x_true` or `y`).

    If `relsigma` is False, then the uncertainties `sigmay` in `y` are
    assumed to be the absolute one-standard-deviation uncertainties in `y`.
    In this case, the reduced chi-squared value :math:`\chi^2/(n-2)` provides a
    measure of the goodness of the fit.  If it is near 1, then the linear
    fitting model is considered to be good and the values of the covariance
    matrix are appropriately scaled.  In particular, the square root of the
    diagonal elements of the covariance matrix give the estimated uncertainty
    in the fitting parameters `a` and `b`.  See Refernece [2] below for more
    information.

    If `relsigma` is True, then the uncertainties `sigmay` in `y` are
    considered to be only relative uncertainties.  They are used to weight
    the data for the fit, but in this case, the covariance matrix is rescaled
    using the residuals between the fit and the data.  In this case, the reduced
    chi-squared value :math:`\chi^2/(n-2)` does not provide a measure of the
    goodness of the fit.  Nevertheless, the diagonal elements of the rescaled
    covariance matrix (returned by linfit) give the estimated uncertainty in the
    fitting parameters `a` and `b`.

    The covariance matrix is a 2x2 symmetric matrix where the diagonal elements
    are the variance of the fitting parameters.  Their square roots provide
    estimates of the uncertainties in the fitting parameters.  The off-diagonal
    elements are equal and give the cross correlation between the two fitting
    parameters `a` and `b`.

    linfit runs faster, by a factor of 2 to 3, if calculation of the residuals
    is suppressed letting `cov`, `chisq`, and `residuals` remain False (the
    default setting).

    Fitting a straight line to a single set of `(x_true, y)` data using ``linfit`` is
    typically 2 to 10 times faster than using either ``polyfit`` or
    ``linalg.lstsq``, especially when weighting is used and for very large data
    sets.

    References
    ----------
    .. [1] An Introduction to Error Analysis, 2nd Ed. by John R. Taylor
           (University Science Books, 1997)
    .. [2] Numerical Recipes, The Art of Scientific Computing, 3rd Edition
           by W.H. Press, S. A. Teukolsky, W. T. Vetterling, & B. P. Flannery
           (Cambridge University Press, 2007)

    Examples
    --------
    Fit a line, `y = ax + b`, through some noisy `(x_true, y)` data-points without
    any weighting (`sigmay` = None) to obtain fitting parameters `a` and `b`:

    >>> x_true = np.array([0, 1, 2, 3])
    >>> y = np.array([-1, 0.2, 0.9, 2.1])
    >>> fit = linfit(x_true, y)
    >>> print("a = {0:0.2f}, b = {1:0.2f}".format(fit[0], fit[1]))
    a = 1.00, b = -0.95

    Setting `cov` = True in the input, returns the covariance matrix `cvm`.
    When uncertainties `sigmay` are left unspecified, meaningful estimates of
    the uncertainties `da` and `db` in the fitting parameters `a` and `b`
    are given by the square roots of the diagonals of the covariance matrix
    `cvm`, provided `relsigma` = True (the default state).

    >>> fit, cvm = linfit(x_true, y, cov=True)
    >>> dfit = [np.sqrt(cvm[i,i]) for i in range(2)]
    >>> print("da = {0:0.2f}, db = {1:0.2f}".format(dfit[0], dfit[1]))
    da = 0.07, db = 0.13

    A better practice is to supply estimates of the uncertainties in the
    input argument `sigmay`.  `sigmay` can be a single float, if the
    uncertainties are the same for all data points, or it can be an array, if
    the uncertainties for different data points are different.  Here we
    enter sigmay as an array.

    >>> dy = np.array([0.18, 0.13, 0.15, 0.17])
    >>> fit, cvm, redchisq, resids = linfit(x_true, y, cov=True, sigmay=dy, relsigma=False, chisq=True, residuals=True)
    >>> print("a = {0:0.2f}, b = {1:0.2f}".format(fit[0], fit[1]))
    a = 0.98, b = -0.91
    >>> dfit = [np.sqrt(cvm[i,i]) for i in range(2)]
    >>> print("da = {0:0.2f}, db = {1:0.2f}".format(dfit[0], dfit[1]))
    da = 0.08, db = 0.14
    >>> print("reduced chi-squared = {0:0.2f}".format(redchisq))
    reduced chi-squared = 1.21
    >>> print(resids)
    [-0.08856653  0.12781099 -0.1558115   0.06056602]

    The value of reduced chi-squared `redchisq` is 1.21 indicating that a
    linear model is valid for these data.  The residuals :math:`y_i - (a+bx_i)`
    are given by the output `resids`.

    If absolute estimates of the uncertainties are not available, but relative
    estimates of the uncertainties are known, a fit can be obtained with
    reasonable estimates of the uncertainties in the fitting parameters by
    setting `relsigma` = True.

    >>> dy = np.array([1.0, 0.75, 0.75, 1.25])
    >>> fit, cvm, redchisq = linfit(x_true, y, cov=True, sigmay=dy, relsigma=True, chisq=True)
    >>> print("a = {0:0.2f}, b = {1:0.2f}".format(fit[0], fit[1]))
    a = 0.97, b = -0.91
    >>> dfit = [np.sqrt(cvm[i,i]) for i in range(2)]
    >>> print("da = {0:0.2f}, db = {1:0.2f}".format(dfit[0], dfit[1]))
    da = 0.09, db = 0.16
    >>> print("reduced chi-squared = {0:0.2f}".format(redchisq))
    reduced chi-squared = 0.04

    In this case, the value `redchisq` is meaningless, because only the
    relative, rather than the absolute uncertainties are known.  Nevertheless,
    by setting `relsigma` = True, reasonable estimates for the uncertainties
    in the fitting parameters are obtained.

    Illustration:

    .. image:: example.png
        :scale: 75 %
    """

    x_true = asarray(x_true)
    y = asarray(y)
    if x_true.size != y.size:
        raise TypeError('Expected x_true and y to have same length')
    if x_true.size <= 2:
        raise TypeError('Expected x_true and y length > 2')
    if sigmay is None: sigmay = 1.0
    sigmay = asarray(sigmay)

    if sigmay.size == 1:
        sigy = float(sigmay)    # convert 0-d array to a float
        wt = 1./(sigy*sigy)
        s = wt * y.size
        sx = wt * x_true.sum()
        sy = wt * y.sum()
        t = x_true-sx/s
        stt = wt * (t*t).sum()
        slope = wt * (t*y).sum()/stt
        yint = (sy - sx * slope)/s
    else:
        if sigmay.size != y.size:
            raise TypeError('Expected sigmay size to be 1 or same as y')
        wt = 1./(sigmay*sigmay)
        s = wt.sum()
        sx = (x_true*wt).sum()
        sy = (y*wt).sum()
        t = (x_true-sx/s)/sigmay
        stt = (t*t).sum()
        slope = (t*y/sigmay).sum()/stt
        yint = (sy - sx * slope)/s
    returns = array([slope, yint])

    if cov is True:
        cvm00 = 1./stt
        cvm01 = -sx/(s*stt)
        cvm11 = (1.0-sx*cvm01)/s
        if relsigma is True:
            redchisq, resids = _resids(x_true, y, sigmay, slope, yint)
            cvm00 *= redchisq
            cvm01 *= redchisq
            cvm11 *= redchisq
        returns = [returns] + [array([[cvm00, cvm01],
                                         [cvm01, cvm11]])]

    if residuals or chisq is True:
        if relsigma is False:
            redchisq, resids = _resids(x_true, y, sigmay, slope, yint)
        if type(returns) is not list:
            returns = [returns]
        if chisq is True:
            returns += [redchisq]
        if residuals is True:
            returns += [resids]

    return returns


Instruction:
Can you tell what is the following Python 3 function doing
def set_base_url(self, platform: str = "prod"):
        platform = platform.lower()
        self.platform = platform
        if platform == "prod":
            ssl = True
            logging.debug("Using production platform.")
        elif platform == "qa":
            ssl = False
            logging.debug("Using Quality Assurance platform (reduced perfs).")
        else:
            logging.error(
                "Platform must be one of: {}".format(" | ".join(self.API_URLS.keys()))
            )
            raise ValueError(
                3,
                "Platform must be one of: {}".format(" | ".join(self.API_URLS.keys())),
            )
        # method ending
        return (
            platform.lower(),
            self.API_URLS.get(platform),
            self.APP_URLS.get(platform),
            self.CSW_URLS.get(platform),
            self.MNG_URLS.get(platform),
            self.OC_URLS.get(platform),
            ssl,
        )

Response: Set Isogeo base URL according to platform.


Instruction:
Write a Python 3 function that can
convert a UUID to its URI equivalent.

Response:
def convert_uuid(self, in_uuid: str = str, mode: bool = 0):
        """Convert a metadata UUID to its URI equivalent. And conversely.

        :param str in_uuid: UUID or URI to convert
        :param int mode: conversion direction. Options:

          * 0 to HEX
          * 1 to URN (RFC4122)
          * 2 to URN (Isogeo specific style)

        """
        # parameters check
        if not isinstance(in_uuid, str):
            raise TypeError("'in_uuid' expected a str value.")
        else:
            pass
        if not checker.check_is_uuid(in_uuid):
            raise ValueError("{} is not a correct UUID".format(in_uuid))
        else:
            pass
        if not isinstance(mode, int):
            raise TypeError("'mode' expects an integer value")
        else:
            pass
        # handle Isogeo specific UUID in XML exports
        if "isogeo:metadata" in in_uuid:
            in_uuid = "urn:uuid:{}".format(in_uuid.split(":")[-1])
            logging.debug("Isogeo UUUID URN spotted: {}".format(in_uuid))
        else:
            pass
        # operate
        if mode == 0:
            return uuid.UUID(in_uuid).hex
        elif mode == 1:
            return uuid.UUID(in_uuid).urn
        elif mode == 2:
            urn = uuid.UUID(in_uuid).urn
            return "urn:isogeo:metadata:uuid:{}".format(urn.split(":")[2])
        else:
            raise ValueError("'mode' must be  one of: 0 | 1 | 2")


Instruction:
Implement a function in Python 3 to
pull out the character set, encoding, and encoded text from the input encoded words. Next, it decodes the encoded words into a byte string, using either the quopri module or base64 module as determined by the encoding. Finally, it decodes the byte string using the character set and returns the result. See: - https://github.com/isogeo/isogeo-api-py-minsdk/issues/32 - https://dmorgan.info/posts/encoded-word-syntax/ :param str in_encoded_words: base64 or quori encoded character string.

Response:
def encoded_words_to_text(self, in_encoded_words: str):
        """Pull out the character set, encoding, and encoded text from the input
        encoded words. Next, it decodes the encoded words into a byte string,
        using either the quopri module or base64 module as determined by the
        encoding. Finally, it decodes the byte string using the
        character set and returns the result.

        See:

        - https://github.com/isogeo/isogeo-api-py-minsdk/issues/32
        - https://dmorgan.info/posts/encoded-word-syntax/

        :param str in_encoded_words: base64 or quori encoded character string.
        """
        # handle RFC2047 quoting
        if '"' in in_encoded_words:
            in_encoded_words = in_encoded_words.strip('"')
        # regex
        encoded_word_regex = r"=\?{1}(.+)\?{1}([B|Q])\?{1}(.+)\?{1}="
        # pull out
        try:
            charset, encoding, encoded_text = re.match(
                encoded_word_regex, in_encoded_words
            ).groups()
        except AttributeError:
            logging.debug("Input text was not encoded into base64 or quori")
            return in_encoded_words

        # decode depending on encoding
        if encoding == "B":
            byte_string = base64.b64decode(encoded_text)
        elif encoding == "Q":
            byte_string = quopri.decodestring(encoded_text)
        return byte_string.decode(charset)


Instruction:
Write a Python 3 script for
getting Isogeo components versions. Authentication not required.

Response:
def get_isogeo_version(self, component: str = "api", prot: str = "https"):
        """Get Isogeo components versions. Authentication not required.

        :param str component: which platform component. Options:

          * api [default]
          * db
          * app

        """
        # which component
        if component == "api":
            version_url = "{}://v1.{}.isogeo.com/about".format(prot, self.api_url)
        elif component == "db":
            version_url = "{}://v1.{}.isogeo.com/about/database".format(
                prot, self.api_url
            )
        elif component == "app" and self.platform == "prod":
            version_url = "https://app.isogeo.com/about"
        elif component == "app" and self.platform == "qa":
            version_url = "https://qa-isogeo-app.azurewebsites.net/about"
        else:
            raise ValueError(
                "Component value must be one of: " "api [default], db, app."
            )

        # send request
        version_req = requests.get(version_url, proxies=self.proxies, verify=self.ssl)

        # checking response
        checker.check_api_response(version_req)

        # end of method
        return version_req.json().get("version")


Instruction:
Make a summary of the following Python 3 code
def get_edit_url(
        self,
        md_id: str = None,
        md_type: str = None,
        owner_id: str = None,
        tab: str = "identification",
    ):
        """Constructs the edition URL of a metadata.

        :param str md_id: metadata/resource UUID
        :param str owner_id: owner UUID
        :param str tab: target tab in the web form
        """
        # checks inputs
        if not checker.check_is_uuid(md_id) or not checker.check_is_uuid(owner_id):
            raise ValueError("One of md_id or owner_id is not a correct UUID.")
        else:
            pass
        if checker.check_edit_tab(tab, md_type=md_type):
            pass
        # construct URL
        return (
            "{}"
            "/groups/{}"
            "/resources/{}"
            "/{}".format(self.APP_URLS.get(self.platform), owner_id, md_id, tab)
        )

Response: Constructs the edition URL of a metadata.


Instruction:
Can you implement a function in Python 3 that
constructs the view URL of a metadata.

Response:
def get_view_url(self, webapp: str = "oc", **kwargs):
        """Constructs the view URL of a metadata.

        :param str webapp: web app destination
        :param dict kwargs: web app specific parameters. For example see WEBAPPS
        """
        # build wbeapp URL depending on choosen webapp
        if webapp in self.WEBAPPS:
            webapp_args = self.WEBAPPS.get(webapp).get("args")
            # check kwargs parameters
            if set(webapp_args) <= set(kwargs):
                # construct and return url
                url = self.WEBAPPS.get(webapp).get("url")

                return url.format(**kwargs)
            else:
                raise TypeError(
                    "'{}' webapp expects {} argument(s): {}."
                    " Args passed: {}".format(
                        webapp, len(webapp_args), webapp_args, kwargs
                    )
                )
        else:
            raise ValueError(
                "'{}' is not a recognized webapp among: {}."
                " Try to register it.".format(self.WEBAPPS.keys(), webapp)
            )


Instruction:
Can you generate the documentation for the following Python 3 function
def register_webapp(self, webapp_name: str, webapp_args: list, webapp_url: str):
        # check parameters
        for arg in webapp_args:
            if arg not in webapp_url:
                raise ValueError(
                    "Inconsistent web app arguments and URL."
                    " It should contain arguments to replace"
                    " dynamically. Example: 'http://webapp.com"
                    "/isogeo?metadata={md_id}'"
                )
        # register
        self.WEBAPPS[webapp_name] = {"args": webapp_args, "url": webapp_url}

Response: Register a new WEBAPP to use with the view URL builder.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_url_base_from_url_token(
        self, url_api_token: str = "https://id.api.isogeo.com/oauth/token"
    ):
        """Returns the Isogeo API root URL (which is not included into
        credentials file) from the token URL (which is always included).

        :param url_api_token str: url to Isogeo API ID token generator
        """
        in_parsed = urlparse(url_api_token)
        api_url_base = in_parsed._replace(
            path="", netloc=in_parsed.netloc.replace("id.", "")
        )
        return api_url_base.geturl()

Response: Returns the Isogeo API root URL from the Isogeo API ID token generator.


Instruction:
Can you tell what is the following Python 3 function doing
def pages_counter(self, total: int, page_size: int = 100) -> int:
        if total <= page_size:
            count_pages = 1
        else:
            if (total % page_size) == 0:
                count_pages = total / page_size
            else:
                count_pages = (total / page_size) + 1
        # method ending
        return int(count_pages)

Response: Simple helper to handle pagination. Returns the number of pages for a specific number of results.


Instruction:
Can you generate the documentation for the following Python 3 function
def tags_to_dict(self, tags=dict, prev_query=dict, duplicated: str = "rename"):
        # for rename option, get workgroups
        if duplicated == "rename":
            wgs = {k.split(":")[1]: v for k, v in tags.items() if k.startswith("owner")}
            # wgs = list(filter(lambda x[1]: x[0].startswith("owner"), tags.items()))
        elif duplicated == "ignore" or duplicated == "merge":
            wgs = None
        else:
            raise ValueError(
                "Duplicated value is not an accepted value."
                " Please refer to __doc__ method."
            )

        # inner function
        def _duplicate_mng(
            target_dict: dict, duplicate, mode: str = duplicated, workgroups: dict = wgs
        ):
            if mode == "merge":
                target_dict[duplicate[0]] += "||" + duplicate[1]
            elif mode == "rename":
                # get workgroup uuid
                if checker.check_is_uuid(k.split(":")[1]):
                    k_uuid = k.split(":")[1]
                else:
                    k_uuid = k.split(":")[2]
                # match with workgroups owners
                if k_uuid in workgroups:
                    repl = workgroups.get(k_uuid)
                else:
                    repl = k_uuid[:5]
                target_dict["{} ({})".format(duplicate[0], repl)] = duplicate[1]
            else:
                pass
            return

        # -- SEARCH TAGS -------------
        # output dicts structure
        tags_as_dicts = {
            "actions": {},
            "catalogs": {},
            "contacts": {},
            "data-sources": {},
            "formats": {},
            "inspires": {},
            "keywords": {},
            "licenses": {},
            "owners": {},
            "providers": {},
            "shares": {},
            "srs": {},
            "types": {},
        }

        # parsing tags and storing each one in a dict
        for k, v in sorted(tags.items()):
            if k.startswith("action"):
                tags_as_dicts.get("actions")[v] = k
                continue
            elif k.startswith("catalog"):
                if v in tags_as_dicts.get("catalogs") and duplicated != "ignore":
                    _duplicate_mng(tags_as_dicts.get("catalogs"), (v, k))
                else:
                    logging.debug(
                        "Duplicated catalog name: {}. Last catalog is retained.".format(
                            v
                        )
                    )
                    tags_as_dicts.get("catalogs")[v] = k
                continue
            elif k.startswith("contact"):
                if v in tags_as_dicts.get("contacts") and duplicated != "ignore":
                    _duplicate_mng(tags_as_dicts.get("contacts"), (v, k))
                else:
                    logging.debug(
                        "Duplicated contact name: {}. Last contact is retained.".format(
                            v
                        )
                    )
                    tags_as_dicts.get("contacts")[v] = k
                continue
            elif k.startswith("coordinate-system"):
                tags_as_dicts.get("srs")[v] = k
                continue
            elif k.startswith("data-source"):
                if v in tags_as_dicts.get("data-sources") and duplicated != "ignore":
                    _duplicate_mng(tags_as_dicts.get("data-sources"), (v, k))
                else:
                    logging.debug(
                        "Duplicated data-source name: {}. Last data-source is retained.".format(
                            v
                        )
                    )
                    tags_as_dicts.get("data-sources")[v] = k
                continue
            elif k.startswith("format"):
                tags_as_dicts.get("formats")[v] = k
                continue
            elif k.startswith("keyword:in"):
                tags_as_dicts.get("inspires")[v] = k
                continue
            elif k.startswith("keyword:is"):
                tags_as_dicts.get("keywords")[v] = k
                continue
            elif k.startswith("license"):
                if v in tags_as_dicts.get("licenses") and duplicated != "ignore":
                    _duplicate_mng(tags_as_dicts.get("licenses"), (v, k))
                else:
                    logging.debug(
                        "Duplicated license name: {}. Last license is retained.".format(
                            v
                        )
                    )
                    tags_as_dicts.get("licenses")[v] = k
                continue
            elif k.startswith("owner"):
                tags_as_dicts.get("owners")[v] = k
                continue
            elif k.startswith("provider"):
                # providers are particular bcause its value is always null.
                tags_as_dicts.get("providers")[k.split(":")[1]] = k
                continue
            elif k.startswith("share"):
                tags_as_dicts.get("shares")[v] = k
                continue
            elif k.startswith("type"):
                tags_as_dicts.get("types")[v] = k
                continue
            # ignored tags
            else:
                logging.debug("A tag has been ignored during parsing: {}".format(k))

        # -- QUERY TAGS -------------
        # handle share case
        if prev_query.get("_shares"):
            prev_query.get("_tags").append(
                "share:{}".format(prev_query.get("_shares")[0])
            )
        else:
            pass
        # output dict struture
        logging.debug(prev_query)
        query_as_dicts = {
            "_tags": {
                "actions": {},
                "catalogs": {},
                "contacts": {},
                "data-sources": {},
                "formats": {},
                "inspires": {},
                "keywords": {},
                "licenses": {},
                "owners": {},
                "providers": {},
                "shares": {},
                "srs": {},
                "types": {},
            },
            "_shares": prev_query.get("_shares"),
            "_terms": prev_query.get("_terms"),
        }

        # parsing and matching tags
        query_tags = query_as_dicts.get("_tags")
        for t in prev_query.get("_tags"):
            if t.startswith("action"):
                query_tags.get("actions")[tags.get(t)] = t
                continue
            elif t.startswith("catalog"):
                if v in query_tags.get("catalogs") and duplicated != "ignore":
                    _duplicate_mng(query_tags.get("catalogs"), (v, k))
                else:
                    logging.debug(
                        "Duplicated catalog name: {}. Last catalog is retained.".format(
                            v
                        )
                    )
                    query_tags.get("catalogs")[tags.get(t)] = t
                continue
            elif t.startswith("contact"):
                if v in query_tags.get("contacts") and duplicated != "ignore":
                    _duplicate_mng(query_tags.get("contacts"), (v, k))
                else:
                    logging.debug(
                        "Duplicated contact name: {}. Last contact is retained.".format(
                            v
                        )
                    )
                    query_tags.get("contacts")[tags.get(t)] = t
                continue
            elif t.startswith("coordinate-system"):
                query_tags.get("srs")[tags.get(t)] = t
                continue
            elif t.startswith("data-source"):
                if v in query_tags.get("data-sources") and duplicated != "ignore":
                    _duplicate_mng(query_tags.get("data-sources"), (v, k))
                else:
                    logging.debug(
                        "Duplicated data-source name: {}. Last data-source is retained.".format(
                            v
                        )
                    )
                    query_tags.get("data-sources")[tags.get(t)] = t
                continue
            elif t.startswith("format"):
                query_tags.get("formats")[tags.get(t)] = t
                continue
            elif t.startswith("keyword:in"):
                query_tags.get("inspires")[tags.get(t)] = t
                continue
            elif t.startswith("keyword:is"):
                query_tags.get("keywords")[tags.get(t)] = t
                continue
            elif t.startswith("license"):
                if v in query_tags.get("licenses") and duplicated != "ignore":
                    _duplicate_mng(query_tags.get("licenses"), (v, k))
                else:
                    logging.debug(
                        "Duplicated license name: {}. Last license is retained.".format(
                            v
                        )
                    )
                    query_tags.get("licenses")[tags.get(t)] = t
                continue
            elif t.startswith("owner"):
                query_tags.get("owners")[tags.get(t)] = t
                continue
            elif t.startswith("provider"):
                # providers are particular bcause its value is always null.
                query_tags.get("providers")[k.split(":")[1]] = k
                continue
            elif t.startswith("share"):
                query_tags.get("shares")[tags.get(t)] = t
                continue
            elif t.startswith("type"):
                query_tags.get("types")[tags.get(t)] = t
                continue
            # ignored tags
            else:
                logging.debug(
                    "A query tag has been ignored during parsing: {}".format(t)
                )

        # return the output
        return tags_as_dicts, query_as_dicts

Response: Reverse search tags dictionary to values as keys. Used to populate filters comboboxes for example.


Instruction:
Given the following Python 3 function, write the documentation
def share_extender(self, share: dict, results_filtered: dict):
        # add share administration URL
        creator_id = share.get("_creator").get("_tag")[6:]
        share["admin_url"] = "{}/groups/{}/admin/shares/{}".format(
            self.app_url, creator_id, share.get("_id")
        )
        # check if OpenCatalog is activated
        opencat_url = "{}/s/{}/{}".format(
            self.oc_url, share.get("_id"), share.get("urlToken")
        )
        if requests.head(opencat_url):
            share["oc_url"] = opencat_url
        else:
            pass
        # add metadata ids list
        share["mds_ids"] = (i.get("_id") for i in results_filtered)

        return share

Response: Extend share model with additional informations.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def configure(username, password):
    # type: (str, str) -> None
    """
    Generate .pypirc config with the given credentials.

    Example:

        $ peltak pypi configure my_pypi_user my_pypi_pass

    """
    from peltak.extra.pypi import logic
    logic.gen_pypirc(username, password)

Response: Generate. pypirc config with the given credentials.


Instruction:
Can you tell what is the following Python 3 function doing
def tr(self, subdomain: str, string_to_translate: str = "") -> str:
        if subdomain not in self.translations.keys():
            raise ValueError(
                "'{}' is not a correct subdomain."
                " Must be one of {}".format(subdomain, self.translations.keys())
            )
        else:
            pass
        # translate
        str_translated = self.translations.get(
            subdomain, {"error": "Subdomain not found: {}".format(subdomain)}
        ).get(string_to_translate, "String not found")

        # end of method
        return str_translated

Response: Returns the string in the translation dictionary.


Instruction:
Given the following Python 3 function, write the documentation
def get_ec2_client(region_name=None, aws_access_key_id=None, aws_secret_access_key=None):
    log = logging.getLogger(mod_logger + '.get_ec2_client')
    # Connect to EC2 API
    try:
        client = boto3.client('ec2', region_name=region_name, aws_access_key_id=aws_access_key_id,
                              aws_secret_access_key=aws_secret_access_key)
    except ClientError:
        _, ex, trace = sys.exc_info()
        msg = 'There was a problem connecting to EC2, please check AWS CLI and boto configuration, ensure ' \
              'credentials and region are set appropriately.\n{e}'.format(e=str(ex))
        log.error(msg)
        raise AWSAPIError, msg, trace
    else:
        log.debug('Successfully created an EC2 client')
        return client

Response: Creates an EC2 client object


Instruction:
Can you write a function in Python 3 where it
gets the VPC ID for this instance

Response:
def get_vpc_id(self):
        """Gets the VPC ID for this EC2 instance

        :return: String instance ID or None
        """
        log = logging.getLogger(self.cls_logger + '.get_vpc_id')

        # Exit if not running on AWS
        if not self.is_aws:
            log.info('This machine is not running in AWS, exiting...')
            return

        if self.instance_id is None:
            log.error('Unable to get the Instance ID for this machine')
            return
        log.info('Found Instance ID: {i}'.format(i=self.instance_id))

        log.info('Querying AWS to get the VPC ID...')
        try:
            response = self.client.describe_instances(
                    DryRun=False,
                    InstanceIds=[self.instance_id])
        except ClientError as ex:
            log.error('Unable to query AWS to get info for instance {i}\n{e}'.format(
                    i=self.instance_id, e=ex))
            return

        # Get the VPC ID from the response
        try:
            vpc_id = response['Reservations'][0]['Instances'][0]['VpcId']
        except KeyError as ex:
            log.error('Unable to get VPC ID from response: {r}\n{e}'.format(r=response, e=ex))
            return
        log.info('Found VPC ID: {v}'.format(v=vpc_id))
        return vpc_id


Instruction:
Can you create a Python 3 function that
gives an interface number gets the elastic network interface ID associated with the interface.

Response:
def get_eni_id(self, interface=1):
        """Given an interface number, gets the AWS elastic network
        interface associated with the interface.

        :param interface: Integer associated to the interface/device number
        :return: String Elastic Network Interface ID or None if not found
        :raises OSError, AWSAPIError, EC2UtilError
        """
        log = logging.getLogger(self.cls_logger + '.get_eni_id')

        # Get the instance-id
        if self.instance_id is None:
            msg = 'Instance ID not found for this machine'
            log.error(msg)
            raise OSError(msg)
        log.info('Found instance ID: {i}'.format(i=self.instance_id))

        log.debug('Querying EC2 instances...')
        try:
            response = self.client.describe_instances(
                    DryRun=False,
                    InstanceIds=[self.instance_id]
            )
        except ClientError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to query EC2 for instances\n{e}'.format(e=str(ex))
            log.error(msg)
            raise AWSAPIError, msg, trace
        log.debug('Found instance info: {r}'.format(r=response))

        # Find the ENI ID
        log.info('Looking for the ENI ID to alias...')
        eni_id = None
        try:
            for reservation in response['Reservations']:
                for instance in reservation['Instances']:
                    if instance['InstanceId'] == self.instance_id:
                        for network_interface in instance['NetworkInterfaces']:
                            if network_interface['Attachment']['DeviceIndex'] == interface:
                                eni_id = network_interface['NetworkInterfaceId']
        except KeyError:
            _, ex, trace = sys.exc_info()
            msg = 'ENI ID not found in AWS response for interface: {i}'.format(i=interface)
            log.error(msg)
            raise EC2UtilError, msg, trace

        log.info('Found ENI ID: {e}'.format(e=eni_id))
        return eni_id


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_secondary_ip(self, ip_address, interface=1):
        log = logging.getLogger(self.cls_logger + '.add_secondary_ip')

        # Get the ENI ID
        eni_id = self.get_eni_id(interface)

        # Verify the ENI ID was found
        if eni_id is None:
            msg = 'Unable to find the corresponding ENI ID for interface: {i}'. \
                format(i=interface)
            log.error(msg)
            raise EC2UtilError(msg)
        else:
            log.info('Found ENI ID: {e}'.format(e=eni_id))

        # Assign the secondary IP address
        log.info('Attempting to assign the secondary IP address...')
        try:
            self.client.assign_private_ip_addresses(
                    NetworkInterfaceId=eni_id,
                    PrivateIpAddresses=[
                        ip_address,
                    ],
                    AllowReassignment=True
            )
        except ClientError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to assign secondary IP address\n{e}'.format(e=str(ex))
            log.error(msg)
            raise AWSAPIError, msg, trace
        log.info('Successfully added secondary IP address {s} to ENI ID {e} on interface {i}'.format(
                s=ip_address, e=eni_id, i=interface))

Response: Adds an IP address as a secondary IP address.


Instruction:
Can you tell what is the following Python 3 function doing
def associate_elastic_ip(self, allocation_id, interface=1, private_ip=None):
        log = logging.getLogger(self.cls_logger + '.associate_elastic_ip')

        if private_ip is None:
            log.info('No private IP address provided, getting the primary IP'
                     'address on interface {i}...'.format(i=interface))
            private_ip = get_ip_addresses()['eth{i}'.format(i=interface)]

        log.info('Associating Elastic IP {e} on interface {i} on IP {p}'.format(
                e=allocation_id, i=interface, p=private_ip))

        # Get the ENI ID
        log.info('Getting the ENI ID for interface: {i}'.format(i=interface))
        eni_id = self.get_eni_id(interface)

        # Verify the ENI ID was found
        if eni_id is None:
            msg = 'Unable to find the corresponding ENI ID for interface: {i}'. \
                format(i=interface)
            log.error(msg)
            raise OSError(msg)
        else:
            log.info('Found ENI ID: {e}'.format(e=eni_id))

        # Assign the secondary IP address
        log.info('Attempting to assign the secondary IP address...')
        try:
            response = self.client.associate_address(
                    NetworkInterfaceId=eni_id,
                    AllowReassociation=True,
                    AllocationId=allocation_id,
                    PrivateIpAddress=private_ip
            )
        except ClientError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to attach elastic IP address {a} to interface {i}\n{e}'.format(
                    a=allocation_id, i=interface, e=str(ex))
            log.error(msg)
            raise AWSAPIError, msg, trace

        code = response['ResponseMetadata']['HTTPStatusCode']
        if code != 200:
            msg = 'associate_address returned invalid code: {c}'.format(c=code)
            log.error(msg)
            raise AWSAPIError(msg)
        log.info('Successfully associated elastic IP address ID {a} to interface {i} on ENI ID {e}'.format(
                a=allocation_id, i=interface, e=eni_id))

Response: This function associates an elastic IP to the interface number on this host.


Instruction:
Write a Python 3 function for
allocating an elastic IP address and returns the allocation ID and Public IP that were created.

Response:
def allocate_elastic_ip(self):
        """Allocates an elastic IP address

        :return: Dict with allocation ID and Public IP that were created
        :raises: AWSAPIError, EC2UtilError
        """
        log = logging.getLogger(self.cls_logger + '.allocate_elastic_ip')

        # Attempt to allocate a new elastic IP
        log.info('Attempting to allocate an elastic IP...')
        try:
            response = self.client.allocate_address(
                    DryRun=False,
                    Domain='vpc'
            )
        except ClientError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to allocate a new elastic IP address\n{e}'.format(e=str(ex))
            log.error(msg)
            raise AWSAPIError, msg, trace

        allocation_id = response['AllocationId']
        public_ip = response['PublicIp']
        log.info('Allocated Elastic IP with ID {a} and Public IP address {p}'.
                 format(a=allocation_id, p=public_ip))

        # Verify the Address was allocated successfully
        log.info('Verifying the elastic IP address was allocated and is available '
                 'for use...')
        ready = False
        verification_timer = [2]*60 + [5]*60 + [10]*18
        num_checks = len(verification_timer)
        for i in range(0, num_checks):
            wait_time = verification_timer[i]
            try:
                self.client.describe_addresses(
                        DryRun=False,
                        AllocationIds=[allocation_id]
                )
            except ClientError:
                _, ex, trace = sys.exc_info()
                log.info('Elastic IP address {p} with Allocation ID {a} is not available for use, trying again in '
                         '{w} sec...\n{e}'.format(p=public_ip, a=allocation_id, w=wait_time, e=str(ex)))
                time.sleep(wait_time)
            else:
                log.info('Elastic IP {p} with Allocation ID {a} is available for use'.format(
                    p=public_ip, a=allocation_id))
                ready = True
                break
        if ready:
            return {'AllocationId': allocation_id, 'PublicIp': public_ip}
        else:
            msg = 'Unable to verify existence of new Elastic IP {p} with Allocation ID: {a}'. \
                format(p=public_ip, a=allocation_id)
            log.error(msg)
            raise EC2UtilError(msg)


Instruction:
Can you tell what is the following Python 3 function doing
def attach_new_eni(self, subnet_name, security_group_ids, device_index=2, allocation_id=None, description=''):
        log = logging.getLogger(self.cls_logger + '.attach_new_eni')
        log.info('Attempting to attach a new network interface to this instance...')

        # Validate args
        if not isinstance(security_group_ids, list):
            msg = 'security_group_name argument is not a string'
            log.error(msg)
            raise EC2UtilError(msg)
        if not isinstance(subnet_name, basestring):
            msg = 'subnet_name argument is not a string'
            log.error(msg)
            raise EC2UtilError(msg)
        if allocation_id is not None:
            if not isinstance(allocation_id, basestring):
                msg = 'allocation_id argument is not a string'
                log.error(msg)
                raise EC2UtilError(msg)
        try:
            device_index = int(device_index)
        except ValueError:
            _, ex, trace = sys.exc_info()
            msg = 'device_index argument is not an int\n{e}'.format(e=str(ex))
            log.error(msg)
            raise EC2UtilError, msg, trace

        # Get the instance ID and VPC ID for this machine
        if self.instance_id is None or self.vpc_id is None:
            msg = 'Unable to obtain instance ID or VPC ID'
            log.error(msg)
            raise EC2UtilError(msg)

        # Get the subnet ID by name
        log.info('Looking up the subnet ID by name: {n}'.format(n=subnet_name))
        filters = [
            {'Name': 'vpc-id', 'Values': [self.vpc_id]},
            {'Name': 'tag-key', 'Values': ['Name']},
            {'Name': 'tag-value', 'Values': [subnet_name]}]
        try:
            response = self.client.describe_subnets(
                    DryRun=False,
                    Filters=filters
            )
        except ClientError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to find subnet by name {n} in VPC {v}\n{e}'.format(n=subnet_name, v=self.vpc_id, e=str(ex))
            log.error(msg)
            raise EC2UtilError, msg, trace

        if len(response['Subnets']) < 1:
            msg = 'No subnets found with name {n} in VPC {v}'.format(n=subnet_name, v=self.vpc_id)
            log.error(msg)
            raise EC2UtilError(msg)
        elif len(response['Subnets']) > 1:
            msg = 'More than 1 subnet found in VPC {v} with name {n}'.format(n=subnet_name, v=self.vpc_id)
            log.error(msg)
            raise EC2UtilError(msg)

        subnet_id = response['Subnets'][0]['SubnetId']
        log.info('Found Subnet ID: {s}'.format(s=subnet_id))

        # Create the ENI
        log.info('Attempting to create the Elastic Network Interface on subnet: {s}, with Security Groups: {g}'.format(
                s=subnet_id, g=security_group_ids))
        try:
            response = self.client.create_network_interface(
                    DryRun=False,
                    SubnetId=subnet_id,
                    Description=description,
                    Groups=security_group_ids)
        except ClientError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to create a network interface on Subnet {s} using Security Groups {g}\n{e}'.format(
                    s=subnet_id, g=security_group_ids, e=str(ex))
            log.error(msg)
            raise AWSAPIError, msg, trace

        code = response['ResponseMetadata']['HTTPStatusCode']
        if code != 200:
            msg = 'create_network_interface returned invalid code: {c}'.format(c=code)
            log.error(msg)
            raise AWSAPIError(msg)

        try:
            eni_id = response['NetworkInterface']['NetworkInterfaceId']
        except KeyError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to parse ENI ID from response: {r}\n{e}'.format(r=response, e=str(ex))
            log.error(msg)
            raise EC2UtilError, msg, trace
        log.info('Created ENI ID: {eni}'.format(eni=eni_id))

        # Verify the ENI was created successfully
        log.info('Verifying the ENI was created and is available for use...')
        ready = False
        num_checks = 60
        for _ in range(num_checks):
            try:
                self.client.describe_network_interfaces(
                        DryRun=False,
                        NetworkInterfaceIds=[eni_id]
                )
            except ClientError as ex:
                log.info('ENI ID {eni} is not available for use, trying again in 1 sec...\n{e}'.format(
                        eni=eni_id, e=ex))
                time.sleep(2)
            else:
                log.info('ENI ID {eni} is available for use'.format(eni=eni_id))
                ready = True
                break
        if not ready:
            msg = 'Unable to verify existence of new ENI ID: {eni}'.format(eni=eni_id)
            log.error(msg)
            raise EC2UtilError(msg)

        # If an allocation_id is specified, attach the elastic IP to the new ENI
        if allocation_id is not None:
            log.info('Attempting to attach elastic IP {a} to ENI {e}'.format(a=allocation_id, e=eni_id))
            try:
                response = self.client.associate_address(
                        AllocationId=allocation_id,
                        DryRun=False,
                        NetworkInterfaceId=eni_id,
                        AllowReassociation=True)
            except ClientError:
                _, ex, trace = sys.exc_info()
                msg = 'Unable to associate Elastic IP {a} to ENI {eni}\n{e}'.format(
                        a=allocation_id, eni=eni_id, e=str(ex))
                log.error(msg)
                raise AWSAPIError, msg, trace

            code = response['ResponseMetadata']['HTTPStatusCode']
            if code != 200:
                msg = 'associate_address returned invalid code: {c}'.format(c=code)
                log.error(msg)
                raise AWSAPIError(msg)
            log.info('Successfully attached Elastic IP {a} to ENI ID {eni}'.format(
                    eni=eni_id, a=allocation_id))

        # Attach the ENI to this EC2 instance
        log.info('Attempting to attach ENI ID {eni} to instance ID {i}'.format(
                eni=eni_id, i=self.instance_id))
        try:
            response = self.client.attach_network_interface(
                    DryRun=False,
                    NetworkInterfaceId=eni_id,
                    InstanceId=self.instance_id,
                    DeviceIndex=device_index)
        except ClientError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to attach ENI ID {eni} to instance {i} at device index {d}\n{e}'.format(
                    eni=eni_id, i=self.instance_id, d=device_index, e=ex)
            log.error(msg)
            raise AWSAPIError, msg, trace

        code = response['ResponseMetadata']['HTTPStatusCode']
        if code != 200:
            msg = 'attach_network_interface returned invalid code: {c}'.format(c=code)
            log.error(msg)
            raise AWSAPIError(msg)
        log.info('Successfully attached ENI ID {eni} to EC2 instance ID {i}'.format(
                eni=eni_id, i=self.instance_id))

Response: Creates a new elastic network interface on the subnet with the specified name and security group IDs and then attaches an Elastic IP address to the EC2 instance instance_id at the specified device index.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_elastic_ips(self):
        log = logging.getLogger(self.cls_logger + '.get_elastic_ips')
        instance_id = get_instance_id()
        if instance_id is None:
            log.error('Unable to get the Instance ID for this machine')
            return
        log.info('Found Instance ID: {i}'.format(i=instance_id))

        log.info('Querying AWS for info about instance ID {i}...'.format(i=instance_id))
        try:
            instance_info = self.client.describe_instances(DryRun=False, InstanceIds=[instance_id])
        except ClientError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to query AWS to get info for instance {i}\n{e}'.format(i=instance_id, e=ex)
            log.error(msg)
            raise AWSAPIError, msg, trace

        # Get the list of Public/Elastic IPs for this instance
        public_ips = []
        for network_interface in instance_info['Reservations'][0]['Instances'][0]['NetworkInterfaces']:
            network_interface_id = network_interface['NetworkInterfaceId']
            log.info('Checking ENI: {n}...'.format(n=network_interface_id))
            try:
                public_ips.append(network_interface['Association']['PublicIp'])
            except KeyError:
                log.info('No Public IP found for Network Interface ID: {n}'.format(n=network_interface_id))
            else:
                log.info('Found public IP for Network Interface ID {n}: {p}'.format(
                        n=network_interface_id, p=network_interface['Association']['PublicIp']))

        # Return if no Public/Elastic IPs found
        if len(public_ips) == 0:
            log.info('No Elastic IPs found for this instance: {i}'.format(i=instance_id))
            return
        else:
            log.info('Found Public IPs: {p}'.format(p=public_ips))

        # Get info for each Public/Elastic IP
        try:
            address_info = self.client.describe_addresses(DryRun=False, PublicIps=public_ips)
        except ClientError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to query AWS to get info for addresses {p}\n{e}'.format(p=public_ips, e=str(ex))
            log.error(msg)
            raise AWSAPIError, msg, trace
        if not address_info:
            msg = 'No address info return for Public IPs: {p}'.format(p=public_ips)
            log.error(msg)
            raise AWSAPIError(msg)
        return address_info

Response:
Returns the Elastic IPs for this instance any are
        attached to the instance.


Instruction:
Write a Python 3 function that can
disassociate each attached Elastic IP from the instance.

Response:
def disassociate_elastic_ips(self):
        """For each attached Elastic IP, disassociate it

        :return: None
        :raises AWSAPIError
        """
        log = logging.getLogger(self.cls_logger + '.disassociate_elastic_ips')

        try:
            address_info = self.get_elastic_ips()
        except AWSAPIError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to determine Elastic IPs on this EC2 instance'
            log.error(msg)
            raise AWSAPIError, msg, trace

        # Return is no elastic IPs were found
        if not address_info:
            log.info('No elastic IPs found to disassociate')
            return

        # Disassociate each Elastic IP
        for address in address_info['Addresses']:
            association_id = address['AssociationId']
            public_ip = address['PublicIp']
            log.info('Attempting to disassociate address {p} from Association ID: {a}'.format(
                    p=public_ip, a=association_id))
            try:
                self.client.disassociate_address(PublicIp=public_ip, AssociationId=association_id)
            except ClientError:
                _, ex, trace = sys.exc_info()
                msg = 'There was a problem disassociating Public IP {p} from Association ID {a}'.format(
                        p=public_ip, a=association_id)
                log.error(msg)
                raise AWSAPIError, msg, trace
            else:
                log.info('Successfully disassociated Public IP: {p}'.format(p=public_ip))


Instruction:
Implement a Python 3 function for
creating a new Security Group with the specified name and description in the specified VPC ID.

Response:
def create_security_group(self, name, description='', vpc_id=None):
        """Creates a new Security Group with the specified name,
        description, in the specified vpc_id if provided.  If
        vpc_id is not provided, use self.vpc_id

        :param name: (str) Security Group Name
        :param description: (str) Security Group Description
        :param vpc_id: (str) VPC ID to create the Security Group
        :return: (str) Security Group ID
        :raises: AWSAPIError, EC2UtilError
        """
        log = logging.getLogger(self.cls_logger + '.create_security_group')
        # Validate args
        if not isinstance(name, basestring):
            msg = 'name argument is not a string'
            log.error(msg)
            raise EC2UtilError(msg)
        if not isinstance(description, basestring):
            msg = 'description argument is not a string'
            log.error(msg)
            raise EC2UtilError(msg)
        if vpc_id is None and self.vpc_id is not None:
            vpc_id = self.vpc_id
        else:
            msg = 'Unable to determine VPC ID to use to create the Security Group'
            log.error(msg)
            raise EC2UtilError(msg)

        # See if a Security Group already exists with the same name
        log.info('Checking for an existing security group with name {n} in VPC: {v}'.format(n=name, v=vpc_id))
        filters = [{
                'Name': 'vpc-id',
                'Values': [vpc_id]
            },
            {
                'Name': 'group-name',
                'Values': [name]
            }]
        try:
            response = self.client.describe_security_groups(DryRun=False, Filters=filters)
        except ClientError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to query Security Groups to determine if {n} exists in VPC ID {v}\n{e}'.format(
                n=name, v=vpc_id, e=str(ex))
            log.error(msg)
            raise AWSAPIError, msg, trace
        else:
            log.debug('Found Security Group: {r}'.format(r=response))
            if len(response['SecurityGroups']) == 1:
                log.info('Found an existing security group with name {n} in VPC: {v}'.format(n=name, v=vpc_id))
                try:
                    group_id = response['SecurityGroups'][0]['GroupId']
                except KeyError:
                    _, ex, trace = sys.exc_info()
                    msg = 'Unable to determine the Security Group GroupId from response: {r}\n{e}'.format(
                        r=response, e=str(ex))
                    log.error(msg)
                    raise AWSAPIError, msg, trace
                else:
                    log.info('Found existing Security Group with GroupId: {g}'.format(g=group_id))
                    return group_id
            else:
                log.info('No existing Security Group with name {n} found in VPC: {v}'.format(n=name, v=vpc_id))

        # Create a new Security Group
        log.info('Attempting to create a Security Group with name <{n}>, description <{d}>, in VPC: {v}'.format(
            n=name, d=description, v=vpc_id))
        try:
            response = self.client.create_security_group(
                DryRun=False,
                GroupName=name,
                Description=description,
                VpcId=vpc_id
            )
        except ClientError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to create Security Group <{n}> in VPC: {v}'.format(n=name, v=vpc_id)
            log.error(msg)
            raise AWSAPIError, msg, trace
        else:
            log.info('Successfully created Security Group <{n}> in VPC: {v}'.format(n=name, v=vpc_id))
        return response['GroupId']


Instruction:
Implement a function in Python 3 to
return a list of security groups in the VPC.

Response:
def list_security_groups_in_vpc(self, vpc_id=None):
        """Lists security groups in the VPC.  If vpc_id is not provided, use self.vpc_id

        :param vpc_id: (str) VPC ID to list security groups for
        :return: (list) Security Group info
        :raises: AWSAPIError, EC2UtilError
        """
        log = logging.getLogger(self.cls_logger + '.list_security_groups_in_vpc')
        if vpc_id is None and self.vpc_id is not None:
            vpc_id = self.vpc_id
        else:
            msg = 'Unable to determine VPC ID to use to create the Security Group'
            log.error(msg)
            raise EC2UtilError(msg)

        # Create a filter on the VPC ID
        filters = [
            {
                'Name': 'vpc-id',
                'Values': [vpc_id]
            }
        ]

        # Get a list of security groups in the VPC
        log.info('Querying for a list of security groups in VPC ID: {v}'.format(v=vpc_id))
        try:
            security_groups = self.client.describe_security_groups(DryRun=False, Filters=filters)
        except ClientError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to query AWS for a list of security groups in VPC ID: {v}\n{e}'.format(
                v=vpc_id, e=str(ex))
            raise AWSAPIError, msg, trace
        return security_groups


Instruction:
Write a Python 3 function for
configuring the security group ID allowing access to the specified CIDR blocks for the specified TCP Port number.

Response:
def configure_security_group_ingress(self, security_group_id, port, desired_cidr_blocks):
        """Configures the security group ID allowing access
        only to the specified CIDR blocks, for the specified
        port number.

        :param security_group_id: (str) Security Group ID
        :param port: (str) TCP Port number
        :param desired_cidr_blocks: (list) List of desired CIDR
               blocks, e.g. 192.168.1.2/32
        :return: None
        :raises: AWSAPIError, EC2UtilError
        """
        log = logging.getLogger(self.cls_logger + '.configure_security_group_ingress')
        # Validate args
        if not isinstance(security_group_id, basestring):
            msg = 'security_group_id argument is not a string'
            log.error(msg)
            raise EC2UtilError(msg)
        if not isinstance(port, basestring):
            msg = 'port argument is not a string'
            log.error(msg)
            raise EC2UtilError(msg)
        if not isinstance(desired_cidr_blocks, list):
            msg = 'desired_cidr_blocks argument is not a list'
            log.error(msg)
            raise EC2UtilError(msg)
        log.info('Configuring Security Group <{g}> on port {p} to allow: {r}'.format(
            g=security_group_id, p=port, r=desired_cidr_blocks
        ))
        log.debug('Querying AWS for info on Security Group ID: {g}...'.format(g=security_group_id))
        try:
            security_group_info = self.client.describe_security_groups(DryRun=False, GroupIds=[security_group_id])
        except ClientError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to query AWS for Security Group ID: {g}\n{e}'.format(g=security_group_id, e=str(ex))
            log.error(msg)
            raise AWSAPIError, msg, trace
        else:
            log.debug('Found Security Group: {g}'.format(g=security_group_info))
        try:
            ingress_rules = security_group_info['SecurityGroups'][0]['IpPermissions']
        except KeyError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to get list of ingress rules for Security Group ID: {g}\n{e}'.format(
                    g=security_group_id, e=str(ex))
            log.error(msg)
            raise AWSAPIError, msg, trace
        else:
            log.debug('Found ingress rules: {r}'.format(r=ingress_rules))

        # Evaluate each rule against the provided port and IP address list
        log.debug('Setting ingress rules...')
        for ingress_rule in ingress_rules:
            log.debug('Evaluating ingress rule: {r}'.format(r=ingress_rule))
            if ingress_rule['ToPort'] != int(port):
                log.debug('Skipping rule not matching port: {p}'.format(p=port))
                continue
            log.info('Removing existing rules from Security Group {g} for port: {p}...'.format(
                    g=security_group_id, p=port))
            try:
                self.client.revoke_security_group_ingress(
                        DryRun=False,
                        GroupId=security_group_id,
                        IpPermissions=[ingress_rule])
            except ClientError:
                _, ex, trace = sys.exc_info()
                msg = 'Unable to remove existing Security Group rules for port {p} from Security Group: ' \
                      '{g}\n{e}'.format(p=port, g=security_group_id, e=str(ex))
                log.error(msg)
                raise AWSAPIError, msg, trace

        # Build ingress rule based on the provided CIDR block list
        desired_ip_permissions = [
            {
                'IpProtocol': 'tcp',
                'FromPort': int(port),
                'ToPort': int(port),
                'UserIdGroupPairs': [],
                'IpRanges': [],
                'PrefixListIds': []
            }
        ]

        # Add IP rules
        for desired_cidr_block in desired_cidr_blocks:
            log.debug('Adding ingress for CIDR block: {b}'.format(b=desired_cidr_block))
            cidr_block_entry = {
                'CidrIp': desired_cidr_block
            }
            desired_ip_permissions[0]['IpRanges'].append(cidr_block_entry)

        # Add the ingress rule
        log.debug('Adding ingress rule: {r}'.format(r=desired_ip_permissions))
        try:
            self.client.authorize_security_group_ingress(
                DryRun=False,
                GroupId=security_group_id,
                IpPermissions=desired_ip_permissions
            )
        except ClientError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to authorize Security Group ingress rule for Security Group {g}: {r}\n{e}'.format(
                    g=security_group_id, r=desired_ip_permissions, e=str(ex))
            log.error(msg)
            raise AWSAPIError, msg, trace
        else:
            log.info('Successfully added ingress rule for Security Group {g} on port: {p}'.format(
                    g=security_group_id, p=port))


Instruction:
How would you explain what the following Python 3 function does
def revoke_security_group_ingress(self, security_group_id, ingress_rules):
        log = logging.getLogger(self.cls_logger + '.revoke_security_group_ingress')
        log.info('Revoking ingress rules from security group: {g}'.format(g=security_group_id))
        try:
            self.client.revoke_security_group_ingress(
                DryRun=False,
                GroupId=security_group_id,
                IpPermissions=ingress_rules)
        except ClientError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to remove existing Security Group rules for port from Security Group: {g}\n{e}'.format(
                g=security_group_id, e=str(ex))
            raise AWSAPIError, msg, trace

Response: Revokes all ingress rules for a security group bu ID