def context_role(self, bill=None, vote=None, session=None, term=None):
<EOL>if not any([bill, vote, session, term]):<EOL><INDENT>try:<EOL><INDENT>bill = self.bill<EOL><DEDENT>except AttributeError:<EOL><INDENT>try:<EOL><INDENT>vote = self.vote<EOL><DEDENT>except AttributeError:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT><DEDENT><DEDENT>if not any([bill, vote, session, term]):<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>if bill is not None:<EOL><INDENT>term = bill['<STR_LIT>']<EOL><DEDENT>elif vote is not None:<EOL><INDENT>try:<EOL><INDENT>_bill = vote.bill<EOL><DEDENT>except AttributeError:<EOL><INDENT>_bill = BillVote(vote).bill<EOL><DEDENT>if callable(_bill):<EOL><INDENT>_bill = _bill()<EOL><DEDENT>term = _bill['<STR_LIT>']<EOL><DEDENT>if term is None and session is not None:<EOL><INDENT>term = term_for_session(self[settings.LEVEL_FIELD], session)<EOL><DEDENT>roles = [r for r in self['<STR_LIT>']<EOL>if r.get('<STR_LIT:type>') == '<STR_LIT>' and r.get('<STR_LIT>') == term]<EOL>roles = list(filter(None, roles))<EOL>if not roles:<EOL><INDENT>roles = [r for r in self.get('<STR_LIT>', {}).get(term, [])<EOL>if r.get('<STR_LIT:type>') == '<STR_LIT>']<EOL><DEDENT>roles = list(filter(None, roles))<EOL>if not roles:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>if len(roles) == <NUM_LIT:1>:<EOL><INDENT>role = roles.pop()<EOL>self['<STR_LIT>'] = role<EOL>return role<EOL><DEDENT>if not list(filter(None, [bill, vote])):<EOL><INDENT>if term is not None:<EOL><INDENT>role = roles[<NUM_LIT:0>]<EOL>self['<STR_LIT>'] = role<EOL>return role<EOL><DEDENT>if session is not None:<EOL><INDENT>session_data = self.metadata['<STR_LIT>'][session]<EOL>for role in roles:<EOL><INDENT>role_start = role.get('<STR_LIT>')<EOL>role_end = role.get('<STR_LIT>')<EOL>session_start = session_data.get('<STR_LIT>')<EOL>session_end = session_data.get('<STR_LIT>')<EOL>if session_start and session_end:<EOL><INDENT>started_during = (role_start < session_start <<EOL>role_end)<EOL>ended_during = (role_start < session_end < role_end)<EOL>if started_during or ended_during:<EOL><INDENT>self['<STR_LIT>'] = role<EOL>return role<EOL><DEDENT><DEDENT>else:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>role = roles[<NUM_LIT:0>]<EOL>self['<STR_LIT>'] = role<EOL>return role<EOL><DEDENT><DEDENT>if vote is not None:<EOL><INDENT>date = vote['<STR_LIT:date>']<EOL><DEDENT>if bill is not None:<EOL><INDENT>date = bill['<STR_LIT>']['<STR_LIT>']<EOL><DEDENT>dates_exist = False<EOL>for role in roles:<EOL><INDENT>start_date = role.get('<STR_LIT>')<EOL>end_date = role.get('<STR_LIT>')<EOL>if start_date and end_date:<EOL><INDENT>dates_exist = True<EOL>if start_date < date < end_date:<EOL><INDENT>self['<STR_LIT>'] = role<EOL>return role<EOL><DEDENT><DEDENT><DEDENT>if dates_exist:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>role = roles.pop()<EOL>self['<STR_LIT>'] = role<EOL>return role<EOL><DEDENT>return '<STR_LIT>'<EOL>
Tell this legislator object which session to use when calculating
        the legisator's context_role for a given bill or vote.