Instruction:
Can you write a function in Python 3 where it
updates the index with any objects in iterable.

Response:
def update(self, index, iterable, commit=True):
        """
        Updates the `index` with any objects in `iterable` by adding/updating
        the database as needed.

        Required arguments:
            `index` -- The `SearchIndex` to process
            `iterable` -- An iterable of model instances to index
        Optional arguments:
            `commit` -- ignored

        For each object in `iterable`, a document is created containing all
        of the terms extracted from `index.full_prepare(obj)` with field prefixes,
        and 'as-is' as needed.  Also, if the field type is 'text' it will be
        stemmed and stored with the 'Z' prefix as well.

        eg. `content:Testing` ==> `testing, Ztest, ZXCONTENTtest, XCONTENTtest`

        Each document also contains an extra term in the format:

        `XCONTENTTYPE<app_name>.<model_name>`

        As well as a unique identifier in the the format:

        `Q<app_name>.<model_name>.<pk>`

        eg.: foo.bar (pk=1) ==> `Qfoo.bar.1`, `XCONTENTTYPEfoo.bar`

        This is useful for querying for a specific document corresponding to
        a model instance.

        The document also contains a pickled version of the object itself and
        the document ID in the document data field.

        Finally, we also store field values to be used for sorting data.  We
        store these in the document value slots (position zero is reserver
        for the document ID).  All values are stored as unicode strings with
        conversion of float, int, double, values being done by Xapian itself
        through the use of the :method:xapian.sortable_serialise method.
        """
        database = self._database(writable=True)

        try:
            term_generator = xapian.TermGenerator()
            term_generator.set_database(database)
            term_generator.set_stemmer(xapian.Stem(self.language))
            try:
                term_generator.set_stemming_strategy(self.stemming_strategy)
            except AttributeError:
                # Versions before Xapian 1.2.11 do not support stemming strategies for TermGenerator
                pass
            if self.include_spelling is True:
                term_generator.set_flags(xapian.TermGenerator.FLAG_SPELLING)

            def _add_text(termpos, text, weight, prefix=''):
                """
                indexes text appending 2 extra terms
                to identify beginning and ending of the text.
                """
                term_generator.set_termpos(termpos)

                start_term = '%s^' % prefix
                end_term = '%s$' % prefix
                # add begin
                document.add_posting(start_term, termpos, weight)
                # add text
                term_generator.index_text(text, weight, prefix)
                termpos = term_generator.get_termpos()
                # add ending
                termpos += 1
                document.add_posting(end_term, termpos, weight)

                # increase termpos
                term_generator.set_termpos(termpos)
                term_generator.increase_termpos(TERMPOS_DISTANCE)

                return term_generator.get_termpos()

            def _add_literal_text(termpos, text, weight, prefix=''):
                """
                Adds sentence to the document with positional information
                but without processing.

                The sentence is bounded by "^" "$" to allow exact matches.
                """
                text = '^ %s $' % text
                for word in text.split():
                    term = '%s%s' % (prefix, word)
                    document.add_posting(term, termpos, weight)
                    termpos += 1
                termpos += TERMPOS_DISTANCE
                return termpos

            def add_text(termpos, prefix, text, weight):
                """
                Adds text to the document with positional information
                and processing (e.g. stemming).
                """
                termpos = _add_text(termpos, text, weight, prefix=prefix)
                termpos = _add_text(termpos, text, weight, prefix='')
                termpos = _add_literal_text(termpos, text, weight, prefix=prefix)
                termpos = _add_literal_text(termpos, text, weight, prefix='')
                return termpos

            def _get_ngram_lengths(value):
                values = value.split()
                for item in values:
                    for ngram_length in six.moves.range(NGRAM_MIN_LENGTH, NGRAM_MAX_LENGTH + 1):
                        yield item, ngram_length

            for obj in iterable:
                document = xapian.Document()
                term_generator.set_document(document)

                def ngram_terms(value):
                    for item, length in _get_ngram_lengths(value):
                        item_length = len(item)
                        for start in six.moves.range(0, item_length - length + 1):
                            for size in six.moves.range(length, length + 1):
                                end = start + size
                                if end > item_length:
                                    continue
                                yield _to_xapian_term(item[start:end])

                def edge_ngram_terms(value):
                    for item, length in _get_ngram_lengths(value):
                        yield _to_xapian_term(item[0:length])

                def add_edge_ngram_to_document(prefix, value, weight):
                    """
                    Splits the term in ngrams and adds each ngram to the index.
                    The minimum and maximum size of the ngram is respectively
                    NGRAM_MIN_LENGTH and NGRAM_MAX_LENGTH.
                    """
                    for term in edge_ngram_terms(value):
                        document.add_term(term, weight)
                        document.add_term(prefix + term, weight)

                def add_ngram_to_document(prefix, value, weight):
                    """
                    Splits the term in ngrams and adds each ngram to the index.
                    The minimum and maximum size of the ngram is respectively
                    NGRAM_MIN_LENGTH and NGRAM_MAX_LENGTH.
                    """
                    for term in ngram_terms(value):
                        document.add_term(term, weight)
                        document.add_term(prefix + term, weight)

                def add_non_text_to_document(prefix, term, weight):
                    """
                    Adds term to the document without positional information
                    and without processing.

                    If the term is alone, also adds it as "^<term>$"
                    to allow exact matches on single terms.
                    """
                    document.add_term(term, weight)
                    document.add_term(prefix + term, weight)

                def add_datetime_to_document(termpos, prefix, term, weight):
                    """
                    Adds a datetime to document with positional order
                    to allow exact matches on it.
                    """
                    date, time = term.split()
                    document.add_posting(date, termpos, weight)
                    termpos += 1
                    document.add_posting(time, termpos, weight)
                    termpos += 1
                    document.add_posting(prefix + date, termpos, weight)
                    termpos += 1
                    document.add_posting(prefix + time, termpos, weight)
                    termpos += TERMPOS_DISTANCE + 1
                    return termpos

                data = index.full_prepare(obj)
                weights = index.get_field_weights()

                termpos = term_generator.get_termpos()  # identifies the current position in the document.
                for field in self.schema:
                    if field['field_name'] not in list(data.keys()):
                        # not supported fields are ignored.
                        continue

                    if field['field_name'] in weights:
                        weight = int(weights[field['field_name']])
                    else:
                        weight = 1

                    value = data[field['field_name']]

                    if field['field_name'] in (ID, DJANGO_ID, DJANGO_CT):
                        # Private fields are indexed in a different way:
                        # `django_id` is an int and `django_ct` is text;
                        # besides, they are indexed by their (unstemmed) value.
                        if field['field_name'] == DJANGO_ID:
                            value = int(value)
                        value = _term_to_xapian_value(value, field['type'])

                        document.add_term(TERM_PREFIXES[field['field_name']] + value, weight)
                        document.add_value(field['column'], value)
                        continue
                    else:
                        prefix = TERM_PREFIXES['field'] + field['field_name'].upper()

                        # if not multi_valued, we add as a document value
                        # for sorting and facets
                        if field['multi_valued'] == 'false':
                            document.add_value(field['column'], _term_to_xapian_value(value, field['type']))
                        else:
                            for t in value:
                                # add the exact match of each value
                                term = _to_xapian_term(t)
                                termpos = add_text(termpos, prefix, term, weight)
                            continue

                        term = _to_xapian_term(value)
                        if term == '':
                            continue
                        # from here on the term is a string;
                        # we now decide how it is indexed

                        if field['type'] == 'text':
                            # text is indexed with positional information
                            termpos = add_text(termpos, prefix, term, weight)
                        elif field['type'] == 'datetime':
                            termpos = add_datetime_to_document(termpos, prefix, term, weight)
                        elif field['type'] == 'ngram':
                            add_ngram_to_document(prefix, value, weight)
                        elif field['type'] == 'edge_ngram':
                            add_edge_ngram_to_document(prefix, value, weight)
                        else:
                            # all other terms are added without positional information
                            add_non_text_to_document(prefix, term, weight)

                # store data without indexing it
                document.set_data(pickle.dumps(
                    (obj._meta.app_label, obj._meta.model_name, obj.pk, data),
                    pickle.HIGHEST_PROTOCOL
                ))

                # add the id of the document
                document_id = TERM_PREFIXES[ID] + get_identifier(obj)
                document.add_term(document_id)

                # finally, replace or add the document to the database
                database.replace_document(document_id, document)

        except UnicodeDecodeError:
            sys.stderr.write('Chunk failed.\n')
            pass

        finally:
            database.close()


Instruction:
Create a Python 3 function for
removing indexes for obj from the database.

Response:
def remove(self, obj, commit=True):
        """
        Remove indexes for `obj` from the database.

        We delete all instances of `Q<app_name>.<model_name>.<pk>` which
        should be unique to this object.

        Optional arguments:
           `commit` -- ignored
        """
        database = self._database(writable=True)
        database.delete_document(TERM_PREFIXES[ID] + get_identifier(obj))
        database.close()


Instruction:
Can you tell what is the following Python 3 function doing
def clear(self, models=(), commit=True):
        if not models:
            # Because there does not appear to be a "clear all" method,
            # it's much quicker to remove the contents of the `self.path`
            # folder than it is to remove each document one at a time.
            if os.path.exists(self.path):
                shutil.rmtree(self.path)
        else:
            database = self._database(writable=True)
            for model in models:
                database.delete_document(TERM_PREFIXES[DJANGO_CT] + get_model_ct(model))
            database.close()

Response: Clears all instances of models from the database or all models.


Instruction:
How would you explain what the following Python 3 function does
def _build_models_query(self, query):
        registered_models_ct = self.build_models_list()
        if registered_models_ct:
            restrictions = [xapian.Query('%s%s' % (TERM_PREFIXES[DJANGO_CT], model_ct))
                            for model_ct in registered_models_ct]
            limit_query = xapian.Query(xapian.Query.OP_OR, restrictions)

            query = xapian.Query(xapian.Query.OP_AND, query, limit_query)

        return query

Response: Builds a query from query that filters to documents only from registered models.


Instruction:
Can you tell what is the following Python 3 function doing
def _check_field_names(self, field_names):
        if field_names:
            for field_name in field_names:
                try:
                    self.column[field_name]
                except KeyError:
                    raise InvalidIndexError('Trying to use non indexed field "%s"' % field_name)

