Instruction:
Write a Python 3 function for
packing a list of items into a wire format.

Response:
def pack_list(from_, pack_type):
    """ Return the wire packed version of `from_`. `pack_type` should be some
    subclass of `xcffib.Struct`, or a string that can be passed to
    `struct.pack`. You must pass `size` if `pack_type` is a struct.pack string.
    """
    # We need from_ to not be empty
    if len(from_) == 0:
        return bytes()

    if pack_type == 'c':
        if isinstance(from_, bytes):
            # Catch Python 3 bytes and Python 2 strings
            # PY3 is "helpful" in that when you do tuple(b'foo') you get
            # (102, 111, 111) instead of something more reasonable like
            # (b'f', b'o', b'o'), so we rebuild from_ as a tuple of bytes
            from_ = [six.int2byte(b) for b in six.iterbytes(from_)]
        elif isinstance(from_, six.string_types):
            # Catch Python 3 strings and Python 2 unicode strings, both of
            # which we encode to bytes as utf-8
            # Here we create the tuple of bytes from the encoded string
            from_ = [six.int2byte(b) for b in bytearray(from_, 'utf-8')]
        elif isinstance(from_[0], six.integer_types):
            # Pack from_ as char array, where from_ may be an array of ints
            # possibly greater than 256
            def to_bytes(v):
                for _ in range(4):
                    v, r = divmod(v, 256)
                    yield r
            from_ = [six.int2byte(b) for i in from_ for b in to_bytes(i)]

    if isinstance(pack_type, six.string_types):
        return struct.pack("=" + pack_type * len(from_), *from_)
    else:
        buf = six.BytesIO()
        for item in from_:
            # If we can't pack it, you'd better have packed it yourself. But
            # let's not confuse things which aren't our Probobjs for packable
            # things.
            if isinstance(item, Protobj) and hasattr(item, "pack"):
                buf.write(item.pack())
            else:
                buf.write(item)
        return buf.getvalue()


Instruction:
Make a summary of the following Python 3 code
def ensure_connected(f):
        @functools.wraps(f)
        def wrapper(*args):
            self = args[0]
            self.invalid()
            try:
                return f(*args)
            finally:
                self.invalid()
        return wrapper

Response: Decorator that ensures that the connection is valid.


Instruction:
How would you code a function in Python 3 to
return the xcb_screen_t for every screen useful for other bindings

Response:
def get_screen_pointers(self):
        """
        Returns the xcb_screen_t for every screen
        useful for other bindings
        """
        root_iter = lib.xcb_setup_roots_iterator(self._setup)

        screens = [root_iter.data]
        for i in range(self._setup.roots_len - 1):
            lib.xcb_screen_next(ffi.addressof((root_iter)))
            screens.append(root_iter.data)
        return screens


Instruction:
Can you generate the documentation for the following Python 3 function
def hoist_event(self, e):
        if e.response_type == 0:
            return self._process_error(ffi.cast("xcb_generic_error_t *", e))

        # We mask off the high bit here because events sent with SendEvent have
        # this bit set. We don't actually care where the event came from, so we
        # just throw this away. Maybe we could expose this, if anyone actually
        # cares about it.
        event = self._event_offsets[e.response_type & 0x7f]

        buf = CffiUnpacker(e)
        return event(buf)

Response: Hoist an xcb_generic_event_t to the right xcffib structure.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def serialize(self, value, greedy=True):

        if greedy and not isinstance(value, Column):
            value = self.normalize(value)

        if isinstance(value, Column):
            return value.id
        else:
            return value

Response: Serialize the value into a column or a string.


Instruction:
Can you implement a function in Python 3 that
generates a query by describing it.

Response:
def describe(profile, description):
    """
    Generate a query by describing it as a series of actions
    and parameters to those actions. These map directly
    to Query methods and arguments to those methods.

    This is an alternative to the chaining interface.
    Mostly useful if you'd like to put your queries
    in a file, rather than in Python code.
    """
    api_type = description.pop('type', 'core')
    api = getattr(profile, api_type)
    return refine(api.query, description)


Instruction:
Here you have a function in Python 3, explain what it does
def refine(query, description):

    for attribute, arguments in description.items():
        if hasattr(query, attribute):
            attribute = getattr(query, attribute)
        else:
            raise ValueError("Unknown query method: " + attribute)

        # query descriptions are often automatically generated, and
        # may include empty calls, which we skip
        if utils.isempty(arguments):
            continue

        if callable(attribute):
            method = attribute
            if isinstance(arguments, dict):
                query = method(**arguments)
            elif isinstance(arguments, list):
                query = method(*arguments)
            else:
                query = method(arguments)
        else:
            setattr(attribute, arguments)

    return query

Response: Refine a query from a dictionary of parameters that describes it.


Instruction:
Write a Python 3 function that can
set the raw properties of the current object.

Response:
def set(self, key=None, value=None, **kwargs):
        """
        `set` is a way to add raw properties to the request,
        for features that this module does not
        support or supports incompletely. For convenience's
        sake, it will serialize Column objects but will
        leave any other kind of value alone.
        """

        serialize = partial(self.api.columns.serialize, greedy=False)

        if key and value:
            self.raw[key] = serialize(value)
        elif key or kwargs:
            properties = key or kwargs
            for key, value in properties.items():
                self.raw[key] = serialize(value)
        else:
            raise ValueError(
                "Query#set requires a key and value, a properties dictionary or keyword arguments.")

        return self


Instruction:
Write a Python 3 script to
return a new query which will produce results sorted by the specified columns or all other columns.

Response:
def sort(self, *columns, **options):
        """
        Return a new query which will produce results sorted by
        one or more metrics or dimensions. You may use plain
        strings for the columns, or actual `Column`, `Metric`
        and `Dimension` objects.

        Add a minus in front of the metric (either the string or
        the object) to sort in descending order.

        ```python
        # sort using strings
        query.sort('pageviews', '-device type')
        # alternatively, ask for a descending sort in a keyword argument
        query.sort('pageviews', descending=True)

        # sort using metric, dimension or column objects
        pageviews = profile.core.metrics['pageviews']
        query.sort(-pageviews)
        ```
        """

        sorts = self.meta.setdefault('sort', [])

        for column in columns:
            if isinstance(column, Column):
                identifier = column.id
            elif isinstance(column, utils.basestring):
                descending = column.startswith('-') or options.get('descending', False)
                identifier = self.api.columns[column.lstrip('-')].id
            else:
                raise ValueError("Can only sort on columns or column strings. Received: {}".format(column))

            if descending:
                sign = '-'
            else:
                sign = ''

            sorts.append(sign + identifier)

        self.raw['sort'] = ",".join(sorts)
        return self


Instruction:
Make a summary of the following Python 3 code
def filter(self, value=None, exclude=False, **selection):
        filters = self.meta.setdefault('filters', [])

        if value and len(selection):
            raise ValueError("Cannot specify a filter string and a filter keyword selection at the same time.")
        elif value:
            value = [value]
        elif len(selection):
            value = select(self.api.columns, selection, invert=exclude)

        filters.append(value)
        self.raw['filters'] = utils.paste(filters, ',', ';')
        return self

Response: Filters the metadata dictionary by the specified value.


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

        if isinstance(precision, int):
            precision = self.PRECISION_LEVELS[precision]

        if precision not in self.PRECISION_LEVELS:
            levels = ", ".join(self.PRECISION_LEVELS)
            raise ValueError("Precision should be one of: " + levels)

        if precision != 'DEFAULT':
            self.raw.update({'samplingLevel': precision})

        return self

Response: Set the precision of the log entries for this instance.


Instruction:
Write a Python 3 function for
adding a new interval to the internal data structure.

Response:
def interval(self, granularity):
        """
        Note that if you don't specify a granularity (either through the `interval`
        method or through the `hourly`, `daily`, `weekly`, `monthly` or `yearly`
        shortcut methods) you will get only a single result, encompassing the
        entire date range, per metric.
        """

        if granularity == 'total':
            return self

        if not isinstance(granularity, int):
            if granularity in self.GRANULARITY_LEVELS:
                granularity = self.GRANULARITY_LEVELS.index(granularity)
            else:
                levels = ", ".join(self.GRANULARITY_LEVELS)
                raise ValueError("Granularity should be one of: lifetime, " + levels)

        dimension = self.GRANULARITY_DIMENSIONS[granularity]
        self.raw['dimensions'].insert(0, dimension)

        return self


Instruction:
Write a Python 3 function that can
return a new query that fetches metrics within a certain date range.

Response:
def range(self, start=None, stop=None, months=0, days=0):
        """
        Return a new query that fetches metrics within a certain date range.

        ```python
        query.range('2014-01-01', '2014-06-30')
        ```

        If you don't specify a `stop` argument, the date range will end today. If instead
        you meant to fetch just a single day's results, try:

        ```python
        query.range('2014-01-01', days=1)
        ```

        More generally, you can specify that you'd like a certain number of days,
        starting from a certain date:

        ```python
        query.range('2014-01-01', months=3)
        query.range('2014-01-01', days=28)
        ```

        Note that if you don't specify a granularity (either through the `interval`
        method or through the `hourly`, `daily`, `weekly`, `monthly` or `yearly`
        shortcut methods) you will get only a single result, encompassing the
        entire date range, per metric.

        **Note:** it is currently not possible to easily specify that you'd like
        to query the last last full week(s), month(s) et cetera.
        This will be added sometime in the future.
        """

        start, stop = utils.date.range(start, stop, months, days)

        self.raw.update({
            'start_date': start,
            'end_date': stop,
        })

        return self


Instruction:
Can you implement a function in Python 3 that
returns a new query limited to a certain number of results.

Response:
def limit(self, *_range):
        """
        Return a new query, limited to a certain number of results.

        ```python
        # first 100
        query.limit(100)
        # 50 to 60
        query.limit(50, 10)
        ```

        Please note carefully that Google Analytics uses
        1-indexing on its rows.
        """

        # uses the same argument order as
        # LIMIT in a SQL database
        if len(_range) == 2:
            start, maximum = _range
        else:
            start = 1
            maximum = _range[0]

        self.meta['limit'] = maximum

        self.raw.update({
            'start_index': start,
            'max_results': maximum,
        })
        return self


