London | SDC-Nov-2025 | Ikenna Agulobi | Sprint 4 | Implement shell tools python#301
London | SDC-Nov-2025 | Ikenna Agulobi | Sprint 4 | Implement shell tools python#301ike-agu wants to merge 8 commits intoCodeYourFuture:mainfrom
Conversation
|
Your PR's title isn't in the expected format. Please check the expected title format, and update yours to match. Reason: Sprint part (Sprint-4) doesn't match expected format (example: 'Sprint 2', without quotes) If this PR is not coursework, please add the NotCoursework label (and message on Slack in #cyf-curriculum or it will probably not be noticed). If this PR needs reviewed, please add the 'Needs Review' label to this PR after you have resolved the issues listed above. |
|
Your PR's title isn't in the expected format. Please check the expected title format, and update yours to match. Reason: Sprint part (Sprint-4) doesn't match expected format (example: 'Sprint 2', without quotes) If this PR is not coursework, please add the NotCoursework label (and message on Slack in #cyf-curriculum or it will probably not be noticed). If this PR needs reviewed, please add the 'Needs Review' label to this PR after you have resolved the issues listed above. |
DaryaShirokova
left a comment
There was a problem hiding this comment.
Looks good! Just added a few comments
| python cat.py sample-files/*.txt | ||
|
|
||
| * Number every line across multiple files(including empty ones) | ||
| python cat.py -n sample-files/*.txt |
There was a problem hiding this comment.
There is a small discrepancy in how you program handles some cases it seems:
$ cat -n sample-files/*.txt
1 Once upon a time...
2 There was a house made of gingerbread.
3 It looked delicious.
4 I was tempted to take a bite of it.
5 But this seemed like a bad idea...
6
7 There's more to come, though...
$ python cat.py -n sample-files/*.txt
1 Once upon a time...
2
3 There was a house made of gingerbread.
4
5 It looked delicious.
6 I was tempted to take a bite of it.
7 But this seemed like a bad idea...
8
9 There's more to come, though...
10
There was a problem hiding this comment.
Thanks for pointing out the discrepancy. I’ve fixed the line-numbering logic so empty lines are handled correctly, matching cat behaviour. Thank you.
implement-shell-tools/cat/cat.py
Outdated
| @@ -0,0 +1,50 @@ | |||
| import argparse | |||
| # ------------------------------------------- | |||
There was a problem hiding this comment.
It's ok to have comments, but it would be even better to have the code split into methods with descriptive names :)
There was a problem hiding this comment.
Thanks for your review and suggestions. I’ve refactored my code by splitting it into small functions with descriptive names.
implement-shell-tools/cat/cat.py
Outdated
| content = f.read() | ||
|
|
||
| lines = content.split("\n") |
There was a problem hiding this comment.
Maybe check if you could use readlines method here.
| return args.paths | ||
|
|
||
| # list a single directory | ||
| def list_directory(directory_path, show_hidden, file_per_line): |
There was a problem hiding this comment.
It is good as is, but as a suggestion - a tiny bit cleaner approach would be to first get the list of directories/files and for list_directory function to just accept this list and print it (filtering whatever is not needed). This would follow 1 method/1 responsibility paradigm (which helps with code readability / reusability).
implement-shell-tools/wc/wc.py
Outdated
|
|
||
|
|
||
| def main(): | ||
| # -------------------------------------------------------- |
There was a problem hiding this comment.
Again, maybe try splitting into functions instead of comment blocks instead :)
There was a problem hiding this comment.
I've refactored my code and removed comments. Thanks for feedback.
| # -------------------------------------------------------- | ||
| # Print totals if there are multiple files | ||
| # -------------------------------------------------------- | ||
| if len(args.paths) > 1: |
There was a problem hiding this comment.
Maybe there is a way to reuse the same functions (with different args) for printing individual lines and totals
| python wc.py sample-files/* | ||
|
|
||
| * Just lines | ||
| python wc.py -l sample-files/3.txt |
There was a problem hiding this comment.
Looks like some outputs are different?
wc sample-files/*
1 4 20 sample-files/1.txt
1 7 39 sample-files/2.txt
5 24 125 sample-files/3.txt
7 35 184 total
$ python wc.py sample-files/*
2 4 20 sample-files/1.txt
2 7 39 sample-files/2.txt
6 24 125 sample-files/3.txt
10 35 184 total
There was a problem hiding this comment.
Hi @DaryaShirokova - thanks for pointing this out. The issue was caused because I used split("\n"), which overcounts when a file ends with a newline. I've now updated my implementation to use content.count("\n"), and the output now matches wc as expected.
|
Hi @DaryaShirokova , apologies for circling back to this PR after such a long time. I should have addressed the feedback earlier. |
implement-shell-tools/wc/wc.py
Outdated
| if len(args.paths) > 1: | ||
| no_flags = not args.l and not args.w and not args.c | ||
|
|
||
| if no_flags: |
There was a problem hiding this comment.
I am not sure this is the correct logic here? e.g.
wc -l sample-files/*
1 sample-files/1.txt
1 sample-files/2.txt
5 sample-files/3.txt
7 total
it still prints total with -l?
There was a problem hiding this comment.
@DaryaShirokova, Thanks again for reviewing my PR. I’ve updated the logic so the total line now matches real wc behaviour. Please take a look when you have a moment. Thank you!
DaryaShirokova
left a comment
There was a problem hiding this comment.
LGTM overall, just one thing is still slightly different
| import re | ||
|
|
||
|
|
||
| def main(): |
There was a problem hiding this comment.
Optional: you could try to organize the code into multiple methods for readability
Print totals when multiple files are provided, even if -l, -w, or -c flags are used.This matches wc real behaviour.
Learners, PR Template
Self checklist
Changelist
This PR contains my implementation of cat, ls and wc shell tool commands in python