Instruction:
Create a Python 3 function to
tell this legislator object which session to use when calculating the legislator s context_role for a given bill or vote.

Response:
def context_role(self, bill=None, vote=None, session=None, term=None):
        '''Tell this legislator object which session to use when calculating
        the legisator's context_role for a given bill or vote.
        '''
        # If no hints were given about the context, look for a related bill,
        # then for a related vote.
        if not any([bill, vote, session, term]):
            try:
                bill = self.bill
            except AttributeError:
                # A vote?
                try:
                    vote = self.vote
                except AttributeError:
                    # If we're here, this method was called on a
                    # Legislator was that doesn't have a related bill or vote.
                    return ''

        # If we still have to historical point of reference, figuring
        # out the context role is impossible. Return emtpy string.
        if not any([bill, vote, session, term]):
            return ''

        # First figure out the term.
        if bill is not None:
            term = bill['_term']

        elif vote is not None:
            try:
                _bill = vote.bill
            except AttributeError:
                _bill = BillVote(vote).bill
            if callable(_bill):
                _bill = _bill()
            term = _bill['_term']

        if term is None and session is not None:
            term = term_for_session(self[settings.LEVEL_FIELD], session)

        # Use the term to get the related roles. First look in the current
        # roles list, then fail over to the old_roles list.
        roles = [r for r in self['roles']
                 if r.get('type') == 'member' and r.get('term') == term]
        roles = list(filter(None, roles))

        if not roles:
            roles = [r for r in self.get('old_roles', {}).get(term, [])
                     if r.get('type') == 'member']
        roles = list(filter(None, roles))

        if not roles:
            # Legislator had no roles for this term. If there is a related
            # bill ro vote, this shouldn't happen, but could if the
            # legislator's roles got deleted.
            return ''

        # If there's only one applicable role, we're done.
        if len(roles) == 1:
            role = roles.pop()
            self['context_role'] = role
            return role

        # If only one of term or session is given and there are multiple roles:
        if not list(filter(None, [bill, vote])):
            if term is not None:
                role = roles[0]
                self['context_role'] = role
                return role

            # Below, use the date of the related bill or vote to determine
            # which (of multiple) roles applies.
            # Get the context date.
            if session is not None:
                # If we're here, we have multiple roles for a single session.
                # Try to find the correct one in self.metadata,
                # else give up.
                session_data = self.metadata['session_details'][session]
                for role in roles:
                    role_start = role.get('start_date')
                    role_end = role.get('end_date')

                    # Return the first role that overlaps at all with the
                    # session.
                    session_start = session_data.get('start_date')
                    session_end = session_data.get('end_date')
                    if session_start and session_end:
                        started_during = (role_start < session_start <
                                          role_end)
                        ended_during = (role_start < session_end < role_end)
                        if started_during or ended_during:
                            self['context_role'] = role
                            return role
                    else:
                        continue

                # Return first role from the session?
                role = roles[0]
                self['context_role'] = role
                return role

        if vote is not None:
            date = vote['date']
        if bill is not None:
            date = bill['action_dates']['first']

        dates_exist = False
        for role in roles:
            start_date = role.get('start_date')
            end_date = role.get('end_date')
            if start_date and end_date:
                dates_exist = True
                if start_date < date < end_date:
                    self['context_role'] = role
                    return role

        if dates_exist:
            # If we're here, the context date didn't fall into any of the
            # legislator's role date ranges.
            return ''

        else:
            # Here the roles didn't have date ranges. Return the last one?
            role = roles.pop()
            self['context_role'] = role
            return role

        return ''


Instruction:
Create a Python 3 function for
returning old roles grouped first by term chamber then by type.

Response:
def old_roles_manager(self):
        '''Return old roles, grouped first by term, then by chamber,
        then by type.'''
        wrapper = self._old_role_wrapper
        chamber_getter = operator.methodcaller('get', 'chamber')
        for term, roles in self.get('old_roles', {}).items():
            chamber_roles = defaultdict(lambda: defaultdict(list))
            for chamber, roles in itertools.groupby(roles, chamber_getter):
                for role in roles:
                    role = wrapper(role)
                    typeslug = role['type'].lower().replace(' ', '_')
                    chamber_roles[chamber][typeslug].append(role)
            yield term, chamber_roles


Instruction:
Can you write a function in Python 3 where it
normalizes a legislator name by stripping titles from the front and converting punctuation to lowercase.

Response:
def _normalize(self, name):
        """
        Normalizes a legislator name by stripping titles from the front,
        converting to lowercase and removing punctuation.
        """
        name = re.sub(
            r'^(Senator|Representative|Sen\.?|Rep\.?|'
            'Hon\.?|Right Hon\.?|Mr\.?|Mrs\.?|Ms\.?|L\'hon\.?|'
            'Assembly(member|man|woman)) ',
            '',
            name)
        return name.strip().lower().replace('.', '')


Instruction:
Write a Python 3 script to
learn a new entry in the database.

Response:
def _learn(self, legislator):
        """
        Expects a dictionary with full_name, first_name, last_name and
        middle_name elements as key.

        While this can grow quickly, we should never be dealing with
        more than a few hundred legislators at a time so don't worry about
        it.
        """
        name, obj = legislator, legislator['_id']

        if (legislator['roles'] and legislator['roles'][0]['term'] ==
                self._term and legislator['roles'][0]['type'] == 'member'):
            chamber = legislator['roles'][0]['chamber']
        else:
            try:
                chamber = legislator['old_roles'][self._term][0].get('chamber')
            except KeyError:
                raise ValueError("no role in legislator %s [%s] for term %s" %
                                 (legislator['full_name'], legislator['_id'],
                                  self._term))

        if '_code' in name:
            code = name['_code']
            if code in self._codes[chamber] or code in self._codes[None]:
                raise ValueError("non-unique legislator code [%s] for %s" %
                                 (code, name['full_name']))
            self._codes[chamber][code] = obj
            self._codes[None][code] = obj

        # We throw possible forms of this name into a set because we
        # don't want to try to add the same form twice for the same
        # name
        forms = set()

        def add_form(form):
            forms.add(self._normalize(form))

        add_form(name['full_name'])
        add_form(name['_scraped_name'])
        add_form(name['last_name'])

        if name['first_name']:
            add_form("%s, %s" % (name['last_name'], name['first_name']))
            add_form("%s %s" % (name['first_name'], name['last_name']))
            add_form("%s, %s" % (name['last_name'], name['first_name'][0]))
            add_form("%s (%s)" % (name['last_name'], name['first_name']))
            add_form("%s %s" % (name['first_name'][0], name['last_name']))
            add_form("%s (%s)" % (name['last_name'], name['first_name'][0]))

            if name['middle_name']:
                add_form("%s, %s %s" % (name['last_name'], name['first_name'],
                                        name['middle_name']))
                add_form("%s, %s %s" % (name['last_name'],
                                        name['first_name'][0],
                                        name['middle_name']))
                add_form("%s %s %s" % (name['first_name'],
                                       name['middle_name'],
                                       name['last_name']))
                add_form("%s, %s %s" % (name['last_name'],
                                        name['first_name'][0],
                                        name['middle_name'][0]))
                add_form("%s %s %s" % (name['first_name'],
                                       name['middle_name'][0],
                                       name['last_name']))
                add_form("%s, %s %s" % (name['last_name'],
                                        name['first_name'],
                                        name['middle_name'][0]))
                add_form("%s, %s.%s." % (name['last_name'],
                                         name['first_name'][0],
                                         name['middle_name'][0]))

        for form in forms:
            form = self._normalize(form)
            if form in self._names[chamber]:
                self._names[chamber][form] = None
            else:
                self._names[chamber][form] = obj

            if form in self._names[None]:
                self._names[None][form] = None
            else:
                self._names[None][form] = obj


Instruction:
Can you tell what is the following Python 3 function doing
def match(self, name, chamber=None):
        try:
            return self._manual[chamber][name]
        except KeyError:
            pass

        if chamber == 'joint':
            chamber = None

        try:
            return self._codes[chamber][name]
        except KeyError:
            pass

        if chamber not in self._names:
            logger.warning("Chamber %s is invalid for a legislator." % (
                chamber
            ))
            return None

        name = self._normalize(name)
        return self._names[chamber].get(name, None)

Response: Returns the associated base - level object for the given name and chamber.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_scraper(mod_path, scraper_type):

    # act of importing puts it into the registry
    try:
        module = importlib.import_module(mod_path)
    except ImportError as e:
        raise ScrapeError("could not import %s" % mod_path, e)

    # now find the class within the module
    ScraperClass = None

    for k, v in module.__dict__.items():
        if k.startswith('_'):
            continue
        if getattr(v, 'scraper_type', None) == scraper_type:
            if ScraperClass:
                raise ScrapeError("two %s scrapers found in module %s: %s %s" %
                                  (scraper_type, mod_path, ScraperClass, k))
            ScraperClass = v

    if not ScraperClass:
        raise ScrapeError("no %s scraper found in module %s" % (
            scraper_type, mod_path))

    return ScraperClass

Response: import a scraper from the scraper registry


Instruction:
Implement a Python 3 function for
loading all schemas into schema dict

Response:
def _load_schemas(self):
        """ load all schemas into schema dict """

        types = ('bill', 'committee', 'person', 'vote', 'event')

        for type in types:
            schema_path = os.path.join(os.path.split(__file__)[0],
                                       '../schemas/%s.json' % type)
            self._schema[type] = json.load(open(schema_path))
            self._schema[type]['properties'][settings.LEVEL_FIELD] = {
                'minLength': 2, 'type': 'string'}

        # bills & votes
        self._schema['bill']['properties']['session']['enum'] = \
            self.all_sessions()
        self._schema['vote']['properties']['session']['enum'] = \
            self.all_sessions()

        # legislators
        terms = [t['name'] for t in self.metadata['terms']]
        # ugly break here b/c this line is nearly impossible to split
        self._schema['person']['properties']['roles'][
            'items']['properties']['term']['enum'] = terms


Instruction:
Create a Python 3 function for
checking that a session is present in the metadata dictionary.

Response:
def validate_session(self, session, latest_only=False):
        """ Check that a session is present in the metadata dictionary.

        raises :exc:`~billy.scrape.NoDataForPeriod` if session is invalid

        :param session:  string representing session to check
        """
        if latest_only:
            if session != self.metadata['terms'][-1]['sessions'][-1]:
                raise NoDataForPeriod(session)

        for t in self.metadata['terms']:
            if session in t['sessions']:
                return True
        raise NoDataForPeriod(session)