Instruction:
Explain what the following Python 3 code does
def segment(self, value=None, scope=None, metric_scope=None, **selection):

        """
        Technical note to self about segments:

        * users or sessions
        * sequence or condition
        * scope (perHit, perSession, perUser -- gte primary scope)

        Multiple conditions can be ANDed or ORed together; these two are equivalent

            users::condition::ga:revenue>10;ga:sessionDuration>60
            users::condition::ga:revenue>10;users::condition::ga:sessionDuration>60

        For sequences, prepending ^ means the first part of the sequence has to match
        the first session/hit/...

        * users and sessions conditions can be combined (but only with AND)
        * sequences and conditions can also be combined (but only with AND)

        sessions::sequence::ga:browser==Chrome;
        condition::perHit::ga:timeOnPage>5
        ->>
        ga:deviceCategory==mobile;ga:revenue>10;

        users::sequence::ga:deviceCategory==desktop
        ->>
        ga:deviceCategory=mobile;
        ga:revenue>100;
        condition::ga:browser==Chrome

        Problem: keyword arguments are passed as a dictionary, not an ordered dictionary!
        So e.g. this is risky

            query.sessions(time_on_page__gt=5, device_category='mobile', followed_by=True)
        """

        SCOPES = {
            'hits': 'perHit',
            'sessions': 'perSession',
            'users': 'perUser',
            }
        segments = self.meta.setdefault('segments', [])

        if value and len(selection):
            raise ValueError("Cannot specify a filter string and a filter keyword selection at the same time.")
        elif value:
            value = [self.api.segments.serialize(value)]
        elif len(selection):
            if not scope:
                raise ValueError("Scope is required. Choose from: users, sessions.")

            if metric_scope:
                metric_scope = SCOPES[metric_scope]

            value = select(self.api.columns, selection)
            value = [[scope, 'condition', metric_scope, condition] for condition in value]
            value = ['::'.join(filter(None, condition)) for condition in value]

        segments.append(value)
        self.raw['segment'] = utils.paste(segments, ',', ';')
        return self

Response: Return a new query limited to a segment of all users or sessions.


Instruction:
Create a Python 3 function to
return a new query with a modified start_index.

Response:
def next(self):
        """
        Return a new query with a modified `start_index`.
        Mainly used internally to paginate through results.
        """
        step = self.raw.get('max_results', 1000)
        start = self.raw.get('start_index', 1) + step
        self.raw['start_index'] = start
        return self


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

        cursor = self
        report = None
        is_complete = False
        is_enough = False

        while not (is_enough or is_complete):
            chunk = cursor.execute()

            if report:
                report.append(chunk.raw[0], cursor)
            else:
                report = chunk

            is_enough = len(report.rows) >= self.meta.get('limit', float('inf'))
            is_complete = chunk.is_complete
            cursor = cursor.next()

        return report

Response: Run the query and return a Report instance.


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

        self.meta['limit'] = maximum
        self.raw.update({
            'max_results': maximum,
        })
        return self

Response: Return a new query limited to a certain number of results.


Instruction:
Create a Python 3 function to
return True if the credentials are not necessarily correct and False otherwise.

Response:
def valid(self):
        """ Valid credentials are not necessarily correct, but
        they contain all necessary information for an
        authentication attempt. """
        two_legged = self.client_email and self.private_key
        three_legged = self.client_id and self.client_secret
        return two_legged or three_legged or False


Instruction:
Can you implement a function in Python 3 that
completes credentials are valid and include a token.

Response:
def complete(self):
        """ Complete credentials are valid and are either two-legged or include a token. """
        return self.valid and (self.access_token or self.refresh_token or self.type == 2)


Instruction:
Implement a function in Python 3 to
authenticate the user with Google Analytics API.

Response:
def authenticate(
        client_id=None, client_secret=None,
        client_email=None, private_key=None,
        access_token=None, refresh_token=None,
        account=None, webproperty=None, profile=None,
        identity=None, prefix=None, suffix=None,
        interactive=False, save=False):
    """
    The `authenticate` function will authenticate the user with the Google Analytics API,
    using a variety of strategies: keyword arguments provided to this function, credentials
    stored in in environment variables, credentials stored in the keychain and, finally, by
    asking for missing information interactively in a command-line prompt.

    If necessary (but only if `interactive=True`) this function will also allow the user
    to authorize this Python module to access Google Analytics data on their behalf,
    using an OAuth2 token.
    """

    credentials = oauth.Credentials.find(
        valid=True,
        interactive=interactive,
        prefix=prefix,
        suffix=suffix,
        client_id=client_id,
        client_secret=client_secret,
        client_email=client_email,
        private_key=private_key,
        access_token=access_token,
        refresh_token=refresh_token,
        identity=identity,
        )

    if credentials.incomplete:
        if interactive:
            credentials = authorize(
                client_id=credentials.client_id,
                client_secret=credentials.client_secret,
                save=save,
                identity=credentials.identity,
                prefix=prefix,
                suffix=suffix,
                )
        elif credentials.type == 2:
            credentials = authorize(
                client_email=credentials.client_email,
                private_key=credentials.private_key,
                identity=credentials.identity,
                save=save,
                )
        else:
            raise KeyError("Cannot authenticate: enable interactive authorization, pass a token or use a service account.")

    accounts = oauth.authenticate(credentials)
    scope = navigate(accounts, account=account, webproperty=webproperty, profile=profile)
    return scope


Instruction:
Implement a Python 3 function for
revoking OAuth access to Google Analytics data.

Response:
def revoke(client_id, client_secret,
        client_email=None, private_key=None,
        access_token=None, refresh_token=None,
        identity=None, prefix=None, suffix=None):

    """
    Given a client id, client secret and either an access token or a refresh token,
    revoke OAuth access to the Google Analytics data and remove any stored credentials
    that use these tokens.
    """

    if client_email and private_key:
        raise ValueError('Two-legged OAuth does not use revokable tokens.')

    credentials = oauth.Credentials.find(
        complete=True,
        interactive=False,
        identity=identity,
        client_id=client_id,
        client_secret=client_secret,
        access_token=access_token,
        refresh_token=refresh_token,
        prefix=prefix,
        suffix=suffix,
        )

    retval = credentials.revoke()
    keyring.delete(credentials.identity)
    return retval


Instruction:
Can you tell what is the following Python 3 function doing
def query(scope, blueprint, debug, output, with_metadata, realtime, **description):

    if realtime:
        description['type'] = 'realtime'

    if blueprint:
        queries = from_blueprint(scope, blueprint)
    else:
        if not isinstance(scope, ga.account.Profile):
            raise ValueError("Account and webproperty needed for query.")

        queries = from_args(scope, **description)

    for query in queries:
        if debug:
            click.echo(query.build())

        report = query.serialize(format=output, with_metadata=with_metadata)
        click.echo(report)

Response: Query the pageviews for the specified scope.


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

    @functools.wraps(fn)
    def vectorized_method(self, values, *vargs, **kwargs):
        wrap = not isinstance(values, (list, tuple))
        should_unwrap = not kwargs.setdefault('wrap', False)
        unwrap = wrap and should_unwrap
        del kwargs['wrap']

        if wrap:
            values = [values]

        results = [fn(self, value, *vargs, **kwargs) for value in values]

        if unwrap:
            results = results[0]

        return results

    return vectorized_method

Response: A method that accepts one or more values and returns a list or a single item depending on what is desired.


Instruction:
Can you generate the documentation for the following Python 3 function
def profiles(self):
        raw_profiles = self.account.service.management().profiles().list(
            accountId=self.account.id,
            webPropertyId=self.id).execute()['items']
        profiles = [Profile(raw, self) for raw in raw_profiles]
        return addressable.List(profiles, indices=['id', 'name'], insensitive=True)

Response: A list of all profiles on this web property.


Instruction:
Here you have a function in Python 3, explain what it does
def check_output_input(*popenargs, **kwargs):
    if 'stdout' in kwargs:
        raise ValueError('stdout argument not allowed, it will be overridden.')
    if 'input' in kwargs:
        if 'stdin' in kwargs:
            raise ValueError('stdin and input arguments may not both be used.')
        inputdata = kwargs['input']
        del kwargs['input']
        kwargs['stdin'] = PIPE
    else:
        inputdata = None
    process = Popen(*popenargs, stdout=PIPE, **kwargs)
    try:
        output, unused_err = process.communicate(inputdata)
    except:
        process.kill()
        process.wait()
        raise
    retcode = process.poll()
    if retcode:
        cmd = kwargs.get("args")
        if cmd is None:
            cmd = popenargs[0]
        raise CalledProcessError(retcode, cmd, output=output)
    return output

Response: Run a command with arguments and return its output as a byte string.


Instruction:
How would you code a function in Python 3 to
create token indices for the given tokens.

Response:
def create_token_indices(self, tokens):
        """If `apply_encoding_options` is inadequate, one can retrieve tokens from `self.token_counts`, filter with
        a desired strategy and regenerate `token_index` using this method. The token index is subsequently used
        when `encode_texts` or `decode_texts` methods are called.
        """
        start_index = len(self.special_token)
        indices = list(range(len(tokens) + start_index))
        # prepend because the special tokens come in the beginning
        tokens_with_special = self.special_token + list(tokens)
        self._token2idx = dict(list(zip(tokens_with_special, indices)))
        self._idx2token = dict(list(zip(indices, tokens_with_special)))


Instruction:
How would you code a function in Python 3 to
apply the given settings for subsequent calls to encode_texts and decode_texts.

Response:
def apply_encoding_options(self, min_token_count=1, limit_top_tokens=None):
        """Applies the given settings for subsequent calls to `encode_texts` and `decode_texts`. This allows you to
        play with different settings without having to re-run tokenization on the entire corpus.

        Args:
            min_token_count: The minimum token count (frequency) in order to include during encoding. All tokens
                below this frequency will be encoded to `0` which corresponds to unknown token. (Default value = 1)
            limit_top_tokens: The maximum number of tokens to keep, based their frequency. Only the most common `limit_top_tokens`
                tokens will be kept. Set to None to keep everything. (Default value: None)
        """
        if not self.has_vocab:
            raise ValueError("You need to build the vocabulary using `build_vocab` "
                             "before using `apply_encoding_options`")
        if min_token_count < 1:
            raise ValueError("`min_token_count` should atleast be 1")

        # Remove tokens with freq < min_token_count
        token_counts = list(self._token_counts.items())
        token_counts = [x for x in token_counts if x[1] >= min_token_count]

        # Clip to max_tokens.
        if limit_top_tokens is not None:
            token_counts.sort(key=lambda x: x[1], reverse=True)
            filtered_tokens = list(zip(*token_counts))[0]
            filtered_tokens = filtered_tokens[:limit_top_tokens]
        else:
            filtered_tokens = zip(*token_counts)[0]

        # Generate indices based on filtered tokens.
        self.create_token_indices(filtered_tokens)


