Instruction:
How would you implement a function in Python 3 that
accesses decorator which checks if a RPC method is enabled by our configuration

Response:
def enabled_checker(func):
        """ Access decorator which checks if a RPC method is enabled by our configuration
        """
        @wraps(func)
        def wrap(self, *args, **kwargs):
            if self.allowed_methods and isinstance(self.allowed_methods, list) and func.__name__ not in self.allowed_methods:
                raise Exception("Method {} is disabled".format(func.__name__))
            return func(self, *args, **kwargs)
        return wrap


Instruction:
Can you generate a brief explanation for the following Python 3 code
async def list_presets(self):
        presets = await self.request.app.vmshepherd.preset_manager.list_presets()
        return list(presets.keys())

Response: List all available presets.


Instruction:
How would you explain what the following Python 3 function does
async def list_vms(self, preset):
        vmshepherd = self.request.app.vmshepherd
        preset = vmshepherd.preset_manager.get_preset(preset)
        result_vms = {vm.id: {'ip': vm.ip[0], 'state': vm.state.value} for vm in preset.vms}
        return preset.count, result_vms

Response: List virtual machines in a given preset


Instruction:
Can you tell what is the following Python 3 function doing
async def terminate_vm(self, preset, vm_id):
        vmshepherd = self.request.app.vmshepherd
        preset = vmshepherd.preset_manager.get_preset(preset)
        await preset.iaas.terminate_vm(vm_id)
        return 'OK'

Response: Discard vm in specified preset


Instruction:
Create a Python 3 function to
get VM metadata for a given VM

Response:
async def get_vm_metadata(self, preset, vm_id):
        """ Get vm metadata

        :arg string preset: preset name
        :arg int vm_id: Virtual Machine id
        :return:  Metadata for Virtual Machine
        :rtype: dict

        Sample response:
           ``{ 'time_shutdown' : "12312312321' }``
        """
        vmshepherd = self.request.app.vmshepherd
        preset = vmshepherd.preset_manager.get_preset(preset)
        vm_info = await preset.iaas.get_vm(vm_id)
        ret_info = copy.deepcopy(vm_info.metadata) if vm_info.metadata else {}
        ret_info['tags'] = vm_info.tags
        ret_info['iaas_shutdown'] = vm_info.timed_shutdown_at
        return ret_info


Instruction:
Here you have a function in Python 3, explain what it does
def linkify(text, attrs={}):
    def separate_parentheses(s):
        start = re_find(r'^\(*', s)
        end = re_find(r'\)*$', s)
        n = min(len(start), len(end))
        if n:
            return s[:n], s[n:-n], s[-n:]
        else:
            return '', s, ''

    def link_repl(url, proto='http://'):
        opening, url, closing = separate_parentheses(url)

        punct = re_find(punct_re, url)
        if punct:
            url = url[:-len(punct)]

        if re.search(proto_re, url):
            href = url
        else:
            href = proto + url
        href = escape_url(href)

        repl = u'{0!s}{3!s}{4!s}{5!s}'
        return repl.format(opening,
                           href, attrs_text, url, punct,
                           closing)

    def repl(match):
        matches = match.groupdict()
        if matches['url']:
            return link_repl(matches['url'])
        else:
            return link_repl(matches['email'], proto='mailto:')

    # Prepare attrs
    attr = ' {0!s}="{1!s}"'
    attrs_text = ''.join(starmap(attr.format, attrs.items()))

    # Make replaces
    return re.sub(combined_re, repl, force_unicode(text))

Response: Convert URL - like and email - like strings into links.


Instruction:
Write a Python 3 function for
forcing unicode string s to be unicode.

Response:
def force_unicode(s, encoding='utf-8', errors='strict'):
    """
    Similar to smart_text, except that lazy instances are resolved to
    strings, rather than kept as lazy objects.
    """
    # Handle the common case first, saves 30-40% when s is an instance of
    # six.text_type. This function gets called often in that setting.
    if isinstance(s, six.text_type):
        return s
    if not isinstance(s, six.string_types):
        if six.PY3:
            if isinstance(s, bytes):
                s = six.text_type(s, encoding, errors)
            else:
                s = six.text_type(s)
        else:
            s = six.text_type(bytes(s), encoding, errors)
    else:
        # Note: We use .decode() here, instead of six.text_type(s,
        # encoding, errors), so that if s is a SafeBytes, it ends up being
        # a SafeText at the end.
        s = s.decode(encoding, errors)
    return s


Instruction:
Explain what the following Python 3 code does
def save_dict_to_hdf5(dic, filename):
    with h5py.File(filename, 'w') as h5file:
        rf = recursively_save_dict_contents_to_group(h5file, '/', dic)
        h5_rf = h5file.create_group("_reconstruction_flags")
        # h5_rf = h5file.create_group("_reconstruction_key_flags")
        for k, v in rf.items():
            h5_rf.create_dataset("/_reconstruction_flags" + k, data=v)

Response: Save dictionary to HDF5 file.


Instruction:
Make a summary of the following Python 3 code
def recursively_load_dict_contents_from_group(h5file, path):
    rf = h5file["_reconstruction_flags"]
    # rkf = h5file["_reconstruction_key_flags"]
    ans = {}
    for key, item in h5file[path].items():
        dest_key = key
        # if key in ("_reconstruction_flags", "_reconstruction_key_flags"):
        if key in "_reconstruction_flags":
            continue
        kkey = key + "_key_"
        tkey = key + "_typ_"
        if kkey in rf:
            flag = rf[kkey]
            if flag.value == "json_key":
                import json
                dest_key = json.loads(key)
                # import pickle
                # dest_key = pickle.loads(key.encode("ascii"))
                # logger.debug("unpickling key")

        if tkey in rf:
            flag = rf[tkey]
            if flag.value == "list":
                dict_to_output = recursively_load_dict_contents_from_group(h5file, path + key + '/')
                ans[dest_key] = list(dict_to_output.values())
                continue
            if flag.value == "tuple":
                dict_to_output = recursively_load_dict_contents_from_group(h5file, path + key + '/')
                ans[dest_key] = tuple(dict_to_output.values())
                continue
            elif flag.value == "json_value":
                import json
                ans[dest_key] = json.loads(item.value)
                continue
            elif flag.value == "float":
                ans[dest_key] = float(item.value)
                continue
            elif flag.value == "int":
                ans[dest_key] = int(item.value)
                continue

        if isinstance(item, h5py._hl.dataset.Dataset):
            ans[dest_key] = item.value
        elif isinstance(item, h5py._hl.group.Group):
            ans[dest_key] = recursively_load_dict_contents_from_group(h5file, path + key + '/')
    return ans

Response: Recursively loads the contents of a group into a single dict.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def parse_args(*args):
    parser = argparse.ArgumentParser(
        description="Send push notifications for a feed")

    parser.add_argument('--version', action='version',
                        version="%(prog)s " + __version__.__version__)

    parser.add_argument('feeds', type=str, nargs='*', metavar='feed_url',
                        help='A URL for a feed to process')
    parser.add_argument('--cache', '-c', type=str, dest='cache_dir',
                        help='Cache storage directory',
                        required=False)

    parser.add_argument("-v", "--verbosity", action="count",
                        help="increase output verbosity",
                        default=0)
    parser.add_argument("-e", "--entry", nargs='+',
                        help='URLs to entries/pages to index directly',
                        metavar='entry_url',
                        dest='entries')
    parser.add_argument("-s", "--websub-only", nargs='+',
                        help='URLs/feeds to only send WebSub notifications for',
                        metavar='feed_url', dest='websub_only')
    parser.add_argument('--timeout', '-t', type=int, dest='timeout',
                        help='Connection timeout, in seconds',
                        default=120)

    parser.add_argument('--max-connections', type=int, dest='max_connections',
                        help='Maximum number of connections to have open at once',
                        default=100)
    parser.add_argument('--max-per-host', type=int, dest='max_per_host',
                        help='Maximum number of connections per host',
                        default=0)

    parser.add_argument('--rel-whitelist', '-w', dest='rel_whitelist', type=str,
                        help="Comma-separated list of link RELs to whitelist"
                        + " for sending webmentions")
    parser.add_argument('--rel-blacklist', '-b', dest='rel_blacklist', type=str,
                        help="Comma-separated list of link RELs to blacklist"
                        + " from sending webmentions",
                        default="nofollow")

    parser.add_argument('--max-time', '-m', dest='max_time', type=float,
                        help="Maximum time (in seconds) to spend on this", default=1800)

    parser.add_argument('--user-agent', dest='user_agent', type=str,
                        help="User-agent string to send", default=__version__.USER_AGENT)

    feature = parser.add_mutually_exclusive_group(required=False)
    feature.add_argument('--keepalive', dest='keepalive', action='store_true',
                         help="Keep TCP connections alive")
    feature.add_argument('--no-keepalive', dest='keepalive', action='store_false',
                         help="Don't keep TCP connections alive")
    feature.set_defaults(keepalive=False)

    feature = parser.add_mutually_exclusive_group(required=False)
    feature.add_argument('--archive', '-a', dest='archive', action='store_true',
                         help='Process archive links in the feed per RFC 5005')
    feature.add_argument('--no-archive', dest='archive', action='store_false',
                         help='Do not process archive links in the feed')
    feature.set_defaults(archive=False)

    feature = parser.add_mutually_exclusive_group(required=False)
    feature.add_argument('--recurse', '-r',
                         help="Recursively check other discovered feeds",
                         action='store_true', dest='recurse')
    feature.add_argument('--no-recurse', dest='recurse',
                         action='store_false',
                         help="Do not recurse into other feeds")
    feature.set_defaults(recurse=False)

    return parser.parse_args(*args)

Response: Parse the arguments for the command line


Instruction:
Write a Python 3 script for
retrieving all environments for a given project.

Response:
def get_environments(self, project_key: str) -> dict:
        """
        Retrieve all environments for a given project.

        Includes name, key, and mobile key.

        :param project_key: Key for project.

        :returns: dictionary of environments.
        """
        try:
            resp = self.client.get_project(project_key)
        except launchdarkly_api.rest.ApiException as ex:
            msg = "Unable to get environments."
            resp = "API response was {0} {1}.".format(ex.status, ex.reason)
            LOG.error("%s %s", msg, resp)
            sys.exit(1)

        envs = []

        for env in resp.environments:
            env = dict(
                key=env.key,
                api_key=env.api_key,
                client_id=env.id
            )
            envs.append(env)

        return envs


Instruction:
Can you implement a function in Python 3 that
updates the status of a feature flag.

