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

        if 'VIRTUAL_ENV' in os.environ:
            self.pip_config_locations.append(os.path.join(os.environ['VIRTUAL_ENV'], 'pip.conf'))
            self.pip_config_locations.append(os.path.join(os.environ['VIRTUAL_ENV'], 'pip.ini'))

        if site_config_files:
            self.pip_config_locations.extend(site_config_files)

        index_url = None
        custom_config = None

        if 'PIP_INDEX_URL' in os.environ and os.environ['PIP_INDEX_URL']:
            # environ variable takes priority
            index_url = os.environ['PIP_INDEX_URL']
            custom_config = 'PIP_INDEX_URL environment variable'
        else:
            for pip_config_filename in self.pip_config_locations:
                if pip_config_filename.startswith('~'):
                    pip_config_filename = os.path.expanduser(pip_config_filename)

                if os.path.isfile(pip_config_filename):
                    config = ConfigParser()
                    config.read([pip_config_filename])
                    try:
                        index_url = config.get('global', 'index-url')
                        custom_config = pip_config_filename
                        break  # stop on first detected, because config locations have a priority
                    except (NoOptionError, NoSectionError):  # pragma: nocover
                        pass

        if index_url:
            self.PYPI_API_URL = self._prepare_api_url(index_url)
            print(Color('Setting API url to {{autoyellow}}{}{{/autoyellow}} as found in {{autoyellow}}{}{{/autoyellow}}'
                        '. Use --default-index-url to use pypi default index'.format(self.PYPI_API_URL, custom_config)))

Response: Updates index - url from pip. conf and pip. ini files.


Instruction:
Explain what the following Python 3 code does
def _fetch_index_package_info(self, package_name, current_version):

        try:
            package_canonical_name = package_name
            if self.PYPI_API_TYPE == 'simple_html':
                package_canonical_name = canonicalize_name(package_name)
            response = requests.get(self.PYPI_API_URL.format(package=package_canonical_name), timeout=15)
        except HTTPError as e:  # pragma: nocover
            return False, e.message

        if not response.ok:  # pragma: nocover
            return False, 'API error: {}'.format(response.reason)

        if self.PYPI_API_TYPE == 'pypi_json':
            return self._parse_pypi_json_package_info(package_name, current_version, response)
        elif self.PYPI_API_TYPE == 'simple_html':
            return self._parse_simple_html_package_info(package_name, current_version, response)
        else:  # pragma: nocover
            raise NotImplementedError('This type of PYPI_API_TYPE type is not supported')

Response: Fetch index information for a package.


Instruction:
How would you code a function in Python 3 to
parse the JSON response from PyPI to get the latest version of the package.

Response:
def _parse_pypi_json_package_info(self, package_name, current_version, response):
        """
        :type package_name: str
        :type current_version: version.Version
        :type response: requests.models.Response
        """

        data = response.json()
        all_versions = [version.parse(vers) for vers in data['releases'].keys()]
        filtered_versions = [vers for vers in all_versions if not vers.is_prerelease and not vers.is_postrelease]

        if not filtered_versions:  # pragma: nocover
            return False, 'error while parsing version'

        latest_version = max(filtered_versions)

        # even if user did not choose prerelease, if the package from requirements is pre/post release, use it
        if self._prerelease or current_version.is_postrelease or current_version.is_prerelease:
            prerelease_versions = [vers for vers in all_versions if vers.is_prerelease or vers.is_postrelease]
            if prerelease_versions:
                latest_version = max(prerelease_versions)
        try:
            try:
                latest_version_info = data['releases'][str(latest_version)][0]
            except KeyError:  # pragma: nocover
                # non-RFC versions, get the latest from pypi response
                latest_version = version.parse(data['info']['version'])
                latest_version_info = data['releases'][str(latest_version)][0]
        except Exception:  # pragma: nocover
            return False, 'error while parsing version'

        upload_time = latest_version_info['upload_time'].replace('T', ' ')

        return {
            'name': package_name,
            'current_version': current_version,
            'latest_version': latest_version,
            'upgrade_available': current_version < latest_version,
            'upload_time': upload_time
        }, 'success'


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _parse_simple_html_package_info(self, package_name, current_version, response):
        pattern = r'<a.*>.*{name}-([A-z0-9\.-]*)(?:-py|\.tar).*<\/a>'.format(name=re.escape(package_name))
        versions_match = re.findall(pattern, response.content.decode('utf-8'), flags=re.IGNORECASE)

        all_versions = [version.parse(vers) for vers in versions_match]
        filtered_versions = [vers for vers in all_versions if not vers.is_prerelease and not vers.is_postrelease]

        if not filtered_versions:  # pragma: nocover
            return False, 'error while parsing version'

        latest_version = max(filtered_versions)

        # even if user did not choose prerelease, if the package from requirements is pre/post release, use it
        if self._prerelease or current_version.is_postrelease or current_version.is_prerelease:
            prerelease_versions = [vers for vers in all_versions if vers.is_prerelease or vers.is_postrelease]
            if prerelease_versions:
                latest_version = max(prerelease_versions)

        return {
           'name': package_name,
           'current_version': current_version,
           'latest_version': latest_version,
           'upgrade_available': current_version < latest_version,
           'upload_time': '-'
        }, 'success'

Response: Parse the simple html package info.


Instruction:
Can you tell what is the following Python 3 function doing
def main():
    options = get_options()
    Windows.enable(auto_colors=True, reset_atexit=True)

    try:
        # maybe check if virtualenv is not activated
        check_for_virtualenv(options)

        # 1. detect requirements files
        filenames = RequirementsDetector(options.get('<requirements_file>')).get_filenames()
        if filenames:
            print(Color('{{autoyellow}}Found valid requirements file(s):{{/autoyellow}} '
                        '{{autocyan}}\n{}{{/autocyan}}'.format('\n'.join(filenames))))
        else:  # pragma: nocover
            print(Color('{autoyellow}No requirements files found in current directory. CD into your project '
                        'or manually specify requirements files as arguments.{/autoyellow}'))
            return
        # 2. detect all packages inside requirements
        packages = PackagesDetector(filenames).get_packages()

        # 3. query pypi API, see which package has a newer version vs the one in requirements (or current env)
        packages_status_map = PackagesStatusDetector(
            packages, options.get('--use-default-index')).detect_available_upgrades(options)

        # 4. [optionally], show interactive screen when user can choose which packages to upgrade
        selected_packages = PackageInteractiveSelector(packages_status_map, options).get_packages()

        # 5. having the list of packages, do the actual upgrade and replace the version inside all filenames
        upgraded_packages = PackagesUpgrader(selected_packages, filenames, options).do_upgrade()

        print(Color('{{autogreen}}Successfully upgraded (and updated requirements) for the following packages: '
                    '{}{{/autogreen}}'.format(','.join([package['name'] for package in upgraded_packages]))))
        if options['--dry-run']:
            print(Color('{automagenta}Actually, no, because this was a simulation using --dry-run{/automagenta}'))

    except KeyboardInterrupt:  # pragma: nocover
        print(Color('\n{autored}Upgrade interrupted.{/autored}'))

Response: This is the main function of the upgrade command.


Instruction:
Can you generate the documentation for the following Python 3 function
def _update_package(self, package):
        try:
            if not self.dry_run and not self.skip_package_installation:  # pragma: nocover
                subprocess.check_call(['pip', 'install', '{}=={}'.format(package['name'], package['latest_version'])])
            else:
                print('[Dry Run]: skipping package installation:', package['name'])
            # update only if installation success
            self._update_requirements_package(package)

        except CalledProcessError:  # pragma: nocover
            print(Color('{{autored}}Failed to install package "{}"{{/autored}}'.format(package['name'])))

Response: Update the package in current environment and if success update requirements file


Instruction:
Create a Python 3 function to
attempt to detect requirements files in the current working directory

Response:
def autodetect_files(self):
        """ Attempt to detect requirements files in the current working directory """
        if self._is_valid_requirements_file('requirements.txt'):
            self.filenames.append('requirements.txt')

        if self._is_valid_requirements_file('requirements.pip'):  # pragma: nocover
            self.filenames.append('requirements.pip')

        if os.path.isdir('requirements'):
            for filename in os.listdir('requirements'):
                file_path = os.path.join('requirements', filename)
                if self._is_valid_requirements_file(file_path):
                    self.filenames.append(file_path)
        self._check_inclusions_recursively()


Instruction:
Can you write a function in Python 3 where it
resolves all streams on the network.

Response:
def resolve_streams(wait_time=1.0):
    """Resolve all streams on the network.

    This function returns all currently available streams from any outlet on
    the network. The network is usually the subnet specified at the local
    router, but may also include a group of machines visible to each other via
    multicast packets (given that the network supports it), or list of
    hostnames. These details may optionally be customized by the experimenter
    in a configuration file (see Network Connectivity in the LSL wiki).

    Keyword arguments:
    wait_time -- The waiting time for the operation, in seconds, to search for
                 streams. Warning: If this is too short (<0.5s) only a subset
                 (or none) of the outlets that are present on the network may
                 be returned. (default 1.0)

    Returns a list of StreamInfo objects (with empty desc field), any of which
    can subsequently be used to open an inlet. The full description can be
    retrieved from the inlet.

    """
    # noinspection PyCallingNonCallable
    buffer = (c_void_p*1024)()
    num_found = lib.lsl_resolve_all(byref(buffer), 1024, c_double(wait_time))
    return [StreamInfo(handle=buffer[k]) for k in range(num_found)]


Instruction:
Implement a function in Python 3 to
resolve all streams with a specific value for a given property.

Response:
def resolve_byprop(prop, value, minimum=1, timeout=FOREVER):
    """Resolve all streams with a specific value for a given property.

    If the goal is to resolve a specific stream, this method is preferred over
    resolving all streams and then selecting the desired one.

    Keyword arguments:
    prop -- The StreamInfo property that should have a specific value (e.g.,
            "name", "type", "source_id", or "desc/manufaturer").
    value -- The string value that the property should have (e.g., "EEG" as
             the type property).
    minimum -- Return at least this many streams. (default 1)
    timeout -- Optionally a timeout of the operation, in seconds. If the
               timeout expires, less than the desired number of streams
               (possibly none) will be returned. (default FOREVER)

    Returns a list of matching StreamInfo objects (with empty desc field), any
    of which can subsequently be used to open an inlet.

    Example: results = resolve_Stream_byprop("type","EEG")

    """
    # noinspection PyCallingNonCallable
    buffer = (c_void_p*1024)()
    num_found = lib.lsl_resolve_byprop(byref(buffer), 1024,
                                       c_char_p(str.encode(prop)),
                                       c_char_p(str.encode(value)),
                                       minimum,
                                       c_double(timeout))
    return [StreamInfo(handle=buffer[k]) for k in range(num_found)]