Instruction:
Can you tell what is the following Python 3 function doing
def validate_term(self, term, latest_only=False):

        if latest_only:
            if term == self.metadata['terms'][-1]['name']:
                return True
            else:
                raise NoDataForPeriod(term)

        for t in self.metadata['terms']:
            if term == t['name']:
                return True
        raise NoDataForPeriod(term)

Response: Check that a term is present in the metadata dictionary.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_source(self, url, **kwargs):
        self['sources'].append(dict(url=url, **kwargs))

Response: Add a source URL from which data related to this object was scraped.


Instruction:
Here you have a function in Python 3, explain what it does
def _get_column_ends(self):
        '''Guess where the ends of the columns lie.
        '''
        ends = collections.Counter()
        for line in self.text.splitlines():
            for matchobj in re.finditer('\s{2,}', line.lstrip()):
                ends[matchobj.end()] += 1
        return ends

Response: Guess where the ends of the columns lie.


Instruction:
Create a Python 3 function to
try to guess the boundaries of the plain text columns.

Response:
def _get_column_boundaries(self):
        '''Use the guessed ends to guess the boundaries of the plain
        text columns.
        '''
        # Try to figure out the most common column boundaries.
        ends = self._get_column_ends()
        if not ends:
            # If there aren't even any nontrivial sequences of whitespace
            # dividing text, there may be just one column. In which case,
            # Return a single span, effectively the whole line.
            return [slice(None, None)]

        most_common = []
        threshold = self.threshold
        for k, v in collections.Counter(ends.values()).most_common():
            if k >= threshold:
                most_common.append(k)

        if most_common:
            boundaries = []
            for k, v in ends.items():
                if v in most_common:
                    boundaries.append(k)
        else:
            # Here there weren't enough boundaries to guess the most common
            # ones, so just use the apparent boundaries. In other words, we
            # have only 1 row. Potentially a source of inaccuracy.
            boundaries = ends.keys()

        # Convert the boundaries into a list of span slices.
        boundaries.sort()
        last_boundary = boundaries[-1]
        boundaries = zip([0] + boundaries, boundaries)
        boundaries = list(itertools.starmap(slice, boundaries))

        # And get from the last boundary to the line ending.
        boundaries.append(slice(last_boundary, None))
        return boundaries


Instruction:
Create a Python 3 function to
extract cells from the given line.

Response:
def getcells(self, line):
        '''Using self.boundaries, extract cells from the given line.
        '''
        for boundary in self.boundaries:
            cell = line.lstrip()[boundary].strip()
            if cell:
                for cell in re.split('\s{3,}', cell):
                    yield cell
            else:
                yield None


Instruction:
How would you explain what the following Python 3 function does
def rows(self):
        '''Returns an iterator of row tuples.
        '''
        for line in self.text.splitlines():
            yield tuple(self.getcells(line))

Response: Returns an iterator of row tuples.


Instruction:
How would you code a function in Python 3 to
return an interator of all cells in the table.

Response:
def cells(self):
        '''Returns an interator of all cells in the table.
        '''
        for line in self.text.splitlines():
            for cell in self.getcells(line):
                yield cell


Instruction:
How would you implement a function in Python 3 that
returns the end of the range of the result set.

Response:
def range_end(self):
        '''"Showing 40 - 50 of 234 results
                         ^
        '''
        count = self.count
        range_end = self.range_start + self.limit - 1
        if count < range_end:
            range_end = count
        return range_end


Instruction:
Write a Python 3 script for
returning a generator of tuples where string page_number is the number of the html link and clickable is True if the link is clickable or False if the link is not clickable.

Response:
def pagination_data(self, max_number_of_links=7):
        '''Returns a generator of tuples (string, page_number, clickable),
        where `string` is the text of the html link, `page_number` is
        the number of the page the link points to, and `clickable` is
        a boolean indicating whether the link is clickable or not.
        '''
        div, mod = divmod(max_number_of_links, 2)
        if not mod == 1:
            msg = 'Max number of links must be odd, was %r.'
            raise ValueError(msg % max_number_of_links)

        midpoint = (max_number_of_links - 1) / 2
        current_page = self.current_page
        last_page = self.last_page

        if current_page > last_page:
            raise Http404('invalid page number')
            current_page = last_page

        show_link_firstpage = midpoint < current_page
        show_link_previouspage = 1 < current_page
        show_link_lastpage = current_page < (self.last_page - midpoint)
        show_link_nextpage = current_page < last_page

        extra_room_previous = midpoint - current_page
        if extra_room_previous < 0:
            extra_room_previous = 0
        if not show_link_firstpage:
            extra_room_previous += 1
        if not show_link_previouspage:
            extra_room_previous += 1

        extra_room_subsequent = current_page - last_page + midpoint
        extra_room_subsequent = max([extra_room_subsequent, 0])
        if not show_link_nextpage:
            extra_room_subsequent += 1
        if not show_link_lastpage:
            extra_room_subsequent += 1

        if self.current_page == 1:
            yield PageLink(string=1, page_number=1, clickable=False)
        else:
            # The  "first page" link.
            if show_link_firstpage:
                #[<<] [<] [7] [8] [9] 10 [11] ...'
                #  ^
                yield PageLink(string=u"\u00AB", page_number=1, clickable=True)

            if show_link_previouspage:
                # The "previous page" link.
                #[<<] [<] [7] [8] [9] 10 [11] ...'
                #      ^
                yield PageLink(string=u"\u2039",
                               page_number=self.previous_page,
                               clickable=True)

        # Up to `midpoint + extra_room_subsequent` previous page numbers.
        previous = itertools.islice(self.previous_pages_numbers(),
                                    midpoint + extra_room_subsequent)
        previous = list(previous)[::-1]

        for page_number in previous:
            yield PageLink(string=page_number,
                           page_number=page_number, clickable=True)

        # The current page, clickable.
        if current_page != 1:
            yield PageLink(string=current_page,
                           page_number=current_page, clickable=False)

        # Up to `midpoint + extra_room_previous` subsequent page numbers.
        subsequent_count = midpoint + extra_room_previous
        _subsequent_pages_count = self._subsequent_pages_count
        if _subsequent_pages_count < subsequent_count:
            subsequent_count = _subsequent_pages_count

        for page_number in itertools.islice(self.subsequent_pages_numbers(),
                                            subsequent_count):
            yield PageLink(string=page_number,
                           page_number=page_number, clickable=True)

        if show_link_nextpage:
            yield PageLink(string=u"\u203A",
                           page_number=current_page + 1,
                           clickable=True)

        if show_link_lastpage:
            yield PageLink(string=u"\u00BB",
                           page_number=last_page,
                           clickable=True)


Instruction:
Given the following Python 3 function, write the documentation
def _run_scraper(scraper_type, options, metadata):
    _clear_scraped_data(options.output_dir, scraper_type)

    scraper = _get_configured_scraper(scraper_type, options, metadata)
    ua_email = os.environ.get('BILLY_UA_EMAIL')
    if ua_email and scraper:
        scraper.user_agent += ' ({})'.format(ua_email)
    if not scraper:
        return [{
            "type": scraper_type,
            "start_time": dt.datetime.utcnow(),
            "noscraper": True,
            "end_time": dt.datetime.utcnow()
        }]

    runs = []

    # Removed from the inner loop due to non-bicameral scrapers
    scrape = {
        "type": scraper_type
    }
    scrape['start_time'] = dt.datetime.utcnow()

    if scraper_type in ('bills', 'votes', 'events'):
        times = options.sessions
        for time in times:
            scraper.validate_session(time, scraper.latest_only)
    elif scraper_type in ('committees', 'legislators'):
        times = options.terms
        for time in times:
            scraper.validate_term(time, scraper.latest_only)

    # run scraper against year/session/term
    for time in times:
        # old style
        chambers = options.chambers
        if scraper_type == 'events' and len(options.chambers) == 2:
            chambers.append('other')

        if _is_old_scrape(scraper.scrape):
            for chamber in chambers:
                scraper.scrape(chamber, time)
        else:
            scraper.scrape(time, chambers=chambers)

        # error out if events or votes don't scrape anything
        if not scraper.object_count and scraper_type not in ('events',
                                                             'votes'):
            raise ScrapeError("%s scraper didn't save any objects" %
                              scraper_type)

    scrape['end_time'] = dt.datetime.utcnow()
    runs.append(scrape)

    return runs

Response: Run scraper against the specified options.