Response:
def update_flag(self, state: str, feature_key: str) \
        -> launchdarkly_api.FeatureFlag:
        """
        Update the flag status for the specified feature flag.

        :param state: New feature flag state
        :param featureKey: Feature flag key

        :returns: FeatureFlag object.
        """
        build_env = "/environments/" + self.environment_key + "/on"
        patch_comment = [{"op": "replace", "path": build_env, "value": state}]

        try:
            resp = self.feature.patch_feature_flag(
                self.project_key, feature_key, patch_comment)
        except launchdarkly_api.rest.ApiException as ex:
            msg = "Unable to update flag."
            resp = "API response was {0} {1}.".format(ex.status, ex.reason)
            LOG.error("%s %s", msg, resp)
            sys.exit(1)

        return resp


Instruction:
Given the following Python 3 function, write the documentation
def _write_result_handler(self, routine):
        self._write_line('ret = {}')
        self._write_execute_rows(routine)
        self._write_line('for row in rows:')

        num_of_dict = len(routine['columns'])

        i = 0
        while i < num_of_dict:
            value = "row['{0!s}']".format(routine['columns'][i])

            stack = ''
            j = 0
            while j < i:
                stack += "[row['{0!s}']]".format(routine['columns'][j])
                j += 1
            line = 'if {0!s} in ret{1!s}:'.format(value, stack)
            self._write_line(line)
            i += 1

        line = "raise Exception('Duplicate key for %s.' % str(({0!s})))". \
            format(", ".join(["row['{0!s}']".format(column_name) for column_name in routine['columns']]))

        self._write_line(line)
        self._indent_level_down()

        i = num_of_dict
        while i > 0:
            self._write_line('else:')

            part1 = ''
            j = 0
            while j < i - 1:
                part1 += "[row['{0!s}']]".format(routine['columns'][j])
                j += 1
            part1 += "[row['{0!s}']]".format(routine['columns'][j])

            part2 = ''
            j = i - 1
            while j < num_of_dict:
                if j + 1 != i:
                    part2 += "{{row['{0!s}']: ".format(routine['columns'][j])
                j += 1
            part2 += "row" + ('}' * (num_of_dict - i))

            line = "ret{0!s} = {1!s}".format(part1, part2)
            self._write_line(line)
            self._indent_level_down()
            if i > 1:
                self._indent_level_down()
            i -= 1

        self._write_line()
        self._write_line('return ret')

Response: Generates code for calling the stored routine in the wrapper method.


Instruction:
Explain what the following Python 3 code does
def _log_query(query):
        query = query.strip()

        if os.linesep in query:
            # Query is a multi line query
            MetadataDataLayer.io.log_very_verbose('Executing query:')
            MetadataDataLayer.io.log_very_verbose('<sql>{0}</sql>'.format(query))
        else:
            # Query is a single line query.
            MetadataDataLayer.io.log_very_verbose('Executing query: <sql>{0}</sql>'.format(query))

Response: Logs the query on the console.


Instruction:
Can you generate the documentation for the following Python 3 function
def request_token(self, board):
    '''send a public key to request a token. When we call this function,
       we already have an RSA key at self.key

       board: the discourse board to post to
    '''
    nonce = str(uuid.uuid4())

    data = {'scopes': 'write',
            'client_id': self.client_id,
            'application_name': 'HelpMe',
            'public_key': self.public_key.replace("'",""),
            'nonce': nonce }

    url = (board + "/user-api-key/new?scopes=write&application_name=HelpMe&public_key=" +
           self.public_key.replace("'", "") +
           "&client_id=" + self.client_id +
           "&nonce=" + nonce )

    bot.newline()
    bot.info('Open browser to:')
    bot.info(url)
    bot.newline()

    # the user will open browser, get a token, and then have it saved here.
    bot.info('Copy paste token, press Ctrl-D to save it:')

    lines = []

    # The message is multiple lines
    while True:
        try:
            line = enter_input()
        except EOFError:
            break
        if line:
            lines.append(line)

    message = "\n".join(lines)

    # Write to temporary file, we only need to get key
    tmpfile = mktemp()
    with open(tmpfile, 'w') as filey:
        filey.write(message)

    # Read in again, and get token **important** is binary
    with open(tmpfile, 'rb') as filey:
        message = filey.read()

    # uses pycryptodome (3.7.2)
    cipher = Cipher_PKCS1_v1_5.new(self.key)
    decrypted = json.loads(cipher.decrypt(b64decode(message), None).decode())

    # Validate nonce is in response
    if "nonce" not in decrypted:
        bot.exit('Missing nonce field in response for token, invalid.')

    # Must return nonce that we sent
    if decrypted['nonce'] != nonce:
        bot.exit('Invalid nonce, exiting.')

    return decrypted['key']

Response: send a public key to request a token


Instruction:
Write a Python 3 script to
create a Discourse post given a title body board and token.

Response:
def create_post(self, title, body, board, category, username):
    '''create a Discourse post, given a title, body, board, and token.

       Parameters
       ==========
       title: the issue title
       body: the issue body
       board: the discourse board to post to

    '''

    category_url = "%s/categories.json" % board
    response = requests.get(category_url)

    if response.status_code != 200:
        print('Error with retrieving %s' % category_url)
        sys.exit(1)

    # Get a list of all categories
    categories = response.json()['category_list']['categories']
    categories = {c['name']:c['id'] for c in categories}

    # And if not valid, warn the user
    if category not in categories:
        bot.warning('%s is not valid, will use default' % category)

    category_id = categories.get(category, None)

    headers = {"Content-Type": "application/json",
               "User-Api-Client-Id": self.client_id,
               "User-Api-Key": self.token }

    # First get the category ids
    data = {'title': title,
            'raw': body,
            'category': category_id}

    response = requests.post("%s/posts.json" % board,
                             headers=headers,
                             data=json.dumps(data))

    if response.status_code in [200, 201, 202]:
        topic = response.json()
        url = "%s/t/%s/%s" %(board, topic['topic_slug'], topic['topic_id'])
        bot.info(url)
        return url

    elif response.status_code == 404:
        bot.error('Cannot post to board, not found. Do you have permission?')
        sys.exit(1)

    else:
        bot.error('Cannot post to board %s' % board)
        bot.error(response.content)
        sys.exit(1)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def main(self, config_filename, file_names=None):
        self._io.title('Loader')

        if file_names:
            self.__load_list(config_filename, file_names)
        else:
            self.__load_all(config_filename)

        if self.error_file_names:
            self.__log_overview_errors()
            return 1
        else:
            return 0

Response: Load the stored routines into the current schema.


Instruction:
Can you tell what is the following Python 3 function doing
def __log_overview_errors(self):
        if self.error_file_names:
            self._io.warning('Routines in the files below are not loaded:')
            self._io.listing(sorted(self.error_file_names))

Response: Log information about stored routines that were not loaded successfully.


Instruction:
How would you code a function in Python 3 to
add a replace part to the map of replace pairs.

Response:
def _add_replace_pair(self, name, value, quote):
        """
        Adds a replace part to the map of replace pairs.

        :param name: The name of the replace pair.
        :param value: The value of value of the replace pair.
        """
        key = '@' + name + '@'
        key = key.lower()

        class_name = value.__class__.__name__

        if class_name in ['int', 'float']:
            value = str(value)
        elif class_name in ['bool']:
            value = '1' if value else '0'
        elif class_name in ['str']:
            if quote:
                value = "'" + value + "'"
        else:
            self._io.log_verbose("Ignoring constant {} which is an instance of {}".format(name, class_name))

        self._replace_pairs[key] = value


Instruction:
Explain what the following Python 3 code does
def __load_list(self, config_filename, file_names):
        self._read_configuration_file(config_filename)
        self.connect()
        self.find_source_files_from_list(file_names)
        self._get_column_type()
        self.__read_stored_routine_metadata()
        self.__get_constants()
        self._get_old_stored_routine_info()
        self._get_correct_sql_mode()
        self.__load_stored_routines()
        self.__write_stored_routine_metadata()
        self.disconnect()

Response: Loads all stored routines in a list into the RDBMS instance.


Instruction:
How would you implement a function in Python 3 that
loads all stored routines into the RDBMS instance.

Response:
def __load_all(self, config_filename):
        """
        Loads all stored routines into the RDBMS instance.

        :param str config_filename: string The filename of the configuration file.
        """
        self._read_configuration_file(config_filename)
        self.connect()
        self.__find_source_files()
        self._get_column_type()
        self.__read_stored_routine_metadata()
        self.__get_constants()
        self._get_old_stored_routine_info()
        self._get_correct_sql_mode()
        self.__load_stored_routines()
        self._drop_obsolete_routines()
        self.__remove_obsolete_metadata()
        self.__write_stored_routine_metadata()
        self.disconnect()


Instruction:
Write a Python 3 function for
reading the parameters from the configuration file.

Response:
def _read_configuration_file(self, config_filename):
        """
        Reads parameters from the configuration file.

        :param str config_filename: The name of the configuration file.
        """
        config = configparser.ConfigParser()
        config.read(config_filename)

        self._source_directory = config.get('loader', 'source_directory')
        self._source_file_extension = config.get('loader', 'extension')
        self._source_file_encoding = config.get('loader', 'encoding')
        self.__shadow_directory = config.get('loader', 'shadow_directory', fallback=None)

        self._pystratum_metadata_filename = config.get('wrapper', 'metadata')

        self._constants_class_name = config.get('constants', 'class')


Instruction:
Make a summary of the following Python 3 code
def __find_source_files(self):
        for dir_path, _, files in os.walk(self._source_directory):
            for name in files:
                if name.lower().endswith(self._source_file_extension):
                    basename = os.path.splitext(os.path.basename(name))[0]
                    relative_path = os.path.relpath(os.path.join(dir_path, name))

                    if basename in self._source_file_names:
                        self._io.error("Files '{0}' and '{1}' have the same basename.".
                                       format(self._source_file_names[basename], relative_path))
                        self.error_file_names.add(relative_path)
                    else:
                        self._source_file_names[basename] = relative_path

Response: Searches recursively for all source files in a directory and adds them to the internal list of error files.


Instruction:
Can you tell what is the following Python 3 function doing
def __read_stored_routine_metadata(self):
        if os.path.isfile(self._pystratum_metadata_filename):
            with open(self._pystratum_metadata_filename, 'r') as file:
                self._pystratum_metadata = json.load(file)

Response: Reads the metadata of stored routines from the metadata file.


Instruction:
Create a Python 3 function to
load all stored routines into the RDBMS instance.

Response:
def __load_stored_routines(self):
        """
        Loads all stored routines into the RDBMS instance.
        """
        self._io.writeln('')

        for routine_name in sorted(self._source_file_names):
            if routine_name in self._pystratum_metadata:
                old_metadata = self._pystratum_metadata[routine_name]
            else:
                old_metadata = None

            if routine_name in self._rdbms_old_metadata:
                old_routine_info = self._rdbms_old_metadata[routine_name]
            else:
                old_routine_info = None

            routine_loader_helper = self.create_routine_loader_helper(routine_name, old_metadata, old_routine_info)
            routine_loader_helper.shadow_directory = self.__shadow_directory

            metadata = routine_loader_helper.load_stored_routine()

            if not metadata:
                self.error_file_names.add(self._source_file_names[routine_name])
                if routine_name in self._pystratum_metadata:
                    del self._pystratum_metadata[routine_name]
            else:
                self._pystratum_metadata[routine_name] = metadata