Instruction:
Implement a Python 3 function for
resolving all streams that match a given predicate.

Response:
def resolve_bypred(predicate, minimum=1, timeout=FOREVER):
    """Resolve all streams that match a given predicate.

    Advanced query that allows to impose more conditions on the retrieved
    streams; the given string is an XPath 1.0 predicate for the <description>
    node (omitting the surrounding []'s), see also
    http://en.wikipedia.org/w/index.php?title=XPath_1.0&oldid=474981951.

    Keyword arguments:
    predicate -- The predicate string, e.g. "name='BioSemi'" or
                "type='EEG' and starts-with(name,'BioSemi') and
                 count(description/desc/channels/channel)=32"
    minimum -- Return at least this many streams. (default 1)
    timeout -- Optionally a timeout of the operation, in seconds. If the
               timeout expires, less than the desired number of streams
               (possibly none) will be returned. (default FOREVER)

    Returns a list of matching StreamInfo objects (with empty desc field), any
    of which can subsequently be used to open an inlet.

    """
    # noinspection PyCallingNonCallable
    buffer = (c_void_p*1024)()
    num_found = lib.lsl_resolve_bypred(byref(buffer), 1024,
                                       c_char_p(str.encode(predicate)),
                                       minimum,
                                       c_double(timeout))
    return [StreamInfo(handle=buffer[k]) for k in range(num_found)]


Instruction:
Write a Python 3 function for
translating an error code into an exception.

Response:
def handle_error(errcode):
    """Error handler function. Translates an error code into an exception."""
    if type(errcode) is c_int:
        errcode = errcode.value
    if errcode == 0:
        pass  # no error
    elif errcode == -1:
        raise TimeoutError("the operation failed due to a timeout.")
    elif errcode == -2:
        raise LostError("the stream has been lost.")
    elif errcode == -3:
        raise InvalidArgumentError("an argument was incorrectly specified.")
    elif errcode == -4:
        raise InternalError("an internal error has occurred.")
    elif errcode < 0:
        raise RuntimeError("an unknown error has occurred.")


Instruction:
Make a summary of the following Python 3 code
def push_sample(self, x, timestamp=0.0, pushthrough=True):
        if len(x) == self.channel_count:
            if self.channel_format == cf_string:
                x = [v.encode('utf-8') for v in x]
            handle_error(self.do_push_sample(self.obj, self.sample_type(*x),
                                             c_double(timestamp),
                                             c_int(pushthrough)))
        else:
            raise ValueError("length of the data must correspond to the "
                             "stream's channel count.")

Response: Push a sample into the outlet.


Instruction:
Can you generate the documentation for the following Python 3 function
def push_chunk(self, x, timestamp=0.0, pushthrough=True):
        try:
            n_values = self.channel_count * len(x)
            data_buff = (self.value_type * n_values).from_buffer(x)
            handle_error(self.do_push_chunk(self.obj, data_buff,
                                            c_long(n_values),
                                            c_double(timestamp),
                                            c_int(pushthrough)))
        except TypeError:
            if len(x):
                if type(x[0]) is list:
                    x = [v for sample in x for v in sample]
                if self.channel_format == cf_string:
                    x = [v.encode('utf-8') for v in x]
                if len(x) % self.channel_count == 0:
                    constructor = self.value_type*len(x)
                    # noinspection PyCallingNonCallable
                    handle_error(self.do_push_chunk(self.obj, constructor(*x),
                                                    c_long(len(x)),
                                                    c_double(timestamp),
                                                    c_int(pushthrough)))
                else:
                    raise ValueError("each sample must have the same number of "
                                     "channels.")

Response: Push a list of samples into the outlet.


Instruction:
Can you write a function in Python 3 where it
waits until some consumers are available. Returns True if the wait was successful False otherwise.

Response:
def wait_for_consumers(self, timeout):
        """Wait until some consumer shows up (without wasting resources).

        Returns True if the wait was successful, False if the timeout expired.

        """
        return bool(lib.lsl_wait_for_consumers(self.obj, c_double(timeout)))


Instruction:
Create a Python 3 function to
retrieve the complete information of the given stream.

Response:
def info(self, timeout=FOREVER):
        """Retrieve the complete information of the given stream.

        This includes the extended description. Can be invoked at any time of
        the stream's lifetime.

        Keyword arguments:
        timeout -- Timeout of the operation. (default FOREVER)

        Throws a TimeoutError (if the timeout expires), or LostError (if the
        stream source has been lost).

        """
        errcode = c_int()
        result = lib.lsl_get_fullinfo(self.obj, c_double(timeout),
                                      byref(errcode))
        handle_error(errcode)
        return StreamInfo(handle=result)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def open_stream(self, timeout=FOREVER):
        errcode = c_int()
        lib.lsl_open_stream(self.obj, c_double(timeout), byref(errcode))
        handle_error(errcode)

Response: Subscribe to the data stream.


Instruction:
Write a Python 3 script to
get an estimated time correction offset for the given stream.

Response:
def time_correction(self, timeout=FOREVER):
        """Retrieve an estimated time correction offset for the given stream.

        The first call to this function takes several miliseconds until a
        reliable first estimate is obtained. Subsequent calls are instantaneous
        (and rely on periodic background updates). The precision of these
        estimates should be below 1 ms (empirically within +/-0.2 ms).

        Keyword arguments:
        timeout -- Timeout to acquire the first time-correction estimate
                   (default FOREVER).

        Returns the current time correction estimate. This is the number that
        needs to be added to a time stamp that was remotely generated via
        local_clock() to map it into the local clock domain of this
        machine.

        Throws a TimeoutError (if the timeout expires), or LostError (if the
        stream source has been lost).

        """
        errcode = c_int()
        result = lib.lsl_time_correction(self.obj, c_double(timeout),
                                         byref(errcode))
        handle_error(errcode)
        return result


Instruction:
Can you tell what is the following Python 3 function doing
def pull_sample(self, timeout=FOREVER, sample=None):

        # support for the legacy API
        if type(timeout) is list:
            assign_to = timeout
            timeout = sample if type(sample) is float else 0.0
        else:
            assign_to = None

        errcode = c_int()
        timestamp = self.do_pull_sample(self.obj, byref(self.sample),
                                        self.channel_count, c_double(timeout),
                                        byref(errcode))
        handle_error(errcode)
        if timestamp:
            sample = [v for v in self.sample]
            if self.channel_format == cf_string:
                sample = [v.decode('utf-8') for v in sample]
            if assign_to is not None:
                assign_to[:] = sample
            return sample, timestamp
        else:
            return None, None

Response: Pull a sample from the inlet and return it.


Instruction:
Write a Python 3 function that can
pull a chunk of samples from the inlet.

Response:
def pull_chunk(self, timeout=0.0, max_samples=1024, dest_obj=None):
        """Pull a chunk of samples from the inlet.

        Keyword arguments:
        timeout -- The timeout of the operation; if passed as 0.0, then only
                   samples available for immediate pickup will be returned.
                   (default 0.0)
        max_samples -- Maximum number of samples to return. (default
                       1024)
        dest_obj -- A Python object that supports the buffer interface.
                    If this is provided then the dest_obj will be updated in place
                    and the samples list returned by this method will be empty.
                    It is up to the caller to trim the buffer to the appropriate
                    number of samples.
                    A numpy buffer must be order='C'
                    (default None)

        Returns a tuple (samples,timestamps) where samples is a list of samples
        (each itself a list of values), and timestamps is a list of time-stamps.

        Throws a LostError if the stream source has been lost.

        """
        # look up a pre-allocated buffer of appropriate length
        num_channels = self.channel_count
        max_values = max_samples*num_channels

        if max_samples not in self.buffers:
            # noinspection PyCallingNonCallable
            self.buffers[max_samples] = ((self.value_type*max_values)(),
                                         (c_double*max_samples)())
        if dest_obj is not None:
            data_buff = (self.value_type * max_values).from_buffer(dest_obj)
        else:
            data_buff = self.buffers[max_samples][0]
        ts_buff = self.buffers[max_samples][1]

        # read data into it
        errcode = c_int()
        # noinspection PyCallingNonCallable
        num_elements = self.do_pull_chunk(self.obj, byref(data_buff),
                                          byref(ts_buff), max_values,
                                          max_samples, c_double(timeout),
                                          byref(errcode))
        handle_error(errcode)
        # return results (note: could offer a more efficient format in the
        # future, e.g., a numpy array)
        num_samples = num_elements/num_channels
        if dest_obj is None:
            samples = [[data_buff[s*num_channels+c] for c in range(num_channels)]
                       for s in range(int(num_samples))]
            if self.channel_format == cf_string:
                samples = [[v.decode('utf-8') for v in s] for s in samples]
                free_char_p_array_memory(data_buff, max_values)
        else:
            samples = None
        timestamps = [ts_buff[s] for s in range(int(num_samples))]
        return samples, timestamps


Instruction:
Explain what the following Python 3 code does
def child(self, name):
        return XMLElement(lib.lsl_child(self.e, str.encode(name)))

Response: Get a child with a specified name.


Instruction:
Make a summary of the following Python 3 code
def next_sibling(self, name=None):
        if name is None:
            return XMLElement(lib.lsl_next_sibling(self.e))
        else:
            return XMLElement(lib.lsl_next_sibling_n(self.e, str.encode(name)))

Response: Get the next sibling in the children list of the parent node. If name is not provided the next sibling is returned.


Instruction:
Implement a function in Python 3 to
get the previous sibling in the children list of the parent node.

Response:
def previous_sibling(self, name=None):
        """Get the previous sibling in the children list of the parent node.

        If a name is provided, the previous sibling with the given name is
        returned.

        """
        if name is None:
            return XMLElement(lib.lsl_previous_sibling(self.e))
        else:
            return XMLElement(lib.lsl_previous_sibling_n(self.e,
                                                         str.encode(name)))


Instruction:
Can you tell what is the following Python 3 function doing
def child_value(self, name=None):
        if name is None:
            res = lib.lsl_child_value(self.e)
        else:
            res = lib.lsl_child_value_n(self.e, str.encode(name))
        return res.decode('utf-8')

Response: Get the child value of the first child that is text.


Instruction:
How would you explain what the following Python 3 function does
def append_child_value(self, name, value):
        return XMLElement(lib.lsl_append_child_value(self.e,
                                                     str.encode(name),
                                                     str.encode(value)))

