Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 220 additions & 0 deletions implement-shell-tools/.gitignore
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
48 changes: 48 additions & 0 deletions implement-shell-tools/cat/cat.py
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:
line_number = 1

for line in file:
line_number = process_line(line_number, line)
33 changes: 33 additions & 0 deletions implement-shell-tools/ls/ls.py
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)

if not args.show_hidden_files:
entries = [entry for entry in entries if not entry.startswith(".")]

if args.one_per_line:
for entry in entries:
print(entry)
else:
joined = "\t".join(entries)
print(joined)
Empty file.
67 changes: 67 additions & 0 deletions implement-shell-tools/wc/wc.py
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)
Loading