196 lines
4.7 KiB
Plaintext
196 lines
4.7 KiB
Plaintext
{
|
|
"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
|
|
}
|