Instruction:
How would you code a function in Python 3 to
encode the given texts using internal vocabulary with optionally applied encoding options.

Response:
def encode_texts(self, texts, unknown_token="<UNK>", verbose=1, **kwargs):
        """Encodes the given texts using internal vocabulary with optionally applied encoding options. See
        ``apply_encoding_options` to set various options.

        Args:
            texts: The list of text items to encode.
            unknown_token: The token to replace words that out of vocabulary. If none, those words are omitted.
            verbose: The verbosity level for progress. Can be 0, 1, 2. (Default value = 1)
            **kwargs: The kwargs for `token_generator`.

        Returns:
            The encoded texts.
        """
        if not self.has_vocab:
            raise ValueError(
                "You need to build the vocabulary using `build_vocab` before using `encode_texts`")

        if unknown_token and unknown_token not in self.special_token:
            raise ValueError(
                "Your special token (" + unknown_token + ") to replace unknown words is not in the list of special token: " + self.special_token)

        progbar = Progbar(len(texts), verbose=verbose, interval=0.25)
        encoded_texts = []
        for token_data in self.token_generator(texts, **kwargs):
            indices, token = token_data[:-1], token_data[-1]

            token_idx = self._token2idx.get(token)
            if token_idx is None and unknown_token:
                token_idx = self.special_token.index(unknown_token)

            if token_idx is not None:
                utils._append(encoded_texts, indices, token_idx)

            # Update progressbar per document level.
            progbar.update(indices[0])

        # All done. Finalize progressbar.
        progbar.update(len(texts))
        return encoded_texts


Instruction:
Implement a function in Python 3 to
decode the texts using internal vocabulary.

Response:
def decode_texts(self, encoded_texts, unknown_token="<UNK>", inplace=True):
        """Decodes the texts using internal vocabulary. The list structure is maintained.

        Args:
            encoded_texts: The list of texts to decode.
            unknown_token: The placeholder value for unknown token. (Default value: "<UNK>")
            inplace: True to make changes inplace. (Default value: True)

        Returns:
            The decoded texts.
        """
        if len(self._token2idx) == 0:
            raise ValueError(
                "You need to build vocabulary using `build_vocab` before using `decode_texts`")

        if not isinstance(encoded_texts, list):
            # assume it's a numpy array
            encoded_texts = encoded_texts.tolist()

        if not inplace:
            encoded_texts = deepcopy(encoded_texts)
        utils._recursive_apply(encoded_texts,
                               lambda token_id: self._idx2token.get(token_id) or unknown_token)
        return encoded_texts


Instruction:
Can you write a function in Python 3 where it
builds the internal vocabulary and computes various statistics.

Response:
def build_vocab(self, texts, verbose=1, **kwargs):
        """Builds the internal vocabulary and computes various statistics.

        Args:
            texts: The list of text items to encode.
            verbose: The verbosity level for progress. Can be 0, 1, 2. (Default value = 1)
            **kwargs: The kwargs for `token_generator`.
        """
        if self.has_vocab:
            logger.warn(
                "Tokenizer already has existing vocabulary. Overriding and building new vocabulary.")

        progbar = Progbar(len(texts), verbose=verbose, interval=0.25)
        count_tracker = utils._CountTracker()

        self._token_counts.clear()
        self._num_texts = len(texts)

        for token_data in self.token_generator(texts, **kwargs):
            indices, token = token_data[:-1], token_data[-1]
            count_tracker.update(indices)
            self._token_counts[token] += 1

            # Update progressbar per document level.
            progbar.update(indices[0])

        # Generate token2idx and idx2token.
        self.create_token_indices(self._token_counts.keys())

        # All done. Finalize progressbar update and count tracker.
        count_tracker.finalize()
        self._counts = count_tracker.counts
        progbar.update(len(texts))


Instruction:
Write a Python 3 script to
pad each sequence in the list of sequences to the same fixed length.

Response:
def pad_sequences(self, sequences, fixed_sentences_seq_length=None, fixed_token_seq_length=None,
                      padding='pre', truncating='post', padding_token="<PAD>"):
        """Pads each sequence to the same fixed length (length of the longest sequence or provided override).

        Args:
            sequences: list of list (samples, words) or list of list of list (samples, sentences, words)
            fixed_sentences_seq_length: The fix sentence sequence length to use. If None, largest sentence length is used.
            fixed_token_seq_length: The fix token sequence length to use. If None, largest word length is used.
            padding: 'pre' or 'post', pad either before or after each sequence.
            truncating: 'pre' or 'post', remove values from sequences larger than fixed_sentences_seq_length or fixed_token_seq_length
                either in the beginning or in the end of the sentence or word sequence respectively.
            padding_token: The token to add for padding.

        Returns:
            Numpy array of (samples, max_sentences, max_tokens) or (samples, max_tokens) depending on the sequence input.

        Raises:
            ValueError: in case of invalid values for `truncating` or `padding`.
        """
        value = self.special_token.index(padding_token)
        if value < 0:
            raise ValueError('The padding token "' + padding_token +
                             " is not in the special tokens of the tokenizer.")
        # Determine if input is (samples, max_sentences, max_tokens) or not.
        if isinstance(sequences[0][0], list):
            x = utils._pad_sent_sequences(sequences, fixed_sentences_seq_length,
                                          fixed_token_seq_length, padding, truncating, value)
        else:
            x = utils._pad_token_sequences(
                sequences, fixed_token_seq_length, padding, truncating, value)
        return np.array(x, dtype='int32')


Instruction:
How would you explain what the following Python 3 function does
def get_stats(self, i):
        # OrderedDict to always show same order if printed.
        result = OrderedDict()
        result['min'] = np.min(self._counts[i])
        result['max'] = np.max(self._counts[i])
        result['std'] = np.std(self._counts[i])
        result['mean'] = np.mean(self._counts[i])
        return result

Response: Gets the standard statistics for aux_index i.


Instruction:
How would you explain what the following Python 3 function does
def build_embedding_weights(word_index, embeddings_index):
    logger.info('Loading embeddings for all words in the corpus')
    embedding_dim = list(embeddings_index.values())[0].shape[-1]

    # setting special tokens such as UNK and PAD to 0
    # all other words are also set to 0.
    embedding_weights = np.zeros((len(word_index), embedding_dim))

    for word, i in word_index.items():
        word_vector = embeddings_index.get(word)
        if word_vector is not None:
            embedding_weights[i] = word_vector

    return embedding_weights

Response: Builds an embedding matrix for all words in vocab using embeddings_index


Instruction:
Write a Python 3 function that can
build a dictionary with the URL and filename used later on for downloading the file.

Response:
def build_fasttext_wiki_embedding_obj(embedding_type):
    """FastText pre-trained word vectors for 294 languages, with 300 dimensions, trained on Wikipedia. It's recommended to use the same tokenizer for your data that was used to construct the embeddings. It's implemented as 'FasttextWikiTokenizer'. More information: https://fasttext.cc/docs/en/pretrained-vectors.html.

    Args:
        embedding_type: A string in the format `fastext.wiki.$LANG_CODE`. e.g. `fasttext.wiki.de` or `fasttext.wiki.es`
    Returns:
        Object with the URL and filename used later on for downloading the file.
    """
    lang = embedding_type.split('.')[2]
    return {
        'file': 'wiki.{}.vec'.format(lang),
        'url': 'https://dl.fbaipublicfiles.com/fasttext/vectors-wiki/wiki.{}.vec'.format(lang),
        'extract': False,
    }


Instruction:
Can you tell what is the following Python 3 function doing
def build_fasttext_cc_embedding_obj(embedding_type):
    lang = embedding_type.split('.')[2]
    return {
        'file': 'cc.{}.300.vec.gz'.format(lang),
        'url': 'https://dl.fbaipublicfiles.com/fasttext/vectors-crawl/cc.{}.300.vec.gz'.format(lang),
        'extract': False
    }

Response: Build a dictionary with the URL and filename used later on for downloading the file.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_embeddings_index(embedding_type='glove.42B.300d', embedding_dims=None, embedding_path=None, cache=True):

    if embedding_path is not None:
        embedding_type = embedding_path  # identify embedding by path

    embeddings_index = _EMBEDDINGS_CACHE.get(embedding_type)
    if embeddings_index is not None:
        return embeddings_index

    if embedding_path is None:
        embedding_type_obj = get_embedding_type(embedding_type)

        # some very rough wrangling of zip files with the keras util `get_file`
        # a special problem: when multiple files are in one zip file
        extract = embedding_type_obj.get('extract', True)
        file_path = get_file(
            embedding_type_obj['file'], origin=embedding_type_obj['url'], extract=extract, cache_subdir='embeddings', file_hash=embedding_type_obj.get('file_hash',))

        if 'file_in_zip' in embedding_type_obj:
            zip_folder = file_path.split('.zip')[0]
            with ZipFile(file_path, 'r') as zf:
                zf.extractall(zip_folder)
            file_path = os.path.join(
                zip_folder, embedding_type_obj['file_in_zip'])
        else:
            if extract:
                if file_path.endswith('.zip'):
                    file_path = file_path.split('.zip')[0]
                # if file_path.endswith('.gz'):
                #     file_path = file_path.split('.gz')[0]
    else:
        file_path = embedding_path

    embeddings_index = _build_embeddings_index(file_path, embedding_dims)

    if cache:
        _EMBEDDINGS_CACHE[embedding_type] = embeddings_index
    return embeddings_index

Response: Retrieves embeddings index from embedding name or path. Will automatically download and cache as needed.


