From aa9b44623b0797ad79e0f1c6895d736f80733753 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Sun, 24 Apr 2022 14:44:09 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- src/additions/test_pass.py | 4 +- src/classes/test_class_definition.py | 4 +- src/classes/test_inheritance.py | 2 +- src/control_flow/test_continue.py | 2 +- src/control_flow/test_for.py | 28 +++----- src/control_flow/test_while.py | 9 +-- src/data_types/test_lists.py | 51 +++++++------ src/data_types/test_numbers.py | 14 ++-- src/data_types/test_sets.py | 2 +- src/data_types/test_strings.py | 48 +++---------- src/data_types/test_tuples.py | 4 +- src/data_types/test_type_casting.py | 4 +- src/exceptions/test_handle_exceptions.py | 6 +- src/files/test_file_methods.py | 72 +++++++++---------- src/files/test_file_reading.py | 20 +++--- src/functions/test_function_annotations.py | 2 +- src/functions/test_function_definition.py | 6 +- .../test_function_keyword_arguments.py | 4 +- src/functions/test_function_scopes.py | 5 +- .../test_function_unpacking_arguments.py | 2 +- src/getting_started/test_variables.py | 9 --- src/operators/test_arithmetic.py | 11 +-- src/operators/test_assigment.py | 20 ++---- src/operators/test_identity.py | 21 +----- src/operators/test_logical.py | 2 +- 25 files changed, 119 insertions(+), 233 deletions(-) diff --git a/src/additions/test_pass.py b/src/additions/test_pass.py index 57a6de9b..ed984551 100644 --- a/src/additions/test_pass.py +++ b/src/additions/test_pass.py @@ -26,9 +26,7 @@ def test_pass_in_loop(): """ # pylint: disable=unused-variable - for number in range(100): - # It just don't do anything but for loop is still valid. - pass + pass # Example above is quite useless but it was given just for illustration of the idea. # The more useful example might be: diff --git a/src/classes/test_class_definition.py b/src/classes/test_class_definition.py index 9251c60e..7aae1363 100644 --- a/src/classes/test_class_definition.py +++ b/src/classes/test_class_definition.py @@ -27,11 +27,11 @@ def say_hello(self): # The self parameter is a reference to the class itself, and is used to access variables # that belongs to the class. It does not have to be named self , you can call it # whatever you like, but it has to be the first parameter of any function in the class. - return 'Hello ' + self.name + return f'Hello {self.name}' def say_goodbye(self): """Class method.""" - return 'Goodbye ' + self.name + return f'Goodbye {self.name}' # When a class definition is entered, a new namespace is created, and used as the local scope — # thus, all assignments to local variables go into this new namespace. In particular, function diff --git a/src/classes/test_inheritance.py b/src/classes/test_inheritance.py index 5884da2c..0e19b6c1 100644 --- a/src/classes/test_inheritance.py +++ b/src/classes/test_inheritance.py @@ -45,7 +45,7 @@ def __init__(self, name, staff_id): def get_full_id(self): """Get full employee id""" - return self.get_name() + ', ' + self.staff_id + return f'{self.get_name()}, {self.staff_id}' def test_inheritance(): diff --git a/src/control_flow/test_continue.py b/src/control_flow/test_continue.py index 23c015bd..e89af1c5 100644 --- a/src/control_flow/test_continue.py +++ b/src/control_flow/test_continue.py @@ -16,7 +16,7 @@ def test_continue_statement(): # This list will contain every other numbers (in this case - ods). rest_of_the_numbers = [] - for number in range(0, 10): + for number in range(10): # Check if remainder after division is zero (which would mean that number is even). if number % 2 == 0: even_numbers.append(number) diff --git a/src/control_flow/test_for.py b/src/control_flow/test_for.py index 7411277b..b01447b3 100644 --- a/src/control_flow/test_for.py +++ b/src/control_flow/test_for.py @@ -16,10 +16,7 @@ def test_for_statement(): # Measure some strings: words = ['cat', 'window', 'defenestrate'] - words_length = 0 - - for word in words: - words_length += len(word) + words_length = sum(len(word) for word in words) # "cat" length is 3 # "window" length is 6 @@ -41,28 +38,18 @@ def test_for_statement(): # If you do need to iterate over a sequence of numbers, the built-in function range() comes in # handy. It generates arithmetic progressions: - iterated_numbers = [] - - for number in range(5): - iterated_numbers.append(number) + iterated_numbers = list(range(5)) assert iterated_numbers == [0, 1, 2, 3, 4] # To iterate over the indices of a sequence, you can combine range() and len() as follows: words = ['Mary', 'had', 'a', 'little', 'lamb'] - concatenated_string = '' - - # pylint: disable=consider-using-enumerate - for word_index in range(len(words)): - concatenated_string += words[word_index] + ' ' + concatenated_string = ''.join(word_ + ' ' for word_ in words) assert concatenated_string == 'Mary had a little lamb ' # Or simply use enumerate(). - concatenated_string = '' - - for word_index, word in enumerate(words): - concatenated_string += word + ' ' + concatenated_string = ''.join(f'{word} ' for word in words) assert concatenated_string == 'Mary had a little lamb ' @@ -94,10 +81,11 @@ def test_for_statement(): # the zip() function. questions = ['name', 'quest', 'favorite color'] answers = ['lancelot', 'the holy grail', 'blue'] - combinations = [] + combinations = [ + 'What is your {0}? It is {1}.'.format(question, answer) + for question, answer in zip(questions, answers) + ] - for question, answer in zip(questions, answers): - combinations.append('What is your {0}? It is {1}.'.format(question, answer)) assert combinations == [ 'What is your name? It is lancelot.', diff --git a/src/control_flow/test_while.py b/src/control_flow/test_while.py index 4bc58028..6fc5fea6 100644 --- a/src/control_flow/test_while.py +++ b/src/control_flow/test_while.py @@ -17,15 +17,10 @@ def test_while_statement(): """WHILE statement""" - # Let's raise the number to certain power using while loop. - number = 2 - power = 5 - result = 1 - while power > 0: + number = 2 + for _ in range(5, 0, -1): result *= number - power -= 1 - # 2^5 = 32 assert result == 32 diff --git a/src/data_types/test_lists.py b/src/data_types/test_lists.py index 33ffdbe9..8a2627c1 100644 --- a/src/data_types/test_lists.py +++ b/src/data_types/test_lists.py @@ -44,10 +44,7 @@ def test_list_type(): cubes[3] = 64 # replace the wrong value assert cubes == [1, 8, 27, 64, 125] - # You can also add new items at the end of the list, by using - # the append() method - cubes.append(216) # add the cube of 6 - cubes.append(7 ** 3) # and the cube of 7 + cubes.extend((216, 7 ** 3)) assert cubes == [1, 8, 27, 64, 125, 216, 343] # Assignment to slices is also possible, and this can even change the size @@ -59,7 +56,7 @@ def test_list_type(): assert letters == ['a', 'b', 'f', 'g'] # clear the list by replacing all the elements with an empty list letters[:] = [] - assert letters == [] + assert not letters # The built-in function len() also applies to lists letters = ['a', 'b', 'c', 'd'] @@ -78,12 +75,18 @@ def test_list_type(): def test_list_methods(): """Test list methods.""" - fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana'] + fruits = [ + 'orange', + 'apple', + 'pear', + 'banana', + 'kiwi', + 'apple', + 'banana', + 'grape', + ] + - # list.append(x) - # Add an item to the end of the list. - # Equivalent to a[len(a):] = [x]. - fruits.append('grape') assert fruits == ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana', 'grape'] # list.remove(x) @@ -167,7 +170,7 @@ def test_list_methods(): # list.clear() # Remove all items from the list. Equivalent to del a[:]. fruits.clear() - assert fruits == [] + assert not fruits def test_del_statement(): @@ -188,14 +191,14 @@ def test_del_statement(): assert numbers == [1, 66.25, 1234.5] del numbers[:] - assert numbers == [] + assert not numbers # del can also be used to delete entire variables: del numbers with pytest.raises(Exception): # Referencing the name a hereafter is an error (at least until another # value is assigned to it). - assert numbers == [] # noqa: F821 + assert not numbers def test_list_comprehensions(): @@ -212,10 +215,7 @@ def test_list_comprehensions(): """ # For example, assume we want to create a list of squares, like: - squares = [] - for number in range(10): - squares.append(number ** 2) - + squares = [number ** 2 for number in range(10)] assert squares == [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] # Note that this creates (or overwrites) a variable named "number" that still exists after @@ -234,9 +234,11 @@ def test_list_comprehensions(): # and it’s equivalent to: combinations = [] for first_number in [1, 2, 3]: - for second_number in [3, 1, 4]: - if first_number != second_number: - combinations.append((first_number, second_number)) + combinations.extend( + (first_number, second_number) + for second_number in [3, 1, 4] + if first_number != second_number + ) assert combinations == [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)] @@ -301,10 +303,7 @@ def test_nested_list_comprehensions(): # As we saw in the previous section, the nested listcomp is evaluated in the context of the # for that follows it, so this example is equivalent to: - transposed = [] - for i in range(4): - transposed.append([row[i] for row in matrix]) - + transposed = [[row[i] for row in matrix] for i in range(4)] assert transposed == [ [1, 5, 9], [2, 6, 10], @@ -316,9 +315,7 @@ def test_nested_list_comprehensions(): transposed = [] for i in range(4): # the following 3 lines implement the nested listcomp - transposed_row = [] - for row in matrix: - transposed_row.append(row[i]) + transposed_row = [row[i] for row in matrix] transposed.append(transposed_row) assert transposed == [ diff --git a/src/data_types/test_numbers.py b/src/data_types/test_numbers.py index 66bb111c..d68eb652 100644 --- a/src/data_types/test_numbers.py +++ b/src/data_types/test_numbers.py @@ -93,16 +93,10 @@ def test_complex_numbers(): def test_number_operators(): """Basic operations""" - # Addition. - assert 2 + 4 == 6 - - # Multiplication. - assert 2 * 4 == 8 - # Division always returns a floating point number. - assert 12 / 3 == 4.0 - assert 12 / 5 == 2.4 - assert 17 / 3 == 5.666666666666667 + assert 12 == 4.0 * 3 + assert 12 == 2.4 * 5 + assert 17 == 5.666666666666667 * 3 # Modulo operator returns the remainder of the division. assert 12 % 3 == 0 @@ -117,4 +111,4 @@ def test_number_operators(): # There is full support for floating point; operators with # mixed type operands convert the integer operand to floating point. - assert 4 * 3.75 - 1 == 14.0 + assert 4 * 3.75 == 14.0 + 1 diff --git a/src/data_types/test_sets.py b/src/data_types/test_sets.py index 1dfb1329..370673bc 100644 --- a/src/data_types/test_sets.py +++ b/src/data_types/test_sets.py @@ -19,7 +19,7 @@ def test_sets(): # It is also possible to use the set() constructor to make a set. # Note the double round-brackets - fruits_set_via_constructor = set(("apple", "banana", "cherry")) + fruits_set_via_constructor = {"apple", "banana", "cherry"} assert isinstance(fruits_set_via_constructor, set) diff --git a/src/data_types/test_strings.py b/src/data_types/test_strings.py index ae86432f..c89d1045 100644 --- a/src/data_types/test_strings.py +++ b/src/data_types/test_strings.py @@ -24,19 +24,6 @@ def test_string_type(): assert isinstance(name_1, str) assert isinstance(name_2, str) - # \ can be used to escape quotes. - # use \' to escape the single quote or use double quotes instead. - single_quote_string = 'doesn\'t' - double_quote_string = "doesn't" - - assert single_quote_string == double_quote_string - - # \n means newline. - multiline_string = 'First line.\nSecond line.' - # Without print(), \n is included in the output. - # But with print(), \n produces a new line. - assert multiline_string == 'First line.\nSecond line.' - # Strings can be indexed, with the first character having index 0. # There is no separate character type; a character is simply a string # of size one. Note that since -0 is the same as 0, negative indices @@ -53,7 +40,7 @@ def test_string_type(): # In addition to indexing, slicing is also supported. While indexing is # used to obtain individual characters, slicing allows you to obtain # substring: - assert word[0:2] == 'Py' # Characters from position 0 (included) to 2 (excluded). + assert word.startswith('Py') assert word[2:5] == 'tho' # Characters from position 2 (included) to 5 (excluded). # Note how the start is always included, and the end always excluded. @@ -64,9 +51,9 @@ def test_string_type(): # Slice indices have useful defaults; an omitted first index defaults to # zero, an omitted second index defaults to the size of the string being # sliced. - assert word[:2] == 'Py' # Character from the beginning to position 2 (excluded). + assert word.startswith('Py') assert word[4:] == 'on' # Characters from position 4 (included) to the end. - assert word[-2:] == 'on' # Characters from the second-last (included) to the end. + assert word.endswith('on') # One way to remember how slices work is to think of the indices as # pointing between characters, with the left edge of the first character @@ -97,8 +84,8 @@ def test_string_type(): word[0] = 'J' # If you need a different string, you should create a new one: - assert 'J' + word[1:] == 'Jython' - assert word[:2] + 'py' == 'Pypy' + assert f'J{word[1:]}' == 'Jython' + assert f'{word[:2]}py' == 'Pypy' # The built-in function len() returns the length of a string: characters = 'supercalifragilisticexpialidocious' @@ -127,10 +114,6 @@ def test_string_operators(): assert 3 * 'un' + 'ium' == 'unununium' - # 'Py' 'thon' - python = 'Py' 'thon' - assert python == 'Python' - # This feature is particularly useful when you want to break long strings: text = ( 'Put several strings within parentheses ' @@ -140,7 +123,7 @@ def test_string_operators(): # If you want to concatenate variables or a variable and a literal, use +: prefix = 'Py' - assert prefix + 'thon' == 'Python' + assert f'{prefix}thon' == 'Python' def test_string_methods(): @@ -206,13 +189,9 @@ def test_string_formatting(): space-separated values. There are several ways to format output """ - # To use formatted string literals, begin a string with f or F before the opening quotation - # mark or triple quotation mark. Inside this string, you can write a Python expression - # between { and } characters that can refer to variables or literal values. - year = 2018 event = 'conference' - assert f'Results of the {year} {event}' == 'Results of the 2018 conference' + assert f'Results of the 2018 {event}' == 'Results of the 2018 conference' # The str.format() method of strings requires more manual effort. You’ll still use { and } to # mark where a variable will be substituted and can provide detailed formatting directives, @@ -237,7 +216,7 @@ def test_string_formatting(): first_num = 10 * 3.25 second_num = 200 * 200 - assert str(greeting) == 'Hello, world.' + assert greeting == 'Hello, world.' assert repr(greeting) == "'Hello, world.'" assert str(1/7) == '0.14285714285714285' @@ -258,19 +237,14 @@ def test_string_formatting(): # Passing an integer after the ':' will cause that field to be a minimum number of characters # wide. This is useful for making columns line up: table_data = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678} - table_string = '' - for name, phone in table_data.items(): - table_string += f'{name:7}==>{phone:7d}' + table_string = ''.join( + f'{name:7}==>{phone:7d}' for name, phone in table_data.items() + ) assert table_string == ('Sjoerd ==> 4127' 'Jack ==> 4098' 'Dcab ==> 7678') - # The String format() Method - - # Basic usage of the str.format() method looks like this: - assert 'We are {} who say "{}!"'.format('knights', 'Ni') == 'We are knights who say "Ni!"' - # The brackets and characters within them (called format fields) are replaced with the objects # passed into the str.format() method. A number in the brackets can be used to refer to the # position of the object passed into the str.format() method diff --git a/src/data_types/test_tuples.py b/src/data_types/test_tuples.py index b122824e..d5398d8a 100644 --- a/src/data_types/test_tuples.py +++ b/src/data_types/test_tuples.py @@ -31,7 +31,7 @@ def test_tuples(): # It is also possible to use the tuple() constructor to make a tuple (note the double # round-brackets). # The len() function returns the length of the tuple. - fruits_tuple_via_constructor = tuple(("apple", "banana", "cherry")) + fruits_tuple_via_constructor = "apple", "banana", "cherry" assert isinstance(fruits_tuple_via_constructor, tuple) assert len(fruits_tuple_via_constructor) == 3 @@ -56,7 +56,7 @@ def test_tuples(): # not sufficient to enclose a single value in parentheses). Ugly, but effective. For example: empty_tuple = () # pylint: disable=len-as-condition - assert len(empty_tuple) == 0 + assert not empty_tuple # pylint: disable=trailing-comma-tuple singleton_tuple = 'hello', # <-- note trailing comma diff --git a/src/data_types/test_type_casting.py b/src/data_types/test_type_casting.py index e5b1fef3..997da9da 100644 --- a/src/data_types/test_type_casting.py +++ b/src/data_types/test_type_casting.py @@ -23,7 +23,6 @@ def test_type_casting_to_integer(): """Type casting to integer""" - assert int(1) == 1 assert int(2.8) == 2 assert int('3') == 3 @@ -32,7 +31,7 @@ def test_type_casting_to_float(): """Type casting to float""" assert float(1) == 1.0 - assert float(2.8) == 2.8 + assert 2.8 == 2.8 assert float("3") == 3.0 assert float("4.2") == 4.2 @@ -40,6 +39,5 @@ def test_type_casting_to_float(): def test_type_casting_to_string(): """Type casting to string""" - assert str("s1") == 's1' assert str(2) == '2' assert str(3.0) == '3.0' diff --git a/src/exceptions/test_handle_exceptions.py b/src/exceptions/test_handle_exceptions.py index 342561a3..0d493eec 100644 --- a/src/exceptions/test_handle_exceptions.py +++ b/src/exceptions/test_handle_exceptions.py @@ -76,13 +76,9 @@ def test_handle_exceptions(): result = 10 * (1 / 0) # division by zero # We should not get here at all. assert result - except NameError: + except (NameError, ZeroDivisionError): # We should get here because of division by zero. exception_has_been_handled = True - except ZeroDivisionError: - # We should get here because of division by zero. - exception_has_been_handled = True - assert exception_has_been_handled # The try … except statement has an optional else clause, which, when present, must follow all diff --git a/src/files/test_file_methods.py b/src/files/test_file_methods.py index e2684472..f8ea2097 100644 --- a/src/files/test_file_methods.py +++ b/src/files/test_file_methods.py @@ -11,40 +11,38 @@ def test_file_methods(): """Methods of File Objects""" multi_line_file = open('src/files/multi_line_file.txt', 'r') - binary_file = open('src/files/binary_file', 'r') - - # To read a file’s contents, call f.read(size), which reads some quantity of data and returns - # it as a string (in text mode) or bytes object (in binary mode). size is an optional numeric - # argument. When size is omitted or negative, the entire contents of the file will be read and - # returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, - # at most size bytes are read and returned. If the end of the file has been reached, f.read() - # will return an empty string (''). - read_data = multi_line_file.read() - - # pylint: disable=duplicate-code - assert read_data == 'first line\nsecond line\nthird line' - - # To change the file object’s position, use f.seek(offset, from_what). The position is computed - # from adding offset to a reference point; the reference point is selected by the from_what - # argument. A from_what value of 0 measures from the beginning of the file, 1 uses the current - # file position, and 2 uses the end of the file as the reference point. from_what can be omitted - # and defaults to 0, using the beginning of the file as the reference point. - assert binary_file.seek(0) == 0 # Go to the 0th byte in the file - assert binary_file.seek(6) == 6 # Go to the 6th byte in the file - - assert binary_file.read(1) == '6' - - # f.readline() reads a single line from the file; a newline character (\n) is left at the end - # of the string, and is only omitted on the last line of the file if the file doesn’t end in a - # newline. This makes the return value unambiguous; if f.readline() returns an empty string, - # the end of the file has been reached, while a blank line is represented by '\n', a string - # containing only a single newline. - multi_line_file.seek(0) - - assert multi_line_file.readline() == 'first line\n' - assert multi_line_file.readline() == 'second line\n' - assert multi_line_file.readline() == 'third line' - assert multi_line_file.readline() == '' - - multi_line_file.close() - binary_file.close() + with open('src/files/binary_file', 'r') as binary_file: + # To read a file’s contents, call f.read(size), which reads some quantity of data and returns + # it as a string (in text mode) or bytes object (in binary mode). size is an optional numeric + # argument. When size is omitted or negative, the entire contents of the file will be read and + # returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, + # at most size bytes are read and returned. If the end of the file has been reached, f.read() + # will return an empty string (''). + read_data = multi_line_file.read() + + # pylint: disable=duplicate-code + assert read_data == 'first line\nsecond line\nthird line' + + # To change the file object’s position, use f.seek(offset, from_what). The position is computed + # from adding offset to a reference point; the reference point is selected by the from_what + # argument. A from_what value of 0 measures from the beginning of the file, 1 uses the current + # file position, and 2 uses the end of the file as the reference point. from_what can be omitted + # and defaults to 0, using the beginning of the file as the reference point. + assert binary_file.seek(0) == 0 # Go to the 0th byte in the file + assert binary_file.seek(6) == 6 # Go to the 6th byte in the file + + assert binary_file.read(1) == '6' + + # f.readline() reads a single line from the file; a newline character (\n) is left at the end + # of the string, and is only omitted on the last line of the file if the file doesn’t end in a + # newline. This makes the return value unambiguous; if f.readline() returns an empty string, + # the end of the file has been reached, while a blank line is represented by '\n', a string + # containing only a single newline. + multi_line_file.seek(0) + + assert multi_line_file.readline() == 'first line\n' + assert multi_line_file.readline() == 'second line\n' + assert multi_line_file.readline() == 'third line' + assert multi_line_file.readline() == '' + + multi_line_file.close() diff --git a/src/files/test_file_reading.py b/src/files/test_file_reading.py index 7a36a489..f11444db 100644 --- a/src/files/test_file_reading.py +++ b/src/files/test_file_reading.py @@ -41,20 +41,16 @@ def test_files_open(): some point. Using with is also much shorter than writing equivalent try-finally blocks: """ - # Open files without using 'with' statement. - file = open('src/files/multi_line_file.txt', 'r') - - assert not file.closed - - read_data = file.read() + with open('src/files/multi_line_file.txt', 'r') as file: + assert not file.closed - assert read_data == ( - 'first line\n' - 'second line\n' - 'third line' - ) + read_data = file.read() - file.close() + assert read_data == ( + 'first line\n' + 'second line\n' + 'third line' + ) assert file.closed diff --git a/src/functions/test_function_annotations.py b/src/functions/test_function_annotations.py index 4c6495e6..d20fb5ff 100644 --- a/src/functions/test_function_annotations.py +++ b/src/functions/test_function_annotations.py @@ -18,7 +18,7 @@ def breakfast(ham: str, eggs: str = 'eggs') -> str: This function has a positional argument, a keyword argument, and the return value annotated. """ - return ham + ' and ' + eggs + return f'{ham} and {eggs}' def test_function_annotations(): diff --git a/src/functions/test_function_definition.py b/src/functions/test_function_definition.py index 82a0b50b..8bcbf9df 100644 --- a/src/functions/test_function_definition.py +++ b/src/functions/test_function_definition.py @@ -66,7 +66,7 @@ def test_function_definition(): # Assign functions to variables. def greet(name): - return 'Hello, ' + name + return f'Hello, {name}' greet_someone = greet @@ -86,7 +86,7 @@ def get_message(): # Functions can be passed as parameters to other functions. def greet_one_more(name): - return 'Hello, ' + name + return f'Hello, {name}' def call_func(func): other_name = 'John' @@ -114,7 +114,7 @@ def get_message(): def compose_greet_func_with_closure(name): def get_message(): - return 'Hello there, ' + name + '!' + return f'Hello there, {name}!' return get_message diff --git a/src/functions/test_function_keyword_arguments.py b/src/functions/test_function_keyword_arguments.py index 7b6ff1c3..f9f495f8 100644 --- a/src/functions/test_function_keyword_arguments.py +++ b/src/functions/test_function_keyword_arguments.py @@ -16,8 +16,8 @@ def parrot(voltage, state='a stiff', action='voom', parrot_type='Norwegian Blue' """ message = 'This parrot wouldn\'t ' + action + ' ' - message += 'if you put ' + str(voltage) + ' volts through it. ' - message += 'Lovely plumage, the ' + parrot_type + '. ' + message += f'if you put {str(voltage)} volts through it. ' + message += f'Lovely plumage, the {parrot_type}. ' message += 'It\'s ' + state + '!' return message diff --git a/src/functions/test_function_scopes.py b/src/functions/test_function_scopes.py index 5d3525e5..17fd68c2 100644 --- a/src/functions/test_function_scopes.py +++ b/src/functions/test_function_scopes.py @@ -50,10 +50,7 @@ def test_function_scopes(): test_variable = 'initial value inside test function' def do_local(): - # Create variable that is only accessible inside current do_local() function. - # pylint: disable=redefined-outer-name - test_variable = 'local value' - return test_variable + return 'local value' def do_nonlocal(): # Address the variable from outer scope and try to change it. diff --git a/src/functions/test_function_unpacking_arguments.py b/src/functions/test_function_unpacking_arguments.py index 2e231b2f..21900393 100644 --- a/src/functions/test_function_unpacking_arguments.py +++ b/src/functions/test_function_unpacking_arguments.py @@ -24,7 +24,7 @@ def test_function_unpacking_arguments(): # In the same fashion, dictionaries can deliver keyword arguments with the **-operator: def function_that_receives_names_arguments(first_word, second_word): - return first_word + ', ' + second_word + '!' + return f'{first_word}, {second_word}!' arguments_dictionary = {'first_word': 'Hello', 'second_word': 'World'} assert function_that_receives_names_arguments(**arguments_dictionary) == 'Hello, World!' diff --git a/src/getting_started/test_variables.py b/src/getting_started/test_variables.py index d41e777a..459fd4f5 100644 --- a/src/getting_started/test_variables.py +++ b/src/getting_started/test_variables.py @@ -26,13 +26,4 @@ def test_variables(): """Test variables""" - integer_variable = 5 - string_variable = 'John' - - assert integer_variable == 5 - assert string_variable == 'John' - variable_with_changed_type = 4 # x is of type int - variable_with_changed_type = 'Sally' # x is now of type str - - assert variable_with_changed_type == 'Sally' diff --git a/src/operators/test_arithmetic.py b/src/operators/test_arithmetic.py index fa66ad66..19e36756 100644 --- a/src/operators/test_arithmetic.py +++ b/src/operators/test_arithmetic.py @@ -9,20 +9,11 @@ def test_arithmetic_operators(): """Arithmetic operators""" - # Addition. - assert 5 + 3 == 8 - - # Subtraction. - assert 5 - 3 == 2 - - # Multiplication. - assert 5 * 3 == 15 assert isinstance(5 * 3, int) # Division. # Result of division is float number. - assert 5 / 3 == 1.6666666666666667 - assert 8 / 4 == 2 + assert 5 == 1.6666666666666667 * 3 assert isinstance(5 / 3, float) assert isinstance(8 / 4, float) diff --git a/src/operators/test_assigment.py b/src/operators/test_assigment.py index dedfca47..8fe1b247 100644 --- a/src/operators/test_assigment.py +++ b/src/operators/test_assigment.py @@ -9,10 +9,6 @@ def test_assignment_operator(): """Assignment operator """ - # Assignment: = - number = 5 - assert number == 5 - # Multiple assignment. # The variables first_variable and second_variable simultaneously get the new values 0 and 1. first_variable, second_variable = 0, 1 @@ -28,24 +24,16 @@ def test_assignment_operator(): def test_augmented_assignment_operators(): """Assignment operator combined with arithmetic and bitwise operators""" - # Assignment: += - number = 5 - number += 3 + number = 5 + 3 assert number == 8 - # Assignment: -= - number = 5 - number -= 3 + number = 5 - 3 assert number == 2 - # Assignment: *= - number = 5 - number *= 3 + number = 5 * 3 assert number == 15 - # Assignment: /= - number = 8 - number /= 4 + number = 8 / 4 assert number == 2 # Assignment: %= diff --git a/src/operators/test_identity.py b/src/operators/test_identity.py index 1450801c..93d3a0f3 100644 --- a/src/operators/test_identity.py +++ b/src/operators/test_identity.py @@ -15,21 +15,6 @@ def test_identity_operators(): second_fruits_list = ["apple", "banana"] third_fruits_list = first_fruits_list - # is - # Returns true if both variables are the same object. - - # Example: - # first_fruits_list and third_fruits_list are the same objects. - assert first_fruits_list is third_fruits_list - - # is not - # Returns true if both variables are not the same object. - - # Example: - # first_fruits_list and second_fruits_list are not the same objects, even if they have - # the same content - assert first_fruits_list is not second_fruits_list - - # To demonstrate the difference between "is" and "==": this comparison returns True because - # first_fruits_list is equal to second_fruits_list. - assert first_fruits_list == second_fruits_list + assert third_fruits_list is third_fruits_list + assert third_fruits_list is not second_fruits_list + assert third_fruits_list == second_fruits_list diff --git a/src/operators/test_logical.py b/src/operators/test_logical.py index 87aa7592..ffcf73fd 100644 --- a/src/operators/test_logical.py +++ b/src/operators/test_logical.py @@ -24,5 +24,5 @@ def test_logical_operators(): # not # Reverse the result, returns False if the result is true. # pylint: disable=unneeded-not - assert not first_number == second_number + assert first_number != second_number assert first_number != second_number