Instruction:
Explain what the following Python 3 code does
def _get_key_alias_from_cache(self, key_arn):
        '''
        Find a key's alias by looking up its key_arn in the KEY_METADATA
        cache. This function will only work after a key has been lookedup by
        its alias and is meant as a convenience function for turning an ARN
        that's already been looked up back into its alias.
        '''
        for alias in self.KEY_METADATA:
            if self.KEY_METADATA[alias]['KeyMetadata']['Arn'] == key_arn:
                return alias
        return None

Response: Find a key s alias by looking up its key_arn in the KEY_METADATA cache.


Instruction:
Write a Python 3 function for
getting a username formatted for a specific token version.

Response:
def get_username(self):
        """Get a username formatted for a specific token version."""
        _from = self.auth_context['from']
        if self.token_version == 1:
            return '{0}'.format(_from)
        elif self.token_version == 2:
            _user_type = self.auth_context['user_type']
            return '{0}/{1}/{2}'.format(
                self.token_version,
                _user_type,
                _from
            )


Instruction:
Write a Python 3 function that can
get an authentication token.

Response:
def get_token(self):
        """Get an authentication token."""
        # Generate string formatted timestamps for not_before and not_after,
        # for the lifetime specified in minutes.
        now = datetime.datetime.utcnow()
        # Start the not_before time x minutes in the past, to avoid clock skew
        # issues.
        _not_before = now - datetime.timedelta(minutes=TOKEN_SKEW)
        not_before = _not_before.strftime(TIME_FORMAT)
        # Set the not_after time in the future, by the lifetime, but ensure the
        # skew we applied to not_before is taken into account.
        _not_after = now + datetime.timedelta(
            minutes=self.token_lifetime - TOKEN_SKEW
        )
        not_after = _not_after.strftime(TIME_FORMAT)
        # Generate a json string for the encryption payload contents.
        payload = json.dumps({
            'not_before': not_before,
            'not_after': not_after
        })
        token = self._get_cached_token()
        if token:
            return token
        # Generate a base64 encoded KMS encrypted token to use for
        # authentication. We encrypt the token lifetime information as the
        # payload for verification in Confidant.
        try:
            token = self.kms_client.encrypt(
                KeyId=self.auth_key,
                Plaintext=payload,
                EncryptionContext=self.auth_context
            )['CiphertextBlob']
            token = base64.b64encode(ensure_bytes(token))
        except (ConnectionError, EndpointConnectionError) as e:
            logging.exception('Failure connecting to AWS: {}'.format(str(e)))
            raise ServiceConnectionError()
        except Exception:
            logging.exception('Failed to create auth token.')
            raise TokenGenerationError()
        self._cache_token(token, not_after)
        return token


Instruction:
Write a Python 3 function that can
parse a key = value string as a tuple

Response:
def dictstr(arg):
    """
    Parse a key=value string as a tuple (key, value) that can be provided as an argument to dict()
    """
    key, value = arg.split("=")

    if value.lower() == "true" or value.lower() == "false":
        value = bool(value)
    elif INT_RE.match(value):
        value = int(value)
    elif FLOAT_RE.match(value):
        value = float(value)
    return (key, value)


Instruction:
How would you explain what the following Python 3 function does
def regex_matches_to_indexed_words(matches):
    words = [dict(v, **dict([('sentence', i)]))
             for i, s in enumerate(matches['sentences'])
             for k, v in s.items() if k != 'length']
    return words

Response: Transforms tokensregex and semgrex matches to indexed words.


Instruction:
Create a Python 3 function for
sending a request to CoreNLP server.

Response:
def _request(self, buf, properties, date=None):
        """Send a request to the CoreNLP server.

        :param (str | unicode) text: raw text for the CoreNLPServer to parse
        :param (dict) properties: properties that the server expects
        :param (str) date: reference date of document, used by server to set docDate - expects YYYY-MM-DD
        :return: request result
        """
        self.ensure_alive()

        try:
            input_format = properties.get("inputFormat", "text")
            if input_format == "text":
                ctype = "text/plain; charset=utf-8"
            elif input_format == "serialized":
                ctype = "application/x-protobuf"
            else:
                raise ValueError("Unrecognized inputFormat " + input_format)

            if date:
                params = {'properties': str(properties),'date': str(date)}
            else:
                params = {'properties': str(properties)}

            r = requests.post(self.endpoint,
                              params=params,
                              data=buf, headers={'content-type': ctype},
                              timeout=(self.timeout*2)/1000)
            r.raise_for_status()
            return r
        except requests.HTTPError as e:
            if r.text == "CoreNLP request timed out. Your document may be too long.":
                raise TimeoutException(r.text)
            else:
                raise AnnotationException(r.text)


Instruction:
Given the following Python 3 function, write the documentation
def annotate(self, text, annotators=None, output_format=None, properties=None, date=None):
        # set properties for server call
        if properties is None:
            properties = self.default_properties
            properties.update({
                'annotators': ','.join(annotators or self.default_annotators),
                'inputFormat': 'text',
                'outputFormat': self.default_output_format,
                'serializer': 'edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer'
            })
        elif "annotators" not in properties:
            properties.update({'annotators': ','.join(annotators or self.default_annotators)})
        # if an output_format is specified, use that to override
        if output_format is not None:
            properties["outputFormat"] = output_format
        # make the request
        r = self._request(text.encode('utf-8'), properties, date)
        # customize what is returned based outputFormat
        if properties["outputFormat"] == "serialized":
            doc = Document()
            parseFromDelimitedString(doc, r.content)
            return doc
        elif properties["outputFormat"] == "json":
            return r.json()
        elif properties["outputFormat"] in ["text", "conllu", "conll", "xml"]:
            return r.text
        else:
            return r

Response: Send a request to the CoreNLP server to parse the raw text.


Instruction:
Here you have a function in Python 3, explain what it does
def __regex(self, path, text, pattern, filter, annotators=None, properties=None):
        self.ensure_alive()
        if properties is None:
            properties = self.default_properties
            properties.update({
                'annotators': ','.join(annotators or self.default_annotators),
                'inputFormat': 'text',
                'outputFormat': self.default_output_format,
                'serializer': 'edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer'
            })
        elif "annotators" not in properties:
            properties.update({'annotators': ','.join(annotators or self.default_annotators)})

        # HACK: For some stupid reason, CoreNLPServer will timeout if we
        # need to annotate something from scratch. So, we need to call
        # this to ensure that the _regex call doesn't timeout.
        self.annotate(text, properties=properties)

        try:
            # Error occurs unless put properties in params
            input_format = properties.get("inputFormat", "text")
            if input_format == "text":
                ctype = "text/plain; charset=utf-8"
            elif input_format == "serialized":
                ctype = "application/x-protobuf"
            else:
                raise ValueError("Unrecognized inputFormat " + input_format)
            # change request method from `get` to `post` as required by CoreNLP
            r = requests.post(
                self.endpoint + path, params={
                    'pattern': pattern,
                    'filter': filter,
                    'properties': str(properties)
                }, data=text,
                    headers={'content-type': ctype},
                    timeout=(self.timeout*2)/1000,
                    )
            r.raise_for_status()
            return json.loads(r.text)
        except requests.HTTPError as e:
            if r.text.startswith("Timeout"):
                raise TimeoutException(r.text)
            else:
                raise AnnotationException(r.text)
        except json.JSONDecodeError:
            raise AnnotationException(r.text)

Response: Send a regex - related request to CoreNLP server.


Instruction:
Can you implement a function in Python 3 that
returns a dictionary of properties that define this anntoator.

Response:
def properties(self):
        """
        Defines a Java property to define this anntoator to CoreNLP.
        """
        return {
            "customAnnotatorClass.{}".format(self.name): "edu.stanford.nlp.pipeline.GenericWebServiceAnnotator",
            "generic.endpoint": "http://{}:{}".format(self.host, self.port),
            "generic.requires": ",".join(self.requires),
            "generic.provides": ",".join(self.provides),
            }


Instruction:
How would you implement a function in Python 3 that
runs the server using Python s simple HTTPServer.

Response:
def run(self):
        """
        Runs the server using Python's simple HTTPServer.
        TODO: make this multithreaded.
        """
        httpd = HTTPServer((self.host, self.port), self._Handler)
        sa = httpd.socket.getsockname()
        serve_message = "Serving HTTP on {host} port {port} (http://{host}:{port}/) ..."
        print(serve_message.format(host=sa[0], port=sa[1]))
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            print("\nKeyboard interrupt received, exiting.")
            httpd.shutdown()


Instruction:
Create a Python 3 function for
compiling or format the wildcard inclusion or exclusion pattern.

Response:
def _compile_wildcard(self, pattern, pathname=False):
        """Compile or format the wildcard inclusion/exclusion pattern."""

        patterns = None
        flags = self.flags
        if pathname:
            flags |= _wcparse.PATHNAME
        if pattern:
            patterns = _wcparse.WcSplit(pattern, flags=flags).split()

        return _wcparse.compile(patterns, flags) if patterns else patterns


Instruction:
Write a Python 3 script to
return whether a file can be searched.

Response:
def _valid_file(self, base, name):
        """Return whether a file can be searched."""

        valid = False
        fullpath = os.path.join(base, name)
        if self.file_check is not None and self.compare_file(fullpath[self._base_len:] if self.file_pathname else name):
            valid = True
        if valid and (not self.show_hidden and util.is_hidden(fullpath)):
            valid = False
        return self.on_validate_file(base, name) if valid else valid


Instruction:
How would you explain what the following Python 3 function does
def _valid_folder(self, base, name):

        valid = True
        fullpath = os.path.join(base, name)
        if (
            not self.recursive or
            (
                self.folder_exclude_check is not None and
                not self.compare_directory(fullpath[self._base_len:] if self.dir_pathname else name)
            )
        ):
            valid = False
        if valid and (not self.show_hidden and util.is_hidden(fullpath)):
            valid = False
        return self.on_validate_directory(base, name) if valid else valid