Instruction:
Can you generate the documentation for the following Python 3 function
def token_generator(self, texts, **kwargs):
        for text_idx, text in enumerate(texts):
            if self.lower:
                text = text.lower()
            for char in text:
                yield text_idx, char

Response: Yields tokens from texts as tuples.


Instruction:
Can you create a Python 3 function that
yields tokens from texts as tuples.

Response:
def token_generator(self, texts, **kwargs):
        """Yields tokens from texts as `(text_idx, sent_idx, character)`

        Args:
            texts: The list of texts.
            **kwargs: Supported args include:
                n_threads/num_threads: Number of threads to use. Uses num_cpus - 1 by default.
                batch_size: The number of texts to accumulate into a common working set before processing.
                    (Default value: 1000)
        """
        # Perf optimization. Only process what is necessary.
        n_threads, batch_size = utils._parse_spacy_kwargs(**kwargs)
        nlp = spacy.load(self.lang)

        kwargs = {
            'batch_size': batch_size,
            'n_threads': n_threads,
            'disable': ['ner']
        }

        # Perf optimization: Lower the entire text instead of individual tokens.
        texts_gen = utils._apply_generator(
            texts, lambda x: x.lower()) if self.lower else texts
        for text_idx, doc in enumerate(nlp.pipe(texts_gen, **kwargs)):
            for sent_idx, sent in enumerate(doc.sents):
                for word in sent:
                    for char in word:
                        yield text_idx, sent_idx, char


Instruction:
Here you have a function in Python 3, explain what it does
def equal_distribution_folds(y, folds=2):
    n, classes = y.shape

    # Compute sample distribution over classes
    dist = y.sum(axis=0).astype('float')
    dist /= dist.sum()

    index_list = []
    fold_dist = np.zeros((folds, classes), dtype='float')
    for _ in range(folds):
        index_list.append([])
    for i in range(n):
        if i < folds:
            target_fold = i
        else:
            normed_folds = fold_dist.T / fold_dist.sum(axis=1)
            how_off = normed_folds.T - dist
            target_fold = np.argmin(
                np.dot((y[i] - .5).reshape(1, -1), how_off.T))
        fold_dist[target_fold] += y[i]
        index_list[target_fold].append(i)

    logger.debug("Fold distributions:")
    logger.debug(fold_dist)
    return index_list

Response: Creates a list of indices that have roughly balanced multi - label distributions.


Instruction:
Create a Python 3 function to
build a model that encodes all words within sentences using token_encoder_model followed by sentence_encoder_model.

Response:
def build_model(self, token_encoder_model, sentence_encoder_model,
                    trainable_embeddings=True, output_activation='softmax'):
        """Builds a model that first encodes all words within sentences using `token_encoder_model`, followed by
        `sentence_encoder_model`.

        Args:
            token_encoder_model: An instance of `SequenceEncoderBase` for encoding tokens within sentences. This model
                will be applied across all sentences to create a sentence encoding.
            sentence_encoder_model: An instance of `SequenceEncoderBase` operating on sentence encoding generated by
                `token_encoder_model`. This encoding is then fed into a final `Dense` layer for classification.
            trainable_embeddings: Whether or not to fine tune embeddings.
            output_activation: The output activation to use. (Default value: 'softmax')
                Use:
                - `softmax` for binary or multi-class.
                - `sigmoid` for multi-label classification.
                - `linear` for regression output.

        Returns:
            The model output tensor.
        """
        if not isinstance(token_encoder_model, SequenceEncoderBase):
            raise ValueError("`token_encoder_model` should be an instance of `{}`".format(
                SequenceEncoderBase))
        if not isinstance(sentence_encoder_model, SequenceEncoderBase):
            raise ValueError("`sentence_encoder_model` should be an instance of `{}`".format(
                SequenceEncoderBase))

        if not sentence_encoder_model.allows_dynamic_length() and self.max_sents is None:
            raise ValueError("Sentence encoder model '{}' requires padding. "
                             "You need to provide `max_sents`")

        if self.embeddings_index is None:
            # The +1 is for unknown token index 0.
            embedding_layer = Embedding(len(self.token_index),
                                        self.embedding_dims,
                                        input_length=self.max_tokens,
                                        mask_zero=token_encoder_model.allows_dynamic_length(),
                                        trainable=trainable_embeddings)
        else:
            embedding_layer = Embedding(len(self.token_index),
                                        self.embedding_dims,
                                        weights=[build_embedding_weights(
                                            self.token_index, self.embeddings_index)],
                                        input_length=self.max_tokens,
                                        mask_zero=token_encoder_model.allows_dynamic_length(),
                                        trainable=trainable_embeddings)

        word_input = Input(shape=(self.max_tokens,), dtype='int32')
        x = embedding_layer(word_input)
        word_encoding = token_encoder_model(x)
        token_encoder_model = Model(
            word_input, word_encoding, name='word_encoder')

        doc_input = Input(
            shape=(self.max_sents, self.max_tokens), dtype='int32')
        sent_encoding = TimeDistributed(token_encoder_model)(doc_input)
        x = sentence_encoder_model(sent_encoding)

        x = Dense(self.num_classes, activation=output_activation)(x)
        return Model(doc_input, x)


Instruction:
Can you write a function in Python 3 where it
processes text and save as Dataset

Response:
def process_save(X, y, tokenizer, proc_data_path, max_len=400, train=False, ngrams=None, limit_top_tokens=None):
    """Process text and save as Dataset
    """
    if train and limit_top_tokens is not None:
        tokenizer.apply_encoding_options(limit_top_tokens=limit_top_tokens)

    X_encoded = tokenizer.encode_texts(X)

    if ngrams is not None:
        X_encoded = tokenizer.add_ngrams(X_encoded, n=ngrams, train=train)

    X_padded = tokenizer.pad_sequences(
        X_encoded, fixed_token_seq_length=max_len)

    if train:
        ds = Dataset(X_padded,
                     y, tokenizer=tokenizer)
    else:
        ds = Dataset(X_padded, y)

    ds.save(proc_data_path)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def split_data(X, y, ratio=(0.8, 0.1, 0.1)):
    assert(sum(ratio) == 1 and len(ratio) == 3)
    X_train, X_rest, y_train, y_rest = train_test_split(
        X, y, train_size=ratio[0])
    X_val, X_test, y_val, y_test = train_test_split(
        X_rest, y_rest, train_size=ratio[1])
    return X_train, X_val, X_test, y_train, y_val, y_test

Response: Splits data into training validation and test set.


Instruction:
Here you have a function in Python 3, explain what it does
def load_data_split(proc_data_dir):
    ds_train = Dataset.load(path.join(proc_data_dir, 'train.bin'))
    ds_val = Dataset.load(path.join(proc_data_dir, 'val.bin'))
    ds_test = Dataset.load(path.join(proc_data_dir, 'test.bin'))
    return ds_train, ds_val, ds_test

Response: Loads a split dataset


Instruction:
Explain what the following Python 3 code does
def build_model(self, token_encoder_model, trainable_embeddings=True, output_activation='softmax'):
        if not isinstance(token_encoder_model, SequenceEncoderBase):
            raise ValueError("`token_encoder_model` should be an instance of `{}`".format(
                SequenceEncoderBase))

        if not token_encoder_model.allows_dynamic_length() and self.max_tokens is None:
            raise ValueError("The provided `token_encoder_model` does not allow variable length mini-batches. "
                             "You need to provide `max_tokens`")

        if self.embeddings_index is None:
            # The +1 is for unknown token index 0.
            embedding_layer = Embedding(len(self.token_index),
                                        self.embedding_dims,
                                        input_length=self.max_tokens,
                                        mask_zero=token_encoder_model.allows_dynamic_length(),
                                        trainable=trainable_embeddings)
        else:
            embedding_layer = Embedding(len(self.token_index),
                                        self.embedding_dims,
                                        weights=[build_embedding_weights(
                                            self.token_index, self.embeddings_index)],
                                        input_length=self.max_tokens,
                                        mask_zero=token_encoder_model.allows_dynamic_length(),
                                        trainable=trainable_embeddings)

        sequence_input = Input(shape=(self.max_tokens,), dtype='int32')
        x = embedding_layer(sequence_input)
        x = token_encoder_model(x)
        x = Dense(self.num_classes, activation=output_activation)(x)
        return Model(sequence_input, x)

Response: Builds a model for classification.


Instruction:
Create a Python 3 function to
compute the softmax along a specified dim.

Response:
def _softmax(x, dim):
    """Computes softmax along a specified dim. Keras currently lacks this feature.
    """

    if K.backend() == 'tensorflow':
        import tensorflow as tf
        return tf.nn.softmax(x, dim)
    elif K.backend() is 'cntk':
        import cntk
        return cntk.softmax(x, dim)
    elif K.backend() == 'theano':
        # Theano cannot softmax along an arbitrary dim.
        # So, we will shuffle `dim` to -1 and un-shuffle after softmax.
        perm = np.arange(K.ndim(x))
        perm[dim], perm[-1] = perm[-1], perm[dim]
        x_perm = K.permute_dimensions(x, perm)
        output = K.softmax(x_perm)

        # Permute back
        perm[dim], perm[-1] = perm[-1], perm[dim]
        output = K.permute_dimensions(x, output)
        return output
    else:
        raise ValueError("Backend '{}' not supported".format(K.backend()))


Instruction:
Write a Python 3 script for
applying various filtering and processing options on token.

Response:
def _apply_options(self, token):
        """Applies various filtering and processing options on token.

        Returns:
            The processed token. None if filtered.
        """
        # Apply work token filtering.
        if token.is_punct and self.remove_punct:
            return None
        if token.is_stop and self.remove_stop_words:
            return None
        if token.is_digit and self.remove_digits:
            return None
        if token.is_oov and self.exclude_oov:
            return None
        if token.pos_ in self.exclude_pos_tags:
            return None
        if token.ent_type_ in self.exclude_entities:
            return None

        # Lemmatized ones are already lowered.
        if self.lemmatize:
            return token.lemma_
        if self.lower:
            return token.lower_
        return token.orth_


Instruction:
How would you code a function in Python 3 to
yield tokens from texts as tuples.