Instruction:
Write a Python 3 function that can
remove obsolete entries from the metadata of all stored routines.

Response:
def __remove_obsolete_metadata(self):
        """
        Removes obsolete entries from the metadata of all stored routines.
        """
        clean = {}
        for key, _ in self._source_file_names.items():
            if key in self._pystratum_metadata:
                clean[key] = self._pystratum_metadata[key]

        self._pystratum_metadata = clean


Instruction:
Can you generate the documentation for the following Python 3 function
def __write_stored_routine_metadata(self):
        with open(self._pystratum_metadata_filename, 'w') as stream:
            json.dump(self._pystratum_metadata, stream, indent=4, sort_keys=True)

Response: Writes the metadata of all stored routines to the metadata file.


Instruction:
Implement a Python 3 function for
finding all source files that actually exists from a list of file names.

Response:
def find_source_files_from_list(self, file_names):
        """
        Finds all source files that actually exists from a list of file names.

        :param list[str] file_names: The list of file names.
        """
        for file_name in file_names:
            if os.path.exists(file_name):
                routine_name = os.path.splitext(os.path.basename(file_name))[0]
                if routine_name not in self._source_file_names:
                    self._source_file_names[routine_name] = file_name
                else:
                    self._io.error("Files '{0}' and '{1}' have the same basename.".
                                   format(self._source_file_names[routine_name], file_name))
                    self.error_file_names.add(file_name)
            else:
                self._io.error("File not exists: '{0}'".format(file_name))
                self.error_file_names.add(file_name)


Instruction:
Can you generate the documentation for the following Python 3 function
def __get_constants(self):
        helper = ConstantClass(self._constants_class_name, self._io)
        helper.reload()
        constants = helper.constants()

        for name, value in constants.items():
            self._add_replace_pair(name, value, True)

        self._io.text('Read {0} constants for substitution from <fso>{1}</fso>'.
                      format(len(constants), helper.file_name()))

Response: Gets the constants from the class that acts like a namespace for constants and adds them to the replace pairs.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def init_logger(
    name="",
    handler_path_levels=None,
    level=logging.INFO,
    formatter=None,
    formatter_str=None,
    datefmt="%Y-%m-%d %H:%M:%S",
):
    """Add a default handler for logger.

    Args:

    name = '' or logger obj.

    handler_path_levels = [['loggerfile.log',13],['','DEBUG'],['','info'],['','notSet']] # [[path,level]]

    level = the least level for the logger.

    formatter = logging.Formatter(
            '%(levelname)-7s %(asctime)s %(name)s (%(filename)s: %(lineno)s): %(message)s',
             "%Y-%m-%d %H:%M:%S")

    formatter_str = '%(levelname)-7s %(asctime)s  %(name)s (%(funcName)s: %(lineno)s): %(message)s'

    custom formatter:
        %(asctime)s  %(created)f  %(filename)s  %(funcName)s  %(levelname)s  %(levelno)s  %(lineno)s   %(message)s   %(module)s    %(name)s   %(pathname)s   %(process)s   %(relativeCreated)s   %(thread)s  %(threadName)s
    """
    levels = {
        "NOTSET": logging.NOTSET,
        "DEBUG": logging.DEBUG,
        "INFO": logging.INFO,
        "WARNING": logging.WARNING,
        "ERROR": logging.ERROR,
        "CRITICAL": logging.CRITICAL,
    }
    if not formatter:
        if formatter_str:
            formatter_str = formatter_str
        else:
            formatter_str = "%(asctime)s %(levelname)-5s [%(name)s] %(filename)s(%(lineno)s): %(message)s"
        formatter = logging.Formatter(formatter_str, datefmt=datefmt)
    logger = name if isinstance(name, logging.Logger) else logging.getLogger(str(name))
    logger.setLevel(level)
    handler_path_levels = handler_path_levels or [["", "INFO"]]
    # ---------------------------------------
    for each_handler in handler_path_levels:
        path, handler_level = each_handler
        handler = logging.FileHandler(path) if path else logging.StreamHandler()
        handler.setLevel(
            levels.get(handler_level.upper(), 1)
            if isinstance(handler_level, str)
            else handler_level
        )
        handler.setFormatter(formatter)
        logger.addHandler(handler)
    return logger

Response: Initialize a new logger object.


Instruction:
Can you tell what is the following Python 3 function doing
def print_info(*messages, **kwargs):
    sep = kwargs.pop("sep", " ")
    frame = sys._getframe(1)
    ln = frame.f_lineno
    _file = frame.f_globals.get("__file__", "")
    fn = os.path.split(_file)[-1]
    return print_logger.info(
        sep.join(map(unicode, messages)), extra={"ln": ln, "fn": fn}
    )

Response: Simple print use logger print with time and file line number.


Instruction:
Write a Python 3 script to
return a list of field names for a Knockout model.

Response:
def get_fields(model):
    """
    Returns a Model's knockout_fields,
    or the default set of field names.
    """

    try:
        if hasattr(model, "knockout_fields"):
            fields = model.knockout_fields()
        else:
            try:
                fields = model_to_dict(model).keys()
            except Exception as e:
                fields = model._meta.get_fields()
        return fields

    # Crash proofing
    except Exception as e:
        logger.exception(e)
        return []


Instruction:
Can you write a function in Python 3 where it
gives an object and a list of fields recursively build an object for serialization.

Response:
def get_object_data(obj, fields, safe):
    """
    Given an object and a list of fields, recursively build an object for serialization.

    Returns a dictionary.
    """

    temp_dict = dict()
    for field in fields:

        try:
            attribute = getattr(obj, str(field))
            if isinstance(attribute, list) and all([isinstance(item, models.Model) for item in attribute]):
                temp_dict[field] = []
                for item in attribute:
                    temp_dict[field].append(get_object_data(item, get_fields(item), safe))  # Recur
            elif isinstance(attribute, models.Model):
                attribute_fields = get_fields(attribute)
                object_data = get_object_data(attribute, attribute_fields, safe)  # Recur
                temp_dict[field] = object_data
            else:
                if not safe:
                    if isinstance(attribute, basestring):
                        attribute = cgi.escape(attribute)
                temp_dict[field] = attribute

        except Exception as e:
            logger.info("Unable to get attribute.")
            logger.error(e)
            continue

    return temp_dict


Instruction:
How would you implement a function in Python 3 that
returns the Knockout Model and Knockout ViewModel.

Response:
def ko_model(model, field_names=None, data=None):
    """
    Given a model, returns the Knockout Model and the Knockout ViewModel.
    Takes optional field names and data.
    """

    try:
        if isinstance(model, str):
            modelName = model
        else:
            modelName = model.__class__.__name__

        if field_names:
            fields = field_names
        else:
            fields = get_fields(model)

        if hasattr(model, "comparator"):
            comparator = str(model.comparator())
        else:
            comparator = 'id'

        modelViewString = render_to_string(
            "knockout_modeler/model.js",
            {'modelName': modelName, 'fields': fields, 'data': data, 'comparator': comparator}
        )

        return modelViewString
    except Exception as e:
        logger.exception(e)
        return ''


Instruction:
Create a Python 3 function to
return the Knockout data bindings for a given model

Response:
def ko_bindings(model):
    """
    Given a model, returns the Knockout data bindings.
    """

    try:
        if isinstance(model, str):
            modelName = model
        else:
            modelName = model.__class__.__name__

        modelBindingsString = "ko.applyBindings(new " + modelName + "ViewModel(), $('#" + modelName.lower() + "s')[0]);"
        return modelBindingsString

    except Exception as e:
        logger.error(e)
        return ''


Instruction:
Implement a Python 3 function for
giving a QuerySet return just the serialized representation of the Knockout object.

Response:
def ko_json(queryset, field_names=None, name=None, safe=False):
    """
    Given a QuerySet, return just the serialized representation
    based on the knockout_fields. Useful for middleware/APIs.

    Convenience method around ko_data.

    """
    return ko_data(queryset, field_names, name, safe, return_json=True)


Instruction:
Write a Python 3 function for
giving a QuerySet return just the serialized representation of the knockout_fields as JavaScript.

Response:
def ko_data(queryset, field_names=None, name=None, safe=False, return_json=False):
    """
    Given a QuerySet, return just the serialized representation
    based on the knockout_fields as JavaScript.

    """

    try:
        try:
            # Get an inital instance of the QS.
            queryset_instance = queryset[0]
        except TypeError as e:
            # We are being passed an object rather than a QuerySet.
            # That's naughty, but we'll survive.
            queryset_instance = queryset
            queryset = [queryset]
        except IndexError as e:
            if not isinstance(queryset, list):
                # This is an empty QS - get the model directly.
                queryset_instance = queryset.model
            else:
                # We have been given an empty list.
                # Return nothing.
                return '[]'

        modelName = queryset_instance.__class__.__name__
        modelNameData = []

        if field_names is not None:
            fields = field_names
        else:
            fields = get_fields(queryset_instance)

        for obj in queryset:
            object_data = get_object_data(obj, fields, safe)
            modelNameData.append(object_data)

        if name:
            modelNameString = name
        else:
            modelNameString = modelName + "Data"

        dthandler = lambda obj: obj.isoformat() if isinstance(obj, (datetime.date, datetime.datetime)) else None
        dumped_json = json.dumps(modelNameData, default=dthandler)

        if return_json:
            return dumped_json
        return "var " + modelNameString + " = " + dumped_json + ';'
    except Exception as e:
        logger.exception(e)
        return '[]'


Instruction:
Here you have a function in Python 3, explain what it does
def ko(queryset, field_names=None):

    try:
        koDataString = ko_data(queryset, field_names)
        koModelString = ko_model(queryset[0].__class__.__name__, field_names, data=True)
        koBindingsString = ko_bindings(queryset[0])

        koString = koDataString + '\n' + koModelString + '\n' + koBindingsString

        return koString
    except Exception as e:
        logger.error(e)
        return ''

Response: Converts a Django QuerySet into a complete Knockout implementation.


Instruction:
How would you explain what the following Python 3 function does
def load_keypair(keypair_file):
    '''load a keypair from a keypair file. We add attributes key (the raw key)
       and public_key (the url prepared public key) to the client.

       Parameters
       ==========
       keypair_file: the pem file to load.
    '''
    from Crypto.PublicKey import RSA

    # Load key
    with open(keypair_file, 'rb') as filey:
        key = RSA.import_key(filey.read())

    return quote_plus(key.publickey().exportKey().decode('utf-8'))

