# Path: libre/apps/data_drivers/utils.py
# def return_attrib(obj, attrib):
#     return reduce(get_value, attrib.split(u'.'), obj)


from operator import itemgetter
from .exceptions import LIBREFieldError
from .utils import return_attrib



class Aggregate(object):
    def __init__(self, argument):
        self.argument = argument
        self.field = argument
        self.properties = None

        if '.' in self.argument:
            # Aggregate by an element property
            self.field, self.properties = self.argument.split('.', 1)

    def execute(self, elements):
        try:
            return self._execute(elements)
        except KeyError:
            raise LIBREFieldError('Unknown field: %s' % self.argument)
        except AttributeError as exception:
            raise LIBREFieldError('Field property error; %s' % exception)
        except TypeError as exception:
            raise LIBREFieldError('Field aggregation error; %s' % exception)


class Count(Aggregate):
    def _execute(self, elements):
        if self.argument == '*':
            return len(list(elements))
        else:
            if self.properties:

# Path: libre/apps/data_drivers/utils.py
# def return_attrib(obj, attrib):
#     return reduce(get_value, attrib.split(u'.'), obj)


# Path: libre/apps/data_drivers/aggregates.py
from operator import itemgetter
from .exceptions import LIBREFieldError
from .utils import return_attrib




class Aggregate(object):
    def __init__(self, argument):
        self.argument = argument
        self.field = argument
        self.properties = None

        if '.' in self.argument:
            # Aggregate by an element property
            self.field, self.properties = self.argument.split('.', 1)

    def execute(self, elements):
        try:
            return self._execute(elements)
        except KeyError:
            raise LIBREFieldError('Unknown field: %s' % self.argument)
        except AttributeError as exception:
            raise LIBREFieldError('Field property error; %s' % exception)
        except TypeError as exception:
            raise LIBREFieldError('Field aggregation error; %s' % exception)


class Count(Aggregate):
    def _execute(self, elements):
        if self.argument == '*':
            return len(list(elements))
        else:
            if self.properties:

                return len(set([return_attrib(itemgetter(self.field)(element), self.properties) for element in elements]))