Response: Append a child node with a given name which has a plain - text child with the given text value.


Instruction:
Explain what the following Python 3 code does
def set_child_value(self, name, value):
        return XMLElement(lib.lsl_set_child_value(self.e,
                                                  str.encode(name),
                                                  str.encode(value)))

Response:
Set the text value of the ( nameless ) plain - text child of a named
        child node.


Instruction:
Can you tell what is the following Python 3 function doing
def set_name(self, name):
        return bool(lib.lsl_set_name(self.e, str.encode(name)))

Response: Set the element s name. Returns False if the element is empty.


Instruction:
Can you tell what is the following Python 3 function doing
def set_value(self, value):
        return bool(lib.lsl_set_value(self.e, str.encode(value)))

Response: Set the element s value. Returns False if the element is empty.


Instruction:
Explain what the following Python 3 code does
def append_child(self, name):
        return XMLElement(lib.lsl_append_child(self.e, str.encode(name)))

Response: Append a child element with the specified name.


Instruction:
Explain what the following Python 3 code does
def prepend_child(self, name):
        return XMLElement(lib.lsl_prepend_child(self.e, str.encode(name)))

Response: Prepend a child element with the specified name.


Instruction:
Create a Python 3 function for
appending a copy of the specified element as a child.

Response:
def append_copy(self, elem):
        """Append a copy of the specified element as a child."""
        return XMLElement(lib.lsl_append_copy(self.e, elem.e))


Instruction:
Can you tell what is the following Python 3 function doing
def prepend_copy(self, elem):
        return XMLElement(lib.lsl_prepend_copy(self.e, elem.e))

Response: Prepend a copy of the specified element as a child.


Instruction:
Implement a Python 3 function for
removing a given child element specified by name or as element.

Response:
def remove_child(self, rhs):
        """Remove a given child element, specified by name or as element."""
        if type(rhs) is XMLElement:
            lib.lsl_remove_child(self.e, rhs.e)
        else:
            lib.lsl_remove_child_n(self.e, rhs)


Instruction:
Given the following Python 3 function, write the documentation
def results(self):
        # noinspection PyCallingNonCallable
        buffer = (c_void_p*1024)()
        num_found = lib.lsl_resolver_results(self.obj, byref(buffer), 1024)
        return [StreamInfo(handle=buffer[k]) for k in range(num_found)]

Response: Obtain the set of currently present streams on the network.


Instruction:
Can you implement a function in Python 3 that
sees all token associated with a given word.

Response:
def pair(cmd, word):
    """See all token associated with a given token.
    PAIR lilas"""
    word = list(preprocess_query(word))[0]
    key = pair_key(word)
    tokens = [t.decode() for t in DB.smembers(key)]
    tokens.sort()
    print(white(tokens))
    print(magenta('(Total: {})'.format(len(tokens))))


Instruction:
Create a Python 3 function for
showing autocomplete results for a given token.

Response:
def do_AUTOCOMPLETE(cmd, s):
    """Shows autocomplete results for a given token."""
    s = list(preprocess_query(s))[0]
    keys = [k.decode() for k in DB.smembers(edge_ngram_key(s))]
    print(white(keys))
    print(magenta('({} elements)'.format(len(keys))))


Instruction:
How would you implement a function in Python 3 that
computes edge ngram of token from min. Does not include token itself.

Response:
def compute_edge_ngrams(token, min=None):
    """Compute edge ngram of token from min. Does not include token itself."""
    if min is None:
        min = config.MIN_EDGE_NGRAMS
    token = token[:config.MAX_EDGE_NGRAMS + 1]
    return [token[:i] for i in range(min, len(token))]


Instruction:
Make a summary of the following Python 3 code
def iter_pipe(pipe, processors):
    if isinstance(pipe, str):
        pipe = [pipe]
    for it in processors:
        pipe = it(pipe)
    yield from pipe

Response: Allow for iterators to return either an item or an iterator of items.


Instruction:
Can you implement a function in Python 3 that
imports functions or class by their path.

Response:
def import_by_path(path):
    """
    Import functions or class by their path. Should be of the form:
    path.to.module.func
    """
    if not isinstance(path, str):
        return path
    module_path, *name = path.rsplit('.', 1)
    func = import_module(module_path)
    if name:
        func = getattr(func, name[0])
    return func


Instruction:
Can you create a Python 3 function that
calculates the great circle distance between two points on the earth.

Response:
def haversine_distance(point1, point2):
    """
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees).
    """
    lat1, lon1 = point1
    lat2, lon2 = point2

    # Convert decimal degrees to radians.
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])

    # Haversine formula.
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a))

    # 6367 km is the radius of the Earth.
    km = 6367 * c
    return km


Instruction:
Can you tell what is the following Python 3 function doing
def imap_unordered(self, func, iterable, chunksize):
        assert self._state == RUN
        task_batches = Pool._get_tasks(func, iterable, chunksize)
        result = IMapUnorderedIterator(self._cache)
        tasks = ((result._job, i, func, chunk, {})
                 for i, (_, chunk) in enumerate(task_batches))
        self._taskqueue.put((tasks, result._set_length))
        return result

Response: Customized version of imap_unordered.


Instruction:
Write a Python 3 script for
computing fuzzy extensions of word. FUZZY lilas

Response:
def do_fuzzy(self, word):
    """Compute fuzzy extensions of word.
    FUZZY lilas"""
    word = list(preprocess_query(word))[0]
    print(white(make_fuzzy(word)))


Instruction:
How would you implement a function in Python 3 that
computes fuzzy extensions of word that exist in index.

Response:
def do_fuzzyindex(self, word):
    """Compute fuzzy extensions of word that exist in index.
    FUZZYINDEX lilas"""
    word = list(preprocess_query(word))[0]
    token = Token(word)
    neighbors = make_fuzzy(token)
    neighbors = [(n, DB.zcard(dbkeys.token_key(n))) for n in neighbors]
    neighbors.sort(key=lambda n: n[1], reverse=True)
    for token, freq in neighbors:
        if freq == 0:
            break
        print(white(token), blue(freq))


Instruction:
Explain what the following Python 3 code does
def extend_results_extrapoling_relations(helper):
    if not helper.bucket_dry:
        return  # No need.
    tokens = set(helper.meaningful + helper.common)
    for relation in _extract_manytomany_relations(tokens):
        helper.add_to_bucket([t.db_key for t in relation])
        if helper.bucket_overflow:
            break
    else:
        helper.debug('No relation extrapolated.')

Response: Try to extract the bigger group of interlinked tokens.


Instruction:
Make a summary of the following Python 3 code
def do_help(self, command):
        if command:
            doc = getattr(self, 'do_' + command).__doc__
            print(cyan(doc.replace(' ' * 8, '')))
        else:
            print(magenta('Available commands:'))
            print(magenta('Type "HELP <command>" to get more info.'))
            names = self.get_names()
            names.sort()
            for name in names:
                if name[:3] != 'do_':
                    continue
                doc = getattr(self, name).__doc__
                doc = doc.split('\n')[0]
                print('{} {}'.format(yellow(name[3:]),
                                     cyan(doc.replace(' ' * 8, ' ')
                                             .replace('\n', ''))))

Response: Display this help message.


Instruction:
Make a summary of the following Python 3 code
def do_BENCH(self, query):
        try:
            count = int(re.match(r'^(\d+).*', query).group(1))
        except AttributeError:
            count = 100
        self._search(query, count=count)

Response:
Run a search many times to benchmark it.
 BENCH [ 100 ] rue des Lilas


Instruction:
Can you generate the documentation for the following Python 3 function
def do_INTERSECT(self, words):
        start = time.time()
        limit = 100
        if 'LIMIT' in words:
            words, limit = words.split('LIMIT')
            limit = int(limit)
        tokens = [keys.token_key(w) for w in preprocess_query(words)]
        DB.zinterstore(words, tokens)
        results = DB.zrevrange(words, 0, limit, withscores=True)
        DB.delete(words)
        for id_, score in results:
            r = Result(id_)
            print('{} {} {}'.format(white(r), blue(r._id), cyan(score)))
        duration = round((time.time() - start) * 1000, 1)
        print(magenta("({} in {} ms)".format(len(results), duration)))

Response: Do a raw intersect between tokens


Instruction:
How would you explain what the following Python 3 function does
def do_DBINFO(self, *args):
        info = DB.info()
        keys = [
            'keyspace_misses', 'keyspace_hits', 'used_memory_human',
            'total_commands_processed', 'total_connections_received',
            'connected_clients']
        for key in keys:
            print('{}: {}'.format(white(key), blue(info[key])))
        nb_of_redis_db = int(DB.config_get('databases')['databases'])
        for db_index in range(nb_of_redis_db - 1):
            db_name = 'db{}'.format(db_index)
            if db_name in info:
                label = white('nb keys (db {})'.format(db_index))
                print('{}: {}'.format(label, blue(info[db_name]['keys'])))

Response: Print some useful infos from Redis DB.


Instruction:
Write a Python 3 script for
printing raw content of a DB key.

Response:
def do_DBKEY(self, key):
        """Print raw content of a DB key.
        DBKEY g|u09tyzfe"""
        type_ = DB.type(key).decode()
        if type_ == 'set':
            out = DB.smembers(key)
        elif type_ == 'string':
            out = DB.get(key)
        else:
            out = 'Unsupported type {}'.format(type_)
        print('type:', magenta(type_))
        print('value:', white(out))


Instruction:
Write a Python 3 function that can
compute geodistance from a result to a point.

Response:
def do_GEODISTANCE(self, s):
        """Compute geodistance from a result to a point.
        GEODISTANCE 772210180J 48.1234 2.9876"""
        try:
            _id, lat, lon = s.split()
        except:
            return self.error('Malformed query. Use: ID lat lon')
        try:
            result = Result(keys.document_key(_id))
        except ValueError as e:
            return self.error(e)
        center = (float(lat), float(lon))
        km = haversine_distance((float(result.lat), float(result.lon)), center)
        score = km_to_score(km)
        print('km: {} | score: {}'.format(white(km), blue(score)))


