-
-
Notifications
You must be signed in to change notification settings - Fork 42
Sheffield | 25-SDC-Nov | Hassan Osman | Sprint 4 | Implement shell tools (cat, ls, wc) in Python #285
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?
Sheffield | 25-SDC-Nov | Hassan Osman | Sprint 4 | Implement shell tools (cat, ls, wc) in Python #285
Changes from 55 commits
4675246
95cfc32
530a234
2fc8e5d
73369ea
3bb2ce9
652c027
1f95e09
9de5129
42b98a5
f1444f9
4da7430
414ebd7
a0daa76
cd5256b
b1bf214
d0bb387
80e3909
3ce06c4
e93d1f9
f1b5a23
f3e7e68
1cbdfd4
719bf23
2987f4b
5b493c9
6c8a567
83e99f3
1d1ac0a
9a40bd6
1e36528
01ae017
7ee60a5
0a68b74
82424f0
a082839
4aad5ab
914cfda
c70d7d9
d996f5b
72c9e8e
dc375e6
c4d2796
d4cd590
76a8647
731c907
baadd85
312abeb
0b90dbc
0bd31b0
a8f7a8d
8a65064
a6642bb
6a5281e
683fc0c
b95cf8e
e3a7ce3
82a6800
e9f2a54
9f5649f
14fca64
140467f
088df21
4b2801f
6551026
b1b1669
5158913
92929e0
85622de
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,38 @@ | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="display-file-content", | ||
| description="Output the content of a file to the terminal", | ||
| ) | ||
|
|
||
| parser.add_argument("-n", help="Number the output lines", action="store_true") | ||
| parser.add_argument("-b", help="Number the non-blank output lines", action="store_true") | ||
| parser.add_argument("paths", help="The file(s)/path(s) to read from", nargs="+") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| line_number = 1 | ||
|
|
||
| for path in args.paths: | ||
| with open(path, "r") as f: | ||
| for line in f: | ||
| line = line.rstrip("\n") | ||
| if args.n: | ||
| print(f"{line_number:>6} {line}") | ||
| line_number += 1 | ||
| elif args.b: | ||
| if line != "": | ||
| print(f"{line_number} {line}") | ||
| line_number += 1 | ||
| else: | ||
| print(f"{line}") | ||
| else: | ||
| print(f"{line}") | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import argparse | ||
| import os | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="list-files-in-directory", | ||
| description="List all files and directories in a directory", | ||
| ) | ||
|
|
||
| parser.add_argument("-1", "--one", dest="one", help="Output one entry per line", action="store_true") | ||
| parser.add_argument("-a", help="List all files & directories, including hidden ones", action="store_true") | ||
| parser.add_argument("paths", nargs="*", default=["."], help="The file path to read from") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| for path in args.paths: | ||
| if os.path.isdir(path): | ||
| items = os.listdir(path) | ||
|
|
||
| if args.a: | ||
| items = ['.', '..'] + items | ||
| else: | ||
| items = [item for item in items if not item.startswith(".")] | ||
|
|
||
| for item in items: | ||
| if args.one: | ||
| print(item) | ||
| else: | ||
| print(item, end=" ") | ||
|
||
| if not args.one: | ||
| print() | ||
| else: | ||
| print(path) | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| # import { program } from "commander"; | ||
|
||
| # import { promises as fs } from "node:fs"; | ||
| # import process from "node:process"; | ||
| # import { stat } from "node:fs/promises"; | ||
|
|
||
| # program | ||
| # .name("count-containing-lines-words-characters") | ||
| # .description("Counts lines, words or characters in a file (or all files) inside a directory") | ||
| # .option("-l, --line", "The number of lines in each file") | ||
| # .option("-w, --word", "The number of words in each file") | ||
| # .option("-c, --character", "The number of characters in each file") | ||
| # .argument("<path...>", "The file path to process"); | ||
|
|
||
| # program.parse(); | ||
|
|
||
| # const argv = program.args; | ||
|
|
||
| # const options = program.opts(); | ||
|
|
||
|
|
||
| # function counter(item) { | ||
| # const lines = item.trim().split("\n").length; | ||
| # const words = item.split(/\s+/).filter(Boolean).length; | ||
| # const characters = item.length; | ||
| # return { lines, words, characters }; | ||
| # } | ||
|
|
||
| # let totalLines = 0; | ||
| # let totalWords = 0; | ||
| # let totalCharacters = 0; | ||
| # let fileCount = 0; | ||
|
|
||
| # for (const path of argv) { | ||
| # const pathInfo = await stat(path); | ||
|
|
||
| # if (pathInfo.isFile()) { | ||
| # const content = await fs.readFile(path, "utf-8"); | ||
| # const stats = counter(content); | ||
| # if (options.line) { | ||
| # console.log(`${stats.lines} ${path}`); | ||
| # } else if (options.word) { | ||
| # console.log(`${stats.words} ${path}`); | ||
| # } else if (options.character) { | ||
| # console.log(`${stats.characters} ${path}`); | ||
| # } else { | ||
| # console.log(`${stats.lines} ${stats.words} ${stats.characters} ${path}`); | ||
| # } | ||
|
|
||
| # totalLines += stats.lines; | ||
| # totalWords += stats.words; | ||
| # totalCharacters += stats.characters; | ||
| # fileCount++; | ||
|
|
||
| # } else if (pathInfo.isDirectory()) { | ||
| # const files = await fs.readdir(path); | ||
| # for (const file of files) { | ||
| # const filePath = `${path}/${file}`; | ||
| # const fileContent = await fs.readFile(filePath, "utf-8"); | ||
| # const stats = counter(fileContent); | ||
|
|
||
| # if (options.line) { | ||
| # console.log(`${stats.lines} ${filePath}`); | ||
| # } else if (options.word) { | ||
| # console.log(`${stats.words} ${filePath}`); | ||
| # } else if (options.character) { | ||
| # console.log(`${stats.characters} ${filePath}`); | ||
| # } else { | ||
| # console.log(`${stats.lines} ${stats.words} ${stats.characters} ${filePath}`); | ||
| # } | ||
|
|
||
| # totalLines += stats.lines; | ||
| # totalWords += stats.words; | ||
| # totalCharacters += stats.characters; | ||
| # fileCount++; | ||
| # } | ||
| # } | ||
|
|
||
| # } | ||
|
|
||
| # if (fileCount > 1) { | ||
| # if (options.line) { | ||
| # console.log(`${totalLines} total`); | ||
| # } else if (options.word) { | ||
| # console.log(`${totalWords} total`); | ||
| # } else if (options.character) { | ||
| # console.log(`${totalCharacters} total`); | ||
| # } else { | ||
| # console.log(`${totalLines} ${totalWords} ${totalCharacters} total`); | ||
| # } | ||
| # } | ||
|
|
||
| import argparse | ||
| import os | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="counter", | ||
| description="Counts lines, words or characters in a file (or all files) inside a directory", | ||
| ) | ||
|
|
||
| parser.add_argument("-l", "--line", dest="line", help="The number of lines in each file", action="store_true") | ||
| parser.add_argument("-w", "--word", dest="word", help="The number of words in each file", action="store_true") | ||
| parser.add_argument("-c", "--char", dest="char", help="The number of characters in each file", action="store_true") | ||
| parser.add_argument("paths", help="The file(s)/path(s) to read from", nargs="+") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| def counter(item): | ||
| lines = len(item.strip().split("\n")) | ||
| words = len(item.split()) | ||
| characters = len(item) | ||
| return {"lines": lines, "words": words, "characters": characters} | ||
|
|
||
|
|
||
| total_lines = 0 | ||
| total_words = 0 | ||
| total_characters = 0 | ||
| file_count = 0 | ||
|
|
||
|
|
||
| for path in args.paths: | ||
| if os.path.isfile(path): | ||
| with open(path, "r") as f: | ||
| content = f.read() | ||
| stats = counter(content) | ||
| if args.line: | ||
| print(f"{stats['lines']} {path}") | ||
| elif args.word: | ||
| print(f"{stats['words']} {path}") | ||
| elif args.char: | ||
| print(f"{stats['characters']} {path}") | ||
| else: | ||
| print(f"{stats['lines']} {stats['words']} {stats['characters']} {path}") | ||
|
|
||
| total_lines += stats['lines'] | ||
| total_words += stats['words'] | ||
| total_characters += stats['characters'] | ||
| file_count += 1 | ||
|
|
||
| elif os.path.isdir(path): | ||
| for file in os.listdir(path): | ||
| file_path = os.path.join(path, file) | ||
|
|
||
| if os.path.isfile(file_path): | ||
| with open(file_path, "r") as f: | ||
| content = f.read() | ||
| stats = counter(content) | ||
| if args.line: | ||
| print(f"{stats['lines']} {file_path}") | ||
| elif args.word: | ||
| print(f"{stats['words']} {file_path}") | ||
| elif args.char: | ||
| print(f"{stats['characters']} {file_path}") | ||
| else: | ||
| print(f"{stats['lines']} {stats['words']} {stats['characters']} {file_path}") | ||
|
||
|
|
||
| total_lines += stats['lines'] | ||
| total_words += stats['words'] | ||
| total_characters += stats['characters'] | ||
| file_count += 1 | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| if file_count > 1: | ||
| if args.line: | ||
| print(f"{total_lines} total") | ||
| elif args.word: | ||
| print(f"{total_words} total") | ||
| elif args.char: | ||
| print(f"{total_characters} total") | ||
| else: | ||
| print(f"{total_lines} {total_words} {total_characters} total") | ||
|
|
||
|
|
||
|
|
||
|
|
||
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.
Arg is paths, comment is path. That will confuse people.
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.
Reworded the confusing bit. Hope it's good now.