# 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

# PaperBundle: a bundle of TicketStates
# TicketState: a ``TicketState`` instance
#  - size: the number of tickets in the bundle
PaperBundle = namedtuple("PaperBundle", ("ticket_state", "size"))


# BundleTransaction: a collection of bundles which have been
# transferred to a candidate, at a common transfer value.
# We store this data so that when the bundles are distributed,
# we can appropriately adjust the votes held by the candidate.
# bundles:
# - bundles: a list of PaperBundle instances
# - transfer_value: the transfer value (a Fraction)
# - votes: the vote value of this bundle transaction (an integer)
BundleTransaction = namedtuple("BundleTransaction", ("bundles", "transfer_value", "votes"))


def make_bundle_transaction(bundles, transfer_value):
    votes_sum = sum(bundle.size for bundle in bundles)
    return BundleTransaction(bundles, transfer_value, int(votes_sum * transfer_value))


def get_preference(ticket_state):
    if ticket_state.up_to < len(ticket_state.preferences):
        return ticket_state.preferences[ticket_state.up_to]
    return None


class CountException(Exception):

# 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


# PaperBundle: a bundle of TicketStates
# TicketState: a ``TicketState`` instance
#  - size: the number of tickets in the bundle
PaperBundle = namedtuple("PaperBundle", ("ticket_state", "size"))


# BundleTransaction: a collection of bundles which have been
# transferred to a candidate, at a common transfer value.
# We store this data so that when the bundles are distributed,
# we can appropriately adjust the votes held by the candidate.
# bundles:
# - bundles: a list of PaperBundle instances
# - transfer_value: the transfer value (a Fraction)
# - votes: the vote value of this bundle transaction (an integer)
BundleTransaction = namedtuple("BundleTransaction", ("bundles", "transfer_value", "votes"))


def make_bundle_transaction(bundles, transfer_value):
    votes_sum = sum(bundle.size for bundle in bundles)
    return BundleTransaction(bundles, transfer_value, int(votes_sum * transfer_value))


def get_preference(ticket_state):
    if ticket_state.up_to < len(ticket_state.preferences):
        return ticket_state.preferences[ticket_state.up_to]
    return None


class CountException(Exception):

    pass