feat(projects): 新增Click综合案例示例代码及素材
This commit is contained in:
25
projects/click-cli/basic/echo.py
Normal file
25
projects/click-cli/basic/echo.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import argparse
|
||||
|
||||
|
||||
def echo(name=None):
|
||||
print(f"Hello, {name}!")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="the argparse usage example",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-n",
|
||||
"--name",
|
||||
dest="name",
|
||||
help="the name pass to echo",
|
||||
default="World",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
echo(name=args.name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
45
projects/click-cli/basic/fake_pip.py
Normal file
45
projects/click-cli/basic/fake_pip.py
Normal file
@@ -0,0 +1,45 @@
|
||||
def fetch(target):
|
||||
print(f"fetch {target}...")
|
||||
|
||||
|
||||
def is_url(src):
|
||||
if not (src.startswith("http") and src.startswith("https")):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def download(url_or_package):
|
||||
if not is_url(url_or_package):
|
||||
fetch(url_or_package)
|
||||
return
|
||||
|
||||
|
||||
def _install(package=None, requirements=None):
|
||||
if not package and requirements:
|
||||
for _package in requirements:
|
||||
download(_package)
|
||||
elif package and not requirements:
|
||||
download(package)
|
||||
else:
|
||||
raise ValueError(
|
||||
"`package` parameter is required "
|
||||
"if there isn't `requirements` parameter."
|
||||
)
|
||||
|
||||
|
||||
class PipCommand:
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
def install(self, *args, **kwargs):
|
||||
_install(*args, **kwargs)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pip = PipCommand()
|
||||
package = "pandas"
|
||||
requirements = ["pandas", "numpy"]
|
||||
|
||||
# usage
|
||||
pip.install(package=package)
|
||||
pip.install(requirements=requirements)
|
||||
80
projects/click-cli/basic/fake_pip_cli.py
Normal file
80
projects/click-cli/basic/fake_pip_cli.py
Normal file
@@ -0,0 +1,80 @@
|
||||
import argparse
|
||||
from importlib.metadata import requires
|
||||
|
||||
|
||||
def fetch(target):
|
||||
print(f"fetch {target}...")
|
||||
|
||||
|
||||
def is_url(src):
|
||||
if not (src.startswith("http") and src.startswith("https")):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def download(url_or_package):
|
||||
if not is_url(url_or_package):
|
||||
fetch(url_or_package)
|
||||
return
|
||||
|
||||
|
||||
def _install(package=None, requirements=None):
|
||||
if not package and requirements:
|
||||
for _package in requirements:
|
||||
download(_package)
|
||||
elif package and not requirements:
|
||||
download(package)
|
||||
else:
|
||||
raise ValueError(
|
||||
"`package` parameter is required "
|
||||
"if there isn't `requirements` parameter."
|
||||
)
|
||||
|
||||
|
||||
class PipCommand:
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
def install(self, *args, **kwargs):
|
||||
_install(*args, **kwargs)
|
||||
|
||||
|
||||
def _tidy_requirements(requirements):
|
||||
filtered = filter(lambda r: not r.startswith("#") and r.strip() != "", requirements)
|
||||
keep = list(map(lambda r: r.strip(), filtered))
|
||||
return keep
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="the fake pip usage example",
|
||||
)
|
||||
subparsers = parser.add_subparsers(
|
||||
description="the sub-command for fake pip tools",
|
||||
)
|
||||
|
||||
parser_install = subparsers.add_parser("install")
|
||||
parser_install.add_argument(
|
||||
"package",
|
||||
type=str,
|
||||
nargs="?",
|
||||
help="the name of package",
|
||||
)
|
||||
parser_install.add_argument(
|
||||
"-r",
|
||||
"--requirements",
|
||||
dest="requirements",
|
||||
help="the requirements file",
|
||||
type=argparse.FileType("r", encoding="utf-8"),
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
deps = (
|
||||
_tidy_requirements(args.requirements.readlines()) if args.requirements else None
|
||||
)
|
||||
pip = PipCommand()
|
||||
pip.install(args.package, deps)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
54
projects/click-cli/basic/pipeline.py
Normal file
54
projects/click-cli/basic/pipeline.py
Normal file
@@ -0,0 +1,54 @@
|
||||
def pipeline(handler, **opts):
|
||||
def decorator(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
result = func(*args, **kwargs)
|
||||
return handler(result, **opts)
|
||||
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
def grep(content: str, pattern: str):
|
||||
import re
|
||||
|
||||
filtered = []
|
||||
content = content.splitlines()
|
||||
for line in content:
|
||||
if re.search(pattern, line):
|
||||
filtered.append(line)
|
||||
|
||||
return "\n".join(filtered)
|
||||
|
||||
|
||||
def tr(content: str, delete: bool, char: str):
|
||||
final = []
|
||||
|
||||
if delete:
|
||||
content = content.splitlines()
|
||||
for line in content:
|
||||
new_line = line.replace(char, "")
|
||||
final.append(new_line)
|
||||
if final:
|
||||
return "".join(final)
|
||||
|
||||
return content
|
||||
|
||||
|
||||
@pipeline(tr, delete=True, char="\n")
|
||||
@pipeline(grep, pattern="ed")
|
||||
def echo():
|
||||
poetry = """
|
||||
Beautiful is better than ugly.
|
||||
Explicit is better than implicit.
|
||||
Simple is better than complex.
|
||||
Complex is better than complicated.
|
||||
Flat is better than nested.
|
||||
Sparse is better than dense.
|
||||
"""
|
||||
return poetry.strip()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
result = echo()
|
||||
print(result)
|
||||
Reference in New Issue
Block a user