-
-
Notifications
You must be signed in to change notification settings - Fork 42
Sheffield | 25-SDC-Nov | Sheida Shabankari | Sprint 4 |Implement Shell tools in python #256
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 14 commits
cdf1a3e
df6314b
29258e9
dad3daa
df3aa31
fc93e92
3d571eb
1766ec3
b9dc023
a35d975
cb49588
b41d36a
9b645e6
516af11
4ebe461
152590c
be70047
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 |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| node_modules | ||
| /.venv | ||
| implement-cowsay/.venv/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import argparse | ||
|
|
||
| parser=argparse.ArgumentParser(prog="cat", | ||
| usage="implement a simple cat") | ||
| parser.add_argument("-n",action="store_true",help="number the output lines") | ||
| parser.add_argument("-b",action="store_true",help="number the output lines without blank ones") | ||
| parser.add_argument("path",nargs="*",help="files to read") | ||
| args=parser.parse_args() | ||
| # print(args) | ||
| line_number=1 | ||
| for per_file in args.path : | ||
| with open(per_file,"r") as f: | ||
| if args.n or args.b : | ||
| lines=f.readlines() | ||
| for line in lines : | ||
| if line=="\n" and args.b : | ||
| print(line,end="") | ||
| else : | ||
| print(line_number,line,end="") | ||
| line_number=line_number+1 | ||
| else : | ||
| print(f.read(),end="") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import os | ||
| import argparse | ||
|
|
||
| parser=argparse.ArgumentParser(prog="cat",usage="implement a simple ls python") | ||
| parser.add_argument("-1",dest="one",action="store_true") | ||
| parser.add_argument("-a",action="store_true") | ||
| parser.add_argument("path",nargs="*",help="path to list files") | ||
| args=parser.parse_args() | ||
|
|
||
| paths=args.path | ||
| if len(paths)==0 : paths=["."] | ||
|
|
||
| for path in paths : | ||
| files_list=os.listdir(path) | ||
| files_list.sort(key=str.lower) | ||
| for item in files_list : | ||
| if args.one and args.a : | ||
| print(item) | ||
| elif args.one : | ||
| if not item.startswith(".") : | ||
| print(item) | ||
|
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. Did you test all the combinations of arguments?
Author
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. Fix -a output order to match ls: non-hidden files first, hidden files last, with '.' and '..' at the top. Previously I tested all combinations and thought showing all files/folders was enough, but now I fixed the order to fully match ls behavior. 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. If you test this without the -1 option, can you see what happens. What would you expect to happen? |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import argparse | ||
| import os | ||
|
|
||
| parser=argparse.ArgumentParser(prog="wc",usage="implement a simple wc in python") | ||
| parser.add_argument("-l",action="store_true",help="count of lines") | ||
| parser.add_argument("-w",action="store_true",help="count of words") | ||
| parser.add_argument("-c",action="store_true",help="count of bytes") | ||
| parser.add_argument("path",nargs="+") | ||
| args=parser.parse_args() | ||
|
|
||
|
|
||
| paths=args.path | ||
| words_count=0 | ||
| total_lines=0 | ||
| total_words=0 | ||
| total_bytes=0 | ||
|
|
||
| for file in paths : | ||
| with open(file,"r") as f : | ||
| bytes_count=os.path.getsize(file) | ||
| lines=f.readlines() | ||
| lines_count=len(lines) | ||
| for line in lines : | ||
| words_count+=len(line.split()) | ||
| total_lines +=lines_count | ||
| total_bytes += bytes_count | ||
| total_words +=words_count | ||
| if not (args.l or args.w or args.c): | ||
| args.l = True | ||
| args.w = True | ||
| args.c = True | ||
| print((f"{lines_count:<5}" if args.l else "")+ | ||
| (f"{words_count:<5}" if args.w else "")+ | ||
| (f"{bytes_count:<5}" if args.c else "")+ | ||
| f"{file:<20}") | ||
| words_count=0 | ||
|
|
||
| if len(paths)>1 : | ||
| print((f"{total_lines:<5}" if args.l else "")+ | ||
| (f"{total_words:<5}" if args.w else "")+ | ||
| (f"{total_bytes:<5}" if args.c else "")+ | ||
LonMcGregor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "total") | ||
Uh oh!
There was an error while loading. Please reload this page.