Response:
def token_generator(self, texts, **kwargs):
        """Yields tokens from texts as `(text_idx, word)`

        Args:
            texts: The list of texts.
            **kwargs: Supported args include:
                n_threads/num_threads: Number of threads to use. Uses num_cpus - 1 by default.
                batch_size: The number of texts to accumulate into a common working set before processing.
                    (Default value: 1000)
        """
        # Perf optimization. Only process what is necessary.
        n_threads, batch_size = utils._parse_spacy_kwargs(**kwargs)
        nlp = spacy.load(self.lang)

        disabled = ['parser']
        if len(self.exclude_entities) > 0:
            disabled.append('ner')

        kwargs = {
            'batch_size': batch_size,
            'n_threads': n_threads,
            'disable': disabled
        }

        for text_idx, doc in enumerate(nlp.pipe(texts, **kwargs)):
            for word in doc:
                processed_word = self._apply_options(word)
                if processed_word is not None:
                    yield text_idx, processed_word


Instruction:
How would you code a function in Python 3 to
add value to list indexed by indices. Will create sub lists as required.

Response:
def _append(lst, indices, value):
    """Adds `value` to `lst` list indexed by `indices`. Will create sub lists as required.
    """
    for i, idx in enumerate(indices):
        # We need to loop because sometimes indices can increment by more than 1 due to missing tokens.
        # Example: Sentence with no words after filtering words.
        while len(lst) <= idx:
            # Update max counts whenever a new sublist is created.
            # There is no need to worry about indices beyond `i` since they will end up creating new lists as well.
            lst.append([])
        lst = lst[idx]

    # Add token and update token max count.
    lst.append(value)


Instruction:
Write a Python 3 script to
parse the spacy kwargs and return the number of threads and batch size.

Response:
def _parse_spacy_kwargs(**kwargs):
    """Supported args include:

    Args:
        n_threads/num_threads: Number of threads to use. Uses num_cpus - 1 by default.
        batch_size: The number of texts to accumulate into a common working set before processing.
            (Default value: 1000)
    """
    n_threads = kwargs.get('n_threads') or kwargs.get('num_threads')
    batch_size = kwargs.get('batch_size')

    if n_threads is None or n_threads is -1:
        n_threads = cpu_count() - 1
    if batch_size is None or batch_size is -1:
        batch_size = 1000
    return n_threads, batch_size


Instruction:
Can you tell what is the following Python 3 function doing
def update(self, indices):
        # Initialize various lists for the first time based on length of indices.
        if self._prev_indices is None:
            self._prev_indices = indices

            # +1 to track token counts in the last index.
            self._local_counts = np.full(len(indices) + 1, 1)
            self._local_counts[-1] = 0
            self.counts = [[] for _ in range(len(self._local_counts))]

        has_reset = False
        for i in range(len(indices)):
            # index value changed. Push all local values beyond i to count and reset those local_counts.
            # For example, if document index changed, push counts on sentences and tokens and reset their local_counts
            # to indicate that we are tracking those for new document. We need to do this at all document hierarchies.
            if indices[i] > self._prev_indices[i]:
                self._local_counts[i] += 1
                has_reset = True
                for j in range(i + 1, len(self.counts)):
                    self.counts[j].append(self._local_counts[j])
                    self._local_counts[j] = 1

        # If none of the aux indices changed, update token count.
        if not has_reset:
            self._local_counts[-1] += 1
        self._prev_indices = indices[:]

Response:
Updates the counts based on indices. The algorithm tracks the index change at i and
        update global counts for all indices beyond i with local counts tracked so far.


Instruction:
Write a Python 3 script to
read text files in directory and returns them as array

Response:
def read_folder(directory):
    """read text files in directory and returns them as array

    Args:
        directory: where the text files are

    Returns:
        Array of text
    """
    res = []
    for filename in os.listdir(directory):
        with io.open(os.path.join(directory, filename), encoding="utf-8") as f:
            content = f.read()
            res.append(content)
    return res


Instruction:
Can you create a Python 3 function that
returns array with positive and negative examples

Response:
def read_pos_neg_data(path, folder, limit):
    """returns array with positive and negative examples"""
    training_pos_path = os.path.join(path, folder, 'pos')
    training_neg_path = os.path.join(path, folder, 'neg')

    X_pos = read_folder(training_pos_path)
    X_neg = read_folder(training_neg_path)

    if limit is None:
        X = X_pos + X_neg
    else:
        X = X_pos[:limit] + X_neg[:limit]

    y = [1] * int(len(X) / 2) + [0] * int(len(X) / 2)

    return X, y


Instruction:
Create a Python 3 function to
download and caches IMDB Moview Reviews. 25k training data 25k test data 25k test data

Response:
def imdb(limit=None, shuffle=True):
    """Downloads (and caches) IMDB Moview Reviews. 25k training data, 25k test data

    Args:
        limit: get only first N items for each class

    Returns:
        [X_train, y_train, X_test, y_test]
    """

    movie_review_url = 'http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz'

    # download and extract, thus remove the suffix '.tar.gz'
    path = keras.utils.get_file(
        'aclImdb.tar.gz', movie_review_url, extract=True)[:-7]

    X_train, y_train = read_pos_neg_data(path, 'train', limit)
    X_test, y_test = read_pos_neg_data(path, 'test', limit)

    if shuffle:
        X_train, y_train = sklearn.utils.shuffle(X_train, y_train)
        X_test, y_test = sklearn.utils.shuffle(X_test, y_test)

    return X_train, X_test, y_train, y_test


Instruction:
Can you tell what is the following Python 3 function doing
def to_absolute(self, x, y):
        return x + self.size/2, y + self.size/2

Response: Converts coordinates provided with reference to the center of the canvas object to absolute coordinates which are used by the canvas object.


Instruction:
Can you tell what is the following Python 3 function doing
def set_value(self, number: (float, int)):
        self.canvas.delete('all')
        self.canvas.create_image(0, 0, image=self.image, anchor='nw')

        number = number if number <= self.max_value else self.max_value
        number = 0.0 if number < 0.0 else number

        radius = 0.9 * self.size/2.0
        angle_in_radians = (2.0 * cmath.pi / 3.0) \
            + number / self.max_value * (5.0 * cmath.pi / 3.0)

        center = cmath.rect(0, 0)
        outer = cmath.rect(radius, angle_in_radians)
        if self.needle_thickness == 0:
            line_width = int(5 * self.size / 200)
            line_width = 1 if line_width < 1 else line_width
        else:
            line_width = self.needle_thickness

        self.canvas.create_line(
            *self.to_absolute(center.real, center.imag),
            *self.to_absolute(outer.real, outer.imag),
            width=line_width,
            fill=self.needle_color
        )

        self.readout['text'] = '{}{}'.format(number, self.unit)

Response: Sets the value of the graphic containing the specified number.


Instruction:
Can you generate the documentation for the following Python 3 function
def _draw_background(self, divisions=10):
        self.canvas.create_arc(2, 2, self.size-2, self.size-2,
                               style=tk.PIESLICE, start=-60, extent=30,
                               fill='red')
        self.canvas.create_arc(2, 2, self.size-2, self.size-2,
                               style=tk.PIESLICE, start=-30, extent=60,
                               fill='yellow')
        self.canvas.create_arc(2, 2, self.size-2, self.size-2,
                               style=tk.PIESLICE, start=30, extent=210,
                               fill='green')

        # find the distance between the center and the inner tick radius
        inner_tick_radius = int(self.size * 0.4)
        outer_tick_radius = int(self.size * 0.5)

        for tick in range(divisions):
            angle_in_radians = (2.0 * cmath.pi / 3.0) \
                               + tick/divisions * (5.0 * cmath.pi / 3.0)
            inner_point = cmath.rect(inner_tick_radius, angle_in_radians)
            outer_point = cmath.rect(outer_tick_radius, angle_in_radians)

            self.canvas.create_line(
                *self.to_absolute(inner_point.real, inner_point.imag),
                *self.to_absolute(outer_point.real, outer_point.imag),
                width=1
            )

Response: Draw the background of the dialtonical time area.


Instruction:
Given the following Python 3 function, write the documentation
def draw_axes(self):
        self.canvas.delete('all')
        rect = 50, 50, self.w - 50, self.h - 50

        self.canvas.create_rectangle(rect, outline="black")

        for x in self.frange(0, self.x_max - self.x_min + 1, self.x_tick):
            value = Decimal(self.x_min + x)
            if self.x_min <= value <= self.x_max:
                x_step = (self.px_x * x) / self.x_tick
                coord = 50 + x_step, self.h - 50, 50 + x_step, self.h - 45
                self.canvas.create_line(coord, fill="black")
                coord = 50 + x_step, self.h - 40

                label = round(Decimal(self.x_min + x), 1)
                self.canvas.create_text(coord, fill="black", text=label)

        for y in self.frange(0, self.y_max - self.y_min + 1, self.y_tick):
            value = Decimal(self.y_max - y)

            if self.y_min <= value <= self.y_max:
                y_step = (self.px_y * y) / self.y_tick
                coord = 45, 50 + y_step, 50, 50 + y_step
                self.canvas.create_line(coord, fill="black")
                coord = 35, 50 + y_step

                label = round(value, 1)
                self.canvas.create_text(coord, fill="black", text=label)

Response: Draws the axes for the current series.


Instruction:
How would you explain what the following Python 3 function does
def plot_point(self, x, y, visible=True, color='black', size=5):
        xp = (self.px_x * (x - self.x_min)) / self.x_tick
        yp = (self.px_y * (self.y_max - y)) / self.y_tick
        coord = 50 + xp, 50 + yp

        if visible:
            # divide down to an appropriate size
            size = int(size/2) if int(size/2) > 1 else 1
            x, y = coord

            self.canvas.create_oval(
                x-size, y-size,
                x+size, y+size,
                fill=color
            )

        return coord

Response: Plots a single point on the grid.


Instruction:
How would you explain what the following Python 3 function does
def plot_line(self, points: list, color='black', point_visibility=False):
        last_point = ()
        for point in points:
            this_point = self.plot_point(point[0], point[1],
                                         color=color, visible=point_visibility)

            if last_point:
                self.canvas.create_line(last_point + this_point, fill=color)
            last_point = this_point

Response: Plot a line of points with the given color.