Response: load a keypair from a keypair file


Instruction:
How would you code a function in Python 3 to
return a string that can be used to construct the exception message.

Response:
def __message(expected_row_count, actual_row_count, query):
        """
        Composes the exception message.

        :param str expected_row_count: The expected row count.
        :param int actual_row_count: The actual row count.
        :param str query: The query.

        :rtype: str
        """
        query = query.strip()

        message = 'Wrong number of rows selected'
        message += os.linesep
        message += 'Expected number of rows: {}'.format(expected_row_count)
        message += os.linesep
        message += 'Actual number of rows: {}'.format(actual_row_count)
        message += os.linesep
        message += 'Query:'
        message += os.linesep if os.linesep in query else ' '
        message += query

        return message


Instruction:
Create a Python 3 function for
submitting the issue to github.

Response:
def _submit(self):
        '''submit the issue to github. When we get here we should have:

           {'user_prompt_issue': 'I want to do the thing.',
            'user_prompt_repo': 'vsoch/hello-world',
            'user_prompt_title': 'Error with this thing',
            'record_asciinema': '/tmp/helpme.93o__nt5.json',
            'record_environment': ((1,1),(2,2)...(N,N))}

           self.token should be propogated with the personal access token
        '''
        body = self.data['user_prompt_issue']
        title = self.data['user_prompt_title']
        repo = self.data['user_prompt_repo']

        # Step 1: Environment

        envars = self.data.get('record_environment')
        body = body + envars_to_markdown(envars)

        # Step 2: Asciinema

        asciinema = self.data.get('record_asciinema')
        if asciinema not in [None, '']:
            url = upload_asciinema(asciinema)

            # If the upload is successful, add a link to it.

            if url is not None:
                body += "\n[View Asciinema Recording](%s)" %url

        # Add other metadata about client

        body += "\n\ngenerated by [HelpMe](https://vsoch.github.io/helpme/)"
        body += "\nHelpMe Github Issue: %s" %(self.run_id)

        # Submit the issue

        issue = create_issue(title, body, repo, self.token)
        return issue


Instruction:
Implement a Python 3 function for
resolving JSON references in a given OpenAPI spec.

Response:
def resolve_refs(uri, spec):
    """Resolve JSON references in a given dictionary.

    OpenAPI spec may contain JSON references to its nodes or external
    sources, so any attempt to rely that there's some expected attribute
    in the spec may fail. So we need to resolve JSON references before
    we use it (i.e. replace with referenced object). For details see:

        https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-02

    The input spec is modified in-place despite being returned from
    the function.
    """
    resolver = jsonschema.RefResolver(uri, spec)

    def _do_resolve(node):
        if isinstance(node, collections.Mapping) and '$ref' in node:
            with resolver.resolving(node['$ref']) as resolved:
                result = deepcopy(resolved)
                for key in resolved:
                    if key in node:
                        merge(result[key], node[key])
                return result
        elif isinstance(node, collections.Mapping):
            for k, v in node.items():
                node[k] = _do_resolve(v)
        elif isinstance(node, (list, tuple)):
            for i in range(len(node)):
                node[i] = _do_resolve(node[i])
        return node

    return _do_resolve(spec)


Instruction:
Here you have a function in Python 3, explain what it does
def main(args, extras):
    '''This is the actual driver for the helper.
    '''

    from helpme.main import get_helper
    name = args.command

    if name in HELPME_HELPERS:

        # Get the helper, do the recording, submit

        helper = get_helper(name=name)

        if args.asciinema is not None:
            if os.path.exists(args.asciinema):
                helper.data['record_asciinema'] = args.asciinema

        helper.run(positionals=extras)

Response: This is the main function for the helper. It is the main function for the helper.


Instruction:
Implement a Python 3 function for
extracting the cleaned html tree as a string and only css rules matching the cleaned html tree.

Response:
def extract(self, html_contents, css_contents=None, base_url=None):
        """
        Extracts the cleaned html tree as a string and only
        css rules matching the cleaned html tree

        :param html_contents: The HTML contents to parse
        :type html_contents: str
        :param css_contents: The CSS contents to parse
        :type css_contents: str
        :param base_url: The base page URL to use for relative to absolute links
        :type base_url: str

        :returns: cleaned HTML contents, cleaned CSS contents
        :rtype: str or tuple
        """
        # Clean HTML
        html_extractor = self.html_extractor(
            html_contents, self._xpaths_to_keep, self._xpaths_to_discard)
        has_matches = html_extractor.parse()

        if has_matches:

            # Relative to absolute URLs
            if base_url is not None:
                html_extractor.rel_to_abs(base_url)

            # Convert ElementTree to string
            cleaned_html = html_extractor.to_string()

        else:
            cleaned_html = None

        # Clean CSS
        if css_contents is not None:

            if cleaned_html is not None:

                css_extractor = self.css_extractor(css_contents, cleaned_html)
                css_extractor.parse()

                # Relative to absolute URLs
                if base_url is not None:
                    css_extractor.rel_to_abs(base_url)

                cleaned_css = css_extractor.to_string()

            else:
                cleaned_css = None

        else:
            return cleaned_html

        return (cleaned_html, cleaned_css)


Instruction:
Implement a Python 3 function for
adding a Xpath expression to the dest list

Response:
def __add(self, dest, xpath):
        """
        Adds a Xpath expression to the dest list

        :param dest: The destination list to add the Xpath
        :type dest: list
        :param xpath: The Xpath expression to add
        :type xpath: str
        """
        assert isinstance(xpath, string_types)
        dest.append(xpath)


Instruction:
Can you implement a function in Python 3 that
appends a line of code to the generated code and adjusts the indent level of the generated code.

Response:
def _write_line(self, line=None):
        """
        Appends a line of code to the generated code and adjust the indent level of the generated code.

        :param line: The line of code (with out LF) that must be appended.
        """
        if line is None:
            self._write("\n")
            if self.__indent_level > 1:
                self.__indent_level -= 1
        elif line == '':
            self._write("\n")
        else:
            line = (' ' * 4 * self.__indent_level) + line
            if line[-1:] == ':':
                self.__indent_level += 1
            self._write(line + "\n")


Instruction:
Write a Python 3 script for
writing a separator line at the top of the generated code.

Response:
def _write_separator(self):
        """
        Inserts a horizontal (commented) line tot the generated code.
        """
        tmp = self._page_width - ((4 * self.__indent_level) + 2)
        self._write_line('# ' + ('-' * tmp))


Instruction:
Explain what the following Python 3 code does
def write_routine_method(self, routine):
        if self._lob_as_string_flag:
            return self._write_routine_method_without_lob(routine)
        else:
            if self.is_lob_parameter(routine['parameters']):
                return self._write_routine_method_with_lob(routine)
            else:
                return self._write_routine_method_without_lob(routine)

Response: Returns a complete wrapper method.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _write_docstring_parameters(self, routine):
        if routine['pydoc']['parameters']:
            self._write_line('')

            for param in routine['pydoc']['parameters']:
                lines = param['description'].split(os.linesep)
                self._write_line(':param {0} {1}: {2}'.format(param['python_type'],
                                                              param['parameter_name'],
                                                              lines[0]))
                del lines[0]

                tmp = ':param {0} {1}:'.format(param['python_type'], param['parameter_name'])
                indent = ' ' * len(tmp)
                for line in lines:
                    self._write_line('{0} {1}'.format(indent, line))

                self._write_line('{0} {1}'.format(indent, param['data_type_descriptor']))

Response: Writes the parameters part of the docstring for the wrapper method of a stored routine.


Instruction:
How would you code a function in Python 3 to
write the return type part of the docstring for the stored routine.

Response:
def __write_docstring_return_type(self):
        """
        Writes the return type part of the docstring for the wrapper method of a stored routine.
        """
        rtype = self._get_docstring_return_type()
        if rtype:
            self._write_line('')
            self._write_line(':rtype: {0}'.format(rtype))


Instruction:
Write a Python 3 function that can
write the docstring for the wrapper method of a stored routine.

Response:
def __write_docstring(self, routine):
        """
        Writes the docstring for the wrapper method of a stored routine.

        :param dict routine: The metadata of the stored routine.
        """
        self._write_line('"""')

        self.__write_docstring_description(routine)
        self._write_docstring_parameters(routine)
        self.__write_docstring_return_type()

        self._write_line('"""')


Instruction:
Make a summary of the following Python 3 code
def _get_wrapper_args(routine):
        ret = ''

        for parameter_info in routine['parameters']:
            if ret:
                ret += ', '

            ret += parameter_info['name']

        return ret

Response: Returns code for the parameters of the wrapper method for the stored routine.


Instruction:
Write a Python 3 function that can
generate the ld - relay. conf file.

Response:
def generate_relay_config(self, environments: list) -> None:
        """Generate ld-relay.conf file.

        Given a list of environments of a project, this will generate a
        ``ld-relay.conf`` file in the current working directory. The conf file
        follows the specification that is documented in the main `ld-relay`_
        documentation.

        .. _ld-relay: https://github.com/launchdarkly/ld-relay#configuration-file-format

        :param environments: list of LaunchDarkly environments.
        """
        template = self.env.get_template('ld-relay.conf.jinja')

        with open('ld-relay.conf', 'w') as ld_relay_config:
            template = template.render(
                envs=environments
            )
            ld_relay_config.write(template)


Instruction:
Implement a function in Python 3 to
load dynamically the class that acts like a namespace for constants.

Response:
def __load(self):
        """
        Loads dynamically the class that acts like a namespace for constants.
        """
        parts = self.__class_name.split('.')
        module_name = ".".join(parts[:-1])
        module = __import__(module_name)
        modules = []
        for comp in parts[1:]:
            module = getattr(module, comp)
            modules.append(module)

        self.__module = modules[-2]


Instruction:
Given the following Python 3 function, write the documentation
def constants(self):
        ret = {}

        name = self.__class_name.split('.')[-1]
        constant_class = getattr(self.__module, name)
        for name, value in constant_class.__dict__.items():
            if re.match(r'^[A-Z][A-Z0-9_]*$', name):
                ret[name] = value

        return ret

Response: Returns the constants from the class that acts like a namespace for constants.


Instruction:
Implement a Python 3 function for
extracting the following info from the source of the module with the class that acts like a namespace for constants.

