Exemple #1
0
    def validate(left, right) -> Tuple['Quantity', 'Quantity']:

        if isinstance(left, Quantity) and isinstance(right, Quantity):
            if left.instrument != right.instrument:
                raise IncompatibleInstrumentOperation(left, right)

            if (left.path_id
                    and right.path_id) and (left.path_id != right.path_id):
                raise QuantityOpPathMismatch(left.path_id, right.path_id)

            elif left.path_id and not right.path_id:
                right.path_id = left.path_id

            elif not left.path_id and right.path_id:
                left.path_id = right.path_id

            return left, right

        elif isinstance(left, Number) and isinstance(right, Quantity):
            left = Quantity(right.instrument, float(left), right.path_id)
            return left, right

        elif isinstance(left, Quantity) and isinstance(right, Number):
            right = Quantity(left.instrument, float(right), left.path_id)
            return left, right

        elif isinstance(left, Quantity):
            raise InvalidNonNumericQuantity(right)

        elif isinstance(right, Quantity):
            raise InvalidNonNumericQuantity(left)

        return left, right
    def _bool_operation(left: Union['Quantity', float,
                                    int], right: Union['Quantity', float, int],
                        bool_op: operator) -> bool:
        right_size = right

        if isinstance(right, Quantity):
            if left.instrument != right.instrument:
                raise IncompatibleInstrumentOperation(left, right)

            right_size = right.size

        if not isinstance(right_size, Number):
            raise InvalidNonNumericQuantity(right_size)

        boolean = bool_op(left.size, right_size)

        if not isinstance(boolean, bool):
            raise Exception(
                "`bool_op` cannot return a non-bool type ({}).".format(
                    boolean))

        return boolean
    def _math_operation(left: Union['Quantity', float,
                                    int], right: Union['Quantity', float, int],
                        op: operator) -> 'Quantity':
        right_size = right

        if isinstance(right, Quantity):
            if left.instrument != right.instrument:
                raise IncompatibleInstrumentOperation(left, right)

            if left.path_id and right.path_id:
                if left._path_id != right._path_id:
                    raise QuantityOpPathMismatch(left.path_id, right.path_id)

            right_size = right.size

        if not isinstance(right_size, Number):
            raise InvalidNonNumericQuantity(right_size)

        size = op(left.size, right_size)

        return Quantity(instrument=left.instrument,
                        size=size,
                        path_id=left.path_id)