Instruction:
Can you tell what is the following Python 3 function doing
def frange(start, stop, step, digits_to_round=3):
        while start < stop:
            yield round(start, digits_to_round)
            start += step

Response: A generator that yields the value of the key - value pair in the range [ start stop ).


Instruction:
How would you code a function in Python 3 to
load a new image.

Response:
def _load_new(self, img_data: str):
        """
        Load a new image.

        :param img_data: the image data as a base64 string
        :return: None
        """
        self._image = tk.PhotoImage(data=img_data)
        self._image = self._image.subsample(int(200 / self._size),
                                            int(200 / self._size))
        self._canvas.delete('all')
        self._canvas.create_image(0, 0, image=self._image, anchor='nw')

        if self._user_click_callback is not None:
            self._user_click_callback(self._on)


Instruction:
How would you explain what the following Python 3 function does
def to_grey(self, on: bool=False):
        self._on = False
        self._load_new(led_grey)

Response: Change the LED to grey.


Instruction:
Make a summary of the following Python 3 code
def to_green(self, on: bool=False):
        self._on = on
        if on:
            self._load_new(led_green_on)

            if self._toggle_on_click:
                self._canvas.bind('<Button-1>', lambda x: self.to_green(False))
        else:
            self._load_new(led_green)

            if self._toggle_on_click:
                self._canvas.bind('<Button-1>', lambda x: self.to_green(True))

Response: Change the LED to green.


Instruction:
Implement a function in Python 3 to
change the LED to red.

Response:
def to_red(self, on: bool=False):
        """
        Change the LED to red (on or off)
        :param on: True or False
        :return: None
        """
        self._on = on
        if on:
            self._load_new(led_red_on)

            if self._toggle_on_click:
                self._canvas.bind('<Button-1>', lambda x: self.to_red(False))
        else:
            self._load_new(led_red)

            if self._toggle_on_click:
                self._canvas.bind('<Button-1>', lambda x: self.to_red(True))


Instruction:
Here you have a function in Python 3, explain what it does
def to_yellow(self, on: bool=False):
        self._on = on
        if on:
            self._load_new(led_yellow_on)

            if self._toggle_on_click:
                self._canvas.bind('<Button-1>',
                                  lambda x: self.to_yellow(False))
        else:
            self._load_new(led_yellow)

            if self._toggle_on_click:
                self._canvas.bind('<Button-1>',
                                  lambda x: self.to_yellow(True))

Response: Change the LED to yellow.


Instruction:
Can you generate the documentation for the following Python 3 function
def _redraw(self):
        for row in self._rows:
            for widget in row:
                widget.grid_forget()

        offset = 0 if not self.headers else 1
        for i, row in enumerate(self._rows):
            for j, widget in enumerate(row):
                widget.grid(row=i+offset, column=j)

Response: Forgets the current layout and redraws with the most recent information


Instruction:
Can you generate a brief explanation for the following Python 3 code
def remove_row(self, row_number: int=-1):
        if len(self._rows) == 0:
            return

        row = self._rows.pop(row_number)
        for widget in row:
            widget.destroy()

Response: Removes a specified row of data


Instruction:
Given the following Python 3 function, write the documentation
def add_row(self, data: list):
        # validation
        if self.headers:
            if len(self.headers) != len(data):
                raise ValueError

        if len(data) != self.num_of_columns:
            raise ValueError

        offset = 0 if not self.headers else 1
        row = list()
        for i, element in enumerate(data):
            label = ttk.Label(self, text=str(element), relief=tk.GROOVE,
                              padding=self.padding)
            label.grid(row=len(self._rows) + offset, column=i, sticky='E,W')
            row.append(label)

        self._rows.append(row)

Response: Adds a row of data to the current widget.


Instruction:
Write a Python 3 script to
add a row of data to the current widget and sets the focus at the beginning of the next row.

Response:
def add_row(self, data: list=None):
        """
        Add a row of data to the current widget, add a <Tab> \
        binding to the last element of the last row, and set \
        the focus at the beginning of the next row.

        :param data: a row of data
        :return: None
        """
        # validation
        if self.headers and data:
            if len(self.headers) != len(data):
                raise ValueError

        offset = 0 if not self.headers else 1
        row = list()

        if data:
            for i, element in enumerate(data):
                contents = '' if element is None else str(element)
                entry = ttk.Entry(self)
                entry.insert(0, contents)
                entry.grid(row=len(self._rows) + offset,
                           column=i,
                           sticky='E,W')
                row.append(entry)
        else:
            for i in range(self.num_of_columns):
                entry = ttk.Entry(self)
                entry.grid(row=len(self._rows) + offset,
                           column=i,
                           sticky='E,W')
                row.append(entry)

        self._rows.append(row)

        # clear all bindings
        for row in self._rows:
            for widget in row:
                widget.unbind('<Tab>')

        def add(e):
            self.add_row()

        last_entry = self._rows[-1][-1]
        last_entry.bind('<Tab>', add)

        e = self._rows[-1][0]
        e.focus_set()

        self._redraw()


Instruction:
Write a Python 3 function that can
read the data contained in all entries as a list of dicts with the headers as the dictionary keys

Response:
def _read_as_dict(self):
        """
        Read the data contained in all entries as a list of
        dictionaries with the headers as the dictionary keys

        :return: list of dicts containing all tabular data
        """
        data = list()
        for row in self._rows:
            row_data = OrderedDict()
            for i, header in enumerate(self.headers):
                row_data[header.cget('text')] = row[i].get()

            data.append(row_data)

        return data


Instruction:
Create a Python 3 function for
reading the data contained in all entries as a list of dicts containing all of the data

Response:
def _read_as_table(self):
        """
        Read the data contained in all entries as a list of
        lists containing all of the data

        :return: list of dicts containing all tabular data
        """
        rows = list()

        for row in self._rows:
            rows.append([row[i].get() for i in range(self.num_of_columns)])

        return rows


Instruction:
Make a summary of the following Python 3 code
def add_row(self, data: list):

        # validation
        if self.headers and data:
            if len(self.headers) != len(data):
                raise ValueError

        offset = 0 if not self.headers else 1
        row = list()

        for i, e in enumerate(data):
            if not isinstance(e, tuple):
                raise ValueError('all elements must be a tuple '
                                 'consisting of ("label", <command>)')

            label, command = e
            button = tk.Button(self, text=str(label), relief=tk.RAISED,
                               command=command,
                               padx=self.padding,
                               pady=self.padding)

            button.grid(row=len(self._rows) + offset, column=i, sticky='ew')
            row.append(button)

        self._rows.append(row)

Response: Adds a row of buttons each with their own callbacks to the current widget.


Instruction:
Write a Python 3 function that can
add a row to the list and re - draw as necessary.

Response:
def add_row(self, key: str, default: str=None,
                unit_label: str=None, enable: bool=None):
        """
        Add a single row and re-draw as necessary

        :param key: the name and dict accessor
        :param default: the default value
        :param unit_label: the label that should be \
        applied at the right of the entry
        :param enable: the 'enabled' state (defaults to True)
        :return:
        """
        self.keys.append(ttk.Label(self, text=key))

        self.defaults.append(default)
        self.unit_labels.append(
            ttk.Label(self, text=unit_label if unit_label else '')
        )
        self.enables.append(enable)
        self.values.append(ttk.Entry(self))

        row_offset = 1 if self.title is not None else 0

        for i in range(len(self.keys)):
            self.keys[i].grid_forget()

            self.keys[i].grid(row=row_offset, column=0, sticky='e')
            self.values[i].grid(row=row_offset, column=1)

            if self.unit_labels[i]:
                self.unit_labels[i].grid(row=row_offset, column=3, sticky='w')

            if self.defaults[i]:
                self.values[i].config(state=tk.NORMAL)
                self.values[i].delete(0, tk.END)
                self.values[i].insert(0, self.defaults[i])

            if self.enables[i] in [True, None]:
                self.values[i].config(state=tk.NORMAL)
            elif self.enables[i] is False:
                self.values[i].config(state=tk.DISABLED)

            row_offset += 1

            # strip <Return> and <Tab> bindings, add callbacks to all entries
            self.values[i].unbind('<Return>')
            self.values[i].unbind('<Tab>')

            if self.callback is not None:
                def callback(event):
                    self.callback()

                self.values[i].bind('<Return>', callback)
                self.values[i].bind('<Tab>', callback)


Instruction:
How would you code a function in Python 3 to
reset all entries in the list to their default values.

Response:
def reset(self):
        """
        Clears all entries.

        :return: None
        """
        for i in range(len(self.values)):
            self.values[i].delete(0, tk.END)

            if self.defaults[i] is not None:
                self.values[i].insert(0, self.defaults[i])


Instruction:
Can you generate a brief explanation for the following Python 3 code
def change_enables(self, enables_list: list):
        for i, entry in enumerate(self.values):
            if enables_list[i]:
                entry.config(state=tk.NORMAL)
            else:
                entry.config(state=tk.DISABLED)

Response: Change the enable states of all key - related items in the archive.


Instruction:
Explain what the following Python 3 code does
def load(self, data: dict):
        for i, label in enumerate(self.keys):
            key = label.cget('text')
            if key in data.keys():
                entry_was_enabled = True if \
                    self.values[i].cget('state') == 'normal' else False
                if not entry_was_enabled:
                    self.values[i].config(state='normal')

                self.values[i].delete(0, tk.END)
                self.values[i].insert(0, str(data[key]))

                if not entry_was_enabled:
                    self.values[i].config(state='disabled')

Response: Load values into the key - value store via dict.


Instruction:
Can you implement a function in Python 3 that
retrieves the GUI elements for program use.

Response:
def get(self):
        """
        Retrieve the GUI elements for program use.

        :return: a dictionary containing all \
        of the data from the key/value entries
        """
        data = dict()
        for label, entry in zip(self.keys, self.values):
            data[label.cget('text')] = entry.get()

        return data


Instruction:
How would you implement a function in Python 3 that
calls when the calendar is clicked.

