import time


def in_purge_window(target, base, now=time.time(), percent=0.1):
    """Return whether the target timestamp is in the "purge window" of the base
    timestamp.  The target timestamp is within the purge window if it is less
    than ``percent`` older than base.

    :param target: Target time that is being tested for purgeability
    :param base: Base time to compare target to
    :param now: Used for calculating ages; defaults to now
    :param percent: Percentage of the age of base to use for the purge window
    :returns: Boolean signifying whether target is in the purge window

    Simple example:

    >>> in_purge_window(4,5,20)
    True
    >>> in_purge_window(4,9,20)
    False

    A more real world example:

    >>> import datetime
    >>> import calendar
    >>> def time_to_timestamp(time):
    ...   return calendar.timegm(time.utctimetuple())
    ...
    >>> now = datetime.datetime(2012, 12, 1)
    >>> base = datetime.datetime(2012, 7, 1)
    >>> target = datetime.datetime(2012, 5, 1)
    >>> for var in ['now', 'base', 'target']:
    ...     locals()[var] = time_to_timestamp(locals()[var])
    ...
    >>> in_purge_window(target, base, now)
    False
    >>> now = time_to_timestamp(datetime.datetime(2014, 12, 1))
    >>> in_purge_window(target, base, now)
    True

    """

    # This code assumes that base is older than now
    assert now > base
    return base > target > (base - percent * (now - base))