From d6bff5c1331864ab41d387896582f422322ca80d Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Sun, 13 Sep 2020 19:27:20 +0800 Subject: [PATCH] Renamed files and fixed Doctest (#2421) * * Renamed files * Fiexed doctest * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- .../{binary_to_decimal => binary_to_decimal.py} | 12 ++++++++---- ...ecimal_to_decimal => hexadecimal_to_decimal.py} | 14 +++++++++----- file_transfer/tests/test_send_file.py | 3 +-- graphs/depth_first_search.py | 2 +- 4 files changed, 19 insertions(+), 12 deletions(-) rename conversions/{binary_to_decimal => binary_to_decimal.py} (79%) rename conversions/{hexadecimal_to_decimal => hexadecimal_to_decimal.py} (81%) diff --git a/conversions/binary_to_decimal b/conversions/binary_to_decimal.py similarity index 79% rename from conversions/binary_to_decimal rename to conversions/binary_to_decimal.py index 1f223daf8..a7625e475 100644 --- a/conversions/binary_to_decimal +++ b/conversions/binary_to_decimal.py @@ -11,10 +11,16 @@ def bin_to_decimal(bin_string: str) -> int: >>> bin_to_decimal("0") 0 >>> bin_to_decimal("a") + Traceback (most recent call last): + ... ValueError: Non-binary value was passed to the function >>> bin_to_decimal("") - ValueError: Empty string value was passed to the function + Traceback (most recent call last): + ... + ValueError: Empty string was passed to the function >>> bin_to_decimal("39") + Traceback (most recent call last): + ... ValueError: Non-binary value was passed to the function """ bin_string = str(bin_string).strip() @@ -28,9 +34,7 @@ def bin_to_decimal(bin_string: str) -> int: decimal_number = 0 for char in bin_string: decimal_number = 2 * decimal_number + int(char) - if is_negative: - decimal_number = -decimal_number - return decimal_number + return -decimal_number if is_negative else decimal_number if __name__ == "__main__": diff --git a/conversions/hexadecimal_to_decimal b/conversions/hexadecimal_to_decimal.py similarity index 81% rename from conversions/hexadecimal_to_decimal rename to conversions/hexadecimal_to_decimal.py index e87caa0f4..beb1c2c3d 100644 --- a/conversions/hexadecimal_to_decimal +++ b/conversions/hexadecimal_to_decimal.py @@ -17,14 +17,20 @@ def hex_to_decimal(hex_string: str) -> int: >>> hex_to_decimal("-Ff") -255 >>> hex_to_decimal("F-f") + Traceback (most recent call last): + ... ValueError: Non-hexadecimal value was passed to the function >>> hex_to_decimal("") - ValueError: Empty string value was passed to the function + Traceback (most recent call last): + ... + ValueError: Empty string was passed to the function >>> hex_to_decimal("12m") + Traceback (most recent call last): + ... ValueError: Non-hexadecimal value was passed to the function """ hex_string = hex_string.strip().lower() - if not hex_string: + if not hex_string: raise ValueError("Empty string was passed to the function") is_negative = hex_string[0] == "-" if is_negative: @@ -34,9 +40,7 @@ def hex_to_decimal(hex_string: str) -> int: decimal_number = 0 for char in hex_string: decimal_number = 16 * decimal_number + hex_table[char] - if is_negative: - decimal_number = -decimal_number - return decimal_number + return -decimal_number if is_negative else decimal_number if __name__ == "__main__": diff --git a/file_transfer/tests/test_send_file.py b/file_transfer/tests/test_send_file.py index 170c2c0ae..2a6008448 100644 --- a/file_transfer/tests/test_send_file.py +++ b/file_transfer/tests/test_send_file.py @@ -1,5 +1,4 @@ -from unittest.mock import patch, Mock - +from unittest.mock import Mock, patch from file_transfer.send_file import send_file diff --git a/graphs/depth_first_search.py b/graphs/depth_first_search.py index fee9ea077..43f2eaaea 100644 --- a/graphs/depth_first_search.py +++ b/graphs/depth_first_search.py @@ -1,6 +1,6 @@ """Non recursive implementation of a DFS algorithm.""" -from typing import Set, Dict +from typing import Dict, Set def depth_first_search(graph: Dict, start: str) -> Set[int]: