-
-
Notifications
You must be signed in to change notification settings - Fork 92
London | 26-March-SDC | Ammad Ur Rehman | Sprint 4 | Implement shell tools (cat, ls, wc) in Python #540
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?
London | 26-March-SDC | Ammad Ur Rehman | Sprint 4 | Implement shell tools (cat, ls, wc) in Python #540
Changes from all commits
5ae0d16
7740eae
b3faa6b
4ee9b6f
655d823
6f5e3dd
9872c14
799576a
cf0e413
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,220 @@ | ||
| # Created by https://www.toptal.com/developers/gitignore/api/python,macos,osx | ||
| # Edit at https://www.toptal.com/developers/gitignore?templates=python,macos,osx | ||
|
|
||
| ### macOS ### | ||
| # General | ||
| .DS_Store | ||
| .AppleDouble | ||
| .LSOverride | ||
|
|
||
| # Icon must end with two \r | ||
| Icon | ||
|
|
||
|
|
||
| # Thumbnails | ||
| ._* | ||
|
|
||
| # Files that might appear in the root of a volume | ||
| .DocumentRevisions-V100 | ||
| .fseventsd | ||
| .Spotlight-V100 | ||
| .TemporaryItems | ||
| .Trashes | ||
| .VolumeIcon.icns | ||
| .com.apple.timemachine.donotpresent | ||
|
|
||
| # Directories potentially created on remote AFP share | ||
| .AppleDB | ||
| .AppleDesktop | ||
| Network Trash Folder | ||
| Temporary Items | ||
| .apdisk | ||
|
|
||
| ### macOS Patch ### | ||
| # iCloud generated files | ||
| *.icloud | ||
|
|
||
| ### OSX ### | ||
| # General | ||
|
|
||
| # Icon must end with two \r | ||
|
|
||
| # Thumbnails | ||
|
|
||
| # Files that might appear in the root of a volume | ||
|
|
||
| # Directories potentially created on remote AFP share | ||
|
|
||
| ### Python ### | ||
| # Byte-compiled / optimized / DLL files | ||
| __pycache__/ | ||
| *.py[cod] | ||
| *$py.class | ||
|
|
||
| # C extensions | ||
| *.so | ||
|
|
||
| # Distribution / packaging | ||
| .Python | ||
| build/ | ||
| develop-eggs/ | ||
| dist/ | ||
| downloads/ | ||
| eggs/ | ||
| .eggs/ | ||
| lib/ | ||
| lib64/ | ||
| parts/ | ||
| sdist/ | ||
| var/ | ||
| wheels/ | ||
| share/python-wheels/ | ||
| *.egg-info/ | ||
| .installed.cfg | ||
| *.egg | ||
| MANIFEST | ||
|
|
||
| # PyInstaller | ||
| # Usually these files are written by a python script from a template | ||
| # before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
| *.manifest | ||
| *.spec | ||
|
|
||
| # Installer logs | ||
| pip-log.txt | ||
| pip-delete-this-directory.txt | ||
|
|
||
| # Unit test / coverage reports | ||
| htmlcov/ | ||
| .tox/ | ||
| .nox/ | ||
| .coverage | ||
| .coverage.* | ||
| .cache | ||
| nosetests.xml | ||
| coverage.xml | ||
| *.cover | ||
| *.py,cover | ||
| .hypothesis/ | ||
| .pytest_cache/ | ||
| cover/ | ||
|
|
||
| # Translations | ||
| *.mo | ||
| *.pot | ||
|
|
||
| # Django stuff: | ||
| *.log | ||
| local_settings.py | ||
| db.sqlite3 | ||
| db.sqlite3-journal | ||
|
|
||
| # Flask stuff: | ||
| instance/ | ||
| .webassets-cache | ||
|
|
||
| # Scrapy stuff: | ||
| .scrapy | ||
|
|
||
| # Sphinx documentation | ||
| docs/_build/ | ||
|
|
||
| # PyBuilder | ||
| .pybuilder/ | ||
| target/ | ||
|
|
||
| # Jupyter Notebook | ||
| .ipynb_checkpoints | ||
|
|
||
| # IPython | ||
| profile_default/ | ||
| ipython_config.py | ||
|
|
||
| # pyenv | ||
| # For a library or package, you might want to ignore these files since the code is | ||
| # intended to run in multiple environments; otherwise, check them in: | ||
| # .python-version | ||
|
|
||
| # pipenv | ||
| # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
| # However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
| # having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
| # install all needed dependencies. | ||
| #Pipfile.lock | ||
|
|
||
| # poetry | ||
| # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. | ||
| # This is especially recommended for binary packages to ensure reproducibility, and is more | ||
| # commonly ignored for libraries. | ||
| # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control | ||
| #poetry.lock | ||
|
|
||
| # pdm | ||
| # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. | ||
| #pdm.lock | ||
| # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it | ||
| # in version control. | ||
| # https://pdm.fming.dev/#use-with-ide | ||
| .pdm.toml | ||
|
|
||
| # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm | ||
| __pypackages__/ | ||
|
|
||
| # Celery stuff | ||
| celerybeat-schedule | ||
| celerybeat.pid | ||
|
|
||
| # SageMath parsed files | ||
| *.sage.py | ||
|
|
||
| # Environments | ||
| .env | ||
| .venv | ||
| env/ | ||
| venv/ | ||
| ENV/ | ||
| env.bak/ | ||
| venv.bak/ | ||
|
|
||
| # Spyder project settings | ||
| .spyderproject | ||
| .spyproject | ||
|
|
||
| # Rope project settings | ||
| .ropeproject | ||
|
|
||
| # mkdocs documentation | ||
| /site | ||
|
|
||
| # mypy | ||
| .mypy_cache/ | ||
| .dmypy.json | ||
| dmypy.json | ||
|
|
||
| # Pyre type checker | ||
| .pyre/ | ||
|
|
||
| # pytype static type analyzer | ||
| .pytype/ | ||
|
|
||
| # Cython debug symbols | ||
| cython_debug/ | ||
|
|
||
| # PyCharm | ||
| # JetBrains specific template is maintained in a separate JetBrains.gitignore that can | ||
| # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore | ||
| # and can be added to the global gitignore or merged into this file. For a more nuclear | ||
| # option (not recommended) you can uncomment the following to ignore the entire idea folder. | ||
| #.idea/ | ||
|
|
||
| ### Python Patch ### | ||
| # Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration | ||
| poetry.toml | ||
|
|
||
| # ruff | ||
| .ruff_cache/ | ||
|
|
||
| # LSP config files | ||
| pyrightconfig.json | ||
|
|
||
| # End of https://www.toptal.com/developers/gitignore/api/python,macos,osx |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import argparse | ||
| import os | ||
|
|
||
| parser = argparse.ArgumentParser(prog="cat", | ||
| description="simple cat clone") | ||
|
|
||
| parser.add_argument("-n", | ||
| "--number", | ||
| dest="number_all", | ||
| action="store_true", | ||
| help="number all output lines") | ||
|
|
||
| parser.add_argument("-b", | ||
| "--number-nonblank", | ||
| dest="number_nonblank", | ||
| action="store_true", | ||
| help="number non-empty output lines") | ||
|
|
||
| parser.add_argument("paths", nargs="+", help="file(s) to process") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| def process_line(line_number, line): | ||
| if args.number_nonblank: | ||
| if line == "\n": | ||
| print(line, end="") | ||
| return line_number | ||
|
|
||
| print(f"\t{line_number} {line}", end="") | ||
| return line_number + 1 | ||
|
|
||
| if args.number_all: | ||
| print(f"\t{line_number} {line}", end="") | ||
| return line_number + 1 | ||
|
|
||
| print(line, end="") | ||
| return line_number | ||
|
|
||
| for path in args.paths: | ||
| if os.path.isdir(path): | ||
| print(f"cat: {path}: Is a directory") | ||
| continue | ||
|
|
||
| with open(path, "r") as file: | ||
|
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 happens if the file is absent? Compare it with real cat behavior |
||
| line_number = 1 | ||
|
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. README requires cat -n sample-files/*.txt to behave like real cat -n, which numbers lines continuously across all files |
||
|
|
||
| for line in file: | ||
| line_number = process_line(line_number, line) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import argparse | ||
| import os | ||
|
|
||
| parser = argparse.ArgumentParser(prog="ls", | ||
| description="simple ls clone") | ||
|
|
||
| parser.add_argument("-1", | ||
| dest="one_per_line", | ||
| action="store_true", | ||
| help="list one file per line") | ||
|
|
||
| parser.add_argument("-a", | ||
| dest="show_hidden_files", | ||
| action="store_true", | ||
| help="show hidden files") | ||
|
|
||
| parser.add_argument("filepath", | ||
| nargs="?", | ||
| default=".") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| entries = os.listdir(args.filepath) | ||
|
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. Does os.listdir return dir list in an alphabetical order? Compare with what real ls does |
||
|
|
||
| if not args.show_hidden_files: | ||
| entries = [entry for entry in entries if not entry.startswith(".")] | ||
|
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. -a filters out names starting with ., but real ls -a also includes . and .. as the first two entries. |
||
|
|
||
| if args.one_per_line: | ||
| for entry in entries: | ||
| print(entry) | ||
| else: | ||
| joined = "\t".join(entries) | ||
| print(joined) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import argparse | ||
| import os | ||
|
|
||
| parser = argparse.ArgumentParser(prog="wc", | ||
| description="simple wc clone") | ||
|
|
||
| parser.add_argument("-w", | ||
| "--words", | ||
| dest="show_words", | ||
| action="store_true", | ||
| help="print word count") | ||
|
|
||
| parser.add_argument("-l", | ||
| "--lines", | ||
| dest="show_lines", | ||
| action="store_true", | ||
| help="print line count") | ||
|
|
||
| parser.add_argument("-c", | ||
| "--bytes", | ||
| dest="show_bytes", | ||
| action="store_true", | ||
| help="print byte count") | ||
|
|
||
| parser.add_argument("paths", nargs="+") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| if not args.show_lines and not args.show_words and not args.show_bytes: | ||
| args.show_lines = True | ||
| args.show_words = True | ||
| args.show_bytes = True | ||
|
|
||
| def format_count(content, path): | ||
| outputs = [] | ||
|
|
||
| word_count = str(len(content.split())) | ||
| line_count = str(content.count(b"\n")) | ||
| byte_count = str(len(content)) | ||
|
|
||
| if args.show_lines: | ||
| outputs.append(line_count) | ||
|
|
||
| if args.show_words: | ||
| outputs.append(word_count) | ||
|
|
||
| if args.show_bytes: | ||
| outputs.append(byte_count) | ||
|
|
||
| outputs.append(path) | ||
|
|
||
| return "\t".join(outputs) | ||
|
|
||
| outputs = [] | ||
|
|
||
| for path in args.paths: | ||
| if os.path.isdir(path): | ||
| print(f"wc: {path}: Is a directory") | ||
| continue | ||
|
|
||
| with open(path, "rb") as file: | ||
| content = file.read() | ||
| output = format_count(content, path) | ||
| outputs.append("\t" + output) | ||
|
|
||
| result = "\n".join(outputs) | ||
| print(result) |
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.
Output format for numbered lines doesn't match real cat. Real cat -n prints the number right-justified in a
6-wide field, then a tab, then the line