# Path: dividebatur/results.py
# class ElectionDistributionPerformed:
#     """
#     Information on any election distribution which is performed during a
#     counting round.
#     """
# 
#     def __init__(self, candidate_id, transfer_value):
#         """
#         transfer_value is a Fraction instance
#         """
#         self.candidate_id = candidate_id
#         self.transfer_value = transfer_value
# 
# class ExclusionDistributionPerformed:
#     """
#     Information on any exclusion distribution which is performed during a
#     counting round.
#     """
# 
#     def __init__(self, candidates, transfer_value):
#         """
#         candidates: a List of candidate_ids
#         transfer_value: transfer value, as a Fraction
#         """
#         self.candidates = candidates
#         self.transfer_value = transfer_value
# 
# class ActProvision:
#     """
#     Note that a provision of the Act has been used.
#     """
# 
#     def __init__(self, text):
#         """
#         text: textual description of the provision used.
#         """
# 
#         self.text = text
# 
# class CandidateElected:
#     """
#     Information on the election of a candidate.
#     """
# 
#     def __init__(self, candidate_id, order, excess_votes, paper_count, transfer_value):
#         """
#         candidate_id: the candidate elected
#         order: the number of the spot the candidate was elected to [1..N_vacancies]
#         excess_votes: the number of excess votes the candidate received
#         paper_count: the number of papers the candidate held at time of elect
#         transfer_value: the transfer value for the excess papers (0 if no excess papers)
#         """
# 
#         self.candidate_id = candidate_id
#         self.order = order
#         self.excess_votes = excess_votes
#         self.paper_count = paper_count
#         self.transfer_value = transfer_value
# 
# class CandidatesExcluded:
#     """
#     Information on the exclusion of one or more candidates.
#     """
# 
#     def __init__(self, candidates, transfer_values, reason):
#         """
#         candidates: list of candidate_ids of those candidates elected
#         transfer_values: the transfer values of papers to be distributed (a list of Fraction instances)
#         reason: an instance of ExclusionReason
#         """
#         self.candidates = candidates
#         self.transfer_values = transfer_values
#         self.reason = reason
# 
# class ExclusionReason:
#     """
#     information of interest about an exclusion of one or more
#     candidates.
# 
#     simply to make this information available to users of this
#     counter, this data is not used to make any decisions which
#     affect count results.
#     """
#     def __init__(self, reason, info):
#         """
#         reason: a string identifying the reason
#         info: additional information
#         """
#         self.reason, self.info = reason, info


from collections import defaultdict, namedtuple
from .results import ElectionDistributionPerformed, \
    ExclusionDistributionPerformed, ActProvision, \
    CandidateElected, CandidatesExcluded, \
    ExclusionReason