Instruction:
Given the following Python 3 function, write the documentation
def import_bill(data, standalone_votes, categorizer):
    abbr = data[settings.LEVEL_FIELD]

    # clean up bill_ids
    data['bill_id'] = fix_bill_id(data['bill_id'])
    if 'alternate_bill_ids' in data:
        data['alternate_bill_ids'] = [fix_bill_id(bid) for bid in
                                      data['alternate_bill_ids']]

    # move subjects to scraped_subjects
    # NOTE: intentionally doesn't copy blank lists of subjects
    # this avoids the problem where a bill is re-run but we can't
    # get subjects anymore (quite common)
    subjects = data.pop('subjects', None)
    if subjects:
        data['scraped_subjects'] = subjects

    # update categorized subjects
    if categorizer:
        categorizer.categorize_bill(data)

    # companions
    for companion in data['companions']:
        companion['bill_id'] = fix_bill_id(companion['bill_id'])
        # query based on companion
        spec = companion.copy()
        spec[settings.LEVEL_FIELD] = abbr
        if not spec['chamber']:
            spec.pop('chamber')
        companion_obj = db.bills.find_one(spec)
        if companion_obj:
            companion['internal_id'] = companion_obj['_id']
        else:
            logger.warning('Unknown companion: {chamber} {session} {bill_id}'
                           .format(**companion))

    # look for a prior version of this bill
    bill = db.bills.find_one({settings.LEVEL_FIELD: abbr,
                              'session': data['session'],
                              'chamber': data['chamber'],
                              'bill_id': data['bill_id']})

    # keep doc ids consistent
    doc_matcher = DocumentMatcher(abbr)
    if bill:
        doc_matcher.learn_ids(bill['versions'] + bill['documents'])
    doc_matcher.set_ids(data['versions'] + data['documents'])

    # match sponsor leg_ids
    match_sponsor_ids(abbr, data)

    # process votes ############

    # pull votes off bill
    bill_votes = data.pop('votes', [])

    # grab the external bill votes if present
    if metadata(abbr).get('_partial_vote_bill_id'):
        # this is a hack initially added for Rhode Island where we can't
        # determine the full bill_id, if this key is in the metadata
        # we just use the numeric portion, not ideal as it won't work
        # where HB/SBs overlap, but in RI they never do
        # pull off numeric portion of bill_id
        numeric_bill_id = data['bill_id'].split()[1]
        bill_votes += standalone_votes.pop((data['chamber'], data['session'],
                                            numeric_bill_id), [])
    else:
        # add loaded votes to data
        bill_votes += standalone_votes.pop((data['chamber'], data['session'],
                                            data['bill_id']), [])

    # do id matching and other vote prep
    if bill:
        prepare_votes(abbr, data['session'], bill['_id'], bill_votes)
    else:
        prepare_votes(abbr, data['session'], None, bill_votes)

    # process actions ###########

    dates = {'first': None, 'last': None, 'passed_upper': None,
             'passed_lower': None, 'signed': None}

    vote_flags = {
        "bill:passed",
        "bill:failed",
        "bill:veto_override:passed",
        "bill:veto_override:failed",
        "amendment:passed",
        "amendment:failed",
        "committee:passed",
        "committee:passed:favorable",
        "committee:passed:unfavorable",
        "committee:passed:failed"
    }
    already_linked = set()
    remove_vote = set()

    for action in data['actions']:
        adate = action['date']

        def _match_committee(name):
            return get_committee_id(abbr, action['actor'], name)

        def _match_legislator(name):
            return get_legislator_id(abbr,
                                     data['session'],
                                     action['actor'],
                                     name)

        resolvers = {
            "committee": _match_committee,
            "legislator": _match_legislator
        }

        if "related_entities" in action:
            for entity in action['related_entities']:
                try:
                    resolver = resolvers[entity['type']]
                except KeyError as e:
                    # We don't know how to deal.
                    logger.error("I don't know how to sort a %s" % e)
                    continue

                id = resolver(entity['name'])
                entity['id'] = id

        # first & last dates
        if not dates['first'] or adate < dates['first']:
            dates['first'] = adate
        if not dates['last'] or adate > dates['last']:
            dates['last'] = adate

        # passed & signed dates
        if (not dates['passed_upper'] and action['actor'] == 'upper'
                and 'bill:passed' in action['type']):
            dates['passed_upper'] = adate
        elif (not dates['passed_lower'] and action['actor'] == 'lower'
                and 'bill:passed' in action['type']):
            dates['passed_lower'] = adate
        elif (not dates['signed'] and 'governor:signed' in action['type']):
            dates['signed'] = adate

        # vote-action matching
        action_attached = False
        # only attempt vote matching if action has a date and is one of the
        # designated vote action types
        if set(action['type']).intersection(vote_flags) and action['date']:
            for vote in bill_votes:
                if not vote['date']:
                    continue

                delta = abs(vote['date'] - action['date'])
                if (delta < datetime.timedelta(hours=20) and
                        vote['chamber'] == action['actor']):
                    if action_attached:
                        # multiple votes match, we can't guess
                        action.pop('related_votes', None)
                    else:
                        related_vote = vote['vote_id']
                        if related_vote in already_linked:
                            remove_vote.add(related_vote)

                        already_linked.add(related_vote)
                        action['related_votes'] = [related_vote]
                        action_attached = True

    # remove related_votes that we linked to multiple actions
    for action in data['actions']:
        for vote in remove_vote:
            if vote in action.get('related_votes', []):
                action['related_votes'].remove(vote)

    # save action dates to data
    data['action_dates'] = dates

    data['_term'] = term_for_session(abbr, data['session'])

    alt_titles = set(data.get('alternate_titles', []))

    for version in data['versions']:
        # Merge any version titles into the alternate_titles list
        if 'title' in version:
            alt_titles.add(version['title'])
        if '+short_title' in version:
            alt_titles.add(version['+short_title'])
    try:
        # Make sure the primary title isn't included in the
        # alternate title list
        alt_titles.remove(data['title'])
    except KeyError:
        pass
    data['alternate_titles'] = list(alt_titles)
    data = apply_filters(filters, data)

    if not bill:
        insert_with_id(data)
        git_add_bill(data)
        save_votes(data, bill_votes)
        return "insert"
    else:
        update(bill, data, db.bills)
        git_add_bill(bill)
        save_votes(bill, bill_votes)
        return "update"

Response: Import a single bill into the database.


Instruction:
Here you have a function in Python 3, explain what it does
def populate_current_fields(abbr):
    meta = db.metadata.find_one({'_id': abbr})
    current_term = meta['terms'][-1]
    current_session = current_term['sessions'][-1]

    for bill in db.bills.find({settings.LEVEL_FIELD: abbr}):
        if bill['session'] == current_session:
            bill['_current_session'] = True
        else:
            bill['_current_session'] = False

        if bill['session'] in current_term['sessions']:
            bill['_current_term'] = True
        else:
            bill['_current_term'] = False

        db.bills.save(bill, safe=True)

Response:
Set/update _current_term and _current_session fields on all bills
    for a given location.


Instruction:
Write a Python 3 script for
reading in already set ids on objects

Response:
def learn_ids(self, item_list):
        """ read in already set ids on objects """
        self._reset_sequence()
        for item in item_list:
            key = self.nondup_key_for_item(item)
            self.ids[key] = item[self.id_key]


Instruction:
How would you implement a function in Python 3 that
sets ids on an object using internal mapping then new ids

Response:
def set_ids(self, item_list):
        """ set ids on an object, using internal mapping then new ids """
        self._reset_sequence()
        for item in item_list:
            key = self.nondup_key_for_item(item)
            item[self.id_key] = self.ids.get(key) or self._get_next_id()


Instruction:
Explain what the following Python 3 code does
def import_committees_from_legislators(current_term, abbr):

    # for all current legislators
    for legislator in db.legislators.find({'roles': {'$elemMatch': {
            'term': current_term, settings.LEVEL_FIELD: abbr}}}):

        # for all committee roles
        for role in legislator['roles']:
            if (role['type'] == 'committee member' and
                    'committee_id' not in role):

                spec = {settings.LEVEL_FIELD: abbr,
                        'chamber': role['chamber'],
                        'committee': role['committee']}
                if 'subcommittee' in role:
                    spec['subcommittee'] = role['subcommittee']

                committee = db.committees.find_one(spec)

                if not committee:
                    committee = spec
                    committee['_type'] = 'committee'
                    # copy LEVEL_FIELD from legislator to committee
                    committee[settings.LEVEL_FIELD] = \
                        legislator[settings.LEVEL_FIELD]
                    committee['members'] = []
                    committee['sources'] = []
                    if 'subcommittee' not in committee:
                        committee['subcommittee'] = None
                    insert_with_id(committee)

                for member in committee['members']:
                    if member['leg_id'] == legislator['leg_id']:
                        break
                else:
                    committee['members'].append(
                        {'name': legislator['full_name'],
                         'leg_id': legislator['leg_id'],
                         'role': role.get('position') or 'member'})
                    for source in legislator['sources']:
                        if source not in committee['sources']:
                            committee['sources'].append(source)
                    db.committees.save(committee, safe=True)

                    role['committee_id'] = committee['_id']

        db.legislators.save(legislator, safe=True)

Response: create committees from all legislators that have committee roles


Instruction:
Can you create a Python 3 function that
associates a sponsor with this bill.

Response:
def add_sponsor(self, type, name, **kwargs):
        """
        Associate a sponsor with this bill.

        :param type: the type of sponsorship, e.g. 'primary', 'cosponsor'
        :param name: the name of the sponsor as provided by the official source
        """
        self['sponsors'].append(dict(type=type, name=name, **kwargs))


Instruction:
Can you tell what is the following Python 3 function doing
def add_version(self, name, url, mimetype=None, on_duplicate='error',
                    **kwargs):
        """
        Add a version of the text of this bill.

        :param name: a name given to this version of the text, e.g.
                     'As Introduced', 'Version 2', 'As amended', 'Enrolled'
        :param url: the location of this version on the legislative website.
        :param mimetype: MIME type of the document
        :param on_duplicate: What to do if a duplicate is seen:
            error - default option, raises a ValueError
            ignore - add the document twice (rarely the right choice)
            use_new - use the new name, removing the old document
            use_old - use the old name, not adding the new document

        If multiple formats are provided, a good rule of thumb is to
        prefer text, followed by html, followed by pdf/word/etc.
        """
        if not mimetype:
            raise ValueError('mimetype parameter to add_version is required')
        if on_duplicate != 'ignore':
            if url in self._seen_versions:
                if on_duplicate == 'error':
                    raise ValueError('duplicate version url %s' % url)
                elif on_duplicate == 'use_new':
                    # delete the old version
                    self['versions'] = [v for v in self['versions']
                                        if v['url'] != url]
                elif on_duplicate == 'use_old':
                    return       # do nothing
            self._seen_versions.add(url)

        d = dict(name=name, url=url, mimetype=mimetype, **kwargs)
        self['versions'].append(d)

Response: Add a version of the text to the current bill.


Instruction:
Can you create a Python 3 function that
adds an action that was performed on this bill.

Response:
def add_action(self, actor, action, date, type=None, committees=None,
                   legislators=None, **kwargs):
        """
        Add an action that was performed on this bill.

        :param actor: a string representing who performed the action.
          If the action is associated with one of the chambers this
          should be 'upper' or 'lower'. Alternatively, this could be
          the name of a committee, a specific legislator, or an outside
          actor such as 'Governor'.
        :param action: a string representing the action performed, e.g.
                       'Introduced', 'Signed by the Governor', 'Amended'
        :param date: the date/time this action was performed.
        :param type: a type classification for this action
        ;param committees: a committee or list of committees to associate with
                           this action
        """

        def _cleanup_list(obj, default):
            if not obj:
                obj = default
            elif isinstance(obj, string_types):
                obj = [obj]
            elif not isinstance(obj, list):
                obj = list(obj)
            return obj

        type = _cleanup_list(type, ['other'])
        committees = _cleanup_list(committees, [])
        legislators = _cleanup_list(legislators, [])

        if 'committee' in kwargs:
            raise ValueError("invalid param 'committee' passed to add_action, "
                             "must use committees")

        if isinstance(committees, string_types):
            committees = [committees]

        related_entities = []         # OK, let's work some magic.
        for committee in committees:
            related_entities.append({
                "type": "committee",
                "name": committee
            })

        for legislator in legislators:
            related_entities.append({
                "type": "legislator",
                "name": legislator
            })

        self['actions'].append(dict(actor=actor, action=action,
                                    date=date, type=type,
                                    related_entities=related_entities,
                                    **kwargs))