Instruction:
Explain what the following Python 3 code does
def do_GEOHASHTOGEOJSON(self, geoh):
        geoh, with_neighbors = self._match_option('NEIGHBORS', geoh)
        bbox = geohash.bbox(geoh)
        try:
            with_neighbors = int(with_neighbors)
        except TypeError:
            with_neighbors = 0

        def expand(bbox, geoh, depth):
            neighbors = geohash.neighbors(geoh)
            for neighbor in neighbors:
                other = geohash.bbox(neighbor)
                if with_neighbors > depth:
                    expand(bbox, neighbor, depth + 1)
                else:
                    if other['n'] > bbox['n']:
                        bbox['n'] = other['n']
                    if other['s'] < bbox['s']:
                        bbox['s'] = other['s']
                    if other['e'] > bbox['e']:
                        bbox['e'] = other['e']
                    if other['w'] < bbox['w']:
                        bbox['w'] = other['w']

        if with_neighbors > 0:
            expand(bbox, geoh, 0)

        geojson = {
            "type": "Polygon",
            "coordinates": [[
                [bbox['w'], bbox['n']],
                [bbox['e'], bbox['n']],
                [bbox['e'], bbox['s']],
                [bbox['w'], bbox['s']],
                [bbox['w'], bbox['n']]
            ]]
        }
        print(white(json.dumps(geojson)))

Response: Build a GeoJSON representation of a COORDINATES GEOHASH.


Instruction:
Make a summary of the following Python 3 code
def do_GEOHASH(self, latlon):
        try:
            lat, lon = map(float, latlon.split())
        except ValueError:
            print(red('Invalid lat and lon {}'.format(latlon)))
        else:
            print(white(geohash.encode(lat, lon, config.GEOHASH_PRECISION)))

Response: Compute a geohash from latitude and longitude.


Instruction:
Implement a Python 3 function for
returning members of a geohash and its neighbors.

Response:
def do_GEOHASHMEMBERS(self, geoh):
        """Return members of a geohash and its neighbors.
        GEOHASHMEMBERS u09vej04 [NEIGHBORS 0]"""
        geoh, with_neighbors = self._match_option('NEIGHBORS', geoh)
        key = compute_geohash_key(geoh, with_neighbors != '0')
        if key:
            for id_ in DB.smembers(key):
                r = Result(id_)
                print('{} {}'.format(white(r), blue(r._id)))


Instruction:
How would you implement a function in Python 3 that
gets a single object from index with its id.

Response:
def do_GET(self, _id):
        """Get document from index with its id.
        GET 772210180J"""
        doc = doc_by_id(_id)
        if not doc:
            return self.error('id "{}" not found'.format(_id))
        for key, value in doc.items():
            if key == config.HOUSENUMBERS_FIELD:
                continue
            print('{} {}'.format(white(key), magenta(value)))
        if doc.get('housenumbers'):
            def sorter(v):
                try:
                    return int(re.match(r'^\d+', v['raw']).group())
                except AttributeError:
                    return -1
            housenumbers = sorted(doc['housenumbers'].values(), key=sorter)
            print(white('housenumbers'),
                  magenta(', '.join(v['raw'] for v in housenumbers)))


Instruction:
Write a Python 3 function that can
get index details for a document by its id.

Response:
def do_INDEX(self, _id):
        """Get index details for a document by its id.
        INDEX 772210180J"""
        doc = doc_by_id(_id)
        if not doc:
            return self.error('id "{}" not found'.format(_id))
        for field in config.FIELDS:
            key = field['key']
            if key in doc:
                self._print_field_index_details(doc[key], _id)


Instruction:
Write a Python 3 script for
returning document linked to word with higher score.

Response:
def do_BESTSCORE(self, word):
        """Return document linked to word with higher score.
        BESTSCORE lilas"""
        key = keys.token_key(indexed_string(word)[0])
        for _id, score in DB.zrevrange(key, 0, 20, withscores=True):
            result = Result(_id)
            print(white(result), blue(score), green(result._id))


Instruction:
Can you tell what is the following Python 3 function doing
def do_REVERSE(self, latlon):
        lat, lon = latlon.split()
        for r in reverse(float(lat), float(lon)):
            print('{} ({} | {} km | {})'.format(white(r), blue(r.score),
                                                blue(r.distance), blue(r._id)))

Response: Do a reverse search.


Instruction:
How would you code a function in Python 3 to
print the distance score between two strings.

Response:
def do_STRDISTANCE(self, s):
        """Print the distance score between two strings. Use | as separator.
        STRDISTANCE rue des lilas|porte des lilas"""
        s = s.split('|')
        if not len(s) == 2:
            print(red('Malformed string. Use | between the two strings.'))
            return
        one, two = s
        print(white(compare_str(one, two)))


Instruction:
Given the following Python 3 function, write the documentation
def do_CONFIG(self, name):
        if not name:
            for name in self.complete_CONFIG():
                self.do_CONFIG(name)
            return
        value = getattr(config, name.upper(), 'Not found.')
        print(blue(name), white(format_config(value)))

Response: Inspect loaded Addok config. Output all config without argument.


Instruction:
Can you implement a function in Python 3 that
runs a Lua script. Takes the raw Redis arguments.

Response:
def do_SCRIPT(self, args):
        """Run a Lua script. Takes the raw Redis arguments.
        SCRIPT script_name number_of_keys key1 key2… arg1 arg2
        """
        try:
            name, keys_count, *args = args.split()
        except ValueError:
            print(red('Not enough arguments'))
            return
        try:
            keys_count = int(keys_count)
        except ValueError:
            print(red('You must pass the number of keys as first argument'))
            self.do_HELP('SCRIPT')
            return
        keys = args[:keys_count]
        args = args[keys_count:]
        try:
            output = getattr(scripts, name)(keys=keys, args=args)
        except AttributeError:
            print(red('No script named {}'.format(name)))
            return
        except DB.Error as e:
            print(red(e))
            return
        if not isinstance(output, list):
            # Script may return just an integer.
            output = [output]
        for line in output:
            print(white(line))


Instruction:
Can you tell what is the following Python 3 function doing
def imap(requests, stream=True, pool=None, size=2, exception_handler=None):

    def send(r):
        return r.send(stream=stream)

    pool = pool if pool else Pool(size)

    for request in pool.imap(send, requests):
        if request.response is not None:
            yield request.response
        elif exception_handler:
            exception_handler(request, request.exception)

    if not pool:
        pool.close()

Response:
Concurrently converts a generator object of Requests to
    a generator of Responses.


Instruction:
Given the following Python 3 function, write the documentation
def imap_unordered(requests, stream=True, pool=None, size=2, exception_handler=None):

    def send(r):
        return r.send(stream=stream)

    pool = pool if pool else Pool(size)

    with contextlib.closing(Pool(size)) as pool:
        for request in pool.imap_unordered(send, requests):
            if request.response is not None:
                yield request.response
            elif exception_handler:
                exception_handler(request, request.exception)

    if not pool:
        pool.close()

Response:
Concurrently converts a generator object of Requests to
   .


Instruction:
How would you explain what the following Python 3 function does
def getBits_from_array(array, wordWidth, start, end,
                       reinterpretElmToType=None):
    """
    Gets value of bits between selected range from memory

    :param start: bit address of start of bit of bits
    :param end: bit address of first bit behind bits
    :return: instance of BitsVal (derived from SimBits type) which contains
        copy of selected bits
    """
    inPartOffset = 0
    value = Bits(end - start, None).fromPy(None)

    while start != end:
        assert start < end, (start, end)

        dataWordIndex = start // wordWidth

        v = array[dataWordIndex]
        if reinterpretElmToType is not None:
            v = v._reinterpret_cast(reinterpretElmToType)

        endOfWord = (dataWordIndex + 1) * wordWidth
        width = min(end, endOfWord) - start
        offset = start % wordWidth

        val = selectBitRange(v.val, offset, width)
        vldMask = selectBitRange(v.vldMask, offset, width)
        updateTime = v.updateTime

        m = mask(width)
        value.val |= (val & m) << inPartOffset
        value.vldMask |= (vldMask & m) << inPartOffset
        value.updateMask = max(value.updateTime, updateTime)

        inPartOffset += width
        start += width

    return value

Response: Gets value of bits between selected range from memory


Instruction:
Can you create a Python 3 function that
casts HArray signal or value to bits

Response:
def reinterptet_harray_to_bits(typeFrom, sigOrVal, bitsT):
    """
    Cast HArray signal or value to signal or value of type Bits
    """
    size = int(typeFrom.size)
    widthOfElm = typeFrom.elmType.bit_length()
    w = bitsT.bit_length()
    if size * widthOfElm != w:
        raise TypeConversionErr(
            "Size of types is different", size * widthOfElm, w)

    partT = Bits(widthOfElm)
    parts = [p._reinterpret_cast(partT) for p in sigOrVal]

    return Concat(*reversed(parts))._reinterpret_cast(bitsT)


Instruction:
Explain what the following Python 3 code does
def slice_to_SLICE(sliceVals, width):
    if sliceVals.step is not None:
        raise NotImplementedError()

    start = sliceVals.start
    stop = sliceVals.stop

    if sliceVals.start is None:
        start = INT.fromPy(width)
    else:
        start = toHVal(sliceVals.start)

    if sliceVals.stop is None:
        stop = INT.fromPy(0)
    else:
        stop = toHVal(sliceVals.stop)

    startIsVal = isinstance(start, Value)
    stopIsVal = isinstance(stop, Value)

    indexesAreValues = startIsVal and stopIsVal
    if indexesAreValues:
        updateTime = max(start.updateTime, stop.updateTime)
    else:
        updateTime = -1

    return Slice.getValueCls()((start, stop), SLICE, 1, updateTime)

Response: convert python slice to value of SLICE hdl type


Instruction:
Write a Python 3 function that can
return the bit range which contains data of this part on bus data signal

Response:
def getBusWordBitRange(self) -> Tuple[int, int]:
        """
        :return: bit range which contains data of this part on bus data signal
        """
        offset = self.startOfPart % self.parent.wordWidth
        return (offset + self.bit_length(), offset)


Instruction:
Can you generate the documentation for the following Python 3 function
def getFieldBitRange(self) -> Tuple[int, int]:
        offset = self.inFieldOffset
        return (self.bit_length() + offset, offset)

Response:
Returns the bit range which contains this part on interface
            of field


Instruction:
Implement a function in Python 3 to
fill all unused code branches with assignments from value specified by enclosure.

