For this lab, I was able to contribute to a teammate's project. The task at hand was to add a --recent
/ -r
flag to their project. When running the program with this flag set, it will only include files that have been modified within the last 7 days.
I once again partnered up with DenisC96 and filed an issue asking the implement the new feature.
I got to forking a copy of their repo and cloning it to my machine. After testing the program to make sure it was working as intended, I created a branch to work on my proposed changes.
Actually implementing the feature was pretty simple and only required a few lines of code. First I added the argument to the parser, then I wrote a small function called check_recent_changes
. This function took in a file's absolute path and the --recent argument to determine the amount of days, and used the time
library to determine the current time. From here I looked into ways of finding the last modified time of a file and found that using the Path
library that was already being utilized in this project, I could call .stat().st_mtime to get an object containing the metadata of a file, specifically the last modified time in Unix timestamp format. So after determining the cutoff, the function will return true if the files time is greater or equal to the cutoff, and false otherwise.
With that checker function done, it was a matter of just inserting it into the author's code where the files were being written. Here, I put in an if statement that called the function, if true the file would be written as normal.
One issue I immediately found while testing was that the program crashed when the -r
flag wasn't used. I fixed this by having the check-recent-changes
function return true if there the days argument was None
.
Finally I made some minor updates to the README file of the project to reflect the addition of the new optional flag.
Once I was happy with the implementation and it passed all of my testing, I filed a pull request detailing the changes I had made.
It was fun to work on another person's project and get a taste for what it is like to contribute to open source projects. I definitely took care to follow the original author's code conventions and even utilized the libraries that they were using (I also did my project in Python but was not using Path
, so I endeavored to find a solution using that).
Top comments (0)