Skip to content

Pricing

Module to calculate pricing of divvy trips.

AdditiveRate

Bases: Rate

A combination of two classes with addition.

Source code in lyft_bikes/pricing.py
class AdditiveRate(Rate):
    """A combination of two classes with addition."""

    def __init__(self, left, right):
        self.left = left
        self.right = right

    def __call__(self, duration: np.ndarray) -> np.ndarray:
        return self.left(duration=duration) + self.right(duration=duration)

    def __repr__(self) -> str:
        return f"AdditiveRate(left={self.left}, right={self.right})"

MinuteRate dataclass

Bases: Rate

Amount in cents per minute starting at start minute.

Parameters:

Name Type Description Default
amount int

the cost in cents per minute

required
start int

the minute where that cost starts

required
Example

Rides that cost 15 cents per minute after 30 minutes of riding.

rate = MinuteRate(amount=15, start=30)
rate([10, 15, 31]) # [0, 0, 16]
Source code in lyft_bikes/pricing.py
@dataclass
class MinuteRate(Rate):
    """Amount in cents per minute starting at `start` minute.

    Args:
        amount: the cost in cents per minute
        start: the minute where that cost starts

    Example:
        Rides that cost 15 cents per minute after 30 minutes of riding.

        ```python
        rate = MinuteRate(amount=15, start=30)
        rate([10, 15, 31]) # [0, 0, 16]
        ```

    """

    amount: int
    start: int

    def __call__(self, duration: np.ndarray) -> np.ndarray:
        return np.maximum(np.rint(duration) - self.start, 0) * self.amount

Rate

All pricing will derive from this class.

Source code in lyft_bikes/pricing.py
class Rate:
    """All pricing will derive from this class."""

    def __call__(self, duration: np.ndarray) -> np.ndarray:
        """Calculate the rate for a given set of durations. Vectorized."""

    def __add__(self, other):
        return AdditiveRate(self, other)

__call__(duration)

Calculate the rate for a given set of durations. Vectorized.

Source code in lyft_bikes/pricing.py
def __call__(self, duration: np.ndarray) -> np.ndarray:
    """Calculate the rate for a given set of durations. Vectorized."""

UnlockFee dataclass

Bases: Rate

Amount in cents to unlock the bike.

Source code in lyft_bikes/pricing.py
@dataclass
class UnlockFee(Rate):
    """Amount in cents to unlock the bike."""

    amount: int

    def __call__(self, duration: np.ndarray) -> np.ndarray:
        return np.ones_like(duration) * self.amount