# Path: dividebatur/common.py
# def make_logger(name: str) -> logging.Logger:
# 
# Path: dividebatur/aecdata/utils.py
# def int_or_none(s):
#     if s == '':
#         return None
#     try:
#         return int(s)
#     except ValueError:
#         return None
# 
# def named_tuple_iter(name, reader, header, **kwargs):
#     field_names = [t for t in [t.strip().replace('-', '_')
#                                for t in header] if t]
#     typ = namedtuple(name, field_names)
#     mappings = []
#     for field_name in kwargs:
#         idx = field_names.index(field_name)
#         mappings.append((idx, kwargs[field_name]))
#     for row in reader:
#         for idx, map_fn in mappings:
#             row[idx] = map_fn(row[idx])
#         yield typ(*row)
# 
# def ticket_sort_key(ticket):
#     "sort key for an ATL ticket, eg. A..Z, AA..ZZ"
#     return (len(ticket), ticket)


import itertools
import gzip
import csv
from collections import defaultdict
from ..common import logger
from .utils import int_or_none, named_tuple_iter, ticket_sort_key
        for group, n in self.ticket_votes:
            size = len(self.gvt[group])
            assert(size <= 3)
            remainder = n % size
            # TODO: implement some randomization here
            remainder_pattern = [1] * remainder + [0] * (size - remainder)
            remainder_pattern = [0] * (size - remainder) + [1] * (remainder)
            if remainder:
                logger.info("GVT split ticket remainder, AEO input needed: %s" % (remainder_pattern))
            # remainder_pattern = [0] * (size-remainder) + [1] * remainder
            for ticket, extra in zip(self.gvt[group], remainder_pattern):
                yield ticket, int(n / size) + extra


class SenateBTL:
    """
    build up Ticket instances for each below-the-line vote in the AEC dataset
    aggregate BTL votes with the same preference flow together
    (pre-2015)
    """

    def __init__(self, btl_csv):
        self.ticket_votes = defaultdict(int)
        self.load_btl(btl_csv)

    def load_btl(self, btl_csv):
        with gzip.open(btl_csv, 'rt') as fd:
            reader = csv.reader(fd)
            next(reader)  # skip the version
            header = next(reader)

# Path: dividebatur/common.py
# def make_logger(name: str) -> logging.Logger:
# 
# Path: dividebatur/aecdata/utils.py
# def int_or_none(s):
#     if s == '':
#         return None
#     try:
#         return int(s)
#     except ValueError:
#         return None
# 
# def named_tuple_iter(name, reader, header, **kwargs):
#     field_names = [t for t in [t.strip().replace('-', '_')
#                                for t in header] if t]
#     typ = namedtuple(name, field_names)
#     mappings = []
#     for field_name in kwargs:
#         idx = field_names.index(field_name)
#         mappings.append((idx, kwargs[field_name]))
#     for row in reader:
#         for idx, map_fn in mappings:
#             row[idx] = map_fn(row[idx])
#         yield typ(*row)
# 
# def ticket_sort_key(ticket):
#     "sort key for an ATL ticket, eg. A..Z, AA..ZZ"
#     return (len(ticket), ticket)


# Path: dividebatur/aecdata/pre2015.py
import itertools
import gzip
import csv
from collections import defaultdict
from ..common import logger
from .utils import int_or_none, named_tuple_iter, ticket_sort_key

        for group, n in self.ticket_votes:
            size = len(self.gvt[group])
            assert(size <= 3)
            remainder = n % size
            # TODO: implement some randomization here
            remainder_pattern = [1] * remainder + [0] * (size - remainder)
            remainder_pattern = [0] * (size - remainder) + [1] * (remainder)
            if remainder:
                logger.info("GVT split ticket remainder, AEO input needed: %s" % (remainder_pattern))
            # remainder_pattern = [0] * (size-remainder) + [1] * remainder
            for ticket, extra in zip(self.gvt[group], remainder_pattern):
                yield ticket, int(n / size) + extra


class SenateBTL:
    """
    build up Ticket instances for each below-the-line vote in the AEC dataset
    aggregate BTL votes with the same preference flow together
    (pre-2015)
    """

    def __init__(self, btl_csv):
        self.ticket_votes = defaultdict(int)
        self.load_btl(btl_csv)

    def load_btl(self, btl_csv):
        with gzip.open(btl_csv, 'rt') as fd:
            reader = csv.reader(fd)
            next(reader)  # skip the version
            header = next(reader)

            it = named_tuple_iter(