Response:
def fill_stm_list_with_enclosure(parentStm: Optional[HdlStatement],
                                 current_enclosure: Set[RtlSignalBase],
                                 statements: List["HdlStatement"],
                                 do_enclose_for: List[RtlSignalBase],
                                 enclosure: Dict[RtlSignalBase, Union[Value, RtlSignalBase]])\
        -> None:
    """
    Apply enclosure on list of statements
    (fill all unused code branches with assignments from value specified by enclosure)

    :param parentStm: optional parent statement where this list is some branch
    :param current_enclosure: list of signals for which this statement list is enclosed
    :param statements: list of statements
    :param do_enclose_for: selected signals for which enclosure should be used
    :param enclosure: enclosure values for signals

    :attention: original statements parameter can be modified
    :return: new statements
    """
    if statements is None:
        statements = []

    for e_sig in do_enclose_for:
        if e_sig in current_enclosure:
            continue
        enclosed = False
        for stm in statements:
            if e_sig in stm._outputs:
                if e_sig not in stm._enclosed_for:
                    stm._fill_enclosure(enclosure)
                enclosed = True
                break
        # any statement was not related with this signal,
        if not enclosed:
            e = enclosure[e_sig]
            a = Assignment(e, e_sig)
            statements.append(a)

            if parentStm is not None:
                a._set_parent_stm(parentStm)

    return statements


Instruction:
Explain what the following Python 3 code does
def find_files(directory, pattern, recursive=True):
    if not os.path.isdir(directory):
        if os.path.exists(directory):
            raise IOError(directory + ' is not directory')
        else:
            raise IOError(directory + " does not exists")
    if recursive:
        for root, _, files in os.walk(directory):
            for basename in files:
                if fnmatch.fnmatch(basename, pattern):
                    filename = os.path.join(root, basename)
                    yield filename
    else:
        root = directory
        for basename in os.listdir(root):
            if fnmatch.fnmatch(basename, pattern):
                filename = os.path.join(root, basename)
                if os.path.isfile(filename):
                    yield filename

Response: Find files by pattern in directory


Instruction:
Can you implement a function in Python 3 that
generates if tree for cases like

Response:
def SwitchLogic(cases, default=None):
    """
    Generate if tree for cases like (syntax shugar for large elifs)

    ..code-block:: python
        if cond0:
            statements0
        elif cond1:
            statements1
        else:
            default

    :param case: iterable of tuples (condition, statements)
    :param default: default statements
    """
    if default is not None:
        assigTop = default
    else:
        assigTop = []

    for cond, statements in reversed(cases):
        assigTop = If(cond,
                      statements
                      ).Else(
            assigTop
        )

    return assigTop


Instruction:
Create a Python 3 function for
generating for loop for static items in a single unit.

Response:
def StaticForEach(parentUnit, items, bodyFn, name=""):
    """
    Generate for loop for static items

    :param parentUnit: unit where this code should be instantiated
    :param items: items which this "for" itering on
    :param bodyFn: function which fn(item, index) or fn(item)
        returns (statementList, ack).
        It's content is performed in every iteration.
        When ack is high loop will fall to next iteration
    """

    items = list(items)
    itemsCnt = len(items)
    if itemsCnt == 0:
        # if there are no items there is nothing to generate
        return []
    elif itemsCnt == 1:
        # if there is only one item do not generate counter logic generate
        return bodyFn(items[0], 0)
    else:
        # if there is multiple items we have to generate counter logic
        index = parentUnit._reg(name + "for_index",
                                Bits(log2ceil(itemsCnt + 1), signed=False),
                                defVal=0)
        ackSig = parentUnit._sig(name + "for_ack")

        statementLists = []
        for i, (statementList, ack) in [(i, bodyFn(item, i))
                                        for i, item in enumerate(items)]:
            statementLists.append(statementList + [(ackSig(ack)), ])

        If(ackSig,
           If(index._eq(itemsCnt - 1),
              index(0)
              ).Else(
               index(index + 1)
           )
           )

        return Switch(index)\
            .addCases(
            enumerate(statementLists)
        ).Default(
            bodyFn(items[0], 0)[0],
            ackSig(True)
        )


Instruction:
Can you generate the documentation for the following Python 3 function
def connect(src, *destinations, exclude: set=None, fit=False):
    assignemnts = []

    if isinstance(src, HObjList):
        for dst in destinations:
            assert len(src) == len(dst), (src, dst)
        _destinations = [iter(d) for d in destinations]
        for _src in src:
            dsts = [next(d) for d in _destinations]
            assignemnts.append(connect(_src, *dsts, exclude=exclude, fit=fit))
    else:
        for dst in destinations:
            assignemnts.append(_connect(src, dst, exclude, fit))

    return assignemnts

Response: Connect src to all destinations


Instruction:
Can you tell what is the following Python 3 function doing
def log2ceil(x):

    if not isinstance(x, (int, float)):
        x = int(x)

    if x == 0 or x == 1:
        res = 1
    else:
        res = math.ceil(math.log2(x))

    return hInt(res)

Response: Returns no of bits required to store x - 1 returns 3


Instruction:
Make a summary of the following Python 3 code
def isPow2(num) -> bool:
    if not isinstance(num, int):
        num = int(num)
    return num != 0 and ((num & (num - 1)) == 0)

Response: Check if number or constant is power of two.


Instruction:
Can you generate the documentation for the following Python 3 function
def addCases(self, tupesValStmnts):
        s = self
        for val, statements in tupesValStmnts:
            s = s.Case(val, statements)
        return s

Response: Adds multiple cases from iterable of tuleles


Instruction:
Make a summary of the following Python 3 code
def Case(self, caseVal, *statements):
        "c-like case of switch statement"
        assert self.parentStm is None
        caseVal = toHVal(caseVal, self.switchOn._dtype)

        assert isinstance(caseVal, Value), caseVal
        assert caseVal._isFullVld(), "Cmp with invalid value"
        assert caseVal not in self._case_value_index, (
            "Switch statement already has case for value ", caseVal)

        self.rank += 1
        case = []
        self._case_value_index[caseVal] = len(self.cases)
        self.cases.append((caseVal, case))

        cond = self.switchOn._eq(caseVal)
        self._inputs.append(cond)
        cond.endpoints.append(self)

        self._register_stements(statements, case)

        return self

Response: c - like case of switch statement


Instruction:
How would you explain what the following Python 3 function does
def Trans(self, stateFrom, *condAndNextState):
        top = []
        last = True

        for cAndS in reversed(condAndNextState):
            if last is True:
                last = False
                # if this is last trans. it does not have to condition
                try:
                    condition, newvalue = cAndS
                except TypeError:
                    top = self.stateReg(cAndS)
                    continue
                top = []

            else:
                condition, newvalue = cAndS

            # building decision tree
            top = \
                If(condition,
                   self.stateReg(newvalue)
                   ).Else(
                    top
                )
        if stateFrom is None:
            return Switch.Default(self, top)
        else:
            return Switch.Case(self, stateFrom, top)

Response: Creates a new transition for the FSM.


Instruction:
Here you have a function in Python 3, explain what it does
def vcdTypeInfoForHType(t) -> Tuple[str, int, Callable[[RtlSignalBase, Value], str]]:
    if isinstance(t, (SimBitsT, Bits, HBool)):
        return (VCD_SIG_TYPE.WIRE, t.bit_length(), vcdBitsFormatter)
    elif isinstance(t, HEnum):
        return (VCD_SIG_TYPE.REAL, 1, vcdEnumFormatter)
    else:
        raise ValueError(t)

Response: Returns a function that returns the vcd type name and vcd width for the given HType.


Instruction:
Implement a Python 3 function for
registering signals from interfaces for Interface or Unit instances and returns the VCDVarWritingScope that is associated with the object.

Response:
def vcdRegisterInterfaces(self, obj: Union[Interface, Unit],
                              parent: Optional[VcdVarWritingScope]):
        """
        Register signals from interfaces for Interface or Unit instances
        """
        if hasattr(obj, "_interfaces") and obj._interfaces:
            name = obj._name
            parent_ = self.vcdWriter if parent is None else parent

            subScope = parent_.varScope(name)
            self._obj2scope[obj] = subScope

            with subScope:
                # register all subinterfaces
                for chIntf in obj._interfaces:
                    self.vcdRegisterInterfaces(chIntf, subScope)

                if isinstance(obj, (Unit, SimModel)):
                    # register interfaces from all subunits
                    for u in obj._units:
                        self.vcdRegisterInterfaces(u, subScope)

            return subScope
        else:
            t = obj._dtype
            if isinstance(t, self.supported_type_classes):
                tName, width, formatter = vcdTypeInfoForHType(t)
                try:
                    parent.addVar(obj, getSignalName(obj),
                                  tName, width, formatter)
                except VarAlreadyRegistered:
                    pass


Instruction:
Can you generate the documentation for the following Python 3 function
def beforeSim(self, simulator, synthesisedUnit):
        vcd = self.vcdWriter
        vcd.date(datetime.now())
        vcd.timescale(1)

        self.vcdRegisterInterfaces(synthesisedUnit, None)
        self.vcdRegisterRemainingSignals(synthesisedUnit)

        vcd.enddefinitions()

Response: This method is called before simulation is started.


Instruction:
Implement a function in Python 3 to
serialize HWProcess instance :param scope: name scope to prevent name collisions

Response:
def HWProcess(cls, proc, ctx):
        """
        Serialize HWProcess instance

        :param scope: name scope to prevent name collisions
        """
        body = proc.statements
        childCtx = ctx.withIndent()
        statemets = [cls.asHdl(s, childCtx) for s in body]
        proc.name = ctx.scope.checkedName(proc.name, proc)

        return cls.methodTmpl.render(
            indent=getIndent(ctx.indent),
            name=proc.name,
            statements=statemets
        )


Instruction:
Implement a Python 3 function for
adding new level to scope.

Response:
def setLevel(self, lvl):
        """
        Trim or extend scope
        lvl = 1 -> only one scope (global)
        """
        while len(self) != lvl:
            if len(self) > lvl:
                self.pop()
            else:
                self.append(NameScopeItem(len(self)))


Instruction:
Can you tell what is the following Python 3 function doing
def _size(self):
        assert isinstance(self, Value)
        return int(self.val[0]) - int(self.val[1])

Response: Returns the number of bits in the current key set


Instruction:
How would you code a function in Python 3 to
walk all interfaces on unit and instantiate agent for every interface.

Response:
def autoAddAgents(unit):
    """
    Walk all interfaces on unit and instantiate agent for every interface.

    :return: all monitor/driver functions which should be added to simulation
         as processes
    """
    proc = []
    for intf in unit._interfaces:
        if not intf._isExtern:
            continue

        intf._initSimAgent()
        assert intf._ag is not None, intf
        agents = [intf._ag, ]

        if intf._direction == INTF_DIRECTION.MASTER:
            agProcs = list(map(lambda a: a.getMonitors(), agents))
        elif intf._direction == INTF_DIRECTION.SLAVE:
            agProcs = list(map(lambda a: a.getDrivers(), agents))
        else:
            raise NotImplementedError("intf._direction %r for %r" % (
                intf._direction, intf))

        for p in agProcs:
            proc.extend(p)

    return proc