Response: Checks that the given field names are indexed.


Instruction:
Write a Python 3 script to
execute a query and returns a dictionary of search results.

Response:
def search(self, query, sort_by=None, start_offset=0, end_offset=None,
               fields='', highlight=False, facets=None, date_facets=None,
               query_facets=None, narrow_queries=None, spelling_query=None,
               limit_to_registered_models=None, result_class=None, **kwargs):
        """
        Executes the Xapian::query as defined in `query`.

        Required arguments:
            `query` -- Search query to execute

        Optional arguments:
            `sort_by` -- Sort results by specified field (default = None)
            `start_offset` -- Slice results from `start_offset` (default = 0)
            `end_offset` -- Slice results at `end_offset` (default = None), if None, then all documents
            `fields` -- Filter results on `fields` (default = '')
            `highlight` -- Highlight terms in results (default = False)
            `facets` -- Facet results on fields (default = None)
            `date_facets` -- Facet results on date ranges (default = None)
            `query_facets` -- Facet results on queries (default = None)
            `narrow_queries` -- Narrow queries (default = None)
            `spelling_query` -- An optional query to execute spelling suggestion on
            `limit_to_registered_models` -- Limit returned results to models registered in
            the current `SearchSite` (default = True)

        Returns:
            A dictionary with the following keys:
                `results` -- A list of `SearchResult`
                `hits` -- The total available results
                `facets` - A dictionary of facets with the following keys:
                    `fields` -- A list of field facets
                    `dates` -- A list of date facets
                    `queries` -- A list of query facets
            If faceting was not used, the `facets` key will not be present

        If `query` is None, returns no results.

        If `INCLUDE_SPELLING` was enabled in the connection options, the
        extra flag `FLAG_SPELLING_CORRECTION` will be passed to the query parser
        and any suggestions for spell correction will be returned as well as
        the results.
        """
        if xapian.Query.empty(query):
            return {
                'results': [],
                'hits': 0,
            }

        self._check_field_names(facets)
        self._check_field_names(date_facets)
        self._check_field_names(query_facets)

        database = self._database()

        if limit_to_registered_models is None:
            limit_to_registered_models = getattr(settings, 'HAYSTACK_LIMIT_TO_REGISTERED_MODELS', True)

        if result_class is None:
            result_class = SearchResult

        if self.include_spelling is True:
            spelling_suggestion = self._do_spelling_suggestion(database, query, spelling_query)
        else:
            spelling_suggestion = ''

        if narrow_queries is not None:
            query = xapian.Query(
                xapian.Query.OP_AND, query, xapian.Query(
                    xapian.Query.OP_AND, [self.parse_query(narrow_query) for narrow_query in narrow_queries]
                )
            )

        if limit_to_registered_models:
            query = self._build_models_query(query)

        enquire = xapian.Enquire(database)
        if hasattr(settings, 'HAYSTACK_XAPIAN_WEIGHTING_SCHEME'):
            enquire.set_weighting_scheme(xapian.BM25Weight(*settings.HAYSTACK_XAPIAN_WEIGHTING_SCHEME))
        enquire.set_query(query)

        if sort_by:
            try:
                _xapian_sort(enquire, sort_by, self.column)
            except NotSupportedError:
                _old_xapian_sort(enquire, sort_by, self.column)

        results = []
        facets_dict = {
            'fields': {},
            'dates': {},
            'queries': {},
        }

        if not end_offset:
            end_offset = database.get_doccount() - start_offset

        ## prepare spies in case of facets
        if facets:
            facets_spies = self._prepare_facet_field_spies(facets)
            for spy in facets_spies:
                enquire.add_matchspy(spy)

        # print enquire.get_query()

        matches = self._get_enquire_mset(database, enquire, start_offset, end_offset)

        for match in matches:
            app_label, model_name, pk, model_data = pickle.loads(self._get_document_data(database, match.document))
            if highlight:
                model_data['highlighted'] = {
                    self.content_field_name: self._do_highlight(
                        model_data.get(self.content_field_name), query
                    )
                }
            results.append(
                result_class(app_label, model_name, pk, match.percent, **model_data)
            )

        if facets:
            # pick single valued facets from spies
            single_facets_dict = self._process_facet_field_spies(facets_spies)

            # pick multivalued valued facets from results
            multi_facets_dict = self._do_multivalued_field_facets(results, facets)

            # merge both results (http://stackoverflow.com/a/38990/931303)
            facets_dict['fields'] = dict(list(single_facets_dict.items()) + list(multi_facets_dict.items()))

        if date_facets:
            facets_dict['dates'] = self._do_date_facets(results, date_facets)

        if query_facets:
            facets_dict['queries'] = self._do_query_facets(results, query_facets)

        return {
            'results': results,
            'hits': self._get_hit_count(database, enquire),
            'facets': facets_dict,
            'spelling_suggestion': spelling_suggestion,
        }


Instruction:
Can you generate the documentation for the following Python 3 function
def more_like_this(self, model_instance, additional_query=None,
                       start_offset=0, end_offset=None,
                       limit_to_registered_models=True, result_class=None, **kwargs):
        """
        Given a model instance, returns a result set of similar documents.

        Required arguments:
            `model_instance` -- The model instance to use as a basis for
                                retrieving similar documents.

        Optional arguments:
            `additional_query` -- An additional query to narrow results
            `start_offset` -- The starting offset (default=0)
            `end_offset` -- The ending offset (default=None), if None, then all documents
            `limit_to_registered_models` -- Limit returned results to models registered in the search (default = True)

        Returns:
            A dictionary with the following keys:
                `results` -- A list of `SearchResult`
                `hits` -- The total available results

        Opens a database connection, then builds a simple query using the
        `model_instance` to build the unique identifier.

        For each document retrieved(should always be one), adds an entry into
        an RSet (relevance set) with the document id, then, uses the RSet
        to query for an ESet (A set of terms that can be used to suggest
        expansions to the original query), omitting any document that was in
        the original query.

        Finally, processes the resulting matches and returns.
        """
        database = self._database()

        if result_class is None:
            result_class = SearchResult

        query = xapian.Query(TERM_PREFIXES[ID] + get_identifier(model_instance))

        enquire = xapian.Enquire(database)
        enquire.set_query(query)

        rset = xapian.RSet()

        if not end_offset:
            end_offset = database.get_doccount()

        match = None
        for match in self._get_enquire_mset(database, enquire, 0, end_offset):
            rset.add_document(match.docid)

        if match is None:
            if not self.silently_fail:
                raise InvalidIndexError('Instance %s with id "%d" not indexed' %
                                        (get_identifier(model_instance), model_instance.id))
            else:
                return {'results': [],
                        'hits': 0}

        query = xapian.Query(
            xapian.Query.OP_ELITE_SET,
            [expand.term for expand in enquire.get_eset(match.document.termlist_count(), rset, XHExpandDecider())],
            match.document.termlist_count()
        )
        query = xapian.Query(
            xapian.Query.OP_AND_NOT, [query, TERM_PREFIXES[ID] + get_identifier(model_instance)]
        )

        if limit_to_registered_models:
            query = self._build_models_query(query)

        if additional_query:
            query = xapian.Query(
                xapian.Query.OP_AND, query, additional_query
            )

        enquire.set_query(query)

        results = []
        matches = self._get_enquire_mset(database, enquire, start_offset, end_offset)

        for match in matches:
            app_label, model_name, pk, model_data = pickle.loads(self._get_document_data(database, match.document))
            results.append(
                result_class(app_label, model_name, pk, match.percent, **model_data)
            )

        return {
            'results': results,
            'hits': self._get_hit_count(database, enquire),
            'facets': {
                'fields': {},
                'dates': {},
                'queries': {},
            },
            'spelling_suggestion': None,
        }

Response: Returns a result set of similar documents.


Instruction:
Implement a function in Python 3 to
parse a query string and return a xapian. Query object

Response:
def parse_query(self, query_string):
        """
        Given a `query_string`, will attempt to return a xapian.Query

        Required arguments:
            ``query_string`` -- A query string to parse

        Returns a xapian.Query
        """
        if query_string == '*':
            return xapian.Query('')  # Match everything
        elif query_string == '':
            return xapian.Query()  # Match nothing

        qp = xapian.QueryParser()
        qp.set_database(self._database())
        qp.set_stemmer(xapian.Stem(self.language))
        qp.set_stemming_strategy(self.stemming_strategy)
        qp.set_default_op(XAPIAN_OPTS[DEFAULT_OPERATOR])
        qp.add_boolean_prefix(DJANGO_CT, TERM_PREFIXES[DJANGO_CT])

        for field_dict in self.schema:
            # since 'django_ct' has a boolean_prefix,
            # we ignore it here.
            if field_dict['field_name'] == DJANGO_CT:
                continue

            qp.add_prefix(
                field_dict['field_name'],
                TERM_PREFIXES['field'] + field_dict['field_name'].upper()
            )

        vrp = XHValueRangeProcessor(self)
        qp.add_valuerangeprocessor(vrp)

        return qp.parse_query(query_string, self.flags)


Instruction:
Make a summary of the following Python 3 code
def build_schema(self, fields):
        content_field_name = ''
        schema_fields = [
            {'field_name': ID,
             'type': 'text',
             'multi_valued': 'false',
             'column': 0},
            {'field_name': DJANGO_ID,
             'type': 'integer',
             'multi_valued': 'false',
             'column': 1},
            {'field_name': DJANGO_CT,
             'type': 'text',
             'multi_valued': 'false',
             'column': 2},
        ]
        self._columns[ID] = 0
        self._columns[DJANGO_ID] = 1
        self._columns[DJANGO_CT] = 2

        column = len(schema_fields)

        for field_name, field_class in sorted(list(fields.items()), key=lambda n: n[0]):
            if field_class.document is True:
                content_field_name = field_class.index_fieldname

            if field_class.indexed is True:
                field_data = {
                    'field_name': field_class.index_fieldname,
                    'type': 'text',
                    'multi_valued': 'false',
                    'column': column,
                }

                if field_class.field_type == 'date':
                    field_data['type'] = 'date'
                elif field_class.field_type == 'datetime':
                    field_data['type'] = 'datetime'
                elif field_class.field_type == 'integer':
                    field_data['type'] = 'integer'
                elif field_class.field_type == 'float':
                    field_data['type'] = 'float'
                elif field_class.field_type == 'boolean':
                    field_data['type'] = 'boolean'
                elif field_class.field_type == 'ngram':
                    field_data['type'] = 'ngram'
                elif field_class.field_type == 'edge_ngram':
                    field_data['type'] = 'edge_ngram'

                if field_class.is_multivalued:
                    field_data['multi_valued'] = 'true'

                schema_fields.append(field_data)
                self._columns[field_data['field_name']] = column
                column += 1

        return content_field_name, schema_fields