Response:
def __extract_info(self, lines):
        """
        Extracts the following info from the source of the module with the class that acts like a namespace for
        constants:
        * Start line with constants
        * Last line with constants
        * Indent for constants

        :param list[str] lines: The source of the module with the class that acts like a namespace for constants.

        :rtype: dict<str,int|str>
        """
        ret = {'start_line': 0,
               'last_line':  0,
               'indent':     ''}

        mode = 1
        count = 0
        for line in lines:
            if mode == 1:
                if line.strip() == self.__annotation:
                    ret['start_line'] = count + 1
                    ret['last_line'] = count + 1
                    parts = re.match(r'^(\s+)', line)
                    ret['indent'] = parts.group(1)
                    mode = 2

            elif mode == 2:
                if line.strip() == '' or line.strip()[0:1] == '#':
                    mode = 3
                else:
                    ret['last_line'] = count + 1

            else:
                break

            count += 1

        if mode != 3:
            raise RuntimeError("Unable to find '{}' in file {}".format(self.__annotation, self.source()))

        return ret


Instruction:
Create a Python 3 function to
return the source of the module with the class that acts like a namespace for constants with new constants.

Response:
def source_with_constants(self, constants):
        """
        Returns the source of the module with the class that acts like a namespace for constants with new constants.

        :param dict[str,int] constants: The new constants.

        :rtype: str
        """
        old_lines = self.source().split("\n")
        info = self.__extract_info(old_lines)

        new_lines = old_lines[0:info['start_line']]

        for constant, value in sorted(constants.items()):
            new_lines.append("{0}{1} = {2}".format(info['indent'], str(constant), str(value)))

        new_lines.extend(old_lines[info['last_line']:])

        return "\n".join(new_lines)


Instruction:
Explain what the following Python 3 code does
def get_configfile_user():
    '''return the full path for the user configuration file. If doesn't
       exist, create it for the user.
    '''
    from helpme.defaults import HELPME_CLIENT_SECRETS

    # The inital file has a funny username

    if not os.path.exists(HELPME_CLIENT_SECRETS):
        bot.debug('Generating settings file at %s' % HELPME_CLIENT_SECRETS)
        config_dir = os.path.dirname(HELPME_CLIENT_SECRETS)

        # The configuration directory might be needed for different clients
        if not os.path.exists(config_dir):
            mkdir_p(config_dir)

        name = RobotNamer().generate()

        # Generate the user config

        config = configparser.ConfigParser()
        config['DEFAULT'] = {'Alias': name }
        write_config(HELPME_CLIENT_SECRETS, config)

    return HELPME_CLIENT_SECRETS

Response: return the full path for the user configuration file. If doesn t exist create it.


Instruction:
Can you generate the documentation for the following Python 3 function
def _remove_setting(section, name, configfile, save=False):
    '''remove a setting from the global config
    '''
    removed = False
    config = _load_config(configfile)
    if section in config:
        if name.lower() in config[section]:
            removed = config.remove_option(section, name)

    # Does the user want to save the file?
    if removed and save:
        write_config(configfile, config)

    return removed

Response: remove a setting from the global config file


Instruction:
Can you generate a brief explanation for the following Python 3 code
def remove_setting(self, section, name, save=False):
    '''remove a setting from the global config
    '''
    configfile = get_configfile()
    return _remove_setting(section, name, configfile, save)

Response: remove a setting from the global config


Instruction:
Can you generate the documentation for the following Python 3 function
def remove_user_setting(self, section, name, save=False):
    '''remove a setting from the user config
    '''
    configfile = get_configfile_user()
    return _remove_setting(section, name, configfile, save)

Response: remove a setting from the user config


Instruction:
Implement a function in Python 3 to
load a tuple of environment variables to add to the user settings

Response:
def load_envars(self, items):
    '''load a tuple of environment variables, to add to the user settings

        Example:

        items = [('HELPME_DISCOURSE_BOARD', 'user_prompt_board'),
                 ('HELPME_DISCOURSE_CATEGORY', 'user_prompt_category')]

        # Note that it's added to the client with an underscore:
        self._load_envars()
    '''
    for item in items:
        envar = item[0]
        key = item[1]
        value = self._get_and_update_setting(envar)

        if value != None:
            self.data[key] = value
            self.config.remove_option(self.name, key)


Instruction:
Implement a Python 3 function for
returning a setting from the environment then secrets file or main config.

Response:
def get_setting(self, name, section=None, default=None, user=True):
    '''return a setting from the environment (first priority) and then
       secrets (second priority) if one can be found. If not, return None.

       Parameters
       ==========
       section: the section in the config, defaults to self.name
       name: they key (index) of the setting to look up
       default: (optional) if not found, return default instead.
       user: if True, load from user config. Otherwise, main config

    '''
    loader = self._load_config_user
    if not user:
        loader = self._load_config

    if section is None:
        section = self.name

    # First priority is the environment

    setting = os.environ.get(name)

    # Second priority is the secrets file

    if setting is None:

        # Loads user config on level of helper (already indexed)
        config = loader()
        if section in config:
            if name.lower() in config[section]:
                setting = config[section][name.lower()]

    # Third priority, return a default

    if setting is None and default is not None:
        setting = default

    return setting


Instruction:
Create a Python 3 function to
get all settings for a client

Response:
def get_settings(self):
    '''get all settings for a client, if defined in config.
    '''
    config = self._load_config_user()
    if self.name in config:
        return config[self.name]


Instruction:
Write a Python 3 function for
updating client secrets will update the data structure for a particular token authentication.

Response:
def update_settings(self, updates, config=None):
    '''update client secrets will update the data structure for a particular
       authentication. This should only be used for a (quasi permanent) token
       or similar. The secrets file, if found, is updated and saved by default.

       Parameters
       ==========
       helper: the name of the helper to look up in the config
       updates: a dictionary of key:value pairs to add to the config
       config: a configparser.ConfigParser(), if already loaded

    '''
    if config is None:
        config = self._load_config_user()

    if self.name not in config:
        config[self.name] = {}

    config[self.name].update(updates)

    # Update the saved file

    configfile = get_configfile_user()
    write_config(configfile, config)
    return config


Instruction:
Can you generate the documentation for the following Python 3 function
def get_and_update_setting(self, name, default=None, user=True):
    '''Look for a setting in the environment (first priority) and then
       the settings file (second). If something is found, the settings
       file is updated. The order of operations works as follows:

       1. The user config file is used as a cache for the variable
       2. the environment variable always takes priority to cache, and if
          found, will update the cache.
       3. If the variable is not found and the cache is set, we are good
       5. If the variable is not found and the cache isn't set, return
          default (default is None)

       So the user of the function can assume a return of None equates to
       not set anywhere, and take the appropriate action.
    '''

    setting = self._get_setting(name, user=user)

    if setting is None and default is not None:
        setting = default

    # If the setting is found, update the client secrets
    if setting is not None:
        updates = {name : setting}
        self._update_settings(updates)

    return setting

Response: Look for a setting in the environment and then update the client secrets with the new values.


Instruction:
Make a summary of the following Python 3 code
def run(app, argv=sys.argv, extra_args=None):
    if app not in argv[:2]:
        # app is automatically added if not present
        argv.insert(1, app)
    if len(argv) < 3 and 'test' not in argv[:2]:
        # test argument is given if not argument is passed
        argv.insert(2, 'test')
    if extra_args:
        argv.extend(extra_args)
    return runner(argv)

Response: Run commands in a plain django environment


Instruction:
Implement a Python 3 function for
running commands in a django CMS environment

Response:
def cms(app, argv=sys.argv, extra_args=None):
    """
    Run commands in a django cMS environment

    :param app: application
    :param argv: arguments (default to sys.argv)
    :param extra_args: list of extra arguments
    """
    try:
        import cms  # NOQA  # nopyflakes
    except ImportError:
        print('runner.cms is available only if django CMS is installed')
        raise
    if app not in argv[:2]:
        # app is automatically added if not present
        argv.insert(1, app)
    if len(argv) < 3 and 'test' not in argv[:2]:
        # test argument is given if not argument is passed
        argv.insert(2, 'test')
    if '--cms' not in argv:
        # this is the cms runner, just add the cms argument
        argv.append('--cms')
    if extra_args:
        argv.extend(extra_args)
    return runner(argv)


Instruction:
Can you generate the documentation for the following Python 3 function
def setup(app, helper_module, extra_args=None, use_cms=False):
    helper = helper_module.__file__
    argv = [os.path.basename(helper), app, 'setup', '--extra-settings={0}'.format(helper)]
    if use_cms:
        argv.append('--cms')
    if extra_args:
        argv.extend(extra_args)
    return runner(argv)

Response: Setup the Django environment and return the environment settings.


Instruction:
Write a Python 3 script for
parsing the HTML and returns a cleaned lxml ElementTree with the elements removed.

Response:
def parse(self):
        """
        Returns a cleaned lxml ElementTree

        :returns: Whether the cleaned HTML has matches or not
        :rtype: bool
        """
        # Create the element tree
        self.tree = self._build_tree(self.html_contents)

        # Get explicits elements to keep and discard
        self.elts_to_keep = self._get_elements_to_keep()
        self.elts_to_discard = self._get_elements_to_discard()

        # Init an empty list of Elements to remove
        self.elts_to_remove = []

        # Check if the root is a match or if there is any matches
        is_root = self._is_keep(self.tree)
        has_descendant = self._has_keep_elt_in_descendants(self.tree)

        if not(is_root or has_descendant):
            return False

        # Parse and clean the ElementTree
        self._parse_element(self.tree, parent_is_keep=is_root)
        self._remove_elements(self.elts_to_remove)

        return True


Instruction:
Make a summary of the following Python 3 code
def rel_to_abs(self, base_url):
        # Delete target attributes
        strip_attributes(self.tree, 'target')

        # Absolute links
        self.tree.rewrite_links(
            lambda link: urljoin(base_url, link)
            if not link.startswith(self.rel_to_abs_excluded_prefixes) else link)

        # Extra attributes
        onclick_elements = self.tree.xpath('//*[@onclick]')

        for element in onclick_elements:
            # Replace attribute with absolute URL
            element.set('onclick', self.javascript_open_re.sub(
                lambda match: '%s%s%s' % (match.group('opening'),
                        urljoin(base_url, match.group('url')),
                        match.group('ending')),
                element.get('onclick')))

Response: Converts relative links from html contents to absolute links


Instruction:
Create a Python 3 function to
return the list of HtmlElements for the source

Response:
def _get_elements(self, source):
        """
        Returns the list of HtmlElements for the source

        :param source: The source list to parse
        :type source: list
        :returns: A list of HtmlElements
        :rtype: list
        """
        return list(chain(*[self.tree.xpath(xpath) for xpath in source]))


Instruction:
Can you tell what is the following Python 3 function doing
def _parse_element(self, elt, parent_is_keep=False):
        for e in elt.iterchildren():

            is_discard_element = self._is_discard(e)
            is_keep_element = self._is_keep(e)

            # Element is an explicit one to discard, flag it and continue
            if is_discard_element and not is_keep_element:
                self.elts_to_remove.append(e)
                continue

            if not parent_is_keep:
                # Parent element is not an explicit keep, normal process
                # Element is an explicit one to keep, inspect it
                if is_keep_element:
                    self._parse_element(e, parent_is_keep=True)
                    continue

                # Has a descendant to keep, inspect it
                if self._has_keep_elt_in_descendants(e):
                    self._parse_element(e)
                    continue

                # Element did not match anything, remove it
                self.elts_to_remove.append(e)

            else:
                # Element is a child of a keep element, only check explicit discards
                self._parse_element(e, parent_is_keep=True)

