-
-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import argparse | ||
| parser = argparse.ArgumentParser( | ||
| prog="my-cat", | ||
| description="Simple cat clone with -n and -b options", | ||
| ) | ||
|
|
||
| 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 commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| parser.add_argument("-b", action="store_true", help="The character to search for" ) | ||
| parser.add_argument("path", nargs="+", help="The file to search") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| 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 commentThe 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. |
||
|
|
||
| if args.b: | ||
| for i in range(len(arrText )): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can potentially use |
||
| if arrText[i].strip() != "": | ||
| print(counterNumber,arrText[i]) | ||
| counterNumber += 1 | ||
|
|
||
| elif args.n: | ||
| for i in range(len(arrText )): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can format your code with tools like ruff. |
||
| print(counterNumber, arrText[i]) | ||
| counterNumber += 1 | ||
| else: | ||
| print(content) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import argparse | ||
| import os | ||
|
|
||
| def show_unhidden_files(listDir): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can these two functions be merged into one using extra method param? |
||
| for file in listDir: | ||
| if not file.startswith('.'): | ||
| print(file) | ||
| def show_all_files(listDir): | ||
| for file in listDir: | ||
| print(file) | ||
|
|
||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="my-ls", | ||
| description="Simple ls clone with -a and -l options", | ||
| ) | ||
|
|
||
| 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 commentThe reason will be displayed to describe this comment to others. Learn more. Looks like the order of file for ls and your command is slightly different, is it possible to make them appear in the same order? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by long listing format 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 commentThe reason will be displayed to describe this comment to others. Learn more. Again, formatting could be improved with ruff here or other formatters :) |
||
|
|
||
|
|
||
|
|
||
| fn = args.path | ||
| listDir = os.listdir(fn) | ||
| if fn!="": | ||
| if args.a and args.one: | ||
| show_all_files(listDir) | ||
| elif args.a: | ||
| show_all_files(listDir) | ||
| elif args.one: | ||
| show_unhidden_files(listDir) | ||
|
|
||
| else: | ||
| show_unhidden_files(listDir) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import argparse | ||
| parser = argparse.ArgumentParser( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): |
||
| prog="my-wc", | ||
| description="Simple wc clone with -l and -w options", | ||
| ) | ||
| lineCounter = 0 | ||
| wordCounter = 0 | ||
| charCounter = 0 | ||
| parser.add_argument("-l", action="store_true", help="count lines") | ||
| parser.add_argument("-w", action="store_true", help="count words") | ||
| parser.add_argument("-c", action="store_true", help="count characters") | ||
| 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 commentThe reason will be displayed to describe this comment to others. Learn more. Can you rework this code to have less duplication, by using methods? |
||
| for file_path in args.path: | ||
| with open(file_path, "r") as f: | ||
| content = f.read() | ||
| arrText = content.split("\n") | ||
| lineCounter += len(arrText) | ||
| arrWords = content.split() | ||
| wordCounter += len(arrWords) | ||
| charCounter += len(content) | ||
| print("Line count:", lineCounter,"lines") | ||
| print("Word count:", wordCounter,"words") | ||
| print("Character count:", charCounter,"characters") | ||
| elif args.l and args.w: | ||
| for file_path in args.path: | ||
| with open(file_path, "r") as f: | ||
| content = f.read() | ||
| arrText = content.split("\n") | ||
| lineCounter += len(arrText) | ||
| arrWords = content.split() | ||
| wordCounter += len(arrWords) | ||
| print("Line count:", lineCounter,"lines") | ||
| print("Word count:", wordCounter,"words") | ||
| elif args.l and args.c: | ||
| for file_path in args.path: | ||
| with open(file_path, "r") as f: | ||
| content = f.read() | ||
| arrText = content.split("\n") | ||
| lineCounter += len(arrText) | ||
| charCounter += len(content) | ||
| print("Line count:", lineCounter,"lines") | ||
| print("Character count:", charCounter,"characters") | ||
| elif args.l: | ||
| for file_path in args.path: | ||
| with open(file_path, "r") as f: | ||
| content = f.read() | ||
| arrText = content.split("\n") | ||
| lineCounter += len(arrText) | ||
| print("Line count:", lineCounter,"lines") | ||
|
|
||
| elif args.w: | ||
| for file_path in args.path: | ||
| with open(file_path, "r") as f: | ||
| content = f.read() | ||
| arrText = content.split() | ||
| wordCounter += len(arrText) | ||
| print("Word count:", wordCounter,"words") | ||
| elif args.c: | ||
| for file_path in args.path: | ||
| with open(file_path, "r") as f: | ||
| content = f.read() | ||
| charCounter += len(content) | ||
| print("Character count:", charCounter,"characters") | ||
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.