Instruction:
Implement a function in Python 3 to
associate another bill with this one.

Response:
def add_companion(self, bill_id, session=None, chamber=None):
        """
        Associate another bill with this one.

        If session isn't set it will be set to self['session'].
        """
        companion = {'bill_id': bill_id,
                     'session': session or self['session'],
                     'chamber': chamber}
        self['companions'].append(companion)


Instruction:
Make a summary of the following Python 3 code
def homepage(request):
    '''
    Context:
        all_metadata

    Templates:
        - billy/web/public/homepage.html
    '''
    all_metadata = db.metadata.find()

    return render(request, templatename('homepage'),
                  dict(all_metadata=all_metadata))

Response: Returns the homepage. html.


Instruction:
Can you tell what is the following Python 3 function doing
def downloads(request):
    '''
    Context:
        - all_metadata

    Templates:
        - billy/web/public/downloads.html
    '''
    all_metadata = sorted(db.metadata.find(), key=lambda x: x['name'])
    return render(request, 'billy/web/public/downloads.html',
                  {'all_metadata': all_metadata})

Response: Returns a list of all downloads.


Instruction:
Explain what the following Python 3 code does
def find_your_legislator(request):
    '''
    Context:
        - request
        - lat
        - long
        - located
        - legislators

    Templates:
        - billy/web/public/find_your_legislator_table.html
    '''

    # check if lat/lon are set
    # if leg_search is set, they most likely don't have ECMAScript enabled.
    # XXX: fallback behavior here for alpha.

    get = request.GET
    context = {}
    template = 'find_your_legislator'

    context['request'] = ""
    if "q" in get:
        context['request'] = get['q']

    if "lat" in get and "lon" in get:
        # We've got a passed lat/lon. Let's build off it.
        lat = get['lat']
        lon = get['lon']

        context['lat'] = lat
        context['lon'] = lon
        context['located'] = True

        qurl = "%slegislators/geo/?long=%s&lat=%s&apikey=%s" % (
            billy_settings.API_BASE_URL,
            lon,
            lat,
            getattr(billy_settings, 'API_KEY', '')
        )
        leg_resp = json.load(urllib2.urlopen(qurl, timeout=0.5))
        # allow limiting lookup to region for region map views
        if 'abbr' in get:
            leg_resp = [leg for leg in leg_resp
                        if leg[billy_settings.LEVEL_FIELD] == get['abbr']]
            context['abbr'] = get['abbr']

        # Also, allow filtering by chamber
        if 'chamber' in get:
            leg_resp = [leg for leg in leg_resp
                        if leg['chamber'] == get['chamber']]
            context['chamber'] = get['chamber']

        if "boundary" in get:
            return HttpResponse(json.dumps([]))

        context['legislators'] = map(Legislator, leg_resp)
        template = 'find_your_legislator_table'

    return render(request, templatename(template), context)

Response: Find the most likely legislator for a given location.


Instruction:
Can you tell what is the following Python 3 function doing
def activate_legislators(current_term, abbr):
    for legislator in db.legislators.find(
        {'roles': {'$elemMatch':
                   {settings.LEVEL_FIELD: abbr, 'term': current_term}}}):
        active_role = legislator['roles'][0]

        if not active_role.get('end_date') and active_role['type'] == 'member':
            legislator['active'] = True
            legislator['party'] = active_role.get('party', None)
            legislator['district'] = active_role.get('district', None)
            legislator['chamber'] = active_role.get('chamber', None)

        legislator['updated_at'] = datetime.datetime.utcnow()
        db.legislators.save(legislator, safe=True)

Response: Activates all legislators with the given term.


Instruction:
Can you tell what is the following Python 3 function doing
def add_role(self, role, term, start_date=None, end_date=None,
                 **kwargs):
        """
        Examples:

        leg.add_role('member', term='2009', chamber='upper',
                     party='Republican', district='10th')
        """
        self['roles'].append(dict(role=role, term=term,
                                  start_date=start_date,
                                  end_date=end_date, **kwargs))

Response: Adds a role to the object.


Instruction:
How would you explain what the following Python 3 function does
def add_office(self, type, name, address=None, phone=None, fax=None,
                   email=None, **kwargs):
        """
        Allowed office types:
            capitol
            district
        """
        office_dict = dict(type=type, address=address, name=name, phone=phone,
                           fax=fax, email=email, **kwargs)
        self['offices'].append(office_dict)

Response: Add an office to the set of allowed offices.


Instruction:
Create a Python 3 function for
getting the metadata for the given two - letter abbreviation.

Response:
def metadata(abbr, __metadata=__metadata):
    """
    Grab the metadata for the given two-letter abbreviation.
    """
    # This data should change very rarely and is queried very often so
    # cache it here
    abbr = abbr.lower()
    if abbr in __metadata:
        return __metadata[abbr]
    rv = db.metadata.find_one({'_id': abbr})

    __metadata[abbr] = rv
    return rv


Instruction:
Can you implement a function in Python 3 that
creates the path if it doesn t exist

Response:
def cd(path):
    '''Creates the path if it doesn't exist'''
    old_dir = os.getcwd()
    try:
        os.makedirs(path)
    except OSError:
        pass
    os.chdir(path)
    try:
        yield
    finally:
        os.chdir(old_dir)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_property_dict(schema):
    pdict = {}
    for k, v in schema['properties'].items():
        pdict[k] = {}
        if 'items' in v and 'properties' in v['items']:
            pdict[k] = _get_property_dict(v['items'])
    pdict[settings.LEVEL_FIELD] = {}
    return pdict

Response: given a schema produce a nested dictionary of fields


Instruction:
How would you explain what the following Python 3 function does
def insert_with_id(obj):
    if '_id' in obj:
        raise ValueError("object already has '_id' field")

    # add created_at/updated_at on insert
    obj['created_at'] = datetime.datetime.utcnow()
    obj['updated_at'] = obj['created_at']

    if obj['_type'] == 'person' or obj['_type'] == 'legislator':
        collection = db.legislators
        id_type = 'L'
    elif obj['_type'] == 'committee':
        collection = db.committees
        id_type = 'C'
    elif obj['_type'] == 'bill':
        collection = db.bills
        id_type = 'B'
    else:
        raise ValueError("unknown _type for object")

    # get abbr
    abbr = obj[settings.LEVEL_FIELD].upper()

    id_reg = re.compile('^%s%s' % (abbr, id_type))

    # Find the next available _id and insert
    id_prefix = '%s%s' % (abbr, id_type)
    cursor = collection.find({'_id': id_reg}).sort('_id', -1).limit(1)

    try:
        new_id = int(next(cursor)['_id'][len(abbr) + 1:]) + 1
    except StopIteration:
        new_id = 1

    while True:
        if obj['_type'] == 'bill':
            obj['_id'] = '%s%08d' % (id_prefix, new_id)
        else:
            obj['_id'] = '%s%06d' % (id_prefix, new_id)
        obj['_all_ids'] = [obj['_id']]

        if obj['_type'] in ['person', 'legislator']:
            obj['leg_id'] = obj['_id']

        try:
            return collection.insert(obj, safe=True)
        except pymongo.errors.DuplicateKeyError:
            new_id += 1

Response: Insert a new object into the appropriate collection.


Instruction:
Make a summary of the following Python 3 code
def update(old, new, collection, sneaky_update_filter=None):
    # need_save = something has changed
    need_save = False

    locked_fields = old.get('_locked_fields', [])

    for key, value in new.items():

        # don't update locked fields
        if key in locked_fields:
            continue

        if old.get(key) != value:
            if sneaky_update_filter and key in sneaky_update_filter:
                if sneaky_update_filter[key](old[key], value):
                    old[key] = value
                    need_save = True
            else:
                old[key] = value
                need_save = True

        # remove old +key field if this field no longer has a +
        plus_key = '+%s' % key
        if plus_key in old:
            del old[plus_key]
            need_save = True

    if need_save:
        old['updated_at'] = datetime.datetime.utcnow()
        collection.save(old, safe=True)

    return need_save

Response: update an existing object with a new object


Instruction:
Can you create a Python 3 function that
converts unix timestamps in the scraper output to python datetimes.

Response:
def convert_timestamps(obj):
    """
    Convert unix timestamps in the scraper output to python datetimes
    so that they will be saved properly as Mongo datetimes.
    """
    for key in ('date', 'when', 'end', 'start_date', 'end_date'):
        value = obj.get(key)
        if value:
            try:
                obj[key] = _timestamp_to_dt(value)
            except TypeError:
                raise TypeError("expected float for %s, got %s" % (key, value))

    for key in ('sources', 'actions', 'votes', 'roles'):
        for child in obj.get(key, []):
            convert_timestamps(child)

    return obj


Instruction:
Can you tell what is the following Python 3 function doing
def split_name(obj):
    if obj['_type'] in ('person', 'legislator'):
        for key in ('first_name', 'last_name'):
            if key not in obj or not obj[key]:
                # Need to split
                (obj['first_name'], obj['last_name'],
                 obj['suffixes']) = name_tools.split(obj['full_name'])[1:]
                break

    return obj

Response: Split name into first_name and last_name.


Instruction:
Make a summary of the following Python 3 code
def _make_plus_helper(obj, fields):
    new_obj = {}

    for key, value in obj.items():
        if key in fields or key.startswith('_'):
            # if there's a subschema apply it to a list or subdict
            if fields.get(key):
                if isinstance(value, list):
                    value = [_make_plus_helper(item, fields[key])
                             for item in value]
            # assign the value (modified potentially) to the new_obj
            new_obj[key] = value
        else:
            # values not in the fields dict get a +
            new_obj['+%s' % key] = value

    return new_obj

Response: Add a + prefix to any fields in obj that aren t in fields.


Instruction:
Write a Python 3 script for
adding a '+' to the key of non - standard fields.