Response: Parses an element recursively and returns a list of all the related elements.


Instruction:
Can you implement a function in Python 3 that
returns whether the element has a descendant to keep or not

Response:
def _has_keep_elt_in_descendants(self, elt):
        """
        Returns whether the element has a descendant to keep or not

        :param elt: The HtmlElement to check
        :type elt: lxml.html.HtmlElement
        :returns: True if the element has a keep element in its descendants
        :rtype: bool
        """
        # iterdescendants is a generator, don't cast it as a list to avoid
        # parsing the whole descendants tree if not necessary
        for d in elt.iterdescendants():
            if d in self.elts_to_keep:
                return True

        return False


Instruction:
Can you create a Python 3 function that
removes flagged elements from the ElementTree.

Response:
def _remove_elements(self, elts_to_remove):
        """
        Removes flagged elements from the ElementTree
        """
        for e in elts_to_remove:

            # Get the element parent
            parent = e.getparent()

            # lxml also remove the element tail, preserve it
            if e.tail and e.tail.strip():
                parent_text = parent.text or ''
                parent.text = parent_text + e.tail

            # Remove the element
            e.getparent().remove(e)


Instruction:
How would you explain what the following Python 3 function does
def getSystemInfo(self, timeout=1):
        namespace = System.getServiceType("getSystemInfo")
        uri = self.getControlURL(namespace)

        results = self.execute(uri, namespace, "GetInfo", timeout=timeout)

        return SystemInfo(results)

Response: Execute GetInfo action to get information about the System on the device.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def getTimeInfo(self, timeout=1):
        namespace = System.getServiceType("getTimeInfo")
        uri = self.getControlURL(namespace)

        results = self.execute(uri, namespace, "GetInfo", timeout=timeout)

        return TimeInfo(results)

Response: Execute GetInfo action to get information about the time on the device.


Instruction:
Write a Python 3 function for
returning if a software update is available for this service.

Response:
def softwareUpdateAvailable(self, timeout=1):
        """Returns if a software update is available

        :return: if a software update is available
        :rtype: bool
        """
        namespace = System.getServiceType("softwareUpdateAvailable")
        uri = self.getControlURL(namespace)

        results = self.execute(uri, namespace, "GetInfo", timeout=timeout)

        return bool(int(results["NewUpgradeAvailable"]))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def make_headers(headers):
    out = {}
    if 'etag' in headers:
        out['if-none-match'] = headers['etag']
    if 'last-modified' in headers:
        out['if-modified-since'] = headers['last-modified']
    return out

Response:
Make the cache control headers based on a previous request s
    response headers


Instruction:
Given the following Python 3 function, write the documentation
def get(self, prefix, url, schema_version=None):
        if not self.cache_dir:
            return None

        filename = self._get_cache_file(prefix, url)

        try:
            with open(filename, 'rb') as file:
                item = pickle.load(file)
            if schema_version and schema_version != item.schema:
                LOGGER.debug("Cache get %s %s: Wanted schema %d, got %d",
                             prefix, url,
                             schema_version, item.schema)
                return None
            return item
        except FileNotFoundError:
            pass
        except Exception:  # pylint:disable=broad-except
            _, msg, _ = sys.exc_info()
            LOGGER.warning("Cache get %s %s failed: %s", prefix, url, msg)

        return None

Response: Get the cached object for a given url.


Instruction:
Can you tell what is the following Python 3 function doing
def set(self, prefix, url, obj):
        if not self.cache_dir:
            return

        filename = self._get_cache_file(prefix, url)

        try:
            os.makedirs(os.path.join(self.cache_dir, prefix))
        except OSError:
            pass

        with open(filename, 'wb') as file:
            pickle.dump(obj, file)

Response: Add an object into the cache


Instruction:
Implement a Python 3 function for
joining input path to sample data path

Response:
def join_path(*path_to_join, **kwargs):
    """Join input path to sample data path (usually in ~/lisa_data)

    :param path_to_join: one or more paths
    :param get_root: return dataset root path. If false, the path would be into "medical/orig"
    :return: joined path
    """
    if "get_root" in kwargs:
        get_root = kwargs["get_root"]
    else:
        # default value
        get_root = False
    sdp = dataset_path(get_root=get_root)
    pth = os.path.join(sdp, *path_to_join)
    logger.debug("sample_data_path" + str(sdp))
    logger.debug("path " + str(pth))
    return pth


Instruction:
Can you generate the documentation for the following Python 3 function
def set_dataset_path(path, cache=None, cachefile="~/.io3d_cache.yaml"):
    if cachefile is not None:
        cache = cachef.CacheFile(cachefile)
    cache.update("local_dataset_dir", path)

Response: Sets path to dataset.


Instruction:
Write a Python 3 script for
getting dataset path. :param cache: CacheFile object :param cachefile: cachefile path, default '~/.io3d_cache.yaml' :return: path to dataset

Response:
def dataset_path(cache=None, cachefile="~/.io3d_cache.yaml", get_root=False):
    """Get dataset path.

    :param cache: CacheFile object
    :param cachefile: cachefile path, default '~/.io3d_cache.yaml'
    :return: path to dataset
    """
    local_data_dir = local_dir

    if cachefile is not None:
        cache = cachef.CacheFile(cachefile)
        # cache.update('local_dataset_dir', head)
    if cache is not None:
        local_data_dir = cache.get_or_save_default("local_dataset_dir", local_dir)

    if get_root:
        local_data_dir
    else:
        logger.warning("Parameter")
        local_data_dir = op.join(local_data_dir, "medical", "orig")

    return op.expanduser(local_data_dir)


Instruction:
Implement a function in Python 3 to
give you metadata for dataset chosen via label

Response:
def get_dataset_meta(label):
    """Gives you metadata for dataset chosen via 'label' param

    :param label: label = key in data_url dict (that big dict containing all possible datasets)
    :return: tuple (data_url, url, expected_hash, hash_path, relative_download_dir)
    relative_download_dir says where will be downloaded the file from url and eventually unzipped

    """
    data_url = data_urls[label]
    if type(data_url) == str:
        # back compatibility
        data_url = [data_url]
    if type(data_url) == list:
        data_url.extend([None, None, None, None])
        data_url = data_url[:4]
        url, expected_hash, hash_path, relative_donwload_dir = data_url
        if hash_path is None:
            hash_path = label
    # elif type(data_url) == dict:
    return data_url, url, expected_hash, hash_path, relative_donwload_dir


Instruction:
Can you create a Python 3 function that
returns list of possible packages contained in dataset in multi dataset eg. lisa.

Response:
def _expand_dataset_packages(dataset_label_dict):
    """Returns list of possible packages contained in dataset, in case the dataset is multi dataset, eg. 'lisa'.

    In case the param is not pointing to multidataset returns only that label in a list.

    :param str dataset_label_dict: label of multi dataset
    :return: list of labels
    """
    new_dataset_label_dict = []
    for label in dataset_label_dict:
        dataset_metadata = data_urls[label]
        if type(dataset_metadata) == dict and "package" in dataset_metadata:
            new_dataset_label_dict.extend(dataset_metadata["package"])
        else:
            new_dataset_label_dict.append(label)
    return new_dataset_label_dict


Instruction:
Can you create a Python 3 function that
downloads sample data by label.

Response:
def download(dataset_label=None, destination_dir=None, dry_run=False):
    """Download sample data by data label. Warning: function with side effect!

    Labels can be listed by sample_data.data_urls.keys(). Returns downloaded files.

    :param dataset_label: label of data. If it is set to None, all data are downloaded
    :param destination_dir: output dir for data
    :param dry_run: runs function without downloading anything
    """
    if destination_dir is None:
        destination_dir = op.join(dataset_path(get_root=True), "medical", "orig")

    destination_dir = op.expanduser(destination_dir)
    logger.info("destination dir: {}".format(destination_dir))

    if dataset_label is None:
        dataset_label = data_urls.keys()

    if type(dataset_label) == str:
        dataset_label = [dataset_label]

    dataset_label = _expand_dataset_packages(dataset_label)

    for label in dataset_label:
        # make all data:url have length 3
        data_url, url, expected_hash, hash_path, relative_download_dir = get_dataset_meta(
            label
        )
        if relative_download_dir is None:
            label_destination_dir = destination_dir
        else:
            label_destination_dir = op.join(destination_dir, relative_download_dir)
        if not op.exists(label_destination_dir):
            logger.debug("creating directory {}".format(label_destination_dir))
            os.makedirs(label_destination_dir)

        if hash_path is None:
            hash_path = label

        path_to_hash = os.path.join(label_destination_dir, hash_path)
        try:
            computed_hash = checksum(path_to_hash)
        except Exception as e:
            # there is probably no checksumdir module
            logger.warning(e)
            logger.warning("problem with sample_data.checksum()")
            computed_hash = None

        logger.info("dataset: '" + label + "'")
        logger.info("path to hash: {}".format(path_to_hash))
        logger.info("expected hash: '" + str(expected_hash) + "'")
        logger.info("computed hash: '" + str(computed_hash) + "'")
        if (computed_hash is not None) and (expected_hash == computed_hash):
            logger.info("match ok - no download needed")
        else:
            logger.info("downloading")
            if not dry_run:
                downzip(url, destination=label_destination_dir)
                logger.info("finished")
                downloaded_hash = checksum(
                    os.path.join(label_destination_dir, hash_path)
                )
                logger.info("downloaded hash: '" + str(downloaded_hash) + "'")
                if downloaded_hash != expected_hash:
                    logger.warning(
                        "downloaded hash is different from expected hash\n"
                        + "expected hash: '"
                        + str(expected_hash)
                        + "'\n"
                        + "downloaded hash: '"
                        + str(downloaded_hash)
                        + "'\n"
                    )
            else:
                logger.debug("dry run")


Instruction:
Make a summary of the following Python 3 code
def get_old(dataset_label, data_id, destination_dir=None):
    # TODO implement
    if destination_dir is None:
        destination_dir = op.join(dataset_path(get_root=True), "medical", "orig")

    destination_dir = op.expanduser(destination_dir)
    data_url, url, expected_hash, hash_path, relative_output_path = get_dataset_meta(
        dataset_label
    )
    paths = glob.glob(os.path.join(destination_dir, hash_path))
    paths.sort()
    import fnmatch

    print(paths)
    print(data_id)
    pathsf = fnmatch.filter(paths, data_id)
    print(pathsf)
    datap = io3d.read(pathsf[0], dataplus_format=True)
    return datap