Response:
Builds the schema from the fields in the index
           .


Instruction:
Create a Python 3 function to
highlight the query terms in content with html tag.

Response:
def _do_highlight(content, query, tag='em'):
        """
        Highlight `query` terms in `content` with html `tag`.

        This method assumes that the input text (`content`) does not contain
        any special formatting.  That is, it does not contain any html tags
        or similar markup that could be screwed up by the highlighting.

        Required arguments:
            `content` -- Content to search for instances of `text`
            `text` -- The text to be highlighted
        """
        for term in query:
            term = term.decode('utf-8')
            for match in re.findall('[^A-Z]+', term):  # Ignore field identifiers
                match_re = re.compile(match, re.I)
                content = match_re.sub('<%s>%s</%s>' % (tag, term, tag), content)

        return content


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _prepare_facet_field_spies(self, facets):
        spies = []
        for facet in facets:
            slot = self.column[facet]
            spy = xapian.ValueCountMatchSpy(slot)
            # add attribute "slot" to know which column this spy is targeting.
            spy.slot = slot
            spies.append(spy)
        return spies

Response: Returns a list of spies based on the facets.


Instruction:
Here you have a function in Python 3, explain what it does
def _process_facet_field_spies(self, spies):
        facet_dict = {}
        for spy in spies:
            field = self.schema[spy.slot]
            field_name, field_type = field['field_name'], field['type']

            facet_dict[field_name] = []
            for facet in list(spy.values()):
                if field_type == 'float':
                    # the float term is a Xapian serialized object, which is
                    # in bytes.
                    term = facet.term
                else:
                    term = facet.term.decode('utf-8')

                facet_dict[field_name].append((_from_xapian_value(term, field_type),
                                               facet.termfreq))
        return facet_dict

Response: Returns a dict of facet names with lists of tuples of the form term term_frequency


Instruction:
Can you generate the documentation for the following Python 3 function
def _do_multivalued_field_facets(self, results, field_facets):
        facet_dict = {}

        for field in field_facets:
            facet_list = {}
            if not self._multi_value_field(field):
                continue

            for result in results:
                field_value = getattr(result, field)
                for item in field_value:  # Facet each item in a MultiValueField
                    facet_list[item] = facet_list.get(item, 0) + 1

            facet_dict[field] = list(facet_list.items())
        return facet_dict

