Skip to content

Learning Python: Day 38

Running a test $ pytest ========================test session starts=================== ========== platform darwin — Python 3.x.x, pytest-8 .x.x, pluggy-1.x.x rootdir: /…/python_work/chapter_11 collected 1 item . test_name_function.py . ======================== 1 passed in 0.00s =================== ========== A failing test name_function.py def get_formatted_name(first, middle, last):… Read More »Learning Python: Day 38

Learning Python: Day 37

Unit tests and test cases A passing test test_name_function.py from name_function import get_formatted_name . def test_first_last_name(): ….”””Do names like ‘Hanis Joplin’ work?””” formatted_name = get_formatted_name(‘janis’, ‘joplin’) assert formatted_name == ‘Janis Joplin’ End of study session.

Learning Python: Day 36

Chapter 11 – Testing Your Code Installing pytest with pip Updating pip $ python -m pip install –upgrade pip Requirement already satisfied: pip in /…/python3.11/site-packages (22.0.4) –snip– Successfully installed pip-22.1.2 $ python -m pip install –upgrade package_name Installing pytest $… Read More »Learning Python: Day 36

Learning Python: Day 35

Refactoring remember_me.py from pathlib import Path import json . def greet_user(): ….”””Greet the user by name.””” ….path = Path(‘username.json’) ….if path.exists(): ……..contents = path.read_text() ……..username – json.loads(contents) ……..print(f”Welcome back, {username}!”) ….else: ……..username = input(“What is your name? “) ……..contents =… Read More »Learning Python: Day 35

Learning Python: Day 34

Storing data Using json.dumps() and json.loads() number_writer.py from pathlib import Path import json . numbers = [2, 3, 5, 7, 11, 13] . path = Path(‘numbers.json’) contents = json.dumps(numbers) path.write_text(contents) [2, 3, 5, 7, 11, 13] number_read.py from pathlib import… Read More »Learning Python: Day 34

Learning Python: Day 33

Analyzing text from pathlib import Path path = Path(‘alice.txt’) try: ….contents = path.read_text(encoding=’utf-8′) except FileNotFoundError: ….print(f”Sorry, the file {path} does not exist.”) else: ….# Count the approximate number of words in the file: words = contents.split() num_words = len(words) print(f”The… Read More »Learning Python: Day 33

Learning Python: Day 32

Large files: One Million Digits pi_string.py from pathlib import Path path = Path(‘pi_million_digits.txt’) contents = path.read_text() lines = contents.splitlines() pi_string = “ for line in lines: ….pi_string += line.lstrip() print(f”{pi_string[:52]}…”) print(len(pi_string)) 3.14…02 Is your birthday contained in Pi pi_birthday.py –snip–… Read More »Learning Python: Day 32

Learning Python: Day 31

Relative and absolute file paths path = Path(‘text_files/filename.txt’) path = Path(‘/home/eric/data_files/text_files/filename.txt’) Accessing a file’s lines file_reader.py from pathlib import Path path = Path(‘pi_digits.txt’) contents = path.read_text() lines = contents.splitlines() for fine in lines: ….print(line) 3.1415926535 8979323846 2643383279 Working with a… Read More »Learning Python: Day 31

Learning Python: Day 30

Chapter 10 – Files and Exceptions Reading from a file Reading the contents of a file pi_digits.txt 3.1415926535 8979323846 2643383279 file_reader.py from pathlib import Path path = Path(‘pi_digits.txt’) contents = path.read_text() print(contents) 3.1415926535 8979323846 2643383279 from pathlib import Path path… Read More »Learning Python: Day 30

Learning Python: Day 29

Defining attributes and methods for the child class class Car: ….–snip– class ElectricCar(Car): ….”””Represent aspects of a car, specific to electric vehicles.””” ….def __init__(self, make, model, year): ……..””” ……..Initialize attributes of the parent class. ……..Then initialize attributes specific to an… Read More »Learning Python: Day 29