update the throtting_duration_parser.py returning tuple instead of dict

This commit is contained in:
Pravin Kamble 2023-08-21 21:41:01 +05:30
parent e4a8a1e08b
commit f62927c1df
2 changed files with 7 additions and 8 deletions

View File

@ -103,7 +103,7 @@ class SimpleRateThrottle(BaseThrottle):
if rate is None: if rate is None:
return (None, None) return (None, None)
num, period = rate.split('/') num, period = rate.split('/')
quantity, unit = parse_quantity_and_unit(period).values() quantity, unit = parse_quantity_and_unit(period)
num_requests = int(num) num_requests = int(num)
duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[unit[0]] duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[unit[0]]
total_duration = duration * int(quantity) total_duration = duration * int(quantity)

View File

@ -7,16 +7,15 @@ def parse_quantity_and_unit(quantity_unit_string):
Returns: Returns:
dict: A dictionary containing the parsed quantity and unit, with keys 'quantity' and 'unit'. dict: A dictionary containing the parsed quantity and unit, with keys 'quantity' and 'unit'.
If the input string contains only a unit (e.g., "ms"), quantity will be set to 1. If the input string contains only a unit (e.g., "m"), quantity will be set to 1.
""" """
i = 0 i = 0
quantity_unit_dict = {}
while i < len(quantity_unit_string) and quantity_unit_string[i].isnumeric(): while i < len(quantity_unit_string) and quantity_unit_string[i].isnumeric():
i += 1 i += 1
if i == 0: if i == 0:
quantity_unit_dict['quantity'] = 1 return (1, quantity_unit_string)
quantity_unit_dict['unit'] = quantity_unit_string
else: else:
quantity_unit_dict['quantity'] = int(quantity_unit_string[:i]) quantity = int(quantity_unit_string[:i])
quantity_unit_dict['unit'] = quantity_unit_string[i:] unit = quantity_unit_string[i:]
return quantity_unit_dict return (quantity, unit)