Instruction:
Can you write a function in Python 3 where it
iterates over the given iterable of values to ints.

Response:
def valuesToInts(values):
    """
    Iterable of values to ints (nonvalid = None)
    """
    res = []
    append = res.append
    for d in values:
        if isinstance(d, int):
            append(d)
        else:
            append(valToInt(d))
    return res


Instruction:
Can you implement a function in Python 3 that
returns the associated rst if any otherwise returns the parent rst

Response:
def _getAssociatedRst(self):
        """
        If interface has associated rst(_n) return it otherwise
        try to find rst(_n) on parent recursively
        """
        a = self._associatedRst

        if a is not None:
            return a

        p = self._parent
        assert p is not None

        if isinstance(p, UnitBase):
            return getRst(p)
        else:
            return p._getAssociatedRst()


Instruction:
How would you implement a function in Python 3 that
returns the associated clk or None if no clk is associated with this interface.

Response:
def _getAssociatedClk(self):
        """
        If interface has associated clk return it otherwise
        try to find clk on parent recursively
        """
        a = self._associatedClk

        if a is not None:
            return a

        p = self._parent
        assert p is not None

        if isinstance(p, UnitBase):
            return getClk(p)
        else:
            return p._getAssociatedClk()


Instruction:
Write a Python 3 function for
adding a variable to the list of extra discovered processes.

Response:
def Architecture_var(cls, v, serializerVars, extraTypes,
                         extraTypes_serialized, ctx, childCtx):
        """
        :return: list of extra discovered processes
        """
        v.name = ctx.scope.checkedName(v.name, v)
        serializedVar = cls.SignalItem(v, childCtx, declaration=True)
        serializerVars.append(serializedVar)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def single(iterable, fn):
    found = False
    ret = None

    for i in iterable:
        if fn(i):
            if found:
                raise DuplicitValueExc(i)
            found = True
            ret = i

    if not found:
        raise NoValueExc()

    return ret

Response:
Get value from iterable where fn(item ) and check
    is True


Instruction:
How would you code a function in Python 3 to
take n items from iterrable

Response:
def take(iterrable, howMay):
    """
    :return: generator of first n items from iterrable
    """
    assert howMay >= 0

    if not howMay:
        return

    last = howMay - 1
    for i, item in enumerate(iterrable):
        yield item
        if i == last:
            return


Instruction:
Make a summary of the following Python 3 code
def iter_with_last(iterable):
    # Ensure it's an iterator and get the first field
    iterable = iter(iterable)
    prev = next(iterable)
    for item in iterable:
        # Lag by one item so I know I'm not at the end
        yield False, prev
        prev = item
    # Last item
    yield True, prev

Response: Iterate over the items in the sequence with the last item


Instruction:
Given the following Python 3 function, write the documentation
def groupedby(collection, fn):
    d = {}
    for item in collection:
        k = fn(item)
        try:
            arr = d[k]
        except KeyError:
            arr = []
            d[k] = arr
        arr.append(item)

    yield from d.items()

Response: groupby - Returns a generator that yields the items of the given collection by a function.


Instruction:
Can you generate the documentation for the following Python 3 function
def flatten(iterables, level=inf):
    if level >= 0 and isinstance(iterables, (list, tuple, GeneratorType,
                                             map, zip)):
        level -= 1
        for i in iterables:
            yield from flatten(i, level=level)
    else:
        yield iterables

Response: Flatten nested lists tuples generators and maps


Instruction:
Make a summary of the following Python 3 code
def _cut_off_drivers_of(self, sig: RtlSignalBase):
        if len(self._outputs) == 1 and sig in self._outputs:
            self.parentStm = None
            return self

        # try to cut off all statements which are drivers of specified signal
        # in all branches
        child_keep_mask = []

        newIfTrue = []
        all_cut_off = True
        all_cut_off &= self._cut_off_drivers_of_list(
            sig, self.ifTrue, child_keep_mask, newIfTrue)
        self.ifTrue = list(compress(self.ifTrue, child_keep_mask))

        newElifs = []
        anyElifHit = False
        for cond, stms in self.elIfs:
            newCase = []
            child_keep_mask.clear()
            all_cut_off &= self._cut_off_drivers_of_list(
                sig, stms, child_keep_mask, newCase)

            _stms = list(compress(stms, child_keep_mask))
            stms.clear()
            stms.extend(_stms)

            if newCase:
                anyElifHit = True
            newElifs.append((cond, newCase))

        newIfFalse = None
        if self.ifFalse:
            newIfFalse = []
            child_keep_mask.clear()
            all_cut_off &= self._cut_off_drivers_of_list(
                sig, self.ifFalse, child_keep_mask, newIfFalse)
            self.ifFalse = list(compress(self.ifFalse, child_keep_mask))

        assert not all_cut_off, "everything was cut of but this should be already known at start"

        if newIfTrue or newIfFalse or anyElifHit or newIfFalse:
            # parts were cut off
            # generate new statement for them
            cond_sig = self.cond
            n = self.__class__(cond_sig, newIfTrue)
            for c, stms in newElifs:
                assert len(c) == 1
                c_sig = c[0]
                n.Elif(c_sig, stms)
            if newIfFalse is not None:
                n.Else(newIfFalse)

            if self.parentStm is None:
                ctx = n._get_rtl_context()
                ctx.statements.add(n)

            # update io of this
            self._inputs.clear()
            self._inputs.append(cond_sig)
            for c, _ in self.elIfs:
                self._inputs.extend(c)

            self._inputs.append(cond_sig)
            self._outputs.clear()

            out_add = self._outputs.append
            in_add = self._inputs.append

            for stm in self._iter_stms():
                for inp in stm._inputs:
                    in_add(inp)

                for outp in stm._outputs:
                    out_add(outp)

            if self._sensitivity is not None or self._enclosed_for is not None:
                raise NotImplementedError(
                    "Sensitivity and enclosure has to be cleaned first")

            return n

Response: Method to cut off all drivers of a signal and return the new statement.


Instruction:
Can you implement a function in Python 3 that
discovers the sensitivity of the current state of the statement.

Response:
def _discover_sensitivity(self, seen: set) -> None:
        """
        Doc on parent class :meth:`HdlStatement._discover_sensitivity`
        """
        assert self._sensitivity is None, self
        ctx = self._sensitivity = SensitivityCtx()

        self._discover_sensitivity_sig(self.cond, seen, ctx)
        if ctx.contains_ev_dependency:
            return

        for stm in self.ifTrue:
            stm._discover_sensitivity(seen)
            ctx.extend(stm._sensitivity)

        # elifs
        for cond, stms in self.elIfs:
            if ctx.contains_ev_dependency:
                break

            self._discover_sensitivity_sig(cond, seen, ctx)
            if ctx.contains_ev_dependency:
                break

            for stm in stms:
                if ctx.contains_ev_dependency:
                    break

                stm._discover_sensitivity(seen)
                ctx.extend(stm._sensitivity)

        if not ctx.contains_ev_dependency and self.ifFalse:
            # else
            for stm in self.ifFalse:
                stm._discover_sensitivity(seen)
                ctx.extend(stm._sensitivity)

        else:
            assert not self.ifFalse, "can not negate event"


Instruction:
Explain what the following Python 3 code does
def _iter_stms(self):
        yield from self.ifTrue
        for _, stms in self.elIfs:
            yield from stms
        if self.ifFalse is not None:
            yield from self.ifFalse

Response: Iterate over the set of elements in this HdlStatement.


Instruction:
Make a summary of the following Python 3 code
def _try_reduce(self) -> Tuple[bool, List[HdlStatement]]:
        # flag if IO of statement has changed
        io_change = False

        self.ifTrue, rank_decrease, _io_change = self._try_reduce_list(
            self.ifTrue)
        self.rank -= rank_decrease
        io_change |= _io_change

        new_elifs = []
        for cond, statements in self.elIfs:
            _statements, rank_decrease, _io_change = self._try_reduce_list(
                statements)
            self.rank -= rank_decrease
            io_change |= _io_change
            new_elifs.append((cond, _statements))

        if self.ifFalse is not None:
            self.ifFalse, rank_decrease, _io_update_required = self._try_reduce_list(
                self.ifFalse)
            self.rank -= rank_decrease
            io_change |= _io_change

        reduce_self = not self.condHasEffect(
            self.ifTrue, self.ifFalse, self.elIfs)

        if reduce_self:
            res = self.ifTrue
        else:
            res = [self, ]

        self._on_reduce(reduce_self, io_change, res)

        # try merge nested ifs as elifs
        if self.ifFalse is not None and len(self.ifFalse) == 1:
            child = self.ifFalse[0]
            if isinstance(child, IfContainer):
                self._merge_nested_if_from_else(child)

        return res, io_change

Response: Try to reduce the list of if conditions.


Instruction:
How would you implement a function in Python 3 that
merges nested IfContarner form else branch to this IfContainer

Response:
def _merge_nested_if_from_else(self, ifStm: "IfContainer"):
        """
        Merge nested IfContarner form else branch to this IfContainer
        as elif and else branches
        """
        self.elIfs.append((ifStm.cond, ifStm.ifTrue))
        self.elIfs.extend(ifStm.elIfs)

        self.ifFalse = ifStm.ifFalse


Instruction:
Make a summary of the following Python 3 code
def _merge_with_other_stm(self, other: "IfContainer") -> None:
        merge = self._merge_statement_lists
        self.ifTrue = merge(self.ifTrue, other.ifTrue)

        new_elifs = []
        for ((c, elifA), (_, elifB)) in zip(self.elIfs, other.elIfs):
            new_elifs.append((c,  merge(elifA, elifB)))
        self.elIfs = new_elifs

        self.ifFalse = merge(self.ifFalse, other.ifFalse)

        other.ifTrue = []
        other.elIfs = []
        other.ifFalse = None

        self._on_merge(other)

Response: Merge two IfContainer objects.


Instruction:
Can you generate the documentation for the following Python 3 function
def isSame(self, other: HdlStatement) -> bool:
        if self is other:
            return True

        if self.rank != other.rank:
            return False

        if isinstance(other, IfContainer):
            if self.cond is other.cond:
                if len(self.ifTrue) == len(other.ifTrue) \
                        and len(self.ifFalse) == len(other.ifFalse) \
                        and len(self.elIfs) == len(other.elIfs):
                    if not isSameStatementList(self.ifTrue,
                                               other.ifTrue) \
                            or not isSameStatementList(self.ifFalse,
                                                       other.ifFalse):
                        return False
                    for (ac, astms), (bc, bstms) in zip(self.elIfs,
                                                        other.elIfs):
                        if not (ac == bc) or\
                                not isSameStatementList(astms, bstms):
                            return False
                    return True
        return False