Response: Return whether a folder can be searched.


Instruction:
Write a Python 3 script to
start the walking function.

Response:
def _walk(self):
        """Start search for valid files."""

        self._base_len = len(self.base)

        for base, dirs, files in os.walk(self.base, followlinks=self.follow_links):
            # Remove child folders based on exclude rules
            for name in dirs[:]:
                try:
                    if not self._valid_folder(base, name):
                        dirs.remove(name)
                except Exception:
                    dirs.remove(name)
                    value = self.on_error(base, name)
                    if value is not None:  # pragma: no cover
                        yield value

                if self._abort:
                    break

            # Search files if they were found
            if len(files):
                # Only search files that are in the include rules
                for name in files:
                    try:
                        valid = self._valid_file(base, name)
                    except Exception:
                        valid = False
                        value = self.on_error(base, name)
                        if value is not None:
                            yield value

                    if valid:
                        yield self.on_match(base, name)
                    else:
                        self._skipped += 1
                        value = self.on_skip(base, name)
                        if value is not None:
                            yield value

                    if self._abort:
                        break

            if self._abort:
                break


Instruction:
Here you have a function in Python 3, explain what it does
def is_negative(pattern, flags):

    if flags & MINUSNEGATE:
        return flags & NEGATE and pattern[0:1] in MINUS_NEGATIVE_SYM
    else:
        return flags & NEGATE and pattern[0:1] in NEGATIVE_SYM

Response: Check if negative pattern.


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

    if not bool(flags & CASE_FLAGS):
        case_sensitive = util.is_case_sensitive()
    elif flags & FORCECASE:
        case_sensitive = True
    else:
        case_sensitive = False
    return case_sensitive

Response: Parse flags for case sensitivity settings.


Instruction:
Can you implement a function in Python 3 that
checks if we should use Unix style.

Response:
def is_unix_style(flags):
    """Check if we should use Unix style."""

    return (util.platform() != "windows" or (not bool(flags & REALPATH) and get_case(flags))) and not flags & _FORCEWIN


Instruction:
Can you implement a function in Python 3 that
compiles the pattern to regex.

Response:
def _compile(pattern, flags):
    """Compile the pattern to regex."""

    return re.compile(WcParse(pattern, flags & FLAG_MASK).parse())


Instruction:
Create a Python 3 function to
match a pattern against a file.

Response:
def _fs_match(pattern, filename, sep, follow, symlinks):
    """
    Match path against the pattern.

    Since `globstar` doesn't match symlinks (unless `FOLLOW` is enabled), we must look for symlinks.
    If we identify a symlink in a `globstar` match, we know this result should not actually match.
    """

    matched = False

    base = None
    m = pattern.fullmatch(filename)
    if m:
        matched = True
        # Lets look at the captured `globstar` groups and see if that part of the path
        # contains symlinks.
        if not follow:
            groups = m.groups()
            last = len(groups)
            for i, star in enumerate(m.groups(), 1):
                if star:
                    parts = star.strip(sep).split(sep)
                    if base is None:
                        base = filename[:m.start(i)]
                    for part in parts:
                        base = os.path.join(base, part)
                        is_link = symlinks.get(base, None)
                        if is_link is not None:
                            matched = not is_link
                        elif i != last or os.path.isdir(base):
                            is_link = os.path.islink(base)
                            symlinks[base] = is_link
                            matched = not is_link
                        if not matched:
                            break
                if matched:
                    break
    return matched


Instruction:
How would you implement a function in Python 3 that
matches real filename includes and excludes.

Response:
def _match_real(filename, include, exclude, follow, symlinks):
    """Match real filename includes and excludes."""

    sep = '\\' if util.platform() == "windows" else '/'
    if isinstance(filename, bytes):
        sep = os.fsencode(sep)
    if not filename.endswith(sep) and os.path.isdir(filename):
        filename += sep

    matched = False
    for pattern in include:
        if _fs_match(pattern, filename, sep, follow, symlinks):
            matched = True
            break

    if matched:
        matched = True
        if exclude:
            for pattern in exclude:
                if _fs_match(pattern, filename, sep, follow, symlinks):
                    matched = False
                    break
    return matched


Instruction:
Here you have a function in Python 3, explain what it does
def _match_pattern(filename, include, exclude, real, path, follow):

    if real:
        symlinks = {}
        if isinstance(filename, bytes):
            curdir = os.fsencode(os.curdir)
            mount = RE_BWIN_MOUNT if util.platform() == "windows" else RE_BMOUNT
        else:
            curdir = os.curdir
            mount = RE_WIN_MOUNT if util.platform() == "windows" else RE_MOUNT

        if not mount.match(filename):
            exists = os.path.lexists(os.path.join(curdir, filename))
        else:
            exists = os.path.lexists(filename)

        if not exists:
            return False
        if path:
            return _match_real(filename, include, exclude, follow, symlinks)

    matched = False
    for pattern in include:
        if pattern.fullmatch(filename):
            matched = True
            break

    if not include and exclude:
        matched = True

    if matched:
        matched = True
        if exclude:
            for pattern in exclude:
                if not pattern.fullmatch(filename):
                    matched = False
                    break
    return matched

Response: Match includes and excludes.


Instruction:
Write a Python 3 script to
parse extended pattern lists.

Response:
def parse_extend(self, c, i):
        """Parse extended pattern lists."""

        # Start list parsing
        success = True
        index = i.index
        list_type = c
        try:
            c = next(i)
            if c != '(':
                raise StopIteration
            while c != ')':
                c = next(i)

                if self.extend and c in EXT_TYPES and self.parse_extend(c, i):
                    continue

                if c == '\\':
                    try:
                        self._references(i)
                    except StopIteration:
                        pass
                elif c == '[':
                    index = i.index
                    try:
                        self._sequence(i)
                    except StopIteration:
                        i.rewind(i.index - index)

        except StopIteration:
            success = False
            c = list_type
            i.rewind(i.index - index)

        return success


Instruction:
Explain what the following Python 3 code does
def store(self, value, l, dir_only):

        if l and value in (b'', ''):
            return

        globstar = value in (b'**', '**') and self.globstar
        magic = self.is_magic(value)
        if magic:
            value = compile(value, self.flags)
        l.append(WcGlob(value, magic, globstar, dir_only, False))

Response: Group patterns by literals and potential magic patterns.


Instruction:
Make a summary of the following Python 3 code
def split(self):

        split_index = []
        parts = []
        start = -1

        pattern = self.pattern.decode('latin-1') if self.is_bytes else self.pattern

        i = util.StringIter(pattern)
        iter(i)

        # Detect and store away windows drive as a literal
        if self.win_drive_detect:
            m = RE_WIN_PATH.match(pattern)
            if m:
                drive = m.group(0).replace('\\\\', '\\')
                if self.is_bytes:
                    drive = drive.encode('latin-1')
                parts.append(WcGlob(drive, False, False, True, True))
                start = m.end(0) - 1
                i.advance(start + 1)
            elif pattern.startswith('\\\\'):
                parts.append(WcGlob(b'\\' if self.is_bytes else '\\', False, False, True, True))
                start = 1
                i.advance(2)
        elif not self.win_drive_detect and pattern.startswith('/'):
            parts.append(WcGlob(b'/' if self.is_bytes else '/', False, False, True, True))
            start = 0
            i.advance(1)

        for c in i:
            if self.extend and c in EXT_TYPES and self.parse_extend(c, i):
                continue

            if c == '\\':
                index = i.index
                value = ''
                try:
                    value = self._references(i)
                    if self.bslash_abort and value == '\\':
                        split_index.append((i.index - 2, 1))
                except StopIteration:
                    i.rewind(i.index - index)
                    if self.bslash_abort:
                        split_index.append((i.index - 1, 0))
            elif c == '/':
                split_index.append((i.index - 1, 0))
            elif c == '[':
                index = i.index
                try:
                    self._sequence(i)
                except StopIteration:
                    i.rewind(i.index - index)

        for split, offset in split_index:
            if self.is_bytes:
                value = pattern[start + 1:split].encode('latin-1')
            else:
                value = pattern[start + 1:split]
            self.store(value, parts, True)
            start = split + offset

        if start < len(pattern):
            if self.is_bytes:
                value = pattern[start + 1:].encode('latin-1')
            else:
                value = pattern[start + 1:]
            if value:
                self.store(value, parts, False)
        if len(pattern) == 0:
            parts.append(WcGlob(pattern.encode('latin-1') if self.is_bytes else pattern, False, False, False, False))

        return parts

Response: Start parsing the pattern and return a list of the parts that are part of the pattern.


Instruction:
How would you implement a function in Python 3 that
starts parsing the pattern.

Response:
def split(self):
        """Start parsing the pattern."""

        split_index = []
        parts = []

        pattern = self.pattern.decode('latin-1') if self.is_bytes else self.pattern

        i = util.StringIter(pattern)
        iter(i)
        for c in i:
            if self.extend and c in EXT_TYPES and self.parse_extend(c, i):
                continue

            if c == '|':
                split_index.append(i.index - 1)
            elif c == '\\':
                index = i.index
                try:
                    self._references(i)
                except StopIteration:
                    i.rewind(i.index - index)
            elif c == '[':
                index = i.index
                try:
                    self._sequence(i)
                except StopIteration:
                    i.rewind(i.index - index)

        start = -1
        for split in split_index:
            p = pattern[start + 1:split]
            parts.append(p.encode('latin-1') if self.is_bytes else p)
            start = split

        if start < len(pattern):
            p = pattern[start + 1:]
            parts.append(p.encode('latin-1') if self.is_bytes else p)

        return tuple(parts)


Instruction:
Here you have a function in Python 3, explain what it does
def update_dir_state(self):

        if self.dir_start and not self.after_start:
            self.set_after_start()
        elif not self.dir_start and self.after_start:
            self.reset_dir_track()