Response: Get the 3D data from specified dataset with specified id.


Instruction:
How would you implement a function in Python 3 that
returns checksum of files given by path.

Response:
def checksum(path, hashfunc="md5"):
    """Return checksum of files given by path.

    Wildcards can be used in check sum. Function is strongly dependent on checksumdir package by 'cakepietoast'.

    :param path: path of files to get hash from
    :param hashfunc: function used to get hash, default 'md5'
    :return: (str) hash of the file/files given by path
    """
    import checksumdir

    hash_func = checksumdir.HASH_FUNCS.get(hashfunc)
    if not hash_func:
        raise NotImplementedError("{} not implemented.".format(hashfunc))

    if os.path.isdir(path):
        return checksumdir.dirhash(path, hashfunc=hashfunc)

    hashvalues = []
    path_list = list(sorted(glob.glob(path)))
    logger.debug("path_list: len: %i", len(path_list))
    if len(path_list) > 0:
        logger.debug("first ... last: %s ... %s", str(path_list[0]), str(path_list[-1]))

    for path in path_list:
        if os.path.isfile(path):
            hashvalues.append(checksumdir._filehash(path, hashfunc=hash_func))
    logger.debug("one hash per file: len: %i", len(hashvalues))
    if len(path_list) > 0:
        logger.debug("first ... last: %s ... %s", str(hashvalues[0]), str(hashvalues[-1]))
    checksum_hash = checksumdir._reduce_hash(hashvalues, hashfunc=hash_func)
    logger.debug("total hash: {}".format(str(checksum_hash)))
    return checksum_hash


Instruction:
Given the following Python 3 function, write the documentation
def generate_donut():
    segmentation = np.zeros([20, 30, 40])
    # generate test data
    segmentation[6:10, 7:24, 10:37] = 1
    segmentation[6:10, 7, 10] = 0
    segmentation[6:10, 23, 10] = 0
    segmentation[6:10, 7, 36] = 0
    segmentation[6:10, 23, 36] = 0
    segmentation[2:18, 12:19, 18:28] = 2

    data3d = segmentation * 100 + np.random.random(segmentation.shape) * 30
    voxelsize_mm = [3, 2, 1]

    datap = {
        "data3d": data3d,
        "segmentation": segmentation,
        "voxelsize_mm": voxelsize_mm,
    }
    # io3d.write(datap, "donut.pklz")
    return datap

Response: Generate donut like shape with stick inside


Instruction:
Can you generate a brief explanation for the following Python 3 code
def generate_round_data(
    sz=32, offset=0, radius=7, seedsz=3, add_object_without_seeds=False
):
    """
    Generate data with two sphere objects.
    :param sz: output data shape is [sz, sz+1, sz+2]
    :param offset:
    :param radius:
    :param seedsz:
    :param add_object_without_seeds: Add also one cube-like object in the corner.
    :return:
    """

    import scipy.ndimage

    # seedsz= int(sz/10)
    space = 2
    seeds = np.zeros([sz, sz + 1, sz + 2], dtype=np.int8)
    xmin = radius + seedsz + offset + 2
    ymin = radius + seedsz + offset + 6
    seeds[offset + 12, xmin + 3 : xmin + 7 + seedsz, ymin : ymin + 2] = 1
    seeds[offset + 20, xmin + 7 : xmin + 12 + seedsz, ymin + 5 : ymin + 7] = 1

    # add temp seed
    if add_object_without_seeds:
        seeds[-3, -3, -3] = 1
    img = np.ones([sz, sz + 1, sz + 2])
    img = img - seeds

    seeds[2 : 10 + seedsz, 2 : 9 + seedsz, 2 : 3 + seedsz] = 2

    # remove temo seed
    if add_object_without_seeds:
        seeds[-3, -3, -3] = 0

    img = scipy.ndimage.morphology.distance_transform_edt(img)
    segm = img < radius
    img = (100 * segm + 80 * np.random.random(img.shape)).astype(np.uint8)
    return img, segm, seeds

Response: Generate data with two sphere objects.


Instruction:
Implement a function in Python 3 to
create synthetic data. There is some liver and porta -like object. :return data3d, segmentation, voxelsize_mm, slab, seeds_liver, seeds_porta:

Response:
def generate_synthetic_liver(return_dataplus=False):
    """
    Create synthetic data. There is some liver and porta -like object.
    :return data3d, segmentation, voxelsize_mm, slab, seeds_liver, seeds_porta:
    """
    # data
    slab = {"none": 0, "liver": 1, "porta": 2}
    voxelsize_mm = np.array([1.0, 1.0, 1.2])

    segm = np.zeros([80, 256, 250], dtype=np.int16)

    # liver
    segm[30:60, 70:180, 40:190] = slab["liver"]

    # porta
    segm[40:45, 120:130, 70:190] = slab["porta"]
    segm[41:44, 122:127, 68:70] = slab[
        "porta"
    ]  # hack to fix stability of skeleton algorithm
    #
    segm[40:45, 80:130, 100:110] = slab["porta"]
    segm[42:44, 77:80, 103:106] = slab[
        "porta"
    ]  # hack to fix stability of skeleton algorithm
    # segm[41:44, 78:80, 101:109] = slab['porta']
    # vertical branch under main branch
    segm[40:44, 120:170, 130:135] = slab["porta"]

    data3d = np.zeros(segm.shape)
    data3d[segm == slab["liver"]] = 146
    data3d[segm == slab["porta"]] = 206
    noise = np.random.normal(0, 10, segm.shape)  # .astype(np.int16)
    data3d = (data3d + noise).astype(np.int16)

    seeds_liver = np.zeros(data3d.shape, np.int8)
    seeds_liver[40:55, 90:120, 70:110] = 1
    seeds_liver[30:45, 190:200, 40:90] = 2

    seeds_porta = np.zeros(data3d.shape, np.int8)
    seeds_porta[40:45, 121:139, 80:95] = 1

    if return_dataplus:
        datap = {
            "data3d": data3d,
            "voxelsize_mm": voxelsize_mm,
            "slab": slab,
            "seeds_liver": seeds_liver,
            "seeds_porta": seeds_porta,
            "segmentation": segm,
        }
        return datap
    else:
        return data3d, segm, voxelsize_mm, slab, seeds_liver, seeds_porta


Instruction:
Implement a Python 3 function for
creating 2D binar face with 2D smiles.

Response:
def _get_face2(shape=None, face_r=1.0, smile_r1=0.5, smile_r2=0.7, eye_r=0.2):
    """
    Create 2D binar face
    :param shape:
    :param face_r:
    :param smile_r1:
    :param smile_r2:
    :param eye_r:
    :return:
    """

    # data3d = np.zeros([1,7,7], dtype=np.int16)
    if shape is None:
        shape = [32, 32]

    center = (np.asarray(shape) - 1) / 2.0
    r = np.min(center) * face_r

    # np.min(np.asarray(shape) / 2.0)
    # shape = data3d.shape[1:]
    # data3d[center[0], center[1], center[2]] = 1
    x, y = np.meshgrid(range(shape[1]), range(shape[0]))

    head = (x - center[0]) ** 2 + (y - center[1]) ** 2 < r ** 2

    smile = (
        ((x - center[0]) ** 2 + (y - center[1]) ** 2 < (r * smile_r2) ** 2)
        & (y > (center[1] + 0.3 * r))
        & ((x - center[0]) ** 2 + (y - center[1]) ** 2 >= (r * smile_r1) ** 2)
    )
    smile
    e1c = center + r * np.array([-0.35, -0.2])
    e2c = center + r * np.array([0.35, -0.2])

    eyes = (x - e1c[0]) ** 2 + (y - e1c[1]) ** 2 <= (r * eye_r) ** 2
    eyes += (x - e2c[0]) ** 2 + (y - e1c[1]) ** 2 <= (r * eye_r) ** 2

    face = head & ~smile & ~eyes
    return face


Instruction:
Make a summary of the following Python 3 code
def generate_face(shape=None, face_r=1.0, smile_r1=0.5, smile_r2=0.7, eye_r=0.2):
    # TODO add axis (ax=0)
    if shape is None:
        shape = [32, 32]
    nd = len(shape)
    if nd == 2:
        sh2 = shape
    else:
        sh2 = shape[1:]
    fc2 = _get_face2(
        sh2, face_r=face_r, smile_r1=smile_r1, smile_r2=smile_r2, eye_r=eye_r
    )

    if nd == 2:
        return fc2
    else:
        fc3 = np.zeros(shape)
        for i in range(fc3.shape[0]):
            fc3[i, :, :] = fc2
        return fc3

Response: Generate 2D or 3D binar data with smile face.


Instruction:
Implement a Python 3 function for
functioning attempts to remove file from the system

Response:
def remove(local_file_name):
    """Function attempts to remove file, if failure occures -> print exception

    :param local_file_name: name of file to remove
    """
    try:
        os.remove(local_file_name)
    except Exception as e:
        print(
            "Cannot remove file '" + local_file_name + "'. Please remove it manually."
        )
        print(e)


Instruction:
Can you create a Python 3 function that
downloads unzip and delete.

Response:
def downzip(url, destination="./sample_data/"):
    """Download, unzip and delete. Warning: function with strong side effects!

    Returns downloaded data.

    :param str url: url from which data should be donloaded
    :param destination: destination to which data should be downloaded
    """

    # url = "http://147.228.240.61/queetech/sample-data/jatra_06mm_jenjatra.zip"
    logmsg = "downloading from '" + url + "' to '" + destination + "'"
    print(logmsg)
    logger.info(logmsg)
    tmp_filename = url[url.rfind("/") + 1 :]
    # tmp_filename = "tmp.zip"
    # urllibr.urlretrieve(url, zip_file_name)
    from . import network

    network.download_file(url, destination, filename=tmp_filename)
    zip_file_name = os.path.join(destination, tmp_filename)
    base, ext = op.splitext(tmp_filename)
    # print(ext)
    # print(tmp_filename)
    if ext in (".zip"):
        unzip_recursive(zip_file_name)


Instruction:
Can you create a Python 3 function that
unzips one file and deletes it.

Response:
def unzip_one(local_file_name):
    """Unzips one file and deletes it. Warning: function with side effects!

    :param str local_file_name: file name of zip file
    :return: list of archive members by name.
    """
    local_file_name = op.expanduser(local_file_name)
    destination = op.dirname(local_file_name)
    datafile = zipfile.ZipFile(local_file_name)
    namelist = datafile.namelist()
    datafile.extractall(destination)
    datafile.close()
    remove(local_file_name)

    fullnamelist = []
    for fn in namelist:
        fullnamelist.append(op.join(destination, fn))
    return fullnamelist


Instruction:
How would you code a function in Python 3 to
unzip file with all recursive zip files inside and delete zip files after that.