Response:
def make_plus_fields(obj):
    """
    Add a '+' to the key of non-standard fields.

    dispatch to recursive _make_plus_helper based on _type field
    """
    fields = standard_fields.get(obj['_type'], dict())
    return _make_plus_helper(obj, fields)


Instruction:
Here you have a function in Python 3, explain what it does
def get_object(cls, abbr):
        '''
        This particular model needs its own constructor in order to take
        advantage of the metadata cache in billy.util, which would otherwise
        return unwrapped objects.
        '''
        obj = get_metadata(abbr)
        if obj is None:
            msg = 'No metadata found for abbreviation %r' % abbr
            raise DoesNotExist(msg)
        return cls(obj)

Response: Returns an object of the class cls with the given abbreviation.


Instruction:
Here you have a function in Python 3, explain what it does
def committees_legislators(self, *args, **kwargs):
        '''Return an iterable of committees with all the
        legislators cached for reference in the Committee model.
        So do a "select_related" operation on committee members.
        '''
        committees = list(self.committees(*args, **kwargs))
        legislators = self.legislators({'active': True},
                                       fields=['full_name',
                                               settings.LEVEL_FIELD])
        _legislators = {}

        # This will be a cache of legislator objects used in
        # the committees.html template. Includes ids in each
        # legislator's _all_ids field (if it exists.)
        for obj in legislators:
            if 'all_ids' in obj:
                for _id in obj['_all_ids']:
                    _legislators[_id] = obj
            else:
                _legislators[obj['_id']] = obj
        del legislators
        for com in committees:
            com._legislators = _legislators
        return committees

Response:
Return an iterable of committees with all the
        legislators cached for reference in the Committee model. So do a select_related operation on members.


Instruction:
Can you tell what is the following Python 3 function doing
def extract_fields(d, fields, delimiter='|'):
    rd = {}
    for f in fields:
        v = d.get(f, None)
        if isinstance(v, (str, unicode)):
            v = v.encode('utf8')
        elif isinstance(v, list):
            v = delimiter.join(v)
        rd[f] = v
    return rd

Response: extracts values out of an object d for saving to a csv


Instruction:
Can you implement a function in Python 3 that
returns a list of Bills for the given session and bill_id.

Response:
def bill(request, abbr, session, bill_id):
    '''
    Context:
        - vote_preview_row_template
        - abbr
        - metadata
        - bill
        - show_all_sponsors
        - sponsors
        - sources
        - nav_active

    Templates:
        - billy/web/public/bill.html
        - billy/web/public/vote_preview_row.html
    '''
    # get fixed version
    fixed_bill_id = fix_bill_id(bill_id)
    # redirect if URL's id isn't fixed id without spaces
    if fixed_bill_id.replace(' ', '') != bill_id:
        return redirect('bill', abbr=abbr, session=session, bill_id=fixed_bill_id.replace(' ', ''))
    bill = db.bills.find_one({settings.LEVEL_FIELD: abbr, 'session': session,
                              'bill_id': fixed_bill_id})
    if bill is None:
        raise Http404(u'no bill found {0} {1} {2}'.format(abbr, session, bill_id))

    show_all_sponsors = request.GET.get('show_all_sponsors')
    if show_all_sponsors:
        sponsors = bill.sponsors_manager
    else:
        sponsors = bill.sponsors_manager.first_fifteen

    return render(
        request, templatename('bill'),
        dict(vote_preview_row_template=templatename('vote_preview_row'),
             abbr=abbr,
             metadata=Metadata.get_object(abbr),
             bill=bill,
             show_all_sponsors=show_all_sponsors,
             sponsors=sponsors,
             sources=bill['sources'],
             nav_active='bills'))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def vote(request, abbr, vote_id):
    '''
    Context:
        - abbr
        - metadata
        - bill
        - vote
        - nav_active

    Templates:
        - vote.html
    '''
    vote = db.votes.find_one(vote_id)
    if vote is None:
        raise Http404('no such vote: {0}'.format(vote_id))
    bill = vote.bill()

    return render(request, templatename('vote'),
                  dict(abbr=abbr, metadata=Metadata.get_object(abbr),
                       bill=bill,
                       vote=vote,
                       nav_active='bills'))

Response: Return a single object with the given ID.


Instruction:
Write a Python 3 function for
returning a list of documents that are available for the given document.

Response:
def document(request, abbr, session, bill_id, doc_id):
    '''
    Context:
        - abbr
        - session
        - bill
        - version
        - metadata
        - nav_active

    Templates:
        - billy/web/public/document.html
    '''
    # get fixed version
    fixed_bill_id = fix_bill_id(bill_id)
    # redirect if URL's id isn't fixed id without spaces
    if fixed_bill_id.replace(' ', '') != bill_id:
        return redirect('document', abbr=abbr, session=session,
                        bill_id=fixed_bill_id.replace(' ', ''), doc_id=doc_id)

    bill = db.bills.find_one({settings.LEVEL_FIELD: abbr, 'session': session,
                              'bill_id': fixed_bill_id})

    if not bill:
        raise Http404('No such bill.')

    for version in bill['versions']:
        if version['doc_id'] == doc_id:
            break
    else:
        raise Http404('No such document.')

    if not settings.ENABLE_DOCUMENT_VIEW.get(abbr, False):
        return redirect(version['url'])

    return render(request, templatename('document'),
                  dict(abbr=abbr, session=session, bill=bill, version=version,
                       metadata=bill.metadata, nav_active='bills'))


Instruction:
Can you generate the documentation for the following Python 3 function
def show_all(key):
    '''
    Context:
        - abbr
        - metadata
        - bill
        - sources
        - nav_active

    Templates:
        - billy/web/public/bill_all_{key}.html
            - where key is passed in, like "actions", etc.
    '''
    def func(request, abbr, session, bill_id, key):
        # get fixed version
        fixed_bill_id = fix_bill_id(bill_id)
        # redirect if URL's id isn't fixed id without spaces
        if fixed_bill_id.replace(' ', '') != bill_id:
            return redirect('bill', abbr=abbr, session=session,
                            bill_id=fixed_bill_id.replace(' ', ''))
        bill = db.bills.find_one({settings.LEVEL_FIELD: abbr,
                                  'session': session,
                                  'bill_id': fixed_bill_id})
        if bill is None:
            raise Http404('no bill found {0} {1} {2}'.format(abbr, session,
                                                             bill_id))
        return render(request, templatename('bill_all_%s' % key),
                      dict(abbr=abbr, metadata=Metadata.get_object(abbr),
                           bill=bill, sources=bill['sources'],
                           nav_active='bills'))
    return func

Response: A view that returns a list of all bills.


Instruction:
How would you implement a function in Python 3 that
returns a context item with the data from the filter bills form.

Response:
def get_context_data(self, *args, **kwargs):
        '''
        Context:
            If GET parameters are given:
            - search_text
            - form (FilterBillsForm)
            - long_description
            - description
            - get_params
            Otherwise, the only context item is an unbound FilterBillsForm.

        Templates:
            - Are specified in subclasses.
        '''
        context = super(RelatedBillsList, self).get_context_data(*args,
                                                                 **kwargs)
        metadata = context['metadata']
        FilterBillsForm = get_filter_bills_form(metadata)

        if self.request.GET:
            form = FilterBillsForm(self.request.GET)
            search_text = form.data.get('search_text')
            context.update(search_text=search_text)
            context.update(form=FilterBillsForm(self.request.GET))

            # human readable description of search
            description = []
            if metadata:
                description.append(metadata['name'])
            else:
                description = ['Search All']
            long_description = []
            chamber = form.data.get('chamber')
            session = form.data.get('session')
            type = form.data.get('type')
            status = form.data.getlist('status')
            subjects = form.data.getlist('subjects')
            sponsor = form.data.get('sponsor__leg_id')
            if chamber:
                if metadata:
                    description.append(metadata['chambers'][chamber]['name']
                                      )
                else:
                    description.extend([chamber.title(), 'Chamber'])
            description.append((type or 'Bill') + 's')
            if session:
                description.append(
                    '(%s)' %
                    metadata['session_details'][session]['display_name']
                )
            if 'signed' in status:
                long_description.append('which have been signed into law')
            elif 'passed_upper' in status and 'passed_lower' in status:
                long_description.append('which have passed both chambers')
            elif 'passed_lower' in status:
                chamber_name = (metadata['chambers']['lower']['name']
                                if metadata else 'lower chamber')
                long_description.append('which have passed the ' +
                                        chamber_name)
            elif 'passed_upper' in status:
                chamber_name = (metadata['chambers']['upper']['name']
                                if metadata else 'upper chamber')
                long_description.append('which have passed the ' +
                                        chamber_name)
            if sponsor:
                leg = db.legislators.find_one({'_all_ids': sponsor},
                                              fields=('full_name', '_id'))
                leg = leg['full_name']
                long_description.append('sponsored by ' + leg)
            if subjects:
                long_description.append('related to ' + ', '.join(subjects))
            if search_text:
                long_description.append(u'containing the term "{0}"'.format(
                    search_text))
            context.update(long_description=long_description)
        else:
            if metadata:
                description = [metadata['name'], 'Bills']
            else:
                description = ['All Bills']
            context.update(form=FilterBillsForm())

        context.update(description=' '.join(description))

        # Add the correct path to paginated links.
        params = list(self.request.GET.lists())
        for k, v in params[:]:
            if k == 'page':
                params.remove((k, v))
        get_params = urllib.urlencode(params, doseq=True)
        context['get_params'] = get_params

        # Add the abbr.
        context['abbr'] = self.kwargs['abbr']
        return context


Instruction:
Write a Python 3 function that can
handle submission of the region selection form in the base template.

