Create the Final Weekly Report Template File
In this step, you will learn how to create the weekly report template file for the final week, starting from the Monday of the week containing the adjusted end date.
- In the
create
function, add the following code after adjusting the end date:
def create(start_date, end_date):
## Create the "weekly" folder
os.makedirs("weekly", exist_ok=True)
## Find the Friday of the week containing the start date
## ... (previous steps)
## Traverse the date range starting from the Friday of the start date's week
## ... (previous steps)
## Adjust the end date to the previous Friday if it falls on a Saturday or Sunday
## ... (previous step)
## Create a submission for the week from Monday to the adjusted end date
## Find the Monday of the week containing the adjusted end date
current_date = end_date
## Construct the file name
file_name = current_date.strftime("%Y-%m-%d") + ".txt"
file_path = os.path.join("weekly", file_name)
while current_date.weekday() != 0: ## 0 represents Monday
current_date -= datetime.timedelta(days=1)
## Write file content
with open(file_path, "w") as file:
## Write the range of weekdays from Monday to the adjusted end date (excluding weekends)
date = current_date
while date <= end_date:
if date.weekday() < 5: ## 0 represents Monday, 4 represents Friday
file.write(date.strftime("%Y-%m-%d") + "\n")
date += datetime.timedelta(days=1)
The code first finds the Monday of the week containing the adjusted end date. It then constructs the file name for the final weekly report template and opens the file for writing.
Inside the file, the code writes the range of weekdays from Monday to the adjusted end date, excluding weekends.
This ensures that the final weekly report template file only includes the weekdays from the Monday of the week containing the adjusted end date to the adjusted end date itself.
With these steps above, you have completed the implementation of the create
function in the report.py
file, which generates the weekly report templates based on the provided start and end dates.
- To generate the weekly report templates, run the
report.py
script with the python report.py
command in your terminal. The script will create the weekly report templates based on the provided start and end dates.
Some examples are provided below:
## example 1
## start_date = datetime.date(2023,10,1) Sunday
## end_date = datetime.date(2023,10,16) Monday
$ ls weekly
2023-10-06.txt 2023-10-13.txt 2023-10-16.txt
$ cat 2023-10-06.txt
2023-10-02
2023-10-03
2023-10-04
2023-10-05
2023-10-06
$ cat 2023-10-16.txt
2023-10-16
## example 2
## start_date = datetime.date(2023,9,10) Sunday
## end_date = datetime.date(2023,9,24) Sunday
$ ls weekly
2023-09-15.txt 2023-09-22.txt
$ cat 2023-09-15.txt
2023-09-11
2023-09-12
2023-09-13
2023-09-14
2023-09-15
$ cat 2023-09-22.txt
2023-09-18
2023-09-19
2023-09-20
2023-09-21
2023-09-22