Response:
def _pressed(self, evt):
        """
        Clicked somewhere in the calendar.
        """
        x, y, widget = evt.x, evt.y, evt.widget
        item = widget.identify_row(y)
        column = widget.identify_column(x)

        if not column or not (item in self._items):
            # clicked in the weekdays row or just outside the columns
            return

        item_values = widget.item(item)['values']
        if not len(item_values):  # row is empty for this month
            return

        text = item_values[int(column[1]) - 1]
        if not text:  # date is empty
            return

        bbox = widget.bbox(item, column)
        if not bbox:  # calendar not visible yet
            return

        # update and then show selection
        text = '%02d' % text
        self._selection = (text, item, column)
        self._show_selection(text, bbox)

        if self.callback is not None:
            self.callback()


Instruction:
Implement a Python 3 function for
adding the contents of string to the entry field and clears the contents of the entry field and inserts the contents of string.

Response:
def add(self, string: (str, list)):
        """
        Clear the contents of the entry field and
        insert the contents of string.

        :param string: an str containing the text to display
        :return:
        """
        if len(self._entries) == 1:
            self._entries[0].delete(0, 'end')
            self._entries[0].insert(0, string)
        else:
            if len(string) != len(self._entries):
                raise ValueError('the "string" list must be '
                                 'equal to the number of entries')

            for i, e in enumerate(self._entries):
                self._entries[i].delete(0, 'end')
                self._entries[i].insert(0, string[i])


Instruction:
Can you implement a function in Python 3 that
deletes itself. :return: None

Response:
def remove(self):
        """
        Deletes itself.
        :return: None
        """
        for e in self._entries:
            e.grid_forget()
            e.destroy()

        self._remove_btn.grid_forget()
        self._remove_btn.destroy()

        self.deleted = True

        if self._remove_callback:
            self._remove_callback()


Instruction:
Create a Python 3 function for
returning the value for the slot.

Response:
def get(self):
        """
        Returns the value for the slot.
        :return: the entry value
        """
        values = [e.get() for e in self._entries]
        if len(self._entries) == 1:
            return values[0]
        else:
            return values


Instruction:
How would you code a function in Python 3 to
clear the current layout and re - draw all elements in self. _slots

Response:
def _redraw(self):
        """
        Clears the current layout and re-draws all elements in self._slots
        :return:
        """
        if self._blank_label:
            self._blank_label.grid_forget()
            self._blank_label.destroy()
            self._blank_label = None

        for slot in self._slots:
            slot.grid_forget()

        self._slots = [slot for slot in self._slots if not slot.deleted]

        max_per_col = 8
        for i, slot in enumerate(self._slots):
            slot.grid(row=i % max_per_col,
                      column=int(i / max_per_col), sticky='ew')


Instruction:
Make a summary of the following Python 3 code
def add(self, string: (str, list)):
        slot = _SlotFrame(self,
                          remove_callback=self._redraw,
                          entries=self._slot_columns)
        slot.add(string)

        self._slots.append(slot)

        self._redraw()

Response: Adds a new slot to the multi - frame containing the string.


Instruction:
Here you have a function in Python 3, explain what it does
def clear(self):
        for slot in self._slots:
            slot.grid_forget()
            slot.destroy()

        self._slots = []

Response: Clear out the multi - frame cache.


Instruction:
Create a Python 3 function to
clear the current color of all the active frames.

Response:
def clear(self):
        """
        Clear the segment.
        :return: None
        """
        for _, frame in self._segments.items():
            frame.configure(background=self._bg_color)


Instruction:
Given the following Python 3 function, write the documentation
def set_value(self, value: str):

        self.clear()

        if '.' in value:
            self._segments['period'].configure(background=self._color)

        if value in ['0', '0.']:
            self._segments['a'].configure(background=self._color)
            self._segments['b'].configure(background=self._color)
            self._segments['c'].configure(background=self._color)
            self._segments['d'].configure(background=self._color)
            self._segments['e'].configure(background=self._color)
            self._segments['f'].configure(background=self._color)
        elif value in ['1', '1.']:
            self._segments['b'].configure(background=self._color)
            self._segments['c'].configure(background=self._color)
        elif value in ['2', '2.']:
            self._segments['a'].configure(background=self._color)
            self._segments['b'].configure(background=self._color)
            self._segments['g'].configure(background=self._color)
            self._segments['e'].configure(background=self._color)
            self._segments['d'].configure(background=self._color)
        elif value in ['3', '3.']:
            self._segments['a'].configure(background=self._color)
            self._segments['b'].configure(background=self._color)
            self._segments['g'].configure(background=self._color)
            self._segments['c'].configure(background=self._color)
            self._segments['d'].configure(background=self._color)
        elif value in ['4', '4.']:
            self._segments['f'].configure(background=self._color)
            self._segments['g'].configure(background=self._color)
            self._segments['b'].configure(background=self._color)
            self._segments['c'].configure(background=self._color)
        elif value in ['5', '5.']:
            self._segments['a'].configure(background=self._color)
            self._segments['f'].configure(background=self._color)
            self._segments['g'].configure(background=self._color)
            self._segments['c'].configure(background=self._color)
            self._segments['d'].configure(background=self._color)
        elif value in ['6', '6.']:
            self._segments['f'].configure(background=self._color)
            self._segments['g'].configure(background=self._color)
            self._segments['c'].configure(background=self._color)
            self._segments['d'].configure(background=self._color)
            self._segments['e'].configure(background=self._color)
        elif value in ['7', '7.']:
            self._segments['a'].configure(background=self._color)
            self._segments['b'].configure(background=self._color)
            self._segments['c'].configure(background=self._color)
        elif value in ['8', '8.']:
            self._segments['a'].configure(background=self._color)
            self._segments['b'].configure(background=self._color)
            self._segments['c'].configure(background=self._color)
            self._segments['d'].configure(background=self._color)
            self._segments['e'].configure(background=self._color)
            self._segments['f'].configure(background=self._color)
            self._segments['g'].configure(background=self._color)
        elif value in ['9', '9.']:
            self._segments['a'].configure(background=self._color)
            self._segments['b'].configure(background=self._color)
            self._segments['c'].configure(background=self._color)
            self._segments['f'].configure(background=self._color)
            self._segments['g'].configure(background=self._color)
        elif value in ['-']:
            self._segments['g'].configure(background=self._color)

        else:
            raise ValueError('unsupported character: {}'.format(value))

Response: Sets the value of the 7 - segment display.


Instruction:
How would you explain what the following Python 3 function does
def _group(self, value: str):
        reversed_v = value[::-1]

        parts = []

        has_period = False
        for c in reversed_v:
            if has_period:
                parts.append(c + '.')
                has_period = False
            elif c == '.':
                has_period = True
            else:
                parts.append(c)

        parts = parts[:len(self._digits)]

        return parts

Response: Takes a string and groups it appropriately with any appropriate punctuation so that it is a log entry.


Instruction:
How would you implement a function in Python 3 that
sets the displayed digits based on the value string.

Response:
def set_value(self, value: str):
        """
        Sets the displayed digits based on the value string.
        :param value: a string containing an integer or float value
        :return: None
        """
        [digit.clear() for digit in self._digits]

        grouped = self._group(value)  # return the parts, reversed
        digits = self._digits[::-1]  # reverse the digits

        # fill from right to left
        has_period = False
        for i, digit_value in enumerate(grouped):
            try:
                if has_period:
                    digits[i].set_value(digit_value + '.')
                    has_period = False

                elif grouped[i] == '.':
                    has_period = True

                else:
                    digits[i].set_value(digit_value)
            except IndexError:
                raise ValueError('the value "{}" contains too '
                                 'many digits'.format(value))


Instruction:
Implement a function in Python 3 to
add a callback on change

Response:
def add_callback(self, callback: callable):
        """
        Add a callback on change

        :param callback: callable function
        :return: None
        """
        def internal_callback(*args):
            try:
                callback()
            except TypeError:
                callback(self.get())

        self._var.trace('w', internal_callback)


Instruction:
Explain what the following Python 3 code does
def set(self, value: int):
        max_value = int(''.join(['1' for _ in range(self._bit_width)]), 2)

        if value > max_value:
            raise ValueError('the value {} is larger than '
                             'the maximum value {}'.format(value, max_value))

        self._value = value
        self._text_update()

Response: Set the current value of the current locale.


Instruction:
How would you code a function in Python 3 to
return the bit value at the specified position

Response:
def get_bit(self, position: int):
        """
        Returns the bit value at position

        :param position: integer between 0 and <width>, inclusive
        :return: the value at position as a integer
        """

        if position > (self._bit_width - 1):
            raise ValueError('position greater than the bit width')

        if self._value & (1 << position):
            return 1
        else:
            return 0


Instruction:
Write a Python 3 function for
toggling the value at position

Response:
def toggle_bit(self, position: int):
        """
        Toggles the value at position

        :param position: integer between 0 and 7, inclusive
        :return: None
        """
        if position > (self._bit_width - 1):
            raise ValueError('position greater than the bit width')

        self._value ^= (1 << position)
        self._text_update()


Instruction:
How would you implement a function in Python 3 that
sets the value at the specified position in the bit string.

Response:
def set_bit(self, position: int):
        """
        Sets the value at position

        :param position: integer between 0 and 7, inclusive
        :return: None
        """
        if position > (self._bit_width - 1):
            raise ValueError('position greater than the bit width')

        self._value |= (1 << position)
        self._text_update()


Instruction:
Write a Python 3 script for
clearing the bit at the specified position.

Response:
def clear_bit(self, position: int):
        """
        Clears the value at position

        :param position: integer between 0 and 7, inclusive
        :return: None
        """
        if position > (self._bit_width - 1):
            raise ValueError('position greater than the bit width')

        self._value &= ~(1 << position)
        self._text_update()


Instruction:
Can you create a Python 3 function that
populates this list by calling populate but only once.

Response:
def _populate(self):
        """ Populate this list by calling populate(), but only once. """
        if not self._populated:
            logging.debug("Populating lazy list %d (%s)" % (id(self), self.__class__.__name__))
            try:
                self.populate()
                self._populated = True
            except Exception, e:
                logging.debug("Currently unable to populate lazy list: %s" % e)


Instruction:
Given the following Python 3 function, write the documentation
def _register_admin(admin_site, model, admin_class):
    try:
        admin_site.register(model, admin_class)
    except admin.sites.AlreadyRegistered:
        pass

Response: Register model in the admin.


Instruction:
Can you write a function in Python 3 where it
returns a list of core choice fields.