Response: Update the directory state.


Instruction:
Write a Python 3 script for
checking if the sequence is in the range of the last entry in the result list.

Response:
def _sequence_range_check(self, result, last):
        """
        If range backwards, remove it.

        A bad range will cause the regular expression to fail,
        so we need to remove it, but return that we removed it
        so the caller can know the sequence wasn't empty.
        Caller will have to craft a sequence that makes sense
        if empty at the end with either an impossible sequence
        for inclusive sequences or a sequence that matches
        everything for an exclusive sequence.
        """

        removed = False
        first = result[-2]
        v1 = ord(first[1:2] if len(first) > 1 else first)
        v2 = ord(last[1:2] if len(last) > 1 else last)
        if v2 < v1:
            result.pop()
            result.pop()
            removed = True
        else:
            result.append(last)
        return removed


Instruction:
Can you generate the documentation for the following Python 3 function
def clean_up_inverse(self, current):

        if not self.inv_ext:
            return

        index = len(current) - 1
        while index >= 0:
            if isinstance(current[index], InvPlaceholder):
                content = current[index + 1:]
                content.append(_EOP if not self.pathname else self.path_eop)
                current[index] = (''.join(content)) + (_EXCLA_GROUP_CLOSE % str(current[index]))
            index -= 1
        self.inv_ext = 0

Response: Clean up the inverse of the current.


Instruction:
How would you explain what the following Python 3 function does
def parse_extend(self, c, i, current, reset_dot=False):

        # Save state
        temp_dir_start = self.dir_start
        temp_after_start = self.after_start
        temp_in_list = self.in_list
        temp_inv_ext = self.inv_ext
        self.in_list = True
        if reset_dot:
            self.allow_special_dir = False

        # Start list parsing
        success = True
        index = i.index
        list_type = c
        extended = []
        try:
            c = next(i)
            if c != '(':
                raise StopIteration
            while c != ')':
                c = next(i)

                if self.extend and c in EXT_TYPES and self.parse_extend(c, i, extended):
                    # Nothing more to do
                    pass
                elif c == '*':
                    self._handle_star(i, extended)
                elif c == '.' and self.after_start:
                    extended.append(re.escape(c))
                    self.allow_special_dir = True
                    self.reset_dir_track()
                elif c == '?':
                    extended.append(self._restrict_sequence() + _QMARK)
                elif c == '/':
                    if self.pathname:
                        extended.append(self._restrict_extended_slash())
                    extended.append(re.escape(c))
                elif c == "|":
                    self.clean_up_inverse(extended)
                    extended.append(c)
                    if temp_after_start:
                        self.set_start_dir()
                elif c == '\\':
                    try:
                        extended.append(self._references(i))
                    except StopIteration:
                        # We've reached the end.
                        # Do nothing because this is going to abort the `extmatch` anyways.
                        pass
                elif c == '[':
                    subindex = i.index
                    try:
                        extended.append(self._sequence(i))
                    except StopIteration:
                        i.rewind(i.index - subindex)
                        extended.append(r'\[')
                elif c != ')':
                    extended.append(re.escape(c))

                self.update_dir_state()

            self.clean_up_inverse(extended)
            if list_type == '?':
                current.append(_QMARK_GROUP % ''.join(extended))
            elif list_type == '*':
                current.append(_STAR_GROUP % ''.join(extended))
            elif list_type == '+':
                current.append(_PLUS_GROUP % ''.join(extended))
            elif list_type == '@':
                current.append(_GROUP % ''.join(extended))
            elif list_type == '!':
                self.inv_ext += 1
                # If pattern is at the end, anchor the match to the end.
                current.append(_EXCLA_GROUP % ''.join(extended))
                if self.pathname:
                    if not temp_after_start or self.allow_special_dir:
                        star = self.path_star
                    elif temp_after_start and not self.dot:
                        star = self.path_star_dot2
                    else:
                        star = self.path_star_dot1
                else:
                    if not temp_after_start or self.dot:
                        star = _STAR
                    else:
                        star = _NO_DOT + _STAR

                if temp_after_start:
                    star = _NEED_CHAR + star
                # Place holder for closing, but store the proper star
                # so we know which one to use
                current.append(InvPlaceholder(star))

        except StopIteration:
            success = False
            self.inv_ext = temp_inv_ext
            i.rewind(i.index - index)

        # Either restore if extend parsing failed, or reset if it worked
        if not temp_in_list:
            self.in_list = False
        if success:
            self.reset_dir_track()
        else:
            self.dir_start = temp_dir_start
            self.after_start = temp_after_start

        return success

Response: Parse extended pattern lists.


Instruction:
Can you generate the documentation for the following Python 3 function
def consume_path_sep(self, i):

        try:
            if self.bslash_abort:
                count = -1
                c = '\\'
                while c == '\\':
                    count += 1
                    c = next(i)
                i.rewind(1)
                # Rewind one more if we have an odd number (escape): \\\*
                if count > 0 and count % 2:
                    i.rewind(1)
            else:
                c = '/'
                while c == '/':
                    c = next(i)
                i.rewind(1)
        except StopIteration:
            pass

Response: Consume any consecutive path separators are they count as one.


Instruction:
How would you implement a function in Python 3 that
starts parsing the pattern.

Response:
def root(self, pattern, current):
        """Start parsing the pattern."""

        self.set_after_start()
        i = util.StringIter(pattern)
        iter(i)
        root_specified = False
        if self.win_drive_detect:
            m = RE_WIN_PATH.match(pattern)
            if m:
                drive = m.group(0).replace('\\\\', '\\')
                if drive.endswith('\\'):
                    slash = True
                drive = drive[:-1]
                current.append(re.escape(drive))
                if slash:
                    current.append(self.get_path_sep() + _ONE_OR_MORE)
                i.advance(m.end(0))
                self.consume_path_sep(i)
                root_specified = True
            elif pattern.startswith('\\\\'):
                root_specified = True
        elif not self.win_drive_detect and self.pathname and pattern.startswith('/'):
            root_specified = True

        if not root_specified and self.realpath:
            current.append(_NO_WIN_ROOT if self.win_drive_detect else _NO_ROOT)
            current.append('')

        for c in i:

            index = i.index
            if self.extend and c in EXT_TYPES and self.parse_extend(c, i, current, True):
                # Nothing to do
                pass
            elif c == '*':
                self._handle_star(i, current)
            elif c == '?':
                current.append(self._restrict_sequence() + _QMARK)
            elif c == '/':
                if self.pathname:
                    self.set_start_dir()
                    self.clean_up_inverse(current)
                    current.append(self.get_path_sep() + _ONE_OR_MORE)
                    self.consume_path_sep(i)
                else:
                    current.append(re.escape(c))
            elif c == '\\':
                index = i.index
                try:
                    value = self._references(i)
                    if self.dir_start:
                        self.clean_up_inverse(current)
                        self.consume_path_sep(i)
                    current.append(value)
                except StopIteration:
                    i.rewind(i.index - index)
                    current.append(re.escape(c))
            elif c == '[':
                index = i.index
                try:
                    current.append(self._sequence(i))
                except StopIteration:
                    i.rewind(i.index - index)
                    current.append(re.escape(c))
            else:
                current.append(re.escape(c))

            self.update_dir_state()

        self.clean_up_inverse(current)
        if self.pathname:
            current.append(_PATH_TRAIL % self.get_path_sep())


Instruction:
Implement a Python 3 function for
parsing the pattern list.

Response:
def parse(self):
        """Parse pattern list."""

        result = ['']
        negative = False

        p = util.norm_pattern(self.pattern, not self.unix, self.raw_chars)

        p = p.decode('latin-1') if self.is_bytes else p
        if is_negative(p, self.flags):
            negative = True
            p = p[1:]

        self.root(p, result)

        case_flag = 'i' if not self.case_sensitive else ''
        if util.PY36:
            pattern = (
                r'^(?!(?s%s:%s)$).*?$' if negative and not self.globstar_capture else r'^(?s%s:%s)$'
            ) % (case_flag, ''.join(result))
        else:
            pattern = (
                r'(?s%s)^(?!(?:%s)$).*?$' if negative and not self.globstar_capture else r'(?s%s)^(?:%s)$'
            ) % (case_flag, ''.join(result))

        if self.is_bytes:
            pattern = pattern.encode('latin-1')

        return pattern


Instruction:
Can you write a function in Python 3 where it
transforms flags to glob defaults.

Response:
def _flag_transform(flags):
    """Transform flags to glob defaults."""

    # Here we force `PATHNAME`.
    flags = (flags & FLAG_MASK) | _wcparse.PATHNAME
    if flags & _wcparse.REALPATH and util.platform() == "windows":
        flags |= _wcparse._FORCEWIN
        if flags & _wcparse.FORCECASE:
            flags ^= _wcparse.FORCECASE
    return flags


Instruction:
Write a Python 3 function that can
check if filename matches patterns.

Response:
def globmatch(filename, patterns, *, flags=0):
    """
    Check if filename matches pattern.

    By default case sensitivity is determined by the file system,
    but if `case_sensitive` is set, respect that instead.
    """

    flags = _flag_transform(flags)
    if not _wcparse.is_unix_style(flags):
        filename = util.norm_slash(filename)
    return _wcparse.compile(_wcparse.split(patterns, flags), flags).match(filename)


Instruction:
Create a Python 3 function to
filter filenames using pattern.

Response:
def globfilter(filenames, patterns, *, flags=0):
    """Filter names using pattern."""

    matches = []

    flags = _flag_transform(flags)
    unix = _wcparse.is_unix_style(flags)
    obj = _wcparse.compile(_wcparse.split(patterns, flags), flags)

    for filename in filenames:
        if not unix:
            filename = util.norm_slash(filename)
        if obj.match(filename):
            matches.append(filename)
    return matches


Instruction:
Create a Python 3 function for
applying raw character transform before applying escape.

