-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_case.py
More file actions
154 lines (110 loc) · 4.32 KB
/
app_case.py
File metadata and controls
154 lines (110 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""app_case.py - Project script (example).
Author: Denise Case
Date: 2026-01
Practice key Python skills related to:
- imports
- logging
- variables
- type hints
- global constants
- f-strings
- functions
- main function
- conditional execution guard
OBS:
Don't edit this file - it should remain a working example.
"""
# === DECLARE IMPORTS (BRING IN FREE CODE) ===
import logging
import statistics
from typing import Final
from datafun_toolkit.logger import get_logger, log_header
# === CONFIGURE LOGGER ONCE PER MODULE (PYTHON FILE) ===
LOG: logging.Logger = get_logger("P01", level="INFO")
# === DECLARE GLOBAL CONSTANTS ===
# All these global variables are CONSTANT, they do NOT change when the program runs.
# By convention, constants are named in UPPERCASE_WITH_UNDERSCORES.
# The following illustrates variables that hold these common types:
# str, int, float, bool, list of strings.
# `Final` is added to indicate these variables should not be reassigned.
COURSE_NAME: Final[str] = "Data Analytics Fundamentals"
COURSE_NUMBER: Final[int] = 608
COURSE_HOURS_PER_WEEK: Final[float] = 20.0
ASSUMES_PRIOR_EXPERIENCE: Final[bool] = False
USES_PROFESSIONAL_PYTHON: Final[bool] = True
HELPFUL_TRAITS: Final[list[str]] = [
"patience",
"curiosity",
"humor",
"tenacity",
]
# === DECLARE A FUNCTION TO FORMAT THE INFORMATION ===
def get_summary() -> str:
"""Get a formatted summary of the information held in the global variables.
Arguments: None (nothing is passed in the parentheses after `get_summary`).
Returns: - a formatted multi-line string (starts with f and wrapped in triple quotes).
"""
summary: str = f"""
Course Information:
Course name: {COURSE_NAME}
Course number: {COURSE_NUMBER}
Course hrs/wk: {COURSE_HOURS_PER_WEEK:.2f}
Assumes prior experience: {ASSUMES_PRIOR_EXPERIENCE}
Uses Professional Python: {USES_PROFESSIONAL_PYTHON}
Helpful traits: {HELPFUL_TRAITS}
"""
LOG.info("Generated formatted multi-line SUMMARY string.")
LOG.info("Returning the str to the calling function.")
return summary
# === DECLARE A FUNCTION TO FORMAT DESCRIPTIVE STATISTICS ===
def get_statistics() -> str:
"""Get a formatted summary showing descriptive statistics.
Arguments: None (nothing is passed in the parentheses).
Returns: - a formatted multi-line string.
"""
# Initialize sample data - snowfall measurements in inches.
snowfall_inches: list[float] = [2.5, 3.5, 4.5, 5.5, 6.5]
# Calculate descriptive statistics.
total: float = sum(snowfall_inches)
count: int = len(snowfall_inches)
minimum: float = min(snowfall_inches) if count > 0 else 0.0
maximum: float = max(snowfall_inches) if count > 0 else 0.0
average: float = statistics.mean(snowfall_inches) if count > 0 else 0.0
std_dev: float = statistics.stdev(snowfall_inches) if count > 1 else 0.0
# Build a formatted multi-line string using f and triple quotes.
summary: str = f"""
Descriptive Statistics for Snowfall (inches):
Total snowfall: {total:.2f} inches
Count of measurements: {count}
Minimum snowfall: {minimum:.2f} inches
Maximum snowfall: {maximum:.2f} inches
Average snowfall: {average:.2f} inches
Standard deviation: {std_dev:.2f} inches
"""
LOG.info("Generated formatted multi-line SUMMARY string.")
LOG.info("Returning the str to the calling function.")
return summary
# === DEFINE THE MAIN FUNCTION THAT CALLS OTHER FUNCTIONS ===
def main() -> None:
"""Entry point when running this file as a Python script.
Arguments: None.
Returns: None.
"""
# Log a header for the application.
LOG.info("=================")
log_header(LOG, "Foundations of Professional Python")
LOG.info("=================")
# Log start of main processing.
LOG.info("START main()..................")
# Call functions to get formatted strings and log them.
summary: str = get_summary()
LOG.info(summary)
stats: str = get_statistics()
LOG.info(stats)
# Log end of main processing.
LOG.info("END main()..................")
# === CONDITIONAL EXECUTION GUARD ===
# WHY: If running this file as a script, then call main() function.
# This is standard Python "boilerplate".
if __name__ == "__main__":
main()