feat(projects): 新增Click综合案例示例代码及素材

This commit is contained in:
100gle
2022-07-06 22:52:54 +08:00
parent e94d3af3be
commit 7e44811195
18 changed files with 965 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
import click
@click.command()
def greet():
pass
if __name__ == '__main__':
greet()

View File

@@ -0,0 +1,20 @@
import click
@click.group()
def pip():
...
@pip.command(name="foo")
def install():
click.echo("Installing...")
@pip.command(name="nonfoo")
def uninstall():
click.echo("Uninstalling...")
if __name__ == '__main__':
pip()

View File

@@ -0,0 +1,38 @@
import click
@click.group()
def pip():
pass
@pip.command()
def install():
"""pip install method"""
click.echo("Installing...")
@pip.command()
def uninstall():
"""pip uninstall method"""
click.echo("Uninstalling...")
@click.group()
def pytest():
pass
@pytest.command()
def test():
"""run the tests."""
click.echo("Running tests...")
cli = click.CommandCollection(
sources=[pip, pytest],
help="the collections of custom commands",
)
if __name__ == '__main__':
cli()

View File

@@ -0,0 +1,195 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Curry and Closure"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"initializing for request function.\n",
"<function init.<locals>.wrapper at 0x7fb82839f0d0>\n",
"Query for https://www.sspai.com using GET method.\n",
"response\n"
]
}
],
"source": [
"# Initialization without decorator\n",
"\n",
"def init(func):\n",
" print(f\"initializing for {func.__name__} function.\")\n",
" def wrapper(*args, **kwargs):\n",
" return func(*args, **kwargs)\n",
" return wrapper\n",
"\n",
"def request(url, method):\n",
" print(f\"Query for {url} using {method} method.\")\n",
" return \"response\"\n",
"\n",
"prepared_request = init(func=request)\n",
"print(prepared_request)\n",
"response = prepared_request(\"https://www.sspai.com\", method=\"GET\")\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"initializing for request function.\n",
"Query for https://www.sspai.com using GET method.\n",
"response\n"
]
}
],
"source": [
"\n",
"# Initialization wit decorator\n",
"\n",
"def init(func):\n",
" print(f\"initializing for {func.__name__} function.\")\n",
" def wrapper(*args, **kwargs):\n",
" return func(*args, **kwargs)\n",
" return wrapper\n",
"\n",
"@init\n",
"def request(url, method):\n",
" print(f\"Query for {url} using {method} method.\")\n",
" return \"response\"\n",
"\n",
"response = request(\"https://www.sspai.com\", method=\"GET\")\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"initializing for request function.\n",
"Use headers: {'User-Agent': 'Chrome/101.0.4951.64'}\n",
"Query for https://www.sspai.com using GET method.\n",
"None\n"
]
}
],
"source": [
"# Initialization with closure\n",
"\n",
"def init(func):\n",
" headers = {\"User-Agent\": \"Chrome/101.0.4951.64\"}\n",
" print(f\"initializing for {func.__name__} function.\")\n",
" def wrapper(*args, **kwargs):\n",
" result = func(headers=headers, *args, **kwargs)\n",
" return result\n",
" return wrapper\n",
"\n",
"@init\n",
"def request(url, method, **kwargs):\n",
" headers = kwargs.get(\"headers\", None)\n",
" if headers:\n",
" print(f\"Use headers: {headers}\")\n",
" print(f\"Query for {url} using {method} method.\")\n",
"\n",
"response = request(\"https://www.sspai.com\", method=\"GET\")\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# currying function\n",
"\n",
"def curry(func):\n",
"\n",
" f_args = []\n",
"\n",
" def wrapper(*args):\n",
" if args:\n",
" f_args.extend(args)\n",
" return wrapper\n",
"\n",
" result = func(f_args)\n",
" return result\n",
"\n",
" return wrapper\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the result is <function curry.<locals>.wrapper at 0x7fb820779b80> before evaling.\n",
"the result is 145 after evaling.\n"
]
}
],
"source": [
"total = curry(sum)\n",
"result = total(1)(2)(3, 4)(5, 6)(7, 8, 9)(10, 20, 30, 40)\n",
"print(f\"the result is {result} before evaling.\")\n",
"result = result()\n",
"print(f\"the result is {result} after evaling.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"interpreter": {
"hash": "13977d4cc82dee5f9d9535ceb495bd0ab12a43c33c664e5f0d53c24cf634b67f"
},
"kernelspec": {
"display_name": "Python 3.9.0 ('pandas-startup')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.0"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@@ -0,0 +1,69 @@
import click
def record_params(**kwargs):
tmpl = ""
for key, value in kwargs.items():
tmpl += f" {key}: {value}\n"
return tmpl
@click.command()
@click.argument("url", type=str)
@click.option(
"-X",
"--method",
default="GET",
type=click.Choice(["GET", "POST"], case_sensitive=False),
show_default=True,
help="the HTTP method to use",
)
@click.option(
"--data",
default=None,
multiple=True,
help="data to be sent with the request",
)
@click.option(
"-d",
"--header",
default=None,
multiple=True,
help="the request header",
)
@click.option(
"-v",
"--verbose",
is_flag=True,
help="verbose mode",
)
def mocked_curl(url, method, verbose=False, **kwargs):
"""request for target url"""
default_params = dict(method=method)
extra = {}
data = kwargs.get("data")
header = kwargs.get("header")
if header:
default_params.update(dict(header=header))
if method == "POST":
if data:
extra = dict(method="POST", data=data)
else:
extra = dict(method="POST")
if extra:
default_params.update(extra)
if verbose:
log = record_params(**default_params)
click.echo(f"request for {url} with: \n{log}")
return
click.echo(f"request for {url} ...")
if __name__ == '__main__':
mocked_curl()