Response:
def raw_escape(pattern, unix=False):
    """Apply raw character transform before applying escape."""

    pattern = util.norm_pattern(pattern, False, True)
    return escape(pattern, unix)


Instruction:
Here you have a function in Python 3, explain what it does
def _match_excluded(self, filename, patterns):

        return _wcparse._match_real(
            filename, patterns._include, patterns._exclude, patterns._follow, self.symlinks
        )

Response: Call match real directly to skip unnecessary exists check.


Instruction:
Given the following Python 3 function, write the documentation
def _is_excluded(self, path, dir_only):

        return self.npatterns and self._match_excluded(path, self.npatterns)

Response: Check if file is excluded.


Instruction:
Make a summary of the following Python 3 code
def _glob_dir(self, curdir, matcher, dir_only=False, deep=False):

        scandir = self.current if not curdir else curdir

        # Python will never return . or .., so fake it.
        if os.path.isdir(scandir) and matcher is not None:
            for special in self.specials:
                if matcher(special):
                    yield os.path.join(curdir, special)

        try:
            if NO_SCANDIR_WORKAROUND:
                # Our current directory can be empty if the path starts with magic,
                # But we don't want to return paths with '.', so just use it to list
                # files, but use '' when constructing the path.
                with os.scandir(scandir) as scan:
                    for f in scan:
                        try:
                            # Quicker to just test this way than to run through `fnmatch`.
                            if deep and self._is_hidden(f.name):
                                continue
                            path = os.path.join(curdir, f.name)
                            is_dir = f.is_dir()
                            if is_dir:
                                is_link = f.is_symlink()
                                self.symlinks[path] = is_link
                            else:
                                # We don't care if a file is a link
                                is_link = False
                            if deep and not self.follow_links and is_link:
                                continue
                            if (not dir_only or is_dir) and (matcher is None or matcher(f.name)):
                                yield path
                            if deep and is_dir:
                                yield from self._glob_dir(path, matcher, dir_only, deep)
                        except OSError:  # pragma: no cover
                            pass
            else:
                for f in os.listdir(scandir):
                    # Quicker to just test this way than to run through `fnmatch`.
                    if deep and self._is_hidden(f):
                        continue
                    path = os.path.join(curdir, f)
                    is_dir = os.path.isdir(path)
                    if is_dir:
                        is_link = os.path.islink(path)
                        self.symlinks[path] = is_link
                    else:
                        is_link = False
                    if deep and not self.follow_links and is_link:
                        continue
                    if (not dir_only or is_dir) and (matcher is None or matcher(f)):
                        yield path
                    if deep and is_dir:
                        yield from self._glob_dir(path, matcher, dir_only, deep)
        except OSError:  # pragma: no cover
            pass

Response: Non recursive directory glob.


Instruction:
Write a Python 3 script to
handle glob flow. There are really only a couple of cases: - File name. - File name pattern (magic). - Directory. - Directory name pattern (magic). - Extra slashes `////`. - `globstar` `**`.

Response:
def _glob(self, curdir, this, rest):
        """
        Handle glob flow.

        There are really only a couple of cases:

        - File name.
        - File name pattern (magic).
        - Directory.
        - Directory name pattern (magic).
        - Extra slashes `////`.
        - `globstar` `**`.
        """

        is_magic = this.is_magic
        dir_only = this.dir_only
        target = this.pattern
        is_globstar = this.is_globstar

        if is_magic and is_globstar:
            # Glob star directory `**`.

            # Throw away multiple consecutive `globstars`
            # and acquire the pattern after the `globstars` if available.
            this = rest.pop(0) if rest else None
            globstar_end = this is None
            while this and not globstar_end:
                if this:
                    dir_only = this.dir_only
                    target = this.pattern
                if this and this.is_globstar:
                    this = rest.pop(0) if rest else None
                    if this is None:
                        globstar_end = True
                else:
                    break

            if globstar_end:
                target = None

            # We match `**/next` during a deep glob, so what ever comes back,
            # we will send back through `_glob` with pattern after `next` (`**/next/after`).
            # So grab `after` if available.
            this = rest.pop(0) if rest else None

            # Deep searching is the unique case where we
            # might feed in a `None` for the next pattern to match.
            # Deep glob will account for this.
            matcher = self._get_matcher(target)

            # If our pattern ends with `curdir/**`, but does not start with `**` it matches zero or more,
            # so it should return `curdir/`, signifying `curdir` + no match.
            # If a pattern follows `**/something`, we always get the appropriate
            # return already, so this isn't needed in that case.
            # There is one quirk though with Bash, if `curdir` had magic before `**`, Bash
            # omits the trailing `/`. We don't worry about that.
            if globstar_end and curdir:
                yield os.path.join(curdir, self.empty)

            # Search
            for path in self._glob_dir(curdir, matcher, dir_only, deep=True):
                if this:
                    yield from self._glob(path, this, rest[:])
                else:
                    yield path

        elif not dir_only:
            # Files: no need to recursively search at this point as we are done.
            matcher = self._get_matcher(target)
            yield from self._glob_dir(curdir, matcher)

        else:
            # Directory: search current directory against pattern
            # and feed the results back through with the next pattern.
            this = rest.pop(0) if rest else None
            matcher = self._get_matcher(target)
            for path in self._glob_dir(curdir, matcher, True):
                if this:
                    yield from self._glob(path, this, rest[:])
                else:
                    yield path


Instruction:
Write a Python 3 function for
getting the starting paths of the log files.

Response:
def _get_starting_paths(self, curdir):
        """
        Get the starting location.

        For case sensitive paths, we have to "glob" for
        it first as Python doesn't like for its users to
        think about case. By scanning for it, we can get
        the actual casing and then compare.
        """

        results = [curdir]

        if not self._is_parent(curdir) and not self._is_this(curdir):
            fullpath = os.path.abspath(curdir)
            basename = os.path.basename(fullpath)
            dirname = os.path.dirname(fullpath)
            if basename:
                matcher = self._get_matcher(basename)
                results = [os.path.basename(name) for name in self._glob_dir(dirname, matcher, self)]

        return results


Instruction:
Can you implement a function in Python 3 that
yields the file names and directories that match the pattern.

Response:
def glob(self):
        """Starts off the glob iterator."""

        # Cached symlinks
        self.symlinks = {}

        if self.is_bytes:
            curdir = os.fsencode(os.curdir)
        else:
            curdir = os.curdir

        for pattern in self.pattern:
            # If the pattern ends with `/` we return the files ending with `/`.
            dir_only = pattern[-1].dir_only if pattern else False

            if pattern:
                if not pattern[0].is_magic:
                    # Path starts with normal plain text
                    # Lets verify the case of the starting directory (if possible)
                    this = pattern[0]

                    curdir = this[0]

                    if not os.path.lexists(curdir):
                        return

                    # Make sure case matches, but running case insensitive
                    # on a case sensitive file system may return more than
                    # one starting location.
                    results = [curdir] if this.is_drive else self._get_starting_paths(curdir)
                    if not results:
                        if not dir_only:
                            # There is no directory with this name,
                            # but we have a file and no directory restriction
                            yield curdir
                        return

                    if this.dir_only:
                        # Glob these directories if they exists
                        for start in results:
                            if os.path.isdir(start):
                                rest = pattern[1:]
                                if rest:
                                    this = rest.pop(0)
                                    for match in self._glob(curdir, this, rest):
                                        if not self._is_excluded(match, dir_only):
                                            yield os.path.join(match, self.empty) if dir_only else match
                                elif not self._is_excluded(curdir, dir_only):
                                    yield os.path.join(curdir, self.empty) if dir_only else curdir
                    else:
                        # Return the file(s) and finish.
                        for start in results:
                            if os.path.lexists(start) and not self._is_excluded(start, dir_only):
                                yield os.path.join(start, self.empty) if dir_only else start
                else:
                    # Path starts with a magic pattern, let's get globbing
                    rest = pattern[:]
                    this = rest.pop(0)
                    for match in self._glob(curdir if not curdir == self.current else self.empty, this, rest):
                        if not self._is_excluded(match, dir_only):
                            yield os.path.join(match, self.empty) if dir_only else match


Instruction:
Can you tell what is the following Python 3 function doing
def norm_pattern(pattern, normalize, is_raw_chars):

    is_bytes = isinstance(pattern, bytes)

    if not normalize and not is_raw_chars:
        return pattern

    def norm_char(token):
        """Normalize slash."""

        if normalize and token in ('/', b'/'):
            token = br'\\' if is_bytes else r'\\'
        return token

    def norm(m):
        """Normalize the pattern."""

        if m.group(1):
            char = m.group(1)
            if normalize:
                char = br'\\\\' if is_bytes else r'\\\\' if len(char) > 1 else norm_char(char)
        elif m.group(2):
            char = norm_char(BACK_SLASH_TRANSLATION[m.group(2)] if is_raw_chars else m.group(2))
        elif is_raw_chars and m.group(4):
            char = norm_char(bytes([int(m.group(4), 8) & 0xFF]) if is_bytes else chr(int(m.group(4), 8)))
        elif is_raw_chars and m.group(3):
            char = norm_char(bytes([int(m.group(3)[2:], 16)]) if is_bytes else chr(int(m.group(3)[2:], 16)))
        elif is_raw_chars and not is_bytes and m.group(5):
            char = norm_char(unicodedata.lookup(m.group(5)[3:-1]))
        elif not is_raw_chars:
            char = m.group(0)
        else:
            value = m.group(5) if is_bytes else m.group(6)
            pos = m.start(5) if is_bytes else m.start(6)
            raise SyntaxError("Could not convert character value %s at position %d" % (value, pos))
        return char

    return (RE_BNORM if is_bytes else RE_NORM).sub(norm, pattern)

Response: r Normalize a pattern.


Instruction:
Implement a function in Python 3 to
check if file is hidden.