Response:
def unzip_recursive(zip_file_name):
    """Unzip file with all recursive zip files inside and delete zip files after that.

    :param zip_file_name: file name of zip file
    :return: list of archive members by name.
    """
    logger.debug("unzipping " + zip_file_name)
    fnlist = unzip_one(zip_file_name)
    for fn in fnlist:
        if zipfile.is_zipfile(fn):
            local_fnlist = unzip_recursive(fn)
            fnlist.extend(local_fnlist)
    return fnlist


Instruction:
How would you explain what the following Python 3 function does
def upload_asciinema(filename):
    '''a wrapper around generation of an asciinema.api.Api to call the
       upload command given an already existing asciinema file.

       Parameters
       ==========
       filename: the asciinema file to upload, can be generated with
                 function record_asciinema in record.py

    '''
    if os.path.exists(filename):

        import asciinema.config as aconfig
        from asciinema.api import Api

        # Load the API class

        cfg = aconfig.load()
        api = Api(cfg.api_url, os.environ.get("USER"), cfg.install_id)

        # Perform the upload, return the url

        uploader = UploadCommand(api, filename)

        try:
            url, warn = uploader.api.upload_asciicast(filename)
            if warn:
                uploader.print_warning(warn)

            # Extract just the url, if provided (always is https)
            if url:
                match = re.search('https://.+', url)
                if match:
                    url = match.group()
            return url

        except:
            bot.error('Problem with upload, skipping')

    else:
        bot.warning('Cannot find %s, skipping submission.' %filename)

Response: a wrapper around generation of an API to call the UploadCommand to upload an already existing asciinema file and return the url of the uploaded file.


Instruction:
Write a Python 3 script for
creating TLS configuration object.

Response:
def make_tls_config(app_config):
    """Creates TLS configuration object."""

    if not app_config['DOCKER_TLS']:
        return False

    cert_path = app_config['DOCKER_TLS_CERT_PATH']
    if cert_path:
        client_cert = '{0}:{1}'.format(
            os.path.join(cert_path, 'cert.pem'),
            os.path.join(cert_path, 'key.pem'))
        ca_cert = os.path.join(cert_path, 'ca.pem')
    else:
        client_cert = app_config['DOCKER_TLS_CLIENT_CERT']
        ca_cert = app_config['DOCKER_TLS_CA_CERT']

    client_cert = parse_client_cert_pair(client_cert)
    return TLSConfig(
        client_cert=client_cert,
        ca_cert=ca_cert,
        verify=app_config['DOCKER_TLS_VERIFY'],
        ssl_version=app_config['DOCKER_TLS_SSL_VERSION'],
        assert_hostname=app_config['DOCKER_TLS_ASSERT_HOSTNAME'])


Instruction:
Can you generate a brief explanation for the following Python 3 code
def parse_client_cert_pair(config_value):
    if not config_value:
        return
    client_cert = config_value.split(':')
    if len(client_cert) != 2:
        tips = ('client_cert should be formatted like '
                '"/path/to/cert.pem:/path/to/key.pem"')
        raise ValueError('{0!r} is invalid.\n{1}'.format(config_value, tips))
    return tuple(client_cert)

Response: Parses the client cert pair from config item.


Instruction:
Implement a function in Python 3 to
initialize an application for using the Docker client.

Response:
def init_app(self, app):
        """Initializes an application for using :class:`docker.Client`.

        :param app: an instance of :class:`~flask.Flask`.
        """
        app.extensions = getattr(app, 'extensions', {})
        app.extensions['docker.client'] = None

        app.config.setdefault('DOCKER_URL', None)
        app.config.setdefault('DOCKER_VERSION', '1.16')
        app.config.setdefault('DOCKER_TIMEOUT', 30)
        app.config.setdefault('DOCKER_TLS', False)
        app.config.setdefault('DOCKER_TLS_VERIFY', True)
        app.config.setdefault('DOCKER_TLS_SSL_VERSION', None)
        app.config.setdefault('DOCKER_TLS_ASSERT_HOSTNAME', None)

        app.config.setdefault('DOCKER_TLS_CERT_PATH', None)
        app.config.setdefault('DOCKER_TLS_CLIENT_CERT', None)
        app.config.setdefault('DOCKER_TLS_CA_CERT', None)


Instruction:
Create a Python 3 function for
returning the original docker. Client object.

Response:
def client(self):
        """The original :class:`docker.Client` object. All docker operation
        calling will be forwarded here. ::

            docker.create_container('ubuntu')
            docker.client.create_container('ubuntu')  # equivalent
        """
        if not self.app.config['DOCKER_URL']:
            raise RuntimeError('"DOCKER_URL" must be specified')

        if not self.app.extensions['docker.client']:
            self.app.extensions['docker.client'] = Client(
                base_url=self.app.config['DOCKER_URL'],
                version=self.app.config['DOCKER_VERSION'],
                timeout=self.app.config['DOCKER_TIMEOUT'],
                tls=make_tls_config(self.app.config))
        return self.app.extensions['docker.client']


Instruction:
Can you implement a function in Python 3 that
processes a feed and schedule the entries.

Response:
async def process_feed(self, url, send_mentions=True):
        """ process a feed """

        self._feed_domains.add(utils.get_domain(url))

        if url in self._processed_feeds:
            LOGGER.debug("Skipping already processed feed %s", url)
            return
        self._processed_feeds.add(url)

        LOGGER.debug("++WAIT: %s: get feed", url)
        feed, previous, updated = await feeds.get_feed(self, url)
        LOGGER.debug("++DONE: %s: get feed", url)

        if updated:
            LOGGER.info("Feed %s has been updated", url)

        if not feed:
            return

        LOGGER.debug("--- starting process_feed %s %s", url, send_mentions)

        pending = []

        try:
            for link in feed.links:
                href = link['href']
                if not href:
                    continue

                #  RFC5005 archive links
                if self.args.archive and link.get('rel') in ('prev-archive',
                                                             'next-archive',
                                                             'prev-page',
                                                             'next-page'):
                    LOGGER.debug("Found archive link %s", link)
                    pending.append(
                        ("process feed " + href, self.process_feed(href, send_mentions)))

                # WebSub notification
                if updated and link.get('rel') == 'hub' and not feed.is_archive:
                    LOGGER.debug("Found WebSub hub %s", link)
                    pending.append(
                        ("update websub " + href, feed.update_websub(self, href)))
        except (AttributeError, KeyError):
            LOGGER.debug("Feed %s has no links", url)

        # Schedule the entries
        items = set(feed.entry_links)
        if previous:
            items |= set(previous.entry_links)
        for entry in items:
            pending.append(("process entry " + entry,
                            self.process_entry(entry, send_mentions=send_mentions)))

        LOGGER.debug("--- finish process_feed %s %s", url, send_mentions)

        if pending:
            LOGGER.debug("+++WAIT: process_feed(%s): %d subtasks",
                         url, len(pending))
            LOGGER.debug("%s", [name for (name, _) in pending])
            await asyncio.wait([task for (_, task) in pending])
            LOGGER.debug("+++DONE: process_feed(%s): %d subtasks",
                         url, len(pending))


Instruction:
Write a Python 3 script to
send a webmention from an entry to a URL.

Response:
async def send_webmention(self, entry, url):
        """ send a webmention from an entry to a URL """

        if (entry.url, url) in self._processed_mentions:
            LOGGER.debug(
                "Skipping already processed mention %s -> %s", entry.url, url)
        self._processed_mentions.add((entry.url, url))

        LOGGER.debug("++WAIT: webmentions.get_target %s", url)
        target = await webmentions.get_target(self, url)
        LOGGER.debug("++DONE: webmentions.get_target %s", url)

        if target:
            LOGGER.debug("++WAIT: Sending webmention %s -> %s", entry.url, url)
            await target.send(self, entry)
            LOGGER.debug("++DONE: Sending webmention %s -> %s", entry.url, url)


Instruction:
Implement a function in Python 3 to
update the locale message files for the given application.

Response:
def makemessages(application, locale):
    """
    Updates the locale message files
    """
    from django.core.management import call_command

    if not locale:
        locale = 'en'
    with work_in(application):
        call_command('makemessages', locale=(locale,))


Instruction:
Given the following Python 3 function, write the documentation
def cms_check(migrate_cmd=False):
    from django.core.management import call_command
    try:
        import cms  # NOQA # nopyflakes
        _create_db(migrate_cmd)
        call_command('cms', 'check')
    except ImportError:
        print('cms_check available only if django CMS is installed')

Response: Runs the django CMS check command


Instruction:
Write a Python 3 script for
generating migrations for the specified application.

Response:
def makemigrations(application, merge=False, dry_run=False, empty=False, extra_applications=None):
    """
    Generate migrations
    """
    from django.core.management import call_command

    apps = [application]
    if extra_applications:
        if isinstance(extra_applications, text_type):
            apps += [extra_applications]
        elif isinstance(extra_applications, list):
            apps += extra_applications

    for app in apps:
        call_command('makemigrations', *(app,), merge=merge, dry_run=dry_run, empty=empty)


Instruction:
Create a Python 3 function for
generating the list of authors for the current user

Response:
def generate_authors():
    """
    Updates the authors list
    """
    print('Generating AUTHORS')

    # Get our list of authors
    print('Collecting author names')
    r = subprocess.Popen(['git', 'log', '--use-mailmap', '--format=%aN'],
                         stdout=subprocess.PIPE)
    seen_authors = []
    authors = []
    for authfile in ('AUTHORS', 'AUTHORS.rst'):
        if os.path.exists(authfile):
            break
    with open(authfile, 'r') as f:
        for line in f.readlines():
            if line.startswith("*"):
                author = force_text(line).strip("* \n")
                if author.lower() not in seen_authors:
                    seen_authors.append(author.lower())
                    authors.append(author)
    for author in r.stdout.readlines():
        author = force_text(author).strip()
        if author.lower() not in seen_authors:
            seen_authors.append(author.lower())
            authors.append(author)

    # Sort our list of Authors by their case insensitive name
    authors = sorted(authors, key=lambda x: x.lower())

    # Write our authors to the AUTHORS file
    print('Authors (%s):\n\n\n* %s' % (len(authors), '\n* '.join(authors)))


Instruction:
Write a Python 3 function for
initialing parse setup. Call at start of `self.parse()`.

Response:
def setup_parse(self, inputstring, document):
        """Initial parse setup.  Call at start of `self.parse()`."""
        self.inputstring = inputstring
        self.document = document


Instruction:
How would you explain what the following Python 3 function does
def check_version(expected_version):

    with open(os.path.join(root_folder, 'VERSION'), 'r') as version_file:
        version = version_file.read().strip()

    if version != expected_version:
        raise EnvironmentError("Version mismatch during release, expected={}, found={}"
                               .format(expected_version, version))

Response: Check that the version of the package in setuptools matches the expected version.