Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/additions/test_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_pass_in_loop refactored with the following changes:

This removes the following comments ( why? ):

# It just don't do anything but for loop is still valid.


# Example above is quite useless but it was given just for illustration of the idea.
# The more useful example might be:
Expand Down
4 changes: 2 additions & 2 deletions src/classes/test_class_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_class_definition.GreetingClass.say_hello refactored with the following changes:


def say_goodbye(self):
"""Class method."""
return 'Goodbye ' + self.name
return f'Goodbye {self.name}'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_class_definition.GreetingClass.say_goodbye refactored with the following changes:


# 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
Expand Down
2 changes: 1 addition & 1 deletion src/classes/test_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Employee.get_full_id refactored with the following changes:



def test_inheritance():
Expand Down
2 changes: 1 addition & 1 deletion src/control_flow/test_continue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_continue_statement refactored with the following changes:

# Check if remainder after division is zero (which would mean that number is even).
if number % 2 == 0:
even_numbers.append(number)
Expand Down
28 changes: 8 additions & 20 deletions src/control_flow/test_for.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_for_statement refactored with the following changes:

This removes the following comments ( why? ):

# pylint: disable=consider-using-enumerate


# "cat" length is 3
# "window" length is 6
Expand All @@ -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 '

Expand Down Expand Up @@ -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.',
Expand Down
9 changes: 2 additions & 7 deletions src/control_flow/test_while.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_while_statement refactored with the following changes:

This removes the following comments ( why? ):

# Let's raise the number to certain power using while loop.

# 2^5 = 32
assert result == 32
51 changes: 24 additions & 27 deletions src/data_types/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_list_type refactored with the following changes:

This removes the following comments ( why? ):

# the append() method
# You can also add new items at the end of the list, by using
# and the cube of 7
# add the cube of 6

assert cubes == [1, 8, 27, 64, 125, 216, 343]

# Assignment to slices is also possible, and this can even change the size
Expand All @@ -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']
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_list_methods refactored with the following changes:

This removes the following comments ( why? ):

# Equivalent to a[len(a):] = [x].
# Add an item to the end of the list.
# list.append(x)

# 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)
Expand Down Expand Up @@ -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():
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_del_statement refactored with the following changes:

This removes the following comments ( why? ):

# noqa: F821



def test_list_comprehensions():
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_list_comprehensions refactored with the following changes:

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
Expand All @@ -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)]

Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_nested_list_comprehensions refactored with the following changes:

assert transposed == [
[1, 5, 9],
[2, 6, 10],
Expand All @@ -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 == [
Expand Down
14 changes: 4 additions & 10 deletions src/data_types/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_number_operators refactored with the following changes:

This removes the following comments ( why? ):

# Multiplication.
# Addition.


# Modulo operator returns the remainder of the division.
assert 12 % 3 == 0
Expand All @@ -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
2 changes: 1 addition & 1 deletion src/data_types/test_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_sets refactored with the following changes:


assert isinstance(fruits_set_via_constructor, set)

Expand Down
48 changes: 11 additions & 37 deletions src/data_types/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_string_type refactored with the following changes:

This removes the following comments ( why? ):

# \n means newline.
# Without print(), \n is included in the output.
# \ can be used to escape quotes.
# But with print(), \n produces a new line.
# Characters from position 0 (included) to 2 (excluded).
# Character from the beginning to position 2 (excluded).
# use \' to escape the single quote or use double quotes instead.
# Characters from the second-last (included) to the end.


# 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
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_string_operators refactored with the following changes:

This removes the following comments ( why? ):

# 'Py' 'thon'


# This feature is particularly useful when you want to break long strings:
text = (
'Put several strings within parentheses '
Expand All @@ -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():
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_string_formatting refactored with the following changes:

This removes the following comments ( why? ):

# To use formatted string literals, begin a string with f or F before the opening quotation
# The String format() Method
# between { and } characters that can refer to variables or literal values.
# Basic usage of the str.format() method looks like this:
# mark or triple quotation mark. Inside this string, you can write a Python expression


# 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,
Expand All @@ -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'

Expand All @@ -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
Expand Down
Loading