Response:
def is_hidden(path):
    """Check if file is hidden."""

    hidden = False
    f = os.path.basename(path)
    if f[:1] in ('.', b'.'):
        # Count dot file as hidden on all systems
        hidden = True
    elif _PLATFORM == 'windows':
        # On Windows, look for `FILE_ATTRIBUTE_HIDDEN`
        FILE_ATTRIBUTE_HIDDEN = 0x2
        if PY35:
            results = os.lstat(path)
            hidden = bool(results.st_file_attributes & FILE_ATTRIBUTE_HIDDEN)
        else:
            if isinstance(path, bytes):
                attrs = ctypes.windll.kernel32.GetFileAttributesA(path)
            else:
                attrs = ctypes.windll.kernel32.GetFileAttributesW(path)
            hidden = attrs != -1 and attrs & FILE_ATTRIBUTE_HIDDEN
    elif _PLATFORM == "osx":  # pragma: no cover
        # On macOS, look for `UF_HIDDEN`
        results = os.lstat(path)
        hidden = bool(results.st_flags & stat.UF_HIDDEN)
    return hidden


Instruction:
Make a summary of the following Python 3 code
def match(self, pattern):

        m = pattern.match(self._string, self._index)
        if m:
            self._index = m.end()
        return m

Response: Perform regex match at index.


Instruction:
Implement a Python 3 function for
iterating through characters of the string.

Response:
def iternext(self):
        """Iterate through characters of the string."""

        try:
            char = self._string[self._index]
            self._index += 1
        except IndexError:  # pragma: no cover
            raise StopIteration

        return char


Instruction:
Can you generate a brief explanation for the following Python 3 code
def generate_slug(text, tail_number=0):
    from wagtail.core.models import Page
    """
    Returns a new unique slug. Object must provide a SlugField called slug.
    URL friendly slugs are generated using django.template.defaultfilters'
    slugify. Numbers are added to the end of slugs for uniqueness.

    based on implementation in jmbo.utils
    https://github.com/praekelt/jmbo/blob/develop/jmbo/utils/__init__.py
    """

    # Empty slugs are ugly (eg. '-1' may be generated) so force non-empty
    if not text:
        text = 'no-title'

    # use django slugify filter to slugify
    slug = cautious_slugify(text)[:255]

    values_list = Page.objects.filter(
        slug__startswith=slug
    ).values_list('id', 'slug')

    # Find highest suffix
    max = -1
    for tu in values_list:
        if tu[1] == slug:
            if max == -1:
                # Set max to indicate a collision
                max = 0

        # Update max if suffix is greater
        match = RE_NUMERICAL_SUFFIX.match(tu[1])
        if match is not None:
            i = int(match.group(1))
            if i > max:
                max = i

    if max >= 0:
        # There were collisions
        return "%s-%s" % (slug, max + 1)
    else:
        # No collisions
        return slug

Response: Generate a unique slug for a page.


Instruction:
Make a summary of the following Python 3 code
def update_media_file(upload_file):
    '''
    Update the Current Media Folder.

    Returns list of files copied across or
    raises an exception.
    '''
    temp_directory = tempfile.mkdtemp()
    temp_file = tempfile.TemporaryFile()
    # assumes the zip file contains a directory called media
    temp_media_file = os.path.join(temp_directory, 'media')
    try:
        for chunk in upload_file.chunks():
            temp_file.write(chunk)

        with zipfile.ZipFile(temp_file, 'r') as z:
            z.extractall(temp_directory)

        if os.path.exists(temp_media_file):
            return distutils.dir_util.copy_tree(
                temp_media_file,
                settings.MEDIA_ROOT)
        else:
            raise Exception("Error: There is no directory called "
                            "'media' in the root of the zipped file")
    except Exception as e:
        raise e
    finally:
        temp_file.close()
        if os.path.exists(temp_directory):
            shutil.rmtree(temp_directory)

Response: Update the Current Media Folder.


Instruction:
Implement a function in Python 3 to
return an MD5 hash of the image file

Response:
def get_image_hash(image):
    '''
    Returns an MD5 hash of the image file

    Handles images stored locally and on AWS

    I know this code is ugly.
    Please don't ask.
    The rabbit hole is deep.
    '''
    md5 = hashlib.md5()
    try:
        for chunk in image.file.chunks():
            md5.update(chunk)
        return md5.hexdigest()
    # this should only occur in tests
    except ValueError:
        # see link below for why we try not to use .open()
        # https://docs.djangoproject.com/en/1.9/ref/files/uploads/#django.core.files.uploadedfile.UploadedFile.chunks  # noqa
        image.file.open()

        for chunk in image.file.chunks():
            md5.update(chunk)
        return md5.hexdigest()
    finally:
        image.file.close()


Instruction:
Can you generate the documentation for the following Python 3 function
def separate_fields(fields):
    flat_fields = {}
    nested_fields = {}

    # exclude "id" and "meta" elements
    for k, v in fields.items():
        # TODO: remove dependence on KEYS_TO_INCLUDE
        if k not in KEYS_TO_EXCLUDE:
            if type(v) not in [type({}), type([])]:
                flat_fields.update({k: v})
            else:
                nested_fields.update({k: v})

    return flat_fields, nested_fields

Response: Separate foreign key fields into a single field.


Instruction:
Make a summary of the following Python 3 code
def attach_image(field, nested_fields, page, record_keeper=None):
    '''
    Returns a function that attaches an image to page if it exists

    Currenlty assumes that images have already been imported and info
    has been stored in record_keeper
    '''
    if (field in nested_fields) and nested_fields[field]:
        foreign_image_id = nested_fields[field]["id"]
        # Handle the following
        # record keeper may not exist
        # record keeper may not have image ref
        if record_keeper:
            try:
                local_image_id = record_keeper.get_local_image(
                    foreign_image_id)
                local_image = Image.objects.get(id=local_image_id)
                setattr(page, field, local_image)
            except ObjectDoesNotExist:
                raise ObjectDoesNotExist(
                    ("executing attach_image: local image referenced"
                     "in record_keeper does not actually exist."),
                    None)
            except Exception:
                raise
        else:
            raise Exception(
                ("Attempted to attach image without record_keeper. "
                 "This functionality is not yet implemented"))

Response: Returns a function that attaches an image to a page if it exists and if it does not exist it will raise an exception if the image is already attached.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def rotate_content(day=None):
    # getting the content rotation settings from site settings

    for main in Main.objects.all():
        site = main.sites_rooted_here.all().first()
        main_lang = Languages.for_site(site).languages.filter(
            is_main_language=True).first()
        index = SectionIndexPage.objects.live().child_of(main).first()
        site_settings = SiteSettings.for_site(site)
        if day is None:
            day = timezone.now().weekday()

        # calls the two rotate methods with the necessary params
        if main and index:
            rotate_latest(main_lang, index, main, site_settings, day)
            rotate_featured_in_homepage(main_lang, day, main)

Response: This method rotates all content in the current site to the given day.


Instruction:
Write a Python 3 function that can
load all descendant articles for a given section.

Response:
def load_descendant_articles_for_section(
        context, section, featured_in_homepage=None, featured_in_section=None,
        featured_in_latest=None, count=5):
    """
    Returns all descendant articles (filtered using the parameters)
    If the `locale_code` in the context is not the main language, it will
    return the translations of the live articles.
    """
    request = context.get('request')
    locale = context.get('locale_code')
    page = section.get_main_language_page()
    settings = SiteSettings.for_site(request.site) \
        if request else None

    qs = ArticlePage.objects.descendant_of(page).filter(
        language__is_main_language=True)

    article_ordering = settings \
        and settings.article_ordering_within_section

    cms_ordering = article_ordering \
        and settings.article_ordering_within_section !=\
        ArticleOrderingChoices.CMS_DEFAULT_SORTING

    if article_ordering and cms_ordering:
        order_by = ArticleOrderingChoices.\
            get(settings.article_ordering_within_section).name.lower()

        order_by = order_by if order_by.find('_desc') == -1 \
            else '-{}'.format(order_by.replace('_desc', ''))
        qs = qs.order_by(order_by)

    if featured_in_homepage is not None:
        qs = qs.filter(featured_in_homepage=featured_in_homepage)\
            .order_by('-featured_in_homepage_start_date')

    if featured_in_latest is not None:
        qs = qs.filter(featured_in_latest=featured_in_latest)

    if featured_in_section is not None:
        qs = qs.filter(featured_in_section=featured_in_section)\
            .order_by('-featured_in_section_start_date')

    if not locale:
        return qs.live()[:count]

    return get_pages(context, qs, locale)[:count]


Instruction:
How would you explain what the following Python 3 function does
def load_child_articles_for_section(
        context, section, featured_in_section=None, count=5):
    """
    Returns all child articles
    If the `locale_code` in the context is not the main language, it will
    return the translations of the live articles.
    """
    request = context.get('request')
    locale = context.get('locale_code')
    main_language_page = section.get_main_language_page()
    settings = SiteSettings.for_site(request.site) \
        if request else None

    # TODO: Consider caching the pks of these articles using a timestamp on
    # section as the key so tha twe don't always do these joins
    article_ordering = settings and settings.article_ordering_within_section
    order_by = ArticleOrderingChoices.\
        get(settings.article_ordering_within_section).name.lower() \
        if article_ordering \
        and settings.article_ordering_within_section !=\
        ArticleOrderingChoices.CMS_DEFAULT_SORTING\
        else '-first_published_at'

    order_by = order_by if order_by.find('_desc') == -1 \
        else '-{}'.format(order_by.replace('_desc', ''))

    child_articles = ArticlePage.objects.child_of(
        main_language_page).filter(
        language__is_main_language=True).order_by(order_by)

    if featured_in_section is not None:
        child_articles = child_articles.filter(
            featured_in_section=featured_in_section)\
            .order_by('-featured_in_section_start_date')

    related_articles = ArticlePage.objects.filter(
        related_sections__section__slug=main_language_page.slug)
    qs = list(chain(
        get_pages(context, child_articles, locale),
        get_pages(context, related_articles, locale)))

    # Pagination
    if count:
        p = context.get('p', 1)
        paginator = Paginator(qs, count)

        try:
            articles = paginator.page(p)
        except PageNotAnInteger:
            articles = paginator.page(1)
        except EmptyPage:
            articles = paginator.page(paginator.num_pages)
    else:
        articles = qs
    if not locale:
        return articles

    context.update({'articles_paginated': articles})
    return articles

