-
-
Notifications
You must be signed in to change notification settings - Fork 42
London | 25-SDC-Nov | Aida Eslamimoghadam | Sprint 4 | Implement Shell tools in python #270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
DaryaShirokova
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great progress on this! Left a few comments here
implement-shell-tools/cat/cat.py
Outdated
| description="Simple cat clone with -n and -b options", | ||
| ) | ||
|
|
||
| counterNumber = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In python the usual code style is to use snake_case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing that out. I’ll update the variable.
implement-shell-tools/cat/cat.py
Outdated
| print(counterNumber, arrText[i]) | ||
| counterNumber += 1 | ||
| else: | ||
| print(content) No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please wrap you logic into a method and run it using if __name__ == "__main__": approach? Ideally the program should not contain global vars, and having methods improves readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion. I’ve moved the logic into functions, added a main() entry point using if name == "main": and removed global variables to improve readability.
implement-shell-tools/cat/cat.py
Outdated
| ) | ||
|
|
||
| counterNumber = 1 | ||
| parser.add_argument("-n", action="store_true", help="number all lines") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cat -n sample-files/1.txt, cat.py sample-files/ *.txt (including -n option) and cat -b sample-files/3.txt all show slightly different result from yours.
implement-shell-tools/cat/cat.py
Outdated
| for file_path in args.path: | ||
| with open(file_path, "r") as f: | ||
| content = f.read() | ||
| arrText = content.split("\n") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a method in python to read file into an array of lines directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the suggestion. I’ll use the built-in method to read the file into a list of lines directly.
implement-shell-tools/cat/cat.py
Outdated
| arrText = content.split("\n") | ||
|
|
||
| if args.b: | ||
| for i in range(len(arrText )): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can potentially use enumerate here.
| parser.add_argument("path", nargs="?", default=".", help="The directory to list") | ||
| args = parser.parse_args() | ||
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, formatting could be improved with ruff here or other formatters :)
implement-shell-tools/ls/ls.py
Outdated
| ) | ||
|
|
||
| parser.add_argument("-a", action="store_true", help="include hidden files") | ||
| parser.add_argument("-1", dest="one" ,action="store_true", help="use a long listing format") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean by long listing format here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Many thanks for your time and help.
I’ll update the help text to reflect that.
implement-shell-tools/ls/ls.py
Outdated
| import argparse | ||
| import os | ||
|
|
||
| def show_unhidden_files(listDir): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can these two functions be merged into one using extra method param?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion.
I merged them into a single function with an extra parameter.
implement-shell-tools/wc/wc.py
Outdated
| parser.add_argument("path", nargs="+", default=".", help="The file to count") | ||
| args = parser.parse_args() | ||
|
|
||
| if not args.l and not args.w and not args.c: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rework this code to have less duplication, by using methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your feedback, I’ve refactored the code to reduce duplication.
| @@ -0,0 +1,66 @@ | |||
| import argparse | |||
| parser = argparse.ArgumentParser( | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please enasure your output is the same as the one suggested for examples in the readme (it is ok if whitespaces are different in your output):
It must act the same as `wc` would, if run from the directory containing this README.md file, for the following command lines:
* `wc sample-files/*`
* `wc -l sample-files/3.txt`
* `wc -w sample-files/3.txt`
* `wc -c sample-files/3.txt`
* `wc -l sample-files/*`
This PR includes my solutions to simulate several shell tools in Python.
I used the argparse module to re-implement them.