Skip to Content
Lab 4

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

  1. Write a Python script (e.g., fibonacci_generator.py) that defines a generator function gen_fibonacci(n) which:
    • Yields the Fibonacci sequence up to the first n numbers. (Recall: Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, …)
  2. 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

  1. Write a Python script (e.g., multiplier_closure.py) that defines a function make_multiplier(factor) which:
    • Returns a closure that takes an input x and returns x * factor.
  2. Demonstrate the closure by:
    • Creating two multiplier functions (e.g., times3 = make_multiplier(3) and times10 = make_multiplier(10)).
    • Calling these functions with sample inputs (e.g., print times3(7) and times10(7)) to show the correct outputs.

Task 3: Diary Entry Program (File I/O and OS Operations)

  1. Write a Python script (e.g., diary.py) that:
    • Checks if a file named diary.txt exists in the current directory (using the os 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 to diary.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.

Task 4: Date and Calendar Utility

  1. 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

CriteriaPointsDescription
Fibonacci Generator10 ptsThe 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 Closure10 ptsThe 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 Operations15 ptsThe 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 Utility10 ptsThe 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 Style5 ptsCode 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

Note

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.