import itertools
import fractions
        return sorted_candidate_ids[self.election_tie_cb(candidates)]

    def determine_quota(self):
        self.total_papers = sum(count for _, count in self.papers_for_count)
        self.quota = int(self.total_papers / (self.vacancies + 1)) + 1

    def determine_elected_candidates_in_order(self, candidate_votes):
        """
        determine all candidates with at least a quota of votes in `candidate_votes'. returns results in
        order of decreasing vote count. Any ties are resolved within this method.
        """
        eligible_by_vote = defaultdict(list)
        for candidate_id, votes in candidate_votes.candidate_votes_iter():
            if candidate_id in self.candidates_elected:
                continue
            if votes < self.quota:
                continue
            eligible_by_vote[votes].append(candidate_id)

        elected = []
        for votes in reversed(sorted(eligible_by_vote)):
            candidate_ids = eligible_by_vote[votes]
            # we sort here to ensure stability, so external callers can hard-coded their response
            candidate_ids.sort(key=self.candidate_order_fn)
            if len(candidate_ids) == 1:
                elected.append(candidate_ids[0])
            else:
                tie_breaker_round = self.find_tie_breaker(candidate_ids)
                if tie_breaker_round is not None:
                    self.results.provision_used(

# Path: dividebatur/results.py
# class ElectionDistributionPerformed:
#     """
#     Information on any election distribution which is performed during a
#     counting round.
#     """
# 
#     def __init__(self, candidate_id, transfer_value):
#         """
#         transfer_value is a Fraction instance
#         """
#         self.candidate_id = candidate_id
#         self.transfer_value = transfer_value
# 
# class ExclusionDistributionPerformed:
#     """
#     Information on any exclusion distribution which is performed during a
#     counting round.
#     """
# 
#     def __init__(self, candidates, transfer_value):
#         """
#         candidates: a List of candidate_ids
#         transfer_value: transfer value, as a Fraction
#         """
#         self.candidates = candidates
#         self.transfer_value = transfer_value
# 
# class ActProvision:
#     """
#     Note that a provision of the Act has been used.
#     """
# 
#     def __init__(self, text):
#         """
#         text: textual description of the provision used.
#         """
# 
#         self.text = text
# 
# class CandidateElected:
#     """
#     Information on the election of a candidate.
#     """
# 
#     def __init__(self, candidate_id, order, excess_votes, paper_count, transfer_value):
#         """
#         candidate_id: the candidate elected
#         order: the number of the spot the candidate was elected to [1..N_vacancies]
#         excess_votes: the number of excess votes the candidate received
#         paper_count: the number of papers the candidate held at time of elect
#         transfer_value: the transfer value for the excess papers (0 if no excess papers)
#         """
# 
#         self.candidate_id = candidate_id
#         self.order = order
#         self.excess_votes = excess_votes
#         self.paper_count = paper_count
#         self.transfer_value = transfer_value
# 
# class CandidatesExcluded:
#     """
#     Information on the exclusion of one or more candidates.
#     """
# 
#     def __init__(self, candidates, transfer_values, reason):
#         """
#         candidates: list of candidate_ids of those candidates elected
#         transfer_values: the transfer values of papers to be distributed (a list of Fraction instances)
#         reason: an instance of ExclusionReason
#         """
#         self.candidates = candidates
#         self.transfer_values = transfer_values
#         self.reason = reason
# 
# class ExclusionReason:
#     """
#     information of interest about an exclusion of one or more
#     candidates.
# 
#     simply to make this information available to users of this
#     counter, this data is not used to make any decisions which
#     affect count results.
#     """
#     def __init__(self, reason, info):
#         """
#         reason: a string identifying the reason
#         info: additional information
#         """
#         self.reason, self.info = reason, info


# Path: dividebatur/counter.py
from collections import defaultdict, namedtuple
from .results import ElectionDistributionPerformed, \
    ExclusionDistributionPerformed, ActProvision, \
    CandidateElected, CandidatesExcluded, \
    ExclusionReason
import itertools
import fractions

        return sorted_candidate_ids[self.election_tie_cb(candidates)]

    def determine_quota(self):
        self.total_papers = sum(count for _, count in self.papers_for_count)
        self.quota = int(self.total_papers / (self.vacancies + 1)) + 1

    def determine_elected_candidates_in_order(self, candidate_votes):
        """
        determine all candidates with at least a quota of votes in `candidate_votes'. returns results in
        order of decreasing vote count. Any ties are resolved within this method.
        """
        eligible_by_vote = defaultdict(list)
        for candidate_id, votes in candidate_votes.candidate_votes_iter():
            if candidate_id in self.candidates_elected:
                continue
            if votes < self.quota:
                continue
            eligible_by_vote[votes].append(candidate_id)

        elected = []
        for votes in reversed(sorted(eligible_by_vote)):
            candidate_ids = eligible_by_vote[votes]
            # we sort here to ensure stability, so external callers can hard-coded their response
            candidate_ids.sort(key=self.candidate_order_fn)
            if len(candidate_ids) == 1:
                elected.append(candidate_ids[0])
            else:
                tie_breaker_round = self.find_tie_breaker(candidate_ids)
                if tie_breaker_round is not None:
                    self.results.provision_used(

                        ActProvision("Multiple candidates elected with %d votes. Tie broken from previous totals." % (votes)))