-
-
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
| 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.
| 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.
| ) | ||
|
|
||
| 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.
| 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.
| 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 :)
| ) | ||
|
|
||
| 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?
| 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?
| 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?
| @@ -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.