Response: Load all child articles for a given section.


Instruction:
Write a Python 3 script to
return all child sections for the given section.

Response:
def load_child_sections_for_section(context, section, count=None):
    '''
    Returns all child sections
    If the `locale_code` in the context is not the main language, it will
    return the translations of the live articles.
    '''
    page = section.get_main_language_page()
    locale = context.get('locale_code')

    qs = SectionPage.objects.child_of(page).filter(
        language__is_main_language=True)

    if not locale:
        return qs[:count]
    return get_pages(context, qs, locale)


Instruction:
Can you tell what is the following Python 3 function doing
def load_sibling_sections(context, section, count=None):
    '''
    Returns all sibling sections
    If the `locale_code` in the context is not the main language, it will
    return the translations of the live articles.
    '''
    page = section.get_main_language_page()
    locale = context.get('locale_code')

    qs = SectionPage.objects.sibling_of(page).filter(
        language__is_main_language=True)

    if not locale:
        return qs[:count]
    return get_pages(context, qs, locale)

Response: Load sibling sections of a given section.


Instruction:
Create a Python 3 function to
create a new page from the content and returns the page and the context dict in a tuple.

Response:
def create_page(self, content, class_, record_keeper=None, logger=None):
        '''
        Robust as possible

        Attempts to create the page
        If any of the functions used to attach content to the page
        fail, keep going, keep a record of those errors in a context dict
        return the page and the context dict in a tuple
        '''
        fields, nested_fields = separate_fields(content)

        foreign_id = content.pop('id')

        # remove unwanted fields
        if 'latest_revision_created_at' in content:
            content.pop('latest_revision_created_at')

        page = class_(**fields)

        # create functions to attach attributes
        function_args_mapping = (
            # add_section_time
            (add_json_dump, ("time", nested_fields, page)),
            # add_tags
            (add_list_of_things, ("tags", nested_fields, page)),
            # add_metadata_tags
            (add_list_of_things, ("metadata_tags", nested_fields, page)),

            # attach_image
            (attach_image, ("image", nested_fields, page, record_keeper)),
            # attach_social_media_image
            (attach_image, ("social_media_image", nested_fields,
                            page, record_keeper)),
            # attach_banner_image
            (attach_image, ("banner", nested_fields, page, record_keeper)),
        )

        for mapping in function_args_mapping:
            function = mapping[0]
            _args = mapping[1]
            try:
                function(*_args)
            except Exception as e:
                if logger:
                    logger.log(
                        ERROR,
                        "Failed to create page content",
                        {
                            "foreign_page_id": foreign_id,
                            "exception": e,
                            "function": function.__name__,
                        })

        # Handle content in nested_fields
        body = add_stream_fields(nested_fields, page)
        # body has not been added as it contains reference to pages
        if body:
            record_keeper.article_bodies[foreign_id] = body

        # Handle relationships in nested_fields
        if record_keeper:
            record_relation_functions = [
                record_keeper.record_nav_tags,
                record_keeper.record_recommended_articles,
                record_keeper.record_reaction_questions,
                record_keeper.record_related_sections,
                record_keeper.record_section_tags,
                record_keeper.record_banner_page_link,
            ]

            for function in record_relation_functions:
                try:
                    function(nested_fields, foreign_id)
                except Exception as e:
                    if logger:
                        logger.log(
                            ERROR,
                            "Failed to record content",
                            {
                                "foreign_page_id": foreign_id,
                                "exception": e,
                                "function": function.__name__,
                            })

        return page


Instruction:
Can you tell what is the following Python 3 function doing
def queryset(self, request, queryset):
        if self.value():
            try:
                section = SectionPage.objects.get(id=self.value())
                return queryset.child_of(section).all()
            except (ObjectDoesNotExist, MultipleObjectsReturned):
                return None

Response:
Returns the queryset based on the value
            provided in the query string and retrievable via the self. value.


Instruction:
Given the following Python 3 function, write the documentation
def upload_file(request):
    '''Upload a Zip File Containing a single file containing media.'''
    if request.method == 'POST':
        form = MediaForm(request.POST, request.FILES)
        if form.is_valid():
            context_dict = {}
            try:
                context_dict['copied_files'] = update_media_file(
                    request.FILES['zip_file'])
            except Exception as e:
                context_dict['error_message'] = e.message
            return render(request,
                          'django_admin/transfer_media_message.html',
                          context_dict)
    else:
        form = MediaForm()
    return render(request, 'django_admin/upload_media.html', {'form': form})

Response: Upload a Zip File Containing a single file containing media.


Instruction:
How would you code a function in Python 3 to
create and download a zip file containing the media file.

Response:
def download_file(request):
    '''Create and download a zip file containing the media file.'''
    if request.method == "GET":
        if path.exists(settings.MEDIA_ROOT):
            zipfile_name = 'media_%s.zip' % settings.SITE_NAME
            in_memory_file = BytesIO()

            media_zipfile = zipfile.ZipFile(in_memory_file, 'w',
                                            zipfile.ZIP_DEFLATED)

            directory_name = path.split(settings.MEDIA_ROOT)[-1]
            for root, dirs, files in walk(directory_name):
                for file in files:
                    media_zipfile.write(path.join(root, file))

            media_zipfile.close()

            resp = HttpResponse(in_memory_file.getvalue(),
                                content_type="application/x-zip-compressed")
            resp['Content-Disposition'] = (
                'attachment; filename=%s' % zipfile_name)

        else:
            resp = render(request,
                          'django_admin/transfer_media_message.html',
                          {'error_message':
                           'media file does not exist'})
    else:
        resp = HttpResponseNotAllowed(permitted_methods=['GET'])

    return resp


Instruction:
Can you tell what is the following Python 3 function doing
def update_permissions_for_group(apps, schema_editor):
    '''
    Update permissions for some users.

    Give bulk-delete permissions to moderators.
    Give edit permission to moderators and editors in order
    to display 'Main' page in the explorer.
    '''
    db_alias = schema_editor.connection.alias
    try:
        # Django 1.9
        emit_post_migrate_signal(2, False, db_alias)
    except TypeError:
        # Django < 1.9
        try:
            # Django 1.8
            emit_post_migrate_signal(2, False, 'default', db_alias)
        except TypeError:  # Django < 1.8
            emit_post_migrate_signal([], 2, False, 'default', db_alias)

    Group = apps.get_model('auth.Group')
    Permission = apps.get_model('auth.Permission')
    GroupPagePermission = apps.get_model('wagtailcore.GroupPagePermission')
    SectionIndexPage = apps.get_model('core.SectionIndexPage')
    MainPage = apps.get_model('core.Main')

    moderator_group = Group.objects.filter(name='Moderators').first()
    editor_group = Group.objects.filter(name='Editors').first()

    if moderator_group:
        sections = SectionIndexPage.objects.first()
        GroupPagePermission.objects.get_or_create(
            group_id=moderator_group.id,
            page_id=sections.id,
            permission_type='bulk_delete'
        )

        main = MainPage.objects.first()
        GroupPagePermission.objects.get_or_create(
            group_id=moderator_group.id,
            page_id=main.id,
            permission_type='edit'
        )

    if editor_group:
        main = MainPage.objects.first()
        GroupPagePermission.objects.get_or_create(
            group_id=editor_group.id,
            page_id=main.id,
            permission_type='edit'
        )

Response: Update permissions for some users in order to display Moderators and editors in order to display Moderators and editors in order to display Moderators and editors in order to display Main page in explorer.


Instruction:
How would you explain what the following Python 3 function does
def filter_queryset(self, request, queryset, view):
        if 'is_main_language' in request.GET:
            # TODO investigate possible error cases where page
            # does not have language
            return queryset.filter(languages__language__is_main_language=True)
        else:
            return queryset

Response: Filter the queryset to only pages in the main language


Instruction:
Can you generate a brief explanation for the following Python 3 code
def create_recomended_articles(main_article, article_list):
    '''
    Creates recommended article objects from article_list
    and _prepends_ to existing recommended articles.
    '''

    # store existing recommended articles
    existing_recommended_articles = [
        ra.recommended_article.specific
        for ra in main_article.recommended_articles.all()]
    # delete existing recommended articles
    ArticlePageRecommendedSections.objects.filter(page=main_article).delete()

    for hyperlinked_article in article_list:
        ArticlePageRecommendedSections(
            page=main_article,
            recommended_article=hyperlinked_article).save()

    # re-create existing recommended articles
    for article in existing_recommended_articles:
        if article not in article_list:
            ArticlePageRecommendedSections(
                page=main_article,
                recommended_article=article).save()

Response: Creates recommended articles from article_list and _prepends_ to existing recommended articles.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def seperate_end_page_links(stream_data):
    '''
    Seperate out page blocks at the end of a StreamField.

    Accepts: List of streamfield blocks
    Returns: Tuple of 2 lists of blocks - (remaining body, final article)
    '''
    stream_data_copy = list(stream_data)
    end_page_links = []

    for block in stream_data_copy[::-1]:
        if block['type'] == 'page':
            end_page_links.insert(0, block)
            stream_data_copy.pop()
        else:
            break

    return (stream_data_copy, end_page_links)

Response: Seperate out page blocks at the end of a StreamField.


Instruction:
Create a Python 3 function for
returning a list of specific page objects from a list of page ids

