Skip to content

fixed small issues in python scripts. #97

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion src/control_flow/test_break.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def test_break_statement():
"""BREAK statement"""
"""test how BREAK statements work"""

# Let's terminate the loop in case if we've found the number we need in a range from 0 to 100.
number_to_be_found = 42
Expand Down
2 changes: 1 addition & 1 deletion src/control_flow/test_for.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# pylint: disable=too-many-locals
def test_for_statement():
"""FOR statement"""
"""test how FOR statement work"""

# Measure some strings:
words = ['cat', 'window', 'defenestrate']
Expand Down
6 changes: 3 additions & 3 deletions src/control_flow/test_try.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_try():

try:
# pylint: disable=undefined-variable
print(not_existing_variable)
print(not_existing_variable) # type: ignore
except NameError:
exception_has_been_caught = True

Expand All @@ -33,7 +33,7 @@ def test_try():

try:
# pylint: disable=undefined-variable
print(not_existing_variable)
print(not_existing_variable) # type: ignore
except NameError:
exception_message = 'Variable is not defined'

Expand All @@ -57,7 +57,7 @@ def test_try():
message = ''
try:
# pylint: undefined-variable
print(not_existing_variable) # noqa: F821
print(not_existing_variable) # type: ignore # noqa: F821
except NameError:
message += 'Something went wrong.'
finally:
Expand Down
59 changes: 24 additions & 35 deletions src/files/test_file_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,30 @@
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() == ''
# Using `with` ensures files are properly closed after their suite finishes execution
with open('src/files/multi_line_file.txt', 'r') as multi_line_file:
with open('src/files/binary_file', 'rb') as binary_file: # Open binary file in binary mode ('rb')

# Reading the entire content of the multi_line_file
read_data = multi_line_file.read()
# Assert to check the file's content
assert read_data == 'first line\nsecond line\nthird line'

# Changing file position in binary_file using seek
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

# Reading one byte and decoding to match comparison (since file is opened in binary mode)
assert binary_file.read(1).decode('utf-8') == '6'

# Resetting position of multi_line_file to beginning for readline operations
multi_line_file.seek(0)

# Reading lines one by one
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() == '' # Ensure end of file returns an empty string

multi_line_file.close()
binary_file.close()
3 changes: 2 additions & 1 deletion src/standard_libraries/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ def test_json():
# To encode a data structure to JSON, use the "dumps" method. This method takes an object and
# returns a String:
encoded_person_string = json.dumps(person_dictionary)

assert json.loads(encoded_person_string) == json.loads(json_string)

assert encoded_person_string == json_string
8 changes: 4 additions & 4 deletions src/standard_libraries/test_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import re


def test_re():
"""String Pattern Matching"""
def test_regex_and_string_operations():

"""Tests regular expressions for pattern matching, substitution, and string replacements."""

assert re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest') == [
'foot',
Expand All @@ -20,6 +21,5 @@ def test_re():

assert re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat') == 'cat in the hat'

# When only simple capabilities are needed, string methods are preferred because they are
# easier to read and debug:
# When only simple capabilities are needed, string methods are preferred because they are easier to read and debug:
assert 'tea for too'.replace('too', 'two') == 'tea for two'
11 changes: 6 additions & 5 deletions src/standard_libraries/test_zlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@


def test_zlib():
"""zlib."""
"""Tests compression, decompression, and CRC32 checksum using zlib."""
string = b'witch which has which witches wrist watch'
assert len(string) == 41

zlib_compressed_string = zlib.compress(string)
assert len(zlib_compressed_string) == 37

zlib_decompressed_string = zlib.decompress(zlib_compressed_string)
assert zlib_decompressed_string == b'witch which has which witches wrist watch'
assert len(zlib_compressed_string) < len(string)

original_data = b'witch which has which witches wrist watch'
compressed_data = zlib.compress(original_data)
decompressed_data = zlib.decompress(compressed_data)

assert zlib.crc32(string) == 226805979
10 changes: 5 additions & 5 deletions src/user_input/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@


def user_input():
"""Input prompt"""
""" prompt the user to enter their name and greets them"""

# Printing statement to signal the user that we are waiting for input.
user_input = input("Please type in your name\n")
# Prompt the user to enter their name
user_name = input("Please type in your name\n")

# Printing a message based on the input.
print(f"Welcome, {user_input}!")
# Printing a greeting message
print(f"Welcome, {user_name}!")