-
Notifications
You must be signed in to change notification settings - Fork 0
Sourcery refactored master branch #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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}' | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def say_goodbye(self): | ||
| """Class method.""" | ||
| return 'Goodbye ' + self.name | ||
| return f'Goodbye {self.name}' | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| # 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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}' | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| def test_inheritance(): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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): | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| # Check if remainder after division is zero (which would mean that number is even). | ||
| if number % 2 == 0: | ||
| even_numbers.append(number) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
|
|
||
| # "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.', | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
||
|
Comment on lines
-20
to
-29
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
| # 2^5 = 32 | ||
| assert result == 32 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
| 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', | ||
| ] | ||
|
|
||
|
|
||
|
Comment on lines
-81
to
89
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
| # 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 | ||
|
Comment on lines
-191
to
+201
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
|
|
||
|
|
||
| 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)] | ||
|
Comment on lines
-215
to
+218
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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)] | ||
|
Comment on lines
-304
to
+306
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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 == [ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Comment on lines
-96
to
+99
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
|
|
||
| # 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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"} | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| assert isinstance(fruits_set_via_constructor, set) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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.' | ||
|
Comment on lines
26
to
-38
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
|
|
||
| # 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' | ||
|
Comment on lines
116
to
-132
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
|
|
||
| # 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' | ||
|
Comment on lines
-209
to
+194
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
|
|
||
| # 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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
test_pass_in_looprefactored with the following changes:remove-empty-nested-block)This removes the following comments ( why? ):