Response:
def region_selection(request):
    '''Handle submission of the region selection form in the base template. '''
    form = get_region_select_form(request.GET)
    abbr = form.data.get('abbr')
    if not abbr or len(abbr) != 2:
        return redirect('homepage')
    return redirect('region', abbr=abbr)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def region(request, abbr):
    '''
    Context:
        - abbr
        - metadata
        - sessions
        - chambers
        - joint_committee_count
        - geo_bounds
        - nav_active

    Templates:
        - bill/web/public/region.html
    '''
    report = db.reports.find_one({'_id': abbr})
    try:
        meta = Metadata.get_object(abbr)
    except DoesNotExist:
        raise Http404

    fallback_bounds = GEO_BOUNDS['US']
    geo_bounds = GEO_BOUNDS.get(abbr.upper(), fallback_bounds)

    # count legislators
    legislators = meta.legislators({'active': True}, {'party': True,
                                                      'chamber': True})
    # Maybe later, mapreduce instead?
    party_counts = defaultdict(lambda: defaultdict(int))
    for leg in legislators:
        if 'chamber' in leg:    # exclude lt. governors
            party_counts[leg['chamber']][leg['party']] += 1

    chambers = []

    for chamber_type, chamber in meta['chambers'].items():
        res = {}

        # chamber metadata
        res['type'] = chamber_type
        res['title'] = chamber['title']
        res['name'] = chamber['name']

        # legislators
        res['legislators'] = {
            'count': sum(party_counts[chamber_type].values()),
            'party_counts': dict(party_counts[chamber_type]),
        }

        # committees
        res['committees_count'] = meta.committees({'chamber': chamber_type}
                                                 ).count()

        res['latest_bills'] = meta.bills({'chamber': chamber_type}).sort(
            [('action_dates.first', -1)]).limit(2)
        res['passed_bills'] = meta.bills({'chamber': chamber_type}).sort(
            [('action_dates.passed_' + chamber_type, -1)]).limit(2)

        chambers.append(res)

    joint_committee_count = meta.committees({'chamber': 'joint'}).count()

    # add bill counts to session listing
    sessions = meta.sessions()
    for s in sessions:
        try:
            s['bill_count'] = (
                report['bills']['sessions'][s['id']]['upper_count']
                + report['bills']['sessions'][s['id']]['lower_count'])
        except KeyError:
            # there's a chance that the session had no bills
            s['bill_count'] = 0

    return render(request, templatename('region'),
                  dict(abbr=abbr, metadata=meta, sessions=sessions,
                       chambers=chambers,
                       joint_committee_count=joint_committee_count,
                       geo_bounds=geo_bounds,
                       nav_active='home'))

Response: Return a single region for the given abbreviation.


Instruction:
Write a Python 3 script for
searching for a specific entry in the list of legislators.

Response:
def search(request, abbr):
    '''
    Context:
        - search_text
        - abbr
        - metadata
        - found_by_id
        - bill_results
        - more_bills_available
        - legislators_list
        - nav_active

    Tempaltes:
        - billy/web/public/search_results_no_query.html
        - billy/web/public/search_results_bills_legislators.html
        - billy/web/public/bills_list_row_with_abbr_and_session.html
    '''
    if not request.GET:
        return render(request, templatename('search_results_no_query'),
                      {'abbr': abbr})

    search_text = unicode(request.GET['search_text']).encode('utf8')

    # First try to get by bill_id.
    if re.search(r'\d', search_text):
        url = '/%s/bills?' % abbr
        url += urllib.urlencode([('search_text', search_text)])
        return redirect(url)

    else:
        found_by_id = False
        kwargs = {}
        if abbr != 'all':
            kwargs['abbr'] = abbr
        bill_results = Bill.search(search_text, sort='last', **kwargs)

        # Limit the bills if it's a search.
        bill_result_count = len(bill_results)
        more_bills_available = (bill_result_count > 5)
        bill_results = bill_results[:5]

        # See if any legislator names match. First split up name to avoid
        # the Richard S. Madaleno problem. See Jira issue OS-32.
        textbits = search_text.split()
        textbits = filter(lambda s: 2 < len(s), textbits)
        textbits = filter(lambda s: '.' not in s, textbits)
        andspec = []
        for text in textbits:
            andspec.append({'full_name': {'$regex': text, '$options': 'i'}})
        if andspec:
            spec = {'$and': andspec}
        else:
            spec = {'full_name': {'$regex': search_text, '$options': 'i'}}

        # Run the query.
        if abbr != 'all':
            spec[settings.LEVEL_FIELD] = abbr
        legislator_results = list(db.legislators.find(spec).sort(
            [('active', -1)]))

    if abbr != 'all':
        metadata = Metadata.get_object(abbr)
    else:
        metadata = None

    return render(
        request, templatename('search_results_bills_legislators'),
        dict(search_text=search_text,
             abbr=abbr,
             metadata=metadata,
             found_by_id=found_by_id,
             bill_results=bill_results,
             bill_result_count=bill_result_count,
             more_bills_available=more_bills_available,
             legislators_list=legislator_results,
             column_headers_tmplname=None,  # not used
             rowtemplate_name=templatename('bills_list_row_with'
                                           '_abbr_and_session'),
             show_chamber_column=True,
             nav_active=None))


Instruction:
Make a summary of the following Python 3 code
def add_member(self, legislator, role='member', **kwargs):
        self['members'].append(dict(name=legislator, role=role,
                                    **kwargs))

Response: Add a member to the committee object.


Instruction:
How would you explain what the following Python 3 function does
def _bytype(self, action_type, action_spec=None):
        '''Return the most recent date on which action_type occurred.
        Action spec is a dictionary of key-value attrs to match.'''
        for action in reversed(self.bill['actions']):
            if action_type in action['type']:
                for k, v in action_spec.items():
                    if action[k] == v:
                        yield action

Response: Return the most recent date on which action_type occurred.


Instruction:
Can you write a function in Python 3 where it
returns the yes / total ratio as a percetage string suitable for use as css attribute.

Response:
def _ratio(self, key):
        '''Return the yes/total ratio as a percetage string
        suitable for use as as css attribute.'''
        total = float(self._total_votes())
        try:
            return math.floor(self[key] / total * 100)
        except ZeroDivisionError:
            return float(0)


Instruction:
How would you code a function in Python 3 to
return the value of this vote.

Response:
def legislator_vote_value(self):
        '''If this vote was accessed through the legislator.votes_manager,
        return the value of this legislator's vote.
        '''
        if not hasattr(self, 'legislator'):
            msg = ('legislator_vote_value can only be called '
                   'from a vote accessed by legislator.votes_manager.')
            raise ValueError(msg)
        leg_id = self.legislator.id
        for k in ('yes', 'no', 'other'):
            for leg in self[k + '_votes']:
                if leg['leg_id'] == leg_id:
                    return k


Instruction:
Given the following Python 3 function, write the documentation
def _vote_legislators(self, yes_no_other):
        '''Return all legislators who votes yes/no/other on this bill.
        '''
        #id_getter = operator.itemgetter('leg_id')
        #ids = map(id_getter, self['%s_votes' % yes_no_other])
        #return map(self._legislator_objects.get, ids)
        result = []
        for voter in self[yes_no_other + '_votes']:
            if voter['leg_id']:
                result.append(self._legislator_objects.get(voter['leg_id']))
            else:
                result.append(voter)
        return result

Response: Return all legislators who votes yes or no on this bill.


Instruction:
How would you code a function in Python 3 to
guess whether this vote is a voice vote.

Response:
def is_probably_a_voice_vote(self):
        '''Guess whether this vote is a "voice vote".'''
        if '+voice_vote' in self:
            return True
        if '+vote_type' in self:
            if self['+vote_type'] == 'Voice':
                return True
        if 'voice vote' in self['motion'].lower():
            return True
        return False


Instruction:
Can you tell what is the following Python 3 function doing
def bill_objects(self):
        '''Returns a cursor of full bill objects for any bills that have
        ids. Not in use anyware as of 12/18/12, but handy to have around.
        '''
        bills = []
        for bill in self['related_bills']:
            if 'id' in bill:
                bills.append(bill['id'])
        return db.bills.find({"_id": {"$in": bills}})

Response:
Returns a cursor of full bill objects for any bills that have
        ids. Not in use anyware as of 12 / 18 / 12 but handy to have around.


Instruction:
Here you have a function in Python 3, explain what it does
def host(self):
        '''Return the host committee.
        '''
        _id = None
        for participant in self['participants']:
            if participant['type'] == 'host':
                if set(['participant_type', 'id']) < set(participant):
                    # This event uses the id keyname "id".
                    if participant['participant_type'] == 'committee':
                        _id = participant['id']
                        if _id is None:
                            continue
                        return self.committees_dict.get(_id)
                else:
                    return participant['participant']

Response: Return the host committee.


Instruction:
Write a Python 3 script for
returning a list of members that chair the host committee and including co - chair and chairperson. This could concievalby yield a false positive positive.

Response:
def host_chairs(self):
        '''Returns a list of members that chair the host committee,
        including "co-chair" and "chairperson." This could concievalby
        yield a false positive if the person's title is 'dunce chair'.
        '''
        chairs = []
        # Host is guaranteed to be a committe or none.
        host = self.host()
        if host is None:
            return
        for member, full_member in host.members_objects:
            if 'chair' in member.get('role', '').lower():
                chairs.append((member, full_member))
        return chairs


Instruction:
Make a summary of the following Python 3 code
def host_members(self):
        '''Return the members of the host committee.
        '''
        host = self.host()
        if host is None:
            return
        for member, full_member in host.members_objects:
            yield full_member

Response: Return the members of the host committee.


Instruction:
Can you implement a function in Python 3 that
returns a new empty tree of committees for the given author.

Response:
def committees(request, abbr):
    '''
    Context:
        chamber
        committees
        abbr
        metadata
        chamber_name
        chamber_select_template
        chamber_select_collection
        chamber_select_chambers
        committees_table_template
        show_chamber_column
        sort_order
        nav_active

    Templates:
        - billy/web/public/committees.html
        - billy/web/public/committees-pjax.html
        - billy/web/public/chamber_select_form.html
        - billy/web/public/committees_table.html
    '''
    try:
        meta = Metadata.get_object(abbr)
    except DoesNotExist:
        raise Http404

    chamber = request.GET.get('chamber', 'both')
    if chamber in ('upper', 'lower'):
        chamber_name = meta['chambers'][chamber]['name']
        spec = {'chamber': chamber}
        show_chamber_column = False
    elif chamber == 'joint':
        chamber_name = 'Joint'
        spec = {'chamber': 'joint'}
        show_chamber_column = False
    else:
        chamber = 'both'
        spec = {}
        show_chamber_column = True
        chamber_name = ''

    chambers = dict((k, v['name']) for k, v in meta['chambers'].items())
    if meta.committees({'chamber': 'joint'}).count():
        chambers['joint'] = 'Joint'

    fields = mongo_fields('committee', 'subcommittee', 'members',
                          settings.LEVEL_FIELD, 'chamber')

    sort_key = request.GET.get('key', 'committee')
    sort_order = int(request.GET.get('order', 1))

    committees = meta.committees_legislators(spec, fields=fields,
                                             sort=[(sort_key, sort_order)])

    sort_order = -sort_order

    return TemplateResponse(
        request, templatename('committees'),
        dict(chamber=chamber, committees=committees, abbr=abbr, metadata=meta,
             chamber_name=chamber_name,
             chamber_select_template=templatename('chamber_select_form'),
             chamber_select_collection='committees',
             chamber_select_chambers=chambers,
             committees_table_template=templatename('committees_table'),
             show_chamber_column=show_chamber_column, sort_order=sort_order,
             nav_active='committees'))


