Generators, Closures, and Utility Modules
Overview and Objectives
In this lab you will explore several advanced Python features including generators, closures, file I/O, and system/date utilities. You will:
- Create a generator function to yield Fibonacci numbers.
- Implement a closure for customized multiplication.
- Develop a diary entry program that uses file I/O and the
os
module to manage a text file. - Build a date and calendar utility that displays a month’s calendar and indicates today’s date.
By the end of this lab, you will be comfortable using generators, closures, file operations, and working with modules such as os
, datetime
, and calendar
.
Instructions and Tasks
Task 1: Fibonacci Generator
- Write a Python script (e.g.,
fibonacci_generator.py
) that defines a generator functiongen_fibonacci(n)
which:- Yields the Fibonacci sequence up to the first
n
numbers. (Recall: Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, …)
- Yields the Fibonacci sequence up to the first
- Write test code that iterates over the generator (e.g., using a for-loop) and prints each Fibonacci number with a clear label.
Task 2: Multiplier Closure
- Write a Python script (e.g.,
multiplier_closure.py
) that defines a functionmake_multiplier(factor)
which:- Returns a closure that takes an input
x
and returnsx * factor
.
- Returns a closure that takes an input
- Demonstrate the closure by:
- Creating two multiplier functions (e.g.,
times3 = make_multiplier(3)
andtimes10 = make_multiplier(10)
). - Calling these functions with sample inputs (e.g., print
times3(7)
andtimes10(7)
) to show the correct outputs.
- Creating two multiplier functions (e.g.,
Task 3: Diary Entry Program (File I/O and OS Operations)
- Write a Python script (e.g.,
diary.py
) that:- Checks if a file named
diary.txt
exists in the current directory (using theos
module). - If not, creates a new file called
diary.txt
. - Prompts the user to input a diary entry.
- Precedes the entry with a timestamp of the current date and time (using
datetime.datetime.now()
), and appends it todiary.txt
. - After appending, reads and prints the entire content of
diary.txt
so the user can see all past entries. - Uses try/except blocks to catch and report errors during file operations.
- Checks if a file named
Task 4: Date and Calendar Utility
- Write a Python script (e.g.,
calendar_utility.py
) that:- Prompts the user for a year and a month (e.g., 2025 and 3 for March 2025).
- Uses the
calendar
module to display the calendar for that month in a neatly formatted grid. - Uses the
datetime
module to determine today’s date. - If today’s date falls in the specified month and year, prints a message indicating so (e.g., “Today is March 4, 2025”) or highlights the day in the output.
- Clearly labels all outputs for readability.
Submission
Submit the following files via a link to a single GitHub repository containing all the files.:
fibonacci_generator.py
multiplier_closure.py
diary.py
calendar_utility.py
Include sample outputs (screenshots or logs) demonstrating that each script runs correctly and meets the task requirements.
Grading Rubric
Criteria | Points | Description |
---|---|---|
Fibonacci Generator | 10 pts | The generator function correctly yields Fibonacci numbers up to the specified count n . The test loop prints the sequence with clear, accurate labels. Partial credit for minor sequence or labeling issues. |
Multiplier Closure | 10 pts | The make_multiplier function returns a closure that retains the factor. Demonstrated examples (e.g., times3 and times10) correctly produce the expected multiplication results. Partial credit for minor errors. |
Diary File I/O & OS Operations | 15 pts | The diary program checks for file existence, creates diary.txt if necessary, appends a timestamped entry, and reads back the file content. Proper use of the os and datetime modules is evident. Partial credit for minor issues in file handling or formatting. |
Calendar Display Utility | 10 pts | The utility correctly uses the calendar module to display the specified month and uses the datetime module to identify today’s date. Output is neatly formatted and clearly indicates if today’s date falls within the displayed month. Partial credit for formatting or incomplete indication. |
Overall Code Robustness and Style | 5 pts | Code is well-organized, uses meaningful variable and function names, includes appropriate comments, and handles exceptions gracefully. Deduct points for disorganized or poorly commented code. |
Total Points: 50 points
Partial credit will be awarded for submissions that demonstrate a solid understanding of the concepts, even if not all elements are fully implemented or if there are minor errors. For example, if the Fibonacci generator yields most—but not all—values correctly, or if the diary program successfully appends entries but has minor formatting issues, evaluators will award partial points proportional to the correct work submitted. Points may be deducted for incomplete error handling, formatting issues, or missing components, but credit will be given where the core logic and approach are sound.