Response:
def get_pages_from_id_list(id_list):
    '''
    Accepts: list of page ids
    Returns: list of specific page objects
    '''
    page_list = []
    for id_ in id_list:
        try:
            page_list.append(
                Page.objects.get(id=id_).specific)
        except ObjectDoesNotExist:
            logging.error(
                "Attempted to fetch non-existent"
                " page with id of {}".format(id_))
    return page_list


Instruction:
How would you explain what the following Python 3 function does
def move_page_links_to_recommended_articles():
    '''
    Derived from https://github.com/wagtail/wagtail/issues/2110
    '''
    articles = ArticlePage.objects.all()

    for article in articles:
        (remaining_blocks, linked_article_blocks) = seperate_end_page_links(
            article.body.stream_data)

        if linked_article_blocks:
            linked_article_ids = get_page_ids_from_page_blocks(
                linked_article_blocks)
            linked_articles = get_pages_from_id_list(linked_article_ids)
            create_recomended_articles(article, linked_articles)
            parent = article.get_parent().specific
            parent.enable_recommended_section = True
            parent.save()

        stream_block = article.body.stream_block
        article.body = StreamValue(stream_block,
                                   remaining_blocks,
                                   is_lazy=True)
        article.save()

Response: Move all page links to recommended articles.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def cookiecutter(template, checkout=None, no_input=False, extra_context=None):

    # Get user config from ~/.cookiecutterrc or equivalent
    # If no config file, sensible defaults from config.DEFAULT_CONFIG are used
    config_dict = get_user_config()

    template = expand_abbreviations(template, config_dict)

    # TODO: find a better way to tell if it's a repo URL
    if 'git@' in template or 'https://' in template:
        repo_dir = clone(
            repo_url=template,
            checkout=checkout,
            clone_to_dir=config_dict['cookiecutters_dir'],
            no_input=no_input
        )
    else:
        # If it's a local repo, no need to clone or copy to your
        # cookiecutters_dir
        repo_dir = template

    context_file = os.path.join(repo_dir, 'cookiecutter.json')
    logging.debug('context_file is {0}'.format(context_file))

    context = generate_context(
        context_file=context_file,
        default_context=config_dict['default_context'],
        extra_context=extra_context,
    )

    # Create project from local context and project template.
    generate_files(
        repo_dir=repo_dir,
        context=context
    )

Response: This function replaces cookiecutter s own cookiecutter function with the contents of the given template.


Instruction:
Can you tell what is the following Python 3 function doing
def get_queryset(self):
        '''
        This is overwritten in order to not exclude drafts
        and pages submitted for moderation
        '''
        request = self.request

        # Allow pages to be filtered to a specific type
        if 'type' not in request.GET:
            model = Page
        else:
            model_name = request.GET['type']
            try:
                model = resolve_model_string(model_name)
            except LookupError:
                raise BadRequestError("type doesn't exist")
            if not issubclass(model, Page):
                raise BadRequestError("type doesn't exist")

        # This is the overwritten line
        queryset = model.objects.public()  # exclude .live()

        # Filter by site
        queryset = queryset.descendant_of(
            request.site.root_page, inclusive=True)

        return queryset

Response: Returns a queryset of all the pages that are in the moderation tree.