Response: Implements a multivalued field facet on the results.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _do_date_facets(results, date_facets):
        def next_datetime(previous, gap_value, gap_type):
            year = previous.year
            month = previous.month

            if gap_type == 'year':
                next = previous.replace(year=year + gap_value)
            elif gap_type == 'month':
                if month + gap_value <= 12:
                    next = previous.replace(month=month + gap_value)
                else:
                    next = previous.replace(
                        month=((month + gap_value) % 12),
                        year=(year + (month + gap_value) // 12)
                    )
            elif gap_type == 'day':
                next = previous + datetime.timedelta(days=gap_value)
            elif gap_type == 'hour':
                return previous + datetime.timedelta(hours=gap_value)
            elif gap_type == 'minute':
                next = previous + datetime.timedelta(minutes=gap_value)
            elif gap_type == 'second':
                next = previous + datetime.timedelta(seconds=gap_value)
            else:
                raise TypeError('\'gap_by\' must be '
                                '{second, minute, day, month, year}')
            return next

        facet_dict = {}

        for date_facet, facet_params in list(date_facets.items()):
            gap_type = facet_params.get('gap_by')
            gap_value = facet_params.get('gap_amount', 1)
            date_range = facet_params['start_date']

            # construct the bins of the histogram
            facet_list = []
            while date_range < facet_params['end_date']:
                facet_list.append((date_range, 0))
                date_range = next_datetime(date_range, gap_value, gap_type)

            facet_list = sorted(facet_list, key=lambda x: x[0], reverse=True)

            for result in results:
                result_date = getattr(result, date_facet)

                # convert date to datetime
                if not isinstance(result_date, datetime.datetime):
                    result_date = datetime.datetime(result_date.year,
                                                    result_date.month,
                                                    result_date.day)

                # ignore results outside the boundaries.
                if facet_list[0][0] < result_date < facet_list[-1][0]:
                    continue

                # populate the histogram by putting the result on the right bin.
                for n, facet_date in enumerate(facet_list):
                    if result_date > facet_date[0]:
                        # equal to facet_list[n][1] += 1, but for a tuple
                        facet_list[n] = (facet_list[n][0], (facet_list[n][1] + 1))
                        break  # bin found; go to next result

            facet_dict[date_facet] = facet_list

        return facet_dict

Response: Private method that returns a dictionary containing faceted date fields and a count of documents that match the date range.


Instruction:
Explain what the following Python 3 code does
def _do_spelling_suggestion(database, query, spelling_query):
        if spelling_query:
            if ' ' in spelling_query:
                return ' '.join([database.get_spelling_suggestion(term).decode('utf-8') for term in spelling_query.split()])
            else:
                return database.get_spelling_suggestion(spelling_query).decode('utf-8')

        term_set = set()
        for term in query:
            for match in re.findall('[^A-Z]+', term.decode('utf-8')):  # Ignore field identifiers
                term_set.add(database.get_spelling_suggestion(match).decode('utf-8'))

        return ' '.join(term_set)

Response: Private method that returns a single spelling suggestion based on spelling_query or query.


Instruction:
Write a Python 3 function for
wrapping for the Xapian. enquire. get_mset method that wraps the Xapian. database. get_mset method and catches any Xapian. DatabaseModifiedError and returns the result.

Response:
def _get_enquire_mset(database, enquire, start_offset, end_offset, checkatleast=DEFAULT_CHECK_AT_LEAST):
        """
        A safer version of Xapian.enquire.get_mset

        Simply wraps the Xapian version and catches any `Xapian.DatabaseModifiedError`,
        attempting a `database.reopen` as needed.

        Required arguments:
            `database` -- The database to be read
            `enquire` -- An instance of an Xapian.enquire object
            `start_offset` -- The start offset to pass to `enquire.get_mset`
            `end_offset` -- The end offset to pass to `enquire.get_mset`
        """
        try:
            return enquire.get_mset(start_offset, end_offset, checkatleast)
        except xapian.DatabaseModifiedError:
            database.reopen()
            return enquire.get_mset(start_offset, end_offset, checkatleast)


Instruction:
Implement a Python 3 function for
returning the estimated number of matches in a database and an enquire instance.

Response:
def _get_hit_count(self, database, enquire):
        """
        Given a database and enquire instance, returns the estimated number
        of matches.

        Required arguments:
            `database` -- The database to be queried
            `enquire` -- The enquire instance
        """
        return self._get_enquire_mset(
            database, enquire, 0, database.get_doccount()
        ).size()


Instruction:
Can you implement a function in Python 3 that
returns a list of xapian. Query s.

Response:
def _query_from_term(self, term, field_name, filter_type, is_not):
        """
        Uses arguments to construct a list of xapian.Query's.
        """
        if field_name != 'content' and field_name not in self.backend.column:
            raise InvalidIndexError('field "%s" not indexed' % field_name)

        # It it is an AutoQuery, it has no filters
        # or others, thus we short-circuit the procedure.
        if isinstance(term, AutoQuery):
            if field_name != 'content':
                query = '%s:%s' % (field_name, term.prepare(self))
            else:
                query = term.prepare(self)
            return [self.backend.parse_query(query)]
        query_list = []

        # Handle `ValuesListQuerySet`.
        if hasattr(term, 'values_list'):
            term = list(term)

        if field_name == 'content':
            # content is the generic search:
            # force no field_name search
            # and the field_type to be 'text'.
            field_name = None
            field_type = 'text'

            # we don't know what is the type(term), so we parse it.
            # Ideally this would not be required, but
            # some filters currently depend on the term to make decisions.
            term = _to_xapian_term(term)

            query_list.append(self._filter_contains(term, field_name, field_type, is_not))
            # when filter has no filter_type, haystack uses
            # filter_type = 'content'. Here we remove it
            # since the above query is already doing this
            if filter_type == 'content':
                filter_type = None
        else:
            # get the field_type from the backend
            field_type = self.backend.schema[self.backend.column[field_name]]['type']

        # private fields don't accept 'contains' or 'startswith'
        # since they have no meaning.
        if filter_type in ('contains', 'startswith') and field_name in (ID, DJANGO_ID, DJANGO_CT):
            filter_type = 'exact'

        if field_type == 'text':
            # we don't know what type "term" is, but we know we are searching as text
            # so we parse it like that.
            # Ideally this would not be required since _term_query does it, but
            # some filters currently depend on the term to make decisions.
            if isinstance(term, list):
                term = [_to_xapian_term(term) for term in term]
            else:
                term = _to_xapian_term(term)

        # todo: we should check that the filter is valid for this field_type or raise InvalidIndexError
        if filter_type == 'contains':
            query_list.append(self._filter_contains(term, field_name, field_type, is_not))
        elif filter_type in ('content', 'exact'):
            query_list.append(self._filter_exact(term, field_name, field_type, is_not))
        elif filter_type == 'in':
            query_list.append(self._filter_in(term, field_name, field_type, is_not))
        elif filter_type == 'startswith':
            query_list.append(self._filter_startswith(term, field_name, field_type, is_not))
        elif filter_type == 'endswith':
            raise NotImplementedError("The Xapian search backend doesn't support endswith queries.")
        elif filter_type == 'gt':
            query_list.append(self._filter_gt(term, field_name, field_type, is_not))
        elif filter_type == 'gte':
            query_list.append(self._filter_gte(term, field_name, field_type, is_not))
        elif filter_type == 'lt':
            query_list.append(self._filter_lt(term, field_name, field_type, is_not))
        elif filter_type == 'lte':
            query_list.append(self._filter_lte(term, field_name, field_type, is_not))
        elif filter_type == 'range':
            query_list.append(self._filter_range(term, field_name, field_type, is_not))
        return query_list


Instruction:
Write a Python 3 script to
return a Query that returns a list of objects that contain the term.

Response:
def _filter_contains(self, term, field_name, field_type, is_not):
        """
        Splits the sentence in terms and join them with OR,
        using stemmed and un-stemmed.

        Assumes term is not a list.
        """
        if field_type == 'text':
            term_list = term.split()
        else:
            term_list = [term]

        query = self._or_query(term_list, field_name, field_type)
        if is_not:
            return xapian.Query(xapian.Query.OP_AND_NOT, self._all_query(), query)
        else:
            return query


Instruction:
Can you implement a function in Python 3 that
returns a query that matches exactly ANY term in term_list.

Response:
def _filter_in(self, term_list, field_name, field_type, is_not):
        """
        Returns a query that matches exactly ANY term in term_list.

        Notice that:
         A in {B,C} <=> (A = B or A = C)
         ~(A in {B,C}) <=> ~(A = B or A = C)
        Because OP_AND_NOT(C, D) <=> (C and ~D), then D=(A in {B,C}) requires `is_not=False`.

        Assumes term is a list.
        """
        query_list = [self._filter_exact(term, field_name, field_type, is_not=False)
                      for term in term_list]

        if is_not:
            return xapian.Query(xapian.Query.OP_AND_NOT, self._all_query(),
                                xapian.Query(xapian.Query.OP_OR, query_list))
        else:
            return xapian.Query(xapian.Query.OP_OR, query_list)


Instruction:
Given the following Python 3 function, write the documentation
def _filter_exact(self, term, field_name, field_type, is_not):
        if field_type == 'text' and field_name not in (DJANGO_CT,):
            term = '^ %s $' % term
            query = self._phrase_query(term.split(), field_name, field_type)
        else:
            query = self._term_query(term, field_name, field_type, stemmed=False)

        if is_not:
            return xapian.Query(xapian.Query.OP_AND_NOT, self._all_query(), query)
        else:
            return query

Response: Returns a query that matches exactly the un - stemmed term term


Instruction:
Given the following Python 3 function, write the documentation
def _filter_startswith(self, term, field_name, field_type, is_not):
        if field_type == 'text':
            if len(term.split()) == 1:
                term = '^ %s*' % term
                query = self.backend.parse_query(term)
            else:
                term = '^ %s' % term
                query = self._phrase_query(term.split(), field_name, field_type)
        else:
            term = '^%s*' % term
            query = self.backend.parse_query(term)

        if is_not:
            return xapian.Query(xapian.Query.OP_AND_NOT, self._all_query(), query)
        return query

Response: Returns a startswith query on the un - stemmed term.


Instruction:
Can you tell what is the following Python 3 function doing
def _or_query(self, term_list, field, field_type):
        term_list = [self._term_query(term, field, field_type) for term in term_list]
        return xapian.Query(xapian.Query.OP_OR, term_list)

Response: Returns an xapian. Query object that is an OR query.


Instruction:
Make a summary of the following Python 3 code
def _phrase_query(self, term_list, field_name, field_type):
        term_list = [self._term_query(term, field_name, field_type,
                                      stemmed=False) for term in term_list]

        query = xapian.Query(xapian.Query.OP_PHRASE, term_list)
        return query

Response:
Returns a query that matches exact terms with
        positional order


Instruction:
Write a Python 3 script to
construct a query for a single term.

Response:
def _term_query(self, term, field_name, field_type, stemmed=True):
        """
        Constructs a query of a single term.

        If `field_name` is not `None`, the term is search on that field only.
        If exact is `True`, the search is restricted to boolean matches.
        """
        constructor = '{prefix}{term}'

        # construct the prefix to be used.
        prefix = ''
        if field_name:
            prefix = TERM_PREFIXES['field'] + field_name.upper()
            term = _to_xapian_term(term)

        if field_name in (ID, DJANGO_ID, DJANGO_CT):
            # to ensure the value is serialized correctly.
            if field_name == DJANGO_ID:
                term = int(term)
            term = _term_to_xapian_value(term, field_type)
            return xapian.Query('%s%s' % (TERM_PREFIXES[field_name], term))

        # we construct the query dates in a slightly different way
        if field_type == 'datetime':
            date, time = term.split()
            return xapian.Query(xapian.Query.OP_AND_MAYBE,
                                constructor.format(prefix=prefix, term=date),
                                constructor.format(prefix=prefix, term=time)
                                )

        # only use stem if field is text or "None"
        if field_type not in ('text', None):
            stemmed = False

        unstemmed_term = constructor.format(prefix=prefix, term=term)
        if stemmed:
            stem = xapian.Stem(self.backend.language)
            stemmed_term = 'Z' + constructor.format(prefix=prefix, term=stem(term).decode('utf-8'))

            return xapian.Query(xapian.Query.OP_OR,
                                xapian.Query(stemmed_term),
                                xapian.Query(unstemmed_term)
                                )
        else:
            return xapian.Query(unstemmed_term)


Instruction:
Can you tell what is the following Python 3 function doing
def _filter_gte(self, term, field_name, field_type, is_not):
        vrp = XHValueRangeProcessor(self.backend)
        pos, begin, end = vrp('%s:%s' % (field_name, _term_to_xapian_value(term, field_type)), '*')
        if is_not:
            return xapian.Query(xapian.Query.OP_AND_NOT,
                                self._all_query(),
                                xapian.Query(xapian.Query.OP_VALUE_RANGE, pos, begin, end)
                                )
        return xapian.Query(xapian.Query.OP_VALUE_RANGE, pos, begin, end)

Response: Private method that returns a xapian. Query that searches for any term that is greater than term in a specified field.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get(company='', company_uri=''):
    if not company and not company_uri:
        raise Exception("glassdoor.gd.get(company='', company_uri=''): "\
                            " company or company_uri required")
    payload = {}
    if not company_uri:
        payload.update({'clickSource': 'searchBtn',
                        'sc.keyword': company
                        })
        uri = '%s/%s' % (GLASSDOOR_API, REVIEWS_URL)
    else:
        uri = '%s%s' % (GLASSDOOR_API, company_uri)
    r = requests.get(uri, params=payload)
    soup = BeautifulSoup(r.content)
    results = parse(soup)
    return results

Response: Performs a HTTP GET for a glassdoor page and returns json


Instruction:
Explain what the following Python 3 code does
def parse_suggestions(soup):
    selector_comps = {'class': 'companyData'}
    companies = soup.findAll('div', selector_comps)

    def is_exact_match(c):
        """Determines if this company suggestion has an [exact match]
        html label or whether its name matches the company name the
        user searched for
        """
        selector_exact = {'class' : 'chickletExactMatch chicklet'}
        searched_name = soup.findAll('input', {'name': 'sc.keyword'})[0]['value']
        actual_name = c.findAll('h3')[0].text
        names_match = searched_name.lower() == actual_name.lower()
        exact_tag = bool(c.findAll('i', selector_exact))
        return exact_tag or names_match

    def parse_suggestion(c):
        return {
            'name': c.findAll('h3')[0].text,
            'uri': c.findAll('a')[1]['href'],
            'exact': is_exact_match(c)
            }

    suggestions = []
    for c in companies:
        try:
            suggestions.append(parse_suggestion(c))
        except IndexError as e:
            pass

    return suggestions

Response: Parses the suggestions from the company and returns a list of dicts.


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

    if is_direct_match(soup):
        return {'satisfaction': parse_satisfaction(soup),
                'ceo': parse_ceo(soup),
                'meta': parse_meta(soup),
                'salary': parse_salary(soup)
                }

    suggestions = parse_suggestions(soup)
    exact_match = next((s for s in suggestions if s['exact']), None)
    if exact_match:
        return get(company_uri=exact_match['uri'])
    return suggestions

Response:
Parses the results for a company search and returns the results
    if is_direct_match.


Instruction:
Make a summary of the following Python 3 code
def init_app(self, app):
        # Save this so we can use it later in the extension
        if not hasattr(app, 'extensions'):  # pragma: no cover
            app.extensions = {}
        app.extensions['flask-graphql-auth'] = self

        self._set_default__configuration_options(app)

Response: Register this extension with the flask app.


Instruction:
Can you write a function in Python 3 where it
sets the default configuration options used by this extension.

Response:
def _set_default__configuration_options(app):
        """
        Sets the default configuration options used by this extension
        """
        app.config.setdefault('JWT_TOKEN_ARGUMENT_NAME', "token")  # Name of token argument in GraphQL request resolver
        app.config.setdefault('JWT_REFRESH_TOKEN_ARGUMENT_NAME', "refresh_token")

        app.config.setdefault('JWT_ACCESS_TOKEN_EXPIRES', datetime.timedelta(minutes=15))
        app.config.setdefault('JWT_REFRESH_TOKEN_EXPIRES', datetime.timedelta(days=30))

        app.config.setdefault('JWT_SECRET_KEY', None)

        app.config.setdefault('JWT_IDENTITY_CLAIM', 'identity')
        app.config.setdefault('JWT_USER_CLAIMS', 'user_claims')


Instruction:
Write a Python 3 script to
decode an encoded JWT into a dictionary containing the contents of the JWT

Response:
def decode_jwt(encoded_token, secret, algorithm, identity_claim_key,
               user_claims_key):
    """
    Decodes an encoded JWT

    :param encoded_token: The encoded JWT string to decode
    :param secret: Secret key used to encode the JWT
    :param algorithm: Algorithm used to encode the JWT
    :param identity_claim_key: expected key that contains the identity
    :param user_claims_key: expected key that contains the user claims
    :return: Dictionary containing contents of the JWT
    """
    # This call verifies the ext, iat, and nbf claims
    data = jwt.decode(encoded_token, secret, algorithms=[algorithm])

    # Make sure that any custom claims we expect in the token are present
    if 'jti' not in data:
        raise JWTDecodeError("Missing claim: jti")
    if identity_claim_key not in data:
        raise JWTDecodeError("Missing claim: {}".format(identity_claim_key))
    if 'type' not in data or data['type'] not in ('refresh', 'access'):
        raise JWTDecodeError("Missing or invalid claim: type")
    if user_claims_key not in data:
        data[user_claims_key] = {}

    return data


Instruction:
Can you tell what is the following Python 3 function doing
def get_jwt_data(token, token_type):
    jwt_data = decode_jwt(
        encoded_token=token,
        secret=current_app.config['JWT_SECRET_KEY'],
        algorithm='HS256',
        identity_claim_key=current_app.config['JWT_IDENTITY_CLAIM'],
        user_claims_key=current_app.config['JWT_USER_CLAIMS']
        )

    # token type verification
    if jwt_data['type'] != token_type:
        raise WrongTokenError('Only {} tokens are allowed'.format(token_type))

    return jwt_data

Response: Decodes an encoded JWT string and validates it


Instruction:
Explain what the following Python 3 code does
def query_jwt_required(fn):
    @wraps(fn)
    def wrapper(*args, **kwargs):
        print(args[0])
        token = kwargs.pop(current_app.config['JWT_TOKEN_ARGUMENT_NAME'])
        try:
            verify_jwt_in_argument(token)
        except Exception as e:
            return AuthInfoField(message=str(e))

        return fn(*args, **kwargs)
    return wrapper

Response: Decorator to protect a query resolver.


Instruction:
Explain what the following Python 3 code does
def mutation_jwt_refresh_token_required(fn):
    @wraps(fn)
    def wrapper(cls, *args, **kwargs):
        token = kwargs.pop(current_app.config['JWT_REFRESH_TOKEN_ARGUMENT_NAME'])
        try:
            verify_refresh_jwt_in_argument(token)
        except Exception as e:
            return cls(AuthInfoField(message=str(e)))

        return fn(*args, **kwargs)
    return wrapper

Response: Decorator to protect a mutation.


Instruction:
Explain what the following Python 3 code does
def _hid_enumerate(vendor_id=0, product_id=0):
    start = hidapi.hid_enumerate(vendor_id, product_id)
    result = []
    cur = ffi.new("struct hid_device_info*");
    cur = start

    # Copy everything into python list
    while cur != ffi.NULL:
        result.append(HIDDevice(cur))
        cur = cur.next

    # Free the C memory
    hidapi.hid_free_enumeration(start)

    return result

Response: Enumerates all the hid devices for VID : PID. Returns a list of HIDDevice objects.


Instruction:
Can you create a Python 3 function that
opens the HID device for reading and writing.

Response:
def open(self):
        """
        Open the HID device for reading and writing.
        """
        if self._is_open:
            raise HIDException("Failed to open device: HIDDevice already open")

        path = self.path.encode('utf-8')
        dev = hidapi.hid_open_path(path)

        if dev:
            self._is_open = True
            self._device = dev
        else:
            raise HIDException("Failed to open device")


Instruction:
Explain what the following Python 3 code does
def close(self):
        if self._is_open:
            self._is_open = False
            hidapi.hid_close(self._device)

Response: Closes the hid device.


Instruction:
Make a summary of the following Python 3 code
def write(self, data, report_id=0):

        if not self._is_open:
            raise HIDException("HIDDevice not open")

        write_data = bytearray([report_id]) + bytearray(data)
        cdata = ffi.new("const unsigned char[]", bytes(write_data))
        num_written = hidapi.hid_write(self._device, cdata, len(write_data))
        if num_written < 0:
            raise HIDException("Failed to write to HID device: " + str(num_written))
        else:
            return num_written

Response: Writes data to the HID device on its endpoint.


Instruction:
Here you have a function in Python 3, explain what it does
def read(self, size=64, timeout=None):

        if not self._is_open:
            raise HIDException("HIDDevice not open")

        data = [0] * size
        cdata = ffi.new("unsigned char[]", data)
        bytes_read = 0

        if timeout == None:
            bytes_read = hidapi.hid_read(self._device, cdata, len(cdata))
        else:
            bytes_read = hidapi.hid_read_timeout(self._device, cdata, len(cdata), timeout)

        if bytes_read < 0:
            raise HIDException("Failed to read from HID device: " + str(bytes_read))
        elif bytes_read == 0:
            return []
        else:
            return bytearray(cdata)

Response: Read from the HID device on its endpoint.


Instruction:
Write a Python 3 script to
check if the USB device is still connected to the USB device.

Response:
def is_connected(self):
        """
        Checks if the USB device is still connected
        """
        if self._is_open:
            err = hidapi.hid_read_timeout(self._device, ffi.NULL, 0, 0)
            if err == -1:
                return False
            else:
                return True
        else:
            en = Enumeration(vid=self.vendor_id, pid=self.product_id).find(path=self.path)
            if len(en) == 0:
                return False
            else:
                return True


Instruction:
Can you tell what is the following Python 3 function doing
def send_feature_report(self, data, report_id=0x00):
        if not self._is_open:
            raise HIDException("HIDDevice not open")

        report = bytearray([report_id]) + bytearray(data)
        cdata = ffi.new("const unsigned char[]", bytes(report))
        bytes_written = hidapi.hid_send_feature_report(self._device, cdata, len(report))

        if bytes_written == -1:
            raise HIDException("Failed to send feature report to HID device")

        return bytes_written

Response: Send a Feature report to a HID device.


Instruction:
Can you tell what is the following Python 3 function doing
def get_feature_report(self, size, report_id=0x00):
        data = [0] * (size+1)
        cdata = ffi.new("unsigned char[]", bytes(data))
        cdata[0] = report_id

        bytes_read = hidapi.hid_get_feature_report(self._device, cdata, len(cdata))

        if bytes_read == -1:
            raise HIDException("Failed to get feature report from HID device")

        return bytearray(cdata[1:size+1])

Response: Get a feature report from a HID device.


Instruction:
Can you implement a function in Python 3 that
gets an error string from the device

Response:
def get_error(self):
        """
        Get an error string from the device
        """
        err_str = hidapi.hid_error(self._device)
        if err_str == ffi.NULL:
            return None
        else:
            return ffi.string(err_str)


Instruction:
How would you explain what the following Python 3 function does
def get_indexed_string(self, index):
        max_len = 128
        str_buf = ffi.new("wchar_t[]", str(bytearray(max_len)))
        ret = hidapi.hid_get_indexed_string(self._device, index, str_buf, max_len)

        if ret < 0:
            raise HIDException(self._device.get_error())
        elif ret == 0:
            return None
        else:
            return ffi.string(str_buf).encode('utf-8')

Response: Get the string with the given index from the device.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def description(self):
        return \
"""HIDDevice:
    {} | {:x}:{:x} | {} | {} | {}
    release_number: {}
    usage_page: {}
    usage: {}
    interface_number: {}\
""".format(self.path,
           self.vendor_id,
           self.product_id,
           self.manufacturer_string,
           self.product_string,
           self.serial_number,
           self.release_number,
           self.usage_page,
           self.usage,
           self.interface_number
        )

Response: Get a string describing the HID device.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def find(self, vid=None, pid=None, serial=None, interface=None, \
            path=None, release_number=None, manufacturer=None,
            product=None, usage=None, usage_page=None):
        """
        Attempts to open a device in this `Enumeration` object. Optional
        arguments can be provided to filter the resulting list based on various
        parameters of the HID devices.

        Args:
            vid: filters by USB Vendor ID
            pid: filters by USB Product ID
            serial: filters by USB serial string (.iSerialNumber)
            interface: filters by interface number (bInterfaceNumber)
            release_number: filters by the USB release number (.bcdDevice)
            manufacturer: filters by USB manufacturer string (.iManufacturer)
            product: filters by USB product string (.iProduct)
            usage: filters by HID usage
            usage_page: filters by HID usage_page
            path: filters by HID API path.
        """
        result = []

        for dev in self.device_list:
            if vid not in [0, None] and dev.vendor_id != vid:
                continue
            if pid not in [0, None] and dev.product_id != pid:
                continue
            if serial and dev.serial_number != serial:
                continue
            if path and dev.path != path:
                continue
            if manufacturer and dev.manufacturer_string != manufacturer:
                continue
            if product and dev.product_string != product:
                continue
            if release_number != None and dev.release_number != release_number:
                continue
            if interface != None and dev.interface_number != interface:
                continue
            if usage != None and dev.usage != usage:
                continue
            if usage_page != None and dev.usage_page != usage_page:
                continue
            result.append(dev)
        return result

Response: Finds a specific HID device in this Enumeration object.


Instruction:
Implement a function in Python 3 to
retrieve all the tracks of the album

Response:
def get_tracks(self):
        """
        Retrieves all the tracks of the album if they haven't been retrieved yet
        :return: List. Tracks of the current album
        """
        if not self._track_list:
            tracks = itunespy.lookup(id=self.collection_id, entity=itunespy.entities['song'])[1:]
            for track in tracks:
                self._track_list.append(track)
        return self._track_list


Instruction:
Write a Python 3 script for
getting all of the track s length and returns the sum of all the track s length and the sum of all the track s length

Response:
def get_album_time(self, round_number=2):
        """
        Retrieves all of the track's length and returns the sum of all
        :param round_number: Int. Number of decimals to round the sum
        :return: Int. Sum of all the track's length
        """
        if not self._track_list:
            self.get_tracks()
        if self._album_time is None:
            album_time = 0.0
            for track in self._track_list:
                album_time += track.get_track_time_minutes()
            self._album_time = round(album_time, round_number)
        return self._album_time


Instruction:
Here you have a function in Python 3, explain what it does
def get_movies(self):
        return itunespy.lookup(id=self.artist_id, entity=itunespy.entities['movie'])[1:]

Response: Retrieves all the movies published by the artist


Instruction:
Can you generate a brief explanation for the following Python 3 code
def solarcalcs(self):

        self.dir = self.forc.dir     # Direct sunlight (perpendicular to the sun's ray)
        self.dif = self.forc.dif     # Diffuse sunlight

        if self.dir + self.dif > 0.:

            self.logger.debug("{} Solar radiation > 0".format(__name__))

            # calculate zenith tangent, and critOrient solar angles
            self.solarangles()

            self.horSol = max(math.cos(self.zenith)*self.dir, 0.0)            # Direct horizontal radiation
            # Fractional terms for wall & road
            self.Kw_term = min(abs(1./self.UCM.canAspect*(0.5-self.critOrient/math.pi) \
                + 1/math.pi*self.tanzen*(1-math.cos(self.critOrient))),1.)
            self.Kr_term = min(abs(2.*self.critOrient/math.pi \
                - (2/math.pi*self.UCM.canAspect*self.tanzen)*(1-math.cos(self.critOrient))), 1-2*self.UCM.canAspect*self.Kw_term)

            # Direct and diffuse solar radiation
            self.bldSol = self.horSol*self.Kw_term + self.UCM.wallConf*self.dif   # Assume trees are shorter than buildings
            self.roadSol = self.horSol*self.Kr_term + self.UCM.roadConf*self.dif

            # Solar reflections. Add diffuse radiation from vegetation to alb_road if in season
            if self.simTime.month < self.parameter.vegStart or self.simTime.month > self.parameter.vegEnd:
                alb_road = self.UCM.road.albedo
            else:
                alb_road = self.UCM.road.albedo*(1.-self.UCM.road.vegCoverage) + self.parameter.vegAlbedo*self.UCM.road.vegCoverage

            # First set of reflections
            rr = alb_road * self.roadSol
            rw = self.UCM.alb_wall * self.bldSol

            # bounces
            fr = (1. - (1. - 2.*self.UCM.wallConf) * self.UCM.alb_wall + (1. - self.UCM.roadConf) \
            * self.UCM.wallConf * alb_road * self.UCM.alb_wall)

            # (1.0-self.UCM.roadConf) road to wall view
            self.mr = (rr + (1.0-self.UCM.roadConf) * alb_road * (rw + self.UCM.wallConf * self.UCM.alb_wall * rr)) / fr
            self.mw = (rw + self.UCM.wallConf * self.UCM.alb_wall * rr) / fr

            # Receiving solar, including bounces (W m-2)
            self.UCM.road.solRec = self.roadSol + (1 - self.UCM.roadConf)*self.mw

            for j in range(len(self.BEM)):
                self.BEM[j].roof.solRec = self.horSol + self.dif
                self.BEM[j].wall.solRec = self.bldSol + (1 - 2*self.UCM.wallConf) * self.mw + self.UCM.wallConf * self.mr

            self.rural.solRec = self.horSol + self.dif            # Solar received by rural
            self.UCM.SolRecRoof = self.horSol + self.dif          # Solar received by roof
            self.UCM.SolRecRoad = self.UCM.road.solRec            # Solar received by road
            self.UCM.SolRecWall = self.bldSol+(1-2*self.UCM.wallConf)*self.UCM.road.albedo*self.roadSol    # Solar received by wall

            # Vegetation heat (per m^2 of veg)
            self.UCM.treeSensHeat = (1-self.parameter.vegAlbedo)*(1-self.parameter.treeFLat)*self.UCM.SolRecRoad
            self.UCM.treeLatHeat = (1-self.parameter.vegAlbedo)*self.parameter.treeFLat*self.UCM.SolRecRoad

        else:    # No Sun

            self.logger.debug("{} Solar radiation = 0".format(__name__))

            self.UCM.road.solRec = 0.
            self.rural.solRec = 0.

            for j in range(len(self.BEM)):
                self.BEM[j].roof.solRec = 0.
                self.BEM[j].wall.solRec = 0.

            self.UCM.SolRecRoad = 0.         # Solar received by road
            self.UCM.SolRecRoof = 0.         # Solar received by roof
            self.UCM.SolRecWall = 0.         # Solar received by wall
            self.UCM.treeSensHeat = 0.
            self.UCM.treeLatHeat = 0.

        return self.rural, self.UCM, self.BEM

Response: Returns a list of solar calibrations for the current object.


Instruction:
Implement a function in Python 3 to
calculate solar angles for the current canyon and the solar declination angle and critical canyon angle for the current canyon and solar radiation.

Response:
def solarangles (self):
        """
        Calculation based on NOAA. Solves for zenith angle, tangent of zenithal angle,
        and critical canyon angle based on following parameters:
            canAspect       # aspect Ratio of canyon
            simTime         # simulation parameters
            RSM.lon         # longitude (deg)
            RSM.lat         # latitude (deg)
            RSM.GMT         # GMT hour correction

        Properties
            self.ut         # elapsed hours on current day
            self.ad         # fractional year in radians
            self.eqtime
            self.decsol     # solar declination angle
            self.zenith     # Angle between normal to earth's surface and sun position
            self.tanzen     # tangente of solar zenithal angle
            self.critOrient # critical canyon angle for which solar radiation reaches the road
        """

        ln = self.RSM.lon

        month = self.simTime.month
        day = self.simTime.day
        secDay = self.simTime.secDay    # Total elapsed seconds in simulation
        inobis = self.simTime.inobis    # total days for first of month
                                        #  i.e [0,31,59,90,120,151,181,212,243,273,304,334]
        canAspect = self.UCM.canAspect
        lon = self.RSM.lon
        lat = self.RSM.lat
        GMT = self.RSM.GMT

        self.ut = (24. + (int(secDay)/3600.%24.)) % 24. # Get elapsed hours on current day
        ibis = list(range(len(inobis)))

        for JI in range(1, 12):
             ibis[JI] = inobis[JI]+1

        date = day + inobis[month-1]-1 # Julian day of the year
        # divide circle by 365 days, multiply by elapsed days + hours
        self.ad = 2.0 * math.pi/365. * (date-1 + (self.ut-(12/24.)))     # Fractional year (radians)

        self.eqtime = 229.18 * (0.000075+0.001868*math.cos(self.ad)-0.032077*math.sin(self.ad) - \
            0.01461*math.cos(2*self.ad)-0.040849*math.sin(2*self.ad))

        # Declination angle (angle of sun with equatorial plane)
        self.decsol = 0.006918-0.399912*math.cos(self.ad)+0.070257*math.sin(self.ad) \
            -0.006758*math.cos(2.*self.ad)+0.000907*math.sin(2.*self.ad) \
            -0.002697*math.cos(3.*self.ad)+0.00148 *math.sin(3.*self.ad)

        time_offset = self.eqtime - 4. * lon + 60 * GMT
        tst = secDay + time_offset * 60

        ha = (tst/4./60.-180.) * math.pi/180.
        zlat = lat * (math.pi/180.)   # change angle units to radians

        # Calculate zenith solar angle
        self.zenith = math.acos(math.sin(zlat)*math.sin(self.decsol) + math.cos(zlat)*math.cos(self.decsol)*math.cos(ha))

        # tangente of solar zenithal angle
        if abs(0.5*math.pi - self.zenith) < 1e-6:
            if 0.5*math.pi - self.zenith > 0.:
                self.tanzen = math.tan(0.5*math.pi-1e-6)

            elif 0.5*math.pi - self.zenith <= 0.:
                self.tanzen = math.tan(0.5*math.pi+1e-6)

        elif abs(self.zenith) < 1e-6:
            # lim x->0 tan(x) -> 0 which results in division by zero error
            # when calculating the critical canyon angle
            # so set tanzen to 1e-6 which will result in critical canyon angle = 90
            self.tanzen = 1e-6

        else:
            self.tanzen = math.tan(self.zenith)

        # critical canyon angle for which solar radiation reaches the road
        # has to do with street canyon orientation for given solar angle
        self.critOrient = math.asin(min(abs( 1./self.tanzen)/canAspect, 1. ))


Instruction:
How would you code a function in Python 3 to
modify version of Psychometrics by Tea Zakula MIT Building Technology Lab Input: Tdb_in, w_in, P Output: Tdb, w, phi, h, Tdp, v where: Tdb_in = [K] dry bulb temperature w_in = [kgv/kgda] Humidity Ratio P = [P] Atmospheric Station Pressure Tdb: [C] dry bulb temperature w: [kgv/kgda] Humidity Ratio phi: [Pw/Pws*100] relative humidity Tdp: [C] dew point temperature h: [J/kga] enthalpy v: [m3/kga] specific volume

Response:
def psychrometrics (Tdb_in, w_in, P):
    """
    Modified version of Psychometrics by Tea Zakula
    MIT Building Technology Lab
    Input: Tdb_in, w_in, P
    Output: Tdb, w, phi, h, Tdp, v

    where:
    Tdb_in = [K] dry bulb temperature
    w_in = [kgv/kgda] Humidity Ratio
    P = [P] Atmospheric Station Pressure

    Tdb: [C] dry bulb temperature
    w: [kgv/kgda] Humidity Ratio
    phi: [Pw/Pws*100] relative humidity
    Tdp: [C] dew point temperature
    h: [J/kga] enthalpy
    v: [m3/kga] specific volume
    """
    # Change units
    c_air = 1006.   # [J/kg] air heat capacity, value from ASHRAE Fundamentals
    hlg = 2501000.  # [J/kg] latent heat, value from ASHRAE Fundamentals
    cw  = 1860.     # [J/kg] value from ASHRAE Fundamentals
    P = P/1000.     # convert from Pa to kPa

    Tdb = Tdb_in - 273.15
    w = w_in

    # phi (RH) calculation from Tdb and w
    Pw = (w*P)/(0.621945 + w)                             # partial pressure of water vapor
    Pws = saturation_pressure(Tdb)                        # Get saturation pressure for given Tdb
    phi = Pw/Pws*100.0

    # enthalpy calculation from Tdb and w
    h = c_air*Tdb + w*(hlg+cw*Tdb)                      # [J kga-1]

    # specific volume calculation from Tdb and w
    v = 0.287042 * (Tdb+273.15)*(1+1.607858*w)/P        # ?

    # dew point calculation from w
    _pw = (w*P)/(0.621945 + w) # water vapor partial pressure in kPa
    alpha = log(_pw)

    Tdp = 6.54 + 14.526*alpha + pow(alpha,2)*0.7389 + pow(alpha,3)*0.09486 + pow(_pw,0.1984)*0.4569  # valid for Tdp between 0 C and 93 C

    return  Tdb, w, phi, h, Tdp, v


Instruction:
Can you write a function in Python 3 where it
searches for a term in an array of result_item objects.

Response:
def search(term, country='US', media='all', entity=None, attribute=None, limit=50):
    """
    Returns the result of the search of the specified term in an array of result_item(s)
    :param term: String. The URL-encoded text string you want to search for. Example: Steven Wilson.
                 The method will take care of spaces so you don't have to.
    :param country: String. The two-letter country code for the store you want to search.
                    For a full list of the codes: http://en.wikipedia.org/wiki/%20ISO_3166-1_alpha-2
    :param media: String. The media type you want to search for. Example: music
    :param entity: String. The type of results you want returned, relative to the specified media type. Example: musicArtist.
                   Full list: musicArtist, musicTrack, album, musicVideo, mix, song
    :param attribute: String. The attribute you want to search for in the stores, relative to the specified media type.
    :param limit: Integer. The number of search results you want the iTunes Store to return.
    :return: An array of result_item(s)
    """
    search_url = _url_search_builder(term, country, media, entity, attribute, limit)
    r = requests.get(search_url)

    try:
        json = r.json()['results']
        result_count = r.json()['resultCount']
    except:
        raise ConnectionError(general_no_connection)

    if result_count == 0:
        raise LookupError(search_error + str(term))

    return _get_result_list(json)


Instruction:
Explain what the following Python 3 code does
def lookup(id=None, artist_amg_id=None, upc=None, country='US', media='all', entity=None, attribute=None, limit=50):
    # If none of the basic lookup arguments are provided, raise a ValueError
    if id is None and artist_amg_id is None and upc is None:
        raise ValueError(lookup_no_ids)

    lookup_url = _url_lookup_builder(id, artist_amg_id, upc, country, media, entity, attribute, limit)
    r = requests.get(lookup_url)

    try:
        json = r.json()['results']
        result_count = r.json()['resultCount']
    except:
        raise ConnectionError(general_no_connection)

    if result_count == 0:
        raise LookupError(lookup_error)

    return _get_result_list(json)

Response: Returns the result of the lookup of the specified id artist_amg_id or upc in an array of result_item objects.


Instruction:
Write a Python 3 script for
analyzing the provided JSON data and returns an array of result_item s based on its content

Response:
def _get_result_list(json):
    """
    Analyzes the provided JSON data and returns an array of result_item(s) based on its content
    :param json: Raw JSON data to analyze
    :return: An array of result_item(s) from the provided JSON data
    """
    result_list = []

    for item in json:
        if 'wrapperType' in item:
            # Music
            if item['wrapperType'] == 'artist' and item['artistType'] == 'Artist':
                music_artist_result = music_artist.MusicArtist(item)
                result_list.append(music_artist_result)
            elif item['wrapperType'] == 'collection' and item['collectionType'] == 'Album':
                music_album_result = music_album.MusicAlbum(item)
                result_list.append(music_album_result)
            elif item['wrapperType'] == 'track' and item['kind'] == 'song':
                music_track_result = track.Track(item)
                result_list.append(music_track_result)
            elif item['wrapperType'] == 'track' and item['kind'] == 'music-video':
                music_video_result = track.Track(item)
                result_list.append(music_video_result)
            # Movies
            elif item['wrapperType'] == 'artist' and item['artistType'] == 'Movie Artist':
                movie_artist_result = movie_artist.MovieArtist(item)
                result_list.append(movie_artist_result)
            elif item['wrapperType'] == 'track' and item['kind'] == 'feature-movie':
                movie_result = track.Track(item)
                result_list.append(movie_result)
            # Ebook Author
            elif item['wrapperType'] == 'artist' and item['artistType'] == 'Author':
                ebook_artist_result = ebook_artist.EbookArtist(item)
                result_list.append(ebook_artist_result)
            # Tv Shows
            elif item['wrapperType'] == 'collection' and item['collectionType'] == 'TV Season':
                tv_season_result = result_item.ResultItem(item)
                result_list.append(tv_season_result)
            elif item['wrapperType'] == 'track' and item['kind'] == 'tv-episode':
                tv_episode_result = track.Track(item)
                result_list.append(tv_episode_result)
            # Software
            elif item['wrapperType'] == 'software' and item['kind'] == 'software':
                software_result = result_item.ResultItem(item)
                result_list.append(software_result)
            elif item['wrapperType'] == 'software' and item['kind'] == 'mac-software':
                mac_software_result = result_item.ResultItem(item)
                result_list.append(mac_software_result)

        elif 'kind' in item and item['kind'] == 'ebook':
            ebook_result = result_item.ResultItem(item)
            result_list.append(ebook_result)
        else:
            unknown_result = result_item.ResultItem(item)
            result_list.append(unknown_result)

    return result_list


Instruction:
Write a Python 3 function that can
build the URL to perform the search based on the provided data.

Response:
def _url_search_builder(term, country='US', media='all', entity=None, attribute=None, limit=50):
    """
    Builds the URL to perform the search based on the provided data
    :param term: String. The URL-encoded text string you want to search for. Example: Steven Wilson.
                 The method will take care of spaces so you don't have to.
    :param country: String. The two-letter country code for the store you want to search.
                    For a full list of the codes: http://en.wikipedia.org/wiki/%20ISO_3166-1_alpha-2
    :param media: String. The media type you want to search for. Example: music
    :param entity: String. The type of results you want returned, relative to the specified media type. Example: musicArtist.
                   Full list: musicArtist, musicTrack, album, musicVideo, mix, song
    :param attribute: String. The attribute you want to search for in the stores, relative to the specified media type.
    :param limit: Integer. The number of search results you want the iTunes Store to return.
    :return: The built URL as a string
    """
    built_url = base_search_url + _parse_query(term)
    built_url += ampersand + parameters[1] + country
    built_url += ampersand + parameters[2] + media

    if entity is not None:
        built_url += ampersand + parameters[3] + entity

    if attribute is not None:
        built_url += ampersand + parameters[4] + attribute

    built_url += ampersand + parameters[5] + str(limit)

    return built_url


Instruction:
Here you have a function in Python 3, explain what it does
def _url_lookup_builder(id=None, artist_amg_id=None, upc=None, country='US', media='music', entity=None, attribute=None,
                        limit=50):
    """
    Builds the URL to perform the lookup based on the provided data
    :param id: String. iTunes ID of the artist, album, track, ebook or software
    :param artist_amg_id: String. All Music Guide ID of the artist
    :param upc: String. UPCs/EANs
    :param country: String. The two-letter country code for the store you want to search.
                    For a full list of the codes: http://en.wikipedia.org/wiki/%20ISO_3166-1_alpha-2
    :param media: String. The media type you want to search for. Example: music
    :param entity: String. The type of results you want returned, relative to the specified media type. Example: musicArtist.
                   Full list: musicArtist, musicTrack, album, musicVideo, mix, song
    :param attribute: String. The attribute you want to search for in the stores, relative to the specified media type.
    :param limit: Integer. The number of search results you want the iTunes Store to return.
    :return: The built URL as a string
    """
    built_url = base_lookup_url
    has_one_argument = False

    if id is not None:
        built_url += parameters[6] + str(id)
        has_one_argument = True

    if artist_amg_id is not None:
        if has_one_argument:
            built_url += ampersand + parameters[7] + artist_amg_id
        else:
            built_url += parameters[7] + str(artist_amg_id)
            has_one_argument = True

    if upc is not None:
        if has_one_argument:
            built_url += ampersand + parameters[8] + upc
        else:
            built_url += parameters[8] + str(upc)

    built_url += ampersand + parameters[1] + country
    built_url += ampersand + parameters[2] + media

    if entity is not None:
        built_url += ampersand + parameters[3] + entity

    if attribute is not None:
        built_url += ampersand + parameters[4] + attribute

    built_url += ampersand + parameters[5] + str(limit)

    return built_url

Response: Builds the URL to perform the lookup based on the provided data.


Instruction:
Can you write a function in Python 3 where it
retrieves all the albums published by the artist

Response:
def get_albums(self):
        """
        Retrieves all the albums by the artist
        :return: List. Albums published by the artist
        """
        return itunespy.lookup(id=self.artist_id, entity=itunespy.entities['album'])[1:]


Instruction:
Can you generate a brief explanation for the following Python 3 code
def str2fl(x):
    def helper_to_fl(s_):
        """ deals with odd string imports converts to float"""
        if s_ == "":
            return "null"
        elif "," in s_:
            s_ = s_.replace(",", "")

        try:
            return float(s_)
        except:
            return (s_)

    fl_lst = []
    if isinstance(x[0], str):                # Check if list of strings, then sent to conversion
        for xi in range(len(x)):
            fl_lst.append(helper_to_fl(x[xi]))
    elif isinstance(x[0], list):                    # Check if list of lists, then recurse
        for xi in range(len(x)):
            fl_lst.append(str2fl(x[xi]))
    else:
        return False

    return fl_lst

Response: Recurses through lists and converts lists of strings to float


Instruction:
Explain what the following Python 3 code does
def procMat(materials, max_thickness, min_thickness):
    newmat = []
    newthickness = []
    k = materials.layerThermalCond
    Vhc = materials.layerVolHeat

    if len(materials.layerThickness) > 1:

        for j in range(len(materials.layerThickness)):
            # Break up each layer that's more than max thickness (0.05m)
            if materials.layerThickness[j] > max_thickness:
                nlayers = math.ceil(materials.layerThickness[j]/float(max_thickness))
                for i in range(int(nlayers)):
                    newmat.append(Material(k[j], Vhc[j], name=materials._name))
                    newthickness.append(materials.layerThickness[j]/float(nlayers))
            # Material that's less then min_thickness is not added.
            elif materials.layerThickness[j] < min_thickness:
                print("WARNING: Material '{}' layer found too thin (<{:.2f}cm), ignored.").format(
                    materials._name, min_thickness*100)
            else:
                newmat.append(Material(k[j], Vhc[j], name=materials._name))
                newthickness.append(materials.layerThickness[j])

    else:

        # Divide single layer into two (uwg assumes at least 2 layers)
        if materials.layerThickness[0] > max_thickness:
            nlayers = math.ceil(materials.layerThickness[0]/float(max_thickness))
            for i in range(int(nlayers)):
                newmat.append(Material(k[0], Vhc[0], name=materials._name))
                newthickness.append(materials.layerThickness[0]/float(nlayers))
        # Material should be at least 1cm thick, so if we're here,
        # should give warning and stop. Only warning given for now.
        elif materials.layerThickness[0] < min_thickness*2:
            newthickness = [min_thickness/2., min_thickness/2.]
            newmat = [Material(k[0], Vhc[0], name=materials._name),
                      Material(k[0], Vhc[0], name=materials._name)]
            print("WARNING: a thin (<2cm) single material '{}' layer found. May cause error.".format(
                materials._name))
        else:
            newthickness = [materials.layerThickness[0]/2., materials.layerThickness[0]/2.]
            newmat = [Material(k[0], Vhc[0], name=materials._name),
                      Material(k[0], Vhc[0], name=materials._name)]
    return newmat, newthickness

Response: Processes a material layer so that it is too narrowed and that it is too narrowed.


Instruction:
Create a Python 3 function for
reading the UWG_Matlab input file and populate self. _init_param_dict with the values extracted from the UWG_Matlab parameter file.

Response:
def read_input(self):
        """Section 3 - Read Input File (.m, file)
        Note: UWG_Matlab input files are xlsm, XML, .m, file.
        properties:
            self._init_param_dict   # dictionary of simulation initialization parameters

            self.sensAnth           # non-building sensible heat (W/m^2)
            self.SchTraffic         # Traffice schedule

            self.BEM                # list of BEMDef objects extracted from readDOE
            self.Sch                # list of Schedule objects extracted from readDOE

        """

        uwg_param_file_path = os.path.join(self.uwgParamDir, self.uwgParamFileName)

        if not os.path.exists(uwg_param_file_path):
            raise Exception("Param file: '{}' does not exist.".format(uwg_param_file_path))

        # Open .uwg file and feed csv data to initializeDataFile
        try:
            uwg_param_data = utilities.read_csv(uwg_param_file_path)
        except Exception as e:
            raise Exception("Failed to read .uwg file! {}".format(e.message))

        # The initialize.uwg is read with a dictionary so that users changing
        # line endings or line numbers doesn't make reading input incorrect
        self._init_param_dict = {}
        count = 0
        while count < len(uwg_param_data):
            row = uwg_param_data[count]
            row = [row[i].replace(" ", "") for i in range(len(row))]  # strip white spaces

            # Optional parameters might be empty so handle separately
            is_optional_parameter = (
                row != [] and
                (
                    row[0] == "albRoof" or
                    row[0] == "vegRoof" or
                    row[0] == "glzR" or
                    row[0] == "hvac" or
                    row[0] == "albWall" or
                    row[0] == "SHGC"
                )
            )
            try:
                if row == [] or "#" in row[0]:
                    count += 1
                    continue
                elif row[0] == "SchTraffic":
                    # SchTraffic: 3 x 24 matrix
                    trafficrows = uwg_param_data[count+1:count+4]
                    self._init_param_dict[row[0]] = [utilities.str2fl(r[:24]) for r in trafficrows]
                    count += 4
                elif row[0] == "bld":
                    # bld: 17 x 3 matrix
                    bldrows = uwg_param_data[count+1:count+17]
                    self._init_param_dict[row[0]] = [utilities.str2fl(r[:3]) for r in bldrows]
                    count += 17
                elif is_optional_parameter:
                    self._init_param_dict[row[0]] = float(row[1]) if row[1] != "" else None
                    count += 1
                else:
                    self._init_param_dict[row[0]] = float(row[1])
                    count += 1
            except ValueError:
                print("Error while reading parameter at {} {}".format(count, row))

        ipd = self._init_param_dict

        # Define Simulation and Weather parameters
        if self.Month is None: self.Month = ipd['Month']
        if self.Day is None: self.Day = ipd['Day']
        if self.nDay is None: self.nDay = ipd['nDay']
        if self.dtSim is None: self.dtSim = ipd['dtSim']
        if self.dtWeather is None: self.dtWeather = ipd['dtWeather']

        # HVAC system and internal laod
        if self.autosize is None: self.autosize = ipd['autosize']
        if self.sensOcc is None: self.sensOcc = ipd['sensOcc']
        if self.LatFOcc is None: self.LatFOcc = ipd['LatFOcc']
        if self.RadFOcc is None: self.RadFOcc = ipd['RadFOcc']
        if self.RadFEquip is None: self.RadFEquip = ipd['RadFEquip']
        if self.RadFLight is None: self.RadFLight = ipd['RadFLight']

        # Define Urban microclimate parameters
        if self.h_ubl1 is None: self.h_ubl1 = ipd['h_ubl1']
        if self.h_ubl2 is None: self.h_ubl2 = ipd['h_ubl2']
        if self.h_ref is None: self.h_ref = ipd['h_ref']
        if self.h_temp is None: self.h_temp = ipd['h_temp']
        if self.h_wind is None: self.h_wind = ipd['h_wind']
        if self.c_circ is None: self.c_circ = ipd['c_circ']
        if self.c_exch is None: self.c_exch = ipd['c_exch']
        if self.maxDay is None: self.maxDay = ipd['maxDay']
        if self.maxNight is None: self.maxNight = ipd['maxNight']
        if self.windMin is None: self.windMin = ipd['windMin']
        if self.h_obs is None: self.h_obs = ipd['h_obs']

        # Urban characteristics
        if self.bldHeight is None: self.bldHeight = ipd['bldHeight']
        if self.h_mix is None: self.h_mix = ipd['h_mix']
        if self.bldDensity is None: self.bldDensity = ipd['bldDensity']
        if self.verToHor is None: self.verToHor = ipd['verToHor']
        if self.charLength is None: self.charLength = ipd['charLength']
        if self.alb_road is None: self.alb_road = ipd['albRoad']
        if self.d_road is None: self.d_road = ipd['dRoad']
        if self.sensAnth is None: self.sensAnth = ipd['sensAnth']
        # if self.latAnth is None: self.latAnth = ipd['latAnth'] # Not used, taken out by JH.

        # climate Zone
        if self.zone is None: self.zone = ipd['zone']

        # Vegetation parameters
        if self.vegCover is None: self.vegCover = ipd['vegCover']
        if self.treeCoverage is None: self.treeCoverage = ipd['treeCoverage']
        if self.vegStart is None: self.vegStart = ipd['vegStart']
        if self.vegEnd is None: self.vegEnd = ipd['vegEnd']
        if self.albVeg is None: self.albVeg = ipd['albVeg']
        if self.rurVegCover is None: self.rurVegCover = ipd['rurVegCover']
        if self.latGrss is None: self.latGrss = ipd['latGrss']
        if self.latTree is None: self.latTree = ipd['latTree']

        # Define Traffic schedule
        if self.SchTraffic is None: self.SchTraffic = ipd['SchTraffic']

        # Define Road (Assume 0.5m of asphalt)
        if self.kRoad is None: self.kRoad = ipd['kRoad']
        if self.cRoad is None: self.cRoad = ipd['cRoad']

        # Building stock fraction
        if self.bld is None: self.bld = ipd['bld']

        # Optional parameters
        if self.albRoof is None: self.albRoof = ipd['albRoof']
        if self.vegRoof is None: self.vegRoof = ipd['vegRoof']
        if self.glzR is None: self.glzR = ipd['glzR']
        if self.albWall is None: self.albWall = ipd['albWall']
        if self.SHGC is None: self.SHGC = ipd['SHGC']


Instruction:
Write a Python 3 function for
setting inputs from. uwg input file if not already defined.

Response:
def set_input(self):
        """ Set inputs from .uwg input file if not already defined, the check if all
        the required input parameters are there.
        """

        # If a uwgParamFileName is set, then read inputs from .uwg file.
        # User-defined class properties will override the inputs from the .uwg file.
        if self.uwgParamFileName is not None:
            print("\nReading uwg file input.")
            self.read_input()
        else:
            print("\nNo .uwg file input.")

        self.check_required_inputs()

        # Modify zone to be used as python index
        self.zone = int(self.zone)-1


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

        if not os.path.exists(self.readDOE_file_path):
            raise Exception("readDOE.pkl file: '{}' does not exist.".format(readDOE_file_path))

        readDOE_file = open(self.readDOE_file_path, 'rb')  # open pickle file in binary form
        refDOE = pickle.load(readDOE_file)
        refBEM = pickle.load(readDOE_file)
        refSchedule = pickle.load(readDOE_file)
        readDOE_file.close()

        # Define building energy models
        k = 0
        self.r_glaze_total = 0.             # Glazing ratio for total building stock
        self.SHGC_total = 0.                # SHGC addition for total building stock
        self.alb_wall_total = 0.            # albedo wall addition for total building stock
        h_floor = self.flr_h or 3.05  # average floor height

        total_urban_bld_area = math.pow(self.charLength, 2)*self.bldDensity * \
            self.bldHeight/h_floor  # total building floor area
        area_matrix = utilities.zeros(16, 3)

        self.BEM = []           # list of BEMDef objects
        self.Sch = []           # list of Schedule objects

        for i in range(16):    # 16 building types
            for j in range(3):  # 3 built eras
                if self.bld[i][j] > 0.:
                    # Add to BEM list
                    self.BEM.append(refBEM[i][j][self.zone])
                    self.BEM[k].frac = self.bld[i][j]
                    self.BEM[k].fl_area = self.bld[i][j] * total_urban_bld_area

                    # Overwrite with optional parameters if provided
                    if self.glzR:
                        self.BEM[k].building.glazingRatio = self.glzR
                    if self.albRoof:
                        self.BEM[k].roof.albedo = self.albRoof
                    if self.vegRoof:
                        self.BEM[k].roof.vegCoverage = self.vegRoof
                    if self.SHGC:
                        self.BEM[k].building.shgc = self.SHGC
                    if self.albWall:
                        self.BEM[k].wall.albedo = self.albWall
                    if self.flr_h:
                        self.BEM[k].building.floorHeight = self.flr_h

                    # Keep track of total urban r_glaze, SHGC, and alb_wall for UCM model
                    self.r_glaze_total += self.BEM[k].frac * self.BEM[k].building.glazingRatio
                    self.SHGC_total += self.BEM[k].frac * self.BEM[k].building.shgc
                    self.alb_wall_total += self.BEM[k].frac * self.BEM[k].wall.albedo
                    # Add to schedule list
                    self.Sch.append(refSchedule[i][j][self.zone])
                    k += 1

Response: Initialize the instance variables for the BEM and Sch objects.