Instruction:
Explain what the following Python 3 code does
def committee(request, abbr, committee_id):
    '''
    Context:
        - committee
        - abbr
        - metadata
        - sources
        - nav_active

    Tempaltes:
        - billy/web/public/committee.html
    '''
    committee = db.committees.find_one({'_id': committee_id})
    if committee is None:
        raise Http404

    return render(request, templatename('committee'),
                  dict(committee=committee, abbr=abbr,
                       metadata=Metadata.get_object(abbr),
                       sources=committee['sources'],
                       nav_active='committees'))

Response: Return a single committee.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def update_common(obj, report):
    # updated checks
    if obj['updated_at'] >= yesterday:
        report['_updated_today_count'] += 1
        if obj['updated_at'] >= last_month:
            report['_updated_this_month_count'] += 1
            if obj['updated_at'] >= last_year:
                report['_updated_this_year_count'] += 1

Response: Update the common statistics for the objects.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def legislators(request, abbr):
    '''
    Context:
        - metadata
        - chamber
        - chamber_title
        - chamber_select_template
        - chamber_select_collection
        - chamber_select_chambers
        - show_chamber_column
        - abbr
        - legislators
        - sort_order
        - sort_key
        - legislator_table
        - nav_active

    Templates:
        - billy/web/public/legislators.html
        - billy/web/public/chamber_select_form.html
        - billy/web/public/legislator_table.html
    '''
    try:
        meta = Metadata.get_object(abbr)
    except DoesNotExist:
        raise Http404

    spec = {'active': True, 'district': {'$exists': True}}

    chambers = dict((k, v['name']) for k, v in meta['chambers'].items())

    chamber = request.GET.get('chamber', 'both')
    if chamber in chambers:
        spec['chamber'] = chamber
        chamber_title = meta['chambers'][chamber]['title'] + 's'
    else:
        chamber = 'both'
        chamber_title = 'Legislators'

    fields = mongo_fields('leg_id', 'full_name', 'photo_url', 'district',
                          'party', 'first_name', 'last_name', 'chamber',
                          billy_settings.LEVEL_FIELD, 'last_name')

    sort_key = 'district'
    sort_order = 1

    if request.GET:
        sort_key = request.GET.get('key', sort_key)
        sort_order = int(request.GET.get('order', sort_order))

    legislators = meta.legislators(extra_spec=spec, fields=fields)

    def sort_by_district(obj):
        matchobj = re.search(r'\d+', obj.get('district', '') or '')
        if matchobj:
            return int(matchobj.group())
        else:
            return obj.get('district', '')

    legislators = sorted(legislators, key=sort_by_district)

    if sort_key != 'district':
        legislators = sorted(legislators, key=operator.itemgetter(sort_key),
                             reverse=(sort_order == -1))
    else:
        legislators = sorted(legislators, key=sort_by_district,
                             reverse=bool(0 > sort_order))

    sort_order = {1: -1, -1: 1}[sort_order]
    legislators = list(legislators)

    return TemplateResponse(
        request, templatename('legislators'),
        dict(metadata=meta, chamber=chamber,
             chamber_title=chamber_title,
             chamber_select_template=templatename('chamber_select_form'),
             chamber_select_collection='legislators',
             chamber_select_chambers=chambers, show_chamber_column=True,
             abbr=abbr, legislators=legislators, sort_order=sort_order,
             sort_key=sort_key,
             legislator_table=templatename('legislator_table'),
             nav_active='legislators'))

Response: Return a list of legislators for the given legislator.


Instruction:
Write a Python 3 function that can
return a single legislator.

Response:
def legislator(request, abbr, _id, slug=None):
    '''
    Context:
        - vote_preview_row_template
        - roles
        - abbr
        - district_id
        - metadata
        - legislator
        - sources
        - sponsored_bills
        - legislator_votes
        - has_votes
        - nav_active

    Templates:
        - billy/web/public/legislator.html
        - billy/web/public/vote_preview_row.html

    '''
    try:
        meta = Metadata.get_object(abbr)
    except DoesNotExist:
        raise Http404

    legislator = db.legislators.find_one({'_id': _id})
    if legislator is None:
        spec = {'_all_ids': _id}
        cursor = db.legislators.find(spec)
        msg = 'Two legislators returned for spec %r' % spec
        assert cursor.count() < 2, msg
        try:
            legislator = next(cursor)
        except StopIteration:
            raise Http404('No legislator was found with leg_id = %r' % _id)
        else:
            return redirect(legislator.get_absolute_url(), permanent=True)

    if not legislator['active']:
        return legislator_inactive(request, abbr, legislator)

    district = db.districts.find({'abbr': abbr, 'chamber': legislator['chamber'],
                                  'name': legislator['district']})
    if district:
        district_id = district[0]['division_id']
    else:
        district_id = None

    sponsored_bills = legislator.sponsored_bills(
        limit=6, sort=[('action_dates.first', pymongo.DESCENDING)])

    # Note to self: Another slow query
    legislator_votes = legislator.votes_6_sorted()
    has_votes = bool(legislator_votes)
    return render(
        request, templatename('legislator'),
        dict(vote_preview_row_template=templatename('vote_preview_row'),
             roles=legislator.roles_manager, abbr=abbr,
             district_id=district_id, metadata=meta, legislator=legislator,
             sources=legislator['sources'],
             sponsored_bills=list(sponsored_bills),
             legislator_votes=list(legislator_votes),
             has_votes=has_votes,
             nav_active='legislators'))


Instruction:
How would you explain what the following Python 3 function does
def legislator_inactive(request, abbr, legislator):
    '''
    Context:
        - vote_preview_row_template
        - old_roles
        - abbr
        - metadata
        - legislator
        - sources
        - sponsored_bills
        - legislator_votes
        - has_votes
        - nav_active

    Templates:
        - billy/web/public/legislator.html
        - billy/web/public/vote_preview_row.html
    '''
    sponsored_bills = legislator.sponsored_bills(
        limit=6, sort=[('action_dates.first', pymongo.DESCENDING)])

    legislator_votes = list(legislator.votes_6_sorted())
    has_votes = bool(legislator_votes)

    return render(
        request, templatename('legislator'),
        dict(vote_preview_row_template=templatename('vote_preview_row'),
             old_roles=legislator.old_roles_manager,
             abbr=abbr,
             metadata=legislator.metadata,
             legislator=legislator,
             sources=legislator['sources'],
             sponsored_bills=list(sponsored_bills),
             legislator_votes=legislator_votes,
             has_votes=has_votes,
             nav_active='legislators'))

Response: Return a new page with the active legislator.


Instruction:
Can you tell what is the following Python 3 function doing
def normalize_rank(rank):
    normalized_ranks = {
        'BA': 'UNDERGRADUATE',
        'BACHELOR': 'UNDERGRADUATE',
        'BS': 'UNDERGRADUATE',
        'BSC': 'UNDERGRADUATE',
        'JUNIOR': 'JUNIOR',
        'MAS': 'MASTER',
        'MASTER': 'MASTER',
        'MS': 'MASTER',
        'MSC': 'MASTER',
        'PD': 'POSTDOC',
        'PHD': 'PHD',
        'POSTDOC': 'POSTDOC',
        'SENIOR': 'SENIOR',
        'STAFF': 'STAFF',
        'STUDENT': 'PHD',
        'UG': 'UNDERGRADUATE',
        'UNDERGRADUATE': 'UNDERGRADUATE',
        'VISITING SCIENTIST': 'VISITOR',
        'VISITOR': 'VISITOR',
    }
    if not rank:
        return None
    rank = rank.upper().replace('.', '')
    return normalized_ranks.get(rank, 'OTHER')

Response: Normalize a rank in order to be schema - compliant.


Instruction:
Make a summary of the following Python 3 code
def get_recid_from_ref(ref_obj):
    if not isinstance(ref_obj, dict):
        return None
    url = ref_obj.get('$ref', '')
    return maybe_int(url.split('/')[-1])

Response: Retrieve recid from jsonref reference object.


Instruction:
Can you create a Python 3 function that
returns an absolute URL from a URL relative to the server root.

Response:
def absolute_url(relative_url):
    """Returns an absolute URL from a URL relative to the server root.

    The base URL is taken from the Flask app config if present, otherwise it
    falls back to ``http://inspirehep.net``.
    """
    default_server = 'http://inspirehep.net'
    server = current_app.config.get('SERVER_NAME', default_server)
    if not re.match('^https?://', server):
        server = u'http://{}'.format(server)
    return urllib.parse.urljoin(server, relative_url)


Instruction:
Can you create a Python 3 function that
converts a file path to a URL pointing to its path on AFS.

Response:
def afs_url(file_path):
    """Convert a file path to a URL pointing to its path on AFS.

    If ``file_path`` doesn't start with ``/opt/cds-invenio/``, and hence is not on
    AFS, it returns it unchanged.

    The base AFS path is taken from the Flask app config if present, otherwise
    it falls back to ``/afs/cern.ch/project/inspire/PROD``.
    """
    default_afs_path = '/afs/cern.ch/project/inspire/PROD'
    afs_path = current_app.config.get('LEGACY_AFS_PATH', default_afs_path)

    if file_path is None:
        return

    if file_path.startswith('/opt/cds-invenio/'):
        file_path = os.path.relpath(file_path, '/opt/cds-invenio/')
        file_path = os.path.join(afs_path, file_path)
        return urllib.parse.urljoin('file://', urllib.request.pathname2url(file_path.encode('utf-8')))

    return file_path