Instruction:
Given the following Python 3 function, write the documentation
def list_of_objects_from_api(url):
    '''
    API only serves 20 pages by default
    This fetches info on all of items and return them as a list

    Assumption: limit of API is not less than 20
    '''
    response = requests.get(url)

    content = json.loads(response.content)
    count = content["meta"]["total_count"]

    if count <= 20:
        return content["items"]
    else:
        items = [] + content["items"]
        num_requests = int(math.ceil(count // 20))

        for i in range(1, num_requests + 1):
            paginated_url = "{}?limit=20&offset={}".format(
                url, str(i * 20))
            paginated_response = requests.get(paginated_url)
            items = items + json.loads(paginated_response.content)["items"]
    return items

Response: This function fetches all of items and returns them as a list of objects


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_content_from_url(self, base_url):
        # assemble url
        base_url = base_url.rstrip("/")
        url = base_url + API_PAGES_ENDPOINT + "?type=" + self._content_type + \
            "&fields=" + ",".join(self._fields) + \
            "&order=latest_revision_created_at"

        # make request
        try:
            response = requests.get(url)
            self._base_url = base_url
            self._content = response.json()
            self._content = self._content["items"]
            return self._content
        except requests.exceptions.ConnectionError:
            return "No content could be found from {}. " \
                "Are you sure this is the correct URL?".format(base_url)
        except requests.exceptions.RequestException:
            return "Content could not be imported at this time. " \
                   "Please try again later."

Response: Get the content from the given base URL.


Instruction:
Explain what the following Python 3 code does
def save(self, indexes, parent_id):
        if self.content():
            parent = Page.objects.get(id=parent_id)

            # Save the selected section page
            response = requests.get(
                self._base_url + API_PAGES_ENDPOINT + str(indexes[0]) + "/"
            )

            section_page = response.json()
            self.process_child_section(section_page["id"], parent)

Response:
Save the selected section. This will save the selected section and all its direct child pages obtained through the?child_of
        query parameter.


Instruction:
How would you explain what the following Python 3 function does
def fetch_and_create_image(self, url, image_title):
        '''
        fetches, creates image object

        returns tuple with Image object and context dictionary containing
        request URL
        '''

        context = {
            "file_url": url,
            "foreign_title": image_title,
        }
        try:
            image_file = requests.get(url)
            local_image = Image(
                title=image_title,
                file=ImageFile(
                    BytesIO(image_file.content),
                    name=image_title
                )
            )
            local_image.save()
            return (local_image, context)
        except Exception as e:
            context.update({
                "exception": e,
            })
            raise ImageCreationFailed(context, None)

Response: Fetches and creates an image object and returns tuple with Image object and context dictionary containing request URL


Instruction:
Create a Python 3 function for
importing and returns tuple with image and context dict.

Response:
def import_image(self, image_id):
        '''
        Imports and returns tuple with image and context dict

        Input: foreign image ID

        Output: (Image: imported image, Dict: info about import)

        Side effects: If Importer object has a record_keeper, it
        will update the record of foreign to local images.

        Attempts to avoid duplicates by matching image dimensions
        and hashes. If a match is found it refers to local instance
        instead. If it is not, the image is fetched, created and
        referenced.
        '''
        image_detail_url = "{}{}/".format(self.image_url, image_id)

        try:
            img_response = requests.get(image_detail_url)
            img_info = json.loads(img_response.content)
        except Exception as e:
            error_context = {
                "image detail url": image_detail_url,
                "exception": e,
            }
            raise ImageInfoFetchFailed(error_context)

        if img_info["image_hash"] is None:
            raise ValueError('image hash should not be none')

        # check if a replica exists
        local_image = self.get_replica_image(
            img_info["image_hash"])

        file_url = img_info['image_url']

        # handle when image_url is relative
        # assumes that image import means local storage
        if img_info['image_url'][0] == '/':
            file_url = "{}{}".format(
                self.base_url, img_info['image_url'])

        if local_image:
            context = {
                "local_version_existed": True,
                "file_url": file_url,
                "image_detail_url": image_detail_url,
                "foreign_title": img_info["title"],
            }
            # update record keeper
            if self.record_keeper:
                self.record_keeper.record_image_relation(
                    image_id,
                    local_image.id)
            return (local_image, context)
        else:
            new_image, context = self.fetch_and_create_image(
                file_url,
                img_info["title"])
            # update record keeper
            if self.record_keeper:
                self.record_keeper.record_image_relation(
                    image_id,
                    new_image.id)
            context.update({
                "local_version_existed": False,
            })
            return (new_image, context)


Instruction:
Write a Python 3 function for
importing all images from site and creates them

Response:
def import_images(self):
        '''
        Fetches all images from site

        Handles Errors in creation process
        Updates record_keeper
        Logs the result of each attempt to create an image
        '''
        self.log(ACTION, "Importing Images")
        try:
            images = list_of_objects_from_api(self.image_url)
        except Exception as e:
            raise ImageInfoFetchFailed(
                "Something went wrong fetching list of images")

        if not images:
            return None

        # iterate through foreign images
        for image_summary in images:
            self.log(ACTION, "Importing Image", depth=1)
            try:
                (image, context) = self.import_image(image_summary["id"])
                # log success
                self.log(SUCCESS, "Importing Image",
                         context=context,
                         depth=1)
            except ImageInfoFetchFailed as e:
                self.log(ERROR, "Importing Images", e, depth=1)
            except ImageCreationFailed as e:
                self.log(ERROR, "Importing Images", e.message, depth=1)
            except Exception as e:
                context = {
                    "exception": e,
                    "foreign_image_id": image_summary["id"],
                }
                self.log(ERROR, "Importing Images", context, depth=1)


Instruction:
Here you have a function in Python 3, explain what it does
def get_language_ids(self):
        '''
        Return list of foreign language IDs from API language endpoint

        TODO: add in validation before creating languages
        '''
        languages = list_of_objects_from_api(self.language_url)

        language_ids = []
        for language in languages:
            language_ids.append(language["id"])
        return language_ids

Response: Return list of foreign language IDs from API language endpoint


Instruction:
Explain what the following Python 3 code does
def recreate_relationships(self, class_, attribute_name, key):
        '''
        Recreates one-to-many relationship
        '''
        iterable = self.record_keeper.foreign_to_many_foreign_map[key]
        for foreign_page_id, foreign_page_id_list in iteritems(iterable):

            # Assumption: local page has been indexed and exists
            # TODO: handle case where it doesn't exist
            local_page_id = self.record_keeper.get_local_page(foreign_page_id)
            local_page = Page.objects.get(id=local_page_id).specific

            for _foreign_page_id in foreign_page_id_list:
                try:
                    local_version_page_id = (self.record_keeper
                                             .get_local_page(_foreign_page_id))
                    foreign_page = Page.objects.get(
                        id=local_version_page_id).specific
                    realtionship_object = class_(page=local_page)
                    setattr(realtionship_object, attribute_name, foreign_page)
                    realtionship_object.save()
                except Exception as e:
                    context = {
                        "exception": e,
                        "function_schema": ("recreate_relationships"
                                            "(class, attribute_name, key)"),
                        "attribute_name": str(attribute_name),
                        "key": str(key),
                        "class": str(class_),
                        "foreign_page_id": str(foreign_page_id),
                    }
                    self.log(ERROR, "recreating relationships",
                             context=context)

Response: Recreates one - to - many relationship.


Instruction:
Given the following Python 3 function, write the documentation
def recreate_relationship(self, attribute_name, key):
        '''
        Recreates one-to-one relationship
        '''
        iterable = self.record_keeper.foreign_to_foreign_map["banner_link_page"]  # noqa
        for foreign_page_id, linked_page_foreign_id in iteritems(iterable):
            # get local banner page
            local_page_id = self.record_keeper.get_local_page(foreign_page_id)
            local_page = Page.objects.get(id=local_page_id).specific

            # get local linked page
            local_id = self.record_keeper.get_local_page(
                linked_page_foreign_id)
            linked_page = Page.objects.get(id=local_id).specific

            # link the two together
            setattr(local_page, attribute_name, linked_page)
            # TODO: review publishing and saving revisions
            local_page.save_revision().publish()

Response: Recreates one - to - one relationship.


Instruction:
How would you explain what the following Python 3 function does
def recreate_article_body(self):
        '''
        Handles case where article body contained page or image.

        Assumes all articles and images have been created.
        '''
        for foreign_id, body in iteritems(self.record_keeper.article_bodies):
            try:
                local_page_id = self.record_keeper.get_local_page(foreign_id)
                page = Page.objects.get(id=local_page_id).specific

                # iterate through the body
                new_body = []
                for item in body:
                    if not item['value']:
                        continue
                    if item['type'] == 'page':
                        new_page_id = self.record_keeper.get_local_page(
                            item['value'])
                        item['value'] = new_page_id
                    elif item['type'] == 'image':
                        new_image_id = self.record_keeper.get_local_image(
                            item['value'])
                        item['value'] = new_image_id

                    new_body.append(item)

                setattr(page, 'body', json.dumps(new_body))
                page.save_revision().publish()

            except Exception as e:
                self.log(ERROR, "recreating article body",
                         {
                             "exception": e,
                             "foreign_id": foreign_id,
                             "body": body,
                         },
                         depth=1)

Response: Handles case where article body contained page or image.


Instruction:
Explain what the following Python 3 code does
def attach_translated_content(self, local_main_lang_page,
                                  content, locale):
        '''
        Wrapper for  attach_page

        Creates the content
        Then attaches a language relation from the main language page to the
        newly created Page
        Note: we get the parent from the main language page
        '''
        try:
            page = self.attach_page(
                local_main_lang_page.get_parent(),
                content)
        except:
            # TODO: log this
            return None

        try:
            # create the translation object for page
            language = SiteLanguageRelation.objects.get(
                language_setting=self.language_setting,
                locale=locale)
            page.language = language
            page.translated_pages.add(local_main_lang_page)
            local_main_lang_page.translated_pages.add(page)
            page.save()
            local_main_lang_page.save()
        except:
            # TODO: log that creating translation failed
            # TODO: log that page is now being deleted
            page.delete()
        return page

Response: Wrapper for attach_page attach_page_with_language_with_language_with_page attach_page attach_page_with_language_with_page attach_page attach


Instruction:
Can you tell what is the following Python 3 function doing
def copy_page_and_children(self, foreign_id, parent_id, depth=0):
        '''
        Recusively copies over pages, their translations, and child pages
        '''
        url = "{}/api/v2/pages/{}/".format(self.base_url, foreign_id)

        self.log(ACTION, "Requesting Data", {"url": url}, depth)
        try:
            # TODO: create a robust wrapper around this functionality
            response = requests.get(url)
            content = json.loads(response.content)
        except Exception as e:
            self.log(ERROR, "Requesting Data - abandoning copy",
                     {"url": url, "exception": e}, depth)
            return None

        parent = Page.objects.get(id=parent_id).specific
        page = None
        try:
            self.log(ACTION, "Create Page", {"url": url}, depth)
            page = self.attach_page(parent, content)
            if page:
                self.log(SUCCESS, "Create Page",
                         {"url": url,
                          "page title": page.title.encode('utf-8')},
                         depth)
        except PageNotImportable as e:
            message = e.message.pop("message")
            self.log(WARNING, message, e.message.pop("message"), depth)
            return None

        if page:
            # create translations
            if content["meta"]["translations"]:
                for translation_obj in content["meta"]["translations"]:
                    _url = "{}/api/v2/pages/{}/".format(self.base_url,
                                                        translation_obj["id"])
                    # TODO: create a robust wrapper around this functionality
                    _response = requests.get(_url)
                    self.log(
                        ACTION,
                        "Getting translated content",
                        {"url": _url}, depth)
                    if _response.content:
                        _content = json.loads(_response.content)

                        if ("locale" in translation_obj and
                                translation_obj["locale"]):
                            self.attach_translated_content(
                                page, _content, translation_obj["locale"])
                        else:
                            self.log(
                                ERROR,
                                "locale is null",
                                {"url": _url, }, depth)
                    else:
                        self.log(
                            ERROR,
                            "Getting translated content",
                            {"url": _url}, depth)

            main_language_child_ids = content["meta"]["main_language_children"]

            # recursively iterate through child nodes
            if main_language_child_ids:
                for main_language_child_id in main_language_child_ids:
                    try:
                        self.copy_page_and_children(
                            foreign_id=main_language_child_id,
                            parent_id=page.id, depth=depth + 1)
                    except Exception as e:
                        self.log(ERROR, "Copying Children",
                                 {"url": url, "exception": e})

Response: Recusively copies over pages their translations and child pages


Instruction:
Implement a Python 3 function for
initiating copying of tree with existing_node acting as root

Response:
def copy_children(self, foreign_id, existing_node):
        '''
        Initiates copying of tree, with existing_node acting as root
        '''
        url = "{}/api/v2/pages/{}/".format(self.base_url, foreign_id)
        self.log(
            ACTION,
            "Copying Children",
            {"existing node type": str(type(existing_node))})

        # TODO: create a robust wrapper around this functionality
        try:
            self.log(ACTION, "Requesting Data", {"url": url})
            response = requests.get(url)
            content = json.loads(response.content)
            self.log(SUCCESS, "Data Fetched Successfully", {"url": url})

            main_language_child_ids = content["meta"]["main_language_children"]
            if main_language_child_ids:
                for main_language_child_id in main_language_child_ids:
                    self.copy_page_and_children(
                        foreign_id=main_language_child_id,
                        parent_id=existing_node.id, depth=1)
            else:
                self.log(SUCCESS, "No children to copy")
        except Exception as e:
            self.log(ERROR, "Copying Children", {"url": url, "exception": e})


Instruction:
How would you implement a function in Python 3 that
returns a string representation of the email logs.

Response:
def get_email_logs(self):
        '''
        Returns a string representation of logs.

        Only displays errors and warnings in the email logs
        to avoid being verbose
        '''
        message = ""
        for log in self.record:
            if log["log_type"] in [ERROR, WARNING]:
                message += self.format_message(**log)

        return message


Instruction:
Here you have a function in Python 3, explain what it does
def chain_to_bitcoin_network(chain):
    if chain == Chain.bitcoin_mainnet:
        return SYS_NETWORK_BITCOIN_MAINNET
    elif chain == Chain.bitcoin_testnet:
        return SYS_NETWORK_BITCOIN_TESTNET
    elif chain == Chain.bitcoin_regtest:
        return SYS_NETWORK_BITCOIN_REGTEST
    else:
        message = 'This chain cannot be converted to a bitcoin netcode; chain='
        if chain:
            message += chain.name
        else:
            message += '<NULL>'
        raise UnknownChainError(message)

Response: Convert a chain to a bitcoin netcode.


Instruction:
Implement a function in Python 3 to
make sure the directories exist for filename.

Response:
def ensure_dirs(filename):
    """Make sure the directories exist for `filename`."""
    dirname, _ = os.path.split(filename)
    if dirname and not os.path.exists(dirname):
        os.makedirs(dirname)


Instruction:
Can you tell what is the following Python 3 function doing
def get_certificate(self, certificate_uid):
        certificate_json = self.get_certificate_json(certificate_uid)
        return model.to_certificate_model(certificate_json)

Response: Returns a certificate object. Propagates KeyError if key isn t found


Instruction:
Make a summary of the following Python 3 code
def get_certificate_json(self, certificate_uid):

        if certificate_uid.startswith(URN_UUID_PREFIX):
            uid = certificate_uid[len(URN_UUID_PREFIX):]
        elif certificate_uid.startswith('http'):
            last_slash = certificate_uid.rindex('/')
            uid = certificate_uid[last_slash + 1:]
        else:
            uid = certificate_uid
        logging.debug('Retrieving certificate for uid=%s', uid)
        certificate_bytes = self._get_certificate_raw(uid)
        logging.debug('Found certificate for uid=%s', uid)
        certificate_json = helpers.certificate_bytes_to_json(certificate_bytes)
        return certificate_json

Response: Get certificate as json. Propagates KeyError if key isn t found


Instruction:
Can you implement a function in Python 3 that
gets the certificate as raw bytes. Per kvstore contract raises KeyError.

Response:
def _get_certificate_raw(self, certificate_uid):
        """
        Returns certificate as raw bytes. Per kvstore contract, raises an KeyError if key isn't found.
        :param certificate_uid:
        :return:
        """
        cert_file_bytes = self.kv_store.get(certificate_uid_to_filename(certificate_uid))
        return cert_file_bytes