Response:
def core_choice_fields(metadata_class):
    """ If the 'optional' core fields (_site and _language) are required,
        list them here.
    """
    fields = []
    if metadata_class._meta.use_sites:
        fields.append('_site')
    if metadata_class._meta.use_i18n:
        fields.append('_language')
    return fields


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _monkey_inline(model, admin_class_instance, metadata_class, inline_class, admin_site):
    if model in metadata_class._meta.seo_models:
        # *Not* adding to the class attribute "inlines", as this will affect
        # all instances from this class. Explicitly adding to instance attribute.
        admin_class_instance.__dict__['inlines'] = admin_class_instance.inlines + [inline_class]

        # Because we've missed the registration, we need to perform actions
        # that were done then (on admin class instantiation)
        inline_instance = inline_class(admin_class_instance.model, admin_site)
        admin_class_instance.inline_instances.append(inline_instance)

Response: Monkey patch the inline onto the given admin_class instance.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _with_inline(func, admin_site, metadata_class, inline_class):

    def register(model_or_iterable, admin_class=None, **options):
        # Call the (bound) function we were given.
        # We have to assume it will be bound to admin_site
        func(model_or_iterable, admin_class, **options)
        _monkey_inline(model_or_iterable, admin_site._registry[model_or_iterable], metadata_class, inline_class, admin_site)

    return register

Response: Decorator for register function that adds an appropriate inline.


Instruction:
Can you tell what is the following Python 3 function doing
def get_linked_metadata(obj, name=None, context=None, site=None, language=None):
    # XXX Check that 'modelinstance' and 'model' metadata are installed in backends
    # I believe that get_model() would return None if not
    Metadata = _get_metadata_model(name)
    InstanceMetadata = Metadata._meta.get_model('modelinstance')
    ModelMetadata = Metadata._meta.get_model('model')
    content_type = ContentType.objects.get_for_model(obj)
    instances = []
    if InstanceMetadata is not None:
        try:
            instance_md = InstanceMetadata.objects.get(_content_type=content_type, _object_id=obj.pk)
        except InstanceMetadata.DoesNotExist:
            instance_md = InstanceMetadata(_content_object=obj)
        instances.append(instance_md)
    if ModelMetadata is not None:
        try:
            model_md = ModelMetadata.objects.get(_content_type=content_type)
        except ModelMetadata.DoesNotExist:
            model_md = ModelMetadata(_content_type=content_type)
        instances.append(model_md)
    return FormattedMetadata(Metadata, instances, '', site, language)

Response: Gets the metadata linked to the given object.


Instruction:
Given the following Python 3 function, write the documentation
def populate_metadata(model, MetadataClass):
    content_type = ContentType.objects.get_for_model(model)
    for instance in model.objects.all():
        create_metadata_instance(MetadataClass, instance)

Response: Populates the metadata class with the metadata for every object in the given model.


Instruction:
Make a summary of the following Python 3 code
def __instances(self):
        for instance in self.__instances_cache:
            yield instance
        for instance in self.__instances_original:
            self.__instances_cache.append(instance)
            yield instance

Response: Yields all the instances in the cache.


Instruction:
Explain what the following Python 3 code does
def _resolve_value(self, name):
        for instance in self.__instances():
            value = instance._resolve_value(name)
            if value:
                return value

        # Otherwise, return an appropriate default value (populate_from)
        # TODO: This is duplicated in meta_models. Move this to a common home.
        if name in self.__metadata._meta.elements:
            populate_from = self.__metadata._meta.elements[name].populate_from
            if callable(populate_from):
                return populate_from(None)
            elif isinstance(populate_from, Literal):
                return populate_from.value
            elif populate_from is not NotSet:
                return self._resolve_value(populate_from)

Response: Resolves the value for the given name.


Instruction:
Here you have a function in Python 3, explain what it does
def _get_formatted_data(cls, path, context=None, site=None, language=None):
        return FormattedMetadata(cls(), cls._get_instances(path, context, site, language), path, site, language)

Response: Return an object to conveniently access the appropriate values.


Instruction:
Can you tell what is the following Python 3 function doing
def _get_instances(cls, path, context=None, site=None, language=None):
        backend_context = {'view_context': context }

        for model in cls._meta.models.values():
            for instance in model.objects.get_instances(path, site, language, backend_context) or []:
                if hasattr(instance, '_process_context'):
                    instance._process_context(backend_context)
                yield instance

Response: A generator that yields all instances from each backend that are available for the given path.


Instruction:
Here you have a function in Python 3, explain what it does
def _resolve(value, model_instance=None, context=None):

    if isinstance(value, basestring) and "{" in value:
        if context is None:
            context = Context()
        if model_instance is not None:
            context[model_instance._meta.module_name] = model_instance
        value = Template(value).render(context)
    return value

Response: Resolves any template references in the given value.


Instruction:
Here you have a function in Python 3, explain what it does
def validate(options):
        try:
            if options.backends.index('modelinstance') > options.backends.index('model'):
                raise Exception("Metadata backend 'modelinstance' must come before 'model' backend")
        except ValueError:
            raise Exception("Metadata backend 'modelinstance' must be installed in order to use 'model' backend")

Response: Validates the application of this backend to a given metadata


Instruction:
Write a Python 3 function that can
take elements from the metadata class and creates a base model for all backend models.

Response:
def _register_elements(self, elements):
        """ Takes elements from the metadata class and creates a base model for all backend models .
        """
        self.elements = elements

        for key, obj in elements.items():
            obj.contribute_to_class(self.metadata, key)

        # Create the common Django fields
        fields = {}
        for key, obj in elements.items():
            if obj.editable:
                field = obj.get_field()
                if not field.help_text:
                    if key in self.bulk_help_text:
                        field.help_text = self.bulk_help_text[key]
                fields[key] = field

        # 0. Abstract base model with common fields
        base_meta = type('Meta', (), self.original_meta)
        class BaseMeta(base_meta):
            abstract = True
            app_label = 'seo'
        fields['Meta'] = BaseMeta
        # Do we need this?
        fields['__module__'] = __name__ #attrs['__module__']
        self.MetadataBaseModel = type('%sBase' % self.name, (models.Model,), fields)


Instruction:
Make a summary of the following Python 3 code
def _add_backend(self, backend):
        md_type = backend.verbose_name
        base = backend().get_model(self)
        # TODO: Rename this field
        new_md_attrs = {'_metadata': self.metadata, '__module__': __name__ }

        new_md_meta = {}
        new_md_meta['verbose_name'] = '%s (%s)' % (self.verbose_name, md_type)
        new_md_meta['verbose_name_plural'] = '%s (%s)' % (self.verbose_name_plural, md_type)
        new_md_meta['unique_together'] = base._meta.unique_together
        new_md_attrs['Meta'] = type("Meta", (), new_md_meta)
        new_md_attrs['_metadata_type'] = backend.name
        model = type("%s%s"%(self.name,"".join(md_type.split())), (base, self.MetadataBaseModel), new_md_attrs.copy())
        self.models[backend.name] = model
        # This is a little dangerous, but because we set __module__ to __name__, the model needs tobe accessible here
        globals()[model.__name__] = model

Response: Adds a backend to the metadata base model


Instruction:
Write a Python 3 function that can
set the list of models to be used.

Response:
def _set_seo_models(self, value):
        """ Gets the actual models to be used. """
        seo_models = []
        for model_name in value:
            if "." in model_name:
                app_label, model_name = model_name.split(".", 1)
                model = models.get_model(app_label, model_name)
                if model:
                    seo_models.append(model)
            else:
                app = models.get_app(model_name)
                if app:
                    seo_models.extend(models.get_models(app))

        self.seo_models = seo_models


Instruction:
Here you have a function in Python 3, explain what it does
def validate(self):
        if not self.editable:
            assert self.populate_from is not NotSet, u"If field (%s) is not editable, you must set populate_from" % self.name

Response: Validate that the field is valid.


Instruction:
Explain what the following Python 3 code does
def populate_all_metadata():
    for Metadata in registry.values():
        InstanceMetadata = Metadata._meta.get_model('modelinstance')
        if InstanceMetadata is not None:
            for model in Metadata._meta.seo_models:
                populate_metadata(model, InstanceMetadata)

Response: Populate all metadata instances for all models in seo_models.


Instruction:
Can you implement a function in Python 3 that
populates this list with all views that take no arguments.

Response:
def populate(self):
        """ Populate this list with all views that take no arguments.
        """
        from django.conf import settings
        from django.core import urlresolvers

        self.append(("", ""))
        urlconf = settings.ROOT_URLCONF
        resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
        # Collect base level views
        for key, value in resolver.reverse_dict.items():
            if isinstance(key, basestring):
                args = value[0][0][1]
                url = "/" + value[0][0][0]
                self.append((key, " ".join(key.split("_"))))
        # Collect namespaces (TODO: merge these two sections into one)
        for namespace, url in resolver.namespace_dict.items():
            for key, value in url[1].reverse_dict.items():
                if isinstance(key, basestring):
                    args = value[0][0][1]
                    full_key = '%s:%s' % (namespace, key)
                    self.append((full_key, "%s: %s" % (namespace, " ".join(key.split("_")))))
        self.sort()


Instruction:
Implement a Python 3 function for
generating a generator that yields a list of blocks of size block_size.

Response:
def block_splitter(data, block_size):
    """
    Creates a generator by slicing ``data`` into chunks of ``block_size``.

    >>> data = range(10)
    >>> list(block_splitter(data, 2))
    [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]

    If ``data`` cannot be evenly divided by ``block_size``, the last block will
    simply be the remainder of the data. Example:

    >>> data = range(10)
    >>> list(block_splitter(data, 3))
    [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

    If the ``block_size`` is greater than the total length of ``data``, a
    single block will be generated:

    >>> data = range(3)
    >>> list(block_splitter(data, 4))
    [[0, 1, 2]]

    :param data:
        Any iterable. If ``data`` is a generator, it will be exhausted,
        obviously.
    :param int block_site:
        Desired (maximum) block size.
    """
    buf = []
    for i, datum in enumerate(data):
        buf.append(datum)
        if len(buf) == block_size:
            yield buf
            buf = []

    # If there's anything leftover (a partial block),
    # yield it as well.
    if buf:
        yield buf