Instruction:
Here you have a function in Python 3, explain what it does
def strip_empty_values(obj):
    if isinstance(obj, dict):
        new_obj = {}
        for key, val in obj.items():
            new_val = strip_empty_values(val)
            if new_val is not None:
                new_obj[key] = new_val
        return new_obj or None
    elif isinstance(obj, (list, tuple, set)):
        new_obj = []
        for val in obj:
            new_val = strip_empty_values(val)
            if new_val is not None:
                new_obj.append(new_val)
        return type(obj)(new_obj) or None
    elif obj or obj is False or obj == 0:
        return obj
    else:
        return None

Response: Recursively strips empty values from the object.


Instruction:
How would you explain what the following Python 3 function does
def dedupe_all_lists(obj, exclude_keys=()):
    squared_dedupe_len = 10
    if isinstance(obj, dict):
        new_obj = {}
        for key, value in obj.items():
            if key in exclude_keys:
                new_obj[key] = value
            else:
                new_obj[key] = dedupe_all_lists(value)
        return new_obj
    elif isinstance(obj, (list, tuple, set)):
        new_elements = [dedupe_all_lists(v) for v in obj]
        if len(new_elements) < squared_dedupe_len:
            new_obj = dedupe_list(new_elements)
        else:
            new_obj = dedupe_list_of_dicts(new_elements)
        return type(obj)(new_obj)
    else:
        return obj

Response: Recursively remove duplucates from all lists.


Instruction:
Make a summary of the following Python 3 code
def normalize_date_aggressively(date):
    def _strip_last_part(date):
        parts = date.split('-')
        return '-'.join(parts[:-1])

    fake_dates = {'0000', '9999'}
    if date in fake_dates:
        return None
    try:
        return normalize_date(date)
    except ValueError:
        if '-' not in date:
            raise
        else:
            new_date = _strip_last_part(date)
            return normalize_date_aggressively(new_date)

Response: Normalize date stripping date parts until a valid date is obtained.


Instruction:
Can you tell what is the following Python 3 function doing
def match_country_name_to_its_code(country_name, city=''):
    if country_name:
        country_name = country_name.upper().replace('.', '').strip()

        if country_to_iso_code.get(country_name):
            return country_to_iso_code.get(country_name)
        elif country_name == 'KOREA':
            if city.upper() in south_korean_cities:
                return 'KR'
        else:
            for c_code, spellings in countries_alternative_spellings.items():
                for spelling in spellings:
                    if country_name == spelling:
                        return c_code

    return None

Response: Try to match country name with its code.


Instruction:
How would you explain what the following Python 3 function does
def match_us_state(state_string):
    if state_string:
        state_string = state_string.upper().replace('.', '').strip()
        if us_state_to_iso_code.get(state_string):
            return us_state_to_iso_code.get(state_string)
        else:
            for code, state_spellings in us_states_alternative_spellings.items():
                for spelling in state_spellings:
                    if state_string == spelling:
                        return code
    return None

Response: Try to match a string with one of the states in the US.


Instruction:
How would you code a function in Python 3 to
parse a conference address.

Response:
def parse_conference_address(address_string):
    """Parse a conference address.

    This is a pretty dummy address parser. It only extracts country
    and state (for US) and should be replaced with something better,
    like Google Geocoding.
    """

    geo_elements = address_string.split(',')
    city = geo_elements[0]
    country_name = geo_elements[-1].upper().replace('.', '').strip()
    us_state = None
    state = None
    country_code = None

    # Try to match the country
    country_code = match_country_name_to_its_code(country_name, city)

    if country_code == 'US' and len(geo_elements) > 1:
        us_state = match_us_state(geo_elements[-2].upper().strip()
                                  .replace('.', ''))

    if not country_code:
        # Sometimes the country name stores info about U.S. state
        us_state = match_us_state(country_name)

    if us_state:
        state = us_state
        country_code = 'US'

    return {
        'cities': [
            city,
        ],
        'country_code': country_code,
        'postal_code': None,
        'state': state,
    }


Instruction:
Can you generate a brief explanation for the following Python 3 code
def parse_institution_address(address, city, state_province,
                              country, postal_code, country_code):
    """Parse an institution address."""
    address_list = force_list(address)
    state_province = match_us_state(state_province) or state_province

    postal_code = force_list(postal_code)
    country = force_list(country)
    country_code = match_country_code(country_code)

    if isinstance(postal_code, (tuple, list)):
        postal_code = ', '.join(postal_code)

    if isinstance(country, (tuple, list)):
        country = ', '.join(set(country))

    if not country_code and country:
        country_code = match_country_name_to_its_code(country)

    if not country_code and state_province and state_province in us_state_to_iso_code.values():
        country_code = 'US'

    return {
        'cities': force_list(city),
        'country_code': country_code,
        'postal_address': address_list,
        'postal_code': postal_code,
        'state': state_province,
    }

Response: Parse an institution address.


Instruction:
Explain what the following Python 3 code does
def ids2marc(self, key, value):
    def _is_schema_inspire_bai(id_, schema):
        return schema == 'INSPIRE BAI'

    def _is_schema_inspire_id(id_, schema):
        return schema == 'INSPIRE ID'

    def _is_schema_spires(id_, schema):
        return schema == 'SPIRES'

    def _is_schema_linkedin(id, schema):
        return schema == 'LINKEDIN'

    def _is_schema_twitter(id, schema):
        return schema == 'TWITTER'

    id_ = value.get('value')
    schema = value.get('schema')

    if _is_schema_spires(id_, schema):
        self.setdefault('970', []).append({'a': id_})
    elif _is_schema_linkedin(id_, schema):
        self.setdefault('8564', []).append(
            {
                'u': u'https://www.linkedin.com/in/{id}'.format(id=quote_url(id_)),
                'y': 'LINKEDIN',
            }
        )
    elif _is_schema_twitter(id_, schema):
        self.setdefault('8564', []).append(
            {
                'u': u'https://twitter.com/{id}'.format(id=id_),
                'y': 'TWITTER',
            }
        )
    elif _is_schema_inspire_id(id_, schema):
        return {
            'a': id_,
            '9': 'INSPIRE',
        }
    elif _is_schema_inspire_bai(id_, schema):
        return {
            'a': id_,
            '9': 'BAI',
        }
    else:
        return {
            'a': id_,
            '9': schema,
        }

Response: Populate the 3035 MARC field.


Instruction:
Create a Python 3 function to
populate the name key.

Response:
def name(self, key, value):
    """Populate the ``name`` key.

    Also populates the ``status``, ``birth_date`` and ``death_date`` keys through side effects.
    """
    def _get_title(value):
        c_value = force_single_element(value.get('c', ''))
        if c_value != 'title (e.g. Sir)':
            return c_value

    def _get_value(value):
        a_value = force_single_element(value.get('a', ''))
        q_value = force_single_element(value.get('q', ''))
        return a_value or normalize_name(q_value)

    if value.get('d'):
        dates = value['d']
        try:
            self['death_date'] = normalize_date(dates)
        except ValueError:
            dates = dates.split(' - ')
            if len(dates) == 1:
                dates = dates[0].split('-')
            self['birth_date'] = normalize_date(dates[0])
            self['death_date'] = normalize_date(dates[1])

    self['status'] = force_single_element(value.get('g', '')).lower()

    return {
        'numeration': force_single_element(value.get('b', '')),
        'preferred_name': force_single_element(value.get('q', '')),
        'title': _get_title(value),
        'value': _get_value(value),
    }


Instruction:
Here you have a function in Python 3, explain what it does
def name2marc(self, key, value):
    result = self.get('100', {})

    result['a'] = value.get('value')
    result['b'] = value.get('numeration')
    result['c'] = value.get('title')
    result['q'] = value.get('preferred_name')

    if 'name_variants' in value:
        self['400'] = [{'a': el} for el in value['name_variants']]
    if 'native_names' in value:
        self['880'] = [{'a': el} for el in value['native_names']]
    if 'previous_names' in value:
        prev_names = [
            {'a': u'Formerly {}'.format(prev_name)}
            for prev_name in value['previous_names']
        ]
        self['667'] = prev_names

    return result

Response: Populate the 100 field.


Instruction:
Explain what the following Python 3 code does
def positions(self, key, value):
    email_addresses = self.get("email_addresses", [])
    current = None
    record = None

    recid_or_status = force_list(value.get('z'))
    for el in recid_or_status:
        if el.lower() == 'current':
            current = True if value.get('a') else None
        else:
            record = get_record_ref(maybe_int(el), 'institutions')

    rank = normalize_rank(value.get('r'))

    current_email_addresses = force_list(value.get('m'))
    non_current_email_addresses = force_list(value.get('o'))

    email_addresses.extend({
        'value': address,
        'current': True,
    } for address in current_email_addresses)
    email_addresses.extend({
        'value': address,
        'current': False,
    } for address in non_current_email_addresses)

    self['email_addresses'] = email_addresses

    if 'a' not in value:
        return None

    return {
        'institution': value['a'],
        'record': record,
        'curated_relation': True if record is not None else None,
        'rank': rank,
        'start_date': normalize_date(value.get('s')),
        'end_date': normalize_date(value.get('t')),
        'current': current,
    }

Response: Populate the positions field by side effect.


Instruction:
Create a Python 3 function for
populating the 595 MARCXML field.

Response:
def email_addresses2marc(self, key, value):
    """Populate the 595 MARCXML field.

    Also populates the 371 field as a side effect.
    """
    m_or_o = 'm' if value.get('current') else 'o'
    element = {
        m_or_o: value.get('value')
    }

    if value.get('hidden'):
        return element
    else:
        self.setdefault('371', []).append(element)
        return None


Instruction:
Implement a function in Python 3 to
populate the email_addresses field using the 595 MARCXML field.

Response:
def email_addresses595(self, key, value):
    """Populates the ``email_addresses`` field using the 595 MARCXML field.

    Also populates ``_private_notes`` as a side effect.
    """
    emails = self.get('email_addresses', [])

    if value.get('o'):
        emails.append({
            'value': value.get('o'),
            'current': False,
            'hidden': True,
        })

    if value.get('m'):
        emails.append({
            'value': value.get('m'),
            'current': True,
            'hidden': True,
        })

    notes = self.get('_private_notes', [])
    new_note = (
        {
            'source': value.get('9'),
            'value': _private_note,
        } for _private_note in force_list(value.get('a'))
    )
    notes.extend(new_note)
    self['_private_notes'] = notes

    return emails