refactor: ♻️ refactored code as suggested in review

This commit is contained in:
Akshay B Shetty 2023-10-10 05:58:10 +00:00
parent 20f9adf4dc
commit 7f689e4f5c

View File

@ -42,7 +42,7 @@ Sources :
valid_variables: list[str] = ["v1", "v2", "p1", "p2"] valid_variables: list[str] = ["v1", "v2", "p1", "p2"]
def check_validity(values: dict[str, float]) -> bool: def check_validity(values: dict[str, float]):
""" """
Function takes dictionary as an input and returns True if the input Function takes dictionary as an input and returns True if the input
@ -51,7 +51,7 @@ def check_validity(values: dict[str, float]) -> bool:
>>> check_validity({}) >>> check_validity({})
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: Invalid input expected 3 items got 0 ValueError: Invalid input expected 3 items, got 0
>>> check_validity({'v1':2,'v2':4,'k':6}) >>> check_validity({'v1':2,'v2':4,'k':6})
Traceback (most recent call last): Traceback (most recent call last):
@ -59,18 +59,17 @@ def check_validity(values: dict[str, float]) -> bool:
ValueError: Invalid input k is not a valid variable ValueError: Invalid input k is not a valid variable
>>> check_validity({'v1':2,'v2':4,'p1':6}) >>> check_validity({'v1':2,'v2':4,'p1':6})
True
""" """
if len(values) == 3: if len(values) != 3:
msg = f"Invalid input expected {3} items, got {len(values)}"
raise ValueError(msg)
else:
for value in values: for value in values:
if value not in valid_variables: if value not in valid_variables:
msg = f"Invalid input {value} is not a valid variable" msg = f"Invalid input {value} is not a valid variable"
raise ValueError(msg) raise ValueError(msg)
return True return
else:
msg = f"Invalid input expected {3} items got {len(values)}"
raise ValueError(msg)
def find_target_variable(values: dict[str, float]) -> str: def find_target_variable(values: dict[str, float]) -> str:
@ -83,7 +82,7 @@ def find_target_variable(values: dict[str, float]) -> str:
>>> find_target_variable({}) >>> find_target_variable({})
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: Invalid input expected 3 items got 0 ValueError: Invalid input expected 3 items, got 0
>>> find_target_variable({'v1':1,'v2':2,'p2':4}) >>> find_target_variable({'v1':1,'v2':2,'p2':4})
'p1' 'p1'
@ -94,14 +93,11 @@ def find_target_variable(values: dict[str, float]) -> str:
ValueError: Invalid input k is not a valid variable ValueError: Invalid input k is not a valid variable
""" """
is_valid = check_validity(values) check_validity(values)
if is_valid:
for variable in valid_variables: for variable in valid_variables:
if variable not in values: if variable not in values:
return variable return variable
raise ValueError("Input is invalid") raise ValueError("Input is invalid")
else:
raise ValueError("Input is invalid")
def boyles_law(values: dict[str, float]) -> dict[str, str]: def boyles_law(values: dict[str, float]) -> dict[str, str]:
@ -115,12 +111,12 @@ def boyles_law(values: dict[str, float]) -> dict[str, str]:
>>> boyles_law({'p1':2,'v2':1}) >>> boyles_law({'p1':2,'v2':1})
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: Invalid input expected 3 items got 2 ValueError: Invalid input expected 3 items, got 2
>>> boyles_law({}) >>> boyles_law({})
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: Invalid input expected 3 items got 0 ValueError: Invalid input expected 3 items, got 0
>>> boyles_law({'p1':2,'v2':1, 'k':6}) >>> boyles_law({'p1':2,'v2':1, 'k':6})
Traceback (most recent call last): Traceback (most recent call last):
@ -140,8 +136,7 @@ def boyles_law(values: dict[str, float]) -> dict[str, str]:
{'v1': '31.32 L'} {'v1': '31.32 L'}
""" """
is_valid = check_validity(values) check_validity(values)
if is_valid:
target = find_target_variable(values) target = find_target_variable(values)
float_precision = ".3f" float_precision = ".3f"
if target == "p1": if target == "p1":
@ -164,8 +159,6 @@ def boyles_law(values: dict[str, float]) -> dict[str, str]:
format((values["p1"] * values["v1"]) / values["p2"], float_precision) format((values["p1"] * values["v1"]) / values["p2"], float_precision)
) )
return {"v2": f"{v2} L"} return {"v2": f"{v2} L"}
else:
raise ValueError("Input is invalid")
if __name__ == "__main__": if __name__ == "__main__":