Response: Returns True if this statement is the same meaning as the other statement.


Instruction:
Explain what the following Python 3 code does
def removeUnconnectedSignals(netlist):

    toDelete = set()
    toSearch = netlist.signals

    while toSearch:
        _toSearch = set()

        for sig in toSearch:
            if not sig.endpoints:
                try:
                    if sig._interface is not None:
                        # skip interfaces before we want to check them,
                        # they should not be optimized out from design
                        continue
                except AttributeError:
                    pass

                for e in sig.drivers:
                    # drivers of this signal are useless rm them
                    if isinstance(e, Operator):
                        inputs = e.operands
                        if e.result is sig:
                            e.result = None
                    else:
                        inputs = e._inputs
                        netlist.statements.discard(e)

                    for op in inputs:
                        if not isinstance(op, Value):
                            try:
                                op.endpoints.remove(e)
                            except KeyError:
                                # this operator has 2x+ same operand
                                continue

                            _toSearch.add(op)

                toDelete.add(sig)

        if toDelete:
            for sig in toDelete:
                if sig.ctx == netlist:
                    netlist.signals.remove(sig)
                _toSearch.discard(sig)
            toDelete = set()
        toSearch = _toSearch

Response: Removes all unconnected signals from the given netlist.


Instruction:
Write a Python 3 function for
checking if process is just unconditional assignments

Response:
def checkIfIsTooSimple(proc):
    """check if process is just unconditional assignments
       and it is useless to merge them"""
    try:
        a, = proc.statements
        if isinstance(a, Assignment):
            return True
    except ValueError:
        pass
    return False


Instruction:
Explain what the following Python 3 code does
def tryToMerge(procA: HWProcess, procB: HWProcess):
    if (checkIfIsTooSimple(procA) or
            checkIfIsTooSimple(procB) or
            areSetsIntersets(procA.outputs, procB.sensitivityList) or
            areSetsIntersets(procB.outputs, procA.sensitivityList) or
            not HdlStatement._is_mergable_statement_list(procA.statements, procB.statements)):
        raise IncompatibleStructure()

    procA.statements = HdlStatement._merge_statement_lists(
        procA.statements, procB.statements)

    procA.outputs.extend(procB.outputs)
    procA.inputs.extend(procB.inputs)
    procA.sensitivityList.extend(procB.sensitivityList)

    return procA

Response: Try to merge two processes into procA.


Instruction:
Can you create a Python 3 function that
tries to merge processes as much is possible and yield processes instances that are not in the same structure of statements.

Response:
def reduceProcesses(processes):
    """
    Try to merge processes as much is possible

    :param processes: list of processes instances
    """
    # sort to make order of merging same deterministic
    processes.sort(key=lambda x: (x.name, maxStmId(x)), reverse=True)
    # now try to reduce processes with nearly same structure of statements into one
    # to minimize number of processes
    for _, procs in groupedby(processes, lambda p: p.rank):
        for iA, pA in enumerate(procs):
            if pA is None:
                continue
            for iB, pB in enumerate(islice(procs, iA + 1, None)):
                if pB is None:
                    continue

                try:
                    pA = tryToMerge(pA, pB)
                except IncompatibleStructure:
                    continue
                procs[iA + 1 + iB] = None
                # procs[iA] = pA

        for p in procs:
            if p is not None:
                yield p


Instruction:
Can you write a function in Python 3 where it
calls when a write request is received.

Response:
def onWriteReq(self, sim, addr, data):
        """
        on writeReqRecieved in monitor mode
        """
        self.requests.append((WRITE, addr, data))


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

Response:
def asHdl(cls, obj, ctx: HwtSerializerCtx):
        """
        Convert object to HDL string

        :param obj: object to serialize
        :param ctx: HwtSerializerCtx instance
        """
        if isinstance(obj, RtlSignalBase):
            return cls.SignalItem(obj, ctx)
        elif isinstance(obj, Value):
            return cls.Value(obj, ctx)
        else:
            try:
                serFn = obj.asHwt
            except AttributeError:
                serFn = None
            if serFn is not None:
                return serFn(cls, ctx)

            try:
                serFn = getattr(cls, obj.__class__.__name__)
            except AttributeError:
                serFn = None

            if serFn is not None:
                return serFn(obj, ctx)

            raise SerializerException("Not implemented for %r" % (obj))


Instruction:
Write a Python 3 script to
return a string representation of the given entity.

Response:
def Entity(cls, ent: Entity, ctx: HwtSerializerCtx):
        """
        Entity is just forward declaration of Architecture, it is not used
        in most HDL languages as there is no recursion in hierarchy
        """

        cls.Entity_prepare(ent, ctx, serialize=False)
        ent.name = ctx.scope.checkedName(ent.name, ent, isGlobal=True)
        ports = list(
            map(lambda p: (p.name, cls.HdlType(p._dtype, ctx)),
                ent.ports))
        return unitHeadTmpl.render(
            name=ent.name,
            ports=ports,
        )


Instruction:
Can you generate the documentation for the following Python 3 function
def toRtl(unitOrCls: Unit, name: str=None,
          serializer: GenericSerializer=VhdlSerializer,
          targetPlatform=DummyPlatform(), saveTo: str=None):
    """
    Convert unit to RTL using specified serializer

    :param unitOrCls: unit instance or class, which should be converted
    :param name: name override of top unit (if is None name is derived
        form class name)
    :param serializer: serializer which should be used for to RTL conversion
    :param targetPlatform: metainformatins about target platform, distributed
        on every unit under _targetPlatform attribute
        before Unit._impl() is called
    :param saveTo: directory where files should be stored
        If None RTL is returned as string.
    :raturn: if saveTo returns RTL string else returns list of file names
        which were created
    """
    if not isinstance(unitOrCls, Unit):
        u = unitOrCls()
    else:
        u = unitOrCls

    u._loadDeclarations()
    if name is not None:
        assert isinstance(name, str)
        u._name = name

    globScope = serializer.getBaseNameScope()
    mouduleScopes = {}

    # unitCls : unitobj
    serializedClasses = {}

    # (unitCls, paramsValues) : unitObj
    # where paramsValues are dict name:value
    serializedConfiguredUnits = {}

    doSerialize = True

    createFiles = saveTo is not None
    if createFiles:
        os.makedirs(saveTo, exist_ok=True)
        files = UniqList()
    else:
        codeBuff = []

    for obj in u._toRtl(targetPlatform):
        doSerialize = serializer.serializationDecision(
            obj,
            serializedClasses,
            serializedConfiguredUnits)
        if doSerialize:
            if isinstance(obj, Entity):
                s = globScope.fork(1)
                s.setLevel(2)
                ctx = serializer.getBaseContext()
                ctx.scope = s
                mouduleScopes[obj] = ctx
                ctx.currentUnit = obj.origin

                sc = serializer.Entity(obj, ctx)
                if createFiles:
                    fName = obj.name + serializer.fileExtension
                    fileMode = 'w'

            elif isinstance(obj, Architecture):
                try:
                    ctx = mouduleScopes[obj.entity]
                except KeyError:
                    raise SerializerException(
                        "Entity should be serialized"
                        " before architecture of %s"
                        % (obj.getEntityName()))

                sc = serializer.Architecture(obj, ctx)
                if createFiles:
                    fName = obj.getEntityName() + serializer.fileExtension
                    fileMode = 'a'
            else:
                if hasattr(obj, "_hdlSources"):
                    for fn in obj._hdlSources:
                        if isinstance(fn, str):
                            shutil.copy2(fn, saveTo)
                            files.append(fn)
                            continue
                else:
                    sc = serializer.asHdl(obj)

            if sc:
                if createFiles:
                    fp = os.path.join(saveTo, fName)
                    files.append(fp)
                    with open(fp, fileMode) as f:
                        if fileMode == 'a':
                            f.write("\n")

                        f.write(
                            serializer.formatter(sc)
                        )
                else:
                    codeBuff.append(sc)

        elif not createFiles:
            try:
                name = '"%s"' % obj.name
            except AttributeError:
                name = ""
            codeBuff.append(serializer.comment(
                "Object of class %s, %s was not serialized as specified" % (
                    obj.__class__.__name__, name)))

    if createFiles:
        return files
    else:
        return serializer.formatter(
            "\n".join(codeBuff)
        )

Response: Convert a unit instance or class to RTL.


Instruction:
Make a summary of the following Python 3 code
def name_for_process_and_mark_outputs(statements: List[HdlStatement])\
        -> str:
    """
    Resolve name for process and mark outputs of statemens as not hidden
    """
    out_names = []
    for stm in statements:
        for sig in stm._outputs:
            if not sig.hasGenericName:
                out_names.append(sig.name)

    if out_names:
        return min(out_names)
    else:
        return ""

Response: Resolve name for process and mark outputs of statemens as not hidden


Instruction:
Can you tell what is the following Python 3 function doing
def cut_off_drivers_of(dstSignal, statements):
    separated = []
    stm_filter = []
    for stm in statements:
        stm._clean_signal_meta()
        d = stm._cut_off_drivers_of(dstSignal)
        if d is not None:
            separated.append(d)

        f = d is not stm
        stm_filter.append(f)

    return list(compress(statements, stm_filter)), separated

Response: Cut off drivers of a list of statements.


Instruction:
Can you implement a function in Python 3 that
converts a list of HdlStatements into a list of HWProcesses.

Response:
def statements_to_HWProcesses(statements: List[HdlStatement])\
        -> Generator[HWProcess, None, None]:
    """
    Pack statements into HWProcess instances,
    * for each out signal resolve it's drivers and collect them
    * split statements if there is and combinational loop
    * merge statements if it is possible
    * resolve sensitivitilists
    * wrap into HWProcess instance
    * for every IO of process generate name if signal has not any
    """
    # create copy because this set will be reduced
    statements = copy(statements)

    # process ranks = how many assignments is probably in process
    # used to minimize number of merge tries
    processes = []
    while statements:
        stm = statements.pop()
        proc_statements = [stm, ]
        ps = _statements_to_HWProcesses(proc_statements, True)
        processes.extend(ps)

    yield from reduceProcesses(processes)


Instruction:
Create a Python 3 function to
mark all signals that are driven by something and have hidden = False if they are connecting statements or if they are external interface

