Instruction:
Write a Python 3 function for
joining a Hangul letter from Korean phonemes.

Response:
def join_phonemes(*args):
    """Joins a Hangul letter from Korean phonemes."""
    # Normalize arguments as onset, nucleus, coda.
    if len(args) == 1:
        # tuple of (onset, nucleus[, coda])
        args = args[0]
    if len(args) == 2:
        args += (CODAS[0],)
    try:
        onset, nucleus, coda = args
    except ValueError:
        raise TypeError('join_phonemes() takes at most 3 arguments')
    offset = (
        (ONSETS.index(onset) * NUM_NUCLEUSES + NUCLEUSES.index(nucleus)) *
        NUM_CODAS + CODAS.index(coda)
    )
    return unichr(FIRST_HANGUL_OFFSET + offset)


Instruction:
Create a Python 3 function to
split Korean phonemes from a single letter.

Response:
def split_phonemes(letter, onset=True, nucleus=True, coda=True):
    """Splits Korean phonemes as known as "자소" from a Hangul letter.

    :returns: (onset, nucleus, coda)
    :raises ValueError: `letter` is not a Hangul single letter.

    """
    if len(letter) != 1 or not is_hangul(letter):
        raise ValueError('Not Hangul letter: %r' % letter)
    offset = ord(letter) - FIRST_HANGUL_OFFSET
    phonemes = [None] * 3
    if onset:
        phonemes[0] = ONSETS[offset // (NUM_NUCLEUSES * NUM_CODAS)]
    if nucleus:
        phonemes[1] = NUCLEUSES[(offset // NUM_CODAS) % NUM_NUCLEUSES]
    if coda:
        phonemes[2] = CODAS[offset % NUM_CODAS]
    return tuple(phonemes)


Instruction:
How would you explain what the following Python 3 function does
def combine_words(word1, word2):
    if word1 and word2 and is_consonant(word2[0]):
        onset, nucleus, coda = split_phonemes(word1[-1])
        if not coda:
            glue = join_phonemes(onset, nucleus, word2[0])
            return word1[:-1] + glue + word2[1:]
    return word1 + word2

Response: Combines two words into one single word.


Instruction:
How would you implement a function in Python 3 that
returns a regex pattern which can be used to index the given particles.

Response:
def index_particles(particles):
    """Indexes :class:`Particle` objects.  It returns a regex pattern which
    matches to any particle morphs and a dictionary indexes the given particles
    by regex groups.
    """
    patterns, indices = [], {}
    for x, p in enumerate(particles):
        group = u'_%d' % x
        indices[group] = x
        patterns.append(u'(?P<%s>%s)' % (group, p.regex_pattern()))
    pattern = re.compile(u'|'.join(patterns))
    return pattern, indices


Instruction:
Create a Python 3 function to
wait for an environment to be healthy

Response:
def execute(helper, config, args):
    """
    Waits for an environment to be healthy
    """
    helper.wait_for_environments(args.environment, health=args.health)


Instruction:
Can you tell what is the following Python 3 function doing
def execute(helper, config, args):
    versions = helper.get_versions()
    out("Deployed versions:")
    for version in versions:
        out(version)

Response: List all versions of the current node


Instruction:
How would you code a function in Python 3 to
add command line arguments for the init command

Response:
def add_arguments(parser):
    """
    Args for the init command
    """
    parser.add_argument('-e', '--environment',  help='Environment name', required=False, nargs='+')
    parser.add_argument('-w', '--dont-wait', help='Skip waiting for the app to be deleted', action='store_true')


Instruction:
Make a summary of the following Python 3 code
def execute(helper, config, args):
    environment_name = args.environment

    (events, next_token) = helper.describe_events(environment_name, start_time=datetime.now().isoformat())

    # swap C-Names
    for event in events:
        print(("["+event['Severity']+"] "+event['Message']))

Response: describe recent events for an environment


Instruction:
Can you write a function in Python 3 where it
rebuilds an environment and waits for all environments to be ready.

Response:
def execute(helper, config, args):
    """
    Rebuilds an environment
    """
    env_config = parse_env_config(config, args.environment)
    helper.rebuild_environment(args.environment)

    # wait
    if not args.dont_wait:
        helper.wait_for_environments(args.environment, health='Green', status='Ready')


Instruction:
How would you implement a function in Python 3 that
generates all reasonable tolerant particle morphs.

Response:
def generate_tolerances(morph1, morph2):
    """Generates all reasonable tolerant particle morphs::

    >>> set(generate_tolerances(u'이', u'가'))
    set([u'이(가)', u'(이)가', u'가(이)', u'(가)이'])
    >>> set(generate_tolerances(u'이면', u'면'))
    set([u'(이)면'])

    """
    if morph1 == morph2:
        # Tolerance not required.
        return
    if not (morph1 and morph2):
        # Null allomorph exists.
        yield u'(%s)' % (morph1 or morph2)
        return
    len1, len2 = len(morph1), len(morph2)
    if len1 != len2:
        longer, shorter = (morph1, morph2) if len1 > len2 else (morph2, morph1)
        if longer.endswith(shorter):
            # Longer morph ends with shorter morph.
            yield u'(%s)%s' % (longer[:-len(shorter)], shorter)
            return
    # Find common suffix between two morphs.
    for x, (let1, let2) in enumerate(zip(reversed(morph1), reversed(morph2))):
        if let1 != let2:
            break
    if x:
        # They share the common suffix.
        x1, x2 = len(morph1) - x, len(morph2) - x
        common_suffix = morph1[x1:]
        morph1, morph2 = morph1[:x1], morph2[:x2]
    else:
        # No similarity with each other.
        common_suffix = ''
    for morph1, morph2 in [(morph1, morph2), (morph2, morph1)]:
        yield u'%s(%s)%s' % (morph1, morph2, common_suffix)
        yield u'(%s)%s%s' % (morph1, morph2, common_suffix)


Instruction:
Implement a function in Python 3 to
resolve a tolerance style of the given particle morph.

Response:
def parse_tolerance_style(style, registry=None):
    """Resolves a tolerance style of the given tolerant particle morph::

    >>> parse_tolerance_style(u'은(는)')
    0
    >>> parse_tolerance_style(u'(은)는')
    1
    >>> parse_tolerance_style(OPTIONAL_MORPH2_AND_MORPH1)
    3

    """
    if isinstance(style, integer_types):
        return style
    if registry is None:
        from . import registry
    particle = registry.parse(style)
    if len(particle.tolerances) != 4:
        raise ValueError('Set tolerance style by general allomorphic particle')
    return particle.tolerances.index(style)


Instruction:
Write a Python 3 script to
list available solution stacks

Response:
def execute(helper, config, args):
    """
    Lists solution stacks
    """
    out("Available solution stacks")
    for stack in helper.list_available_solution_stacks():
        out("    "+str(stack))
    return 0


Instruction:
Write a Python 3 script to
add the arguments for the deploy command to the argument parser

Response:
def add_arguments(parser):
    """
    adds arguments for the deploy command
    """
    parser.add_argument('-e', '--environment', help='Environment name', required=True)
    parser.add_argument('-w', '--dont-wait', help='Skip waiting', action='store_true')
    parser.add_argument('-a', '--archive', help='Archive file', required=False)
    parser.add_argument('-d', '--directory', help='Directory', required=False)
    parser.add_argument('-l', '--version-label', help='Version label', required=False)
    parser.add_argument('-t', '--termination-delay',
                        help='Delay termination of old environment by this number of seconds',
                        type=int, required=False)


Instruction:
How would you explain what the following Python 3 function does
def execute(helper, config, args):
    version_label = args.version_label
    archive = args.archive

    # get the environment configuration
    env_config = parse_env_config(config, args.environment)
    option_settings = parse_option_settings(env_config.get('option_settings', {}))
    cname_prefix = env_config.get('cname_prefix', None)

    # no zdt for anything but web server
    tier_name = env_config.get('tier_name', 'WebServer')
    if tier_name != 'WebServer':
        raise Exception(
            "Only able to do zero downtime deployments for "
            "WebServer tiers, can't do them for %s" % (tier_name, ))

    # find an available environment name
    out("Determining new environment name...")
    new_env_name = None
    if not helper.environment_exists(args.environment):
        new_env_name = args.environment
    else:
        for i in range(10):
            temp_env_name = args.environment + '-' + str(i)
            if not helper.environment_exists(temp_env_name):
                new_env_name = temp_env_name
                break
    if new_env_name is None:
        raise Exception("Unable to determine new environment name")
    out("New environment name will be " + new_env_name)

    # find an available cname name
    out("Determining new environment cname...")
    new_env_cname = None
    for i in range(10):
        temp_cname = cname_prefix + '-' + str(i)
        if not helper.environment_name_for_cname(temp_cname):
            new_env_cname = temp_cname
            break
    if new_env_cname is None:
        raise Exception("Unable to determine new environment cname")
    out("New environment cname will be " + new_env_cname)

    # upload or build an archive
    version_label = upload_application_archive(
        helper, env_config, archive=args.archive, directory=args.directory, version_label=version_label)

    # create the new environment
    helper.create_environment(new_env_name,
                              solution_stack_name=env_config.get('solution_stack_name'),
                              cname_prefix=new_env_cname,
                              description=env_config.get('description', None),
                              option_settings=option_settings,
                              version_label=version_label,
                              tier_name=tier_name,
                              tier_type=env_config.get('tier_type'),
                              tier_version=env_config.get('tier_version'))
    helper.wait_for_environments(new_env_name, status='Ready', health='Green', include_deleted=False)

    # find existing environment name
    old_env_name = helper.environment_name_for_cname(cname_prefix)
    if old_env_name is None:
        raise Exception("Unable to find current environment with cname: " + cname_prefix)
    out("Current environment name is " + old_env_name)

    # swap C-Names
    out("Swapping environment cnames")
    helper.swap_environment_cnames(old_env_name, new_env_name)
    helper.wait_for_environments([old_env_name, new_env_name], status='Ready', include_deleted=False)

    # delete the old environment
    if args.termination_delay:
        out("Termination delay specified, sleeping for {} seconds...".format(args.termination_delay))
        time.sleep(args.termination_delay)
    out("Deleting old environment {}".format(old_env_name))
    helper.delete_environment(old_env_name)

    # delete unused
    helper.delete_unused_versions(versions_to_keep=int(get(config, 'app.versions_to_keep', 10)))

Response: Execute the zdt deployment


Instruction:
Write a Python 3 function for
returning a word which removes insignificant letters at the end of the given .

Response:
def filter_only_significant(word):
    """Gets a word which removes insignificant letters at the end of the given
    word::

    >>> pick_significant(u'넥슨(코리아)')
    넥슨
    >>> pick_significant(u'메이플스토리...')
    메이플스토리

    """
    if not word:
        return word
    # Unwrap a complete parenthesis.
    if word.startswith(u'(') and word.endswith(u')'):
        return filter_only_significant(word[1:-1])
    x = len(word)
    while x > 0:
        x -= 1
        c = word[x]
        # Skip a complete parenthesis.
        if c == u')':
            m = INSIGNIFICANT_PARENTHESIS_PATTERN.search(word[:x + 1])
            if m is not None:
                x = m.start()
            continue
        # Skip unreadable characters such as punctuations.
        unicode_category = unicodedata.category(c)
        if not SIGNIFICANT_UNICODE_CATEGORY_PATTERN.match(unicode_category):
            continue
        break
    return word[:x + 1]


Instruction:
Create a Python 3 function to
pick only a coda from a Hangul letter. It returns None if the letter is not Hangul.

Response:
def pick_coda_from_letter(letter):
    """Picks only a coda from a Hangul letter.  It returns ``None`` if the
    given letter is not Hangul.
    """
    try:
        __, __, coda = \
            split_phonemes(letter, onset=False, nucleus=False, coda=True)
    except ValueError:
        return None
    else:
        return coda


Instruction:
Can you create a Python 3 function that
picks only a coda from a decimal.

Response:
def pick_coda_from_decimal(decimal):
    """Picks only a coda from a decimal."""
    decimal = Decimal(decimal)
    __, digits, exp = decimal.as_tuple()
    if exp < 0:
        return DIGIT_CODAS[digits[-1]]
    __, digits, exp = decimal.normalize().as_tuple()
    index = bisect_right(EXP_INDICES, exp) - 1
    if index < 0:
        return DIGIT_CODAS[digits[-1]]
    else:
        return EXP_CODAS[EXP_INDICES[index]]


Instruction:
Can you implement a function in Python 3 that
fetches a deposit identifier.

Response:
def deposit_fetcher(record_uuid, data):
    """Fetch a deposit identifier.

    :param record_uuid: Record UUID.
    :param data: Record content.
    :returns: A :class:`invenio_pidstore.fetchers.FetchedPID` that contains
        data['_deposit']['id'] as pid_value.
    """
    return FetchedPID(
        provider=DepositProvider,
        pid_type=DepositProvider.pid_type,
        pid_value=str(data['_deposit']['id']),
    )


Instruction:
Write a Python 3 function for
minting a deposit identifier.

Response:
def deposit_minter(record_uuid, data):
    """Mint a deposit identifier.

    A PID with the following characteristics is created:

    .. code-block:: python

        {
            "object_type": "rec",
            "object_uuid": record_uuid,
            "pid_value": "<new-pid-value>",
            "pid_type": "depid",
        }

    The following deposit meta information are updated:

    .. code-block:: python

        deposit['_deposit'] = {
            "id": "<new-pid-value>",
            "status": "draft",
        }

    :param record_uuid: Record UUID.
    :param data: Record content.
    :returns: A :class:`invenio_pidstore.models.PersistentIdentifier` object.
    """
    provider = DepositProvider.create(
        object_type='rec',
        object_uuid=record_uuid,
        pid_value=uuid.uuid4().hex,
    )
    data['_deposit'] = {
        'id': provider.pid.pid_value,
        'status': 'draft',
    }
    return provider.pid


Instruction:
Can you tell what is the following Python 3 function doing
def admin_permission_factory():
    try:
        pkg_resources.get_distribution('invenio-access')
        from invenio_access.permissions import DynamicPermission as Permission
    except pkg_resources.DistributionNotFound:
        from flask_principal import Permission

    return Permission(action_admin_access)

Response: Factory for creating a permission for an admin deposit - admin - access.


Instruction:
Write a Python 3 function for
creating Invenio - Deposit - UI blueprint.

Response:
def create_blueprint(endpoints):
    """Create Invenio-Deposit-UI blueprint.

    See: :data:`invenio_deposit.config.DEPOSIT_RECORDS_UI_ENDPOINTS`.

    :param endpoints: List of endpoints configuration.
    :returns: The configured blueprint.
    """
    from invenio_records_ui.views import create_url_rule

    blueprint = Blueprint(
        'invenio_deposit_ui',
        __name__,
        static_folder='../static',
        template_folder='../templates',
        url_prefix='',
    )

    @blueprint.errorhandler(PIDDeletedError)
    def tombstone_errorhandler(error):
        """Render tombstone page."""
        return render_template(
            current_app.config['DEPOSIT_UI_TOMBSTONE_TEMPLATE'],
            pid=error.pid,
            record=error.record or {},
        ), 410

    for endpoint, options in (endpoints or {}).items():
        options = deepcopy(options)
        options.pop('jsonschema', None)
        options.pop('schemaform', None)
        blueprint.add_url_rule(**create_url_rule(endpoint, **options))

    @blueprint.route('/deposit')
    @login_required
    def index():
        """List user deposits."""
        return render_template(current_app.config['DEPOSIT_UI_INDEX_TEMPLATE'])

    @blueprint.route('/deposit/new')
    @login_required
    def new():
        """Create new deposit."""
        deposit_type = request.values.get('type')
        return render_template(
            current_app.config['DEPOSIT_UI_NEW_TEMPLATE'],
            record={'_deposit': {'id': None}},
            jsonschema=current_deposit.jsonschemas[deposit_type],
            schemaform=current_deposit.schemaforms[deposit_type],
        )

    return blueprint


Instruction:
Write a Python 3 function that can
default view method. Sends ``record_viewed`` signal and renders template.

Response:
def default_view_method(pid, record, template=None):
    """Default view method.

    Sends ``record_viewed`` signal and renders template.
    """
    record_viewed.send(
        current_app._get_current_object(),
        pid=pid,
        record=record,
    )

    deposit_type = request.values.get('type')

    return render_template(
        template,
        pid=pid,
        record=record,
        jsonschema=current_deposit.jsonschemas[deposit_type],
        schemaform=current_deposit.schemaforms[deposit_type],
    )


Instruction:
Can you generate a brief explanation for the following Python 3 code
def create(cls, object_type=None, object_uuid=None, **kwargs):
        assert 'pid_value' in kwargs
        kwargs.setdefault('status', cls.default_status)
        return super(DepositProvider, cls).create(
            object_type=object_type, object_uuid=object_uuid, **kwargs)

Response: Create a new deposit identifier.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def extract_actions_from_class(record_class):
    for name in dir(record_class):
        method = getattr(record_class, name, None)
        if method and getattr(method, '__deposit_action__', False):
            yield method.__name__

Response: Extract actions from class.


Instruction:
How would you implement a function in Python 3 that
bases permission factory that checks OAuth2 scope and can_method.

Response:
def check_oauth2_scope(can_method, *myscopes):
    """Base permission factory that check OAuth2 scope and can_method.

    :param can_method: Permission check function that accept a record in input
        and return a boolean.
    :param myscopes: List of scopes required to permit the access.
    :returns: A :class:`flask_principal.Permission` factory.
    """
    def check(record, *args, **kwargs):
        @require_api_auth()
        @require_oauth_scopes(*myscopes)
        def can(self):
            return can_method(record)

        return type('CheckOAuth2Scope', (), {'can': can})()
    return check


Instruction:
Implement a function in Python 3 to
check if a given record is indexed.

Response:
def can_elasticsearch(record):
    """Check if a given record is indexed.

    :param record: A record object.
    :returns: If the record is indexed returns `True`, otherwise `False`.
    """
    search = request._methodview.search_class()
    search = search.get_record(str(record.id))
    return search.count() == 1


Instruction:
How would you explain what the following Python 3 function does
def create_error_handlers(blueprint):
    blueprint.errorhandler(PIDInvalidAction)(create_api_errorhandler(
        status=403, message='Invalid action'
    ))
    records_rest_error_handlers(blueprint)

Response: Create error handlers on blueprint.


Instruction:
Implement a function in Python 3 to
create Invenio - Deposit - REST blueprint.

Response:
def create_blueprint(endpoints):
    """Create Invenio-Deposit-REST blueprint.

    See: :data:`invenio_deposit.config.DEPOSIT_REST_ENDPOINTS`.

    :param endpoints: List of endpoints configuration.
    :returns: The configured blueprint.
    """
    blueprint = Blueprint(
        'invenio_deposit_rest',
        __name__,
        url_prefix='',
    )
    create_error_handlers(blueprint)

    for endpoint, options in (endpoints or {}).items():
        options = deepcopy(options)

        if 'files_serializers' in options:
            files_serializers = options.get('files_serializers')
            files_serializers = {mime: obj_or_import_string(func)
                                 for mime, func in files_serializers.items()}
            del options['files_serializers']
        else:
            files_serializers = {}

        if 'record_serializers' in options:
            serializers = options.get('record_serializers')
            serializers = {mime: obj_or_import_string(func)
                           for mime, func in serializers.items()}
        else:
            serializers = {}

        file_list_route = options.pop(
            'file_list_route',
            '{0}/files'.format(options['item_route'])
        )
        file_item_route = options.pop(
            'file_item_route',
            '{0}/files/<path:key>'.format(options['item_route'])
        )

        options.setdefault('search_class', DepositSearch)
        search_class = obj_or_import_string(options['search_class'])

        # records rest endpoints will use the deposit class as record class
        options.setdefault('record_class', Deposit)
        record_class = obj_or_import_string(options['record_class'])

        # backward compatibility for indexer class
        options.setdefault('indexer_class', None)

        for rule in records_rest_url_rules(endpoint, **options):
            blueprint.add_url_rule(**rule)

        search_class_kwargs = {}
        if options.get('search_index'):
            search_class_kwargs['index'] = options['search_index']

        if options.get('search_type'):
            search_class_kwargs['doc_type'] = options['search_type']

        ctx = dict(
            read_permission_factory=obj_or_import_string(
                options.get('read_permission_factory_imp')
            ),
            create_permission_factory=obj_or_import_string(
                options.get('create_permission_factory_imp')
            ),
            update_permission_factory=obj_or_import_string(
                options.get('update_permission_factory_imp')
            ),
            delete_permission_factory=obj_or_import_string(
                options.get('delete_permission_factory_imp')
            ),
            record_class=record_class,
            search_class=partial(search_class, **search_class_kwargs),
            default_media_type=options.get('default_media_type'),
        )

        deposit_actions = DepositActionResource.as_view(
            DepositActionResource.view_name.format(endpoint),
            serializers=serializers,
            pid_type=options['pid_type'],
            ctx=ctx,
        )

        blueprint.add_url_rule(
            '{0}/actions/<any({1}):action>'.format(
                options['item_route'],
                ','.join(extract_actions_from_class(record_class)),
            ),
            view_func=deposit_actions,
            methods=['POST'],
        )

        deposit_files = DepositFilesResource.as_view(
            DepositFilesResource.view_name.format(endpoint),
            serializers=files_serializers,
            pid_type=options['pid_type'],
            ctx=ctx,
        )

        blueprint.add_url_rule(
            file_list_route,
            view_func=deposit_files,
            methods=['GET', 'POST', 'PUT'],
        )

        deposit_file = DepositFileResource.as_view(
            DepositFileResource.view_name.format(endpoint),
            serializers=files_serializers,
            pid_type=options['pid_type'],
            ctx=ctx,
        )

        blueprint.add_url_rule(
            file_item_route,
            view_func=deposit_file,
            methods=['GET', 'PUT', 'DELETE'],
        )
    return blueprint


Instruction:
Can you generate the documentation for the following Python 3 function
def post(self, pid, record, action):
        record = getattr(record, action)(pid=pid)

        db.session.commit()
        # Refresh the PID and record metadata
        db.session.refresh(pid)
        db.session.refresh(record.model)
        post_action.send(current_app._get_current_object(), action=action,
                         pid=pid, deposit=record)
        response = self.make_response(pid, record,
                                      202 if action == 'publish' else 201)
        endpoint = '.{0}_item'.format(pid.pid_type)
        location = url_for(endpoint, pid_value=pid.pid_value, _external=True)
        response.headers.extend(dict(Location=location))
        return response

Response:
Handle deposit action.

        After the action is executed, a
        :class:`invenio_deposit.signals.post_action` signal is sent.

        Permission required: `update_permission_factory`.

        :param pid: Pid object (from url).
        :param record: Record object resolved from the pid.
        :param action: The action to execute.


Instruction:
Can you tell what is the following Python 3 function doing
def get(self, pid, record):
        return self.make_response(obj=record.files, pid=pid, record=record)

Response:
Get files.

        Permission required: `read_permission_factory`.

        :param pid: Pid object (from url).
        :param record: Record object resolved from the pid.
        :returns: The files.


Instruction:
How would you code a function in Python 3 to
handle POST deposit files. Permission required: `update_permission_factory`. :param pid: Pid object (from url). :param record: Record object resolved from the pid.

Response:
def post(self, pid, record):
        """Handle POST deposit files.

        Permission required: `update_permission_factory`.

        :param pid: Pid object (from url).
        :param record: Record object resolved from the pid.
        """
        # load the file
        uploaded_file = request.files['file']
        # file name
        key = secure_filename(
            request.form.get('name') or uploaded_file.filename
        )
        # check if already exists a file with this name
        if key in record.files:
            raise FileAlreadyExists()
        # add it to the deposit
        record.files[key] = uploaded_file.stream
        record.commit()
        db.session.commit()
        return self.make_response(
            obj=record.files[key].obj, pid=pid, record=record, status=201)


Instruction:
Here you have a function in Python 3, explain what it does
def put(self, pid, record):
        try:
            ids = [data['id'] for data in json.loads(
                request.data.decode('utf-8'))]
        except KeyError:
            raise WrongFile()

        record.files.sort_by(*ids)
        record.commit()
        db.session.commit()
        return self.make_response(obj=record.files, pid=pid, record=record)

Response: Handle the sort of the files through the PUT deposit files.


Instruction:
Create a Python 3 function to
get file. Permission required: `read_permission_factory`. :param pid: Pid object (from url). :param record: Record object resolved from the pid. :param key: Unique identifier for the file in the deposit. :param version_id: File version. Optional. If no version is provided, the last version is retrieved. :returns: the file content.

Response:
def get(self, pid, record, key, version_id, **kwargs):
        """Get file.

        Permission required: `read_permission_factory`.

        :param pid: Pid object (from url).
        :param record: Record object resolved from the pid.
        :param key: Unique identifier for the file in the deposit.
        :param version_id: File version. Optional. If no version is provided,
            the last version is retrieved.
        :returns: the file content.
        """
        try:
            obj = record.files[str(key)].get_version(version_id=version_id)
            return self.make_response(
                obj=obj or abort(404), pid=pid, record=record)
        except KeyError:
            abort(404)


Instruction:
Make a summary of the following Python 3 code
def put(self, pid, record, key):
        try:
            data = json.loads(request.data.decode('utf-8'))
            new_key = data['filename']
        except KeyError:
            raise WrongFile()
        new_key_secure = secure_filename(new_key)
        if not new_key_secure or new_key != new_key_secure:
            raise WrongFile()
        try:
            obj = record.files.rename(str(key), new_key_secure)
        except KeyError:
            abort(404)
        record.commit()
        db.session.commit()
        return self.make_response(obj=obj, pid=pid, record=record)

Response: Handle the file rename through the PUT deposit file.


Instruction:
Implement a Python 3 function for
handling DELETE deposit file.

Response:
def delete(self, pid, record, key):
        """Handle DELETE deposit file.

        Permission required: `update_permission_factory`.

        :param pid: Pid object (from url).
        :param record: Record object resolved from the pid.
        :param key: Unique identifier for the file in the deposit.
        """
        try:
            del record.files[str(key)]
            record.commit()
            db.session.commit()
            return make_response('', 204)
        except KeyError:
            abort(404, 'The specified object does not exist or has already '
                  'been deleted.')


Instruction:
Write a Python 3 script to
load records from file.

Response:
def records():
    """Load records."""
    import pkg_resources
    from dojson.contrib.marc21 import marc21
    from dojson.contrib.marc21.utils import create_record, split_blob
    from flask_login import login_user, logout_user
    from invenio_accounts.models import User
    from invenio_deposit.api import Deposit

    users = User.query.all()

    # pkg resources the demodata
    data_path = pkg_resources.resource_filename(
        'invenio_records', 'data/marc21/bibliographic.xml'
    )
    with open(data_path) as source:
        with current_app.test_request_context():
            indexer = RecordIndexer()
            with db.session.begin_nested():
                for index, data in enumerate(split_blob(source.read()),
                                             start=1):
                    login_user(users[index % len(users)])
                    # do translate
                    record = marc21.do(create_record(data))
                    # create record
                    indexer.index(Deposit.create(record))
                    logout_user()
            db.session.commit()


Instruction:
Implement a Python 3 function for
loading deposit JSON schemas.

Response:
def jsonschemas(self):
        """Load deposit JSON schemas."""
        _jsonschemas = {
            k: v['jsonschema']
            for k, v in self.app.config['DEPOSIT_RECORDS_UI_ENDPOINTS'].items()
            if 'jsonschema' in v
        }
        return defaultdict(
            lambda: self.app.config['DEPOSIT_DEFAULT_JSONSCHEMA'], _jsonschemas
        )


Instruction:
How would you code a function in Python 3 to
load deposit schema forms.

Response:
def schemaforms(self):
        """Load deposit schema forms."""
        _schemaforms = {
            k: v['schemaform']
            for k, v in self.app.config['DEPOSIT_RECORDS_UI_ENDPOINTS'].items()
            if 'schemaform' in v
        }
        return defaultdict(
            lambda: self.app.config['DEPOSIT_DEFAULT_SCHEMAFORM'], _schemaforms
        )


Instruction:
Make a summary of the following Python 3 code
def init_app(self, app):
        self.init_config(app)
        app.register_blueprint(ui.create_blueprint(
            app.config['DEPOSIT_RECORDS_UI_ENDPOINTS']
        ))
        app.extensions['invenio-deposit'] = _DepositState(app)
        if app.config['DEPOSIT_REGISTER_SIGNALS']:
            post_action.connect(index_deposit_after_publish, sender=app,
                                weak=False)

Response:
Flask application initialization.

        Initialize the UI endpoints.  Connect all signals if
        `DEPOSIT_REGISTER_SIGNALS` is ``True``.

        :param app: An instance of :class:`flask.Flask`.


Instruction:
How would you implement a function in Python 3 that
initializes the Flask application.

Response:
def init_app(self, app):
        """Flask application initialization.

        Initialize the REST endpoints.  Connect all signals if
        `DEPOSIT_REGISTER_SIGNALS` is True.

        :param app: An instance of :class:`flask.Flask`.
        """
        self.init_config(app)
        blueprint = rest.create_blueprint(
            app.config['DEPOSIT_REST_ENDPOINTS']
        )

        # FIXME: This is a temporary fix. This means that
        # invenio-records-rest's endpoint_prefixes cannot be used before
        # the first request or in other processes, ex: Celery tasks.
        @app.before_first_request
        def extend_default_endpoint_prefixes():
            """Extend redirects between PID types."""
            endpoint_prefixes = utils.build_default_endpoint_prefixes(
                dict(app.config['DEPOSIT_REST_ENDPOINTS'])
            )
            current_records_rest = app.extensions['invenio-records-rest']
            overlap = set(endpoint_prefixes.keys()) & set(
                current_records_rest.default_endpoint_prefixes
            )
            if overlap:
                raise RuntimeError(
                    'Deposit wants to override endpoint prefixes {0}.'.format(
                        ', '.join(overlap)
                    )
                )
            current_records_rest.default_endpoint_prefixes.update(
                endpoint_prefixes
            )

        app.register_blueprint(blueprint)
        app.extensions['invenio-deposit-rest'] = _DepositState(app)
        if app.config['DEPOSIT_REGISTER_SIGNALS']:
            post_action.connect(index_deposit_after_publish, sender=app,
                                weak=False)


Instruction:
Given the following Python 3 function, write the documentation
def deposit_links_factory(pid):
    links = default_links_factory(pid)

    def _url(name, **kwargs):
        """URL builder."""
        endpoint = '.{0}_{1}'.format(
            current_records_rest.default_endpoint_prefixes[pid.pid_type],
            name,
        )
        return url_for(endpoint, pid_value=pid.pid_value, _external=True,
                       **kwargs)

    links['files'] = _url('files')

    ui_endpoint = current_app.config.get('DEPOSIT_UI_ENDPOINT')
    if ui_endpoint is not None:
        links['html'] = ui_endpoint.format(
            host=request.host,
            scheme=request.scheme,
            pid_value=pid.pid_value,
        )

    deposit_cls = Deposit
    if 'pid_value' in request.view_args:
        deposit_cls = request.view_args['pid_value'].data[1].__class__

    for action in extract_actions_from_class(deposit_cls):
        links[action] = _url('actions', action=action)
    return links

Response: Factory for record links generation.


Instruction:
Can you create a Python 3 function that
loads minter from PIDStore registry based on given value.

Response:
def process_minter(value):
    """Load minter from PIDStore registry based on given value.

    :param value: Name of the minter.
    :returns: The minter.
    """
    try:
        return current_pidstore.minters[value]
    except KeyError:
        raise click.BadParameter(
            'Unknown minter {0}. Please use one of {1}.'.format(
                value, ', '.join(current_pidstore.minters.keys())
            )
        )


Instruction:
Write a Python 3 function for
loading schema from JSONSchema registry based on given value.

Response:
def process_schema(value):
    """Load schema from JSONSchema registry based on given value.

    :param value: Schema path, relative to the directory when it was
        registered.
    :returns: The schema absolute path.
    """
    schemas = current_app.extensions['invenio-jsonschemas'].schemas
    try:
        return schemas[value]
    except KeyError:
        raise click.BadParameter(
            'Unknown schema {0}. Please use one of:\n {1}'.format(
                value, '\n'.join(schemas.keys())
            )
        )


Instruction:
Given the following Python 3 function, write the documentation
def json_serializer(pid, data, *args):
    if data is not None:
        response = Response(
            json.dumps(data.dumps()),
            mimetype='application/json'
        )
    else:
        response = Response(mimetype='application/json')
    return response

Response: Builds a JSON Flask response using the given data.


Instruction:
Write a Python 3 script for
serializing a object version to a dictionary.

Response:
def file_serializer(obj):
    """Serialize a object.

    :param obj: A :class:`invenio_files_rest.models.ObjectVersion` instance.
    :returns: A dictionary with the fields to serialize.
    """
    return {
        "id": str(obj.file_id),
        "filename": obj.key,
        "filesize": obj.file.size,
        "checksum": obj.file.checksum,
    }


Instruction:
Can you tell what is the following Python 3 function doing
def json_file_response(obj=None, pid=None, record=None, status=None):
    from invenio_records_files.api import FilesIterator

    if isinstance(obj, FilesIterator):
        return json_files_serializer(obj, status=status)
    else:
        return json_file_serializer(obj, status=status)

Response: Returns a Flask response with JSON data.


Instruction:
Can you generate the documentation for the following Python 3 function
def index_deposit_after_publish(sender, action=None, pid=None, deposit=None):
    if action == 'publish':
        _, record = deposit.fetch_published()
        index_record.delay(str(record.id))

Response: Index the record after publishing.


Instruction:
Explain what the following Python 3 code does
def has_status(method=None, status='draft'):
    if method is None:
        return partial(has_status, status=status)

    @wraps(method)
    def wrapper(self, *args, **kwargs):
        """Check current deposit status."""
        if status != self.status:
            raise PIDInvalidAction()

        return method(self, *args, **kwargs)
    return wrapper

Response: Decorator to check that the current deposit has a defined status.


Instruction:
Can you write a function in Python 3 where it
preserves fields in deposit. :param method: Function to execute. (Default: ``None``) :param result: If `True` returns the result of method execution, otherwise `self`. (Default: ``True``) :param fields: List of fields to preserve (default: ``('_deposit',)``).

Response:
def preserve(method=None, result=True, fields=None):
    """Preserve fields in deposit.

    :param method: Function to execute. (Default: ``None``)
    :param result: If `True` returns the result of method execution,
        otherwise `self`. (Default: ``True``)
    :param fields: List of fields to preserve (default: ``('_deposit',)``).
    """
    if method is None:
        return partial(preserve, result=result, fields=fields)

    fields = fields or ('_deposit', )

    @wraps(method)
    def wrapper(self, *args, **kwargs):
        """Check current deposit status."""
        data = {field: self[field] for field in fields if field in self}
        result_ = method(self, *args, **kwargs)
        replace = result_ if result else self
        for field in data:
            replace[field] = data[field]
        return result_
    return wrapper


Instruction:
Can you write a function in Python 3 where it
returns an instance of deposit PID.

Response:
def pid(self):
        """Return an instance of deposit PID."""
        pid = self.deposit_fetcher(self.id, self)
        return PersistentIdentifier.get(pid.pid_type,
                                        pid.pid_value)


Instruction:
Write a Python 3 script to
convert deposit schema to a valid record schema.

Response:
def record_schema(self):
        """Convert deposit schema to a valid record schema."""
        schema_path = current_jsonschemas.url_to_path(self['$schema'])
        schema_prefix = current_app.config['DEPOSIT_JSONSCHEMAS_PREFIX']
        if schema_path and schema_path.startswith(schema_prefix):
            return current_jsonschemas.path_to_url(
                schema_path[len(schema_prefix):]
            )


Instruction:
Write a Python 3 script for
converting record schema to a valid deposit schema.

Response:
def build_deposit_schema(self, record):
        """Convert record schema to a valid deposit schema.

        :param record: The record used to build deposit schema.
        :returns: The absolute URL to the schema or `None`.
        """
        schema_path = current_jsonschemas.url_to_path(record['$schema'])
        schema_prefix = current_app.config['DEPOSIT_JSONSCHEMAS_PREFIX']
        if schema_path:
            return current_jsonschemas.path_to_url(
                schema_prefix + schema_path
            )


Instruction:
Implement a Python 3 function for
returning a tuple with PID and published record.

Response:
def fetch_published(self):
        """Return a tuple with PID and published record."""
        pid_type = self['_deposit']['pid']['type']
        pid_value = self['_deposit']['pid']['value']

        resolver = Resolver(
            pid_type=pid_type, object_type='rec',
            getter=partial(self.published_record_class.get_record,
                           with_deleted=True)
        )
        return resolver.resolve(pid_value)


Instruction:
How would you code a function in Python 3 to
merge changes with latest published version.

Response:
def merge_with_published(self):
        """Merge changes with latest published version."""
        pid, first = self.fetch_published()
        lca = first.revisions[self['_deposit']['pid']['revision_id']]
        # ignore _deposit and $schema field
        args = [lca.dumps(), first.dumps(), self.dumps()]
        for arg in args:
            del arg['$schema'], arg['_deposit']
        args.append({})
        m = Merger(*args)
        try:
            m.run()
        except UnresolvedConflictsException:
            raise MergeConflict()
        return patch(m.unified_patches, lca)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def commit(self, *args, **kwargs):
        return super(Deposit, self).commit(*args, **kwargs)

Response: Store changes on current instance in database and index it.


Instruction:
Given the following Python 3 function, write the documentation
def create(cls, data, id_=None):
        data.setdefault('$schema', current_jsonschemas.path_to_url(
            current_app.config['DEPOSIT_DEFAULT_JSONSCHEMA']
        ))
        if '_deposit' not in data:
            id_ = id_ or uuid.uuid4()
            cls.deposit_minter(id_, data)

        data['_deposit'].setdefault('owners', list())
        if current_user and current_user.is_authenticated:
            creator_id = int(current_user.get_id())

            if creator_id not in data['_deposit']['owners']:
                data['_deposit']['owners'].append(creator_id)

            data['_deposit']['created_by'] = creator_id

        return super(Deposit, cls).create(data, id_=id_)

Response: Create a new deposit.


Instruction:
Here you have a function in Python 3, explain what it does
def _process_files(self, record_id, data):
        if self.files:
            assert not self.files.bucket.locked
            self.files.bucket.locked = True
            snapshot = self.files.bucket.snapshot(lock=True)
            data['_files'] = self.files.dumps(bucket=snapshot.id)
            yield data
            db.session.add(RecordsBuckets(
                record_id=record_id, bucket_id=snapshot.id
            ))
        else:
            yield data

Response: Add files in record during first publishing.


Instruction:
Can you tell what is the following Python 3 function doing
def _publish_new(self, id_=None):
        minter = current_pidstore.minters[
            current_app.config['DEPOSIT_PID_MINTER']
        ]
        id_ = id_ or uuid.uuid4()
        record_pid = minter(id_, self)

        self['_deposit']['pid'] = {
            'type': record_pid.pid_type,
            'value': record_pid.pid_value,
            'revision_id': 0,
        }

        data = dict(self.dumps())
        data['$schema'] = self.record_schema

        with self._process_files(id_, data):
            record = self.published_record_class.create(data, id_=id_)

        return record

Response: Publish a new record.


Instruction:
How would you code a function in Python 3 to
publish the deposit after editing.

Response:
def _publish_edited(self):
        """Publish the deposit after for editing."""
        record_pid, record = self.fetch_published()
        if record.revision_id == self['_deposit']['pid']['revision_id']:
            data = dict(self.dumps())
        else:
            data = self.merge_with_published()

        data['$schema'] = self.record_schema
        data['_deposit'] = self['_deposit']
        record = record.__class__(data, model=record.model)
        return record


Instruction:
Write a Python 3 function that can
publish a new version of a deposit.

Response:
def publish(self, pid=None, id_=None):
        """Publish a deposit.

        If it's the first time:

        * it calls the minter and set the following meta information inside
            the deposit:

        .. code-block:: python

            deposit['_deposit'] = {
                'type': pid_type,
                'value': pid_value,
                'revision_id': 0,
            }

        * A dump of all information inside the deposit is done.

        * A snapshot of the files is done.

        Otherwise, published the new edited version.
        In this case, if in the mainwhile someone already published a new
        version, it'll try to merge the changes with the latest version.

        .. note:: no need for indexing as it calls `self.commit()`.

        Status required: ``'draft'``.

        :param pid: Force the new pid value. (Default: ``None``)
        :param id_: Force the new uuid value as deposit id. (Default: ``None``)
        :returns: Returns itself.
        """
        pid = pid or self.pid

        if not pid.is_registered():
            raise PIDInvalidAction()

        self['_deposit']['status'] = 'published'

        if self['_deposit'].get('pid') is None:  # First publishing
            self._publish_new(id_=id_)
        else:  # Update after edit
            record = self._publish_edited()
            record.commit()
        self.commit()
        return self


Instruction:
Write a Python 3 script for
updating selected keys. :param record: The record to prepare.

Response:
def _prepare_edit(self, record):
        """Update selected keys.

        :param record: The record to prepare.
        """
        data = record.dumps()
        # Keep current record revision for merging.
        data['_deposit']['pid']['revision_id'] = record.revision_id
        data['_deposit']['status'] = 'draft'
        data['$schema'] = self.build_deposit_schema(record)
        return data


Instruction:
Can you implement a function in Python 3 that
edits the current object.

Response:
def edit(self, pid=None):
        """Edit deposit.

        #. The signal :data:`invenio_records.signals.before_record_update`
           is sent before the edit execution.

        #. The following meta information are saved inside the deposit:

        .. code-block:: python

            deposit['_deposit']['pid'] = record.revision_id
            deposit['_deposit']['status'] = 'draft'
            deposit['$schema'] = deposit_schema_from_record_schema

        #. The signal :data:`invenio_records.signals.after_record_update` is
            sent after the edit execution.

        #. The deposit index is updated.

        Status required: `published`.

        .. note:: the process fails if the pid has status
            :attr:`invenio_pidstore.models.PIDStatus.REGISTERED`.

        :param pid: Force a pid object. (Default: ``None``)
        :returns: A new Deposit object.
        """
        pid = pid or self.pid

        with db.session.begin_nested():
            before_record_update.send(
                current_app._get_current_object(), record=self)

            record_pid, record = self.fetch_published()
            assert PIDStatus.REGISTERED == record_pid.status
            assert record['_deposit'] == self['_deposit']

            self.model.json = self._prepare_edit(record)

            flag_modified(self.model, 'json')
            db.session.merge(self.model)

        after_record_update.send(
            current_app._get_current_object(), record=self)
        return self.__class__(self.model.json, model=self.model)


Instruction:
Here you have a function in Python 3, explain what it does
def discard(self, pid=None):
        pid = pid or self.pid

        with db.session.begin_nested():
            before_record_update.send(
                current_app._get_current_object(), record=self)

            _, record = self.fetch_published()
            self.model.json = deepcopy(record.model.json)
            self.model.json['$schema'] = self.build_deposit_schema(record)

            flag_modified(self.model, 'json')
            db.session.merge(self.model)

        after_record_update.send(
            current_app._get_current_object(), record=self)
        return self.__class__(self.model.json, model=self.model)

Response: Discards the current record.


Instruction:
How would you code a function in Python 3 to
delete deposit. Status required: ``'draft'``. :param force: Force deposit delete. (Default: ``True``) :param pid: Force pid object. (Default: ``None``) :returns: A new Deposit object.

Response:
def delete(self, force=True, pid=None):
        """Delete deposit.

        Status required: ``'draft'``.

        :param force: Force deposit delete.  (Default: ``True``)
        :param pid: Force pid object.  (Default: ``None``)
        :returns: A new Deposit object.
        """
        pid = pid or self.pid

        if self['_deposit'].get('pid'):
            raise PIDInvalidAction()
        if pid:
            pid.delete()
        return super(Deposit, self).delete(force=force)


Instruction:
Write a Python 3 script to
clear only drafts. Status required: ``'draft'``. Meta information inside `_deposit` are preserved.

Response:
def clear(self, *args, **kwargs):
        """Clear only drafts.

        Status required: ``'draft'``.

        Meta information inside `_deposit` are preserved.
        """
        super(Deposit, self).clear(*args, **kwargs)


Instruction:
How would you implement a function in Python 3 that
updates only drafts. Status required : drafts. Meta information inside _deposit are preserved.

Response:
def update(self, *args, **kwargs):
        """Update only drafts.

        Status required: ``'draft'``.

        Meta information inside `_deposit` are preserved.
        """
        super(Deposit, self).update(*args, **kwargs)


Instruction:
Make a summary of the following Python 3 code
def patch(self, *args, **kwargs):
        return super(Deposit, self).patch(*args, **kwargs)

Response:
Patch only drafts.
        Status required.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def files(self):
        files_ = super(Deposit, self).files

        if files_:
            sort_by_ = files_.sort_by

            def sort_by(*args, **kwargs):
                """Only in draft state."""
                if 'draft' != self.status:
                    raise PIDInvalidAction()
                return sort_by_(*args, **kwargs)

            files_.sort_by = sort_by

        return files_

Response: List of files inside the deposit.


Instruction:
Implement a function in Python 3 to
convert a reStructuredText into its node

Response:
def rst2node(doc_name, data):
    """Converts a reStructuredText into its node
    """
    if not data:
        return
    parser = docutils.parsers.rst.Parser()
    document = docutils.utils.new_document('<%s>' % doc_name)
    document.settings = docutils.frontend.OptionParser().get_default_values()
    document.settings.tab_width = 4
    document.settings.pep_references = False
    document.settings.rfc_references = False
    document.settings.env = Env()
    parser.parse(data, document)
    if len(document.children) == 1:
        return document.children[0]
    else:
        par = docutils.nodes.paragraph()
        for child in document.children:
            par += child
        return par


Instruction:
Here you have a function in Python 3, explain what it does
def setup(app):
    if 'http' not in app.domains:
        httpdomain.setup(app)

    app.add_directive('autopyramid', RouteDirective)

Response: Hook the directives when Sphinx asks for it.


Instruction:
Explain what the following Python 3 code does
def _parse_response(self, response):
        if not self._raise_errors:
            return response

        is_4xx_error = str(response.status_code)[0] == '4'
        is_5xx_error = str(response.status_code)[0] == '5'
        content = response.content

        if response.status_code == 403:
            raise AuthenticationError(content)
        elif is_4xx_error:
            raise APIError(content)
        elif is_5xx_error:
            raise ServerError(content)

        return response

Response: Parses the response and raises appropriate errors if raise_errors is set to True.


Instruction:
Explain what the following Python 3 code does
def _api_request(self, endpoint, http_method, *args, **kwargs):
        logger.debug(' > Sending API request to endpoint: %s' % endpoint)

        auth = self._build_http_auth()

        headers = self._build_request_headers(kwargs.get('headers'))
        logger.debug('\theaders: %s' % headers)

        path = self._build_request_path(endpoint)
        logger.debug('\tpath: %s' % path)

        data = self._build_payload(kwargs.get('payload'))
        if not data:
            data = kwargs.get('data')
        logger.debug('\tdata: %s' % data)

        req_kw = dict(
            auth=auth,
            headers=headers,
            timeout=kwargs.get('timeout', self.DEFAULT_TIMEOUT)
        )

        # do some error handling
        if (http_method == self.HTTP_POST):
            if (data):
                r = requests.post(path, data=data, **req_kw)
            else:
                r = requests.post(path, **req_kw)
        elif http_method == self.HTTP_PUT:
            if (data):
                r = requests.put(path, data=data, **req_kw)
            else:
                r = requests.put(path, **req_kw)
        elif http_method == self.HTTP_DELETE:
            r = requests.delete(path, **req_kw)
        else:
            r = requests.get(path, **req_kw)

        logger.debug('\tresponse code:%s' % r.status_code)
        try:
            logger.debug('\tresponse: %s' % r.json())
        except:
            logger.debug('\tresponse: %s' % r.content)

        return self._parse_response(r)

Response: Private method for making API requests


Instruction:
Given the following Python 3 function, write the documentation
def get_log(self, log_id, timeout=None):
        return self._api_request(
            self.GET_LOG_ENDPOINT % log_id,
            self.HTTP_GET,
            timeout=timeout
        )

Response: Get a specific log entry


Instruction:
Write a Python 3 function for
getting log events for a specific log entry

Response:
def get_log_events(self, log_id, timeout=None):
        """ API call to get a specific log entry """
        return self._api_request(
            self.GET_LOG_EVENTS_ENDPOINT % log_id,
            self.HTTP_GET,
            timeout=timeout
        )


Instruction:
Write a Python 3 script to
get a list of templates

Response:
def templates(self, timeout=None):
        """ API call to get a list of templates """
        return self._api_request(
            self.TEMPLATES_ENDPOINT,
            self.HTTP_GET,
            timeout=timeout
        )


Instruction:
Explain what the following Python 3 code does
def create_new_version(
        self,
        name,
        subject,
        text='',
        template_id=None,
        html=None,
        locale=None,
        timeout=None
    ):
        """ API call to create a new version of a template """
        if(html):
            payload = {
                'name': name,
                'subject': subject,
                'html': html,
                'text': text
            }
        else:
            payload = {
                'name': name,
                'subject': subject,
                'text': text
            }

        if locale:
            url = self.TEMPLATES_SPECIFIC_LOCALE_VERSIONS_ENDPOINT % (
                template_id,
                locale
            )
        else:
            url = self.TEMPLATES_NEW_VERSION_ENDPOINT % template_id

        return self._api_request(
            url,
            self.HTTP_POST,
            payload=payload,
            timeout=timeout
        )

Response: This method creates a new version of a template.


Instruction:
Can you tell what is the following Python 3 function doing
def update_template_version(
        self,
        name,
        subject,
        template_id,
        version_id,
        text='',
        html=None,
        timeout=None
    ):
        """ API call to update a template version """
        if(html):
            payload = {
                'name': name,
                'subject': subject,
                'html': html,
                'text': text
            }
        else:
            payload = {
                'name': name,
                'subject': subject,
                'text': text
            }

        return self._api_request(
            self.TEMPLATES_VERSION_ENDPOINT % (template_id, version_id),
            self.HTTP_PUT,
            payload=payload,
            timeout=timeout
        )

Response: Update a template version


Instruction:
Can you create a Python 3 function that
gets list of snippets from the API

Response:
def snippets(self, timeout=None):
        """ API call to get list of snippets """
        return self._api_request(
            self.SNIPPETS_ENDPOINT,
            self.HTTP_GET,
            timeout=timeout
        )


Instruction:
Given the following Python 3 function, write the documentation
def get_snippet(self, snippet_id, timeout=None):
        return self._api_request(
            self.SNIPPET_ENDPOINT % (snippet_id),
            self.HTTP_GET,
            timeout=timeout
        )

Response: Get a specific Snippet


Instruction:
Can you generate the documentation for the following Python 3 function
def create_snippet(self, name, body, timeout=None):
        payload = {
            'name': name,
            'body': body
        }
        return self._api_request(
            self.SNIPPETS_ENDPOINT,
            self.HTTP_POST,
            payload=payload,
            timeout=timeout
        )

Response: API call to create a Snippet


Instruction:
Write a Python 3 script for
making a dictionary with filename and base64 file data

Response:
def _make_file_dict(self, f):
        """Make a dictionary with filename and base64 file data"""
        if isinstance(f, dict):
            file_obj = f['file']
            if 'filename' in f:
                file_name = f['filename']
            else:
                file_name = file_obj.name
        else:
            file_obj = f
            file_name = f.name

        b64_data = base64.b64encode(file_obj.read())
        return {
            'id': file_name,
            'data': b64_data.decode() if six.PY3 else b64_data,
        }


Instruction:
Implement a function in Python 3 to
send an email to the specified recipient.

Response:
def send(
        self,
        email_id,
        recipient,
        email_data=None,
        sender=None,
        cc=None,
        bcc=None,
        tags=[],
        headers={},
        esp_account=None,
        locale=None,
        email_version_name=None,
        inline=None,
        files=[],
        timeout=None
    ):
        """ API call to send an email """
        if not email_data:
            email_data = {}

        # for backwards compatibility, will be removed
        if isinstance(recipient, string_types):
            warnings.warn(
                "Passing email directly for recipient is deprecated",
                DeprecationWarning)
            recipient = {'address': recipient}

        payload = {
            'email_id': email_id,
            'recipient': recipient,
            'email_data': email_data
        }

        if sender:
            payload['sender'] = sender
        if cc:
            if not type(cc) == list:
                logger.error(
                    'kwarg cc must be type(list), got %s' % type(cc))
            payload['cc'] = cc
        if bcc:
            if not type(bcc) == list:
                logger.error(
                    'kwarg bcc must be type(list), got %s' % type(bcc))
            payload['bcc'] = bcc

        if tags:
            if not type(tags) == list:
                logger.error(
                    'kwarg tags must be type(list), got %s' % (type(tags)))
            payload['tags'] = tags

        if headers:
            if not type(headers) == dict:
                logger.error(
                    'kwarg headers must be type(dict), got %s' % (
                        type(headers)
                    )
                )
            payload['headers'] = headers

        if esp_account:
            if not isinstance(esp_account, string_types):
                logger.error(
                    'kwarg esp_account must be a string, got %s' % (
                        type(esp_account)
                    )
                )
            payload['esp_account'] = esp_account

        if locale:
            if not isinstance(locale, string_types):
                logger.error(
                    'kwarg locale must be a string, got %s' % (type(locale))
                )
            payload['locale'] = locale

        if email_version_name:
            if not isinstance(email_version_name, string_types):
                logger.error(
                    'kwarg email_version_name must be a string, got %s' % (
                        type(email_version_name)))
            payload['version_name'] = email_version_name

        if inline:
            payload['inline'] = self._make_file_dict(inline)

        if files:
            payload['files'] = [self._make_file_dict(f) for f in files]

        return self._api_request(
            self.SEND_ENDPOINT,
            self.HTTP_POST,
            payload=payload,
            timeout=timeout
        )


Instruction:
Can you generate the documentation for the following Python 3 function
def _api_request(self, endpoint, http_method, *args, **kwargs):
        logger.debug(' > Queing batch api request for endpoint: %s' % endpoint)

        path = self._build_request_path(endpoint, absolute=False)
        logger.debug('\tpath: %s' % path)

        data = None
        if 'payload' in kwargs:
            data = kwargs['payload']
        logger.debug('\tdata: %s' % data)

        command = {
            "path": path,
            "method": http_method
        }
        if data:
            command['body'] = data

        self._commands.append(command)

Response: Private method for api requests


Instruction:
Write a Python 3 function that can
execute all currently queued batch commands

Response:
def execute(self, timeout=None):
        """Execute all currently queued batch commands"""
        logger.debug(' > Batch API request (length %s)' % len(self._commands))

        auth = self._build_http_auth()

        headers = self._build_request_headers()
        logger.debug('\tbatch headers: %s' % headers)

        logger.debug('\tbatch command length: %s' % len(self._commands))

        path = self._build_request_path(self.BATCH_ENDPOINT)

        data = json.dumps(self._commands, cls=self._json_encoder)
        r = requests.post(
            path,
            auth=auth,
            headers=headers,
            data=data,
            timeout=(self.DEFAULT_TIMEOUT if timeout is None else timeout)
        )

        self._commands = []

        logger.debug('\tresponse code:%s' % r.status_code)
        try:
            logger.debug('\tresponse: %s' % r.json())
        except:
            logger.debug('\tresponse: %s' % r.content)

        return r


Instruction:
Here you have a function in Python 3, explain what it does
def get_group_tabs(self):
        if self.tab_group is None:
            raise ImproperlyConfigured(
                "%s requires a definition of 'tab_group'" %
                self.__class__.__name__)
        group_members = [t for t in self._registry if t.tab_group == self.tab_group]
        return [t() for t in group_members]

Response: Return all other tabs that are members of the tab s tab_group.


Instruction:
Write a Python 3 script for
processing and prepares tabs.

Response:
def _process_tabs(self, tabs, current_tab, group_current_tab):
        """
        Process and prepare tabs.

        This includes steps like updating references to the current tab,
        filtering out hidden tabs, sorting tabs etc...

        Args:
            tabs:
                The list of tabs to process.
            current_tab:
                The reference to the currently loaded tab.
            group_current_tab:
                The reference to the active tab in the current tab group. For
                parent tabs, this is different than for the current tab group.

        Returns:
            Processed list of tabs. Note that the method may have side effects.

        """
        # Update references to the current tab
        for t in tabs:
            t.current_tab = current_tab
            t.group_current_tab = group_current_tab

        # Filter out hidden tabs
        tabs = list(filter(lambda t: t.tab_visible, tabs))

        # Sort remaining tabs in-place
        tabs.sort(key=lambda t: t.weight)

        return tabs


Instruction:
Create a Python 3 function for
adding tab information to the context.

Response:
def get_context_data(self, **kwargs):
        """
        Adds tab information to context.

        To retrieve a list of all group tab instances, use
        ``{{ tabs }}`` in your template.

        The id of the current tab is added as ``current_tab_id`` to the
        template context.

        If the current tab has a parent tab the parent's id is added to
        the template context as ``parent_tab_id``. Instances of all tabs
        of the parent level are added as ``parent_tabs`` to the context.

        If the current tab has children they are added to the template
        context as ``child_tabs``.

        """
        context = super(TabView, self).get_context_data(**kwargs)

        # Update the context with kwargs, TemplateView doesn't do this.
        context.update(kwargs)

        # Add tabs and "current" references to context
        process_tabs_kwargs = {
            'tabs': self.get_group_tabs(),
            'current_tab': self,
            'group_current_tab': self,
        }
        context['tabs'] = self._process_tabs(**process_tabs_kwargs)
        context['current_tab_id'] = self.tab_id

        # Handle parent tabs
        if self.tab_parent is not None:
            # Verify that tab parent is valid
            if self.tab_parent not in self._registry:
                msg = '%s has no attribute _is_tab' % self.tab_parent.__class__.__name__
                raise ImproperlyConfigured(msg)

            # Get parent tab instance
            parent = self.tab_parent()

            # Add parent tabs to context
            process_parents_kwargs = {
                'tabs': parent.get_group_tabs(),
                'current_tab': self,
                'group_current_tab': parent,
            }
            context['parent_tabs'] = self._process_tabs(**process_parents_kwargs)
            context['parent_tab_id'] = parent.tab_id

        # Handle child tabs
        if self.tab_id in self._children:
            process_children_kwargs = {
                'tabs': [t() for t in self._children[self.tab_id]],
                'current_tab': self,
                'group_current_tab': None,
            }
            context['child_tabs'] = self._process_tabs(**process_children_kwargs)

        return context


Instruction:
Write a Python 3 script to
convert a string into a valid python attribute name.

Response:
def normalize_name(s):
    """Convert a string into a valid python attribute name.
    This function is called to convert ASCII strings to something that can pass as
    python attribute name, to be used with namedtuples.

    >>> str(normalize_name('class'))
    'class_'
    >>> str(normalize_name('a-name'))
    'a_name'
    >>> str(normalize_name('a n\u00e4me'))
    'a_name'
    >>> str(normalize_name('Name'))
    'Name'
    >>> str(normalize_name(''))
    '_'
    >>> str(normalize_name('1'))
    '_1'
    """
    s = s.replace('-', '_').replace('.', '_').replace(' ', '_')
    if s in keyword.kwlist:
        return s + '_'
    s = '_'.join(slug(ss, lowercase=False) for ss in s.split('_'))
    if not s:
        s = '_'
    if s[0] not in string.ascii_letters + '_':
        s = '_' + s
    return s


Instruction:
How would you explain what the following Python 3 function does
def schema(tg):
    tables = {}
    for tname, table in tg.tabledict.items():
        t = TableSpec.from_table_metadata(table)
        tables[t.name] = t
        for at in t.many_to_many.values():
            tables[at.name] = at

    # We must determine the order in which tables must be created!
    ordered = OrderedDict()
    i = 0

    # We loop through the tables repeatedly, and whenever we find one, which has all
    # referenced tables already in ordered, we move it from tables to ordered.
    while tables and i < 100:
        i += 1
        for table in list(tables.keys()):
            if all((ref[1] in ordered) or ref[1] == table for ref in tables[table].foreign_keys):
                # All referenced tables are already created (or self-referential).
                ordered[table] = tables.pop(table)
                break
    if tables:  # pragma: no cover
        raise ValueError('there seem to be cyclic dependencies between the tables')

    return list(ordered.values())

Response: Convert the table and column descriptions of a TableGroup into a pair of tables and reference tables.


Instruction:
How would you explain what the following Python 3 function does
def write(self, _force=False, _exists_ok=False, **items):
        if self.fname and self.fname.exists():
            raise ValueError('db file already exists, use force=True to overwrite')

        with self.connection() as db:
            for table in self.tables:
                db.execute(table.sql(translate=self.translate))

            db.execute('PRAGMA foreign_keys = ON;')
            db.commit()

            refs = defaultdict(list)  # collects rows in association tables.
            for t in self.tables:
                if t.name not in items:
                    continue
                rows, keys = [], []
                cols = {c.name: c for c in t.columns}
                for i, row in enumerate(items[t.name]):
                    pk = row[t.primary_key[0]] \
                        if t.primary_key and len(t.primary_key) == 1 else None
                    values = []
                    for k, v in row.items():
                        if k in t.many_to_many:
                            assert pk
                            at = t.many_to_many[k]
                            atkey = tuple([at.name] + [c.name for c in at.columns])
                            for vv in v:
                                fkey, context = self.association_table_context(t, k, vv)
                                refs[atkey].append((pk, fkey, context))
                        else:
                            col = cols[k]
                            if isinstance(v, list):
                                # Note: This assumes list-valued columns are of datatype string!
                                v = (col.separator or ';').join(
                                    col.convert(vv) for vv in v)
                            else:
                                v = col.convert(v) if v is not None else None
                            if i == 0:
                                keys.append(col.name)
                            values.append(v)
                    rows.append(tuple(values))
                insert(db, self.translate, t.name, keys, *rows)

            for atkey, rows in refs.items():
                insert(db, self.translate, atkey[0], atkey[1:], *rows)

            db.commit()

Response: Writes the items to the database.


Instruction:
Given the following Python 3 function, write the documentation
def iterrows(lines_or_file, namedtuples=False, dicts=False, encoding='utf-8', **kw):
    if namedtuples and dicts:
        raise ValueError('either namedtuples or dicts can be chosen as output format')
    elif namedtuples:
        _reader = NamedTupleReader
    elif dicts:
        _reader = UnicodeDictReader
    else:
        _reader = UnicodeReader

    with _reader(lines_or_file, encoding=encoding, **fix_kw(kw)) as r:
        for item in r:
            yield item

Response: A generator function that yields the rows of a n - tuple or dict.


Instruction:
Can you generate the documentation for the following Python 3 function
def filter_rows_as_dict(fname, filter_, **kw):
    filter_ = DictFilter(filter_)
    rewrite(fname, filter_, **kw)
    return filter_.removed

Response: Rewrite a dsv file with filtering the rows.


Instruction:
Explain what the following Python 3 code does
def dump_grid(grid):
    header = 'ver:%s' % dump_str(str(grid._version), version=grid._version)
    if bool(grid.metadata):
        header += ' ' + dump_meta(grid.metadata, version=grid._version)
    columns = dump_columns(grid.column, version=grid._version)
    rows = dump_rows(grid)
    return '\n'.join([header, columns] + rows + [''])

Response: Dump a single grid to its ZINC representation.


Instruction:
Write a Python 3 function for
parsing the given Zinc text and returns the equivalent data.

Response:
def parse(grid_str, mode=MODE_ZINC, charset='utf-8'):
    '''
    Parse the given Zinc text and return the equivalent data.
    '''
    # Decode incoming text (or python3 will whine!)
    if isinstance(grid_str, six.binary_type):
        grid_str = grid_str.decode(encoding=charset)

    # Split the separate grids up, the grammar definition has trouble splitting
    # them up normally.  This will truncate the newline off the end of the last
    # row.
    _parse = functools.partial(parse_grid, mode=mode,
            charset=charset)
    if mode == MODE_JSON:
        if isinstance(grid_str, six.string_types):
            grid_data = json.loads(grid_str)
        else:
            grid_data = grid_str
        if isinstance(grid_data, dict):
            return _parse(grid_data)
        else:
            return list(map(_parse, grid_data))
    else:
        return list(map(_parse, GRID_SEP.split(grid_str.rstrip())))


Instruction:
Explain what the following Python 3 code does
def append(self, key, value=MARKER, replace=True):
        '''
        Append the item to the metadata.
        '''
        return self.add_item(key, value, replace=replace)

Response: Append the item to the metadata.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def extend(self, items, replace=True):
        '''
        Append the items to the metadata.
        '''
        if isinstance(items, dict) or isinstance(items, SortableDict):
            items = list(items.items())

        for (key, value) in items:
            self.append(key, value, replace=replace)

Response: Append the items to the metadata.


Instruction:
Can you generate the documentation for the following Python 3 function
def regular_polygon(cls, center, radius, n_vertices, start_angle=0, **kwargs):
        angles = (np.arange(n_vertices) * 2 * np.pi / n_vertices) + start_angle
        return cls(center + radius * np.array([np.cos(angles), np.sin(angles)]).T, **kwargs)

Response: Construct a regular polygon.


Instruction:
Create a Python 3 function to
construct a circle. Parameters ---------- center : array-like radius : float n_vertices : int, optional Number of points to draw. Decrease for performance, increase for appearance. kwargs Other keyword arguments are passed to the |Shape| constructor.

Response:
def circle(cls, center, radius, n_vertices=50, **kwargs):
        """Construct a circle.

        Parameters
        ----------
        center : array-like
        radius : float
        n_vertices : int, optional
            Number of points to draw.
            Decrease for performance, increase for appearance.
        kwargs
            Other keyword arguments are passed to the |Shape| constructor.

        """
        return cls.regular_polygon(center, radius, n_vertices, **kwargs)


Instruction:
Here you have a function in Python 3, explain what it does
def rectangle(cls, vertices, **kwargs):
        bottom_left, top_right = vertices
        top_left = [bottom_left[0], top_right[1]]
        bottom_right = [top_right[0], bottom_left[1]]
        return cls([bottom_left, bottom_right, top_right, top_left], **kwargs)

Response: Shortcut for creating a rectangle aligned with the screen axes from only two corners.


Instruction:
Create a Python 3 function to
create a |Shape| from a dictionary specification.

Response:
def from_dict(cls, spec):
        """Create a |Shape| from a dictionary specification.

        Parameters
        ----------
        spec : dict
            A dictionary with either the fields ``'center'`` and ``'radius'`` (for a circle),
            ``'center'``, ``'radius'``, and ``'n_vertices'`` (for a regular polygon),
            or ``'vertices'``.
            If only two vertices are given, they are assumed to be lower left and top right corners of a rectangle.
            Other fields are interpreted as keyword arguments.

        """
        spec = spec.copy()
        center = spec.pop('center', None)
        radius = spec.pop('radius', None)
        if center and radius:
            return cls.circle(center, radius, **spec)

        vertices = spec.pop('vertices')
        if len(vertices) == 2:
            return cls.rectangle(vertices, **spec)

        return cls(vertices, **spec)


Instruction:
Can you write a function in Python 3 where it
rotates the shape in - place.

Response:
def rotate(self, angle, center=None):
        """Rotate the shape, in-place.

        Parameters
        ----------
        angle : float
            Angle to rotate, in radians counter-clockwise.
        center : array-like, optional
            Point about which to rotate.
            If not passed, the center of the shape will be used.

        """
        args = [angle]
        if center is not None:
            args.extend(center)
        self.poly.rotate(*args)
        return self


Instruction:
Can you write a function in Python 3 where it
flips the shape in the x direction in - place.

Response:
def flip_x(self, center=None):
        """Flip the shape in the x direction, in-place.

        Parameters
        ----------
        center : array-like, optional
            Point about which to flip.
            If not passed, the center of the shape will be used.

         """
        if center is None:
            self.poly.flip()
        else:
            self.poly.flip(center[0])


Instruction:
Given the following Python 3 function, write the documentation
def flip_y(self, center=None):
        if center is None:
            self.poly.flop()
        else:
            self.poly.flop(center[1])
        return self

Response: Flip the shape in the y direction in - place.


Instruction:
Write a Python 3 function for
flipping the shape in an arbitrary direction.

Response:
def flip(self, angle, center=None):
        """ Flip the shape in an arbitrary direction.

        Parameters
        ----------
        angle : array-like
            The angle, in radians counter-clockwise from the horizontal axis,
            defining the angle about which to flip the shape (of a line through `center`).
        center : array-like, optional
            The point about which to flip.
            If not passed, the center of the shape will be used.

        """
        return self.rotate(-angle, center=center).flip_y(center=center).rotate(angle, center=center)


Instruction:
Given the following Python 3 function, write the documentation
def draw(self):
        if self.enabled:
            self._vertex_list.colors = self._gl_colors
            self._vertex_list.vertices = self._gl_vertices
            self._vertex_list.draw(pyglet.gl.GL_TRIANGLES)

Response: Draw the shape in the current OpenGL context.


Instruction:
Write a Python 3 function for
updating the shape s position by moving it forward according to its velocity.

Response:
def update(self, dt):
        """Update the shape's position by moving it forward according to its velocity.

        Parameters
        ----------
        dt : float

        """
        self.translate(dt * self.velocity)
        self.rotate(dt * self.angular_velocity)


Instruction:
How would you implement a function in Python 3 that
maps the official Haystack timezone list to those recognised by pytz.

Response:
def _map_timezones():
    """
    Map the official Haystack timezone list to those recognised by pytz.
    """
    tz_map = {}
    todo = HAYSTACK_TIMEZONES_SET.copy()
    for full_tz in pytz.all_timezones:
        # Finished case:
        if not bool(todo): # pragma: no cover
            # This is nearly impossible for us to cover, and an unlikely case.
            break

        # Case 1: exact match
        if full_tz in todo:
            tz_map[full_tz] = full_tz   # Exact match
            todo.discard(full_tz)
            continue

        # Case 2: suffix match after '/'
        if '/' not in full_tz:
            continue

        (prefix, suffix) = full_tz.split('/',1)
        # Case 2 exception: full timezone contains more than one '/' -> ignore
        if '/' in suffix:
            continue

        if suffix in todo:
            tz_map[suffix] = full_tz
            todo.discard(suffix)
            continue

    return tz_map


Instruction:
Can you implement a function in Python 3 that
retrieves the Haystack timezone

Response:
def timezone(haystack_tz, version=LATEST_VER):
    """
    Retrieve the Haystack timezone
    """
    tz_map = get_tz_map(version=version)
    try:
        tz_name = tz_map[haystack_tz]
    except KeyError:
        raise ValueError('%s is not a recognised timezone on this host' \
                % haystack_tz)
    return pytz.timezone(tz_name)


Instruction:
Given the following Python 3 function, write the documentation
def timezone_name(dt, version=LATEST_VER):
    tz_rmap = get_tz_rmap(version=version)
    if dt.tzinfo is None:
        raise ValueError('%r has no timezone' % dt)

    # Easy case: pytz timezone.
    try:
        tz_name = dt.tzinfo.zone
        return tz_rmap[tz_name]
    except KeyError:
        # Not in timezone map
        pass
    except AttributeError:
        # Not a pytz-compatible tzinfo
        pass

    # Hard case, try to find one that's equivalent.  Hopefully we don't get
    # many of these.  Start by getting the current timezone offset, and a
    # timezone-naïve copy of the timestamp.
    offset  = dt.utcoffset()
    dt_notz = dt.replace(tzinfo=None)

    if offset == datetime.timedelta(0):
        # UTC?
        return 'UTC'

    for olson_name, haystack_name in list(tz_rmap.items()):
        if pytz.timezone(olson_name).utcoffset(dt_notz) == offset:
            return haystack_name

    raise ValueError('Unable to get timezone of %r' % dt)

Response: Determine an appropriate timezone name for the given datetime object.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _unescape(s, uri=False):
    out = ''
    while len(s) > 0:
        c = s[0]
        if c == '\\':
            # Backslash escape
            esc_c = s[1]

            if esc_c in ('u', 'U'):
                # Unicode escape
                out += six.unichr(int(s[2:6], base=16))
                s = s[6:]
                continue
            else:
                if esc_c == 'b':
                    out += '\b'
                elif esc_c == 'f':
                    out += '\f'
                elif esc_c == 'n':
                    out += '\n'
                elif esc_c == 'r':
                    out += '\r'
                elif esc_c == 't':
                    out += '\t'
                else:
                    if uri and (esc_c == '#'):
                        # \# is passed through with backslash.
                        out += '\\'
                    # Pass through
                    out += esc_c
                s = s[2:]
                continue
        else:
            out += c
            s = s[1:]
    return out

Response: Iterative parser for string escapes.


Instruction:
Make a summary of the following Python 3 code
def parse_grid(grid_data):
    try:
        # Split the grid up.
        grid_parts = NEWLINE_RE.split(grid_data)
        if len(grid_parts) < 2:
            raise ZincParseException('Malformed grid received',
                    grid_data, 1, 1)

        # Grid and column metadata are the first two lines.
        grid_meta_str = grid_parts.pop(0)
        col_meta_str = grid_parts.pop(0)

        # First element is the grid metadata
        ver_match = VERSION_RE.match(grid_meta_str)
        if ver_match is None:
            raise ZincParseException(
                    'Could not determine version from %r' % grid_meta_str,
                    grid_data, 1, 1)
        version = Version(ver_match.group(1))

        # Now parse the rest of the grid accordingly
        try:
            grid_meta = hs_gridMeta[version].parseString(grid_meta_str, parseAll=True)[0]
        except pp.ParseException as pe:
            # Raise a new exception with the appropriate line number.
            raise ZincParseException(
                    'Failed to parse grid metadata: %s' % pe,
                    grid_data, 1, pe.col)
        except: # pragma: no cover
            # Report an error to the log if we fail to parse something.
            LOG.debug('Failed to parse grid meta: %r', grid_meta_str)
            raise

        try:
            col_meta = hs_cols[version].parseString(col_meta_str, parseAll=True)[0]
        except pp.ParseException as pe:
            # Raise a new exception with the appropriate line number.
            raise ZincParseException(
                    'Failed to parse column metadata: %s' \
                            % reformat_exception(pe, 2),
                    grid_data, 2, pe.col)
        except: # pragma: no cover
            # Report an error to the log if we fail to parse something.
            LOG.debug('Failed to parse column meta: %r', col_meta_str)
            raise

        row_grammar = hs_row[version]
        def _parse_row(row_num_and_data):
            (row_num, row) = row_num_and_data
            line_num = row_num + 3

            try:
                return dict(zip(col_meta.keys(),
                    row_grammar.parseString(row, parseAll=True)[0].asList()))
            except pp.ParseException as pe:
                # Raise a new exception with the appropriate line number.
                raise ZincParseException(
                        'Failed to parse row: %s' \
                            % reformat_exception(pe, line_num),
                        grid_data, line_num, pe.col)
            except: # pragma: no cover
                # Report an error to the log if we fail to parse something.
                LOG.debug('Failed to parse row: %r', row)
                raise

        g = Grid(version=grid_meta.pop('ver'),
                metadata=grid_meta,
                columns=list(col_meta.items()))
        g.extend(map(_parse_row, filter(lambda gp : bool(gp[1]), enumerate(grid_parts))))
        return g
    except:
        LOG.debug('Failing grid: %r', grid_data)
        raise

Response: Parses the incoming grid and returns a list of objects.


Instruction:
Write a Python 3 script to
parse a Zinc scalar in Zinc format.

Response:
def parse_scalar(scalar_data, version):
    """
    Parse a Project Haystack scalar in ZINC format.
    """
    try:
        return hs_scalar[version].parseString(scalar_data, parseAll=True)[0]
    except pp.ParseException as pe:
        # Raise a new exception with the appropriate line number.
        raise ZincParseException(
                'Failed to parse scalar: %s' % reformat_exception(pe),
                scalar_data, 1, pe.col)
    except:
        LOG.debug('Failing scalar data: %r (version %r)',
                scalar_data, version)


Instruction:
Here you have a function in Python 3, explain what it does
def add_item(self, key, value, after=False, index=None, pos_key=None,
            replace=True):
        """
        Add an item at a specific location, possibly replacing the
        existing item.

        If after is True, we insert *after* the given index, otherwise we
        insert before.

        The position is specified using either index or pos_key, the former
        specifies the position from the start of the array (base 0).  pos_key
        specifies the name of another key, and positions the new key relative
        to that key.

        When replacing, the position will be left un-changed unless a location
        is specified explicitly.
        """
        if self._validate_fn:
            self._validate_fn(value)

        if (index is not None) and (pos_key is not None):
            raise ValueError('Either specify index or pos_key, not both.')
        elif pos_key is not None:
            try:
                index = self.index(pos_key)
            except ValueError:
                raise KeyError('%r not found' % pos_key)

        if after and (index is not None):
            # insert inserts *before* index, so increment by one.
            index += 1

        if key in self._values:
            if not replace:
                raise KeyError('%r is duplicate' % key)

            if index is not None:
                # We are re-locating.
                del self[key]
            else:
                # We are updating
                self._values[key] = value
                return

        if index is not None:
            # Place at given position
            self._order.insert(index, key)
        else:
            # Place at end
            self._order.append(key)
        self._values[key] = value

Response: Add an item to the internal dictionary.


Instruction:
Write a Python 3 function that can
dump the given grids in the specified over - the - wire format.

Response:
def dump(grids, mode=MODE_ZINC):
    """
    Dump the given grids in the specified over-the-wire format.
    """
    if isinstance(grids, Grid):
        return dump_grid(grids, mode=mode)
    _dump = functools.partial(dump_grid, mode=mode)
    if mode == MODE_ZINC:
        return '\n'.join(map(_dump, grids))
    elif mode == MODE_JSON:
        return '[%s]' % ','.join(map(_dump, grids))
    else: # pragma: no cover
        raise NotImplementedError('Format not implemented: %s' % mode)


Instruction:
Can you write a function in Python 3 where it
converts a unit to a haystack unit.

Response:
def to_haystack(unit):
    """
    Some parsing tweaks to fit pint units / handling of edge cases.
    """
    unit = str(unit)
    global HAYSTACK_CONVERSION
    global PINT_CONVERSION
    if unit == 'per_minute' or \
        unit == '/min' or \
        unit == 'per_second' or \
        unit == '/s' or \
        unit == 'per_hour' or \
        unit == '/h' or \
        unit == None:
        return ''
        # Those units are not units... they are impossible to fit anywhere in Pint

    for pint_value, haystack_value in PINT_CONVERSION:
        unit = unit.replace(pint_value, haystack_value)
    for haystack_value, pint_value in HAYSTACK_CONVERSION:
        if pint_value == '':
            continue
        unit = unit.replace(pint_value, haystack_value)
    return unit


Instruction:
Write a Python 3 script for
converting a given unit to a pint unit.

Response:
def to_pint(unit):
    """
    Some parsing tweaks to fit pint units / handling of edge cases.
    """
    global HAYSTACK_CONVERSION
    if unit == 'per_minute' or \
        unit == '/min' or \
        unit == 'per_second' or \
        unit == '/s' or \
        unit == 'per_hour' or \
        unit == '/h' or \
        unit == None:
        return ''
        # Those units are not units... they are impossible to fit anywhere in Pint
    for haystack_value, pint_value in HAYSTACK_CONVERSION:
        unit = unit.replace(haystack_value, pint_value)
    return unit


Instruction:
How would you implement a function in Python 3 that
defines the units that are missing in project - haystack.

Response:
def define_haystack_units():
    """
    Missing units found in project-haystack
    Added to the registry
    """
    ureg = UnitRegistry()
    ureg.define('% = [] = percent')
    ureg.define('pixel = [] = px = dot = picture_element = pel')
    ureg.define('decibel = [] = dB')
    ureg.define('ppu = [] = parts_per_unit')
    ureg.define('ppm = [] = parts_per_million')
    ureg.define('ppb = [] = parts_per_billion')
    ureg.define('%RH = [] = percent_relative_humidity = percentRH')
    ureg.define('cubic_feet = ft ** 3 = cu_ft')
    ureg.define('cfm = cu_ft * minute = liter_per_second / 0.4719475')
    ureg.define('cfh = cu_ft * hour')
    ureg.define('cfs = cu_ft * second')
    ureg.define('VAR = volt * ampere')
    ureg.define('kVAR = 1000 * volt * ampere')
    ureg.define('MVAR = 1000000 * volt * ampere')
    ureg.define('inH2O = in_H2O')
    ureg.define('dry_air = []')
    ureg.define('gas = []')
    ureg.define('energy_efficiency_ratio = [] = EER')
    ureg.define('coefficient_of_performance = [] = COP')
    ureg.define('data_center_infrastructure_efficiency = [] = DCIE')
    ureg.define('power_usage_effectiveness = [] = PUE')
    ureg.define('formazin_nephelometric_unit = [] = fnu')
    ureg.define('nephelometric_turbidity_units = [] = ntu')
    ureg.define('power_factor = [] = PF')
    ureg.define('degree_day_celsius = [] = degdaysC')
    ureg.define('degree_day_farenheit = degree_day_celsius * 9 / 5 = degdaysF')
    ureg.define('footcandle = lumen / sq_ft = ftcd')
    ureg.define('Nm = newton * meter')
    ureg.define('%obsc = [] = percent_obscuration = percentobsc')
    ureg.define('cycle = []')
    ureg.define('cph = cycle / hour')
    ureg.define('cpm = cycle / minute')
    ureg.define('cps = cycle / second')
    ureg.define('hecto_cubic_foot = 100 * cubic_foot')
    ureg.define('tenths_second = second / 10')
    ureg.define('hundredths_second = second / 100')

    #ureg.define('irradiance = W / sq_meter = irr')
    # In the definition of project haystack, there's a redundancy as irr = W/m^2
    # no need to use : watts_per_square_meter_irradiance

    # CURRENCY
    # I know...we won'T be able to convert right now !
    ureg.define('australian_dollar = [] = AUD')
    ureg.define('british_pound = [] = GBP = £')
    ureg.define('canadian_dollar = [] = CAD')
    ureg.define('chinese_yuan = [] = CNY = 元')
    ureg.define('emerati_dirham = [] = AED')
    ureg.define('euro = [] = EUR = €')
    ureg.define('indian_rupee = [] = INR = ₹')
    ureg.define('japanese_yen = [] = JPY = ¥')
    ureg.define('russian_ruble = [] = RUB = руб')
    ureg.define('south_korean_won = [] = KRW = ₩')
    ureg.define('swedish_krona = [] = SEK = kr')
    ureg.define('swiss_franc = [] = CHF = Fr')
    ureg.define('taiwan_dollar = [] = TWD')
    ureg.define('us_dollar = [] = USD = $')
    ureg.define('new_israeli_shekel = [] = NIS')

    return ureg


Instruction:
Implement a Python 3 function for
detecting the version used from the row content or validate against the version if given.

Response:
def _detect_or_validate(self, val):
        '''
        Detect the version used from the row content, or validate against
        the version if given.
        '''
        if isinstance(val, list) \
                or isinstance(val, dict) \
                or isinstance(val, SortableDict) \
                or isinstance(val, Grid):
            # Project Haystack 3.0 type.
            self._assert_version(VER_3_0)


Instruction:
Implement a function in Python 3 to
assert that the grid version is equal to or above the given value.

Response:
def _assert_version(self, version):
        '''
        Assert that the grid version is equal to or above the given value.
        If no version is set, set the version.
        '''
        if self.nearest_version < version:
            if self._version_given:
                raise ValueError(
                        'Data type requires version %s' \
                        % version)
            else:
                self._version = version


Instruction:
Here you have a function in Python 3, explain what it does
def _cmp(self, other):
        if not isinstance(other, Version):
            other = Version(other)

        num1 = self.version_nums
        num2 = other.version_nums

        # Pad both to be the same length
        ver_len = max(len(num1), len(num2))
        num1 += tuple([0 for n in range(len(num1), ver_len)])
        num2 += tuple([0 for n in range(len(num2), ver_len)])

        # Compare the versions
        for (p1, p2) in zip(num1, num2):
            if p1 < p2:
                return -1
            elif p1 > p2:
                return 1

        # All the same, compare the extra strings.
        # If a version misses the extra part; we consider that as coming *before*.
        if self.version_extra is None:
            if other.version_extra is None:
                return 0
            else:
                return -1
        elif other.version_extra is None:
            return 1
        elif self.version_extra == other.version_extra:
            return 0
        elif self.version_extra < other.version_extra:
            return -1
        else:
            return 1

Response: Compares two Project Haystack version strings and returns a value of 0 if self < other 0 if self > other