No, you are doing fine here, but you can just return the result of the >
comparison, it already gives you a boolean value. There is also no need to turn the time.time()
float to an integer here:
def is_allowed(time_rated, duration):
durations = {'day': 86400, 'week': 604800}
if duration in durations and isinstance(time_rated, int):
time_difference = time.time() - time_rated
return time_rated > durations[duration]
return False
You can simplify this further by defaulting to a duration
of float('inf')
, saving you an if
test:
def is_allowed(time_rated, duration):
if not isinstance(time_rated, int):
return False
duration = {'day': 86400, 'week': 604800}.get(duration, float('inf'))
return time.time() - time_rated > duration
This returns False
if:
time_rated
is not an integerduration
is not equal to'day'
or'week'
(because no number is ever greater thanfloat('inf')
).time_rated
is less than 86400 or 604800 seconds ago, depending onduration
.