Response:
def markVisibilityOfSignals(ctx, ctxName, signals, interfaceSignals):
    """
    * check if all signals are driven by something
    * mark signals with hidden = False if they are connecting statements
      or if they are external interface
    """
    for sig in signals:
        driver_cnt = len(sig.drivers)
        has_comb_driver = False
        if driver_cnt > 1:
            sig.hidden = False
            for d in sig.drivers:
                if not isinstance(d, Operator):
                    sig.hidden = False

                is_comb_driver = False

                if isinstance(d, PortItem):
                    is_comb_driver = True
                elif not d._now_is_event_dependent:
                    for a in walk_assignments(d, sig):
                        if not a.indexes\
                                and not a._is_completly_event_dependent:
                            is_comb_driver = True
                            break

                if has_comb_driver and is_comb_driver:
                    raise MultipleDriversErr(
                        "%s: Signal %r has multiple combinational drivers" %
                        (ctx.getDebugScopeName(), sig))

                has_comb_driver |= is_comb_driver
        elif driver_cnt == 1:
            if not isinstance(sig.drivers[0], Operator):
                sig.hidden = False
        else:
            sig.hidden = False
            if sig not in interfaceSignals:
                if not sig.defVal._isFullVld():
                    raise NoDriverErr(
                        sig, "Signal without any driver or valid value in ", ctxName)
                sig._const = True


Instruction:
Can you generate the documentation for the following Python 3 function
def sig(self, name, dtype=BIT, clk=None, syncRst=None, defVal=None):
        if isinstance(defVal, RtlSignal):
            assert defVal._const, \
                "Initial value of register has to be constant"
            _defVal = defVal._auto_cast(dtype)
        elif isinstance(defVal, Value):
            _defVal = defVal._auto_cast(dtype)
        elif isinstance(defVal, InterfaceBase):
            _defVal = defVal._sig
        else:
            _defVal = dtype.fromPy(defVal)

        if clk is not None:
            s = RtlSyncSignal(self, name, dtype, _defVal)
            if syncRst is not None and defVal is None:
                raise SigLvlConfErr(
                    "Probably forgotten default value on sync signal %s", name)
            if syncRst is not None:
                r = If(syncRst._isOn(),
                       RtlSignal.__call__(s, _defVal)
                       ).Else(
                    RtlSignal.__call__(s, s.next)
                )
            else:
                r = [RtlSignal.__call__(s, s.next)]

            If(clk._onRisingEdge(),
               r
               )
        else:
            if syncRst:
                raise SigLvlConfErr(
                    "Signal %s has reset but has no clk" % name)
            s = RtlSignal(self, name, dtype, defVal=_defVal)

        self.signals.add(s)

        return s

Response: Create a new signal in this context.


Instruction:
Write a Python 3 function that can
build Entity and Architecture instance out of netlist representation.

Response:
def synthesize(self, name, interfaces, targetPlatform):
        """
        Build Entity and Architecture instance out of netlist representation
        """
        ent = Entity(name)
        ent._name = name + "_inst"  # instance name

        # create generics
        for _, v in self.params.items():
            ent.generics.append(v)

        # interface set for faster lookup
        if isinstance(interfaces, set):
            intfSet = interfaces
        else:
            intfSet = set(interfaces)

        # create ports
        for s in interfaces:
            pi = portItemfromSignal(s, ent)
            pi.registerInternSig(s)
            ent.ports.append(pi)
            s.hidden = False

        removeUnconnectedSignals(self)
        markVisibilityOfSignals(self, name, self.signals, intfSet)

        for proc in targetPlatform.beforeHdlArchGeneration:
            proc(self)

        arch = Architecture(ent)
        for p in statements_to_HWProcesses(self.statements):
            arch.processes.append(p)

        # add signals, variables etc. in architecture
        for s in self.signals:
            if s not in intfSet and not s.hidden:
                arch.variables.append(s)

        # instantiate subUnits in architecture
        for u in self.subUnits:
            arch.componentInstances.append(u)

        # add components in architecture
        for su in distinctBy(self.subUnits, lambda x: x.name):
            arch.components.append(su)

        self.synthesised = True

        return [ent, arch]


Instruction:
Create a Python 3 function to
convert python or hdl value or signal object to hdl value or signal object

Response:
def toHVal(op: Any, suggestedType: Optional[HdlType]=None):
    """Convert python or hdl value/signal object to hdl value/signal object"""
    if isinstance(op, Value) or isinstance(op, SignalItem):
        return op
    elif isinstance(op, InterfaceBase):
        return op._sig
    else:
        if isinstance(op, int):
            if suggestedType is not None:
                return suggestedType.fromPy(op)

            if op >= 1 << 31:
                raise TypeError(
                    "Number %d is too big to fit in 32 bit integer of HDL"
                    " use Bits type instead" % op)
            elif op < -(1 << 31):
                raise TypeError(
                    "Number %d is too small to fit in 32 bit integer"
                    " of HDL use Bits type instead" % op)
        try:
            hType = defaultPyConversions[type(op)]
        except KeyError:
            hType = None

        if hType is None:
            raise TypeError("Unknown hardware type for %s" % (op.__class__))

        return hType.fromPy(op)


Instruction:
How would you explain what the following Python 3 function does
def Value(cls, val, ctx: SerializerCtx):
        t = val._dtype

        if isinstance(val, RtlSignalBase):
            return cls.SignalItem(val, ctx)

        c = cls.Value_try_extract_as_const(val, ctx)
        if c:
            return c

        if isinstance(t, Slice):
            return cls.Slice_valAsHdl(t, val, ctx)
        elif isinstance(t, HArray):
            return cls.HArrayValAsHdl(t, val, ctx)
        elif isinstance(t, Bits):
            return cls.Bits_valAsHdl(t, val, ctx)
        elif isinstance(t, HBool):
            return cls.Bool_valAsHdl(t, val, ctx)
        elif isinstance(t, HEnum):
            return cls.HEnumValAsHdl(t, val, ctx)
        elif isinstance(t, Integer):
            return cls.Integer_valAsHdl(t, val, ctx)
        elif isinstance(t, String):
            return cls.String_valAsHdl(t, val, ctx)
        else:
            raise SerializerException(
                "can not resolve value serialization for %r"
                % (val))

Response: returns the value in the specified context


Instruction:
Given the following Python 3 function, write the documentation
def getMaxStmIdForStm(stm):
    maxId = 0
    if isinstance(stm, Assignment):
        return stm._instId
    elif isinstance(stm, WaitStm):
        return maxId
    else:
        for _stm in stm._iter_stms():
            maxId = max(maxId, getMaxStmIdForStm(_stm))
        return maxId

Response: Get the maximum _instId for a statement


Instruction:
How would you explain what the following Python 3 function does
def maxStmId(proc):
    maxId = 0
    for stm in proc.statements:
        maxId = max(maxId, getMaxStmIdForStm(stm))
    return maxId

Response: get max statement id used for sorting of processes in architecture


Instruction:
Implement a function in Python 3 to
collect data from interface

Response:
def monitor(self, sim):
        """Collect data from interface"""
        if self.notReset(sim) and self._enabled:
            self.wrRd(sim.write, 1)

            yield sim.waitOnCombUpdate()

            d = self.doRead(sim)
            self.data.append(d)
        else:
            self.wrRd(sim.write, 0)


Instruction:
Can you write a function in Python 3 where it
writes data to interface

Response:
def doWrite(self, sim, data):
        """write data to interface"""
        sim.write(data, self.intf.data)


Instruction:
Write a Python 3 function for
pushing data to the interface

Response:
def driver(self, sim):
        """Push data to interface"""
        r = sim.read
        if self.actualData is NOP and self.data:
            self.actualData = self.data.popleft()

        do = self.actualData is not NOP

        if do:
            self.doWrite(sim, self.actualData)
        else:
            self.doWrite(sim, None)

        en = self.notReset(sim) and self._enabled
        if not (en and do):
            return

        yield sim.waitOnCombUpdate()

        rd = self.isRd(r)
        if en:
            assert rd.vldMask, (
                ("%r: ready signal for interface %r is in invalid state,"
                 " this would cause desynchronization") %
                (sim.now, self.intf))
        if rd.val:
            if self._debugOutput is not None:
                self._debugOutput.write("%s, wrote, %d: %r\n" % (
                                           self.intf._getFullName(),
                                           sim.now, self.actualData))
            if self.data:
                self.actualData = self.data.popleft()
            else:
                self.actualData = NOP


Instruction:
Can you implement a function in Python 3 that
creates a new instance of the class from a python value.

Response:
def fromPy(cls, val, typeObj, vldMask=None):
        """
        :param val: value of python type int or None
        :param typeObj: instance of Integer
        :param vldMask: None vldMask is resolved from val,
            if is 0 value is invalidated
            if is 1 value has to be valid
        """
        assert isinstance(typeObj, Integer)
        vld = int(val is not None)
        if not vld:
            assert vldMask is None or vldMask == 0
            val = 0
        else:
            if vldMask == 0:
                val = False
                vld = 0
            else:
                val = int(val)

        return cls(val, typeObj, vld)


Instruction:
Implement a function in Python 3 to
change the direction of the master interface

Response:
def _m(self):
        """
        Note that this interface will be master

        :return: self
        """
        assert not hasattr(self, "_interfaces") or not self._interfaces, \
            "Too late to change direction of interface"
        self._direction = DIRECTION.asIntfDirection(DIRECTION.opposite(self._masterDir))

        return self


Instruction:
Write a Python 3 script to
load declaratoins from _declr method

Response:
def _loadDeclarations(self):
        """
        load declaratoins from _declr method
        This function is called first for parent and then for children
        """
        if not hasattr(self, "_interfaces"):
            self._interfaces = []
        self._setAttrListener = self._declrCollector
        self._declr()
        self._setAttrListener = None

        for i in self._interfaces:
            i._isExtern = self._isExtern
            i._loadDeclarations()

        for p in self._params:
            p.setReadOnly()

        if self._isExtern:
            # direction from inside of unit (reverset compared to outside direction)
            if self._direction == INTF_DIRECTION.UNKNOWN:
                self._direction = INTF_DIRECTION.MASTER
            self._setDirectionsLikeIn(self._direction)


Instruction:
Create a Python 3 function for
cleaning all signals from this unit.

Response:
def _clean(self, rmConnetions=True, lockNonExternal=True):
        """
        Remove all signals from this interface (used after unit is synthesized
        and its parent is connecting its interface to this unit)
        """

        if self._interfaces:
            for i in self._interfaces:
                i._clean(rmConnetions=rmConnetions,
                         lockNonExternal=lockNonExternal)
        else:
            self._sigInside = self._sig
            del self._sig

        if lockNonExternal and not self._isExtern:
            self._isAccessible = False